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
43 #define wxOSX_USE_NATIVE_TOOLBAR 1
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // We have a dual implementation for each tool, WXWidget and NSToolbarItem*
52 // when embedding native controls in the native toolbar we must make sure the
53 // control does not get deleted behind our backs, so the retain count gets increased
54 // (after creation it is 1), first be the creation of the custom NSToolbarItem wrapper
55 // object, and second by the code 'creating' the custom HIView (which is the same as the
56 // already existing native control, therefore we just increase the ref count)
57 // when this view is removed from the native toolbar its count gets decremented again
58 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
59 // so in the end the control lives with a refcount of one and can be disposed of by the
60 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
61 // so we can only test against a range, not a specific value of the refcount.
63 class wxToolBarTool : public wxToolBarToolBase
69 const wxString& label,
70 const wxBitmap& bmpNormal,
71 const wxBitmap& bmpDisabled,
74 const wxString& shortHelp,
75 const wxString& longHelp );
77 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
78 : wxToolBarToolBase(tbar, control, label)
82 SetControlHandle( (WXWidget) control->GetHandle() );
85 virtual ~wxToolBarTool()
90 WXWidget GetControlHandle()
92 return (WXWidget) m_controlHandle;
95 void SetControlHandle( WXWidget handle )
97 m_controlHandle = handle;
100 void SetPosition( const wxPoint& position );
104 if ( m_controlHandle )
108 [m_controlHandle retain];
112 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
113 // proper wxControl destructor
115 m_controlHandle = NULL ;
118 #if wxOSX_USE_NATIVE_TOOLBAR
121 [m_toolbarItem release];
122 m_toolbarItem = NULL;
124 #endif // wxOSX_USE_NATIVE_TOOLBAR
127 wxSize GetSize() const
133 curSize = GetControl()->GetSize();
135 else if ( IsButton() )
137 curSize = GetToolBar()->GetToolSize();
142 curSize = GetToolBar()->GetToolSize();
143 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
152 wxPoint GetPosition() const
154 return wxPoint( m_x, m_y );
157 bool DoEnable( bool enable );
161 void UpdateToggleImage( bool toggle );
167 // strip mnemonics from the label for compatibility with the usual
168 // labels in wxStaticText sense
169 wxString labelStr = wxStripMenuCodes(m_label);
170 wxCFStringRef l(labelStr, GetToolBarFontEncoding());
172 [m_toolbarItem setLabel:l.AsNSString()];
174 wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() );
175 [m_toolbarItem setToolTip:sh.AsNSString()];
181 wxToolBar *tbar = (wxToolBar*) GetToolBar();
186 shouldToggle = !IsToggled();
187 tbar->ToggleTool( GetId(), shouldToggle );
190 tbar->OnLeftClick( GetId(), IsToggled() );
193 #if wxOSX_USE_NATIVE_TOOLBAR
194 void SetToolbarItemRef( NSToolbarItem* ref )
196 if ( m_controlHandle )
197 [m_controlHandle setHidden:YES];
199 [m_toolbarItem release];
204 NSToolbarItem* GetToolbarItemRef() const
206 return m_toolbarItem;
209 void SetIndex( CFIndex idx )
214 CFIndex GetIndex() const
219 virtual void SetLabel(const wxString& label)
221 wxToolBarToolBase::SetLabel(label);
224 #endif // wxOSX_USE_NATIVE_TOOLBAR
227 #if wxOSX_USE_NATIVE_TOOLBAR
228 wxFontEncoding GetToolBarFontEncoding() const
232 f = GetToolBar()->GetFont();
233 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
235 #endif // wxOSX_USE_NATIVE_TOOLBAR
239 m_controlHandle = NULL;
241 #if wxOSX_USE_NATIVE_TOOLBAR
242 m_toolbarItem = NULL;
247 WXWidget m_controlHandle;
250 wxBitmap m_alternateBitmap;
252 #if wxOSX_USE_NATIVE_TOOLBAR
253 NSToolbarItem* m_toolbarItem;
254 // position in its toolbar, -1 means not inserted
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;
288 @interface wxNSToolBarButton : NSButton
293 - (id)initWithFrame:(NSRect)frame;
294 - (void) clickedAction: (id) sender;
295 - (void)setImplementation: (wxToolBarTool *) theImplementation;
296 - (wxToolBarTool*) implementation;
301 @implementation wxNSToolbarItem
303 - (id)initWithItemIdentifier: (NSString*) identifier
305 [super initWithItemIdentifier:identifier];
307 [self setTarget: self];
308 [self setAction: @selector(clickedAction:)];
312 - (void) clickedAction: (id) sender
320 - (void)setImplementation: (wxToolBarTool *) theImplementation
322 impl = theImplementation;
325 - (wxToolBarTool*) implementation
332 @implementation wxNSToolbarDelegate
334 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
339 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
344 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
349 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
351 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
354 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
355 if ( flag && tool->IsControl() )
357 NSView* view = tool->GetControl()->GetHandle();
358 [view removeFromSuperview];
360 wxSize sz = tool->GetControl()->GetSize();
361 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
362 [item setMaxSize:size];
363 [item setMinSize:size];
373 @implementation wxNSToolBarButton
375 - (id)initWithFrame:(NSRect)frame
377 [super initWithFrame:frame];
379 [self setTarget: self];
380 [self setAction: @selector(clickedAction:)];
384 - (void) clickedAction: (id) sender
392 - (void)setImplementation: (wxToolBarTool *) theImplementation
394 impl = theImplementation;
397 - (wxToolBarTool*) implementation
409 bool wxToolBarTool::DoEnable( bool enable )
413 GetControl()->Enable( enable );
415 else if ( IsButton() )
417 #if wxOSX_USE_NATIVE_TOOLBAR
418 if ( m_toolbarItem != NULL )
419 [m_toolbarItem setEnabled:enable];
422 if ( m_controlHandle != NULL )
423 [(NSControl*)m_controlHandle setEnabled:enable];
429 void wxToolBarTool::SetPosition( const wxPoint& position )
434 int mac_x = position.x;
435 int mac_y = position.y;
439 NSRect frame = [m_controlHandle frame];
440 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
442 frame.origin.x = mac_x;
443 frame.origin.y = mac_y;
444 [m_controlHandle setFrame:frame];
447 else if ( IsControl() )
449 // embedded native controls are moved by the OS
450 #if wxOSX_USE_NATIVE_TOOLBAR
451 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
454 GetControl()->Move( position );
459 NSRect frame = [m_controlHandle frame];
460 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
462 frame.origin.x = mac_x;
463 frame.origin.y = mac_y;
464 [m_controlHandle setFrame:frame];
469 void wxToolBarTool::UpdateImages()
471 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
473 if ( CanBeToggled() )
475 int w = m_bmpNormal.GetWidth();
476 int h = m_bmpNormal.GetHeight();
477 m_alternateBitmap = wxBitmap( w, h );
480 dc.SelectObject( m_alternateBitmap );
481 dc.SetPen( wxPen(*wxBLACK) );
482 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
483 dc.DrawRectangle( 0, 0, w, h );
484 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
485 dc.SelectObject( wxNullBitmap );
487 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
489 UpdateToggleImage( CanBeToggled() && IsToggled() );
492 void wxToolBarTool::UpdateToggleImage( bool toggle )
494 #if wxOSX_USE_NATIVE_TOOLBAR
495 if (m_toolbarItem != NULL )
497 // the native toolbar item only has a 'selected' state (one for one toolbar)
498 // so we emulate the toggle here
499 if ( CanBeToggled() && toggle )
500 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
502 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
507 wxToolBarTool::wxToolBarTool(
510 const wxString& label,
511 const wxBitmap& bmpNormal,
512 const wxBitmap& bmpDisabled,
514 wxObject *clientData,
515 const wxString& shortHelp,
516 const wxString& longHelp )
519 tbar, id, label, bmpNormal, bmpDisabled, kind,
520 clientData, shortHelp, longHelp )
526 #pragma mark Toolbar Implementation
528 wxToolBarToolBase *wxToolBar::CreateTool(
530 const wxString& label,
531 const wxBitmap& bmpNormal,
532 const wxBitmap& bmpDisabled,
534 wxObject *clientData,
535 const wxString& shortHelp,
536 const wxString& longHelp )
538 return new wxToolBarTool(
539 this, id, label, bmpNormal, bmpDisabled, kind,
540 clientData, shortHelp, longHelp );
544 wxToolBar::CreateTool(wxControl *control, const wxString& label)
546 return new wxToolBarTool(this, control, label);
549 void wxToolBar::Init()
553 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
554 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
556 #if wxOSX_USE_NATIVE_TOOLBAR
558 m_macUsesNativeToolbar = false;
562 // also for the toolbar we have the dual implementation:
563 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
565 bool wxToolBar::Create(
571 const wxString& name )
573 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
578 OSStatus err = noErr;
580 #if wxOSX_USE_NATIVE_TOOLBAR
582 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
584 static wxNSToolbarDelegate* controller = nil;
586 if ( controller == nil )
587 controller = [[wxNSToolbarDelegate alloc] init];
588 wxString identifier = wxString::Format( wxT("%p"), this );
589 wxCFStringRef cfidentifier(identifier);
590 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
594 if (m_macToolbar != NULL)
596 [tb setDelegate:controller];
598 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
599 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
601 if ( style & wxTB_NOICONS )
602 mode = NSToolbarDisplayModeLabelOnly;
603 else if ( style & wxTB_TEXT )
604 mode = NSToolbarDisplayModeIconAndLabel;
606 mode = NSToolbarDisplayModeIconOnly;
608 [tb setDisplayMode:mode];
609 [tb setSizeMode:displaySize];
612 #endif // wxOSX_USE_NATIVE_TOOLBAR
614 return (err == noErr);
617 wxToolBar::~wxToolBar()
619 #if wxOSX_USE_NATIVE_TOOLBAR
620 if (m_macToolbar != NULL)
622 // if this is the installed toolbar, then deinstall it
623 if (m_macUsesNativeToolbar)
624 MacInstallNativeToolbar( false );
626 [(NSToolbar*)m_macToolbar release];
632 bool wxToolBar::Show( bool show )
634 WXWindow tlw = MacGetTopLevelWindowRef();
635 bool bResult = (tlw != NULL);
639 #if wxOSX_USE_NATIVE_TOOLBAR
640 bool ownToolbarInstalled = false;
641 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
642 if (ownToolbarInstalled)
644 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
646 [(NSToolbar*)m_macToolbar setVisible:show];
649 bResult = wxToolBarBase::Show( show );
652 bResult = wxToolBarBase::Show( show );
659 bool wxToolBar::IsShown() const
663 #if wxOSX_USE_NATIVE_TOOLBAR
664 bool ownToolbarInstalled;
666 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
667 if (ownToolbarInstalled)
669 bResult = [(NSToolbar*)m_macToolbar isVisible];
672 bResult = wxToolBarBase::IsShown();
675 bResult = wxToolBarBase::IsShown();
681 void wxToolBar::DoGetSize( int *width, int *height ) const
683 #if wxOSX_USE_NATIVE_TOOLBAR
684 bool ownToolbarInstalled;
686 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
687 if ( ownToolbarInstalled )
689 WXWindow tlw = MacGetTopLevelWindowRef();
690 float toolbarHeight = 0.0;
693 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
695 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
696 styleMask:[tlw styleMask]];
697 toolbarHeight = NSHeight(windowFrame)
698 - NSHeight([[tlw contentView] frame]);
702 *width = windowFrame.size.width;
703 if ( height != NULL )
704 *height = toolbarHeight;
707 wxToolBarBase::DoGetSize( width, height );
710 wxToolBarBase::DoGetSize( width, height );
714 wxSize wxToolBar::DoGetBestSize() const
718 DoGetSize( &width, &height );
720 return wxSize( width, height );
723 void wxToolBar::SetWindowStyleFlag( long style )
725 wxToolBarBase::SetWindowStyleFlag( style );
727 #if wxOSX_USE_NATIVE_TOOLBAR
728 if (m_macToolbar != NULL)
730 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
732 if ( style & wxTB_NOICONS )
733 mode = NSToolbarDisplayModeLabelOnly;
734 else if ( style & wxTB_TEXT )
735 mode = NSToolbarDisplayModeIconAndLabel;
737 mode = NSToolbarDisplayModeIconOnly;
739 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
744 #if wxOSX_USE_NATIVE_TOOLBAR
745 bool wxToolBar::MacWantsNativeToolbar()
747 return m_macUsesNativeToolbar;
750 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
752 bool bResultV = false;
754 if (ownToolbarInstalled != NULL)
755 *ownToolbarInstalled = false;
757 WXWindow tlw = MacGetTopLevelWindowRef();
760 NSToolbar* curToolbarRef = [tlw toolbar];
761 bResultV = (curToolbarRef != NULL);
762 if (bResultV && (ownToolbarInstalled != NULL))
763 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
769 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
771 bool bResult = false;
773 if (usesNative && (m_macToolbar == NULL))
776 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
779 WXWindow tlw = MacGetTopLevelWindowRef();
783 // check the existing toolbar
784 NSToolbar* curToolbarRef = [tlw toolbar];
786 m_macUsesNativeToolbar = usesNative;
788 if (m_macUsesNativeToolbar)
790 // only install toolbar if there isn't one installed already
791 if (curToolbarRef == NULL)
794 [tlw setToolbar:(NSToolbar*) m_macToolbar];
795 [(NSToolbar*) m_macToolbar setVisible:YES];
797 m_peer->Move(0,0,0,0 );
798 SetSize( wxSIZE_AUTO_WIDTH, 0 );
799 m_peer->SetVisibility( false );
800 wxToolBarBase::Show( false );
805 // only deinstall toolbar if this is the installed one
806 if (m_macToolbar == curToolbarRef)
809 [(NSToolbar*) m_macToolbar setVisible:NO];
810 [tlw setToolbar:nil];
811 m_peer->SetVisibility( true );
816 InvalidateBestSize();
818 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
823 bool wxToolBar::Realize()
825 if (m_tools.GetCount() == 0)
831 int maxToolWidth = 0;
832 int maxToolHeight = 0;
834 int x = m_xMargin + kwxMacToolBarLeftMargin;
835 int y = m_yMargin + kwxMacToolBarTopMargin;
840 // find the maximum tool width and height
842 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
845 tool = (wxToolBarTool *) node->GetData();
848 wxSize sz = tool->GetSize();
850 if ( sz.x > maxToolWidth )
852 if ( sz.y > maxToolHeight )
853 maxToolHeight = sz.y;
856 node = node->GetNext();
859 bool lastIsRadio = false;
860 bool curIsRadio = false;
862 #if wxOSX_USE_NATIVE_TOOLBAR
863 CFIndex currentPosition = 0;
864 bool insertAll = false;
866 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
871 enc = f.GetEncoding();
873 enc = wxFont::GetDefaultEncoding();
876 node = m_tools.GetFirst();
879 tool = (wxToolBarTool*) node->GetData();
882 node = node->GetNext();
886 // set tool position:
887 // for the moment just perform a single row/column alignment
888 wxSize cursize = tool->GetSize();
889 if ( x + cursize.x > maxWidth )
890 maxWidth = x + cursize.x;
891 if ( y + cursize.y > maxHeight )
892 maxHeight = y + cursize.y;
894 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
896 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
897 tool->SetPosition( wxPoint(x1, y) );
901 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
902 tool->SetPosition( wxPoint(x, y1) );
905 // update the item positioning state
906 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
907 y += cursize.y + kwxMacToolSpacing;
909 x += cursize.x + kwxMacToolSpacing;
911 #if wxOSX_USE_NATIVE_TOOLBAR
912 // install in native NSToolbar
915 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
916 if ( hiItemRef != NULL )
918 // since setting the help texts is non-virtual we have to update
920 wxCFStringRef sh( tool->GetShortHelp(), enc);
921 [hiItemRef setToolTip:sh.AsNSString()];
923 if ( insertAll || (tool->GetIndex() != currentPosition) )
929 // if this is the first tool that gets newly inserted or repositioned
930 // first remove all 'old' tools from here to the right, because of this
931 // all following tools will have to be reinserted (insertAll).
932 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
934 node2 = node2->GetPrevious() )
936 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
938 const long idx = tool2->GetIndex();
941 [refTB removeItemAtIndex:idx];
946 wxString identifier = wxString::Format( wxT("%d"), (int) tool );
947 wxCFStringRef cfidentifier(identifier);
949 [refTB insertItemWithItemIdentifier:cfidentifier.AsNSString() atIndex:currentPosition];
950 tool->SetIndex( currentPosition );
958 // update radio button (and group) state
959 lastIsRadio = curIsRadio;
960 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
964 if ( tool->IsToggled() )
965 DoToggleTool( tool, true );
971 if ( tool->Toggle( true ) )
973 DoToggleTool( tool, true );
976 else if ( tool->IsToggled() )
978 if ( tool->IsToggled() )
979 DoToggleTool( tool, true );
981 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
984 wxToolBarToolBase *toggleTool = nodePrev->GetData();
985 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
988 if ( toggleTool->Toggle( false ) )
989 DoToggleTool( toggleTool, false );
991 nodePrev = nodePrev->GetPrevious();
996 node = node->GetNext();
999 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
1001 // if not set yet, only one row
1002 if ( m_maxRows <= 0 )
1005 m_minWidth = maxWidth;
1007 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1008 m_minHeight = m_maxHeight = maxHeight;
1012 // if not set yet, have one column
1013 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1014 SetRows( GetToolsCount() );
1016 m_minHeight = maxHeight;
1018 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1019 m_minWidth = m_maxWidth = maxWidth;
1023 // FIXME: should this be OSX-only?
1025 bool wantNativeToolbar, ownToolbarInstalled;
1027 // attempt to install the native toolbar
1028 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1029 MacInstallNativeToolbar( wantNativeToolbar );
1030 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1031 if (!ownToolbarInstalled)
1033 SetSize( maxWidth, maxHeight );
1034 InvalidateBestSize();
1038 SetSize( maxWidth, maxHeight );
1039 InvalidateBestSize();
1047 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1049 m_defaultWidth = size.x + kwxMacToolBorder;
1050 m_defaultHeight = size.y + kwxMacToolBorder;
1052 #if wxOSX_USE_NATIVE_TOOLBAR
1053 if (m_macToolbar != NULL)
1055 int maxs = wxMax( size.x, size.y );
1056 NSToolbarSizeMode sizeSpec;
1058 sizeSpec = NSToolbarSizeModeRegular;
1059 else if ( maxs > 24 )
1060 sizeSpec = NSToolbarSizeModeDefault;
1062 sizeSpec = NSToolbarSizeModeSmall;
1064 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1069 // The button size is bigger than the bitmap size
1070 wxSize wxToolBar::GetToolSize() const
1072 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1075 void wxToolBar::SetRows(int nRows)
1077 // avoid resizing the frame uselessly
1078 if ( nRows != m_maxRows )
1082 void wxToolBar::MacSuperChangedPosition()
1084 wxWindow::MacSuperChangedPosition();
1086 #if wxOSX_USE_NATIVE_TOOLBAR
1087 if (! m_macUsesNativeToolbar )
1095 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1097 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1100 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1102 tool->SetNormalBitmap(bitmap);
1104 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1105 tool->UpdateImages();
1109 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1111 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1114 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1116 tool->SetDisabledBitmap(bitmap);
1118 // TODO: what to do for this one?
1122 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1124 wxToolBarTool *tool;
1125 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1128 tool = (wxToolBarTool *)node->GetData();
1131 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1132 if ( r.Contains( wxPoint( x, y ) ) )
1136 node = node->GetNext();
1139 return (wxToolBarToolBase*)NULL;
1142 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1144 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1146 return tool->GetShortHelp();
1148 return wxEmptyString;
1151 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1154 ((wxToolBarTool*)t)->DoEnable( enable );
1157 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1159 wxToolBarTool *tool = (wxToolBarTool *)t;
1160 if ( ( tool != NULL ) && tool->IsButton() )
1161 tool->UpdateToggleImage( toggle );
1164 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1166 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
1170 wxSize toolSize = GetToolSize();
1171 WXWidget controlHandle = NULL;
1172 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1174 #if wxOSX_USE_NATIVE_TOOLBAR
1175 wxString label = tool->GetLabel();
1176 if (m_macToolbar && !label.empty() )
1178 // strip mnemonics from the label for compatibility
1179 // with the usual labels in wxStaticText sense
1180 label = wxStripMenuCodes(label);
1182 #endif // wxOSX_USE_NATIVE_TOOLBAR
1184 switch (tool->GetStyle())
1186 case wxTOOL_STYLE_SEPARATOR:
1188 wxASSERT( tool->GetControlHandle() == NULL );
1191 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1192 toolrect.size.height = toolSize.y;
1194 toolrect.size.width = toolSize.x;
1196 // in flat style we need a visual separator
1197 #if wxOSX_USE_NATIVE_TOOLBAR
1198 if (m_macToolbar != NULL)
1200 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarSeparatorItemIdentifier];
1201 tool->SetToolbarItemRef( item );
1203 #endif // wxOSX_USE_NATIVE_TOOLBAR
1205 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1206 [box setBoxType:NSBoxSeparator];
1207 controlHandle = box;
1208 tool->SetControlHandle( controlHandle );
1212 case wxTOOL_STYLE_BUTTON:
1214 wxASSERT( tool->GetControlHandle() == NULL );
1216 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1218 [v setBezelStyle:NSRegularSquareBezelStyle];
1220 [v setButtonType: ( tool->CanBeToggled() ? NSOnOffButton : NSMomentaryPushInButton )];
1221 [v setImplementation:tool];
1225 #if wxOSX_USE_NATIVE_TOOLBAR
1226 if (m_macToolbar != NULL)
1228 wxString identifier = wxString::Format(wxT("%d"), (int) tool);
1229 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1230 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1231 [item setImplementation:tool];
1232 tool->SetToolbarItemRef( item );
1235 #endif // wxOSX_USE_NATIVE_TOOLBAR
1236 tool->SetControlHandle( controlHandle );
1237 tool->UpdateImages();
1238 tool->UpdateLabel();
1240 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1241 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1244 InstallControlEventHandler(
1245 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1246 GetEventTypeCount(eventList), eventList, tool, NULL );
1251 case wxTOOL_STYLE_CONTROL:
1253 #if wxOSX_USE_NATIVE_TOOLBAR
1254 if (m_macToolbar != NULL)
1256 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1257 wxCHECK_MSG( view, false, _T("control must be non-NULL") );
1259 wxString identifier = wxString::Format(wxT("%d"), (int) tool);
1260 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1261 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1262 [item setImplementation:tool];
1263 tool->SetToolbarItemRef( item );
1266 // right now there's nothing to do here
1268 tool->UpdateLabel();
1275 if ( controlHandle )
1277 WXWidget container = (WXWidget) GetHandle();
1278 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1280 // SetControlVisibility( controlHandle, true, true );
1281 [container addSubview:controlHandle];
1284 // nothing special to do here - we relayout in Realize() later
1285 InvalidateBestSize();
1291 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1293 wxFAIL_MSG( wxT("not implemented") );
1296 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1298 wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase );
1299 wxToolBarToolsList::compatibility_iterator node;
1300 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1302 wxToolBarToolBase *tool2 = node->GetData();
1303 if ( tool2 == tool )
1305 // let node point to the next node in the list
1306 node = node->GetNext();
1312 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1314 #if wxOSX_USE_NATIVE_TOOLBAR
1315 CFIndex removeIndex = tool->GetIndex();
1318 #if wxOSX_USE_NATIVE_TOOLBAR
1319 if (m_macToolbar != NULL)
1321 if ( removeIndex != -1 && m_macToolbar )
1323 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1324 tool->SetIndex( -1 );
1329 tool->ClearControl();
1331 // and finally reposition all the controls after this one
1333 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1335 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1336 wxPoint pt = tool2->GetPosition();
1338 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1343 tool2->SetPosition( pt );
1345 #if wxOSX_USE_NATIVE_TOOLBAR
1346 if (m_macToolbar != NULL)
1348 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1349 tool2->SetIndex( tool2->GetIndex() - 1 );
1354 InvalidateBestSize();
1359 #include <Carbon/Carbon.h>
1361 void wxToolBar::OnPaint(wxPaintEvent& event)
1363 #if wxOSX_USE_NATIVE_TOOLBAR
1364 if ( m_macUsesNativeToolbar )
1376 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1378 if ( !drawMetalTheme )
1380 HIThemePlacardDrawInfo info;
1381 memset( &info, 0, sizeof(info) );
1383 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1385 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1386 HIRect rect = CGRectMake( 0, 0, w, h );
1387 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1391 // leave the background as it is (striped or metal)
1397 #endif // wxUSE_TOOLBAR