]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
cleanup OSX native font dialog stuff a bit (it should prob. be an option in configure
[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 ((wxToolBar*)tbartool->GetToolBar())->ToggleTool(tbartool->GetId(), GetControl32BitValue((ControlRef)tbartool->GetControlHandle()));
127 }
128 ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ;
129 result = noErr;
130 }
131 break ;
132 default :
133 break ;
134 }
135 return result ;
136 }
137
138 pascal 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
153 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
154
155 // ============================================================================
156 // implementation
157 // ============================================================================
158
159 // ----------------------------------------------------------------------------
160 // wxToolBarTool
161 // ----------------------------------------------------------------------------
162
163 bool 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 }
185 void wxToolBarTool::SetSize(const wxSize& size)
186 {
187 if ( IsControl() )
188 {
189 GetControl()->SetSize( size ) ;
190 }
191 }
192
193 void 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 ;
202 int mac_x = position.x ;
203 int mac_y = position.y ;
204 #if !TARGET_API_MAC_OSX
205 WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetTopLevelWindowRef() ;
206 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
207 mac_x += x;
208 mac_y += y;
209 #endif
210 Rect contrlRect ;
211 GetControlBounds( m_controlHandle , &contrlRect ) ;
212 int former_mac_x = contrlRect.left ;
213 int former_mac_y = contrlRect.top ;
214 GetToolBar()->GetToolSize() ;
215
216 if ( mac_x != former_mac_x || mac_y != former_mac_y )
217 {
218 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
219 }
220 }
221 else if ( IsControl() )
222 {
223 GetControl()->Move( position ) ;
224 }
225 }
226
227 const short kwxMacToolBarToolDefaultWidth = 24 ;
228 const short kwxMacToolBarToolDefaultHeight = 22 ;
229 const short kwxMacToolBarTopMargin = 2 ;
230 const short kwxMacToolBarLeftMargin = 2 ;
231
232 wxToolBarTool::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 {
244 Init();
245
246 if (id == wxID_SEPARATOR) return;
247
248 WindowRef window = (WindowRef) tbar->MacGetTopLevelWindowRef() ;
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 ;
258
259 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
260 0 , 0 , 0 , &m_controlHandle ) ;
261
262 InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
263 GetEventTypeCount(eventList), eventList, this,NULL);
264
265 UMAShowControl( m_controlHandle ) ;
266
267 if ( CanBeToggled() && IsToggled() )
268 ::SetControl32BitValue( m_controlHandle , 1 ) ;
269 else
270 ::SetControl32BitValue( m_controlHandle , 0 ) ;
271
272 ControlRef container = (ControlRef) tbar->GetHandle() ;
273 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
274 ::EmbedControl( m_controlHandle , container ) ;
275 }
276
277
278 wxToolBarToolBase *wxToolBar::CreateTool(int id,
279 const wxString& label,
280 const wxBitmap& bmpNormal,
281 const wxBitmap& bmpDisabled,
282 wxItemKind kind,
283 wxObject *clientData,
284 const wxString& shortHelp,
285 const wxString& longHelp)
286 {
287 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
288 clientData, shortHelp, longHelp);
289 }
290
291 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
292 {
293 return new wxToolBarTool(this, control);
294 }
295
296 void wxToolBar::Init()
297 {
298 m_maxWidth = -1;
299 m_maxHeight = -1;
300 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
301 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
302 }
303
304 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
305 long style, const wxString& name)
306 {
307 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
308 return FALSE ;
309
310 return TRUE;
311 }
312
313 wxToolBar::~wxToolBar()
314 {
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
317 }
318
319 bool wxToolBar::Realize()
320 {
321 if (m_tools.GetCount() == 0)
322 return FALSE;
323
324 int x = m_xMargin + kwxMacToolBarLeftMargin ;
325 int y = m_yMargin + kwxMacToolBarTopMargin ;
326
327 int tw, th;
328 GetSize(& tw, & th);
329
330 int maxWidth = 0 ;
331 int maxHeight = 0 ;
332
333 int maxToolWidth = 0;
334 int maxToolHeight = 0;
335
336 // Find the maximum tool width and height
337 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
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
351 bool lastWasRadio = FALSE;
352 node = m_tools.GetFirst();
353 while (node)
354 {
355 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
356 wxSize cursize = tool->GetSize() ;
357
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
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 ;
382
383 tool->SetPosition( wxPoint( x , y ) ) ;
384
385 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
386 {
387 y += cursize.y ;
388 }
389 else
390 {
391 x += cursize.x ;
392 }
393
394 node = node->GetNext();
395 }
396
397 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
398 {
399 if ( m_maxRows == 0 )
400 {
401 // if not set yet, only one row
402 SetRows(1);
403 }
404 maxWidth = tw ;
405 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
406 m_maxHeight = maxHeight ;
407 }
408 else
409 {
410 if ( GetToolsCount() > 0 && m_maxRows == 0 )
411 {
412 // if not set yet, have one column
413 SetRows(GetToolsCount());
414 }
415 maxHeight = th ;
416 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
417 m_maxWidth = maxWidth ;
418 }
419
420 SetSize( maxWidth, maxHeight );
421 InvalidateBestSize();
422
423 return TRUE;
424 }
425
426 void wxToolBar::SetToolBitmapSize(const wxSize& size)
427 {
428 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
429 }
430
431 // The button size is bigger than the bitmap size
432 wxSize wxToolBar::GetToolSize() const
433 {
434 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
435 }
436
437 void wxToolBar::SetRows(int nRows)
438 {
439 if ( nRows == m_maxRows )
440 {
441 // avoid resizing the frame uselessly
442 return;
443 }
444
445 m_maxRows = nRows;
446 }
447
448 void wxToolBar::MacSuperChangedPosition()
449 {
450 wxWindow::MacSuperChangedPosition() ;
451 Realize() ;
452 }
453
454 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
455 {
456 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
457 while (node)
458 {
459 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
460 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
461 if ( r.Contains( wxPoint( x , y ) ) )
462 {
463 return tool;
464 }
465
466 node = node->GetNext();
467 }
468
469 return (wxToolBarToolBase *)NULL;
470 }
471
472 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
473 {
474 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
475 if ( tool )
476 {
477 return tool->GetShortHelp() ;
478 }
479 return wxEmptyString ;
480 }
481
482 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
483 {
484 ((wxToolBarTool*)t)->DoEnable( enable ) ;
485 }
486
487 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
488 {
489 wxToolBarTool *tool = (wxToolBarTool *)t;
490 if ( tool->IsButton() )
491 {
492 ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ;
493 }
494 }
495
496 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
497 wxToolBarToolBase *tool)
498 {
499 // nothing special to do here - we relayout in Realize() later
500 tool->Attach(this);
501 InvalidateBestSize();
502
503 return TRUE;
504 }
505
506 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
507 {
508 wxFAIL_MSG( _T("not implemented") );
509 }
510
511 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
512 {
513 wxToolBarToolsList::compatibility_iterator node;
514 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
515 {
516 wxToolBarToolBase *tool2 = node->GetData();
517 if ( tool2 == tool )
518 {
519 // let node point to the next node in the list
520 node = node->GetNext();
521
522 break;
523 }
524 }
525
526 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
527
528 tool->Detach();
529
530 // and finally reposition all the controls after this one
531
532 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
533 {
534 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
535 wxPoint pt = tool2->GetPosition() ;
536
537 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
538 {
539 pt.y -= sz.y ;
540 }
541 else
542 {
543 pt.x -= sz.x ;
544 }
545 tool2->SetPosition( pt ) ;
546 }
547
548 InvalidateBestSize();
549 return TRUE ;
550 }
551
552 void wxToolBar::OnPaint(wxPaintEvent& event)
553 {
554 wxPaintDC dc(this) ;
555 wxMacPortSetter helper(&dc) ;
556 int w, h ;
557 GetSize( &w , &h ) ;
558
559 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
560 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
561 /*
562 if( toolbarrect.left < 0 )
563 toolbarrect.left = 0 ;
564 if ( toolbarrect.top < 0 )
565 toolbarrect.top = 0 ;
566 */
567 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
568 {
569 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
570 }
571 else
572 {
573 #if TARGET_API_MAC_OSX
574 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
575 if ( UMAGetSystemVersion() >= 0x1030 )
576 {
577 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
578 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
579 CGContextRef cgContext ;
580 Rect bounds ;
581 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
582 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
583 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
584 CGContextScaleCTM( cgContext , 1 , -1 ) ;
585
586 {
587 HIThemeBackgroundDrawInfo drawInfo ;
588 drawInfo.version = 0 ;
589 drawInfo.state = kThemeStateActive ;
590 drawInfo.kind = kThemeBackgroundMetal ;
591 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
592 }
593 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
594 }
595 else
596 #endif
597 {
598 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
599 }
600 #endif
601 }
602 event.Skip() ;
603 }
604
605 #endif // wxUSE_TOOLBAR
606