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