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 release];
642 bool wxToolBar::Show( bool show )
644 WXWindow tlw = MacGetTopLevelWindowRef();
645 bool bResult = (tlw != NULL);
649 #if wxOSX_USE_NATIVE_TOOLBAR
650 bool ownToolbarInstalled = false;
651 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
652 if (ownToolbarInstalled)
654 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
656 [(NSToolbar*)m_macToolbar setVisible:show];
659 bResult = wxToolBarBase::Show( show );
662 bResult = wxToolBarBase::Show( show );
669 bool wxToolBar::IsShown() const
673 #if wxOSX_USE_NATIVE_TOOLBAR
674 bool ownToolbarInstalled;
676 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
677 if (ownToolbarInstalled)
679 bResult = [(NSToolbar*)m_macToolbar isVisible];
682 bResult = wxToolBarBase::IsShown();
685 bResult = wxToolBarBase::IsShown();
691 void wxToolBar::DoGetSize( int *width, int *height ) const
693 #if wxOSX_USE_NATIVE_TOOLBAR
694 bool ownToolbarInstalled;
696 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
697 if ( ownToolbarInstalled )
699 WXWindow tlw = MacGetTopLevelWindowRef();
700 float toolbarHeight = 0.0;
703 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
705 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
706 styleMask:[tlw styleMask]];
707 toolbarHeight = NSHeight(windowFrame)
708 - NSHeight([[tlw contentView] frame]);
712 *width = windowFrame.size.width;
713 if ( height != NULL )
714 *height = toolbarHeight;
717 wxToolBarBase::DoGetSize( width, height );
720 wxToolBarBase::DoGetSize( width, height );
724 wxSize wxToolBar::DoGetBestSize() const
728 DoGetSize( &width, &height );
730 return wxSize( width, height );
733 void wxToolBar::SetWindowStyleFlag( long style )
735 wxToolBarBase::SetWindowStyleFlag( style );
737 #if wxOSX_USE_NATIVE_TOOLBAR
738 if (m_macToolbar != NULL)
740 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
742 if ( style & wxTB_NOICONS )
743 mode = NSToolbarDisplayModeLabelOnly;
744 else if ( style & wxTB_TEXT )
745 mode = NSToolbarDisplayModeIconAndLabel;
747 mode = NSToolbarDisplayModeIconOnly;
749 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
754 #if wxOSX_USE_NATIVE_TOOLBAR
755 bool wxToolBar::MacWantsNativeToolbar()
757 return m_macUsesNativeToolbar;
760 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
762 bool bResultV = false;
764 if (ownToolbarInstalled != NULL)
765 *ownToolbarInstalled = false;
767 WXWindow tlw = MacGetTopLevelWindowRef();
770 NSToolbar* curToolbarRef = [tlw toolbar];
771 bResultV = (curToolbarRef != NULL);
772 if (bResultV && (ownToolbarInstalled != NULL))
773 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
779 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
781 bool bResult = false;
783 if (usesNative && (m_macToolbar == NULL))
786 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
789 WXWindow tlw = MacGetTopLevelWindowRef();
793 // check the existing toolbar
794 NSToolbar* curToolbarRef = [tlw toolbar];
796 m_macUsesNativeToolbar = usesNative;
798 if (m_macUsesNativeToolbar)
800 // only install toolbar if there isn't one installed already
801 if (curToolbarRef == NULL)
804 [tlw setToolbar:(NSToolbar*) m_macToolbar];
805 [(NSToolbar*) m_macToolbar setVisible:YES];
807 m_peer->Move(0,0,0,0 );
808 SetSize( wxSIZE_AUTO_WIDTH, 0 );
809 m_peer->SetVisibility( false );
810 wxToolBarBase::Show( false );
815 // only deinstall toolbar if this is the installed one
816 if (m_macToolbar == curToolbarRef)
819 [(NSToolbar*) m_macToolbar setVisible:NO];
820 [tlw setToolbar:nil];
821 m_peer->SetVisibility( true );
826 InvalidateBestSize();
828 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
833 bool wxToolBar::Realize()
835 if (m_tools.GetCount() == 0)
841 int maxToolWidth = 0;
842 int maxToolHeight = 0;
844 int x = m_xMargin + kwxMacToolBarLeftMargin;
845 int y = m_yMargin + kwxMacToolBarTopMargin;
850 // find the maximum tool width and height
852 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
855 tool = (wxToolBarTool *) node->GetData();
858 wxSize sz = tool->GetSize();
860 if ( sz.x > maxToolWidth )
862 if ( sz.y > maxToolHeight )
863 maxToolHeight = sz.y;
866 node = node->GetNext();
869 bool lastIsRadio = false;
870 bool curIsRadio = false;
872 #if wxOSX_USE_NATIVE_TOOLBAR
873 CFIndex currentPosition = 0;
874 bool insertAll = false;
876 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
881 enc = f.GetEncoding();
883 enc = wxFont::GetDefaultEncoding();
886 node = m_tools.GetFirst();
889 tool = (wxToolBarTool*) node->GetData();
892 node = node->GetNext();
896 // set tool position:
897 // for the moment just perform a single row/column alignment
898 wxSize cursize = tool->GetSize();
899 if ( x + cursize.x > maxWidth )
900 maxWidth = x + cursize.x;
901 if ( y + cursize.y > maxHeight )
902 maxHeight = y + cursize.y;
904 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
906 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
907 tool->SetPosition( wxPoint(x1, y) );
911 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
912 tool->SetPosition( wxPoint(x, y1) );
915 // update the item positioning state
916 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
917 y += cursize.y + kwxMacToolSpacing;
919 x += cursize.x + kwxMacToolSpacing;
921 #if wxOSX_USE_NATIVE_TOOLBAR
922 // install in native NSToolbar
925 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
926 if ( hiItemRef != NULL )
928 // since setting the help texts is non-virtual we have to update
930 wxCFStringRef sh( tool->GetShortHelp(), enc);
931 [hiItemRef setToolTip:sh.AsNSString()];
933 if ( insertAll || (tool->GetIndex() != currentPosition) )
939 // if this is the first tool that gets newly inserted or repositioned
940 // first remove all 'old' tools from here to the right, because of this
941 // all following tools will have to be reinserted (insertAll).
942 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
944 node2 = node2->GetPrevious() )
946 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
948 const long idx = tool2->GetIndex();
951 [refTB removeItemAtIndex:idx];
956 wxString identifier = wxString::Format( wxT("%ld"), (long) tool );
957 wxCFStringRef cfidentifier(identifier);
959 [refTB insertItemWithItemIdentifier:cfidentifier.AsNSString() atIndex:currentPosition];
960 tool->SetIndex( currentPosition );
968 // update radio button (and group) state
969 lastIsRadio = curIsRadio;
970 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
974 if ( tool->IsToggled() )
975 DoToggleTool( tool, true );
981 if ( tool->Toggle( true ) )
983 DoToggleTool( tool, true );
986 else if ( tool->IsToggled() )
988 if ( tool->IsToggled() )
989 DoToggleTool( tool, true );
991 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
994 wxToolBarToolBase *toggleTool = nodePrev->GetData();
995 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
998 if ( toggleTool->Toggle( false ) )
999 DoToggleTool( toggleTool, false );
1001 nodePrev = nodePrev->GetPrevious();
1006 node = node->GetNext();
1009 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
1011 // if not set yet, only one row
1012 if ( m_maxRows <= 0 )
1015 m_minWidth = maxWidth;
1017 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1018 m_minHeight = m_maxHeight = maxHeight;
1022 // if not set yet, have one column
1023 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1024 SetRows( GetToolsCount() );
1026 m_minHeight = maxHeight;
1028 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1029 m_minWidth = m_maxWidth = maxWidth;
1033 // FIXME: should this be OSX-only?
1035 bool wantNativeToolbar, ownToolbarInstalled;
1037 // attempt to install the native toolbar
1038 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1039 MacInstallNativeToolbar( wantNativeToolbar );
1040 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1041 if (!ownToolbarInstalled)
1043 SetSize( maxWidth, maxHeight );
1044 InvalidateBestSize();
1048 SetSize( maxWidth, maxHeight );
1049 InvalidateBestSize();
1057 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1059 m_defaultWidth = size.x + kwxMacToolBorder;
1060 m_defaultHeight = size.y + kwxMacToolBorder;
1062 #if wxOSX_USE_NATIVE_TOOLBAR
1063 if (m_macToolbar != NULL)
1065 int maxs = wxMax( size.x, size.y );
1066 NSToolbarSizeMode sizeSpec;
1068 sizeSpec = NSToolbarSizeModeRegular;
1069 else if ( maxs > 24 )
1070 sizeSpec = NSToolbarSizeModeDefault;
1072 sizeSpec = NSToolbarSizeModeSmall;
1074 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1079 // The button size is bigger than the bitmap size
1080 wxSize wxToolBar::GetToolSize() const
1082 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1085 void wxToolBar::SetRows(int nRows)
1087 // avoid resizing the frame uselessly
1088 if ( nRows != m_maxRows )
1092 void wxToolBar::MacSuperChangedPosition()
1094 wxWindow::MacSuperChangedPosition();
1096 #if wxOSX_USE_NATIVE_TOOLBAR
1097 if (! m_macUsesNativeToolbar )
1105 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1107 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1110 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1112 tool->SetNormalBitmap(bitmap);
1114 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1115 tool->UpdateImages();
1119 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1121 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1124 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1126 tool->SetDisabledBitmap(bitmap);
1128 // TODO: what to do for this one?
1132 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1134 wxToolBarTool *tool;
1135 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1138 tool = (wxToolBarTool *)node->GetData();
1141 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1142 if ( r.Contains( wxPoint( x, y ) ) )
1146 node = node->GetNext();
1149 return (wxToolBarToolBase*)NULL;
1152 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1154 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1156 return tool->GetShortHelp();
1158 return wxEmptyString;
1161 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1164 ((wxToolBarTool*)t)->DoEnable( enable );
1167 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1169 wxToolBarTool *tool = (wxToolBarTool *)t;
1170 if ( ( tool != NULL ) && tool->IsButton() )
1171 tool->UpdateToggleImage( toggle );
1174 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1176 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1180 wxSize toolSize = GetToolSize();
1181 WXWidget controlHandle = NULL;
1182 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1184 #if wxOSX_USE_NATIVE_TOOLBAR
1185 wxString label = tool->GetLabel();
1186 if (m_macToolbar && !label.empty() )
1188 // strip mnemonics from the label for compatibility
1189 // with the usual labels in wxStaticText sense
1190 label = wxStripMenuCodes(label);
1192 #endif // wxOSX_USE_NATIVE_TOOLBAR
1194 switch (tool->GetStyle())
1196 case wxTOOL_STYLE_SEPARATOR:
1198 wxASSERT( tool->GetControlHandle() == NULL );
1201 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1202 toolrect.size.height = toolSize.y;
1204 toolrect.size.width = toolSize.x;
1206 // in flat style we need a visual separator
1207 #if wxOSX_USE_NATIVE_TOOLBAR
1208 if (m_macToolbar != NULL)
1210 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarSeparatorItemIdentifier];
1211 tool->SetToolbarItemRef( item );
1213 #endif // wxOSX_USE_NATIVE_TOOLBAR
1215 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1216 [box setBoxType:NSBoxSeparator];
1217 controlHandle = box;
1218 tool->SetControlHandle( controlHandle );
1222 case wxTOOL_STYLE_BUTTON:
1224 wxASSERT( tool->GetControlHandle() == NULL );
1226 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1228 [v setBezelStyle:NSRegularSquareBezelStyle];
1230 [v setButtonType: ( tool->CanBeToggled() ? NSOnOffButton : NSMomentaryPushInButton )];
1231 [v setImplementation:tool];
1235 #if wxOSX_USE_NATIVE_TOOLBAR
1236 if (m_macToolbar != NULL)
1238 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1239 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1240 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1241 [item setImplementation:tool];
1242 tool->SetToolbarItemRef( item );
1245 #endif // wxOSX_USE_NATIVE_TOOLBAR
1246 tool->SetControlHandle( controlHandle );
1247 tool->UpdateImages();
1248 tool->UpdateLabel();
1250 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1251 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1254 InstallControlEventHandler(
1255 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1256 GetEventTypeCount(eventList), eventList, tool, NULL );
1261 case wxTOOL_STYLE_CONTROL:
1263 #if wxOSX_USE_NATIVE_TOOLBAR
1264 if (m_macToolbar != NULL)
1266 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1267 wxCHECK_MSG( view, false, _T("control must be non-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 // right now there's nothing to do here
1278 tool->UpdateLabel();
1285 if ( controlHandle )
1287 WXWidget container = (WXWidget) GetHandle();
1288 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1290 // SetControlVisibility( controlHandle, true, true );
1291 [container addSubview:controlHandle];
1294 // nothing special to do here - we relayout in Realize() later
1295 InvalidateBestSize();
1301 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1303 wxFAIL_MSG( wxT("not implemented") );
1306 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1308 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1309 wxToolBarToolsList::compatibility_iterator node;
1310 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1312 wxToolBarToolBase *tool2 = node->GetData();
1313 if ( tool2 == tool )
1315 // let node point to the next node in the list
1316 node = node->GetNext();
1322 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1324 #if wxOSX_USE_NATIVE_TOOLBAR
1325 CFIndex removeIndex = tool->GetIndex();
1328 #if wxOSX_USE_NATIVE_TOOLBAR
1329 if (m_macToolbar != NULL)
1331 if ( removeIndex != -1 && m_macToolbar )
1333 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1334 tool->SetIndex( -1 );
1339 tool->ClearControl();
1341 // and finally reposition all the controls after this one
1343 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1345 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1346 wxPoint pt = tool2->GetPosition();
1348 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1353 tool2->SetPosition( pt );
1355 #if wxOSX_USE_NATIVE_TOOLBAR
1356 if (m_macToolbar != NULL)
1358 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1359 tool2->SetIndex( tool2->GetIndex() - 1 );
1364 InvalidateBestSize();
1369 #include <Carbon/Carbon.h>
1371 void wxToolBar::OnPaint(wxPaintEvent& event)
1373 #if wxOSX_USE_NATIVE_TOOLBAR
1374 if ( m_macUsesNativeToolbar )
1386 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1388 if ( !drawMetalTheme )
1390 HIThemePlacardDrawInfo info;
1391 memset( &info, 0, sizeof(info) );
1393 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1395 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1396 HIRect rect = CGRectMake( 0, 0, w, h );
1397 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1401 // leave the background as it is (striped or metal)
1407 #endif // wxUSE_TOOLBAR