1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/toolbar.mm
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/toolbar.h"
22 #include "wx/osx/private.h"
23 #include "wx/geometry.h"
24 #include "wx/sysopt.h"
26 const short kwxMacToolBarToolDefaultWidth = 16;
27 const short kwxMacToolBarToolDefaultHeight = 16;
28 const short kwxMacToolBarTopMargin = 4;
29 const short kwxMacToolBarLeftMargin = 4;
30 const short kwxMacToolBorder = 0;
31 const short kwxMacToolSpacing = 6;
33 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
34 EVT_PAINT( wxToolBar::OnPaint )
39 #pragma mark Tool Implementation
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 @interface wxNSToolBarButton : NSButton
52 - (id)initWithFrame:(NSRect)frame;
53 - (void) clickedAction: (id) sender;
54 - (void)setImplementation: (wxToolBarTool *) theImplementation;
55 - (wxToolBarTool*) implementation;
60 // We have a dual implementation for each tool, WXWidget and NSToolbarItem*
62 // when embedding native controls in the native toolbar we must make sure the
63 // control does not get deleted behind our backs, so the retain count gets increased
64 // (after creation it is 1), first be the creation of the custom NSToolbarItem wrapper
65 // object, and second by the code 'creating' the custom HIView (which is the same as the
66 // already existing native control, therefore we just increase the ref count)
67 // when this view is removed from the native toolbar its count gets decremented again
68 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
69 // so in the end the control lives with a refcount of one and can be disposed of by the
70 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
71 // so we can only test against a range, not a specific value of the refcount.
73 class wxToolBarTool : public wxToolBarToolBase
79 const wxString& label,
80 const wxBitmap& bmpNormal,
81 const wxBitmap& bmpDisabled,
84 const wxString& shortHelp,
85 const wxString& longHelp );
87 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
88 : wxToolBarToolBase(tbar, control, label)
92 SetControlHandle( (WXWidget) control->GetHandle() );
95 virtual ~wxToolBarTool()
100 WXWidget GetControlHandle()
102 return (WXWidget) m_controlHandle;
105 void SetControlHandle( WXWidget handle )
107 m_controlHandle = handle;
110 void SetPosition( const wxPoint& position );
114 if ( m_controlHandle )
118 [m_controlHandle retain];
122 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
123 // proper wxControl destructor
125 m_controlHandle = NULL ;
128 #if wxOSX_USE_NATIVE_TOOLBAR
131 [m_toolbarItem release];
132 m_toolbarItem = NULL;
134 #endif // wxOSX_USE_NATIVE_TOOLBAR
137 wxSize GetSize() const
143 curSize = GetControl()->GetSize();
145 else if ( IsButton() )
147 // curSize = GetToolBar()->GetToolSize();
148 NSRect best = [(wxNSToolBarButton*)m_controlHandle frame];
149 curSize = wxSize(best.size.width, best.size.height);
154 curSize = GetToolBar()->GetToolSize();
155 if ( GetToolBar()->IsVertical() )
164 wxPoint GetPosition() const
166 return wxPoint( m_x, m_y );
169 bool Enable( bool enable );
173 void UpdateToggleImage( bool toggle );
177 wxString labelStr = wxStripMenuCodes(m_label);
178 wxCFStringRef l(labelStr, GetToolBarFontEncoding());
179 wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() );
180 #if wxOSX_USE_NATIVE_TOOLBAR
183 // strip mnemonics from the label for compatibility with the usual
184 // labels in wxStaticText sense
186 [m_toolbarItem setLabel:l.AsNSString()];
188 [m_toolbarItem setToolTip:sh.AsNSString()];
192 [(NSButton*)m_controlHandle setTitle:l.AsNSString()];
194 if ( m_controlHandle )
196 [m_controlHandle setToolTip:sh.AsNSString()];
202 wxToolBar *tbar = (wxToolBar*) GetToolBar();
207 shouldToggle = !IsToggled();
208 tbar->ToggleTool( GetId(), shouldToggle );
211 tbar->OnLeftClick( GetId(), IsToggled() );
214 #if wxOSX_USE_NATIVE_TOOLBAR
215 void SetToolbarItemRef( NSToolbarItem* ref )
217 if ( m_controlHandle )
218 [m_controlHandle setHidden:YES];
220 [m_toolbarItem release];
225 NSToolbarItem* GetToolbarItemRef() const
227 return m_toolbarItem;
230 void SetIndex( CFIndex idx )
235 CFIndex GetIndex() const
240 virtual void SetLabel(const wxString& label)
242 wxToolBarToolBase::SetLabel(label);
246 virtual bool SetShortHelp(const wxString& help)
248 if ( !wxToolBarToolBase::SetShortHelp(help) )
255 #endif // wxOSX_USE_NATIVE_TOOLBAR
258 #if wxOSX_USE_NATIVE_TOOLBAR
259 wxFontEncoding GetToolBarFontEncoding() const
263 f = GetToolBar()->GetFont();
264 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
266 #endif // wxOSX_USE_NATIVE_TOOLBAR
270 m_controlHandle = NULL;
272 #if wxOSX_USE_NATIVE_TOOLBAR
273 m_toolbarItem = NULL;
278 WXWidget m_controlHandle;
281 wxBitmap m_alternateBitmap;
283 #if wxOSX_USE_NATIVE_TOOLBAR
284 NSToolbarItem* m_toolbarItem;
285 // position in its toolbar, -1 means not inserted
290 #if wxOSX_USE_NATIVE_TOOLBAR
292 @interface wxNSToolbarItem : NSToolbarItem
297 - (id) initWithItemIdentifier: (NSString*) identifier;
298 - (void)setImplementation: (wxToolBarTool *) theImplementation;
299 - (wxToolBarTool*) implementation;
300 - (void) clickedAction: (id) sender;
301 - (BOOL) validateToolbarItem:(NSToolbarItem *)theItem;
306 @interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER(<NSToolbarDelegate>)
311 - (void)setSelectable:(bool) value;
313 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
315 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
317 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
319 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
327 #if wxOSX_USE_NATIVE_TOOLBAR
329 @implementation wxNSToolbarItem
331 - (id)initWithItemIdentifier: (NSString*) identifier
333 self = [super initWithItemIdentifier:identifier];
335 [self setTarget: self];
336 [self setAction: @selector(clickedAction:)];
340 - (void) clickedAction: (id) sender
349 - (void)setImplementation: (wxToolBarTool *) theImplementation
351 impl = theImplementation;
354 - (wxToolBarTool*) implementation
359 - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
361 wxUnusedVar(theItem);
362 return impl->IsEnabled() ? YES:NO;
367 @implementation wxNSToolbarDelegate
371 m_isSelectable = false;
375 - (void)setSelectable:(bool) value
377 m_isSelectable = true;
380 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
382 wxUnusedVar(toolbar);
386 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
388 wxUnusedVar(toolbar);
392 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
394 if ( m_isSelectable )
395 return [[toolbar items] valueForKey:@"itemIdentifier"];
400 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
402 wxUnusedVar(toolbar);
404 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue];
406 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
410 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
411 if ( flag && tool->IsControl() )
413 NSView* view = tool->GetControl()->GetHandle();
414 [view removeFromSuperview];
416 wxSize sz = tool->GetControl()->GetSize();
417 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
418 [item setMaxSize:size];
419 [item setMinSize:size];
431 @implementation wxNSToolBarButton
433 - (id)initWithFrame:(NSRect)frame
435 self = [super initWithFrame:frame];
437 [self setTarget: self];
438 [self setAction: @selector(clickedAction:)];
442 - (void) clickedAction: (id) sender
451 - (void)setImplementation: (wxToolBarTool *) theImplementation
453 impl = theImplementation;
456 - (wxToolBarTool*) implementation
468 bool wxToolBarTool::Enable( bool enable )
470 if ( wxToolBarToolBase::Enable( enable ) == false )
475 GetControl()->Enable( enable );
477 else if ( IsButton() )
479 #if wxOSX_USE_NATIVE_TOOLBAR
480 if ( m_toolbarItem != NULL )
481 [m_toolbarItem setEnabled:enable];
484 if ( m_controlHandle != NULL )
485 [(NSControl*)m_controlHandle setEnabled:enable];
491 void wxToolBarTool::SetPosition( const wxPoint& position )
496 int mac_x = position.x;
497 int mac_y = position.y;
501 NSRect frame = [m_controlHandle frame];
502 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
504 frame.origin.x = mac_x;
505 frame.origin.y = mac_y;
506 [m_controlHandle setFrame:frame];
509 else if ( IsControl() )
511 // embedded native controls are moved by the OS
512 #if wxOSX_USE_NATIVE_TOOLBAR
513 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
516 GetControl()->Move( position );
521 NSRect frame = [m_controlHandle frame];
522 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
524 frame.origin.x = mac_x;
525 frame.origin.y = mac_y;
526 [m_controlHandle setFrame:frame];
531 void wxToolBarTool::UpdateImages()
533 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
535 if ( CanBeToggled() )
537 int w = m_bmpNormal.GetWidth();
538 int h = m_bmpNormal.GetHeight();
539 m_alternateBitmap = wxBitmap( w, h );
542 dc.SelectObject( m_alternateBitmap );
543 dc.SetPen( wxPen(*wxBLACK) );
544 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
545 dc.DrawRoundedRectangle( 0, 0, w, h, 2 );
546 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
547 dc.SelectObject( wxNullBitmap );
549 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
551 UpdateToggleImage( CanBeToggled() && IsToggled() );
554 void wxToolBarTool::UpdateToggleImage( bool toggle )
556 #if wxOSX_USE_NATIVE_TOOLBAR
557 if (m_toolbarItem != NULL )
559 // the native toolbar item only has a 'selected' state (one for one toolbar)
560 // so we emulate the toggle here
561 if ( CanBeToggled() && toggle )
562 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
564 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
570 [(NSButton*)m_controlHandle setState:(toggle ? NSOnState : NSOffState)];
574 wxToolBarTool::wxToolBarTool(
577 const wxString& label,
578 const wxBitmap& bmpNormal,
579 const wxBitmap& bmpDisabled,
581 wxObject *clientData,
582 const wxString& shortHelp,
583 const wxString& longHelp )
586 tbar, id, label, bmpNormal, bmpDisabled, kind,
587 clientData, shortHelp, longHelp )
593 #pragma mark Toolbar Implementation
595 wxToolBarToolBase *wxToolBar::CreateTool(
597 const wxString& label,
598 const wxBitmap& bmpNormal,
599 const wxBitmap& bmpDisabled,
601 wxObject *clientData,
602 const wxString& shortHelp,
603 const wxString& longHelp )
605 return new wxToolBarTool(
606 this, id, label, bmpNormal, bmpDisabled, kind,
607 clientData, shortHelp, longHelp );
611 wxToolBar::CreateTool(wxControl *control, const wxString& label)
613 return new wxToolBarTool(this, control, label);
616 void wxToolBar::Init()
620 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
621 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
623 #if wxOSX_USE_NATIVE_TOOLBAR
625 m_macUsesNativeToolbar = false;
629 // also for the toolbar we have the dual implementation:
630 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
632 bool wxToolBar::Create(
638 const wxString& name )
640 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
645 OSStatus err = noErr;
647 #if wxOSX_USE_NATIVE_TOOLBAR
649 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
651 static wxNSToolbarDelegate* controller = nil;
653 if ( controller == nil )
654 controller = [[wxNSToolbarDelegate alloc] init];
655 wxString identifier = wxString::Format( wxT("%p"), this );
656 wxCFStringRef cfidentifier(identifier);
657 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
661 if (m_macToolbar != NULL)
663 [tb setDelegate:controller];
665 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
666 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
668 if ( style & wxTB_NOICONS )
669 mode = NSToolbarDisplayModeLabelOnly;
670 else if ( style & wxTB_TEXT )
671 mode = NSToolbarDisplayModeIconAndLabel;
673 mode = NSToolbarDisplayModeIconOnly;
675 [tb setDisplayMode:mode];
676 [tb setSizeMode:displaySize];
679 #endif // wxOSX_USE_NATIVE_TOOLBAR
681 return (err == noErr);
684 wxToolBar::~wxToolBar()
686 // removal only works while the toolbar is there
687 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
688 if ( frame && frame->GetToolBar() == this )
690 frame->SetToolBar(NULL);
693 [(NSToolbar*)m_macToolbar setDelegate:nil];
694 [(NSToolbar*)m_macToolbar release];
698 bool wxToolBar::Show( bool show )
700 WXWindow tlw = MacGetTopLevelWindowRef();
701 bool bResult = (tlw != NULL);
705 #if wxOSX_USE_NATIVE_TOOLBAR
706 bool ownToolbarInstalled = false;
707 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
708 if (ownToolbarInstalled)
710 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
712 [(NSToolbar*)m_macToolbar setVisible:show];
715 bResult = wxToolBarBase::Show( show );
718 bResult = wxToolBarBase::Show( show );
725 bool wxToolBar::IsShown() const
729 #if wxOSX_USE_NATIVE_TOOLBAR
730 bool ownToolbarInstalled;
732 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
733 if (ownToolbarInstalled)
735 bResult = [(NSToolbar*)m_macToolbar isVisible];
738 bResult = wxToolBarBase::IsShown();
741 bResult = wxToolBarBase::IsShown();
747 void wxToolBar::DoGetSize( int *width, int *height ) const
749 #if wxOSX_USE_NATIVE_TOOLBAR
750 bool ownToolbarInstalled;
752 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
753 if ( ownToolbarInstalled )
755 WXWindow tlw = MacGetTopLevelWindowRef();
756 float toolbarHeight = 0.0;
757 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
759 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
761 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
762 styleMask:[tlw styleMask]];
763 toolbarHeight = NSHeight(windowFrame)
764 - NSHeight([[tlw contentView] frame]);
768 *width = (int)windowFrame.size.width;
769 if ( height != NULL )
770 *height = (int)toolbarHeight;
773 wxToolBarBase::DoGetSize( width, height );
776 wxToolBarBase::DoGetSize( width, height );
780 void wxToolBar::DoGetPosition(int*x, int *y) const
782 #if wxOSX_USE_NATIVE_TOOLBAR
783 bool ownToolbarInstalled;
785 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
786 if ( ownToolbarInstalled )
788 WXWindow tlw = MacGetTopLevelWindowRef();
789 float toolbarHeight = 0.0;
790 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
792 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
794 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
795 styleMask:[tlw styleMask]];
796 toolbarHeight = NSHeight(windowFrame)
797 - NSHeight([[tlw contentView] frame]);
800 // it is extending to the north of the content area
808 wxToolBarBase::DoGetPosition( x, y );
811 wxToolBarBase::DoGetPosition( x, y );
815 wxSize wxToolBar::DoGetBestSize() const
817 // was updated in Realize()
819 wxSize size = GetMinSize();
824 void wxToolBar::SetWindowStyleFlag( long style )
826 wxToolBarBase::SetWindowStyleFlag( style );
828 #if wxOSX_USE_NATIVE_TOOLBAR
829 if (m_macToolbar != NULL)
831 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
833 if ( style & wxTB_NOICONS )
834 mode = NSToolbarDisplayModeLabelOnly;
835 else if ( style & wxTB_TEXT )
836 mode = NSToolbarDisplayModeIconAndLabel;
838 mode = NSToolbarDisplayModeIconOnly;
840 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
845 #if wxOSX_USE_NATIVE_TOOLBAR
846 bool wxToolBar::MacWantsNativeToolbar()
848 return m_macUsesNativeToolbar;
851 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
853 bool bResultV = false;
855 if (ownToolbarInstalled != NULL)
856 *ownToolbarInstalled = false;
858 WXWindow tlw = MacGetTopLevelWindowRef();
861 NSToolbar* curToolbarRef = [tlw toolbar];
862 bResultV = (curToolbarRef != NULL);
863 if (bResultV && (ownToolbarInstalled != NULL))
864 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
870 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
872 bool bResult = false;
874 if (usesNative && (m_macToolbar == NULL))
877 if (usesNative && HasFlag(wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM) )
880 WXWindow tlw = MacGetTopLevelWindowRef();
884 // check the existing toolbar
885 NSToolbar* curToolbarRef = [tlw toolbar];
887 m_macUsesNativeToolbar = usesNative;
889 if (m_macUsesNativeToolbar)
891 // only install toolbar if there isn't one installed already
892 if (curToolbarRef == NULL)
895 [tlw setToolbar:(NSToolbar*) m_macToolbar];
896 [(NSToolbar*) m_macToolbar setVisible:YES];
898 GetPeer()->Move(0,0,0,0 );
899 SetSize( wxSIZE_AUTO_WIDTH, 0 );
900 GetPeer()->SetVisibility( false );
901 wxToolBarBase::Show( false );
906 // only deinstall toolbar if this is the installed one
907 if (m_macToolbar == curToolbarRef)
910 [(NSToolbar*) m_macToolbar setVisible:NO];
911 MacUninstallNativeToolbar();
912 GetPeer()->SetVisibility( true );
917 InvalidateBestSize();
919 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
923 void wxToolBar::MacUninstallNativeToolbar()
928 WXWindow tlw = MacGetTopLevelWindowRef();
930 [tlw setToolbar:nil];
934 void wxToolBar::DoLayout()
936 int maxToolWidth = 0;
937 int maxToolHeight = 0;
942 // find the maximum tool width and height
943 // and the number of stretchable items
944 int numStretchableSpaces = 0;
946 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
949 tool = (wxToolBarTool *) node->GetData();
952 wxSize sz = tool->GetSize();
954 if ( sz.x > maxToolWidth )
956 if ( sz.y > maxToolHeight )
957 maxToolHeight = sz.y;
958 if ( tool->IsStretchableSpace() )
959 numStretchableSpaces++;
962 node = node->GetNext();
965 // layout non-native toolbar
967 bool isHorizontal = !IsVertical();
972 int x = m_xMargin + kwxMacToolBarLeftMargin;
973 int y = m_yMargin + kwxMacToolBarTopMargin;
975 node = m_tools.GetFirst();
978 tool = (wxToolBarTool*) node->GetData();
981 node = node->GetNext();
985 // set tool position:
986 // for the moment just perform a single row/column alignment
987 wxSize cursize = tool->GetSize();
988 if ( x + cursize.x > maxWidth )
989 maxWidth = x + cursize.x;
990 if ( y + cursize.y > maxHeight )
991 maxHeight = y + cursize.y;
993 // update the item positioning state
995 y += cursize.y + kwxMacToolSpacing;
997 x += cursize.x + kwxMacToolSpacing;
999 node = node->GetNext();
1004 // if not set yet, only one row
1005 if ( m_maxRows <= 0 )
1008 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1009 m_minWidth = maxWidth;
1010 m_minHeight = m_maxHeight = maxToolHeight + 2 * (m_yMargin + kwxMacToolBarTopMargin);
1014 // if not set yet, have one column
1015 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1016 SetRows( GetToolsCount() );
1018 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1019 m_minHeight = maxHeight;
1020 m_minWidth = m_maxWidth = maxToolWidth + 2 * (m_yMargin + kwxMacToolBarTopMargin);
1023 int totalStretchableSpace = 0;
1024 int spacePerStretchable = 0;
1025 if ( numStretchableSpaces > 0 )
1028 totalStretchableSpace = tw - maxWidth;
1030 totalStretchableSpace = th - maxHeight;
1032 if ( totalStretchableSpace > 0 )
1033 spacePerStretchable = totalStretchableSpace / numStretchableSpaces;
1036 // perform real positioning
1038 x = m_xMargin + kwxMacToolBarLeftMargin;
1039 y = m_yMargin + kwxMacToolBarTopMargin;
1041 node = m_tools.GetFirst();
1042 int currentStretchable = 0;
1045 tool = (wxToolBarTool*) node->GetData();
1048 node = node->GetNext();
1052 wxSize cursize = tool->GetSize();
1053 if ( tool->IsStretchableSpace() )
1055 ++currentStretchable;
1056 int thisSpace = currentStretchable == numStretchableSpaces ?
1057 totalStretchableSpace - (currentStretchable-1)*spacePerStretchable :
1058 spacePerStretchable;
1060 cursize.x += thisSpace;
1062 cursize.y += thisSpace;
1065 if ( !isHorizontal )
1067 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1068 tool->SetPosition( wxPoint(x1, y) );
1072 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1073 tool->SetPosition( wxPoint(x, y1) );
1076 // update the item positioning state
1077 if ( !isHorizontal )
1078 y += cursize.y + kwxMacToolSpacing;
1080 x += cursize.x + kwxMacToolSpacing;
1082 node = node->GetNext();
1087 bool wxToolBar::Realize()
1089 if ( !wxToolBarBase::Realize() )
1092 wxToolBarTool *tool;
1093 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1095 #if wxOSX_USE_NATIVE_TOOLBAR
1096 CFIndex currentPosition = 0;
1097 bool insertAll = false;
1099 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
1104 enc = f.GetEncoding();
1106 enc = wxFont::GetDefaultEncoding();
1108 node = m_tools.GetFirst();
1111 tool = (wxToolBarTool*) node->GetData();
1114 node = node->GetNext();
1118 // install in native NSToolbar
1121 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
1122 if ( hiItemRef != NULL )
1124 // since setting the help texts is non-virtual we have to update
1126 wxCFStringRef sh( tool->GetShortHelp(), enc);
1127 [hiItemRef setToolTip:sh.AsNSString()];
1129 if ( insertAll || (tool->GetIndex() != currentPosition) )
1135 // if this is the first tool that gets newly inserted or repositioned
1136 // first remove all 'old' tools from here to the right, because of this
1137 // all following tools will have to be reinserted (insertAll).
1138 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
1140 node2 = node2->GetPrevious() )
1142 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
1144 const long idx = tool2->GetIndex();
1147 [refTB removeItemAtIndex:idx];
1148 tool2->SetIndex(-1);
1153 wxCFStringRef cfidentifier;
1155 if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR)
1157 if ( tool->IsStretchable() )
1158 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1161 if ( UMAGetSystemVersion() < 0x1070 )
1162 nsItemId = NSToolbarSeparatorItemIdentifier;
1164 nsItemId = NSToolbarSpaceItemIdentifier;
1169 cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool));
1170 nsItemId = cfidentifier.AsNSString();
1173 [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition];
1174 tool->SetIndex( currentPosition );
1180 node = node->GetNext();
1187 // adjust radio items
1189 bool lastIsRadio = false;
1190 bool curIsRadio = false;
1192 node = m_tools.GetFirst();
1195 tool = (wxToolBarTool*) node->GetData();
1198 node = node->GetNext();
1202 // update radio button (and group) state
1203 lastIsRadio = curIsRadio;
1204 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1208 if ( tool->IsToggled() )
1209 DoToggleTool( tool, true );
1215 if ( tool->Toggle( true ) )
1217 DoToggleTool( tool, true );
1220 else if ( tool->IsToggled() )
1222 if ( tool->IsToggled() )
1223 DoToggleTool( tool, true );
1225 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1228 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1229 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1232 if ( toggleTool->Toggle( false ) )
1233 DoToggleTool( toggleTool, false );
1235 nodePrev = nodePrev->GetPrevious();
1240 node = node->GetNext();
1243 InvalidateBestSize();
1244 SetInitialSize( wxSize(m_minWidth, m_minHeight));
1246 SendSizeEventToParent();
1247 wxWindow * const parent = GetParent();
1248 if ( parent && !parent->IsBeingDeleted() )
1249 parent->MacOnInternalSize();
1254 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1256 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
1261 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1263 m_defaultWidth = size.x + kwxMacToolBorder;
1264 m_defaultHeight = size.y + kwxMacToolBorder;
1266 #if wxOSX_USE_NATIVE_TOOLBAR
1267 if (m_macToolbar != NULL)
1269 int maxs = wxMax( size.x, size.y );
1270 NSToolbarSizeMode sizeSpec;
1272 sizeSpec = NSToolbarSizeModeRegular;
1273 else if ( maxs > 24 )
1274 sizeSpec = NSToolbarSizeModeDefault;
1276 sizeSpec = NSToolbarSizeModeSmall;
1278 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1283 // The button size is bigger than the bitmap size
1284 wxSize wxToolBar::GetToolSize() const
1286 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1289 void wxToolBar::SetRows(int nRows)
1291 // avoid resizing the frame uselessly
1292 if ( nRows != m_maxRows )
1296 void wxToolBar::MacSuperChangedPosition()
1298 wxWindow::MacSuperChangedPosition();
1301 #if wxOSX_USE_NATIVE_TOOLBAR
1302 if (! m_macUsesNativeToolbar )
1311 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1313 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1316 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1318 tool->SetNormalBitmap(bitmap);
1320 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1321 tool->UpdateImages();
1325 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1327 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1330 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1332 tool->SetDisabledBitmap(bitmap);
1334 // TODO: what to do for this one?
1338 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1340 wxToolBarTool *tool;
1341 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1344 tool = (wxToolBarTool *)node->GetData();
1347 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1348 if ( r.Contains( wxPoint( x, y ) ) )
1352 node = node->GetNext();
1358 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1360 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1362 return tool->GetShortHelp();
1364 return wxEmptyString;
1367 void wxToolBar::DoEnableTool(wxToolBarToolBase * WXUNUSED(t), bool WXUNUSED(enable))
1369 // everything already done in the tool's Enable implementation
1372 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1374 wxToolBarTool *tool = (wxToolBarTool *)t;
1375 if ( ( tool != NULL ) && tool->IsButton() )
1376 tool->UpdateToggleImage( toggle );
1379 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1381 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1385 long style = GetWindowStyleFlag();
1387 wxSize toolSize = GetToolSize();
1388 WXWidget controlHandle = NULL;
1389 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1391 #if wxOSX_USE_NATIVE_TOOLBAR
1392 wxString label = tool->GetLabel();
1393 if (m_macToolbar && !label.empty() )
1395 // strip mnemonics from the label for compatibility
1396 // with the usual labels in wxStaticText sense
1397 label = wxStripMenuCodes(label);
1399 #endif // wxOSX_USE_NATIVE_TOOLBAR
1401 switch (tool->GetStyle())
1403 case wxTOOL_STYLE_SEPARATOR:
1405 wxASSERT( tool->GetControlHandle() == NULL );
1409 toolrect.size.height = toolSize.y;
1411 toolrect.size.width = toolSize.x;
1413 // in flat style we need a visual separator
1414 #if wxOSX_USE_NATIVE_TOOLBAR
1415 if (m_macToolbar != NULL)
1417 NSString * nsItemId = nil;
1419 if ( tool->IsStretchable() )
1420 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1423 if ( UMAGetSystemVersion() < 0x1070 )
1424 nsItemId = NSToolbarSeparatorItemIdentifier;
1426 nsItemId = NSToolbarSpaceItemIdentifier;
1429 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId];
1430 tool->SetToolbarItemRef( item );
1432 #endif // wxOSX_USE_NATIVE_TOOLBAR
1434 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1435 [box setBoxType:NSBoxSeparator];
1436 controlHandle = box;
1437 tool->SetControlHandle( controlHandle );
1441 case wxTOOL_STYLE_BUTTON:
1443 wxASSERT( tool->GetControlHandle() == NULL );
1445 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1447 [v setBezelStyle:NSSmallSquareBezelStyle];
1448 [[v cell] setControlSize:NSSmallControlSize];
1449 [v setFont:[NSFont fontWithName:[[v font] fontName] size:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
1451 [v setButtonType: ( tool->CanBeToggled() ? NSToggleButton : NSMomentaryPushInButton )];
1452 [v setImplementation:tool];
1456 #if wxOSX_USE_NATIVE_TOOLBAR
1457 if (m_macToolbar != NULL)
1459 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1460 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1461 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1462 [item setImplementation:tool];
1463 tool->SetToolbarItemRef( item );
1466 #endif // wxOSX_USE_NATIVE_TOOLBAR
1467 tool->SetControlHandle( controlHandle );
1468 tool->UpdateImages();
1469 tool->UpdateLabel();
1471 if ( style & wxTB_NOICONS )
1472 [v setImagePosition:NSNoImage];
1473 else if ( style & wxTB_TEXT )
1474 [v setImagePosition:NSImageAbove];
1476 [v setImagePosition:NSImageOnly];
1480 InstallControlEventHandler(
1481 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1482 GetEventTypeCount(eventList), eventList, tool, NULL );
1487 case wxTOOL_STYLE_CONTROL:
1489 #if wxOSX_USE_NATIVE_TOOLBAR
1490 if (m_macToolbar != NULL)
1492 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1493 wxCHECK_MSG( view, false, wxT("control must be non-NULL") );
1495 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1496 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1497 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1498 [item setImplementation:tool];
1499 tool->SetToolbarItemRef( item );
1502 // right now there's nothing to do here
1504 tool->UpdateLabel();
1511 if ( controlHandle )
1513 WXWidget container = (WXWidget) GetHandle();
1514 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1516 // SetControlVisibility( controlHandle, true, true );
1517 [container addSubview:controlHandle];
1520 // nothing special to do here - we relayout in Realize() later
1521 InvalidateBestSize();
1527 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1529 wxFAIL_MSG( wxT("not implemented") );
1532 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1534 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1535 wxToolBarToolsList::compatibility_iterator node;
1536 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1538 wxToolBarToolBase *tool2 = node->GetData();
1539 if ( tool2 == tool )
1541 // let node point to the next node in the list
1542 node = node->GetNext();
1548 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1550 #if wxOSX_USE_NATIVE_TOOLBAR
1551 CFIndex removeIndex = tool->GetIndex();
1554 #if wxOSX_USE_NATIVE_TOOLBAR
1555 if (m_macToolbar != NULL)
1557 if ( removeIndex != -1 && m_macToolbar )
1559 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1560 tool->SetIndex( -1 );
1565 tool->ClearControl();
1567 // and finally reposition all the controls after this one
1569 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1571 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1572 wxPoint pt = tool2->GetPosition();
1579 tool2->SetPosition( pt );
1581 #if wxOSX_USE_NATIVE_TOOLBAR
1582 if (m_macToolbar != NULL)
1584 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1585 tool2->SetIndex( tool2->GetIndex() - 1 );
1590 InvalidateBestSize();
1595 #include <Carbon/Carbon.h>
1597 void wxToolBar::OnPaint(wxPaintEvent& event)
1599 #if wxOSX_USE_NATIVE_TOOLBAR
1600 if ( m_macUsesNativeToolbar )
1602 // nothing to do here
1612 wxRect rect(0,0,w,h);
1614 dc.GradientFillLinear( rect , wxColour( 0xCC,0xCC,0xCC ), wxColour( 0xA8,0xA8,0xA8 ) , wxSOUTH );
1615 dc.SetPen( wxPen( wxColour( 0x51,0x51,0x51 ) ) );
1616 if ( HasFlag(wxTB_LEFT) )
1617 dc.DrawLine(w-1, 0, w-1, h);
1618 else if ( HasFlag(wxTB_RIGHT) )
1619 dc.DrawLine(0, 0, 0, h);
1620 else if ( HasFlag(wxTB_BOTTOM) )
1621 dc.DrawLine(0, 0, w, 0);
1622 else if ( HasFlag(wxTB_TOP) )
1623 dc.DrawLine(0, h-1, w, h-1);
1628 #if wxOSX_USE_NATIVE_TOOLBAR
1629 void wxToolBar::OSXSetSelectableTools(bool set)
1631 wxCHECK_RET( m_macToolbar, "toolbar must be non-NULL" );
1632 [(wxNSToolbarDelegate*)[(NSToolbar*)m_macToolbar delegate] setSelectable:set];
1635 void wxToolBar::OSXSelectTool(int toolId)
1637 wxToolBarToolBase *tool = FindById(toolId);
1638 wxCHECK_RET( tool, "invalid tool ID" );
1639 wxCHECK_RET( m_macToolbar, "toolbar must be non-NULL" );
1641 wxString identifier = wxString::Format(wxT("%ld"), (long)tool);
1642 wxCFStringRef cfidentifier(identifier, wxFont::GetDefaultEncoding());
1643 [(NSToolbar*)m_macToolbar setSelectedItemIdentifier:cfidentifier.AsNSString()];
1645 #endif // wxOSX_USE_NATIVE_TOOLBAR
1647 #endif // wxUSE_TOOLBAR