]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
metal appearance supported under 10.3
[wxWidgets.git] / src / mac / carbon / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: toolbar.cpp
3 // Purpose: wxToolBar
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "toolbar.h"
14 #endif
15
16 #include "wx/wx.h"
17
18 #if wxUSE_TOOLBAR
19
20 #include "wx/toolbar.h"
21 #include "wx/notebook.h"
22 #include "wx/tabctrl.h"
23 #include "wx/bitmap.h"
24
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
27
28 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
29 EVT_PAINT( wxToolBar::OnPaint )
30 END_EVENT_TABLE()
31 #endif
32
33 #include "wx/mac/uma.h"
34 #include "wx/geometry.h"
35 // ----------------------------------------------------------------------------
36 // private classes
37 // ----------------------------------------------------------------------------
38
39 class wxToolBarTool : public wxToolBarToolBase
40 {
41 public:
42 wxToolBarTool(wxToolBar *tbar,
43 int id,
44 const wxString& label,
45 const wxBitmap& bmpNormal,
46 const wxBitmap& bmpDisabled,
47 wxItemKind kind,
48 wxObject *clientData,
49 const wxString& shortHelp,
50 const wxString& longHelp) ;
51
52 wxToolBarTool(wxToolBar *tbar, wxControl *control)
53 : wxToolBarToolBase(tbar, control)
54 {
55 Init() ;
56 }
57
58 ~wxToolBarTool()
59 {
60 if ( m_controlHandle )
61 DisposeControl( m_controlHandle ) ;
62 }
63
64 WXWidget GetControlHandle() { return (WXWidget) m_controlHandle ; }
65 void SetControlHandle( ControlRef handle ) { m_controlHandle = handle ; }
66
67 void SetSize(const wxSize& size) ;
68 void SetPosition( const wxPoint& position ) ;
69
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 }
92 bool DoEnable( bool enable ) ;
93 private :
94 void Init()
95 {
96 m_controlHandle = NULL ;
97 }
98 ControlRef m_controlHandle ;
99
100 wxCoord m_x;
101 wxCoord m_y;
102 };
103
104 static const EventTypeSpec eventList[] =
105 {
106 { kEventClassControl , kEventControlHit } ,
107 } ;
108
109 static 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 {
126 tbartool->Toggle( GetControl32BitValue( (ControlRef) tbartool->GetControlHandle() ) ) ;
127 }
128 ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ;
129
130 result = noErr;
131 }
132 break ;
133 default :
134 break ;
135 }
136 return result ;
137 }
138
139 pascal 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
154 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
155
156 // ============================================================================
157 // implementation
158 // ============================================================================
159
160 // ----------------------------------------------------------------------------
161 // wxToolBarTool
162 // ----------------------------------------------------------------------------
163
164 bool 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 }
186 void wxToolBarTool::SetSize(const wxSize& size)
187 {
188 if ( IsControl() )
189 {
190 GetControl()->SetSize( size ) ;
191 }
192 }
193
194 void 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 ;
203 int mac_x = position.x ;
204 int mac_y = position.y ;
205 #if !TARGET_API_MAC_OSX
206 WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetTopLevelWindowRef() ;
207 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
208 mac_x += x;
209 mac_y += y;
210 #endif
211 Rect contrlRect ;
212 GetControlBounds( m_controlHandle , &contrlRect ) ;
213 int former_mac_x = contrlRect.left ;
214 int former_mac_y = contrlRect.top ;
215 wxSize sz = GetToolBar()->GetToolSize() ;
216
217 if ( mac_x != former_mac_x || mac_y != former_mac_y )
218 {
219 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
220 }
221 }
222 else if ( IsControl() )
223 {
224 GetControl()->Move( position ) ;
225 }
226 }
227
228 const short kwxMacToolBarToolDefaultWidth = 24 ;
229 const short kwxMacToolBarToolDefaultHeight = 22 ;
230 const short kwxMacToolBarTopMargin = 2 ;
231 const short kwxMacToolBarLeftMargin = 2 ;
232
233 wxToolBarTool::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 {
245 Init();
246
247 if (id == wxID_SEPARATOR) return;
248
249 WindowRef window = (WindowRef) tbar->MacGetTopLevelWindowRef() ;
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 ;
259
260 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
261 0 , 0 , 0 , &m_controlHandle ) ;
262
263 InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
264 GetEventTypeCount(eventList), eventList, this,NULL);
265
266 UMAShowControl( m_controlHandle ) ;
267
268 if ( CanBeToggled() && IsToggled() )
269 ::SetControl32BitValue( m_controlHandle , 1 ) ;
270 else
271 ::SetControl32BitValue( m_controlHandle , 0 ) ;
272
273 ControlRef container = (ControlRef) tbar->GetHandle() ;
274 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
275 ::EmbedControl( m_controlHandle , container ) ;
276 }
277
278
279 wxToolBarToolBase *wxToolBar::CreateTool(int id,
280 const wxString& label,
281 const wxBitmap& bmpNormal,
282 const wxBitmap& bmpDisabled,
283 wxItemKind kind,
284 wxObject *clientData,
285 const wxString& shortHelp,
286 const wxString& longHelp)
287 {
288 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
289 clientData, shortHelp, longHelp);
290 }
291
292 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
293 {
294 return new wxToolBarTool(this, control);
295 }
296
297 void wxToolBar::Init()
298 {
299 m_maxWidth = -1;
300 m_maxHeight = -1;
301 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
302 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
303 }
304
305 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
306 long style, const wxString& name)
307 {
308 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
309 return FALSE ;
310
311 return TRUE;
312 }
313
314 wxToolBar::~wxToolBar()
315 {
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
318 }
319
320 bool wxToolBar::Realize()
321 {
322 if (m_tools.GetCount() == 0)
323 return FALSE;
324
325 int x = m_xMargin + kwxMacToolBarLeftMargin ;
326 int y = m_yMargin + kwxMacToolBarTopMargin ;
327
328 int tw, th;
329 GetSize(& tw, & th);
330
331 int maxWidth = 0 ;
332 int maxHeight = 0 ;
333
334 int maxToolWidth = 0;
335 int maxToolHeight = 0;
336
337 // Find the maximum tool width and height
338 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
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
352 node = m_tools.GetFirst();
353 while (node)
354 {
355 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
356 wxSize cursize = tool->GetSize() ;
357
358 // for the moment we just do a single row/column alignement
359 if ( x + cursize.x > maxWidth )
360 maxWidth = x + cursize.x ;
361 if ( y + cursize.y > maxHeight )
362 maxHeight = y + cursize.y ;
363
364 tool->SetPosition( wxPoint( x , y ) ) ;
365
366 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
367 {
368 y += cursize.y ;
369 }
370 else
371 {
372 x += cursize.x ;
373 }
374
375 node = node->GetNext();
376 }
377
378 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
379 {
380 if ( m_maxRows == 0 )
381 {
382 // if not set yet, only one row
383 SetRows(1);
384 }
385 maxWidth = tw ;
386 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
387 m_maxHeight = maxHeight ;
388 }
389 else
390 {
391 if ( GetToolsCount() > 0 && m_maxRows == 0 )
392 {
393 // if not set yet, have one column
394 SetRows(GetToolsCount());
395 }
396 maxHeight = th ;
397 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
398 m_maxWidth = maxWidth ;
399 }
400
401 SetSize( maxWidth, maxHeight );
402 InvalidateBestSize();
403
404 return TRUE;
405 }
406
407 void wxToolBar::SetToolBitmapSize(const wxSize& size)
408 {
409 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
410 }
411
412 // The button size is bigger than the bitmap size
413 wxSize wxToolBar::GetToolSize() const
414 {
415 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
416 }
417
418 void wxToolBar::SetRows(int nRows)
419 {
420 if ( nRows == m_maxRows )
421 {
422 // avoid resizing the frame uselessly
423 return;
424 }
425
426 m_maxRows = nRows;
427 }
428
429 void wxToolBar::MacSuperChangedPosition()
430 {
431 wxWindow::MacSuperChangedPosition() ;
432 Realize() ;
433 }
434
435 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
436 {
437 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
438 while (node)
439 {
440 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
441 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
442 if ( r.Contains( wxPoint( x , y ) ) )
443 {
444 return tool;
445 }
446
447 node = node->GetNext();
448 }
449
450 return (wxToolBarToolBase *)NULL;
451 }
452
453 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
454 {
455 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
456 if ( tool )
457 {
458 return tool->GetShortHelp() ;
459 }
460 return wxEmptyString ;
461 }
462
463 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
464 {
465 if (!IsShown())
466 return ;
467
468 ((wxToolBarTool*)t)->DoEnable( enable ) ;
469 }
470
471 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
472 {
473 if (!IsShown())
474 return ;
475
476 wxToolBarTool *tool = (wxToolBarTool *)t;
477 if ( tool->IsButton() )
478 {
479 ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ;
480 }
481 }
482
483 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
484 wxToolBarToolBase *tool)
485 {
486 // nothing special to do here - we relayout in Realize() later
487 tool->Attach(this);
488 InvalidateBestSize();
489
490 return TRUE;
491 }
492
493 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
494 {
495 wxFAIL_MSG( _T("not implemented") );
496 }
497
498 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
499 {
500 wxToolBarToolsList::compatibility_iterator node;
501 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
502 {
503 wxToolBarToolBase *tool2 = node->GetData();
504 if ( tool2 == tool )
505 {
506 // let node point to the next node in the list
507 node = node->GetNext();
508
509 break;
510 }
511 }
512
513 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
514
515 tool->Detach();
516
517 // and finally reposition all the controls after this one
518
519 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
520 {
521 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
522 wxPoint pt = tool2->GetPosition() ;
523
524 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
525 {
526 pt.y -= sz.y ;
527 }
528 else
529 {
530 pt.x -= sz.x ;
531 }
532 tool2->SetPosition( pt ) ;
533 }
534
535 InvalidateBestSize();
536 return TRUE ;
537 }
538
539 void wxToolBar::OnPaint(wxPaintEvent& event)
540 {
541 wxPaintDC dc(this) ;
542 wxMacPortSetter helper(&dc) ;
543 int w, h ;
544 GetSize( &w , &h ) ;
545
546 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
547 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
548 /*
549 if( toolbarrect.left < 0 )
550 toolbarrect.left = 0 ;
551 if ( toolbarrect.top < 0 )
552 toolbarrect.top = 0 ;
553 */
554 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
555 {
556 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
557 }
558 else
559 {
560 #if TARGET_API_MAC_OSX
561 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
562 if ( UMAGetSystemVersion() >= 0x1030 )
563 {
564 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
565 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
566 CGContextRef cgContext ;
567 Rect bounds ;
568 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
569 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
570 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
571 CGContextScaleCTM( cgContext , 1 , -1 ) ;
572
573 {
574 HIThemeBackgroundDrawInfo drawInfo ;
575 drawInfo.version = 0 ;
576 drawInfo.state = kThemeStateActive ;
577 drawInfo.kind = kThemeBackgroundMetal ;
578 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
579 }
580 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
581 }
582 else
583 #endif
584 {
585 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
586 }
587 #endif
588 }
589 event.Skip() ;
590 }
591
592 #endif // wxUSE_TOOLBAR
593