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