]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toolbar.cpp
compilation fix: don't try to convert wxString to bool (apparently unnecessarily)
[wxWidgets.git] / src / mac / carbon / toolbar.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
3b6a1179 2// Name: src/mac/carbon/toolbar.cpp
e9576ca5 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
e56d2520 16#include "wx/toolbar.h"
e9576ca5 17
4e3e485b
WS
18#ifndef WX_PRECOMP
19 #include "wx/wx.h"
20#endif
21
83f787ba 22#include "wx/app.h"
d497dca4 23#include "wx/mac/uma.h"
bfe9ffbc 24#include "wx/geometry.h"
ee799df7 25
991f71dc 26
ee799df7 27#ifdef __WXMAC_OSX__
3b6a1179
DS
28const short kwxMacToolBarToolDefaultWidth = 16;
29const short kwxMacToolBarToolDefaultHeight = 16;
30const short kwxMacToolBarTopMargin = 4;
31const short kwxMacToolBarLeftMargin = 4;
32const short kwxMacToolBorder = 0;
33const short kwxMacToolSpacing = 6;
ee799df7 34#else
3b6a1179
DS
35const short kwxMacToolBarToolDefaultWidth = 24;
36const short kwxMacToolBarToolDefaultHeight = 22;
37const short kwxMacToolBarTopMargin = 2;
38const short kwxMacToolBarLeftMargin = 2;
39const short kwxMacToolBorder = 4;
40const short kwxMacToolSpacing = 0;
ee799df7
SC
41#endif
42
991f71dc
DS
43
44IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
45
46BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
47 EVT_PAINT( wxToolBar::OnPaint )
48END_EVENT_TABLE()
49
50
e56d2520
SC
51#pragma mark -
52#pragma mark Tool Implementation
53
54
37e2cb08
SC
55// ----------------------------------------------------------------------------
56// private classes
57// ----------------------------------------------------------------------------
58
e56d2520
SC
59// We have a dual implementation for each tool, ControlRef and HIToolbarItemRef
60
37e2cb08
SC
61class wxToolBarTool : public wxToolBarToolBase
62{
63public:
3025abca
DS
64 wxToolBarTool(
65 wxToolBar *tbar,
66 int id,
67 const wxString& label,
68 const wxBitmap& bmpNormal,
69 const wxBitmap& bmpDisabled,
70 wxItemKind kind,
71 wxObject *clientData,
72 const wxString& shortHelp,
73 const wxString& longHelp );
f3a65c3e 74
cdb11cb9
VZ
75 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
76 : wxToolBarToolBase(tbar, control, label)
37e2cb08 77 {
3b6a1179 78 Init();
e56d2520 79 if (control != NULL)
3b6a1179 80 SetControlHandle( (ControlRef) control->GetHandle() );
37e2cb08 81 }
f3a65c3e 82
d3c7fc99 83 virtual ~wxToolBarTool()
bfe9ffbc 84 {
3b6a1179 85 ClearControl();
bfe9ffbc 86 }
f3a65c3e
VZ
87
88 WXWidget GetControlHandle()
89 {
3b6a1179 90 return (WXWidget) m_controlHandle;
e56d2520 91 }
f3a65c3e
VZ
92
93 void SetControlHandle( ControlRef handle )
94 {
3b6a1179 95 m_controlHandle = handle;
e56d2520 96 }
37e2cb08 97
3b6a1179 98 void SetPosition( const wxPoint& position );
f3a65c3e
VZ
99
100 void ClearControl()
101 {
3b6a1179 102 m_control = NULL;
8c07d8b3
SC
103 if ( m_controlHandle )
104 {
9d5ccdd3
SC
105 if ( !IsControl() )
106 DisposeControl( m_controlHandle );
107 else
108 {
109 // the embedded control is not under the responsibility of the tool
110 }
8c07d8b3
SC
111 m_controlHandle = NULL ;
112 }
991f71dc 113
e56d2520 114#if wxMAC_USE_NATIVE_TOOLBAR
507d5748
SC
115 if ( m_toolbarItemRef )
116 {
117 CFIndex count = CFGetRetainCount( m_toolbarItemRef ) ;
118 wxASSERT_MSG( count == 1 , wxT("Reference Count of native tool was not 1 in wxToolBarTool destructor") );
83f787ba
SC
119 wxTheApp->MacAddToAutorelease(m_toolbarItemRef);
120 CFRelease(m_toolbarItemRef);
507d5748
SC
121 m_toolbarItemRef = NULL;
122 }
f3a65c3e 123#endif
e56d2520 124 }
f3a65c3e 125
bfe9ffbc
SC
126 wxSize GetSize() const
127 {
3025abca
DS
128 wxSize curSize;
129
bfe9ffbc
SC
130 if ( IsControl() )
131 {
3025abca 132 curSize = GetControl()->GetSize();
bfe9ffbc
SC
133 }
134 else if ( IsButton() )
135 {
3025abca 136 curSize = GetToolBar()->GetToolSize();
bfe9ffbc
SC
137 }
138 else
139 {
abbcdf3f 140 // separator size
3025abca 141 curSize = GetToolBar()->GetToolSize();
5a904a32 142 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
3025abca 143 curSize.y /= 4;
5a904a32 144 else
3025abca 145 curSize.x /= 4;
bfe9ffbc 146 }
3025abca
DS
147
148 return curSize;
bfe9ffbc 149 }
991f71dc 150
bfe9ffbc
SC
151 wxPoint GetPosition() const
152 {
3b6a1179 153 return wxPoint( m_x, m_y );
f3a65c3e 154 }
991f71dc 155
3b6a1179 156 bool DoEnable( bool enable );
f3a65c3e 157
3b6a1179 158 void UpdateToggleImage( bool toggle );
f3a65c3e
VZ
159
160#if wxMAC_USE_NATIVE_TOOLBAR
161 void SetToolbarItemRef( HIToolbarItemRef ref )
162 {
e56d2520 163 if ( m_controlHandle )
3b6a1179 164 HideControl( m_controlHandle );
e56d2520 165 if ( m_toolbarItemRef )
3b6a1179 166 CFRelease( m_toolbarItemRef );
991f71dc 167
3b6a1179 168 m_toolbarItemRef = ref;
e56d2520
SC
169 if ( m_toolbarItemRef )
170 {
bbd321ff
RD
171 wxFont f;
172 wxFontEncoding enc;
173 if ( GetToolBar() )
174 f = GetToolBar()->GetFont();
175 if ( f.IsOk() )
176 enc = f.GetEncoding();
177 else
178 enc = wxFont::GetDefaultEncoding();
cdb11cb9 179
e56d2520 180 HIToolbarItemSetHelpText(
991f71dc 181 m_toolbarItemRef,
bbd321ff
RD
182 wxMacCFStringHolder( GetShortHelp(), enc ),
183 wxMacCFStringHolder( GetLongHelp(), enc ) );
e56d2520
SC
184 }
185 }
991f71dc 186
f3a65c3e
VZ
187 HIToolbarItemRef GetToolbarItemRef() const
188 {
3b6a1179 189 return m_toolbarItemRef;
e56d2520 190 }
df7998fc
VZ
191
192 void SetIndex( CFIndex idx )
2c1dbc95 193 {
3b6a1179 194 m_index = idx;
2c1dbc95
SC
195 }
196
df7998fc 197 CFIndex GetIndex() const
2c1dbc95 198 {
3b6a1179 199 return m_index;
2c1dbc95 200 }
e56d2520 201#endif
5d0bf05a 202
3b6a1179 203private:
f3a65c3e 204 void Init()
bfe9ffbc 205 {
3b6a1179 206 m_controlHandle = NULL;
991f71dc 207
e56d2520 208#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
209 m_toolbarItemRef = NULL;
210 m_index = -1;
e56d2520 211#endif
bfe9ffbc 212 }
991f71dc 213
3b6a1179 214 ControlRef m_controlHandle;
991f71dc
DS
215 wxCoord m_x;
216 wxCoord m_y;
217
e56d2520 218#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 219 HIToolbarItemRef m_toolbarItemRef;
2c1dbc95 220 // position in its toolbar, -1 means not inserted
3b6a1179 221 CFIndex m_index;
e56d2520 222#endif
37e2cb08
SC
223};
224
facd6764
SC
225static const EventTypeSpec eventList[] =
226{
3b6a1179 227 { kEventClassControl, kEventControlHit },
73fd9428 228#ifdef __WXMAC_OSX__
3b6a1179 229 { kEventClassControl, kEventControlHitTest },
73fd9428 230#endif
3b6a1179 231};
facd6764 232
3b6a1179 233static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
facd6764 234{
3b6a1179
DS
235 OSStatus result = eventNotHandledErr;
236 ControlRef controlRef;
237 wxMacCarbonEvent cEvent( event );
facd6764 238
3b6a1179 239 cEvent.GetParameter( kEventParamDirectObject, &controlRef );
facd6764 240
991f71dc 241 switch ( GetEventKind( event ) )
facd6764 242 {
3b6a1179 243 case kEventControlHit:
facd6764 244 {
3b6a1179
DS
245 wxToolBarTool *tbartool = (wxToolBarTool*)data;
246 wxToolBar *tbar = tbartool != NULL ? (wxToolBar*) (tbartool->GetToolBar()) : NULL;
991f71dc 247 if ((tbartool != NULL) && tbartool->CanBeToggled())
facd6764 248 {
e56d2520 249 bool shouldToggle;
991f71dc 250
64f553a4 251#ifdef __WXMAC_OSX__
e56d2520 252 shouldToggle = !tbartool->IsToggled();
64f553a4 253#else
3025abca 254 shouldToggle = (GetControl32BitValue( (ControlRef)(tbartool->GetControlHandle()) ) != 0);
64f553a4 255#endif
991f71dc 256
e56d2520 257 tbar->ToggleTool( tbartool->GetId(), shouldToggle );
facd6764 258 }
991f71dc 259
e56d2520
SC
260 if (tbartool != NULL)
261 tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() );
f3a65c3e 262 result = noErr;
facd6764 263 }
3b6a1179 264 break;
5d0bf05a 265
73fd9428 266#ifdef __WXMAC_OSX__
3b6a1179 267 case kEventControlHitTest:
73fd9428 268 {
3b6a1179
DS
269 HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation);
270 HIRect rect;
271 HIViewGetBounds( controlRef, &rect );
272
273 ControlPartCode pc = kControlNoPart;
274 if ( CGRectContainsPoint( rect, pt ) )
275 pc = kControlIconPart;
276 cEvent.SetParameter( kEventParamControlPart, typeControlPartCode, pc );
277 result = noErr;
73fd9428 278 }
3b6a1179 279 break;
73fd9428 280#endif
5d0bf05a 281
3b6a1179
DS
282 default:
283 break;
facd6764 284 }
991f71dc 285
3b6a1179 286 return result;
facd6764
SC
287}
288
3b6a1179 289static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
facd6764 290{
3b6a1179 291 OSStatus result = eventNotHandledErr;
facd6764
SC
292
293 switch ( GetEventClass( event ) )
294 {
3b6a1179
DS
295 case kEventClassControl:
296 result = wxMacToolBarToolControlEventHandler( handler, event, data );
297 break;
5d0bf05a 298
3b6a1179
DS
299 default:
300 break;
facd6764 301 }
991f71dc 302
3b6a1179 303 return result;
facd6764
SC
304}
305
306DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
307
e56d2520
SC
308#if wxMAC_USE_NATIVE_TOOLBAR
309
e56d2520
SC
310static const EventTypeSpec toolBarEventList[] =
311{
3b6a1179
DS
312 { kEventClassToolbarItem, kEventToolbarItemPerformAction },
313};
e56d2520 314
3b6a1179 315static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
e56d2520 316{
3b6a1179 317 OSStatus result = eventNotHandledErr;
f3a65c3e 318
991f71dc 319 switch ( GetEventKind( event ) )
e56d2520 320 {
3b6a1179 321 case kEventToolbarItemPerformAction:
e56d2520 322 {
3b6a1179 323 wxToolBarTool* tbartool = (wxToolBarTool*) data;
e56d2520
SC
324 if ( tbartool != NULL )
325 {
991f71dc
DS
326 wxToolBar *tbar = (wxToolBar*)(tbartool->GetToolBar());
327 int toolID = tbartool->GetId();
328
e56d2520
SC
329 if ( tbartool->CanBeToggled() )
330 {
3025abca 331 if ( tbar != NULL )
2c1dbc95 332 tbar->ToggleTool(toolID, !tbartool->IsToggled() );
e56d2520 333 }
991f71dc 334
3025abca 335 if ( tbar != NULL )
3b6a1179 336 tbar->OnLeftClick( toolID, tbartool->IsToggled() );
f3a65c3e 337 result = noErr;
e56d2520
SC
338 }
339 }
3b6a1179 340 break;
5d0bf05a 341
3b6a1179
DS
342 default:
343 break;
e56d2520 344 }
991f71dc 345
3b6a1179 346 return result;
e56d2520
SC
347}
348
349static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
350{
3b6a1179 351 OSStatus result = eventNotHandledErr;
991f71dc
DS
352
353 switch ( GetEventClass( event ) )
e56d2520 354 {
3b6a1179
DS
355 case kEventClassToolbarItem:
356 result = wxMacToolBarCommandEventHandler( handler, event, data );
357 break;
5d0bf05a 358
3b6a1179
DS
359 default:
360 break;
e56d2520 361 }
991f71dc 362
3b6a1179 363 return result;
e56d2520
SC
364}
365
366DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler )
367
368#endif
369
3b6a1179 370bool wxToolBarTool::DoEnable( bool enable )
a2fe01c9
SC
371{
372 if ( IsControl() )
373 {
3b6a1179 374 GetControl()->Enable( enable );
a2fe01c9
SC
375 }
376 else if ( IsButton() )
377 {
f3a65c3e 378#if wxMAC_USE_NATIVE_TOOLBAR
3025abca 379 if ( m_toolbarItemRef != NULL )
3b6a1179 380 HIToolbarItemSetEnabled( m_toolbarItemRef, enable );
e56d2520
SC
381#endif
382
3025abca 383 if ( m_controlHandle != NULL )
e56d2520 384 {
a2fe01c9 385#if TARGET_API_MAC_OSX
e56d2520 386 if ( enable )
3b6a1179 387 EnableControl( m_controlHandle );
e56d2520 388 else
3b6a1179 389 DisableControl( m_controlHandle );
a2fe01c9 390#else
e56d2520 391 if ( enable )
3b6a1179 392 ActivateControl( m_controlHandle );
e56d2520 393 else
3b6a1179 394 DeactivateControl( m_controlHandle );
a2fe01c9 395#endif
e56d2520 396 }
a2fe01c9 397 }
991f71dc 398
3b6a1179 399 return true;
a2fe01c9 400}
bfe9ffbc 401
3b6a1179 402void wxToolBarTool::SetPosition( const wxPoint& position )
bfe9ffbc
SC
403{
404 m_x = position.x;
405 m_y = position.y;
406
3b6a1179
DS
407 int mac_x = position.x;
408 int mac_y = position.y;
789ae0cf 409
789ae0cf
SC
410 if ( IsButton() )
411 {
3b6a1179
DS
412 Rect contrlRect;
413 GetControlBounds( m_controlHandle, &contrlRect );
414 int former_mac_x = contrlRect.left;
415 int former_mac_y = contrlRect.top;
416 GetToolBar()->GetToolSize();
f3a65c3e 417
bfe9ffbc
SC
418 if ( mac_x != former_mac_x || mac_y != former_mac_y )
419 {
3b6a1179 420 UMAMoveControl( m_controlHandle, mac_x, mac_y );
bfe9ffbc
SC
421 }
422 }
423 else if ( IsControl() )
424 {
9d5ccdd3
SC
425 // embedded native controls are moved by the OS
426#if wxMAC_USE_NATIVE_TOOLBAR
427 if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false )
428#endif
429 {
430 GetControl()->Move( position );
431 }
bfe9ffbc 432 }
abbcdf3f
SC
433 else
434 {
f3a65c3e 435 // separator
abbcdf3f 436#ifdef __WXMAC_OSX__
3b6a1179
DS
437 Rect contrlRect;
438 GetControlBounds( m_controlHandle, &contrlRect );
439 int former_mac_x = contrlRect.left;
440 int former_mac_y = contrlRect.top;
f3a65c3e 441
abbcdf3f 442 if ( mac_x != former_mac_x || mac_y != former_mac_y )
3b6a1179 443 UMAMoveControl( m_controlHandle, mac_x, mac_y );
abbcdf3f
SC
444#endif
445 }
bfe9ffbc
SC
446}
447
f3a65c3e 448void wxToolBarTool::UpdateToggleImage( bool toggle )
73fd9428
SC
449{
450#ifdef __WXMAC_OSX__
451 if ( toggle )
452 {
3b6a1179
DS
453 int w = m_bmpNormal.GetWidth();
454 int h = m_bmpNormal.GetHeight();
455 wxBitmap bmp( w, h );
456 wxMemoryDC dc;
457
458 dc.SelectObject( bmp );
ce9da810
RD
459 dc.SetPen( wxPen(*wxBLACK) );
460 dc.SetBrush( wxBrush( *wxLIGHT_GREY ));
3b6a1179
DS
461 dc.DrawRectangle( 0, 0, w, h );
462 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
463 dc.SelectObject( wxNullBitmap );
464 ControlButtonContentInfo info;
1fe8ec6d 465 wxMacCreateBitmapButton( &info, bmp, kControlContentIconRef );
991f71dc 466 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
ce9da810
RD
467#if wxMAC_USE_NATIVE_TOOLBAR
468 if (m_toolbarItemRef != NULL)
469 {
470 HIToolbarItemSetIconRef( m_toolbarItemRef, info.u.iconRef );
471 }
472#endif
3b6a1179 473 wxMacReleaseBitmapButton( &info );
73fd9428
SC
474 }
475 else
476 {
3b6a1179 477 ControlButtonContentInfo info;
1fe8ec6d 478 wxMacCreateBitmapButton( &info, m_bmpNormal, kControlContentIconRef );
3025abca 479 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
ce9da810
RD
480#if wxMAC_USE_NATIVE_TOOLBAR
481 if (m_toolbarItemRef != NULL)
482 {
483 HIToolbarItemSetIconRef( m_toolbarItemRef, info.u.iconRef );
484 }
485#endif
3b6a1179 486 wxMacReleaseBitmapButton( &info );
73fd9428
SC
487 }
488
3b6a1179
DS
489 IconTransformType transform = toggle ? kTransformSelected : kTransformNone;
490 SetControlData(
491 m_controlHandle, 0, kControlIconTransformTag,
3025abca 492 sizeof(transform), (Ptr)&transform );
3b6a1179 493 HIViewSetNeedsDisplay( m_controlHandle, true );
73fd9428
SC
494
495#else
3b6a1179 496 ::SetControl32BitValue( m_controlHandle, toggle );
73fd9428
SC
497#endif
498}
499
3b6a1179
DS
500wxToolBarTool::wxToolBarTool(
501 wxToolBar *tbar,
502 int id,
503 const wxString& label,
504 const wxBitmap& bmpNormal,
505 const wxBitmap& bmpDisabled,
506 wxItemKind kind,
507 wxObject *clientData,
508 const wxString& shortHelp,
509 const wxString& longHelp )
510 :
511 wxToolBarToolBase(
512 tbar, id, label, bmpNormal, bmpDisabled, kind,
513 clientData, shortHelp, longHelp )
bfe9ffbc 514{
f4e0be4f 515 Init();
bfe9ffbc
SC
516}
517
e56d2520
SC
518#pragma mark -
519#pragma mark Toolbar Implementation
2f1ae414 520
3b6a1179
DS
521wxToolBarToolBase *wxToolBar::CreateTool(
522 int id,
523 const wxString& label,
524 const wxBitmap& bmpNormal,
525 const wxBitmap& bmpDisabled,
526 wxItemKind kind,
527 wxObject *clientData,
528 const wxString& shortHelp,
529 const wxString& longHelp )
37e2cb08 530{
3b6a1179
DS
531 return new wxToolBarTool(
532 this, id, label, bmpNormal, bmpDisabled, kind,
533 clientData, shortHelp, longHelp );
37e2cb08
SC
534}
535
cdb11cb9
VZ
536wxToolBarToolBase *
537wxToolBar::CreateTool(wxControl *control, const wxString& label)
37e2cb08 538{
cdb11cb9 539 return new wxToolBarTool(this, control, label);
37e2cb08
SC
540}
541
37e2cb08 542void wxToolBar::Init()
e9576ca5 543{
e40298d5
JS
544 m_maxWidth = -1;
545 m_maxHeight = -1;
546 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
547 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
991f71dc 548
e56d2520 549#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
550 m_macHIToolbarRef = NULL;
551 m_macUsesNativeToolbar = false;
e56d2520 552#endif
e9576ca5
SC
553}
554
0ac091ae 555#define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
6d4835dc 556
cdb11cb9 557const EventTypeSpec kEvents[] =
6d4835dc 558{
0ac091ae
RD
559 { kEventClassHIObject, kEventHIObjectConstruct },
560 { kEventClassHIObject, kEventHIObjectInitialize },
561 { kEventClassHIObject, kEventHIObjectDestruct },
cdb11cb9 562
0ac091ae 563 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
6d4835dc
SC
564};
565
cdb11cb9
VZ
566const EventTypeSpec kViewEvents[] =
567{
568 { kEventClassControl, kEventControlGetSizeConstraints }
6d4835dc
SC
569};
570
cdb11cb9
VZ
571struct ControlToolbarItem
572{
573 HIToolbarItemRef toolbarItem;
6d4835dc
SC
574 HIViewRef viewRef;
575 wxSize lastValidSize ;
cdb11cb9 576};
6d4835dc
SC
577
578static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
579{
0ac091ae
RD
580 OSStatus result = eventNotHandledErr;
581 ControlToolbarItem* object = (ControlToolbarItem*)inUserData;
582
583 switch ( GetEventClass( inEvent ) )
584 {
585 case kEventClassHIObject:
586 switch ( GetEventKind( inEvent ) )
587 {
588 case kEventHIObjectConstruct:
589 {
590 HIObjectRef toolbarItem;
591 ControlToolbarItem* item;
cdb11cb9 592
0ac091ae 593 GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL,
6d4835dc 594 sizeof( HIObjectRef ), NULL, &toolbarItem );
cdb11cb9 595
6d4835dc
SC
596 item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ;
597 item->toolbarItem = toolbarItem ;
598 item->viewRef = NULL ;
cdb11cb9 599
0ac091ae 600 SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item );
cdb11cb9 601
6d4835dc 602 result = noErr ;
0ac091ae
RD
603 }
604 break;
cdb11cb9 605
6d4835dc
SC
606 case kEventHIObjectInitialize:
607 result = CallNextEventHandler( inCallRef, inEvent );
0ac091ae 608 if ( result == noErr )
6d4835dc
SC
609 {
610 CFDataRef data;
611 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
612 sizeof( CFTypeRef ), NULL, &data );
cdb11cb9 613
6d4835dc 614 HIViewRef viewRef ;
cdb11cb9 615
6d4835dc
SC
616 wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ;
617 memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ;
cdb11cb9 618
6d4835dc
SC
619 object->viewRef = (HIViewRef) viewRef ;
620
0ac091ae
RD
621 result = noErr ;
622 }
6d4835dc
SC
623 break;
624
0ac091ae 625 case kEventHIObjectDestruct:
507d5748
SC
626 {
627 // we've increased the ref count when creating this, so we decrease manually again in case
628 // it was never really installed and deinstalled
629 HIViewRef viewRef = object->viewRef ;
83f787ba 630 if( viewRef && IsValidControlHandle( viewRef) )
507d5748 631 {
cdb11cb9 632 CFIndex count = CFGetRetainCount( viewRef ) ;
83f787ba
SC
633 if ( count >= 1 )
634 CFRelease( viewRef ) ;
507d5748
SC
635 }
636 free( object ) ;
637 result = noErr;
638 }
0ac091ae
RD
639 break;
640 }
641 break;
cdb11cb9 642
0ac091ae
RD
643 case kEventClassToolbarItem:
644 switch ( GetEventKind( inEvent ) )
cdb11cb9 645 {
0ac091ae
RD
646 case kEventToolbarItemCreateCustomView:
647 {
6d4835dc
SC
648 HIViewRef viewRef = object->viewRef ;
649
650 HIViewRemoveFromSuperview( viewRef ) ;
651 HIViewSetVisible(viewRef, true) ;
652 InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler,
653 GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL );
cdb11cb9 654
6d4835dc 655 result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef );
0ac091ae
RD
656 }
657 break;
658 }
659 break;
cdb11cb9 660
0ac091ae
RD
661 case kEventClassControl:
662 switch ( GetEventKind( inEvent ) )
663 {
664 case kEventControlGetSizeConstraints:
665 {
6d4835dc
SC
666 wxWindow* wxwindow = wxFindControlFromMacControl(object->viewRef ) ;
667 if ( wxwindow )
668 {
669 wxSize sz = wxwindow->GetSize() ;
da14a87d
SC
670 sz.x -= wxwindow->MacGetLeftBorderSize() + wxwindow->MacGetRightBorderSize();
671 sz.y -= wxwindow->MacGetTopBorderSize() + wxwindow->MacGetBottomBorderSize();
6d4835dc
SC
672 // during toolbar layout the native window sometimes gets negative sizes
673 // so we always keep the last valid size here, to make sure we survive the
674 // shuffle ...
675 if ( sz.x > 0 && sz.y > 0 )
676 object->lastValidSize = sz ;
677 else
678 sz = object->lastValidSize ;
cdb11cb9 679
6d4835dc
SC
680 HISize min, max;
681 min.width = max.width = sz.x ;
682 min.height = max.height = sz.y ;
cdb11cb9 683
6d4835dc
SC
684 result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize,
685 sizeof( HISize ), &min );
cdb11cb9 686
6d4835dc
SC
687 result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize,
688 sizeof( HISize ), &max );
689 result = noErr ;
690 }
0ac091ae
RD
691 }
692 break;
693 }
694 break;
695 }
cdb11cb9 696
0ac091ae 697 return result;
6d4835dc
SC
698}
699
700void RegisterControlToolbarItemClass()
701{
0ac091ae 702 static bool sRegistered;
cdb11cb9 703
0ac091ae
RD
704 if ( !sRegistered )
705 {
706 HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0,
707 ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL );
cdb11cb9 708
0ac091ae
RD
709 sRegistered = true;
710 }
6d4835dc
SC
711}
712
713HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData)
714{
0ac091ae 715 RegisterControlToolbarItemClass();
cdb11cb9 716
0ac091ae
RD
717 OSStatus err;
718 EventRef event;
719 UInt32 options = kHIToolbarItemAllowDuplicates;
720 HIToolbarItemRef result = NULL;
cdb11cb9 721
0ac091ae
RD
722 err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
723 require_noerr( err, CantCreateEvent );
cdb11cb9 724
0ac091ae
RD
725 SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options );
726 SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier );
cdb11cb9 727
0ac091ae
RD
728 if ( inConfigData )
729 SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData );
cdb11cb9 730
0ac091ae
RD
731 err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result );
732 check_noerr( err );
cdb11cb9 733
0ac091ae 734 ReleaseEvent( event );
cdb11cb9 735CantCreateEvent :
0ac091ae 736 return result ;
6d4835dc
SC
737}
738
716d0327 739#if wxMAC_USE_NATIVE_TOOLBAR
6d4835dc
SC
740static const EventTypeSpec kToolbarEvents[] =
741{
0ac091ae
RD
742 { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
743 { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
744 { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
6d4835dc
SC
745};
746
747static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
748{
0ac091ae 749 OSStatus result = eventNotHandledErr;
9d5ccdd3
SC
750 // Not yet needed
751 // wxToolBar* toolbar = (wxToolBar*) inUserData ;
0ac091ae 752 CFMutableArrayRef array;
6d4835dc 753
0ac091ae
RD
754 switch ( GetEventKind( inEvent ) )
755 {
756 case kEventToolbarGetDefaultIdentifiers:
6d4835dc
SC
757 {
758 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
0ac091ae 759 sizeof( CFMutableArrayRef ), NULL, &array );
6d4835dc
SC
760 // not implemented yet
761 // GetToolbarDefaultItems( array );
762 result = noErr;
763 }
0ac091ae 764 break;
cdb11cb9 765
0ac091ae 766 case kEventToolbarGetAllowedIdentifiers:
6d4835dc
SC
767 {
768 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
0ac091ae 769 sizeof( CFMutableArrayRef ), NULL, &array );
6d4835dc
SC
770 // not implemented yet
771 // GetToolbarAllowedItems( array );
772 result = noErr;
773 }
0ac091ae
RD
774 break;
775 case kEventToolbarCreateItemWithIdentifier:
776 {
777 HIToolbarItemRef item = NULL;
778 CFTypeRef data = NULL;
6d4835dc 779 CFStringRef identifier = NULL ;
cdb11cb9 780
0ac091ae
RD
781 GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL,
782 sizeof( CFStringRef ), NULL, &identifier );
cdb11cb9 783
0ac091ae
RD
784 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
785 sizeof( CFTypeRef ), NULL, &data );
cdb11cb9 786
6d4835dc
SC
787 if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo )
788 {
789 item = CreateControlToolbarItem( kControlToolbarItemClassID, data );
790 if ( item )
791 {
792 SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef,
793 sizeof( HIToolbarItemRef ), &item );
794 result = noErr;
795 }
0ac091ae 796 }
cdb11cb9 797
0ac091ae
RD
798 }
799 break;
6d4835dc
SC
800 }
801 return result ;
802}
716d0327 803#endif // wxMAC_USE_NATIVE_TOOLBAR
6d4835dc 804
e56d2520
SC
805// also for the toolbar we have the dual implementation:
806// only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
6d4835dc 807
3b6a1179
DS
808bool wxToolBar::Create(
809 wxWindow *parent,
991f71dc
DS
810 wxWindowID id,
811 const wxPoint& pos,
812 const wxSize& size,
813 long style,
3b6a1179 814 const wxString& name )
5d0bf05a 815{
3b6a1179
DS
816 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
817 return false;
e56d2520 818
d408730c
VZ
819 FixupStyle();
820
991f71dc 821 OSStatus err = noErr;
e56d2520
SC
822
823#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
824 wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
825 err = HIToolbarCreate(
826 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
827 (HIToolbarRef*) &m_macHIToolbarRef );
e56d2520
SC
828
829 if (m_macHIToolbarRef != NULL)
f3a65c3e 830 {
0ac091ae
RD
831 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler,
832 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
6d4835dc 833
3b6a1179
DS
834 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
835 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
e56d2520
SC
836
837 if ( style & wxTB_NOICONS )
3b6a1179 838 mode = kHIToolbarDisplayModeLabelOnly;
e56d2520 839 else if ( style & wxTB_TEXT )
3b6a1179 840 mode = kHIToolbarDisplayModeIconAndLabel;
e56d2520 841 else
3b6a1179 842 mode = kHIToolbarDisplayModeIconOnly;
e56d2520 843
3b6a1179
DS
844 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
845 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
e56d2520 846 }
cdb11cb9 847#endif // wxMAC_USE_NATIVE_TOOLBAR
e56d2520 848
991f71dc 849 return (err == noErr);
e9576ca5
SC
850}
851
852wxToolBar::~wxToolBar()
f3a65c3e 853{
e56d2520 854#if wxMAC_USE_NATIVE_TOOLBAR
3025abca 855 if (m_macHIToolbarRef != NULL)
e56d2520
SC
856 {
857 // if this is the installed toolbar, then deinstall it
858 if (m_macUsesNativeToolbar)
859 MacInstallNativeToolbar( false );
860
507d5748
SC
861 CFIndex count = CFGetRetainCount( m_macHIToolbarRef ) ;
862 wxASSERT_MSG( count == 1 , wxT("Reference Count of native control was not 1 in wxToolBar destructor") );
863
3025abca 864 CFRelease( (HIToolbarRef)m_macHIToolbarRef );
e56d2520
SC
865 m_macHIToolbarRef = NULL;
866 }
867#endif
868}
869
e56d2520
SC
870bool wxToolBar::Show( bool show )
871{
e56d2520 872 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
3025abca 873 bool bResult = (tlw != NULL);
f3a65c3e 874
e56d2520
SC
875 if (bResult)
876 {
877#if wxMAC_USE_NATIVE_TOOLBAR
f3a65c3e 878 bool ownToolbarInstalled = false;
e56d2520
SC
879 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
880 if (ownToolbarInstalled)
881 {
3025abca 882 bResult = (IsWindowToolbarVisible( tlw ) != show);
a749a99f
SC
883 if ( bResult )
884 ShowHideWindowToolbar( tlw, show, false );
e56d2520
SC
885 }
886 else
e56d2520 887 bResult = wxToolBarBase::Show( show );
3025abca
DS
888#else
889
890 bResult = wxToolBarBase::Show( show );
891#endif
e56d2520 892 }
f3a65c3e 893
e56d2520
SC
894 return bResult;
895}
896
897bool wxToolBar::IsShown() const
898{
899 bool bResult;
900
901#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 902 bool ownToolbarInstalled;
3025abca 903
e56d2520
SC
904 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
905 if (ownToolbarInstalled)
a749a99f
SC
906 {
907 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
3b6a1179 908 bResult = IsWindowToolbarVisible( tlw );
a749a99f 909 }
e56d2520 910 else
e56d2520 911 bResult = wxToolBarBase::IsShown();
3025abca
DS
912#else
913
914 bResult = wxToolBarBase::IsShown();
915#endif
f3a65c3e 916
e56d2520
SC
917 return bResult;
918}
919
e56d2520
SC
920void wxToolBar::DoGetSize( int *width, int *height ) const
921{
922#if wxMAC_USE_NATIVE_TOOLBAR
923 Rect boundsR;
924 bool ownToolbarInstalled;
f3a65c3e 925
e56d2520
SC
926 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
927 if ( ownToolbarInstalled )
928 {
991f71dc 929 // TODO: is this really a control ?
e56d2520
SC
930 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
931 if ( width != NULL )
932 *width = boundsR.right - boundsR.left;
933 if ( height != NULL )
934 *height = boundsR.bottom - boundsR.top;
935 }
936 else
e56d2520 937 wxToolBarBase::DoGetSize( width, height );
3025abca
DS
938
939#else
940 wxToolBarBase::DoGetSize( width, height );
941#endif
e9576ca5
SC
942}
943
b13095df
SC
944wxSize wxToolBar::DoGetBestSize() const
945{
3b6a1179 946 int width, height;
991f71dc 947
3b6a1179 948 DoGetSize( &width, &height );
991f71dc 949
3b6a1179 950 return wxSize( width, height );
b13095df
SC
951}
952
f3a65c3e 953void wxToolBar::SetWindowStyleFlag( long style )
e56d2520
SC
954{
955 wxToolBarBase::SetWindowStyleFlag( style );
991f71dc 956
e56d2520
SC
957#if wxMAC_USE_NATIVE_TOOLBAR
958 if (m_macHIToolbarRef != NULL)
959 {
960 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
961
962 if ( style & wxTB_NOICONS )
963 mode = kHIToolbarDisplayModeLabelOnly;
964 else if ( style & wxTB_TEXT )
965 mode = kHIToolbarDisplayModeIconAndLabel;
966 else
967 mode = kHIToolbarDisplayModeIconOnly;
f3a65c3e 968
e56d2520
SC
969 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
970 }
971#endif
972}
973
974#if wxMAC_USE_NATIVE_TOOLBAR
975bool wxToolBar::MacWantsNativeToolbar()
976{
977 return m_macUsesNativeToolbar;
978}
979
980bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
981{
982 bool bResultV = false;
983
984 if (ownToolbarInstalled != NULL)
985 *ownToolbarInstalled = false;
986
987 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
988 if (tlw != NULL)
989 {
990 HIToolbarRef curToolbarRef = NULL;
991 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
3025abca 992 bResultV = ((err == noErr) && (curToolbarRef != NULL));
e56d2520
SC
993 if (bResultV && (ownToolbarInstalled != NULL))
994 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
995 }
996
997 return bResultV;
998}
999
f3a65c3e 1000bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
e56d2520 1001{
df7998fc 1002 bool bResult = false;
e56d2520 1003
e56d2520
SC
1004 if (usesNative && (m_macHIToolbarRef == NULL))
1005 return bResult;
f3a65c3e 1006
e56d2520
SC
1007 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
1008 return bResult;
f3a65c3e 1009
3025abca
DS
1010 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
1011 if (tlw == NULL)
1012 return bResult;
1013
e56d2520
SC
1014 // check the existing toolbar
1015 HIToolbarRef curToolbarRef = NULL;
1016 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
991f71dc 1017 if (err != noErr)
e56d2520
SC
1018 curToolbarRef = NULL;
1019
1020 m_macUsesNativeToolbar = usesNative;
1021
1022 if (m_macUsesNativeToolbar)
1023 {
1024 // only install toolbar if there isn't one installed already
1025 if (curToolbarRef == NULL)
1026 {
1027 bResult = true;
1028
1029 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
1030 ShowHideWindowToolbar( tlw, true, false );
1031 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
1032 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
f3a65c3e 1033
3b6a1179 1034 Rect r = { 0, 0, 0, 0 };
e56d2520 1035 m_peer->SetRect( &r );
e56d2520 1036 SetSize( wxSIZE_AUTO_WIDTH, 0 );
e56d2520
SC
1037 m_peer->SetVisibility( false, true );
1038 wxToolBarBase::Show( false );
1039 }
1040 }
1041 else
1042 {
1043 // only deinstall toolbar if this is the installed one
1044 if (m_macHIToolbarRef == curToolbarRef)
1045 {
1046 bResult = true;
1047
1048 ShowHideWindowToolbar( tlw, false, false );
3b6a1179 1049 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
e56d2520 1050 SetWindowToolbar( tlw, NULL );
f3a65c3e 1051
e56d2520 1052 m_peer->SetVisibility( true, true );
e56d2520
SC
1053 }
1054 }
1055
1056 if (bResult)
1057 InvalidateBestSize();
1058
1059// wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1060 return bResult;
1061}
1062#endif
1063
37e2cb08 1064bool wxToolBar::Realize()
e9576ca5 1065{
eb22f2a6 1066 if (m_tools.GetCount() == 0)
3803c372 1067 return false;
0b7a8cd3 1068
e56d2520
SC
1069 int maxWidth = 0;
1070 int maxHeight = 0;
f3a65c3e 1071
bfe9ffbc
SC
1072 int maxToolWidth = 0;
1073 int maxToolHeight = 0;
f3a65c3e 1074
991f71dc
DS
1075 int x = m_xMargin + kwxMacToolBarLeftMargin;
1076 int y = m_yMargin + kwxMacToolBarTopMargin;
1077
1078 int tw, th;
1079 GetSize( &tw, &th );
1080
e56d2520 1081 // find the maximum tool width and height
3025abca
DS
1082 wxToolBarTool *tool;
1083 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
e56d2520 1084 while ( node != NULL )
bfe9ffbc 1085 {
3025abca 1086 tool = (wxToolBarTool *) node->GetData();
e56d2520
SC
1087 if ( tool != NULL )
1088 {
1089 wxSize sz = tool->GetSize();
f3a65c3e 1090
e56d2520
SC
1091 if ( sz.x > maxToolWidth )
1092 maxToolWidth = sz.x;
1093 if ( sz.y > maxToolHeight )
1094 maxToolHeight = sz.y;
1095 }
f3a65c3e 1096
bfe9ffbc
SC
1097 node = node->GetNext();
1098 }
f3a65c3e 1099
991f71dc
DS
1100 bool lastIsRadio = false;
1101 bool curIsRadio = false;
df7998fc
VZ
1102
1103#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
1104 CFIndex currentPosition = 0;
1105 bool insertAll = false;
991f71dc 1106#endif
df7998fc 1107
991f71dc 1108 node = m_tools.GetFirst();
e56d2520 1109 while ( node != NULL )
7810c95b 1110 {
3025abca 1111 tool = (wxToolBarTool*) node->GetData();
e56d2520 1112 if ( tool == NULL )
214b9484 1113 {
e56d2520
SC
1114 node = node->GetNext();
1115 continue;
214b9484 1116 }
f3a65c3e 1117
991f71dc 1118 // set tool position:
e56d2520
SC
1119 // for the moment just perform a single row/column alignment
1120 wxSize cursize = tool->GetSize();
bfe9ffbc 1121 if ( x + cursize.x > maxWidth )
e56d2520 1122 maxWidth = x + cursize.x;
bfe9ffbc 1123 if ( y + cursize.y > maxHeight )
e56d2520 1124 maxHeight = y + cursize.y;
f3a65c3e 1125
73fd9428
SC
1126 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1127 {
e56d2520
SC
1128 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1129 tool->SetPosition( wxPoint(x1, y) );
73fd9428
SC
1130 }
1131 else
1132 {
e56d2520
SC
1133 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1134 tool->SetPosition( wxPoint(x, y1) );
1135 }
f3a65c3e 1136
e56d2520 1137 // update the item positioning state
bfe9ffbc 1138 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
e56d2520
SC
1139 y += cursize.y + kwxMacToolSpacing;
1140 else
1141 x += cursize.x + kwxMacToolSpacing;
f3a65c3e 1142
e56d2520
SC
1143#if wxMAC_USE_NATIVE_TOOLBAR
1144 // install in native HIToolbar
1145 if ( m_macHIToolbarRef != NULL )
bfe9ffbc 1146 {
e56d2520
SC
1147 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
1148 if ( hiItemRef != NULL )
1149 {
991f71dc 1150 if ( insertAll || (tool->GetIndex() != currentPosition) )
e56d2520 1151 {
dcae64c2 1152 OSStatus err = noErr;
8138cfec
SC
1153 if ( !insertAll )
1154 {
dcae64c2
DS
1155 insertAll = true;
1156
8138cfec
SC
1157 // if this is the first tool that gets newly inserted or repositioned
1158 // first remove all 'old' tools from here to the right, because of this
1159 // all following tools will have to be reinserted (insertAll). i = 100 because there's
1160 // no way to determine how many there are in a toolbar, so just a high number :-(
3b6a1179 1161 for ( CFIndex i = 100; i >= currentPosition; --i )
8138cfec 1162 {
dcae64c2 1163 err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i );
8138cfec 1164 }
991f71dc 1165
dcae64c2
DS
1166 if (err != noErr)
1167 {
1168 wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err );
4362c705 1169 wxFAIL_MSG( errMsg.c_str() );
dcae64c2
DS
1170 }
1171 }
991f71dc 1172
dcae64c2
DS
1173 err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition );
1174 if (err != noErr)
1175 {
1176 wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
4362c705 1177 wxFAIL_MSG( errMsg.c_str() );
dcae64c2 1178 }
3b6a1179 1179
dcae64c2 1180 tool->SetIndex( currentPosition );
e56d2520 1181 }
991f71dc 1182
dcae64c2 1183 currentPosition++;
e56d2520
SC
1184 }
1185 }
dcae64c2 1186#endif
f3a65c3e 1187
e56d2520
SC
1188 // update radio button (and group) state
1189 lastIsRadio = curIsRadio;
1190 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
f3a65c3e 1191
e56d2520
SC
1192 if ( !curIsRadio )
1193 {
1194 if ( tool->IsToggled() )
1195 DoToggleTool( tool, true );
0b7a8cd3
GD
1196 }
1197 else
1198 {
e56d2520
SC
1199 if ( !lastIsRadio )
1200 {
dcae64c2 1201 if ( tool->Toggle( true ) )
e56d2520
SC
1202 {
1203 DoToggleTool( tool, true );
e56d2520
SC
1204 }
1205 }
1206 else if ( tool->IsToggled() )
1207 {
1208 if ( tool->IsToggled() )
1209 DoToggleTool( tool, true );
f3a65c3e 1210
e56d2520
SC
1211 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1212 while ( nodePrev != NULL )
1213 {
1214 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1215 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1216 break;
f3a65c3e 1217
dcae64c2 1218 if ( toggleTool->Toggle( false ) )
e56d2520 1219 DoToggleTool( toggleTool, false );
f3a65c3e 1220
e56d2520
SC
1221 nodePrev = nodePrev->GetPrevious();
1222 }
1223 }
0b7a8cd3 1224 }
f3a65c3e 1225
eb22f2a6 1226 node = node->GetNext();
7810c95b 1227 }
f3a65c3e 1228
0b7a8cd3 1229 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
7810c95b 1230 {
e56d2520
SC
1231 // if not set yet, only one row
1232 if ( m_maxRows <= 0 )
1233 SetRows( 1 );
f3a65c3e 1234
90d3f91a 1235 m_minWidth = maxWidth;
e56d2520 1236 maxWidth = tw;
0b7a8cd3 1237 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
e56d2520 1238 m_minHeight = m_maxHeight = maxHeight;
7810c95b 1239 }
0b7a8cd3
GD
1240 else
1241 {
e56d2520
SC
1242 // if not set yet, have one column
1243 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1244 SetRows( GetToolsCount() );
f3a65c3e 1245
90d3f91a 1246 m_minHeight = maxHeight;
e56d2520 1247 maxHeight = th;
0b7a8cd3 1248 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
e56d2520 1249 m_minWidth = m_maxWidth = maxWidth;
0b7a8cd3 1250 }
e56d2520 1251
f3a65c3e 1252#if 0
e56d2520
SC
1253 // FIXME: should this be OSX-only?
1254 {
1255 bool wantNativeToolbar, ownToolbarInstalled;
1256
1257 // attempt to install the native toolbar
1258 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1259 MacInstallNativeToolbar( wantNativeToolbar );
1260 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1261 if (!ownToolbarInstalled)
1262 {
1263 SetSize( maxWidth, maxHeight );
1264 InvalidateBestSize();
1265 }
1266 }
f3a65c3e 1267#else
facd6764 1268 SetSize( maxWidth, maxHeight );
9f884528 1269 InvalidateBestSize();
e56d2520 1270#endif
2c1dbc95 1271
170acdc9 1272 SetInitialSize();
2c1dbc95 1273
3803c372 1274 return true;
e9576ca5
SC
1275}
1276
1277void wxToolBar::SetToolBitmapSize(const wxSize& size)
1278{
e56d2520
SC
1279 m_defaultWidth = size.x + kwxMacToolBorder;
1280 m_defaultHeight = size.y + kwxMacToolBorder;
f3a65c3e 1281
e56d2520
SC
1282#if wxMAC_USE_NATIVE_TOOLBAR
1283 if (m_macHIToolbarRef != NULL)
1284 {
1285 int maxs = wxMax( size.x, size.y );
3b6a1179 1286 HIToolbarDisplaySize sizeSpec;
328f4fee 1287 if ( maxs > 32 )
3b6a1179 1288 sizeSpec = kHIToolbarDisplaySizeNormal;
00a7bd85 1289 else if ( maxs > 24 )
3b6a1179 1290 sizeSpec = kHIToolbarDisplaySizeDefault;
328f4fee 1291 else
3b6a1179 1292 sizeSpec = kHIToolbarDisplaySizeSmall;
f3a65c3e 1293
e56d2520
SC
1294 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
1295 }
1296#endif
e9576ca5
SC
1297}
1298
e9576ca5
SC
1299// The button size is bigger than the bitmap size
1300wxSize wxToolBar::GetToolSize() const
1301{
ee799df7 1302 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
e9576ca5
SC
1303}
1304
37e2cb08 1305void wxToolBar::SetRows(int nRows)
e9576ca5 1306{
e56d2520
SC
1307 // avoid resizing the frame uselessly
1308 if ( nRows != m_maxRows )
e56d2520 1309 m_maxRows = nRows;
e9576ca5
SC
1310}
1311
f3a65c3e 1312void wxToolBar::MacSuperChangedPosition()
c257d44d 1313{
e56d2520 1314 wxWindow::MacSuperChangedPosition();
991f71dc 1315
e56d2520
SC
1316#if wxMAC_USE_NATIVE_TOOLBAR
1317 if (! m_macUsesNativeToolbar )
e56d2520 1318 Realize();
991f71dc 1319#else
3025abca 1320
991f71dc
DS
1321 Realize();
1322#endif
c257d44d
SC
1323}
1324
bbd321ff
RD
1325void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1326{
1327 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1328 if ( tool )
1329 {
1330 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1331
1332 tool->SetNormalBitmap(bitmap);
1333
1334 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1335 tool->UpdateToggleImage( tool->CanBeToggled() && tool->IsToggled() );
cdb11cb9 1336 }
bbd321ff
RD
1337}
1338
1339void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1340{
1341 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1342 if ( tool )
1343 {
1344 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1345
1346 tool->SetDisabledBitmap(bitmap);
cdb11cb9 1347
bbd321ff 1348 // TODO: what to do for this one?
cdb11cb9 1349 }
bbd321ff
RD
1350}
1351
37e2cb08 1352wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
e9576ca5 1353{
3025abca 1354 wxToolBarTool *tool;
affd2611 1355 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
e56d2520 1356 while ( node != NULL )
e044f600 1357 {
3025abca 1358 tool = (wxToolBarTool *)node->GetData();
e56d2520 1359 if (tool != NULL)
e044f600 1360 {
e56d2520
SC
1361 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1362 if ( r.Contains( wxPoint( x, y ) ) )
1363 return tool;
e044f600 1364 }
bfe9ffbc
SC
1365
1366 node = node->GetNext();
e044f600 1367 }
37e2cb08 1368
3025abca 1369 return (wxToolBarToolBase*)NULL;
e9576ca5
SC
1370}
1371
2f1ae414
SC
1372wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1373{
3b6a1179 1374 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
e56d2520 1375 if ( tool != NULL )
3b6a1179 1376 return tool->GetShortHelp();
e56d2520 1377
3b6a1179 1378 return wxEmptyString;
2f1ae414
SC
1379}
1380
37e2cb08 1381void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
e9576ca5 1382{
e56d2520 1383 if ( t != NULL )
3b6a1179 1384 ((wxToolBarTool*)t)->DoEnable( enable );
e9576ca5
SC
1385}
1386
37e2cb08 1387void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
e9576ca5 1388{
e044f600 1389 wxToolBarTool *tool = (wxToolBarTool *)t;
e56d2520
SC
1390 if ( ( tool != NULL ) && tool->IsButton() )
1391 tool->UpdateToggleImage( toggle );
37e2cb08 1392}
7c551d95 1393
3b6a1179 1394bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
37e2cb08 1395{
3b6a1179 1396 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
e56d2520
SC
1397 if (tool == NULL)
1398 return false;
1399
1400 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1401 wxSize toolSize = GetToolSize();
3b6a1179 1402 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
e56d2520
SC
1403 ControlRef controlHandle = NULL;
1404 OSStatus err = 0;
bbd321ff 1405 tool->Attach( this );
be5fe3aa 1406
cdb11cb9
VZ
1407#if wxMAC_USE_NATIVE_TOOLBAR
1408 HIToolbarItemRef item;
1409#endif
1410
e56d2520 1411 switch (tool->GetStyle())
be5fe3aa 1412 {
3b6a1179 1413 case wxTOOL_STYLE_SEPARATOR:
be5fe3aa 1414 {
e56d2520
SC
1415 wxASSERT( tool->GetControlHandle() == NULL );
1416 toolSize.x /= 4;
1417 toolSize.y /= 4;
be5fe3aa 1418 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
e56d2520 1419 toolrect.bottom = toolSize.y;
be5fe3aa 1420 else
e56d2520
SC
1421 toolrect.right = toolSize.x;
1422
be5fe3aa 1423 // in flat style we need a visual separator
991f71dc 1424#if wxMAC_USE_NATIVE_TOOLBAR
991f71dc
DS
1425 err = HIToolbarItemCreate(
1426 kHIToolbarSeparatorIdentifier,
1427 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1428 &item );
e56d2520
SC
1429 if (err == noErr)
1430 tool->SetToolbarItemRef( item );
cdb11cb9 1431#endif // wxMAC_USE_NATIVE_TOOLBAR
991f71dc 1432
e56d2520
SC
1433 CreateSeparatorControl( window, &toolrect, &controlHandle );
1434 tool->SetControlHandle( controlHandle );
be5fe3aa 1435 }
e56d2520
SC
1436 break;
1437
3b6a1179 1438 case wxTOOL_STYLE_BUTTON:
be5fe3aa 1439 {
3b6a1179
DS
1440 wxASSERT( tool->GetControlHandle() == NULL );
1441 ControlButtonContentInfo info;
1442 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
f3a65c3e 1443
df7998fc 1444 if ( UMAGetSystemVersion() >= 0x1000)
3b6a1179
DS
1445 {
1446 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1447 }
df7998fc
VZ
1448 else
1449 {
3b6a1179 1450 SInt16 behaviour = kControlBehaviorOffsetContents;
df7998fc 1451 if ( tool->CanBeToggled() )
3b6a1179
DS
1452 behaviour |= kControlBehaviorToggles;
1453 err = CreateBevelButtonControl( window,
1454 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1455 behaviour, &info, 0, 0, 0, &controlHandle );
df7998fc 1456 }
e56d2520
SC
1457
1458#if wxMAC_USE_NATIVE_TOOLBAR
30962327 1459 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
e56d2520
SC
1460 err = HIToolbarItemCreate(
1461 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1462 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1463 if (err == noErr)
1464 {
3025abca
DS
1465 InstallEventHandler(
1466 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1467 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
cdb11cb9 1468
e56d2520 1469 HIToolbarItemSetIconRef( item, info.u.iconRef );
2c1dbc95 1470 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
e56d2520
SC
1471 tool->SetToolbarItemRef( item );
1472 }
cdb11cb9 1473#endif // wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1474
3b6a1179 1475 wxMacReleaseBitmapButton( &info );
3025abca 1476
991f71dc 1477#if 0
3b6a1179
DS
1478 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1479 UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );
991f71dc 1480#endif
f3a65c3e 1481
3b6a1179
DS
1482 InstallControlEventHandler(
1483 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
e56d2520 1484 GetEventTypeCount(eventList), eventList, tool, NULL );
be5fe3aa 1485
e56d2520 1486 tool->SetControlHandle( controlHandle );
be5fe3aa 1487 }
e56d2520
SC
1488 break;
1489
3b6a1179 1490 case wxTOOL_STYLE_CONTROL:
3b6a1179 1491
6d4835dc 1492#if wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1493 {
0ac091ae 1494 wxASSERT( tool->GetControl() != NULL );
e56d2520 1495 HIToolbarItemRef item;
6d4835dc 1496 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
9d5ccdd3
SC
1497 // as this control now is part of both the wxToolBar children and the native toolbar, we have to increase the
1498 // reference count to make sure we are not dealing with zombie controls after the native toolbar has released its views
1499 CFRetain( viewRef ) ;
6d4835dc 1500 CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ;
cdb11cb9
VZ
1501 err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID,
1502 data , &item ) ;
6d4835dc
SC
1503
1504 if (err == noErr)
e56d2520 1505 {
e56d2520 1506 tool->SetToolbarItemRef( item );
e56d2520 1507 }
6d4835dc
SC
1508 CFRelease( data ) ;
1509 }
f3a65c3e 1510
e56d2520 1511#else
6d4835dc 1512 // right now there's nothing to do here
e56d2520
SC
1513#endif
1514 break;
1515
3b6a1179 1516 default:
e56d2520 1517 break;
be5fe3aa 1518 }
f3a65c3e 1519
cdb11cb9
VZ
1520#if wxMAC_USE_NATIVE_TOOLBAR
1521 wxString label = tool->GetLabel();
1522 if ( !label.empty() )
1523 {
1524 // strip mnemonics from the label for compatibility
1525 // with the usual labels in wxStaticText sense
6b4f099d 1526 label = wxStripMenuCodes(label);
cdb11cb9
VZ
1527
1528 HIToolbarItemSetLabel(item,
1529 wxMacCFStringHolder(label, m_font.GetEncoding()));
1530 }
1531#endif // wxMAC_USE_NATIVE_TOOLBAR
1532
991f71dc 1533 if ( err == noErr )
be5fe3aa 1534 {
e56d2520
SC
1535 if ( controlHandle )
1536 {
1537 ControlRef container = (ControlRef) GetHandle();
3b6a1179 1538 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
be5fe3aa 1539
e56d2520
SC
1540 UMAShowControl( controlHandle );
1541 ::EmbedControl( controlHandle, container );
1542 }
1543
1544 if ( tool->CanBeToggled() && tool->IsToggled() )
1545 tool->UpdateToggleImage( true );
be5fe3aa 1546
e56d2520 1547 // nothing special to do here - we relayout in Realize() later
e56d2520
SC
1548 InvalidateBestSize();
1549 }
1550 else
be5fe3aa 1551 {
3b6a1179 1552 wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err );
4362c705 1553 wxFAIL_MSG( errMsg.c_str() );
be5fe3aa 1554 }
f3a65c3e 1555
991f71dc 1556 return (err == noErr);
37e2cb08 1557}
e9576ca5 1558
5115c51a 1559void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
37e2cb08 1560{
3b6a1179 1561 wxFAIL_MSG( wxT("not implemented") );
e9576ca5
SC
1562}
1563
be5fe3aa 1564bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
37e2cb08 1565{
3b6a1179 1566 wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase );
affd2611 1567 wxToolBarToolsList::compatibility_iterator node;
bfe9ffbc
SC
1568 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1569 {
1570 wxToolBarToolBase *tool2 = node->GetData();
1571 if ( tool2 == tool )
1572 {
1573 // let node point to the next node in the list
1574 node = node->GetNext();
1575
1576 break;
1577 }
1578 }
1579
3b6a1179 1580 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
bfe9ffbc
SC
1581
1582 tool->Detach();
df7998fc
VZ
1583
1584#if wxMAC_USE_NATIVE_TOOLBAR
1585 CFIndex removeIndex = tool->GetIndex();
dcae64c2 1586#endif
bfe9ffbc 1587
507d5748
SC
1588#if wxMAC_USE_NATIVE_TOOLBAR
1589 if ( removeIndex != -1 && m_macHIToolbarRef )
1590 {
1591 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
1592 tool->SetIndex( -1 );
1593 }
1594#endif
488b2c29
SC
1595 switch ( tool->GetStyle() )
1596 {
1597 case wxTOOL_STYLE_CONTROL:
507d5748 1598 if ( tool->GetControl() )
be5fe3aa 1599 tool->GetControl()->Destroy();
488b2c29
SC
1600 break;
1601
1602 case wxTOOL_STYLE_BUTTON:
1603 case wxTOOL_STYLE_SEPARATOR:
507d5748 1604 // nothing special
488b2c29 1605 break;
e56d2520
SC
1606
1607 default:
1608 break;
488b2c29 1609 }
507d5748 1610 tool->ClearControl();
488b2c29 1611
bfe9ffbc 1612 // and finally reposition all the controls after this one
f3a65c3e 1613
3b6a1179 1614 for ( /* node -> first after deleted */; node; node = node->GetNext() )
bfe9ffbc
SC
1615 {
1616 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
3b6a1179 1617 wxPoint pt = tool2->GetPosition();
bfe9ffbc
SC
1618
1619 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
3b6a1179 1620 pt.y -= sz.y;
bfe9ffbc 1621 else
3b6a1179 1622 pt.x -= sz.x;
e56d2520 1623
3b6a1179 1624 tool2->SetPosition( pt );
df7998fc 1625
2c1dbc95
SC
1626#if wxMAC_USE_NATIVE_TOOLBAR
1627 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
3b6a1179 1628 tool2->SetIndex( tool2->GetIndex() - 1 );
2c1dbc95 1629#endif
bfe9ffbc 1630 }
f3a65c3e 1631
9f884528 1632 InvalidateBestSize();
991f71dc 1633
3b6a1179 1634 return true;
37e2cb08 1635}
2f1ae414
SC
1636
1637void wxToolBar::OnPaint(wxPaintEvent& event)
1638{
e56d2520
SC
1639#if wxMAC_USE_NATIVE_TOOLBAR
1640 if ( m_macUsesNativeToolbar )
1641 {
dcae64c2
DS
1642 event.Skip(true);
1643 return;
e56d2520
SC
1644 }
1645#endif
f3a65c3e 1646
3b6a1179 1647 wxPaintDC dc(this);
ddc548ec 1648
3b6a1179
DS
1649 int w, h;
1650 GetSize( &w, &h );
991f71dc 1651
dcae64c2
DS
1652 bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance();
1653 bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);
1654
ddc548ec 1655#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
dcae64c2 1656 if ( !drawMetalTheme && minimumUmaAvailable )
ddc548ec 1657 {
dcae64c2
DS
1658 HIThemePlacardDrawInfo info;
1659 memset( &info, 0, sizeof(info) );
1660 info.version = 0;
1661 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1662
1663 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1664 HIRect rect = CGRectMake( 0, 0, w, h );
1665 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
ddc548ec
SC
1666 }
1667 else
1668 {
1669 // leave the background as it is (striped or metal)
1670 }
991f71dc 1671
ddc548ec 1672#else
dcae64c2
DS
1673
1674 const bool drawBorder = true;
1675
1676 if (drawBorder)
01ffa8f7 1677 {
3b6a1179 1678 wxMacPortSetter helper( &dc );
dcae64c2
DS
1679
1680 if ( !drawMetalTheme || !minimumUmaAvailable )
20b69855 1681 {
3b6a1179
DS
1682 Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1683 dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };
dcae64c2
DS
1684
1685#if 0
1686 if ( toolbarrect.left < 0 )
3b6a1179 1687 toolbarrect.left = 0;
dcae64c2 1688 if ( toolbarrect.top < 0 )
3b6a1179 1689 toolbarrect.top = 0;
dcae64c2
DS
1690#endif
1691
1692 UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive );
1693 }
1694 else
1695 {
1696#if TARGET_API_MAC_OSX
991f71dc 1697 HIRect hiToolbarrect = CGRectMake(
3b6a1179
DS
1698 dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1699 dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) );
1700 CGContextRef cgContext;
1701 Rect bounds;
dcae64c2 1702
3b6a1179
DS
1703 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
1704 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
dcae64c2 1705
3b6a1179
DS
1706 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
1707 CGContextScaleCTM( cgContext, 1, -1 );
785f5eaa 1708
3b6a1179
DS
1709 HIThemeBackgroundDrawInfo drawInfo;
1710 drawInfo.version = 0;
1711 drawInfo.state = kThemeStateActive;
1712 drawInfo.kind = kThemeBackgroundMetal;
dcae64c2 1713 HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );
e56d2520 1714
f387b80e 1715#ifndef __LP64__
3b6a1179 1716 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
f387b80e 1717#endif
20b69855 1718#endif
01ffa8f7 1719 }
01ffa8f7
SC
1720 }
1721#endif
20b69855 1722
dcae64c2 1723 event.Skip();
2f1ae414 1724}
895f5af7 1725
519cb848 1726#endif // wxUSE_TOOLBAR