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