]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
corrections for theme brush alignments under X (no more SetOrigin calls)
[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 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 = MacGetRootWindow() ;
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 if (!IsShown())
531 return ;
532
533 wxToolBarTool *tool = (wxToolBarTool *)t;
534 if ( tool->m_index < 0 )
535 return ;
536
537 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
538
539 if ( enable )
540 UMAActivateControl( control ) ;
541 else
542 UMADeactivateControl( control ) ;
543 }
544
545 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
546 {
547 if (!IsShown())
548 return ;
549
550 wxToolBarTool *tool = (wxToolBarTool *)t;
551 if ( tool->m_index < 0 )
552 return ;
553
554 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
555 ::SetControlValue( control , toggle ) ;
556 }
557
558 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
559 wxToolBarToolBase *tool)
560 {
561 // nothing special to do here - we really create the toolbar buttons in
562 // Realize() later
563 tool->Attach(this);
564
565 return TRUE;
566 }
567
568 void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle)
569 {
570 wxToolBarTool *tool = (wxToolBarTool *)t;
571 // TODO: set toggle state
572 }
573
574 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
575 {
576 return TRUE ;
577 }
578
579 void wxToolBar::OnPaint(wxPaintEvent& event)
580 {
581 Point localOrigin ;
582 Rect clipRect ;
583 WindowRef window ;
584 wxWindow *win ;
585
586 GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
587 if ( window && win )
588 {
589 wxMacDrawingHelper help( win ) ;
590 // the mac control manager always assumes to have the origin at 0,0
591 bool hasTabBehind = false ;
592 wxWindow* parent = GetParent() ;
593 while ( parent )
594 {
595 if( parent->IsTopLevel() )
596 {
597 // ::SetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
598 break ;
599 }
600
601 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
602 {
603 if ( ((wxControl*)parent)->GetMacControl() )
604 SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ;
605 break ;
606 }
607
608 parent = parent->GetParent() ;
609 }
610
611 Rect toolbarrect = { m_y + localOrigin.v , m_x + localOrigin.h ,
612 m_y + localOrigin.v + m_height , m_x + localOrigin.h + m_width } ;
613
614 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
615 {
616 int index = 0 ;
617 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
618 {
619 if ( m_macToolHandles[index] )
620 {
621 UMADrawControl( (ControlHandle) m_macToolHandles[index] ) ;
622 }
623 }
624 }
625 // ::SetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
626 }
627 }
628
629 void wxToolBar::OnMouse( wxMouseEvent &event )
630 {
631 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
632 {
633
634 int x = event.m_x ;
635 int y = event.m_y ;
636
637 MacClientToRootWindow( &x , &y ) ;
638
639 ControlHandle control ;
640 Point localwhere ;
641 GrafPtr port ;
642 SInt16 controlpart ;
643 WindowRef window = MacGetRootWindow() ;
644
645 localwhere.h = x ;
646 localwhere.v = y ;
647
648 short modifiers = 0;
649
650 if ( !event.m_leftDown && !event.m_rightDown )
651 modifiers |= btnState ;
652
653 if ( event.m_shiftDown )
654 modifiers |= shiftKey ;
655
656 if ( event.m_controlDown )
657 modifiers |= controlKey ;
658
659 if ( event.m_altDown )
660 modifiers |= optionKey ;
661
662 if ( event.m_metaDown )
663 modifiers |= cmdKey ;
664
665 controlpart = FindControl( localwhere , window , &control ) ;
666 {
667 if ( control && ::IsControlActive( control ) )
668 {
669 {
670 if ( controlpart == kControlIndicatorPart && !UMAHasAppearance() )
671 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) NULL ) ;
672 else
673 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
674 wxTheApp->s_lastMouseDown = 0 ;
675 if ( controlpart && ! ( ( UMAHasAppearance() || (controlpart != kControlIndicatorPart) )
676 && (IsKindOf( CLASSINFO( wxScrollBar ) ) ) ) ) // otherwise we will get the event twice
677 {
678 MacHandleControlClick( control , controlpart ) ;
679 }
680 }
681 }
682 }
683 }
684 }
685
686 #endif // wxUSE_TOOLBAR
687