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