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