]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
making getter const
[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 wxSize GetSize() const
70 {
71 if ( IsControl() )
72 {
73 return GetControl()->GetSize() ;
74 }
75 else if ( IsButton() )
76 {
77 return GetToolBar()->GetToolSize() ;
78 }
79 else
80 {
81 wxSize sz = GetToolBar()->GetToolSize() ;
82 sz.x /= 4 ;
83 sz.y /= 4 ;
84 return sz ;
85 }
86 }
87 wxPoint GetPosition() const
88 {
89 return wxPoint(m_x, m_y);
90 }
91 private :
92 void Init()
93 {
94 m_controlHandle = NULL ;
95 }
96 ControlRef m_controlHandle ;
97
98 wxCoord m_x;
99 wxCoord m_y;
100 };
101
102 static const EventTypeSpec eventList[] =
103 {
104 { kEventClassControl , kEventControlHit } ,
105 } ;
106
107 static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
108 {
109 OSStatus result = eventNotHandledErr ;
110
111 wxMacCarbonEvent cEvent( event ) ;
112
113 ControlRef controlRef ;
114
115 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
116
117 switch( GetEventKind( event ) )
118 {
119 case kEventControlHit :
120 {
121 wxToolBarTool* tbartool = (wxToolBarTool*)data ;
122 if ( tbartool->CanBeToggled() )
123 {
124 tbartool->Toggle( GetControl32BitValue( (ControlRef) tbartool->GetControlHandle() ) ) ;
125 }
126 ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ;
127
128 result = noErr;
129 }
130 break ;
131 default :
132 break ;
133 }
134 return result ;
135 }
136
137 pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
138 {
139 OSStatus result = eventNotHandledErr ;
140
141 switch ( GetEventClass( event ) )
142 {
143 case kEventClassControl :
144 result = wxMacToolBarToolControlEventHandler( handler, event, data ) ;
145 break ;
146 default :
147 break ;
148 }
149 return result ;
150 }
151
152 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
153
154 // ============================================================================
155 // implementation
156 // ============================================================================
157
158 // ----------------------------------------------------------------------------
159 // wxToolBarTool
160 // ----------------------------------------------------------------------------
161
162 void wxToolBarTool::SetSize(const wxSize& size)
163 {
164 if ( IsControl() )
165 {
166 GetControl()->SetSize( size ) ;
167 }
168 }
169
170 void wxToolBarTool::SetPosition(const wxPoint& position)
171 {
172 m_x = position.x;
173 m_y = position.y;
174
175 if ( IsButton() )
176 {
177 int x , y ;
178 x = y = 0 ;
179 int mac_x = position.x ;
180 int mac_y = position.y ;
181 #if !TARGET_API_MAC_OSX
182 WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetTopLevelWindowRef() ;
183 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
184 mac_x += x;
185 mac_y += y;
186 #endif
187 Rect contrlRect ;
188 GetControlBounds( m_controlHandle , &contrlRect ) ;
189 int former_mac_x = contrlRect.left ;
190 int former_mac_y = contrlRect.top ;
191 wxSize sz = GetToolBar()->GetToolSize() ;
192
193 if ( mac_x != former_mac_x || mac_y != former_mac_y )
194 {
195 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
196 }
197 }
198 else if ( IsControl() )
199 {
200 GetControl()->Move( position ) ;
201 }
202 }
203
204 const short kwxMacToolBarToolDefaultWidth = 24 ;
205 const short kwxMacToolBarToolDefaultHeight = 22 ;
206 const short kwxMacToolBarTopMargin = 2 ;
207 const short kwxMacToolBarLeftMargin = 2 ;
208
209 wxToolBarTool::wxToolBarTool(wxToolBar *tbar,
210 int id,
211 const wxString& label,
212 const wxBitmap& bmpNormal,
213 const wxBitmap& bmpDisabled,
214 wxItemKind kind,
215 wxObject *clientData,
216 const wxString& shortHelp,
217 const wxString& longHelp)
218 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
219 clientData, shortHelp, longHelp)
220 {
221 Init();
222
223 if (id == wxID_SEPARATOR) return;
224
225 WindowRef window = (WindowRef) tbar->MacGetTopLevelWindowRef() ;
226 wxSize toolSize = tbar->GetToolSize() ;
227 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x } ;
228
229 ControlButtonContentInfo info ;
230 wxMacCreateBitmapButton( &info , GetNormalBitmap() ) ;
231
232 SInt16 behaviour = kControlBehaviorOffsetContents ;
233 if ( CanBeToggled() )
234 behaviour += kControlBehaviorToggles ;
235
236 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
237 0 , 0 , 0 , &m_controlHandle ) ;
238
239 InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
240 GetEventTypeCount(eventList), eventList, this,NULL);
241
242 UMAShowControl( m_controlHandle ) ;
243 if ( !IsEnabled() )
244 DisableControl( m_controlHandle ) ;
245
246 if ( CanBeToggled() && IsToggled() )
247 ::SetControl32BitValue( m_controlHandle , 1 ) ;
248 else
249 ::SetControl32BitValue( m_controlHandle , 0 ) ;
250
251 ControlRef container = (ControlRef) tbar->GetHandle() ;
252 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
253 ::EmbedControl( m_controlHandle , container ) ;
254 }
255
256
257 wxToolBarToolBase *wxToolBar::CreateTool(int id,
258 const wxString& label,
259 const wxBitmap& bmpNormal,
260 const wxBitmap& bmpDisabled,
261 wxItemKind kind,
262 wxObject *clientData,
263 const wxString& shortHelp,
264 const wxString& longHelp)
265 {
266 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
267 clientData, shortHelp, longHelp);
268 }
269
270 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
271 {
272 return new wxToolBarTool(this, control);
273 }
274
275 void wxToolBar::Init()
276 {
277 m_maxWidth = -1;
278 m_maxHeight = -1;
279 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
280 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
281 }
282
283 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
284 long style, const wxString& name)
285 {
286 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
287 return FALSE ;
288
289 return TRUE;
290 }
291
292 wxToolBar::~wxToolBar()
293 {
294 // we must refresh the frame size when the toolbar is deleted but the frame
295 // is not - otherwise toolbar leaves a hole in the place it used to occupy
296 }
297
298 bool wxToolBar::Realize()
299 {
300 if (m_tools.GetCount() == 0)
301 return FALSE;
302
303 int x = m_xMargin + kwxMacToolBarLeftMargin ;
304 int y = m_yMargin + kwxMacToolBarTopMargin ;
305
306 int tw, th;
307 GetSize(& tw, & th);
308
309 int maxWidth = 0 ;
310 int maxHeight = 0 ;
311
312 int maxToolWidth = 0;
313 int maxToolHeight = 0;
314
315 // Find the maximum tool width and height
316 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
317 while ( node )
318 {
319 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
320 wxSize sz = tool->GetSize() ;
321
322 if ( sz.x > maxToolWidth )
323 maxToolWidth = sz.x ;
324 if (sz.y> maxToolHeight)
325 maxToolHeight = sz.y;
326
327 node = node->GetNext();
328 }
329
330 node = m_tools.GetFirst();
331 while (node)
332 {
333 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
334 wxSize cursize = tool->GetSize() ;
335
336 // for the moment we just do a single row/column alignement
337 if ( x + cursize.x > maxWidth )
338 maxWidth = x + cursize.x ;
339 if ( y + cursize.y > maxHeight )
340 maxHeight = y + cursize.y ;
341
342 tool->SetPosition( wxPoint( x , y ) ) ;
343
344 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
345 {
346 y += cursize.y ;
347 }
348 else
349 {
350 x += cursize.x ;
351 }
352
353 node = node->GetNext();
354 }
355
356 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
357 {
358 if ( m_maxRows == 0 )
359 {
360 // if not set yet, only one row
361 SetRows(1);
362 }
363 maxWidth = tw ;
364 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
365 m_maxHeight = maxHeight ;
366 }
367 else
368 {
369 if ( GetToolsCount() > 0 && m_maxRows == 0 )
370 {
371 // if not set yet, have one column
372 SetRows(GetToolsCount());
373 }
374 maxHeight = th ;
375 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
376 m_maxWidth = maxWidth ;
377 }
378
379 SetSize( maxWidth, maxHeight );
380 InvalidateBestSize();
381
382 return TRUE;
383 }
384
385 void wxToolBar::SetToolBitmapSize(const wxSize& size)
386 {
387 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
388 }
389
390 // The button size is bigger than the bitmap size
391 wxSize wxToolBar::GetToolSize() const
392 {
393 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
394 }
395
396 void wxToolBar::SetRows(int nRows)
397 {
398 if ( nRows == m_maxRows )
399 {
400 // avoid resizing the frame uselessly
401 return;
402 }
403
404 m_maxRows = nRows;
405 }
406
407 void wxToolBar::MacSuperChangedPosition()
408 {
409 wxWindow::MacSuperChangedPosition() ;
410 Realize() ;
411 }
412
413 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
414 {
415 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
416 while (node)
417 {
418 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
419 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
420 if ( r.Contains( wxPoint( x , y ) ) )
421 {
422 return tool;
423 }
424
425 node = node->GetNext();
426 }
427
428 return (wxToolBarToolBase *)NULL;
429 }
430
431 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
432 {
433 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
434 if ( tool )
435 {
436 return tool->GetShortHelp() ;
437 }
438 return wxEmptyString ;
439 }
440
441 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
442 {
443 if (!IsShown())
444 return ;
445
446 wxToolBarTool *tool = (wxToolBarTool *)t;
447 if ( tool->IsControl() )
448 {
449 tool->GetControl()->Enable( enable ) ;
450 }
451 else if ( tool->IsButton() )
452 {
453 if ( enable )
454 UMAActivateControl( (ControlRef) tool->GetControlHandle() ) ;
455 else
456 UMADeactivateControl( (ControlRef) tool->GetControlHandle() ) ;
457 }
458 }
459
460 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
461 {
462 if (!IsShown())
463 return ;
464
465 wxToolBarTool *tool = (wxToolBarTool *)t;
466 if ( tool->IsButton() )
467 {
468 ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ;
469 }
470 }
471
472 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
473 wxToolBarToolBase *tool)
474 {
475 // nothing special to do here - we relayout in Realize() later
476 tool->Attach(this);
477 InvalidateBestSize();
478
479 return TRUE;
480 }
481
482 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
483 {
484 wxFAIL_MSG( _T("not implemented") );
485 }
486
487 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
488 {
489 wxToolBarToolsList::compatibility_iterator node;
490 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
491 {
492 wxToolBarToolBase *tool2 = node->GetData();
493 if ( tool2 == tool )
494 {
495 // let node point to the next node in the list
496 node = node->GetNext();
497
498 break;
499 }
500 }
501
502 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
503
504 tool->Detach();
505
506 // and finally reposition all the controls after this one
507
508 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
509 {
510 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
511 wxPoint pt = tool2->GetPosition() ;
512
513 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
514 {
515 pt.y -= sz.y ;
516 }
517 else
518 {
519 pt.x -= sz.x ;
520 }
521 tool2->SetPosition( pt ) ;
522 }
523
524 InvalidateBestSize();
525 return TRUE ;
526 }
527
528 void wxToolBar::OnPaint(wxPaintEvent& event)
529 {
530 wxPaintDC dc(this) ;
531 wxMacPortSetter helper(&dc) ;
532 int w, h ;
533 GetSize( &w , &h ) ;
534
535 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
536 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
537 /*
538 if( toolbarrect.left < 0 )
539 toolbarrect.left = 0 ;
540 if ( toolbarrect.top < 0 )
541 toolbarrect.top = 0 ;
542 */
543 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
544
545 event.Skip() ;
546 }
547
548 #endif // wxUSE_TOOLBAR
549