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