]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
respect styles during creation as well
[wxWidgets.git] / src / mac / carbon / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "toolbar.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #if wxUSE_TOOLBAR
19
20 #include "wx/wx.h"
21 #include "wx/bitmap.h"
22 #include "wx/toolbar.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
25
26 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
27 EVT_PAINT( wxToolBar::OnPaint )
28 END_EVENT_TABLE()
29
30 #include "wx/mac/uma.h"
31 #include "wx/geometry.h"
32
33 #ifdef __WXMAC_OSX__
34 const short kwxMacToolBarToolDefaultWidth = 16 ;
35 const short kwxMacToolBarToolDefaultHeight = 16 ;
36 const short kwxMacToolBarTopMargin = 4 ; // 1 ; // used to be 4
37 const short kwxMacToolBarLeftMargin = 4 ; //1 ; // used to be 4
38 const short kwxMacToolBorder = 0 ; // used to be 0
39 const short kwxMacToolSpacing = 6 ; // 2 ; // used to be 6
40 #else
41 const short kwxMacToolBarToolDefaultWidth = 24 ;
42 const short kwxMacToolBarToolDefaultHeight = 22 ;
43 const short kwxMacToolBarTopMargin = 2 ;
44 const short kwxMacToolBarLeftMargin = 2 ;
45 const short kwxMacToolBorder = 4 ;
46 const short kwxMacToolSpacing = 0 ;
47 #endif
48
49 #pragma mark -
50 #pragma mark Tool Implementation
51
52
53 // ----------------------------------------------------------------------------
54 // private classes
55 // ----------------------------------------------------------------------------
56
57 // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
58
59 class wxToolBarTool : public wxToolBarToolBase
60 {
61 public:
62 wxToolBarTool(wxToolBar *tbar,
63 int id,
64 const wxString& label,
65 const wxBitmap& bmpNormal,
66 const wxBitmap& bmpDisabled,
67 wxItemKind kind,
68 wxObject *clientData,
69 const wxString& shortHelp,
70 const wxString& longHelp) ;
71
72 wxToolBarTool(wxToolBar *tbar, wxControl *control)
73 : wxToolBarToolBase(tbar, control)
74 {
75 Init() ;
76 if (control != NULL)
77 SetControlHandle( (ControlRef) control->GetHandle() ) ;
78 }
79
80 ~wxToolBarTool()
81 {
82 ClearControl() ;
83 if ( m_controlHandle )
84 DisposeControl( m_controlHandle ) ;
85 #if wxMAC_USE_NATIVE_TOOLBAR
86 if ( m_toolbarItemRef )
87 CFRelease( m_toolbarItemRef ) ;
88 #endif
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 m_control = NULL ;
106 #if wxMAC_USE_NATIVE_TOOLBAR
107 m_toolbarItemRef = NULL ;
108 #endif
109 }
110
111 wxSize GetSize() const
112 {
113 if ( IsControl() )
114 {
115 return GetControl()->GetSize() ;
116 }
117 else if ( IsButton() )
118 {
119 return GetToolBar()->GetToolSize() ;
120 }
121 else
122 {
123 // separator size
124 wxSize sz = GetToolBar()->GetToolSize() ;
125 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
126 sz.y /= 4 ;
127 else
128 sz.x /= 4 ;
129 return sz ;
130 }
131 }
132 wxPoint GetPosition() const
133 {
134 return wxPoint(m_x, m_y);
135 }
136 bool DoEnable( bool enable ) ;
137
138 void UpdateToggleImage( bool toggle ) ;
139
140 #if wxMAC_USE_NATIVE_TOOLBAR
141 void SetToolbarItemRef( HIToolbarItemRef ref )
142 {
143 if ( m_controlHandle )
144 HideControl( m_controlHandle ) ;
145 if ( m_toolbarItemRef )
146 CFRelease( m_toolbarItemRef ) ;
147 m_toolbarItemRef = ref ;
148 if ( m_toolbarItemRef )
149 {
150 HIToolbarItemSetHelpText(
151 m_toolbarItemRef, wxMacCFStringHolder( GetShortHelp() , GetToolBar()->GetFont().GetEncoding() ) ,
152 wxMacCFStringHolder( GetLongHelp() , GetToolBar()->GetFont().GetEncoding() ) ) ;
153 }
154 }
155 HIToolbarItemRef GetToolbarItemRef() const
156 {
157 return m_toolbarItemRef ;
158 }
159 #endif
160
161 private :
162 void Init()
163 {
164 m_controlHandle = NULL ;
165 #if wxMAC_USE_NATIVE_TOOLBAR
166 m_toolbarItemRef = NULL ;
167 #endif
168 }
169 ControlRef m_controlHandle ;
170 #if wxMAC_USE_NATIVE_TOOLBAR
171 HIToolbarItemRef m_toolbarItemRef ;
172 #endif
173 wxCoord m_x;
174 wxCoord m_y;
175 };
176
177 static const EventTypeSpec eventList[] =
178 {
179 { kEventClassControl , kEventControlHit } ,
180 #ifdef __WXMAC_OSX__
181 { kEventClassControl , kEventControlHitTest } ,
182 #endif
183 } ;
184
185 static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
186 {
187 OSStatus result = eventNotHandledErr ;
188
189 wxMacCarbonEvent cEvent( event ) ;
190
191 ControlRef controlRef ;
192
193 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
194
195 switch( GetEventKind( event ) )
196 {
197 case kEventControlHit :
198 {
199 wxToolBarTool* tbartool = (wxToolBarTool*)data ;
200 wxToolBar *tbar = tbartool != NULL ? ( wxToolBar * ) ( tbartool->GetToolBar() ) : NULL ;
201 if ((tbartool != NULL) && tbartool->CanBeToggled() )
202 {
203 bool shouldToggle;
204 #ifdef __WXMAC_OSX__
205 shouldToggle = !tbartool->IsToggled();
206 #else
207 shouldToggle = ( GetControl32BitValue((ControlRef) tbartool->GetControlHandle()) != 0 );
208 #endif
209 tbar->ToggleTool( tbartool->GetId(), shouldToggle );
210 }
211 if (tbartool != NULL)
212 tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() );
213 result = noErr;
214 }
215 break ;
216
217 #ifdef __WXMAC_OSX__
218 case kEventControlHitTest :
219 {
220 HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation) ;
221 HIRect rect ;
222 HIViewGetBounds( controlRef , &rect ) ;
223
224 ControlPartCode pc = kControlNoPart ;
225 if ( CGRectContainsPoint( rect , pt ) )
226 pc = kControlIconPart ;
227 cEvent.SetParameter( kEventParamControlPart , typeControlPartCode, pc ) ;
228 result = noErr ;
229 }
230 break ;
231 #endif
232
233 default :
234 break ;
235 }
236 return result ;
237 }
238
239 static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
240 {
241 OSStatus result = eventNotHandledErr ;
242
243 switch ( GetEventClass( event ) )
244 {
245 case kEventClassControl :
246 result = wxMacToolBarToolControlEventHandler( handler, event, data ) ;
247 break ;
248
249 default :
250 break ;
251 }
252 return result ;
253 }
254
255 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
256
257 #if wxMAC_USE_NATIVE_TOOLBAR
258
259 //
260 // native toolbar
261 //
262
263 static const EventTypeSpec toolBarEventList[] =
264 {
265 { kEventClassToolbarItem , kEventToolbarItemPerformAction } ,
266 } ;
267
268 static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
269 {
270 OSStatus result = eventNotHandledErr ;
271
272 switch( GetEventKind( event ) )
273 {
274 case kEventToolbarItemPerformAction :
275 {
276 wxToolBarTool* tbartool = (wxToolBarTool*) data ;
277 if ( tbartool != NULL )
278 {
279 int toolID = tbartool->GetId();
280 wxToolBar *tbar = ( wxToolBar * ) ( tbartool->GetToolBar() );
281 if ( tbartool->CanBeToggled() )
282 {
283 tbar->ToggleTool(toolID, !tbartool->IsToggled() );
284 }
285 tbar->OnLeftClick( toolID , tbartool -> IsToggled() ) ;
286 result = noErr;
287 }
288 }
289 break ;
290
291 default :
292 break ;
293 }
294 return result ;
295 }
296
297 static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
298 {
299 OSStatus result = eventNotHandledErr ;
300 switch( GetEventClass( event ) )
301 {
302 case kEventClassToolbarItem :
303 result = wxMacToolBarCommandEventHandler( handler, event, data ) ;
304 break ;
305
306 default :
307 break ;
308 }
309 return result ;
310 }
311
312 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler )
313
314 #endif
315
316 // ============================================================================
317 // implementation
318 // ============================================================================
319
320 // ----------------------------------------------------------------------------
321 // wxToolBarTool
322 // ----------------------------------------------------------------------------
323
324 bool wxToolBarTool::DoEnable(bool enable)
325 {
326 if ( IsControl() )
327 {
328 GetControl()->Enable( enable ) ;
329 }
330 else if ( IsButton() )
331 {
332 #if wxMAC_USE_NATIVE_TOOLBAR
333 if ( m_toolbarItemRef )
334 HIToolbarItemSetEnabled( m_toolbarItemRef , enable ) ;
335 #endif
336
337 if ( m_controlHandle )
338 {
339 #if TARGET_API_MAC_OSX
340 if ( enable )
341 EnableControl( m_controlHandle ) ;
342 else
343 DisableControl( m_controlHandle ) ;
344 #else
345 if ( enable )
346 ActivateControl( m_controlHandle ) ;
347 else
348 DeactivateControl( m_controlHandle ) ;
349 #endif
350 }
351 }
352 return true ;
353 }
354
355 void wxToolBarTool::SetPosition(const wxPoint& position)
356 {
357 m_x = position.x;
358 m_y = position.y;
359
360 int x , y ;
361 x = y = 0 ;
362 int mac_x = position.x ;
363 int mac_y = position.y ;
364
365 if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() )
366 {
367 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
368 mac_x += x;
369 mac_y += y;
370 }
371
372 if ( IsButton() )
373 {
374 Rect contrlRect ;
375 GetControlBounds( m_controlHandle , &contrlRect ) ;
376 int former_mac_x = contrlRect.left ;
377 int former_mac_y = contrlRect.top ;
378 GetToolBar()->GetToolSize() ;
379
380 if ( mac_x != former_mac_x || mac_y != former_mac_y )
381 {
382 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
383 }
384 }
385 else if ( IsControl() )
386 {
387 GetControl()->Move( position ) ;
388 }
389 else
390 {
391 // separator
392 #ifdef __WXMAC_OSX__
393 Rect contrlRect ;
394 GetControlBounds( m_controlHandle , &contrlRect ) ;
395 int former_mac_x = contrlRect.left ;
396 int former_mac_y = contrlRect.top ;
397
398 if ( mac_x != former_mac_x || mac_y != former_mac_y )
399 {
400 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
401 }
402 #endif
403 }
404 }
405
406 void wxToolBarTool::UpdateToggleImage( bool toggle )
407 {
408 #if wxMAC_USE_NATIVE_TOOLBAR
409
410 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
411 #define kHIToolbarItemSelected (1 << 7)
412 #endif
413
414 // FIXME: this should be a OSX v10.4 runtime check
415 if (m_toolbarItemRef != NULL)
416 {
417 OptionBits addAttrs, removeAttrs;
418 OSStatus result;
419
420 if (toggle)
421 {
422 addAttrs = kHIToolbarItemSelected;
423 removeAttrs = kHIToolbarItemNoAttributes;
424 }
425 else
426 {
427 addAttrs = kHIToolbarItemNoAttributes;
428 removeAttrs = kHIToolbarItemSelected;
429 }
430
431 result = HIToolbarItemChangeAttributes( m_toolbarItemRef, addAttrs, removeAttrs );
432 }
433 #endif
434
435 #ifdef __WXMAC_OSX__
436 if ( toggle )
437 {
438 int w = m_bmpNormal.GetWidth() ;
439 int h = m_bmpNormal.GetHeight() ;
440 wxBitmap bmp( w , h ) ;
441 wxMemoryDC dc ;
442 dc.SelectObject( bmp ) ;
443 dc.SetPen( wxNullPen ) ;
444 dc.SetBackground( *wxWHITE ) ;
445 dc.DrawRectangle( 0 , 0 , w , h ) ;
446 dc.DrawBitmap( m_bmpNormal , 0 , 0 , true) ;
447 dc.SelectObject( wxNullBitmap ) ;
448 ControlButtonContentInfo info ;
449 wxMacCreateBitmapButton( &info , bmp ) ;
450 SetControlData( m_controlHandle , 0, kControlIconContentTag, sizeof( info ),
451 (Ptr)&info );
452 wxMacReleaseBitmapButton( &info ) ;
453 }
454 else
455 {
456 ControlButtonContentInfo info ;
457 wxMacCreateBitmapButton( &info , m_bmpNormal ) ;
458 SetControlData( m_controlHandle , 0, kControlIconContentTag, sizeof( info ),
459 (Ptr)&info );
460 wxMacReleaseBitmapButton( &info ) ;
461 }
462
463 IconTransformType transform = toggle ? kTransformSelected : kTransformNone ;
464 SetControlData( m_controlHandle, 0, kControlIconTransformTag, sizeof( transform ),
465 (Ptr)&transform );
466 HIViewSetNeedsDisplay( m_controlHandle , true ) ;
467
468 #else
469 ::SetControl32BitValue( m_controlHandle , toggle ) ;
470 #endif
471 }
472
473 wxToolBarTool::wxToolBarTool(wxToolBar *tbar,
474 int id,
475 const wxString& label,
476 const wxBitmap& bmpNormal,
477 const wxBitmap& bmpDisabled,
478 wxItemKind kind,
479 wxObject *clientData,
480 const wxString& shortHelp,
481 const wxString& longHelp)
482 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
483 clientData, shortHelp, longHelp)
484 {
485 Init();
486 }
487
488 #pragma mark -
489 #pragma mark Toolbar Implementation
490
491 wxToolBarToolBase *wxToolBar::CreateTool(int id,
492 const wxString& label,
493 const wxBitmap& bmpNormal,
494 const wxBitmap& bmpDisabled,
495 wxItemKind kind,
496 wxObject *clientData,
497 const wxString& shortHelp,
498 const wxString& longHelp)
499 {
500 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
501 clientData, shortHelp, longHelp);
502 }
503
504 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
505 {
506 return new wxToolBarTool(this, control);
507 }
508
509 void wxToolBar::Init()
510 {
511 m_maxWidth = -1;
512 m_maxHeight = -1;
513 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
514 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
515 #if wxMAC_USE_NATIVE_TOOLBAR
516 m_macHIToolbarRef = NULL ;
517 m_macUsesNativeToolbar = false ;
518 #endif
519 }
520
521 // also for the toolbar we have the dual implementation:
522 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
523 //
524 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
525 long style, const wxString& name)
526 {
527 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
528 return false ;
529
530 OSStatus err = 0;
531
532 #if wxMAC_USE_NATIVE_TOOLBAR
533 wxString labelStr;
534 labelStr.Format(wxT("%xd"), (int)this);
535 err = HIToolbarCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding() ) , 0 ,
536 (HIToolbarRef*) &m_macHIToolbarRef );
537
538 if (m_macHIToolbarRef != NULL)
539 {
540 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault ;
541 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall ;
542
543 if ( style & wxTB_NOICONS )
544 mode = kHIToolbarDisplayModeLabelOnly ;
545 else if ( style & wxTB_TEXT )
546 mode = kHIToolbarDisplayModeIconAndLabel ;
547 else
548 mode = kHIToolbarDisplayModeIconOnly ;
549
550 displaySize = kHIToolbarDisplaySizeDefault ;
551
552 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef , mode ) ;
553 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef , displaySize ) ;
554 }
555 #endif
556
557 return (err == 0);
558 }
559
560 wxToolBar::~wxToolBar()
561 {
562 #if wxMAC_USE_NATIVE_TOOLBAR
563 if ( m_macHIToolbarRef )
564 {
565 // if this is the installed toolbar, then deinstall it
566 if (m_macUsesNativeToolbar)
567 MacInstallNativeToolbar( false );
568
569 CFRelease( (HIToolbarRef) m_macHIToolbarRef );
570 m_macHIToolbarRef = NULL;
571 }
572 #endif
573 }
574
575 bool wxToolBar::Show( bool show )
576 {
577 bool bResult, ownToolbarInstalled = false;
578 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
579
580 bResult = (tlw != NULL);
581 if (bResult)
582 {
583 #if wxMAC_USE_NATIVE_TOOLBAR
584 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
585 if (ownToolbarInstalled)
586 {
587 bResult = (HIViewIsVisible( (HIViewRef)m_macHIToolbarRef ) != show);
588 ShowHideWindowToolbar( tlw, show, false );
589 }
590 else
591 #endif
592 bResult = wxToolBarBase::Show( show );
593 }
594
595 return bResult;
596 }
597
598 bool wxToolBar::IsShown() const
599 {
600 bool bResult;
601
602 #if wxMAC_USE_NATIVE_TOOLBAR
603 bool ownToolbarInstalled ;
604 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
605 if (ownToolbarInstalled)
606 bResult = HIViewIsVisible( (HIViewRef)m_macHIToolbarRef );
607 else
608 #endif
609 bResult = wxToolBarBase::IsShown();
610
611 return bResult;
612 }
613
614 void wxToolBar::DoGetSize( int *width, int *height ) const
615 {
616 #if wxMAC_USE_NATIVE_TOOLBAR
617 Rect boundsR;
618 bool ownToolbarInstalled;
619
620 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
621 if ( ownToolbarInstalled )
622 {
623 // TODO is this really a control ?
624 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
625 if ( width != NULL )
626 *width = boundsR.right - boundsR.left;
627 if ( height != NULL )
628 *height = boundsR.bottom - boundsR.top;
629 }
630 else
631 #endif
632 wxToolBarBase::DoGetSize( width, height );
633 }
634
635 void wxToolBar::SetWindowStyleFlag( long style )
636 {
637 wxToolBarBase::SetWindowStyleFlag( style );
638 #if wxMAC_USE_NATIVE_TOOLBAR
639 if (m_macHIToolbarRef != NULL)
640 {
641 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
642
643 if ( style & wxTB_NOICONS )
644 mode = kHIToolbarDisplayModeLabelOnly;
645 else if ( style & wxTB_TEXT )
646 mode = kHIToolbarDisplayModeIconAndLabel;
647 else
648 mode = kHIToolbarDisplayModeIconOnly;
649
650 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
651 }
652 #endif
653 }
654
655 #if wxMAC_USE_NATIVE_TOOLBAR
656 bool wxToolBar::MacWantsNativeToolbar()
657 {
658 return m_macUsesNativeToolbar;
659 }
660
661 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
662 {
663 bool bResultV = false;
664
665 if (ownToolbarInstalled != NULL)
666 *ownToolbarInstalled = false;
667
668 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
669 if (tlw != NULL)
670 {
671 HIToolbarRef curToolbarRef = NULL;
672 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
673 bResultV = ((err == 0) && (curToolbarRef != NULL));
674 if (bResultV && (ownToolbarInstalled != NULL))
675 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
676 }
677
678 return bResultV;
679 }
680
681 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
682 {
683 bool bResult = false;
684
685 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
686 if (tlw == NULL)
687 return bResult;
688
689 if (usesNative && (m_macHIToolbarRef == NULL))
690 return bResult;
691
692 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
693 return bResult;
694
695 // check the existing toolbar
696 HIToolbarRef curToolbarRef = NULL;
697 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
698 if (err != 0)
699 curToolbarRef = NULL;
700
701 m_macUsesNativeToolbar = usesNative;
702
703 if (m_macUsesNativeToolbar)
704 {
705 // only install toolbar if there isn't one installed already
706 if (curToolbarRef == NULL)
707 {
708 bResult = true;
709
710 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
711 ShowHideWindowToolbar( tlw, true, false );
712 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
713 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
714
715 // FIXME: which is best, which is necessary?
716 //
717 // m_peer->SetVisibility( false, true );
718 //
719 //
720 Rect r = { 0 , 0 , 0 , 0 };
721 //
722 //
723 m_peer->SetRect( &r );
724 //
725 // FIXME: which is best, which is necessary?
726 //
727 SetSize( wxSIZE_AUTO_WIDTH, 0 );
728 //
729 m_peer->SetVisibility( false, true );
730 wxToolBarBase::Show( false );
731 }
732 }
733 else
734 {
735 // only deinstall toolbar if this is the installed one
736 if (m_macHIToolbarRef == curToolbarRef)
737 {
738 bResult = true;
739
740 ShowHideWindowToolbar( tlw, false, false );
741 ChangeWindowAttributes( tlw, 0 , kWindowToolbarButtonAttribute );
742 SetWindowToolbar( tlw, NULL );
743
744 // FIXME: which is best, which is necessary?
745 m_peer->SetVisibility( true, true );
746
747 //
748 // wxToolBarBase::Show( true );
749 //
750 }
751 }
752
753 if (bResult)
754 InvalidateBestSize();
755
756 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
757 return bResult;
758 }
759 #endif
760
761 bool wxToolBar::Realize()
762 {
763 if (m_tools.GetCount() == 0)
764 return false;
765
766 int x = m_xMargin + kwxMacToolBarLeftMargin;
767 int y = m_yMargin + kwxMacToolBarTopMargin;
768
769 int tw, th;
770 GetSize( &tw, &th );
771
772 int maxWidth = 0;
773 int maxHeight = 0;
774
775 int maxToolWidth = 0;
776 int maxToolHeight = 0;
777
778 // find the maximum tool width and height
779 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
780 while ( node != NULL )
781 {
782 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
783
784 if ( tool != NULL )
785 {
786 wxSize sz = tool->GetSize();
787
788 if ( sz.x > maxToolWidth )
789 maxToolWidth = sz.x;
790 if ( sz.y > maxToolHeight )
791 maxToolHeight = sz.y;
792 }
793
794 node = node->GetNext();
795 }
796
797 bool lastIsRadio = false;
798 bool curIsRadio = false;
799 bool setChoiceInGroup = false;
800
801 node = m_tools.GetFirst();
802 while ( node != NULL )
803 {
804 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
805
806 if ( tool == NULL )
807 {
808 node = node->GetNext();
809 continue;
810 }
811
812 // set tool position
813 // for the moment just perform a single row/column alignment
814 wxSize cursize = tool->GetSize();
815 if ( x + cursize.x > maxWidth )
816 maxWidth = x + cursize.x;
817 if ( y + cursize.y > maxHeight )
818 maxHeight = y + cursize.y;
819
820 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
821 {
822 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
823 tool->SetPosition( wxPoint(x1, y) );
824 }
825 else
826 {
827 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
828 tool->SetPosition( wxPoint(x, y1) );
829 }
830
831 // update the item positioning state
832 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
833 y += cursize.y + kwxMacToolSpacing;
834 else
835 x += cursize.x + kwxMacToolSpacing;
836
837 #if wxMAC_USE_NATIVE_TOOLBAR
838 // install in native HIToolbar
839 if ( m_macHIToolbarRef != NULL )
840 {
841 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
842 if ( hiItemRef != NULL )
843 {
844 OSStatus result = HIToolbarAppendItem( (HIToolbarRef) m_macHIToolbarRef, hiItemRef );
845 if ( result == 0 )
846 {
847 InstallEventHandler( HIObjectGetEventTarget(hiItemRef), GetwxMacToolBarEventHandlerUPP(),
848 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
849 }
850 }
851 }
852 #endif
853
854 // update radio button (and group) state
855 lastIsRadio = curIsRadio;
856 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
857
858 if ( !curIsRadio )
859 {
860 if ( tool->IsToggled() )
861 DoToggleTool( tool, true );
862
863 setChoiceInGroup = false;
864 }
865 else
866 {
867 if ( !lastIsRadio )
868 {
869 if ( tool->Toggle(true) )
870 {
871 DoToggleTool( tool, true );
872 setChoiceInGroup = true;
873 }
874 }
875 else if ( tool->IsToggled() )
876 {
877 if ( tool->IsToggled() )
878 DoToggleTool( tool, true );
879
880 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
881 while ( nodePrev != NULL )
882 {
883 wxToolBarToolBase *toggleTool = nodePrev->GetData();
884 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
885 break;
886
887 if ( toggleTool->Toggle(false) )
888 DoToggleTool( toggleTool, false );
889
890 nodePrev = nodePrev->GetPrevious();
891 }
892 }
893 }
894
895 node = node->GetNext();
896 }
897
898 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
899 {
900 // if not set yet, only one row
901 if ( m_maxRows <= 0 )
902 SetRows( 1 );
903
904 m_minWidth = maxWidth;
905 maxWidth = tw;
906 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
907 m_minHeight = m_maxHeight = maxHeight;
908 }
909 else
910 {
911 // if not set yet, have one column
912 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
913 SetRows( GetToolsCount() );
914
915 m_minHeight = maxHeight;
916 maxHeight = th;
917 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
918 m_minWidth = m_maxWidth = maxWidth;
919 }
920
921 #if 0
922 // FIXME: should this be OSX-only?
923 {
924 bool wantNativeToolbar, ownToolbarInstalled;
925
926 // attempt to install the native toolbar
927 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
928 MacInstallNativeToolbar( wantNativeToolbar );
929 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
930 if (!ownToolbarInstalled)
931 {
932 SetSize( maxWidth, maxHeight );
933 InvalidateBestSize();
934 }
935 }
936 #else
937 SetSize( maxWidth, maxHeight );
938 InvalidateBestSize();
939 #endif
940 return true;
941 }
942
943 void wxToolBar::SetToolBitmapSize(const wxSize& size)
944 {
945 m_defaultWidth = size.x + kwxMacToolBorder;
946 m_defaultHeight = size.y + kwxMacToolBorder;
947
948 #if wxMAC_USE_NATIVE_TOOLBAR
949 if (m_macHIToolbarRef != NULL)
950 {
951 int maxs = wxMax( size.x, size.y );
952 // TODO CHECK
953 HIToolbarDisplaySize sizeSpec = ((maxs > 16) ? kHIToolbarDisplaySizeNormal : kHIToolbarDisplaySizeSmall);
954 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
955 }
956 #endif
957 }
958
959 // The button size is bigger than the bitmap size
960 wxSize wxToolBar::GetToolSize() const
961 {
962 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
963 }
964
965 void wxToolBar::SetRows(int nRows)
966 {
967 // avoid resizing the frame uselessly
968 if ( nRows != m_maxRows )
969 {
970 m_maxRows = nRows;
971 }
972 }
973
974 void wxToolBar::MacSuperChangedPosition()
975 {
976 wxWindow::MacSuperChangedPosition();
977 #if wxMAC_USE_NATIVE_TOOLBAR
978 if (! m_macUsesNativeToolbar )
979 #endif
980 {
981 Realize();
982 }
983 }
984
985 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
986 {
987 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
988 while ( node != NULL )
989 {
990 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
991
992 if (tool != NULL)
993 {
994 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
995 if ( r.Contains( wxPoint( x, y ) ) )
996 return tool;
997 }
998
999 node = node->GetNext();
1000 }
1001
1002 return (wxToolBarToolBase *)NULL;
1003 }
1004
1005 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1006 {
1007 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
1008 if ( tool != NULL )
1009 return tool->GetShortHelp() ;
1010
1011 return wxEmptyString ;
1012 }
1013
1014 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1015 {
1016 if ( t != NULL )
1017 ((wxToolBarTool*)t)->DoEnable( enable ) ;
1018 }
1019
1020 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1021 {
1022 wxToolBarTool *tool = (wxToolBarTool *)t;
1023 if ( ( tool != NULL ) && tool->IsButton() )
1024 tool->UpdateToggleImage( toggle );
1025 }
1026
1027 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
1028 wxToolBarToolBase *toolBase)
1029 {
1030 wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolBase );
1031 if (tool == NULL)
1032 return false;
1033
1034 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1035 wxSize toolSize = GetToolSize();
1036 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x };
1037 ControlRef controlHandle = NULL;
1038 OSStatus err = 0;
1039
1040 switch (tool->GetStyle())
1041 {
1042 case wxTOOL_STYLE_SEPARATOR :
1043 {
1044 wxASSERT( tool->GetControlHandle() == NULL );
1045 toolSize.x /= 4;
1046 toolSize.y /= 4;
1047 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1048 toolrect.bottom = toolSize.y;
1049 else
1050 toolrect.right = toolSize.x;
1051
1052 #ifdef __WXMAC_OSX__
1053 // in flat style we need a visual separator
1054 #if wxMAC_USE_NATIVE_TOOLBAR
1055 HIToolbarItemRef item;
1056 err = HIToolbarItemCreate( kHIToolbarSeparatorIdentifier, kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates, &item );
1057 if (err == noErr)
1058 tool->SetToolbarItemRef( item );
1059 #endif
1060 CreateSeparatorControl( window, &toolrect, &controlHandle );
1061 tool->SetControlHandle( controlHandle );
1062 #endif
1063 }
1064 break;
1065
1066 case wxTOOL_STYLE_BUTTON :
1067 {
1068 wxASSERT( tool->GetControlHandle() == NULL ) ;
1069 ControlButtonContentInfo info ;
1070 wxMacCreateBitmapButton( &info , tool->GetNormalBitmap() , kControlContentIconRef ) ;
1071
1072 if ( UMAGetSystemVersion() >= 0x1000)
1073 CreateIconControl( window , &toolrect , &info , false , &controlHandle ) ;
1074 else
1075 {
1076 SInt16 behaviour = kControlBehaviorOffsetContents ;
1077 if ( tool->CanBeToggled() )
1078 behaviour += kControlBehaviorToggles ;
1079 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
1080 0 , 0 , 0 , &controlHandle ) ;
1081 }
1082
1083 #if wxMAC_USE_NATIVE_TOOLBAR
1084 HIToolbarItemRef item ;
1085 wxString labelStr;
1086 labelStr.Format(wxT("%xd"), (int)tool);
1087 err = HIToolbarItemCreate(
1088 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1089 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1090 if (err == noErr)
1091 {
1092 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1093 HIToolbarItemSetIconRef( item, info.u.iconRef );
1094 HIToolbarItemSetCommandID( item, tool->GetId() );
1095 tool->SetToolbarItemRef( item );
1096 }
1097 #endif
1098
1099 wxMacReleaseBitmapButton( &info ) ;
1100 /*
1101 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
1102 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
1103 */
1104
1105 InstallControlEventHandler( (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1106 GetEventTypeCount(eventList), eventList, tool, NULL );
1107
1108 tool->SetControlHandle( controlHandle );
1109 }
1110 break;
1111
1112 case wxTOOL_STYLE_CONTROL :
1113 wxASSERT( tool->GetControl() != NULL );
1114 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1115 // FIXME: doesn't work yet...
1116 {
1117 HIToolbarItemRef item;
1118 wxString labelStr;
1119 labelStr.Format( wxT("%xd"), (int) tool );
1120 result = HIToolbarItemCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1121 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates,
1122 &item );
1123 if ( result == 0 )
1124 {
1125 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1126 HIToolbarItemSetCommandID( item, tool->GetId() );
1127 tool->SetToolbarItemRef( item );
1128
1129 controlHandle = ( ControlRef ) tool->GetControlHandle();
1130 wxASSERT_MSG( controlHandle != NULL, wxT("NULL tool control") );
1131
1132 // FIXME: is this necessary ??
1133 ::GetControlBounds( controlHandle, &toolrect );
1134 UMAMoveControl( controlHandle, -toolrect.left, -toolrect.top );
1135
1136 // FIXME: is this necessary ??
1137 InstallControlEventHandler( controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1138 GetEventTypeCount(eventList), eventList, tool, NULL );
1139 }
1140 }
1141
1142 #else
1143 // FIXME: right now there's nothing to do here
1144 #endif
1145 break;
1146
1147 default :
1148 break;
1149 }
1150
1151 if ( err == 0 )
1152 {
1153 if ( controlHandle )
1154 {
1155 ControlRef container = (ControlRef) GetHandle();
1156 wxASSERT_MSG( container != NULL, wxT("No valid mac container control") );
1157
1158 UMAShowControl( controlHandle );
1159 ::EmbedControl( controlHandle, container );
1160 }
1161
1162 if ( tool->CanBeToggled() && tool->IsToggled() )
1163 tool->UpdateToggleImage( true );
1164
1165 // nothing special to do here - we relayout in Realize() later
1166 tool->Attach(this);
1167 InvalidateBestSize();
1168 }
1169 else
1170 {
1171 wxString errMsg;
1172 errMsg.Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err );
1173 wxASSERT_MSG( false, errMsg.c_str() );
1174 }
1175
1176 return( err == 0 );
1177 }
1178
1179 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1180 {
1181 wxFAIL_MSG( _T("not implemented") );
1182 }
1183
1184 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1185 {
1186 wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolbase ) ;
1187 wxToolBarToolsList::compatibility_iterator node;
1188 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1189 {
1190 wxToolBarToolBase *tool2 = node->GetData();
1191 if ( tool2 == tool )
1192 {
1193 // let node point to the next node in the list
1194 node = node->GetNext();
1195
1196 break;
1197 }
1198 }
1199
1200 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
1201
1202 tool->Detach();
1203
1204 switch ( tool->GetStyle() )
1205 {
1206 case wxTOOL_STYLE_CONTROL:
1207 {
1208 tool->GetControl()->Destroy();
1209 tool->ClearControl() ;
1210 }
1211 break;
1212
1213 case wxTOOL_STYLE_BUTTON:
1214 case wxTOOL_STYLE_SEPARATOR:
1215 if ( tool->GetControlHandle() )
1216 {
1217 DisposeControl( (ControlRef) tool->GetControlHandle() ) ;
1218 #if wxMAC_USE_NATIVE_TOOLBAR
1219 if ( tool->GetToolbarItemRef() )
1220 CFRelease( tool->GetToolbarItemRef() ) ;
1221 #endif
1222 tool->ClearControl() ;
1223 }
1224 break;
1225
1226 default:
1227 break;
1228 }
1229
1230 // and finally reposition all the controls after this one
1231
1232 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
1233 {
1234 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1235 wxPoint pt = tool2->GetPosition() ;
1236
1237 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1238 pt.y -= sz.y ;
1239 else
1240 pt.x -= sz.x ;
1241
1242 tool2->SetPosition( pt ) ;
1243 }
1244
1245 InvalidateBestSize();
1246 return true ;
1247 }
1248
1249 void wxToolBar::OnPaint(wxPaintEvent& event)
1250 {
1251 #if wxMAC_USE_NATIVE_TOOLBAR
1252 if ( m_macUsesNativeToolbar )
1253 {
1254 event.Skip(true) ;
1255 return ;
1256 }
1257 #endif
1258
1259 wxPaintDC dc(this) ;
1260
1261 int w, h ;
1262 GetSize( &w , &h ) ;
1263 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1264 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1265 {
1266 if ( UMAGetSystemVersion() >= 0x1030 )
1267 {
1268 HIThemePlacardDrawInfo info ;
1269 memset( &info, 0 , sizeof( info ) ) ;
1270 info.version = 0 ;
1271 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
1272
1273 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef() ;
1274 HIRect rect = CGRectMake( 0 , 0 , w , h ) ;
1275 HIThemeDrawPlacard( &rect , & info , cgContext, kHIThemeOrientationNormal) ;
1276 }
1277 }
1278 else
1279 {
1280 // leave the background as it is (striped or metal)
1281 }
1282 #else
1283 wxMacPortSetter helper(&dc) ;
1284
1285 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
1286 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
1287 /*
1288 if( toolbarrect.left < 0 )
1289 toolbarrect.left = 0 ;
1290 if ( toolbarrect.top < 0 )
1291 toolbarrect.top = 0 ;
1292 */
1293 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1294 {
1295 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1296 }
1297 else
1298 {
1299 #if TARGET_API_MAC_OSX
1300 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1301 if ( UMAGetSystemVersion() >= 0x1030 )
1302 {
1303 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
1304 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
1305 CGContextRef cgContext ;
1306 Rect bounds ;
1307 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
1308 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
1309 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
1310 CGContextScaleCTM( cgContext , 1 , -1 ) ;
1311
1312 {
1313 HIThemeBackgroundDrawInfo drawInfo ;
1314 drawInfo.version = 0 ;
1315 drawInfo.state = kThemeStateActive ;
1316 drawInfo.kind = kThemeBackgroundMetal ;
1317 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
1318 }
1319
1320 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
1321 }
1322 else
1323 #endif
1324 {
1325 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1326 }
1327 #endif
1328 }
1329 #endif
1330
1331 event.Skip() ;
1332 }
1333
1334 #endif // wxUSE_TOOLBAR
1335