00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024 @import <Foundation/CPString.j>
00025
00026 @import "CPResponder.j"
00027 @import "CPWindow.j"
00028 @import "CPDocument.j"
00029
00030 #include "Platform/Platform.h"
00031
00032
00042 @implementation CPWindowController : CPResponder
00043 {
00044 id _owner;
00045 CPWindow _window;
00046 CPDocument _document;
00047 CPString _windowCibName;
00048 }
00049
00055 - (id)initWithWindow:(CPWindow)aWindow
00056 {
00057 self = [super init];
00058
00059 if (self)
00060 {
00061 [self setWindow:aWindow];
00062
00063 [self setNextResponder:CPApp];
00064 }
00065
00066 return self;
00067 }
00068
00074 - (id)initWithWindowCibName:(CPString)aWindowCibName
00075 {
00076 return [self initWithWindowCibName:aWindowCibName owner:self];
00077 }
00078
00085 - (id)initWithWindowCibName:(CPString)aWindowCibName owner:(id)anOwner
00086 {
00087 self = [super init];
00088
00089 if (self)
00090 {
00091 _owner = anOwner;
00092 _windowCibName = aWindowCibName;
00093
00094 [self setNextResponder:CPApp];
00095 }
00096
00097 return self;
00098 }
00099
00103 - (void)loadWindow
00104 {
00105 [self windowWillLoad];
00106
00107 [self setWindow:CPApp._keyWindow = [[CPWindow alloc] initWithContentRect:CPRectMakeZero() styleMask:CPBorderlessBridgeWindowMask|CPTitledWindowMask|CPClosableWindowMask|CPResizableWindowMask]];
00108
00109 [self windowDidLoad];
00110 }
00111
00116 - (CFAction)showWindow:(id)aSender
00117 {
00118 var theWindow = [self window];
00119
00120 if ([theWindow respondsToSelector:@selector(becomesKeyOnlyIfNeeded)] && [theWindow becomesKeyOnlyIfNeeded])
00121 [theWindow orderFront:aSender];
00122 else
00123 [theWindow makeKeyAndOrderFront:aSender];
00124 }
00125
00130 - (BOOL)isWindowLoaded
00131 {
00132 return _window;
00133 }
00134
00138 - (CPWindow)window
00139 {
00140 if (!_window)
00141 [self loadWindow];
00142
00143 return _window;
00144 }
00145
00150 - (void)setWindow:(CPWindow)aWindow
00151 {
00152 _window = aWindow;
00153
00154 [_window setWindowController:self];
00155 [_window setNextResponder:self];
00156 }
00157
00161 - (void)windowDidLoad
00162 {
00163 [_document windowControllerDidLoadNib:self];
00164
00165 [self synchronizeWindowTitleWithDocumentName];
00166 }
00167
00171 - (void)windowWillLoad
00172 {
00173 [_document windowControllerWillLoadNib:self];
00174 }
00175
00180 - (void)setDocument:(CPDocument)aDocument
00181 {
00182 if (_document == aDocument)
00183 return;
00184
00185 var defaultCenter = [CPNotificationCenter defaultCenter];
00186
00187 if (_document)
00188 {
00189 [defaultCenter removeObserver:self
00190 name:CPDocumentWillSaveNotification
00191 object:_document];
00192
00193 [defaultCenter removeObserver:self
00194 name:CPDocumentDidSaveNotification
00195 object:_document];
00196
00197 [defaultCenter removeObserver:self
00198 name:CPDocumentDidFailToSaveNotification
00199 object:_document];
00200 }
00201
00202 _document = aDocument;
00203
00204 if (_document)
00205 {
00206 [defaultCenter addObserver:self
00207 selector:@selector(_documentWillSave:)
00208 name:CPDocumentWillSaveNotification
00209 object:_document];
00210
00211 [defaultCenter addObserver:self
00212 selector:@selector(_documentDidSave:)
00213 name:CPDocumentDidSaveNotification
00214 object:_document];
00215
00216 [defaultCenter addObserver:self
00217 selector:@selector(_documentDidFailToSave:)
00218 name:CPDocumentDidFailToSaveNotification
00219 object:_document];
00220
00221 [self setDocumentEdited:[_document isDocumentEdited]];
00222 }
00223
00224 [self synchronizeWindowTitleWithDocumentName];
00225 }
00226
00227
00228 - (void)_documentWillSave:(CPNotification)aNotification
00229 {
00230 [[self window] setDocumentSaving:YES];
00231 }
00232
00233
00234 - (void)_documentDidSave:(CPNotification)aNotification
00235 {
00236 [[self window] setDocumentSaving:NO];
00237 }
00238
00239
00240 - (void)_documentDidFailToSave:(CPNotification)aNotification
00241 {
00242 [[self window] setDocumentSaving:NO];
00243 }
00244
00248 - (CPDocument)document
00249 {
00250 return _document;
00251 }
00252
00257 - (void)setDocumentEdited:(BOOL)isEdited
00258 {
00259 [[self window] setDocumentEdited:isEdited];
00260 }
00261
00262
00263
00267 - (void)synchronizeWindowTitleWithDocumentName
00268 {
00269 if (!_document || !_window)
00270 return;
00271
00272
00273 [_window setTitle:[self windowTitleForDocumentDisplayName:[_document displayName]]];
00274 }
00275
00280 - (CPString)windowTitleForDocumentDisplayName:(CPString)aDisplayName
00281 {
00282 return aDisplayName;
00283 }
00284
00285 @end