]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/toolbar.cpp
ATSU Support in font extended
[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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "toolbar.h"
14#endif
15
3d1a4878 16#include "wx/wxprec.h"
519cb848
SC
17
18#if wxUSE_TOOLBAR
19
3d1a4878 20#include "wx/wx.h"
e9576ca5 21#include "wx/toolbar.h"
2f1ae414
SC
22#include "wx/notebook.h"
23#include "wx/tabctrl.h"
72055702 24#include "wx/bitmap.h"
e9576ca5 25
2f1ae414 26#if !USE_SHARED_LIBRARY
2eb10e2a 27IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
e9576ca5
SC
28
29BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
e044f600 30 EVT_PAINT( wxToolBar::OnPaint )
e9576ca5 31END_EVENT_TABLE()
2f1ae414 32#endif
e9576ca5 33
d497dca4 34#include "wx/mac/uma.h"
bfe9ffbc 35#include "wx/geometry.h"
37e2cb08
SC
36// ----------------------------------------------------------------------------
37// private classes
38// ----------------------------------------------------------------------------
39
40class wxToolBarTool : public wxToolBarToolBase
41{
42public:
43 wxToolBarTool(wxToolBar *tbar,
44 int id,
27e2d606
GD
45 const wxString& label,
46 const wxBitmap& bmpNormal,
47 const wxBitmap& bmpDisabled,
48 wxItemKind kind,
37e2cb08 49 wxObject *clientData,
27e2d606 50 const wxString& shortHelp,
bfe9ffbc
SC
51 const wxString& longHelp) ;
52
37e2cb08
SC
53 wxToolBarTool(wxToolBar *tbar, wxControl *control)
54 : wxToolBarToolBase(tbar, control)
55 {
bfe9ffbc 56 Init() ;
37e2cb08 57 }
bfe9ffbc
SC
58
59 ~wxToolBarTool()
60 {
61 if ( m_controlHandle )
62 DisposeControl( m_controlHandle ) ;
63 }
64
facd6764
SC
65 WXWidget GetControlHandle() { return (WXWidget) m_controlHandle ; }
66 void SetControlHandle( ControlRef handle ) { m_controlHandle = handle ; }
37e2cb08 67
bfe9ffbc
SC
68 void SetSize(const wxSize& size) ;
69 void SetPosition( const wxPoint& position ) ;
a2fe01c9 70
bfe9ffbc
SC
71 wxSize GetSize() const
72 {
73 if ( IsControl() )
74 {
75 return GetControl()->GetSize() ;
76 }
77 else if ( IsButton() )
78 {
79 return GetToolBar()->GetToolSize() ;
80 }
81 else
82 {
83 wxSize sz = GetToolBar()->GetToolSize() ;
84 sz.x /= 4 ;
85 sz.y /= 4 ;
86 return sz ;
87 }
88 }
89 wxPoint GetPosition() const
90 {
91 return wxPoint(m_x, m_y);
92 }
a2fe01c9 93 bool DoEnable( bool enable ) ;
bfe9ffbc
SC
94private :
95 void Init()
96 {
97 m_controlHandle = NULL ;
98 }
facd6764 99 ControlRef m_controlHandle ;
37e2cb08 100
bfe9ffbc
SC
101 wxCoord m_x;
102 wxCoord m_y;
37e2cb08
SC
103};
104
facd6764
SC
105static const EventTypeSpec eventList[] =
106{
107 { kEventClassControl , kEventControlHit } ,
108} ;
109
110static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
111{
112 OSStatus result = eventNotHandledErr ;
113
114 wxMacCarbonEvent cEvent( event ) ;
115
116 ControlRef controlRef ;
117
118 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
119
120 switch( GetEventKind( event ) )
121 {
122 case kEventControlHit :
123 {
124 wxToolBarTool* tbartool = (wxToolBarTool*)data ;
125 if ( tbartool->CanBeToggled() )
126 {
214b9484 127 ((wxToolBar*)tbartool->GetToolBar())->ToggleTool(tbartool->GetId(), GetControl32BitValue((ControlRef)tbartool->GetControlHandle()));
facd6764
SC
128 }
129 ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ;
facd6764
SC
130 result = noErr;
131 }
132 break ;
133 default :
134 break ;
135 }
136 return result ;
137}
138
139pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
140{
141 OSStatus result = eventNotHandledErr ;
142
143 switch ( GetEventClass( event ) )
144 {
145 case kEventClassControl :
146 result = wxMacToolBarToolControlEventHandler( handler, event, data ) ;
147 break ;
148 default :
149 break ;
150 }
151 return result ;
152}
153
154DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
155
37e2cb08
SC
156// ============================================================================
157// implementation
158// ============================================================================
159
160// ----------------------------------------------------------------------------
161// wxToolBarTool
162// ----------------------------------------------------------------------------
163
a2fe01c9
SC
164bool wxToolBarTool::DoEnable(bool enable)
165{
166 if ( IsControl() )
167 {
168 GetControl()->Enable( enable ) ;
169 }
170 else if ( IsButton() )
171 {
172#if TARGET_API_MAC_OSX
173 if ( enable )
174 EnableControl( m_controlHandle ) ;
175 else
176 DisableControl( m_controlHandle ) ;
177#else
178 if ( enable )
179 ActivateControl( m_controlHandle ) ;
180 else
181 DeactivateControl( m_controlHandle ) ;
182#endif
183 }
184 return true ;
185}
bfe9ffbc
SC
186void wxToolBarTool::SetSize(const wxSize& size)
187{
188 if ( IsControl() )
189 {
190 GetControl()->SetSize( size ) ;
191 }
192}
193
194void wxToolBarTool::SetPosition(const wxPoint& position)
195{
196 m_x = position.x;
197 m_y = position.y;
198
199 if ( IsButton() )
200 {
201 int x , y ;
202 x = y = 0 ;
facd6764
SC
203 int mac_x = position.x ;
204 int mac_y = position.y ;
205#if !TARGET_API_MAC_OSX
206 WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetTopLevelWindowRef() ;
bfe9ffbc 207 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
facd6764
SC
208 mac_x += x;
209 mac_y += y;
210#endif
bfe9ffbc
SC
211 Rect contrlRect ;
212 GetControlBounds( m_controlHandle , &contrlRect ) ;
213 int former_mac_x = contrlRect.left ;
214 int former_mac_y = contrlRect.top ;
80e3f464 215 GetToolBar()->GetToolSize() ;
bfe9ffbc
SC
216
217 if ( mac_x != former_mac_x || mac_y != former_mac_y )
218 {
bfe9ffbc 219 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
bfe9ffbc
SC
220 }
221 }
222 else if ( IsControl() )
223 {
224 GetControl()->Move( position ) ;
225 }
226}
227
895f5af7
SC
228const short kwxMacToolBarToolDefaultWidth = 24 ;
229const short kwxMacToolBarToolDefaultHeight = 22 ;
230const short kwxMacToolBarTopMargin = 2 ;
231const short kwxMacToolBarLeftMargin = 2 ;
232
bfe9ffbc
SC
233wxToolBarTool::wxToolBarTool(wxToolBar *tbar,
234 int id,
235 const wxString& label,
236 const wxBitmap& bmpNormal,
237 const wxBitmap& bmpDisabled,
238 wxItemKind kind,
239 wxObject *clientData,
240 const wxString& shortHelp,
241 const wxString& longHelp)
242 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
243 clientData, shortHelp, longHelp)
244{
f4e0be4f
RR
245 Init();
246
247 if (id == wxID_SEPARATOR) return;
bfe9ffbc 248
facd6764 249 WindowRef window = (WindowRef) tbar->MacGetTopLevelWindowRef() ;
bfe9ffbc
SC
250 wxSize toolSize = tbar->GetToolSize() ;
251 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x } ;
252
253 ControlButtonContentInfo info ;
254 wxMacCreateBitmapButton( &info , GetNormalBitmap() ) ;
255
256 SInt16 behaviour = kControlBehaviorOffsetContents ;
257 if ( CanBeToggled() )
258 behaviour += kControlBehaviorToggles ;
bfe9ffbc 259
4c37f124
SC
260 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
261 0 , 0 , 0 , &m_controlHandle ) ;
262
facd6764
SC
263 InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
264 GetEventTypeCount(eventList), eventList, this,NULL);
4c37f124 265
bfe9ffbc 266 UMAShowControl( m_controlHandle ) ;
4c37f124 267
bfe9ffbc 268 if ( CanBeToggled() && IsToggled() )
bfe9ffbc 269 ::SetControl32BitValue( m_controlHandle , 1 ) ;
bfe9ffbc 270 else
bfe9ffbc 271 ::SetControl32BitValue( m_controlHandle , 0 ) ;
bfe9ffbc 272
facd6764 273 ControlRef container = (ControlRef) tbar->GetHandle() ;
bfe9ffbc
SC
274 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
275 ::EmbedControl( m_controlHandle , container ) ;
276}
277
2f1ae414 278
37e2cb08 279wxToolBarToolBase *wxToolBar::CreateTool(int id,
27e2d606
GD
280 const wxString& label,
281 const wxBitmap& bmpNormal,
282 const wxBitmap& bmpDisabled,
283 wxItemKind kind,
37e2cb08 284 wxObject *clientData,
27e2d606
GD
285 const wxString& shortHelp,
286 const wxString& longHelp)
37e2cb08 287{
27e2d606
GD
288 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
289 clientData, shortHelp, longHelp);
37e2cb08
SC
290}
291
292wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
293{
294 return new wxToolBarTool(this, control);
295}
296
37e2cb08 297void wxToolBar::Init()
e9576ca5 298{
e40298d5
JS
299 m_maxWidth = -1;
300 m_maxHeight = -1;
301 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
302 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
e9576ca5
SC
303}
304
305bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
306 long style, const wxString& name)
e40298d5 307{
facd6764
SC
308 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
309 return FALSE ;
e40298d5
JS
310
311 return TRUE;
e9576ca5
SC
312}
313
314wxToolBar::~wxToolBar()
bfe9ffbc 315{
7810c95b
SC
316 // we must refresh the frame size when the toolbar is deleted but the frame
317 // is not - otherwise toolbar leaves a hole in the place it used to occupy
e9576ca5
SC
318}
319
37e2cb08 320bool wxToolBar::Realize()
e9576ca5 321{
eb22f2a6 322 if (m_tools.GetCount() == 0)
0b7a8cd3
GD
323 return FALSE;
324
bfe9ffbc
SC
325 int x = m_xMargin + kwxMacToolBarLeftMargin ;
326 int y = m_yMargin + kwxMacToolBarTopMargin ;
0b7a8cd3 327
7c551d95
SC
328 int tw, th;
329 GetSize(& tw, & th);
895f5af7
SC
330
331 int maxWidth = 0 ;
332 int maxHeight = 0 ;
333
bfe9ffbc
SC
334 int maxToolWidth = 0;
335 int maxToolHeight = 0;
336
337 // Find the maximum tool width and height
affd2611 338 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
bfe9ffbc
SC
339 while ( node )
340 {
341 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
342 wxSize sz = tool->GetSize() ;
343
344 if ( sz.x > maxToolWidth )
345 maxToolWidth = sz.x ;
346 if (sz.y> maxToolHeight)
347 maxToolHeight = sz.y;
348
349 node = node->GetNext();
350 }
351
214b9484 352 bool lastWasRadio = FALSE;
bfe9ffbc 353 node = m_tools.GetFirst();
0b7a8cd3 354 while (node)
7810c95b 355 {
eb22f2a6 356 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
bfe9ffbc 357 wxSize cursize = tool->GetSize() ;
0b7a8cd3 358
214b9484
RD
359 bool isRadio = FALSE;
360
361 if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO )
362 {
363 if ( !lastWasRadio )
364 {
365 if (tool->Toggle(true))
366 {
367 DoToggleTool(tool, true);
368 }
369 }
370 isRadio = TRUE;
371 }
372 else
373 {
374 isRadio = FALSE;
375 }
376 lastWasRadio = isRadio;
377
bfe9ffbc
SC
378 // for the moment we just do a single row/column alignement
379 if ( x + cursize.x > maxWidth )
380 maxWidth = x + cursize.x ;
381 if ( y + cursize.y > maxHeight )
382 maxHeight = y + cursize.y ;
0b7a8cd3 383
bfe9ffbc
SC
384 tool->SetPosition( wxPoint( x , y ) ) ;
385
386 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
387 {
388 y += cursize.y ;
0b7a8cd3
GD
389 }
390 else
391 {
bfe9ffbc 392 x += cursize.x ;
0b7a8cd3 393 }
bfe9ffbc 394
eb22f2a6 395 node = node->GetNext();
7810c95b 396 }
0b7a8cd3
GD
397
398 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
7810c95b 399 {
0b7a8cd3
GD
400 if ( m_maxRows == 0 )
401 {
402 // if not set yet, only one row
403 SetRows(1);
404 }
90d3f91a 405 m_minWidth = maxWidth;
0b7a8cd3 406 maxWidth = tw ;
0b7a8cd3 407 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
90d3f91a 408 m_minHeight = m_maxHeight = maxHeight ;
7810c95b 409 }
0b7a8cd3
GD
410 else
411 {
bfe9ffbc 412 if ( GetToolsCount() > 0 && m_maxRows == 0 )
0b7a8cd3
GD
413 {
414 // if not set yet, have one column
bfe9ffbc 415 SetRows(GetToolsCount());
0b7a8cd3 416 }
90d3f91a 417 m_minHeight = maxHeight;
0b7a8cd3 418 maxHeight = th ;
0b7a8cd3 419 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
90d3f91a 420 m_minWidth = m_maxWidth = maxWidth ;
0b7a8cd3
GD
421 }
422
facd6764 423 SetSize( maxWidth, maxHeight );
9f884528 424 InvalidateBestSize();
0b7a8cd3
GD
425
426 return TRUE;
e9576ca5
SC
427}
428
429void wxToolBar::SetToolBitmapSize(const wxSize& size)
430{
9aad97fd 431 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
e9576ca5
SC
432}
433
e9576ca5
SC
434// The button size is bigger than the bitmap size
435wxSize wxToolBar::GetToolSize() const
436{
2f1ae414 437 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
e9576ca5
SC
438}
439
37e2cb08 440void wxToolBar::SetRows(int nRows)
e9576ca5 441{
37e2cb08 442 if ( nRows == m_maxRows )
e9576ca5 443 {
37e2cb08
SC
444 // avoid resizing the frame uselessly
445 return;
e9576ca5 446 }
37e2cb08
SC
447
448 m_maxRows = nRows;
e9576ca5
SC
449}
450
c257d44d
SC
451void wxToolBar::MacSuperChangedPosition()
452{
c257d44d 453 wxWindow::MacSuperChangedPosition() ;
bfe9ffbc 454 Realize() ;
c257d44d
SC
455}
456
37e2cb08 457wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
e9576ca5 458{
affd2611 459 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
bfe9ffbc 460 while (node)
e044f600 461 {
bfe9ffbc
SC
462 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
463 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
464 if ( r.Contains( wxPoint( x , y ) ) )
e044f600 465 {
bfe9ffbc 466 return tool;
e044f600 467 }
bfe9ffbc
SC
468
469 node = node->GetNext();
e044f600 470 }
37e2cb08
SC
471
472 return (wxToolBarToolBase *)NULL;
e9576ca5
SC
473}
474
2f1ae414
SC
475wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
476{
e044f600
RR
477 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
478 if ( tool )
479 {
480 return tool->GetShortHelp() ;
481 }
427ff662 482 return wxEmptyString ;
2f1ae414
SC
483}
484
37e2cb08 485void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
e9576ca5 486{
a2fe01c9 487 ((wxToolBarTool*)t)->DoEnable( enable ) ;
e9576ca5
SC
488}
489
37e2cb08 490void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
e9576ca5 491{
e044f600 492 wxToolBarTool *tool = (wxToolBarTool *)t;
bfe9ffbc
SC
493 if ( tool->IsButton() )
494 {
facd6764 495 ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ;
bfe9ffbc 496 }
37e2cb08 497}
7c551d95 498
37e2cb08
SC
499bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
500 wxToolBarToolBase *tool)
501{
bfe9ffbc 502 // nothing special to do here - we relayout in Realize() later
37e2cb08 503 tool->Attach(this);
9f884528 504 InvalidateBestSize();
7c551d95 505
37e2cb08
SC
506 return TRUE;
507}
e9576ca5 508
5115c51a 509void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
37e2cb08 510{
5115c51a 511 wxFAIL_MSG( _T("not implemented") );
e9576ca5
SC
512}
513
bfe9ffbc 514bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
37e2cb08 515{
affd2611 516 wxToolBarToolsList::compatibility_iterator node;
bfe9ffbc
SC
517 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
518 {
519 wxToolBarToolBase *tool2 = node->GetData();
520 if ( tool2 == tool )
521 {
522 // let node point to the next node in the list
523 node = node->GetNext();
524
525 break;
526 }
527 }
528
529 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
530
531 tool->Detach();
532
533 // and finally reposition all the controls after this one
534
535 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
536 {
537 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
538 wxPoint pt = tool2->GetPosition() ;
539
540 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
541 {
542 pt.y -= sz.y ;
543 }
544 else
545 {
546 pt.x -= sz.x ;
547 }
548 tool2->SetPosition( pt ) ;
549 }
550
9f884528 551 InvalidateBestSize();
5115c51a 552 return TRUE ;
37e2cb08 553}
2f1ae414
SC
554
555void wxToolBar::OnPaint(wxPaintEvent& event)
556{
e40298d5
JS
557 wxPaintDC dc(this) ;
558 wxMacPortSetter helper(&dc) ;
facd6764
SC
559 int w, h ;
560 GetSize( &w , &h ) ;
e40298d5 561
1fd1922a 562 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
facd6764
SC
563 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
564/*
565 if( toolbarrect.left < 0 )
566 toolbarrect.left = 0 ;
567 if ( toolbarrect.top < 0 )
568 toolbarrect.top = 0 ;
569*/
01ffa8f7
SC
570 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
571 {
572 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
573 }
574 else
575 {
576#if TARGET_API_MAC_OSX
577#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
578 if ( UMAGetSystemVersion() >= 0x1030 )
579 {
580 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
581 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
582 CGContextRef cgContext ;
583 Rect bounds ;
584 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
585 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
586 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
587 CGContextScaleCTM( cgContext , 1 , -1 ) ;
785f5eaa 588
01ffa8f7
SC
589 {
590 HIThemeBackgroundDrawInfo drawInfo ;
591 drawInfo.version = 0 ;
592 drawInfo.state = kThemeStateActive ;
593 drawInfo.kind = kThemeBackgroundMetal ;
594 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
595 }
596 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
597 }
598 else
599#endif
600 {
601 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
602 }
603#endif
604 }
facd6764 605 event.Skip() ;
2f1ae414 606}
895f5af7 607
519cb848
SC
608#endif // wxUSE_TOOLBAR
609