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