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 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 wxOSX_10_6_AND_LATER(<NSToolbarDelegate>)
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
325 - (void)setImplementation: (wxToolBarTool *) theImplementation
327 impl = theImplementation;
330 - (wxToolBarTool*) implementation
337 @implementation wxNSToolbarDelegate
339 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
341 wxUnusedVar(toolbar);
345 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
347 wxUnusedVar(toolbar);
351 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
353 wxUnusedVar(toolbar);
357 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
359 wxUnusedVar(toolbar);
361 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue];
363 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
367 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
368 if ( flag && tool->IsControl() )
370 NSView* view = tool->GetControl()->GetHandle();
371 [view removeFromSuperview];
373 wxSize sz = tool->GetControl()->GetSize();
374 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
375 [item setMaxSize:size];
376 [item setMinSize:size];
388 @implementation wxNSToolBarButton
390 - (id)initWithFrame:(NSRect)frame
392 [super initWithFrame:frame];
394 [self setTarget: self];
395 [self setAction: @selector(clickedAction:)];
399 - (void) clickedAction: (id) sender
408 - (void)setImplementation: (wxToolBarTool *) theImplementation
410 impl = theImplementation;
413 - (wxToolBarTool*) implementation
425 bool wxToolBarTool::DoEnable( bool enable )
429 GetControl()->Enable( enable );
431 else if ( IsButton() )
433 #if wxOSX_USE_NATIVE_TOOLBAR
434 if ( m_toolbarItem != NULL )
435 [m_toolbarItem setEnabled:enable];
438 if ( m_controlHandle != NULL )
439 [(NSControl*)m_controlHandle setEnabled:enable];
445 void wxToolBarTool::SetPosition( const wxPoint& position )
450 int mac_x = position.x;
451 int mac_y = position.y;
455 NSRect frame = [m_controlHandle frame];
456 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
458 frame.origin.x = mac_x;
459 frame.origin.y = mac_y;
460 [m_controlHandle setFrame:frame];
463 else if ( IsControl() )
465 // embedded native controls are moved by the OS
466 #if wxOSX_USE_NATIVE_TOOLBAR
467 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
470 GetControl()->Move( position );
475 NSRect frame = [m_controlHandle frame];
476 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
478 frame.origin.x = mac_x;
479 frame.origin.y = mac_y;
480 [m_controlHandle setFrame:frame];
485 void wxToolBarTool::UpdateImages()
487 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
489 if ( CanBeToggled() )
491 int w = m_bmpNormal.GetWidth();
492 int h = m_bmpNormal.GetHeight();
493 m_alternateBitmap = wxBitmap( w, h );
496 dc.SelectObject( m_alternateBitmap );
497 dc.SetPen( wxPen(*wxBLACK) );
498 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
499 dc.DrawRectangle( 0, 0, w, h );
500 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
501 dc.SelectObject( wxNullBitmap );
503 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
505 UpdateToggleImage( CanBeToggled() && IsToggled() );
508 void wxToolBarTool::UpdateToggleImage( bool toggle )
510 #if wxOSX_USE_NATIVE_TOOLBAR
511 if (m_toolbarItem != NULL )
513 // the native toolbar item only has a 'selected' state (one for one toolbar)
514 // so we emulate the toggle here
515 if ( CanBeToggled() && toggle )
516 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
518 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
523 wxToolBarTool::wxToolBarTool(
526 const wxString& label,
527 const wxBitmap& bmpNormal,
528 const wxBitmap& bmpDisabled,
530 wxObject *clientData,
531 const wxString& shortHelp,
532 const wxString& longHelp )
535 tbar, id, label, bmpNormal, bmpDisabled, kind,
536 clientData, shortHelp, longHelp )
542 #pragma mark Toolbar Implementation
544 wxToolBarToolBase *wxToolBar::CreateTool(
546 const wxString& label,
547 const wxBitmap& bmpNormal,
548 const wxBitmap& bmpDisabled,
550 wxObject *clientData,
551 const wxString& shortHelp,
552 const wxString& longHelp )
554 return new wxToolBarTool(
555 this, id, label, bmpNormal, bmpDisabled, kind,
556 clientData, shortHelp, longHelp );
560 wxToolBar::CreateTool(wxControl *control, const wxString& label)
562 return new wxToolBarTool(this, control, label);
565 void wxToolBar::Init()
569 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
570 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
572 #if wxOSX_USE_NATIVE_TOOLBAR
574 m_macUsesNativeToolbar = false;
578 // also for the toolbar we have the dual implementation:
579 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
581 bool wxToolBar::Create(
587 const wxString& name )
589 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
594 OSStatus err = noErr;
596 #if wxOSX_USE_NATIVE_TOOLBAR
598 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
600 static wxNSToolbarDelegate* controller = nil;
602 if ( controller == nil )
603 controller = [[wxNSToolbarDelegate alloc] init];
604 wxString identifier = wxString::Format( wxT("%p"), this );
605 wxCFStringRef cfidentifier(identifier);
606 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
610 if (m_macToolbar != NULL)
612 [tb setDelegate:controller];
614 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
615 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
617 if ( style & wxTB_NOICONS )
618 mode = NSToolbarDisplayModeLabelOnly;
619 else if ( style & wxTB_TEXT )
620 mode = NSToolbarDisplayModeIconAndLabel;
622 mode = NSToolbarDisplayModeIconOnly;
624 [tb setDisplayMode:mode];
625 [tb setSizeMode:displaySize];
628 #endif // wxOSX_USE_NATIVE_TOOLBAR
630 return (err == noErr);
633 wxToolBar::~wxToolBar()
635 [(NSToolbar*)m_macToolbar setDelegate:nil];
636 [(NSToolbar*)m_macToolbar release];
640 bool wxToolBar::Show( bool show )
642 WXWindow tlw = MacGetTopLevelWindowRef();
643 bool bResult = (tlw != NULL);
647 #if wxOSX_USE_NATIVE_TOOLBAR
648 bool ownToolbarInstalled = false;
649 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
650 if (ownToolbarInstalled)
652 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
654 [(NSToolbar*)m_macToolbar setVisible:show];
657 bResult = wxToolBarBase::Show( show );
660 bResult = wxToolBarBase::Show( show );
667 bool wxToolBar::IsShown() const
671 #if wxOSX_USE_NATIVE_TOOLBAR
672 bool ownToolbarInstalled;
674 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
675 if (ownToolbarInstalled)
677 bResult = [(NSToolbar*)m_macToolbar isVisible];
680 bResult = wxToolBarBase::IsShown();
683 bResult = wxToolBarBase::IsShown();
689 void wxToolBar::DoGetSize( int *width, int *height ) const
691 #if wxOSX_USE_NATIVE_TOOLBAR
692 bool ownToolbarInstalled;
694 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
695 if ( ownToolbarInstalled )
697 WXWindow tlw = MacGetTopLevelWindowRef();
698 float toolbarHeight = 0.0;
699 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
701 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
703 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
704 styleMask:[tlw styleMask]];
705 toolbarHeight = NSHeight(windowFrame)
706 - NSHeight([[tlw contentView] frame]);
710 *width = (int)windowFrame.size.width;
711 if ( height != NULL )
712 *height = (int)toolbarHeight;
715 wxToolBarBase::DoGetSize( width, height );
718 wxToolBarBase::DoGetSize( width, height );
722 wxSize wxToolBar::DoGetBestSize() const
726 DoGetSize( &width, &height );
728 return wxSize( width, height );
731 void wxToolBar::SetWindowStyleFlag( long style )
733 wxToolBarBase::SetWindowStyleFlag( style );
735 #if wxOSX_USE_NATIVE_TOOLBAR
736 if (m_macToolbar != NULL)
738 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
740 if ( style & wxTB_NOICONS )
741 mode = NSToolbarDisplayModeLabelOnly;
742 else if ( style & wxTB_TEXT )
743 mode = NSToolbarDisplayModeIconAndLabel;
745 mode = NSToolbarDisplayModeIconOnly;
747 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
752 #if wxOSX_USE_NATIVE_TOOLBAR
753 bool wxToolBar::MacWantsNativeToolbar()
755 return m_macUsesNativeToolbar;
758 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
760 bool bResultV = false;
762 if (ownToolbarInstalled != NULL)
763 *ownToolbarInstalled = false;
765 WXWindow tlw = MacGetTopLevelWindowRef();
768 NSToolbar* curToolbarRef = [tlw toolbar];
769 bResultV = (curToolbarRef != NULL);
770 if (bResultV && (ownToolbarInstalled != NULL))
771 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
777 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
779 bool bResult = false;
781 if (usesNative && (m_macToolbar == NULL))
784 if (usesNative && ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM)) != 0))
787 WXWindow tlw = MacGetTopLevelWindowRef();
791 // check the existing toolbar
792 NSToolbar* curToolbarRef = [tlw toolbar];
794 m_macUsesNativeToolbar = usesNative;
796 if (m_macUsesNativeToolbar)
798 // only install toolbar if there isn't one installed already
799 if (curToolbarRef == NULL)
802 [tlw setToolbar:(NSToolbar*) m_macToolbar];
803 [(NSToolbar*) m_macToolbar setVisible:YES];
805 m_peer->Move(0,0,0,0 );
806 SetSize( wxSIZE_AUTO_WIDTH, 0 );
807 m_peer->SetVisibility( false );
808 wxToolBarBase::Show( false );
813 // only deinstall toolbar if this is the installed one
814 if (m_macToolbar == curToolbarRef)
817 [(NSToolbar*) m_macToolbar setVisible:NO];
818 MacUninstallNativeToolbar();
819 m_peer->SetVisibility( true );
824 InvalidateBestSize();
826 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
830 void wxToolBar::MacUninstallNativeToolbar()
835 WXWindow tlw = MacGetTopLevelWindowRef();
837 [tlw setToolbar:nil];
841 bool wxToolBar::Realize()
843 if ( !wxToolBarBase::Realize() )
849 int maxToolWidth = 0;
850 int maxToolHeight = 0;
852 int x = m_xMargin + kwxMacToolBarLeftMargin;
853 int y = m_yMargin + kwxMacToolBarTopMargin;
858 // find the maximum tool width and height
860 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
863 tool = (wxToolBarTool *) node->GetData();
866 wxSize sz = tool->GetSize();
868 if ( sz.x > maxToolWidth )
870 if ( sz.y > maxToolHeight )
871 maxToolHeight = sz.y;
874 node = node->GetNext();
877 bool lastIsRadio = false;
878 bool curIsRadio = false;
880 #if wxOSX_USE_NATIVE_TOOLBAR
881 CFIndex currentPosition = 0;
882 bool insertAll = false;
884 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
889 enc = f.GetEncoding();
891 enc = wxFont::GetDefaultEncoding();
894 node = m_tools.GetFirst();
897 tool = (wxToolBarTool*) node->GetData();
900 node = node->GetNext();
904 // set tool position:
905 // for the moment just perform a single row/column alignment
906 wxSize cursize = tool->GetSize();
907 if ( x + cursize.x > maxWidth )
908 maxWidth = x + cursize.x;
909 if ( y + cursize.y > maxHeight )
910 maxHeight = y + cursize.y;
912 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
914 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
915 tool->SetPosition( wxPoint(x1, y) );
919 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
920 tool->SetPosition( wxPoint(x, y1) );
923 // update the item positioning state
924 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
925 y += cursize.y + kwxMacToolSpacing;
927 x += cursize.x + kwxMacToolSpacing;
929 #if wxOSX_USE_NATIVE_TOOLBAR
930 // install in native NSToolbar
933 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
934 if ( hiItemRef != NULL )
936 // since setting the help texts is non-virtual we have to update
938 wxCFStringRef sh( tool->GetShortHelp(), enc);
939 [hiItemRef setToolTip:sh.AsNSString()];
941 if ( insertAll || (tool->GetIndex() != currentPosition) )
947 // if this is the first tool that gets newly inserted or repositioned
948 // first remove all 'old' tools from here to the right, because of this
949 // all following tools will have to be reinserted (insertAll).
950 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
952 node2 = node2->GetPrevious() )
954 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
956 const long idx = tool2->GetIndex();
959 [refTB removeItemAtIndex:idx];
965 wxCFStringRef cfidentifier;
966 const NSString *nsItemId;
967 if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR)
969 nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier
970 : NSToolbarSeparatorItemIdentifier;
974 cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool));
975 nsItemId = cfidentifier.AsNSString();
978 [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition];
979 tool->SetIndex( currentPosition );
987 // update radio button (and group) state
988 lastIsRadio = curIsRadio;
989 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
993 if ( tool->IsToggled() )
994 DoToggleTool( tool, true );
1000 if ( tool->Toggle( true ) )
1002 DoToggleTool( tool, true );
1005 else if ( tool->IsToggled() )
1007 if ( tool->IsToggled() )
1008 DoToggleTool( tool, true );
1010 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1013 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1014 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1017 if ( toggleTool->Toggle( false ) )
1018 DoToggleTool( toggleTool, false );
1020 nodePrev = nodePrev->GetPrevious();
1025 node = node->GetNext();
1028 if ( GetWindowStyleFlag() & (wxTB_TOP|wxTB_BOTTOM) )
1030 // if not set yet, only one row
1031 if ( m_maxRows <= 0 )
1034 m_minWidth = maxWidth;
1036 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1037 m_minHeight = m_maxHeight = maxHeight;
1041 // if not set yet, have one column
1042 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1043 SetRows( GetToolsCount() );
1045 m_minHeight = maxHeight;
1047 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1048 m_minWidth = m_maxWidth = maxWidth;
1052 // FIXME: should this be OSX-only?
1054 bool wantNativeToolbar, ownToolbarInstalled;
1056 // attempt to install the native toolbar
1057 wantNativeToolbar = ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_BOTTOM|wxTB_RIGHT)) == 0);
1058 MacInstallNativeToolbar( wantNativeToolbar );
1059 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1060 if (!ownToolbarInstalled)
1062 SetSize( maxWidth, maxHeight );
1063 InvalidateBestSize();
1067 SetSize( maxWidth, maxHeight );
1068 InvalidateBestSize();
1076 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1078 m_defaultWidth = size.x + kwxMacToolBorder;
1079 m_defaultHeight = size.y + kwxMacToolBorder;
1081 #if wxOSX_USE_NATIVE_TOOLBAR
1082 if (m_macToolbar != NULL)
1084 int maxs = wxMax( size.x, size.y );
1085 NSToolbarSizeMode sizeSpec;
1087 sizeSpec = NSToolbarSizeModeRegular;
1088 else if ( maxs > 24 )
1089 sizeSpec = NSToolbarSizeModeDefault;
1091 sizeSpec = NSToolbarSizeModeSmall;
1093 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1098 // The button size is bigger than the bitmap size
1099 wxSize wxToolBar::GetToolSize() const
1101 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1104 void wxToolBar::SetRows(int nRows)
1106 // avoid resizing the frame uselessly
1107 if ( nRows != m_maxRows )
1111 void wxToolBar::MacSuperChangedPosition()
1113 wxWindow::MacSuperChangedPosition();
1115 #if wxOSX_USE_NATIVE_TOOLBAR
1116 if (! m_macUsesNativeToolbar )
1124 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1126 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1129 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1131 tool->SetNormalBitmap(bitmap);
1133 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1134 tool->UpdateImages();
1138 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1140 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1143 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1145 tool->SetDisabledBitmap(bitmap);
1147 // TODO: what to do for this one?
1151 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1153 wxToolBarTool *tool;
1154 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1157 tool = (wxToolBarTool *)node->GetData();
1160 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1161 if ( r.Contains( wxPoint( x, y ) ) )
1165 node = node->GetNext();
1171 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1173 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1175 return tool->GetShortHelp();
1177 return wxEmptyString;
1180 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1183 ((wxToolBarTool*)t)->DoEnable( enable );
1186 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1188 wxToolBarTool *tool = (wxToolBarTool *)t;
1189 if ( ( tool != NULL ) && tool->IsButton() )
1190 tool->UpdateToggleImage( toggle );
1193 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1195 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1199 wxSize toolSize = GetToolSize();
1200 WXWidget controlHandle = NULL;
1201 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1203 #if wxOSX_USE_NATIVE_TOOLBAR
1204 wxString label = tool->GetLabel();
1205 if (m_macToolbar && !label.empty() )
1207 // strip mnemonics from the label for compatibility
1208 // with the usual labels in wxStaticText sense
1209 label = wxStripMenuCodes(label);
1211 #endif // wxOSX_USE_NATIVE_TOOLBAR
1213 switch (tool->GetStyle())
1215 case wxTOOL_STYLE_SEPARATOR:
1217 wxASSERT( tool->GetControlHandle() == NULL );
1220 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
1221 toolrect.size.height = toolSize.y;
1223 toolrect.size.width = toolSize.x;
1225 // in flat style we need a visual separator
1226 #if wxOSX_USE_NATIVE_TOOLBAR
1227 if (m_macToolbar != NULL)
1229 const NSString * const
1230 nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier
1231 : NSToolbarSeparatorItemIdentifier;
1232 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId];
1233 tool->SetToolbarItemRef( item );
1235 #endif // wxOSX_USE_NATIVE_TOOLBAR
1237 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1238 [box setBoxType:NSBoxSeparator];
1239 controlHandle = box;
1240 tool->SetControlHandle( controlHandle );
1244 case wxTOOL_STYLE_BUTTON:
1246 wxASSERT( tool->GetControlHandle() == NULL );
1248 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1250 [v setBezelStyle:NSRegularSquareBezelStyle];
1252 [v setButtonType: ( tool->CanBeToggled() ? NSOnOffButton : NSMomentaryPushInButton )];
1253 [v setImplementation:tool];
1257 #if wxOSX_USE_NATIVE_TOOLBAR
1258 if (m_macToolbar != NULL)
1260 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1261 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1262 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1263 [item setImplementation:tool];
1264 tool->SetToolbarItemRef( item );
1267 #endif // wxOSX_USE_NATIVE_TOOLBAR
1268 tool->SetControlHandle( controlHandle );
1269 tool->UpdateImages();
1270 tool->UpdateLabel();
1272 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1273 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1276 InstallControlEventHandler(
1277 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1278 GetEventTypeCount(eventList), eventList, tool, NULL );
1283 case wxTOOL_STYLE_CONTROL:
1285 #if wxOSX_USE_NATIVE_TOOLBAR
1286 if (m_macToolbar != NULL)
1288 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1289 wxCHECK_MSG( view, false, wxT("control must be non-NULL") );
1291 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1292 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1293 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1294 [item setImplementation:tool];
1295 tool->SetToolbarItemRef( item );
1298 // right now there's nothing to do here
1300 tool->UpdateLabel();
1307 if ( controlHandle )
1309 WXWidget container = (WXWidget) GetHandle();
1310 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1312 // SetControlVisibility( controlHandle, true, true );
1313 [container addSubview:controlHandle];
1316 // nothing special to do here - we relayout in Realize() later
1317 InvalidateBestSize();
1323 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1325 wxFAIL_MSG( wxT("not implemented") );
1328 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1330 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1331 wxToolBarToolsList::compatibility_iterator node;
1332 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1334 wxToolBarToolBase *tool2 = node->GetData();
1335 if ( tool2 == tool )
1337 // let node point to the next node in the list
1338 node = node->GetNext();
1344 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1346 #if wxOSX_USE_NATIVE_TOOLBAR
1347 CFIndex removeIndex = tool->GetIndex();
1350 #if wxOSX_USE_NATIVE_TOOLBAR
1351 if (m_macToolbar != NULL)
1353 if ( removeIndex != -1 && m_macToolbar )
1355 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1356 tool->SetIndex( -1 );
1361 tool->ClearControl();
1363 // and finally reposition all the controls after this one
1365 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1367 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1368 wxPoint pt = tool2->GetPosition();
1370 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
1375 tool2->SetPosition( pt );
1377 #if wxOSX_USE_NATIVE_TOOLBAR
1378 if (m_macToolbar != NULL)
1380 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1381 tool2->SetIndex( tool2->GetIndex() - 1 );
1386 InvalidateBestSize();
1391 #include <Carbon/Carbon.h>
1393 void wxToolBar::OnPaint(wxPaintEvent& event)
1395 #if wxOSX_USE_NATIVE_TOOLBAR
1396 if ( m_macUsesNativeToolbar )
1408 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1410 if ( !drawMetalTheme )
1412 HIThemePlacardDrawInfo info;
1413 memset( &info, 0, sizeof(info) );
1415 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1417 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1418 HIRect rect = CGRectMake( 0, 0, w, h );
1419 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1423 // leave the background as it is (striped or metal)
1429 #endif // wxUSE_TOOLBAR