]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/toolbar.mm
minor fixes; replace references to Windows95 with references to wxMSW where possible
[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
636 [(NSToolbar*)m_macToolbar release];
637 m_macToolbar = NULL;
638 }
639#endif
640}
641
642bool wxToolBar::Show( bool show )
643{
644 WXWindow tlw = MacGetTopLevelWindowRef();
645 bool bResult = (tlw != NULL);
646
647 if (bResult)
648 {
649#if wxOSX_USE_NATIVE_TOOLBAR
650 bool ownToolbarInstalled = false;
651 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
652 if (ownToolbarInstalled)
653 {
654 bResult = ([(NSToolbar*)m_macToolbar isVisible] != show);
655 if ( bResult )
656 [(NSToolbar*)m_macToolbar setVisible:show];
657 }
658 else
659 bResult = wxToolBarBase::Show( show );
660#else
661
662 bResult = wxToolBarBase::Show( show );
663#endif
664 }
665
666 return bResult;
667}
668
669bool wxToolBar::IsShown() const
670{
671 bool bResult;
672
673#if wxOSX_USE_NATIVE_TOOLBAR
674 bool ownToolbarInstalled;
675
676 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
677 if (ownToolbarInstalled)
678 {
679 bResult = [(NSToolbar*)m_macToolbar isVisible];
680 }
681 else
682 bResult = wxToolBarBase::IsShown();
683#else
684
685 bResult = wxToolBarBase::IsShown();
686#endif
687
688 return bResult;
689}
690
691void wxToolBar::DoGetSize( int *width, int *height ) const
692{
693#if wxOSX_USE_NATIVE_TOOLBAR
694 bool ownToolbarInstalled;
695
696 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
697 if ( ownToolbarInstalled )
698 {
699 WXWindow tlw = MacGetTopLevelWindowRef();
700 float toolbarHeight = 0.0;
701 NSRect windowFrame;
702
703 if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible])
704 {
705 windowFrame = [NSWindow contentRectForFrameRect:[tlw frame]
706 styleMask:[tlw styleMask]];
707 toolbarHeight = NSHeight(windowFrame)
708 - NSHeight([[tlw contentView] frame]);
709 }
710
711 if ( width != NULL )
712 *width = windowFrame.size.width;
713 if ( height != NULL )
714 *height = toolbarHeight;
715 }
716 else
717 wxToolBarBase::DoGetSize( width, height );
718
719#else
720 wxToolBarBase::DoGetSize( width, height );
721#endif
722}
723
724wxSize wxToolBar::DoGetBestSize() const
725{
726 int width, height;
727
728 DoGetSize( &width, &height );
729
730 return wxSize( width, height );
731}
732
733void wxToolBar::SetWindowStyleFlag( long style )
734{
735 wxToolBarBase::SetWindowStyleFlag( style );
736
737#if wxOSX_USE_NATIVE_TOOLBAR
738 if (m_macToolbar != NULL)
739 {
740 NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault;
741
742 if ( style & wxTB_NOICONS )
743 mode = NSToolbarDisplayModeLabelOnly;
744 else if ( style & wxTB_TEXT )
745 mode = NSToolbarDisplayModeIconAndLabel;
746 else
747 mode = NSToolbarDisplayModeIconOnly;
748
749 [(NSToolbar*) m_macToolbar setDisplayMode:mode];
750 }
751#endif
752}
753
754#if wxOSX_USE_NATIVE_TOOLBAR
755bool wxToolBar::MacWantsNativeToolbar()
756{
757 return m_macUsesNativeToolbar;
758}
759
760bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
761{
762 bool bResultV = false;
763
764 if (ownToolbarInstalled != NULL)
765 *ownToolbarInstalled = false;
766
767 WXWindow tlw = MacGetTopLevelWindowRef();
768 if (tlw != NULL)
769 {
770 NSToolbar* curToolbarRef = [tlw toolbar];
771 bResultV = (curToolbarRef != NULL);
772 if (bResultV && (ownToolbarInstalled != NULL))
773 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
774 }
775
776 return bResultV;
777}
778
779bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
780{
781 bool bResult = false;
782
783 if (usesNative && (m_macToolbar == NULL))
784 return bResult;
785
786 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
787 return bResult;
788
789 WXWindow tlw = MacGetTopLevelWindowRef();
790 if (tlw == NULL)
791 return bResult;
792
793 // check the existing toolbar
794 NSToolbar* curToolbarRef = [tlw toolbar];
795
796 m_macUsesNativeToolbar = usesNative;
797
798 if (m_macUsesNativeToolbar)
799 {
800 // only install toolbar if there isn't one installed already
801 if (curToolbarRef == NULL)
802 {
803 bResult = true;
804 [tlw setToolbar:(NSToolbar*) m_macToolbar];
805 [(NSToolbar*) m_macToolbar setVisible:YES];
806
807 m_peer->Move(0,0,0,0 );
808 SetSize( wxSIZE_AUTO_WIDTH, 0 );
809 m_peer->SetVisibility( false );
810 wxToolBarBase::Show( false );
811 }
812 }
813 else
814 {
815 // only deinstall toolbar if this is the installed one
816 if (m_macToolbar == curToolbarRef)
817 {
818 bResult = true;
819 [(NSToolbar*) m_macToolbar setVisible:NO];
820 [tlw setToolbar:nil];
821 m_peer->SetVisibility( true );
822 }
823 }
824
825 if (bResult)
826 InvalidateBestSize();
827
828// wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
829 return bResult;
830}
831#endif
832
833bool wxToolBar::Realize()
834{
835 if (m_tools.GetCount() == 0)
836 return false;
837
838 int maxWidth = 0;
839 int maxHeight = 0;
840
841 int maxToolWidth = 0;
842 int maxToolHeight = 0;
843
844 int x = m_xMargin + kwxMacToolBarLeftMargin;
845 int y = m_yMargin + kwxMacToolBarTopMargin;
846
847 int tw, th;
848 GetSize( &tw, &th );
849
850 // find the maximum tool width and height
851 wxToolBarTool *tool;
852 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
853 while ( node )
854 {
855 tool = (wxToolBarTool *) node->GetData();
856 if ( tool != NULL )
857 {
858 wxSize sz = tool->GetSize();
859
860 if ( sz.x > maxToolWidth )
861 maxToolWidth = sz.x;
862 if ( sz.y > maxToolHeight )
863 maxToolHeight = sz.y;
864 }
865
866 node = node->GetNext();
867 }
868
869 bool lastIsRadio = false;
870 bool curIsRadio = false;
871
872#if wxOSX_USE_NATIVE_TOOLBAR
873 CFIndex currentPosition = 0;
874 bool insertAll = false;
875
876 NSToolbar* refTB = (NSToolbar*)m_macToolbar;
877 wxFont f;
878 wxFontEncoding enc;
879 f = GetFont();
880 if ( f.IsOk() )
881 enc = f.GetEncoding();
882 else
883 enc = wxFont::GetDefaultEncoding();
884#endif
885
886 node = m_tools.GetFirst();
887 while ( node )
888 {
889 tool = (wxToolBarTool*) node->GetData();
890 if ( tool == NULL )
891 {
892 node = node->GetNext();
893 continue;
894 }
895
896 // set tool position:
897 // for the moment just perform a single row/column alignment
898 wxSize cursize = tool->GetSize();
899 if ( x + cursize.x > maxWidth )
900 maxWidth = x + cursize.x;
901 if ( y + cursize.y > maxHeight )
902 maxHeight = y + cursize.y;
903
904 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
905 {
906 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
907 tool->SetPosition( wxPoint(x1, y) );
908 }
909 else
910 {
911 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
912 tool->SetPosition( wxPoint(x, y1) );
913 }
914
915 // update the item positioning state
916 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
917 y += cursize.y + kwxMacToolSpacing;
918 else
919 x += cursize.x + kwxMacToolSpacing;
920
921#if wxOSX_USE_NATIVE_TOOLBAR
922 // install in native NSToolbar
923 if ( refTB )
924 {
925 NSToolbarItem* hiItemRef = tool->GetToolbarItemRef();
926 if ( hiItemRef != NULL )
927 {
928 // since setting the help texts is non-virtual we have to update
929 // the strings now
930 wxCFStringRef sh( tool->GetShortHelp(), enc);
931 [hiItemRef setToolTip:sh.AsNSString()];
932
933 if ( insertAll || (tool->GetIndex() != currentPosition) )
934 {
935 if ( !insertAll )
936 {
937 insertAll = true;
938
939 // if this is the first tool that gets newly inserted or repositioned
940 // first remove all 'old' tools from here to the right, because of this
941 // all following tools will have to be reinserted (insertAll).
942 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
943 node2 != node;
944 node2 = node2->GetPrevious() )
945 {
946 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
947
948 const long idx = tool2->GetIndex();
949 if ( idx != -1 )
950 {
951 [refTB removeItemAtIndex:idx];
952 tool2->SetIndex(-1);
953 }
954 }
955 }
5bf56597 956 wxString identifier = wxString::Format( wxT("%ld"), (long) tool );
0f9b48d1
SC
957 wxCFStringRef cfidentifier(identifier);
958
959 [refTB insertItemWithItemIdentifier:cfidentifier.AsNSString() atIndex:currentPosition];
960 tool->SetIndex( currentPosition );
961 }
962
963 currentPosition++;
964 }
965 }
966#endif
967
968 // update radio button (and group) state
969 lastIsRadio = curIsRadio;
970 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
971
972 if ( !curIsRadio )
973 {
974 if ( tool->IsToggled() )
975 DoToggleTool( tool, true );
976 }
977 else
978 {
979 if ( !lastIsRadio )
980 {
981 if ( tool->Toggle( true ) )
982 {
983 DoToggleTool( tool, true );
984 }
985 }
986 else if ( tool->IsToggled() )
987 {
988 if ( tool->IsToggled() )
989 DoToggleTool( tool, true );
990
991 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
992 while ( nodePrev )
993 {
994 wxToolBarToolBase *toggleTool = nodePrev->GetData();
995 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
996 break;
997
998 if ( toggleTool->Toggle( false ) )
999 DoToggleTool( toggleTool, false );
1000
1001 nodePrev = nodePrev->GetPrevious();
1002 }
1003 }
1004 }
1005
1006 node = node->GetNext();
1007 }
1008
1009 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
1010 {
1011 // if not set yet, only one row
1012 if ( m_maxRows <= 0 )
1013 SetRows( 1 );
1014
1015 m_minWidth = maxWidth;
1016 maxWidth = tw;
1017 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1018 m_minHeight = m_maxHeight = maxHeight;
1019 }
1020 else
1021 {
1022 // if not set yet, have one column
1023 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1024 SetRows( GetToolsCount() );
1025
1026 m_minHeight = maxHeight;
1027 maxHeight = th;
1028 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1029 m_minWidth = m_maxWidth = maxWidth;
1030 }
1031
1032#if 0
1033 // FIXME: should this be OSX-only?
1034 {
1035 bool wantNativeToolbar, ownToolbarInstalled;
1036
1037 // attempt to install the native toolbar
1038 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1039 MacInstallNativeToolbar( wantNativeToolbar );
1040 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1041 if (!ownToolbarInstalled)
1042 {
1043 SetSize( maxWidth, maxHeight );
1044 InvalidateBestSize();
1045 }
1046 }
1047#else
1048 SetSize( maxWidth, maxHeight );
1049 InvalidateBestSize();
1050#endif
1051
1052 SetInitialSize();
1053
1054 return true;
1055}
1056
1057void wxToolBar::SetToolBitmapSize(const wxSize& size)
1058{
1059 m_defaultWidth = size.x + kwxMacToolBorder;
1060 m_defaultHeight = size.y + kwxMacToolBorder;
1061
1062#if wxOSX_USE_NATIVE_TOOLBAR
1063 if (m_macToolbar != NULL)
1064 {
1065 int maxs = wxMax( size.x, size.y );
1066 NSToolbarSizeMode sizeSpec;
1067 if ( maxs > 32 )
1068 sizeSpec = NSToolbarSizeModeRegular;
1069 else if ( maxs > 24 )
1070 sizeSpec = NSToolbarSizeModeDefault;
1071 else
1072 sizeSpec = NSToolbarSizeModeSmall;
1073
1074 [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ];
1075 }
1076#endif
1077}
1078
1079// The button size is bigger than the bitmap size
1080wxSize wxToolBar::GetToolSize() const
1081{
1082 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1083}
1084
1085void wxToolBar::SetRows(int nRows)
1086{
1087 // avoid resizing the frame uselessly
1088 if ( nRows != m_maxRows )
1089 m_maxRows = nRows;
1090}
1091
1092void wxToolBar::MacSuperChangedPosition()
1093{
1094 wxWindow::MacSuperChangedPosition();
1095
1096#if wxOSX_USE_NATIVE_TOOLBAR
1097 if (! m_macUsesNativeToolbar )
1098 Realize();
1099#else
1100
1101 Realize();
1102#endif
1103}
1104
1105void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1106{
5c33522f 1107 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
0f9b48d1
SC
1108 if ( tool )
1109 {
1110 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1111
1112 tool->SetNormalBitmap(bitmap);
1113
1114 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1115 tool->UpdateImages();
1116 }
1117}
1118
1119void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1120{
5c33522f 1121 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
0f9b48d1
SC
1122 if ( tool )
1123 {
1124 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1125
1126 tool->SetDisabledBitmap(bitmap);
1127
1128 // TODO: what to do for this one?
1129 }
1130}
1131
1132wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1133{
1134 wxToolBarTool *tool;
1135 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1136 while ( node )
1137 {
1138 tool = (wxToolBarTool *)node->GetData();
1139 if (tool != NULL)
1140 {
1141 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1142 if ( r.Contains( wxPoint( x, y ) ) )
1143 return tool;
1144 }
1145
1146 node = node->GetNext();
1147 }
1148
1149 return (wxToolBarToolBase*)NULL;
1150}
1151
1152wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1153{
1154 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1155 if ( tool != NULL )
1156 return tool->GetShortHelp();
1157
1158 return wxEmptyString;
1159}
1160
1161void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1162{
1163 if ( t != NULL )
1164 ((wxToolBarTool*)t)->DoEnable( enable );
1165}
1166
1167void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1168{
1169 wxToolBarTool *tool = (wxToolBarTool *)t;
1170 if ( ( tool != NULL ) && tool->IsButton() )
1171 tool->UpdateToggleImage( toggle );
1172}
1173
1174bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1175{
5c33522f 1176 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
0f9b48d1
SC
1177 if (tool == NULL)
1178 return false;
1179
1180 wxSize toolSize = GetToolSize();
1181 WXWidget controlHandle = NULL;
1182 NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y );
1183
1184#if wxOSX_USE_NATIVE_TOOLBAR
1185 wxString label = tool->GetLabel();
1186 if (m_macToolbar && !label.empty() )
1187 {
1188 // strip mnemonics from the label for compatibility
1189 // with the usual labels in wxStaticText sense
1190 label = wxStripMenuCodes(label);
1191 }
1192#endif // wxOSX_USE_NATIVE_TOOLBAR
1193
1194 switch (tool->GetStyle())
1195 {
1196 case wxTOOL_STYLE_SEPARATOR:
1197 {
1198 wxASSERT( tool->GetControlHandle() == NULL );
1199 toolSize.x /= 4;
1200 toolSize.y /= 4;
1201 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1202 toolrect.size.height = toolSize.y;
1203 else
1204 toolrect.size.width = toolSize.x;
1205
1206 // in flat style we need a visual separator
1207#if wxOSX_USE_NATIVE_TOOLBAR
1208 if (m_macToolbar != NULL)
1209 {
1210 NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:NSToolbarSeparatorItemIdentifier];
1211 tool->SetToolbarItemRef( item );
1212 }
1213#endif // wxOSX_USE_NATIVE_TOOLBAR
1214
1215 NSBox* box = [[NSBox alloc] initWithFrame:toolrect];
1216 [box setBoxType:NSBoxSeparator];
1217 controlHandle = box;
1218 tool->SetControlHandle( controlHandle );
1219 }
1220 break;
1221
1222 case wxTOOL_STYLE_BUTTON:
1223 {
1224 wxASSERT( tool->GetControlHandle() == NULL );
1225
1226 wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect];
1227
1228 [v setBezelStyle:NSRegularSquareBezelStyle];
1229 [v setBordered:NO];
1230 [v setButtonType: ( tool->CanBeToggled() ? NSOnOffButton : NSMomentaryPushInButton )];
1231 [v setImplementation:tool];
1232
1233 controlHandle = v;
1234
1235#if wxOSX_USE_NATIVE_TOOLBAR
1236 if (m_macToolbar != NULL)
1237 {
5bf56597 1238 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
0f9b48d1
SC
1239 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1240 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1241 [item setImplementation:tool];
1242 tool->SetToolbarItemRef( item );
1243 }
1244
1245#endif // wxOSX_USE_NATIVE_TOOLBAR
1246 tool->SetControlHandle( controlHandle );
1247 tool->UpdateImages();
1248 tool->UpdateLabel();
1249#if 0
1250 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1251 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1252#endif
1253#if 0
1254 InstallControlEventHandler(
1255 (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1256 GetEventTypeCount(eventList), eventList, tool, NULL );
1257#endif
1258 }
1259 break;
1260
1261 case wxTOOL_STYLE_CONTROL:
1262
1263#if wxOSX_USE_NATIVE_TOOLBAR
1264 if (m_macToolbar != NULL)
1265 {
1266 WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ;
1267 wxCHECK_MSG( view, false, _T("control must be non-NULL") );
1268
5bf56597 1269 wxString identifier = wxString::Format(wxT("%ld"), (long) tool);
0f9b48d1
SC
1270 wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() );
1271 wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ];
1272 [item setImplementation:tool];
1273 tool->SetToolbarItemRef( item );
1274 }
1275#else
1276 // right now there's nothing to do here
1277#endif
1278 tool->UpdateLabel();
1279 break;
1280
1281 default:
1282 break;
1283 }
1284
1285 if ( controlHandle )
1286 {
1287 WXWidget container = (WXWidget) GetHandle();
1288 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1289
1290// SetControlVisibility( controlHandle, true, true );
1291 [container addSubview:controlHandle];
1292 }
1293
1294 // nothing special to do here - we relayout in Realize() later
1295 InvalidateBestSize();
1296
1297 return true;
1298
1299}
1300
1301void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1302{
1303 wxFAIL_MSG( wxT("not implemented") );
1304}
1305
1306bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1307{
5c33522f 1308 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
0f9b48d1
SC
1309 wxToolBarToolsList::compatibility_iterator node;
1310 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1311 {
1312 wxToolBarToolBase *tool2 = node->GetData();
1313 if ( tool2 == tool )
1314 {
1315 // let node point to the next node in the list
1316 node = node->GetNext();
1317
1318 break;
1319 }
1320 }
1321
1322 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1323
1324#if wxOSX_USE_NATIVE_TOOLBAR
1325 CFIndex removeIndex = tool->GetIndex();
1326#endif
1327
1328#if wxOSX_USE_NATIVE_TOOLBAR
1329 if (m_macToolbar != NULL)
1330 {
1331 if ( removeIndex != -1 && m_macToolbar )
1332 {
1333 [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex];
1334 tool->SetIndex( -1 );
1335 }
1336 }
1337#endif
1338
1339 tool->ClearControl();
1340
1341 // and finally reposition all the controls after this one
1342
1343 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1344 {
1345 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1346 wxPoint pt = tool2->GetPosition();
1347
1348 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1349 pt.y -= sz.y;
1350 else
1351 pt.x -= sz.x;
1352
1353 tool2->SetPosition( pt );
1354
1355#if wxOSX_USE_NATIVE_TOOLBAR
1356 if (m_macToolbar != NULL)
1357 {
1358 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1359 tool2->SetIndex( tool2->GetIndex() - 1 );
1360 }
1361#endif
1362 }
1363
1364 InvalidateBestSize();
1365
1366 return true;
1367}
1368
1369#include <Carbon/Carbon.h>
1370
1371void wxToolBar::OnPaint(wxPaintEvent& event)
1372{
1373#if wxOSX_USE_NATIVE_TOOLBAR
1374 if ( m_macUsesNativeToolbar )
1375 {
1376 event.Skip(true);
1377 return;
1378 }
1379#endif
1380
1381 wxPaintDC dc(this);
1382
1383 int w, h;
1384 GetSize( &w, &h );
1385
1386 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
1387
1388 if ( !drawMetalTheme )
1389 {
1390 HIThemePlacardDrawInfo info;
1391 memset( &info, 0, sizeof(info) );
1392 info.version = 0;
1393 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1394
1395 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1396 HIRect rect = CGRectMake( 0, 0, w, h );
1397 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1398 }
1399 else
1400 {
1401 // leave the background as it is (striped or metal)
1402 }
1403
1404 event.Skip();
1405}
1406
1407#endif // wxUSE_TOOLBAR