]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
As reported by Chris Elliott some releases of Lesstif crash
[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 = wxString::Format(wxT("%xd"), (int)this);
534 err = HIToolbarCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding() ) , 0 ,
535 (HIToolbarRef*) &m_macHIToolbarRef );
536
537 if (m_macHIToolbarRef != NULL)
538 {
539 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault ;
540 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall ;
541
542 if ( style & wxTB_NOICONS )
543 mode = kHIToolbarDisplayModeLabelOnly ;
544 else if ( style & wxTB_TEXT )
545 mode = kHIToolbarDisplayModeIconAndLabel ;
546 else
547 mode = kHIToolbarDisplayModeIconOnly ;
548
549 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef , mode ) ;
550 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef , displaySize ) ;
551 }
552 #endif
553
554 return (err == 0);
555 }
556
557 wxToolBar::~wxToolBar()
558 {
559 #if wxMAC_USE_NATIVE_TOOLBAR
560 if ( m_macHIToolbarRef )
561 {
562 // if this is the installed toolbar, then deinstall it
563 if (m_macUsesNativeToolbar)
564 MacInstallNativeToolbar( false );
565
566 CFRelease( (HIToolbarRef) m_macHIToolbarRef );
567 m_macHIToolbarRef = NULL;
568 }
569 #endif
570 }
571
572 bool wxToolBar::Show( bool show )
573 {
574 bool bResult;
575 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
576
577 bResult = (tlw != NULL);
578 if (bResult)
579 {
580 #if wxMAC_USE_NATIVE_TOOLBAR
581 bool ownToolbarInstalled = false;
582 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
583 if (ownToolbarInstalled)
584 {
585 bResult = (HIViewIsVisible( (HIViewRef)m_macHIToolbarRef ) != show);
586 ShowHideWindowToolbar( tlw, show, false );
587 }
588 else
589 #endif
590 bResult = wxToolBarBase::Show( show );
591 }
592
593 return bResult;
594 }
595
596 bool wxToolBar::IsShown() const
597 {
598 bool bResult;
599
600 #if wxMAC_USE_NATIVE_TOOLBAR
601 bool ownToolbarInstalled ;
602 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
603 if (ownToolbarInstalled)
604 bResult = HIViewIsVisible( (HIViewRef)m_macHIToolbarRef );
605 else
606 #endif
607 bResult = wxToolBarBase::IsShown();
608
609 return bResult;
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 HIToolbarDisplaySize sizeSpec ;
951 if ( maxs > 32 )
952 sizeSpec = kHIToolbarDisplaySizeNormal ;
953 else if ( maxs > 24 )
954 sizeSpec = kHIToolbarDisplaySizeDefault ;
955 else
956 sizeSpec = kHIToolbarDisplaySizeSmall ;
957
958 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
959 }
960 #endif
961 }
962
963 // The button size is bigger than the bitmap size
964 wxSize wxToolBar::GetToolSize() const
965 {
966 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
967 }
968
969 void wxToolBar::SetRows(int nRows)
970 {
971 // avoid resizing the frame uselessly
972 if ( nRows != m_maxRows )
973 {
974 m_maxRows = nRows;
975 }
976 }
977
978 void wxToolBar::MacSuperChangedPosition()
979 {
980 wxWindow::MacSuperChangedPosition();
981 #if wxMAC_USE_NATIVE_TOOLBAR
982 if (! m_macUsesNativeToolbar )
983 #endif
984 {
985 Realize();
986 }
987 }
988
989 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
990 {
991 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
992 while ( node != NULL )
993 {
994 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
995
996 if (tool != NULL)
997 {
998 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
999 if ( r.Contains( wxPoint( x, y ) ) )
1000 return tool;
1001 }
1002
1003 node = node->GetNext();
1004 }
1005
1006 return (wxToolBarToolBase *)NULL;
1007 }
1008
1009 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1010 {
1011 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
1012 if ( tool != NULL )
1013 return tool->GetShortHelp() ;
1014
1015 return wxEmptyString ;
1016 }
1017
1018 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1019 {
1020 if ( t != NULL )
1021 ((wxToolBarTool*)t)->DoEnable( enable ) ;
1022 }
1023
1024 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1025 {
1026 wxToolBarTool *tool = (wxToolBarTool *)t;
1027 if ( ( tool != NULL ) && tool->IsButton() )
1028 tool->UpdateToggleImage( toggle );
1029 }
1030
1031 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
1032 wxToolBarToolBase *toolBase)
1033 {
1034 wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolBase );
1035 if (tool == NULL)
1036 return false;
1037
1038 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1039 wxSize toolSize = GetToolSize();
1040 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x };
1041 ControlRef controlHandle = NULL;
1042 OSStatus err = 0;
1043
1044 switch (tool->GetStyle())
1045 {
1046 case wxTOOL_STYLE_SEPARATOR :
1047 {
1048 wxASSERT( tool->GetControlHandle() == NULL );
1049 toolSize.x /= 4;
1050 toolSize.y /= 4;
1051 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1052 toolrect.bottom = toolSize.y;
1053 else
1054 toolrect.right = toolSize.x;
1055
1056 #ifdef __WXMAC_OSX__
1057 // in flat style we need a visual separator
1058 #if wxMAC_USE_NATIVE_TOOLBAR
1059 HIToolbarItemRef item;
1060 err = HIToolbarItemCreate( kHIToolbarSeparatorIdentifier, kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates, &item );
1061 if (err == noErr)
1062 tool->SetToolbarItemRef( item );
1063 #endif
1064 CreateSeparatorControl( window, &toolrect, &controlHandle );
1065 tool->SetControlHandle( controlHandle );
1066 #endif
1067 }
1068 break;
1069
1070 case wxTOOL_STYLE_BUTTON :
1071 {
1072 wxASSERT( tool->GetControlHandle() == NULL ) ;
1073 ControlButtonContentInfo info ;
1074 wxMacCreateBitmapButton( &info , tool->GetNormalBitmap() , kControlContentIconRef ) ;
1075
1076 if ( UMAGetSystemVersion() >= 0x1000)
1077 CreateIconControl( window , &toolrect , &info , false , &controlHandle ) ;
1078 else
1079 {
1080 SInt16 behaviour = kControlBehaviorOffsetContents ;
1081 if ( tool->CanBeToggled() )
1082 behaviour += kControlBehaviorToggles ;
1083 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
1084 0 , 0 , 0 , &controlHandle ) ;
1085 }
1086
1087 #if wxMAC_USE_NATIVE_TOOLBAR
1088 HIToolbarItemRef item ;
1089 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
1090 err = HIToolbarItemCreate(
1091 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1092 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1093 if (err == noErr)
1094 {
1095 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1096 HIToolbarItemSetIconRef( item, info.u.iconRef );
1097 HIToolbarItemSetCommandID( item, tool->GetId() );
1098 tool->SetToolbarItemRef( item );
1099 }
1100 #endif
1101
1102 wxMacReleaseBitmapButton( &info ) ;
1103 /*
1104 SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ;
1105 UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ;
1106 */
1107
1108 InstallControlEventHandler( (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1109 GetEventTypeCount(eventList), eventList, tool, NULL );
1110
1111 tool->SetControlHandle( controlHandle );
1112 }
1113 break;
1114
1115 case wxTOOL_STYLE_CONTROL :
1116 wxASSERT( tool->GetControl() != NULL );
1117 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1118 // FIXME: doesn't work yet...
1119 {
1120 HIToolbarItemRef item;
1121 wxString labelStr = wxString::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 = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err );
1174 wxASSERT_MSG( false, errMsg.c_str() );
1175 }
1176
1177 return( err == 0 );
1178 }
1179
1180 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1181 {
1182 wxFAIL_MSG( _T("not implemented") );
1183 }
1184
1185 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1186 {
1187 wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolbase ) ;
1188 wxToolBarToolsList::compatibility_iterator node;
1189 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1190 {
1191 wxToolBarToolBase *tool2 = node->GetData();
1192 if ( tool2 == tool )
1193 {
1194 // let node point to the next node in the list
1195 node = node->GetNext();
1196
1197 break;
1198 }
1199 }
1200
1201 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
1202
1203 tool->Detach();
1204
1205 switch ( tool->GetStyle() )
1206 {
1207 case wxTOOL_STYLE_CONTROL:
1208 {
1209 tool->GetControl()->Destroy();
1210 tool->ClearControl() ;
1211 }
1212 break;
1213
1214 case wxTOOL_STYLE_BUTTON:
1215 case wxTOOL_STYLE_SEPARATOR:
1216 if ( tool->GetControlHandle() )
1217 {
1218 DisposeControl( (ControlRef) tool->GetControlHandle() ) ;
1219 #if wxMAC_USE_NATIVE_TOOLBAR
1220 if ( tool->GetToolbarItemRef() )
1221 CFRelease( tool->GetToolbarItemRef() ) ;
1222 #endif
1223 tool->ClearControl() ;
1224 }
1225 break;
1226
1227 default:
1228 break;
1229 }
1230
1231 // and finally reposition all the controls after this one
1232
1233 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
1234 {
1235 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1236 wxPoint pt = tool2->GetPosition() ;
1237
1238 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1239 pt.y -= sz.y ;
1240 else
1241 pt.x -= sz.x ;
1242
1243 tool2->SetPosition( pt ) ;
1244 }
1245
1246 InvalidateBestSize();
1247 return true ;
1248 }
1249
1250 void wxToolBar::OnPaint(wxPaintEvent& event)
1251 {
1252 #if wxMAC_USE_NATIVE_TOOLBAR
1253 if ( m_macUsesNativeToolbar )
1254 {
1255 event.Skip(true) ;
1256 return ;
1257 }
1258 #endif
1259
1260 wxPaintDC dc(this) ;
1261
1262 int w, h ;
1263 GetSize( &w , &h ) ;
1264 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1265 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1266 {
1267 if ( UMAGetSystemVersion() >= 0x1030 )
1268 {
1269 HIThemePlacardDrawInfo info ;
1270 memset( &info, 0 , sizeof( info ) ) ;
1271 info.version = 0 ;
1272 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ;
1273
1274 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef() ;
1275 HIRect rect = CGRectMake( 0 , 0 , w , h ) ;
1276 HIThemeDrawPlacard( &rect , & info , cgContext, kHIThemeOrientationNormal) ;
1277 }
1278 }
1279 else
1280 {
1281 // leave the background as it is (striped or metal)
1282 }
1283 #else
1284 wxMacPortSetter helper(&dc) ;
1285
1286 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
1287 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
1288 /*
1289 if( toolbarrect.left < 0 )
1290 toolbarrect.left = 0 ;
1291 if ( toolbarrect.top < 0 )
1292 toolbarrect.top = 0 ;
1293 */
1294 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
1295 {
1296 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1297 }
1298 else
1299 {
1300 #if TARGET_API_MAC_OSX
1301 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1302 if ( UMAGetSystemVersion() >= 0x1030 )
1303 {
1304 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
1305 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
1306 CGContextRef cgContext ;
1307 Rect bounds ;
1308 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
1309 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
1310 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
1311 CGContextScaleCTM( cgContext , 1 , -1 ) ;
1312
1313 {
1314 HIThemeBackgroundDrawInfo drawInfo ;
1315 drawInfo.version = 0 ;
1316 drawInfo.state = kThemeStateActive ;
1317 drawInfo.kind = kThemeBackgroundMetal ;
1318 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
1319 }
1320
1321 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
1322 }
1323 else
1324 #endif
1325 {
1326 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
1327 }
1328 #endif
1329 }
1330 #endif
1331
1332 event.Skip() ;
1333 }
1334
1335 #endif // wxUSE_TOOLBAR
1336