aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorMax <dmoagx@users.noreply.github.com>2017-12-27 23:18:54 +0100
committerMax <dmoagx@users.noreply.github.com>2018-01-20 02:42:35 +0100
commit3ff09ebc6109fd8eb4e291796f21a50f2765b661 (patch)
tree16ab534118b05a72741e07963f77605fcd0a9f60 /Source
parent2f0099e1bf3caf42cfe76aa4074efdfc7f351cef (diff)
downloadsequelpro-3ff09ebc6109fd8eb4e291796f21a50f2765b661.tar.gz
sequelpro-3ff09ebc6109fd8eb4e291796f21a50f2765b661.tar.bz2
sequelpro-3ff09ebc6109fd8eb4e291796f21a50f2765b661.zip
Merge SPAppController (part of #2789)
Diffstat (limited to 'Source')
-rw-r--r--Source/SPAppController.h22
-rw-r--r--Source/SPAppController.m240
-rw-r--r--Source/SPAppleScriptSupport.h41
-rw-r--r--Source/SPAppleScriptSupport.m128
-rw-r--r--Source/SPContentFilterManager.m1
-rw-r--r--Source/SPDatabaseDocument.m1
-rw-r--r--Source/SPNavigatorController.m1
-rw-r--r--Source/SPQueryDocumentsController.m1
-rw-r--r--Source/SPQueryFavoriteManager.m1
-rw-r--r--Source/SPTablesList.m1
-rw-r--r--Source/SPTextViewAdditions.m1
-rw-r--r--Source/SPWindowManagement.h55
-rw-r--r--Source/SPWindowManagement.m182
13 files changed, 261 insertions, 414 deletions
diff --git a/Source/SPAppController.h b/Source/SPAppController.h
index 4dd93839..818a781c 100644
--- a/Source/SPAppController.h
+++ b/Source/SPAppController.h
@@ -37,6 +37,7 @@
@class SPAboutController;
@class SPDatabaseDocument;
@class SPBundleEditorController;
+@class SPWindowController;
@interface SPAppController : NSObject <FRFeedbackReporterDelegate, NSApplicationDelegate, NSOpenSavePanelDelegate, NSFileManagerDelegate>
{
@@ -116,4 +117,25 @@
- (void)addHTMLOutputController:(id)controller;
- (void)removeHTMLOutputController:(id)controller;
+#pragma mark - SPAppleScriptSupport
+
+- (NSArray *)orderedDocuments;
+- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc;
+- (NSArray *)orderedWindows;
+- (id)handleQuitScriptCommand:(NSScriptCommand *)command;
+- (id)handleOpenScriptCommand:(NSScriptCommand *)command;
+
+#pragma mark - SPWindowManagement
+
+- (IBAction)newWindow:(id)sender;
+- (IBAction)newTab:(id)sender;
+- (IBAction)duplicateTab:(id)sender;
+
+- (SPWindowController *)newWindow;
+- (SPDatabaseDocument *)makeNewConnectionTabOrWindow;
+- (SPWindowController *)frontController;
+
+- (NSWindow *)frontDocumentWindow;
+- (void)tabDragStarted:(id)sender;
+
@end
diff --git a/Source/SPAppController.m b/Source/SPAppController.m
index afdfd16f..58080265 100644
--- a/Source/SPAppController.m
+++ b/Source/SPAppController.m
@@ -47,10 +47,10 @@
#import "SPFavoritesController.h"
#import "SPEditorTokens.h"
#import "SPBundleCommandRunner.h"
-#import "SPWindowManagement.h"
#import "SPCopyTable.h"
#import "SPSyntaxParser.h"
#import "SPOSInfo.h"
+#import "SPPrintController.h"
#import <PSMTabBar/PSMTabBarControl.h>
#import <Sparkle/Sparkle.h>
@@ -2329,6 +2329,244 @@
}
}
+#pragma mark - SPAppleScriptSupport
+
+/**
+ * Is needed to interact with AppleScript for set/get internal SP variables
+ */
+- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key
+{
+ NSLog(@"Not yet implemented.");
+
+ return NO;
+}
+
+/**
+ * AppleScript call to get the available documents.
+ */
+- (NSArray *)orderedDocuments
+{
+ NSMutableArray *orderedDocuments = [NSMutableArray array];
+
+ for (NSWindow *aWindow in [self orderedWindows])
+ {
+ if ([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) {
+ [orderedDocuments addObjectsFromArray:[[aWindow windowController] documents]];
+ }
+ }
+
+ return orderedDocuments;
+}
+
+/**
+ * AppleScript support for 'make new document'.
+ *
+ * TODO: following tab support this has been disabled - need to discuss reimplmenting vs syntax.
+ */
+- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc
+{
+ [self newWindow:self];
+
+ // Set autoconnection if appropriate
+ if ([[NSUserDefaults standardUserDefaults] boolForKey:SPAutoConnectToDefault]) {
+ [[self frontDocument] connect];
+ }
+}
+
+/**
+ * AppleScript call to get the available windows.
+ */
+- (NSArray *)orderedWindows
+{
+ return [NSApp orderedWindows];
+}
+
+/**
+ * AppleScript handler to quit Sequel Pro
+ *
+ * This handler is required to allow termination via the Dock or AppleScript event after activating it using AppleScript
+ */
+- (id)handleQuitScriptCommand:(NSScriptCommand *)command
+{
+ [NSApp terminate:self];
+
+ return nil;
+}
+
+/**
+ * AppleScript open handler
+ *
+ * This handler is required to catch the 'open' command if no argument was passed which would cause a crash.
+ */
+- (id)handleOpenScriptCommand:(NSScriptCommand *)command
+{
+ return nil;
+}
+
+/**
+ * AppleScript print handler
+ *
+ * This handler prints the active view.
+ */
+- (id)handlePrintScriptCommand:(NSScriptCommand *)command
+{
+ SPDatabaseDocument *frontDoc = [self frontDocument];
+
+ if (frontDoc && ![frontDoc isWorking] && ![[frontDoc connectionID] isEqualToString:@"_"]) {
+ [frontDoc startPrintDocumentOperation];
+ }
+
+ return nil;
+}
+
+#pragma mark - SPWindowManagement
+
+- (IBAction)newWindow:(id)sender
+{
+ [self newWindow];
+}
+
+/**
+ * Create a new window, containing a single tab.
+ */
+- (SPWindowController *)newWindow
+{
+ static NSPoint cascadeLocation = {.x = 0, .y = 0};
+
+ // Create a new window controller, and set up a new connection view within it.
+ SPWindowController *newWindowController = [[SPWindowController alloc] initWithWindowNibName:@"MainWindow"];
+ NSWindow *newWindow = [newWindowController window];
+
+ // Cascading defaults to on - retrieve the window origin automatically assigned by cascading,
+ // and convert to a top left point.
+ NSPoint topLeftPoint = [newWindow frame].origin;
+ topLeftPoint.y += [newWindow frame].size.height;
+
+ // The first window should use autosaving; subsequent windows should cascade.
+ // So attempt to set the frame autosave name; this will succeed for the very
+ // first window, and fail for others.
+ BOOL usedAutosave = [newWindow setFrameAutosaveName:@"DBView"];
+
+ if (!usedAutosave) {
+ [newWindow setFrameUsingName:@"DBView"];
+ }
+
+ // Add the connection view
+ [newWindowController addNewConnection];
+
+ // Cascade according to the statically stored cascade location.
+ cascadeLocation = [newWindow cascadeTopLeftFromPoint:cascadeLocation];
+
+ // Set the window controller as the window's delegate
+ [newWindow setDelegate:newWindowController];
+
+ // Show the window, and perform frontmost tasks again once the window has drawn
+ [newWindowController showWindow:self];
+ [[newWindowController selectedTableDocument] didBecomeActiveTabInWindow];
+
+ return newWindowController;
+}
+
+/**
+ * Create a new tab in the frontmost window.
+ */
+- (IBAction)newTab:(id)sender
+{
+ SPWindowController *frontController = [self frontController];
+
+ // If no window was found, create a new one
+ if (!frontController) {
+ [self newWindow:self];
+ }
+ else {
+ if ([[frontController window] isMiniaturized]) {
+ [[frontController window] deminiaturize:self];
+ }
+
+ [frontController addNewConnection:self];
+ }
+}
+
+- (SPDatabaseDocument *)makeNewConnectionTabOrWindow
+{
+ SPWindowController *frontController = [self frontController];
+
+ SPDatabaseDocument *frontDocument;
+ // If no window was found or the front most window has no tabs, create a new one
+ if (!frontController || [[frontController valueForKeyPath:@"tabView"] numberOfTabViewItems] == 1) {
+ frontController = [self newWindow];
+ frontDocument = [frontController selectedTableDocument];
+ }
+ // Open the spf file in a new tab if the tab bar is visible
+ else {
+ if ([[frontController window] isMiniaturized]) [[frontController window] deminiaturize:self];
+ frontDocument = [frontController addNewConnection];
+ }
+
+ return frontDocument;
+}
+
+/**
+ * Duplicate the current connection tab
+ */
+- (IBAction)duplicateTab:(id)sender
+{
+ SPDatabaseDocument *theFrontDocument = [self frontDocument];
+
+ if (!theFrontDocument) return [self newTab:sender];
+
+ // Add a new tab to the window
+ if ([[self frontDocumentWindow] isMiniaturized]) {
+ [[self frontDocumentWindow] deminiaturize:self];
+ }
+
+ SPDatabaseDocument *newConnection = [[self frontController] addNewConnection];
+
+ // Get the state of the previously-frontmost document
+ NSDictionary *allStateDetails = @{
+ @"connection" : @YES,
+ @"history" : @YES,
+ @"session" : @YES,
+ @"query" : @YES,
+ @"password" : @YES
+ };
+
+ NSMutableDictionary *frontState = [NSMutableDictionary dictionaryWithDictionary:[theFrontDocument stateIncludingDetails:allStateDetails]];
+
+ // Ensure it's set to autoconnect
+ [frontState setObject:@YES forKey:@"auto_connect"];
+
+ // Set the connection on the new tab
+ [newConnection setState:frontState];
+}
+
+/**
+ * Retrieve the frontmost document window; returns nil if not found.
+ */
+- (NSWindow *)frontDocumentWindow
+{
+ return [[self frontController] window];
+}
+
+- (SPWindowController *)frontController
+{
+ for (NSWindow *aWindow in [NSApp orderedWindows]) {
+ id ctr = [aWindow windowController];
+ if ([ctr isMemberOfClass:[SPWindowController class]]) {
+ return ctr;
+ }
+ }
+ return nil;
+}
+
+/**
+ * When tab drags start, bring all the windows in front of other applications.
+ */
+- (void)tabDragStarted:(id)sender
+{
+ [NSApp arrangeInFront:self];
+}
+
#pragma mark -
- (void)dealloc
diff --git a/Source/SPAppleScriptSupport.h b/Source/SPAppleScriptSupport.h
deleted file mode 100644
index 463a4b19..00000000
--- a/Source/SPAppleScriptSupport.h
+++ /dev/null
@@ -1,41 +0,0 @@
-//
-// SPAppleScriptSupport.h
-// sequel-pro
-//
-// Created by Stuart Connolly (stuconnolly.com) on October 14, 2010.
-// Copyright (c) 2010 Stuart Connolly. All rights reserved.
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
-// More info at <https://github.com/sequelpro/sequelpro>
-
-#import "SPAppController.h"
-
-@interface SPAppController (SPAppleScriptSupport)
-
-- (NSArray *)orderedDocuments;
-- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc;
-- (NSArray *)orderedWindows;
-- (id)handleQuitScriptCommand:(NSScriptCommand *)command;
-- (id)handleOpenScriptCommand:(NSScriptCommand *)command;
-
-@end
diff --git a/Source/SPAppleScriptSupport.m b/Source/SPAppleScriptSupport.m
deleted file mode 100644
index 17b9e436..00000000
--- a/Source/SPAppleScriptSupport.m
+++ /dev/null
@@ -1,128 +0,0 @@
-//
-// SPAppleScriptSupport.m
-// sequel-pro
-//
-// Created by Stuart Connolly (stuconnolly.com) on October 14, 2010.
-// Copyright (c) 2010 Stuart Connolly. All rights reserved.
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
-// More info at <https://github.com/sequelpro/sequelpro>
-
-#import "SPAppleScriptSupport.h"
-#import "SPWindowController.h"
-#import "SPAppController.h"
-#import "SPPrintController.h"
-#import "SPDatabaseDocument.h"
-#import "SPWindowManagement.h"
-
-@implementation SPAppController (SPAppleScriptSupport)
-
-/**
- * Is needed to interact with AppleScript for set/get internal SP variables
- */
-- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key
-{
- NSLog(@"Not yet implemented.");
-
- return NO;
-}
-
-/**
- * AppleScript call to get the available documents.
- */
-- (NSArray *)orderedDocuments
-{
- NSMutableArray *orderedDocuments = [NSMutableArray array];
-
- for (NSWindow *aWindow in [self orderedWindows])
- {
- if ([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) {
- [orderedDocuments addObjectsFromArray:[[aWindow windowController] documents]];
- }
- }
-
- return orderedDocuments;
-}
-
-/**
- * AppleScript support for 'make new document'.
- *
- * TODO: following tab support this has been disabled - need to discuss reimplmenting vs syntax.
- */
-- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc
-{
- [self newWindow:self];
-
- // Set autoconnection if appropriate
- if ([[NSUserDefaults standardUserDefaults] boolForKey:SPAutoConnectToDefault]) {
- [[self frontDocument] connect];
- }
-}
-
-/**
- * AppleScript call to get the available windows.
- */
-- (NSArray *)orderedWindows
-{
- return [NSApp orderedWindows];
-}
-
-/**
- * AppleScript handler to quit Sequel Pro
- *
- * This handler is required to allow termination via the Dock or AppleScript event after activating it using AppleScript
- */
-- (id)handleQuitScriptCommand:(NSScriptCommand *)command
-{
- [NSApp terminate:self];
-
- return nil;
-}
-
-/**
- * AppleScript open handler
- *
- * This handler is required to catch the 'open' command if no argument was passed which would cause a crash.
- */
-- (id)handleOpenScriptCommand:(NSScriptCommand *)command
-{
- return nil;
-}
-
-/**
- * AppleScript print handler
- *
- * This handler prints the active view.
- */
-- (id)handlePrintScriptCommand:(NSScriptCommand *)command
-{
- SPDatabaseDocument *frontDoc = [self frontDocument];
-
- if (frontDoc && ![frontDoc isWorking] && ![[frontDoc connectionID] isEqualToString:@"_"]) {
- [frontDoc startPrintDocumentOperation];
- }
-
- return nil;
-}
-
-@end
diff --git a/Source/SPContentFilterManager.m b/Source/SPContentFilterManager.m
index 5dc0c48b..512e19d8 100644
--- a/Source/SPContentFilterManager.m
+++ b/Source/SPContentFilterManager.m
@@ -38,7 +38,6 @@
#import "SPConnectionController.h"
#import "SPSplitView.h"
#import "SPAppController.h"
-#import "SPAppleScriptSupport.h"
static NSString *SPExportFilterAction = @"SPExportFilter";
diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m
index 2a10b900..a82632d0 100644
--- a/Source/SPDatabaseDocument.m
+++ b/Source/SPDatabaseDocument.m
@@ -55,7 +55,6 @@
#import "SPDatabaseData.h"
#import "SPDatabaseStructure.h"
#import "SPAppController.h"
-#import "SPWindowManagement.h"
#import "SPExtendedTableInfo.h"
#import "SPHistoryController.h"
#import "SPPreferenceController.h"
diff --git a/Source/SPNavigatorController.m b/Source/SPNavigatorController.m
index 108727fb..93b4f13d 100644
--- a/Source/SPNavigatorController.m
+++ b/Source/SPNavigatorController.m
@@ -39,7 +39,6 @@
#import "SPLogger.h"
#import "SPTooltip.h"
#import "SPAppController.h"
-#import "SPAppleScriptSupport.h"
#import "SPDatabaseViewController.h"
#import "SPDatabaseStructure.h"
#import "SPThreadAdditions.h"
diff --git a/Source/SPQueryDocumentsController.m b/Source/SPQueryDocumentsController.m
index a9602b61..e4689c41 100644
--- a/Source/SPQueryDocumentsController.m
+++ b/Source/SPQueryDocumentsController.m
@@ -31,7 +31,6 @@
#import "SPQueryDocumentsController.h"
#import "SPCustomQuery.h"
#import "SPAppController.h"
-#import "SPAppleScriptSupport.h"
@implementation SPQueryController (SPQueryDocumentsController)
diff --git a/Source/SPQueryFavoriteManager.m b/Source/SPQueryFavoriteManager.m
index 2f88bbcf..76fdf288 100644
--- a/Source/SPQueryFavoriteManager.m
+++ b/Source/SPQueryFavoriteManager.m
@@ -39,7 +39,6 @@
#import "SPTextView.h"
#import "SPSplitView.h"
#import "SPAppController.h"
-#import "SPAppleScriptSupport.h"
#define SP_MULTIPLE_SELECTION_PLACEHOLDER_STRING NSLocalizedString(@"[multiple selection]", @"[multiple selection]")
#define SP_NO_SELECTION_PLACEHOLDER_STRING NSLocalizedString(@"[no selection]", @"[no selection]")
diff --git a/Source/SPTablesList.m b/Source/SPTablesList.m
index 34d1dfcb..6096b493 100644
--- a/Source/SPTablesList.m
+++ b/Source/SPTablesList.m
@@ -52,7 +52,6 @@
#import "SPThreadAdditions.h"
#import "SPFunctions.h"
#import "SPCharsetCollationHelper.h"
-#import "SPWindowManagement.h"
#import <SPMySQL/SPMySQL.h>
diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m
index 88096a8f..a81dcd4d 100644
--- a/Source/SPTextViewAdditions.m
+++ b/Source/SPTextViewAdditions.m
@@ -36,7 +36,6 @@
#import "SPCustomQuery.h"
#ifndef SP_CODA /* headers */
#import "SPAppController.h"
-#import "SPWindowManagement.h"
#endif
#import "SPFieldEditorController.h"
#import "SPTextView.h"
diff --git a/Source/SPWindowManagement.h b/Source/SPWindowManagement.h
deleted file mode 100644
index 4d32d6d4..00000000
--- a/Source/SPWindowManagement.h
+++ /dev/null
@@ -1,55 +0,0 @@
-//
-// SPWindowManagement.h
-// sequel-pro
-//
-// Created by Stuart Connolly (stuconnolly.com) on July 7, 2012.
-// Copyright (c) 2012 Stuart Connolly. All rights reserved.
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-//
-// More info at <https://github.com/sequelpro/sequelpro>
-
-#import "SPAppController.h"
-
-@class SPWindowController;
-
-/**
- * @category SPWindowManagement SPWindowManagement.h
- *
- * @author Stuart Connolly http://stuconnolly.com/
- *
- * Contains all of the main window management methods.
- */
-@interface SPAppController (SPWindowManagement)
-
-- (IBAction)newWindow:(id)sender;
-- (IBAction)newTab:(id)sender;
-- (IBAction)duplicateTab:(id)sender;
-
-- (SPWindowController *)newWindow;
-- (SPDatabaseDocument *)makeNewConnectionTabOrWindow;
-- (SPWindowController *)frontController;
-
-- (NSWindow *)frontDocumentWindow;
-- (void)tabDragStarted:(id)sender;
-
-@end
diff --git a/Source/SPWindowManagement.m b/Source/SPWindowManagement.m
deleted file mode 100644
index 6225c52f..00000000
--- a/Source/SPWindowManagement.m
+++ /dev/null
@@ -1,182 +0,0 @@
-//
-// SPWindowManagement.m
-// sequel-pro
-//
-// Created by Stuart Connolly (stuconnolly.com) on July 7, 2012.
-// Copyright (c) 2012 Stuart Connolly. All rights reserved.
-//
-// Permission is hereby granted, free of charge, to any person
-// obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without
-// restriction, including without limitation the rights to use,
-// copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following
-// conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-// OTHER DEALINGS IN THE SOFTWARE.
-
-#import "SPWindowManagement.h"
-#import "SPWindowController.h"
-#import "SPDatabaseDocument.h"
-
-@implementation SPAppController (SPWindowManagement)
-
-
-- (IBAction)newWindow:(id)sender
-{
- [self newWindow];
-}
-
-/**
- * Create a new window, containing a single tab.
- */
-- (SPWindowController *)newWindow
-{
- static NSPoint cascadeLocation = {.x = 0, .y = 0};
-
- // Create a new window controller, and set up a new connection view within it.
- SPWindowController *newWindowController = [[SPWindowController alloc] initWithWindowNibName:@"MainWindow"];
- NSWindow *newWindow = [newWindowController window];
-
- // Cascading defaults to on - retrieve the window origin automatically assigned by cascading,
- // and convert to a top left point.
- NSPoint topLeftPoint = [newWindow frame].origin;
- topLeftPoint.y += [newWindow frame].size.height;
-
- // The first window should use autosaving; subsequent windows should cascade.
- // So attempt to set the frame autosave name; this will succeed for the very
- // first window, and fail for others.
- BOOL usedAutosave = [newWindow setFrameAutosaveName:@"DBView"];
-
- if (!usedAutosave) {
- [newWindow setFrameUsingName:@"DBView"];
- }
-
- // Add the connection view
- [newWindowController addNewConnection];
-
- // Cascade according to the statically stored cascade location.
- cascadeLocation = [newWindow cascadeTopLeftFromPoint:cascadeLocation];
-
- // Set the window controller as the window's delegate
- [newWindow setDelegate:newWindowController];
-
- // Show the window, and perform frontmost tasks again once the window has drawn
- [newWindowController showWindow:self];
- [[newWindowController selectedTableDocument] didBecomeActiveTabInWindow];
-
- return newWindowController;
-}
-
-/**
- * Create a new tab in the frontmost window.
- */
-- (IBAction)newTab:(id)sender
-{
- SPWindowController *frontController = [self frontController];
-
- // If no window was found, create a new one
- if (!frontController) {
- [self newWindow:self];
- }
- else {
- if ([[frontController window] isMiniaturized]) {
- [[frontController window] deminiaturize:self];
- }
-
- [frontController addNewConnection:self];
- }
-}
-
-- (SPDatabaseDocument *)makeNewConnectionTabOrWindow
-{
- SPWindowController *frontController = [self frontController];
-
- SPDatabaseDocument *frontDocument;
- // If no window was found or the front most window has no tabs, create a new one
- if (!frontController || [[frontController valueForKeyPath:@"tabView"] numberOfTabViewItems] == 1) {
- frontController = [self newWindow];
- frontDocument = [frontController selectedTableDocument];
- }
- // Open the spf file in a new tab if the tab bar is visible
- else {
- if ([[frontController window] isMiniaturized]) [[frontController window] deminiaturize:self];
- frontDocument = [frontController addNewConnection];
- }
-
- return frontDocument;
-}
-
-/**
- * Duplicate the current connection tab
- */
-- (IBAction)duplicateTab:(id)sender
-{
- SPDatabaseDocument *theFrontDocument = [self frontDocument];
-
- if (!theFrontDocument) return [self newTab:sender];
-
- // Add a new tab to the window
- if ([[self frontDocumentWindow] isMiniaturized]) {
- [[self frontDocumentWindow] deminiaturize:self];
- }
-
- SPDatabaseDocument *newConnection = [[self frontController] addNewConnection];
-
- // Get the state of the previously-frontmost document
- NSDictionary *allStateDetails = @{
- @"connection" : @YES,
- @"history" : @YES,
- @"session" : @YES,
- @"query" : @YES,
- @"password" : @YES
- };
-
- NSMutableDictionary *frontState = [NSMutableDictionary dictionaryWithDictionary:[theFrontDocument stateIncludingDetails:allStateDetails]];
-
- // Ensure it's set to autoconnect
- [frontState setObject:@YES forKey:@"auto_connect"];
-
- // Set the connection on the new tab
- [newConnection setState:frontState];
-}
-
-/**
- * Retrieve the frontmost document window; returns nil if not found.
- */
-- (NSWindow *)frontDocumentWindow
-{
- return [[self frontController] window];
-}
-
-- (SPWindowController *)frontController
-{
- for (NSWindow *aWindow in [NSApp orderedWindows]) {
- id ctr = [aWindow windowController];
- if ([ctr isMemberOfClass:[SPWindowController class]]) {
- return ctr;
- }
- }
- return nil;
-}
-
-/**
- * When tab drags start, bring all the windows in front of other applications.
- */
-- (void)tabDragStarted:(id)sender
-{
- [NSApp arrangeInFront:self];
-}
-
-@end