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