]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
fix crash introduced in last commit (the correct item variable was shadowed by anothe...
[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, const wxString& label)
76 : wxToolBarToolBase(tbar, control, label)
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 *
537 wxToolBar::CreateTool(wxControl *control, const wxString& label)
538 {
539 return new wxToolBarTool(this, control, label);
540 }
541
542 void wxToolBar::Init()
543 {
544 m_maxWidth = -1;
545 m_maxHeight = -1;
546 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
547 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
548
549 #if wxMAC_USE_NATIVE_TOOLBAR
550 m_macHIToolbarRef = NULL;
551 m_macUsesNativeToolbar = false;
552 #endif
553 }
554
555 #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
556
557 const EventTypeSpec kEvents[] =
558 {
559 { kEventClassHIObject, kEventHIObjectConstruct },
560 { kEventClassHIObject, kEventHIObjectInitialize },
561 { kEventClassHIObject, kEventHIObjectDestruct },
562
563 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
564 };
565
566 const EventTypeSpec kViewEvents[] =
567 {
568 { kEventClassControl, kEventControlGetSizeConstraints }
569 };
570
571 struct ControlToolbarItem
572 {
573 HIToolbarItemRef toolbarItem;
574 HIViewRef viewRef;
575 wxSize lastValidSize ;
576 };
577
578 static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
579 {
580 OSStatus result = eventNotHandledErr;
581 ControlToolbarItem* object = (ControlToolbarItem*)inUserData;
582
583 switch ( GetEventClass( inEvent ) )
584 {
585 case kEventClassHIObject:
586 switch ( GetEventKind( inEvent ) )
587 {
588 case kEventHIObjectConstruct:
589 {
590 HIObjectRef toolbarItem;
591 ControlToolbarItem* item;
592
593 GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL,
594 sizeof( HIObjectRef ), NULL, &toolbarItem );
595
596 item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ;
597 item->toolbarItem = toolbarItem ;
598 item->viewRef = NULL ;
599
600 SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item );
601
602 result = noErr ;
603 }
604 break;
605
606 case kEventHIObjectInitialize:
607 result = CallNextEventHandler( inCallRef, inEvent );
608 if ( result == noErr )
609 {
610 CFDataRef data;
611 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
612 sizeof( CFTypeRef ), NULL, &data );
613
614 HIViewRef viewRef ;
615
616 wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ;
617 memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ;
618
619 object->viewRef = (HIViewRef) viewRef ;
620
621 result = noErr ;
622 }
623 break;
624
625 case kEventHIObjectDestruct:
626 {
627 // we've increased the ref count when creating this, so we decrease manually again in case
628 // it was never really installed and deinstalled
629 HIViewRef viewRef = object->viewRef ;
630 if( viewRef && IsValidControlHandle( viewRef) )
631 {
632 CFIndex count = CFGetRetainCount( viewRef ) ;
633 if ( count >= 1 )
634 CFRelease( viewRef ) ;
635 }
636 free( object ) ;
637 result = noErr;
638 }
639 break;
640 }
641 break;
642
643 case kEventClassToolbarItem:
644 switch ( GetEventKind( inEvent ) )
645 {
646 case kEventToolbarItemCreateCustomView:
647 {
648 HIViewRef viewRef = object->viewRef ;
649
650 HIViewRemoveFromSuperview( viewRef ) ;
651 HIViewSetVisible(viewRef, true) ;
652 InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler,
653 GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL );
654
655 result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef );
656 }
657 break;
658 }
659 break;
660
661 case kEventClassControl:
662 switch ( GetEventKind( inEvent ) )
663 {
664 case kEventControlGetSizeConstraints:
665 {
666 wxWindow* wxwindow = wxFindControlFromMacControl(object->viewRef ) ;
667 if ( wxwindow )
668 {
669 wxSize sz = wxwindow->GetSize() ;
670 sz.x -= wxwindow->MacGetLeftBorderSize() + wxwindow->MacGetRightBorderSize();
671 sz.y -= wxwindow->MacGetTopBorderSize() + wxwindow->MacGetBottomBorderSize();
672 // during toolbar layout the native window sometimes gets negative sizes
673 // so we always keep the last valid size here, to make sure we survive the
674 // shuffle ...
675 if ( sz.x > 0 && sz.y > 0 )
676 object->lastValidSize = sz ;
677 else
678 sz = object->lastValidSize ;
679
680 HISize min, max;
681 min.width = max.width = sz.x ;
682 min.height = max.height = sz.y ;
683
684 result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize,
685 sizeof( HISize ), &min );
686
687 result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize,
688 sizeof( HISize ), &max );
689 result = noErr ;
690 }
691 }
692 break;
693 }
694 break;
695 }
696
697 return result;
698 }
699
700 void RegisterControlToolbarItemClass()
701 {
702 static bool sRegistered;
703
704 if ( !sRegistered )
705 {
706 HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0,
707 ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL );
708
709 sRegistered = true;
710 }
711 }
712
713 HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData)
714 {
715 RegisterControlToolbarItemClass();
716
717 OSStatus err;
718 EventRef event;
719 UInt32 options = kHIToolbarItemAllowDuplicates;
720 HIToolbarItemRef result = NULL;
721
722 err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
723 require_noerr( err, CantCreateEvent );
724
725 SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options );
726 SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier );
727
728 if ( inConfigData )
729 SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData );
730
731 err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result );
732 check_noerr( err );
733
734 ReleaseEvent( event );
735 CantCreateEvent :
736 return result ;
737 }
738
739 #if wxMAC_USE_NATIVE_TOOLBAR
740 static const EventTypeSpec kToolbarEvents[] =
741 {
742 { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
743 { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
744 { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
745 };
746
747 static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
748 {
749 OSStatus result = eventNotHandledErr;
750 // Not yet needed
751 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
752 CFMutableArrayRef array;
753
754 switch ( GetEventKind( inEvent ) )
755 {
756 case kEventToolbarGetDefaultIdentifiers:
757 {
758 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
759 sizeof( CFMutableArrayRef ), NULL, &array );
760 // not implemented yet
761 // GetToolbarDefaultItems( array );
762 result = noErr;
763 }
764 break;
765
766 case kEventToolbarGetAllowedIdentifiers:
767 {
768 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
769 sizeof( CFMutableArrayRef ), NULL, &array );
770 // not implemented yet
771 // GetToolbarAllowedItems( array );
772 result = noErr;
773 }
774 break;
775 case kEventToolbarCreateItemWithIdentifier:
776 {
777 HIToolbarItemRef item = NULL;
778 CFTypeRef data = NULL;
779 CFStringRef identifier = NULL ;
780
781 GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL,
782 sizeof( CFStringRef ), NULL, &identifier );
783
784 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
785 sizeof( CFTypeRef ), NULL, &data );
786
787 if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo )
788 {
789 item = CreateControlToolbarItem( kControlToolbarItemClassID, data );
790 if ( item )
791 {
792 SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef,
793 sizeof( HIToolbarItemRef ), &item );
794 result = noErr;
795 }
796 }
797
798 }
799 break;
800 }
801 return result ;
802 }
803 #endif // wxMAC_USE_NATIVE_TOOLBAR
804
805 // also for the toolbar we have the dual implementation:
806 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
807
808 bool wxToolBar::Create(
809 wxWindow *parent,
810 wxWindowID id,
811 const wxPoint& pos,
812 const wxSize& size,
813 long style,
814 const wxString& name )
815 {
816 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
817 return false;
818
819 FixupStyle();
820
821 OSStatus err = noErr;
822
823 #if wxMAC_USE_NATIVE_TOOLBAR
824 wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
825 err = HIToolbarCreate(
826 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
827 (HIToolbarRef*) &m_macHIToolbarRef );
828
829 if (m_macHIToolbarRef != NULL)
830 {
831 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler,
832 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
833
834 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
835 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
836
837 if ( style & wxTB_NOICONS )
838 mode = kHIToolbarDisplayModeLabelOnly;
839 else if ( style & wxTB_TEXT )
840 mode = kHIToolbarDisplayModeIconAndLabel;
841 else
842 mode = kHIToolbarDisplayModeIconOnly;
843
844 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
845 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
846 }
847 #endif // wxMAC_USE_NATIVE_TOOLBAR
848
849 return (err == noErr);
850 }
851
852 wxToolBar::~wxToolBar()
853 {
854 #if wxMAC_USE_NATIVE_TOOLBAR
855 if (m_macHIToolbarRef != NULL)
856 {
857 // if this is the installed toolbar, then deinstall it
858 if (m_macUsesNativeToolbar)
859 MacInstallNativeToolbar( false );
860
861 CFIndex count = CFGetRetainCount( m_macHIToolbarRef ) ;
862 wxASSERT_MSG( count == 1 , wxT("Reference Count of native control was not 1 in wxToolBar destructor") );
863
864 CFRelease( (HIToolbarRef)m_macHIToolbarRef );
865 m_macHIToolbarRef = NULL;
866 }
867 #endif
868 }
869
870 bool wxToolBar::Show( bool show )
871 {
872 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
873 bool bResult = (tlw != NULL);
874
875 if (bResult)
876 {
877 #if wxMAC_USE_NATIVE_TOOLBAR
878 bool ownToolbarInstalled = false;
879 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
880 if (ownToolbarInstalled)
881 {
882 bResult = (IsWindowToolbarVisible( tlw ) != show);
883 if ( bResult )
884 ShowHideWindowToolbar( tlw, show, false );
885 }
886 else
887 bResult = wxToolBarBase::Show( show );
888 #else
889
890 bResult = wxToolBarBase::Show( show );
891 #endif
892 }
893
894 return bResult;
895 }
896
897 bool wxToolBar::IsShown() const
898 {
899 bool bResult;
900
901 #if wxMAC_USE_NATIVE_TOOLBAR
902 bool ownToolbarInstalled;
903
904 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
905 if (ownToolbarInstalled)
906 {
907 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
908 bResult = IsWindowToolbarVisible( tlw );
909 }
910 else
911 bResult = wxToolBarBase::IsShown();
912 #else
913
914 bResult = wxToolBarBase::IsShown();
915 #endif
916
917 return bResult;
918 }
919
920 void wxToolBar::DoGetSize( int *width, int *height ) const
921 {
922 #if wxMAC_USE_NATIVE_TOOLBAR
923 Rect boundsR;
924 bool ownToolbarInstalled;
925
926 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
927 if ( ownToolbarInstalled )
928 {
929 // TODO: is this really a control ?
930 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
931 if ( width != NULL )
932 *width = boundsR.right - boundsR.left;
933 if ( height != NULL )
934 *height = boundsR.bottom - boundsR.top;
935 }
936 else
937 wxToolBarBase::DoGetSize( width, height );
938
939 #else
940 wxToolBarBase::DoGetSize( width, height );
941 #endif
942 }
943
944 wxSize wxToolBar::DoGetBestSize() const
945 {
946 int width, height;
947
948 DoGetSize( &width, &height );
949
950 return wxSize( width, height );
951 }
952
953 void wxToolBar::SetWindowStyleFlag( long style )
954 {
955 wxToolBarBase::SetWindowStyleFlag( style );
956
957 #if wxMAC_USE_NATIVE_TOOLBAR
958 if (m_macHIToolbarRef != NULL)
959 {
960 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
961
962 if ( style & wxTB_NOICONS )
963 mode = kHIToolbarDisplayModeLabelOnly;
964 else if ( style & wxTB_TEXT )
965 mode = kHIToolbarDisplayModeIconAndLabel;
966 else
967 mode = kHIToolbarDisplayModeIconOnly;
968
969 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
970 }
971 #endif
972 }
973
974 #if wxMAC_USE_NATIVE_TOOLBAR
975 bool wxToolBar::MacWantsNativeToolbar()
976 {
977 return m_macUsesNativeToolbar;
978 }
979
980 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
981 {
982 bool bResultV = false;
983
984 if (ownToolbarInstalled != NULL)
985 *ownToolbarInstalled = false;
986
987 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
988 if (tlw != NULL)
989 {
990 HIToolbarRef curToolbarRef = NULL;
991 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
992 bResultV = ((err == noErr) && (curToolbarRef != NULL));
993 if (bResultV && (ownToolbarInstalled != NULL))
994 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
995 }
996
997 return bResultV;
998 }
999
1000 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
1001 {
1002 bool bResult = false;
1003
1004 if (usesNative && (m_macHIToolbarRef == NULL))
1005 return bResult;
1006
1007 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
1008 return bResult;
1009
1010 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
1011 if (tlw == NULL)
1012 return bResult;
1013
1014 // check the existing toolbar
1015 HIToolbarRef curToolbarRef = NULL;
1016 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
1017 if (err != noErr)
1018 curToolbarRef = NULL;
1019
1020 m_macUsesNativeToolbar = usesNative;
1021
1022 if (m_macUsesNativeToolbar)
1023 {
1024 // only install toolbar if there isn't one installed already
1025 if (curToolbarRef == NULL)
1026 {
1027 bResult = true;
1028
1029 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
1030 ShowHideWindowToolbar( tlw, true, false );
1031 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
1032 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
1033
1034 Rect r = { 0, 0, 0, 0 };
1035 m_peer->SetRect( &r );
1036 SetSize( wxSIZE_AUTO_WIDTH, 0 );
1037 m_peer->SetVisibility( false, true );
1038 wxToolBarBase::Show( false );
1039 }
1040 }
1041 else
1042 {
1043 // only deinstall toolbar if this is the installed one
1044 if (m_macHIToolbarRef == curToolbarRef)
1045 {
1046 bResult = true;
1047
1048 ShowHideWindowToolbar( tlw, false, false );
1049 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
1050 SetWindowToolbar( tlw, NULL );
1051
1052 m_peer->SetVisibility( true, true );
1053 }
1054 }
1055
1056 if (bResult)
1057 InvalidateBestSize();
1058
1059 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1060 return bResult;
1061 }
1062 #endif
1063
1064 bool wxToolBar::Realize()
1065 {
1066 if (m_tools.GetCount() == 0)
1067 return false;
1068
1069 int maxWidth = 0;
1070 int maxHeight = 0;
1071
1072 int maxToolWidth = 0;
1073 int maxToolHeight = 0;
1074
1075 int x = m_xMargin + kwxMacToolBarLeftMargin;
1076 int y = m_yMargin + kwxMacToolBarTopMargin;
1077
1078 int tw, th;
1079 GetSize( &tw, &th );
1080
1081 // find the maximum tool width and height
1082 wxToolBarTool *tool;
1083 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1084 while ( node != NULL )
1085 {
1086 tool = (wxToolBarTool *) node->GetData();
1087 if ( tool != NULL )
1088 {
1089 wxSize sz = tool->GetSize();
1090
1091 if ( sz.x > maxToolWidth )
1092 maxToolWidth = sz.x;
1093 if ( sz.y > maxToolHeight )
1094 maxToolHeight = sz.y;
1095 }
1096
1097 node = node->GetNext();
1098 }
1099
1100 bool lastIsRadio = false;
1101 bool curIsRadio = false;
1102
1103 #if wxMAC_USE_NATIVE_TOOLBAR
1104 CFIndex currentPosition = 0;
1105 bool insertAll = false;
1106 #endif
1107
1108 node = m_tools.GetFirst();
1109 while ( node != NULL )
1110 {
1111 tool = (wxToolBarTool*) node->GetData();
1112 if ( tool == NULL )
1113 {
1114 node = node->GetNext();
1115 continue;
1116 }
1117
1118 // set tool position:
1119 // for the moment just perform a single row/column alignment
1120 wxSize cursize = tool->GetSize();
1121 if ( x + cursize.x > maxWidth )
1122 maxWidth = x + cursize.x;
1123 if ( y + cursize.y > maxHeight )
1124 maxHeight = y + cursize.y;
1125
1126 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1127 {
1128 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1129 tool->SetPosition( wxPoint(x1, y) );
1130 }
1131 else
1132 {
1133 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1134 tool->SetPosition( wxPoint(x, y1) );
1135 }
1136
1137 // update the item positioning state
1138 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1139 y += cursize.y + kwxMacToolSpacing;
1140 else
1141 x += cursize.x + kwxMacToolSpacing;
1142
1143 #if wxMAC_USE_NATIVE_TOOLBAR
1144 // install in native HIToolbar
1145 if ( m_macHIToolbarRef != NULL )
1146 {
1147 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
1148 if ( hiItemRef != NULL )
1149 {
1150 if ( insertAll || (tool->GetIndex() != currentPosition) )
1151 {
1152 OSStatus err = noErr;
1153 if ( !insertAll )
1154 {
1155 insertAll = true;
1156
1157 // if this is the first tool that gets newly inserted or repositioned
1158 // first remove all 'old' tools from here to the right, because of this
1159 // all following tools will have to be reinserted (insertAll). i = 100 because there's
1160 // no way to determine how many there are in a toolbar, so just a high number :-(
1161 for ( CFIndex i = 100; i >= currentPosition; --i )
1162 {
1163 err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i );
1164 }
1165
1166 if (err != noErr)
1167 {
1168 wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err );
1169 wxFAIL_MSG( errMsg.c_str() );
1170 }
1171 }
1172
1173 err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition );
1174 if (err != noErr)
1175 {
1176 wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
1177 wxFAIL_MSG( errMsg.c_str() );
1178 }
1179
1180 tool->SetIndex( currentPosition );
1181 }
1182
1183 currentPosition++;
1184 }
1185 }
1186 #endif
1187
1188 // update radio button (and group) state
1189 lastIsRadio = curIsRadio;
1190 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
1191
1192 if ( !curIsRadio )
1193 {
1194 if ( tool->IsToggled() )
1195 DoToggleTool( tool, true );
1196 }
1197 else
1198 {
1199 if ( !lastIsRadio )
1200 {
1201 if ( tool->Toggle( true ) )
1202 {
1203 DoToggleTool( tool, true );
1204 }
1205 }
1206 else if ( tool->IsToggled() )
1207 {
1208 if ( tool->IsToggled() )
1209 DoToggleTool( tool, true );
1210
1211 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1212 while ( nodePrev != NULL )
1213 {
1214 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1215 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1216 break;
1217
1218 if ( toggleTool->Toggle( false ) )
1219 DoToggleTool( toggleTool, false );
1220
1221 nodePrev = nodePrev->GetPrevious();
1222 }
1223 }
1224 }
1225
1226 node = node->GetNext();
1227 }
1228
1229 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
1230 {
1231 // if not set yet, only one row
1232 if ( m_maxRows <= 0 )
1233 SetRows( 1 );
1234
1235 m_minWidth = maxWidth;
1236 maxWidth = tw;
1237 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
1238 m_minHeight = m_maxHeight = maxHeight;
1239 }
1240 else
1241 {
1242 // if not set yet, have one column
1243 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1244 SetRows( GetToolsCount() );
1245
1246 m_minHeight = maxHeight;
1247 maxHeight = th;
1248 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
1249 m_minWidth = m_maxWidth = maxWidth;
1250 }
1251
1252 #if 0
1253 // FIXME: should this be OSX-only?
1254 {
1255 bool wantNativeToolbar, ownToolbarInstalled;
1256
1257 // attempt to install the native toolbar
1258 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1259 MacInstallNativeToolbar( wantNativeToolbar );
1260 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1261 if (!ownToolbarInstalled)
1262 {
1263 SetSize( maxWidth, maxHeight );
1264 InvalidateBestSize();
1265 }
1266 }
1267 #else
1268 SetSize( maxWidth, maxHeight );
1269 InvalidateBestSize();
1270 #endif
1271
1272 SetInitialSize();
1273
1274 return true;
1275 }
1276
1277 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1278 {
1279 m_defaultWidth = size.x + kwxMacToolBorder;
1280 m_defaultHeight = size.y + kwxMacToolBorder;
1281
1282 #if wxMAC_USE_NATIVE_TOOLBAR
1283 if (m_macHIToolbarRef != NULL)
1284 {
1285 int maxs = wxMax( size.x, size.y );
1286 HIToolbarDisplaySize sizeSpec;
1287 if ( maxs > 32 )
1288 sizeSpec = kHIToolbarDisplaySizeNormal;
1289 else if ( maxs > 24 )
1290 sizeSpec = kHIToolbarDisplaySizeDefault;
1291 else
1292 sizeSpec = kHIToolbarDisplaySizeSmall;
1293
1294 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
1295 }
1296 #endif
1297 }
1298
1299 // The button size is bigger than the bitmap size
1300 wxSize wxToolBar::GetToolSize() const
1301 {
1302 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1303 }
1304
1305 void wxToolBar::SetRows(int nRows)
1306 {
1307 // avoid resizing the frame uselessly
1308 if ( nRows != m_maxRows )
1309 m_maxRows = nRows;
1310 }
1311
1312 void wxToolBar::MacSuperChangedPosition()
1313 {
1314 wxWindow::MacSuperChangedPosition();
1315
1316 #if wxMAC_USE_NATIVE_TOOLBAR
1317 if (! m_macUsesNativeToolbar )
1318 Realize();
1319 #else
1320
1321 Realize();
1322 #endif
1323 }
1324
1325 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1326 {
1327 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1328 if ( tool )
1329 {
1330 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1331
1332 tool->SetNormalBitmap(bitmap);
1333
1334 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1335 tool->UpdateToggleImage( tool->CanBeToggled() && tool->IsToggled() );
1336 }
1337 }
1338
1339 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1340 {
1341 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1342 if ( tool )
1343 {
1344 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1345
1346 tool->SetDisabledBitmap(bitmap);
1347
1348 // TODO: what to do for this one?
1349 }
1350 }
1351
1352 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1353 {
1354 wxToolBarTool *tool;
1355 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1356 while ( node != NULL )
1357 {
1358 tool = (wxToolBarTool *)node->GetData();
1359 if (tool != NULL)
1360 {
1361 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1362 if ( r.Contains( wxPoint( x, y ) ) )
1363 return tool;
1364 }
1365
1366 node = node->GetNext();
1367 }
1368
1369 return (wxToolBarToolBase*)NULL;
1370 }
1371
1372 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1373 {
1374 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1375 if ( tool != NULL )
1376 return tool->GetShortHelp();
1377
1378 return wxEmptyString;
1379 }
1380
1381 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1382 {
1383 if ( t != NULL )
1384 ((wxToolBarTool*)t)->DoEnable( enable );
1385 }
1386
1387 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1388 {
1389 wxToolBarTool *tool = (wxToolBarTool *)t;
1390 if ( ( tool != NULL ) && tool->IsButton() )
1391 tool->UpdateToggleImage( toggle );
1392 }
1393
1394 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1395 {
1396 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
1397 if (tool == NULL)
1398 return false;
1399
1400 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1401 wxSize toolSize = GetToolSize();
1402 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
1403 ControlRef controlHandle = NULL;
1404 OSStatus err = 0;
1405 tool->Attach( this );
1406
1407 #if wxMAC_USE_NATIVE_TOOLBAR
1408 HIToolbarItemRef item;
1409 #endif
1410
1411 switch (tool->GetStyle())
1412 {
1413 case wxTOOL_STYLE_SEPARATOR:
1414 {
1415 wxASSERT( tool->GetControlHandle() == NULL );
1416 toolSize.x /= 4;
1417 toolSize.y /= 4;
1418 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1419 toolrect.bottom = toolSize.y;
1420 else
1421 toolrect.right = toolSize.x;
1422
1423 // in flat style we need a visual separator
1424 #if wxMAC_USE_NATIVE_TOOLBAR
1425 err = HIToolbarItemCreate(
1426 kHIToolbarSeparatorIdentifier,
1427 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1428 &item );
1429 if (err == noErr)
1430 tool->SetToolbarItemRef( item );
1431 #endif // wxMAC_USE_NATIVE_TOOLBAR
1432
1433 CreateSeparatorControl( window, &toolrect, &controlHandle );
1434 tool->SetControlHandle( controlHandle );
1435 }
1436 break;
1437
1438 case wxTOOL_STYLE_BUTTON:
1439 {
1440 wxASSERT( tool->GetControlHandle() == NULL );
1441 ControlButtonContentInfo info;
1442 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
1443
1444 if ( UMAGetSystemVersion() >= 0x1000)
1445 {
1446 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1447 }
1448 else
1449 {
1450 SInt16 behaviour = kControlBehaviorOffsetContents;
1451 if ( tool->CanBeToggled() )
1452 behaviour |= kControlBehaviorToggles;
1453 err = CreateBevelButtonControl( window,
1454 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1455 behaviour, &info, 0, 0, 0, &controlHandle );
1456 }
1457
1458 #if wxMAC_USE_NATIVE_TOOLBAR
1459 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
1460 err = HIToolbarItemCreate(
1461 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1462 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1463 if (err == noErr)
1464 {
1465 InstallEventHandler(
1466 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1467 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
1468
1469 HIToolbarItemSetIconRef( item, info.u.iconRef );
1470 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
1471 tool->SetToolbarItemRef( item );
1472 }
1473 #endif // wxMAC_USE_NATIVE_TOOLBAR
1474
1475 wxMacReleaseBitmapButton( &info );
1476
1477 #if 0
1478 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1479 UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );
1480 #endif
1481
1482 InstallControlEventHandler(
1483 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1484 GetEventTypeCount(eventList), eventList, tool, NULL );
1485
1486 tool->SetControlHandle( controlHandle );
1487 }
1488 break;
1489
1490 case wxTOOL_STYLE_CONTROL:
1491
1492 #if wxMAC_USE_NATIVE_TOOLBAR
1493 {
1494 wxCHECK_MSG( tool->GetControl(), false, _T("control must be non-NULL") );
1495
1496 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
1497 // as this control now is part of both the wxToolBar children and the native toolbar, we have to increase the
1498 // reference count to make sure we are not dealing with zombie controls after the native toolbar has released its views
1499 CFRetain( viewRef ) ;
1500 CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ;
1501 err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID,
1502 data , &item ) ;
1503
1504 if (err == noErr)
1505 {
1506 tool->SetToolbarItemRef( item );
1507 }
1508 CFRelease( data ) ;
1509 }
1510
1511 #else
1512 // right now there's nothing to do here
1513 #endif
1514 break;
1515
1516 default:
1517 break;
1518 }
1519
1520 #if wxMAC_USE_NATIVE_TOOLBAR
1521 wxString label = tool->GetLabel();
1522 if ( !label.empty() )
1523 {
1524 // strip mnemonics from the label for compatibility
1525 // with the usual labels in wxStaticText sense
1526 label = wxStripMenuCodes(label);
1527
1528 HIToolbarItemSetLabel(item,
1529 wxMacCFStringHolder(label, m_font.GetEncoding()));
1530 }
1531 #endif // wxMAC_USE_NATIVE_TOOLBAR
1532
1533 if ( err == noErr )
1534 {
1535 if ( controlHandle )
1536 {
1537 ControlRef container = (ControlRef) GetHandle();
1538 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1539
1540 UMAShowControl( controlHandle );
1541 ::EmbedControl( controlHandle, container );
1542 }
1543
1544 if ( tool->CanBeToggled() && tool->IsToggled() )
1545 tool->UpdateToggleImage( true );
1546
1547 // nothing special to do here - we relayout in Realize() later
1548 InvalidateBestSize();
1549 }
1550 else
1551 {
1552 wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err );
1553 wxFAIL_MSG( errMsg.c_str() );
1554 }
1555
1556 return (err == noErr);
1557 }
1558
1559 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1560 {
1561 wxFAIL_MSG( wxT("not implemented") );
1562 }
1563
1564 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1565 {
1566 wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase );
1567 wxToolBarToolsList::compatibility_iterator node;
1568 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1569 {
1570 wxToolBarToolBase *tool2 = node->GetData();
1571 if ( tool2 == tool )
1572 {
1573 // let node point to the next node in the list
1574 node = node->GetNext();
1575
1576 break;
1577 }
1578 }
1579
1580 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1581
1582 tool->Detach();
1583
1584 #if wxMAC_USE_NATIVE_TOOLBAR
1585 CFIndex removeIndex = tool->GetIndex();
1586 #endif
1587
1588 #if wxMAC_USE_NATIVE_TOOLBAR
1589 if ( removeIndex != -1 && m_macHIToolbarRef )
1590 {
1591 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
1592 tool->SetIndex( -1 );
1593 }
1594 #endif
1595 switch ( tool->GetStyle() )
1596 {
1597 case wxTOOL_STYLE_CONTROL:
1598 if ( tool->GetControl() )
1599 tool->GetControl()->Destroy();
1600 break;
1601
1602 case wxTOOL_STYLE_BUTTON:
1603 case wxTOOL_STYLE_SEPARATOR:
1604 // nothing special
1605 break;
1606
1607 default:
1608 break;
1609 }
1610 tool->ClearControl();
1611
1612 // and finally reposition all the controls after this one
1613
1614 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1615 {
1616 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1617 wxPoint pt = tool2->GetPosition();
1618
1619 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1620 pt.y -= sz.y;
1621 else
1622 pt.x -= sz.x;
1623
1624 tool2->SetPosition( pt );
1625
1626 #if wxMAC_USE_NATIVE_TOOLBAR
1627 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1628 tool2->SetIndex( tool2->GetIndex() - 1 );
1629 #endif
1630 }
1631
1632 InvalidateBestSize();
1633
1634 return true;
1635 }
1636
1637 void wxToolBar::OnPaint(wxPaintEvent& event)
1638 {
1639 #if wxMAC_USE_NATIVE_TOOLBAR
1640 if ( m_macUsesNativeToolbar )
1641 {
1642 event.Skip(true);
1643 return;
1644 }
1645 #endif
1646
1647 wxPaintDC dc(this);
1648
1649 int w, h;
1650 GetSize( &w, &h );
1651
1652 bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance();
1653 bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);
1654
1655 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1656 if ( !drawMetalTheme && minimumUmaAvailable )
1657 {
1658 HIThemePlacardDrawInfo info;
1659 memset( &info, 0, sizeof(info) );
1660 info.version = 0;
1661 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1662
1663 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1664 HIRect rect = CGRectMake( 0, 0, w, h );
1665 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1666 }
1667 else
1668 {
1669 // leave the background as it is (striped or metal)
1670 }
1671
1672 #else
1673
1674 const bool drawBorder = true;
1675
1676 if (drawBorder)
1677 {
1678 wxMacPortSetter helper( &dc );
1679
1680 if ( !drawMetalTheme || !minimumUmaAvailable )
1681 {
1682 Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1683 dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };
1684
1685 #if 0
1686 if ( toolbarrect.left < 0 )
1687 toolbarrect.left = 0;
1688 if ( toolbarrect.top < 0 )
1689 toolbarrect.top = 0;
1690 #endif
1691
1692 UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive );
1693 }
1694 else
1695 {
1696 #if TARGET_API_MAC_OSX
1697 HIRect hiToolbarrect = CGRectMake(
1698 dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1699 dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) );
1700 CGContextRef cgContext;
1701 Rect bounds;
1702
1703 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
1704 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1705
1706 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
1707 CGContextScaleCTM( cgContext, 1, -1 );
1708
1709 HIThemeBackgroundDrawInfo drawInfo;
1710 drawInfo.version = 0;
1711 drawInfo.state = kThemeStateActive;
1712 drawInfo.kind = kThemeBackgroundMetal;
1713 HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );
1714
1715 #ifndef __LP64__
1716 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1717 #endif
1718 #endif
1719 }
1720 }
1721 #endif
1722
1723 event.Skip();
1724 }
1725
1726 #endif // wxUSE_TOOLBAR