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