1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/toolbar.cpp
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: toolbar.cpp 54954 2008-08-03 11:27:03Z VZ $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/toolbar.h"
23 #include "wx/osx/private.h"
24 #include "wx/geometry.h"
25 #include "wx/sysopt.h"
27 const short kwxMacToolBarToolDefaultWidth = 16;
28 const short kwxMacToolBarToolDefaultHeight = 16;
29 const short kwxMacToolBarTopMargin = 4;
30 const short kwxMacToolBarLeftMargin = 4;
31 const short kwxMacToolBorder = 0;
32 const short kwxMacToolSpacing = 6;
34 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
35 EVT_PAINT( wxToolBar::OnPaint )
40 #pragma mark Tool Implementation
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // We have a dual implementation for each tool, WXWidget and NSToolbarItem*
48 // when embedding native controls in the native toolbar we must make sure the
49 // control does not get deleted behind our backs, so the retain count gets increased
50 // (after creation it is 1), first be the creation of the custom NSToolbarItem wrapper
51 // object, and second by the code 'creating' the custom HIView (which is the same as the
52 // already existing native control, therefore we just increase the ref count)
53 // when this view is removed from the native toolbar its count gets decremented again
54 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
55 // so in the end the control lives with a refcount of one and can be disposed of by the
56 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
57 // so we can only test against a range, not a specific value of the refcount.
59 class wxToolBarTool : public wxToolBarToolBase
65 const wxString& label,
66 const wxBitmap& bmpNormal,
67 const wxBitmap& bmpDisabled,
70 const wxString& shortHelp,
71 const wxString& longHelp );
73 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
74 : wxToolBarToolBase(tbar, control, label)
78 SetControlHandle( (WXWidget) control->GetHandle() );
81 virtual ~wxToolBarTool()
86 WXWidget GetControlHandle()
88 return (WXWidget) m_controlHandle;
91 void SetControlHandle( WXWidget handle )
93 m_controlHandle = handle;
96 void SetPosition( const wxPoint& position );
100 if ( m_controlHandle )
104 [m_controlHandle retain];
108 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
109 // proper wxControl destructor
111 m_controlHandle = NULL ;
114 #if wxOSX_USE_NATIVE_TOOLBAR
117 [m_toolbarItem release];
118 m_toolbarItem = NULL;
120 #endif // wxOSX_USE_NATIVE_TOOLBAR
123 wxSize GetSize() const
129 curSize = GetControl()->GetSize();
131 else if ( IsButton() )
133 curSize = GetToolBar()->GetToolSize();
138 curSize = GetToolBar()->GetToolSize();
139 if ( GetToolBar()->GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
148 wxPoint GetPosition() const
150 return wxPoint( m_x, m_y );
153 bool Enable( bool enable );
157 void UpdateToggleImage( bool toggle );
161 #if wxOSX_USE_NATIVE_TOOLBAR
164 // strip mnemonics from the label for compatibility with the usual
165 // labels in wxStaticText sense
166 wxString labelStr = wxStripMenuCodes(m_label);
167 wxCFStringRef l(labelStr, GetToolBarFontEncoding());
169 [m_toolbarItem setLabel:l.AsNSString()];
171 wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() );
172 [m_toolbarItem setToolTip:sh.AsNSString()];
179 wxToolBar *tbar = (wxToolBar*) GetToolBar();
184 shouldToggle = !IsToggled();
185 tbar->ToggleTool( GetId(), shouldToggle );
188 tbar->OnLeftClick( GetId(), IsToggled() );
191 #if wxOSX_USE_NATIVE_TOOLBAR
192 void SetToolbarItemRef( NSToolbarItem* ref )
194 if ( m_controlHandle )
195 [m_controlHandle setHidden:YES];
197 [m_toolbarItem release];
202 NSToolbarItem* GetToolbarItemRef() const
204 return m_toolbarItem;
207 void SetIndex( CFIndex idx )
212 CFIndex GetIndex() const
217 virtual void SetLabel(const wxString& label)
219 wxToolBarToolBase::SetLabel(label);
222 #endif // wxOSX_USE_NATIVE_TOOLBAR
225 #if wxOSX_USE_NATIVE_TOOLBAR
226 wxFontEncoding GetToolBarFontEncoding() const
230 f = GetToolBar()->GetFont();
231 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
233 #endif // wxOSX_USE_NATIVE_TOOLBAR
237 m_controlHandle = NULL;
239 #if wxOSX_USE_NATIVE_TOOLBAR
240 m_toolbarItem = NULL;
245 WXWidget m_controlHandle;
248 wxBitmap m_alternateBitmap;
250 #if wxOSX_USE_NATIVE_TOOLBAR
251 NSToolbarItem* m_toolbarItem;
252 // position in its toolbar, -1 means not inserted
257 #if wxOSX_USE_NATIVE_TOOLBAR
259 @interface wxNSToolbarItem : NSToolbarItem
264 - (id) initWithItemIdentifier: (NSString*) identifier;
265 - (void)setImplementation: (wxToolBarTool *) theImplementation;
266 - (wxToolBarTool*) implementation;
267 - (void) clickedAction: (id) sender;
268 - (BOOL) validateToolbarItem:(NSToolbarItem *)theItem;
273 @interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER(<NSToolbarDelegate>)
277 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
279 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
281 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
283 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
291 @interface wxNSToolBarButton : NSButton
296 - (id)initWithFrame:(NSRect)frame;
297 - (void) clickedAction: (id) sender;
298 - (void)setImplementation: (wxToolBarTool *) theImplementation;
299 - (wxToolBarTool*) implementation;
304 #if wxOSX_USE_NATIVE_TOOLBAR
306 @implementation wxNSToolbarItem
308 - (id)initWithItemIdentifier: (NSString*) identifier
310 [super initWithItemIdentifier:identifier];
312 [self setTarget: self];
313 [self setAction: @selector(clickedAction:)];
317 - (void) clickedAction: (id) sender
326 - (void)setImplementation: (wxToolBarTool *) theImplementation
328 impl = theImplementation;
331 - (wxToolBarTool*) implementation
336 - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
338 wxUnusedVar(theItem);
339 return impl->IsEnabled() ? YES:NO;
344 @implementation wxNSToolbarDelegate
346 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
348 wxUnusedVar(toolbar);
352 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
354 wxUnusedVar(toolbar);
358 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
360 wxUnusedVar(toolbar);
364 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
366 wxUnusedVar(toolbar);
368 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue];
370 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
374 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
375 if ( flag && tool->IsControl() )
377 NSView* view = tool->GetControl()->GetHandle();
378 [view removeFromSuperview];
380 wxSize sz = tool->GetControl()->GetSize();
381 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
382 [item setMaxSize:size];
383 [item setMinSize:size];
395 @implementation wxNSToolBarButton
397 - (id)initWithFrame:(NSRect)frame
399 [super initWithFrame:frame];
401 [self setTarget: self];
402 [self setAction: @selector(clickedAction:)];
406 - (void) clickedAction: (id) sender
415 - (void)setImplementation: (wxToolBarTool *) theImplementation
417 impl = theImplementation;
420 - (wxToolBarTool*) implementation
432 bool wxToolBarTool::Enable( bool enable )
434 if ( wxToolBarToolBase::Enable( enable ) == false )
439 GetControl()->Enable( enable );
441 else if ( IsButton() )
443 #if wxOSX_USE_NATIVE_TOOLBAR
444 if ( m_toolbarItem != NULL )
445 [m_toolbarItem setEnabled:enable];
448 if ( m_controlHandle != NULL )
449 [(NSControl*)m_controlHandle setEnabled:enable];
455 void wxToolBarTool::SetPosition( const wxPoint& position )
460 int mac_x = position.x;
461 int mac_y = position.y;
465 NSRect frame = [m_controlHandle frame];
466 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
468 frame.origin.x = mac_x;
469 frame.origin.y = mac_y;
470 [m_controlHandle setFrame:frame];
473 else if ( IsControl() )
475 // embedded native controls are moved by the OS
476 #if wxOSX_USE_NATIVE_TOOLBAR
477 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
480 GetControl()->Move( position );
485 NSRect frame = [m_controlHandle frame];
486 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
488 frame.origin.x = mac_x;
489 frame.origin.y = mac_y;
490 [m_controlHandle setFrame:frame];
495 void wxToolBarTool::UpdateImages()
497 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
499 if ( CanBeToggled() )
501 int w = m_bmpNormal.GetWidth();
502 int h = m_bmpNormal.GetHeight();
503 m_alternateBitmap = wxBitmap( w, h );
506 dc.SelectObject( m_alternateBitmap );
507 dc.SetPen( wxPen(*wxBLACK) );
508 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
509 dc.DrawRectangle( 0, 0, w, h );
510 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
511 dc.SelectObject( wxNullBitmap );
513 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
515 UpdateToggleImage( CanBeToggled() && IsToggled() );
518 void wxToolBarTool::UpdateToggleImage( bool toggle )
520 #if wxOSX_USE_NATIVE_TOOLBAR
521 if (m_toolbarItem != NULL )
523 // the native toolbar item only has a 'selected' state (one for one toolbar)
524 // so we emulate the toggle here
525 if ( CanBeToggled() && toggle )
526 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
528 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
533 wxToolBarTool::wxToolBarTool(
536 const wxString& label,
537 const wxBitmap& bmpNormal,
538 const wxBitmap& bmpDisabled,
540 wxObject *clientData,
541 const wxString& shortHelp,
542 const wxString& longHelp )
545 tbar, id, label, bmpNormal, bmpDisabled, kind,
546 clientData, shortHelp, longHelp )
552 #pragma mark Toolbar Implementation
554 wxToolBarToolBase *wxToolBar::CreateTool(
556 const wxString& label,
557 const wxBitmap& bmpNormal,
558 const wxBitmap& bmpDisabled,
560 wxObject *clientData,
561 const wxString& shortHelp,
562 const wxString& longHelp )
564 return new wxToolBarTool(
565 this, id, label, bmpNormal, bmpDisabled, kind,
566 clientData, shortHelp, longHelp );
570 wxToolBar::CreateTool(wxControl *control, const wxString& label)
572 return new wxToolBarTool(this, control, label);
575 void wxToolBar::Init()
579 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
580 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
582 #if wxOSX_USE_NATIVE_TOOLBAR
584 m_macUsesNativeToolbar = false;
588 // also for the toolbar we have the dual implementation:
589 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
591 bool wxToolBar::Create(
597 const wxString& name )
599 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
604 OSStatus err = noErr;
606 #if wxOSX_USE_NATIVE_TOOLBAR
608 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
610 static wxNSToolbarDelegate* controller = nil;
612 if ( controller == nil )
613 controller = [[wxNSToolbarDelegate alloc] init];
614 wxString identifier = wxString::Format( wxT("%p"), this );
615 wxCFStringRef cfidentifier(identifier);
616 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
620 if (m_macToolbar != NULL)
622 [tb setDelegate:controller];
624 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
625 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
627 if ( style & wxTB_NOICONS )
628 mode = NSToolbarDisplayModeLabelOnly;
629 else if ( style & wxTB_TEXT )
630 mode = NSToolbarDisplayModeIconAndLabel;
632 mode = NSToolbarDisplayModeIconOnly;
634 [tb setDisplayMode:mode];
635 [tb setSizeMode:displaySize];
638 #endif // wxOSX_USE_NATIVE_TOOLBAR
640 return (err == noErr);
643 wxToolBar::~wxToolBar()
645 [(NSToolbar*)m_macToolbar setDelegate:nil];
646 [(NSToolbar*)m_macToolbar release];
650 bool wxToolBar::Show( bool show )
652 WXWindow tlw = MacGetTopLevelWindowRef();
653 bool bResult = (tlw != NULL);
657 #if wxOSX_USE_NATIVE_TOOLBAR
658 bool ownToolbarInstalled = false;
659 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
660 if (ownToolbarInstalled)
662 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
664 [(NSToolbar*)m_macToolbar setVisible:show];
667 bResult = wxToolBarBase::Show( show );
670 bResult = wxToolBarBase::Show( show );
677 bool wxToolBar::IsShown() const
681 #if wxOSX_USE_NATIVE_TOOLBAR
682 bool ownToolbarInstalled;
684 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
685 if (ownToolbarInstalled)
687 bResult = [(NSToolbar*)m_macToolbar isVisible];
690 bResult = wxToolBarBase::IsShown();
693 bResult = wxToolBarBase::IsShown();
699 void wxToolBar::DoGetSize( int *width, int *height ) const
701 #if wxOSX_USE_NATIVE_TOOLBAR
702 bool ownToolbarInstalled;
704 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
705 if ( ownToolbarInstalled )
707 WXWindow tlw = MacGetTopLevelWindowRef();
708 float toolbarHeight = 0.0;
709 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
711 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
713 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
714 styleMask:[tlw styleMask]];
715 toolbarHeight = NSHeight(windowFrame)
716 - NSHeight([[tlw contentView] frame]);
720 *width = (int)windowFrame.size.width;
721 if ( height != NULL )
722 *height = (int)toolbarHeight;
725 wxToolBarBase::DoGetSize( width, height );
728 wxToolBarBase::DoGetSize( width, height );
732 wxSize wxToolBar::DoGetBestSize() const
736 DoGetSize( &width, &height );
738 return wxSize( width, height );
741 void wxToolBar::SetWindowStyleFlag( long style )
743 wxToolBarBase::SetWindowStyleFlag( style );
745 #if wxOSX_USE_NATIVE_TOOLBAR
746 if (m_macToolbar != NULL)
748 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
750 if ( style & wxTB_NOICONS )
751 mode = NSToolbarDisplayModeLabelOnly;
752 else if ( style & wxTB_TEXT )
753 mode = NSToolbarDisplayModeIconAndLabel;
755 mode = NSToolbarDisplayModeIconOnly;
757 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
762 #if wxOSX_USE_NATIVE_TOOLBAR
763 bool wxToolBar::MacWantsNativeToolbar()
765 return m_macUsesNativeToolbar;
768 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
770 bool bResultV = false;
772 if (ownToolbarInstalled != NULL)
773 *ownToolbarInstalled = false;
775 WXWindow tlw = MacGetTopLevelWindowRef();
778 NSToolbar* curToolbarRef = [tlw toolbar];
779 bResultV = (curToolbarRef != NULL);
780 if (bResultV && (ownToolbarInstalled != NULL))
781 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
787 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
789 bool bResult = false;
791 if (usesNative && (m_macToolbar == NULL))
794 if (usesNative && ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM)) != 0))
797 WXWindow tlw = MacGetTopLevelWindowRef();
801 // check the existing toolbar
802 NSToolbar* curToolbarRef = [tlw toolbar];
804 m_macUsesNativeToolbar = usesNative;
806 if (m_macUsesNativeToolbar)
808 // only install toolbar if there isn't one installed already
809 if (curToolbarRef == NULL)
812 [tlw setToolbar:(NSToolbar*) m_macToolbar];
813 [(NSToolbar*) m_macToolbar setVisible:YES];
815 m_peer->Move(0,0,0,0 );
816 SetSize( wxSIZE_AUTO_WIDTH, 0 );
817 m_peer->SetVisibility( false );
818 wxToolBarBase::Show( false );
823 // only deinstall toolbar if this is the installed one
824 if (m_macToolbar == curToolbarRef)
827 [(NSToolbar*) m_macToolbar setVisible:NO];
828 MacUninstallNativeToolbar();
829 m_peer->SetVisibility( true );
834 InvalidateBestSize();
836 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
840 void wxToolBar::MacUninstallNativeToolbar()
845 WXWindow tlw = MacGetTopLevelWindowRef();
847 [tlw setToolbar:nil];
851 bool wxToolBar::Realize()
853 if ( !wxToolBarBase::Realize() )
859 int maxToolWidth = 0;
860 int maxToolHeight = 0;
862 int x = m_xMargin + kwxMacToolBarLeftMargin;
863 int y = m_yMargin + kwxMacToolBarTopMargin;
868 // find the maximum tool width and height
870 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
873 tool = (wxToolBarTool *) node->GetData();
876 wxSize sz = tool->GetSize();
878 if ( sz.x > maxToolWidth )
880 if ( sz.y > maxToolHeight )
881 maxToolHeight = sz.y;
884 node = node->GetNext();
887 bool lastIsRadio = false;
888 bool curIsRadio = false;
890 #if wxOSX_USE_NATIVE_TOOLBAR
891 CFIndex currentPosition = 0;
892 bool insertAll = false;
894 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
899 enc = f.GetEncoding();
901 enc = wxFont::GetDefaultEncoding();
904 node = m_tools.GetFirst();
907 tool = (wxToolBarTool*) node->GetData();
910 node = node->GetNext();
914 // set tool position:
915 // for the moment just perform a single row/column alignment
916 wxSize cursize = tool->GetSize();
917 if ( x + cursize.x > maxWidth )
918 maxWidth = x + cursize.x;
919 if ( y + cursize.y > maxHeight )
920 maxHeight = y + cursize.y;
922 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
924 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
925 tool->SetPosition( wxPoint(x1, y) );
929 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
930 tool->SetPosition( wxPoint(x, y1) );
933 // update the item positioning state
934 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
935 y += cursize.y + kwxMacToolSpacing;
937 x += cursize.x + kwxMacToolSpacing;
939 #if wxOSX_USE_NATIVE_TOOLBAR
940 // install in native NSToolbar
943 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
944 if ( hiItemRef != NULL )
946 // since setting the help texts is non-virtual we have to update
948 wxCFStringRef sh( tool->GetShortHelp(), enc);
949 [hiItemRef setToolTip:sh.AsNSString()];
951 if ( insertAll || (tool->GetIndex() != currentPosition) )
957 // if this is the first tool that gets newly inserted or repositioned
958 // first remove all 'old' tools from here to the right, because of this
959 // all following tools will have to be reinserted (insertAll).
960 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
962 node2 = node2->GetPrevious() )
964 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
966 const long idx = tool2->GetIndex();
969 [refTB removeItemAtIndex:idx];
975 wxCFStringRef cfidentifier;
976 const NSString *nsItemId;
977 if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR)
979 nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier
980 : NSToolbarSeparatorItemIdentifier;
984 cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool));
985 nsItemId = cfidentifier.AsNSString();
988 [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition];
989 tool->SetIndex( currentPosition );
997 // update radio button (and group) state
998 lastIsRadio = curIsRadio;
999 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1003 if ( tool->IsToggled() )
1004 DoToggleTool( tool, true );
1010 if ( tool->Toggle( true ) )
1012 DoToggleTool( tool, true );
1015 else if ( tool->IsToggled() )
1017 if ( tool->IsToggled() )
1018 DoToggleTool( tool, true );
1020 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1023 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1024 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1027 if ( toggleTool->Toggle( false ) )
1028 DoToggleTool( toggleTool, false );
1030 nodePrev = nodePrev->GetPrevious();
1035 node = node->GetNext();
1038 if ( GetWindowStyleFlag() & (wxTB_TOP|wxTB_BOTTOM) )
1040 // if not set yet, only one row
1041 if ( m_maxRows <= 0 )
1044 m_minWidth = maxWidth;
1046 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1047 m_minHeight = m_maxHeight = maxHeight;
1051 // if not set yet, have one column
1052 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1053 SetRows( GetToolsCount() );
1055 m_minHeight = maxHeight;
1057 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1058 m_minWidth = m_maxWidth = maxWidth;
1062 // FIXME: should this be OSX-only?
1064 bool wantNativeToolbar, ownToolbarInstalled;
1066 // attempt to install the native toolbar
1067 wantNativeToolbar = ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_BOTTOM|wxTB_RIGHT)) == 0);
1068 MacInstallNativeToolbar( wantNativeToolbar );
1069 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1070 if (!ownToolbarInstalled)
1072 SetSize( maxWidth, maxHeight );
1073 InvalidateBestSize();
1077 SetSize( maxWidth, maxHeight );
1078 InvalidateBestSize();
1086 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1088 m_defaultWidth = size.x + kwxMacToolBorder;
1089 m_defaultHeight = size.y + kwxMacToolBorder;
1091 #if wxOSX_USE_NATIVE_TOOLBAR
1092 if (m_macToolbar != NULL)
1094 int maxs = wxMax( size.x, size.y );
1095 NSToolbarSizeMode sizeSpec;
1097 sizeSpec = NSToolbarSizeModeRegular;
1098 else if ( maxs > 24 )
1099 sizeSpec = NSToolbarSizeModeDefault;
1101 sizeSpec = NSToolbarSizeModeSmall;
1103 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1108 // The button size is bigger than the bitmap size
1109 wxSize wxToolBar::GetToolSize() const
1111 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1114 void wxToolBar::SetRows(int nRows)
1116 // avoid resizing the frame uselessly
1117 if ( nRows != m_maxRows )
1121 void wxToolBar::MacSuperChangedPosition()
1123 wxWindow::MacSuperChangedPosition();
1125 #if wxOSX_USE_NATIVE_TOOLBAR
1126 if (! m_macUsesNativeToolbar )
1134 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1136 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1139 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1141 tool->SetNormalBitmap(bitmap);
1143 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1144 tool->UpdateImages();
1148 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1150 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1153 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1155 tool->SetDisabledBitmap(bitmap);
1157 // TODO: what to do for this one?
1161 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1163 wxToolBarTool *tool;
1164 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1167 tool = (wxToolBarTool *)node->GetData();
1170 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1171 if ( r.Contains( wxPoint( x, y ) ) )
1175 node = node->GetNext();
1181 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1183 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1185 return tool->GetShortHelp();
1187 return wxEmptyString;
1190 void wxToolBar::DoEnableTool(wxToolBarToolBase * WXUNUSED(t), bool WXUNUSED(enable))
1192 // everything already done in the tool's Enable implementation
1195 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1197 wxToolBarTool *tool = (wxToolBarTool *)t;
1198 if ( ( tool != NULL ) && tool->IsButton() )
1199 tool->UpdateToggleImage( toggle );
1202 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1204 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1208 wxSize toolSize = GetToolSize();
1209 WXWidget controlHandle = NULL;
1210 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1212 #if wxOSX_USE_NATIVE_TOOLBAR
1213 wxString label = tool->GetLabel();
1214 if (m_macToolbar && !label.empty() )
1216 // strip mnemonics from the label for compatibility
1217 // with the usual labels in wxStaticText sense
1218 label = wxStripMenuCodes(label);
1220 #endif // wxOSX_USE_NATIVE_TOOLBAR
1222 switch (tool->GetStyle())
1224 case wxTOOL_STYLE_SEPARATOR:
1226 wxASSERT( tool->GetControlHandle() == NULL );
1229 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
1230 toolrect.size.height = toolSize.y;
1232 toolrect.size.width = toolSize.x;
1234 // in flat style we need a visual separator
1235 #if wxOSX_USE_NATIVE_TOOLBAR
1236 if (m_macToolbar != NULL)
1238 const NSString * const
1239 nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier
1240 : NSToolbarSeparatorItemIdentifier;
1241 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId];
1242 tool->SetToolbarItemRef( item );
1244 #endif // wxOSX_USE_NATIVE_TOOLBAR
1246 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1247 [box setBoxType:NSBoxSeparator];
1248 controlHandle = box;
1249 tool->SetControlHandle( controlHandle );
1253 case wxTOOL_STYLE_BUTTON:
1255 wxASSERT( tool->GetControlHandle() == NULL );
1257 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1259 [v setBezelStyle:NSRegularSquareBezelStyle];
1261 [v setButtonType: ( tool->CanBeToggled() ? NSOnOffButton : NSMomentaryPushInButton )];
1262 [v setImplementation:tool];
1266 #if wxOSX_USE_NATIVE_TOOLBAR
1267 if (m_macToolbar != NULL)
1269 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1270 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1271 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1272 [item setImplementation:tool];
1273 tool->SetToolbarItemRef( item );
1276 #endif // wxOSX_USE_NATIVE_TOOLBAR
1277 tool->SetControlHandle( controlHandle );
1278 tool->UpdateImages();
1279 tool->UpdateLabel();
1281 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1282 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1285 InstallControlEventHandler(
1286 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1287 GetEventTypeCount(eventList), eventList, tool, NULL );
1292 case wxTOOL_STYLE_CONTROL:
1294 #if wxOSX_USE_NATIVE_TOOLBAR
1295 if (m_macToolbar != NULL)
1297 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1298 wxCHECK_MSG( view, false, wxT("control must be non-NULL") );
1300 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1301 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1302 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1303 [item setImplementation:tool];
1304 tool->SetToolbarItemRef( item );
1307 // right now there's nothing to do here
1309 tool->UpdateLabel();
1316 if ( controlHandle )
1318 WXWidget container = (WXWidget) GetHandle();
1319 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1321 // SetControlVisibility( controlHandle, true, true );
1322 [container addSubview:controlHandle];
1325 // nothing special to do here - we relayout in Realize() later
1326 InvalidateBestSize();
1332 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1334 wxFAIL_MSG( wxT("not implemented") );
1337 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1339 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1340 wxToolBarToolsList::compatibility_iterator node;
1341 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1343 wxToolBarToolBase *tool2 = node->GetData();
1344 if ( tool2 == tool )
1346 // let node point to the next node in the list
1347 node = node->GetNext();
1353 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1355 #if wxOSX_USE_NATIVE_TOOLBAR
1356 CFIndex removeIndex = tool->GetIndex();
1359 #if wxOSX_USE_NATIVE_TOOLBAR
1360 if (m_macToolbar != NULL)
1362 if ( removeIndex != -1 && m_macToolbar )
1364 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1365 tool->SetIndex( -1 );
1370 tool->ClearControl();
1372 // and finally reposition all the controls after this one
1374 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1376 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1377 wxPoint pt = tool2->GetPosition();
1379 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
1384 tool2->SetPosition( pt );
1386 #if wxOSX_USE_NATIVE_TOOLBAR
1387 if (m_macToolbar != NULL)
1389 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1390 tool2->SetIndex( tool2->GetIndex() - 1 );
1395 InvalidateBestSize();
1400 #include <Carbon/Carbon.h>
1402 void wxToolBar::OnPaint(wxPaintEvent& event)
1404 #if wxOSX_USE_NATIVE_TOOLBAR
1405 if ( m_macUsesNativeToolbar )
1417 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1419 if ( !drawMetalTheme )
1421 HIThemePlacardDrawInfo info;
1422 memset( &info, 0, sizeof(info) );
1424 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1426 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1427 HIRect rect = CGRectMake( 0, 0, w, h );
1428 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1432 // leave the background as it is (striped or metal)
1438 #endif // wxUSE_TOOLBAR