1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/toolbar.cpp
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: toolbar.cpp 54954 2008-08-03 11:27:03Z VZ $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/toolbar.h"
23 #include "wx/osx/private.h"
24 #include "wx/geometry.h"
25 #include "wx/sysopt.h"
27 const short kwxMacToolBarToolDefaultWidth = 16;
28 const short kwxMacToolBarToolDefaultHeight = 16;
29 const short kwxMacToolBarTopMargin = 4;
30 const short kwxMacToolBarLeftMargin = 4;
31 const short kwxMacToolBorder = 0;
32 const short kwxMacToolSpacing = 6;
34 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
35 EVT_PAINT( wxToolBar::OnPaint )
40 #pragma mark Tool Implementation
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // We have a dual implementation for each tool, WXWidget and NSToolbarItem*
48 // when embedding native controls in the native toolbar we must make sure the
49 // control does not get deleted behind our backs, so the retain count gets increased
50 // (after creation it is 1), first be the creation of the custom NSToolbarItem wrapper
51 // object, and second by the code 'creating' the custom HIView (which is the same as the
52 // already existing native control, therefore we just increase the ref count)
53 // when this view is removed from the native toolbar its count gets decremented again
54 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
55 // so in the end the control lives with a refcount of one and can be disposed of by the
56 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
57 // so we can only test against a range, not a specific value of the refcount.
59 class wxToolBarTool : public wxToolBarToolBase
65 const wxString& label,
66 const wxBitmap& bmpNormal,
67 const wxBitmap& bmpDisabled,
70 const wxString& shortHelp,
71 const wxString& longHelp );
73 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
74 : wxToolBarToolBase(tbar, control, label)
78 SetControlHandle( (WXWidget) control->GetHandle() );
81 virtual ~wxToolBarTool()
86 WXWidget GetControlHandle()
88 return (WXWidget) m_controlHandle;
91 void SetControlHandle( WXWidget handle )
93 m_controlHandle = handle;
96 void SetPosition( const wxPoint& position );
100 if ( m_controlHandle )
104 [m_controlHandle retain];
108 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
109 // proper wxControl destructor
111 m_controlHandle = NULL ;
114 #if wxOSX_USE_NATIVE_TOOLBAR
117 [m_toolbarItem release];
118 m_toolbarItem = NULL;
120 #endif // wxOSX_USE_NATIVE_TOOLBAR
123 wxSize GetSize() const
129 curSize = GetControl()->GetSize();
131 else if ( IsButton() )
133 curSize = GetToolBar()->GetToolSize();
138 curSize = GetToolBar()->GetToolSize();
139 if ( GetToolBar()->GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
148 wxPoint GetPosition() const
150 return wxPoint( m_x, m_y );
153 bool Enable( bool enable );
157 void UpdateToggleImage( bool toggle );
161 #if wxOSX_USE_NATIVE_TOOLBAR
164 // strip mnemonics from the label for compatibility with the usual
165 // labels in wxStaticText sense
166 wxString labelStr = wxStripMenuCodes(m_label);
167 wxCFStringRef l(labelStr, GetToolBarFontEncoding());
169 [m_toolbarItem setLabel:l.AsNSString()];
171 wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() );
172 [m_toolbarItem setToolTip:sh.AsNSString()];
179 wxToolBar *tbar = (wxToolBar*) GetToolBar();
184 shouldToggle = !IsToggled();
185 tbar->ToggleTool( GetId(), shouldToggle );
188 tbar->OnLeftClick( GetId(), IsToggled() );
191 #if wxOSX_USE_NATIVE_TOOLBAR
192 void SetToolbarItemRef( NSToolbarItem* ref )
194 if ( m_controlHandle )
195 [m_controlHandle setHidden:YES];
197 [m_toolbarItem release];
202 NSToolbarItem* GetToolbarItemRef() const
204 return m_toolbarItem;
207 void SetIndex( CFIndex idx )
212 CFIndex GetIndex() const
217 virtual void SetLabel(const wxString& label)
219 wxToolBarToolBase::SetLabel(label);
223 virtual bool SetShortHelp(const wxString& help)
225 if ( !wxToolBarToolBase::SetShortHelp(help) )
232 #endif // wxOSX_USE_NATIVE_TOOLBAR
235 #if wxOSX_USE_NATIVE_TOOLBAR
236 wxFontEncoding GetToolBarFontEncoding() const
240 f = GetToolBar()->GetFont();
241 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
243 #endif // wxOSX_USE_NATIVE_TOOLBAR
247 m_controlHandle = NULL;
249 #if wxOSX_USE_NATIVE_TOOLBAR
250 m_toolbarItem = NULL;
255 WXWidget m_controlHandle;
258 wxBitmap m_alternateBitmap;
260 #if wxOSX_USE_NATIVE_TOOLBAR
261 NSToolbarItem* m_toolbarItem;
262 // position in its toolbar, -1 means not inserted
267 #if wxOSX_USE_NATIVE_TOOLBAR
269 @interface wxNSToolbarItem : NSToolbarItem
274 - (id) initWithItemIdentifier: (NSString*) identifier;
275 - (void)setImplementation: (wxToolBarTool *) theImplementation;
276 - (wxToolBarTool*) implementation;
277 - (void) clickedAction: (id) sender;
278 - (BOOL) validateToolbarItem:(NSToolbarItem *)theItem;
283 @interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER(<NSToolbarDelegate>)
287 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
289 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
291 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
293 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
301 @interface wxNSToolBarButton : NSButton
306 - (id)initWithFrame:(NSRect)frame;
307 - (void) clickedAction: (id) sender;
308 - (void)setImplementation: (wxToolBarTool *) theImplementation;
309 - (wxToolBarTool*) implementation;
314 #if wxOSX_USE_NATIVE_TOOLBAR
316 @implementation wxNSToolbarItem
318 - (id)initWithItemIdentifier: (NSString*) identifier
320 [super initWithItemIdentifier:identifier];
322 [self setTarget: self];
323 [self setAction: @selector(clickedAction:)];
327 - (void) clickedAction: (id) sender
336 - (void)setImplementation: (wxToolBarTool *) theImplementation
338 impl = theImplementation;
341 - (wxToolBarTool*) implementation
346 - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
348 wxUnusedVar(theItem);
349 return impl->IsEnabled() ? YES:NO;
354 @implementation wxNSToolbarDelegate
356 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
358 wxUnusedVar(toolbar);
362 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
364 wxUnusedVar(toolbar);
368 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
370 wxUnusedVar(toolbar);
374 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
376 wxUnusedVar(toolbar);
378 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue];
380 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
384 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
385 if ( flag && tool->IsControl() )
387 NSView* view = tool->GetControl()->GetHandle();
388 [view removeFromSuperview];
390 wxSize sz = tool->GetControl()->GetSize();
391 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
392 [item setMaxSize:size];
393 [item setMinSize:size];
405 @implementation wxNSToolBarButton
407 - (id)initWithFrame:(NSRect)frame
409 [super initWithFrame:frame];
411 [self setTarget: self];
412 [self setAction: @selector(clickedAction:)];
416 - (void) clickedAction: (id) sender
425 - (void)setImplementation: (wxToolBarTool *) theImplementation
427 impl = theImplementation;
430 - (wxToolBarTool*) implementation
442 bool wxToolBarTool::Enable( bool enable )
444 if ( wxToolBarToolBase::Enable( enable ) == false )
449 GetControl()->Enable( enable );
451 else if ( IsButton() )
453 #if wxOSX_USE_NATIVE_TOOLBAR
454 if ( m_toolbarItem != NULL )
455 [m_toolbarItem setEnabled:enable];
458 if ( m_controlHandle != NULL )
459 [(NSControl*)m_controlHandle setEnabled:enable];
465 void wxToolBarTool::SetPosition( const wxPoint& position )
470 int mac_x = position.x;
471 int mac_y = position.y;
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];
483 else if ( IsControl() )
485 // embedded native controls are moved by the OS
486 #if wxOSX_USE_NATIVE_TOOLBAR
487 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
490 GetControl()->Move( position );
495 NSRect frame = [m_controlHandle frame];
496 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
498 frame.origin.x = mac_x;
499 frame.origin.y = mac_y;
500 [m_controlHandle setFrame:frame];
505 void wxToolBarTool::UpdateImages()
507 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
509 if ( CanBeToggled() )
511 int w = m_bmpNormal.GetWidth();
512 int h = m_bmpNormal.GetHeight();
513 m_alternateBitmap = wxBitmap( w, h );
516 dc.SelectObject( m_alternateBitmap );
517 dc.SetPen( wxPen(*wxBLACK) );
518 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
519 dc.DrawRoundedRectangle( 0, 0, w, h, 2 );
520 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
521 dc.SelectObject( wxNullBitmap );
523 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
525 UpdateToggleImage( CanBeToggled() && IsToggled() );
528 void wxToolBarTool::UpdateToggleImage( bool toggle )
530 #if wxOSX_USE_NATIVE_TOOLBAR
531 if (m_toolbarItem != NULL )
533 // the native toolbar item only has a 'selected' state (one for one toolbar)
534 // so we emulate the toggle here
535 if ( CanBeToggled() && toggle )
536 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
538 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
543 [(NSButton*)m_controlHandle setState:(toggle ? NSOnState : NSOffState)];
547 wxToolBarTool::wxToolBarTool(
550 const wxString& label,
551 const wxBitmap& bmpNormal,
552 const wxBitmap& bmpDisabled,
554 wxObject *clientData,
555 const wxString& shortHelp,
556 const wxString& longHelp )
559 tbar, id, label, bmpNormal, bmpDisabled, kind,
560 clientData, shortHelp, longHelp )
566 #pragma mark Toolbar Implementation
568 wxToolBarToolBase *wxToolBar::CreateTool(
570 const wxString& label,
571 const wxBitmap& bmpNormal,
572 const wxBitmap& bmpDisabled,
574 wxObject *clientData,
575 const wxString& shortHelp,
576 const wxString& longHelp )
578 return new wxToolBarTool(
579 this, id, label, bmpNormal, bmpDisabled, kind,
580 clientData, shortHelp, longHelp );
584 wxToolBar::CreateTool(wxControl *control, const wxString& label)
586 return new wxToolBarTool(this, control, label);
589 void wxToolBar::Init()
593 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
594 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
596 #if wxOSX_USE_NATIVE_TOOLBAR
598 m_macUsesNativeToolbar = false;
602 // also for the toolbar we have the dual implementation:
603 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
605 bool wxToolBar::Create(
611 const wxString& name )
613 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
618 OSStatus err = noErr;
620 #if wxOSX_USE_NATIVE_TOOLBAR
622 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
624 static wxNSToolbarDelegate* controller = nil;
626 if ( controller == nil )
627 controller = [[wxNSToolbarDelegate alloc] init];
628 wxString identifier = wxString::Format( wxT("%p"), this );
629 wxCFStringRef cfidentifier(identifier);
630 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
634 if (m_macToolbar != NULL)
636 [tb setDelegate:controller];
638 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
639 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
641 if ( style & wxTB_NOICONS )
642 mode = NSToolbarDisplayModeLabelOnly;
643 else if ( style & wxTB_TEXT )
644 mode = NSToolbarDisplayModeIconAndLabel;
646 mode = NSToolbarDisplayModeIconOnly;
648 [tb setDisplayMode:mode];
649 [tb setSizeMode:displaySize];
652 #endif // wxOSX_USE_NATIVE_TOOLBAR
654 return (err == noErr);
657 wxToolBar::~wxToolBar()
659 [(NSToolbar*)m_macToolbar setDelegate:nil];
660 [(NSToolbar*)m_macToolbar release];
664 bool wxToolBar::Show( bool show )
666 WXWindow tlw = MacGetTopLevelWindowRef();
667 bool bResult = (tlw != NULL);
671 #if wxOSX_USE_NATIVE_TOOLBAR
672 bool ownToolbarInstalled = false;
673 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
674 if (ownToolbarInstalled)
676 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
678 [(NSToolbar*)m_macToolbar setVisible:show];
681 bResult = wxToolBarBase::Show( show );
684 bResult = wxToolBarBase::Show( show );
691 bool wxToolBar::IsShown() const
695 #if wxOSX_USE_NATIVE_TOOLBAR
696 bool ownToolbarInstalled;
698 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
699 if (ownToolbarInstalled)
701 bResult = [(NSToolbar*)m_macToolbar isVisible];
704 bResult = wxToolBarBase::IsShown();
707 bResult = wxToolBarBase::IsShown();
713 void wxToolBar::DoGetSize( int *width, int *height ) const
715 #if wxOSX_USE_NATIVE_TOOLBAR
716 bool ownToolbarInstalled;
718 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
719 if ( ownToolbarInstalled )
721 WXWindow tlw = MacGetTopLevelWindowRef();
722 float toolbarHeight = 0.0;
723 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
725 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
727 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
728 styleMask:[tlw styleMask]];
729 toolbarHeight = NSHeight(windowFrame)
730 - NSHeight([[tlw contentView] frame]);
734 *width = (int)windowFrame.size.width;
735 if ( height != NULL )
736 *height = (int)toolbarHeight;
739 wxToolBarBase::DoGetSize( width, height );
742 wxToolBarBase::DoGetSize( width, height );
746 wxSize wxToolBar::DoGetBestSize() const
750 DoGetSize( &width, &height );
752 return wxSize( width, height );
755 void wxToolBar::SetWindowStyleFlag( long style )
757 wxToolBarBase::SetWindowStyleFlag( style );
759 #if wxOSX_USE_NATIVE_TOOLBAR
760 if (m_macToolbar != NULL)
762 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
764 if ( style & wxTB_NOICONS )
765 mode = NSToolbarDisplayModeLabelOnly;
766 else if ( style & wxTB_TEXT )
767 mode = NSToolbarDisplayModeIconAndLabel;
769 mode = NSToolbarDisplayModeIconOnly;
771 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
776 #if wxOSX_USE_NATIVE_TOOLBAR
777 bool wxToolBar::MacWantsNativeToolbar()
779 return m_macUsesNativeToolbar;
782 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
784 bool bResultV = false;
786 if (ownToolbarInstalled != NULL)
787 *ownToolbarInstalled = false;
789 WXWindow tlw = MacGetTopLevelWindowRef();
792 NSToolbar* curToolbarRef = [tlw toolbar];
793 bResultV = (curToolbarRef != NULL);
794 if (bResultV && (ownToolbarInstalled != NULL))
795 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
801 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
803 bool bResult = false;
805 if (usesNative && (m_macToolbar == NULL))
808 if (usesNative && ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM)) != 0))
811 WXWindow tlw = MacGetTopLevelWindowRef();
815 // check the existing toolbar
816 NSToolbar* curToolbarRef = [tlw toolbar];
818 m_macUsesNativeToolbar = usesNative;
820 if (m_macUsesNativeToolbar)
822 // only install toolbar if there isn't one installed already
823 if (curToolbarRef == NULL)
826 [tlw setToolbar:(NSToolbar*) m_macToolbar];
827 [(NSToolbar*) m_macToolbar setVisible:YES];
829 m_peer->Move(0,0,0,0 );
830 SetSize( wxSIZE_AUTO_WIDTH, 0 );
831 m_peer->SetVisibility( false );
832 wxToolBarBase::Show( false );
837 // only deinstall toolbar if this is the installed one
838 if (m_macToolbar == curToolbarRef)
841 [(NSToolbar*) m_macToolbar setVisible:NO];
842 MacUninstallNativeToolbar();
843 m_peer->SetVisibility( true );
848 InvalidateBestSize();
850 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
854 void wxToolBar::MacUninstallNativeToolbar()
859 WXWindow tlw = MacGetTopLevelWindowRef();
861 [tlw setToolbar:nil];
865 bool wxToolBar::Realize()
867 if ( !wxToolBarBase::Realize() )
873 int maxToolWidth = 0;
874 int maxToolHeight = 0;
876 int x = m_xMargin + kwxMacToolBarLeftMargin;
877 int y = m_yMargin + kwxMacToolBarTopMargin;
882 // find the maximum tool width and height
884 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
887 tool = (wxToolBarTool *) node->GetData();
890 wxSize sz = tool->GetSize();
892 if ( sz.x > maxToolWidth )
894 if ( sz.y > maxToolHeight )
895 maxToolHeight = sz.y;
898 node = node->GetNext();
901 bool lastIsRadio = false;
902 bool curIsRadio = false;
904 #if wxOSX_USE_NATIVE_TOOLBAR
905 CFIndex currentPosition = 0;
906 bool insertAll = false;
908 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
913 enc = f.GetEncoding();
915 enc = wxFont::GetDefaultEncoding();
918 node = m_tools.GetFirst();
921 tool = (wxToolBarTool*) node->GetData();
924 node = node->GetNext();
928 // set tool position:
929 // for the moment just perform a single row/column alignment
930 wxSize cursize = tool->GetSize();
931 if ( x + cursize.x > maxWidth )
932 maxWidth = x + cursize.x;
933 if ( y + cursize.y > maxHeight )
934 maxHeight = y + cursize.y;
936 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
938 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
939 tool->SetPosition( wxPoint(x1, y) );
943 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
944 tool->SetPosition( wxPoint(x, y1) );
947 // update the item positioning state
948 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
949 y += cursize.y + kwxMacToolSpacing;
951 x += cursize.x + kwxMacToolSpacing;
953 #if wxOSX_USE_NATIVE_TOOLBAR
954 // install in native NSToolbar
957 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
958 if ( hiItemRef != NULL )
960 // since setting the help texts is non-virtual we have to update
962 wxCFStringRef sh( tool->GetShortHelp(), enc);
963 [hiItemRef setToolTip:sh.AsNSString()];
965 if ( insertAll || (tool->GetIndex() != currentPosition) )
971 // if this is the first tool that gets newly inserted or repositioned
972 // first remove all 'old' tools from here to the right, because of this
973 // all following tools will have to be reinserted (insertAll).
974 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
976 node2 = node2->GetPrevious() )
978 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
980 const long idx = tool2->GetIndex();
983 [refTB removeItemAtIndex:idx];
989 wxCFStringRef cfidentifier;
990 const NSString *nsItemId;
991 if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR)
993 nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier
994 : NSToolbarSeparatorItemIdentifier;
998 cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool));
999 nsItemId = cfidentifier.AsNSString();
1002 [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition];
1003 tool->SetIndex( currentPosition );
1011 // update radio button (and group) state
1012 lastIsRadio = curIsRadio;
1013 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1017 if ( tool->IsToggled() )
1018 DoToggleTool( tool, true );
1024 if ( tool->Toggle( true ) )
1026 DoToggleTool( tool, true );
1029 else if ( tool->IsToggled() )
1031 if ( tool->IsToggled() )
1032 DoToggleTool( tool, true );
1034 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1037 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1038 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1041 if ( toggleTool->Toggle( false ) )
1042 DoToggleTool( toggleTool, false );
1044 nodePrev = nodePrev->GetPrevious();
1049 node = node->GetNext();
1052 if ( GetWindowStyleFlag() & (wxTB_TOP|wxTB_BOTTOM) )
1054 // if not set yet, only one row
1055 if ( m_maxRows <= 0 )
1058 m_minWidth = maxWidth;
1060 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1061 m_minHeight = m_maxHeight = maxHeight;
1065 // if not set yet, have one column
1066 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1067 SetRows( GetToolsCount() );
1069 m_minHeight = maxHeight;
1071 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1072 m_minWidth = m_maxWidth = maxWidth;
1076 // FIXME: should this be OSX-only?
1078 bool wantNativeToolbar, ownToolbarInstalled;
1080 // attempt to install the native toolbar
1081 wantNativeToolbar = ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_BOTTOM|wxTB_RIGHT)) == 0);
1082 MacInstallNativeToolbar( wantNativeToolbar );
1083 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1084 if (!ownToolbarInstalled)
1086 SetSize( maxWidth, maxHeight );
1087 InvalidateBestSize();
1091 SetSize( maxWidth, maxHeight );
1092 InvalidateBestSize();
1100 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1102 m_defaultWidth = size.x + kwxMacToolBorder;
1103 m_defaultHeight = size.y + kwxMacToolBorder;
1105 #if wxOSX_USE_NATIVE_TOOLBAR
1106 if (m_macToolbar != NULL)
1108 int maxs = wxMax( size.x, size.y );
1109 NSToolbarSizeMode sizeSpec;
1111 sizeSpec = NSToolbarSizeModeRegular;
1112 else if ( maxs > 24 )
1113 sizeSpec = NSToolbarSizeModeDefault;
1115 sizeSpec = NSToolbarSizeModeSmall;
1117 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1122 // The button size is bigger than the bitmap size
1123 wxSize wxToolBar::GetToolSize() const
1125 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1128 void wxToolBar::SetRows(int nRows)
1130 // avoid resizing the frame uselessly
1131 if ( nRows != m_maxRows )
1135 void wxToolBar::MacSuperChangedPosition()
1137 wxWindow::MacSuperChangedPosition();
1139 #if wxOSX_USE_NATIVE_TOOLBAR
1140 if (! m_macUsesNativeToolbar )
1148 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1150 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1153 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1155 tool->SetNormalBitmap(bitmap);
1157 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1158 tool->UpdateImages();
1162 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1164 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1167 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1169 tool->SetDisabledBitmap(bitmap);
1171 // TODO: what to do for this one?
1175 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1177 wxToolBarTool *tool;
1178 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1181 tool = (wxToolBarTool *)node->GetData();
1184 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1185 if ( r.Contains( wxPoint( x, y ) ) )
1189 node = node->GetNext();
1195 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1197 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1199 return tool->GetShortHelp();
1201 return wxEmptyString;
1204 void wxToolBar::DoEnableTool(wxToolBarToolBase * WXUNUSED(t), bool WXUNUSED(enable))
1206 // everything already done in the tool's Enable implementation
1209 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1211 wxToolBarTool *tool = (wxToolBarTool *)t;
1212 if ( ( tool != NULL ) && tool->IsButton() )
1213 tool->UpdateToggleImage( toggle );
1216 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1218 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1222 wxSize toolSize = GetToolSize();
1223 WXWidget controlHandle = NULL;
1224 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1226 #if wxOSX_USE_NATIVE_TOOLBAR
1227 wxString label = tool->GetLabel();
1228 if (m_macToolbar && !label.empty() )
1230 // strip mnemonics from the label for compatibility
1231 // with the usual labels in wxStaticText sense
1232 label = wxStripMenuCodes(label);
1234 #endif // wxOSX_USE_NATIVE_TOOLBAR
1236 switch (tool->GetStyle())
1238 case wxTOOL_STYLE_SEPARATOR:
1240 wxASSERT( tool->GetControlHandle() == NULL );
1243 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
1244 toolrect.size.height = toolSize.y;
1246 toolrect.size.width = toolSize.x;
1248 // in flat style we need a visual separator
1249 #if wxOSX_USE_NATIVE_TOOLBAR
1250 if (m_macToolbar != NULL)
1252 const NSString * const
1253 nsItemId = tool->IsStretchable() ? NSToolbarFlexibleSpaceItemIdentifier
1254 : NSToolbarSeparatorItemIdentifier;
1255 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId];
1256 tool->SetToolbarItemRef( item );
1258 #endif // wxOSX_USE_NATIVE_TOOLBAR
1260 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1261 [box setBoxType:NSBoxSeparator];
1262 controlHandle = box;
1263 tool->SetControlHandle( controlHandle );
1267 case wxTOOL_STYLE_BUTTON:
1269 wxASSERT( tool->GetControlHandle() == NULL );
1271 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1273 [v setBezelStyle:NSRegularSquareBezelStyle];
1275 [v setButtonType: ( tool->CanBeToggled() ? NSToggleButton : NSMomentaryPushInButton )];
1276 [v setImplementation:tool];
1280 #if wxOSX_USE_NATIVE_TOOLBAR
1281 if (m_macToolbar != NULL)
1283 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1284 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1285 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1286 [item setImplementation:tool];
1287 tool->SetToolbarItemRef( item );
1290 #endif // wxOSX_USE_NATIVE_TOOLBAR
1291 tool->SetControlHandle( controlHandle );
1292 tool->UpdateImages();
1293 tool->UpdateLabel();
1295 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1296 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1299 InstallControlEventHandler(
1300 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1301 GetEventTypeCount(eventList), eventList, tool, NULL );
1306 case wxTOOL_STYLE_CONTROL:
1308 #if wxOSX_USE_NATIVE_TOOLBAR
1309 if (m_macToolbar != NULL)
1311 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1312 wxCHECK_MSG( view, false, wxT("control must be non-NULL") );
1314 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1315 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1316 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1317 [item setImplementation:tool];
1318 tool->SetToolbarItemRef( item );
1321 // right now there's nothing to do here
1323 tool->UpdateLabel();
1330 if ( controlHandle )
1332 WXWidget container = (WXWidget) GetHandle();
1333 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1335 // SetControlVisibility( controlHandle, true, true );
1336 [container addSubview:controlHandle];
1339 // nothing special to do here - we relayout in Realize() later
1340 InvalidateBestSize();
1346 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1348 wxFAIL_MSG( wxT("not implemented") );
1351 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1353 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1354 wxToolBarToolsList::compatibility_iterator node;
1355 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1357 wxToolBarToolBase *tool2 = node->GetData();
1358 if ( tool2 == tool )
1360 // let node point to the next node in the list
1361 node = node->GetNext();
1367 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1369 #if wxOSX_USE_NATIVE_TOOLBAR
1370 CFIndex removeIndex = tool->GetIndex();
1373 #if wxOSX_USE_NATIVE_TOOLBAR
1374 if (m_macToolbar != NULL)
1376 if ( removeIndex != -1 && m_macToolbar )
1378 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1379 tool->SetIndex( -1 );
1384 tool->ClearControl();
1386 // and finally reposition all the controls after this one
1388 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1390 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1391 wxPoint pt = tool2->GetPosition();
1393 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
1398 tool2->SetPosition( pt );
1400 #if wxOSX_USE_NATIVE_TOOLBAR
1401 if (m_macToolbar != NULL)
1403 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1404 tool2->SetIndex( tool2->GetIndex() - 1 );
1409 InvalidateBestSize();
1414 #include <Carbon/Carbon.h>
1416 void wxToolBar::OnPaint(wxPaintEvent& event)
1418 #if wxOSX_USE_NATIVE_TOOLBAR
1419 if ( m_macUsesNativeToolbar )
1431 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1433 if ( !drawMetalTheme )
1435 HIThemePlacardDrawInfo info;
1436 memset( &info, 0, sizeof(info) );
1438 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1440 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1441 HIRect rect = CGRectMake( 0, 0, w, h );
1442 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1446 // leave the background as it is (striped or metal)
1452 #endif // wxUSE_TOOLBAR