Fixed tab selection bug where the wrong tab would get opened if multiple files
[MacVim/jjgod.git] / MMVimController.m
blob5ca667d7137e71b3d32285953a338e82e413af85
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 "MMVimController.h"
12 #import "MMWindowController.h"
13 #import "MMAppController.h"
14 #import "MMTextView.h"
15 #import "MMTextStorage.h"
18 //static NSString *AttentionToolbarItemID = @"Attention";
19 static NSString *DefaultToolbarImageName = @"Attention";
22 @interface MMVimController (Private)
23 - (void)handleMessage:(int)msgid data:(NSData *)data;
24 - (void)performBatchDrawWithData:(NSData *)data;
25 - (void)panelDidEnd:(NSSavePanel *)panel code:(int)code
26             context:(void *)context;
27 - (NSMenuItem *)menuItemForTag:(int)tag;
28 - (NSMenu *)menuForTag:(int)tag;
29 - (void)addMenuWithTag:(int)tag parent:(NSMenu *)parent title:(NSString *)title
30                atIndex:(int)idx;
31 - (void)addMenuItemWithTag:(int)tag parent:(NSMenu *)parent
32                      title:(NSString *)title tip:(NSString *)tip
33              keyEquivalent:(int)key modifiers:(int)mask
34                     action:(NSString *)action atIndex:(int)idx;
35 - (void)updateMainMenu;
36 - (NSToolbarItem *)toolbarItemForTag:(int)tag index:(int *)index;
37 - (IBAction)toolbarAction:(id)sender;
38 - (void)addToolbarItemToDictionaryWithTag:(int)tag label:(NSString *)title
39         toolTip:(NSString *)tip icon:(NSString *)icon;
40 - (void)addToolbarItemWithTag:(int)tag label:(NSString *)label
41                           tip:(NSString *)tip icon:(NSString *)icon
42                       atIndex:(int)idx;
43 #if MM_USE_DO
44 - (void)connectionDidDie:(NSNotification *)notification;
45 #endif
46 - (BOOL)executeActionWithName:(NSString *)name;
47 @end
51 // TODO: Move to separate file
52 @interface NSColor (MMProtocol)
53 + (NSColor *)colorWithRgbInt:(int)rgb;
54 @end
58 static NSMenuItem *findMenuItemWithTagInMenu(NSMenu *root, int tag)
60     if (root) {
61         NSMenuItem *item = [root itemWithTag:tag];
62         if (item) return item;
64         NSArray *items = [root itemArray];
65         unsigned i, count = [items count];
66         for (i = 0; i < count; ++i) {
67             item = [items objectAtIndex:i];
68             if ([item hasSubmenu]) {
69                 item = findMenuItemWithTagInMenu([item submenu], tag);
70                 if (item) return item;
71             }
72         }
73     }
75     return nil;
80 @implementation MMVimController
82 #if MM_USE_DO
83 - (id)initWithBackend:(id)backend
84 #else
85 - (id)initWithPort:(NSPort *)port
86 #endif
88     if ((self = [super init])) {
89         windowController =
90             [[MMWindowController alloc] initWithVimController:self];
91 #if MM_USE_DO
92         backendProxy = [backend retain];
93 # if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
94         sendQueue = [NSMutableArray new];
95 # endif
97         NSConnection *connection = [backendProxy connectionForProxy];
98         [[NSNotificationCenter defaultCenter] addObserver:self
99                 selector:@selector(connectionDidDie:)
100                     name:NSConnectionDidDieNotification object:connection];
101 #else
102         sendPort = [port retain];
104         // Init receive port and send connected message to VimTask
105         receivePort = [NSMachPort new];
106         [receivePort setDelegate:self];
108         // Add to the default run loop mode as well as the event tracking mode;
109         // the latter ensures that updates from the VimTask reaches
110         // MMVimController whilst the user resizes a window with the mouse.
111         [[NSRunLoop currentRunLoop] addPort:receivePort
112                                     forMode:NSDefaultRunLoopMode];
113         [[NSRunLoop currentRunLoop] addPort:receivePort
114                                     forMode:NSEventTrackingRunLoopMode];
116         [NSPortMessage sendMessage:ConnectedMsgID withSendPort:sendPort
117                        receivePort:receivePort wait:YES];
118 #endif
120         mainMenuItems = [[NSMutableArray alloc] init];
122         toolbarItemDict = [[NSMutableDictionary alloc] init];
123         //[self addToolbarItemToDictionaryWithTag:0 label:@"Attention"
124         //                                toolTip:@"A toolbar item is missing"
125         //                                   icon:@"Attention"];
127         [[NSNotificationCenter defaultCenter]
128                 addObserver:self
129                    selector:@selector(windowWillClose:)
130                        name:NSWindowWillCloseNotification
131                      object:[windowController window]];
132         [[NSNotificationCenter defaultCenter]
133                 addObserver:self
134                    selector:@selector(windowDidBecomeMain:)
135                        name:NSWindowDidBecomeMainNotification
136                      object:[windowController window]];
138     }
140     return self;
143 - (void)dealloc
145     //NSLog(@"%@ %s", [self className], _cmd);
147     [[NSNotificationCenter defaultCenter] removeObserver:self];
149 #if MM_USE_DO
150     [backendProxy release];
151 # if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
152     [sendQueue release];
153 # endif
154 #else
155     if (sendPort) {
156         // Kill task immediately
157         [NSPortMessage sendMessage:KillTaskMsgID withSendPort:sendPort
158                        receivePort:receivePort wait:NO];
159     }
161     [sendPort release];
162     [receivePort release];
163 #endif
165     [toolbarItemDict release];
166     [toolbar release];
167     [mainMenuItems release];
168     [windowController release];
170     [super dealloc];
173 - (MMWindowController *)windowController
175     return windowController;
178 - (void)sendMessage:(int)msgid data:(NSData *)data wait:(BOOL)wait
180 #if MM_USE_DO
181 # if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
182     if (inProcessCommandQueue) {
183         //NSLog(@"In process command queue; delaying message send.");
184         [sendQueue addObject:[NSNumber numberWithInt:msgid]];
185         if (data)
186             [sendQueue addObject:data];
187         else
188             [sendQueue addObject:[NSNull null]];
189         return;
190     }
191 # endif
192     if (wait) {
193         [backendProxy processInput:msgid data:data];
194     } else {
195         // Do not wait for the message to be sent, i.e. drop the message if it
196         // can't be delivered immediately.
197         NSConnection *connection = [backendProxy connectionForProxy];
198         if (connection) {
199             NSTimeInterval req = [connection requestTimeout];
200             [connection setRequestTimeout:0];
201             @try {
202                 [backendProxy processInput:msgid data:data];
203             }
204             @catch (NSException *e) {
205                 // Connection timed out, just ignore this.
206                 //NSLog(@"WARNING! Connection timed out in %s", _cmd);
207             }
208             @finally {
209                 [connection setRequestTimeout:req];
210             }
211         }
212     }
213 #else
214     [NSPortMessage sendMessage:msgid withSendPort:sendPort data:data
215                           wait:wait];
216 #endif
219 #if MM_USE_DO
220 - (id)backendProxy
222     return backendProxy;
225 - (oneway void)showSavePanelForDirectory:(in bycopy NSString *)dir
226                                    title:(in bycopy NSString *)title
227                                   saving:(int)saving
229     [windowController setStatusText:title];
231     if (saving) {
232         [[NSSavePanel savePanel] beginSheetForDirectory:dir file:nil
233                 modalForWindow:[windowController window]
234                  modalDelegate:self
235                 didEndSelector:@selector(panelDidEnd:code:context:)
236                    contextInfo:NULL];
237     } else {
238         NSOpenPanel *panel = [NSOpenPanel openPanel];
239         [panel setAllowsMultipleSelection:NO];
240         [panel beginSheetForDirectory:dir file:nil types:nil
241                 modalForWindow:[windowController window]
242                  modalDelegate:self
243                 didEndSelector:@selector(panelDidEnd:code:context:)
244                    contextInfo:NULL];
245     }
248 - (oneway void)processCommandQueue:(in NSArray *)queue
250     unsigned i, count = [queue count];
251     if (count % 2) {
252         NSLog(@"WARNING: Uneven number of components (%d) in flush queue "
253                 "message; ignoring this message.", count);
254         return;
255     }
257 #if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
258     inProcessCommandQueue = YES;
259 #endif
261     //NSLog(@"======== %s BEGIN ========", _cmd);
262     for (i = 0; i < count; i += 2) {
263         NSData *value = [queue objectAtIndex:i];
264         NSData *data = [queue objectAtIndex:i+1];
266         int msgid = *((int*)[value bytes]);
267 #if 0
268         if (msgid != EnableMenuItemMsgID && msgid != AddMenuItemMsgID
269                 && msgid != AddMenuMsgID) {
270             NSLog(@"%s%s", _cmd, MessageStrings[msgid]);
271         }
272 #endif
274         [self handleMessage:msgid data:data];
275     }
276     //NSLog(@"======== %s  END  ========", _cmd);
278     if (shouldUpdateMainMenu) {
279         [self updateMainMenu];
280     }
282 #if MM_DELAY_SEND_IN_PROCESS_CMD_QUEUE
283     inProcessCommandQueue = NO;
285     count = [sendQueue count];
286     if (count > 0) {
287         if (count % 2 == 0) {
288             //NSLog(@"%s Sending %d queued messages", _cmd, count/2);
290             for (i = 0; i < count; i += 2) {
291                 int msgid = [[sendQueue objectAtIndex:i] intValue];
292                 id data = [sendQueue objectAtIndex:i+1];
293                 if ([data isEqual:[NSNull null]])
294                     data = nil;
296                 [backendProxy processInput:msgid data:data];
297             }
298         }
300         [sendQueue removeAllObjects];
301     }
302 #endif
305 #else // MM_USE_DO
307 - (NSPort *)sendPort
309     return sendPort;
312 - (void)handlePortMessage:(NSPortMessage *)portMessage
314     //NSLog(@"%@ %s %@", [self className], _cmd, portMessage);
316     NSArray *components = [portMessage components];
317     unsigned msgid = [portMessage msgid];
319     //NSLog(@"%s%d", _cmd, msgid);
321     if (FlushQueueMsgID == msgid) {
322         unsigned i, count = [components count];
323         if (count % 2) {
324             NSLog(@"WARNING: Uneven number of components (%d) in flush queue "
325                     "message; ignoring this message.", count);
326             return;
327         }
329         for (i = 0; i < count; i += 2) {
330             NSData *value = [components objectAtIndex:i];
331             NSData *data = [components objectAtIndex:i+1];
333             [self handleMessage:*((int*)[value bytes]) data:data];
334         }
335     } else {
336         NSData *data = nil;
337         if ([components count] > 0)
338             data = [components objectAtIndex:0];
340         [self handleMessage:msgid data:data];
341     }
343     if (shouldUpdateMainMenu) {
344         [self updateMainMenu];
345     }
347 #endif // MM_USE_DO
349 - (void)windowWillClose:(NSNotification *)notification
351     // NOTE!  This causes the call to removeVimController: to be delayed.
352     [[NSApp delegate]
353             performSelectorOnMainThread:@selector(removeVimController:)
354                              withObject:self waitUntilDone:NO];
357 - (void)windowDidBecomeMain:(NSNotification *)notification
359     [self updateMainMenu];
362 - (NSToolbarItem *)toolbar:(NSToolbar *)theToolbar
363     itemForItemIdentifier:(NSString *)itemId
364     willBeInsertedIntoToolbar:(BOOL)flag
366     //NSLog(@"%s", _cmd);
368     NSToolbarItem *item = [toolbarItemDict objectForKey:itemId];
369     if (!item) {
370         NSLog(@"WARNING:  No toolbar item with id '%@'", itemId);
371     }
373     return item;
376 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)theToolbar
378     //NSLog(@"%s", _cmd);
379     return nil;
382 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)theToolbar
384     //NSLog(@"%s", _cmd);
385     return nil;
388 @end // MMVimController
392 @implementation MMVimController (Private)
394 - (void)handleMessage:(int)msgid data:(NSData *)data
396     //NSLog(@"%@ %s", [self className], _cmd);
398     if (OpenVimWindowMsgID == msgid) {
399         [windowController openWindow];
400     }
401 #if !MM_USE_DO
402     else if (TaskExitedMsgID == msgid) {
403         //NSLog(@"Received task exited message from VimTask; closing window.");
405         // Release sendPort immediately to avoid dealloc trying to send a 'kill
406         // task' message to the task.
407         [sendPort release];  sendPort = nil;
408         // NOTE!  This causes windowWillClose: to be called, which in turn asks
409         // the MMAppController to remove this MMVimController.
410         [windowController close];
412         // HACK! Make sure no menu updating is done, we're about to close.
413         shouldUpdateMainMenu = NO;
414     }
415 #endif // !MM_USE_DO
416     else if (BatchDrawMsgID == msgid) {
417         //NSLog(@"Received batch draw message from VimTask.");
419         [self performBatchDrawWithData:data];
420     } else if (SelectTabMsgID == msgid) {
421         const void *bytes = [data bytes];
422         int idx = *((int*)bytes);
423         //NSLog(@"Selecting tab with index %d", idx);
424         [windowController selectTabWithIndex:idx];
425     } else if (UpdateTabBarMsgID == msgid) {
426         //NSLog(@"Updating tabs");
427         [windowController updateTabsWithData:data];
428     } else if (ShowTabBarMsgID == msgid) {
429         //NSLog(@"Showing tab bar");
431         // HACK! Vim sends several draw commands etc. after the show message
432         // and these can mess up the display when showing the tab bar results
433         // in the window having to resize to fit the screen; delaying this
434         // message alleviates this problem.
435         [windowController performSelectorOnMainThread:@selector(showTabBar:)
436                                            withObject:self waitUntilDone:NO];
437         //[windowController showTabBar:self];
438     } else if (HideTabBarMsgID == msgid) {
439         //NSLog(@"Hiding tab bar");
440         [windowController hideTabBar:self];
441     } else if (SetTextDimensionsMsgID == msgid) {
442         const void *bytes = [data bytes];
443         int rows = *((int*)bytes);  bytes += sizeof(int);
444         int cols = *((int*)bytes);  bytes += sizeof(int);
446         [windowController setTextDimensionsWithRows:rows columns:cols];
447     } else if (SetVimWindowTitleMsgID == msgid) {
448         const void *bytes = [data bytes];
449         int len = *((int*)bytes);  bytes += sizeof(int);
451 #if 0
452         // BUG!  Using this call leads to ALL windows getting the same title
453         // and then the app crashes if you :q a window.
454         NSString *string = [[NSString alloc]
455                 initWithBytesNoCopy:(void*)bytes
456                              length:len
457                            encoding:NSUTF8StringEncoding
458                        freeWhenDone:NO];
459 #else
460         NSString *string = [[NSString alloc] initWithBytes:(void*)bytes
461                 length:len encoding:NSUTF8StringEncoding];
462 #endif
464         [[windowController window] setTitle:string];
466         [string release];
467     } else if (BrowseForFileMsgID == msgid) {
468         const void *bytes = [data bytes];
469         int save = *((int*)bytes);  bytes += sizeof(int);
471         int len = *((int*)bytes);  bytes += sizeof(int);
472         NSString *dir = nil;
473         if (len > 0) {
474             dir = [[NSString alloc] initWithBytes:(void*)bytes
475                                            length:len
476                                          encoding:NSUTF8StringEncoding];
477             bytes += len;
478         }
480         len = *((int*)bytes);  bytes += sizeof(int);
481         if (len > 0) {
482             NSString *title = [[NSString alloc]
483                     initWithBytes:(void*)bytes length:len
484                          encoding:NSUTF8StringEncoding];
485             bytes += len;
487             [windowController setStatusText:title];
488             [title release];
489         }
491         if (save) {
492             [[NSSavePanel savePanel] beginSheetForDirectory:dir file:nil
493                 modalForWindow:[windowController window]
494                  modalDelegate:self
495                 didEndSelector:@selector(panelDidEnd:code:context:)
496                    contextInfo:NULL];
497         } else {
498             NSOpenPanel *panel = [NSOpenPanel openPanel];
499             [panel setAllowsMultipleSelection:NO];
500             [panel beginSheetForDirectory:dir file:nil types:nil
501                     modalForWindow:[windowController window]
502                      modalDelegate:self
503                     didEndSelector:@selector(panelDidEnd:code:context:)
504                        contextInfo:NULL];
505         }
507         [dir release];
508     } else if (UpdateInsertionPointMsgID == msgid) {
509         const void *bytes = [data bytes];
510         int color = *((int*)bytes);  bytes += sizeof(int);
511         int row = *((int*)bytes);  bytes += sizeof(int);
512         int col = *((int*)bytes);  bytes += sizeof(int);
513         int state = *((int*)bytes);  bytes += sizeof(int);
515         // TODO! Move to window controller.
516         MMTextView *textView = [windowController textView];
517         if (textView) {
518             MMTextStorage *textStorage = (MMTextStorage*)[textView textStorage];
519             unsigned off = [textStorage offsetFromRow:row column:col];
521             [textView setInsertionPointColor:[NSColor colorWithRgbInt:color]];
522             [textView setSelectedRange:NSMakeRange(off, 0)];
523             [textView setShouldDrawInsertionPoint:state];
524         }
525     } else if (AddMenuMsgID == msgid) {
526         NSString *title = nil;
527         const void *bytes = [data bytes];
528         int tag = *((int*)bytes);  bytes += sizeof(int);
529         int parentTag = *((int*)bytes);  bytes += sizeof(int);
530         int len = *((int*)bytes);  bytes += sizeof(int);
531         if (len > 0) {
532             title = [[NSString alloc] initWithBytes:(void*)bytes length:len
533                                            encoding:NSUTF8StringEncoding];
534             bytes += len;
535         }
536         int idx = *((int*)bytes);  bytes += sizeof(int);
538         if (MenuToolbarType == parentTag) {
539             if (!toolbar) {
540                 NSString *ident = [NSString stringWithFormat:@"%d.%d",
541                          (int)self, tag];
542                 //NSLog(@"Creating toolbar with identifier %@", ident);
543                 toolbar = [[NSToolbar alloc] initWithIdentifier:ident];
545                 [toolbar setShowsBaselineSeparator:NO];
546                 [toolbar setDelegate:self];
547                 [toolbar setDisplayMode:NSToolbarDisplayModeIconOnly];
548                 [toolbar setSizeMode:NSToolbarSizeModeSmall];
550                 [[windowController window] setToolbar:toolbar];
551             }
552         } else if (MenuPopupType == parentTag) {
553             // TODO!
554         } else if (title) {
555             NSMenu *parent = [self menuForTag:parentTag];
556             [self addMenuWithTag:tag parent:parent title:title atIndex:idx];
557         }
559         [title release];
560     } else if (AddMenuItemMsgID == msgid) {
561         NSString *title = nil, *tip = nil, *icon = nil, *action = nil;
562         const void *bytes = [data bytes];
563         int tag = *((int*)bytes);  bytes += sizeof(int);
564         int parentTag = *((int*)bytes);  bytes += sizeof(int);
565         int namelen = *((int*)bytes);  bytes += sizeof(int);
566         if (namelen > 0) {
567             title = [[NSString alloc] initWithBytes:(void*)bytes length:namelen
568                                            encoding:NSUTF8StringEncoding];
569             bytes += namelen;
570         }
571         int tiplen = *((int*)bytes);  bytes += sizeof(int);
572         if (tiplen > 0) {
573             tip = [[NSString alloc] initWithBytes:(void*)bytes length:tiplen
574                                            encoding:NSUTF8StringEncoding];
575             bytes += tiplen;
576         }
577         int iconlen = *((int*)bytes);  bytes += sizeof(int);
578         if (iconlen > 0) {
579             icon = [[NSString alloc] initWithBytes:(void*)bytes length:iconlen
580                                            encoding:NSUTF8StringEncoding];
581             bytes += iconlen;
582         }
583         int actionlen = *((int*)bytes);  bytes += sizeof(int);
584         if (actionlen > 0) {
585             action = [[NSString alloc] initWithBytes:(void*)bytes
586                                               length:actionlen
587                                             encoding:NSUTF8StringEncoding];
588             bytes += actionlen;
589         }
590         int idx = *((int*)bytes);  bytes += sizeof(int);
591         if (idx < 0) idx = 0;
592         int key = *((int*)bytes);  bytes += sizeof(int);
593         int mask = *((int*)bytes);  bytes += sizeof(int);
595         NSString *ident = [NSString stringWithFormat:@"%d.%d",
596                 (int)self, parentTag];
597         if (toolbar && [[toolbar identifier] isEqual:ident]) {
598             [self addToolbarItemWithTag:tag label:title tip:tip icon:icon
599                                 atIndex:idx];
600         } else {
601             NSMenu *parent = [self menuForTag:parentTag];
602             [self addMenuItemWithTag:tag parent:parent title:title tip:tip
603                        keyEquivalent:key modifiers:mask action:action
604                              atIndex:idx];
605         }
607         [title release];
608         [tip release];
609         [icon release];
610         [action release];
611     } else if (RemoveMenuItemMsgID == msgid) {
612         const void *bytes = [data bytes];
613         int tag = *((int*)bytes);  bytes += sizeof(int);
615         // TODO: Search for tag in popup menus.
616         id item;
617         int idx;
618         if ((item = [self toolbarItemForTag:tag index:&idx])) {
619             [toolbar removeItemAtIndex:idx];
620         } else if ((item = [self menuItemForTag:tag])) {
621             if ([item menu] == [NSApp mainMenu]) {
622                 NSLog(@"Removing menu: %@", item);
623                 [mainMenuItems removeObject:item];
624             }
625             [[item menu] removeItem:item];
626         }
627     } else if (EnableMenuItemMsgID == msgid) {
628         const void *bytes = [data bytes];
629         int tag = *((int*)bytes);  bytes += sizeof(int);
630         int state = *((int*)bytes);  bytes += sizeof(int);
632         // TODO: Search for tag in popup menus.
633         id item = [self toolbarItemForTag:tag index:NULL];
634         if (!item)
635             item = [self menuItemForTag:tag];
637         [item setEnabled:state];
638     } else if (ShowToolbarMsgID == msgid) {
639         const void *bytes = [data bytes];
640         int enable = *((int*)bytes);  bytes += sizeof(int);
641         int flags = *((int*)bytes);  bytes += sizeof(int);
643         int mode = NSToolbarDisplayModeDefault;
644         if (flags & ToolbarLabelFlag) {
645             mode = flags & ToolbarIconFlag ? NSToolbarDisplayModeIconAndLabel
646                     : NSToolbarDisplayModeLabelOnly;
647         } else if (flags & ToolbarIconFlag) {
648             mode = NSToolbarDisplayModeIconOnly;
649         }
651         int size = flags & ToolbarSizeRegularFlag ? NSToolbarSizeModeRegular
652                 : NSToolbarSizeModeSmall;
654         [toolbar setSizeMode:size];
655         [toolbar setDisplayMode:mode];
656         [toolbar setVisible:enable];
657     } else if (CreateScrollbarMsgID == msgid) {
658         const void *bytes = [data bytes];
659         long ident = *((long*)bytes);  bytes += sizeof(long);
660         int type = *((int*)bytes);  bytes += sizeof(int);
662         [windowController createScrollbarWithIdentifier:ident type:type];
663     } else if (DestroyScrollbarMsgID == msgid) {
664         const void *bytes = [data bytes];
665         long ident = *((long*)bytes);  bytes += sizeof(long);
667         [windowController destroyScrollbarWithIdentifier:ident];
668     } else if (ShowScrollbarMsgID == msgid) {
669         const void *bytes = [data bytes];
670         long ident = *((long*)bytes);  bytes += sizeof(long);
671         int visible = *((int*)bytes);  bytes += sizeof(int);
673         [windowController showScrollbarWithIdentifier:ident state:visible];
674     } else if (SetScrollbarPositionMsgID == msgid) {
675         const void *bytes = [data bytes];
676         long ident = *((long*)bytes);  bytes += sizeof(long);
677         int pos = *((int*)bytes);  bytes += sizeof(int);
678         int len = *((int*)bytes);  bytes += sizeof(int);
680         [windowController setScrollbarPosition:pos length:len
681                                     identifier:ident];
682     } else if (SetScrollbarThumbMsgID == msgid) {
683         const void *bytes = [data bytes];
684         long ident = *((long*)bytes);  bytes += sizeof(long);
685         float val = *((float*)bytes);  bytes += sizeof(float);
686         float prop = *((float*)bytes);  bytes += sizeof(float);
688         [windowController setScrollbarThumbValue:val proportion:prop
689                                       identifier:ident];
690     } else if (SetFontMsgID == msgid) {
691         const void *bytes = [data bytes];
692         float size = *((float*)bytes);  bytes += sizeof(float);
693         int len = *((int*)bytes);  bytes += sizeof(int);
694         NSString *name = [[NSString alloc]
695                 initWithBytes:(void*)bytes length:len
696                      encoding:NSUTF8StringEncoding];
697         NSFont *font = [NSFont fontWithName:name size:size];
699         if (font)
700             [windowController setFont:font];
702         [name release];
703     } else if (SetDefaultColorsMsgID == msgid) {
704         const void *bytes = [data bytes];
705         int bg = *((int*)bytes);  bytes += sizeof(int);
706         int fg = *((int*)bytes);  bytes += sizeof(int);
707         NSColor *back = [NSColor colorWithRgbInt:bg];
708         NSColor *fore = [NSColor colorWithRgbInt:fg];
710         [windowController setDefaultColorsBackground:back foreground:fore];
711     } else if (ExecuteActionMsgID == msgid) {
712         const void *bytes = [data bytes];
713         int len = *((int*)bytes);  bytes += sizeof(int);
714         NSString *actionName = [[NSString alloc]
715                 initWithBytesNoCopy:(void*)bytes
716                              length:len
717                            encoding:NSUTF8StringEncoding
718                        freeWhenDone:NO];
720         SEL sel = NSSelectorFromString(actionName);
721         [NSApp sendAction:sel to:nil from:self];
723         [actionName release];
724     } else {
725         NSLog(@"WARNING: Unknown message received (msgid=%d)", msgid);
726     }
729 - (void)performBatchDrawWithData:(NSData *)data
731     // TODO!  Move to window controller.
732     MMTextStorage *textStorage = [windowController textStorage];
733     if (!textStorage)
734         return;
736     const void *bytes = [data bytes];
737     const void *end = bytes + [data length];
739     [textStorage beginEditing];
741     // TODO:
742     // 1. Sanity check input
743     // 2. Cache rgb -> NSColor lookups?
745     while (bytes < end) {
746         int type = *((int*)bytes);  bytes += sizeof(int);
748         if (ClearAllDrawType == type) {
749             int color = *((int*)bytes);  bytes += sizeof(int);
751             [textStorage clearAllWithColor:[NSColor colorWithRgbInt:color]];
752         } else if (ClearBlockDrawType == type) {
753             int color = *((int*)bytes);  bytes += sizeof(int);
754             int row1 = *((int*)bytes);  bytes += sizeof(int);
755             int col1 = *((int*)bytes);  bytes += sizeof(int);
756             int row2 = *((int*)bytes);  bytes += sizeof(int);
757             int col2 = *((int*)bytes);  bytes += sizeof(int);
759             [textStorage clearBlockFromRow:row1 column:col1
760                     toRow:row2 column:col2
761                     color:[NSColor colorWithRgbInt:color]];
762         } else if (DeleteLinesDrawType == type) {
763             int color = *((int*)bytes);  bytes += sizeof(int);
764             int row = *((int*)bytes);  bytes += sizeof(int);
765             int count = *((int*)bytes);  bytes += sizeof(int);
766             int bot = *((int*)bytes);  bytes += sizeof(int);
767             int left = *((int*)bytes);  bytes += sizeof(int);
768             int right = *((int*)bytes);  bytes += sizeof(int);
770             [textStorage deleteLinesFromRow:row lineCount:count
771                     scrollBottom:bot left:left right:right
772                            color:[NSColor colorWithRgbInt:color]];
773         } else if (ReplaceStringDrawType == type) {
774             int bg = *((int*)bytes);  bytes += sizeof(int);
775             int fg = *((int*)bytes);  bytes += sizeof(int);
776             int row = *((int*)bytes);  bytes += sizeof(int);
777             int col = *((int*)bytes);  bytes += sizeof(int);
778             int flags = *((int*)bytes);  bytes += sizeof(int);
779             int len = *((int*)bytes);  bytes += sizeof(int);
780             NSString *string = [[NSString alloc]
781                     initWithBytesNoCopy:(void*)bytes
782                                  length:len
783                                encoding:NSUTF8StringEncoding
784                            freeWhenDone:NO];
785             bytes += len;
787             [textStorage replaceString:string
788                                  atRow:row column:col
789                              withFlags:flags
790                        foregroundColor:[NSColor colorWithRgbInt:fg]
791                        backgroundColor:[NSColor colorWithRgbInt:bg]];
793             [string release];
794         } else if (InsertLinesDrawType == type) {
795             int color = *((int*)bytes);  bytes += sizeof(int);
796             int row = *((int*)bytes);  bytes += sizeof(int);
797             int count = *((int*)bytes);  bytes += sizeof(int);
798             int bot = *((int*)bytes);  bytes += sizeof(int);
799             int left = *((int*)bytes);  bytes += sizeof(int);
800             int right = *((int*)bytes);  bytes += sizeof(int);
802             [textStorage insertLinesAtRow:row lineCount:count
803                              scrollBottom:bot left:left right:right
804                                     color:[NSColor colorWithRgbInt:color]];
805         } else {
806             NSLog(@"WARNING: Unknown draw type (type=%d)", type);
807         }
808     }
810     [textStorage endEditing];
813 - (void)panelDidEnd:(NSSavePanel *)panel code:(int)code context:(void *)context
815 #if MM_USE_DO
816     [windowController setStatusText:@""];
818     NSString *string = (code == NSOKButton) ? [panel filename] : nil;
819     [backendProxy setBrowseForFileString:string];
820 #else
821     NSMutableData *data = [NSMutableData data];
822     int ok = (code == NSOKButton);
823     NSString *filename = [panel filename];
824     int len = [filename lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
826     [data appendBytes:&ok length:sizeof(int)];
827     [data appendBytes:&len length:sizeof(int)];
828     if (len > 0)
829         [data appendBytes:[filename UTF8String] length:len];
831     if (![NSPortMessage sendMessage:BrowseForFileReplyMsgID
832                        withSendPort:sendPort data:data wait:YES]) {
833         NSLog(@"WARNING: Failed to send browse for files reply back to "
834                 "VimTask.");
835     }
837     [windowController setStatusText:@""];
838 #endif // !MM_USE_DO
841 - (NSMenuItem *)menuItemForTag:(int)tag
843     int i, count = [mainMenuItems count];
844     for (i = 0; i < count; ++i) {
845         NSMenuItem *item = [mainMenuItems objectAtIndex:i];
846         if ([item tag] == tag) return item;
847         item = findMenuItemWithTagInMenu([item submenu], tag);
848         if (item) return item;
849     }
851     return nil;
854 - (NSMenu *)menuForTag:(int)tag
856     return [[self menuItemForTag:tag] submenu];
859 - (void)addMenuWithTag:(int)tag parent:(NSMenu *)parent title:(NSString *)title
860                atIndex:(int)idx
862     NSMenuItem *item = [[NSMenuItem alloc] init];
863     NSMenu *menu = [[NSMenu alloc] initWithTitle:title];
865     [menu setAutoenablesItems:NO];
866     [item setTag:tag];
867     [item setTitle:title];
868     [item setSubmenu:menu];
870     if (parent) {
871         if ([parent numberOfItems] <= idx) {
872             [parent addItem:item];
873         } else {
874             [parent insertItem:item atIndex:idx];
875         }
876     } else {
877         if ([mainMenuItems count] <= idx) {
878             [mainMenuItems addObject:item];
879         } else {
880             [mainMenuItems insertObject:item atIndex:idx];
881         }
883         shouldUpdateMainMenu = YES;
884     }
886     [item release];
887     [menu release];
890 - (void)addMenuItemWithTag:(int)tag parent:(NSMenu *)parent
891                      title:(NSString *)title tip:(NSString *)tip
892              keyEquivalent:(int)key modifiers:(int)mask
893                     action:(NSString *)action atIndex:(int)idx
895     if (parent) {
896         NSMenuItem *item = nil;
897         if (title) {
898             item = [[[NSMenuItem alloc] init] autorelease];
899             [item setTitle:title];
900             // TODO: Check that 'action' is a valid action (nothing will happen
901             // if it isn't, but it would be nice with a warning).
902             if (action) [item setAction:NSSelectorFromString(action)];
903             else        [item setAction:@selector(vimMenuItemAction:)];
904             if (tip) [item setToolTip:tip];
906             if (key != 0) {
907                 NSString *keyString =
908                     [NSString stringWithFormat:@"%C", key];
909                 [item setKeyEquivalent:keyString];
910                 [item setKeyEquivalentModifierMask:mask];
911             }
912         } else {
913             item = [NSMenuItem separatorItem];
914         }
916         // NOTE!  The tag is used to idenfity which menu items were
917         // added by Vim (tag != 0) and which were added by the AppKit
918         // (tag == 0).
919         [item setTag:tag];
921         if ([parent numberOfItems] <= idx) {
922             [parent addItem:item];
923         } else {
924             [parent insertItem:item atIndex:idx];
925         }
926     }
929 - (void)updateMainMenu
931     NSMenu *mainMenu = [NSApp mainMenu];
933     // Stop NSApp from updating the Window menu.
934     [NSApp setWindowsMenu:nil];
936     // Remove all menus from main menu (except the MacVim menu).
937     int i, count = [mainMenu numberOfItems];
938     for (i = count-1; i > 0; --i) {
939         [mainMenu removeItemAtIndex:i];
940     }
942     // Add menus from 'mainMenuItems' to main menu.
943     count = [mainMenuItems count];
944     for (i = 0; i < count; ++i) {
945         [mainMenu addItem:[mainMenuItems objectAtIndex:i]];
946     }
948     // Set the new Window menu.
949     // TODO!  Need to look for 'Window' in all localized languages.
950     NSMenu *windowMenu = [[mainMenu itemWithTitle:@"Window"] submenu];
951     if (windowMenu) {
952         // Remove all AppKit owned menu items (tag == 0); they will be added
953         // again when setWindowsMenu: is called.
954         count = [windowMenu numberOfItems];
955         for (i = count-1; i >= 0; --i) {
956             NSMenuItem *item = [windowMenu itemAtIndex:i];
957             if (![item tag]) {
958                 [windowMenu removeItem:item];
959             }
960         }
962         [NSApp setWindowsMenu:windowMenu];
963     }
965     shouldUpdateMainMenu = NO;
968 - (NSToolbarItem *)toolbarItemForTag:(int)tag index:(int *)index
970     if (!toolbar) return nil;
972     NSArray *items = [toolbar items];
973     int i, count = [items count];
974     for (i = 0; i < count; ++i) {
975         NSToolbarItem *item = [items objectAtIndex:i];
976         if ([item tag] == tag) {
977             if (index) *index = i;
978             return item;
979         }
980     }
982     return nil;
985 - (IBAction)toolbarAction:(id)sender
987     NSLog(@"%s%@", _cmd, sender);
990 - (void)addToolbarItemToDictionaryWithTag:(int)tag label:(NSString *)title
991         toolTip:(NSString *)tip icon:(NSString *)icon
993     // NOTE!  'title' is nul for separator item.  Since this is already defined
994     // by Coca, we don't need to do anything here.
995     if (!title) return;
997     NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:title];
998     [item setTag:tag];
999     [item setLabel:title];
1000     [item setToolTip:tip];
1001     [item setAction:@selector(vimMenuItemAction:)];
1002     [item setAutovalidates:NO];
1004     NSImage *img = [NSImage imageNamed:icon];
1005     if (!img) {
1006         NSLog(@"WARNING: Could not find image with name '%@' to use as toolbar"
1007                " image for identifier '%@';"
1008                " using default toolbar icon '%@' instead.",
1009                icon, title, DefaultToolbarImageName);
1011         img = [NSImage imageNamed:DefaultToolbarImageName];
1012     }
1014     [item setImage:img];
1016     [toolbarItemDict setObject:item forKey:title];
1018     [item release];
1021 - (void)addToolbarItemWithTag:(int)tag label:(NSString *)label tip:(NSString
1022                    *)tip icon:(NSString *)icon atIndex:(int)idx
1024     if (!toolbar) return;
1026     [self addToolbarItemToDictionaryWithTag:tag label:label toolTip:tip
1027                                        icon:icon];
1029     int maxIdx = [[toolbar items] count];
1030     if (maxIdx < idx) idx = maxIdx;
1032     // If 'label' is nul, insert a separator.
1033     if (!label) label = NSToolbarSeparatorItemIdentifier;
1034     [toolbar insertItemWithItemIdentifier:label atIndex:idx];
1037 #if MM_USE_DO
1038 - (void)connectionDidDie:(NSNotification *)notification
1040     //NSLog(@"A MMVimController lost its connection to the backend; "
1041     //       "closing the controller.");
1042     [windowController close];
1044 #endif // MM_USE_DO
1046 - (BOOL)executeActionWithName:(NSString *)name
1048 #if 0
1049     static NSDictionary *actionDict = nil;
1051     if (!actionDict) {
1052         NSBundle *mainBundle = [NSBundle mainBundle];
1053         NSString *path = [mainBundle pathForResource:@"Actions"
1054                                               ofType:@"plist"];
1055         if (path) {
1056             actionDict = [[NSDictionary alloc] initWithContentsOfFile:path];
1057             NSLog(@"Actions = %@", actionDict);
1058         } else {
1059             NSLog(@"WARNING: Failed to load dictionary of actions "
1060                     "(Actions.plist).");
1061             return NO;
1062         }
1063     }
1065     if ([actionDict objectForKey:name]) {
1066         NSLog(@"Executing action %@", name);
1067         SEL sel = NSSelectorFromString(name);
1069         if ([NSApp sendAction:sel to:nil from:self])
1070             return YES;
1072         NSLog(@"WARNING: Failed to send action");
1073     } else {
1074         NSLog(@"WARNING: Action with name '%@' cannot be executed.", name);
1075     }
1077 #endif
1078     return NO;
1081 @end // MMVimController (Private)
1085 @implementation NSColor (MMProtocol)
1087 + (NSColor *)colorWithRgbInt:(int)rgb
1089     float r = ((rgb>>16) & 0xff)/255.0f;
1090     float g = ((rgb>>8) & 0xff)/255.0f;
1091     float b = (rgb & 0xff)/255.0f;
1093     return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f];
1096 @end // NSColor (MMProtocol)