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