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