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