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