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