]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toolbar.cpp
Document wxWindow::Navigate() instead of generation
[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;
58862dfa
VZ
1106
1107 HIToolbarRef refTB = (HIToolbarRef)m_macHIToolbarRef;
991f71dc 1108#endif
df7998fc 1109
991f71dc 1110 node = m_tools.GetFirst();
e56d2520 1111 while ( node != NULL )
7810c95b 1112 {
3025abca 1113 tool = (wxToolBarTool*) node->GetData();
e56d2520 1114 if ( tool == NULL )
214b9484 1115 {
e56d2520
SC
1116 node = node->GetNext();
1117 continue;
214b9484 1118 }
f3a65c3e 1119
991f71dc 1120 // set tool position:
e56d2520
SC
1121 // for the moment just perform a single row/column alignment
1122 wxSize cursize = tool->GetSize();
bfe9ffbc 1123 if ( x + cursize.x > maxWidth )
e56d2520 1124 maxWidth = x + cursize.x;
bfe9ffbc 1125 if ( y + cursize.y > maxHeight )
e56d2520 1126 maxHeight = y + cursize.y;
f3a65c3e 1127
73fd9428
SC
1128 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1129 {
e56d2520
SC
1130 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1131 tool->SetPosition( wxPoint(x1, y) );
73fd9428
SC
1132 }
1133 else
1134 {
e56d2520
SC
1135 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1136 tool->SetPosition( wxPoint(x, y1) );
1137 }
f3a65c3e 1138
e56d2520 1139 // update the item positioning state
bfe9ffbc 1140 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
e56d2520
SC
1141 y += cursize.y + kwxMacToolSpacing;
1142 else
1143 x += cursize.x + kwxMacToolSpacing;
f3a65c3e 1144
e56d2520
SC
1145#if wxMAC_USE_NATIVE_TOOLBAR
1146 // install in native HIToolbar
58862dfa 1147 if ( refTB )
bfe9ffbc 1148 {
58862dfa 1149 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
e56d2520
SC
1150 if ( hiItemRef != NULL )
1151 {
991f71dc 1152 if ( insertAll || (tool->GetIndex() != currentPosition) )
e56d2520 1153 {
dcae64c2 1154 OSStatus err = noErr;
8138cfec
SC
1155 if ( !insertAll )
1156 {
dcae64c2
DS
1157 insertAll = true;
1158
8138cfec
SC
1159 // if this is the first tool that gets newly inserted or repositioned
1160 // first remove all 'old' tools from here to the right, because of this
58862dfa
VZ
1161 // all following tools will have to be reinserted (insertAll).
1162 for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast();
1163 node2 != node;
1164 node2 = node2->GetPrevious() )
dcae64c2 1165 {
58862dfa
VZ
1166 wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData();
1167
1168 const long idx = tool2->GetIndex();
1169 if ( idx != -1 )
1170 {
1171 err = HIToolbarRemoveItemAtIndex(refTB, idx);
1172 if ( err != noErr )
1173 {
1174 wxLogDebug(wxT("HIToolbarRemoveItemAtIndex(%ld) failed [%ld]"),
1175 idx, (long)err);
1176 }
1177
1178 tool2->SetIndex(-1);
1179 }
dcae64c2
DS
1180 }
1181 }
991f71dc 1182
58862dfa 1183 err = HIToolbarInsertItemAtIndex( refTB, hiItemRef, currentPosition );
dcae64c2
DS
1184 if (err != noErr)
1185 {
58862dfa 1186 wxLogDebug( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
dcae64c2 1187 }
3b6a1179 1188
dcae64c2 1189 tool->SetIndex( currentPosition );
e56d2520 1190 }
991f71dc 1191
dcae64c2 1192 currentPosition++;
e56d2520
SC
1193 }
1194 }
dcae64c2 1195#endif
f3a65c3e 1196
e56d2520
SC
1197 // update radio button (and group) state
1198 lastIsRadio = curIsRadio;
1199 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
f3a65c3e 1200
e56d2520
SC
1201 if ( !curIsRadio )
1202 {
1203 if ( tool->IsToggled() )
1204 DoToggleTool( tool, true );
0b7a8cd3
GD
1205 }
1206 else
1207 {
e56d2520
SC
1208 if ( !lastIsRadio )
1209 {
dcae64c2 1210 if ( tool->Toggle( true ) )
e56d2520
SC
1211 {
1212 DoToggleTool( tool, true );
e56d2520
SC
1213 }
1214 }
1215 else if ( tool->IsToggled() )
1216 {
1217 if ( tool->IsToggled() )
1218 DoToggleTool( tool, true );
f3a65c3e 1219
e56d2520
SC
1220 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1221 while ( nodePrev != NULL )
1222 {
1223 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1224 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1225 break;
f3a65c3e 1226
dcae64c2 1227 if ( toggleTool->Toggle( false ) )
e56d2520 1228 DoToggleTool( toggleTool, false );
f3a65c3e 1229
e56d2520
SC
1230 nodePrev = nodePrev->GetPrevious();
1231 }
1232 }
0b7a8cd3 1233 }
f3a65c3e 1234
eb22f2a6 1235 node = node->GetNext();
7810c95b 1236 }
f3a65c3e 1237
0b7a8cd3 1238 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
7810c95b 1239 {
e56d2520
SC
1240 // if not set yet, only one row
1241 if ( m_maxRows <= 0 )
1242 SetRows( 1 );
f3a65c3e 1243
90d3f91a 1244 m_minWidth = maxWidth;
e56d2520 1245 maxWidth = tw;
0b7a8cd3 1246 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
e56d2520 1247 m_minHeight = m_maxHeight = maxHeight;
7810c95b 1248 }
0b7a8cd3
GD
1249 else
1250 {
e56d2520
SC
1251 // if not set yet, have one column
1252 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1253 SetRows( GetToolsCount() );
f3a65c3e 1254
90d3f91a 1255 m_minHeight = maxHeight;
e56d2520 1256 maxHeight = th;
0b7a8cd3 1257 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
e56d2520 1258 m_minWidth = m_maxWidth = maxWidth;
0b7a8cd3 1259 }
e56d2520 1260
f3a65c3e 1261#if 0
e56d2520
SC
1262 // FIXME: should this be OSX-only?
1263 {
1264 bool wantNativeToolbar, ownToolbarInstalled;
1265
1266 // attempt to install the native toolbar
1267 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1268 MacInstallNativeToolbar( wantNativeToolbar );
1269 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1270 if (!ownToolbarInstalled)
1271 {
1272 SetSize( maxWidth, maxHeight );
1273 InvalidateBestSize();
1274 }
1275 }
f3a65c3e 1276#else
facd6764 1277 SetSize( maxWidth, maxHeight );
9f884528 1278 InvalidateBestSize();
e56d2520 1279#endif
2c1dbc95 1280
170acdc9 1281 SetInitialSize();
2c1dbc95 1282
3803c372 1283 return true;
e9576ca5
SC
1284}
1285
1286void wxToolBar::SetToolBitmapSize(const wxSize& size)
1287{
e56d2520
SC
1288 m_defaultWidth = size.x + kwxMacToolBorder;
1289 m_defaultHeight = size.y + kwxMacToolBorder;
f3a65c3e 1290
e56d2520
SC
1291#if wxMAC_USE_NATIVE_TOOLBAR
1292 if (m_macHIToolbarRef != NULL)
1293 {
1294 int maxs = wxMax( size.x, size.y );
3b6a1179 1295 HIToolbarDisplaySize sizeSpec;
328f4fee 1296 if ( maxs > 32 )
3b6a1179 1297 sizeSpec = kHIToolbarDisplaySizeNormal;
00a7bd85 1298 else if ( maxs > 24 )
3b6a1179 1299 sizeSpec = kHIToolbarDisplaySizeDefault;
328f4fee 1300 else
3b6a1179 1301 sizeSpec = kHIToolbarDisplaySizeSmall;
f3a65c3e 1302
e56d2520
SC
1303 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
1304 }
1305#endif
e9576ca5
SC
1306}
1307
e9576ca5
SC
1308// The button size is bigger than the bitmap size
1309wxSize wxToolBar::GetToolSize() const
1310{
ee799df7 1311 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
e9576ca5
SC
1312}
1313
37e2cb08 1314void wxToolBar::SetRows(int nRows)
e9576ca5 1315{
e56d2520
SC
1316 // avoid resizing the frame uselessly
1317 if ( nRows != m_maxRows )
e56d2520 1318 m_maxRows = nRows;
e9576ca5
SC
1319}
1320
f3a65c3e 1321void wxToolBar::MacSuperChangedPosition()
c257d44d 1322{
e56d2520 1323 wxWindow::MacSuperChangedPosition();
991f71dc 1324
e56d2520
SC
1325#if wxMAC_USE_NATIVE_TOOLBAR
1326 if (! m_macUsesNativeToolbar )
e56d2520 1327 Realize();
991f71dc 1328#else
3025abca 1329
991f71dc
DS
1330 Realize();
1331#endif
c257d44d
SC
1332}
1333
bbd321ff
RD
1334void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
1335{
1336 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1337 if ( tool )
1338 {
1339 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1340
1341 tool->SetNormalBitmap(bitmap);
1342
1343 // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button.
1344 tool->UpdateToggleImage( tool->CanBeToggled() && tool->IsToggled() );
cdb11cb9 1345 }
bbd321ff
RD
1346}
1347
1348void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
1349{
1350 wxToolBarTool* tool = wx_static_cast(wxToolBarTool*, FindById(id));
1351 if ( tool )
1352 {
1353 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
1354
1355 tool->SetDisabledBitmap(bitmap);
cdb11cb9 1356
bbd321ff 1357 // TODO: what to do for this one?
cdb11cb9 1358 }
bbd321ff
RD
1359}
1360
37e2cb08 1361wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
e9576ca5 1362{
3025abca 1363 wxToolBarTool *tool;
affd2611 1364 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
e56d2520 1365 while ( node != NULL )
e044f600 1366 {
3025abca 1367 tool = (wxToolBarTool *)node->GetData();
e56d2520 1368 if (tool != NULL)
e044f600 1369 {
e56d2520
SC
1370 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1371 if ( r.Contains( wxPoint( x, y ) ) )
1372 return tool;
e044f600 1373 }
bfe9ffbc
SC
1374
1375 node = node->GetNext();
e044f600 1376 }
37e2cb08 1377
3025abca 1378 return (wxToolBarToolBase*)NULL;
e9576ca5
SC
1379}
1380
2f1ae414
SC
1381wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1382{
3b6a1179 1383 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
e56d2520 1384 if ( tool != NULL )
3b6a1179 1385 return tool->GetShortHelp();
e56d2520 1386
3b6a1179 1387 return wxEmptyString;
2f1ae414
SC
1388}
1389
37e2cb08 1390void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
e9576ca5 1391{
e56d2520 1392 if ( t != NULL )
3b6a1179 1393 ((wxToolBarTool*)t)->DoEnable( enable );
e9576ca5
SC
1394}
1395
37e2cb08 1396void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
e9576ca5 1397{
e044f600 1398 wxToolBarTool *tool = (wxToolBarTool *)t;
e56d2520
SC
1399 if ( ( tool != NULL ) && tool->IsButton() )
1400 tool->UpdateToggleImage( toggle );
37e2cb08 1401}
7c551d95 1402
3b6a1179 1403bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
37e2cb08 1404{
3b6a1179 1405 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
e56d2520
SC
1406 if (tool == NULL)
1407 return false;
1408
1409 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1410 wxSize toolSize = GetToolSize();
3b6a1179 1411 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
e56d2520
SC
1412 ControlRef controlHandle = NULL;
1413 OSStatus err = 0;
bbd321ff 1414 tool->Attach( this );
be5fe3aa 1415
cdb11cb9
VZ
1416#if wxMAC_USE_NATIVE_TOOLBAR
1417 HIToolbarItemRef item;
1418#endif
1419
e56d2520 1420 switch (tool->GetStyle())
be5fe3aa 1421 {
3b6a1179 1422 case wxTOOL_STYLE_SEPARATOR:
be5fe3aa 1423 {
e56d2520
SC
1424 wxASSERT( tool->GetControlHandle() == NULL );
1425 toolSize.x /= 4;
1426 toolSize.y /= 4;
be5fe3aa 1427 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
e56d2520 1428 toolrect.bottom = toolSize.y;
be5fe3aa 1429 else
e56d2520
SC
1430 toolrect.right = toolSize.x;
1431
be5fe3aa 1432 // in flat style we need a visual separator
991f71dc 1433#if wxMAC_USE_NATIVE_TOOLBAR
991f71dc
DS
1434 err = HIToolbarItemCreate(
1435 kHIToolbarSeparatorIdentifier,
1436 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1437 &item );
e56d2520
SC
1438 if (err == noErr)
1439 tool->SetToolbarItemRef( item );
cdb11cb9 1440#endif // wxMAC_USE_NATIVE_TOOLBAR
991f71dc 1441
e56d2520
SC
1442 CreateSeparatorControl( window, &toolrect, &controlHandle );
1443 tool->SetControlHandle( controlHandle );
be5fe3aa 1444 }
e56d2520
SC
1445 break;
1446
3b6a1179 1447 case wxTOOL_STYLE_BUTTON:
be5fe3aa 1448 {
3b6a1179
DS
1449 wxASSERT( tool->GetControlHandle() == NULL );
1450 ControlButtonContentInfo info;
1451 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
f3a65c3e 1452
df7998fc 1453 if ( UMAGetSystemVersion() >= 0x1000)
3b6a1179
DS
1454 {
1455 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1456 }
df7998fc
VZ
1457 else
1458 {
3b6a1179 1459 SInt16 behaviour = kControlBehaviorOffsetContents;
df7998fc 1460 if ( tool->CanBeToggled() )
3b6a1179
DS
1461 behaviour |= kControlBehaviorToggles;
1462 err = CreateBevelButtonControl( window,
1463 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1464 behaviour, &info, 0, 0, 0, &controlHandle );
df7998fc 1465 }
e56d2520
SC
1466
1467#if wxMAC_USE_NATIVE_TOOLBAR
30962327 1468 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
e56d2520
SC
1469 err = HIToolbarItemCreate(
1470 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1471 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1472 if (err == noErr)
1473 {
3025abca
DS
1474 InstallEventHandler(
1475 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1476 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
cdb11cb9 1477
e56d2520 1478 HIToolbarItemSetIconRef( item, info.u.iconRef );
2c1dbc95 1479 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
e56d2520
SC
1480 tool->SetToolbarItemRef( item );
1481 }
cdb11cb9 1482#endif // wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1483
3b6a1179 1484 wxMacReleaseBitmapButton( &info );
3025abca 1485
991f71dc 1486#if 0
3b6a1179
DS
1487 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1488 UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );
991f71dc 1489#endif
f3a65c3e 1490
3b6a1179
DS
1491 InstallControlEventHandler(
1492 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
e56d2520 1493 GetEventTypeCount(eventList), eventList, tool, NULL );
be5fe3aa 1494
e56d2520 1495 tool->SetControlHandle( controlHandle );
be5fe3aa 1496 }
e56d2520
SC
1497 break;
1498
3b6a1179 1499 case wxTOOL_STYLE_CONTROL:
3b6a1179 1500
6d4835dc 1501#if wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1502 {
d5c87edd
VZ
1503 wxCHECK_MSG( tool->GetControl(), false, _T("control must be non-NULL") );
1504
6d4835dc 1505 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
9d5ccdd3
SC
1506 // as this control now is part of both the wxToolBar children and the native toolbar, we have to increase the
1507 // reference count to make sure we are not dealing with zombie controls after the native toolbar has released its views
1508 CFRetain( viewRef ) ;
6d4835dc 1509 CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ;
cdb11cb9
VZ
1510 err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID,
1511 data , &item ) ;
6d4835dc
SC
1512
1513 if (err == noErr)
e56d2520 1514 {
e56d2520 1515 tool->SetToolbarItemRef( item );
e56d2520 1516 }
6d4835dc
SC
1517 CFRelease( data ) ;
1518 }
f3a65c3e 1519
e56d2520 1520#else
6d4835dc 1521 // right now there's nothing to do here
e56d2520
SC
1522#endif
1523 break;
1524
3b6a1179 1525 default:
e56d2520 1526 break;
be5fe3aa 1527 }
f3a65c3e 1528
cdb11cb9
VZ
1529#if wxMAC_USE_NATIVE_TOOLBAR
1530 wxString label = tool->GetLabel();
1531 if ( !label.empty() )
1532 {
1533 // strip mnemonics from the label for compatibility
1534 // with the usual labels in wxStaticText sense
6b4f099d 1535 label = wxStripMenuCodes(label);
cdb11cb9
VZ
1536
1537 HIToolbarItemSetLabel(item,
1538 wxMacCFStringHolder(label, m_font.GetEncoding()));
1539 }
1540#endif // wxMAC_USE_NATIVE_TOOLBAR
1541
991f71dc 1542 if ( err == noErr )
be5fe3aa 1543 {
e56d2520
SC
1544 if ( controlHandle )
1545 {
1546 ControlRef container = (ControlRef) GetHandle();
3b6a1179 1547 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
be5fe3aa 1548
e56d2520
SC
1549 UMAShowControl( controlHandle );
1550 ::EmbedControl( controlHandle, container );
1551 }
1552
1553 if ( tool->CanBeToggled() && tool->IsToggled() )
1554 tool->UpdateToggleImage( true );
be5fe3aa 1555
e56d2520 1556 // nothing special to do here - we relayout in Realize() later
e56d2520
SC
1557 InvalidateBestSize();
1558 }
1559 else
be5fe3aa 1560 {
3b6a1179 1561 wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err );
4362c705 1562 wxFAIL_MSG( errMsg.c_str() );
be5fe3aa 1563 }
f3a65c3e 1564
991f71dc 1565 return (err == noErr);
37e2cb08 1566}
e9576ca5 1567
5115c51a 1568void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
37e2cb08 1569{
3b6a1179 1570 wxFAIL_MSG( wxT("not implemented") );
e9576ca5
SC
1571}
1572
be5fe3aa 1573bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
37e2cb08 1574{
3b6a1179 1575 wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase );
affd2611 1576 wxToolBarToolsList::compatibility_iterator node;
bfe9ffbc
SC
1577 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1578 {
1579 wxToolBarToolBase *tool2 = node->GetData();
1580 if ( tool2 == tool )
1581 {
1582 // let node point to the next node in the list
1583 node = node->GetNext();
1584
1585 break;
1586 }
1587 }
1588
3b6a1179 1589 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
bfe9ffbc
SC
1590
1591 tool->Detach();
df7998fc
VZ
1592
1593#if wxMAC_USE_NATIVE_TOOLBAR
1594 CFIndex removeIndex = tool->GetIndex();
dcae64c2 1595#endif
bfe9ffbc 1596
507d5748
SC
1597#if wxMAC_USE_NATIVE_TOOLBAR
1598 if ( removeIndex != -1 && m_macHIToolbarRef )
1599 {
1600 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
1601 tool->SetIndex( -1 );
1602 }
1603#endif
488b2c29
SC
1604 switch ( tool->GetStyle() )
1605 {
1606 case wxTOOL_STYLE_CONTROL:
507d5748 1607 if ( tool->GetControl() )
be5fe3aa 1608 tool->GetControl()->Destroy();
488b2c29
SC
1609 break;
1610
1611 case wxTOOL_STYLE_BUTTON:
1612 case wxTOOL_STYLE_SEPARATOR:
507d5748 1613 // nothing special
488b2c29 1614 break;
e56d2520
SC
1615
1616 default:
1617 break;
488b2c29 1618 }
507d5748 1619 tool->ClearControl();
488b2c29 1620
bfe9ffbc 1621 // and finally reposition all the controls after this one
f3a65c3e 1622
3b6a1179 1623 for ( /* node -> first after deleted */; node; node = node->GetNext() )
bfe9ffbc
SC
1624 {
1625 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
3b6a1179 1626 wxPoint pt = tool2->GetPosition();
bfe9ffbc
SC
1627
1628 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
3b6a1179 1629 pt.y -= sz.y;
bfe9ffbc 1630 else
3b6a1179 1631 pt.x -= sz.x;
e56d2520 1632
3b6a1179 1633 tool2->SetPosition( pt );
df7998fc 1634
2c1dbc95
SC
1635#if wxMAC_USE_NATIVE_TOOLBAR
1636 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
3b6a1179 1637 tool2->SetIndex( tool2->GetIndex() - 1 );
2c1dbc95 1638#endif
bfe9ffbc 1639 }
f3a65c3e 1640
9f884528 1641 InvalidateBestSize();
991f71dc 1642
3b6a1179 1643 return true;
37e2cb08 1644}
2f1ae414
SC
1645
1646void wxToolBar::OnPaint(wxPaintEvent& event)
1647{
e56d2520
SC
1648#if wxMAC_USE_NATIVE_TOOLBAR
1649 if ( m_macUsesNativeToolbar )
1650 {
dcae64c2
DS
1651 event.Skip(true);
1652 return;
e56d2520
SC
1653 }
1654#endif
f3a65c3e 1655
3b6a1179 1656 wxPaintDC dc(this);
ddc548ec 1657
3b6a1179
DS
1658 int w, h;
1659 GetSize( &w, &h );
991f71dc 1660
dcae64c2
DS
1661 bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance();
1662 bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);
1663
ddc548ec 1664#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
dcae64c2 1665 if ( !drawMetalTheme && minimumUmaAvailable )
ddc548ec 1666 {
dcae64c2
DS
1667 HIThemePlacardDrawInfo info;
1668 memset( &info, 0, sizeof(info) );
1669 info.version = 0;
1670 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1671
1672 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1673 HIRect rect = CGRectMake( 0, 0, w, h );
1674 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
ddc548ec
SC
1675 }
1676 else
1677 {
1678 // leave the background as it is (striped or metal)
1679 }
991f71dc 1680
ddc548ec 1681#else
dcae64c2
DS
1682
1683 const bool drawBorder = true;
1684
1685 if (drawBorder)
01ffa8f7 1686 {
3b6a1179 1687 wxMacPortSetter helper( &dc );
dcae64c2
DS
1688
1689 if ( !drawMetalTheme || !minimumUmaAvailable )
20b69855 1690 {
3b6a1179
DS
1691 Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1692 dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };
dcae64c2
DS
1693
1694#if 0
1695 if ( toolbarrect.left < 0 )
3b6a1179 1696 toolbarrect.left = 0;
dcae64c2 1697 if ( toolbarrect.top < 0 )
3b6a1179 1698 toolbarrect.top = 0;
dcae64c2
DS
1699#endif
1700
1701 UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive );
1702 }
1703 else
1704 {
1705#if TARGET_API_MAC_OSX
991f71dc 1706 HIRect hiToolbarrect = CGRectMake(
3b6a1179
DS
1707 dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1708 dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) );
1709 CGContextRef cgContext;
1710 Rect bounds;
dcae64c2 1711
3b6a1179
DS
1712 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
1713 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
dcae64c2 1714
3b6a1179
DS
1715 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
1716 CGContextScaleCTM( cgContext, 1, -1 );
785f5eaa 1717
3b6a1179
DS
1718 HIThemeBackgroundDrawInfo drawInfo;
1719 drawInfo.version = 0;
1720 drawInfo.state = kThemeStateActive;
1721 drawInfo.kind = kThemeBackgroundMetal;
dcae64c2 1722 HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );
e56d2520 1723
f387b80e 1724#ifndef __LP64__
3b6a1179 1725 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
f387b80e 1726#endif
20b69855 1727#endif
01ffa8f7 1728 }
01ffa8f7
SC
1729 }
1730#endif
20b69855 1731
dcae64c2 1732 event.Skip();
2f1ae414 1733}
895f5af7 1734
519cb848 1735#endif // wxUSE_TOOLBAR