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