]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toolbar.cpp
adding alpha to wxColour
[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
bfe9ffbc
SC
82 ~wxToolBarTool()
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 {
109 DisposeControl( m_controlHandle );
110 m_controlHandle = NULL ;
111 }
991f71dc 112
e56d2520 113#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 114 m_toolbarItemRef = NULL;
f3a65c3e 115#endif
e56d2520 116 }
f3a65c3e 117
bfe9ffbc
SC
118 wxSize GetSize() const
119 {
3025abca
DS
120 wxSize curSize;
121
bfe9ffbc
SC
122 if ( IsControl() )
123 {
3025abca 124 curSize = GetControl()->GetSize();
bfe9ffbc
SC
125 }
126 else if ( IsButton() )
127 {
3025abca 128 curSize = GetToolBar()->GetToolSize();
bfe9ffbc
SC
129 }
130 else
131 {
abbcdf3f 132 // separator size
3025abca 133 curSize = GetToolBar()->GetToolSize();
5a904a32 134 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
3025abca 135 curSize.y /= 4;
5a904a32 136 else
3025abca 137 curSize.x /= 4;
bfe9ffbc 138 }
3025abca
DS
139
140 return curSize;
bfe9ffbc 141 }
991f71dc 142
bfe9ffbc
SC
143 wxPoint GetPosition() const
144 {
3b6a1179 145 return wxPoint( m_x, m_y );
f3a65c3e 146 }
991f71dc 147
3b6a1179 148 bool DoEnable( bool enable );
f3a65c3e 149
3b6a1179 150 void UpdateToggleImage( bool toggle );
f3a65c3e
VZ
151
152#if wxMAC_USE_NATIVE_TOOLBAR
153 void SetToolbarItemRef( HIToolbarItemRef ref )
154 {
e56d2520 155 if ( m_controlHandle )
3b6a1179 156 HideControl( m_controlHandle );
e56d2520 157 if ( m_toolbarItemRef )
3b6a1179 158 CFRelease( m_toolbarItemRef );
991f71dc 159
3b6a1179 160 m_toolbarItemRef = ref;
e56d2520
SC
161 if ( m_toolbarItemRef )
162 {
163 HIToolbarItemSetHelpText(
991f71dc 164 m_toolbarItemRef,
3b6a1179
DS
165 wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ),
166 wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) );
e56d2520
SC
167 }
168 }
991f71dc 169
f3a65c3e
VZ
170 HIToolbarItemRef GetToolbarItemRef() const
171 {
3b6a1179 172 return m_toolbarItemRef;
e56d2520 173 }
df7998fc
VZ
174
175 void SetIndex( CFIndex idx )
2c1dbc95 176 {
3b6a1179 177 m_index = idx;
2c1dbc95
SC
178 }
179
df7998fc 180 CFIndex GetIndex() const
2c1dbc95 181 {
3b6a1179 182 return m_index;
2c1dbc95 183 }
e56d2520 184#endif
5d0bf05a 185
3b6a1179 186private:
f3a65c3e 187 void Init()
bfe9ffbc 188 {
3b6a1179 189 m_controlHandle = NULL;
991f71dc 190
e56d2520 191#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
192 m_toolbarItemRef = NULL;
193 m_index = -1;
e56d2520 194#endif
bfe9ffbc 195 }
991f71dc 196
3b6a1179 197 ControlRef m_controlHandle;
991f71dc
DS
198 wxCoord m_x;
199 wxCoord m_y;
200
e56d2520 201#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 202 HIToolbarItemRef m_toolbarItemRef;
2c1dbc95 203 // position in its toolbar, -1 means not inserted
3b6a1179 204 CFIndex m_index;
e56d2520 205#endif
37e2cb08
SC
206};
207
facd6764
SC
208static const EventTypeSpec eventList[] =
209{
3b6a1179 210 { kEventClassControl, kEventControlHit },
73fd9428 211#ifdef __WXMAC_OSX__
3b6a1179 212 { kEventClassControl, kEventControlHitTest },
73fd9428 213#endif
3b6a1179 214};
facd6764 215
3b6a1179 216static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
facd6764 217{
3b6a1179
DS
218 OSStatus result = eventNotHandledErr;
219 ControlRef controlRef;
220 wxMacCarbonEvent cEvent( event );
facd6764 221
3b6a1179 222 cEvent.GetParameter( kEventParamDirectObject, &controlRef );
facd6764 223
991f71dc 224 switch ( GetEventKind( event ) )
facd6764 225 {
3b6a1179 226 case kEventControlHit:
facd6764 227 {
3b6a1179
DS
228 wxToolBarTool *tbartool = (wxToolBarTool*)data;
229 wxToolBar *tbar = tbartool != NULL ? (wxToolBar*) (tbartool->GetToolBar()) : NULL;
991f71dc 230 if ((tbartool != NULL) && tbartool->CanBeToggled())
facd6764 231 {
e56d2520 232 bool shouldToggle;
991f71dc 233
64f553a4 234#ifdef __WXMAC_OSX__
e56d2520 235 shouldToggle = !tbartool->IsToggled();
64f553a4 236#else
3025abca 237 shouldToggle = (GetControl32BitValue( (ControlRef)(tbartool->GetControlHandle()) ) != 0);
64f553a4 238#endif
991f71dc 239
e56d2520 240 tbar->ToggleTool( tbartool->GetId(), shouldToggle );
facd6764 241 }
991f71dc 242
e56d2520
SC
243 if (tbartool != NULL)
244 tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() );
f3a65c3e 245 result = noErr;
facd6764 246 }
3b6a1179 247 break;
5d0bf05a 248
73fd9428 249#ifdef __WXMAC_OSX__
3b6a1179 250 case kEventControlHitTest:
73fd9428 251 {
3b6a1179
DS
252 HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation);
253 HIRect rect;
254 HIViewGetBounds( controlRef, &rect );
255
256 ControlPartCode pc = kControlNoPart;
257 if ( CGRectContainsPoint( rect, pt ) )
258 pc = kControlIconPart;
259 cEvent.SetParameter( kEventParamControlPart, typeControlPartCode, pc );
260 result = noErr;
73fd9428 261 }
3b6a1179 262 break;
73fd9428 263#endif
5d0bf05a 264
3b6a1179
DS
265 default:
266 break;
facd6764 267 }
991f71dc 268
3b6a1179 269 return result;
facd6764
SC
270}
271
3b6a1179 272static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
facd6764 273{
3b6a1179 274 OSStatus result = eventNotHandledErr;
facd6764
SC
275
276 switch ( GetEventClass( event ) )
277 {
3b6a1179
DS
278 case kEventClassControl:
279 result = wxMacToolBarToolControlEventHandler( handler, event, data );
280 break;
5d0bf05a 281
3b6a1179
DS
282 default:
283 break;
facd6764 284 }
991f71dc 285
3b6a1179 286 return result;
facd6764
SC
287}
288
289DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
290
e56d2520
SC
291#if wxMAC_USE_NATIVE_TOOLBAR
292
e56d2520
SC
293static const EventTypeSpec toolBarEventList[] =
294{
3b6a1179
DS
295 { kEventClassToolbarItem, kEventToolbarItemPerformAction },
296};
e56d2520 297
3b6a1179 298static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
e56d2520 299{
3b6a1179 300 OSStatus result = eventNotHandledErr;
f3a65c3e 301
991f71dc 302 switch ( GetEventKind( event ) )
e56d2520 303 {
3b6a1179 304 case kEventToolbarItemPerformAction:
e56d2520 305 {
3b6a1179 306 wxToolBarTool* tbartool = (wxToolBarTool*) data;
e56d2520
SC
307 if ( tbartool != NULL )
308 {
991f71dc
DS
309 wxToolBar *tbar = (wxToolBar*)(tbartool->GetToolBar());
310 int toolID = tbartool->GetId();
311
e56d2520
SC
312 if ( tbartool->CanBeToggled() )
313 {
3025abca 314 if ( tbar != NULL )
2c1dbc95 315 tbar->ToggleTool(toolID, !tbartool->IsToggled() );
e56d2520 316 }
991f71dc 317
3025abca 318 if ( tbar != NULL )
3b6a1179 319 tbar->OnLeftClick( toolID, tbartool->IsToggled() );
f3a65c3e 320 result = noErr;
e56d2520
SC
321 }
322 }
3b6a1179 323 break;
5d0bf05a 324
3b6a1179
DS
325 default:
326 break;
e56d2520 327 }
991f71dc 328
3b6a1179 329 return result;
e56d2520
SC
330}
331
332static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data )
333{
3b6a1179 334 OSStatus result = eventNotHandledErr;
991f71dc
DS
335
336 switch ( GetEventClass( event ) )
e56d2520 337 {
3b6a1179
DS
338 case kEventClassToolbarItem:
339 result = wxMacToolBarCommandEventHandler( handler, event, data );
340 break;
5d0bf05a 341
3b6a1179
DS
342 default:
343 break;
e56d2520 344 }
991f71dc 345
3b6a1179 346 return result;
e56d2520
SC
347}
348
349DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler )
350
351#endif
352
3b6a1179 353bool wxToolBarTool::DoEnable( bool enable )
a2fe01c9
SC
354{
355 if ( IsControl() )
356 {
3b6a1179 357 GetControl()->Enable( enable );
a2fe01c9
SC
358 }
359 else if ( IsButton() )
360 {
f3a65c3e 361#if wxMAC_USE_NATIVE_TOOLBAR
3025abca 362 if ( m_toolbarItemRef != NULL )
3b6a1179 363 HIToolbarItemSetEnabled( m_toolbarItemRef, enable );
e56d2520
SC
364#endif
365
3025abca 366 if ( m_controlHandle != NULL )
e56d2520 367 {
a2fe01c9 368#if TARGET_API_MAC_OSX
e56d2520 369 if ( enable )
3b6a1179 370 EnableControl( m_controlHandle );
e56d2520 371 else
3b6a1179 372 DisableControl( m_controlHandle );
a2fe01c9 373#else
e56d2520 374 if ( enable )
3b6a1179 375 ActivateControl( m_controlHandle );
e56d2520 376 else
3b6a1179 377 DeactivateControl( m_controlHandle );
a2fe01c9 378#endif
e56d2520 379 }
a2fe01c9 380 }
991f71dc 381
3b6a1179 382 return true;
a2fe01c9 383}
bfe9ffbc 384
3b6a1179 385void wxToolBarTool::SetPosition( const wxPoint& position )
bfe9ffbc
SC
386{
387 m_x = position.x;
388 m_y = position.y;
389
3b6a1179
DS
390 int x, y;
391 x = y = 0;
392 int mac_x = position.x;
393 int mac_y = position.y;
789ae0cf 394
789ae0cf
SC
395 if ( IsButton() )
396 {
3b6a1179
DS
397 Rect contrlRect;
398 GetControlBounds( m_controlHandle, &contrlRect );
399 int former_mac_x = contrlRect.left;
400 int former_mac_y = contrlRect.top;
401 GetToolBar()->GetToolSize();
f3a65c3e 402
bfe9ffbc
SC
403 if ( mac_x != former_mac_x || mac_y != former_mac_y )
404 {
3b6a1179 405 UMAMoveControl( m_controlHandle, mac_x, mac_y );
bfe9ffbc
SC
406 }
407 }
408 else if ( IsControl() )
409 {
3b6a1179 410 GetControl()->Move( position );
bfe9ffbc 411 }
abbcdf3f
SC
412 else
413 {
f3a65c3e 414 // separator
abbcdf3f 415#ifdef __WXMAC_OSX__
3b6a1179
DS
416 Rect contrlRect;
417 GetControlBounds( m_controlHandle, &contrlRect );
418 int former_mac_x = contrlRect.left;
419 int former_mac_y = contrlRect.top;
f3a65c3e 420
abbcdf3f 421 if ( mac_x != former_mac_x || mac_y != former_mac_y )
3b6a1179 422 UMAMoveControl( m_controlHandle, mac_x, mac_y );
abbcdf3f
SC
423#endif
424 }
bfe9ffbc
SC
425}
426
f3a65c3e 427void wxToolBarTool::UpdateToggleImage( bool toggle )
73fd9428 428{
f3a65c3e 429#if wxMAC_USE_NATIVE_TOOLBAR
e56d2520
SC
430
431#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
3025abca 432#define kHIToolbarItemSelected (1 << 7)
e56d2520
SC
433#endif
434
435 // FIXME: this should be a OSX v10.4 runtime check
436 if (m_toolbarItemRef != NULL)
437 {
438 OptionBits addAttrs, removeAttrs;
439 OSStatus result;
440
441 if (toggle)
442 {
443 addAttrs = kHIToolbarItemSelected;
444 removeAttrs = kHIToolbarItemNoAttributes;
445 }
446 else
447 {
448 addAttrs = kHIToolbarItemNoAttributes;
449 removeAttrs = kHIToolbarItemSelected;
450 }
451
452 result = HIToolbarItemChangeAttributes( m_toolbarItemRef, addAttrs, removeAttrs );
453 }
454#endif
5d0bf05a 455
73fd9428
SC
456#ifdef __WXMAC_OSX__
457 if ( toggle )
458 {
3b6a1179
DS
459 int w = m_bmpNormal.GetWidth();
460 int h = m_bmpNormal.GetHeight();
461 wxBitmap bmp( w, h );
462 wxMemoryDC dc;
463
464 dc.SelectObject( bmp );
465 dc.SetPen( wxNullPen );
466 dc.SetBackground( *wxWHITE );
467 dc.DrawRectangle( 0, 0, w, h );
468 dc.DrawBitmap( m_bmpNormal, 0, 0, true );
469 dc.SelectObject( wxNullBitmap );
470 ControlButtonContentInfo info;
471 wxMacCreateBitmapButton( &info, bmp );
991f71dc 472 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
3b6a1179 473 wxMacReleaseBitmapButton( &info );
73fd9428
SC
474 }
475 else
476 {
3b6a1179
DS
477 ControlButtonContentInfo info;
478 wxMacCreateBitmapButton( &info, m_bmpNormal );
3025abca 479 SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info );
3b6a1179 480 wxMacReleaseBitmapButton( &info );
73fd9428
SC
481 }
482
3b6a1179
DS
483 IconTransformType transform = toggle ? kTransformSelected : kTransformNone;
484 SetControlData(
485 m_controlHandle, 0, kControlIconTransformTag,
3025abca 486 sizeof(transform), (Ptr)&transform );
3b6a1179 487 HIViewSetNeedsDisplay( m_controlHandle, true );
73fd9428
SC
488
489#else
3b6a1179 490 ::SetControl32BitValue( m_controlHandle, toggle );
73fd9428
SC
491#endif
492}
493
3b6a1179
DS
494wxToolBarTool::wxToolBarTool(
495 wxToolBar *tbar,
496 int id,
497 const wxString& label,
498 const wxBitmap& bmpNormal,
499 const wxBitmap& bmpDisabled,
500 wxItemKind kind,
501 wxObject *clientData,
502 const wxString& shortHelp,
503 const wxString& longHelp )
504 :
505 wxToolBarToolBase(
506 tbar, id, label, bmpNormal, bmpDisabled, kind,
507 clientData, shortHelp, longHelp )
bfe9ffbc 508{
f4e0be4f 509 Init();
bfe9ffbc
SC
510}
511
e56d2520
SC
512#pragma mark -
513#pragma mark Toolbar Implementation
2f1ae414 514
3b6a1179
DS
515wxToolBarToolBase *wxToolBar::CreateTool(
516 int id,
517 const wxString& label,
518 const wxBitmap& bmpNormal,
519 const wxBitmap& bmpDisabled,
520 wxItemKind kind,
521 wxObject *clientData,
522 const wxString& shortHelp,
523 const wxString& longHelp )
37e2cb08 524{
3b6a1179
DS
525 return new wxToolBarTool(
526 this, id, label, bmpNormal, bmpDisabled, kind,
527 clientData, shortHelp, longHelp );
37e2cb08
SC
528}
529
3b6a1179 530wxToolBarToolBase * wxToolBar::CreateTool( wxControl *control )
37e2cb08 531{
3025abca 532 return new wxToolBarTool( this, control );
37e2cb08
SC
533}
534
37e2cb08 535void wxToolBar::Init()
e9576ca5 536{
e40298d5
JS
537 m_maxWidth = -1;
538 m_maxHeight = -1;
539 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
540 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
991f71dc 541
e56d2520 542#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
543 m_macHIToolbarRef = NULL;
544 m_macUsesNativeToolbar = false;
e56d2520 545#endif
e9576ca5
SC
546}
547
6d4835dc
SC
548#define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" )
549
550const EventTypeSpec kEvents[] =
551{
552 { kEventClassHIObject, kEventHIObjectConstruct },
553 { kEventClassHIObject, kEventHIObjectInitialize },
554 { kEventClassHIObject, kEventHIObjectDestruct },
555
556 { kEventClassToolbarItem, kEventToolbarItemCreateCustomView }
557};
558
559const EventTypeSpec kViewEvents[] =
560{
561 { kEventClassControl, kEventControlGetSizeConstraints }
562};
563
564struct ControlToolbarItem
565{
566 HIToolbarItemRef toolbarItem;
567 HIViewRef viewRef;
568 wxSize lastValidSize ;
569};
570
571static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
572{
573 OSStatus result = eventNotHandledErr;
574 ControlToolbarItem* object = (ControlToolbarItem*)inUserData;
575
576 switch ( GetEventClass( inEvent ) )
577 {
578 case kEventClassHIObject:
579 switch ( GetEventKind( inEvent ) )
580 {
581 case kEventHIObjectConstruct:
582 {
583 HIObjectRef toolbarItem;
584 ControlToolbarItem* item;
585
586 GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL,
587 sizeof( HIObjectRef ), NULL, &toolbarItem );
588
589 item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ;
590 item->toolbarItem = toolbarItem ;
591 item->viewRef = NULL ;
592
593 SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item );
594
595 result = noErr ;
596 }
597 break;
598
599 case kEventHIObjectInitialize:
600 result = CallNextEventHandler( inCallRef, inEvent );
601 if ( result == noErr )
602 {
603 CFDataRef data;
604 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
605 sizeof( CFTypeRef ), NULL, &data );
606
607 HIViewRef viewRef ;
608
609 wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ;
610 memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ;
611
612 object->viewRef = (HIViewRef) viewRef ;
613
614 result = noErr ;
615 }
616 break;
617
618 case kEventHIObjectDestruct:
619 free( object ) ;
620 result = noErr;
621 break;
622 }
623 break;
624
625 case kEventClassToolbarItem:
626 switch ( GetEventKind( inEvent ) )
627 {
628 case kEventToolbarItemCreateCustomView:
629 {
630 HIViewRef viewRef = object->viewRef ;
631
632 HIViewRemoveFromSuperview( viewRef ) ;
633 HIViewSetVisible(viewRef, true) ;
634 InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler,
635 GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL );
636
637 result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef );
638 }
639 break;
640 }
641 break;
642
643 case kEventClassControl:
644 switch ( GetEventKind( inEvent ) )
645 {
646 case kEventControlGetSizeConstraints:
647 {
648 wxWindow* wxwindow = wxFindControlFromMacControl(object->viewRef ) ;
649 if ( wxwindow )
650 {
651 wxSize sz = wxwindow->GetSize() ;
652 // during toolbar layout the native window sometimes gets negative sizes
653 // so we always keep the last valid size here, to make sure we survive the
654 // shuffle ...
655 if ( sz.x > 0 && sz.y > 0 )
656 object->lastValidSize = sz ;
657 else
658 sz = object->lastValidSize ;
659
660 HISize min, max;
661 min.width = max.width = sz.x ;
662 min.height = max.height = sz.y ;
663
664 result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize,
665 sizeof( HISize ), &min );
666
667 result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize,
668 sizeof( HISize ), &max );
669 result = noErr ;
670 }
671 }
672 break;
673 }
674 break;
675 }
676
677 return result;
678}
679
680void RegisterControlToolbarItemClass()
681{
682 static bool sRegistered;
683
684 if ( !sRegistered )
685 {
686 HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0,
687 ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL );
688
689 sRegistered = true;
690 }
691}
692
693HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData)
694{
695 RegisterControlToolbarItemClass();
696
697 OSStatus err;
698 EventRef event;
699 UInt32 options = kHIToolbarItemAllowDuplicates;
700 HIToolbarItemRef result = NULL;
701
702 err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
703 require_noerr( err, CantCreateEvent );
704
705 SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options );
706 SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier );
707
708 if ( inConfigData )
709 SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData );
710
711 err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result );
712 check_noerr( err );
713
714 ReleaseEvent( event );
715CantCreateEvent :
716 return result ;
717}
718
719static const EventTypeSpec kToolbarEvents[] =
720{
721 { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers },
722 { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers },
723 { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier },
724};
725
726static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData )
727{
728 OSStatus result = eventNotHandledErr;
729 wxToolBar* toolbar = (wxToolBar*) inUserData ;
730 CFMutableArrayRef array;
731
732 switch ( GetEventKind( inEvent ) )
733 {
734 case kEventToolbarGetDefaultIdentifiers:
735 {
736 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
737 sizeof( CFMutableArrayRef ), NULL, &array );
738 // not implemented yet
739 // GetToolbarDefaultItems( array );
740 result = noErr;
741 }
742 break;
743
744 case kEventToolbarGetAllowedIdentifiers:
745 {
746 GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL,
747 sizeof( CFMutableArrayRef ), NULL, &array );
748 // not implemented yet
749 // GetToolbarAllowedItems( array );
750 result = noErr;
751 }
752 break;
753 case kEventToolbarCreateItemWithIdentifier:
754 {
755 HIToolbarItemRef item = NULL;
756 CFTypeRef data = NULL;
757 CFStringRef identifier = NULL ;
758
759 GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL,
760 sizeof( CFStringRef ), NULL, &identifier );
761
762 GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL,
763 sizeof( CFTypeRef ), NULL, &data );
764
765 if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo )
766 {
767 item = CreateControlToolbarItem( kControlToolbarItemClassID, data );
768 if ( item )
769 {
770 SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef,
771 sizeof( HIToolbarItemRef ), &item );
772 result = noErr;
773 }
774 }
775
776 }
777 break;
778 }
779 return result ;
780}
781
e56d2520
SC
782// also for the toolbar we have the dual implementation:
783// only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
6d4835dc 784
3b6a1179
DS
785bool wxToolBar::Create(
786 wxWindow *parent,
991f71dc
DS
787 wxWindowID id,
788 const wxPoint& pos,
789 const wxSize& size,
790 long style,
3b6a1179 791 const wxString& name )
5d0bf05a 792{
3b6a1179
DS
793 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
794 return false;
e56d2520 795
991f71dc 796 OSStatus err = noErr;
e56d2520
SC
797
798#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179
DS
799 wxString labelStr = wxString::Format( wxT("%xd"), (int)this );
800 err = HIToolbarCreate(
801 wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0,
802 (HIToolbarRef*) &m_macHIToolbarRef );
e56d2520
SC
803
804 if (m_macHIToolbarRef != NULL)
f3a65c3e 805 {
6d4835dc
SC
806 InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler,
807 GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL );
808
3b6a1179
DS
809 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
810 HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall;
e56d2520
SC
811
812 if ( style & wxTB_NOICONS )
3b6a1179 813 mode = kHIToolbarDisplayModeLabelOnly;
e56d2520 814 else if ( style & wxTB_TEXT )
3b6a1179 815 mode = kHIToolbarDisplayModeIconAndLabel;
e56d2520 816 else
3b6a1179 817 mode = kHIToolbarDisplayModeIconOnly;
e56d2520 818
3b6a1179
DS
819 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
820 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize );
e56d2520
SC
821 }
822#endif
823
991f71dc 824 return (err == noErr);
e9576ca5
SC
825}
826
827wxToolBar::~wxToolBar()
f3a65c3e 828{
e56d2520 829#if wxMAC_USE_NATIVE_TOOLBAR
3025abca 830 if (m_macHIToolbarRef != NULL)
e56d2520
SC
831 {
832 // if this is the installed toolbar, then deinstall it
833 if (m_macUsesNativeToolbar)
834 MacInstallNativeToolbar( false );
835
3025abca 836 CFRelease( (HIToolbarRef)m_macHIToolbarRef );
e56d2520
SC
837 m_macHIToolbarRef = NULL;
838 }
839#endif
840}
841
e56d2520
SC
842bool wxToolBar::Show( bool show )
843{
e56d2520 844 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
3025abca 845 bool bResult = (tlw != NULL);
f3a65c3e 846
e56d2520
SC
847 if (bResult)
848 {
849#if wxMAC_USE_NATIVE_TOOLBAR
f3a65c3e 850 bool ownToolbarInstalled = false;
e56d2520
SC
851 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
852 if (ownToolbarInstalled)
853 {
3025abca 854 bResult = (IsWindowToolbarVisible( tlw ) != show);
a749a99f
SC
855 if ( bResult )
856 ShowHideWindowToolbar( tlw, show, false );
e56d2520
SC
857 }
858 else
e56d2520 859 bResult = wxToolBarBase::Show( show );
3025abca
DS
860#else
861
862 bResult = wxToolBarBase::Show( show );
863#endif
e56d2520 864 }
f3a65c3e 865
e56d2520
SC
866 return bResult;
867}
868
869bool wxToolBar::IsShown() const
870{
871 bool bResult;
872
873#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 874 bool ownToolbarInstalled;
3025abca 875
e56d2520
SC
876 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
877 if (ownToolbarInstalled)
a749a99f
SC
878 {
879 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
3b6a1179 880 bResult = IsWindowToolbarVisible( tlw );
a749a99f 881 }
e56d2520 882 else
e56d2520 883 bResult = wxToolBarBase::IsShown();
3025abca
DS
884#else
885
886 bResult = wxToolBarBase::IsShown();
887#endif
f3a65c3e 888
e56d2520
SC
889 return bResult;
890}
891
e56d2520
SC
892void wxToolBar::DoGetSize( int *width, int *height ) const
893{
894#if wxMAC_USE_NATIVE_TOOLBAR
895 Rect boundsR;
896 bool ownToolbarInstalled;
f3a65c3e 897
e56d2520
SC
898 MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
899 if ( ownToolbarInstalled )
900 {
991f71dc 901 // TODO: is this really a control ?
e56d2520
SC
902 GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR );
903 if ( width != NULL )
904 *width = boundsR.right - boundsR.left;
905 if ( height != NULL )
906 *height = boundsR.bottom - boundsR.top;
907 }
908 else
e56d2520 909 wxToolBarBase::DoGetSize( width, height );
3025abca
DS
910
911#else
912 wxToolBarBase::DoGetSize( width, height );
913#endif
e9576ca5
SC
914}
915
b13095df
SC
916wxSize wxToolBar::DoGetBestSize() const
917{
3b6a1179 918 int width, height;
991f71dc 919
3b6a1179 920 DoGetSize( &width, &height );
991f71dc 921
3b6a1179 922 return wxSize( width, height );
b13095df
SC
923}
924
f3a65c3e 925void wxToolBar::SetWindowStyleFlag( long style )
e56d2520
SC
926{
927 wxToolBarBase::SetWindowStyleFlag( style );
991f71dc 928
e56d2520
SC
929#if wxMAC_USE_NATIVE_TOOLBAR
930 if (m_macHIToolbarRef != NULL)
931 {
932 HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault;
933
934 if ( style & wxTB_NOICONS )
935 mode = kHIToolbarDisplayModeLabelOnly;
936 else if ( style & wxTB_TEXT )
937 mode = kHIToolbarDisplayModeIconAndLabel;
938 else
939 mode = kHIToolbarDisplayModeIconOnly;
f3a65c3e 940
e56d2520
SC
941 HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode );
942 }
943#endif
944}
945
946#if wxMAC_USE_NATIVE_TOOLBAR
947bool wxToolBar::MacWantsNativeToolbar()
948{
949 return m_macUsesNativeToolbar;
950}
951
952bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const
953{
954 bool bResultV = false;
955
956 if (ownToolbarInstalled != NULL)
957 *ownToolbarInstalled = false;
958
959 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
960 if (tlw != NULL)
961 {
962 HIToolbarRef curToolbarRef = NULL;
963 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
3025abca 964 bResultV = ((err == noErr) && (curToolbarRef != NULL));
e56d2520
SC
965 if (bResultV && (ownToolbarInstalled != NULL))
966 *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef);
967 }
968
969 return bResultV;
970}
971
f3a65c3e 972bool wxToolBar::MacInstallNativeToolbar(bool usesNative)
e56d2520 973{
df7998fc 974 bool bResult = false;
e56d2520 975
e56d2520
SC
976 if (usesNative && (m_macHIToolbarRef == NULL))
977 return bResult;
f3a65c3e 978
e56d2520
SC
979 if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0))
980 return bResult;
f3a65c3e 981
3025abca
DS
982 WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef());
983 if (tlw == NULL)
984 return bResult;
985
e56d2520
SC
986 // check the existing toolbar
987 HIToolbarRef curToolbarRef = NULL;
988 OSStatus err = GetWindowToolbar( tlw, &curToolbarRef );
991f71dc 989 if (err != noErr)
e56d2520
SC
990 curToolbarRef = NULL;
991
992 m_macUsesNativeToolbar = usesNative;
993
994 if (m_macUsesNativeToolbar)
995 {
996 // only install toolbar if there isn't one installed already
997 if (curToolbarRef == NULL)
998 {
999 bResult = true;
1000
1001 SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef );
1002 ShowHideWindowToolbar( tlw, true, false );
1003 ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 );
1004 SetAutomaticControlDragTrackingEnabledForWindow( tlw, true );
f3a65c3e 1005
3b6a1179 1006 Rect r = { 0, 0, 0, 0 };
e56d2520 1007 m_peer->SetRect( &r );
e56d2520 1008 SetSize( wxSIZE_AUTO_WIDTH, 0 );
e56d2520
SC
1009 m_peer->SetVisibility( false, true );
1010 wxToolBarBase::Show( false );
1011 }
1012 }
1013 else
1014 {
1015 // only deinstall toolbar if this is the installed one
1016 if (m_macHIToolbarRef == curToolbarRef)
1017 {
1018 bResult = true;
1019
1020 ShowHideWindowToolbar( tlw, false, false );
3b6a1179 1021 ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute );
e56d2520 1022 SetWindowToolbar( tlw, NULL );
f3a65c3e 1023
e56d2520 1024 m_peer->SetVisibility( true, true );
e56d2520
SC
1025 }
1026 }
1027
1028 if (bResult)
1029 InvalidateBestSize();
1030
1031// wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") );
1032 return bResult;
1033}
1034#endif
1035
37e2cb08 1036bool wxToolBar::Realize()
e9576ca5 1037{
eb22f2a6 1038 if (m_tools.GetCount() == 0)
3803c372 1039 return false;
0b7a8cd3 1040
e56d2520
SC
1041 int maxWidth = 0;
1042 int maxHeight = 0;
f3a65c3e 1043
bfe9ffbc
SC
1044 int maxToolWidth = 0;
1045 int maxToolHeight = 0;
f3a65c3e 1046
991f71dc
DS
1047 int x = m_xMargin + kwxMacToolBarLeftMargin;
1048 int y = m_yMargin + kwxMacToolBarTopMargin;
1049
1050 int tw, th;
1051 GetSize( &tw, &th );
1052
e56d2520 1053 // find the maximum tool width and height
3025abca
DS
1054 wxToolBarTool *tool;
1055 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
e56d2520 1056 while ( node != NULL )
bfe9ffbc 1057 {
3025abca 1058 tool = (wxToolBarTool *) node->GetData();
e56d2520
SC
1059 if ( tool != NULL )
1060 {
1061 wxSize sz = tool->GetSize();
f3a65c3e 1062
e56d2520
SC
1063 if ( sz.x > maxToolWidth )
1064 maxToolWidth = sz.x;
1065 if ( sz.y > maxToolHeight )
1066 maxToolHeight = sz.y;
1067 }
f3a65c3e 1068
bfe9ffbc
SC
1069 node = node->GetNext();
1070 }
f3a65c3e 1071
991f71dc
DS
1072 bool lastIsRadio = false;
1073 bool curIsRadio = false;
1074 bool setChoiceInGroup = 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 );
f3a65c3e 1169
e56d2520 1170 setChoiceInGroup = false;
0b7a8cd3
GD
1171 }
1172 else
1173 {
e56d2520
SC
1174 if ( !lastIsRadio )
1175 {
dcae64c2 1176 if ( tool->Toggle( true ) )
e56d2520
SC
1177 {
1178 DoToggleTool( tool, true );
1179 setChoiceInGroup = true;
1180 }
1181 }
1182 else if ( tool->IsToggled() )
1183 {
1184 if ( tool->IsToggled() )
1185 DoToggleTool( tool, true );
f3a65c3e 1186
e56d2520
SC
1187 wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious();
1188 while ( nodePrev != NULL )
1189 {
1190 wxToolBarToolBase *toggleTool = nodePrev->GetData();
1191 if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) )
1192 break;
f3a65c3e 1193
dcae64c2 1194 if ( toggleTool->Toggle( false ) )
e56d2520 1195 DoToggleTool( toggleTool, false );
f3a65c3e 1196
e56d2520
SC
1197 nodePrev = nodePrev->GetPrevious();
1198 }
1199 }
0b7a8cd3 1200 }
f3a65c3e 1201
eb22f2a6 1202 node = node->GetNext();
7810c95b 1203 }
f3a65c3e 1204
0b7a8cd3 1205 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
7810c95b 1206 {
e56d2520
SC
1207 // if not set yet, only one row
1208 if ( m_maxRows <= 0 )
1209 SetRows( 1 );
f3a65c3e 1210
90d3f91a 1211 m_minWidth = maxWidth;
e56d2520 1212 maxWidth = tw;
0b7a8cd3 1213 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
e56d2520 1214 m_minHeight = m_maxHeight = maxHeight;
7810c95b 1215 }
0b7a8cd3
GD
1216 else
1217 {
e56d2520
SC
1218 // if not set yet, have one column
1219 if ( (GetToolsCount() > 0) && (m_maxRows <= 0) )
1220 SetRows( GetToolsCount() );
f3a65c3e 1221
90d3f91a 1222 m_minHeight = maxHeight;
e56d2520 1223 maxHeight = th;
0b7a8cd3 1224 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
e56d2520 1225 m_minWidth = m_maxWidth = maxWidth;
0b7a8cd3 1226 }
e56d2520 1227
f3a65c3e 1228#if 0
e56d2520
SC
1229 // FIXME: should this be OSX-only?
1230 {
1231 bool wantNativeToolbar, ownToolbarInstalled;
1232
1233 // attempt to install the native toolbar
1234 wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0);
1235 MacInstallNativeToolbar( wantNativeToolbar );
1236 (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled );
1237 if (!ownToolbarInstalled)
1238 {
1239 SetSize( maxWidth, maxHeight );
1240 InvalidateBestSize();
1241 }
1242 }
f3a65c3e 1243#else
facd6764 1244 SetSize( maxWidth, maxHeight );
9f884528 1245 InvalidateBestSize();
e56d2520 1246#endif
2c1dbc95
SC
1247
1248 SetBestFittingSize();
1249
3803c372 1250 return true;
e9576ca5
SC
1251}
1252
1253void wxToolBar::SetToolBitmapSize(const wxSize& size)
1254{
e56d2520
SC
1255 m_defaultWidth = size.x + kwxMacToolBorder;
1256 m_defaultHeight = size.y + kwxMacToolBorder;
f3a65c3e 1257
e56d2520
SC
1258#if wxMAC_USE_NATIVE_TOOLBAR
1259 if (m_macHIToolbarRef != NULL)
1260 {
1261 int maxs = wxMax( size.x, size.y );
3b6a1179 1262 HIToolbarDisplaySize sizeSpec;
328f4fee 1263 if ( maxs > 32 )
3b6a1179 1264 sizeSpec = kHIToolbarDisplaySizeNormal;
00a7bd85 1265 else if ( maxs > 24 )
3b6a1179 1266 sizeSpec = kHIToolbarDisplaySizeDefault;
328f4fee 1267 else
3b6a1179 1268 sizeSpec = kHIToolbarDisplaySizeSmall;
f3a65c3e 1269
e56d2520
SC
1270 HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec );
1271 }
1272#endif
e9576ca5
SC
1273}
1274
e9576ca5
SC
1275// The button size is bigger than the bitmap size
1276wxSize wxToolBar::GetToolSize() const
1277{
ee799df7 1278 return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder);
e9576ca5
SC
1279}
1280
37e2cb08 1281void wxToolBar::SetRows(int nRows)
e9576ca5 1282{
e56d2520
SC
1283 // avoid resizing the frame uselessly
1284 if ( nRows != m_maxRows )
e56d2520 1285 m_maxRows = nRows;
e9576ca5
SC
1286}
1287
f3a65c3e 1288void wxToolBar::MacSuperChangedPosition()
c257d44d 1289{
e56d2520 1290 wxWindow::MacSuperChangedPosition();
991f71dc 1291
e56d2520
SC
1292#if wxMAC_USE_NATIVE_TOOLBAR
1293 if (! m_macUsesNativeToolbar )
e56d2520 1294 Realize();
991f71dc 1295#else
3025abca 1296
991f71dc
DS
1297 Realize();
1298#endif
c257d44d
SC
1299}
1300
37e2cb08 1301wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
e9576ca5 1302{
3025abca 1303 wxToolBarTool *tool;
affd2611 1304 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
e56d2520 1305 while ( node != NULL )
e044f600 1306 {
3025abca 1307 tool = (wxToolBarTool *)node->GetData();
e56d2520 1308 if (tool != NULL)
e044f600 1309 {
e56d2520
SC
1310 wxRect2DInt r( tool->GetPosition(), tool->GetSize() );
1311 if ( r.Contains( wxPoint( x, y ) ) )
1312 return tool;
e044f600 1313 }
bfe9ffbc
SC
1314
1315 node = node->GetNext();
e044f600 1316 }
37e2cb08 1317
3025abca 1318 return (wxToolBarToolBase*)NULL;
e9576ca5
SC
1319}
1320
2f1ae414
SC
1321wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
1322{
3b6a1179 1323 wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y );
e56d2520 1324 if ( tool != NULL )
3b6a1179 1325 return tool->GetShortHelp();
e56d2520 1326
3b6a1179 1327 return wxEmptyString;
2f1ae414
SC
1328}
1329
37e2cb08 1330void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
e9576ca5 1331{
e56d2520 1332 if ( t != NULL )
3b6a1179 1333 ((wxToolBarTool*)t)->DoEnable( enable );
e9576ca5
SC
1334}
1335
37e2cb08 1336void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
e9576ca5 1337{
e044f600 1338 wxToolBarTool *tool = (wxToolBarTool *)t;
e56d2520
SC
1339 if ( ( tool != NULL ) && tool->IsButton() )
1340 tool->UpdateToggleImage( toggle );
37e2cb08 1341}
7c551d95 1342
3b6a1179 1343bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase)
37e2cb08 1344{
3b6a1179 1345 wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase );
e56d2520
SC
1346 if (tool == NULL)
1347 return false;
1348
1349 WindowRef window = (WindowRef) MacGetTopLevelWindowRef();
1350 wxSize toolSize = GetToolSize();
3b6a1179 1351 Rect toolrect = { 0, 0, toolSize.y, toolSize.x };
e56d2520
SC
1352 ControlRef controlHandle = NULL;
1353 OSStatus err = 0;
be5fe3aa 1354
e56d2520 1355 switch (tool->GetStyle())
be5fe3aa 1356 {
3b6a1179 1357 case wxTOOL_STYLE_SEPARATOR:
be5fe3aa 1358 {
e56d2520
SC
1359 wxASSERT( tool->GetControlHandle() == NULL );
1360 toolSize.x /= 4;
1361 toolSize.y /= 4;
be5fe3aa 1362 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
e56d2520 1363 toolrect.bottom = toolSize.y;
be5fe3aa 1364 else
e56d2520
SC
1365 toolrect.right = toolSize.x;
1366
991f71dc 1367#ifdef __WXMAC_OSX__
be5fe3aa 1368 // in flat style we need a visual separator
991f71dc 1369#if wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1370 HIToolbarItemRef item;
991f71dc
DS
1371 err = HIToolbarItemCreate(
1372 kHIToolbarSeparatorIdentifier,
1373 kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates,
1374 &item );
e56d2520
SC
1375 if (err == noErr)
1376 tool->SetToolbarItemRef( item );
991f71dc
DS
1377#endif
1378
e56d2520
SC
1379 CreateSeparatorControl( window, &toolrect, &controlHandle );
1380 tool->SetControlHandle( controlHandle );
991f71dc 1381#endif
be5fe3aa 1382 }
e56d2520
SC
1383 break;
1384
3b6a1179 1385 case wxTOOL_STYLE_BUTTON:
be5fe3aa 1386 {
3b6a1179
DS
1387 wxASSERT( tool->GetControlHandle() == NULL );
1388 ControlButtonContentInfo info;
1389 wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef );
f3a65c3e 1390
df7998fc 1391 if ( UMAGetSystemVersion() >= 0x1000)
3b6a1179
DS
1392 {
1393 CreateIconControl( window, &toolrect, &info, false, &controlHandle );
1394 }
df7998fc
VZ
1395 else
1396 {
3b6a1179 1397 SInt16 behaviour = kControlBehaviorOffsetContents;
df7998fc 1398 if ( tool->CanBeToggled() )
3b6a1179
DS
1399 behaviour |= kControlBehaviorToggles;
1400 err = CreateBevelButtonControl( window,
1401 &toolrect, CFSTR(""), kControlBevelButtonNormalBevel,
1402 behaviour, &info, 0, 0, 0, &controlHandle );
df7998fc 1403 }
e56d2520
SC
1404
1405#if wxMAC_USE_NATIVE_TOOLBAR
3b6a1179 1406 HIToolbarItemRef item;
30962327 1407 wxString labelStr = wxString::Format(wxT("%xd"), (int)tool);
e56d2520
SC
1408 err = HIToolbarItemCreate(
1409 wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()),
1410 kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item );
1411 if (err == noErr)
1412 {
3025abca
DS
1413 InstallEventHandler(
1414 HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(),
1415 GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL );
e56d2520
SC
1416 HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) );
1417 HIToolbarItemSetIconRef( item, info.u.iconRef );
2c1dbc95 1418 HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction );
e56d2520
SC
1419 tool->SetToolbarItemRef( item );
1420 }
991f71dc 1421#endif
e56d2520 1422
3b6a1179 1423 wxMacReleaseBitmapButton( &info );
3025abca 1424
991f71dc 1425#if 0
3b6a1179
DS
1426 SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic );
1427 UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() );
991f71dc 1428#endif
f3a65c3e 1429
3b6a1179
DS
1430 InstallControlEventHandler(
1431 (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
e56d2520 1432 GetEventTypeCount(eventList), eventList, tool, NULL );
be5fe3aa 1433
e56d2520 1434 tool->SetControlHandle( controlHandle );
be5fe3aa 1435 }
e56d2520
SC
1436 break;
1437
3b6a1179 1438 case wxTOOL_STYLE_CONTROL:
3b6a1179 1439
6d4835dc 1440#if wxMAC_USE_NATIVE_TOOLBAR
e56d2520 1441 {
6d4835dc 1442 wxASSERT( tool->GetControl() != NULL );
e56d2520 1443 HIToolbarItemRef item;
6d4835dc
SC
1444 HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ;
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
3b6a1179 1653 QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext );
20b69855 1654#endif
01ffa8f7 1655 }
01ffa8f7
SC
1656 }
1657#endif
20b69855 1658
dcae64c2 1659 event.Skip();
2f1ae414 1660}
895f5af7 1661
519cb848 1662#endif // wxUSE_TOOLBAR