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