1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/toolbar.mm
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/toolbar.h"
22 #include "wx/osx/private.h"
23 #include "wx/geometry.h"
24 #include "wx/sysopt.h"
26 const short kwxMacToolBarToolDefaultWidth = 16;
27 const short kwxMacToolBarToolDefaultHeight = 16;
28 const short kwxMacToolBarTopMargin = 4;
29 const short kwxMacToolBarLeftMargin = 4;
30 const short kwxMacToolBorder = 0;
31 const short kwxMacToolSpacing = 6;
33 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
34 EVT_PAINT( wxToolBar::OnPaint )
39 #pragma mark Tool Implementation
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
47 @interface wxNSToolBarButton : NSButton
52 - (id)initWithFrame:(NSRect)frame;
53 - (void) clickedAction: (id) sender;
54 - (void)setImplementation: (wxToolBarTool *) theImplementation;
55 - (wxToolBarTool*) implementation;
60 // We have a dual implementation for each tool, WXWidget and NSToolbarItem*
62 // when embedding native controls in the native toolbar we must make sure the
63 // control does not get deleted behind our backs, so the retain count gets increased
64 // (after creation it is 1), first be the creation of the custom NSToolbarItem wrapper
65 // object, and second by the code 'creating' the custom HIView (which is the same as the
66 // already existing native control, therefore we just increase the ref count)
67 // when this view is removed from the native toolbar its count gets decremented again
68 // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well
69 // so in the end the control lives with a refcount of one and can be disposed of by the
70 // wxControl code. For embedded controls on a non-native toolbar this ref count is less
71 // so we can only test against a range, not a specific value of the refcount.
73 class wxToolBarTool : public wxToolBarToolBase
79 const wxString& label,
80 const wxBitmap& bmpNormal,
81 const wxBitmap& bmpDisabled,
84 const wxString& shortHelp,
85 const wxString& longHelp );
87 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
88 : wxToolBarToolBase(tbar, control, label)
92 SetControlHandle( (WXWidget) control->GetHandle() );
95 virtual ~wxToolBarTool()
100 WXWidget GetControlHandle()
102 return (WXWidget) m_controlHandle;
105 void SetControlHandle( WXWidget handle )
107 m_controlHandle = handle;
110 void SetPosition( const wxPoint& position );
114 if ( m_controlHandle )
118 [m_controlHandle retain];
122 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
123 // proper wxControl destructor
125 m_controlHandle = NULL ;
128 #if wxOSX_USE_NATIVE_TOOLBAR
131 [m_toolbarItem release];
132 m_toolbarItem = NULL;
134 #endif // wxOSX_USE_NATIVE_TOOLBAR
137 wxSize GetSize() const
143 curSize = GetControl()->GetSize();
145 else if ( IsButton() )
147 // curSize = GetToolBar()->GetToolSize();
148 NSRect best = [(wxNSToolBarButton*)m_controlHandle frame];
149 curSize = wxSize(best.size.width, best.size.height);
154 curSize = GetToolBar()->GetToolSize();
155 if ( GetToolBar()->IsVertical() )
164 wxPoint GetPosition() const
166 return wxPoint( m_x, m_y );
169 bool Enable( bool enable );
173 void UpdateToggleImage( bool toggle );
177 wxString labelStr = wxStripMenuCodes(m_label);
178 wxCFStringRef l(labelStr, GetToolBarFontEncoding());
179 wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() );
180 #if wxOSX_USE_NATIVE_TOOLBAR
183 // strip mnemonics from the label for compatibility with the usual
184 // labels in wxStaticText sense
186 [m_toolbarItem setLabel:l.AsNSString()];
188 [m_toolbarItem setToolTip:sh.AsNSString()];
192 [(NSButton*)m_controlHandle setTitle:l.AsNSString()];
194 if ( m_controlHandle )
196 [m_controlHandle setToolTip:sh.AsNSString()];
202 wxToolBar *tbar = (wxToolBar*) GetToolBar();
207 shouldToggle = !IsToggled();
208 tbar->ToggleTool( GetId(), shouldToggle );
211 tbar->OnLeftClick( GetId(), IsToggled() );
214 #if wxOSX_USE_NATIVE_TOOLBAR
215 void SetToolbarItemRef( NSToolbarItem* ref )
217 if ( m_controlHandle )
218 [m_controlHandle setHidden:YES];
220 [m_toolbarItem release];
225 NSToolbarItem* GetToolbarItemRef() const
227 return m_toolbarItem;
230 void SetIndex( CFIndex idx )
235 CFIndex GetIndex() const
240 virtual void SetLabel(const wxString& label)
242 wxToolBarToolBase::SetLabel(label);
246 virtual bool SetShortHelp(const wxString& help)
248 if ( !wxToolBarToolBase::SetShortHelp(help) )
255 #endif // wxOSX_USE_NATIVE_TOOLBAR
258 #if wxOSX_USE_NATIVE_TOOLBAR
259 wxFontEncoding GetToolBarFontEncoding() const
263 f = GetToolBar()->GetFont();
264 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
266 #endif // wxOSX_USE_NATIVE_TOOLBAR
270 m_controlHandle = NULL;
272 #if wxOSX_USE_NATIVE_TOOLBAR
273 m_toolbarItem = NULL;
278 WXWidget m_controlHandle;
281 wxBitmap m_alternateBitmap;
283 #if wxOSX_USE_NATIVE_TOOLBAR
284 NSToolbarItem* m_toolbarItem;
285 // position in its toolbar, -1 means not inserted
290 #if wxOSX_USE_NATIVE_TOOLBAR
292 @interface wxNSToolbarItem : NSToolbarItem
297 - (id) initWithItemIdentifier: (NSString*) identifier;
298 - (void)setImplementation: (wxToolBarTool *) theImplementation;
299 - (wxToolBarTool*) implementation;
300 - (void) clickedAction: (id) sender;
301 - (BOOL) validateToolbarItem:(NSToolbarItem *)theItem;
306 @interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER(<NSToolbarDelegate>)
310 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
312 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
314 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
316 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
324 #if wxOSX_USE_NATIVE_TOOLBAR
326 @implementation wxNSToolbarItem
328 - (id)initWithItemIdentifier: (NSString*) identifier
330 self = [super initWithItemIdentifier:identifier];
332 [self setTarget: self];
333 [self setAction: @selector(clickedAction:)];
337 - (void) clickedAction: (id) sender
346 - (void)setImplementation: (wxToolBarTool *) theImplementation
348 impl = theImplementation;
351 - (wxToolBarTool*) implementation
356 - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
358 wxUnusedVar(theItem);
359 return impl->IsEnabled() ? YES:NO;
364 @implementation wxNSToolbarDelegate
366 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
368 wxUnusedVar(toolbar);
372 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
374 wxUnusedVar(toolbar);
378 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
380 wxUnusedVar(toolbar);
384 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
386 wxUnusedVar(toolbar);
388 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue];
390 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
394 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
395 if ( flag && tool->IsControl() )
397 NSView* view = tool->GetControl()->GetHandle();
398 [view removeFromSuperview];
400 wxSize sz = tool->GetControl()->GetSize();
401 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
402 [item setMaxSize:size];
403 [item setMinSize:size];
415 @implementation wxNSToolBarButton
417 - (id)initWithFrame:(NSRect)frame
419 self = [super initWithFrame:frame];
421 [self setTarget: self];
422 [self setAction: @selector(clickedAction:)];
426 - (void) clickedAction: (id) sender
435 - (void)setImplementation: (wxToolBarTool *) theImplementation
437 impl = theImplementation;
440 - (wxToolBarTool*) implementation
452 bool wxToolBarTool::Enable( bool enable )
454 if ( wxToolBarToolBase::Enable( enable ) == false )
459 GetControl()->Enable( enable );
461 else if ( IsButton() )
463 #if wxOSX_USE_NATIVE_TOOLBAR
464 if ( m_toolbarItem != NULL )
465 [m_toolbarItem setEnabled:enable];
468 if ( m_controlHandle != NULL )
469 [(NSControl*)m_controlHandle setEnabled:enable];
475 void wxToolBarTool::SetPosition( const wxPoint& position )
480 int mac_x = position.x;
481 int mac_y = position.y;
485 NSRect frame = [m_controlHandle frame];
486 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
488 frame.origin.x = mac_x;
489 frame.origin.y = mac_y;
490 [m_controlHandle setFrame:frame];
493 else if ( IsControl() )
495 // embedded native controls are moved by the OS
496 #if wxOSX_USE_NATIVE_TOOLBAR
497 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
500 GetControl()->Move( position );
505 NSRect frame = [m_controlHandle frame];
506 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
508 frame.origin.x = mac_x;
509 frame.origin.y = mac_y;
510 [m_controlHandle setFrame:frame];
515 void wxToolBarTool::UpdateImages()
517 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
519 if ( CanBeToggled() )
521 int w = m_bmpNormal.GetWidth();
522 int h = m_bmpNormal.GetHeight();
523 m_alternateBitmap = wxBitmap( w, h );
526 dc.SelectObject( m_alternateBitmap );
527 dc.SetPen( wxPen(*wxBLACK) );
528 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
529 dc.DrawRoundedRectangle( 0, 0, w, h, 2 );
530 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
531 dc.SelectObject( wxNullBitmap );
533 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
535 UpdateToggleImage( CanBeToggled() && IsToggled() );
538 void wxToolBarTool::UpdateToggleImage( bool toggle )
540 #if wxOSX_USE_NATIVE_TOOLBAR
541 if (m_toolbarItem != NULL )
543 // the native toolbar item only has a 'selected' state (one for one toolbar)
544 // so we emulate the toggle here
545 if ( CanBeToggled() && toggle )
546 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
548 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
554 [(NSButton*)m_controlHandle setState:(toggle ? NSOnState : NSOffState)];
558 wxToolBarTool::wxToolBarTool(
561 const wxString& label,
562 const wxBitmap& bmpNormal,
563 const wxBitmap& bmpDisabled,
565 wxObject *clientData,
566 const wxString& shortHelp,
567 const wxString& longHelp )
570 tbar, id, label, bmpNormal, bmpDisabled, kind,
571 clientData, shortHelp, longHelp )
577 #pragma mark Toolbar Implementation
579 wxToolBarToolBase *wxToolBar::CreateTool(
581 const wxString& label,
582 const wxBitmap& bmpNormal,
583 const wxBitmap& bmpDisabled,
585 wxObject *clientData,
586 const wxString& shortHelp,
587 const wxString& longHelp )
589 return new wxToolBarTool(
590 this, id, label, bmpNormal, bmpDisabled, kind,
591 clientData, shortHelp, longHelp );
595 wxToolBar::CreateTool(wxControl *control, const wxString& label)
597 return new wxToolBarTool(this, control, label);
600 void wxToolBar::Init()
604 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
605 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
607 #if wxOSX_USE_NATIVE_TOOLBAR
609 m_macUsesNativeToolbar = false;
613 // also for the toolbar we have the dual implementation:
614 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
616 bool wxToolBar::Create(
622 const wxString& name )
624 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
629 OSStatus err = noErr;
631 #if wxOSX_USE_NATIVE_TOOLBAR
633 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
635 static wxNSToolbarDelegate* controller = nil;
637 if ( controller == nil )
638 controller = [[wxNSToolbarDelegate alloc] init];
639 wxString identifier = wxString::Format( wxT("%p"), this );
640 wxCFStringRef cfidentifier(identifier);
641 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
645 if (m_macToolbar != NULL)
647 [tb setDelegate:controller];
649 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
650 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
652 if ( style & wxTB_NOICONS )
653 mode = NSToolbarDisplayModeLabelOnly;
654 else if ( style & wxTB_TEXT )
655 mode = NSToolbarDisplayModeIconAndLabel;
657 mode = NSToolbarDisplayModeIconOnly;
659 [tb setDisplayMode:mode];
660 [tb setSizeMode:displaySize];
663 #endif // wxOSX_USE_NATIVE_TOOLBAR
665 return (err == noErr);
668 wxToolBar::~wxToolBar()
670 // removal only works while the toolbar is there
671 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
672 if ( frame && frame->GetToolBar() == this )
674 frame->SetToolBar(NULL);
677 [(NSToolbar*)m_macToolbar setDelegate:nil];
678 [(NSToolbar*)m_macToolbar release];
682 bool wxToolBar::Show( bool show )
684 WXWindow tlw = MacGetTopLevelWindowRef();
685 bool bResult = (tlw != NULL);
689 #if wxOSX_USE_NATIVE_TOOLBAR
690 bool ownToolbarInstalled = false;
691 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
692 if (ownToolbarInstalled)
694 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
696 [(NSToolbar*)m_macToolbar setVisible:show];
699 bResult = wxToolBarBase::Show( show );
702 bResult = wxToolBarBase::Show( show );
709 bool wxToolBar::IsShown() const
713 #if wxOSX_USE_NATIVE_TOOLBAR
714 bool ownToolbarInstalled;
716 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
717 if (ownToolbarInstalled)
719 bResult = [(NSToolbar*)m_macToolbar isVisible];
722 bResult = wxToolBarBase::IsShown();
725 bResult = wxToolBarBase::IsShown();
731 void wxToolBar::DoGetSize( int *width, int *height ) const
733 #if wxOSX_USE_NATIVE_TOOLBAR
734 bool ownToolbarInstalled;
736 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
737 if ( ownToolbarInstalled )
739 WXWindow tlw = MacGetTopLevelWindowRef();
740 float toolbarHeight = 0.0;
741 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
743 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
745 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
746 styleMask:[tlw styleMask]];
747 toolbarHeight = NSHeight(windowFrame)
748 - NSHeight([[tlw contentView] frame]);
752 *width = (int)windowFrame.size.width;
753 if ( height != NULL )
754 *height = (int)toolbarHeight;
757 wxToolBarBase::DoGetSize( width, height );
760 wxToolBarBase::DoGetSize( width, height );
764 wxSize wxToolBar::DoGetBestSize() const
766 // was updated in Realize()
768 wxSize size = GetMinSize();
773 void wxToolBar::SetWindowStyleFlag( long style )
775 wxToolBarBase::SetWindowStyleFlag( style );
777 #if wxOSX_USE_NATIVE_TOOLBAR
778 if (m_macToolbar != NULL)
780 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
782 if ( style & wxTB_NOICONS )
783 mode = NSToolbarDisplayModeLabelOnly;
784 else if ( style & wxTB_TEXT )
785 mode = NSToolbarDisplayModeIconAndLabel;
787 mode = NSToolbarDisplayModeIconOnly;
789 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
794 #if wxOSX_USE_NATIVE_TOOLBAR
795 bool wxToolBar::MacWantsNativeToolbar()
797 return m_macUsesNativeToolbar;
800 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
802 bool bResultV = false;
804 if (ownToolbarInstalled != NULL)
805 *ownToolbarInstalled = false;
807 WXWindow tlw = MacGetTopLevelWindowRef();
810 NSToolbar* curToolbarRef = [tlw toolbar];
811 bResultV = (curToolbarRef != NULL);
812 if (bResultV && (ownToolbarInstalled != NULL))
813 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
819 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
821 bool bResult = false;
823 if (usesNative && (m_macToolbar == NULL))
826 if (usesNative && HasFlag(wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM) )
829 WXWindow tlw = MacGetTopLevelWindowRef();
833 // check the existing toolbar
834 NSToolbar* curToolbarRef = [tlw toolbar];
836 m_macUsesNativeToolbar = usesNative;
838 if (m_macUsesNativeToolbar)
840 // only install toolbar if there isn't one installed already
841 if (curToolbarRef == NULL)
844 [tlw setToolbar:(NSToolbar*) m_macToolbar];
845 [(NSToolbar*) m_macToolbar setVisible:YES];
847 GetPeer()->Move(0,0,0,0 );
848 SetSize( wxSIZE_AUTO_WIDTH, 0 );
849 GetPeer()->SetVisibility( false );
850 wxToolBarBase::Show( false );
855 // only deinstall toolbar if this is the installed one
856 if (m_macToolbar == curToolbarRef)
859 [(NSToolbar*) m_macToolbar setVisible:NO];
860 MacUninstallNativeToolbar();
861 GetPeer()->SetVisibility( true );
866 InvalidateBestSize();
868 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
872 void wxToolBar::MacUninstallNativeToolbar()
877 WXWindow tlw = MacGetTopLevelWindowRef();
879 [tlw setToolbar:nil];
883 void wxToolBar::DoLayout()
885 int maxToolWidth = 0;
886 int maxToolHeight = 0;
891 // find the maximum tool width and height
892 // and the number of stretchable items
893 int numStretchableSpaces = 0;
895 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
898 tool = (wxToolBarTool *) node->GetData();
901 wxSize sz = tool->GetSize();
903 if ( sz.x > maxToolWidth )
905 if ( sz.y > maxToolHeight )
906 maxToolHeight = sz.y;
907 if ( tool->IsStretchableSpace() )
908 numStretchableSpaces++;
911 node = node->GetNext();
914 // layout non-native toolbar
916 bool isHorizontal = !IsVertical();
921 int x = m_xMargin + kwxMacToolBarLeftMargin;
922 int y = m_yMargin + kwxMacToolBarTopMargin;
924 node = m_tools.GetFirst();
927 tool = (wxToolBarTool*) node->GetData();
930 node = node->GetNext();
934 // set tool position:
935 // for the moment just perform a single row/column alignment
936 wxSize cursize = tool->GetSize();
937 if ( x + cursize.x > maxWidth )
938 maxWidth = x + cursize.x;
939 if ( y + cursize.y > maxHeight )
940 maxHeight = y + cursize.y;
942 // update the item positioning state
944 y += cursize.y + kwxMacToolSpacing;
946 x += cursize.x + kwxMacToolSpacing;
948 node = node->GetNext();
953 // if not set yet, only one row
954 if ( m_maxRows <= 0 )
957 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
958 m_minWidth = maxWidth;
959 m_minHeight = m_maxHeight = maxToolHeight + 2 * (m_yMargin + kwxMacToolBarTopMargin);
963 // if not set yet, have one column
964 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
965 SetRows( GetToolsCount() );
967 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
968 m_minHeight = maxHeight;
969 m_minWidth = m_maxWidth = maxToolWidth + 2 * (m_yMargin + kwxMacToolBarTopMargin);
972 int totalStretchableSpace = 0;
973 int spacePerStretchable = 0;
974 if ( numStretchableSpaces > 0 )
977 totalStretchableSpace = tw - maxWidth;
979 totalStretchableSpace = th - maxHeight;
981 if ( totalStretchableSpace > 0 )
982 spacePerStretchable = totalStretchableSpace / numStretchableSpaces;
985 // perform real positioning
987 x = m_xMargin + kwxMacToolBarLeftMargin;
988 y = m_yMargin + kwxMacToolBarTopMargin;
990 node = m_tools.GetFirst();
991 int currentStretchable = 0;
994 tool = (wxToolBarTool*) node->GetData();
997 node = node->GetNext();
1001 wxSize cursize = tool->GetSize();
1002 if ( tool->IsStretchableSpace() )
1004 ++currentStretchable;
1005 int thisSpace = currentStretchable == numStretchableSpaces ?
1006 totalStretchableSpace - (currentStretchable-1)*spacePerStretchable :
1007 spacePerStretchable;
1009 cursize.x += thisSpace;
1011 cursize.y += thisSpace;
1014 if ( !isHorizontal )
1016 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1017 tool->SetPosition( wxPoint(x1, y) );
1021 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1022 tool->SetPosition( wxPoint(x, y1) );
1025 // update the item positioning state
1026 if ( !isHorizontal )
1027 y += cursize.y + kwxMacToolSpacing;
1029 x += cursize.x + kwxMacToolSpacing;
1031 node = node->GetNext();
1036 bool wxToolBar::Realize()
1038 if ( !wxToolBarBase::Realize() )
1041 wxToolBarTool *tool;
1042 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1044 #if wxOSX_USE_NATIVE_TOOLBAR
1045 CFIndex currentPosition = 0;
1046 bool insertAll = false;
1048 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
1053 enc = f.GetEncoding();
1055 enc = wxFont::GetDefaultEncoding();
1057 node = m_tools.GetFirst();
1060 tool = (wxToolBarTool*) node->GetData();
1063 node = node->GetNext();
1067 // install in native NSToolbar
1070 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
1071 if ( hiItemRef != NULL )
1073 // since setting the help texts is non-virtual we have to update
1075 wxCFStringRef sh( tool->GetShortHelp(), enc);
1076 [hiItemRef setToolTip:sh.AsNSString()];
1078 if ( insertAll || (tool->GetIndex() != currentPosition) )
1084 // if this is the first tool that gets newly inserted or repositioned
1085 // first remove all 'old' tools from here to the right, because of this
1086 // all following tools will have to be reinserted (insertAll).
1087 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
1089 node2 = node2->GetPrevious() )
1091 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
1093 const long idx = tool2->GetIndex();
1096 [refTB removeItemAtIndex:idx];
1097 tool2->SetIndex(-1);
1102 wxCFStringRef cfidentifier;
1104 if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR)
1106 if ( tool->IsStretchable() )
1107 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1110 if ( UMAGetSystemVersion() < 0x1070 )
1111 nsItemId = NSToolbarSeparatorItemIdentifier;
1113 nsItemId = NSToolbarSpaceItemIdentifier;
1118 cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool));
1119 nsItemId = cfidentifier.AsNSString();
1122 [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition];
1123 tool->SetIndex( currentPosition );
1129 node = node->GetNext();
1136 // adjust radio items
1138 bool lastIsRadio = false;
1139 bool curIsRadio = false;
1141 node = m_tools.GetFirst();
1144 tool = (wxToolBarTool*) node->GetData();
1147 node = node->GetNext();
1151 // update radio button (and group) state
1152 lastIsRadio = curIsRadio;
1153 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1157 if ( tool->IsToggled() )
1158 DoToggleTool( tool, true );
1164 if ( tool->Toggle( true ) )
1166 DoToggleTool( tool, true );
1169 else if ( tool->IsToggled() )
1171 if ( tool->IsToggled() )
1172 DoToggleTool( tool, true );
1174 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1177 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1178 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1181 if ( toggleTool->Toggle( false ) )
1182 DoToggleTool( toggleTool, false );
1184 nodePrev = nodePrev->GetPrevious();
1189 node = node->GetNext();
1192 InvalidateBestSize();
1193 SetInitialSize( wxSize(m_minWidth, m_minHeight));
1195 SendSizeEventToParent();
1200 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1202 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
1207 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1209 m_defaultWidth = size.x + kwxMacToolBorder;
1210 m_defaultHeight = size.y + kwxMacToolBorder;
1212 #if wxOSX_USE_NATIVE_TOOLBAR
1213 if (m_macToolbar != NULL)
1215 int maxs = wxMax( size.x, size.y );
1216 NSToolbarSizeMode sizeSpec;
1218 sizeSpec = NSToolbarSizeModeRegular;
1219 else if ( maxs > 24 )
1220 sizeSpec = NSToolbarSizeModeDefault;
1222 sizeSpec = NSToolbarSizeModeSmall;
1224 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1229 // The button size is bigger than the bitmap size
1230 wxSize wxToolBar::GetToolSize() const
1232 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1235 void wxToolBar::SetRows(int nRows)
1237 // avoid resizing the frame uselessly
1238 if ( nRows != m_maxRows )
1242 void wxToolBar::MacSuperChangedPosition()
1244 wxWindow::MacSuperChangedPosition();
1247 #if wxOSX_USE_NATIVE_TOOLBAR
1248 if (! m_macUsesNativeToolbar )
1257 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1259 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1262 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1264 tool->SetNormalBitmap(bitmap);
1266 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1267 tool->UpdateImages();
1271 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1273 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1276 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1278 tool->SetDisabledBitmap(bitmap);
1280 // TODO: what to do for this one?
1284 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1286 wxToolBarTool *tool;
1287 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1290 tool = (wxToolBarTool *)node->GetData();
1293 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1294 if ( r.Contains( wxPoint( x, y ) ) )
1298 node = node->GetNext();
1304 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1306 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1308 return tool->GetShortHelp();
1310 return wxEmptyString;
1313 void wxToolBar::DoEnableTool(wxToolBarToolBase * WXUNUSED(t), bool WXUNUSED(enable))
1315 // everything already done in the tool's Enable implementation
1318 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1320 wxToolBarTool *tool = (wxToolBarTool *)t;
1321 if ( ( tool != NULL ) && tool->IsButton() )
1322 tool->UpdateToggleImage( toggle );
1325 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1327 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1331 long style = GetWindowStyleFlag();
1333 wxSize toolSize = GetToolSize();
1334 WXWidget controlHandle = NULL;
1335 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1337 #if wxOSX_USE_NATIVE_TOOLBAR
1338 wxString label = tool->GetLabel();
1339 if (m_macToolbar && !label.empty() )
1341 // strip mnemonics from the label for compatibility
1342 // with the usual labels in wxStaticText sense
1343 label = wxStripMenuCodes(label);
1345 #endif // wxOSX_USE_NATIVE_TOOLBAR
1347 switch (tool->GetStyle())
1349 case wxTOOL_STYLE_SEPARATOR:
1351 wxASSERT( tool->GetControlHandle() == NULL );
1355 toolrect.size.height = toolSize.y;
1357 toolrect.size.width = toolSize.x;
1359 // in flat style we need a visual separator
1360 #if wxOSX_USE_NATIVE_TOOLBAR
1361 if (m_macToolbar != NULL)
1363 NSString * nsItemId = nil;
1365 if ( tool->IsStretchable() )
1366 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1369 if ( UMAGetSystemVersion() < 0x1070 )
1370 nsItemId = NSToolbarSeparatorItemIdentifier;
1372 nsItemId = NSToolbarSpaceItemIdentifier;
1375 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId];
1376 tool->SetToolbarItemRef( item );
1378 #endif // wxOSX_USE_NATIVE_TOOLBAR
1380 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1381 [box setBoxType:NSBoxSeparator];
1382 controlHandle = box;
1383 tool->SetControlHandle( controlHandle );
1387 case wxTOOL_STYLE_BUTTON:
1389 wxASSERT( tool->GetControlHandle() == NULL );
1391 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1393 [v setBezelStyle:NSRegularSquareBezelStyle];
1395 [v setButtonType: ( tool->CanBeToggled() ? NSToggleButton : NSMomentaryPushInButton )];
1396 [v setImplementation:tool];
1400 #if wxOSX_USE_NATIVE_TOOLBAR
1401 if (m_macToolbar != NULL)
1403 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1404 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1405 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1406 [item setImplementation:tool];
1407 tool->SetToolbarItemRef( item );
1410 #endif // wxOSX_USE_NATIVE_TOOLBAR
1411 tool->SetControlHandle( controlHandle );
1412 tool->UpdateImages();
1413 tool->UpdateLabel();
1415 if ( style & wxTB_NOICONS )
1416 [v setImagePosition:NSNoImage];
1417 else if ( style & wxTB_TEXT )
1418 [v setImagePosition:NSImageAbove];
1420 [v setImagePosition:NSImageOnly];
1424 InstallControlEventHandler(
1425 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1426 GetEventTypeCount(eventList), eventList, tool, NULL );
1431 case wxTOOL_STYLE_CONTROL:
1433 #if wxOSX_USE_NATIVE_TOOLBAR
1434 if (m_macToolbar != NULL)
1436 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1437 wxCHECK_MSG( view, false, wxT("control must be non-NULL") );
1439 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1440 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1441 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1442 [item setImplementation:tool];
1443 tool->SetToolbarItemRef( item );
1446 // right now there's nothing to do here
1448 tool->UpdateLabel();
1455 if ( controlHandle )
1457 WXWidget container = (WXWidget) GetHandle();
1458 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1460 // SetControlVisibility( controlHandle, true, true );
1461 [container addSubview:controlHandle];
1464 // nothing special to do here - we relayout in Realize() later
1465 InvalidateBestSize();
1471 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1473 wxFAIL_MSG( wxT("not implemented") );
1476 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1478 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1479 wxToolBarToolsList::compatibility_iterator node;
1480 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1482 wxToolBarToolBase *tool2 = node->GetData();
1483 if ( tool2 == tool )
1485 // let node point to the next node in the list
1486 node = node->GetNext();
1492 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1494 #if wxOSX_USE_NATIVE_TOOLBAR
1495 CFIndex removeIndex = tool->GetIndex();
1498 #if wxOSX_USE_NATIVE_TOOLBAR
1499 if (m_macToolbar != NULL)
1501 if ( removeIndex != -1 && m_macToolbar )
1503 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1504 tool->SetIndex( -1 );
1509 tool->ClearControl();
1511 // and finally reposition all the controls after this one
1513 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1515 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1516 wxPoint pt = tool2->GetPosition();
1523 tool2->SetPosition( pt );
1525 #if wxOSX_USE_NATIVE_TOOLBAR
1526 if (m_macToolbar != NULL)
1528 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1529 tool2->SetIndex( tool2->GetIndex() - 1 );
1534 InvalidateBestSize();
1539 #include <Carbon/Carbon.h>
1541 void wxToolBar::OnPaint(wxPaintEvent& event)
1543 #if wxOSX_USE_NATIVE_TOOLBAR
1544 if ( m_macUsesNativeToolbar )
1546 // nothing to do here
1554 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1556 if ( UMAGetSystemVersion() < 0x1050 )
1558 if ( !drawMetalTheme )
1560 HIThemePlacardDrawInfo info;
1561 memset( &info, 0, sizeof(info) );
1563 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1565 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1566 HIRect rect = CGRectMake( 0, 0, w, h );
1567 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1571 // leave the background as it is (striped or metal)
1578 wxRect rect(0,0,w,h);
1580 dc.GradientFillLinear( rect , wxColour( 0xCC,0xCC,0xCC ), wxColour( 0xA8,0xA8,0xA8 ) , wxSOUTH );
1581 dc.SetPen( wxPen( wxColour( 0x51,0x51,0x51 ) ) );
1582 if ( HasFlag(wxTB_LEFT) )
1583 dc.DrawLine(w-1, 0, w-1, h);
1584 else if ( HasFlag(wxTB_RIGHT) )
1585 dc.DrawLine(0, 0, 0, h);
1586 else if ( HasFlag(wxTB_BOTTOM) )
1587 dc.DrawLine(0, 0, w, 0);
1588 else if ( HasFlag(wxTB_TOP) )
1589 dc.DrawLine(0, h-1, w, h-1);
1595 #endif // wxUSE_TOOLBAR