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