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