]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
Patch by David Brinegar to fix menubar updating after modal dialog shown
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "toolbar.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #if wxUSE_TOOLBAR
19
20 #include "wx/wx.h"
21 #include "wx/toolbar.h"
22 #include "wx/notebook.h"
23 #include "wx/tabctrl.h"
24 #include "wx/bitmap.h"
25
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
28
29 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
30 EVT_PAINT( wxToolBar::OnPaint )
31 END_EVENT_TABLE()
32 #endif
33
34 #include "wx/mac/uma.h"
35 #include "wx/geometry.h"
36 // ----------------------------------------------------------------------------
37 // private classes
38 // ----------------------------------------------------------------------------
39
40 class wxToolBarTool : public wxToolBarToolBase
41 {
42 public:
43 wxToolBarTool(wxToolBar *tbar,
44 int id,
45 const wxString& label,
46 const wxBitmap& bmpNormal,
47 const wxBitmap& bmpDisabled,
48 wxItemKind kind,
49 wxObject *clientData,
50 const wxString& shortHelp,
51 const wxString& longHelp) ;
52
53 wxToolBarTool(wxToolBar *tbar, wxControl *control)
54 : wxToolBarToolBase(tbar, control)
55 {
56 Init() ;
57 }
58
59 ~wxToolBarTool()
60 {
61 if ( m_controlHandle )
62 DisposeControl( m_controlHandle ) ;
63 }
64
65 WXWidget GetControlHandle() { return (WXWidget) m_controlHandle ; }
66 void SetControlHandle( ControlRef handle ) { m_controlHandle = handle ; }
67
68 void SetSize(const wxSize& size) ;
69 void SetPosition( const wxPoint& position ) ;
70
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 }
93 bool DoEnable( bool enable ) ;
94 private :
95 void Init()
96 {
97 m_controlHandle = NULL ;
98 }
99 ControlRef m_controlHandle ;
100
101 wxCoord m_x;
102 wxCoord m_y;
103 };
104
105 static const EventTypeSpec eventList[] =
106 {
107 { kEventClassControl , kEventControlHit } ,
108 } ;
109
110 static 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 {
127 ((wxToolBar*)tbartool->GetToolBar())->ToggleTool(tbartool->GetId(), GetControl32BitValue((ControlRef)tbartool->GetControlHandle()));
128 }
129 ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ;
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 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 wxMacReleaseBitmapButton( &info ) ;
264
265 InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
266 GetEventTypeCount(eventList), eventList, this,NULL);
267
268 UMAShowControl( m_controlHandle ) ;
269
270 if ( CanBeToggled() && IsToggled() )
271 ::SetControl32BitValue( m_controlHandle , 1 ) ;
272 else
273 ::SetControl32BitValue( m_controlHandle , 0 ) ;
274
275 ControlRef container = (ControlRef) tbar->GetHandle() ;
276 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
277 ::EmbedControl( m_controlHandle , container ) ;
278 }
279
280
281 wxToolBarToolBase *wxToolBar::CreateTool(int id,
282 const wxString& label,
283 const wxBitmap& bmpNormal,
284 const wxBitmap& bmpDisabled,
285 wxItemKind kind,
286 wxObject *clientData,
287 const wxString& shortHelp,
288 const wxString& longHelp)
289 {
290 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
291 clientData, shortHelp, longHelp);
292 }
293
294 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
295 {
296 return new wxToolBarTool(this, control);
297 }
298
299 void wxToolBar::Init()
300 {
301 m_maxWidth = -1;
302 m_maxHeight = -1;
303 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
304 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
305 }
306
307 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
308 long style, const wxString& name)
309 {
310 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
311 return FALSE ;
312
313 return TRUE;
314 }
315
316 wxToolBar::~wxToolBar()
317 {
318 // we must refresh the frame size when the toolbar is deleted but the frame
319 // is not - otherwise toolbar leaves a hole in the place it used to occupy
320 }
321
322 bool wxToolBar::Realize()
323 {
324 if (m_tools.GetCount() == 0)
325 return FALSE;
326
327 int x = m_xMargin + kwxMacToolBarLeftMargin ;
328 int y = m_yMargin + kwxMacToolBarTopMargin ;
329
330 int tw, th;
331 GetSize(& tw, & th);
332
333 int maxWidth = 0 ;
334 int maxHeight = 0 ;
335
336 int maxToolWidth = 0;
337 int maxToolHeight = 0;
338
339 // Find the maximum tool width and height
340 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
341 while ( node )
342 {
343 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
344 wxSize sz = tool->GetSize() ;
345
346 if ( sz.x > maxToolWidth )
347 maxToolWidth = sz.x ;
348 if (sz.y> maxToolHeight)
349 maxToolHeight = sz.y;
350
351 node = node->GetNext();
352 }
353
354 bool lastWasRadio = FALSE;
355 node = m_tools.GetFirst();
356 while (node)
357 {
358 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
359 wxSize cursize = tool->GetSize() ;
360
361 bool isRadio = FALSE;
362
363 if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO )
364 {
365 if ( !lastWasRadio )
366 {
367 if (tool->Toggle(true))
368 {
369 DoToggleTool(tool, true);
370 }
371 }
372 isRadio = TRUE;
373 }
374 else
375 {
376 isRadio = FALSE;
377 }
378 lastWasRadio = isRadio;
379
380 // for the moment we just do a single row/column alignement
381 if ( x + cursize.x > maxWidth )
382 maxWidth = x + cursize.x ;
383 if ( y + cursize.y > maxHeight )
384 maxHeight = y + cursize.y ;
385
386 tool->SetPosition( wxPoint( x , y ) ) ;
387
388 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
389 {
390 y += cursize.y ;
391 }
392 else
393 {
394 x += cursize.x ;
395 }
396
397 node = node->GetNext();
398 }
399
400 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
401 {
402 if ( m_maxRows == 0 )
403 {
404 // if not set yet, only one row
405 SetRows(1);
406 }
407 m_minWidth = maxWidth;
408 maxWidth = tw ;
409 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
410 m_minHeight = m_maxHeight = maxHeight ;
411 }
412 else
413 {
414 if ( GetToolsCount() > 0 && m_maxRows == 0 )
415 {
416 // if not set yet, have one column
417 SetRows(GetToolsCount());
418 }
419 m_minHeight = maxHeight;
420 maxHeight = th ;
421 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
422 m_minWidth = m_maxWidth = maxWidth ;
423 }
424
425 SetSize( maxWidth, maxHeight );
426 InvalidateBestSize();
427
428 return TRUE;
429 }
430
431 void wxToolBar::SetToolBitmapSize(const wxSize& size)
432 {
433 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
434 }
435
436 // The button size is bigger than the bitmap size
437 wxSize wxToolBar::GetToolSize() const
438 {
439 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
440 }
441
442 void wxToolBar::SetRows(int nRows)
443 {
444 if ( nRows == m_maxRows )
445 {
446 // avoid resizing the frame uselessly
447 return;
448 }
449
450 m_maxRows = nRows;
451 }
452
453 void wxToolBar::MacSuperChangedPosition()
454 {
455 wxWindow::MacSuperChangedPosition() ;
456 Realize() ;
457 }
458
459 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
460 {
461 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
462 while (node)
463 {
464 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
465 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
466 if ( r.Contains( wxPoint( x , y ) ) )
467 {
468 return tool;
469 }
470
471 node = node->GetNext();
472 }
473
474 return (wxToolBarToolBase *)NULL;
475 }
476
477 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
478 {
479 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
480 if ( tool )
481 {
482 return tool->GetShortHelp() ;
483 }
484 return wxEmptyString ;
485 }
486
487 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
488 {
489 ((wxToolBarTool*)t)->DoEnable( enable ) ;
490 }
491
492 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
493 {
494 wxToolBarTool *tool = (wxToolBarTool *)t;
495 if ( tool->IsButton() )
496 {
497 ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ;
498 }
499 }
500
501 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
502 wxToolBarToolBase *tool)
503 {
504 // nothing special to do here - we relayout in Realize() later
505 tool->Attach(this);
506 InvalidateBestSize();
507
508 return TRUE;
509 }
510
511 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
512 {
513 wxFAIL_MSG( _T("not implemented") );
514 }
515
516 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
517 {
518 wxToolBarToolsList::compatibility_iterator node;
519 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
520 {
521 wxToolBarToolBase *tool2 = node->GetData();
522 if ( tool2 == tool )
523 {
524 // let node point to the next node in the list
525 node = node->GetNext();
526
527 break;
528 }
529 }
530
531 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
532
533 tool->Detach();
534
535 // and finally reposition all the controls after this one
536
537 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
538 {
539 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
540 wxPoint pt = tool2->GetPosition() ;
541
542 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
543 {
544 pt.y -= sz.y ;
545 }
546 else
547 {
548 pt.x -= sz.x ;
549 }
550 tool2->SetPosition( pt ) ;
551 }
552
553 InvalidateBestSize();
554 return TRUE ;
555 }
556
557 void wxToolBar::OnPaint(wxPaintEvent& event)
558 {
559 wxPaintDC dc(this) ;
560 #if wxMAC_USE_CORE_GRAPHICS
561 #else
562 wxMacPortSetter helper(&dc) ;
563 int w, h ;
564 GetSize( &w , &h ) ;
565
566 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
567 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
568 /*
569 if( toolbarrect.left < 0 )
570 toolbarrect.left = 0 ;
571 if ( toolbarrect.top < 0 )
572 toolbarrect.top = 0 ;
573 */
574 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
575 {
576 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
577 }
578 else
579 {
580 #if TARGET_API_MAC_OSX
581 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
582 if ( UMAGetSystemVersion() >= 0x1030 )
583 {
584 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
585 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
586 CGContextRef cgContext ;
587 Rect bounds ;
588 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
589 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
590 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
591 CGContextScaleCTM( cgContext , 1 , -1 ) ;
592
593 {
594 HIThemeBackgroundDrawInfo drawInfo ;
595 drawInfo.version = 0 ;
596 drawInfo.state = kThemeStateActive ;
597 drawInfo.kind = kThemeBackgroundMetal ;
598 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
599 }
600 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
601 }
602 else
603 #endif
604 {
605 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
606 }
607 #endif
608 }
609 #endif
610
611 event.Skip() ;
612 }
613
614 #endif // wxUSE_TOOLBAR
615