]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
Fixed label positions for horizontal slider [ modified patch 1102150 ], fixed value...
[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 // FIXME: override for testing
551 mode = kHIToolbarDisplayModeIconAndLabel ;
552 displaySize = kHIToolbarDisplaySizeDefault ;
553
554 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef , mode ) ;
555 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef , displaySize ) ;
556 }
557 #endif
558
559 return (err == 0);
560 }
561
562 wxToolBar::~wxToolBar()
563 {
564 #if wxMAC_USE_NATIVE_TOOLBAR
565 if ( m_macHIToolbarRef )
566 {
567 // if this is the installed toolbar, then deinstall it
568 if (m_macUsesNativeToolbar)
569 MacInstallNativeToolbar( false );
570
571 CFRelease( (HIToolbarRef) m_macHIToolbarRef );
572 m_macHIToolbarRef = NULL;
573 }
574 #endif
575 }
576
577 bool wxToolBar::Show( bool show )
578 {
579 bool bResult, ownToolbarInstalled = false;
580 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
581
582 bResult = (tlw != NULL);
583 if (bResult)
584 {
585 #if wxMAC_USE_NATIVE_TOOLBAR
586 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
587 if (ownToolbarInstalled)
588 {
589 bResult = (HIViewIsVisible( (HIViewRef)m_macHIToolbarRef ) != show);
590 ShowHideWindowToolbar( tlw, show, false );
591 }
592 else
593 #endif
594 bResult = wxToolBarBase::Show( show );
595 }
596
597 return bResult;
598 }
599
600 bool wxToolBar::IsShown() const
601 {
602 bool bResult;
603
604 #if wxMAC_USE_NATIVE_TOOLBAR
605 bool ownToolbarInstalled ;
606 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
607 if (ownToolbarInstalled)
608 bResult = HIViewIsVisible( (HIViewRef)m_macHIToolbarRef );
609 else
610 #endif
611 bResult = wxToolBarBase::IsShown();
612
613 return bResult;
614 }
615
616 void wxToolBar::DoGetSize( int *width, int *height ) const
617 {
618 #if wxMAC_USE_NATIVE_TOOLBAR
619 Rect boundsR;
620 bool ownToolbarInstalled;
621
622 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
623 if ( ownToolbarInstalled )
624 {
625 // TODO is this really a control ?
626 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
627 if ( width != NULL )
628 *width = boundsR.right - boundsR.left;
629 if ( height != NULL )
630 *height = boundsR.bottom - boundsR.top;
631 }
632 else
633 #endif
634 wxToolBarBase::DoGetSize( width, height );
635 }
636
637 void wxToolBar::SetWindowStyleFlag( long style )
638 {
639 wxToolBarBase::SetWindowStyleFlag( style );
640 #if wxMAC_USE_NATIVE_TOOLBAR
641 if (m_macHIToolbarRef != NULL)
642 {
643 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
644
645 if ( style & wxTB_NOICONS )
646 mode = kHIToolbarDisplayModeLabelOnly;
647 else if ( style & wxTB_TEXT )
648 mode = kHIToolbarDisplayModeIconAndLabel;
649 else
650 mode = kHIToolbarDisplayModeIconOnly;
651
652 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
653 }
654 #endif
655 }
656
657 #if wxMAC_USE_NATIVE_TOOLBAR
658 bool wxToolBar::MacWantsNativeToolbar()
659 {
660 return m_macUsesNativeToolbar;
661 }
662
663 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
664 {
665 bool bResultV = false;
666
667 if (ownToolbarInstalled != NULL)
668 *ownToolbarInstalled = false;
669
670 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
671 if (tlw != NULL)
672 {
673 HIToolbarRef curToolbarRef = NULL;
674 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
675 bResultV = ((err == 0) && (curToolbarRef != NULL));
676 if (bResultV && (ownToolbarInstalled != NULL))
677 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
678 }
679
680 return bResultV;
681 }
682
683 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
684 {
685 bool bResult = false;
686
687 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
688 if (tlw == NULL)
689 return bResult;
690
691 if (usesNative && (m_macHIToolbarRef == NULL))
692 return bResult;
693
694 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
695 return bResult;
696
697 // check the existing toolbar
698 HIToolbarRef curToolbarRef = NULL;
699 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
700 if (err != 0)
701 curToolbarRef = NULL;
702
703 m_macUsesNativeToolbar = usesNative;
704
705 if (m_macUsesNativeToolbar)
706 {
707 // only install toolbar if there isn't one installed already
708 if (curToolbarRef == NULL)
709 {
710 bResult = true;
711
712 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
713 ShowHideWindowToolbar( tlw, true, false );
714 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
715 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
716
717 // FIXME: which is best, which is necessary?
718 //
719 // m_peer->SetVisibility( false, true );
720 //
721 //
722 Rect r = { 0 , 0 , 0 , 0 };
723 //
724 //
725 m_peer->SetRect( &r );
726 //
727 // FIXME: which is best, which is necessary?
728 //
729 SetSize( wxSIZE_AUTO_WIDTH, 0 );
730 //
731 m_peer->SetVisibility( false, true );
732 wxToolBarBase::Show( false );
733 }
734 }
735 else
736 {
737 // only deinstall toolbar if this is the installed one
738 if (m_macHIToolbarRef == curToolbarRef)
739 {
740 bResult = true;
741
742 ShowHideWindowToolbar( tlw, false, false );
743 ChangeWindowAttributes( tlw, 0 , kWindowToolbarButtonAttribute );
744 SetWindowToolbar( tlw, NULL );
745
746 // FIXME: which is best, which is necessary?
747 m_peer->SetVisibility( true, true );
748
749 //
750 // wxToolBarBase::Show( true );
751 //
752 }
753 }
754
755 if (bResult)
756 InvalidateBestSize();
757
758 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
759 return bResult;
760 }
761 #endif
762
763 bool wxToolBar::Realize()
764 {
765 if (m_tools.GetCount() == 0)
766 return false;
767
768 int x = m_xMargin + kwxMacToolBarLeftMargin;
769 int y = m_yMargin + kwxMacToolBarTopMargin;
770
771 int tw, th;
772 GetSize( &tw, &th );
773
774 int maxWidth = 0;
775 int maxHeight = 0;
776
777 int maxToolWidth = 0;
778 int maxToolHeight = 0;
779
780 // find the maximum tool width and height
781 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
782 while ( node != NULL )
783 {
784 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
785
786 if ( tool != NULL )
787 {
788 wxSize sz = tool->GetSize();
789
790 if ( sz.x > maxToolWidth )
791 maxToolWidth = sz.x;
792 if ( sz.y > maxToolHeight )
793 maxToolHeight = sz.y;
794 }
795
796 node = node->GetNext();
797 }
798
799 bool lastIsRadio = false;
800 bool curIsRadio = false;
801 bool setChoiceInGroup = false;
802
803 node = m_tools.GetFirst();
804 while ( node != NULL )
805 {
806 wxToolBarTool *tool = (wxToolBarTool *) node->GetData();
807
808 if ( tool == NULL )
809 {
810 node = node->GetNext();
811 continue;
812 }
813
814 // set tool position
815 // for the moment just perform a single row/column alignment
816 wxSize cursize = tool->GetSize();
817 if ( x + cursize.x > maxWidth )
818 maxWidth = x + cursize.x;
819 if ( y + cursize.y > maxHeight )
820 maxHeight = y + cursize.y;
821
822 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
823 {
824 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
825 tool->SetPosition( wxPoint(x1, y) );
826 }
827 else
828 {
829 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
830 tool->SetPosition( wxPoint(x, y1) );
831 }
832
833 // update the item positioning state
834 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
835 y += cursize.y + kwxMacToolSpacing;
836 else
837 x += cursize.x + kwxMacToolSpacing;
838
839 #if wxMAC_USE_NATIVE_TOOLBAR
840 // install in native HIToolbar
841 if ( m_macHIToolbarRef != NULL )
842 {
843 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
844 if ( hiItemRef != NULL )
845 {
846 OSStatus result = HIToolbarAppendItem( (HIToolbarRef) m_macHIToolbarRef, hiItemRef );
847 if ( result == 0 )
848 {
849 InstallEventHandler( HIObjectGetEventTarget(hiItemRef), GetwxMacToolBarEventHandlerUPP(),
850 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
851 }
852 }
853 }
854 #endif
855
856 // update radio button (and group) state
857 lastIsRadio = curIsRadio;
858 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
859
860 if ( !curIsRadio )
861 {
862 if ( tool->IsToggled() )
863 DoToggleTool( tool, true );
864
865 setChoiceInGroup = false;
866 }
867 else
868 {
869 if ( !lastIsRadio )
870 {
871 if ( tool->Toggle(true) )
872 {
873 DoToggleTool( tool, true );
874 setChoiceInGroup = true;
875 }
876 }
877 else if ( tool->IsToggled() )
878 {
879 if ( tool->IsToggled() )
880 DoToggleTool( tool, true );
881
882 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
883 while ( nodePrev != NULL )
884 {
885 wxToolBarToolBase *toggleTool = nodePrev->GetData();
886 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
887 break;
888
889 if ( toggleTool->Toggle(false) )
890 DoToggleTool( toggleTool, false );
891
892 nodePrev = nodePrev->GetPrevious();
893 }
894 }
895 }
896
897 node = node->GetNext();
898 }
899
900 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
901 {
902 // if not set yet, only one row
903 if ( m_maxRows <= 0 )
904 SetRows( 1 );
905
906 m_minWidth = maxWidth;
907 maxWidth = tw;
908 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
909 m_minHeight = m_maxHeight = maxHeight;
910 }
911 else
912 {
913 // if not set yet, have one column
914 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
915 SetRows( GetToolsCount() );
916
917 m_minHeight = maxHeight;
918 maxHeight = th;
919 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
920 m_minWidth = m_maxWidth = maxWidth;
921 }
922
923 #if 0
924 // FIXME: should this be OSX-only?
925 {
926 bool wantNativeToolbar, ownToolbarInstalled;
927
928 // attempt to install the native toolbar
929 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
930 MacInstallNativeToolbar( wantNativeToolbar );
931 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
932 if (!ownToolbarInstalled)
933 {
934 SetSize( maxWidth, maxHeight );
935 InvalidateBestSize();
936 }
937 }
938 #else
939 SetSize( maxWidth, maxHeight );
940 InvalidateBestSize();
941 #endif
942 return true;
943 }
944
945 void wxToolBar::SetToolBitmapSize(const wxSize& size)
946 {
947 m_defaultWidth = size.x + kwxMacToolBorder;
948 m_defaultHeight = size.y + kwxMacToolBorder;
949
950 #if wxMAC_USE_NATIVE_TOOLBAR
951 if (m_macHIToolbarRef != NULL)
952 {
953 int maxs = wxMax( size.x, size.y );
954 // TODO CHECK
955 HIToolbarDisplaySize sizeSpec = ((maxs > 16) ? kHIToolbarDisplaySizeNormal : kHIToolbarDisplaySizeSmall);
956 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
957 }
958 #endif
959 }
960
961 // The button size is bigger than the bitmap size
962 wxSize wxToolBar::GetToolSize() const
963 {
964 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
965 }
966
967 void wxToolBar::SetRows(int nRows)
968 {
969 // avoid resizing the frame uselessly
970 if ( nRows != m_maxRows )
971 {
972 m_maxRows = nRows;
973 }
974 }
975
976 void wxToolBar::MacSuperChangedPosition()
977 {
978 wxWindow::MacSuperChangedPosition();
979 #if wxMAC_USE_NATIVE_TOOLBAR
980 if (! m_macUsesNativeToolbar )
981 #endif
982 {
983 Realize();
984 }
985 }
986
987 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
988 {
989 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
990 while ( node != NULL )
991 {
992 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
993
994 if (tool != NULL)
995 {
996 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
997 if ( r.Contains( wxPoint( x, y ) ) )
998 return tool;
999 }
1000
1001 node = node->GetNext();
1002 }
1003
1004 return (wxToolBarToolBase *)NULL;
1005 }
1006
1007 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1008 {
1009 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
1010 if ( tool != NULL )
1011 return tool->GetShortHelp() ;
1012
1013 return wxEmptyString ;
1014 }
1015
1016 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1017 {
1018 if ( t != NULL )
1019 ((wxToolBarTool*)t)->DoEnable( enable ) ;
1020 }
1021
1022 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1023 {
1024 wxToolBarTool *tool = (wxToolBarTool *)t;
1025 if ( ( tool != NULL ) && tool->IsButton() )
1026 tool->UpdateToggleImage( toggle );
1027 }
1028
1029 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
1030 wxToolBarToolBase *toolBase)
1031 {
1032 wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolBase );
1033 if (tool == NULL)
1034 return false;
1035
1036 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1037 wxSize toolSize = GetToolSize();
1038 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x };
1039 ControlRef controlHandle = NULL;
1040 OSStatus err = 0;
1041
1042 switch (tool->GetStyle())
1043 {
1044 case wxTOOL_STYLE_SEPARATOR :
1045 {
1046 wxASSERT( tool->GetControlHandle() == NULL );
1047 toolSize.x /= 4;
1048 toolSize.y /= 4;
1049 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1050 toolrect.bottom = toolSize.y;
1051 else
1052 toolrect.right = toolSize.x;
1053
1054 #ifdef __WXMAC_OSX__
1055 // in flat style we need a visual separator
1056 #if wxMAC_USE_NATIVE_TOOLBAR
1057 HIToolbarItemRef item;
1058 err = HIToolbarItemCreate( kHIToolbarSeparatorIdentifier, kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates, &item );
1059 if (err == noErr)
1060 tool->SetToolbarItemRef( item );
1061 #endif
1062 CreateSeparatorControl( window, &toolrect, &controlHandle );
1063 tool->SetControlHandle( controlHandle );
1064 #endif
1065 }
1066 break;
1067
1068 case wxTOOL_STYLE_BUTTON :
1069 {
1070 wxASSERT( tool->GetControlHandle() == NULL ) ;
1071 ControlButtonContentInfo info ;
1072 wxMacCreateBitmapButton( &info , tool->GetNormalBitmap() , kControlContentIconRef ) ;
1073
1074 if ( UMAGetSystemVersion() >= 0x1000)
1075 CreateIconControl( window , &toolrect , &info , false , &controlHandle ) ;
1076 else
1077 {
1078 SInt16 behaviour = kControlBehaviorOffsetContents ;
1079 if ( tool->CanBeToggled() )
1080 behaviour += kControlBehaviorToggles ;
1081 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
1082 0 , 0 , 0 , &controlHandle ) ;
1083 }
1084
1085 #if wxMAC_USE_NATIVE_TOOLBAR
1086 HIToolbarItemRef item ;
1087 wxString labelStr;
1088 labelStr.Format(wxT("%xd"), (int)tool);
1089 err = HIToolbarItemCreate(
1090 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1091 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1092 if (err == noErr)
1093 {
1094 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1095 HIToolbarItemSetIconRef( item, info.u.iconRef );
1096 HIToolbarItemSetCommandID( item, tool->GetId() );
1097 tool->SetToolbarItemRef( item );
1098 }
1099 #endif
1100
1101 wxMacReleaseBitmapButton( &info ) ;
1102 /*
1103 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
1104 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
1105 */
1106
1107 InstallControlEventHandler( (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1108 GetEventTypeCount(eventList), eventList, tool, NULL );
1109
1110 tool->SetControlHandle( controlHandle );
1111 }
1112 break;
1113
1114 case wxTOOL_STYLE_CONTROL :
1115 wxASSERT( tool->GetControl() != NULL );
1116 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1117 // FIXME: doesn't work yet...
1118 {
1119 HIToolbarItemRef item;
1120 wxString labelStr;
1121 labelStr.Format( wxT("%xd"), (int) tool );
1122 result = HIToolbarItemCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1123 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates,
1124 &item );
1125 if ( result == 0 )
1126 {
1127 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1128 HIToolbarItemSetCommandID( item, tool->GetId() );
1129 tool->SetToolbarItemRef( item );
1130
1131 controlHandle = ( ControlRef ) tool->GetControlHandle();
1132 wxASSERT_MSG( controlHandle != NULL, wxT("NULL tool control") );
1133
1134 // FIXME: is this necessary ??
1135 ::GetControlBounds( controlHandle, &toolrect );
1136 UMAMoveControl( controlHandle, -toolrect.left, -toolrect.top );
1137
1138 // FIXME: is this necessary ??
1139 InstallControlEventHandler( controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1140 GetEventTypeCount(eventList), eventList, tool, NULL );
1141 }
1142 }
1143
1144 #else
1145 // FIXME: right now there's nothing to do here
1146 #endif
1147 break;
1148
1149 default :
1150 break;
1151 }
1152
1153 if ( err == 0 )
1154 {
1155 if ( controlHandle )
1156 {
1157 ControlRef container = (ControlRef) GetHandle();
1158 wxASSERT_MSG( container != NULL, wxT("No valid mac container control") );
1159
1160 UMAShowControl( controlHandle );
1161 ::EmbedControl( controlHandle, container );
1162 }
1163
1164 if ( tool->CanBeToggled() && tool->IsToggled() )
1165 tool->UpdateToggleImage( true );
1166
1167 // nothing special to do here - we relayout in Realize() later
1168 tool->Attach(this);
1169 InvalidateBestSize();
1170 }
1171 else
1172 {
1173 wxString errMsg;
1174 errMsg.Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err );
1175 wxASSERT_MSG( false, errMsg.c_str() );
1176 }
1177
1178 return( err == 0 );
1179 }
1180
1181 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1182 {
1183 wxFAIL_MSG( _T("not implemented") );
1184 }
1185
1186 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1187 {
1188 wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolbase ) ;
1189 wxToolBarToolsList::compatibility_iterator node;
1190 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1191 {
1192 wxToolBarToolBase *tool2 = node->GetData();
1193 if ( tool2 == tool )
1194 {
1195 // let node point to the next node in the list
1196 node = node->GetNext();
1197
1198 break;
1199 }
1200 }
1201
1202 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
1203
1204 tool->Detach();
1205
1206 switch ( tool->GetStyle() )
1207 {
1208 case wxTOOL_STYLE_CONTROL:
1209 {
1210 tool->GetControl()->Destroy();
1211 tool->ClearControl() ;
1212 }
1213 break;
1214
1215 case wxTOOL_STYLE_BUTTON:
1216 case wxTOOL_STYLE_SEPARATOR:
1217 if ( tool->GetControlHandle() )
1218 {
1219 DisposeControl( (ControlRef) tool->GetControlHandle() ) ;
1220 #if wxMAC_USE_NATIVE_TOOLBAR
1221 if ( tool->GetToolbarItemRef() )
1222 CFRelease( tool->GetToolbarItemRef() ) ;
1223 #endif
1224 tool->ClearControl() ;
1225 }
1226 break;
1227
1228 default:
1229 break;
1230 }
1231
1232 // and finally reposition all the controls after this one
1233
1234 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
1235 {
1236 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1237 wxPoint pt = tool2->GetPosition() ;
1238
1239 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1240 pt.y -= sz.y ;
1241 else
1242 pt.x -= sz.x ;
1243
1244 tool2->SetPosition( pt ) ;
1245 }
1246
1247 InvalidateBestSize();
1248 return true ;
1249 }
1250
1251 void wxToolBar::OnPaint(wxPaintEvent& event)
1252 {
1253 #if wxMAC_USE_NATIVE_TOOLBAR
1254 if ( m_macUsesNativeToolbar )
1255 {
1256 event.Skip(true) ;
1257 return ;
1258 }
1259 #endif
1260
1261 wxPaintDC dc(this) ;
1262
1263 int w, h ;
1264 GetSize( &w , &h ) ;
1265 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1266 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1267 {
1268 if ( UMAGetSystemVersion() >= 0x1030 )
1269 {
1270 HIThemePlacardDrawInfo info ;
1271 memset( &info, 0 , sizeof( info ) ) ;
1272 info.version = 0 ;
1273 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
1274
1275 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef() ;
1276 HIRect rect = CGRectMake( 0 , 0 , w , h ) ;
1277 HIThemeDrawPlacard( &rect , & info , cgContext, kHIThemeOrientationNormal) ;
1278 }
1279 }
1280 else
1281 {
1282 // leave the background as it is (striped or metal)
1283 }
1284 #else
1285 wxMacPortSetter helper(&dc) ;
1286
1287 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
1288 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
1289 /*
1290 if( toolbarrect.left < 0 )
1291 toolbarrect.left = 0 ;
1292 if ( toolbarrect.top < 0 )
1293 toolbarrect.top = 0 ;
1294 */
1295 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1296 {
1297 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1298 }
1299 else
1300 {
1301 #if TARGET_API_MAC_OSX
1302 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1303 if ( UMAGetSystemVersion() >= 0x1030 )
1304 {
1305 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
1306 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
1307 CGContextRef cgContext ;
1308 Rect bounds ;
1309 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
1310 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
1311 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
1312 CGContextScaleCTM( cgContext , 1 , -1 ) ;
1313
1314 {
1315 HIThemeBackgroundDrawInfo drawInfo ;
1316 drawInfo.version = 0 ;
1317 drawInfo.state = kThemeStateActive ;
1318 drawInfo.kind = kThemeBackgroundMetal ;
1319 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
1320 }
1321
1322 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
1323 }
1324 else
1325 #endif
1326 {
1327 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1328 }
1329 #endif
1330 }
1331 #endif
1332
1333 event.Skip() ;
1334 }
1335
1336 #endif // wxUSE_TOOLBAR
1337