28 @global CPDocumentWillSaveNotification
29 @global CPDocumentDidSaveNotification
30 @global CPDocumentDidFailToSaveNotification
53 BOOL _shouldCloseDocument;
54 BOOL _supportsMultipleDocuments;
61 CPView _viewControllerContainerView;
66 return [
self initWithWindow:nil];
74 - (id)initWithWindow:(
CPWindow)aWindow
80 [
self setWindow:aWindow];
81 [
self setShouldCloseDocument:NO];
83 [
self setNextResponder:CPApp];
96 - (id)initWithWindowCibName:(
CPString)aWindowCibName
98 return [
self initWithWindowCibName:aWindowCibName owner:self];
107 - (id)initWithWindowCibName:(
CPString)aWindowCibName owner:(
id)anOwner
109 self = [
self initWithWindow:nil];
114 _windowCibName = aWindowCibName;
120 - (id)initWithWindowCibPath:(
CPString)aWindowCibPath owner:(
id)anOwner
122 self = [
self initWithWindow:nil];
127 _windowCibPath = aWindowCibPath;
142 [[
CPBundle mainBundle] loadCibFile:[
self windowCibPath] externalNameTable:@{ CPCibOwner: _cibOwner }];
149 - (@action)showWindow:(
id)aSender
151 var theWindow = [
self window];
153 if ([theWindow respondsToSelector:
@selector(becomesKeyOnlyIfNeeded)] && [theWindow becomesKeyOnlyIfNeeded])
154 [theWindow orderFront:aSender];
156 [theWindow makeKeyAndOrderFront:aSender];
163 - (BOOL)isWindowLoaded
165 return _window !== nil;
176 [
self windowWillLoad];
177 [_document windowControllerWillLoadCib:self];
181 if (_window === nil && [_cibOwner isKindOfClass:[
CPDocument class]])
182 [
self setWindow:[_cibOwner valueForKey:@"window"]];
186 var reason = [
CPString stringWithFormat:@"Window for %@ could not be loaded from Cib or no window specified. Override loadWindow to load the window manually.", self];
191 [
self windowDidLoad];
192 [_document windowControllerDidLoadCib:self];
194 [
self synchronizeWindowTitleWithDocumentName];
206 [_window setWindowController:nil];
210 [_window setWindowController:self];
211 [_window setNextResponder:self];
217 - (void)windowDidLoad
224 - (void)windowWillLoad
234 if (_document === aDocument)
241 if (![
self supportsMultipleDocuments])
242 [
self removeDocument:_document];
244 [defaultCenter removeObserver:self
245 name:CPDocumentWillSaveNotification
248 [defaultCenter removeObserver:self
249 name:CPDocumentDidSaveNotification
252 [defaultCenter removeObserver:self
253 name:CPDocumentDidFailToSaveNotification
257 _document = aDocument;
261 [
self addDocument:_document];
263 [defaultCenter addObserver:self
264 selector:@selector(_documentWillSave:)
265 name:CPDocumentWillSaveNotification
268 [defaultCenter addObserver:self
269 selector:@selector(_documentDidSave:)
270 name:CPDocumentDidSaveNotification
273 [defaultCenter addObserver:self
274 selector:@selector(_documentDidFailToSave:)
275 name:CPDocumentDidFailToSaveNotification
278 [
self setDocumentEdited:[_document isDocumentEdited]];
281 var viewController = [_document viewControllerForWindowController:self];
284 [
self setViewController:viewController];
286 [
self synchronizeWindowTitleWithDocumentName];
290 [[[
self window] toolbar] _autoValidateVisibleItems];
293 - (void)setSupportsMultipleDocuments:(BOOL)shouldSupportMultipleDocuments
295 _supportsMultipleDocuments = shouldSupportMultipleDocuments;
298 - (BOOL)supportsMultipleDocuments
300 return _supportsMultipleDocuments;
305 if (aDocument && ![_documents containsObject:aDocument])
306 [_documents addObject:aDocument];
311 var index = [_documents indexOfObjectIdenticalTo:aDocument];
316 [_documents removeObjectAtIndex:index];
318 if (_document === aDocument && [_documents count])
319 [
self setDocument:[_documents objectAtIndex:MIN(index, [_documents count] - 1)]];
322 - (void)removeDocumentAndCloseIfNecessary:(
CPDocument)aDocument
324 [
self removeDocument:aDocument];
326 if (![_documents count])
335 - (void)setViewControllerContainerView:(
CPView)aView
337 if (!_viewControllerContainerView && !aView)
340 var viewController = [
self viewController],
341 viewControllerView = [viewController isViewLoaded] ? [viewController view] : nil,
342 contentView = [[
self window] contentView];
346 [aView setFrame:[contentView frame]];
347 [aView setAutoresizingMask:[contentView autoresizingMask]];
349 if (viewControllerView)
351 [viewControllerView removeFromSuperview];
352 [aView addSubview:viewControllerView];
355 [[
self window] setContentView:aView];
357 else if (viewControllerView)
359 [viewControllerView removeFromSuperview];
360 [viewControllerView setFrame:[contentView frame]];
361 [viewControllerView setAutoresizingMask:[contentView autoresizingMask]]
362 [[
self window] setContentView:viewControllerView];
366 var view = [[
CPView alloc] init];
367 [view setFrame:[contentView frame]];
368 [view setAutoresizingMask:[contentView autoresizingMask]];
369 [[
self window] setContentView:view]
372 _viewControllerContainerView = aView;
375 - (void)viewControllerContainerView
377 return _viewControllerContainerView;
382 if (!_viewController && !aViewController)
385 var containerView = [
self viewControllerContainerView],
386 newView = [aViewController isViewLoaded] ? [aViewController view] : nil;
390 var oldView = [_viewController isViewLoaded] ? [_viewController view] : nil;
394 [newView setFrame:[oldView frame]];
395 [newView setAutoresizingMask:[oldView autoresizingMask]];
398 if (oldView && newView)
399 [containerView replaceSubview:oldView with:newView];
401 [oldView removeFromSuperview];
403 [containerView addSubview:newView];
407 var contentView = [[
self window] contentView];
408 [newView setFrame:[contentView frame]];
409 [newView setAutoresizingMask:[contentView autoresizingMask]];
410 [[
self window] setContentView:newView];
414 var view = [[
CPView alloc] init],
415 contentView = [[
self window] contentView];
417 [view setFrame:[contentView frame]];
418 [view setAutoresizingMask:[contentView autoresizingMask]];
419 [[
self window] setContentView:view]
422 _viewController = aViewController;
427 return _viewController;
433 [[
self window] setDocumentSaving:YES];
439 [[
self window] setDocumentSaving:NO];
445 [[
self window] setDocumentSaving:NO];
460 - (void)setDocumentEdited:(BOOL)isEdited
462 [[
self window] setDocumentEdited:isEdited];
467 [[
self window] close];
470 - (void)setShouldCloseDocument:(BOOL)shouldCloseDocument
472 _shouldCloseDocument = shouldCloseDocument;
475 - (BOOL)shouldCloseDocument
477 return _shouldCloseDocument;
488 return _windowCibName;
490 return [[_windowCibPath lastPathComponent] stringByDeletingPathExtension];
496 return _windowCibPath;
506 - (void)synchronizeWindowTitleWithDocumentName
508 if (!_document || !_window)
512 [_window setTitle:[
self windowTitleForDocumentDisplayName:[_document displayName]]];