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