]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/toolbar.mm
Add support for Cocoa's selectable toolbar items.
[wxWidgets.git] / src / osx / cocoa / toolbar.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/toolbar.mm
3 // Purpose: wxToolBar
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_TOOLBAR
15
16 #ifndef WX_PRECOMP
17 #include "wx/wx.h"
18 #endif
19
20 #include "wx/toolbar.h"
21 #include "wx/app.h"
22 #include "wx/osx/private.h"
23 #include "wx/geometry.h"
24 #include "wx/sysopt.h"
25
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;
32
33 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
34 EVT_PAINT( wxToolBar::OnPaint )
35 END_EVENT_TABLE()
36
37
38 #pragma mark -
39 #pragma mark Tool Implementation
40
41 // ----------------------------------------------------------------------------
42 // private classes
43 // ----------------------------------------------------------------------------
44
45 class wxToolBarTool;
46
47 @interface wxNSToolBarButton : NSButton
48 {
49 wxToolBarTool* impl;
50 }
51
52 - (id)initWithFrame:(NSRect)frame;
53 - (void) clickedAction: (id) sender;
54 - (void)setImplementation: (wxToolBarTool *) theImplementation;
55 - (wxToolBarTool*) implementation;
56 - (BOOL) isFlipped;
57
58 @end
59
60 // We have a dual implementation for each tool, WXWidget and NSToolbarItem*
61
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.
72
73 class wxToolBarTool : public wxToolBarToolBase
74 {
75 public:
76 wxToolBarTool(
77 wxToolBar *tbar,
78 int id,
79 const wxString& label,
80 const wxBitmap& bmpNormal,
81 const wxBitmap& bmpDisabled,
82 wxItemKind kind,
83 wxObject *clientData,
84 const wxString& shortHelp,
85 const wxString& longHelp );
86
87 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
88 : wxToolBarToolBase(tbar, control, label)
89 {
90 Init();
91 if (control != NULL)
92 SetControlHandle( (WXWidget) control->GetHandle() );
93 }
94
95 virtual ~wxToolBarTool()
96 {
97 ClearControl();
98 }
99
100 WXWidget GetControlHandle()
101 {
102 return (WXWidget) m_controlHandle;
103 }
104
105 void SetControlHandle( WXWidget handle )
106 {
107 m_controlHandle = handle;
108 }
109
110 void SetPosition( const wxPoint& position );
111
112 void ClearControl()
113 {
114 if ( m_controlHandle )
115 {
116 if ( !IsControl() )
117 {
118 [m_controlHandle retain];
119 }
120 else
121 {
122 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
123 // proper wxControl destructor
124 }
125 m_controlHandle = NULL ;
126 }
127
128 #if wxOSX_USE_NATIVE_TOOLBAR
129 if ( m_toolbarItem )
130 {
131 [m_toolbarItem release];
132 m_toolbarItem = NULL;
133 }
134 #endif // wxOSX_USE_NATIVE_TOOLBAR
135 }
136
137 wxSize GetSize() const
138 {
139 wxSize curSize;
140
141 if ( IsControl() )
142 {
143 curSize = GetControl()->GetSize();
144 }
145 else if ( IsButton() )
146 {
147 // curSize = GetToolBar()->GetToolSize();
148 NSRect best = [(wxNSToolBarButton*)m_controlHandle frame];
149 curSize = wxSize(best.size.width, best.size.height);
150 }
151 else
152 {
153 // separator size
154 curSize = GetToolBar()->GetToolSize();
155 if ( GetToolBar()->IsVertical() )
156 curSize.y /= 4;
157 else
158 curSize.x /= 4;
159 }
160
161 return curSize;
162 }
163
164 wxPoint GetPosition() const
165 {
166 return wxPoint( m_x, m_y );
167 }
168
169 bool Enable( bool enable );
170
171 void UpdateImages();
172
173 void UpdateToggleImage( bool toggle );
174
175 void UpdateLabel()
176 {
177 wxString labelStr = wxStripMenuCodes(m_label);
178 wxCFStringRef l(labelStr, GetToolBarFontEncoding());
179 wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() );
180 #if wxOSX_USE_NATIVE_TOOLBAR
181 if ( m_toolbarItem )
182 {
183 // strip mnemonics from the label for compatibility with the usual
184 // labels in wxStaticText sense
185
186 [m_toolbarItem setLabel:l.AsNSString()];
187
188 [m_toolbarItem setToolTip:sh.AsNSString()];
189 }
190 #endif
191 if ( IsButton() )
192 [(NSButton*)m_controlHandle setTitle:l.AsNSString()];
193
194 if ( m_controlHandle )
195 {
196 [m_controlHandle setToolTip:sh.AsNSString()];
197 }
198 }
199
200 void Action()
201 {
202 wxToolBar *tbar = (wxToolBar*) GetToolBar();
203 if (CanBeToggled())
204 {
205 bool shouldToggle;
206
207 shouldToggle = !IsToggled();
208 tbar->ToggleTool( GetId(), shouldToggle );
209 }
210
211 tbar->OnLeftClick( GetId(), IsToggled() );
212 }
213
214 #if wxOSX_USE_NATIVE_TOOLBAR
215 void SetToolbarItemRef( NSToolbarItem* ref )
216 {
217 if ( m_controlHandle )
218 [m_controlHandle setHidden:YES];
219 if ( m_toolbarItem )
220 [m_toolbarItem release];
221
222 m_toolbarItem = ref;
223 }
224
225 NSToolbarItem* GetToolbarItemRef() const
226 {
227 return m_toolbarItem;
228 }
229
230 void SetIndex( CFIndex idx )
231 {
232 m_index = idx;
233 }
234
235 CFIndex GetIndex() const
236 {
237 return m_index;
238 }
239
240 virtual void SetLabel(const wxString& label)
241 {
242 wxToolBarToolBase::SetLabel(label);
243 UpdateLabel();
244 }
245
246 virtual bool SetShortHelp(const wxString& help)
247 {
248 if ( !wxToolBarToolBase::SetShortHelp(help) )
249 return false;
250
251 UpdateLabel();
252
253 return true;
254 }
255 #endif // wxOSX_USE_NATIVE_TOOLBAR
256
257 private:
258 #if wxOSX_USE_NATIVE_TOOLBAR
259 wxFontEncoding GetToolBarFontEncoding() const
260 {
261 wxFont f;
262 if ( GetToolBar() )
263 f = GetToolBar()->GetFont();
264 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
265 }
266 #endif // wxOSX_USE_NATIVE_TOOLBAR
267
268 void Init()
269 {
270 m_controlHandle = NULL;
271
272 #if wxOSX_USE_NATIVE_TOOLBAR
273 m_toolbarItem = NULL;
274 m_index = -1;
275 #endif
276 }
277
278 WXWidget m_controlHandle;
279 wxCoord m_x;
280 wxCoord m_y;
281 wxBitmap m_alternateBitmap;
282
283 #if wxOSX_USE_NATIVE_TOOLBAR
284 NSToolbarItem* m_toolbarItem;
285 // position in its toolbar, -1 means not inserted
286 CFIndex m_index;
287 #endif
288 };
289
290 #if wxOSX_USE_NATIVE_TOOLBAR
291
292 @interface wxNSToolbarItem : NSToolbarItem
293 {
294 wxToolBarTool* impl;
295 }
296
297 - (id) initWithItemIdentifier: (NSString*) identifier;
298 - (void)setImplementation: (wxToolBarTool *) theImplementation;
299 - (wxToolBarTool*) implementation;
300 - (void) clickedAction: (id) sender;
301 - (BOOL) validateToolbarItem:(NSToolbarItem *)theItem;
302
303 @end
304
305
306 @interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER(<NSToolbarDelegate>)
307 {
308 bool m_isSelectable;
309 }
310
311 - (void)setSelectable:(bool) value;
312
313 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
314
315 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
316
317 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
318
319 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar;
320
321
322 @end
323
324 #endif
325
326
327 #if wxOSX_USE_NATIVE_TOOLBAR
328
329 @implementation wxNSToolbarItem
330
331 - (id)initWithItemIdentifier: (NSString*) identifier
332 {
333 self = [super initWithItemIdentifier:identifier];
334 impl = NULL;
335 [self setTarget: self];
336 [self setAction: @selector(clickedAction:)];
337 return self;
338 }
339
340 - (void) clickedAction: (id) sender
341 {
342 wxUnusedVar(sender);
343 if ( impl )
344 {
345 impl->Action();
346 }
347 }
348
349 - (void)setImplementation: (wxToolBarTool *) theImplementation
350 {
351 impl = theImplementation;
352 }
353
354 - (wxToolBarTool*) implementation
355 {
356 return impl;
357 }
358
359 - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
360 {
361 wxUnusedVar(theItem);
362 return impl->IsEnabled() ? YES:NO;
363 }
364
365 @end
366
367 @implementation wxNSToolbarDelegate
368
369 - (id)init
370 {
371 m_isSelectable = false;
372 return [super init];
373 }
374
375 - (void)setSelectable:(bool) value
376 {
377 m_isSelectable = true;
378 }
379
380 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
381 {
382 wxUnusedVar(toolbar);
383 return nil;
384 }
385
386 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
387 {
388 wxUnusedVar(toolbar);
389 return nil;
390 }
391
392 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
393 {
394 if ( m_isSelectable )
395 return [[toolbar items] valueForKey:@"itemIdentifier"];
396 else
397 return nil;
398 }
399
400 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
401 {
402 wxUnusedVar(toolbar);
403 #ifdef __LP64__
404 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue];
405 #else
406 wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue];
407 #endif
408 if ( tool )
409 {
410 wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef();
411 if ( flag && tool->IsControl() )
412 {
413 NSView* view = tool->GetControl()->GetHandle();
414 [view removeFromSuperview];
415 [item setView:view];
416 wxSize sz = tool->GetControl()->GetSize();
417 NSSize size = NSMakeSize((float)sz.x, (float)sz.y);
418 [item setMaxSize:size];
419 [item setMinSize:size];
420 [view setHidden:NO];
421 }
422 return item;
423 }
424 return nil;
425 }
426
427 @end
428
429 #endif
430
431 @implementation wxNSToolBarButton
432
433 - (id)initWithFrame:(NSRect)frame
434 {
435 self = [super initWithFrame:frame];
436 impl = NULL;
437 [self setTarget: self];
438 [self setAction: @selector(clickedAction:)];
439 return self;
440 }
441
442 - (void) clickedAction: (id) sender
443 {
444 wxUnusedVar(sender);
445 if ( impl )
446 {
447 impl->Action();
448 }
449 }
450
451 - (void)setImplementation: (wxToolBarTool *) theImplementation
452 {
453 impl = theImplementation;
454 }
455
456 - (wxToolBarTool*) implementation
457 {
458 return impl;
459 }
460
461 - (BOOL) isFlipped
462 {
463 return YES;
464 }
465
466 @end
467
468 bool wxToolBarTool::Enable( bool enable )
469 {
470 if ( wxToolBarToolBase::Enable( enable ) == false )
471 return false;
472
473 if ( IsControl() )
474 {
475 GetControl()->Enable( enable );
476 }
477 else if ( IsButton() )
478 {
479 #if wxOSX_USE_NATIVE_TOOLBAR
480 if ( m_toolbarItem != NULL )
481 [m_toolbarItem setEnabled:enable];
482 #endif
483
484 if ( m_controlHandle != NULL )
485 [(NSControl*)m_controlHandle setEnabled:enable];
486 }
487
488 return true;
489 }
490
491 void wxToolBarTool::SetPosition( const wxPoint& position )
492 {
493 m_x = position.x;
494 m_y = position.y;
495
496 int mac_x = position.x;
497 int mac_y = position.y;
498
499 if ( IsButton() )
500 {
501 NSRect frame = [m_controlHandle frame];
502 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
503 {
504 frame.origin.x = mac_x;
505 frame.origin.y = mac_y;
506 [m_controlHandle setFrame:frame];
507 }
508 }
509 else if ( IsControl() )
510 {
511 // embedded native controls are moved by the OS
512 #if wxOSX_USE_NATIVE_TOOLBAR
513 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
514 #endif
515 {
516 GetControl()->Move( position );
517 }
518 }
519 else
520 {
521 NSRect frame = [m_controlHandle frame];
522 if ( frame.origin.x != mac_x || frame.origin.y != mac_y )
523 {
524 frame.origin.x = mac_x;
525 frame.origin.y = mac_y;
526 [m_controlHandle setFrame:frame];
527 }
528 }
529 }
530
531 void wxToolBarTool::UpdateImages()
532 {
533 [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()];
534
535 if ( CanBeToggled() )
536 {
537 int w = m_bmpNormal.GetWidth();
538 int h = m_bmpNormal.GetHeight();
539 m_alternateBitmap = wxBitmap( w, h );
540 wxMemoryDC dc;
541
542 dc.SelectObject( m_alternateBitmap );
543 dc.SetPen( wxPen(*wxBLACK) );
544 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
545 dc.DrawRoundedRectangle( 0, 0, w, h, 2 );
546 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
547 dc.SelectObject( wxNullBitmap );
548
549 [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()];
550 }
551 UpdateToggleImage( CanBeToggled() && IsToggled() );
552 }
553
554 void wxToolBarTool::UpdateToggleImage( bool toggle )
555 {
556 #if wxOSX_USE_NATIVE_TOOLBAR
557 if (m_toolbarItem != NULL )
558 {
559 // the native toolbar item only has a 'selected' state (one for one toolbar)
560 // so we emulate the toggle here
561 if ( CanBeToggled() && toggle )
562 [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()];
563 else
564 [m_toolbarItem setImage:m_bmpNormal.GetNSImage()];
565 }
566 else
567 #endif
568 {
569 if ( IsButton() )
570 [(NSButton*)m_controlHandle setState:(toggle ? NSOnState : NSOffState)];
571 }
572 }
573
574 wxToolBarTool::wxToolBarTool(
575 wxToolBar *tbar,
576 int id,
577 const wxString& label,
578 const wxBitmap& bmpNormal,
579 const wxBitmap& bmpDisabled,
580 wxItemKind kind,
581 wxObject *clientData,
582 const wxString& shortHelp,
583 const wxString& longHelp )
584 :
585 wxToolBarToolBase(
586 tbar, id, label, bmpNormal, bmpDisabled, kind,
587 clientData, shortHelp, longHelp )
588 {
589 Init();
590 }
591
592 #pragma mark -
593 #pragma mark Toolbar Implementation
594
595 wxToolBarToolBase *wxToolBar::CreateTool(
596 int id,
597 const wxString& label,
598 const wxBitmap& bmpNormal,
599 const wxBitmap& bmpDisabled,
600 wxItemKind kind,
601 wxObject *clientData,
602 const wxString& shortHelp,
603 const wxString& longHelp )
604 {
605 return new wxToolBarTool(
606 this, id, label, bmpNormal, bmpDisabled, kind,
607 clientData, shortHelp, longHelp );
608 }
609
610 wxToolBarToolBase *
611 wxToolBar::CreateTool(wxControl *control, const wxString& label)
612 {
613 return new wxToolBarTool(this, control, label);
614 }
615
616 void wxToolBar::Init()
617 {
618 m_maxWidth = -1;
619 m_maxHeight = -1;
620 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
621 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
622
623 #if wxOSX_USE_NATIVE_TOOLBAR
624 m_macToolbar = NULL;
625 m_macUsesNativeToolbar = false;
626 #endif
627 }
628
629 // also for the toolbar we have the dual implementation:
630 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
631
632 bool wxToolBar::Create(
633 wxWindow *parent,
634 wxWindowID id,
635 const wxPoint& pos,
636 const wxSize& size,
637 long style,
638 const wxString& name )
639 {
640 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
641 return false;
642
643 FixupStyle();
644
645 OSStatus err = noErr;
646
647 #if wxOSX_USE_NATIVE_TOOLBAR
648
649 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
650 {
651 static wxNSToolbarDelegate* controller = nil;
652
653 if ( controller == nil )
654 controller = [[wxNSToolbarDelegate alloc] init];
655 wxString identifier = wxString::Format( wxT("%p"), this );
656 wxCFStringRef cfidentifier(identifier);
657 NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()];
658
659 m_macToolbar = tb ;
660
661 if (m_macToolbar != NULL)
662 {
663 [tb setDelegate:controller];
664
665 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
666 NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall;
667
668 if ( style & wxTB_NOICONS )
669 mode = NSToolbarDisplayModeLabelOnly;
670 else if ( style & wxTB_TEXT )
671 mode = NSToolbarDisplayModeIconAndLabel;
672 else
673 mode = NSToolbarDisplayModeIconOnly;
674
675 [tb setDisplayMode:mode];
676 [tb setSizeMode:displaySize];
677 }
678 }
679 #endif // wxOSX_USE_NATIVE_TOOLBAR
680
681 return (err == noErr);
682 }
683
684 wxToolBar::~wxToolBar()
685 {
686 // removal only works while the toolbar is there
687 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
688 if ( frame && frame->GetToolBar() == this )
689 {
690 frame->SetToolBar(NULL);
691 }
692
693 [(NSToolbar*)m_macToolbar setDelegate:nil];
694 [(NSToolbar*)m_macToolbar release];
695 m_macToolbar = NULL;
696 }
697
698 bool wxToolBar::Show( bool show )
699 {
700 WXWindow tlw = MacGetTopLevelWindowRef();
701 bool bResult = (tlw != NULL);
702
703 if (bResult)
704 {
705 #if wxOSX_USE_NATIVE_TOOLBAR
706 bool ownToolbarInstalled = false;
707 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
708 if (ownToolbarInstalled)
709 {
710 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
711 if ( bResult )
712 [(NSToolbar*)m_macToolbar setVisible:show];
713 }
714 else
715 bResult = wxToolBarBase::Show( show );
716 #else
717
718 bResult = wxToolBarBase::Show( show );
719 #endif
720 }
721
722 return bResult;
723 }
724
725 bool wxToolBar::IsShown() const
726 {
727 bool bResult;
728
729 #if wxOSX_USE_NATIVE_TOOLBAR
730 bool ownToolbarInstalled;
731
732 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
733 if (ownToolbarInstalled)
734 {
735 bResult = [(NSToolbar*)m_macToolbar isVisible];
736 }
737 else
738 bResult = wxToolBarBase::IsShown();
739 #else
740
741 bResult = wxToolBarBase::IsShown();
742 #endif
743
744 return bResult;
745 }
746
747 void wxToolBar::DoGetSize( int *width, int *height ) const
748 {
749 #if wxOSX_USE_NATIVE_TOOLBAR
750 bool ownToolbarInstalled;
751
752 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
753 if ( ownToolbarInstalled )
754 {
755 WXWindow tlw = MacGetTopLevelWindowRef();
756 float toolbarHeight = 0.0;
757 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
758
759 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
760 {
761 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
762 styleMask:[tlw styleMask]];
763 toolbarHeight = NSHeight(windowFrame)
764 - NSHeight([[tlw contentView] frame]);
765 }
766
767 if ( width != NULL )
768 *width = (int)windowFrame.size.width;
769 if ( height != NULL )
770 *height = (int)toolbarHeight;
771 }
772 else
773 wxToolBarBase::DoGetSize( width, height );
774
775 #else
776 wxToolBarBase::DoGetSize( width, height );
777 #endif
778 }
779
780 void wxToolBar::DoGetPosition(int*x, int *y) const
781 {
782 #if wxOSX_USE_NATIVE_TOOLBAR
783 bool ownToolbarInstalled;
784
785 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
786 if ( ownToolbarInstalled )
787 {
788 WXWindow tlw = MacGetTopLevelWindowRef();
789 float toolbarHeight = 0.0;
790 NSRect windowFrame = NSMakeRect(0, 0, 0, 0);
791
792 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
793 {
794 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
795 styleMask:[tlw styleMask]];
796 toolbarHeight = NSHeight(windowFrame)
797 - NSHeight([[tlw contentView] frame]);
798 }
799
800 // it is extending to the north of the content area
801
802 if ( x != NULL )
803 *x = 0;
804 if ( y != NULL )
805 *y = -toolbarHeight;
806 }
807 else
808 wxToolBarBase::DoGetPosition( x, y );
809
810 #else
811 wxToolBarBase::DoGetPosition( x, y );
812 #endif
813 }
814
815 wxSize wxToolBar::DoGetBestSize() const
816 {
817 // was updated in Realize()
818
819 wxSize size = GetMinSize();
820
821 return size;
822 }
823
824 void wxToolBar::SetWindowStyleFlag( long style )
825 {
826 wxToolBarBase::SetWindowStyleFlag( style );
827
828 #if wxOSX_USE_NATIVE_TOOLBAR
829 if (m_macToolbar != NULL)
830 {
831 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
832
833 if ( style & wxTB_NOICONS )
834 mode = NSToolbarDisplayModeLabelOnly;
835 else if ( style & wxTB_TEXT )
836 mode = NSToolbarDisplayModeIconAndLabel;
837 else
838 mode = NSToolbarDisplayModeIconOnly;
839
840 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
841 }
842 #endif
843 }
844
845 #if wxOSX_USE_NATIVE_TOOLBAR
846 bool wxToolBar::MacWantsNativeToolbar()
847 {
848 return m_macUsesNativeToolbar;
849 }
850
851 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
852 {
853 bool bResultV = false;
854
855 if (ownToolbarInstalled != NULL)
856 *ownToolbarInstalled = false;
857
858 WXWindow tlw = MacGetTopLevelWindowRef();
859 if (tlw != NULL)
860 {
861 NSToolbar* curToolbarRef = [tlw toolbar];
862 bResultV = (curToolbarRef != NULL);
863 if (bResultV && (ownToolbarInstalled != NULL))
864 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
865 }
866
867 return bResultV;
868 }
869
870 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
871 {
872 bool bResult = false;
873
874 if (usesNative && (m_macToolbar == NULL))
875 return bResult;
876
877 if (usesNative && HasFlag(wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM) )
878 return bResult;
879
880 WXWindow tlw = MacGetTopLevelWindowRef();
881 if (tlw == NULL)
882 return bResult;
883
884 // check the existing toolbar
885 NSToolbar* curToolbarRef = [tlw toolbar];
886
887 m_macUsesNativeToolbar = usesNative;
888
889 if (m_macUsesNativeToolbar)
890 {
891 // only install toolbar if there isn't one installed already
892 if (curToolbarRef == NULL)
893 {
894 bResult = true;
895 [tlw setToolbar:(NSToolbar*) m_macToolbar];
896 [(NSToolbar*) m_macToolbar setVisible:YES];
897
898 GetPeer()->Move(0,0,0,0 );
899 SetSize( wxSIZE_AUTO_WIDTH, 0 );
900 GetPeer()->SetVisibility( false );
901 wxToolBarBase::Show( false );
902 }
903 }
904 else
905 {
906 // only deinstall toolbar if this is the installed one
907 if (m_macToolbar == curToolbarRef)
908 {
909 bResult = true;
910 [(NSToolbar*) m_macToolbar setVisible:NO];
911 MacUninstallNativeToolbar();
912 GetPeer()->SetVisibility( true );
913 }
914 }
915
916 if (bResult)
917 InvalidateBestSize();
918
919 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
920 return bResult;
921 }
922
923 void wxToolBar::MacUninstallNativeToolbar()
924 {
925 if (!m_macToolbar)
926 return;
927
928 WXWindow tlw = MacGetTopLevelWindowRef();
929 if (tlw)
930 [tlw setToolbar:nil];
931 }
932 #endif
933
934 void wxToolBar::DoLayout()
935 {
936 int maxToolWidth = 0;
937 int maxToolHeight = 0;
938
939 int tw, th;
940 GetSize( &tw, &th );
941
942 // find the maximum tool width and height
943 // and the number of stretchable items
944 int numStretchableSpaces = 0;
945 wxToolBarTool *tool;
946 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
947 while ( node )
948 {
949 tool = (wxToolBarTool *) node->GetData();
950 if ( tool != NULL )
951 {
952 wxSize sz = tool->GetSize();
953
954 if ( sz.x > maxToolWidth )
955 maxToolWidth = sz.x;
956 if ( sz.y > maxToolHeight )
957 maxToolHeight = sz.y;
958 if ( tool->IsStretchableSpace() )
959 numStretchableSpaces++;
960 }
961
962 node = node->GetNext();
963 }
964
965 // layout non-native toolbar
966
967 bool isHorizontal = !IsVertical();
968
969 int maxWidth = 0;
970 int maxHeight = 0;
971
972 int x = m_xMargin + kwxMacToolBarLeftMargin;
973 int y = m_yMargin + kwxMacToolBarTopMargin;
974
975 node = m_tools.GetFirst();
976 while ( node )
977 {
978 tool = (wxToolBarTool*) node->GetData();
979 if ( tool == NULL )
980 {
981 node = node->GetNext();
982 continue;
983 }
984
985 // set tool position:
986 // for the moment just perform a single row/column alignment
987 wxSize cursize = tool->GetSize();
988 if ( x + cursize.x > maxWidth )
989 maxWidth = x + cursize.x;
990 if ( y + cursize.y > maxHeight )
991 maxHeight = y + cursize.y;
992
993 // update the item positioning state
994 if ( !isHorizontal )
995 y += cursize.y + kwxMacToolSpacing;
996 else
997 x += cursize.x + kwxMacToolSpacing;
998
999 node = node->GetNext();
1000 }
1001
1002 if ( isHorizontal )
1003 {
1004 // if not set yet, only one row
1005 if ( m_maxRows <= 0 )
1006 SetRows( 1 );
1007
1008 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1009 m_minWidth = maxWidth;
1010 m_minHeight = m_maxHeight = maxToolHeight + 2 * (m_yMargin + kwxMacToolBarTopMargin);
1011 }
1012 else
1013 {
1014 // if not set yet, have one column
1015 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1016 SetRows( GetToolsCount() );
1017
1018 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1019 m_minHeight = maxHeight;
1020 m_minWidth = m_maxWidth = maxToolWidth + 2 * (m_yMargin + kwxMacToolBarTopMargin);
1021 }
1022
1023 int totalStretchableSpace = 0;
1024 int spacePerStretchable = 0;
1025 if ( numStretchableSpaces > 0 )
1026 {
1027 if ( isHorizontal )
1028 totalStretchableSpace = tw - maxWidth;
1029 else
1030 totalStretchableSpace = th - maxHeight;
1031
1032 if ( totalStretchableSpace > 0 )
1033 spacePerStretchable = totalStretchableSpace / numStretchableSpaces;
1034 }
1035
1036 // perform real positioning
1037
1038 x = m_xMargin + kwxMacToolBarLeftMargin;
1039 y = m_yMargin + kwxMacToolBarTopMargin;
1040
1041 node = m_tools.GetFirst();
1042 int currentStretchable = 0;
1043 while ( node )
1044 {
1045 tool = (wxToolBarTool*) node->GetData();
1046 if ( tool == NULL )
1047 {
1048 node = node->GetNext();
1049 continue;
1050 }
1051
1052 wxSize cursize = tool->GetSize();
1053 if ( tool->IsStretchableSpace() )
1054 {
1055 ++currentStretchable;
1056 int thisSpace = currentStretchable == numStretchableSpaces ?
1057 totalStretchableSpace - (currentStretchable-1)*spacePerStretchable :
1058 spacePerStretchable;
1059 if ( isHorizontal )
1060 cursize.x += thisSpace;
1061 else
1062 cursize.y += thisSpace;
1063 }
1064
1065 if ( !isHorizontal )
1066 {
1067 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1068 tool->SetPosition( wxPoint(x1, y) );
1069 }
1070 else
1071 {
1072 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1073 tool->SetPosition( wxPoint(x, y1) );
1074 }
1075
1076 // update the item positioning state
1077 if ( !isHorizontal )
1078 y += cursize.y + kwxMacToolSpacing;
1079 else
1080 x += cursize.x + kwxMacToolSpacing;
1081
1082 node = node->GetNext();
1083 }
1084
1085 }
1086
1087 bool wxToolBar::Realize()
1088 {
1089 if ( !wxToolBarBase::Realize() )
1090 return false;
1091
1092 wxToolBarTool *tool;
1093 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1094
1095 #if wxOSX_USE_NATIVE_TOOLBAR
1096 CFIndex currentPosition = 0;
1097 bool insertAll = false;
1098
1099 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
1100 wxFont f;
1101 wxFontEncoding enc;
1102 f = GetFont();
1103 if ( f.IsOk() )
1104 enc = f.GetEncoding();
1105 else
1106 enc = wxFont::GetDefaultEncoding();
1107
1108 node = m_tools.GetFirst();
1109 while ( node )
1110 {
1111 tool = (wxToolBarTool*) node->GetData();
1112 if ( tool == NULL )
1113 {
1114 node = node->GetNext();
1115 continue;
1116 }
1117
1118 // install in native NSToolbar
1119 if ( refTB )
1120 {
1121 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
1122 if ( hiItemRef != NULL )
1123 {
1124 // since setting the help texts is non-virtual we have to update
1125 // the strings now
1126 wxCFStringRef sh( tool->GetShortHelp(), enc);
1127 [hiItemRef setToolTip:sh.AsNSString()];
1128
1129 if ( insertAll || (tool->GetIndex() != currentPosition) )
1130 {
1131 if ( !insertAll )
1132 {
1133 insertAll = true;
1134
1135 // if this is the first tool that gets newly inserted or repositioned
1136 // first remove all 'old' tools from here to the right, because of this
1137 // all following tools will have to be reinserted (insertAll).
1138 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
1139 node2 != node;
1140 node2 = node2->GetPrevious() )
1141 {
1142 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
1143
1144 const long idx = tool2->GetIndex();
1145 if ( idx != -1 )
1146 {
1147 [refTB removeItemAtIndex:idx];
1148 tool2->SetIndex(-1);
1149 }
1150 }
1151 }
1152
1153 wxCFStringRef cfidentifier;
1154 NSString *nsItemId;
1155 if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR)
1156 {
1157 if ( tool->IsStretchable() )
1158 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1159 else
1160 {
1161 if ( UMAGetSystemVersion() < 0x1070 )
1162 nsItemId = NSToolbarSeparatorItemIdentifier;
1163 else
1164 nsItemId = NSToolbarSpaceItemIdentifier;
1165 }
1166 }
1167 else
1168 {
1169 cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool));
1170 nsItemId = cfidentifier.AsNSString();
1171 }
1172
1173 [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition];
1174 tool->SetIndex( currentPosition );
1175 }
1176
1177 currentPosition++;
1178 }
1179 }
1180 node = node->GetNext();
1181 }
1182
1183 #endif
1184
1185 DoLayout();
1186
1187 // adjust radio items
1188
1189 bool lastIsRadio = false;
1190 bool curIsRadio = false;
1191
1192 node = m_tools.GetFirst();
1193 while ( node )
1194 {
1195 tool = (wxToolBarTool*) node->GetData();
1196 if ( tool == NULL )
1197 {
1198 node = node->GetNext();
1199 continue;
1200 }
1201
1202 // update radio button (and group) state
1203 lastIsRadio = curIsRadio;
1204 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1205
1206 if ( !curIsRadio )
1207 {
1208 if ( tool->IsToggled() )
1209 DoToggleTool( tool, true );
1210 }
1211 else
1212 {
1213 if ( !lastIsRadio )
1214 {
1215 if ( tool->Toggle( true ) )
1216 {
1217 DoToggleTool( tool, true );
1218 }
1219 }
1220 else if ( tool->IsToggled() )
1221 {
1222 if ( tool->IsToggled() )
1223 DoToggleTool( tool, true );
1224
1225 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1226 while ( nodePrev )
1227 {
1228 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1229 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1230 break;
1231
1232 if ( toggleTool->Toggle( false ) )
1233 DoToggleTool( toggleTool, false );
1234
1235 nodePrev = nodePrev->GetPrevious();
1236 }
1237 }
1238 }
1239
1240 node = node->GetNext();
1241 }
1242
1243 InvalidateBestSize();
1244 SetInitialSize( wxSize(m_minWidth, m_minHeight));
1245
1246 SendSizeEventToParent();
1247 wxWindow * const parent = GetParent();
1248 if ( parent && !parent->IsBeingDeleted() )
1249 parent->MacOnInternalSize();
1250
1251 return true;
1252 }
1253
1254 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1255 {
1256 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
1257
1258 DoLayout();
1259 }
1260
1261 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1262 {
1263 m_defaultWidth = size.x + kwxMacToolBorder;
1264 m_defaultHeight = size.y + kwxMacToolBorder;
1265
1266 #if wxOSX_USE_NATIVE_TOOLBAR
1267 if (m_macToolbar != NULL)
1268 {
1269 int maxs = wxMax( size.x, size.y );
1270 NSToolbarSizeMode sizeSpec;
1271 if ( maxs > 32 )
1272 sizeSpec = NSToolbarSizeModeRegular;
1273 else if ( maxs > 24 )
1274 sizeSpec = NSToolbarSizeModeDefault;
1275 else
1276 sizeSpec = NSToolbarSizeModeSmall;
1277
1278 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1279 }
1280 #endif
1281 }
1282
1283 // The button size is bigger than the bitmap size
1284 wxSize wxToolBar::GetToolSize() const
1285 {
1286 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1287 }
1288
1289 void wxToolBar::SetRows(int nRows)
1290 {
1291 // avoid resizing the frame uselessly
1292 if ( nRows != m_maxRows )
1293 m_maxRows = nRows;
1294 }
1295
1296 void wxToolBar::MacSuperChangedPosition()
1297 {
1298 wxWindow::MacSuperChangedPosition();
1299
1300 /*
1301 #if wxOSX_USE_NATIVE_TOOLBAR
1302 if (! m_macUsesNativeToolbar )
1303 Realize();
1304 #else
1305
1306 Realize();
1307 #endif
1308 */
1309 }
1310
1311 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1312 {
1313 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1314 if ( tool )
1315 {
1316 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1317
1318 tool->SetNormalBitmap(bitmap);
1319
1320 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1321 tool->UpdateImages();
1322 }
1323 }
1324
1325 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1326 {
1327 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
1328 if ( tool )
1329 {
1330 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1331
1332 tool->SetDisabledBitmap(bitmap);
1333
1334 // TODO: what to do for this one?
1335 }
1336 }
1337
1338 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1339 {
1340 wxToolBarTool *tool;
1341 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1342 while ( node )
1343 {
1344 tool = (wxToolBarTool *)node->GetData();
1345 if (tool != NULL)
1346 {
1347 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1348 if ( r.Contains( wxPoint( x, y ) ) )
1349 return tool;
1350 }
1351
1352 node = node->GetNext();
1353 }
1354
1355 return NULL;
1356 }
1357
1358 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1359 {
1360 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1361 if ( tool != NULL )
1362 return tool->GetShortHelp();
1363
1364 return wxEmptyString;
1365 }
1366
1367 void wxToolBar::DoEnableTool(wxToolBarToolBase * WXUNUSED(t), bool WXUNUSED(enable))
1368 {
1369 // everything already done in the tool's Enable implementation
1370 }
1371
1372 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1373 {
1374 wxToolBarTool *tool = (wxToolBarTool *)t;
1375 if ( ( tool != NULL ) && tool->IsButton() )
1376 tool->UpdateToggleImage( toggle );
1377 }
1378
1379 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1380 {
1381 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
1382 if (tool == NULL)
1383 return false;
1384
1385 long style = GetWindowStyleFlag();
1386
1387 wxSize toolSize = GetToolSize();
1388 WXWidget controlHandle = NULL;
1389 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1390
1391 #if wxOSX_USE_NATIVE_TOOLBAR
1392 wxString label = tool->GetLabel();
1393 if (m_macToolbar && !label.empty() )
1394 {
1395 // strip mnemonics from the label for compatibility
1396 // with the usual labels in wxStaticText sense
1397 label = wxStripMenuCodes(label);
1398 }
1399 #endif // wxOSX_USE_NATIVE_TOOLBAR
1400
1401 switch (tool->GetStyle())
1402 {
1403 case wxTOOL_STYLE_SEPARATOR:
1404 {
1405 wxASSERT( tool->GetControlHandle() == NULL );
1406 toolSize.x /= 4;
1407 toolSize.y /= 4;
1408 if ( IsVertical() )
1409 toolrect.size.height = toolSize.y;
1410 else
1411 toolrect.size.width = toolSize.x;
1412
1413 // in flat style we need a visual separator
1414 #if wxOSX_USE_NATIVE_TOOLBAR
1415 if (m_macToolbar != NULL)
1416 {
1417 NSString * nsItemId = nil;
1418
1419 if ( tool->IsStretchable() )
1420 nsItemId = NSToolbarFlexibleSpaceItemIdentifier;
1421 else
1422 {
1423 if ( UMAGetSystemVersion() < 0x1070 )
1424 nsItemId = NSToolbarSeparatorItemIdentifier;
1425 else
1426 nsItemId = NSToolbarSpaceItemIdentifier;
1427 }
1428
1429 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId];
1430 tool->SetToolbarItemRef( item );
1431 }
1432 #endif // wxOSX_USE_NATIVE_TOOLBAR
1433
1434 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1435 [box setBoxType:NSBoxSeparator];
1436 controlHandle = box;
1437 tool->SetControlHandle( controlHandle );
1438 }
1439 break;
1440
1441 case wxTOOL_STYLE_BUTTON:
1442 {
1443 wxASSERT( tool->GetControlHandle() == NULL );
1444
1445 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1446
1447 [v setBezelStyle:NSSmallSquareBezelStyle];
1448 [[v cell] setControlSize:NSSmallControlSize];
1449 [v setFont:[NSFont fontWithName:[[v font] fontName] size:[NSFont systemFontSizeForControlSize:NSSmallControlSize]]];
1450 [v setBordered:NO];
1451 [v setButtonType: ( tool->CanBeToggled() ? NSToggleButton : NSMomentaryPushInButton )];
1452 [v setImplementation:tool];
1453
1454 controlHandle = v;
1455
1456 #if wxOSX_USE_NATIVE_TOOLBAR
1457 if (m_macToolbar != NULL)
1458 {
1459 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1460 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1461 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1462 [item setImplementation:tool];
1463 tool->SetToolbarItemRef( item );
1464 }
1465
1466 #endif // wxOSX_USE_NATIVE_TOOLBAR
1467 tool->SetControlHandle( controlHandle );
1468 tool->UpdateImages();
1469 tool->UpdateLabel();
1470
1471 if ( style & wxTB_NOICONS )
1472 [v setImagePosition:NSNoImage];
1473 else if ( style & wxTB_TEXT )
1474 [v setImagePosition:NSImageAbove];
1475 else
1476 [v setImagePosition:NSImageOnly];
1477 [v sizeToFit];
1478
1479 #if 0
1480 InstallControlEventHandler(
1481 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1482 GetEventTypeCount(eventList), eventList, tool, NULL );
1483 #endif
1484 }
1485 break;
1486
1487 case wxTOOL_STYLE_CONTROL:
1488
1489 #if wxOSX_USE_NATIVE_TOOLBAR
1490 if (m_macToolbar != NULL)
1491 {
1492 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1493 wxCHECK_MSG( view, false, wxT("control must be non-NULL") );
1494
1495 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
1496 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1497 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1498 [item setImplementation:tool];
1499 tool->SetToolbarItemRef( item );
1500 }
1501 #else
1502 // right now there's nothing to do here
1503 #endif
1504 tool->UpdateLabel();
1505 break;
1506
1507 default:
1508 break;
1509 }
1510
1511 if ( controlHandle )
1512 {
1513 WXWidget container = (WXWidget) GetHandle();
1514 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1515
1516 // SetControlVisibility( controlHandle, true, true );
1517 [container addSubview:controlHandle];
1518 }
1519
1520 // nothing special to do here - we relayout in Realize() later
1521 InvalidateBestSize();
1522
1523 return true;
1524
1525 }
1526
1527 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1528 {
1529 wxFAIL_MSG( wxT("not implemented") );
1530 }
1531
1532 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1533 {
1534 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
1535 wxToolBarToolsList::compatibility_iterator node;
1536 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1537 {
1538 wxToolBarToolBase *tool2 = node->GetData();
1539 if ( tool2 == tool )
1540 {
1541 // let node point to the next node in the list
1542 node = node->GetNext();
1543
1544 break;
1545 }
1546 }
1547
1548 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1549
1550 #if wxOSX_USE_NATIVE_TOOLBAR
1551 CFIndex removeIndex = tool->GetIndex();
1552 #endif
1553
1554 #if wxOSX_USE_NATIVE_TOOLBAR
1555 if (m_macToolbar != NULL)
1556 {
1557 if ( removeIndex != -1 && m_macToolbar )
1558 {
1559 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1560 tool->SetIndex( -1 );
1561 }
1562 }
1563 #endif
1564
1565 tool->ClearControl();
1566
1567 // and finally reposition all the controls after this one
1568
1569 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1570 {
1571 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1572 wxPoint pt = tool2->GetPosition();
1573
1574 if ( IsVertical() )
1575 pt.y -= sz.y;
1576 else
1577 pt.x -= sz.x;
1578
1579 tool2->SetPosition( pt );
1580
1581 #if wxOSX_USE_NATIVE_TOOLBAR
1582 if (m_macToolbar != NULL)
1583 {
1584 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1585 tool2->SetIndex( tool2->GetIndex() - 1 );
1586 }
1587 #endif
1588 }
1589
1590 InvalidateBestSize();
1591
1592 return true;
1593 }
1594
1595 #include <Carbon/Carbon.h>
1596
1597 void wxToolBar::OnPaint(wxPaintEvent& event)
1598 {
1599 #if wxOSX_USE_NATIVE_TOOLBAR
1600 if ( m_macUsesNativeToolbar )
1601 {
1602 // nothing to do here
1603 }
1604 else
1605 #endif
1606 {
1607 int w, h;
1608 GetSize( &w, &h );
1609
1610 wxPaintDC dc(this);
1611
1612 wxRect rect(0,0,w,h);
1613
1614 dc.GradientFillLinear( rect , wxColour( 0xCC,0xCC,0xCC ), wxColour( 0xA8,0xA8,0xA8 ) , wxSOUTH );
1615 dc.SetPen( wxPen( wxColour( 0x51,0x51,0x51 ) ) );
1616 if ( HasFlag(wxTB_LEFT) )
1617 dc.DrawLine(w-1, 0, w-1, h);
1618 else if ( HasFlag(wxTB_RIGHT) )
1619 dc.DrawLine(0, 0, 0, h);
1620 else if ( HasFlag(wxTB_BOTTOM) )
1621 dc.DrawLine(0, 0, w, 0);
1622 else if ( HasFlag(wxTB_TOP) )
1623 dc.DrawLine(0, h-1, w, h-1);
1624 }
1625 event.Skip();
1626 }
1627
1628 #if wxOSX_USE_NATIVE_TOOLBAR
1629 void wxToolBar::OSXSetSelectableTools(bool set)
1630 {
1631 wxCHECK_RET( m_macToolbar, "toolbar must be non-NULL" );
1632 [(wxNSToolbarDelegate*)[(NSToolbar*)m_macToolbar delegate] setSelectable:set];
1633 }
1634
1635 void wxToolBar::OSXSelectTool(int toolId)
1636 {
1637 wxToolBarToolBase *tool = FindById(toolId);
1638 wxCHECK_RET( tool, "invalid tool ID" );
1639 wxCHECK_RET( m_macToolbar, "toolbar must be non-NULL" );
1640
1641 wxString identifier = wxString::Format(wxT("%ld"), (long)tool);
1642 wxCFStringRef cfidentifier(identifier, wxFont::GetDefaultEncoding());
1643 [(NSToolbar*)m_macToolbar setSelectedItemIdentifier:cfidentifier.AsNSString()];
1644 }
1645 #endif // wxOSX_USE_NATIVE_TOOLBAR
1646
1647 #endif // wxUSE_TOOLBAR