Merge commit 'origin/jg/tree_context'
[GitX.git] / PBGitWindowController.m
blobb8267344943dc8c6c7fb2b130c308a6792d7b805
1 //
2 //  PBDetailController.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 16-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBGitWindowController.h"
10 #import "PBGitHistoryController.h"
11 #import "PBGitCommitController.h"
13 @implementation PBGitWindowController
16 @synthesize repository, viewController, selectedViewIndex;
18 - (id)initWithRepository:(PBGitRepository*)theRepository displayDefault:(BOOL)displayDefault
20         if(self = [self initWithWindowNibName:@"RepositoryWindow"])
21         {
22                 self.repository = theRepository;
23                 [self showWindow:nil];
24         }
25         
26         if (displayDefault) {
27                 self.selectedViewIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedViewIndex"];
28         } else {
29                 self.selectedViewIndex = -1;
30         }
32         return self;
35 - (void)windowWillClose:(NSNotification *)notification
37         NSLog(@"Window will close!");
38         if (historyViewController)
39                 [historyViewController removeView];
40         if (commitViewController)
41                 [commitViewController removeView];
44 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
46         if ([menuItem action] == @selector(showCommitView:) || [menuItem action] == @selector(showHistoryView:)) {
47                 return ![repository isBareRepository];
48         }
49         return YES;
52 - (void) setSelectedViewIndex: (int) i
54         [self changeViewController: i];
57 - (void)changeViewController:(NSInteger)whichViewTag
59         [self willChangeValueForKey:@"viewController"];
61         if (viewController != nil)
62                 [[viewController view] removeFromSuperview];
64         if ([repository isBareRepository]) {    // in bare repository we don't want to view commit
65                 whichViewTag = 0;               // even if it was selected by default
66         }
68         // Set our default here because we might have changed it (based on bare repo) before
69         selectedViewIndex = whichViewTag;
70         [[NSUserDefaults standardUserDefaults] setInteger:whichViewTag forKey:@"selectedViewIndex"];
72         switch (whichViewTag)
73         {
74                 case 0: // swap in the "CustomImageViewController - NSImageView"
75                         if (!historyViewController)
76                                 historyViewController = [[PBGitHistoryController alloc] initWithRepository:repository superController:self];
77                         else
78                                 [historyViewController updateView];
79                         viewController = historyViewController;
80                         break;
81                 case 1:
82                         if (!commitViewController)
83                                 commitViewController = [[PBGitCommitController alloc] initWithRepository:repository superController:self];
84                         else
85                                 [commitViewController updateView];
87                         viewController = commitViewController;
88                         break;
89         }
91         // make sure we automatically resize the controller's view to the current window size
92         [[viewController view] setFrame: [contentView bounds]];
93         
94         //// embed the current view to our host view
95         [contentView addSubview: [viewController view]];
97         [self useToolbar: [viewController viewToolbar]];
99         // Allow the viewcontroller to catch actions
100         [self setNextResponder: viewController];
101         [self didChangeValueForKey:@"viewController"];  // this will trigger the NSTextField's value binding to change
105 - (void)awakeFromNib
107         [[self window] setDelegate:self];
108         [[self window] setAutorecalculatesContentBorderThickness:NO forEdge:NSMinYEdge];
109         [[self window] setContentBorderThickness:35.0f forEdge:NSMinYEdge];
110         [self showHistoryView:nil];
113 - (void) showCommitView:(id)sender
115         if (self.selectedViewIndex != 1)
116                 self.selectedViewIndex = 1;
119 - (void) showHistoryView:(id)sender
121         if (self.selectedViewIndex != 0)
122                 self.selectedViewIndex = 0;
125 - (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText
127         [[NSAlert alertWithMessageText:messageText
128                          defaultButton:nil
129                        alternateButton:nil
130                            otherButton:nil
131              informativeTextWithFormat:infoText] beginSheetModalForWindow: [self window] modalDelegate:self didEndSelector:nil contextInfo:nil];
134 - (void)showErrorSheet:(NSError *)error
136         [[NSAlert alertWithError:error] beginSheetModalForWindow: [self window] modalDelegate:self didEndSelector:nil contextInfo:nil];
140 #pragma mark -
141 #pragma mark Toolbar Delegates
143 - (void) useToolbar:(NSToolbar *)toolbar
145         NSSegmentedControl *item = nil;
146         for (NSToolbarItem *toolbarItem in [toolbar items]) {
147                 if ([[toolbarItem view] isKindOfClass:[NSSegmentedControl class]]) {
148                         item = (NSSegmentedControl *)[toolbarItem view];
149                         break;
150                 }
151         }
152         [item bind:@"selectedIndex" toObject:self withKeyPath:@"selectedViewIndex" options:0];
153         [item setEnabled: ![repository isBareRepository]];
155         [self.window setToolbar:toolbar];
158 @end