]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
added new focus behaviour (like MSW) and toolbar fixes
[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
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
25
26 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
27 EVT_MOUSE_EVENTS( wxToolBar::OnMouse )
28 EVT_PAINT( wxToolBar::OnPaint )
29 END_EVENT_TABLE()
30 #endif
31
32 #include <wx/mac/uma.h>
33
34 // ----------------------------------------------------------------------------
35 // private classes
36 // ----------------------------------------------------------------------------
37
38 class wxToolBarTool : public wxToolBarToolBase
39 {
40 public:
41 wxToolBarTool(wxToolBar *tbar,
42 int id,
43 const wxBitmap& bitmap1,
44 const wxBitmap& bitmap2,
45 bool toggle,
46 wxObject *clientData,
47 const wxString& shortHelpString,
48 const wxString& longHelpString)
49 : wxToolBarToolBase(tbar, id, bitmap1, bitmap2, toggle,
50 clientData, shortHelpString, longHelpString)
51 {
52 m_nSepCount = 0;
53 m_index = -1 ;
54 }
55
56 wxToolBarTool(wxToolBar *tbar, wxControl *control)
57 : wxToolBarToolBase(tbar, control)
58 {
59 m_nSepCount = 1;
60 m_index = -1 ;
61 }
62
63 // set/get the number of separators which we use to cover the space used by
64 // a control in the toolbar
65 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
66 size_t GetSeparatorsCount() const { return m_nSepCount; }
67
68 int m_index ;
69 private:
70 size_t m_nSepCount;
71 };
72
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // wxToolBarTool
80 // ----------------------------------------------------------------------------
81
82 const short defwidth = 24 ;
83 const short defheight = 22 ;
84
85 wxToolBarToolBase *wxToolBar::CreateTool(int id,
86 const wxBitmap& bitmap1,
87 const wxBitmap& bitmap2,
88 bool toggle,
89 wxObject *clientData,
90 const wxString& shortHelpString,
91 const wxString& longHelpString)
92 {
93 return new wxToolBarTool(this, id, bitmap1, bitmap2, toggle,
94 clientData, shortHelpString, longHelpString);
95 }
96
97 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
98 {
99 return new wxToolBarTool(this, control);
100 }
101
102 void wxToolBar::Init()
103 {
104 m_maxWidth = -1;
105 m_maxHeight = -1;
106 m_defaultWidth = defwidth;
107 m_defaultHeight = defheight;
108 // TODO
109 }
110
111 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
112 long style, const wxString& name)
113 {
114
115 m_defaultWidth = defwidth;
116 m_defaultHeight = defheight;
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 #if 1
132 {
133 SetName(name);
134
135 m_windowStyle = style;
136 parent->AddChild(this);
137
138 m_backgroundColour = parent->GetBackgroundColour() ;
139 m_foregroundColour = parent->GetForegroundColour() ;
140
141 if (id == -1)
142 m_windowId = NewControlId();
143 else
144 m_windowId = id;
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 #else
155 Rect bounds ;
156 Str255 title ;
157
158 MacPreControlCreate( parent , id , "" , wxPoint( x , y ) , wxSize( width , height ) ,style, wxDefaultValidator , name , &bounds , title ) ;
159
160 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1,
161 kControlPlacardProc , (long) this ) ;
162 MacPostControlCreate() ;
163 #endif
164 return TRUE;
165 }
166
167 wxToolBar::~wxToolBar()
168 {
169 // we must refresh the frame size when the toolbar is deleted but the frame
170 // is not - otherwise toolbar leaves a hole in the place it used to occupy
171 }
172
173 PicHandle MakePict(GWorldPtr wp, GWorldPtr mask ) ;
174 PicHandle MakePict(GWorldPtr wp, GWorldPtr mask )
175 {
176 CGrafPtr origPort ;
177 GDHandle origDev ;
178
179 PicHandle pict; // this is the Picture we give back
180
181 RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ;
182 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
183 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
184
185 unsigned char *maskimage = NULL ;
186 Rect portRect ;
187 GetPortBounds( wp , &portRect ) ;
188 int width = portRect.right - portRect.left ;
189 int height = portRect.bottom - portRect.top ;
190
191 LockPixels( GetGWorldPixMap( wp ) ) ;
192 GetGWorld( &origPort , &origDev ) ;
193
194 if ( mask )
195 {
196
197 maskimage = (unsigned char*) malloc( width * height ) ;
198 SetGWorld( mask , NULL ) ;
199 LockPixels( GetGWorldPixMap( mask ) ) ;
200 for ( int y = 0 ; y < height ; ++y )
201 {
202 for( int x = 0 ; x < width ; ++x )
203 {
204 RGBColor col ;
205
206 GetCPixel( x + portRect.left , y + portRect.top , &col ) ;
207 maskimage[y*width + x] = ( col.red == 0 ) ; // for monochrome masks
208 }
209 }
210 UnlockPixels( GetGWorldPixMap( mask ) ) ;
211 }
212
213 SetGWorld( wp , NULL ) ;
214
215 pict = OpenPicture(&portRect); // open a picture, this disables drawing
216 if(!pict)
217 return NULL;
218
219 RGBBackColor( &gray ) ;
220 RGBForeColor( &black ) ;
221 EraseRect(&portRect) ;
222 RGBBackColor( &white ) ;
223
224 if ( maskimage )
225 {
226 for ( int y = 0 ; y < height ; ++y )
227 {
228 for( int x = 0 ; x < width ; ++x )
229 {
230 if ( maskimage[y*width + x] )
231 {
232 RGBColor col ;
233
234 GetCPixel( x + portRect.left , y + portRect.top , &col ) ;
235 SetCPixel( x + portRect.left , y + portRect.top , &col ) ;
236 }
237 }
238 }
239 free( maskimage ) ;
240 maskimage = NULL ;
241 }
242 else
243 {
244 CopyBits(GetPortBitMapForCopyBits(wp), // src PixMap - we copy image over itself -
245 GetPortBitMapForCopyBits(wp), // dst PixMap - no drawing occurs -
246 &portRect, // srcRect - it will be recorded and compressed -
247 &portRect, // dstRect - into the picture that is open -
248 srcCopy,NULL); // copyMode and no clip region
249
250 }
251 ClosePicture(); // We are done recording the picture
252 UnlockPixels( GetGWorldPixMap( wp ) ) ;
253 SetGWorld( origPort , origDev ) ;
254 return pict; // return our groovy pict handle
255 }
256
257 const short kwxMacToolBarTopMargin = 2 ;
258 const short kwxMacToolBarLeftMargin = 2 ;
259
260 bool wxToolBar::Realize()
261 {
262 if (m_tools.Number() == 0)
263 return FALSE;
264
265 Point localOrigin ;
266 Rect clipRect ;
267 WindowRef window ;
268 wxWindow *win ;
269
270 GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
271
272 Rect toolbarrect = { m_y + localOrigin.v , m_x + localOrigin.h ,
273 m_y + m_height + localOrigin.v , m_x + m_width + localOrigin.h} ;
274 ControlFontStyleRec controlstyle ;
275
276 controlstyle.flags = kControlUseFontMask ;
277 controlstyle.font = kControlFontSmallSystemFont ;
278
279 wxNode *node = m_tools.First();
280 int noButtons = 0;
281 int x = 0 ;
282 wxSize toolSize = GetToolSize() ;
283 int tw, th;
284 GetSize(& tw, & th);
285 m_maxWidth = m_maxHeight = 0 ;
286 while (node)
287 {
288 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
289 wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->GetBitmap1().GetRefData()) ;
290
291 if( !tool->IsSeparator() )
292 {
293 Rect toolrect = { toolbarrect.top + kwxMacToolBarTopMargin , toolbarrect.left + x + kwxMacToolBarLeftMargin , 0 , 0 } ;
294 toolrect.right = toolrect.left + toolSize.x ;
295 toolrect.bottom = toolrect.top + toolSize.y ;
296
297 PicHandle icon = NULL ;
298 if ( bmap )
299 {
300 if ( bmap->m_bitmapType == kMacBitmapTypePict )
301 icon = bmap->m_hPict ;
302 else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
303 {
304 if ( tool->GetBitmap1().GetMask() )
305 {
306 icon = MakePict( bmap->m_hBitmap , tool->GetBitmap1().GetMask()->GetMaskBitmap() ) ;
307 }
308 else
309 {
310 icon = MakePict( bmap->m_hBitmap , NULL ) ;
311 }
312 }
313 }
314
315 ControlHandle m_macToolHandle ;
316
317 SInt16 behaviour = kControlBehaviorOffsetContents ;
318 if ( tool->CanBeToggled() )
319 behaviour += kControlBehaviorToggles ;
320
321 if ( icon )
322 {
323 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
324 behaviour + kControlContentPictHandle , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
325 ControlButtonContentInfo info ;
326
327 info.contentType = kControlContentPictHandle ;
328 info.u.picture = icon ;
329
330 UMASetControlData( m_macToolHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
331 }
332 else
333 {
334 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
335 behaviour , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
336 }
337 m_macToolHandles.Add( m_macToolHandle ) ;
338 tool->m_index = m_macToolHandles.Count() -1 ;
339 if ( !tool->IsEnabled() )
340 {
341 UMADeactivateControl( m_macToolHandle ) ;
342 }
343 if ( tool->CanBeToggled() && tool->IsToggled() )
344 {
345 ::SetControlValue( m_macToolHandle , 1 ) ;
346 }
347 UMASetControlFontStyle( m_macToolHandle , &controlstyle ) ;
348 ControlHandle container = GetParent()->MacGetContainerForEmbedding() ;
349 wxASSERT_MSG( container != NULL , "No valid mac container control" ) ;
350 UMAEmbedControl( m_macToolHandle , container ) ;
351
352 x += (int)toolSize.x;
353 noButtons ++;
354 }
355 else
356 {
357 m_macToolHandles.Add( NULL ) ;
358 x += (int)toolSize.x / 4;
359 }
360 if ( toolbarrect.left + x + kwxMacToolBarLeftMargin - m_x - localOrigin.h > m_maxWidth)
361 m_maxWidth = toolbarrect.left + x + kwxMacToolBarLeftMargin - m_x - localOrigin.h;
362 if (toolbarrect.top + kwxMacToolBarTopMargin - m_y - localOrigin.v > m_maxHeight)
363 m_maxHeight = toolbarrect.top + kwxMacToolBarTopMargin - m_y - localOrigin.v ;
364
365 node = node->Next();
366 }
367
368 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
369 {
370 if ( m_maxRows == 0 )
371 {
372 // if not set yet, only one row
373 SetRows(1);
374 }
375 m_maxWidth = clipRect.right - m_x ;
376 //m_maxWidth = tw ; // +=toolSize.x;
377 //m_maxWidth += m_xMargin ;
378 m_maxHeight += toolSize.y;
379 m_maxHeight += m_yMargin;
380 }
381 else
382 {
383 if ( noButtons > 0 && m_maxRows == 0 )
384 {
385 // if not set yet, have one column
386 SetRows(noButtons);
387 }
388 m_maxHeight = clipRect.bottom - m_y ;
389 //m_maxHeight += m_yMargin ;
390 m_maxWidth += toolSize.x;
391 m_maxWidth += m_xMargin;
392 }
393
394 SetSize(m_maxWidth, m_maxHeight);
395
396 return TRUE;
397 }
398
399 void wxToolBar::SetToolBitmapSize(const wxSize& size)
400 {
401 m_defaultWidth = size.x; m_defaultHeight = size.y;
402 }
403
404 // The button size is bigger than the bitmap size
405 wxSize wxToolBar::GetToolSize() const
406 {
407 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
408 }
409
410 void wxToolBar::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
411 {
412 int index = 0 ;
413 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
414 {
415 if ( m_macToolHandles[index] == (void*) control )
416 {
417 wxToolBarTool *tool = (wxToolBarTool *)m_tools.Nth( index )->Data();
418 if ( tool->CanBeToggled() )
419 {
420 tool->Toggle( GetControlValue( control ) ) ;
421 }
422 OnLeftClick( tool->GetId() , tool -> IsToggled() ) ;
423 break ;
424 }
425 }
426 }
427
428 void wxToolBar::SetRows(int nRows)
429 {
430 if ( nRows == m_maxRows )
431 {
432 // avoid resizing the frame uselessly
433 return;
434 }
435
436 m_maxRows = nRows;
437 }
438
439 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
440 {
441 MacClientToRootWindow( &x , &y ) ;
442 Point pt = { y ,x } ;
443
444 int index = 0 ;
445 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
446 {
447 if ( m_macToolHandles[index] )
448 {
449 Rect bounds ;
450 GetControlBounds((ControlHandle) m_macToolHandles[index], &bounds ) ;
451 if ( PtInRect( pt , &bounds ) )
452 {
453 return (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ;
454 }
455 }
456 }
457
458 return (wxToolBarToolBase *)NULL;
459 }
460
461 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
462 {
463 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
464 if ( tool )
465 {
466 return tool->GetShortHelp() ;
467 }
468 return "" ;
469 }
470
471 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
472 {
473 wxToolBarTool *tool = (wxToolBarTool *)t;
474 if ( tool->m_index < 0 )
475 return ;
476
477 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
478
479 if ( enable )
480 UMAActivateControl( control ) ;
481 else
482 UMADeactivateControl( control ) ;
483 }
484
485 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
486 {
487 wxToolBarTool *tool = (wxToolBarTool *)t;
488 if ( tool->m_index < 0 )
489 return ;
490
491 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
492 ::SetControlValue( control , toggle ) ;
493 }
494
495 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
496 wxToolBarToolBase *tool)
497 {
498 // nothing special to do here - we really create the toolbar buttons in
499 // Realize() later
500 tool->Attach(this);
501
502 return TRUE;
503 }
504
505 void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle)
506 {
507 wxToolBarTool *tool = (wxToolBarTool *)t;
508 // TODO: set toggle state
509 }
510
511 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
512 {
513 return TRUE ;
514 }
515
516 void wxToolBar::OnPaint(wxPaintEvent& event)
517 {
518 Point localOrigin ;
519 Rect clipRect ;
520 WindowRef window ;
521 wxWindow *win ;
522
523 GetParent()->MacGetPortParams( &localOrigin , &clipRect , &window , &win ) ;
524 if ( window && win )
525 {
526 wxMacDrawingHelper help( win ) ;
527 // the mac control manager always assumes to have the origin at 0,0
528 SetOrigin( 0 , 0 ) ;
529
530 bool hasTabBehind = false ;
531 wxWindow* parent = GetParent() ;
532 while ( parent )
533 {
534 if( parent->MacGetWindowData() )
535 {
536 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
537 break ;
538 }
539
540 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
541 {
542 if ( ((wxControl*)parent)->GetMacControl() )
543 SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ;
544 break ;
545 }
546
547 parent = parent->GetParent() ;
548 }
549 int w = m_width ;
550 int h = m_height ;
551
552 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
553 {
554 w = clipRect.right - m_x ;
555 }
556 else
557 {
558 h = clipRect.bottom - m_y ;
559 }
560
561 Rect toolbarrect = { m_y + localOrigin.v , m_x + localOrigin.h ,
562 m_y + localOrigin.v + h , m_x + localOrigin.h + w } ;
563
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 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
576 wxDC::MacInvalidateSetup() ;
577 }
578 /*
579 WindowRef window = GetMacRootWindow() ;
580 if ( window )
581 {
582 wxWindow* win = wxFindWinFromMacWindow( window ) ;
583 if ( win )
584 {
585 wxMacDrawingHelper help( win ) ;
586 // the mac control manager always assumes to have the origin at 0,0
587 SetOrigin( 0 , 0 ) ;
588
589 bool hasTabBehind = false ;
590 wxWindow* parent = GetParent() ;
591 while ( parent )
592 {
593 if( parent->MacGetWindowData() )
594 {
595 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
596 break ;
597 }
598
599 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
600 {
601 if ( ((wxControl*)parent)->GetMacControl() )
602 SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ;
603 break ;
604 }
605
606 parent = parent->GetParent() ;
607 }
608 Rect toolbarrect = { m_y , m_x , m_y + m_height , m_x + 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 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
622 wxDC::MacInvalidateSetup() ;
623 }
624 }
625 */
626 }
627 void wxToolBar::OnMouse( wxMouseEvent &event )
628 {
629
630 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
631 {
632
633 int x = event.m_x ;
634 int y = event.m_y ;
635
636 MacClientToRootWindow( &x , &y ) ;
637
638 ControlHandle control ;
639 Point localwhere ;
640 GrafPtr port ;
641 SInt16 controlpart ;
642 WindowRef window = GetMacRootWindow() ;
643
644 localwhere.h = x ;
645 localwhere.v = y ;
646
647 short modifiers = 0;
648
649 if ( !event.m_leftDown && !event.m_rightDown )
650 modifiers |= btnState ;
651
652 if ( event.m_shiftDown )
653 modifiers |= shiftKey ;
654
655 if ( event.m_controlDown )
656 modifiers |= controlKey ;
657
658 if ( event.m_altDown )
659 modifiers |= optionKey ;
660
661 if ( event.m_metaDown )
662 modifiers |= cmdKey ;
663
664 controlpart = FindControl( localwhere , window , &control ) ;
665 {
666 if ( AcceptsFocus() && FindFocus() != this )
667 {
668 SetFocus() ;
669 }
670 if ( control && UMAIsControlActive( control ) )
671 {
672 {
673 if ( controlpart == kControlIndicatorPart && !UMAHasAppearance() )
674 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) NULL ) ;
675 else
676 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
677 wxTheApp->s_lastMouseDown = 0 ;
678 if ( controlpart && ! ( ( UMAHasAppearance() || (controlpart != kControlIndicatorPart) )
679 && (IsKindOf( CLASSINFO( wxScrollBar ) ) ) ) ) // otherwise we will get the event twice
680 {
681 MacHandleControlClick( control , controlpart ) ;
682 }
683 }
684 }
685 }
686 }
687 }
688
689 #endif // wxUSE_TOOLBAR
690