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