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