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_VERTICAL )
148 wxPoint GetPosition() const
150 return wxPoint( m_x, m_y );
153 bool DoEnable( 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;
272 @interface wxNSToolbarDelegate : NSObject
276 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
278 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
280 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
282 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
290 @interface wxNSToolBarButton : NSButton
295 - (id)initWithFrame:(NSRect)frame;
296 - (void) clickedAction: (id) sender;
297 - (void)setImplementation: (wxToolBarTool *) theImplementation;
298 - (wxToolBarTool*) implementation;
303 #if wxOSX_USE_NATIVE_TOOLBAR
305 @implementation wxNSToolbarItem
307 - (id)initWithItemIdentifier: (NSString*) identifier
309 [super initWithItemIdentifier:identifier];
311 [self setTarget: self];
312 [self setAction: @selector(clickedAction:)];
316 - (void) clickedAction: (id) sender
324 - (void)setImplementation: (wxToolBarTool *) theImplementation
326 impl = theImplementation;
329 - (wxToolBarTool*) implementation
336 @implementation wxNSToolbarDelegate
338 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
343 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
348 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
353 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
356 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue];
358 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
362 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
363 if ( flag && tool->IsControl() )
365 NSView* view = tool->GetControl()->GetHandle();
366 [view removeFromSuperview];
368 wxSize sz = tool->GetControl()->GetSize();
369 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
370 [item setMaxSize:size];
371 [item setMinSize:size];
383 @implementation wxNSToolBarButton
385 - (id)initWithFrame:(NSRect)frame
387 [super initWithFrame:frame];
389 [self setTarget: self];
390 [self setAction: @selector(clickedAction:)];
394 - (void) clickedAction: (id) sender
402 - (void)setImplementation: (wxToolBarTool *) theImplementation
404 impl = theImplementation;
407 - (wxToolBarTool*) implementation
419 bool wxToolBarTool::DoEnable( bool enable )
423 GetControl()->Enable( enable );
425 else if ( IsButton() )
427 #if wxOSX_USE_NATIVE_TOOLBAR
428 if ( m_toolbarItem != NULL )
429 [m_toolbarItem setEnabled:enable];
432 if ( m_controlHandle != NULL )
433 [(NSControl*)m_controlHandle setEnabled:enable];
439 void wxToolBarTool::SetPosition( const wxPoint& position )
444 int mac_x = position.x;
445 int mac_y = position.y;
449 NSRect frame = [m_controlHandle frame];
450 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
452 frame.origin.x = mac_x;
453 frame.origin.y = mac_y;
454 [m_controlHandle setFrame:frame];
457 else if ( IsControl() )
459 // embedded native controls are moved by the OS
460 #if wxOSX_USE_NATIVE_TOOLBAR
461 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
464 GetControl()->Move( position );
469 NSRect frame = [m_controlHandle frame];
470 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
472 frame.origin.x = mac_x;
473 frame.origin.y = mac_y;
474 [m_controlHandle setFrame:frame];
479 void wxToolBarTool::UpdateImages()
481 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
483 if ( CanBeToggled() )
485 int w = m_bmpNormal.GetWidth();
486 int h = m_bmpNormal.GetHeight();
487 m_alternateBitmap = wxBitmap( w, h );
490 dc.SelectObject( m_alternateBitmap );
491 dc.SetPen( wxPen(*wxBLACK) );
492 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
493 dc.DrawRectangle( 0, 0, w, h );
494 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
495 dc.SelectObject( wxNullBitmap );
497 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
499 UpdateToggleImage( CanBeToggled() && IsToggled() );
502 void wxToolBarTool::UpdateToggleImage( bool toggle )
504 #if wxOSX_USE_NATIVE_TOOLBAR
505 if (m_toolbarItem != NULL )
507 // the native toolbar item only has a 'selected' state (one for one toolbar)
508 // so we emulate the toggle here
509 if ( CanBeToggled() && toggle )
510 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
512 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
517 wxToolBarTool::wxToolBarTool(
520 const wxString& label,
521 const wxBitmap& bmpNormal,
522 const wxBitmap& bmpDisabled,
524 wxObject *clientData,
525 const wxString& shortHelp,
526 const wxString& longHelp )
529 tbar, id, label, bmpNormal, bmpDisabled, kind,
530 clientData, shortHelp, longHelp )
536 #pragma mark Toolbar Implementation
538 wxToolBarToolBase *wxToolBar::CreateTool(
540 const wxString& label,
541 const wxBitmap& bmpNormal,
542 const wxBitmap& bmpDisabled,
544 wxObject *clientData,
545 const wxString& shortHelp,
546 const wxString& longHelp )
548 return new wxToolBarTool(
549 this, id, label, bmpNormal, bmpDisabled, kind,
550 clientData, shortHelp, longHelp );
554 wxToolBar::CreateTool(wxControl *control, const wxString& label)
556 return new wxToolBarTool(this, control, label);
559 void wxToolBar::Init()
563 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
564 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
566 #if wxOSX_USE_NATIVE_TOOLBAR
568 m_macUsesNativeToolbar = false;
572 // also for the toolbar we have the dual implementation:
573 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
575 bool wxToolBar::Create(
581 const wxString& name )
583 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
588 OSStatus err = noErr;
590 #if wxOSX_USE_NATIVE_TOOLBAR
592 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
594 static wxNSToolbarDelegate* controller = nil;
596 if ( controller == nil )
597 controller = [[wxNSToolbarDelegate alloc] init];
598 wxString identifier = wxString::Format( wxT("%p"), this );
599 wxCFStringRef cfidentifier(identifier);
600 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
604 if (m_macToolbar != NULL)
606 [tb setDelegate:controller];
608 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
609 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
611 if ( style & wxTB_NOICONS )
612 mode = NSToolbarDisplayModeLabelOnly;
613 else if ( style & wxTB_TEXT )
614 mode = NSToolbarDisplayModeIconAndLabel;
616 mode = NSToolbarDisplayModeIconOnly;
618 [tb setDisplayMode:mode];
619 [tb setSizeMode:displaySize];
622 #endif // wxOSX_USE_NATIVE_TOOLBAR
624 return (err == noErr);
627 wxToolBar::~wxToolBar()
629 #if wxOSX_USE_NATIVE_TOOLBAR
630 if (m_macToolbar != NULL)
632 // if this is the installed toolbar, then deinstall it
633 if (m_macUsesNativeToolbar)
634 MacInstallNativeToolbar( false );
636 [(NSToolbar*)m_macToolbar setDelegate:nil];
637 [(NSToolbar*)m_macToolbar release];
643 bool wxToolBar::Show( bool show )
645 WXWindow tlw = MacGetTopLevelWindowRef();
646 bool bResult = (tlw != NULL);
650 #if wxOSX_USE_NATIVE_TOOLBAR
651 bool ownToolbarInstalled = false;
652 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
653 if (ownToolbarInstalled)
655 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
657 [(NSToolbar*)m_macToolbar setVisible:show];
660 bResult = wxToolBarBase::Show( show );
663 bResult = wxToolBarBase::Show( show );
670 bool wxToolBar::IsShown() const
674 #if wxOSX_USE_NATIVE_TOOLBAR
675 bool ownToolbarInstalled;
677 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
678 if (ownToolbarInstalled)
680 bResult = [(NSToolbar*)m_macToolbar isVisible];
683 bResult = wxToolBarBase::IsShown();
686 bResult = wxToolBarBase::IsShown();
692 void wxToolBar::DoGetSize( int *width, int *height ) const
694 #if wxOSX_USE_NATIVE_TOOLBAR
695 bool ownToolbarInstalled;
697 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
698 if ( ownToolbarInstalled )
700 WXWindow tlw = MacGetTopLevelWindowRef();
701 float toolbarHeight = 0.0;
704 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
706 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
707 styleMask:[tlw styleMask]];
708 toolbarHeight = NSHeight(windowFrame)
709 - NSHeight([[tlw contentView] frame]);
713 *width = windowFrame.size.width;
714 if ( height != NULL )
715 *height = toolbarHeight;
718 wxToolBarBase::DoGetSize( width, height );
721 wxToolBarBase::DoGetSize( width, height );
725 wxSize wxToolBar::DoGetBestSize() const
729 DoGetSize( &width, &height );
731 return wxSize( width, height );
734 void wxToolBar::SetWindowStyleFlag( long style )
736 wxToolBarBase::SetWindowStyleFlag( style );
738 #if wxOSX_USE_NATIVE_TOOLBAR
739 if (m_macToolbar != NULL)
741 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
743 if ( style & wxTB_NOICONS )
744 mode = NSToolbarDisplayModeLabelOnly;
745 else if ( style & wxTB_TEXT )
746 mode = NSToolbarDisplayModeIconAndLabel;
748 mode = NSToolbarDisplayModeIconOnly;
750 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
755 #if wxOSX_USE_NATIVE_TOOLBAR
756 bool wxToolBar::MacWantsNativeToolbar()
758 return m_macUsesNativeToolbar;
761 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
763 bool bResultV = false;
765 if (ownToolbarInstalled != NULL)
766 *ownToolbarInstalled = false;
768 WXWindow tlw = MacGetTopLevelWindowRef();
771 NSToolbar* curToolbarRef = [tlw toolbar];
772 bResultV = (curToolbarRef != NULL);
773 if (bResultV && (ownToolbarInstalled != NULL))
774 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
780 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
782 bool bResult = false;
784 if (usesNative && (m_macToolbar == NULL))
787 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
790 WXWindow tlw = MacGetTopLevelWindowRef();
794 // check the existing toolbar
795 NSToolbar* curToolbarRef = [tlw toolbar];
797 m_macUsesNativeToolbar = usesNative;
799 if (m_macUsesNativeToolbar)
801 // only install toolbar if there isn't one installed already
802 if (curToolbarRef == NULL)
805 [tlw setToolbar:(NSToolbar*) m_macToolbar];
806 [(NSToolbar*) m_macToolbar setVisible:YES];
808 m_peer->Move(0,0,0,0 );
809 SetSize( wxSIZE_AUTO_WIDTH, 0 );
810 m_peer->SetVisibility( false );
811 wxToolBarBase::Show( false );
816 // only deinstall toolbar if this is the installed one
817 if (m_macToolbar == curToolbarRef)
820 [(NSToolbar*) m_macToolbar setVisible:NO];
821 [tlw setToolbar:nil];
822 m_peer->SetVisibility( true );
827 InvalidateBestSize();
829 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
834 bool wxToolBar::Realize()
836 if (m_tools.GetCount() == 0)
842 int maxToolWidth = 0;
843 int maxToolHeight = 0;
845 int x = m_xMargin + kwxMacToolBarLeftMargin;
846 int y = m_yMargin + kwxMacToolBarTopMargin;
851 // find the maximum tool width and height
853 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
856 tool = (wxToolBarTool *) node->GetData();
859 wxSize sz = tool->GetSize();
861 if ( sz.x > maxToolWidth )
863 if ( sz.y > maxToolHeight )
864 maxToolHeight = sz.y;
867 node = node->GetNext();
870 bool lastIsRadio = false;
871 bool curIsRadio = false;
873 #if wxOSX_USE_NATIVE_TOOLBAR
874 CFIndex currentPosition = 0;
875 bool insertAll = false;
877 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
882 enc = f.GetEncoding();
884 enc = wxFont::GetDefaultEncoding();
887 node = m_tools.GetFirst();
890 tool = (wxToolBarTool*) node->GetData();
893 node = node->GetNext();
897 // set tool position:
898 // for the moment just perform a single row/column alignment
899 wxSize cursize = tool->GetSize();
900 if ( x + cursize.x > maxWidth )
901 maxWidth = x + cursize.x;
902 if ( y + cursize.y > maxHeight )
903 maxHeight = y + cursize.y;
905 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
907 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
908 tool->SetPosition( wxPoint(x1, y) );
912 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
913 tool->SetPosition( wxPoint(x, y1) );
916 // update the item positioning state
917 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
918 y += cursize.y + kwxMacToolSpacing;
920 x += cursize.x + kwxMacToolSpacing;
922 #if wxOSX_USE_NATIVE_TOOLBAR
923 // install in native NSToolbar
926 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
927 if ( hiItemRef != NULL )
929 // since setting the help texts is non-virtual we have to update
931 wxCFStringRef sh( tool->GetShortHelp(), enc);
932 [hiItemRef setToolTip:sh.AsNSString()];
934 if ( insertAll || (tool->GetIndex() != currentPosition) )
940 // if this is the first tool that gets newly inserted or repositioned
941 // first remove all 'old' tools from here to the right, because of this
942 // all following tools will have to be reinserted (insertAll).
943 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
945 node2 = node2->GetPrevious() )
947 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
949 const long idx = tool2->GetIndex();
952 [refTB removeItemAtIndex:idx];
957 wxString identifier = wxString::Format( wxT("%ld"), (long) tool );
958 wxCFStringRef cfidentifier(identifier);
960 [refTB insertItemWithItemIdentifier:cfidentifier.AsNSString() atIndex:currentPosition];
961 tool->SetIndex( currentPosition );
969 // update radio button (and group) state
970 lastIsRadio = curIsRadio;
971 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
975 if ( tool->IsToggled() )
976 DoToggleTool( tool, true );
982 if ( tool->Toggle( true ) )
984 DoToggleTool( tool, true );
987 else if ( tool->IsToggled() )
989 if ( tool->IsToggled() )
990 DoToggleTool( tool, true );
992 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
995 wxToolBarToolBase *toggleTool = nodePrev->GetData();
996 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
999 if ( toggleTool->Toggle( false ) )
1000 DoToggleTool( toggleTool, false );
1002 nodePrev = nodePrev->GetPrevious();
1007 node = node->GetNext();
1010 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
1012 // if not set yet, only one row
1013 if ( m_maxRows <= 0 )
1016 m_minWidth = maxWidth;
1018 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1019 m_minHeight = m_maxHeight = maxHeight;
1023 // if not set yet, have one column
1024 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1025 SetRows( GetToolsCount() );
1027 m_minHeight = maxHeight;
1029 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1030 m_minWidth = m_maxWidth = maxWidth;
1034 // FIXME: should this be OSX-only?
1036 bool wantNativeToolbar, ownToolbarInstalled;
1038 // attempt to install the native toolbar
1039 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1040 MacInstallNativeToolbar( wantNativeToolbar );
1041 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1042 if (!ownToolbarInstalled)
1044 SetSize( maxWidth, maxHeight );
1045 InvalidateBestSize();
1049 SetSize( maxWidth, maxHeight );
1050 InvalidateBestSize();
1058 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1060 m_defaultWidth = size.x + kwxMacToolBorder;
1061 m_defaultHeight = size.y + kwxMacToolBorder;
1063 #if wxOSX_USE_NATIVE_TOOLBAR
1064 if (m_macToolbar != NULL)
1066 int maxs = wxMax( size.x, size.y );
1067 NSToolbarSizeMode sizeSpec;
1069 sizeSpec = NSToolbarSizeModeRegular;
1070 else if ( maxs > 24 )
1071 sizeSpec = NSToolbarSizeModeDefault;
1073 sizeSpec = NSToolbarSizeModeSmall;
1075 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1080 // The button size is bigger than the bitmap size
1081 wxSize wxToolBar::GetToolSize() const
1083 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1086 void wxToolBar::SetRows(int nRows)
1088 // avoid resizing the frame uselessly
1089 if ( nRows != m_maxRows )
1093 void wxToolBar::MacSuperChangedPosition()
1095 wxWindow::MacSuperChangedPosition();
1097 #if wxOSX_USE_NATIVE_TOOLBAR
1098 if (! m_macUsesNativeToolbar )
1106 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1108 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1111 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1113 tool->SetNormalBitmap(bitmap);
1115 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1116 tool->UpdateImages();
1120 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1122 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1125 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1127 tool->SetDisabledBitmap(bitmap);
1129 // TODO: what to do for this one?
1133 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1135 wxToolBarTool *tool;
1136 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1139 tool = (wxToolBarTool *)node->GetData();
1142 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1143 if ( r.Contains( wxPoint( x, y ) ) )
1147 node = node->GetNext();
1153 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1155 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1157 return tool->GetShortHelp();
1159 return wxEmptyString;
1162 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1165 ((wxToolBarTool*)t)->DoEnable( enable );
1168 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1170 wxToolBarTool *tool = (wxToolBarTool *)t;
1171 if ( ( tool != NULL ) && tool->IsButton() )
1172 tool->UpdateToggleImage( toggle );
1175 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1177 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1181 wxSize toolSize = GetToolSize();
1182 WXWidget controlHandle = NULL;
1183 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1185 #if wxOSX_USE_NATIVE_TOOLBAR
1186 wxString label = tool->GetLabel();
1187 if (m_macToolbar && !label.empty() )
1189 // strip mnemonics from the label for compatibility
1190 // with the usual labels in wxStaticText sense
1191 label = wxStripMenuCodes(label);
1193 #endif // wxOSX_USE_NATIVE_TOOLBAR
1195 switch (tool->GetStyle())
1197 case wxTOOL_STYLE_SEPARATOR:
1199 wxASSERT( tool->GetControlHandle() == NULL );
1202 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1203 toolrect.size.height = toolSize.y;
1205 toolrect.size.width = toolSize.x;
1207 // in flat style we need a visual separator
1208 #if wxOSX_USE_NATIVE_TOOLBAR
1209 if (m_macToolbar != NULL)
1211 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarSeparatorItemIdentifier];
1212 tool->SetToolbarItemRef( item );
1214 #endif // wxOSX_USE_NATIVE_TOOLBAR
1216 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1217 [box setBoxType:NSBoxSeparator];
1218 controlHandle = box;
1219 tool->SetControlHandle( controlHandle );
1223 case wxTOOL_STYLE_BUTTON:
1225 wxASSERT( tool->GetControlHandle() == NULL );
1227 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1229 [v setBezelStyle:NSRegularSquareBezelStyle];
1231 [v setButtonType: ( tool->CanBeToggled() ? NSOnOffButton : NSMomentaryPushInButton )];
1232 [v setImplementation:tool];
1236 #if wxOSX_USE_NATIVE_TOOLBAR
1237 if (m_macToolbar != NULL)
1239 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1240 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1241 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1242 [item setImplementation:tool];
1243 tool->SetToolbarItemRef( item );
1246 #endif // wxOSX_USE_NATIVE_TOOLBAR
1247 tool->SetControlHandle( controlHandle );
1248 tool->UpdateImages();
1249 tool->UpdateLabel();
1251 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1252 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1255 InstallControlEventHandler(
1256 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1257 GetEventTypeCount(eventList), eventList, tool, NULL );
1262 case wxTOOL_STYLE_CONTROL:
1264 #if wxOSX_USE_NATIVE_TOOLBAR
1265 if (m_macToolbar != NULL)
1267 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1268 wxCHECK_MSG( view, false, _T("control must be non-NULL") );
1270 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1271 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1272 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1273 [item setImplementation:tool];
1274 tool->SetToolbarItemRef( item );
1277 // right now there's nothing to do here
1279 tool->UpdateLabel();
1286 if ( controlHandle )
1288 WXWidget container = (WXWidget) GetHandle();
1289 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1291 // SetControlVisibility( controlHandle, true, true );
1292 [container addSubview:controlHandle];
1295 // nothing special to do here - we relayout in Realize() later
1296 InvalidateBestSize();
1302 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1304 wxFAIL_MSG( wxT("not implemented") );
1307 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1309 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1310 wxToolBarToolsList::compatibility_iterator node;
1311 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1313 wxToolBarToolBase *tool2 = node->GetData();
1314 if ( tool2 == tool )
1316 // let node point to the next node in the list
1317 node = node->GetNext();
1323 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1325 #if wxOSX_USE_NATIVE_TOOLBAR
1326 CFIndex removeIndex = tool->GetIndex();
1329 #if wxOSX_USE_NATIVE_TOOLBAR
1330 if (m_macToolbar != NULL)
1332 if ( removeIndex != -1 && m_macToolbar )
1334 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1335 tool->SetIndex( -1 );
1340 tool->ClearControl();
1342 // and finally reposition all the controls after this one
1344 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1346 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1347 wxPoint pt = tool2->GetPosition();
1349 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1354 tool2->SetPosition( pt );
1356 #if wxOSX_USE_NATIVE_TOOLBAR
1357 if (m_macToolbar != NULL)
1359 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1360 tool2->SetIndex( tool2->GetIndex() - 1 );
1365 InvalidateBestSize();
1370 #include <Carbon/Carbon.h>
1372 void wxToolBar::OnPaint(wxPaintEvent& event)
1374 #if wxOSX_USE_NATIVE_TOOLBAR
1375 if ( m_macUsesNativeToolbar )
1387 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1389 if ( !drawMetalTheme )
1391 HIThemePlacardDrawInfo info;
1392 memset( &info, 0, sizeof(info) );
1394 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1396 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1397 HIRect rect = CGRectMake( 0, 0, w, h );
1398 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1402 // leave the background as it is (striped or metal)
1408 #endif // wxUSE_TOOLBAR