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
00040 @implementation CPWindowController : CPResponder
00041 {
00042 id _owner;
00043 CPWindow _window;
00044 CPDocument _document;
00045 CPString _windowCibName;
00046 }
00047
00053 - (id)initWithWindow:(CPWindow)aWindow
00054 {
00055 self = [super init];
00056
00057 if (self)
00058 {
00059 [self setWindow:aWindow];
00060
00061 [self setNextResponder:CPApp];
00062 }
00063
00064 return self;
00065 }
00066
00072 - (id)initWithWindowCibName:(CPString)aWindowCibName
00073 {
00074 return [self initWithWindowCibName:aWindowCibName owner:self];
00075 }
00076
00083 - (id)initWithWindowCibName:(CPString)aWindowCibName owner:(id)anOwner
00084 {
00085 self = [super init];
00086
00087 if (self)
00088 {
00089 _owner = anOwner;
00090 _windowCibName = aWindowCibName;
00091
00092 [self setNextResponder:CPApp];
00093 }
00094
00095 return self;
00096 }
00097
00101 - (void)loadWindow
00102 {
00103 [self windowWillLoad];
00104
00105 [self setWindow:CPApp._keyWindow = [[CPWindow alloc] initWithContentRect:CPRectMakeZero() styleMask:CPBorderlessBridgeWindowMask|CPTitledWindowMask|CPClosableWindowMask|CPResizableWindowMask]];
00106
00107 [self windowDidLoad];
00108 }
00109
00114 - (CFAction)showWindow:(id)aSender
00115 {
00116 var theWindow = [self window];
00117
00118 if ([theWindow respondsToSelector:@selector(becomesKeyOnlyIfNeeded)] && [theWindow becomesKeyOnlyIfNeeded])
00119 [theWindow orderFront:aSender];
00120 else
00121 [theWindow makeKeyAndOrderFront:aSender];
00122 }
00123
00128 - (BOOL)isWindowLoaded
00129 {
00130 return _window;
00131 }
00132
00136 - (CPWindow)window
00137 {
00138 if (!_window)
00139 [self loadWindow];
00140
00141 return _window;
00142 }
00143
00148 - (void)setWindow:(CPWindow)aWindow
00149 {
00150 _window = aWindow;
00151
00152 [_window setWindowController:self];
00153 [_window setNextResponder:self];
00154 }
00155
00159 - (void)windowDidLoad
00160 {
00161 [_document windowControllerDidLoadNib:self];
00162
00163 [self synchronizeWindowTitleWithDocumentName];
00164 }
00165
00169 - (void)windowWillLoad
00170 {
00171 [_document windowControllerWillLoadNib:self];
00172 }
00173
00178 - (void)setDocument:(CPDocument)aDocument
00179 {
00180 if (_document == aDocument)
00181 return;
00182
00183 var defaultCenter = [CPNotificationCenter defaultCenter];
00184
00185 if (_document)
00186 {
00187 [defaultCenter removeObserver:self
00188 name:CPDocumentWillSaveNotification
00189 object:_document];
00190
00191 [defaultCenter removeObserver:self
00192 name:CPDocumentDidSaveNotification
00193 object:_document];
00194
00195 [defaultCenter removeObserver:self
00196 name:CPDocumentDidFailToSaveNotification
00197 object:_document];
00198 }
00199
00200 _document = aDocument;
00201
00202 if (_document)
00203 {
00204 [defaultCenter addObserver:self
00205 selector:@selector(_documentWillSave:)
00206 name:CPDocumentWillSaveNotification
00207 object:_document];
00208
00209 [defaultCenter addObserver:self
00210 selector:@selector(_documentDidSave:)
00211 name:CPDocumentDidSaveNotification
00212 object:_document];
00213
00214 [defaultCenter addObserver:self
00215 selector:@selector(_documentDidFailToSave:)
00216 name:CPDocumentDidFailToSaveNotification
00217 object:_document];
00218
00219 [self setDocumentEdited:[_document isDocumentEdited]];
00220 }
00221
00222 [self synchronizeWindowTitleWithDocumentName];
00223 }
00224
00225
00226 - (void)_documentWillSave:(CPNotification)aNotification
00227 {
00228 [[self window] setDocumentSaving:YES];
00229 }
00230
00231
00232 - (void)_documentDidSave:(CPNotification)aNotification
00233 {
00234 [[self window] setDocumentSaving:NO];
00235 }
00236
00237
00238 - (void)_documentDidFailToSave:(CPNotification)aNotification
00239 {
00240 [[self window] setDocumentSaving:NO];
00241 }
00242
00246 - (CPDocument)document
00247 {
00248 return _document;
00249 }
00250
00255 - (void)setDocumentEdited:(BOOL)isEdited
00256 {
00257 [[self window] setDocumentEdited:isEdited];
00258 }
00259
00260
00261
00265 - (void)synchronizeWindowTitleWithDocumentName
00266 {
00267 if (!_document || !_window)
00268 return;
00269
00270
00271 [_window setTitle:[self windowTitleForDocumentDisplayName:[_document displayName]]];
00272 }
00273
00278 - (CPString)windowTitleForDocumentDisplayName:(CPString)aDisplayName
00279 {
00280 return aDisplayName;
00281 }
00282
00283 @end