]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
removing non-compositing code, adding individual transparency
[wxWidgets.git] / src / mac / carbon / toolbar.cpp
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__
27 const short kwxMacToolBarToolDefaultWidth = 16;
28 const short kwxMacToolBarToolDefaultHeight = 16;
29 const short kwxMacToolBarTopMargin = 4;
30 const short kwxMacToolBarLeftMargin = 4;
31 const short kwxMacToolBorder = 0;
32 const short kwxMacToolSpacing = 6;
33 #else
34 const short kwxMacToolBarToolDefaultWidth = 24;
35 const short kwxMacToolBarToolDefaultHeight = 22;
36 const short kwxMacToolBarTopMargin = 2;
37 const short kwxMacToolBarLeftMargin = 2;
38 const short kwxMacToolBorder = 4;
39 const short kwxMacToolSpacing = 0;
40 #endif
41
42
43 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
44
45 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
46 EVT_PAINT( wxToolBar::OnPaint )
47 END_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
60 class wxToolBarTool : public wxToolBarToolBase
61 {
62 public:
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
186 private:
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
208 static const EventTypeSpec eventList[] =
209 {
210 { kEventClassControl, kEventControlHit },
211 #ifdef __WXMAC_OSX__
212 { kEventClassControl, kEventControlHitTest },
213 #endif
214 };
215
216 static 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
272 static 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
289 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
290
291 #if wxMAC_USE_NATIVE_TOOLBAR
292
293 static const EventTypeSpec toolBarEventList[] =
294 {
295 { kEventClassToolbarItem, kEventToolbarItemPerformAction },
296 };
297
298 static 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
332 static 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
349 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler )
350
351 #endif
352
353 bool 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
385 void 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 ( IsButton() )
396 {
397 Rect contrlRect;
398 GetControlBounds( m_controlHandle, &contrlRect );
399 int former_mac_x = contrlRect.left;
400 int former_mac_y = contrlRect.top;
401 GetToolBar()->GetToolSize();
402
403 if ( mac_x != former_mac_x || mac_y != former_mac_y )
404 {
405 UMAMoveControl( m_controlHandle, mac_x, mac_y );
406 }
407 }
408 else if ( IsControl() )
409 {
410 GetControl()->Move( position );
411 }
412 else
413 {
414 // separator
415 #ifdef __WXMAC_OSX__
416 Rect contrlRect;
417 GetControlBounds( m_controlHandle, &contrlRect );
418 int former_mac_x = contrlRect.left;
419 int former_mac_y = contrlRect.top;
420
421 if ( mac_x != former_mac_x || mac_y != former_mac_y )
422 UMAMoveControl( m_controlHandle, mac_x, mac_y );
423 #endif
424 }
425 }
426
427 void wxToolBarTool::UpdateToggleImage( bool toggle )
428 {
429 #if wxMAC_USE_NATIVE_TOOLBAR
430
431 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
432 #define kHIToolbarItemSelected (1 << 7)
433 #endif
434
435 // FIXME: this should be a OSX v10.4 runtime check
436 if (m_toolbarItemRef != NULL)
437 {
438 OptionBits addAttrs, removeAttrs;
439 OSStatus result;
440
441 if (toggle)
442 {
443 addAttrs = kHIToolbarItemSelected;
444 removeAttrs = kHIToolbarItemNoAttributes;
445 }
446 else
447 {
448 addAttrs = kHIToolbarItemNoAttributes;
449 removeAttrs = kHIToolbarItemSelected;
450 }
451
452 result = HIToolbarItemChangeAttributes( m_toolbarItemRef, addAttrs, removeAttrs );
453 }
454 #endif
455
456 #ifdef __WXMAC_OSX__
457 if ( toggle )
458 {
459 int w = m_bmpNormal.GetWidth();
460 int h = m_bmpNormal.GetHeight();
461 wxBitmap bmp( w, h );
462 wxMemoryDC dc;
463
464 dc.SelectObject( bmp );
465 dc.SetPen( wxNullPen );
466 dc.SetBackground( *wxWHITE );
467 dc.DrawRectangle( 0, 0, w, h );
468 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
469 dc.SelectObject( wxNullBitmap );
470 ControlButtonContentInfo info;
471 wxMacCreateBitmapButton( &info, bmp );
472 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
473 wxMacReleaseBitmapButton( &info );
474 }
475 else
476 {
477 ControlButtonContentInfo info;
478 wxMacCreateBitmapButton( &info, m_bmpNormal );
479 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
480 wxMacReleaseBitmapButton( &info );
481 }
482
483 IconTransformType transform = toggle ? kTransformSelected : kTransformNone;
484 SetControlData(
485 m_controlHandle, 0, kControlIconTransformTag,
486 sizeof(transform), (Ptr)&transform );
487 HIViewSetNeedsDisplay( m_controlHandle, true );
488
489 #else
490 ::SetControl32BitValue( m_controlHandle, toggle );
491 #endif
492 }
493
494 wxToolBarTool::wxToolBarTool(
495 wxToolBar *tbar,
496 int id,
497 const wxString& label,
498 const wxBitmap& bmpNormal,
499 const wxBitmap& bmpDisabled,
500 wxItemKind kind,
501 wxObject *clientData,
502 const wxString& shortHelp,
503 const wxString& longHelp )
504 :
505 wxToolBarToolBase(
506 tbar, id, label, bmpNormal, bmpDisabled, kind,
507 clientData, shortHelp, longHelp )
508 {
509 Init();
510 }
511
512 #pragma mark -
513 #pragma mark Toolbar Implementation
514
515 wxToolBarToolBase *wxToolBar::CreateTool(
516 int id,
517 const wxString& label,
518 const wxBitmap& bmpNormal,
519 const wxBitmap& bmpDisabled,
520 wxItemKind kind,
521 wxObject *clientData,
522 const wxString& shortHelp,
523 const wxString& longHelp )
524 {
525 return new wxToolBarTool(
526 this, id, label, bmpNormal, bmpDisabled, kind,
527 clientData, shortHelp, longHelp );
528 }
529
530 wxToolBarToolBase * wxToolBar::CreateTool( wxControl *control )
531 {
532 return new wxToolBarTool( this, control );
533 }
534
535 void wxToolBar::Init()
536 {
537 m_maxWidth = -1;
538 m_maxHeight = -1;
539 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
540 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
541
542 #if wxMAC_USE_NATIVE_TOOLBAR
543 m_macHIToolbarRef = NULL;
544 m_macUsesNativeToolbar = false;
545 #endif
546 }
547
548 // also for the toolbar we have the dual implementation:
549 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
550 //
551 bool wxToolBar::Create(
552 wxWindow *parent,
553 wxWindowID id,
554 const wxPoint& pos,
555 const wxSize& size,
556 long style,
557 const wxString& name )
558 {
559 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
560 return false;
561
562 OSStatus err = noErr;
563
564 #if wxMAC_USE_NATIVE_TOOLBAR
565 wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
566 err = HIToolbarCreate(
567 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
568 (HIToolbarRef*) &m_macHIToolbarRef );
569
570 if (m_macHIToolbarRef != NULL)
571 {
572 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
573 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
574
575 if ( style & wxTB_NOICONS )
576 mode = kHIToolbarDisplayModeLabelOnly;
577 else if ( style & wxTB_TEXT )
578 mode = kHIToolbarDisplayModeIconAndLabel;
579 else
580 mode = kHIToolbarDisplayModeIconOnly;
581
582 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
583 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
584 }
585 #endif
586
587 return (err == noErr);
588 }
589
590 wxToolBar::~wxToolBar()
591 {
592 #if wxMAC_USE_NATIVE_TOOLBAR
593 if (m_macHIToolbarRef != NULL)
594 {
595 // if this is the installed toolbar, then deinstall it
596 if (m_macUsesNativeToolbar)
597 MacInstallNativeToolbar( false );
598
599 CFRelease( (HIToolbarRef)m_macHIToolbarRef );
600 m_macHIToolbarRef = NULL;
601 }
602 #endif
603 }
604
605 bool wxToolBar::Show( bool show )
606 {
607 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
608 bool bResult = (tlw != NULL);
609
610 if (bResult)
611 {
612 #if wxMAC_USE_NATIVE_TOOLBAR
613 bool ownToolbarInstalled = false;
614 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
615 if (ownToolbarInstalled)
616 {
617 bResult = (IsWindowToolbarVisible( tlw ) != show);
618 if ( bResult )
619 ShowHideWindowToolbar( tlw, show, false );
620 }
621 else
622 bResult = wxToolBarBase::Show( show );
623 #else
624
625 bResult = wxToolBarBase::Show( show );
626 #endif
627 }
628
629 return bResult;
630 }
631
632 bool wxToolBar::IsShown() const
633 {
634 bool bResult;
635
636 #if wxMAC_USE_NATIVE_TOOLBAR
637 bool ownToolbarInstalled;
638
639 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
640 if (ownToolbarInstalled)
641 {
642 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
643 bResult = IsWindowToolbarVisible( tlw );
644 }
645 else
646 bResult = wxToolBarBase::IsShown();
647 #else
648
649 bResult = wxToolBarBase::IsShown();
650 #endif
651
652 return bResult;
653 }
654
655 void wxToolBar::DoGetSize( int *width, int *height ) const
656 {
657 #if wxMAC_USE_NATIVE_TOOLBAR
658 Rect boundsR;
659 bool ownToolbarInstalled;
660
661 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
662 if ( ownToolbarInstalled )
663 {
664 // TODO: is this really a control ?
665 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
666 if ( width != NULL )
667 *width = boundsR.right - boundsR.left;
668 if ( height != NULL )
669 *height = boundsR.bottom - boundsR.top;
670 }
671 else
672 wxToolBarBase::DoGetSize( width, height );
673
674 #else
675 wxToolBarBase::DoGetSize( width, height );
676 #endif
677 }
678
679 wxSize wxToolBar::DoGetBestSize() const
680 {
681 int width, height;
682
683 DoGetSize( &width, &height );
684
685 return wxSize( width, height );
686 }
687
688 void wxToolBar::SetWindowStyleFlag( long style )
689 {
690 wxToolBarBase::SetWindowStyleFlag( style );
691
692 #if wxMAC_USE_NATIVE_TOOLBAR
693 if (m_macHIToolbarRef != NULL)
694 {
695 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
696
697 if ( style & wxTB_NOICONS )
698 mode = kHIToolbarDisplayModeLabelOnly;
699 else if ( style & wxTB_TEXT )
700 mode = kHIToolbarDisplayModeIconAndLabel;
701 else
702 mode = kHIToolbarDisplayModeIconOnly;
703
704 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
705 }
706 #endif
707 }
708
709 #if wxMAC_USE_NATIVE_TOOLBAR
710 bool wxToolBar::MacWantsNativeToolbar()
711 {
712 return m_macUsesNativeToolbar;
713 }
714
715 bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
716 {
717 bool bResultV = false;
718
719 if (ownToolbarInstalled != NULL)
720 *ownToolbarInstalled = false;
721
722 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
723 if (tlw != NULL)
724 {
725 HIToolbarRef curToolbarRef = NULL;
726 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
727 bResultV = ((err == noErr) && (curToolbarRef != NULL));
728 if (bResultV && (ownToolbarInstalled != NULL))
729 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
730 }
731
732 return bResultV;
733 }
734
735 bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
736 {
737 bool bResult = false;
738
739 if (usesNative && (m_macHIToolbarRef == NULL))
740 return bResult;
741
742 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
743 return bResult;
744
745 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
746 if (tlw == NULL)
747 return bResult;
748
749 // check the existing toolbar
750 HIToolbarRef curToolbarRef = NULL;
751 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
752 if (err != noErr)
753 curToolbarRef = NULL;
754
755 m_macUsesNativeToolbar = usesNative;
756
757 if (m_macUsesNativeToolbar)
758 {
759 // only install toolbar if there isn't one installed already
760 if (curToolbarRef == NULL)
761 {
762 bResult = true;
763
764 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
765 ShowHideWindowToolbar( tlw, true, false );
766 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
767 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
768
769 Rect r = { 0, 0, 0, 0 };
770 m_peer->SetRect( &r );
771 SetSize( wxSIZE_AUTO_WIDTH, 0 );
772 m_peer->SetVisibility( false, true );
773 wxToolBarBase::Show( false );
774 }
775 }
776 else
777 {
778 // only deinstall toolbar if this is the installed one
779 if (m_macHIToolbarRef == curToolbarRef)
780 {
781 bResult = true;
782
783 ShowHideWindowToolbar( tlw, false, false );
784 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
785 SetWindowToolbar( tlw, NULL );
786
787 m_peer->SetVisibility( true, true );
788 }
789 }
790
791 if (bResult)
792 InvalidateBestSize();
793
794 // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
795 return bResult;
796 }
797 #endif
798
799 bool wxToolBar::Realize()
800 {
801 if (m_tools.GetCount() == 0)
802 return false;
803
804 int maxWidth = 0;
805 int maxHeight = 0;
806
807 int maxToolWidth = 0;
808 int maxToolHeight = 0;
809
810 int x = m_xMargin + kwxMacToolBarLeftMargin;
811 int y = m_yMargin + kwxMacToolBarTopMargin;
812
813 int tw, th;
814 GetSize( &tw, &th );
815
816 // find the maximum tool width and height
817 wxToolBarTool *tool;
818 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
819 while ( node != NULL )
820 {
821 tool = (wxToolBarTool *) node->GetData();
822 if ( tool != NULL )
823 {
824 wxSize sz = tool->GetSize();
825
826 if ( sz.x > maxToolWidth )
827 maxToolWidth = sz.x;
828 if ( sz.y > maxToolHeight )
829 maxToolHeight = sz.y;
830 }
831
832 node = node->GetNext();
833 }
834
835 bool lastIsRadio = false;
836 bool curIsRadio = false;
837 bool setChoiceInGroup = false;
838
839 #if wxMAC_USE_NATIVE_TOOLBAR
840 CFIndex currentPosition = 0;
841 bool insertAll = false;
842 #endif
843
844 node = m_tools.GetFirst();
845 while ( node != NULL )
846 {
847 tool = (wxToolBarTool*) node->GetData();
848 if ( tool == NULL )
849 {
850 node = node->GetNext();
851 continue;
852 }
853
854 // set tool position:
855 // for the moment just perform a single row/column alignment
856 wxSize cursize = tool->GetSize();
857 if ( x + cursize.x > maxWidth )
858 maxWidth = x + cursize.x;
859 if ( y + cursize.y > maxHeight )
860 maxHeight = y + cursize.y;
861
862 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
863 {
864 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
865 tool->SetPosition( wxPoint(x1, y) );
866 }
867 else
868 {
869 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
870 tool->SetPosition( wxPoint(x, y1) );
871 }
872
873 // update the item positioning state
874 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
875 y += cursize.y + kwxMacToolSpacing;
876 else
877 x += cursize.x + kwxMacToolSpacing;
878
879 #if wxMAC_USE_NATIVE_TOOLBAR
880 // install in native HIToolbar
881 if ( m_macHIToolbarRef != NULL )
882 {
883 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
884 if ( hiItemRef != NULL )
885 {
886 if ( insertAll || (tool->GetIndex() != currentPosition) )
887 {
888 OSStatus err = noErr;
889 if ( !insertAll )
890 {
891 insertAll = true;
892
893 // if this is the first tool that gets newly inserted or repositioned
894 // first remove all 'old' tools from here to the right, because of this
895 // all following tools will have to be reinserted (insertAll). i = 100 because there's
896 // no way to determine how many there are in a toolbar, so just a high number :-(
897 for ( CFIndex i = 100; i >= currentPosition; --i )
898 {
899 err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i );
900 }
901
902 if (err != noErr)
903 {
904 wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err );
905 wxFAIL_MSG( errMsg.c_str() );
906 }
907 }
908
909 err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition );
910 if (err != noErr)
911 {
912 wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
913 wxFAIL_MSG( errMsg.c_str() );
914 }
915
916 tool->SetIndex( currentPosition );
917 }
918
919 currentPosition++;
920 }
921 }
922 #endif
923
924 // update radio button (and group) state
925 lastIsRadio = curIsRadio;
926 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
927
928 if ( !curIsRadio )
929 {
930 if ( tool->IsToggled() )
931 DoToggleTool( tool, true );
932
933 setChoiceInGroup = false;
934 }
935 else
936 {
937 if ( !lastIsRadio )
938 {
939 if ( tool->Toggle( true ) )
940 {
941 DoToggleTool( tool, true );
942 setChoiceInGroup = true;
943 }
944 }
945 else if ( tool->IsToggled() )
946 {
947 if ( tool->IsToggled() )
948 DoToggleTool( tool, true );
949
950 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
951 while ( nodePrev != NULL )
952 {
953 wxToolBarToolBase *toggleTool = nodePrev->GetData();
954 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
955 break;
956
957 if ( toggleTool->Toggle( false ) )
958 DoToggleTool( toggleTool, false );
959
960 nodePrev = nodePrev->GetPrevious();
961 }
962 }
963 }
964
965 node = node->GetNext();
966 }
967
968 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
969 {
970 // if not set yet, only one row
971 if ( m_maxRows <= 0 )
972 SetRows( 1 );
973
974 m_minWidth = maxWidth;
975 maxWidth = tw;
976 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
977 m_minHeight = m_maxHeight = maxHeight;
978 }
979 else
980 {
981 // if not set yet, have one column
982 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
983 SetRows( GetToolsCount() );
984
985 m_minHeight = maxHeight;
986 maxHeight = th;
987 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
988 m_minWidth = m_maxWidth = maxWidth;
989 }
990
991 #if 0
992 // FIXME: should this be OSX-only?
993 {
994 bool wantNativeToolbar, ownToolbarInstalled;
995
996 // attempt to install the native toolbar
997 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
998 MacInstallNativeToolbar( wantNativeToolbar );
999 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1000 if (!ownToolbarInstalled)
1001 {
1002 SetSize( maxWidth, maxHeight );
1003 InvalidateBestSize();
1004 }
1005 }
1006 #else
1007 SetSize( maxWidth, maxHeight );
1008 InvalidateBestSize();
1009 #endif
1010
1011 SetBestFittingSize();
1012
1013 return true;
1014 }
1015
1016 void wxToolBar::SetToolBitmapSize(const wxSize& size)
1017 {
1018 m_defaultWidth = size.x + kwxMacToolBorder;
1019 m_defaultHeight = size.y + kwxMacToolBorder;
1020
1021 #if wxMAC_USE_NATIVE_TOOLBAR
1022 if (m_macHIToolbarRef != NULL)
1023 {
1024 int maxs = wxMax( size.x, size.y );
1025 HIToolbarDisplaySize sizeSpec;
1026 if ( maxs > 32 )
1027 sizeSpec = kHIToolbarDisplaySizeNormal;
1028 else if ( maxs > 24 )
1029 sizeSpec = kHIToolbarDisplaySizeDefault;
1030 else
1031 sizeSpec = kHIToolbarDisplaySizeSmall;
1032
1033 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
1034 }
1035 #endif
1036 }
1037
1038 // The button size is bigger than the bitmap size
1039 wxSize wxToolBar::GetToolSize() const
1040 {
1041 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
1042 }
1043
1044 void wxToolBar::SetRows(int nRows)
1045 {
1046 // avoid resizing the frame uselessly
1047 if ( nRows != m_maxRows )
1048 m_maxRows = nRows;
1049 }
1050
1051 void wxToolBar::MacSuperChangedPosition()
1052 {
1053 wxWindow::MacSuperChangedPosition();
1054
1055 #if wxMAC_USE_NATIVE_TOOLBAR
1056 if (! m_macUsesNativeToolbar )
1057 Realize();
1058 #else
1059
1060 Realize();
1061 #endif
1062 }
1063
1064 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
1065 {
1066 wxToolBarTool *tool;
1067 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
1068 while ( node != NULL )
1069 {
1070 tool = (wxToolBarTool *)node->GetData();
1071 if (tool != NULL)
1072 {
1073 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1074 if ( r.Contains( wxPoint( x, y ) ) )
1075 return tool;
1076 }
1077
1078 node = node->GetNext();
1079 }
1080
1081 return (wxToolBarToolBase*)NULL;
1082 }
1083
1084 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1085 {
1086 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
1087 if ( tool != NULL )
1088 return tool->GetShortHelp();
1089
1090 return wxEmptyString;
1091 }
1092
1093 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
1094 {
1095 if ( t != NULL )
1096 ((wxToolBarTool*)t)->DoEnable( enable );
1097 }
1098
1099 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
1100 {
1101 wxToolBarTool *tool = (wxToolBarTool *)t;
1102 if ( ( tool != NULL ) && tool->IsButton() )
1103 tool->UpdateToggleImage( toggle );
1104 }
1105
1106 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
1107 {
1108 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
1109 if (tool == NULL)
1110 return false;
1111
1112 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1113 wxSize toolSize = GetToolSize();
1114 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
1115 ControlRef controlHandle = NULL;
1116 OSStatus err = 0;
1117
1118 switch (tool->GetStyle())
1119 {
1120 case wxTOOL_STYLE_SEPARATOR:
1121 {
1122 wxASSERT( tool->GetControlHandle() == NULL );
1123 toolSize.x /= 4;
1124 toolSize.y /= 4;
1125 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1126 toolrect.bottom = toolSize.y;
1127 else
1128 toolrect.right = toolSize.x;
1129
1130 #ifdef __WXMAC_OSX__
1131 // in flat style we need a visual separator
1132 #if wxMAC_USE_NATIVE_TOOLBAR
1133 HIToolbarItemRef item;
1134 err = HIToolbarItemCreate(
1135 kHIToolbarSeparatorIdentifier,
1136 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1137 &item );
1138 if (err == noErr)
1139 tool->SetToolbarItemRef( item );
1140 #endif
1141
1142 CreateSeparatorControl( window, &toolrect, &controlHandle );
1143 tool->SetControlHandle( controlHandle );
1144 #endif
1145 }
1146 break;
1147
1148 case wxTOOL_STYLE_BUTTON:
1149 {
1150 wxASSERT( tool->GetControlHandle() == NULL );
1151 ControlButtonContentInfo info;
1152 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
1153
1154 if ( UMAGetSystemVersion() >= 0x1000)
1155 {
1156 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1157 }
1158 else
1159 {
1160 SInt16 behaviour = kControlBehaviorOffsetContents;
1161 if ( tool->CanBeToggled() )
1162 behaviour |= kControlBehaviorToggles;
1163 err = CreateBevelButtonControl( window,
1164 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1165 behaviour, &info, 0, 0, 0, &controlHandle );
1166 }
1167
1168 #if wxMAC_USE_NATIVE_TOOLBAR
1169 HIToolbarItemRef item;
1170 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
1171 err = HIToolbarItemCreate(
1172 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1173 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1174 if (err == noErr)
1175 {
1176 InstallEventHandler(
1177 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1178 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
1179 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1180 HIToolbarItemSetIconRef( item, info.u.iconRef );
1181 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
1182 tool->SetToolbarItemRef( item );
1183 }
1184 #endif
1185
1186 wxMacReleaseBitmapButton( &info );
1187
1188 #if 0
1189 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1190 UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );
1191 #endif
1192
1193 InstallControlEventHandler(
1194 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1195 GetEventTypeCount(eventList), eventList, tool, NULL );
1196
1197 tool->SetControlHandle( controlHandle );
1198 }
1199 break;
1200
1201 case wxTOOL_STYLE_CONTROL:
1202 wxASSERT( tool->GetControl() != NULL );
1203
1204 #if 0 // wxMAC_USE_NATIVE_TOOLBAR
1205 // FIXME: doesn't work yet...
1206 {
1207 HIToolbarItemRef item;
1208 wxString labelStr = wxString::Format( wxT("%xd"), (int)tool );
1209 result = HIToolbarItemCreate(
1210 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ),
1211 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates,
1212 &item );
1213 if ( result == noErr )
1214 {
1215 HIToolbarItemSetLabel( item, wxMacCFStringHolder( tool->GetLabel(), m_font.GetEncoding() ) );
1216 HIToolbarItemSetCommandID( item, tool->GetId() );
1217 tool->SetToolbarItemRef( item );
1218
1219 controlHandle = ( ControlRef ) tool->GetControlHandle();
1220 wxASSERT_MSG( controlHandle != NULL, wxT("NULL tool control") );
1221
1222 // FIXME: is this necessary ??
1223 ::GetControlBounds( controlHandle, &toolrect );
1224 UMAMoveControl( controlHandle, -toolrect.left, -toolrect.top );
1225
1226 // FIXME: is this necessary ??
1227 InstallControlEventHandler(
1228 controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
1229 GetEventTypeCount(eventList), eventList, tool, NULL );
1230 }
1231 }
1232
1233 #else
1234 // FIXME: right now there's nothing to do here
1235 #endif
1236 break;
1237
1238 default:
1239 break;
1240 }
1241
1242 if ( err == noErr )
1243 {
1244 if ( controlHandle )
1245 {
1246 ControlRef container = (ControlRef) GetHandle();
1247 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
1248
1249 UMAShowControl( controlHandle );
1250 ::EmbedControl( controlHandle, container );
1251 }
1252
1253 if ( tool->CanBeToggled() && tool->IsToggled() )
1254 tool->UpdateToggleImage( true );
1255
1256 // nothing special to do here - we relayout in Realize() later
1257 tool->Attach( this );
1258 InvalidateBestSize();
1259 }
1260 else
1261 {
1262 wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err );
1263 wxFAIL_MSG( errMsg.c_str() );
1264 }
1265
1266 return (err == noErr);
1267 }
1268
1269 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
1270 {
1271 wxFAIL_MSG( wxT("not implemented") );
1272 }
1273
1274 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
1275 {
1276 wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase );
1277 wxToolBarToolsList::compatibility_iterator node;
1278 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1279 {
1280 wxToolBarToolBase *tool2 = node->GetData();
1281 if ( tool2 == tool )
1282 {
1283 // let node point to the next node in the list
1284 node = node->GetNext();
1285
1286 break;
1287 }
1288 }
1289
1290 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
1291
1292 tool->Detach();
1293
1294 #if wxMAC_USE_NATIVE_TOOLBAR
1295 CFIndex removeIndex = tool->GetIndex();
1296 #endif
1297
1298 switch ( tool->GetStyle() )
1299 {
1300 case wxTOOL_STYLE_CONTROL:
1301 {
1302 tool->GetControl()->Destroy();
1303 tool->ClearControl();
1304 }
1305 break;
1306
1307 case wxTOOL_STYLE_BUTTON:
1308 case wxTOOL_STYLE_SEPARATOR:
1309 if ( tool->GetControlHandle() )
1310 {
1311 #if wxMAC_USE_NATIVE_TOOLBAR
1312 if ( removeIndex != -1 && m_macHIToolbarRef )
1313 {
1314 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
1315 tool->SetIndex( -1 );
1316 }
1317 #endif
1318
1319 tool->ClearControl();
1320 }
1321 break;
1322
1323 default:
1324 break;
1325 }
1326
1327 // and finally reposition all the controls after this one
1328
1329 for ( /* node -> first after deleted */; node; node = node->GetNext() )
1330 {
1331 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
1332 wxPoint pt = tool2->GetPosition();
1333
1334 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1335 pt.y -= sz.y;
1336 else
1337 pt.x -= sz.x;
1338
1339 tool2->SetPosition( pt );
1340
1341 #if wxMAC_USE_NATIVE_TOOLBAR
1342 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
1343 tool2->SetIndex( tool2->GetIndex() - 1 );
1344 #endif
1345 }
1346
1347 InvalidateBestSize();
1348
1349 return true;
1350 }
1351
1352 void wxToolBar::OnPaint(wxPaintEvent& event)
1353 {
1354 #if wxMAC_USE_NATIVE_TOOLBAR
1355 if ( m_macUsesNativeToolbar )
1356 {
1357 event.Skip(true);
1358 return;
1359 }
1360 #endif
1361
1362 wxPaintDC dc(this);
1363
1364 int w, h;
1365 GetSize( &w, &h );
1366
1367 bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance();
1368 bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);
1369
1370 #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1371 if ( !drawMetalTheme && minimumUmaAvailable )
1372 {
1373 HIThemePlacardDrawInfo info;
1374 memset( &info, 0, sizeof(info) );
1375 info.version = 0;
1376 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1377
1378 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1379 HIRect rect = CGRectMake( 0, 0, w, h );
1380 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
1381 }
1382 else
1383 {
1384 // leave the background as it is (striped or metal)
1385 }
1386
1387 #else
1388
1389 const bool drawBorder = true;
1390
1391 if (drawBorder)
1392 {
1393 wxMacPortSetter helper( &dc );
1394
1395 if ( !drawMetalTheme || !minimumUmaAvailable )
1396 {
1397 Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1398 dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };
1399
1400 #if 0
1401 if ( toolbarrect.left < 0 )
1402 toolbarrect.left = 0;
1403 if ( toolbarrect.top < 0 )
1404 toolbarrect.top = 0;
1405 #endif
1406
1407 UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive );
1408 }
1409 else
1410 {
1411 #if TARGET_API_MAC_OSX
1412 HIRect hiToolbarrect = CGRectMake(
1413 dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1414 dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) );
1415 CGContextRef cgContext;
1416 Rect bounds;
1417
1418 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
1419 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1420
1421 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
1422 CGContextScaleCTM( cgContext, 1, -1 );
1423
1424 HIThemeBackgroundDrawInfo drawInfo;
1425 drawInfo.version = 0;
1426 drawInfo.state = kThemeStateActive;
1427 drawInfo.kind = kThemeBackgroundMetal;
1428 HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );
1429
1430 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
1431 #endif
1432 }
1433 }
1434 #endif
1435
1436 event.Skip();
1437 }
1438
1439 #endif // wxUSE_TOOLBAR