2004-02-25 03:27:23 +00:00
|
|
|
/** <title>NSWindow+Toolbar</title>
|
|
|
|
|
|
|
|
<abstract>The window class category to include toolbar support</abstract>
|
|
|
|
|
|
|
|
Copyright (C) 2004 Free Software Foundation, Inc.
|
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
Author: Quentin Mathe <qmathe@club-internet.fr>
|
2004-02-25 03:27:23 +00:00
|
|
|
Date: January 2004
|
|
|
|
|
|
|
|
This file is part of the GNUstep GUI Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
|
|
|
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
|
2004-04-02 15:40:49 +00:00
|
|
|
#include "AppKit/NSWindow+Toolbar.h"
|
2004-02-25 03:27:23 +00:00
|
|
|
#include "AppKit/NSView.h"
|
|
|
|
#include "AppKit/NSToolbar.h"
|
|
|
|
#include "GNUstepGUI/GSToolbarView.h"
|
|
|
|
|
|
|
|
static const int ToolbarHeight = 61;
|
|
|
|
|
|
|
|
@interface NSToolbar (GNUstepPrivate)
|
|
|
|
+ (NSArray *) _toolbars;
|
|
|
|
- (GSToolbarView *) _toolbarView;
|
|
|
|
- (void) _setWindow: (NSWindow *)window;
|
|
|
|
- (NSWindow *) _window;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSToolbarView (GNUstepPrivate)
|
2004-03-12 22:55:02 +00:00
|
|
|
- (void) _setToolbar: (NSToolbar *)toolbar;
|
2004-02-25 03:27:23 +00:00
|
|
|
- (void) _handleViewsVisibility;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSWindow (ToolbarPrivate)
|
|
|
|
- (void) _toggleToolbarView: (GSToolbarView *)toolbarView display: (BOOL)flag;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSWindow (Toolbar)
|
|
|
|
|
|
|
|
- (void) runToolbarCustomizationPalette: (id)sender
|
|
|
|
{
|
|
|
|
[[self toolbar] runCustomizationPalette: sender];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) toggleToolbarShown: (id)sender
|
|
|
|
{
|
|
|
|
NSToolbar *toolbar = [self toolbar];
|
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
if ([sender isEqual: toolbar])
|
|
|
|
// we can enter this branch when the toolbar class has called
|
|
|
|
// toggleToolbarShown:
|
2004-02-25 03:27:23 +00:00
|
|
|
{
|
|
|
|
[self _toggleToolbarView: [toolbar _toolbarView] display: YES];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we call the toolbar class letting it call back on toggleToolbarShown:
|
|
|
|
[toolbar setVisible: ![toolbar isVisible]];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
|
|
|
|
- (NSView *) contentViewWithoutToolbar
|
|
|
|
{
|
|
|
|
NSToolbar *toolbar = [self toolbar];
|
|
|
|
|
|
|
|
if (toolbar != nil && [toolbar isVisible])
|
|
|
|
{
|
|
|
|
NSArray *subviews = [_contentView subviews];
|
|
|
|
id subview;
|
|
|
|
int i, n = [subviews count];
|
|
|
|
GSToolbarView *toolbarView = [toolbar _toolbarView];
|
|
|
|
|
|
|
|
if (n > 2 || ![[toolbarView superview] isEqual: _contentView]) {
|
|
|
|
[NSException raise: @"_contentView error"
|
2004-03-12 22:55:02 +00:00
|
|
|
format: @"_contenView is not valid. _contentView needs a \
|
|
|
|
toolbar view and a contentViewWithoutToolbar, with \
|
|
|
|
no others subviews."];
|
2004-02-25 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
subview = [subviews objectAtIndex: i];
|
|
|
|
if (![subview isEqual: toolbarView])
|
|
|
|
{
|
|
|
|
return subview;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [self contentView];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSToolbar *) toolbar
|
|
|
|
{
|
|
|
|
NSArray *toolbars = [NSToolbar _toolbars];
|
|
|
|
NSArray *windows;
|
|
|
|
unsigned index = 0;
|
|
|
|
|
|
|
|
if (toolbars == nil)
|
|
|
|
return nil;
|
2004-03-12 22:55:02 +00:00
|
|
|
|
2004-02-25 03:27:23 +00:00
|
|
|
windows = [toolbars valueForKey: @"_window"];
|
|
|
|
index = [windows indexOfObjectIdenticalTo: self];
|
|
|
|
|
|
|
|
return (index == NSNotFound) ? nil : [toolbars objectAtIndex: index];
|
|
|
|
}
|
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
// user oriented method
|
|
|
|
- (void) setContentViewWithoutToolbar: (NSView *)contentViewWithoutToolbar
|
2004-02-25 03:27:23 +00:00
|
|
|
{
|
|
|
|
NSToolbar *toolbar = [self toolbar];
|
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
if (toolbar != nil && [toolbar isVisible])
|
|
|
|
{
|
|
|
|
[_contentView replaceSubview: [self contentViewWithoutToolbar]
|
|
|
|
with: contentViewWithoutToolbar];
|
2004-02-25 03:27:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self setContentView: contentViewWithoutToolbar];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) setToolbar: (NSToolbar*)toolbar
|
|
|
|
{
|
|
|
|
NSToolbar *lastToolbar = [self toolbar];
|
|
|
|
GSToolbarView *toolbarView = nil;
|
2004-03-12 22:55:02 +00:00
|
|
|
// ---
|
2004-02-25 03:27:23 +00:00
|
|
|
|
|
|
|
if (lastToolbar != nil)
|
|
|
|
{
|
|
|
|
// we throw the last toolbar out
|
|
|
|
|
|
|
|
[self _toggleToolbarView : [lastToolbar _toolbarView] display: NO];
|
|
|
|
[lastToolbar _setWindow: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
// when there is no new toolbar
|
|
|
|
|
|
|
|
if (toolbar == nil)
|
|
|
|
{
|
|
|
|
[self display]; // to show we have toggle the previous toolbar view
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ELSE
|
|
|
|
// the window want to know which toolbar is binded
|
|
|
|
|
|
|
|
[toolbar _setWindow : self];
|
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
// insert the toolbar view (we create this view when the toolbar hasn't such
|
|
|
|
// view)...
|
2004-02-25 03:27:23 +00:00
|
|
|
|
|
|
|
toolbarView = [toolbar _toolbarView];
|
|
|
|
if (toolbarView == nil)
|
|
|
|
{
|
|
|
|
toolbarView = [[GSToolbarView alloc] initWithFrame: NSMakeRect(0, 0, 0, 0)];
|
2004-03-12 22:55:02 +00:00
|
|
|
// _toggleToolbarView:display: method will set the toolbar view to the right
|
|
|
|
// frame
|
2004-02-25 03:27:23 +00:00
|
|
|
[toolbarView setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
|
|
|
|
}
|
|
|
|
[toolbarView setBorderMask: GSToolbarViewBottomBorder];
|
|
|
|
[self _toggleToolbarView: toolbarView display: YES];
|
|
|
|
|
|
|
|
// load the toolbar inside the toolbar view
|
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
[toolbarView _setToolbar: toolbar];
|
2004-02-25 03:27:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private methods
|
|
|
|
|
|
|
|
- (void) _toggleToolbarView: (GSToolbarView *)toolbarView display: (BOOL)flag {
|
|
|
|
NSRect windowFrame;
|
|
|
|
|
|
|
|
if ([toolbarView superview] == nil)
|
|
|
|
{
|
|
|
|
NSView *contentViewWithoutToolbar;
|
|
|
|
NSRect contentViewWithoutToolbarFrame;
|
|
|
|
|
|
|
|
contentViewWithoutToolbar = _contentView;
|
|
|
|
|
|
|
|
// Switch the content view
|
|
|
|
|
|
|
|
RETAIN(contentViewWithoutToolbar);
|
2004-03-12 22:55:02 +00:00
|
|
|
[self setContentView:
|
|
|
|
[[NSView alloc] initWithFrame: [_contentView frame]]];
|
2004-02-25 03:27:23 +00:00
|
|
|
|
|
|
|
// Resize the window
|
|
|
|
|
|
|
|
windowFrame = [self frame];
|
2004-03-12 22:55:02 +00:00
|
|
|
[self setFrame: NSMakeRect(windowFrame.origin.x,
|
|
|
|
windowFrame.origin.y - ToolbarHeight, windowFrame.size.width,
|
|
|
|
windowFrame.size.height + ToolbarHeight) display: flag];
|
2004-02-25 03:27:23 +00:00
|
|
|
|
|
|
|
// Plug the toolbar view
|
|
|
|
|
|
|
|
contentViewWithoutToolbarFrame = [contentViewWithoutToolbar frame];
|
2004-03-12 22:55:02 +00:00
|
|
|
[toolbarView setFrame: NSMakeRect(0,contentViewWithoutToolbarFrame.size.height,
|
|
|
|
contentViewWithoutToolbarFrame.size.width, ToolbarHeight)];
|
|
|
|
|
2004-02-25 03:27:23 +00:00
|
|
|
[_contentView addSubview: toolbarView];
|
|
|
|
[toolbarView _handleViewsVisibility];
|
|
|
|
[toolbarView setNextResponder: self];
|
|
|
|
|
|
|
|
// Insert the previous content view
|
|
|
|
|
|
|
|
[_contentView addSubview: contentViewWithoutToolbar];
|
|
|
|
RELEASE(contentViewWithoutToolbar);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSView *contentViewWithoutToolbar;
|
|
|
|
|
|
|
|
contentViewWithoutToolbar = [self contentViewWithoutToolbar];
|
|
|
|
|
|
|
|
// Unplug the toolbar view
|
|
|
|
|
|
|
|
[toolbarView removeFromSuperview];
|
|
|
|
|
|
|
|
// Resize the window
|
|
|
|
|
|
|
|
[contentViewWithoutToolbar setAutoresizingMask: NSViewMaxYMargin];
|
|
|
|
|
|
|
|
windowFrame = [self frame];
|
2004-03-12 22:55:02 +00:00
|
|
|
[self setFrame: NSMakeRect(windowFrame.origin.x,
|
|
|
|
windowFrame.origin.y + ToolbarHeight, windowFrame.size.width,
|
|
|
|
windowFrame.size.height - ToolbarHeight) display: flag];
|
2004-02-25 03:27:23 +00:00
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
[contentViewWithoutToolbar setAutoresizingMask: NSViewWidthSizable
|
|
|
|
| NSViewHeightSizable];
|
2004-02-25 03:27:23 +00:00
|
|
|
// Autoresizing mask will be set again by the setContentView: method
|
|
|
|
|
|
|
|
// Switch the content view
|
|
|
|
|
2004-03-12 22:55:02 +00:00
|
|
|
RETAIN(contentViewWithoutToolbar);
|
|
|
|
// because setContentView: will release the parent view and their subviews
|
|
|
|
|
2004-02-25 03:27:23 +00:00
|
|
|
[self setContentView: contentViewWithoutToolbar];
|
2004-03-12 22:55:02 +00:00
|
|
|
|
2004-02-25 03:27:23 +00:00
|
|
|
RELEASE(contentViewWithoutToolbar);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|