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