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