]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
ensure that wxToolBar has one of wxTB_TOP/LEFT/RIGHT/BOTTOM styles set, otherwise...
[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 FixupStyle();
796
797 OSStatus err = noErr;
798
799 #if wxMAC_USE_NATIVE_TOOLBAR
800 wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
801 err = HIToolbarCreate(
802 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
803 (HIToolbarRef*) &m_macHIToolbarRef );
804
805 if (m_macHIToolbarRef != NULL)
806 {
807 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler,
808 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
809
810 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
811 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
812
813 if ( style & wxTB_NOICONS )
814 mode = kHIToolbarDisplayModeLabelOnly;
815 else if ( style & wxTB_TEXT )
816 mode = kHIToolbarDisplayModeIconAndLabel;
817 else
818 mode = kHIToolbarDisplayModeIconOnly;
819
820 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
821 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
822 }
823 #endif
824
825 return (err == noErr);
826 }
827
828 wxToolBar::~wxToolBar()
829 {
830 #if wxMAC_USE_NATIVE_TOOLBAR
831 if (m_macHIToolbarRef != NULL)
832 {
833 // if this is the installed toolbar, then deinstall it
834 if (m_macUsesNativeToolbar)
835 MacInstallNativeToolbar( false );
836
837 CFRelease( (HIToolbarRef)m_macHIToolbarRef );
838 m_macHIToolbarRef = NULL;
839 }
840 #endif
841 }
842
843 bool wxToolBar::Show( bool show )
844 {
845 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
846 bool bResult = (tlw != NULL);
847
848 if (bResult)
849 {
850 #if wxMAC_USE_NATIVE_TOOLBAR
851 bool ownToolbarInstalled = false;
852 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
853 if (ownToolbarInstalled)
854 {
855 bResult = (IsWindowToolbarVisible( tlw ) != show);
856 if ( bResult )
857 ShowHideWindowToolbar( tlw, show, false );
858 }
859 else
860 bResult = wxToolBarBase::Show( show );
861 #else
862
863 bResult = wxToolBarBase::Show( show );
864 #endif
865 }
866
867 return bResult;
868 }
869
870 bool wxToolBar::IsShown() const
871 {
872 bool bResult;
873
874 #if wxMAC_USE_NATIVE_TOOLBAR
875 bool ownToolbarInstalled;
876
877 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
878 if (ownToolbarInstalled)
879 {
880 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
881 bResult = IsWindowToolbarVisible( tlw );
882 }
883 else
884 bResult = wxToolBarBase::IsShown();
885 #else
886
887 bResult = wxToolBarBase::IsShown();
888 #endif
889
890 return bResult;
891 }
892
893 void wxToolBar::DoGetSize( int *width, int *height ) const
894 {
895 #if wxMAC_USE_NATIVE_TOOLBAR
896 Rect boundsR;
897 bool ownToolbarInstalled;
898
899 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
900 if ( ownToolbarInstalled )
901 {
902 // TODO: is this really a control ?
903 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
904 if ( width != NULL )
905 *width = boundsR.right - boundsR.left;
906 if ( height != NULL )
907 *height = boundsR.bottom - boundsR.top;
908 }
909 else
910 wxToolBarBase::DoGetSize( width, height );
911
912 #else
913 wxToolBarBase::DoGetSize( width, height );
914 #endif
915 }
916
917 wxSize wxToolBar::DoGetBestSize() const
918 {
919 int width, height;
920
921 DoGetSize( &width, &height );
922
923 return wxSize( width, height );
924 }
925
926 void wxToolBar::SetWindowStyleFlag( long style )
927 {
928 wxToolBarBase::SetWindowStyleFlag( style );
929
930 #if wxMAC_USE_NATIVE_TOOLBAR
931 if (m_macHIToolbarRef != NULL)
932 {
933 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
934
935 if ( style & wxTB_NOICONS )
936 mode = kHIToolbarDisplayModeLabelOnly;
937 else if ( style & wxTB_TEXT )
938 mode = kHIToolbarDisplayModeIconAndLabel;
939 else
940 mode = kHIToolbarDisplayModeIconOnly;
941
942 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
943 }
944 #endif
945 }
946
947 #if wxMAC_USE_NATIVE_TOOLBAR
948 bool wxToolBar::MacWantsNativeToolbar()
949 {
950 return m_macUsesNativeToolbar;
951 }
952
953 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
954 {
955 bool bResultV = false;
956
957 if (ownToolbarInstalled != NULL)
958 *ownToolbarInstalled = false;
959
960 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
961 if (tlw != NULL)
962 {
963 HIToolbarRef curToolbarRef = NULL;
964 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
965 bResultV = ((err == noErr) && (curToolbarRef != NULL));
966 if (bResultV && (ownToolbarInstalled != NULL))
967 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
968 }
969
970 return bResultV;
971 }
972
973 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
974 {
975 bool bResult = false;
976
977 if (usesNative && (m_macHIToolbarRef == NULL))
978 return bResult;
979
980 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
981 return bResult;
982
983 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
984 if (tlw == NULL)
985 return bResult;
986
987 // check the existing toolbar
988 HIToolbarRef curToolbarRef = NULL;
989 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
990 if (err != noErr)
991 curToolbarRef = NULL;
992
993 m_macUsesNativeToolbar = usesNative;
994
995 if (m_macUsesNativeToolbar)
996 {
997 // only install toolbar if there isn't one installed already
998 if (curToolbarRef == NULL)
999 {
1000 bResult = true;
1001
1002 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
1003 ShowHideWindowToolbar( tlw, true, false );
1004 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
1005 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
1006
1007 Rect r = { 0, 0, 0, 0 };
1008 m_peer->SetRect( &r );
1009 SetSize( wxSIZE_AUTO_WIDTH, 0 );
1010 m_peer->SetVisibility( false, true );
1011 wxToolBarBase::Show( false );
1012 }
1013 }
1014 else
1015 {
1016 // only deinstall toolbar if this is the installed one
1017 if (m_macHIToolbarRef == curToolbarRef)
1018 {
1019 bResult = true;
1020
1021 ShowHideWindowToolbar( tlw, false, false );
1022 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
1023 SetWindowToolbar( tlw, NULL );
1024
1025 m_peer->SetVisibility( true, true );
1026 }
1027 }
1028
1029 if (bResult)
1030 InvalidateBestSize();
1031
1032 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1033 return bResult;
1034 }
1035 #endif
1036
1037 bool wxToolBar::Realize()
1038 {
1039 if (m_tools.GetCount() == 0)
1040 return false;
1041
1042 int maxWidth = 0;
1043 int maxHeight = 0;
1044
1045 int maxToolWidth = 0;
1046 int maxToolHeight = 0;
1047
1048 int x = m_xMargin + kwxMacToolBarLeftMargin;
1049 int y = m_yMargin + kwxMacToolBarTopMargin;
1050
1051 int tw, th;
1052 GetSize( &tw, &th );
1053
1054 // find the maximum tool width and height
1055 wxToolBarTool *tool;
1056 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1057 while ( node != NULL )
1058 {
1059 tool = (wxToolBarTool *) node->GetData();
1060 if ( tool != NULL )
1061 {
1062 wxSize sz = tool->GetSize();
1063
1064 if ( sz.x > maxToolWidth )
1065 maxToolWidth = sz.x;
1066 if ( sz.y > maxToolHeight )
1067 maxToolHeight = sz.y;
1068 }
1069
1070 node = node->GetNext();
1071 }
1072
1073 bool lastIsRadio = false;
1074 bool curIsRadio = false;
1075
1076 #if wxMAC_USE_NATIVE_TOOLBAR
1077 CFIndex currentPosition = 0;
1078 bool insertAll = false;
1079 #endif
1080
1081 node = m_tools.GetFirst();
1082 while ( node != NULL )
1083 {
1084 tool = (wxToolBarTool*) node->GetData();
1085 if ( tool == NULL )
1086 {
1087 node = node->GetNext();
1088 continue;
1089 }
1090
1091 // set tool position:
1092 // for the moment just perform a single row/column alignment
1093 wxSize cursize = tool->GetSize();
1094 if ( x + cursize.x > maxWidth )
1095 maxWidth = x + cursize.x;
1096 if ( y + cursize.y > maxHeight )
1097 maxHeight = y + cursize.y;
1098
1099 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1100 {
1101 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1102 tool->SetPosition( wxPoint(x1, y) );
1103 }
1104 else
1105 {
1106 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1107 tool->SetPosition( wxPoint(x, y1) );
1108 }
1109
1110 // update the item positioning state
1111 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1112 y += cursize.y + kwxMacToolSpacing;
1113 else
1114 x += cursize.x + kwxMacToolSpacing;
1115
1116 #if wxMAC_USE_NATIVE_TOOLBAR
1117 // install in native HIToolbar
1118 if ( m_macHIToolbarRef != NULL )
1119 {
1120 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
1121 if ( hiItemRef != NULL )
1122 {
1123 if ( insertAll || (tool->GetIndex() != currentPosition) )
1124 {
1125 OSStatus err = noErr;
1126 if ( !insertAll )
1127 {
1128 insertAll = true;
1129
1130 // if this is the first tool that gets newly inserted or repositioned
1131 // first remove all 'old' tools from here to the right, because of this
1132 // all following tools will have to be reinserted (insertAll). i = 100 because there's
1133 // no way to determine how many there are in a toolbar, so just a high number :-(
1134 for ( CFIndex i = 100; i >= currentPosition; --i )
1135 {
1136 err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i );
1137 }
1138
1139 if (err != noErr)
1140 {
1141 wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err );
1142 wxFAIL_MSG( errMsg.c_str() );
1143 }
1144 }
1145
1146 err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition );
1147 if (err != noErr)
1148 {
1149 wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
1150 wxFAIL_MSG( errMsg.c_str() );
1151 }
1152
1153 tool->SetIndex( currentPosition );
1154 }
1155
1156 currentPosition++;
1157 }
1158 }
1159 #endif
1160
1161 // update radio button (and group) state
1162 lastIsRadio = curIsRadio;
1163 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1164
1165 if ( !curIsRadio )
1166 {
1167 if ( tool->IsToggled() )
1168 DoToggleTool( tool, true );
1169 }
1170 else
1171 {
1172 if ( !lastIsRadio )
1173 {
1174 if ( tool->Toggle( true ) )
1175 {
1176 DoToggleTool( tool, true );
1177 }
1178 }
1179 else if ( tool->IsToggled() )
1180 {
1181 if ( tool->IsToggled() )
1182 DoToggleTool( tool, true );
1183
1184 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1185 while ( nodePrev != NULL )
1186 {
1187 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1188 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1189 break;
1190
1191 if ( toggleTool->Toggle( false ) )
1192 DoToggleTool( toggleTool, false );
1193
1194 nodePrev = nodePrev->GetPrevious();
1195 }
1196 }
1197 }
1198
1199 node = node->GetNext();
1200 }
1201
1202 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
1203 {
1204 // if not set yet, only one row
1205 if ( m_maxRows <= 0 )
1206 SetRows( 1 );
1207
1208 m_minWidth = maxWidth;
1209 maxWidth = tw;
1210 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1211 m_minHeight = m_maxHeight = maxHeight;
1212 }
1213 else
1214 {
1215 // if not set yet, have one column
1216 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1217 SetRows( GetToolsCount() );
1218
1219 m_minHeight = maxHeight;
1220 maxHeight = th;
1221 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1222 m_minWidth = m_maxWidth = maxWidth;
1223 }
1224
1225 #if 0
1226 // FIXME: should this be OSX-only?
1227 {
1228 bool wantNativeToolbar, ownToolbarInstalled;
1229
1230 // attempt to install the native toolbar
1231 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1232 MacInstallNativeToolbar( wantNativeToolbar );
1233 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1234 if (!ownToolbarInstalled)
1235 {
1236 SetSize( maxWidth, maxHeight );
1237 InvalidateBestSize();
1238 }
1239 }
1240 #else
1241 SetSize( maxWidth, maxHeight );
1242 InvalidateBestSize();
1243 #endif
1244
1245 SetInitialSize();
1246
1247 return true;
1248 }
1249
1250 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1251 {
1252 m_defaultWidth = size.x + kwxMacToolBorder;
1253 m_defaultHeight = size.y + kwxMacToolBorder;
1254
1255 #if wxMAC_USE_NATIVE_TOOLBAR
1256 if (m_macHIToolbarRef != NULL)
1257 {
1258 int maxs = wxMax( size.x, size.y );
1259 HIToolbarDisplaySize sizeSpec;
1260 if ( maxs > 32 )
1261 sizeSpec = kHIToolbarDisplaySizeNormal;
1262 else if ( maxs > 24 )
1263 sizeSpec = kHIToolbarDisplaySizeDefault;
1264 else
1265 sizeSpec = kHIToolbarDisplaySizeSmall;
1266
1267 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
1268 }
1269 #endif
1270 }
1271
1272 // The button size is bigger than the bitmap size
1273 wxSize wxToolBar::GetToolSize() const
1274 {
1275 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1276 }
1277
1278 void wxToolBar::SetRows(int nRows)
1279 {
1280 // avoid resizing the frame uselessly
1281 if ( nRows != m_maxRows )
1282 m_maxRows = nRows;
1283 }
1284
1285 void wxToolBar::MacSuperChangedPosition()
1286 {
1287 wxWindow::MacSuperChangedPosition();
1288
1289 #if wxMAC_USE_NATIVE_TOOLBAR
1290 if (! m_macUsesNativeToolbar )
1291 Realize();
1292 #else
1293
1294 Realize();
1295 #endif
1296 }
1297
1298 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1299 {
1300 wxToolBarTool *tool;
1301 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1302 while ( node != NULL )
1303 {
1304 tool = (wxToolBarTool *)node->GetData();
1305 if (tool != NULL)
1306 {
1307 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1308 if ( r.Contains( wxPoint( x, y ) ) )
1309 return tool;
1310 }
1311
1312 node = node->GetNext();
1313 }
1314
1315 return (wxToolBarToolBase*)NULL;
1316 }
1317
1318 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1319 {
1320 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1321 if ( tool != NULL )
1322 return tool->GetShortHelp();
1323
1324 return wxEmptyString;
1325 }
1326
1327 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1328 {
1329 if ( t != NULL )
1330 ((wxToolBarTool*)t)->DoEnable( enable );
1331 }
1332
1333 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1334 {
1335 wxToolBarTool *tool = (wxToolBarTool *)t;
1336 if ( ( tool != NULL ) && tool->IsButton() )
1337 tool->UpdateToggleImage( toggle );
1338 }
1339
1340 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1341 {
1342 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
1343 if (tool == NULL)
1344 return false;
1345
1346 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1347 wxSize toolSize = GetToolSize();
1348 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
1349 ControlRef controlHandle = NULL;
1350 OSStatus err = 0;
1351
1352 switch (tool->GetStyle())
1353 {
1354 case wxTOOL_STYLE_SEPARATOR:
1355 {
1356 wxASSERT( tool->GetControlHandle() == NULL );
1357 toolSize.x /= 4;
1358 toolSize.y /= 4;
1359 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1360 toolrect.bottom = toolSize.y;
1361 else
1362 toolrect.right = toolSize.x;
1363
1364 #ifdef __WXMAC_OSX__
1365 // in flat style we need a visual separator
1366 #if wxMAC_USE_NATIVE_TOOLBAR
1367 HIToolbarItemRef item;
1368 err = HIToolbarItemCreate(
1369 kHIToolbarSeparatorIdentifier,
1370 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1371 &item );
1372 if (err == noErr)
1373 tool->SetToolbarItemRef( item );
1374 #endif
1375
1376 CreateSeparatorControl( window, &toolrect, &controlHandle );
1377 tool->SetControlHandle( controlHandle );
1378 #endif
1379 }
1380 break;
1381
1382 case wxTOOL_STYLE_BUTTON:
1383 {
1384 wxASSERT( tool->GetControlHandle() == NULL );
1385 ControlButtonContentInfo info;
1386 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
1387
1388 if ( UMAGetSystemVersion() >= 0x1000)
1389 {
1390 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1391 }
1392 else
1393 {
1394 SInt16 behaviour = kControlBehaviorOffsetContents;
1395 if ( tool->CanBeToggled() )
1396 behaviour |= kControlBehaviorToggles;
1397 err = CreateBevelButtonControl( window,
1398 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1399 behaviour, &info, 0, 0, 0, &controlHandle );
1400 }
1401
1402 #if wxMAC_USE_NATIVE_TOOLBAR
1403 HIToolbarItemRef item;
1404 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
1405 err = HIToolbarItemCreate(
1406 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1407 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1408 if (err == noErr)
1409 {
1410 InstallEventHandler(
1411 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1412 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
1413 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1414 HIToolbarItemSetIconRef( item, info.u.iconRef );
1415 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
1416 tool->SetToolbarItemRef( item );
1417 }
1418 #endif
1419
1420 wxMacReleaseBitmapButton( &info );
1421
1422 #if 0
1423 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1424 UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );
1425 #endif
1426
1427 InstallControlEventHandler(
1428 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1429 GetEventTypeCount(eventList), eventList, tool, NULL );
1430
1431 tool->SetControlHandle( controlHandle );
1432 }
1433 break;
1434
1435 case wxTOOL_STYLE_CONTROL:
1436
1437 #if wxMAC_USE_NATIVE_TOOLBAR
1438 {
1439 wxASSERT( tool->GetControl() != NULL );
1440 HIToolbarItemRef item;
1441 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
1442 // as this control now is part of both the wxToolBar children and the native toolbar, we have to increase the
1443 // reference count to make sure we are not dealing with zombie controls after the native toolbar has released its views
1444 CFRetain( viewRef ) ;
1445 CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ;
1446 err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID,
1447 data , &item ) ;
1448
1449 if (err == noErr)
1450 {
1451 tool->SetToolbarItemRef( item );
1452 }
1453 CFRelease( data ) ;
1454 }
1455
1456 #else
1457 // right now there's nothing to do here
1458 #endif
1459 break;
1460
1461 default:
1462 break;
1463 }
1464
1465 if ( err == noErr )
1466 {
1467 if ( controlHandle )
1468 {
1469 ControlRef container = (ControlRef) GetHandle();
1470 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1471
1472 UMAShowControl( controlHandle );
1473 ::EmbedControl( controlHandle, container );
1474 }
1475
1476 if ( tool->CanBeToggled() && tool->IsToggled() )
1477 tool->UpdateToggleImage( true );
1478
1479 // nothing special to do here - we relayout in Realize() later
1480 tool->Attach( this );
1481 InvalidateBestSize();
1482 }
1483 else
1484 {
1485 wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err );
1486 wxFAIL_MSG( errMsg.c_str() );
1487 }
1488
1489 return (err == noErr);
1490 }
1491
1492 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1493 {
1494 wxFAIL_MSG( wxT("not implemented") );
1495 }
1496
1497 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1498 {
1499 wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase );
1500 wxToolBarToolsList::compatibility_iterator node;
1501 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1502 {
1503 wxToolBarToolBase *tool2 = node->GetData();
1504 if ( tool2 == tool )
1505 {
1506 // let node point to the next node in the list
1507 node = node->GetNext();
1508
1509 break;
1510 }
1511 }
1512
1513 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1514
1515 tool->Detach();
1516
1517 #if wxMAC_USE_NATIVE_TOOLBAR
1518 CFIndex removeIndex = tool->GetIndex();
1519 #endif
1520
1521 switch ( tool->GetStyle() )
1522 {
1523 case wxTOOL_STYLE_CONTROL:
1524 {
1525 tool->GetControl()->Destroy();
1526 tool->ClearControl();
1527 }
1528 break;
1529
1530 case wxTOOL_STYLE_BUTTON:
1531 case wxTOOL_STYLE_SEPARATOR:
1532 if ( tool->GetControlHandle() )
1533 {
1534 #if wxMAC_USE_NATIVE_TOOLBAR
1535 if ( removeIndex != -1 && m_macHIToolbarRef )
1536 {
1537 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
1538 tool->SetIndex( -1 );
1539 }
1540 #endif
1541
1542 tool->ClearControl();
1543 }
1544 break;
1545
1546 default:
1547 break;
1548 }
1549
1550 // and finally reposition all the controls after this one
1551
1552 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1553 {
1554 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1555 wxPoint pt = tool2->GetPosition();
1556
1557 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1558 pt.y -= sz.y;
1559 else
1560 pt.x -= sz.x;
1561
1562 tool2->SetPosition( pt );
1563
1564 #if wxMAC_USE_NATIVE_TOOLBAR
1565 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1566 tool2->SetIndex( tool2->GetIndex() - 1 );
1567 #endif
1568 }
1569
1570 InvalidateBestSize();
1571
1572 return true;
1573 }
1574
1575 void wxToolBar::OnPaint(wxPaintEvent& event)
1576 {
1577 #if wxMAC_USE_NATIVE_TOOLBAR
1578 if ( m_macUsesNativeToolbar )
1579 {
1580 event.Skip(true);
1581 return;
1582 }
1583 #endif
1584
1585 wxPaintDC dc(this);
1586
1587 int w, h;
1588 GetSize( &w, &h );
1589
1590 bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance();
1591 bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);
1592
1593 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1594 if ( !drawMetalTheme && minimumUmaAvailable )
1595 {
1596 HIThemePlacardDrawInfo info;
1597 memset( &info, 0, sizeof(info) );
1598 info.version = 0;
1599 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1600
1601 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1602 HIRect rect = CGRectMake( 0, 0, w, h );
1603 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1604 }
1605 else
1606 {
1607 // leave the background as it is (striped or metal)
1608 }
1609
1610 #else
1611
1612 const bool drawBorder = true;
1613
1614 if (drawBorder)
1615 {
1616 wxMacPortSetter helper( &dc );
1617
1618 if ( !drawMetalTheme || !minimumUmaAvailable )
1619 {
1620 Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1621 dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };
1622
1623 #if 0
1624 if ( toolbarrect.left < 0 )
1625 toolbarrect.left = 0;
1626 if ( toolbarrect.top < 0 )
1627 toolbarrect.top = 0;
1628 #endif
1629
1630 UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive );
1631 }
1632 else
1633 {
1634 #if TARGET_API_MAC_OSX
1635 HIRect hiToolbarrect = CGRectMake(
1636 dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1637 dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) );
1638 CGContextRef cgContext;
1639 Rect bounds;
1640
1641 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
1642 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1643
1644 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
1645 CGContextScaleCTM( cgContext, 1, -1 );
1646
1647 HIThemeBackgroundDrawInfo drawInfo;
1648 drawInfo.version = 0;
1649 drawInfo.state = kThemeStateActive;
1650 drawInfo.kind = kThemeBackgroundMetal;
1651 HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );
1652
1653 #ifndef __LP64__
1654 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1655 #endif
1656 #endif
1657 }
1658 }
1659 #endif
1660
1661 event.Skip();
1662 }
1663
1664 #endif // wxUSE_TOOLBAR