]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/toolbar.cpp
Set missing Language: headers in PO files.
[wxWidgets.git] / src / osx / carbon / toolbar.cpp
CommitLineData
489468fe 1/////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/toolbar.cpp
489468fe
SC
3// Purpose: wxToolBar
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
489468fe
SC
7// Copyright: (c) Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
13#if wxUSE_TOOLBAR
14
15#include "wx/toolbar.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/wx.h"
19#endif
20
21#include "wx/app.h"
524c47aa 22#include "wx/osx/private.h"
489468fe
SC
23#include "wx/geometry.h"
24#include "wx/sysopt.h"
25
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
489468fe
SC
34BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
35 EVT_PAINT( wxToolBar::OnPaint )
36END_EVENT_TABLE()
37
38
39#pragma mark -
40#pragma mark Tool Implementation
41
489468fe
SC
42// ----------------------------------------------------------------------------
43// private classes
44// ----------------------------------------------------------------------------
45
46// We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
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 HIToolbarItem 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( (ControlRef) 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( ControlRef 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 DisposeControl( m_controlHandle );
104 else
105 {
106 // the embedded control is not under the responsibility of the tool, it gets disposed of in the
107 // proper wxControl destructor
108 }
109 m_controlHandle = NULL ;
110 }
111
292e5e1f 112#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
113 if ( m_toolbarItemRef )
114 {
115 CFIndex count = CFGetRetainCount( m_toolbarItemRef ) ;
116 // different behaviour under Leopard
117 if ( UMAGetSystemVersion() < 0x1050 )
118 {
119 if ( count != 1 )
120 {
121 wxFAIL_MSG("Reference count of native tool was not 1 in wxToolBarTool destructor");
122 }
123 }
124 wxTheApp->MacAddToAutorelease(m_toolbarItemRef);
125 CFRelease(m_toolbarItemRef);
126 m_toolbarItemRef = NULL;
127 }
292e5e1f 128#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
129 }
130
131 wxSize GetSize() const
132 {
133 wxSize curSize;
134
135 if ( IsControl() )
136 {
137 curSize = GetControl()->GetSize();
138 }
139 else if ( IsButton() )
140 {
141 curSize = GetToolBar()->GetToolSize();
142 }
143 else
144 {
145 // separator size
146 curSize = GetToolBar()->GetToolSize();
f0ceebb7 147 if ( GetToolBar()->GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
148 curSize.y /= 4;
149 else
150 curSize.x /= 4;
151 }
152
153 return curSize;
154 }
155
156 wxPoint GetPosition() const
157 {
158 return wxPoint( m_x, m_y );
159 }
160
2bed17c8 161 virtual bool Enable( bool enable );
489468fe
SC
162
163 void UpdateToggleImage( bool toggle );
164
2bed17c8 165 virtual bool Toggle(bool toggle)
489468fe 166 {
2bed17c8
SC
167 if ( wxToolBarToolBase::Toggle( toggle ) == false )
168 return false;
bb2212e6 169
2bed17c8
SC
170 UpdateToggleImage(toggle);
171 return true;
172 }
bb2212e6 173
2bed17c8
SC
174 void UpdateHelpStrings()
175 {
bb2212e6 176#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
177 if ( m_toolbarItemRef )
178 {
402c3347 179 wxFontEncoding enc = GetToolBarFontEncoding();
489468fe
SC
180
181 HIToolbarItemSetHelpText(
182 m_toolbarItemRef,
183 wxCFStringRef( GetShortHelp(), enc ),
184 wxCFStringRef( GetLongHelp(), enc ) );
185 }
2bed17c8 186#endif
489468fe 187 }
bb2212e6 188
2bed17c8 189 virtual bool SetShortHelp(const wxString& help)
489468fe 190 {
2bed17c8
SC
191 if ( wxToolBarToolBase::SetShortHelp( help ) == false )
192 return false;
bb2212e6
VZ
193
194 UpdateHelpStrings();
2bed17c8 195 return true;
489468fe 196 }
bb2212e6 197
2bed17c8 198 virtual bool SetLongHelp(const wxString& help)
489468fe 199 {
2bed17c8
SC
200 if ( wxToolBarToolBase::SetLongHelp( help ) == false )
201 return false;
202
203 UpdateHelpStrings();
204 return true;
489468fe
SC
205 }
206
bb2212e6 207 virtual void SetNormalBitmap(const wxBitmap& bmp)
489468fe 208 {
2bed17c8
SC
209 wxToolBarToolBase::SetNormalBitmap(bmp);
210 UpdateToggleImage(CanBeToggled() && IsToggled());
489468fe 211 }
bb2212e6 212
e3e061ee
VZ
213 virtual void SetLabel(const wxString& label)
214 {
215 wxToolBarToolBase::SetLabel(label);
2bed17c8 216#if wxOSX_USE_NATIVE_TOOLBAR
e3e061ee
VZ
217 if ( m_toolbarItemRef )
218 {
e3e061ee
VZ
219 // strip mnemonics from the label for compatibility with the usual
220 // labels in wxStaticText sense
221 wxString labelStr = wxStripMenuCodes(label);
222
402c3347
VZ
223 HIToolbarItemSetLabel(
224 m_toolbarItemRef,
225 wxCFStringRef(labelStr, GetToolBarFontEncoding()) );
e3e061ee 226 }
2bed17c8
SC
227#endif
228 }
229
230#if wxOSX_USE_NATIVE_TOOLBAR
231 void SetToolbarItemRef( HIToolbarItemRef ref )
232 {
233 if ( m_controlHandle )
234 HideControl( m_controlHandle );
235 if ( m_toolbarItemRef )
236 CFRelease( m_toolbarItemRef );
237
238 m_toolbarItemRef = ref;
239 UpdateHelpStrings();
240 }
241
242 HIToolbarItemRef GetToolbarItemRef() const
243 {
244 return m_toolbarItemRef;
245 }
246
247 void SetIndex( CFIndex idx )
248 {
249 m_index = idx;
250 }
251
252 CFIndex GetIndex() const
253 {
254 return m_index;
e3e061ee
VZ
255 }
256#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
257
258private:
402c3347
VZ
259#if wxOSX_USE_NATIVE_TOOLBAR
260 wxFontEncoding GetToolBarFontEncoding() const
261 {
262 wxFont f;
402c3347
VZ
263 if ( GetToolBar() )
264 f = GetToolBar()->GetFont();
265 return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding();
266 }
267#endif // wxOSX_USE_NATIVE_TOOLBAR
268
489468fe
SC
269 void Init()
270 {
271 m_controlHandle = NULL;
272
292e5e1f 273#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
274 m_toolbarItemRef = NULL;
275 m_index = -1;
276#endif
277 }
278
279 ControlRef m_controlHandle;
280 wxCoord m_x;
281 wxCoord m_y;
282
292e5e1f 283#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
284 HIToolbarItemRef m_toolbarItemRef;
285 // position in its toolbar, -1 means not inserted
286 CFIndex m_index;
287#endif
288};
289
290static const EventTypeSpec eventList[] =
291{
292 { kEventClassControl, kEventControlHit },
293 { kEventClassControl, kEventControlHitTest },
294};
295
296static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef WXUNUSED(handler), EventRef event, void *data )
297{
298 OSStatus result = eventNotHandledErr;
299 ControlRef controlRef;
300 wxMacCarbonEvent cEvent( event );
301
302 cEvent.GetParameter( kEventParamDirectObject, &controlRef );
303
304 switch ( GetEventKind( event ) )
305 {
306 case kEventControlHit:
307 {
308 wxToolBarTool *tbartool = (wxToolBarTool*)data;
309 wxToolBar *tbar = tbartool != NULL ? (wxToolBar*) (tbartool->GetToolBar()) : NULL;
310 if ((tbartool != NULL) && tbartool->CanBeToggled())
311 {
312 bool shouldToggle;
313
314 shouldToggle = !tbartool->IsToggled();
315
316 tbar->ToggleTool( tbartool->GetId(), shouldToggle );
317 }
318
319 if (tbartool != NULL)
320 tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() );
321 result = noErr;
322 }
323 break;
324
325 case kEventControlHitTest:
326 {
327 HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation);
328 HIRect rect;
329 HIViewGetBounds( controlRef, &rect );
330
331 ControlPartCode pc = kControlNoPart;
332 if ( CGRectContainsPoint( rect, pt ) )
333 pc = kControlIconPart;
334 cEvent.SetParameter( kEventParamControlPart, typeControlPartCode, pc );
335 result = noErr;
336 }
337 break;
338
339 default:
340 break;
341 }
342
343 return result;
344}
345
346static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
347{
348 OSStatus result = eventNotHandledErr;
349
350 switch ( GetEventClass( event ) )
351 {
352 case kEventClassControl:
353 result = wxMacToolBarToolControlEventHandler( handler, event, data );
354 break;
355
356 default:
357 break;
358 }
359
360 return result;
361}
362
363DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
364
292e5e1f 365#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
366
367static const EventTypeSpec toolBarEventList[] =
368{
369 { kEventClassToolbarItem, kEventToolbarItemPerformAction },
370};
371
372static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef WXUNUSED(handler), EventRef event, void *data )
373{
374 OSStatus result = eventNotHandledErr;
375
376 switch ( GetEventKind( event ) )
377 {
378 case kEventToolbarItemPerformAction:
379 {
380 wxToolBarTool* tbartool = (wxToolBarTool*) data;
381 if ( tbartool != NULL )
382 {
383 wxToolBar *tbar = (wxToolBar*)(tbartool->GetToolBar());
384 int toolID = tbartool->GetId();
385
386 if ( tbartool->CanBeToggled() )
387 {
388 if ( tbar != NULL )
389 tbar->ToggleTool(toolID, !tbartool->IsToggled() );
390 }
391
392 if ( tbar != NULL )
393 tbar->OnLeftClick( toolID, tbartool->IsToggled() );
394 result = noErr;
395 }
396 }
397 break;
398
399 default:
400 break;
401 }
402
403 return result;
404}
405
406static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
407{
408 OSStatus result = eventNotHandledErr;
409
410 switch ( GetEventClass( event ) )
411 {
412 case kEventClassToolbarItem:
413 result = wxMacToolBarCommandEventHandler( handler, event, data );
414 break;
415
416 default:
417 break;
418 }
419
420 return result;
421}
422
423DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler )
424
425#endif
426
2bed17c8 427bool wxToolBarTool::Enable( bool enable )
489468fe 428{
2bed17c8
SC
429 if ( wxToolBarToolBase::Enable( enable ) == false )
430 return false;
bb2212e6 431
489468fe
SC
432 if ( IsControl() )
433 {
434 GetControl()->Enable( enable );
435 }
436 else if ( IsButton() )
437 {
292e5e1f 438#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
439 if ( m_toolbarItemRef != NULL )
440 HIToolbarItemSetEnabled( m_toolbarItemRef, enable );
441#endif
442
443 if ( m_controlHandle != NULL )
444 {
445 if ( enable )
446 EnableControl( m_controlHandle );
447 else
448 DisableControl( m_controlHandle );
449 }
450 }
451
452 return true;
453}
454
455void wxToolBarTool::SetPosition( const wxPoint& position )
456{
457 m_x = position.x;
458 m_y = position.y;
459
460 int mac_x = position.x;
461 int mac_y = position.y;
462
463 if ( IsButton() )
464 {
465 Rect contrlRect;
466 GetControlBounds( m_controlHandle, &contrlRect );
467 int former_mac_x = contrlRect.left;
468 int former_mac_y = contrlRect.top;
469 GetToolBar()->GetToolSize();
470
471 if ( mac_x != former_mac_x || mac_y != former_mac_y )
472 {
473 ::MoveControl( m_controlHandle, mac_x, mac_y );
474 }
475 }
476 else if ( IsControl() )
477 {
478 // embedded native controls are moved by the OS
292e5e1f 479#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
480 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
481#endif
482 {
483 GetControl()->Move( position );
484 }
485 }
486 else
487 {
488 // separator
489 Rect contrlRect;
490 GetControlBounds( m_controlHandle, &contrlRect );
491 int former_mac_x = contrlRect.left;
492 int former_mac_y = contrlRect.top;
493
494 if ( mac_x != former_mac_x || mac_y != former_mac_y )
495 ::MoveControl( m_controlHandle, mac_x, mac_y );
496 }
497}
498
499void wxToolBarTool::UpdateToggleImage( bool toggle )
500{
501 if ( toggle )
502 {
f656d379
RR
503 int w = m_bmpNormal.GetWidth() + 6;
504 int h = m_bmpNormal.GetHeight() + 6;
489468fe
SC
505 wxBitmap bmp( w, h );
506 wxMemoryDC dc;
507
508 dc.SelectObject( bmp );
f656d379
RR
509 wxColour mid_grey_75 = wxColour(128, 128, 128, 196);
510 wxColour light_grey_75 = wxColour(196, 196, 196, 196);
511 dc.GradientFillLinear( wxRect(1, 1, w - 1, h-1),
512 light_grey_75, mid_grey_75, wxNORTH);
513 wxColour black_50 = wxColour(0, 0, 0, 127);
514 dc.SetPen( wxPen(black_50) );
515 dc.DrawRoundedRectangle( 0, 0, w, h, 1.5 );
516 dc.DrawBitmap( m_bmpNormal, 3, 3, true );
489468fe
SC
517 dc.SelectObject( wxNullBitmap );
518 ControlButtonContentInfo info;
519 wxMacCreateBitmapButton( &info, bmp );
520 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
292e5e1f 521#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
522 if (m_toolbarItemRef != NULL)
523 {
524 ControlButtonContentInfo info2;
525 wxMacCreateBitmapButton( &info2, bmp, kControlContentCGImageRef);
526 HIToolbarItemSetImage( m_toolbarItemRef, info2.u.imageRef );
527 wxMacReleaseBitmapButton( &info2 );
528 }
529#endif
530 wxMacReleaseBitmapButton( &info );
531 }
532 else
533 {
534 ControlButtonContentInfo info;
535 wxMacCreateBitmapButton( &info, m_bmpNormal );
536 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
292e5e1f 537#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
538 if (m_toolbarItemRef != NULL)
539 {
540 ControlButtonContentInfo info2;
541 wxMacCreateBitmapButton( &info2, m_bmpNormal, kControlContentCGImageRef);
542 HIToolbarItemSetImage( m_toolbarItemRef, info2.u.imageRef );
543 wxMacReleaseBitmapButton( &info2 );
544 }
545#endif
546 wxMacReleaseBitmapButton( &info );
547 }
548
549 IconTransformType transform = toggle ? kTransformSelected : kTransformNone;
550 SetControlData(
551 m_controlHandle, 0, kControlIconTransformTag,
552 sizeof(transform), (Ptr)&transform );
553 HIViewSetNeedsDisplay( m_controlHandle, true );
554
555}
556
557wxToolBarTool::wxToolBarTool(
558 wxToolBar *tbar,
559 int id,
560 const wxString& label,
561 const wxBitmap& bmpNormal,
562 const wxBitmap& bmpDisabled,
563 wxItemKind kind,
564 wxObject *clientData,
565 const wxString& shortHelp,
566 const wxString& longHelp )
567 :
568 wxToolBarToolBase(
569 tbar, id, label, bmpNormal, bmpDisabled, kind,
570 clientData, shortHelp, longHelp )
571{
572 Init();
573}
574
575#pragma mark -
576#pragma mark Toolbar Implementation
577
578wxToolBarToolBase *wxToolBar::CreateTool(
579 int id,
580 const wxString& label,
581 const wxBitmap& bmpNormal,
582 const wxBitmap& bmpDisabled,
583 wxItemKind kind,
584 wxObject *clientData,
585 const wxString& shortHelp,
586 const wxString& longHelp )
587{
588 return new wxToolBarTool(
589 this, id, label, bmpNormal, bmpDisabled, kind,
590 clientData, shortHelp, longHelp );
591}
592
593wxToolBarToolBase *
594wxToolBar::CreateTool(wxControl *control, const wxString& label)
595{
596 return new wxToolBarTool(this, control, label);
597}
598
599void wxToolBar::Init()
600{
601 m_maxWidth = -1;
602 m_maxHeight = -1;
603 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
604 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
605
292e5e1f 606#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 607 m_macToolbar = NULL;
489468fe
SC
608 m_macUsesNativeToolbar = false;
609#endif
610}
611
612#define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
613
614const EventTypeSpec kEvents[] =
615{
616 { kEventClassHIObject, kEventHIObjectConstruct },
617 { kEventClassHIObject, kEventHIObjectInitialize },
618 { kEventClassHIObject, kEventHIObjectDestruct },
619
620 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
621};
622
623const EventTypeSpec kViewEvents[] =
624{
625 { kEventClassControl, kEventControlGetSizeConstraints }
626};
627
628struct ControlToolbarItem
629{
630 HIToolbarItemRef toolbarItem;
631 HIViewRef viewRef;
632 wxSize lastValidSize ;
633};
634
635static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
636{
637 OSStatus result = eventNotHandledErr;
638 ControlToolbarItem* object = (ControlToolbarItem*)inUserData;
639
640 switch ( GetEventClass( inEvent ) )
641 {
642 case kEventClassHIObject:
643 switch ( GetEventKind( inEvent ) )
644 {
645 case kEventHIObjectConstruct:
646 {
647 HIObjectRef toolbarItem;
648 ControlToolbarItem* item;
649
650 GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL,
651 sizeof( HIObjectRef ), NULL, &toolbarItem );
652
653 item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ;
654 item->toolbarItem = toolbarItem ;
655 item->lastValidSize = wxSize(-1,-1);
656 item->viewRef = NULL ;
657
658 SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item );
659
660 result = noErr ;
661 }
662 break;
663
664 case kEventHIObjectInitialize:
665 result = CallNextEventHandler( inCallRef, inEvent );
666 if ( result == noErr )
667 {
668 CFDataRef data;
669 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
670 sizeof( CFTypeRef ), NULL, &data );
671
672 HIViewRef viewRef ;
673
674 wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ;
675 memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ;
676
677 object->viewRef = (HIViewRef) viewRef ;
678 // make sure we keep that control during our lifetime
679 CFRetain( object->viewRef ) ;
680
681 verify_noerr(InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler,
682 GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL ));
683 result = noErr ;
684 }
685 break;
686
687 case kEventHIObjectDestruct:
688 {
689 HIViewRef viewRef = object->viewRef ;
690 if( viewRef && IsValidControlHandle( viewRef) )
691 {
692 // depending whether the wxControl corresponding to this HIView has already been destroyed or
693 // not, ref counts differ, so we cannot assert a special value
694 CFIndex count = CFGetRetainCount( viewRef ) ;
695 if ( count >= 1 )
696 {
489468fe
SC
697 CFRelease( viewRef ) ;
698 }
699 }
700 free( object ) ;
701 result = noErr;
702 }
703 break;
704 }
705 break;
706
707 case kEventClassToolbarItem:
708 switch ( GetEventKind( inEvent ) )
709 {
710 case kEventToolbarItemCreateCustomView:
711 {
712 HIViewRef viewRef = object->viewRef ;
713 HIViewRemoveFromSuperview( viewRef ) ;
714 HIViewSetVisible(viewRef, true) ;
715 CFRetain( viewRef ) ;
716 result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef );
717 }
718 break;
719 }
720 break;
721
722 case kEventClassControl:
723 switch ( GetEventKind( inEvent ) )
724 {
725 case kEventControlGetSizeConstraints:
726 {
b2680ced 727 wxWindow* wxwindow = wxFindWindowFromWXWidget( (WXWidget) object->viewRef ) ;
489468fe
SC
728 if ( wxwindow )
729 {
730 // during toolbar layout the native window sometimes gets negative sizes,
731 // sometimes it just gets shrunk behind our back, so in order to avoid
732 // ever shrinking more, once a valid size is captured, we keep it
733
734 wxSize sz = object->lastValidSize;
735 if ( sz.x <= 0 || sz.y <= 0 )
736 {
737 sz = wxwindow->GetSize() ;
738 sz.x -= wxwindow->MacGetLeftBorderSize() + wxwindow->MacGetRightBorderSize();
739 sz.y -= wxwindow->MacGetTopBorderSize() + wxwindow->MacGetBottomBorderSize();
740 if ( sz.x > 0 && sz.y > 0 )
741 object->lastValidSize = sz ;
742 else
743 sz = wxSize(0,0) ;
744 }
745
746 // Extra width to avoid edge of combobox being cut off
747 sz.x += 3;
748
749 HISize min, max;
750 min.width = max.width = sz.x ;
751 min.height = max.height = sz.y ;
752
753 result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize,
754 sizeof( HISize ), &min );
755
756 result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize,
757 sizeof( HISize ), &max );
758 result = noErr ;
759 }
760 }
761 break;
762 }
763 break;
764 }
765
766 return result;
767}
768
769void RegisterControlToolbarItemClass()
770{
771 static bool sRegistered;
772
773 if ( !sRegistered )
774 {
775 HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0,
776 ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL );
777
778 sRegistered = true;
779 }
780}
781
782HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData)
783{
784 RegisterControlToolbarItemClass();
785
786 OSStatus err;
787 EventRef event;
788 UInt32 options = kHIToolbarItemAllowDuplicates;
789 HIToolbarItemRef result = NULL;
790
791 err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
792 require_noerr( err, CantCreateEvent );
793
794 SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options );
795 SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier );
796
797 if ( inConfigData )
798 SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData );
799
800 err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result );
801 check_noerr( err );
802
803 ReleaseEvent( event );
804CantCreateEvent :
805 return result ;
806}
807
292e5e1f 808#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
809static const EventTypeSpec kToolbarEvents[] =
810{
811 { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
812 { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
813 { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
814};
815
816static OSStatus ToolbarDelegateHandler(EventHandlerCallRef WXUNUSED(inCallRef),
817 EventRef inEvent,
818 void* WXUNUSED(inUserData))
819{
820 OSStatus result = eventNotHandledErr;
821 // Not yet needed
822 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
823 CFMutableArrayRef array;
824
825 switch ( GetEventKind( inEvent ) )
826 {
827 case kEventToolbarGetDefaultIdentifiers:
828 {
829 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
830 sizeof( CFMutableArrayRef ), NULL, &array );
831 // not implemented yet
832 // GetToolbarDefaultItems( array );
833 result = noErr;
834 }
835 break;
836
837 case kEventToolbarGetAllowedIdentifiers:
838 {
839 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
840 sizeof( CFMutableArrayRef ), NULL, &array );
841 // not implemented yet
842 // GetToolbarAllowedItems( array );
843 result = noErr;
844 }
845 break;
846 case kEventToolbarCreateItemWithIdentifier:
847 {
848 HIToolbarItemRef item = NULL;
849 CFTypeRef data = NULL;
850 CFStringRef identifier = NULL ;
851
852 GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL,
853 sizeof( CFStringRef ), NULL, &identifier );
854
855 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
856 sizeof( CFTypeRef ), NULL, &data );
857
858 if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo )
859 {
860 item = CreateControlToolbarItem( kControlToolbarItemClassID, data );
861 if ( item )
862 {
863 SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef,
864 sizeof( HIToolbarItemRef ), &item );
865 result = noErr;
866 }
867 }
868
869 }
870 break;
871 }
872 return result ;
873}
292e5e1f 874#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
875
876// also for the toolbar we have the dual implementation:
877// only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
878
879bool wxToolBar::Create(
880 wxWindow *parent,
881 wxWindowID id,
882 const wxPoint& pos,
883 const wxSize& size,
884 long style,
885 const wxString& name )
886{
887 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
888 return false;
889
890 FixupStyle();
891
892 OSStatus err = noErr;
893
292e5e1f 894#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
895 if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1)
896 {
897 wxString labelStr = wxString::Format( wxT("%p"), this );
898 err = HIToolbarCreate(
899 wxCFStringRef( labelStr, wxFont::GetDefaultEncoding() ), 0,
524c47aa 900 (HIToolbarRef*) &m_macToolbar );
489468fe 901
524c47aa 902 if (m_macToolbar != NULL)
489468fe 903 {
524c47aa 904 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macToolbar ), ToolbarDelegateHandler,
489468fe
SC
905 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
906
907 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
908 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
909
910 if ( style & wxTB_NOICONS )
911 mode = kHIToolbarDisplayModeLabelOnly;
912 else if ( style & wxTB_TEXT )
913 mode = kHIToolbarDisplayModeIconAndLabel;
914 else
915 mode = kHIToolbarDisplayModeIconOnly;
916
524c47aa
SC
917 HIToolbarSetDisplayMode( (HIToolbarRef) m_macToolbar, mode );
918 HIToolbarSetDisplaySize( (HIToolbarRef) m_macToolbar, displaySize );
489468fe
SC
919 }
920 }
292e5e1f 921#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
922
923 return (err == noErr);
924}
925
926wxToolBar::~wxToolBar()
927{
e6b3b53c 928#if wxOSX_USE_NATIVE_TOOLBAR
3cd37395
VZ
929 // We could be not using a native tool bar at all, this happens when we're
930 // created with something other than the frame as parent for example.
931 if ( !m_macToolbar )
932 return;
933
71940de6
SC
934 // it might already have been uninstalled due to a previous call to Destroy, but in case
935 // wasn't, do so now, otherwise redraw events may occur for deleted objects
936 bool ownToolbarInstalled = false;
937 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
938 if (ownToolbarInstalled)
939 {
940 MacUninstallNativeToolbar();
941 }
942
1665389a
KO
943 CFIndex count = CFGetRetainCount( m_macToolbar ) ;
944 // Leopard seems to have one refcount more, so we cannot check reliably at the moment
945 if ( UMAGetSystemVersion() < 0x1050 )
489468fe 946 {
1665389a 947 if ( count != 1 )
489468fe 948 {
1665389a 949 wxFAIL_MSG("Reference count of native control was not 1 in wxToolBar destructor");
489468fe 950 }
489468fe 951 }
1665389a
KO
952 CFRelease( (HIToolbarRef)m_macToolbar );
953 m_macToolbar = NULL;
e6b3b53c 954#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
955}
956
957bool wxToolBar::Show( bool show )
958{
959 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
960 bool bResult = (tlw != NULL);
961
962 if (bResult)
963 {
292e5e1f 964#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
965 bool ownToolbarInstalled = false;
966 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
967 if (ownToolbarInstalled)
968 {
969 bResult = (IsWindowToolbarVisible( tlw ) != show);
970 if ( bResult )
971 ShowHideWindowToolbar( tlw, show, false );
972 }
973 else
974 bResult = wxToolBarBase::Show( show );
975#else
976
977 bResult = wxToolBarBase::Show( show );
978#endif
979 }
980
981 return bResult;
982}
983
984bool wxToolBar::IsShown() const
985{
986 bool bResult;
987
292e5e1f 988#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
989 bool ownToolbarInstalled;
990
991 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
992 if (ownToolbarInstalled)
993 {
994 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
995 bResult = IsWindowToolbarVisible( tlw );
996 }
997 else
998 bResult = wxToolBarBase::IsShown();
999#else
1000
1001 bResult = wxToolBarBase::IsShown();
1002#endif
1003
1004 return bResult;
1005}
1006
1007void wxToolBar::DoGetSize( int *width, int *height ) const
1008{
292e5e1f 1009#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1010 Rect boundsR;
1011 bool ownToolbarInstalled;
1012
1013 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1014 if ( ownToolbarInstalled )
1015 {
1016 // TODO: is this really a control ?
524c47aa 1017 GetControlBounds( (ControlRef) m_macToolbar, &boundsR );
489468fe
SC
1018 if ( width != NULL )
1019 *width = boundsR.right - boundsR.left;
1020 if ( height != NULL )
1021 *height = boundsR.bottom - boundsR.top;
1022 }
1023 else
1024 wxToolBarBase::DoGetSize( width, height );
1025
1026#else
1027 wxToolBarBase::DoGetSize( width, height );
1028#endif
1029}
1030
1031wxSize wxToolBar::DoGetBestSize() const
1032{
1033 int width, height;
1034
1035 DoGetSize( &width, &height );
1036
1037 return wxSize( width, height );
1038}
1039
1040void wxToolBar::SetWindowStyleFlag( long style )
1041{
1042 wxToolBarBase::SetWindowStyleFlag( style );
1043
292e5e1f 1044#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1045 if (m_macToolbar != NULL)
489468fe
SC
1046 {
1047 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
1048
1049 if ( style & wxTB_NOICONS )
1050 mode = kHIToolbarDisplayModeLabelOnly;
1051 else if ( style & wxTB_TEXT )
1052 mode = kHIToolbarDisplayModeIconAndLabel;
1053 else
1054 mode = kHIToolbarDisplayModeIconOnly;
1055
524c47aa 1056 HIToolbarSetDisplayMode( (HIToolbarRef) m_macToolbar, mode );
489468fe
SC
1057 }
1058#endif
1059}
1060
292e5e1f 1061#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1062bool wxToolBar::MacWantsNativeToolbar()
1063{
1064 return m_macUsesNativeToolbar;
1065}
1066
1067bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
1068{
1069 bool bResultV = false;
1070
1071 if (ownToolbarInstalled != NULL)
1072 *ownToolbarInstalled = false;
1073
1074 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
1075 if (tlw != NULL)
1076 {
1077 HIToolbarRef curToolbarRef = NULL;
1078 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
1079 bResultV = ((err == noErr) && (curToolbarRef != NULL));
1080 if (bResultV && (ownToolbarInstalled != NULL))
524c47aa 1081 *ownToolbarInstalled = (curToolbarRef == m_macToolbar);
489468fe
SC
1082 }
1083
1084 return bResultV;
1085}
1086
1087bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
1088{
1089 bool bResult = false;
1090
524c47aa 1091 if (usesNative && (m_macToolbar == NULL))
489468fe
SC
1092 return bResult;
1093
f0ceebb7 1094 if (usesNative && ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM)) != 0))
489468fe
SC
1095 return bResult;
1096
1097 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
1098 if (tlw == NULL)
1099 return bResult;
1100
1101 // check the existing toolbar
1102 HIToolbarRef curToolbarRef = NULL;
1103 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
1104 if (err != noErr)
1105 curToolbarRef = NULL;
1106
1107 m_macUsesNativeToolbar = usesNative;
1108
1109 if (m_macUsesNativeToolbar)
1110 {
1111 // only install toolbar if there isn't one installed already
1112 if (curToolbarRef == NULL)
1113 {
1114 bResult = true;
1115
524c47aa 1116 SetWindowToolbar( tlw, (HIToolbarRef) m_macToolbar );
bb2212e6 1117
b0184e59
RR
1118 // ShowHideWindowToolbar will make the wxFrame grow
1119 // which we don't want in this case
1120 wxSize sz = GetParent()->GetSize();
489468fe 1121 ShowHideWindowToolbar( tlw, true, false );
3d167787 1122 // Restore the original size
b0184e59 1123 GetParent()->SetSize( sz );
bb2212e6 1124
489468fe 1125 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
bb2212e6 1126
489468fe 1127 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
bb2212e6 1128
22756322 1129 GetPeer()->Move(0,0,0,0 );
489468fe 1130 SetSize( wxSIZE_AUTO_WIDTH, 0 );
22756322 1131 GetPeer()->SetVisibility( false );
489468fe
SC
1132 wxToolBarBase::Show( false );
1133 }
1134 }
1135 else
1136 {
1137 // only deinstall toolbar if this is the installed one
524c47aa 1138 if (m_macToolbar == curToolbarRef)
489468fe
SC
1139 {
1140 bResult = true;
1141
1142 ShowHideWindowToolbar( tlw, false, false );
1143 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
1665389a 1144 MacUninstallNativeToolbar();
489468fe 1145
22756322 1146 GetPeer()->SetVisibility( true );
489468fe
SC
1147 }
1148 }
1149
1150 if (bResult)
1151 InvalidateBestSize();
1152
1153// wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1154 return bResult;
1155}
1665389a
KO
1156
1157void wxToolBar::MacUninstallNativeToolbar()
1158{
1159 if (!m_macToolbar)
1160 return;
ce00f59b 1161
1665389a
KO
1162 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
1163 if (tlw)
1164 SetWindowToolbar( tlw, NULL );
1165}
489468fe
SC
1166#endif
1167
1168bool wxToolBar::Realize()
1169{
bb2212e6 1170 if ( !wxToolBarBase::Realize() )
489468fe 1171 return false;
bb2212e6 1172
b0184e59 1173 wxSize tlw_sz = GetParent()->GetSize();
489468fe
SC
1174
1175 int maxWidth = 0;
1176 int maxHeight = 0;
1177
1178 int maxToolWidth = 0;
1179 int maxToolHeight = 0;
1180
1181 int x = m_xMargin + kwxMacToolBarLeftMargin;
1182 int y = m_yMargin + kwxMacToolBarTopMargin;
1183
1184 int tw, th;
1185 GetSize( &tw, &th );
1186
1187 // find the maximum tool width and height
1188 wxToolBarTool *tool;
1189 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1190 while ( node )
1191 {
1192 tool = (wxToolBarTool *) node->GetData();
1193 if ( tool != NULL )
1194 {
1195 wxSize sz = tool->GetSize();
1196
1197 if ( sz.x > maxToolWidth )
1198 maxToolWidth = sz.x;
1199 if ( sz.y > maxToolHeight )
1200 maxToolHeight = sz.y;
1201 }
1202
1203 node = node->GetNext();
1204 }
1205
1206 bool lastIsRadio = false;
1207 bool curIsRadio = false;
1208
292e5e1f 1209#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1210 CFIndex currentPosition = 0;
1211 bool insertAll = false;
1212
524c47aa 1213 HIToolbarRef refTB = (HIToolbarRef)m_macToolbar;
b2680ced
SC
1214 wxFont f;
1215 wxFontEncoding enc;
1216 f = GetFont();
1217 if ( f.IsOk() )
1218 enc = f.GetEncoding();
1219 else
1220 enc = wxFont::GetDefaultEncoding();
489468fe
SC
1221#endif
1222
1223 node = m_tools.GetFirst();
1224 while ( node )
1225 {
1226 tool = (wxToolBarTool*) node->GetData();
1227 if ( tool == NULL )
1228 {
1229 node = node->GetNext();
1230 continue;
1231 }
1232
1233 // set tool position:
1234 // for the moment just perform a single row/column alignment
1235 wxSize cursize = tool->GetSize();
1236 if ( x + cursize.x > maxWidth )
1237 maxWidth = x + cursize.x;
1238 if ( y + cursize.y > maxHeight )
1239 maxHeight = y + cursize.y;
1240
f0ceebb7 1241 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1242 {
1243 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1244 tool->SetPosition( wxPoint(x1, y) );
1245 }
1246 else
1247 {
1248 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1249 tool->SetPosition( wxPoint(x, y1) );
1250 }
1251
1252 // update the item positioning state
f0ceebb7 1253 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1254 y += cursize.y + kwxMacToolSpacing;
1255 else
1256 x += cursize.x + kwxMacToolSpacing;
1257
292e5e1f 1258#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1259 // install in native HIToolbar
1260 if ( refTB )
1261 {
1262 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
1263 if ( hiItemRef != NULL )
1264 {
b2680ced
SC
1265 // since setting the help texts is non-virtual we have to update
1266 // the strings now
489468fe
SC
1267 if ( insertAll || (tool->GetIndex() != currentPosition) )
1268 {
1269 OSStatus err = noErr;
1270 if ( !insertAll )
1271 {
1272 insertAll = true;
1273
1274 // if this is the first tool that gets newly inserted or repositioned
1275 // first remove all 'old' tools from here to the right, because of this
1276 // all following tools will have to be reinserted (insertAll).
1277 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
1278 node2 != node;
1279 node2 = node2->GetPrevious() )
1280 {
1281 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
1282
1283 const long idx = tool2->GetIndex();
1284 if ( idx != -1 )
1285 {
1286 if ( tool2->IsControl() )
1287 {
1288 CFIndex count = CFGetRetainCount( tool2->GetControl()->GetPeer()->GetControlRef() ) ;
1289 if ( count != 3 && count != 2 )
1290 {
1291 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1292 }
1293
1294 wxASSERT( IsValidControlHandle(tool2->GetControl()->GetPeer()->GetControlRef() )) ;
1295 }
1296 err = HIToolbarRemoveItemAtIndex(refTB, idx);
1297 if ( err != noErr )
1298 {
1299 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1300 idx, (long)err);
1301 }
1302 if ( tool2->IsControl() )
1303 {
1304 CFIndex count = CFGetRetainCount( tool2->GetControl()->GetPeer()->GetControlRef() ) ;
1305 if ( count != 2 )
1306 {
1307 wxFAIL_MSG("Reference count of native tool was not 2 after removal");
1308 }
1309
1310 wxASSERT( IsValidControlHandle(tool2->GetControl()->GetPeer()->GetControlRef() )) ;
1311 }
1312
1313 tool2->SetIndex(-1);
1314 }
1315 }
1316 }
1317
1318 err = HIToolbarInsertItemAtIndex( refTB, hiItemRef, currentPosition );
1319 if (err != noErr)
1320 {
1321 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
1322 }
1323
1324 tool->SetIndex( currentPosition );
1325 if ( tool->IsControl() )
1326 {
1327 CFIndex count = CFGetRetainCount( tool->GetControl()->GetPeer()->GetControlRef() ) ;
1328 if ( count != 3 && count != 2 )
1329 {
1330 wxFAIL_MSG("Reference count of native tool was illegal before removal");
1331 }
1332 wxASSERT( IsValidControlHandle(tool->GetControl()->GetPeer()->GetControlRef() )) ;
bd044e27
VZ
1333
1334 wxString label = tool->GetLabel();
1335 if ( !label.empty() )
1336 HIToolbarItemSetLabel( hiItemRef, wxCFStringRef(label, GetFont().GetEncoding()) );
489468fe
SC
1337 }
1338 }
1339
1340 currentPosition++;
1341 }
1342 }
1343#endif
1344
1345 // update radio button (and group) state
1346 lastIsRadio = curIsRadio;
1347 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1348
1349 if ( !curIsRadio )
1350 {
1351 if ( tool->IsToggled() )
1352 DoToggleTool( tool, true );
1353 }
1354 else
1355 {
1356 if ( !lastIsRadio )
1357 {
1358 if ( tool->Toggle( true ) )
1359 {
1360 DoToggleTool( tool, true );
1361 }
1362 }
1363 else if ( tool->IsToggled() )
1364 {
1365 if ( tool->IsToggled() )
1366 DoToggleTool( tool, true );
1367
1368 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1369 while ( nodePrev )
1370 {
1371 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1372 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1373 break;
1374
1375 if ( toggleTool->Toggle( false ) )
1376 DoToggleTool( toggleTool, false );
1377
1378 nodePrev = nodePrev->GetPrevious();
1379 }
1380 }
1381 }
1382
1383 node = node->GetNext();
1384 }
1385
b0184e59
RR
1386 if (m_macUsesNativeToolbar)
1387 GetParent()->SetSize( tlw_sz );
bb2212e6 1388
f0ceebb7 1389 if ( GetWindowStyleFlag() & (wxTB_TOP|wxTB_BOTTOM) )
489468fe
SC
1390 {
1391 // if not set yet, only one row
1392 if ( m_maxRows <= 0 )
1393 SetRows( 1 );
1394
1395 m_minWidth = maxWidth;
489468fe
SC
1396 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1397 m_minHeight = m_maxHeight = maxHeight;
1398 }
1399 else
1400 {
1401 // if not set yet, have one column
1402 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1403 SetRows( GetToolsCount() );
1404
1405 m_minHeight = maxHeight;
489468fe
SC
1406 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1407 m_minWidth = m_maxWidth = maxWidth;
1408 }
1409
1410#if 0
1411 // FIXME: should this be OSX-only?
1412 {
1413 bool wantNativeToolbar, ownToolbarInstalled;
1414
1415 // attempt to install the native toolbar
f0ceebb7 1416 wantNativeToolbar = ((GetWindowStyleFlag() & (wxTB_LEFT|wxTB_BOTTOM|wxTB_RIGHT)) == 0);
489468fe
SC
1417 MacInstallNativeToolbar( wantNativeToolbar );
1418 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1419 if (!ownToolbarInstalled)
1420 {
1421 SetSize( maxWidth, maxHeight );
1422 InvalidateBestSize();
1423 }
1424 }
1425#else
1426 SetSize( maxWidth, maxHeight );
1427 InvalidateBestSize();
1428#endif
1429
1430 SetInitialSize();
1431
1432 return true;
1433}
1434
baac7154
SC
1435void wxToolBar::DoLayout()
1436{
1437 // TODO port back osx_cocoa layout solution
1438}
1439
1440void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1441{
1442 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
1443
1444 DoLayout();
1445}
1446
489468fe
SC
1447void wxToolBar::SetToolBitmapSize(const wxSize& size)
1448{
1449 m_defaultWidth = size.x + kwxMacToolBorder;
1450 m_defaultHeight = size.y + kwxMacToolBorder;
1451
292e5e1f 1452#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1453 if (m_macToolbar != NULL)
489468fe
SC
1454 {
1455 int maxs = wxMax( size.x, size.y );
1456 HIToolbarDisplaySize sizeSpec;
1457 if ( maxs > 32 )
1458 sizeSpec = kHIToolbarDisplaySizeNormal;
1459 else if ( maxs > 24 )
1460 sizeSpec = kHIToolbarDisplaySizeDefault;
1461 else
1462 sizeSpec = kHIToolbarDisplaySizeSmall;
1463
524c47aa 1464 HIToolbarSetDisplaySize( (HIToolbarRef) m_macToolbar, sizeSpec );
489468fe
SC
1465 }
1466#endif
1467}
1468
1469// The button size is bigger than the bitmap size
1470wxSize wxToolBar::GetToolSize() const
1471{
1472 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1473}
1474
1475void wxToolBar::SetRows(int nRows)
1476{
1477 // avoid resizing the frame uselessly
1478 if ( nRows != m_maxRows )
1479 m_maxRows = nRows;
1480}
1481
1482void wxToolBar::MacSuperChangedPosition()
1483{
1484 wxWindow::MacSuperChangedPosition();
1485
292e5e1f 1486#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1487 if (! m_macUsesNativeToolbar )
1488 Realize();
1489#else
1490
1491 Realize();
1492#endif
1493}
1494
1495void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1496{
5c33522f 1497 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
489468fe
SC
1498 if ( tool )
1499 {
1500 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1501
1502 tool->SetNormalBitmap(bitmap);
1503
1504 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1505 tool->UpdateToggleImage( tool->CanBeToggled() && tool->IsToggled() );
1506 }
1507}
1508
1509void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1510{
5c33522f 1511 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
489468fe
SC
1512 if ( tool )
1513 {
1514 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1515
1516 tool->SetDisabledBitmap(bitmap);
1517
1518 // TODO: what to do for this one?
1519 }
1520}
1521
1522wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1523{
1524 wxToolBarTool *tool;
1525 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1526 while ( node )
1527 {
1528 tool = (wxToolBarTool *)node->GetData();
1529 if (tool != NULL)
1530 {
1531 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1532 if ( r.Contains( wxPoint( x, y ) ) )
1533 return tool;
1534 }
1535
1536 node = node->GetNext();
1537 }
1538
d3b9f782 1539 return NULL;
489468fe
SC
1540}
1541
1542wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1543{
1544 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1545 if ( tool != NULL )
1546 return tool->GetShortHelp();
1547
1548 return wxEmptyString;
1549}
1550
2bed17c8 1551void wxToolBar::DoEnableTool(wxToolBarToolBase *WXUNUSED(t), bool WXUNUSED(enable))
489468fe 1552{
2bed17c8 1553 // everything already done in the tool's implementation
489468fe
SC
1554}
1555
a4fec5b4 1556void wxToolBar::DoToggleTool(wxToolBarToolBase *WXUNUSED(t), bool WXUNUSED(toggle))
489468fe 1557{
2bed17c8 1558 // everything already done in the tool's implementation
489468fe
SC
1559}
1560
1561bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1562{
5c33522f 1563 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
489468fe
SC
1564 if (tool == NULL)
1565 return false;
1566
1567 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1568 wxSize toolSize = GetToolSize();
1569 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
1570 ControlRef controlHandle = NULL;
1571 OSStatus err = 0;
1572
292e5e1f 1573#if wxOSX_USE_NATIVE_TOOLBAR
489468fe 1574 wxString label = tool->GetLabel();
524c47aa 1575 if (m_macToolbar && !label.empty() )
489468fe
SC
1576 {
1577 // strip mnemonics from the label for compatibility
1578 // with the usual labels in wxStaticText sense
1579 label = wxStripMenuCodes(label);
1580 }
292e5e1f 1581#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1582
1583 switch (tool->GetStyle())
1584 {
1585 case wxTOOL_STYLE_SEPARATOR:
1586 {
1587 wxASSERT( tool->GetControlHandle() == NULL );
1588 toolSize.x /= 4;
1589 toolSize.y /= 4;
f0ceebb7 1590 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1591 toolrect.bottom = toolSize.y;
1592 else
1593 toolrect.right = toolSize.x;
1594
1595 // in flat style we need a visual separator
292e5e1f 1596#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1597 if (m_macToolbar != NULL)
489468fe
SC
1598 {
1599 HIToolbarItemRef item;
1600 err = HIToolbarItemCreate(
1601 kHIToolbarSeparatorIdentifier,
1602 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1603 &item );
1604 if (err == noErr)
1605 tool->SetToolbarItemRef( item );
1606 }
1607 else
1608 err = noErr;
292e5e1f 1609#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1610
1611 CreateSeparatorControl( window, &toolrect, &controlHandle );
1612 tool->SetControlHandle( controlHandle );
1613 }
1614 break;
1615
1616 case wxTOOL_STYLE_BUTTON:
1617 {
1618 wxASSERT( tool->GetControlHandle() == NULL );
1619 ControlButtonContentInfo info;
1620 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap() );
1621
1622 if ( UMAGetSystemVersion() >= 0x1000)
1623 {
1624 // contrary to the docs this control only works with iconrefs
1625 ControlButtonContentInfo info;
1626 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
1627 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1628 wxMacReleaseBitmapButton( &info );
1629 }
1630 else
1631 {
1632 SInt16 behaviour = kControlBehaviorOffsetContents;
1633 if ( tool->CanBeToggled() )
1634 behaviour |= kControlBehaviorToggles;
1635 err = CreateBevelButtonControl( window,
1636 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1637 behaviour, &info, 0, 0, 0, &controlHandle );
1638 }
1639
292e5e1f 1640#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1641 if (m_macToolbar != NULL)
489468fe
SC
1642 {
1643 HIToolbarItemRef item;
1644 wxString labelStr = wxString::Format(wxT("%p"), tool);
1645 err = HIToolbarItemCreate(
1646 wxCFStringRef(labelStr, wxFont::GetDefaultEncoding()),
1647 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1648 if (err == noErr)
1649 {
1650 ControlButtonContentInfo info2;
1651 wxMacCreateBitmapButton( &info2, tool->GetNormalBitmap(), kControlContentCGImageRef);
1652
1653 InstallEventHandler(
1654 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1655 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
1656 HIToolbarItemSetLabel( item, wxCFStringRef(label, GetFont().GetEncoding()) );
1657 HIToolbarItemSetImage( item, info2.u.imageRef );
1658 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
1659 tool->SetToolbarItemRef( item );
1660
1661 wxMacReleaseBitmapButton( &info2 );
1662 }
1663 }
1664 else
1665 err = noErr;
292e5e1f 1666#endif // wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1667
1668 wxMacReleaseBitmapButton( &info );
1669
1670#if 0
1671 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1672 SetControlTitleWithCFString( m_controlHandle , wxCFStringRef( label, wxFont::GetDefaultEncoding() );
1673#endif
1674
1675 InstallControlEventHandler(
1676 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1677 GetEventTypeCount(eventList), eventList, tool, NULL );
1678
1679 tool->SetControlHandle( controlHandle );
1680 }
1681 break;
1682
1683 case wxTOOL_STYLE_CONTROL:
1684
292e5e1f 1685#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1686 if (m_macToolbar != NULL)
489468fe 1687 {
9a83f860 1688 wxCHECK_MSG( tool->GetControl(), false, wxT("control must be non-NULL") );
489468fe
SC
1689 HIToolbarItemRef item;
1690 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
1691 CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ;
524c47aa 1692 err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macToolbar,kControlToolbarItemClassID,
489468fe
SC
1693 data , &item ) ;
1694
1695 if (err == noErr)
1696 {
1697 tool->SetToolbarItemRef( item );
1698 }
1699 CFRelease( data ) ;
1700 }
1701 else
1702 {
1703 err = noErr;
1704 break;
1705 }
1706#else
1707 // right now there's nothing to do here
1708#endif
1709 break;
1710
1711 default:
1712 break;
1713 }
1714
1715 if ( err == noErr )
1716 {
1717 if ( controlHandle )
1718 {
1719 ControlRef container = (ControlRef) GetHandle();
1720 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1721
1722 SetControlVisibility( controlHandle, true, true );
1723 ::EmbedControl( controlHandle, container );
1724 }
1725
1726 if ( tool->CanBeToggled() && tool->IsToggled() )
1727 tool->UpdateToggleImage( true );
1728
1729 // nothing special to do here - we relayout in Realize() later
1730 InvalidateBestSize();
1731 }
1732 else
1733 {
1734 wxFAIL_MSG( wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err ) );
1735 }
1736
1737 return (err == noErr);
1738}
1739
1740void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1741{
2bed17c8 1742 // nothing to do
489468fe
SC
1743}
1744
1745bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1746{
5c33522f 1747 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
489468fe
SC
1748 wxToolBarToolsList::compatibility_iterator node;
1749 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1750 {
1751 wxToolBarToolBase *tool2 = node->GetData();
1752 if ( tool2 == tool )
1753 {
1754 // let node point to the next node in the list
1755 node = node->GetNext();
1756
1757 break;
1758 }
1759 }
1760
1761 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1762
292e5e1f 1763#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1764 CFIndex removeIndex = tool->GetIndex();
1765#endif
1766
292e5e1f 1767#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1768 if (m_macToolbar != NULL)
489468fe 1769 {
524c47aa 1770 if ( removeIndex != -1 && m_macToolbar )
489468fe 1771 {
524c47aa 1772 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macToolbar, removeIndex );
489468fe
SC
1773 tool->SetIndex( -1 );
1774 }
1775 }
1776#endif
1777
1778 tool->ClearControl();
1779
1780 // and finally reposition all the controls after this one
1781
1782 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1783 {
1784 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1785 wxPoint pt = tool2->GetPosition();
1786
f0ceebb7 1787 if ( GetWindowStyleFlag() & (wxTB_LEFT|wxTB_RIGHT) )
489468fe
SC
1788 pt.y -= sz.y;
1789 else
1790 pt.x -= sz.x;
1791
1792 tool2->SetPosition( pt );
1793
292e5e1f 1794#if wxOSX_USE_NATIVE_TOOLBAR
524c47aa 1795 if (m_macToolbar != NULL)
489468fe
SC
1796 {
1797 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1798 tool2->SetIndex( tool2->GetIndex() - 1 );
1799 }
1800#endif
1801 }
1802
1803 InvalidateBestSize();
1804
1805 return true;
1806}
1807
1808void wxToolBar::OnPaint(wxPaintEvent& event)
1809{
292e5e1f 1810#if wxOSX_USE_NATIVE_TOOLBAR
489468fe
SC
1811 if ( m_macUsesNativeToolbar )
1812 {
1813 event.Skip(true);
1814 return;
1815 }
1816#endif
1817
1818 wxPaintDC dc(this);
1819
1820 int w, h;
1821 GetSize( &w, &h );
1822
b2680ced 1823 bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL;
489468fe
SC
1824
1825 if ( !drawMetalTheme )
1826 {
1827 HIThemePlacardDrawInfo info;
1828 memset( &info, 0, sizeof(info) );
1829 info.version = 0;
1830 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1831
1832 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1833 HIRect rect = CGRectMake( 0, 0, w, h );
1834 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1835 }
1836 else
1837 {
1838 // leave the background as it is (striped or metal)
1839 }
1840
1841 event.Skip();
1842}
1843
1844#endif // wxUSE_TOOLBAR