]> git.saurik.com Git - wxWidgets.git/blame - src/mac/toolbar.cpp
fixed (rare but fatal) bug in wxWindowDisabler
[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$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "toolbar.h"
14#endif
15
16#include "wx/wx.h"
519cb848
SC
17
18#if wxUSE_TOOLBAR
19
e9576ca5
SC
20#include "wx/toolbar.h"
21
e9576ca5
SC
22IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
23
24BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
25END_EVENT_TABLE()
e9576ca5 26
519cb848
SC
27#include <wx/mac/uma.h>
28
37e2cb08
SC
29// ----------------------------------------------------------------------------
30// private classes
31// ----------------------------------------------------------------------------
32
33class wxToolBarTool : public wxToolBarToolBase
34{
35public:
36 wxToolBarTool(wxToolBar *tbar,
37 int id,
38 const wxBitmap& bitmap1,
39 const wxBitmap& bitmap2,
40 bool toggle,
41 wxObject *clientData,
42 const wxString& shortHelpString,
43 const wxString& longHelpString)
44 : wxToolBarToolBase(tbar, id, bitmap1, bitmap2, toggle,
45 clientData, shortHelpString, longHelpString)
46 {
47 m_nSepCount = 0;
48 m_index = 0 ;
49 }
50
51 wxToolBarTool(wxToolBar *tbar, wxControl *control)
52 : wxToolBarToolBase(tbar, control)
53 {
54 m_nSepCount = 1;
55 m_index = 0 ;
56 }
57
58 // set/get the number of separators which we use to cover the space used by
59 // a control in the toolbar
60 void SetSeparatorsCount(size_t count) { m_nSepCount = count; }
61 size_t GetSeparatorsCount() const { return m_nSepCount; }
62
63 int m_index ;
64private:
65 size_t m_nSepCount;
66};
67
68
69// ============================================================================
70// implementation
71// ============================================================================
72
73// ----------------------------------------------------------------------------
74// wxToolBarTool
75// ----------------------------------------------------------------------------
76
77wxToolBarToolBase *wxToolBar::CreateTool(int id,
78 const wxBitmap& bitmap1,
79 const wxBitmap& bitmap2,
80 bool toggle,
81 wxObject *clientData,
82 const wxString& shortHelpString,
83 const wxString& longHelpString)
84{
85 return new wxToolBarTool(this, id, bitmap1, bitmap2, toggle,
86 clientData, shortHelpString, longHelpString);
87}
88
89wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
90{
91 return new wxToolBarTool(this, control);
92}
93
94// ----------------------------------------------------------------------------
95// wxToolBar construction
96// ----------------------------------------------------------------------------
97
98void wxToolBar::Init()
e9576ca5
SC
99{
100 m_maxWidth = -1;
101 m_maxHeight = -1;
102 m_defaultWidth = 24;
103 m_defaultHeight = 22;
104 // TODO
105}
106
107bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
108 long style, const wxString& name)
109{
7c551d95 110 m_maxWidth = m_maxHeight = 0;
519cb848
SC
111
112 m_defaultWidth = 24;
113 m_defaultHeight = 22;
e9576ca5 114
519cb848
SC
115 int x = pos.x;
116 int y = pos.y;
117 int width = size.x;
118 int height = size.y;
e9576ca5 119
519cb848
SC
120 if (width <= 0)
121 width = 100;
122 if (height <= 0)
123 height = 30;
124 if (x < 0)
125 x = 0;
126 if (y < 0)
127 y = 0;
e9576ca5 128
519cb848
SC
129 Rect bounds ;
130 Str255 title ;
131
132 MacPreControlCreate( parent , id , "" , wxPoint( x , y ) , wxSize( width , height ) ,style, *((wxValidator*)NULL) , name , &bounds , title ) ;
e9576ca5 133
519cb848
SC
134 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1,
135 kControlPlacardProc , (long) this ) ;
136 MacPostControlCreate() ;
e9576ca5 137
519cb848 138 return TRUE;
e9576ca5
SC
139}
140
141wxToolBar::~wxToolBar()
142{
143 // TODO
144}
145
519cb848
SC
146PicHandle MakePict(GWorldPtr wp)
147{
148 CGrafPtr origPort ;
149 GDHandle origDev ;
150
151 PicHandle pict; // this is the Picture we give back
152
153 RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ;
154
155 GetGWorld( &origPort , &origDev ) ;
156 SetGWorld( wp , NULL ) ;
157
158 pict = OpenPicture(&wp->portRect); // open a picture, this disables drawing
159 if(!pict)
160 return NULL;
161
162 RGBBackColor( &gray ) ;
163 EraseRect(&wp->portRect) ;
164 CopyBits((BitMap*)*wp->portPixMap, // src PixMap - we copy image over itself -
165 (BitMap*)*wp->portPixMap, // dst PixMap - no drawing occurs -
166 &wp->portRect, // srcRect - it will be recorded and compressed -
167 &wp->portRect, // dstRect - into the picture that is open -
168 srcCopy,NULL); // copyMode and no clip region
169
170 ClosePicture(); // We are done recording the picture
171 SetGWorld( origPort , origDev ) ;
172 return pict; // return our groovy pict handle
173}
174
175PicHandle MakePictWhite(GWorldPtr wp)
176{
177 CGrafPtr origPort ;
178 GDHandle origDev ;
179
180 PicHandle pict; // this is the Picture we give back
181
182 RGBColor white = { 0xFFFF ,0xFFFF , 0xFFFF } ;
183
184 GetGWorld( &origPort , &origDev ) ;
185 SetGWorld( wp , NULL ) ;
186
187 pict = OpenPicture(&wp->portRect); // open a picture, this disables drawing
188 if(!pict)
189 return NULL;
190
191 RGBBackColor( &white ) ;
192 EraseRect(&wp->portRect) ;
193 CopyBits((BitMap*)*wp->portPixMap, // src PixMap - we copy image over itself -
194 (BitMap*)*wp->portPixMap, // dst PixMap - no drawing occurs -
195 &wp->portRect, // srcRect - it will be recorded and compressed -
196 &wp->portRect, // dstRect - into the picture that is open -
197 srcCopy,NULL); // copyMode and no clip region
198
199 ClosePicture(); // We are done recording the picture
200 SetGWorld( origPort , origDev ) ;
201 return pict; // return our groovy pict handle
202}
203
204const short kwxMacToolBarTopMargin = 2 ;
205const short kwxMacToolBarLeftMargin = 2 ;
206
37e2cb08 207bool wxToolBar::Realize()
e9576ca5 208{
519cb848
SC
209 if (m_tools.Number() == 0)
210 return FALSE;
e9576ca5 211
519cb848
SC
212 Rect toolbarrect = { m_y , m_x , m_y + m_height , m_x + m_width } ;
213 ControlFontStyleRec controlstyle ;
214 WindowPtr window = GetMacRootWindow() ;
215 controlstyle.flags = kControlUseFontMask ;
216 controlstyle.font = kControlFontSmallSystemFont ;
217
218 wxNode *node = m_tools.First();
219 int noButtons = 0;
220 int x = 0 ;
7c551d95
SC
221 wxSize toolSize = GetToolSize() ;
222 int tw, th;
223 GetSize(& tw, & th);
519cb848
SC
224 while (node)
225 {
226 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
37e2cb08 227 wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->GetBitmap1().GetRefData()) ;
519cb848 228
37e2cb08 229 if( !tool->IsSeparator() )
519cb848
SC
230 {
231 Rect toolrect = { toolbarrect.top + kwxMacToolBarTopMargin , toolbarrect.left + x + kwxMacToolBarLeftMargin , 0 , 0 } ;
7c551d95
SC
232 toolrect.right = toolrect.left + toolSize.x ;
233 toolrect.bottom = toolrect.top + toolSize.y ;
519cb848
SC
234
235 PicHandle icon = NULL ;
236 if ( bmap )
237 {
238 if ( bmap->m_bitmapType == kMacBitmapTypePict )
239 icon = bmap->m_hPict ;
240 else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
241 {
242 icon = MakePict( bmap->m_hBitmap ) ;
243 }
244 }
245
246 ControlHandle m_macToolHandle ;
247
8208e181
SC
248 SInt16 behaviour = kControlBehaviorOffsetContents ;
249 if ( tool->CanBeToggled() )
250 behaviour += kControlBehaviorToggles ;
251
519cb848
SC
252 if ( icon )
253 {
254 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
8208e181 255 behaviour + kControlContentPictHandle , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
519cb848
SC
256 ControlButtonContentInfo info ;
257
258 info.contentType = kControlContentPictHandle ;
259 info.u.picture = icon ;
260
261 UMASetControlData( m_macToolHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
262 }
263 else
264 {
265 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
8208e181 266 behaviour , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
519cb848
SC
267 }
268 m_macToolHandles.Add( m_macToolHandle ) ;
269 UMASetControlFontStyle( m_macToolHandle , &controlstyle ) ;
270 UMAEmbedControl( m_macToolHandle , m_macControl ) ;
271
7c551d95 272 x += (int)toolSize.x;
519cb848
SC
273 noButtons ++;
274 }
275 else
276 {
277 m_macToolHandles.Add( NULL ) ;
7c551d95 278 x += (int)toolSize.x / 4;
519cb848 279 }
7c551d95
SC
280 if ( toolbarrect.left + x + kwxMacToolBarLeftMargin > m_maxWidth)
281 m_maxWidth = toolbarrect.left + x + kwxMacToolBarLeftMargin;
282 if (toolbarrect.top + kwxMacToolBarTopMargin + toolSize.y > m_maxHeight)
283 m_maxHeight = toolbarrect.top + kwxMacToolBarTopMargin ;
284
519cb848
SC
285 node = node->Next();
286 }
287
7c551d95
SC
288 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
289 {
290 m_maxWidth = tw ; // +=toolSize.x;
291 m_maxHeight += toolSize.y;
292 m_maxHeight += m_yMargin;
293 }
294 else
295 {
296 m_maxHeight = th ;// += toolSize.y;
297 m_maxWidth += toolSize.x;
298 m_maxWidth += m_xMargin;
299 }
300
301 SetSize(m_maxWidth, m_maxHeight);
302
519cb848 303 return TRUE;
e9576ca5
SC
304}
305
306void wxToolBar::SetToolBitmapSize(const wxSize& size)
307{
308 m_defaultWidth = size.x; m_defaultHeight = size.y;
e9576ca5
SC
309}
310
e9576ca5
SC
311// The button size is bigger than the bitmap size
312wxSize wxToolBar::GetToolSize() const
313{
e9576ca5
SC
314 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
315}
316
519cb848
SC
317void wxToolBar::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
318{
319 int index = 0 ;
320 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
321 {
322 if ( m_macToolHandles[index] == (void*) control )
323 {
37e2cb08 324 OnLeftClick( ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_index , ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->IsToggled() ) ;
519cb848
SC
325 }
326 }
327}
328
37e2cb08 329void wxToolBar::SetRows(int nRows)
e9576ca5 330{
37e2cb08 331 if ( nRows == m_maxRows )
e9576ca5 332 {
37e2cb08
SC
333 // avoid resizing the frame uselessly
334 return;
e9576ca5 335 }
37e2cb08
SC
336
337 m_maxRows = nRows;
e9576ca5
SC
338}
339
37e2cb08 340wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
e9576ca5 341{
37e2cb08
SC
342 MacClientToRootWindow( &x , &y ) ;
343 Point pt = { x ,y } ;
344
345 int index = 0 ;
346 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
347 {
348 if ( PtInRect( pt , &(**(ControlHandle)(m_macToolHandles[index])).contrlRect) )
349 {
350 return (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ;
351 }
352 }
353
354 return (wxToolBarToolBase *)NULL;
e9576ca5
SC
355}
356
37e2cb08 357void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
e9576ca5 358{
37e2cb08 359 wxToolBarTool *tool = (wxToolBarTool *)t;
8208e181
SC
360 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
361 if ( UMAHasAppearance() )
362 {
363 if ( enable )
364 ::ActivateControl( control ) ;
365 else
366 ::DeactivateControl( control ) ;
367 }
368 else
369 {
370 if ( enable )
371 ::HiliteControl( control , 0 ) ;
372 else
373 ::HiliteControl( control , 255 ) ;
374 }
e9576ca5
SC
375}
376
37e2cb08 377void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
e9576ca5 378{
37e2cb08 379 wxToolBarTool *tool = (wxToolBarTool *)t;
8208e181
SC
380
381 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
382 ::SetControlValue( control , toggle ) ;
37e2cb08 383}
7c551d95 384
37e2cb08
SC
385bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
386 wxToolBarToolBase *tool)
387{
388 // nothing special to do here - we really create the toolbar buttons in
389 // Realize() later
390 tool->Attach(this);
7c551d95 391
37e2cb08
SC
392 return TRUE;
393}
e9576ca5 394
37e2cb08
SC
395void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle)
396{
397 wxToolBarTool *tool = (wxToolBarTool *)t;
398 // TODO: set toggle state
e9576ca5
SC
399}
400
37e2cb08
SC
401bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
402{
403 return TRUE ;
404}
519cb848
SC
405#endif // wxUSE_TOOLBAR
406