]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
fixing ownership and positioning of embedded controls in native toolbars
[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 x, y;
396 x = y = 0;
397 int mac_x = position.x;
398 int mac_y = position.y;
399
400 if ( IsButton() )
401 {
402 Rect contrlRect;
403 GetControlBounds( m_controlHandle, &contrlRect );
404 int former_mac_x = contrlRect.left;
405 int former_mac_y = contrlRect.top;
406 GetToolBar()->GetToolSize();
407
408 if ( mac_x != former_mac_x || mac_y != former_mac_y )
409 {
410 UMAMoveControl( m_controlHandle, mac_x, mac_y );
411 }
412 }
413 else if ( IsControl() )
414 {
415 // embedded native controls are moved by the OS
416 #if wxMAC_USE_NATIVE_TOOLBAR
417 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
418 #endif
419 {
420 GetControl()->Move( position );
421 }
422 }
423 else
424 {
425 // separator
426 #ifdef __WXMAC_OSX__
427 Rect contrlRect;
428 GetControlBounds( m_controlHandle, &contrlRect );
429 int former_mac_x = contrlRect.left;
430 int former_mac_y = contrlRect.top;
431
432 if ( mac_x != former_mac_x || mac_y != former_mac_y )
433 UMAMoveControl( m_controlHandle, mac_x, mac_y );
434 #endif
435 }
436 }
437
438 void wxToolBarTool::UpdateToggleImage( bool toggle )
439 {
440 #if wxMAC_USE_NATIVE_TOOLBAR
441
442 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
443 #define kHIToolbarItemSelected (1 << 7)
444 #endif
445
446 // FIXME: this should be a OSX v10.4 runtime check
447 if (m_toolbarItemRef != NULL)
448 {
449 OptionBits addAttrs, removeAttrs;
450 OSStatus result;
451
452 if (toggle)
453 {
454 addAttrs = kHIToolbarItemSelected;
455 removeAttrs = kHIToolbarItemNoAttributes;
456 }
457 else
458 {
459 addAttrs = kHIToolbarItemNoAttributes;
460 removeAttrs = kHIToolbarItemSelected;
461 }
462
463 result = HIToolbarItemChangeAttributes( m_toolbarItemRef, addAttrs, removeAttrs );
464 }
465 #endif
466
467 #ifdef __WXMAC_OSX__
468 if ( toggle )
469 {
470 int w = m_bmpNormal.GetWidth();
471 int h = m_bmpNormal.GetHeight();
472 wxBitmap bmp( w, h );
473 wxMemoryDC dc;
474
475 dc.SelectObject( bmp );
476 dc.SetPen( wxNullPen );
477 dc.SetBackground( *wxWHITE );
478 dc.DrawRectangle( 0, 0, w, h );
479 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
480 dc.SelectObject( wxNullBitmap );
481 ControlButtonContentInfo info;
482 wxMacCreateBitmapButton( &info, bmp );
483 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
484 wxMacReleaseBitmapButton( &info );
485 }
486 else
487 {
488 ControlButtonContentInfo info;
489 wxMacCreateBitmapButton( &info, m_bmpNormal );
490 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
491 wxMacReleaseBitmapButton( &info );
492 }
493
494 IconTransformType transform = toggle ? kTransformSelected : kTransformNone;
495 SetControlData(
496 m_controlHandle, 0, kControlIconTransformTag,
497 sizeof(transform), (Ptr)&transform );
498 HIViewSetNeedsDisplay( m_controlHandle, true );
499
500 #else
501 ::SetControl32BitValue( m_controlHandle, toggle );
502 #endif
503 }
504
505 wxToolBarTool::wxToolBarTool(
506 wxToolBar *tbar,
507 int id,
508 const wxString& label,
509 const wxBitmap& bmpNormal,
510 const wxBitmap& bmpDisabled,
511 wxItemKind kind,
512 wxObject *clientData,
513 const wxString& shortHelp,
514 const wxString& longHelp )
515 :
516 wxToolBarToolBase(
517 tbar, id, label, bmpNormal, bmpDisabled, kind,
518 clientData, shortHelp, longHelp )
519 {
520 Init();
521 }
522
523 #pragma mark -
524 #pragma mark Toolbar Implementation
525
526 wxToolBarToolBase *wxToolBar::CreateTool(
527 int id,
528 const wxString& label,
529 const wxBitmap& bmpNormal,
530 const wxBitmap& bmpDisabled,
531 wxItemKind kind,
532 wxObject *clientData,
533 const wxString& shortHelp,
534 const wxString& longHelp )
535 {
536 return new wxToolBarTool(
537 this, id, label, bmpNormal, bmpDisabled, kind,
538 clientData, shortHelp, longHelp );
539 }
540
541 wxToolBarToolBase * wxToolBar::CreateTool( wxControl *control )
542 {
543 return new wxToolBarTool( this, control );
544 }
545
546 void wxToolBar::Init()
547 {
548 m_maxWidth = -1;
549 m_maxHeight = -1;
550 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
551 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
552
553 #if wxMAC_USE_NATIVE_TOOLBAR
554 m_macHIToolbarRef = NULL;
555 m_macUsesNativeToolbar = false;
556 #endif
557 }
558
559 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
560
561 const EventTypeSpec kEvents[] =
562 {
563 { kEventClassHIObject, kEventHIObjectConstruct },
564 { kEventClassHIObject, kEventHIObjectInitialize },
565 { kEventClassHIObject, kEventHIObjectDestruct },
566
567 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
568 };
569
570 const EventTypeSpec kViewEvents[] =
571 {
572 { kEventClassControl, kEventControlGetSizeConstraints }
573 };
574
575 struct ControlToolbarItem
576 {
577 HIToolbarItemRef toolbarItem;
578 HIViewRef viewRef;
579 wxSize lastValidSize ;
580 };
581
582 static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
583 {
584 OSStatus result = eventNotHandledErr;
585 ControlToolbarItem* object = (ControlToolbarItem*)inUserData;
586
587 switch ( GetEventClass( inEvent ) )
588 {
589 case kEventClassHIObject:
590 switch ( GetEventKind( inEvent ) )
591 {
592 case kEventHIObjectConstruct:
593 {
594 HIObjectRef toolbarItem;
595 ControlToolbarItem* item;
596
597 GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL,
598 sizeof( HIObjectRef ), NULL, &toolbarItem );
599
600 item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ;
601 item->toolbarItem = toolbarItem ;
602 item->viewRef = NULL ;
603
604 SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item );
605
606 result = noErr ;
607 }
608 break;
609
610 case kEventHIObjectInitialize:
611 result = CallNextEventHandler( inCallRef, inEvent );
612 if ( result == noErr )
613 {
614 CFDataRef data;
615 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
616 sizeof( CFTypeRef ), NULL, &data );
617
618 HIViewRef viewRef ;
619
620 wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ;
621 memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ;
622
623 object->viewRef = (HIViewRef) viewRef ;
624
625 result = noErr ;
626 }
627 break;
628
629 case kEventHIObjectDestruct:
630 free( object ) ;
631 result = noErr;
632 break;
633 }
634 break;
635
636 case kEventClassToolbarItem:
637 switch ( GetEventKind( inEvent ) )
638 {
639 case kEventToolbarItemCreateCustomView:
640 {
641 HIViewRef viewRef = object->viewRef ;
642
643 HIViewRemoveFromSuperview( viewRef ) ;
644 HIViewSetVisible(viewRef, true) ;
645 InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler,
646 GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL );
647
648 result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef );
649 }
650 break;
651 }
652 break;
653
654 case kEventClassControl:
655 switch ( GetEventKind( inEvent ) )
656 {
657 case kEventControlGetSizeConstraints:
658 {
659 wxWindow* wxwindow = wxFindControlFromMacControl(object->viewRef ) ;
660 if ( wxwindow )
661 {
662 wxSize sz = wxwindow->GetSize() ;
663 sz.x -= wxwindow->MacGetLeftBorderSize() + wxwindow->MacGetRightBorderSize();
664 sz.y -= wxwindow->MacGetTopBorderSize() + wxwindow->MacGetBottomBorderSize();
665 // during toolbar layout the native window sometimes gets negative sizes
666 // so we always keep the last valid size here, to make sure we survive the
667 // shuffle ...
668 if ( sz.x > 0 && sz.y > 0 )
669 object->lastValidSize = sz ;
670 else
671 sz = object->lastValidSize ;
672
673 HISize min, max;
674 min.width = max.width = sz.x ;
675 min.height = max.height = sz.y ;
676
677 result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize,
678 sizeof( HISize ), &min );
679
680 result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize,
681 sizeof( HISize ), &max );
682 result = noErr ;
683 }
684 }
685 break;
686 }
687 break;
688 }
689
690 return result;
691 }
692
693 void RegisterControlToolbarItemClass()
694 {
695 static bool sRegistered;
696
697 if ( !sRegistered )
698 {
699 HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0,
700 ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL );
701
702 sRegistered = true;
703 }
704 }
705
706 HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData)
707 {
708 RegisterControlToolbarItemClass();
709
710 OSStatus err;
711 EventRef event;
712 UInt32 options = kHIToolbarItemAllowDuplicates;
713 HIToolbarItemRef result = NULL;
714
715 err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
716 require_noerr( err, CantCreateEvent );
717
718 SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options );
719 SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier );
720
721 if ( inConfigData )
722 SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData );
723
724 err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result );
725 check_noerr( err );
726
727 ReleaseEvent( event );
728 CantCreateEvent :
729 return result ;
730 }
731
732 static const EventTypeSpec kToolbarEvents[] =
733 {
734 { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
735 { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
736 { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
737 };
738
739 static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
740 {
741 OSStatus result = eventNotHandledErr;
742 // Not yet needed
743 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
744 CFMutableArrayRef array;
745
746 switch ( GetEventKind( inEvent ) )
747 {
748 case kEventToolbarGetDefaultIdentifiers:
749 {
750 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
751 sizeof( CFMutableArrayRef ), NULL, &array );
752 // not implemented yet
753 // GetToolbarDefaultItems( array );
754 result = noErr;
755 }
756 break;
757
758 case kEventToolbarGetAllowedIdentifiers:
759 {
760 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
761 sizeof( CFMutableArrayRef ), NULL, &array );
762 // not implemented yet
763 // GetToolbarAllowedItems( array );
764 result = noErr;
765 }
766 break;
767 case kEventToolbarCreateItemWithIdentifier:
768 {
769 HIToolbarItemRef item = NULL;
770 CFTypeRef data = NULL;
771 CFStringRef identifier = NULL ;
772
773 GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL,
774 sizeof( CFStringRef ), NULL, &identifier );
775
776 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
777 sizeof( CFTypeRef ), NULL, &data );
778
779 if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo )
780 {
781 item = CreateControlToolbarItem( kControlToolbarItemClassID, data );
782 if ( item )
783 {
784 SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef,
785 sizeof( HIToolbarItemRef ), &item );
786 result = noErr;
787 }
788 }
789
790 }
791 break;
792 }
793 return result ;
794 }
795
796 // also for the toolbar we have the dual implementation:
797 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
798
799 bool wxToolBar::Create(
800 wxWindow *parent,
801 wxWindowID id,
802 const wxPoint& pos,
803 const wxSize& size,
804 long style,
805 const wxString& name )
806 {
807 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
808 return false;
809
810 OSStatus err = noErr;
811
812 #if wxMAC_USE_NATIVE_TOOLBAR
813 wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
814 err = HIToolbarCreate(
815 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
816 (HIToolbarRef*) &m_macHIToolbarRef );
817
818 if (m_macHIToolbarRef != NULL)
819 {
820 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler,
821 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
822
823 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
824 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
825
826 if ( style & wxTB_NOICONS )
827 mode = kHIToolbarDisplayModeLabelOnly;
828 else if ( style & wxTB_TEXT )
829 mode = kHIToolbarDisplayModeIconAndLabel;
830 else
831 mode = kHIToolbarDisplayModeIconOnly;
832
833 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
834 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
835 }
836 #endif
837
838 return (err == noErr);
839 }
840
841 wxToolBar::~wxToolBar()
842 {
843 #if wxMAC_USE_NATIVE_TOOLBAR
844 if (m_macHIToolbarRef != NULL)
845 {
846 // if this is the installed toolbar, then deinstall it
847 if (m_macUsesNativeToolbar)
848 MacInstallNativeToolbar( false );
849
850 CFRelease( (HIToolbarRef)m_macHIToolbarRef );
851 m_macHIToolbarRef = NULL;
852 }
853 #endif
854 }
855
856 bool wxToolBar::Show( bool show )
857 {
858 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
859 bool bResult = (tlw != NULL);
860
861 if (bResult)
862 {
863 #if wxMAC_USE_NATIVE_TOOLBAR
864 bool ownToolbarInstalled = false;
865 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
866 if (ownToolbarInstalled)
867 {
868 bResult = (IsWindowToolbarVisible( tlw ) != show);
869 if ( bResult )
870 ShowHideWindowToolbar( tlw, show, false );
871 }
872 else
873 bResult = wxToolBarBase::Show( show );
874 #else
875
876 bResult = wxToolBarBase::Show( show );
877 #endif
878 }
879
880 return bResult;
881 }
882
883 bool wxToolBar::IsShown() const
884 {
885 bool bResult;
886
887 #if wxMAC_USE_NATIVE_TOOLBAR
888 bool ownToolbarInstalled;
889
890 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
891 if (ownToolbarInstalled)
892 {
893 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
894 bResult = IsWindowToolbarVisible( tlw );
895 }
896 else
897 bResult = wxToolBarBase::IsShown();
898 #else
899
900 bResult = wxToolBarBase::IsShown();
901 #endif
902
903 return bResult;
904 }
905
906 void wxToolBar::DoGetSize( int *width, int *height ) const
907 {
908 #if wxMAC_USE_NATIVE_TOOLBAR
909 Rect boundsR;
910 bool ownToolbarInstalled;
911
912 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
913 if ( ownToolbarInstalled )
914 {
915 // TODO: is this really a control ?
916 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
917 if ( width != NULL )
918 *width = boundsR.right - boundsR.left;
919 if ( height != NULL )
920 *height = boundsR.bottom - boundsR.top;
921 }
922 else
923 wxToolBarBase::DoGetSize( width, height );
924
925 #else
926 wxToolBarBase::DoGetSize( width, height );
927 #endif
928 }
929
930 wxSize wxToolBar::DoGetBestSize() const
931 {
932 int width, height;
933
934 DoGetSize( &width, &height );
935
936 return wxSize( width, height );
937 }
938
939 void wxToolBar::SetWindowStyleFlag( long style )
940 {
941 wxToolBarBase::SetWindowStyleFlag( style );
942
943 #if wxMAC_USE_NATIVE_TOOLBAR
944 if (m_macHIToolbarRef != NULL)
945 {
946 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
947
948 if ( style & wxTB_NOICONS )
949 mode = kHIToolbarDisplayModeLabelOnly;
950 else if ( style & wxTB_TEXT )
951 mode = kHIToolbarDisplayModeIconAndLabel;
952 else
953 mode = kHIToolbarDisplayModeIconOnly;
954
955 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
956 }
957 #endif
958 }
959
960 #if wxMAC_USE_NATIVE_TOOLBAR
961 bool wxToolBar::MacWantsNativeToolbar()
962 {
963 return m_macUsesNativeToolbar;
964 }
965
966 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
967 {
968 bool bResultV = false;
969
970 if (ownToolbarInstalled != NULL)
971 *ownToolbarInstalled = false;
972
973 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
974 if (tlw != NULL)
975 {
976 HIToolbarRef curToolbarRef = NULL;
977 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
978 bResultV = ((err == noErr) && (curToolbarRef != NULL));
979 if (bResultV && (ownToolbarInstalled != NULL))
980 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
981 }
982
983 return bResultV;
984 }
985
986 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
987 {
988 bool bResult = false;
989
990 if (usesNative && (m_macHIToolbarRef == NULL))
991 return bResult;
992
993 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
994 return bResult;
995
996 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
997 if (tlw == NULL)
998 return bResult;
999
1000 // check the existing toolbar
1001 HIToolbarRef curToolbarRef = NULL;
1002 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
1003 if (err != noErr)
1004 curToolbarRef = NULL;
1005
1006 m_macUsesNativeToolbar = usesNative;
1007
1008 if (m_macUsesNativeToolbar)
1009 {
1010 // only install toolbar if there isn't one installed already
1011 if (curToolbarRef == NULL)
1012 {
1013 bResult = true;
1014
1015 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
1016 ShowHideWindowToolbar( tlw, true, false );
1017 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
1018 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
1019
1020 Rect r = { 0, 0, 0, 0 };
1021 m_peer->SetRect( &r );
1022 SetSize( wxSIZE_AUTO_WIDTH, 0 );
1023 m_peer->SetVisibility( false, true );
1024 wxToolBarBase::Show( false );
1025 }
1026 }
1027 else
1028 {
1029 // only deinstall toolbar if this is the installed one
1030 if (m_macHIToolbarRef == curToolbarRef)
1031 {
1032 bResult = true;
1033
1034 ShowHideWindowToolbar( tlw, false, false );
1035 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
1036 SetWindowToolbar( tlw, NULL );
1037
1038 m_peer->SetVisibility( true, true );
1039 }
1040 }
1041
1042 if (bResult)
1043 InvalidateBestSize();
1044
1045 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1046 return bResult;
1047 }
1048 #endif
1049
1050 bool wxToolBar::Realize()
1051 {
1052 if (m_tools.GetCount() == 0)
1053 return false;
1054
1055 int maxWidth = 0;
1056 int maxHeight = 0;
1057
1058 int maxToolWidth = 0;
1059 int maxToolHeight = 0;
1060
1061 int x = m_xMargin + kwxMacToolBarLeftMargin;
1062 int y = m_yMargin + kwxMacToolBarTopMargin;
1063
1064 int tw, th;
1065 GetSize( &tw, &th );
1066
1067 // find the maximum tool width and height
1068 wxToolBarTool *tool;
1069 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1070 while ( node != NULL )
1071 {
1072 tool = (wxToolBarTool *) node->GetData();
1073 if ( tool != NULL )
1074 {
1075 wxSize sz = tool->GetSize();
1076
1077 if ( sz.x > maxToolWidth )
1078 maxToolWidth = sz.x;
1079 if ( sz.y > maxToolHeight )
1080 maxToolHeight = sz.y;
1081 }
1082
1083 node = node->GetNext();
1084 }
1085
1086 bool lastIsRadio = false;
1087 bool curIsRadio = false;
1088 bool setChoiceInGroup = false;
1089
1090 #if wxMAC_USE_NATIVE_TOOLBAR
1091 CFIndex currentPosition = 0;
1092 bool insertAll = false;
1093 #endif
1094
1095 node = m_tools.GetFirst();
1096 while ( node != NULL )
1097 {
1098 tool = (wxToolBarTool*) node->GetData();
1099 if ( tool == NULL )
1100 {
1101 node = node->GetNext();
1102 continue;
1103 }
1104
1105 // set tool position:
1106 // for the moment just perform a single row/column alignment
1107 wxSize cursize = tool->GetSize();
1108 if ( x + cursize.x > maxWidth )
1109 maxWidth = x + cursize.x;
1110 if ( y + cursize.y > maxHeight )
1111 maxHeight = y + cursize.y;
1112
1113 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1114 {
1115 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1116 tool->SetPosition( wxPoint(x1, y) );
1117 }
1118 else
1119 {
1120 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1121 tool->SetPosition( wxPoint(x, y1) );
1122 }
1123
1124 // update the item positioning state
1125 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1126 y += cursize.y + kwxMacToolSpacing;
1127 else
1128 x += cursize.x + kwxMacToolSpacing;
1129
1130 #if wxMAC_USE_NATIVE_TOOLBAR
1131 // install in native HIToolbar
1132 if ( m_macHIToolbarRef != NULL )
1133 {
1134 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
1135 if ( hiItemRef != NULL )
1136 {
1137 if ( insertAll || (tool->GetIndex() != currentPosition) )
1138 {
1139 OSStatus err = noErr;
1140 if ( !insertAll )
1141 {
1142 insertAll = true;
1143
1144 // if this is the first tool that gets newly inserted or repositioned
1145 // first remove all 'old' tools from here to the right, because of this
1146 // all following tools will have to be reinserted (insertAll). i = 100 because there's
1147 // no way to determine how many there are in a toolbar, so just a high number :-(
1148 for ( CFIndex i = 100; i >= currentPosition; --i )
1149 {
1150 err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i );
1151 }
1152
1153 if (err != noErr)
1154 {
1155 wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err );
1156 wxFAIL_MSG( errMsg.c_str() );
1157 }
1158 }
1159
1160 err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition );
1161 if (err != noErr)
1162 {
1163 wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
1164 wxFAIL_MSG( errMsg.c_str() );
1165 }
1166
1167 tool->SetIndex( currentPosition );
1168 }
1169
1170 currentPosition++;
1171 }
1172 }
1173 #endif
1174
1175 // update radio button (and group) state
1176 lastIsRadio = curIsRadio;
1177 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1178
1179 if ( !curIsRadio )
1180 {
1181 if ( tool->IsToggled() )
1182 DoToggleTool( tool, true );
1183
1184 setChoiceInGroup = false;
1185 }
1186 else
1187 {
1188 if ( !lastIsRadio )
1189 {
1190 if ( tool->Toggle( true ) )
1191 {
1192 DoToggleTool( tool, true );
1193 setChoiceInGroup = 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 SetBestFittingSize();
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 switch ( tool->GetStyle() )
1539 {
1540 case wxTOOL_STYLE_CONTROL:
1541 {
1542 tool->GetControl()->Destroy();
1543 tool->ClearControl();
1544 }
1545 break;
1546
1547 case wxTOOL_STYLE_BUTTON:
1548 case wxTOOL_STYLE_SEPARATOR:
1549 if ( tool->GetControlHandle() )
1550 {
1551 #if wxMAC_USE_NATIVE_TOOLBAR
1552 if ( removeIndex != -1 && m_macHIToolbarRef )
1553 {
1554 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
1555 tool->SetIndex( -1 );
1556 }
1557 #endif
1558
1559 tool->ClearControl();
1560 }
1561 break;
1562
1563 default:
1564 break;
1565 }
1566
1567 // and finally reposition all the controls after this one
1568
1569 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1570 {
1571 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1572 wxPoint pt = tool2->GetPosition();
1573
1574 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1575 pt.y -= sz.y;
1576 else
1577 pt.x -= sz.x;
1578
1579 tool2->SetPosition( pt );
1580
1581 #if wxMAC_USE_NATIVE_TOOLBAR
1582 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1583 tool2->SetIndex( tool2->GetIndex() - 1 );
1584 #endif
1585 }
1586
1587 InvalidateBestSize();
1588
1589 return true;
1590 }
1591
1592 void wxToolBar::OnPaint(wxPaintEvent& event)
1593 {
1594 #if wxMAC_USE_NATIVE_TOOLBAR
1595 if ( m_macUsesNativeToolbar )
1596 {
1597 event.Skip(true);
1598 return;
1599 }
1600 #endif
1601
1602 wxPaintDC dc(this);
1603
1604 int w, h;
1605 GetSize( &w, &h );
1606
1607 bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance();
1608 bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);
1609
1610 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1611 if ( !drawMetalTheme && minimumUmaAvailable )
1612 {
1613 HIThemePlacardDrawInfo info;
1614 memset( &info, 0, sizeof(info) );
1615 info.version = 0;
1616 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1617
1618 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1619 HIRect rect = CGRectMake( 0, 0, w, h );
1620 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1621 }
1622 else
1623 {
1624 // leave the background as it is (striped or metal)
1625 }
1626
1627 #else
1628
1629 const bool drawBorder = true;
1630
1631 if (drawBorder)
1632 {
1633 wxMacPortSetter helper( &dc );
1634
1635 if ( !drawMetalTheme || !minimumUmaAvailable )
1636 {
1637 Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1638 dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };
1639
1640 #if 0
1641 if ( toolbarrect.left < 0 )
1642 toolbarrect.left = 0;
1643 if ( toolbarrect.top < 0 )
1644 toolbarrect.top = 0;
1645 #endif
1646
1647 UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive );
1648 }
1649 else
1650 {
1651 #if TARGET_API_MAC_OSX
1652 HIRect hiToolbarrect = CGRectMake(
1653 dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1654 dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) );
1655 CGContextRef cgContext;
1656 Rect bounds;
1657
1658 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
1659 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1660
1661 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
1662 CGContextScaleCTM( cgContext, 1, -1 );
1663
1664 HIThemeBackgroundDrawInfo drawInfo;
1665 drawInfo.version = 0;
1666 drawInfo.state = kThemeStateActive;
1667 drawInfo.kind = kThemeBackgroundMetal;
1668 HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );
1669
1670 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1671 #endif
1672 }
1673 }
1674 #endif
1675
1676 event.Skip();
1677 }
1678
1679 #endif // wxUSE_TOOLBAR