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