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