Now possible to have space and flexible space in toolbar (by creating items
[MacVim/jjgod.git] / gui_macvim.m
blob598755f6a22adfd3675517dbf4c3890cc9d33eb5
1 /* vi:set ts=8 sts=4 sw=4 ft=objc:
2  *
3  * VIM - Vi IMproved            by Bram Moolenaar
4  *                              MacVim GUI port by Bjorn Winckler
5  *
6  * Do ":help uganda"  in Vim to read copying and usage conditions.
7  * Do ":help credits" in Vim to see a list of people who contributed.
8  * See README.txt for an overview of the Vim source code.
9  */
11 #import <Foundation/Foundation.h>
12 #import "MMBackend.h"
13 #import "MacVim.h"
14 #import "vim.h"
17 static BOOL gui_cocoa_is_valid_action(NSString *action);
20 // -- Initialization --------------------------------------------------------
23  * Parse the GUI related command-line arguments.  Any arguments used are
24  * deleted from argv, and *argc is decremented accordingly.  This is called
25  * when vim is started, whether or not the GUI has been started.
26  */
27     void
28 gui_mch_prepare(int *argc, char **argv)
30     //NSLog(@"gui_mch_prepare(argc=%d)", *argc);
32     // Set environment variables $VIM and $VIMRUNTIME
33     // NOTE!  If vim_getenv is called with one of these as parameters before
34     // they have been set here, they will most likely end up with the wrong
35     // values!
36     //
37     // TODO:
38     // - ensure this is called first to avoid above problem
39     // - encoding
41     NSString *path = [[[NSBundle mainBundle] resourcePath]
42         stringByAppendingPathComponent:@"vim"];
43     vim_setenv((char_u*)"VIM", (char_u*)[path UTF8String]);
45     path = [path stringByAppendingPathComponent:@"runtime"];
46     vim_setenv((char_u*)"VIMRUNTIME", (char_u*)[path UTF8String]);
51  * Check if the GUI can be started.  Called before gvimrc is sourced.
52  * Return OK or FAIL.
53  */
54     int
55 gui_mch_init_check(void)
57     //NSLog(@"gui_mch_init_check()");
58     return OK;
63  * Initialise the GUI.  Create all the windows, set up all the call-backs etc.
64  * Returns OK for success, FAIL when the GUI can't be started.
65  */
66     int
67 gui_mch_init(void)
69     //NSLog(@"gui_mch_init()");
71     if (![[MMBackend sharedInstance] checkin])
72         return FAIL;
74     // HACK!  Force the 'termencoding to utf-8.  For the moment also force
75     // 'encoding', although this will change in the future.  The user can still
76     // change 'encoding'; doing so WILL crash the program.
77     set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
78     set_option_value((char_u *)"encoding", 0L, (char_u *)"utf-8", 0);
80     // Set values so that pixels and characters are in one-to-one
81     // correspondence (assuming all characters have the same dimensions).
82     gui.scrollbar_width = gui.scrollbar_height = 0;
84     gui.char_height = 1;
85     gui.char_width = 1;
86     gui.char_ascent = 0;
88     gui_mch_def_colors();
90     [[MMBackend sharedInstance]
91         setDefaultColorsBackground:gui.back_pixel foreground:gui.norm_pixel];
92     [[MMBackend sharedInstance] setBackgroundColor:gui.back_pixel];
93     [[MMBackend sharedInstance] setForegroundColor:gui.norm_pixel];
95     // NOTE: If this call is left out the cursor is opaque.
96     highlight_gui_started();
98     return OK;
103     void
104 gui_mch_exit(int rc)
106     //NSLog(@"gui_mch_exit(rc=%d)", rc);
108     [[MMBackend sharedInstance] exit];
113  * Open the GUI window which was created by a call to gui_mch_init().
114  */
115     int
116 gui_mch_open(void)
118     //NSLog(@"gui_mch_open()");
120     return [[MMBackend sharedInstance] openVimWindow];
124 // -- Updating --------------------------------------------------------------
128  * Catch up with any queued X events.  This may put keyboard input into the
129  * input buffer, call resize call-backs, trigger timers etc.  If there is
130  * nothing in the X event queue (& no timers pending), then we return
131  * immediately.
132  */
133     void
134 gui_mch_update(void)
136     // HACK!  Nothing to do here since we tend to the run loop (which holds
137     // incoming events) in gui_mch_wait_for_chars().
141 /* Flush any output to the screen */
142     void
143 gui_mch_flush(void)
145     // HACK!  This function is called so often that draw performance suffers.
146     // Instead of actually flushing the output it is placed on a queue and
147     // flushed in gui_mch_wait_for_chars(), which makes the program feel much
148     // more responsive.  This might have unintended side effects though;  if
149     // so, another solution might have to be found.
151     //[[MMBackend sharedInstance] flush];
156  * GUI input routine called by gui_wait_for_chars().  Waits for a character
157  * from the keyboard.
158  *  wtime == -1     Wait forever.
159  *  wtime == 0      This should never happen.
160  *  wtime > 0       Wait wtime milliseconds for a character.
161  * Returns OK if a character was found to be available within the given time,
162  * or FAIL otherwise.
163  */
164     int
165 gui_mch_wait_for_chars(int wtime)
167     // HACK!  See comment in gui_mch_flush().
168     [[MMBackend sharedInstance] flushQueue];
170     return [[MMBackend sharedInstance] waitForInput:wtime];
174 // -- Drawing ---------------------------------------------------------------
178  * Clear the whole text window.
179  */
180     void
181 gui_mch_clear_all(void)
183     [[MMBackend sharedInstance] clearAll];
188  * Clear a rectangular region of the screen from text pos (row1, col1) to
189  * (row2, col2) inclusive.
190  */
191     void
192 gui_mch_clear_block(int row1, int col1, int row2, int col2)
194     [[MMBackend sharedInstance] clearBlockFromRow:row1 column:col1
195                                                     toRow:row2 column:col2];
200  * Delete the given number of lines from the given row, scrolling up any
201  * text further down within the scroll region.
202  */
203     void
204 gui_mch_delete_lines(int row, int num_lines)
206     [[MMBackend sharedInstance] deleteLinesFromRow:row count:num_lines
207             scrollBottom:gui.scroll_region_bot
208                     left:gui.scroll_region_left
209                    right:gui.scroll_region_right];
213     void
214 gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
216     [[MMBackend sharedInstance] replaceString:(char*)s length:len
217             row:row column:col flags:flags];
221     int
222 gui_macvim_draw_string(int row, int col, char_u *s, int len, int flags)
224 #if 0
225     NSString *string = [[NSString alloc]
226             initWithBytesNoCopy:(void*)s
227                          length:len
228                        encoding:NSUTF8StringEncoding
229                    freeWhenDone:NO];
230     int cells = [string length];
231     [string release];
233     NSLog(@"gui_macvim_draw_string(row=%d, col=%d, len=%d, cells=%d, flags=%d)",
234             row, col, len, cells, flags);
236     [[MMBackend sharedInstance] replaceString:(char*)s length:len
237             row:row column:col flags:flags];
239     return cells;
240 #elif 0
241     int c;
242     int cn;
243     int cl;
244     int i;
245     BOOL wide = NO;
246     int start = 0;
247     int endcol = col;
248     int startcol = col;
249     MMBackend *backend = [MMBackend sharedInstance];
251     for (i = 0; i < len; i += cl) {
252         c = utf_ptr2char(s + i);
253         cl = utf_ptr2len(s + i);
254         cn = utf_char2cells(c);
255         comping = utf_iscomposing(c);
257         if (!comping)
258             endcol += cn;
260         if (cn > 1 && !wide) {
261             // Start of wide characters.
262             wide = YES;
264             // Output non-wide characters.
265             if (start > i) {
266                 NSLog(@"Outputting %d non-wide chars (%d bytes)",
267                         endcol-startcol, start-i);
268                 [backend replaceString:(char*)(s+start) length:start-i
269                         row:row column:startcol flags:flags];
270                 startcol = endcol;
271                 start = i;
272             }
273         } else if (cn <= 1 && !comping && wide) {
274             // End of wide characters.
275             wide = NO;
277             // Output wide characters.
278             if (start > i) {
279                 NSLog(@"Outputting %d wide chars (%d bytes)",
280                         endcol-startcol, start-i);
281                 [backend replaceString:(char*)(s+start) length:start-i
282                         row:row column:startcol flags:(flags|0x80)];
283                 startcol = endcol;
284                 start = i;
285             }
286         }
287     }
289     // Output remaining characters.
290     flags = wide ? flags|0x80 : flags;
291     NSLog(@"Outputting %d %s chars (%d bytes)", endcol-startcol, wide ? "wide"
292             : "non-wide", len-start);
293     [backend replaceString:(char*)(s+start) length:len-start
294             row:row column:startcol flags:flags];
296     return endcol - col;
297 #elif 1
298     //
299     // Output chars until a wide char found.  If a wide char is found, output a
300     // zero-width space after it so that a wide char looks like two chars to
301     // MMTextStorage.  This way 1 char corresponds to 1 column.
302     //
304     int c;
305     int cn;
306     int cl;
307     int i;
308     int start = 0;
309     int endcol = col;
310     int startcol = col;
311     BOOL outPad = NO;
312     MMBackend *backend = [MMBackend sharedInstance];
313     static char ZeroWidthSpace[] = { 0xe2, 0x80, 0x8b };
315     for (i = 0; i < len; i += cl) {
316         c = utf_ptr2char(s + i);
317         cl = utf_ptr2len(s + i);
318         cn = utf_char2cells(c);
320         if (!utf_iscomposing(c)) {
321             if (outPad) {
322                 outPad = NO;
323 #if 0
324                 NSString *string = [[NSString alloc]
325                         initWithBytesNoCopy:(void*)(s+start)
326                                      length:i-start
327                                    encoding:NSUTF8StringEncoding
328                                freeWhenDone:NO];
329                 NSLog(@"Flushing string=%@ len=%d row=%d col=%d end=%d",
330                         string, i-start, row, startcol, endcol);
331                 [string release];
332 #endif
333                 [backend replaceString:(char*)(s+start) length:i-start
334                         row:row column:startcol flags:flags];
335                 start = i;
336                 startcol = endcol;
337 #if 0
338                 NSLog(@"Padding len=%d row=%d col=%d", sizeof(ZeroWidthSpace),
339                         row, endcol-1);
340 #endif
341                 [backend replaceString:ZeroWidthSpace
342                              length:sizeof(ZeroWidthSpace)
343                         row:row column:endcol-1 flags:flags];
344             }
346             endcol += cn;
347         }
349         if (cn > 1) {
350 #if 0
351             NSLog(@"Wide char detected! (char=%C hex=%x cells=%d)", c, c, cn);
352 #endif
353             outPad = YES;
354         }
355     }
357 #if 0
358     if (row < 1) {
359         NSString *string = [[NSString alloc]
360                 initWithBytesNoCopy:(void*)(s+start)
361                              length:len-start
362                            encoding:NSUTF8StringEncoding
363                        freeWhenDone:NO];
364         NSLog(@"Output string=%@ len=%d row=%d col=%d", string, len-start, row,
365                 startcol);
366         [string release];
367     }
368 #endif
370     // Output remaining characters.
371     [backend replaceString:(char*)(s+start) length:len-start
372             row:row column:startcol flags:flags];
374     if (outPad) {
375 #if 0
376         NSLog(@"Padding len=%d row=%d col=%d", sizeof(ZeroWidthSpace), row,
377                 endcol-1);
378 #endif
379         [backend replaceString:ZeroWidthSpace
380                      length:sizeof(ZeroWidthSpace)
381                 row:row column:endcol-1 flags:flags];
382     }
384     return endcol - col;
385 #else
386     // This will fail abysmally when wide or composing characters are used.
387     [[MMBackend sharedInstance]
388             replaceString:(char*)s length:len row:row column:col flags:flags];
390     int i, c, cl, cn, cells = 0;
391     for (i = 0; i < len; i += cl) {
392         c = utf_ptr2char(s + i);
393         cl = utf_ptr2len(s + i);
394         cn = utf_char2cells(c);
396         if (!utf_iscomposing(c))
397             cells += cn;
398     }
400     return cells;
401 #endif
406  * Insert the given number of lines before the given row, scrolling down any
407  * following text within the scroll region.
408  */
409     void
410 gui_mch_insert_lines(int row, int num_lines)
412     [[MMBackend sharedInstance] insertLinesFromRow:row count:num_lines
413             scrollBottom:gui.scroll_region_bot
414                     left:gui.scroll_region_left
415                    right:gui.scroll_region_right];
419 // -- Tab line --------------------------------------------------------------
423  * Set the current tab to "nr".  First tab is 1.
424  */
425     void
426 gui_mch_set_curtab(int nr)
428     //NSLog(@"gui_mch_set_curtab(nr=%d)", nr);
429     [[MMBackend sharedInstance] selectTab:nr];
434  * Return TRUE when tabline is displayed.
435  */
436     int
437 gui_mch_showing_tabline(void)
439     //NSLog(@"gui_mch_showing_tabline()");
440     return [[MMBackend sharedInstance] tabBarVisible];
444  * Update the labels of the tabline.
445  */
446     void
447 gui_mch_update_tabline(void)
449     //NSLog(@"gui_mch_update_tabline()");
450     [[MMBackend sharedInstance] updateTabBar];
454  * Show or hide the tabline.
455  */
456     void
457 gui_mch_show_tabline(int showit)
459     //NSLog(@"gui_mch_show_tabline(showit=%d)", showit);
460     [[MMBackend sharedInstance] showTabBar:showit];
464 // -- Clipboard -------------------------------------------------------------
467     void
468 clip_mch_lose_selection(VimClipboard *cbd)
473     int
474 clip_mch_own_selection(VimClipboard *cbd)
476     return 0;
480     void
481 clip_mch_request_selection(VimClipboard *cbd)
483     NSPasteboard *pb = [NSPasteboard generalPasteboard];
484     NSString *type = [pb availableTypeFromArray:
485             [NSArray arrayWithObject:NSStringPboardType]];
486     if (type) {
487         NSMutableString *string =
488                 [[pb stringForType:NSStringPboardType] mutableCopy];
490         // Replace unrecognized end-of-line sequences with \x0a (line feed).
491         NSRange range = { 0, [string length] };
492         unsigned n = [string replaceOccurrencesOfString:@"\x0d\x0a"
493                                              withString:@"\x0a" options:0
494                                                   range:range];
495         if (0 == n) {
496             n = [string replaceOccurrencesOfString:@"\x0d" withString:@"\x0a"
497                                            options:0 range:range];
498         }
499         
500         // Scan for newline character to decide whether the string should be
501         // pasted linewise or characterwise.
502         int type = MCHAR;
503         if (0 < n || NSNotFound != [string rangeOfString:@"\n"].location)
504             type = MLINE;
505         
506         const char *utf8chars = [string UTF8String];
507         clip_yank_selection(type, (char_u*)utf8chars, strlen(utf8chars), cbd);
508     }
513  * Send the current selection to the clipboard.
514  */
515     void
516 clip_mch_set_selection(VimClipboard *cbd)
518     // If the '*' register isn't already filled in, fill it in now.
519     cbd->owned = TRUE;
520     clip_get_selection(cbd);
521     cbd->owned = FALSE;
522     
523     // Get the text to put on the pasteboard.
524     long_u len = 0; char_u *str = 0;
525     int type = clip_convert_selection(&str, &len, cbd);
526     if (type < 0)
527         return;
528     
529     NSString *string = [[NSString alloc] initWithBytes:str length:len
530                                               encoding:NSUTF8StringEncoding];
531     
532     NSPasteboard *pb = [NSPasteboard generalPasteboard];
533     [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
534     [pb setString:string forType:NSStringPboardType];
535     
536     [string release];
537     vim_free(str);
541 // -- Menu ------------------------------------------------------------------
545  * Add a sub menu to the menu bar.
546  */
547     void
548 gui_mch_add_menu(vimmenu_T *menu, int idx)
550     //NSLog(@"gui_mch_add_menu(name=%s, idx=%d)", menu->name, idx);
552     // HACK!  If menu has no parent, then we set the parent tag to the type of
553     // menu it is.  This will not mix up tag and type because pointers can not
554     // take values close to zero (and the tag is simply the value of the
555     // pointer).
556     int parent = (int)menu->parent;
557     if (!parent) {
558         parent = menu_is_popup(menu->name) ? MenuPopupType :
559                  menu_is_toolbar(menu->name) ? MenuToolbarType :
560                  MenuMenubarType;
561     }
563     [[MMBackend sharedInstance]
564             addMenuWithTag:(int)menu parent:parent name:(char*)menu->dname
565                    atIndex:idx];
570  * Add a menu item to a menu
571  */
572     void
573 gui_mch_add_menu_item(vimmenu_T *menu, int idx)
575     //NSLog(@"gui_mch_add_menu_item(name=%s, accel=%s idx=%d)", menu->dname,
576     //        menu->actext, idx);
578     // NOTE!  If 'iconfile' is not set but 'iconidx' is, use the name of the
579     // menu item.  (Should correspond to a stock item.)
580     char *icon = menu->iconfile ? (char*)menu->iconfile :
581                  menu->iconidx >= 0 ? (char*)menu->dname :
582                  NULL;
583     //char *name = menu_is_separator(menu->name) ? NULL : (char*)menu->dname;
584     char *name = (char*)menu->dname;
585     char *tip = menu->strings[MENU_INDEX_TIP]
586             ? (char*)menu->strings[MENU_INDEX_TIP] : (char*)menu->actext;
588     // HACK!  Check if menu is mapped to ':action actionName:'; if so, pass the
589     // action along so that MacVim can bind the menu item to this action.  This
590     // means that if a menu item maps to an action in normal mode, then all
591     // other modes will also use the same action.
592     NSString *action = nil;
593     char_u *map_str = menu->strings[MENU_INDEX_NORMAL];
594     if (map_str) {
595         NSString *mapping = [NSString stringWithCString:(char*)map_str
596                                                encoding:NSUTF8StringEncoding];
597         NSArray *parts = [mapping componentsSeparatedByString:@" "];
598         if ([parts count] >=2 
599                 && [[parts objectAtIndex:0] isEqual:@":action"]) {
600             action = [parts objectAtIndex:1];
601             action = [action stringByTrimmingCharactersInSet:
602                     [NSCharacterSet whitespaceAndNewlineCharacterSet]];
603             if (!gui_cocoa_is_valid_action(action))
604                 action = nil;
605         }
606     }
608     [[MMBackend sharedInstance]
609             addMenuItemWithTag:(int)menu
610                         parent:(int)menu->parent
611                           name:name
612                            tip:tip
613                           icon:(char*)icon
614                  keyEquivalent:menu->ke_key
615                      modifiers:menu->ke_mods
616                         action:action
617                        atIndex:idx];
622  * Destroy the machine specific menu widget.
623  */
624     void
625 gui_mch_destroy_menu(vimmenu_T *menu)
627     //NSLog(@"gui_mch_destroy_menu(name=%s)", menu->name);
629     [[MMBackend sharedInstance] removeMenuItemWithTag:(int)menu];
634  * Make a menu either grey or not grey.
635  */
636     void
637 gui_mch_menu_grey(vimmenu_T *menu, int grey)
639     //NSLog(@"gui_mch_menu_grey(name=%s, grey=%d)", menu->name, grey);
640     [[MMBackend sharedInstance]
641             enableMenuItemWithTag:(int)menu state:!grey];
646  * Make menu item hidden or not hidden
647  */
648     void
649 gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
651     //NSLog(@"gui_mch_menu_hidden(name=%s, hidden=%d)", menu->name, hidden);
653     // HACK! There is no (obvious) way to hide a menu item, so simply
654     // enable/disable it instead.
655     [[MMBackend sharedInstance]
656             enableMenuItemWithTag:(int)menu state:!hidden];
661  * This is called when user right clicks.
662  */
663     void
664 gui_mch_show_popupmenu(vimmenu_T *menu)
666     //NSLog(@"gui_mch_show_popupmenu(name=%s)", menu->name);
668     [[MMBackend sharedInstance] showPopupMenuWithName:(char*)menu->name
669                                       atMouseLocation:YES];
674  * This is called when a :popup command is executed.
675  */
676     void
677 gui_make_popup(char_u *path_name, int mouse_pos)
679     // TODO: Unless mouse_pos set, popup at cursor location.
680     [[MMBackend sharedInstance] showPopupMenuWithName:(char*)path_name
681                                       atMouseLocation:mouse_pos];
686  * This is called after setting all the menus to grey/hidden or not.
687  */
688     void
689 gui_mch_draw_menubar(void)
691     // The (main) menu draws itself in Mac OS X.
695     void
696 gui_mch_enable_menu(int flag)
698     // The (main) menu is always enabled in Mac OS X.
702 #if 0
703     void
704 gui_mch_set_menu_pos(int x, int y, int w, int h)
706     // The (main) menu cannot be moved in Mac OS X.
708 #endif
711     void
712 gui_mch_show_toolbar(int showit)
714     int flags = 0;
715     if (toolbar_flags & TOOLBAR_TEXT) flags |= ToolbarLabelFlag;
716     if (toolbar_flags & TOOLBAR_ICONS) flags |= ToolbarIconFlag;
717     if (tbis_flags & (TBIS_MEDIUM|TBIS_LARGE)) flags |= ToolbarSizeRegularFlag;
719     //NSLog(@"gui_mch_show_toolbar(showit=%d, flags=%d)", showit, flags);
721     [[MMBackend sharedInstance] showToolbar:showit flags:flags];
727 // -- Fonts -----------------------------------------------------------------
731  * If a font is not going to be used, free its structure.
732  */
733     void
734 gui_mch_free_font(font)
735     GuiFont     font;
741  * Get a font structure for highlighting.
742  */
743     GuiFont
744 gui_mch_get_font(char_u *name, int giveErrorIfMissing)
746     //NSLog(@"gui_mch_get_font(name=%s, giveErrorIfMissing=%d)", name,
747     //        giveErrorIfMissing);
748     return 0;
752 #if defined(FEAT_EVAL) || defined(PROTO)
754  * Return the name of font "font" in allocated memory.
755  * Don't know how to get the actual name, thus use the provided name.
756  */
757     char_u *
758 gui_mch_get_fontname(GuiFont font, char_u *name)
760     //NSLog(@"gui_mch_get_fontname(font=%d, name=%s)", font, name);
761     return 0;
763 #endif
767  * Initialise vim to use the font with the given name.  Return FAIL if the font
768  * could not be loaded, OK otherwise.
769  */
770     int
771 gui_mch_init_font(char_u *font_name, int fontset)
773     //NSLog(@"gui_mch_init_font(font_name=%s, fontset=%d)", font_name, fontset);
775     // HACK!  This gets called whenever the user types :set gfn=fontname, so
776     // for now we set the font here.
777     // TODO!  Proper font handling, the way Vim expects it.
778     return [[MMBackend sharedInstance]
779             setFontWithName:(char*)font_name];
784  * Set the current text font.
785  */
786     void
787 gui_mch_set_font(GuiFont font)
794 // -- Scrollbars ------------------------------------------------------------
797     void
798 gui_mch_create_scrollbar(
799         scrollbar_T *sb,
800         int orient)     /* SBAR_VERT or SBAR_HORIZ */
802     //NSLog(@"gui_mch_create_scrollbar(id=%d, orient=%d, type=%d)",
803     //        sb->ident, orient, sb->type);
805     [[MMBackend sharedInstance] 
806             createScrollbarWithIdentifier:sb->ident type:sb->type];
810     void
811 gui_mch_destroy_scrollbar(scrollbar_T *sb)
813     //NSLog(@"gui_mch_destroy_scrollbar(id=%d)", sb->ident);
815     [[MMBackend sharedInstance] 
816             destroyScrollbarWithIdentifier:sb->ident];
820     void
821 gui_mch_enable_scrollbar(
822         scrollbar_T     *sb,
823         int             flag)
825     //NSLog(@"gui_mch_enable_scrollbar(id=%d, flag=%d)", sb->ident, flag);
827     [[MMBackend sharedInstance] 
828             showScrollbarWithIdentifier:sb->ident state:flag];
832     void
833 gui_mch_set_scrollbar_pos(
834         scrollbar_T *sb,
835         int x,
836         int y,
837         int w,
838         int h)
840     //NSLog(@"gui_mch_set_scrollbar_pos(id=%d, x=%d, y=%d, w=%d, h=%d)",
841     //        sb->ident, x, y, w, h);
843     int pos = y;
844     int len = h;
845     if (SBAR_BOTTOM == sb->type) {
846         pos = x;
847         len = w; 
848     }
850     [[MMBackend sharedInstance] 
851             setScrollbarPosition:pos length:len identifier:sb->ident];
855     void
856 gui_mch_set_scrollbar_thumb(
857         scrollbar_T *sb,
858         long val,
859         long size,
860         long max)
862     //NSLog(@"gui_mch_set_scrollbar_thumb(id=%d, val=%d, size=%d, max=%d)",
863     //        sb->ident, val, size, max);
865     [[MMBackend sharedInstance] 
866             setScrollbarThumbValue:val size:size max:max identifier:sb->ident];
873 // -- Unsorted --------------------------------------------------------------
876     void
877 ex_action(eap)
878     exarg_T     *eap;
880     if (!gui.in_use) {
881         EMSG(_("E???: Command only available in GUI mode"));
882         return;
883     }
885     NSString *name = [NSString stringWithCString:(char*)eap->arg
886                                         encoding:NSUTF8StringEncoding];
887     if (gui_cocoa_is_valid_action(name)) {
888         [[MMBackend sharedInstance] executeActionWithName:name];
889     } else {
890         EMSG2(_("E???: \"%s\" is not a valid action"), eap->arg);
891     }
896  * Adjust gui.char_height (after 'linespace' was changed).
897  */
898     int
899 gui_mch_adjust_charheight(void)
901     return 0;
905     void
906 gui_mch_beep(void)
912 #ifdef FEAT_BROWSE
914  * Pop open a file browser and return the file selected, in allocated memory,
915  * or NULL if Cancel is hit.
916  *  saving  - TRUE if the file will be saved to, FALSE if it will be opened.
917  *  title   - Title message for the file browser dialog.
918  *  dflt    - Default name of file.
919  *  ext     - Default extension to be added to files without extensions.
920  *  initdir - directory in which to open the browser (NULL = current dir)
921  *  filter  - Filter for matched files to choose from.
922  *  Has a format like this:
923  *  "C Files (*.c)\0*.c\0"
924  *  "All Files\0*.*\0\0"
925  *  If these two strings were concatenated, then a choice of two file
926  *  filters will be selectable to the user.  Then only matching files will
927  *  be shown in the browser.  If NULL, the default allows all files.
929  *  *NOTE* - the filter string must be terminated with TWO nulls.
930  */
931     char_u *
932 gui_mch_browse(
933     int saving,
934     char_u *title,
935     char_u *dflt,
936     char_u *ext,
937     char_u *initdir,
938     char_u *filter)
940     //NSLog(@"gui_mch_browse(saving=%d, title=%s, dflt=%s, ext=%s, initdir=%s,"
941     //        " filter=%s", saving, title, dflt, ext, initdir, filter);
943     char_u *s = (char_u*)[[MMBackend sharedInstance]
944             browseForFileInDirectory:(char*)initdir title:(char*)title
945                               saving:saving];
947     return s;
949 #endif /* FEAT_BROWSE */
953     int
954 gui_mch_dialog(
955     int         type,
956     char_u      *title,
957     char_u      *message,
958     char_u      *buttons,
959     int         dfltbutton,
960     char_u      *textfield)
962     return 0;
967  * Draw a cursor without focus.
968  */
969     void
970 gui_mch_draw_hollow_cursor(guicolor_T color)
976  * Draw part of a cursor, only w pixels wide, and h pixels high.
977  */
978     void
979 gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
984     void
985 gui_mch_flash(int msec)
991  * Return the Pixel value (color) for the given color name.  This routine was
992  * pretty much taken from example code in the Silicon Graphics OSF/Motif
993  * Programmer's Guide.
994  * Return INVALCOLOR when failed.
995  */
996     guicolor_T
997 gui_mch_get_color(char_u *name)
999     NSString *key = [NSString stringWithUTF8String:(char*)name];
1000     return [[MMBackend sharedInstance] lookupColorWithKey:key];
1005  * Return the RGB value of a pixel as long.
1006  */
1007     long_u
1008 gui_mch_get_rgb(guicolor_T pixel)
1010     // This is only implemented so that vim can guess the correct value for
1011     // 'background' (which otherwise defaults to 'dark'); it is not used for
1012     // anything else (as far as I know).
1013     // The implementation is simple since colors are stored in an int as
1014     // "rrggbb".
1015     return pixel;
1020  * Get the screen dimensions.
1021  * Allow 10 pixels for horizontal borders, 40 for vertical borders.
1022  * Is there no way to find out how wide the borders really are?
1023  * TODO: Add live udate of those value on suspend/resume.
1024  */
1025     void
1026 gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
1028     //NSLog(@"gui_mch_get_screen_dimensions()");
1029     *screen_w = Columns;
1030     *screen_h = Rows;
1035  * Get the position of the top left corner of the window.
1036  */
1037     int
1038 gui_mch_get_winpos(int *x, int *y)
1040     *x = *y = 0;
1041     return OK;
1046  * Get current mouse coordinates in text window.
1047  */
1048     void
1049 gui_mch_getmouse(int *x, int *y)
1055  * Return OK if the key with the termcap name "name" is supported.
1056  */
1057     int
1058 gui_mch_haskey(char_u *name)
1060     NSLog(@"gui_mch_haskey(name=%s)", name);
1061     return 0;
1066  * Iconify the GUI window.
1067  */
1068     void
1069 gui_mch_iconify(void)
1075  * Invert a rectangle from row r, column c, for nr rows and nc columns.
1076  */
1077     void
1078 gui_mch_invert_rectangle(int r, int c, int nr, int nc)
1084  * Called when the foreground or background color has been changed.
1085  */
1086     void
1087 gui_mch_new_colors(void)
1089     gui.def_back_pixel = gui.back_pixel;
1090     gui.def_norm_pixel = gui.norm_pixel;
1092     //NSLog(@"gui_mch_new_colors(back=%x, norm=%x)", gui.def_back_pixel,
1093     //        gui.def_norm_pixel);
1095     [[MMBackend sharedInstance]
1096         setDefaultColorsBackground:gui.def_back_pixel
1097                         foreground:gui.def_norm_pixel];
1101     void
1102 gui_mch_def_colors()
1104     // Default foreground and background colors are black and white.
1105     gui.def_norm_pixel = gui.norm_pixel = 0;
1106     gui.def_back_pixel = gui.back_pixel = 0xffffff;
1111  * Set the current text background color.
1112  */
1113     void
1114 gui_mch_set_bg_color(guicolor_T color)
1116     [[MMBackend sharedInstance] setBackgroundColor:color];
1121  * Cursor blink functions.
1123  * This is a simple state machine:
1124  * BLINK_NONE   not blinking at all
1125  * BLINK_OFF    blinking, cursor is not shown
1126  * BLINK_ON blinking, cursor is shown
1127  */
1128     void
1129 gui_mch_set_blinking(long wait, long on, long off)
1135  * Set the current text foreground color.
1136  */
1137     void
1138 gui_mch_set_fg_color(guicolor_T color)
1140     [[MMBackend sharedInstance] setForegroundColor:color];
1144 #if defined(FEAT_EVAL) || defined(PROTO)
1146  * Bring the Vim window to the foreground.
1147  */
1148     void
1149 gui_mch_set_foreground(void)
1152 #endif
1156     void
1157 gui_mch_set_shellsize(
1158     int         width,
1159     int         height,
1160     int         min_width,
1161     int         min_height,
1162     int         base_width,
1163     int         base_height,
1164     int         direction)
1166     //NSLog(@"gui_mch_set_shellsize(width=%d, height=%d, min_width=%d,"
1167     //        " min_height=%d, base_width=%d, base_height=%d, direction=%d)",
1168     //        width, height, min_width, min_height, base_width, base_height,
1169     //        direction);
1170     [[MMBackend sharedInstance] setRows:height columns:width];
1175  * Set the current text special color.
1176  */
1177     void
1178 gui_mch_set_sp_color(guicolor_T color)
1183     void
1184 gui_mch_set_text_area_pos(int x, int y, int w, int h)
1189  * Set the position of the top left corner of the window to the given
1190  * coordinates.
1191  */
1192     void
1193 gui_mch_set_winpos(int x, int y)
1198     void
1199 gui_mch_setmouse(int x, int y)
1204 #ifdef FEAT_TITLE
1206  * Set the window title and icon.
1207  * (The icon is not taken care of).
1208  */
1209     void
1210 gui_mch_settitle(char_u *title, char_u *icon)
1212     //NSLog(@"gui_mch_settitle(title=%s, icon=%s)", title, icon);
1214     [[MMBackend sharedInstance] setVimWindowTitle:(char*)title];
1216 #endif
1220  * Start the cursor blinking.  If it was already blinking, this restarts the
1221  * waiting time and shows the cursor.
1222  */
1223     void
1224 gui_mch_start_blink(void)
1230  * Stop the cursor blinking.  Show the cursor if it wasn't shown.
1231  */
1232     void
1233 gui_mch_stop_blink(void)
1238     void
1239 gui_mch_toggle_tearoffs(int enable)
1244     void
1245 mch_set_mouse_shape(int shape)
1250     static BOOL
1251 gui_cocoa_is_valid_action(NSString *action)
1253     static NSDictionary *actionDict = nil;
1255     if (!actionDict) {
1256         NSBundle *mainBundle = [NSBundle mainBundle];
1257         NSString *path = [mainBundle pathForResource:@"Actions"
1258                                               ofType:@"plist"];
1259         if (path) {
1260             actionDict = [[NSDictionary alloc] initWithContentsOfFile:path];
1261         } else {
1262             // Allocate bogus dictionary so that error only pops up once.
1263             actionDict = [NSDictionary new];
1264             EMSG(_("E???: Failed to load action dictionary"));
1265         }
1266     }
1268     return [actionDict objectForKey:action] != nil;