From dc29496e335f047cbee8a1c58371ad1827d0467d Mon Sep 17 00:00:00 2001 From: Bibiko Date: Thu, 30 Apr 2009 08:58:19 +0000 Subject: =?UTF-8?q?=E2=80=A2=20MySQL=20Help=20-=20refactored=20and=20clean?= =?UTF-8?q?ed=20contextMenuItems=20for=20the=20webview=20-=20help-html-tem?= =?UTF-8?q?plate=20will=20be=20loaded=20only=20once?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CustomQuery.h | 7 ++-- Source/CustomQuery.m | 109 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 73 insertions(+), 43 deletions(-) diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index fd305662..8973804c 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -75,12 +75,13 @@ NSString *usedQuery; NSString *mySQLversion; - - + int queryStartPosition; + int helpTarget; WebHistory *helpHistory; - + NSString *helpHTMLTemplate; + } // IBAction methods diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 0a4681bf..4af056b1 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -1723,16 +1723,6 @@ traps enter key and [tableDetails release]; - NSString *helpHTMLTemplatePath = [[NSBundle mainBundle] pathForResource:@"sequel-pro-mysql-help-template" ofType:@"html"]; - NSError *error; - NSString *helpHTMLTemplate = [[NSString alloc] - initWithContentsOfFile:helpHTMLTemplatePath - encoding:NSUTF8StringEncoding - error:&error]; - // an error occurred while reading - if (helpHTMLTemplate == nil) - return [NSString stringWithFormat:@"Error reading “sequel-pro-mysql-help-template.html”!
%@", [error localizedFailureReason]]; - return [NSString stringWithFormat:helpHTMLTemplate, theHelp]; } @@ -1781,45 +1771,71 @@ traps enter key and /* * Manage contextual menu in helpWebView - * Ignore "Reload", "Open Link", "Open Link in new Window", "Download link". + * Ignore "Reload", "Open Link", "Open Link in new Window", "Download link" etc. */ - (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems { - int menuType = [defaultMenuItems count]; - NSMutableArray *returnArray = [[NSMutableArray array] autorelease]; - NSMenuItem *searchInMySQL; - NSMenuItem *searchInMySQLonline; - // dispatcher for different context menus via the number of menu items - switch(menuType) + NSMutableArray *webViewMenuItems = [[defaultMenuItems mutableCopy] autorelease]; + + if (webViewMenuItems) { - case 1: // only reload - return nil; - break; - case 2: // back and reload - return [NSArray arrayWithObjects:[defaultMenuItems objectAtIndex:0], nil]; - break; - case 3: // back, forward, and reload - return [NSArray arrayWithObjects:[defaultMenuItems objectAtIndex:0], [defaultMenuItems objectAtIndex:1], nil]; - break; - case 4: // cursor over link (only allow copy link) - return [NSArray arrayWithObjects:[defaultMenuItems objectAtIndex:3], nil]; - break; - case 6: // menu for a selection, add Search in MySQL Help - searchInMySQL = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Search in MySQL Help", @"Search in MySQL Help") action:@selector(showHelpForWebViewSelection:) keyEquivalent:@""]; - [searchInMySQL setEnabled:YES]; - [searchInMySQL setTarget:self]; - [returnArray addObject:searchInMySQL]; + // Remove all needless default menu items + NSEnumerator *itemEnumerator = [defaultMenuItems objectEnumerator]; + NSMenuItem *menuItem = nil; + while (menuItem = [itemEnumerator nextObject]) + { + int tag = [menuItem tag]; + switch (tag) + { + case 2000: // WebMenuItemTagOpenLink + case WebMenuItemTagOpenLinkInNewWindow: + case WebMenuItemTagDownloadLinkToDisk: + case WebMenuItemTagOpenImageInNewWindow: + case WebMenuItemTagDownloadImageToDisk: + case WebMenuItemTagCopyImageToClipboard: + case WebMenuItemTagOpenFrameInNewWindow: + case WebMenuItemTagStop: + case WebMenuItemTagReload: + case WebMenuItemTagCut: + case WebMenuItemTagPaste: + case WebMenuItemTagSpellingGuess: + case WebMenuItemTagNoGuessesFound: + case WebMenuItemTagIgnoreSpelling: + case WebMenuItemTagLearnSpelling: + case WebMenuItemTagOther: + case WebMenuItemTagOpenWithDefaultApplication: + [webViewMenuItems removeObjectIdenticalTo: menuItem]; + break; + } + } + } + + // Add two menu items for a selection + if(webViewMenuItems + && [[element objectForKey:@"WebElementIsSelected"] boolValue] + && ![[element objectForKey:@"WebElementLinkIsLive"] boolValue]) + { + + NSMenuItem *searchInMySQL; + NSMenuItem *searchInMySQLonline; + + [webViewMenuItems insertObject:[NSMenuItem separatorItem] atIndex:0]; + searchInMySQLonline = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Search in MySQL Documentation", @"Search in MySQL Documentation") action:@selector(searchInDocForWebViewSelection:) keyEquivalent:@""]; [searchInMySQLonline setEnabled:YES]; [searchInMySQLonline setTarget:self]; - [returnArray addObject:searchInMySQLonline]; - [returnArray addObject:[NSMenuItem separatorItem]]; - [returnArray addObjectsFromArray:defaultMenuItems]; - return returnArray; - break; + [webViewMenuItems insertObject:searchInMySQLonline atIndex:0]; + + searchInMySQL = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Search in MySQL Help", @"Search in MySQL Help") action:@selector(showHelpForWebViewSelection:) keyEquivalent:@""]; + [searchInMySQL setEnabled:YES]; + [searchInMySQL setTarget:self]; + [webViewMenuItems insertObject:searchInMySQL atIndex:0]; + } - return nil; + + return webViewMenuItems; + } @@ -1832,6 +1848,19 @@ traps enter key and prefs = nil; usedQuery = [[NSString stringWithString:@""] retain]; + // init helpHTMLTemplate + NSError *error; + helpHTMLTemplate = [[NSString alloc] + initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sequel-pro-mysql-help-template" ofType:@"html"] + encoding:NSUTF8StringEncoding + error:&error]; + // an error occurred while reading + if (helpHTMLTemplate == nil) + { + NSLog(@"%@", [NSString stringWithFormat:@"Error reading “sequel-pro-mysql-help-template.html”!
%@", [error localizedFailureReason]]); + NSBeep(); + } + return self; } -- cgit v1.2.3