]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toolbar.cpp
Explicitly forward non-command events along to the native control first.
[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
d408730c
VZ
795 FixupStyle();
796
991f71dc 797 OSStatus err = noErr;
e56d2520
SC
798
799#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
800 wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
801 err = HIToolbarCreate(
802 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
803 (HIToolbarRef*) &m_macHIToolbarRef );
e56d2520
SC
804
805 if (m_macHIToolbarRef != NULL)
f3a65c3e 806 {
0ac091ae
RD
807 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler,
808 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
6d4835dc 809
3b6a1179
DS
810 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
811 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
e56d2520
SC
812
813 if ( style & wxTB_NOICONS )
3b6a1179 814 mode = kHIToolbarDisplayModeLabelOnly;
e56d2520 815 else if ( style & wxTB_TEXT )
3b6a1179 816 mode = kHIToolbarDisplayModeIconAndLabel;
e56d2520 817 else
3b6a1179 818 mode = kHIToolbarDisplayModeIconOnly;
e56d2520 819
3b6a1179
DS
820 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
821 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
e56d2520
SC
822 }
823#endif
824
991f71dc 825 return (err == noErr);
e9576ca5
SC
826}
827
828wxToolBar::~wxToolBar()
f3a65c3e 829{
e56d2520 830#if wxMAC_USE_NATIVE_TOOLBAR
3025abca 831 if (m_macHIToolbarRef != NULL)
e56d2520
SC
832 {
833 // if this is the installed toolbar, then deinstall it
834 if (m_macUsesNativeToolbar)
835 MacInstallNativeToolbar( false );
836
3025abca 837 CFRelease( (HIToolbarRef)m_macHIToolbarRef );
e56d2520
SC
838 m_macHIToolbarRef = NULL;
839 }
840#endif
841}
842
e56d2520
SC
843bool wxToolBar::Show( bool show )
844{
e56d2520 845 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
3025abca 846 bool bResult = (tlw != NULL);
f3a65c3e 847
e56d2520
SC
848 if (bResult)
849 {
850#if wxMAC_USE_NATIVE_TOOLBAR
f3a65c3e 851 bool ownToolbarInstalled = false;
e56d2520
SC
852 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
853 if (ownToolbarInstalled)
854 {
3025abca 855 bResult = (IsWindowToolbarVisible( tlw ) != show);
a749a99f
SC
856 if ( bResult )
857 ShowHideWindowToolbar( tlw, show, false );
e56d2520
SC
858 }
859 else
e56d2520 860 bResult = wxToolBarBase::Show( show );
3025abca
DS
861#else
862
863 bResult = wxToolBarBase::Show( show );
864#endif
e56d2520 865 }
f3a65c3e 866
e56d2520
SC
867 return bResult;
868}
869
870bool wxToolBar::IsShown() const
871{
872 bool bResult;
873
874#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 875 bool ownToolbarInstalled;
3025abca 876
e56d2520
SC
877 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
878 if (ownToolbarInstalled)
a749a99f
SC
879 {
880 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
3b6a1179 881 bResult = IsWindowToolbarVisible( tlw );
a749a99f 882 }
e56d2520 883 else
e56d2520 884 bResult = wxToolBarBase::IsShown();
3025abca
DS
885#else
886
887 bResult = wxToolBarBase::IsShown();
888#endif
f3a65c3e 889
e56d2520
SC
890 return bResult;
891}
892
e56d2520
SC
893void wxToolBar::DoGetSize( int *width, int *height ) const
894{
895#if wxMAC_USE_NATIVE_TOOLBAR
896 Rect boundsR;
897 bool ownToolbarInstalled;
f3a65c3e 898
e56d2520
SC
899 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
900 if ( ownToolbarInstalled )
901 {
991f71dc 902 // TODO: is this really a control ?
e56d2520
SC
903 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
904 if ( width != NULL )
905 *width = boundsR.right - boundsR.left;
906 if ( height != NULL )
907 *height = boundsR.bottom - boundsR.top;
908 }
909 else
e56d2520 910 wxToolBarBase::DoGetSize( width, height );
3025abca
DS
911
912#else
913 wxToolBarBase::DoGetSize( width, height );
914#endif
e9576ca5
SC
915}
916
b13095df
SC
917wxSize wxToolBar::DoGetBestSize() const
918{
3b6a1179 919 int width, height;
991f71dc 920
3b6a1179 921 DoGetSize( &width, &height );
991f71dc 922
3b6a1179 923 return wxSize( width, height );
b13095df
SC
924}
925
f3a65c3e 926void wxToolBar::SetWindowStyleFlag( long style )
e56d2520
SC
927{
928 wxToolBarBase::SetWindowStyleFlag( style );
991f71dc 929
e56d2520
SC
930#if wxMAC_USE_NATIVE_TOOLBAR
931 if (m_macHIToolbarRef != NULL)
932 {
933 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
934
935 if ( style & wxTB_NOICONS )
936 mode = kHIToolbarDisplayModeLabelOnly;
937 else if ( style & wxTB_TEXT )
938 mode = kHIToolbarDisplayModeIconAndLabel;
939 else
940 mode = kHIToolbarDisplayModeIconOnly;
f3a65c3e 941
e56d2520
SC
942 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
943 }
944#endif
945}
946
947#if wxMAC_USE_NATIVE_TOOLBAR
948bool wxToolBar::MacWantsNativeToolbar()
949{
950 return m_macUsesNativeToolbar;
951}
952
953bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
954{
955 bool bResultV = false;
956
957 if (ownToolbarInstalled != NULL)
958 *ownToolbarInstalled = false;
959
960 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
961 if (tlw != NULL)
962 {
963 HIToolbarRef curToolbarRef = NULL;
964 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
3025abca 965 bResultV = ((err == noErr) && (curToolbarRef != NULL));
e56d2520
SC
966 if (bResultV && (ownToolbarInstalled != NULL))
967 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
968 }
969
970 return bResultV;
971}
972
f3a65c3e 973bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
e56d2520 974{
df7998fc 975 bool bResult = false;
e56d2520 976
e56d2520
SC
977 if (usesNative && (m_macHIToolbarRef == NULL))
978 return bResult;
f3a65c3e 979
e56d2520
SC
980 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
981 return bResult;
f3a65c3e 982
3025abca
DS
983 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
984 if (tlw == NULL)
985 return bResult;
986
e56d2520
SC
987 // check the existing toolbar
988 HIToolbarRef curToolbarRef = NULL;
989 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
991f71dc 990 if (err != noErr)
e56d2520
SC
991 curToolbarRef = NULL;
992
993 m_macUsesNativeToolbar = usesNative;
994
995 if (m_macUsesNativeToolbar)
996 {
997 // only install toolbar if there isn't one installed already
998 if (curToolbarRef == NULL)
999 {
1000 bResult = true;
1001
1002 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
1003 ShowHideWindowToolbar( tlw, true, false );
1004 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
1005 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
f3a65c3e 1006
3b6a1179 1007 Rect r = { 0, 0, 0, 0 };
e56d2520 1008 m_peer->SetRect( &r );
e56d2520 1009 SetSize( wxSIZE_AUTO_WIDTH, 0 );
e56d2520
SC
1010 m_peer->SetVisibility( false, true );
1011 wxToolBarBase::Show( false );
1012 }
1013 }
1014 else
1015 {
1016 // only deinstall toolbar if this is the installed one
1017 if (m_macHIToolbarRef == curToolbarRef)
1018 {
1019 bResult = true;
1020
1021 ShowHideWindowToolbar( tlw, false, false );
3b6a1179 1022 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
e56d2520 1023 SetWindowToolbar( tlw, NULL );
f3a65c3e 1024
e56d2520 1025 m_peer->SetVisibility( true, true );
e56d2520
SC
1026 }
1027 }
1028
1029 if (bResult)
1030 InvalidateBestSize();
1031
1032// wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1033 return bResult;
1034}
1035#endif
1036
37e2cb08 1037bool wxToolBar::Realize()
e9576ca5 1038{
eb22f2a6 1039 if (m_tools.GetCount() == 0)
3803c372 1040 return false;
0b7a8cd3 1041
e56d2520
SC
1042 int maxWidth = 0;
1043 int maxHeight = 0;
f3a65c3e 1044
bfe9ffbc
SC
1045 int maxToolWidth = 0;
1046 int maxToolHeight = 0;
f3a65c3e 1047
991f71dc
DS
1048 int x = m_xMargin + kwxMacToolBarLeftMargin;
1049 int y = m_yMargin + kwxMacToolBarTopMargin;
1050
1051 int tw, th;
1052 GetSize( &tw, &th );
1053
e56d2520 1054 // find the maximum tool width and height
3025abca
DS
1055 wxToolBarTool *tool;
1056 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
e56d2520 1057 while ( node != NULL )
bfe9ffbc 1058 {
3025abca 1059 tool = (wxToolBarTool *) node->GetData();
e56d2520
SC
1060 if ( tool != NULL )
1061 {
1062 wxSize sz = tool->GetSize();
f3a65c3e 1063
e56d2520
SC
1064 if ( sz.x > maxToolWidth )
1065 maxToolWidth = sz.x;
1066 if ( sz.y > maxToolHeight )
1067 maxToolHeight = sz.y;
1068 }
f3a65c3e 1069
bfe9ffbc
SC
1070 node = node->GetNext();
1071 }
f3a65c3e 1072
991f71dc
DS
1073 bool lastIsRadio = false;
1074 bool curIsRadio = false;
df7998fc
VZ
1075
1076#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
1077 CFIndex currentPosition = 0;
1078 bool insertAll = false;
991f71dc 1079#endif
df7998fc 1080
991f71dc 1081 node = m_tools.GetFirst();
e56d2520 1082 while ( node != NULL )
7810c95b 1083 {
3025abca 1084 tool = (wxToolBarTool*) node->GetData();
e56d2520 1085 if ( tool == NULL )
214b9484 1086 {
e56d2520
SC
1087 node = node->GetNext();
1088 continue;
214b9484 1089 }
f3a65c3e 1090
991f71dc 1091 // set tool position:
e56d2520
SC
1092 // for the moment just perform a single row/column alignment
1093 wxSize cursize = tool->GetSize();
bfe9ffbc 1094 if ( x + cursize.x > maxWidth )
e56d2520 1095 maxWidth = x + cursize.x;
bfe9ffbc 1096 if ( y + cursize.y > maxHeight )
e56d2520 1097 maxHeight = y + cursize.y;
f3a65c3e 1098
73fd9428
SC
1099 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
1100 {
e56d2520
SC
1101 int x1 = x + ( maxToolWidth - cursize.x ) / 2;
1102 tool->SetPosition( wxPoint(x1, y) );
73fd9428
SC
1103 }
1104 else
1105 {
e56d2520
SC
1106 int y1 = y + ( maxToolHeight - cursize.y ) / 2;
1107 tool->SetPosition( wxPoint(x, y1) );
1108 }
f3a65c3e 1109
e56d2520 1110 // update the item positioning state
bfe9ffbc 1111 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
e56d2520
SC
1112 y += cursize.y + kwxMacToolSpacing;
1113 else
1114 x += cursize.x + kwxMacToolSpacing;
f3a65c3e 1115
e56d2520
SC
1116#if wxMAC_USE_NATIVE_TOOLBAR
1117 // install in native HIToolbar
1118 if ( m_macHIToolbarRef != NULL )
bfe9ffbc 1119 {
e56d2520
SC
1120 HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef();
1121 if ( hiItemRef != NULL )
1122 {
991f71dc 1123 if ( insertAll || (tool->GetIndex() != currentPosition) )
e56d2520 1124 {
dcae64c2 1125 OSStatus err = noErr;
8138cfec
SC
1126 if ( !insertAll )
1127 {
dcae64c2
DS
1128 insertAll = true;
1129
8138cfec
SC
1130 // if this is the first tool that gets newly inserted or repositioned
1131 // first remove all 'old' tools from here to the right, because of this
1132 // all following tools will have to be reinserted (insertAll). i = 100 because there's
1133 // no way to determine how many there are in a toolbar, so just a high number :-(
3b6a1179 1134 for ( CFIndex i = 100; i >= currentPosition; --i )
8138cfec 1135 {
dcae64c2 1136 err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i );
8138cfec 1137 }
991f71dc 1138
dcae64c2
DS
1139 if (err != noErr)
1140 {
1141 wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err );
4362c705 1142 wxFAIL_MSG( errMsg.c_str() );
dcae64c2
DS
1143 }
1144 }
991f71dc 1145
dcae64c2
DS
1146 err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition );
1147 if (err != noErr)
1148 {
1149 wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err );
4362c705 1150 wxFAIL_MSG( errMsg.c_str() );
dcae64c2 1151 }
3b6a1179 1152
dcae64c2 1153 tool->SetIndex( currentPosition );
e56d2520 1154 }
991f71dc 1155
dcae64c2 1156 currentPosition++;
e56d2520
SC
1157 }
1158 }
dcae64c2 1159#endif
f3a65c3e 1160
e56d2520
SC
1161 // update radio button (and group) state
1162 lastIsRadio = curIsRadio;
1163 curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) );
f3a65c3e 1164
e56d2520
SC
1165 if ( !curIsRadio )
1166 {
1167 if ( tool->IsToggled() )
1168 DoToggleTool( tool, true );
0b7a8cd3
GD
1169 }
1170 else
1171 {
e56d2520
SC
1172 if ( !lastIsRadio )
1173 {
dcae64c2 1174 if ( tool->Toggle( true ) )
e56d2520
SC
1175 {
1176 DoToggleTool( tool, true );
e56d2520
SC
1177 }
1178 }
1179 else if ( tool->IsToggled() )
1180 {
1181 if ( tool->IsToggled() )
1182 DoToggleTool( tool, true );
f3a65c3e 1183
e56d2520
SC
1184 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1185 while ( nodePrev != NULL )
1186 {
1187 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1188 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1189 break;
f3a65c3e 1190
dcae64c2 1191 if ( toggleTool->Toggle( false ) )
e56d2520 1192 DoToggleTool( toggleTool, false );
f3a65c3e 1193
e56d2520
SC
1194 nodePrev = nodePrev->GetPrevious();
1195 }
1196 }
0b7a8cd3 1197 }
f3a65c3e 1198
eb22f2a6 1199 node = node->GetNext();
7810c95b 1200 }
f3a65c3e 1201
0b7a8cd3 1202 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
7810c95b 1203 {
e56d2520
SC
1204 // if not set yet, only one row
1205 if ( m_maxRows <= 0 )
1206 SetRows( 1 );
f3a65c3e 1207
90d3f91a 1208 m_minWidth = maxWidth;
e56d2520 1209 maxWidth = tw;
0b7a8cd3 1210 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
e56d2520 1211 m_minHeight = m_maxHeight = maxHeight;
7810c95b 1212 }
0b7a8cd3
GD
1213 else
1214 {
e56d2520
SC
1215 // if not set yet, have one column
1216 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1217 SetRows( GetToolsCount() );
f3a65c3e 1218
90d3f91a 1219 m_minHeight = maxHeight;
e56d2520 1220 maxHeight = th;
0b7a8cd3 1221 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
e56d2520 1222 m_minWidth = m_maxWidth = maxWidth;
0b7a8cd3 1223 }
e56d2520 1224
f3a65c3e 1225#if 0
e56d2520
SC
1226 // FIXME: should this be OSX-only?
1227 {
1228 bool wantNativeToolbar, ownToolbarInstalled;
1229
1230 // attempt to install the native toolbar
1231 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1232 MacInstallNativeToolbar( wantNativeToolbar );
1233 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1234 if (!ownToolbarInstalled)
1235 {
1236 SetSize( maxWidth, maxHeight );
1237 InvalidateBestSize();
1238 }
1239 }
f3a65c3e 1240#else
facd6764 1241 SetSize( maxWidth, maxHeight );
9f884528 1242 InvalidateBestSize();
e56d2520 1243#endif
2c1dbc95 1244
170acdc9 1245 SetInitialSize();
2c1dbc95 1246
3803c372 1247 return true;
e9576ca5
SC
1248}
1249
1250void wxToolBar::SetToolBitmapSize(const wxSize& size)
1251{
e56d2520
SC
1252 m_defaultWidth = size.x + kwxMacToolBorder;
1253 m_defaultHeight = size.y + kwxMacToolBorder;
f3a65c3e 1254
e56d2520
SC
1255#if wxMAC_USE_NATIVE_TOOLBAR
1256 if (m_macHIToolbarRef != NULL)
1257 {
1258 int maxs = wxMax( size.x, size.y );
3b6a1179 1259 HIToolbarDisplaySize sizeSpec;
328f4fee 1260 if ( maxs > 32 )
3b6a1179 1261 sizeSpec = kHIToolbarDisplaySizeNormal;
00a7bd85 1262 else if ( maxs > 24 )
3b6a1179 1263 sizeSpec = kHIToolbarDisplaySizeDefault;
328f4fee 1264 else
3b6a1179 1265 sizeSpec = kHIToolbarDisplaySizeSmall;
f3a65c3e 1266
e56d2520
SC
1267 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
1268 }
1269#endif
e9576ca5
SC
1270}
1271
e9576ca5
SC
1272// The button size is bigger than the bitmap size
1273wxSize wxToolBar::GetToolSize() const
1274{
ee799df7 1275 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
e9576ca5
SC
1276}
1277
37e2cb08 1278void wxToolBar::SetRows(int nRows)
e9576ca5 1279{
e56d2520
SC
1280 // avoid resizing the frame uselessly
1281 if ( nRows != m_maxRows )
e56d2520 1282 m_maxRows = nRows;
e9576ca5
SC
1283}
1284
f3a65c3e 1285void wxToolBar::MacSuperChangedPosition()
c257d44d 1286{
e56d2520 1287 wxWindow::MacSuperChangedPosition();
991f71dc 1288
e56d2520
SC
1289#if wxMAC_USE_NATIVE_TOOLBAR
1290 if (! m_macUsesNativeToolbar )
e56d2520 1291 Realize();
991f71dc 1292#else
3025abca 1293
991f71dc
DS
1294 Realize();
1295#endif
c257d44d
SC
1296}
1297
37e2cb08 1298wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
e9576ca5 1299{
3025abca 1300 wxToolBarTool *tool;
affd2611 1301 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
e56d2520 1302 while ( node != NULL )
e044f600 1303 {
3025abca 1304 tool = (wxToolBarTool *)node->GetData();
e56d2520 1305 if (tool != NULL)
e044f600 1306 {
e56d2520
SC
1307 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1308 if ( r.Contains( wxPoint( x, y ) ) )
1309 return tool;
e044f600 1310 }
bfe9ffbc
SC
1311
1312 node = node->GetNext();
e044f600 1313 }
37e2cb08 1314
3025abca 1315 return (wxToolBarToolBase*)NULL;
e9576ca5
SC
1316}
1317
2f1ae414
SC
1318wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1319{
3b6a1179 1320 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
e56d2520 1321 if ( tool != NULL )
3b6a1179 1322 return tool->GetShortHelp();
e56d2520 1323
3b6a1179 1324 return wxEmptyString;
2f1ae414
SC
1325}
1326
37e2cb08 1327void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
e9576ca5 1328{
e56d2520 1329 if ( t != NULL )
3b6a1179 1330 ((wxToolBarTool*)t)->DoEnable( enable );
e9576ca5
SC
1331}
1332
37e2cb08 1333void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
e9576ca5 1334{
e044f600 1335 wxToolBarTool *tool = (wxToolBarTool *)t;
e56d2520
SC
1336 if ( ( tool != NULL ) && tool->IsButton() )
1337 tool->UpdateToggleImage( toggle );
37e2cb08 1338}
7c551d95 1339
3b6a1179 1340bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
37e2cb08 1341{
3b6a1179 1342 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
e56d2520
SC
1343 if (tool == NULL)
1344 return false;
1345
1346 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1347 wxSize toolSize = GetToolSize();
3b6a1179 1348 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
e56d2520
SC
1349 ControlRef controlHandle = NULL;
1350 OSStatus err = 0;
be5fe3aa 1351
e56d2520 1352 switch (tool->GetStyle())
be5fe3aa 1353 {
3b6a1179 1354 case wxTOOL_STYLE_SEPARATOR:
be5fe3aa 1355 {
e56d2520
SC
1356 wxASSERT( tool->GetControlHandle() == NULL );
1357 toolSize.x /= 4;
1358 toolSize.y /= 4;
be5fe3aa 1359 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
e56d2520 1360 toolrect.bottom = toolSize.y;
be5fe3aa 1361 else
e56d2520
SC
1362 toolrect.right = toolSize.x;
1363
991f71dc 1364#ifdef __WXMAC_OSX__
be5fe3aa 1365 // in flat style we need a visual separator
991f71dc 1366#if wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1367 HIToolbarItemRef item;
991f71dc
DS
1368 err = HIToolbarItemCreate(
1369 kHIToolbarSeparatorIdentifier,
1370 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1371 &item );
e56d2520
SC
1372 if (err == noErr)
1373 tool->SetToolbarItemRef( item );
991f71dc
DS
1374#endif
1375
e56d2520
SC
1376 CreateSeparatorControl( window, &toolrect, &controlHandle );
1377 tool->SetControlHandle( controlHandle );
991f71dc 1378#endif
be5fe3aa 1379 }
e56d2520
SC
1380 break;
1381
3b6a1179 1382 case wxTOOL_STYLE_BUTTON:
be5fe3aa 1383 {
3b6a1179
DS
1384 wxASSERT( tool->GetControlHandle() == NULL );
1385 ControlButtonContentInfo info;
1386 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
f3a65c3e 1387
df7998fc 1388 if ( UMAGetSystemVersion() >= 0x1000)
3b6a1179
DS
1389 {
1390 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1391 }
df7998fc
VZ
1392 else
1393 {
3b6a1179 1394 SInt16 behaviour = kControlBehaviorOffsetContents;
df7998fc 1395 if ( tool->CanBeToggled() )
3b6a1179
DS
1396 behaviour |= kControlBehaviorToggles;
1397 err = CreateBevelButtonControl( window,
1398 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1399 behaviour, &info, 0, 0, 0, &controlHandle );
df7998fc 1400 }
e56d2520
SC
1401
1402#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 1403 HIToolbarItemRef item;
30962327 1404 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
e56d2520
SC
1405 err = HIToolbarItemCreate(
1406 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1407 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1408 if (err == noErr)
1409 {
3025abca
DS
1410 InstallEventHandler(
1411 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1412 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
e56d2520
SC
1413 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1414 HIToolbarItemSetIconRef( item, info.u.iconRef );
2c1dbc95 1415 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
e56d2520
SC
1416 tool->SetToolbarItemRef( item );
1417 }
991f71dc 1418#endif
e56d2520 1419
3b6a1179 1420 wxMacReleaseBitmapButton( &info );
3025abca 1421
991f71dc 1422#if 0
3b6a1179
DS
1423 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1424 UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );
991f71dc 1425#endif
f3a65c3e 1426
3b6a1179
DS
1427 InstallControlEventHandler(
1428 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
e56d2520 1429 GetEventTypeCount(eventList), eventList, tool, NULL );
be5fe3aa 1430
e56d2520 1431 tool->SetControlHandle( controlHandle );
be5fe3aa 1432 }
e56d2520
SC
1433 break;
1434
3b6a1179 1435 case wxTOOL_STYLE_CONTROL:
3b6a1179 1436
6d4835dc 1437#if wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1438 {
0ac091ae 1439 wxASSERT( tool->GetControl() != NULL );
e56d2520 1440 HIToolbarItemRef item;
6d4835dc 1441 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
9d5ccdd3
SC
1442 // as this control now is part of both the wxToolBar children and the native toolbar, we have to increase the
1443 // reference count to make sure we are not dealing with zombie controls after the native toolbar has released its views
1444 CFRetain( viewRef ) ;
6d4835dc
SC
1445 CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ;
1446 err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID,
1447 data , &item ) ;
1448
1449 if (err == noErr)
e56d2520 1450 {
e56d2520 1451 tool->SetToolbarItemRef( item );
e56d2520 1452 }
6d4835dc
SC
1453 CFRelease( data ) ;
1454 }
f3a65c3e 1455
e56d2520 1456#else
6d4835dc 1457 // right now there's nothing to do here
e56d2520
SC
1458#endif
1459 break;
1460
3b6a1179 1461 default:
e56d2520 1462 break;
be5fe3aa 1463 }
f3a65c3e 1464
991f71dc 1465 if ( err == noErr )
be5fe3aa 1466 {
e56d2520
SC
1467 if ( controlHandle )
1468 {
1469 ControlRef container = (ControlRef) GetHandle();
3b6a1179 1470 wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") );
be5fe3aa 1471
e56d2520
SC
1472 UMAShowControl( controlHandle );
1473 ::EmbedControl( controlHandle, container );
1474 }
1475
1476 if ( tool->CanBeToggled() && tool->IsToggled() )
1477 tool->UpdateToggleImage( true );
be5fe3aa 1478
e56d2520 1479 // nothing special to do here - we relayout in Realize() later
3b6a1179 1480 tool->Attach( this );
e56d2520
SC
1481 InvalidateBestSize();
1482 }
1483 else
be5fe3aa 1484 {
3b6a1179 1485 wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err );
4362c705 1486 wxFAIL_MSG( errMsg.c_str() );
be5fe3aa 1487 }
f3a65c3e 1488
991f71dc 1489 return (err == noErr);
37e2cb08 1490}
e9576ca5 1491
5115c51a 1492void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
37e2cb08 1493{
3b6a1179 1494 wxFAIL_MSG( wxT("not implemented") );
e9576ca5
SC
1495}
1496
be5fe3aa 1497bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
37e2cb08 1498{
3b6a1179 1499 wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase );
affd2611 1500 wxToolBarToolsList::compatibility_iterator node;
bfe9ffbc
SC
1501 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
1502 {
1503 wxToolBarToolBase *tool2 = node->GetData();
1504 if ( tool2 == tool )
1505 {
1506 // let node point to the next node in the list
1507 node = node->GetNext();
1508
1509 break;
1510 }
1511 }
1512
3b6a1179 1513 wxSize sz = ((wxToolBarTool*)tool)->GetSize();
bfe9ffbc
SC
1514
1515 tool->Detach();
df7998fc
VZ
1516
1517#if wxMAC_USE_NATIVE_TOOLBAR
1518 CFIndex removeIndex = tool->GetIndex();
dcae64c2 1519#endif
bfe9ffbc 1520
488b2c29
SC
1521 switch ( tool->GetStyle() )
1522 {
1523 case wxTOOL_STYLE_CONTROL:
be5fe3aa
SC
1524 {
1525 tool->GetControl()->Destroy();
3b6a1179 1526 tool->ClearControl();
be5fe3aa 1527 }
488b2c29
SC
1528 break;
1529
1530 case wxTOOL_STYLE_BUTTON:
1531 case wxTOOL_STYLE_SEPARATOR:
be5fe3aa 1532 if ( tool->GetControlHandle() )
488b2c29 1533 {
e56d2520 1534#if wxMAC_USE_NATIVE_TOOLBAR
df7998fc 1535 if ( removeIndex != -1 && m_macHIToolbarRef )
2c1dbc95 1536 {
3b6a1179
DS
1537 HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
1538 tool->SetIndex( -1 );
2c1dbc95 1539 }
991f71dc
DS
1540#endif
1541
3b6a1179 1542 tool->ClearControl();
488b2c29
SC
1543 }
1544 break;
e56d2520
SC
1545
1546 default:
1547 break;
488b2c29
SC
1548 }
1549
bfe9ffbc 1550 // and finally reposition all the controls after this one
f3a65c3e 1551
3b6a1179 1552 for ( /* node -> first after deleted */; node; node = node->GetNext() )
bfe9ffbc
SC
1553 {
1554 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
3b6a1179 1555 wxPoint pt = tool2->GetPosition();
bfe9ffbc
SC
1556
1557 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
3b6a1179 1558 pt.y -= sz.y;
bfe9ffbc 1559 else
3b6a1179 1560 pt.x -= sz.x;
e56d2520 1561
3b6a1179 1562 tool2->SetPosition( pt );
df7998fc 1563
2c1dbc95
SC
1564#if wxMAC_USE_NATIVE_TOOLBAR
1565 if ( removeIndex != -1 && tool2->GetIndex() > removeIndex )
3b6a1179 1566 tool2->SetIndex( tool2->GetIndex() - 1 );
2c1dbc95 1567#endif
bfe9ffbc 1568 }
f3a65c3e 1569
9f884528 1570 InvalidateBestSize();
991f71dc 1571
3b6a1179 1572 return true;
37e2cb08 1573}
2f1ae414
SC
1574
1575void wxToolBar::OnPaint(wxPaintEvent& event)
1576{
e56d2520
SC
1577#if wxMAC_USE_NATIVE_TOOLBAR
1578 if ( m_macUsesNativeToolbar )
1579 {
dcae64c2
DS
1580 event.Skip(true);
1581 return;
e56d2520
SC
1582 }
1583#endif
f3a65c3e 1584
3b6a1179 1585 wxPaintDC dc(this);
ddc548ec 1586
3b6a1179
DS
1587 int w, h;
1588 GetSize( &w, &h );
991f71dc 1589
dcae64c2
DS
1590 bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance();
1591 bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030);
1592
ddc548ec 1593#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
dcae64c2 1594 if ( !drawMetalTheme && minimumUmaAvailable )
ddc548ec 1595 {
dcae64c2
DS
1596 HIThemePlacardDrawInfo info;
1597 memset( &info, 0, sizeof(info) );
1598 info.version = 0;
1599 info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive;
1600
1601 CGContextRef cgContext = (CGContextRef) MacGetCGContextRef();
1602 HIRect rect = CGRectMake( 0, 0, w, h );
1603 HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal );
ddc548ec
SC
1604 }
1605 else
1606 {
1607 // leave the background as it is (striped or metal)
1608 }
991f71dc 1609
ddc548ec 1610#else
dcae64c2
DS
1611
1612 const bool drawBorder = true;
1613
1614 if (drawBorder)
01ffa8f7 1615 {
3b6a1179 1616 wxMacPortSetter helper( &dc );
dcae64c2
DS
1617
1618 if ( !drawMetalTheme || !minimumUmaAvailable )
20b69855 1619 {
3b6a1179
DS
1620 Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1621 dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) };
dcae64c2
DS
1622
1623#if 0
1624 if ( toolbarrect.left < 0 )
3b6a1179 1625 toolbarrect.left = 0;
dcae64c2 1626 if ( toolbarrect.top < 0 )
3b6a1179 1627 toolbarrect.top = 0;
dcae64c2
DS
1628#endif
1629
1630 UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive );
1631 }
1632 else
1633 {
1634#if TARGET_API_MAC_OSX
991f71dc 1635 HIRect hiToolbarrect = CGRectMake(
3b6a1179
DS
1636 dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0),
1637 dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) );
1638 CGContextRef cgContext;
1639 Rect bounds;
dcae64c2 1640
3b6a1179
DS
1641 GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds );
1642 QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
dcae64c2 1643
3b6a1179
DS
1644 CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top );
1645 CGContextScaleCTM( cgContext, 1, -1 );
785f5eaa 1646
3b6a1179
DS
1647 HIThemeBackgroundDrawInfo drawInfo;
1648 drawInfo.version = 0;
1649 drawInfo.state = kThemeStateActive;
1650 drawInfo.kind = kThemeBackgroundMetal;
dcae64c2 1651 HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal );
e56d2520 1652
f387b80e 1653#ifndef __LP64__
3b6a1179 1654 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
f387b80e 1655#endif
20b69855 1656#endif
01ffa8f7 1657 }
01ffa8f7
SC
1658 }
1659#endif
20b69855 1660
dcae64c2 1661 event.Skip();
2f1ae414 1662}
895f5af7 1663
519cb848 1664#endif // wxUSE_TOOLBAR