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