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