forked from KosmicTask/Fragaria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MGSPreferencesController.m
159 lines (112 loc) · 3.56 KB
/
MGSPreferencesController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// MGSPreferencesController.m
// KosmicEditor
//
// Created by Jonathan on 30/04/2010.
// Copyright 2010 mugginsoft.com. All rights reserved.
//
#import "MGSFragariaFramework.h"
@interface MGSPreferencesController()
- (BOOL)commitEditingAndDiscard:(BOOL)discard;
@end
@implementation MGSPreferencesController
#pragma mark -
#pragma mark Instance methods
/*
-initWithWindow: is the designated initializer for NSWindowController.
*/
- (id)initWithWindow:(NSWindow *)window
{
#pragma unused(window)
self = [super initWithWindow:window];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:window];
}
return self;
}
/*
- showWindow:
*/
- (void)showWindow:(id)sender
{
// load view controllers
textEditingPrefsViewController = [MGSFragariaPreferences sharedInstance].textEditingPrefsViewController;
fontsAndColoursPrefsViewController = [MGSFragariaPreferences sharedInstance].fontsAndColoursPrefsViewController;
[super showWindow:sender];
}
/*
- setupToolbar
*/
- (void)setupToolbar
{
generalIdentifier = NSLocalizedString(@"General", @"Preferences tab name");
textIdentifier = NSLocalizedString(@"Text Editing", @"Preferences tab name");
fontIdentifier = NSLocalizedString(@"Fonts & Colours", @"Preferences tab name");
[self addView:generalView label:generalIdentifier];
[self addView:textEditingPrefsViewController.view label:textIdentifier image:[NSImage imageNamed:@"General.png"]];
[self addView:fontsAndColoursPrefsViewController.view label:fontIdentifier image:[NSImage imageNamed:@"General.png"]];
}
/*
- changeFont:
*/
- (void)changeFont:(id)sender
{
/* NSFontManager will send this method up the responder chain */
[fontsAndColoursPrefsViewController changeFont:sender];
}
/*
- revertToStandardSettings:
*/
- (void)revertToStandardSettings:(id)sender
{
[[MGSFragariaPreferences sharedInstance] revertToStandardSettings:sender];
}
/*
- commitEditingAndDiscard:
*/
- (BOOL)commitEditingAndDiscard:(BOOL)discard
{
BOOL commit = YES;
if ([toolbarIdentifier isEqual:textIdentifier]) {
if (![textEditingPrefsViewController commitEditingAndDiscard:discard]) {
commit = NO;
}
} else if ([toolbarIdentifier isEqual:fontIdentifier]) {
if (![fontsAndColoursPrefsViewController commitEditingAndDiscard:discard]) {
commit = NO;
}
} else {
// commit edits, discarding changes on error
if (![[NSUserDefaultsController sharedUserDefaultsController] commitEditing]) {
if (discard) [[NSUserDefaultsController sharedUserDefaultsController] discardEditing];
commit = NO;
}
}
return commit;
}
/*
- windowWillClose
*/
- (void)windowWillClose:(NSNotification *)notification
{
if ([notification object] != [self window]) {
return;
}
// commit editing, discard any uncommitted changes
[self commitEditingAndDiscard:YES];
}
/*
- displayViewForIdentifier:animate:
*/
- (void)displayViewForIdentifier:(NSString *)identifier animate:(BOOL)animate
{
// look for uncommitted changes
if (![self commitEditingAndDiscard:NO] && toolbarIdentifier) {
// we have uncommited changes, reselect the tool bar item
[[[self window] toolbar] setSelectedItemIdentifier:toolbarIdentifier];
} else {
[super displayViewForIdentifier:identifier animate:animate];
}
toolbarIdentifier = identifier;
}
@end