|
47 | 47 | #import "SPFavoritesController.h" |
48 | 48 | #import "SPEditorTokens.h" |
49 | 49 | #import "SPBundleCommandRunner.h" |
50 | | -#import "SPWindowManagement.h" |
51 | 50 | #import "SPCopyTable.h" |
52 | 51 | #import "SPSyntaxParser.h" |
53 | 52 | #import "SPOSInfo.h" |
@@ -2362,6 +2361,244 @@ - (void)_copyDefaultThemes |
2362 | 2361 | } |
2363 | 2362 | } |
2364 | 2363 |
|
| 2364 | +#pragma mark - SPAppleScriptSupport |
| 2365 | + |
| 2366 | +/** |
| 2367 | + * Is needed to interact with AppleScript for set/get internal SP variables |
| 2368 | + */ |
| 2369 | +- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key |
| 2370 | +{ |
| 2371 | + NSLog(@"Not yet implemented."); |
| 2372 | + |
| 2373 | + return NO; |
| 2374 | +} |
| 2375 | + |
| 2376 | +/** |
| 2377 | + * AppleScript call to get the available documents. |
| 2378 | + */ |
| 2379 | +- (NSArray *)orderedDocuments |
| 2380 | +{ |
| 2381 | + NSMutableArray *orderedDocuments = [NSMutableArray array]; |
| 2382 | + |
| 2383 | + for (NSWindow *aWindow in [self orderedWindows]) |
| 2384 | + { |
| 2385 | + if ([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) { |
| 2386 | + [orderedDocuments addObjectsFromArray:[[aWindow windowController] documents]]; |
| 2387 | + } |
| 2388 | + } |
| 2389 | + |
| 2390 | + return orderedDocuments; |
| 2391 | +} |
| 2392 | + |
| 2393 | +/** |
| 2394 | + * AppleScript support for 'make new document'. |
| 2395 | + * |
| 2396 | + * TODO: following tab support this has been disabled - need to discuss reimplmenting vs syntax. |
| 2397 | + */ |
| 2398 | +- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc |
| 2399 | +{ |
| 2400 | + [self newWindow:self]; |
| 2401 | + |
| 2402 | + // Set autoconnection if appropriate |
| 2403 | + if ([[NSUserDefaults standardUserDefaults] boolForKey:SPAutoConnectToDefault]) { |
| 2404 | + [[self frontDocument] connect]; |
| 2405 | + } |
| 2406 | +} |
| 2407 | + |
| 2408 | +/** |
| 2409 | + * AppleScript call to get the available windows. |
| 2410 | + */ |
| 2411 | +- (NSArray *)orderedWindows |
| 2412 | +{ |
| 2413 | + return [NSApp orderedWindows]; |
| 2414 | +} |
| 2415 | + |
| 2416 | +/** |
| 2417 | + * AppleScript handler to quit Sequel Pro |
| 2418 | + * |
| 2419 | + * This handler is required to allow termination via the Dock or AppleScript event after activating it using AppleScript |
| 2420 | + */ |
| 2421 | +- (id)handleQuitScriptCommand:(NSScriptCommand *)command |
| 2422 | +{ |
| 2423 | + [NSApp terminate:self]; |
| 2424 | + |
| 2425 | + return nil; |
| 2426 | +} |
| 2427 | + |
| 2428 | +/** |
| 2429 | + * AppleScript open handler |
| 2430 | + * |
| 2431 | + * This handler is required to catch the 'open' command if no argument was passed which would cause a crash. |
| 2432 | + */ |
| 2433 | +- (id)handleOpenScriptCommand:(NSScriptCommand *)command |
| 2434 | +{ |
| 2435 | + return nil; |
| 2436 | +} |
| 2437 | + |
| 2438 | +/** |
| 2439 | + * AppleScript print handler |
| 2440 | + * |
| 2441 | + * This handler prints the active view. |
| 2442 | + */ |
| 2443 | +- (id)handlePrintScriptCommand:(NSScriptCommand *)command |
| 2444 | +{ |
| 2445 | + SPDatabaseDocument *frontDoc = [self frontDocument]; |
| 2446 | + |
| 2447 | + if (frontDoc && ![frontDoc isWorking] && ![[frontDoc connectionID] isEqualToString:@"_"]) { |
| 2448 | + [frontDoc startPrintDocumentOperation]; |
| 2449 | + } |
| 2450 | + |
| 2451 | + return nil; |
| 2452 | +} |
| 2453 | + |
| 2454 | +#pragma mark - SPWindowManagement |
| 2455 | + |
| 2456 | +- (IBAction)newWindow:(id)sender |
| 2457 | +{ |
| 2458 | + [self newWindow]; |
| 2459 | +} |
| 2460 | + |
| 2461 | +/** |
| 2462 | + * Create a new window, containing a single tab. |
| 2463 | + */ |
| 2464 | +- (SPWindowController *)newWindow |
| 2465 | +{ |
| 2466 | + static NSPoint cascadeLocation = {.x = 0, .y = 0}; |
| 2467 | + |
| 2468 | + // Create a new window controller, and set up a new connection view within it. |
| 2469 | + SPWindowController *newWindowController = [[SPWindowController alloc] initWithWindowNibName:@"MainWindow"]; |
| 2470 | + NSWindow *newWindow = [newWindowController window]; |
| 2471 | + |
| 2472 | + // Cascading defaults to on - retrieve the window origin automatically assigned by cascading, |
| 2473 | + // and convert to a top left point. |
| 2474 | + NSPoint topLeftPoint = [newWindow frame].origin; |
| 2475 | + topLeftPoint.y += [newWindow frame].size.height; |
| 2476 | + |
| 2477 | + // The first window should use autosaving; subsequent windows should cascade. |
| 2478 | + // So attempt to set the frame autosave name; this will succeed for the very |
| 2479 | + // first window, and fail for others. |
| 2480 | + BOOL usedAutosave = [newWindow setFrameAutosaveName:@"DBView"]; |
| 2481 | + |
| 2482 | + if (!usedAutosave) { |
| 2483 | + [newWindow setFrameUsingName:@"DBView"]; |
| 2484 | + } |
| 2485 | + |
| 2486 | + // Add the connection view |
| 2487 | + [newWindowController addNewConnection]; |
| 2488 | + |
| 2489 | + // Cascade according to the statically stored cascade location. |
| 2490 | + cascadeLocation = [newWindow cascadeTopLeftFromPoint:cascadeLocation]; |
| 2491 | + |
| 2492 | + // Set the window controller as the window's delegate |
| 2493 | + [newWindow setDelegate:newWindowController]; |
| 2494 | + |
| 2495 | + // Show the window, and perform frontmost tasks again once the window has drawn |
| 2496 | + [newWindowController showWindow:self]; |
| 2497 | + [[newWindowController selectedTableDocument] didBecomeActiveTabInWindow]; |
| 2498 | + |
| 2499 | + return newWindowController; |
| 2500 | +} |
| 2501 | + |
| 2502 | +/** |
| 2503 | + * Create a new tab in the frontmost window. |
| 2504 | + */ |
| 2505 | +- (IBAction)newTab:(id)sender |
| 2506 | +{ |
| 2507 | + SPWindowController *frontController = [self frontController]; |
| 2508 | + |
| 2509 | + // If no window was found, create a new one |
| 2510 | + if (!frontController) { |
| 2511 | + [self newWindow:self]; |
| 2512 | + } |
| 2513 | + else { |
| 2514 | + if ([[frontController window] isMiniaturized]) { |
| 2515 | + [[frontController window] deminiaturize:self]; |
| 2516 | + } |
| 2517 | + |
| 2518 | + [frontController addNewConnection:self]; |
| 2519 | + } |
| 2520 | +} |
| 2521 | + |
| 2522 | +- (SPDatabaseDocument *)makeNewConnectionTabOrWindow |
| 2523 | +{ |
| 2524 | + SPWindowController *frontController = [self frontController]; |
| 2525 | + |
| 2526 | + SPDatabaseDocument *frontDocument; |
| 2527 | + // If no window was found or the front most window has no tabs, create a new one |
| 2528 | + if (!frontController || [[frontController valueForKeyPath:@"tabView"] numberOfTabViewItems] == 1) { |
| 2529 | + frontController = [self newWindow]; |
| 2530 | + frontDocument = [frontController selectedTableDocument]; |
| 2531 | + } |
| 2532 | + // Open the spf file in a new tab if the tab bar is visible |
| 2533 | + else { |
| 2534 | + if ([[frontController window] isMiniaturized]) [[frontController window] deminiaturize:self]; |
| 2535 | + frontDocument = [frontController addNewConnection]; |
| 2536 | + } |
| 2537 | + |
| 2538 | + return frontDocument; |
| 2539 | +} |
| 2540 | + |
| 2541 | +/** |
| 2542 | + * Duplicate the current connection tab |
| 2543 | + */ |
| 2544 | +- (IBAction)duplicateTab:(id)sender |
| 2545 | +{ |
| 2546 | + SPDatabaseDocument *theFrontDocument = [self frontDocument]; |
| 2547 | + |
| 2548 | + if (!theFrontDocument) return [self newTab:sender]; |
| 2549 | + |
| 2550 | + // Add a new tab to the window |
| 2551 | + if ([[self frontDocumentWindow] isMiniaturized]) { |
| 2552 | + [[self frontDocumentWindow] deminiaturize:self]; |
| 2553 | + } |
| 2554 | + |
| 2555 | + SPDatabaseDocument *newConnection = [[self frontController] addNewConnection]; |
| 2556 | + |
| 2557 | + // Get the state of the previously-frontmost document |
| 2558 | + NSDictionary *allStateDetails = @{ |
| 2559 | + @"connection" : @YES, |
| 2560 | + @"history" : @YES, |
| 2561 | + @"session" : @YES, |
| 2562 | + @"query" : @YES, |
| 2563 | + @"password" : @YES |
| 2564 | + }; |
| 2565 | + |
| 2566 | + NSMutableDictionary *frontState = [NSMutableDictionary dictionaryWithDictionary:[theFrontDocument stateIncludingDetails:allStateDetails]]; |
| 2567 | + |
| 2568 | + // Ensure it's set to autoconnect |
| 2569 | + [frontState setObject:@YES forKey:@"auto_connect"]; |
| 2570 | + |
| 2571 | + // Set the connection on the new tab |
| 2572 | + [newConnection setState:frontState]; |
| 2573 | +} |
| 2574 | + |
| 2575 | +/** |
| 2576 | + * Retrieve the frontmost document window; returns nil if not found. |
| 2577 | + */ |
| 2578 | +- (NSWindow *)frontDocumentWindow |
| 2579 | +{ |
| 2580 | + return [[self frontController] window]; |
| 2581 | +} |
| 2582 | + |
| 2583 | +- (SPWindowController *)frontController |
| 2584 | +{ |
| 2585 | + for (NSWindow *aWindow in [NSApp orderedWindows]) { |
| 2586 | + id ctr = [aWindow windowController]; |
| 2587 | + if ([ctr isMemberOfClass:[SPWindowController class]]) { |
| 2588 | + return ctr; |
| 2589 | + } |
| 2590 | + } |
| 2591 | + return nil; |
| 2592 | +} |
| 2593 | + |
| 2594 | +/** |
| 2595 | + * When tab drags start, bring all the windows in front of other applications. |
| 2596 | + */ |
| 2597 | +- (void)tabDragStarted:(id)sender |
| 2598 | +{ |
| 2599 | + [NSApp arrangeInFront:self]; |
| 2600 | +} |
| 2601 | + |
2365 | 2602 | #pragma mark - |
2366 | 2603 |
|
2367 | 2604 | - (void)dealloc |
|
0 commit comments