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 void wxToolBar::DoGetPosition(int*x, int *y) const
766 #if wxOSX_USE_NATIVE_TOOLBAR
767 bool ownToolbarInstalled;
769 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
770 if ( ownToolbarInstalled )
772 WXWindow tlw = MacGetTopLevelWindowRef();
773 float toolbarHeight = 0.0;
774 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
776 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
778 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
779 styleMask:[tlw styleMask]];
780 toolbarHeight = NSHeight(windowFrame)
781 - NSHeight([[tlw contentView] frame]);
784 // it is extending to the north of the content area
792 wxToolBarBase::DoGetPosition( x, y );
795 wxToolBarBase::DoGetPosition( x, y );
799 wxSize wxToolBar::DoGetBestSize() const
801 // was updated in Realize()
803 wxSize size = GetMinSize();
808 void wxToolBar::SetWindowStyleFlag( long style )
810 wxToolBarBase::SetWindowStyleFlag( style );
812 #if wxOSX_USE_NATIVE_TOOLBAR
813 if (m_macToolbar != NULL)
815 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
817 if ( style & wxTB_NOICONS )
818 mode = NSToolbarDisplayModeLabelOnly;
819 else if ( style & wxTB_TEXT )
820 mode = NSToolbarDisplayModeIconAndLabel;
822 mode = NSToolbarDisplayModeIconOnly;
824 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
829 #if wxOSX_USE_NATIVE_TOOLBAR
830 bool wxToolBar::MacWantsNativeToolbar()
832 return m_macUsesNativeToolbar;
835 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
837 bool bResultV = false;
839 if (ownToolbarInstalled != NULL)
840 *ownToolbarInstalled = false;
842 WXWindow tlw = MacGetTopLevelWindowRef();
845 NSToolbar* curToolbarRef = [tlw toolbar];
846 bResultV = (curToolbarRef != NULL);
847 if (bResultV && (ownToolbarInstalled != NULL))
848 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
854 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
856 bool bResult = false;
858 if (usesNative && (m_macToolbar == NULL))
861 if (usesNative && HasFlag(wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM) )
864 WXWindow tlw = MacGetTopLevelWindowRef();
868 // check the existing toolbar
869 NSToolbar* curToolbarRef = [tlw toolbar];
871 m_macUsesNativeToolbar = usesNative;
873 if (m_macUsesNativeToolbar)
875 // only install toolbar if there isn't one installed already
876 if (curToolbarRef == NULL)
879 [tlw setToolbar:(NSToolbar*) m_macToolbar];
880 [(NSToolbar*) m_macToolbar setVisible:YES];
882 GetPeer()->Move(0,0,0,0 );
883 SetSize( wxSIZE_AUTO_WIDTH, 0 );
884 GetPeer()->SetVisibility( false );
885 wxToolBarBase::Show( false );
890 // only deinstall toolbar if this is the installed one
891 if (m_macToolbar == curToolbarRef)
894 [(NSToolbar*) m_macToolbar setVisible:NO];
895 MacUninstallNativeToolbar();
896 GetPeer()->SetVisibility( true );
901 InvalidateBestSize();
903 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
907 void wxToolBar::MacUninstallNativeToolbar()
912 WXWindow tlw = MacGetTopLevelWindowRef();
914 [tlw setToolbar:nil];
918 void wxToolBar::DoLayout()
920 int maxToolWidth = 0;
921 int maxToolHeight = 0;
926 // find the maximum tool width and height
927 // and the number of stretchable items
928 int numStretchableSpaces = 0;
930 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
933 tool = (wxToolBarTool *) node->GetData();
936 wxSize sz = tool->GetSize();
938 if ( sz.x > maxToolWidth )
940 if ( sz.y > maxToolHeight )
941 maxToolHeight = sz.y;
942 if ( tool->IsStretchableSpace() )
943 numStretchableSpaces++;
946 node = node->GetNext();
949 // layout non-native toolbar
951 bool isHorizontal = !IsVertical();
956 int x = m_xMargin + kwxMacToolBarLeftMargin;
957 int y = m_yMargin + kwxMacToolBarTopMargin;
959 node = m_tools.GetFirst();
962 tool = (wxToolBarTool*) node->GetData();
965 node = node->GetNext();
969 // set tool position:
970 // for the moment just perform a single row/column alignment
971 wxSize cursize = tool->GetSize();
972 if ( x + cursize.x > maxWidth )
973 maxWidth = x + cursize.x;
974 if ( y + cursize.y > maxHeight )
975 maxHeight = y + cursize.y;
977 // update the item positioning state
979 y += cursize.y + kwxMacToolSpacing;
981 x += cursize.x + kwxMacToolSpacing;
983 node = node->GetNext();
988 // if not set yet, only one row
989 if ( m_maxRows <= 0 )
992 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
993 m_minWidth = maxWidth;
994 m_minHeight = m_maxHeight = maxToolHeight + 2 * (m_yMargin + kwxMacToolBarTopMargin);
998 // if not set yet, have one column
999 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1000 SetRows( GetToolsCount() );
1002 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1003 m_minHeight = maxHeight;
1004 m_minWidth = m_maxWidth = maxToolWidth + 2 * (m_yMargin + kwxMacToolBarTopMargin);
1007 int totalStretchableSpace = 0;
1008 int spacePerStretchable = 0;
1009 if ( numStretchableSpaces > 0 )
1012 totalStretchableSpace = tw - maxWidth;
1014 totalStretchableSpace = th - maxHeight;
1016 if ( totalStretchableSpace > 0 )
1017 spacePerStretchable = totalStretchableSpace / numStretchableSpaces;
1020 // perform real positioning
1022 x = m_xMargin + kwxMacToolBarLeftMargin;
1023 y = m_yMargin + kwxMacToolBarTopMargin;
1025 node = m_tools.GetFirst();
1026 int currentStretchable = 0;
1029 tool = (wxToolBarTool*) node->GetData();
1032 node = node->GetNext();
1036 wxSize cursize = tool->GetSize();
1037 if ( tool->IsStretchableSpace() )
1039 ++currentStretchable;
1040 int thisSpace = currentStretchable == numStretchableSpaces ?
1041 totalStretchableSpace - (currentStretchable-1)*spacePerStretchable :
1042 spacePerStretchable;
1044 cursize.x += thisSpace;
1046 cursize.y += thisSpace;
1049 if ( !isHorizontal )
1051 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1052 tool->SetPosition( wxPoint(x1, y) );
1056 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1057 tool->SetPosition( wxPoint(x, y1) );
1060 // update the item positioning state
1061 if ( !isHorizontal )
1062 y += cursize.y + kwxMacToolSpacing;
1064 x += cursize.x + kwxMacToolSpacing;
1066 node = node->GetNext();
1071 bool wxToolBar::Realize()
1073 if ( !wxToolBarBase::Realize() )
1076 wxToolBarTool *tool;
1077 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1079 #if wxOSX_USE_NATIVE_TOOLBAR
1080 CFIndex currentPosition = 0;
1081 bool insertAll = false;
1083 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
1088 enc = f.GetEncoding();
1090 enc = wxFont::GetDefaultEncoding();
1092 node = m_tools.GetFirst();
1095 tool = (wxToolBarTool*) node->GetData();
1098 node = node->GetNext();
1102 // install in native NSToolbar
1105 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
1106 if ( hiItemRef != NULL )
1108 // since setting the help texts is non-virtual we have to update
1110 wxCFStringRef sh( tool->GetShortHelp(), enc);
1111 [hiItemRef setToolTip:sh.AsNSString()];
1113 if ( insertAll || (tool->GetIndex() != currentPosition) )
1119 // if this is the first tool that gets newly inserted or repositioned
1120 // first remove all 'old' tools from here to the right, because of this
1121 // all following tools will have to be reinserted (insertAll).
1122 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
1124 node2 = node2->GetPrevious() )
1126 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
1128 const long idx = tool2->GetIndex();
1131 [refTB removeItemAtIndex:idx];
1132 tool2->SetIndex(-1);
1137 wxCFStringRef cfidentifier;
1139 if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR)
1141 if ( tool->IsStretchable() )
1142 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1145 if ( UMAGetSystemVersion() < 0x1070 )
1146 nsItemId = NSToolbarSeparatorItemIdentifier;
1148 nsItemId = NSToolbarSpaceItemIdentifier;
1153 cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool));
1154 nsItemId = cfidentifier.AsNSString();
1157 [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition];
1158 tool->SetIndex( currentPosition );
1164 node = node->GetNext();
1171 // adjust radio items
1173 bool lastIsRadio = false;
1174 bool curIsRadio = false;
1176 node = m_tools.GetFirst();
1179 tool = (wxToolBarTool*) node->GetData();
1182 node = node->GetNext();
1186 // update radio button (and group) state
1187 lastIsRadio = curIsRadio;
1188 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1192 if ( tool->IsToggled() )
1193 DoToggleTool( tool, true );
1199 if ( tool->Toggle( true ) )
1201 DoToggleTool( tool, true );
1204 else if ( tool->IsToggled() )
1206 if ( tool->IsToggled() )
1207 DoToggleTool( tool, true );
1209 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1212 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1213 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1216 if ( toggleTool->Toggle( false ) )
1217 DoToggleTool( toggleTool, false );
1219 nodePrev = nodePrev->GetPrevious();
1224 node = node->GetNext();
1227 InvalidateBestSize();
1228 SetInitialSize( wxSize(m_minWidth, m_minHeight));
1230 SendSizeEventToParent();
1231 wxWindow * const parent = GetParent();
1232 if ( parent && !parent->IsBeingDeleted() )
1233 parent->MacOnInternalSize();
1238 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1240 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
1245 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1247 m_defaultWidth = size.x + kwxMacToolBorder;
1248 m_defaultHeight = size.y + kwxMacToolBorder;
1250 #if wxOSX_USE_NATIVE_TOOLBAR
1251 if (m_macToolbar != NULL)
1253 int maxs = wxMax( size.x, size.y );
1254 NSToolbarSizeMode sizeSpec;
1256 sizeSpec = NSToolbarSizeModeRegular;
1257 else if ( maxs > 24 )
1258 sizeSpec = NSToolbarSizeModeDefault;
1260 sizeSpec = NSToolbarSizeModeSmall;
1262 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1267 // The button size is bigger than the bitmap size
1268 wxSize wxToolBar::GetToolSize() const
1270 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1273 void wxToolBar::SetRows(int nRows)
1275 // avoid resizing the frame uselessly
1276 if ( nRows != m_maxRows )
1280 void wxToolBar::MacSuperChangedPosition()
1282 wxWindow::MacSuperChangedPosition();
1285 #if wxOSX_USE_NATIVE_TOOLBAR
1286 if (! m_macUsesNativeToolbar )
1295 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1297 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1300 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1302 tool->SetNormalBitmap(bitmap);
1304 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1305 tool->UpdateImages();
1309 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1311 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1314 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1316 tool->SetDisabledBitmap(bitmap);
1318 // TODO: what to do for this one?
1322 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1324 wxToolBarTool *tool;
1325 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1328 tool = (wxToolBarTool *)node->GetData();
1331 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1332 if ( r.Contains( wxPoint( x, y ) ) )
1336 node = node->GetNext();
1342 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1344 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1346 return tool->GetShortHelp();
1348 return wxEmptyString;
1351 void wxToolBar::DoEnableTool(wxToolBarToolBase * WXUNUSED(t), bool WXUNUSED(enable))
1353 // everything already done in the tool's Enable implementation
1356 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1358 wxToolBarTool *tool = (wxToolBarTool *)t;
1359 if ( ( tool != NULL ) && tool->IsButton() )
1360 tool->UpdateToggleImage( toggle );
1363 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1365 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1369 long style = GetWindowStyleFlag();
1371 wxSize toolSize = GetToolSize();
1372 WXWidget controlHandle = NULL;
1373 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1375 #if wxOSX_USE_NATIVE_TOOLBAR
1376 wxString label = tool->GetLabel();
1377 if (m_macToolbar && !label.empty() )
1379 // strip mnemonics from the label for compatibility
1380 // with the usual labels in wxStaticText sense
1381 label = wxStripMenuCodes(label);
1383 #endif // wxOSX_USE_NATIVE_TOOLBAR
1385 switch (tool->GetStyle())
1387 case wxTOOL_STYLE_SEPARATOR:
1389 wxASSERT( tool->GetControlHandle() == NULL );
1393 toolrect.size.height = toolSize.y;
1395 toolrect.size.width = toolSize.x;
1397 // in flat style we need a visual separator
1398 #if wxOSX_USE_NATIVE_TOOLBAR
1399 if (m_macToolbar != NULL)
1401 NSString * nsItemId = nil;
1403 if ( tool->IsStretchable() )
1404 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1407 if ( UMAGetSystemVersion() < 0x1070 )
1408 nsItemId = NSToolbarSeparatorItemIdentifier;
1410 nsItemId = NSToolbarSpaceItemIdentifier;
1413 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId];
1414 tool->SetToolbarItemRef( item );
1416 #endif // wxOSX_USE_NATIVE_TOOLBAR
1418 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1419 [box setBoxType:NSBoxSeparator];
1420 controlHandle = box;
1421 tool->SetControlHandle( controlHandle );
1425 case wxTOOL_STYLE_BUTTON:
1427 wxASSERT( tool->GetControlHandle() == NULL );
1429 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1431 [v setBezelStyle:NSRegularSquareBezelStyle];
1433 [v setButtonType: ( tool->CanBeToggled() ? NSToggleButton : NSMomentaryPushInButton )];
1434 [v setImplementation:tool];
1438 #if wxOSX_USE_NATIVE_TOOLBAR
1439 if (m_macToolbar != NULL)
1441 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1442 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1443 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1444 [item setImplementation:tool];
1445 tool->SetToolbarItemRef( item );
1448 #endif // wxOSX_USE_NATIVE_TOOLBAR
1449 tool->SetControlHandle( controlHandle );
1450 tool->UpdateImages();
1451 tool->UpdateLabel();
1453 if ( style & wxTB_NOICONS )
1454 [v setImagePosition:NSNoImage];
1455 else if ( style & wxTB_TEXT )
1456 [v setImagePosition:NSImageAbove];
1458 [v setImagePosition:NSImageOnly];
1462 InstallControlEventHandler(
1463 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1464 GetEventTypeCount(eventList), eventList, tool, NULL );
1469 case wxTOOL_STYLE_CONTROL:
1471 #if wxOSX_USE_NATIVE_TOOLBAR
1472 if (m_macToolbar != NULL)
1474 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1475 wxCHECK_MSG( view, false, wxT("control must be non-NULL") );
1477 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1478 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1479 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1480 [item setImplementation:tool];
1481 tool->SetToolbarItemRef( item );
1484 // right now there's nothing to do here
1486 tool->UpdateLabel();
1493 if ( controlHandle )
1495 WXWidget container = (WXWidget) GetHandle();
1496 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1498 // SetControlVisibility( controlHandle, true, true );
1499 [container addSubview:controlHandle];
1502 // nothing special to do here - we relayout in Realize() later
1503 InvalidateBestSize();
1509 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1511 wxFAIL_MSG( wxT("not implemented") );
1514 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1516 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1517 wxToolBarToolsList::compatibility_iterator node;
1518 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1520 wxToolBarToolBase *tool2 = node->GetData();
1521 if ( tool2 == tool )
1523 // let node point to the next node in the list
1524 node = node->GetNext();
1530 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1532 #if wxOSX_USE_NATIVE_TOOLBAR
1533 CFIndex removeIndex = tool->GetIndex();
1536 #if wxOSX_USE_NATIVE_TOOLBAR
1537 if (m_macToolbar != NULL)
1539 if ( removeIndex != -1 && m_macToolbar )
1541 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1542 tool->SetIndex( -1 );
1547 tool->ClearControl();
1549 // and finally reposition all the controls after this one
1551 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1553 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1554 wxPoint pt = tool2->GetPosition();
1561 tool2->SetPosition( pt );
1563 #if wxOSX_USE_NATIVE_TOOLBAR
1564 if (m_macToolbar != NULL)
1566 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1567 tool2->SetIndex( tool2->GetIndex() - 1 );
1572 InvalidateBestSize();
1577 #include <Carbon/Carbon.h>
1579 void wxToolBar::OnPaint(wxPaintEvent& event)
1581 #if wxOSX_USE_NATIVE_TOOLBAR
1582 if ( m_macUsesNativeToolbar )
1584 // nothing to do here
1592 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1594 if ( UMAGetSystemVersion() < 0x1050 )
1596 if ( !drawMetalTheme )
1598 HIThemePlacardDrawInfo info;
1599 memset( &info, 0, sizeof(info) );
1601 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1603 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1604 HIRect rect = CGRectMake( 0, 0, w, h );
1605 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1609 // leave the background as it is (striped or metal)
1616 wxRect rect(0,0,w,h);
1618 dc.GradientFillLinear( rect , wxColour( 0xCC,0xCC,0xCC ), wxColour( 0xA8,0xA8,0xA8 ) , wxSOUTH );
1619 dc.SetPen( wxPen( wxColour( 0x51,0x51,0x51 ) ) );
1620 if ( HasFlag(wxTB_LEFT) )
1621 dc.DrawLine(w-1, 0, w-1, h);
1622 else if ( HasFlag(wxTB_RIGHT) )
1623 dc.DrawLine(0, 0, 0, h);
1624 else if ( HasFlag(wxTB_BOTTOM) )
1625 dc.DrawLine(0, 0, w, 0);
1626 else if ( HasFlag(wxTB_TOP) )
1627 dc.DrawLine(0, h-1, w, h-1);
1633 #endif // wxUSE_TOOLBAR