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