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