Remove use of deprecated stringWithCString
[GitX.git] / PBGitWindowController.m
blob9f1a93ee1a1197e5197d3695110d3636ef743edc
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 #pragma mark -
126 #pragma mark Toolbar Delegates
128 - (void) useToolbar:(NSToolbar *)toolbar
130         NSSegmentedControl *item = nil;
131         for (NSToolbarItem *toolbarItem in [toolbar items]) {
132                 if ([[toolbarItem view] isKindOfClass:[NSSegmentedControl class]]) {
133                         item = (NSSegmentedControl *)[toolbarItem view];
134                         break;
135                 }
136         }
137         [item bind:@"selectedIndex" toObject:self withKeyPath:@"selectedViewIndex" options:0];
138         [item setEnabled: ![repository isBareRepository]];
140         [self.window setToolbar:toolbar];
143 @end