]> git.saurik.com Git - wxWidgets.git/blame - src/mac/toolbar.cpp
Committing in .
[wxWidgets.git] / src / mac / toolbar.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: toolbar.cpp
3// Purpose: wxToolBar
4// Author: AUTHOR
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
2f1ae414 8// Copyright: (c) AUTHORy
e9576ca5
SC
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "toolbar.h"
13#endif
14
15#include "wx/wx.h"
519cb848
SC
16
17#if wxUSE_TOOLBAR
18
e9576ca5 19#include "wx/toolbar.h"
2f1ae414
SC
20#include "wx/notebook.h"
21#include "wx/tabctrl.h"
e9576ca5 22
2f1ae414 23#if !USE_SHARED_LIBRARY
12ed316d 24IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
e9576ca5
SC
25
26BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
2f1ae414
SC
27 EVT_MOUSE_EVENTS( wxToolBar::OnMouse )
28 EVT_PAINT( wxToolBar::OnPaint )
e9576ca5 29END_EVENT_TABLE()
2f1ae414 30#endif
e9576ca5 31
519cb848
SC
32#include <wx/mac/uma.h>
33
37e2cb08
SC
34// ----------------------------------------------------------------------------
35// private classes
36// ----------------------------------------------------------------------------
37
38class wxToolBarTool : public wxToolBarToolBase
39{
40public:
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;
2f1ae414 53 m_index = -1 ;
37e2cb08
SC
54 }
55
56 wxToolBarTool(wxToolBar *tbar, wxControl *control)
57 : wxToolBarToolBase(tbar, control)
58 {
59 m_nSepCount = 1;
2f1ae414 60 m_index = -1 ;
37e2cb08
SC
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 ;
69private:
70 size_t m_nSepCount;
71};
72
73
74// ============================================================================
75// implementation
76// ============================================================================
77
78// ----------------------------------------------------------------------------
79// wxToolBarTool
80// ----------------------------------------------------------------------------
81
2f1ae414
SC
82const short defwidth = 24 ;
83const short defheight = 22 ;
84
37e2cb08
SC
85wxToolBarToolBase *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
97wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
98{
99 return new wxToolBarTool(this, control);
100}
101
37e2cb08 102void wxToolBar::Init()
e9576ca5
SC
103{
104 m_maxWidth = -1;
105 m_maxHeight = -1;
2f1ae414
SC
106 m_defaultWidth = defwidth;
107 m_defaultHeight = defheight;
e9576ca5
SC
108 // TODO
109}
110
111bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
112 long style, const wxString& name)
113{
7c551d95 114 m_maxWidth = m_maxHeight = 0;
519cb848 115
2f1ae414
SC
116 m_defaultWidth = defwidth;
117 m_defaultHeight = defheight;
e9576ca5 118
519cb848
SC
119 int x = pos.x;
120 int y = pos.y;
121 int width = size.x;
122 int height = size.y;
e9576ca5 123
519cb848
SC
124 if (width <= 0)
125 width = 100;
126 if (height <= 0)
127 height = 30;
128 if (x < 0)
129 x = 0;
130 if (y < 0)
131 y = 0;
2f1ae414
SC
132#if 1
133 {
134 SetName(name);
135
136 m_windowStyle = style;
137 parent->AddChild(this);
138
139 m_backgroundColour = parent->GetBackgroundColour() ;
140 m_foregroundColour = parent->GetForegroundColour() ;
141
142 if (id == -1)
143 m_windowId = NewControlId();
144 else
145 m_windowId = id;
146
147 m_width = size.x ;
148 m_height = size.y ;
149 int x = pos.x ;
150 int y = pos.y ;
151 AdjustForParentClientOrigin(x, y, wxSIZE_USE_EXISTING);
152 m_x = x ;
153 m_y = y ;
154 }
155#else
519cb848
SC
156 Rect bounds ;
157 Str255 title ;
158
2f1ae414 159 MacPreControlCreate( parent , id , "" , wxPoint( x , y ) , wxSize( width , height ) ,style, wxDefaultValidator , name , &bounds , title ) ;
e9576ca5 160
519cb848
SC
161 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1,
162 kControlPlacardProc , (long) this ) ;
163 MacPostControlCreate() ;
2f1ae414 164#endif
519cb848 165 return TRUE;
e9576ca5
SC
166}
167
168wxToolBar::~wxToolBar()
169{
170 // TODO
171}
172
2f1ae414
SC
173PicHandle MakePict(GWorldPtr wp, GWorldPtr mask ) ;
174PicHandle MakePict(GWorldPtr wp, GWorldPtr mask )
519cb848
SC
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 } ;
2f1ae414
SC
182 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
183 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
519cb848 184
2f1ae414
SC
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 ;
519cb848 190
2f1ae414
SC
191 LockPixels( GetGWorldPixMap( wp ) ) ;
192 GetGWorld( &origPort , &origDev ) ;
519cb848 193
2f1ae414
SC
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 }
519cb848 212
519cb848
SC
213 SetGWorld( wp , NULL ) ;
214
2f1ae414 215 pict = OpenPicture(&portRect); // open a picture, this disables drawing
519cb848
SC
216 if(!pict)
217 return NULL;
2f1ae414
SC
218
219 RGBBackColor( &gray ) ;
220 RGBForeColor( &black ) ;
221 EraseRect(&portRect) ;
519cb848 222 RGBBackColor( &white ) ;
2f1ae414
SC
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 -
519cb848
SC
248 srcCopy,NULL); // copyMode and no clip region
249
2f1ae414 250 }
519cb848 251 ClosePicture(); // We are done recording the picture
2f1ae414 252 UnlockPixels( GetGWorldPixMap( wp ) ) ;
519cb848
SC
253 SetGWorld( origPort , origDev ) ;
254 return pict; // return our groovy pict handle
255}
256
257const short kwxMacToolBarTopMargin = 2 ;
258const short kwxMacToolBarLeftMargin = 2 ;
259
37e2cb08 260bool wxToolBar::Realize()
e9576ca5 261{
519cb848
SC
262 if (m_tools.Number() == 0)
263 return FALSE;
e9576ca5 264
519cb848
SC
265 Rect toolbarrect = { m_y , m_x , m_y + m_height , m_x + m_width } ;
266 ControlFontStyleRec controlstyle ;
267 WindowPtr window = GetMacRootWindow() ;
268 controlstyle.flags = kControlUseFontMask ;
269 controlstyle.font = kControlFontSmallSystemFont ;
270
271 wxNode *node = m_tools.First();
272 int noButtons = 0;
273 int x = 0 ;
7c551d95
SC
274 wxSize toolSize = GetToolSize() ;
275 int tw, th;
276 GetSize(& tw, & th);
519cb848
SC
277 while (node)
278 {
279 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
37e2cb08 280 wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->GetBitmap1().GetRefData()) ;
519cb848 281
2f1ae414 282 if( !tool->IsSeparator() )
519cb848
SC
283 {
284 Rect toolrect = { toolbarrect.top + kwxMacToolBarTopMargin , toolbarrect.left + x + kwxMacToolBarLeftMargin , 0 , 0 } ;
7c551d95
SC
285 toolrect.right = toolrect.left + toolSize.x ;
286 toolrect.bottom = toolrect.top + toolSize.y ;
519cb848
SC
287
288 PicHandle icon = NULL ;
289 if ( bmap )
290 {
291 if ( bmap->m_bitmapType == kMacBitmapTypePict )
292 icon = bmap->m_hPict ;
293 else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
294 {
2f1ae414
SC
295 if ( tool->GetBitmap1().GetMask() )
296 {
297 icon = MakePict( bmap->m_hBitmap , tool->GetBitmap1().GetMask()->GetMaskBitmap() ) ;
298 }
299 else
300 {
301 icon = MakePict( bmap->m_hBitmap , NULL ) ;
302 }
519cb848
SC
303 }
304 }
305
306 ControlHandle m_macToolHandle ;
2f1ae414 307
8208e181
SC
308 SInt16 behaviour = kControlBehaviorOffsetContents ;
309 if ( tool->CanBeToggled() )
310 behaviour += kControlBehaviorToggles ;
311
519cb848
SC
312 if ( icon )
313 {
314 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
8208e181 315 behaviour + kControlContentPictHandle , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
519cb848
SC
316 ControlButtonContentInfo info ;
317
318 info.contentType = kControlContentPictHandle ;
319 info.u.picture = icon ;
320
321 UMASetControlData( m_macToolHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
322 }
323 else
324 {
325 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
8208e181 326 behaviour , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
519cb848
SC
327 }
328 m_macToolHandles.Add( m_macToolHandle ) ;
2f1ae414
SC
329 tool->m_index = m_macToolHandles.Count() -1 ;
330 if ( !tool->IsEnabled() )
331 {
332 UMADeactivateControl( m_macToolHandle ) ;
333 }
334 if ( tool->CanBeToggled() && tool->IsToggled() )
335 {
336 ::SetControlValue( m_macToolHandle , 1 ) ;
337 }
519cb848 338 UMASetControlFontStyle( m_macToolHandle , &controlstyle ) ;
2f1ae414
SC
339 ControlHandle container = GetParent()->MacGetContainerForEmbedding() ;
340 wxASSERT_MSG( container != NULL , "No valid mac container control" ) ;
341 UMAEmbedControl( m_macToolHandle , container ) ;
519cb848 342
7c551d95 343 x += (int)toolSize.x;
519cb848
SC
344 noButtons ++;
345 }
346 else
347 {
348 m_macToolHandles.Add( NULL ) ;
7c551d95 349 x += (int)toolSize.x / 4;
519cb848 350 }
7c551d95
SC
351 if ( toolbarrect.left + x + kwxMacToolBarLeftMargin > m_maxWidth)
352 m_maxWidth = toolbarrect.left + x + kwxMacToolBarLeftMargin;
353 if (toolbarrect.top + kwxMacToolBarTopMargin + toolSize.y > m_maxHeight)
354 m_maxHeight = toolbarrect.top + kwxMacToolBarTopMargin ;
355
519cb848
SC
356 node = node->Next();
357 }
358
7c551d95
SC
359 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
360 {
361 m_maxWidth = tw ; // +=toolSize.x;
362 m_maxHeight += toolSize.y;
363 m_maxHeight += m_yMargin;
364 }
365 else
366 {
367 m_maxHeight = th ;// += toolSize.y;
368 m_maxWidth += toolSize.x;
369 m_maxWidth += m_xMargin;
370 }
371
372 SetSize(m_maxWidth, m_maxHeight);
373
519cb848 374 return TRUE;
e9576ca5
SC
375}
376
377void wxToolBar::SetToolBitmapSize(const wxSize& size)
378{
379 m_defaultWidth = size.x; m_defaultHeight = size.y;
e9576ca5
SC
380}
381
e9576ca5
SC
382// The button size is bigger than the bitmap size
383wxSize wxToolBar::GetToolSize() const
384{
2f1ae414 385 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
e9576ca5
SC
386}
387
519cb848
SC
388void wxToolBar::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
389{
390 int index = 0 ;
391 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
392 {
393 if ( m_macToolHandles[index] == (void*) control )
394 {
2f1ae414
SC
395 wxToolBarTool *tool = (wxToolBarTool *)m_tools.Nth( index )->Data();
396 if ( tool->CanBeToggled() )
397 {
398 tool->Toggle( GetControlValue( control ) ) ;
399 }
400 OnLeftClick( tool->GetId() , tool -> IsToggled() ) ;
401 break ;
519cb848
SC
402 }
403 }
404}
405
37e2cb08 406void wxToolBar::SetRows(int nRows)
e9576ca5 407{
37e2cb08 408 if ( nRows == m_maxRows )
e9576ca5 409 {
37e2cb08
SC
410 // avoid resizing the frame uselessly
411 return;
e9576ca5 412 }
37e2cb08
SC
413
414 m_maxRows = nRows;
e9576ca5
SC
415}
416
37e2cb08 417wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
e9576ca5 418{
37e2cb08 419 MacClientToRootWindow( &x , &y ) ;
2f1ae414 420 Point pt = { y ,x } ;
37e2cb08
SC
421
422 int index = 0 ;
423 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
424 {
2f1ae414 425 if ( m_macToolHandles[index] )
37e2cb08 426 {
2f1ae414
SC
427 Rect bounds ;
428 GetControlBounds((ControlHandle) m_macToolHandles[index], &bounds ) ;
429 if ( PtInRect( pt , &bounds ) )
430 {
431 return (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ;
432 }
37e2cb08
SC
433 }
434 }
435
436 return (wxToolBarToolBase *)NULL;
e9576ca5
SC
437}
438
2f1ae414
SC
439wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
440{
441 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
442 if ( tool )
443 {
444 return tool->GetShortHelp() ;
445 }
446 return "" ;
447}
448
37e2cb08 449void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
e9576ca5 450{
37e2cb08 451 wxToolBarTool *tool = (wxToolBarTool *)t;
2f1ae414
SC
452 if ( tool->m_index < 0 )
453 return ;
454
8208e181 455 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
2f1ae414
SC
456
457 if ( enable )
458 UMAActivateControl( control ) ;
8208e181 459 else
2f1ae414 460 UMADeactivateControl( control ) ;
e9576ca5
SC
461}
462
37e2cb08 463void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
e9576ca5 464{
37e2cb08 465 wxToolBarTool *tool = (wxToolBarTool *)t;
2f1ae414
SC
466 if ( tool->m_index < 0 )
467 return ;
8208e181
SC
468
469 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
470 ::SetControlValue( control , toggle ) ;
37e2cb08 471}
7c551d95 472
37e2cb08
SC
473bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
474 wxToolBarToolBase *tool)
475{
476 // nothing special to do here - we really create the toolbar buttons in
477 // Realize() later
478 tool->Attach(this);
7c551d95 479
37e2cb08
SC
480 return TRUE;
481}
e9576ca5 482
37e2cb08
SC
483void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle)
484{
485 wxToolBarTool *tool = (wxToolBarTool *)t;
486 // TODO: set toggle state
e9576ca5
SC
487}
488
37e2cb08
SC
489bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
490{
491 return TRUE ;
492}
2f1ae414
SC
493
494void wxToolBar::OnPaint(wxPaintEvent& event)
495{
496 WindowRef window = GetMacRootWindow() ;
497 if ( window )
498 {
499 wxWindow* win = wxFindWinFromMacWindow( window ) ;
500 if ( win )
501 {
502 wxMacDrawingHelper help( win ) ;
503 // the mac control manager always assumes to have the origin at 0,0
504 SetOrigin( 0 , 0 ) ;
505
506 bool hasTabBehind = false ;
507 wxWindow* parent = GetParent() ;
508 while ( parent )
509 {
510 if( parent->MacGetWindowData() )
511 {
512 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
513 break ;
514 }
515
516 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
517 {
518 if ( ((wxControl*)parent)->GetMacControl() )
519 SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ;
520 break ;
521 }
522
523 parent = parent->GetParent() ;
524 }
525 Rect toolbarrect = { m_y , m_x , m_y + m_height , m_x + m_width } ;
526
527 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
528 {
529 int index = 0 ;
530 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
531 {
532 if ( m_macToolHandles[index] )
533 {
534 UMADrawControl( (ControlHandle) m_macToolHandles[index] ) ;
535 }
536 }
537 }
538 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
539 }
540 }
541}
542void wxToolBar::OnMouse( wxMouseEvent &event )
543{
544
545 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
546 {
547
548 int x = event.m_x ;
549 int y = event.m_y ;
550
551 MacClientToRootWindow( &x , &y ) ;
552
553 ControlHandle control ;
554 Point localwhere ;
555 GrafPtr port ;
556 SInt16 controlpart ;
557 WindowRef window = GetMacRootWindow() ;
558
559 localwhere.h = x ;
560 localwhere.v = y ;
561
562 short modifiers = 0;
563
564 if ( !event.m_leftDown && !event.m_rightDown )
565 modifiers |= btnState ;
566
567 if ( event.m_shiftDown )
568 modifiers |= shiftKey ;
569
570 if ( event.m_controlDown )
571 modifiers |= controlKey ;
572
573 if ( event.m_altDown )
574 modifiers |= optionKey ;
575
576 if ( event.m_metaDown )
577 modifiers |= cmdKey ;
578
579 controlpart = FindControl( localwhere , window , &control ) ;
580 {
581 if ( AcceptsFocus() && FindFocus() != this )
582 {
583 SetFocus() ;
584 }
585 if ( control && UMAIsControlActive( control ) )
586 {
587 {
588 if ( controlpart == kControlIndicatorPart && !UMAHasAppearance() )
589 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) NULL ) ;
590 else
591 controlpart = UMAHandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
592 wxTheApp->s_lastMouseDown = 0 ;
593 if ( controlpart && ! ( ( UMAHasAppearance() || (controlpart != kControlIndicatorPart) )
594 && (IsKindOf( CLASSINFO( wxScrollBar ) ) ) ) ) // otherwise we will get the event twice
595 {
596 MacHandleControlClick( control , controlpart ) ;
597 }
598 }
599 }
600 }
601 }
602}
603
519cb848
SC
604#endif // wxUSE_TOOLBAR
605