]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
corrected warnings when compiling with -Wall -W
[wxWidgets.git] / src / mac / carbon / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: toolbar.cpp
3 // Purpose: wxToolBar
4 // Author: AUTHOR
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHORy
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "toolbar.h"
13 #endif
14
15 #include "wx/wx.h"
16
17 #if wxUSE_TOOLBAR
18
19 #include "wx/toolbar.h"
20 #include "wx/notebook.h"
21 #include "wx/tabctrl.h"
22 #include "wx/bitmap.h"
23
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
26
27 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
28 EVT_MOUSE_EVENTS( wxToolBar::OnMouse )
29 EVT_PAINT( wxToolBar::OnPaint )
30 END_EVENT_TABLE()
31 #endif
32
33 #include "wx/mac/uma.h"
34
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 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
52 clientData, shortHelp, longHelp)
53 {
54 m_nSepCount = 0;
55 m_index = -1 ;
56 }
57
58 wxToolBarTool(wxToolBar *tbar, wxControl *control)
59 : wxToolBarToolBase(tbar, control)
60 {
61 m_nSepCount = 1;
62 m_index = -1 ;
63 }
64
65 // set/get the number of separators which we use to cover the space used by
66 // a control in the toolbar
67 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
68 size_t GetSeparatorsCount() const { return m_nSepCount; }
69
70 int m_index ;
71 private:
72 size_t m_nSepCount;
73 };
74
75
76 // ============================================================================
77 // implementation
78 // ============================================================================
79
80 // ----------------------------------------------------------------------------
81 // wxToolBarTool
82 // ----------------------------------------------------------------------------
83
84 const short kwxMacToolBarToolDefaultWidth = 24 ;
85 const short kwxMacToolBarToolDefaultHeight = 22 ;
86 const short kwxMacToolBarTopMargin = 2 ;
87 const short kwxMacToolBarLeftMargin = 2 ;
88
89
90 wxToolBarToolBase *wxToolBar::CreateTool(int id,
91 const wxString& label,
92 const wxBitmap& bmpNormal,
93 const wxBitmap& bmpDisabled,
94 wxItemKind kind,
95 wxObject *clientData,
96 const wxString& shortHelp,
97 const wxString& longHelp)
98 {
99 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
100 clientData, shortHelp, longHelp);
101 }
102
103 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
104 {
105 return new wxToolBarTool(this, control);
106 }
107
108 void wxToolBar::Init()
109 {
110 m_maxWidth = -1;
111 m_maxHeight = -1;
112 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
113 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
114 }
115
116 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
117 long style, const wxString& name)
118 {
119
120 int x = pos.x;
121 int y = pos.y;
122 int width = size.x;
123 int height = size.y;
124
125 if (width <= 0)
126 width = 100;
127 if (height <= 0)
128 height = 30;
129 if (x < 0)
130 x = 0;
131 if (y < 0)
132 y = 0;
133
134 SetName(name);
135
136 m_windowStyle = style;
137 parent->AddChild(this);
138
139 m_backgroundColour = parent->GetBackgroundColour() ;
140 m_foregroundColour = parent->GetForegroundColour() ;
141
142 if (id == -1)
143 m_windowId = NewControlId();
144 else
145 m_windowId = id;
146
147 {
148 m_width = size.x ;
149 m_height = size.y ;
150 int x = pos.x ;
151 int y = pos.y ;
152 AdjustForParentClientOrigin(x, y, wxSIZE_USE_EXISTING);
153 m_x = x ;
154 m_y = y ;
155 }
156
157 return TRUE;
158 }
159
160 wxToolBar::~wxToolBar()
161 {
162 // we must refresh the frame size when the toolbar is deleted but the frame
163 // is not - otherwise toolbar leaves a hole in the place it used to occupy
164 }
165
166 bool wxToolBar::Realize()
167 {
168 if (m_tools.Number() == 0)
169 return FALSE;
170
171 Point localOrigin ;
172 // Rect clipRect ;
173 WindowRef window = (WindowRef) MacGetRootWindow() ;
174 // wxWindow *win ;
175
176 int lx , ly ;
177 lx = ly = 0 ;
178 MacWindowToRootWindow( &lx , &ly ) ;
179 localOrigin.v = ly ;
180 localOrigin.h = lx ;
181
182 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
183
184 Rect toolbarrect = { localOrigin.v ,localOrigin.h ,
185 m_height + localOrigin.v , m_width + localOrigin.h} ;
186 ControlFontStyleRec controlstyle ;
187
188 controlstyle.flags = kControlUseFontMask ;
189 controlstyle.font = kControlFontSmallSystemFont ;
190
191 wxNode *node = m_tools.First();
192 int noButtons = 0;
193 int x = 0 ;
194 int y = 0 ;
195 wxSize toolSize = GetToolSize() ;
196 int tw, th;
197 GetSize(& tw, & th);
198
199 int maxWidth = 0 ;
200 int maxHeight = 0 ;
201
202 while (node)
203 {
204 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
205 wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->GetNormalBitmap().GetRefData()) ;
206
207 if( !tool->IsSeparator() )
208 {
209 Rect toolrect = { toolbarrect.top + y + m_yMargin + kwxMacToolBarTopMargin,
210 toolbarrect.left + x + m_xMargin + kwxMacToolBarLeftMargin , 0 , 0 } ;
211 toolrect.right = toolrect.left + toolSize.x ;
212 toolrect.bottom = toolrect.top + toolSize.y ;
213
214 ControlButtonContentInfo info ;
215 wxMacCreateBitmapButton( &info , tool->GetNormalBitmap() ) ;
216 ControlHandle m_macToolHandle ;
217
218 SInt16 behaviour = kControlBehaviorOffsetContents ;
219 if ( tool->CanBeToggled() )
220 behaviour += kControlBehaviorToggles ;
221
222 if ( info.contentType != kControlNoContent )
223 {
224 m_macToolHandle = ::NewControl( window , &toolrect , "\p" , false , 0 ,
225 behaviour + info.contentType , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
226
227 ::SetControlData( m_macToolHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
228 }
229 else
230 {
231 m_macToolHandle = ::NewControl( window , &toolrect , "\p" , false , 0 ,
232 behaviour , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
233 }
234 UMAShowControl( m_macToolHandle ) ;
235 m_macToolHandles.Add( m_macToolHandle ) ;
236 tool->m_index = m_macToolHandles.Count() -1 ;
237 if ( !tool->IsEnabled() )
238 {
239 UMADeactivateControl( m_macToolHandle ) ;
240 }
241 if ( tool->CanBeToggled() && tool->IsToggled() )
242 {
243 ::SetControlValue( m_macToolHandle , 1 ) ;
244 }
245 else
246 {
247 ::SetControlValue( m_macToolHandle , 0 ) ;
248 }
249 /*
250 ::SetControlFontStyle( m_macToolHandle , &controlstyle ) ;
251 */
252 ControlHandle container = (ControlHandle) GetParent()->MacGetContainerForEmbedding() ;
253 wxASSERT_MSG( container != NULL , "No valid mac container control" ) ;
254 ::EmbedControl( m_macToolHandle , container ) ;
255
256 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
257 {
258 x += (int)toolSize.x;
259 }
260 else
261 {
262 y += (int)toolSize.y;
263 }
264 noButtons ++;
265 }
266 else
267 {
268 m_macToolHandles.Add( NULL ) ;
269 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
270 {
271 x += (int)toolSize.x / 4;
272 }
273 else
274 {
275 y += (int)toolSize.y / 4;
276 }
277 }
278 if ( toolbarrect.left + x + m_xMargin + kwxMacToolBarLeftMargin - m_x - localOrigin.h > maxWidth) {
279 maxWidth = toolbarrect.left + x + m_xMargin + kwxMacToolBarLeftMargin - m_x - localOrigin.h;
280 }
281 if (toolbarrect.top + y + m_yMargin + kwxMacToolBarTopMargin - m_y - localOrigin.v > maxHeight) {
282 maxHeight = toolbarrect.top + y + m_yMargin + kwxMacToolBarTopMargin - m_y - localOrigin.v ;
283 }
284 node = node->Next();
285 }
286
287 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
288 {
289 if ( m_maxRows == 0 )
290 {
291 // if not set yet, only one row
292 SetRows(1);
293 }
294 maxWidth = tw ;
295 maxHeight += toolSize.y;
296 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
297 m_maxHeight = maxHeight ;
298 }
299 else
300 {
301 if ( noButtons > 0 && m_maxRows == 0 )
302 {
303 // if not set yet, have one column
304 SetRows(noButtons);
305 }
306 maxHeight = th ;
307 maxWidth += toolSize.x;
308 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
309 m_maxWidth = maxWidth ;
310 }
311
312 SetSize(maxWidth, maxHeight);
313
314 return TRUE;
315 }
316
317 void wxToolBar::SetToolBitmapSize(const wxSize& size)
318 {
319 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
320 }
321
322 // The button size is bigger than the bitmap size
323 wxSize wxToolBar::GetToolSize() const
324 {
325 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
326 }
327
328 void wxToolBar::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
329 {
330 int index = 0 ;
331 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
332 {
333 if ( m_macToolHandles[index] == (void*) control )
334 {
335 wxToolBarTool *tool = (wxToolBarTool *)m_tools.Nth( index )->Data();
336 if ( tool->CanBeToggled() )
337 {
338 tool->Toggle( GetControlValue( (ControlHandle) control ) ) ;
339 }
340 OnLeftClick( tool->GetId() , tool -> IsToggled() ) ;
341 break ;
342 }
343 }
344 }
345
346 void wxToolBar::SetRows(int nRows)
347 {
348 if ( nRows == m_maxRows )
349 {
350 // avoid resizing the frame uselessly
351 return;
352 }
353
354 m_maxRows = nRows;
355 }
356
357 void wxToolBar::MacSuperChangedPosition()
358 {
359 if (m_tools.Number() > 0)
360 {
361
362 Point localOrigin ;
363 // Rect clipRect ;
364 // WindowRef window ;
365 // wxWindow *win ;
366 int lx , ly ;
367 lx = ly = 0 ;
368 MacWindowToRootWindow( &lx , &ly ) ;
369 localOrigin.v = ly ;
370 localOrigin.h = lx ;
371
372 // GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
373
374 Rect toolbarrect = { localOrigin.v ,localOrigin.h ,
375 m_height + localOrigin.v , m_width + localOrigin.h} ;
376 ControlFontStyleRec controlstyle ;
377
378 controlstyle.flags = kControlUseFontMask ;
379 controlstyle.font = kControlFontSmallSystemFont ;
380
381 wxNode *node = m_tools.First();
382 int noButtons = 0;
383 int x = 0 ;
384 wxSize toolSize = GetToolSize() ;
385 int tw, th;
386 GetSize(& tw, & th);
387
388 int maxWidth = 0 ;
389 int maxHeight = 0 ;
390 int toolcount = 0 ;
391 {
392 WindowRef rootwindow = (WindowRef) MacGetRootWindow() ;
393 while (node)
394 {
395 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
396 wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->GetNormalBitmap().GetRefData()) ;
397
398 if( !tool->IsSeparator() )
399 {
400 Rect toolrect = { toolbarrect.top + m_yMargin + kwxMacToolBarTopMargin, toolbarrect.left + x + m_xMargin + kwxMacToolBarLeftMargin , 0 , 0 } ;
401 toolrect.right = toolrect.left + toolSize.x ;
402 toolrect.bottom = toolrect.top + toolSize.y ;
403
404 ControlHandle m_macToolHandle = (ControlHandle) m_macToolHandles[toolcount++] ;
405
406 {
407 Rect contrlRect ;
408 GetControlBounds( m_macToolHandle , &contrlRect ) ;
409 int former_mac_x = contrlRect.left ;
410 int former_mac_y = contrlRect.top ;
411 int mac_x = toolrect.left ;
412 int mac_y = toolrect.top ;
413
414 if ( mac_x != former_mac_x || mac_y != former_mac_y )
415 {
416 {
417 Rect inval = { former_mac_y , former_mac_x , former_mac_y + toolSize.y , former_mac_x + toolSize.y } ;
418 InvalWindowRect( rootwindow , &inval ) ;
419 }
420 UMAMoveControl( m_macToolHandle , mac_x , mac_y ) ;
421 {
422 Rect inval = { mac_y , mac_x , mac_y + toolSize.y , mac_x + toolSize.y } ;
423 InvalWindowRect( rootwindow , &inval ) ;
424 }
425 }
426 }
427
428 x += (int)toolSize.x;
429 noButtons ++;
430 }
431 else
432 {
433 toolcount++ ;
434 x += (int)toolSize.x / 4;
435 }
436 if ( toolbarrect.left + x + m_xMargin + kwxMacToolBarLeftMargin- m_x - localOrigin.h > maxWidth)
437 maxWidth = toolbarrect.left + x + kwxMacToolBarLeftMargin+ m_xMargin - m_x - localOrigin.h;
438 if (toolbarrect.top + m_yMargin + kwxMacToolBarTopMargin - m_y - localOrigin.v > maxHeight)
439 maxHeight = toolbarrect.top + kwxMacToolBarTopMargin + m_yMargin - m_y - localOrigin.v ;
440
441 node = node->Next();
442 }
443 }
444
445 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
446 {
447 if ( m_maxRows == 0 )
448 {
449 // if not set yet, only one row
450 SetRows(1);
451 }
452 maxWidth = tw ;
453 maxHeight += toolSize.y;
454 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
455 m_maxHeight = maxHeight ;
456 }
457 else
458 {
459 if ( noButtons > 0 && m_maxRows == 0 )
460 {
461 // if not set yet, have one column
462 SetRows(noButtons);
463 }
464 maxHeight = th ;
465 maxWidth += toolSize.x;
466 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
467 m_maxWidth = maxWidth ;
468 }
469
470 SetSize(maxWidth, maxHeight);
471 }
472
473 wxWindow::MacSuperChangedPosition() ;
474 }
475
476 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
477 {
478 MacClientToRootWindow( &x , &y ) ;
479 Point pt = { y ,x } ;
480
481 int index = 0 ;
482 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
483 {
484 if ( m_macToolHandles[index] )
485 {
486 Rect bounds ;
487 GetControlBounds((ControlHandle) m_macToolHandles[index], &bounds ) ;
488 if ( PtInRect( pt , &bounds ) )
489 {
490 return (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ;
491 }
492 }
493 }
494
495 return (wxToolBarToolBase *)NULL;
496 }
497
498 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
499 {
500 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
501 if ( tool )
502 {
503 return tool->GetShortHelp() ;
504 }
505 return "" ;
506 }
507
508 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
509 {
510 if (!IsShown())
511 return ;
512
513 wxToolBarTool *tool = (wxToolBarTool *)t;
514 if ( tool->m_index < 0 )
515 return ;
516
517 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
518
519 if ( enable )
520 UMAActivateControl( control ) ;
521 else
522 UMADeactivateControl( control ) ;
523 }
524
525 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
526 {
527 if (!IsShown())
528 return ;
529
530 wxToolBarTool *tool = (wxToolBarTool *)t;
531 if ( tool->m_index < 0 )
532 return ;
533
534 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
535 ::SetControlValue( control , toggle ) ;
536 }
537
538 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
539 wxToolBarToolBase *tool)
540 {
541 // nothing special to do here - we really create the toolbar buttons in
542 // Realize() later
543 tool->Attach(this);
544
545 return TRUE;
546 }
547
548 void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle)
549 {
550 wxToolBarTool *tool = (wxToolBarTool *)t;
551 // TODO: set toggle state
552 }
553
554 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
555 {
556 return TRUE ;
557 }
558
559 void wxToolBar::OnPaint(wxPaintEvent& event)
560 {
561 wxPaintDC dc(this) ;
562 wxMacPortSetter helper(&dc) ;
563
564 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
565 dc.YLOG2DEVMAC(m_height) , dc.XLOG2DEVMAC(m_width) } ;
566 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
567 {
568 int index = 0 ;
569 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
570 {
571 if ( m_macToolHandles[index] )
572 {
573 UMADrawControl( (ControlHandle) m_macToolHandles[index] ) ;
574 }
575 }
576 }
577 }
578
579 void wxToolBar::OnMouse( wxMouseEvent &event )
580 {
581 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
582 {
583
584 int x = event.m_x ;
585 int y = event.m_y ;
586
587 MacClientToRootWindow( &x , &y ) ;
588
589 ControlHandle control ;
590 Point localwhere ;
591 GrafPtr port ;
592 SInt16 controlpart ;
593 WindowRef window = (WindowRef) MacGetRootWindow() ;
594
595 localwhere.h = x ;
596 localwhere.v = y ;
597
598 short modifiers = 0;
599
600 if ( !event.m_leftDown && !event.m_rightDown )
601 modifiers |= btnState ;
602
603 if ( event.m_shiftDown )
604 modifiers |= shiftKey ;
605
606 if ( event.m_controlDown )
607 modifiers |= controlKey ;
608
609 if ( event.m_altDown )
610 modifiers |= optionKey ;
611
612 if ( event.m_metaDown )
613 modifiers |= cmdKey ;
614
615 controlpart = FindControl( localwhere , window , &control ) ;
616 {
617 if ( control && ::IsControlActive( control ) )
618 {
619 {
620 if ( controlpart == kControlIndicatorPart && !UMAHasAppearance() )
621 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) NULL ) ;
622 else
623 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
624 wxTheApp->s_lastMouseDown = 0 ;
625 if ( controlpart && ! ( ( UMAHasAppearance() || (controlpart != kControlIndicatorPart) )
626 && (IsKindOf( CLASSINFO( wxScrollBar ) ) ) ) ) // otherwise we will get the event twice
627 {
628 MacHandleControlClick( control , controlpart ) ;
629 }
630 }
631 }
632 }
633 }
634 }
635
636 #endif // wxUSE_TOOLBAR
637