]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
fixed refresh problem due to rounding errors
[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) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "toolbar.h"
14 #endif
15
16 #include "wx/wx.h"
17
18 #if wxUSE_TOOLBAR
19
20 #include "wx/toolbar.h"
21
22 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
23
24 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
25 END_EVENT_TABLE()
26
27 #include <wx/mac/uma.h>
28
29 // ----------------------------------------------------------------------------
30 // private classes
31 // ----------------------------------------------------------------------------
32
33 class wxToolBarTool : public wxToolBarToolBase
34 {
35 public:
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 ;
64 private:
65 size_t m_nSepCount;
66 };
67
68
69 // ============================================================================
70 // implementation
71 // ============================================================================
72
73 // ----------------------------------------------------------------------------
74 // wxToolBarTool
75 // ----------------------------------------------------------------------------
76
77 wxToolBarToolBase *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
89 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
90 {
91 return new wxToolBarTool(this, control);
92 }
93
94 // ----------------------------------------------------------------------------
95 // wxToolBar construction
96 // ----------------------------------------------------------------------------
97
98 void wxToolBar::Init()
99 {
100 m_maxWidth = -1;
101 m_maxHeight = -1;
102 m_defaultWidth = 24;
103 m_defaultHeight = 22;
104 // TODO
105 }
106
107 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
108 long style, const wxString& name)
109 {
110 m_maxWidth = m_maxHeight = 0;
111
112 m_defaultWidth = 24;
113 m_defaultHeight = 22;
114
115 int x = pos.x;
116 int y = pos.y;
117 int width = size.x;
118 int height = size.y;
119
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;
128
129 Rect bounds ;
130 Str255 title ;
131
132 MacPreControlCreate( parent , id , "" , wxPoint( x , y ) , wxSize( width , height ) ,style, *((wxValidator*)NULL) , name , &bounds , title ) ;
133
134 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1,
135 kControlPlacardProc , (long) this ) ;
136 MacPostControlCreate() ;
137
138 return TRUE;
139 }
140
141 wxToolBar::~wxToolBar()
142 {
143 // TODO
144 }
145
146 PicHandle 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
175 PicHandle 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
204 const short kwxMacToolBarTopMargin = 2 ;
205 const short kwxMacToolBarLeftMargin = 2 ;
206
207 bool wxToolBar::Realize()
208 {
209 if (m_tools.Number() == 0)
210 return FALSE;
211
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 ;
221 wxSize toolSize = GetToolSize() ;
222 int tw, th;
223 GetSize(& tw, & th);
224 while (node)
225 {
226 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
227 wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->GetBitmap1().GetRefData()) ;
228
229 if( !tool->IsSeparator() )
230 {
231 Rect toolrect = { toolbarrect.top + kwxMacToolBarTopMargin , toolbarrect.left + x + kwxMacToolBarLeftMargin , 0 , 0 } ;
232 toolrect.right = toolrect.left + toolSize.x ;
233 toolrect.bottom = toolrect.top + toolSize.y ;
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
248 SInt16 behaviour = kControlBehaviorOffsetContents ;
249 if ( tool->CanBeToggled() )
250 behaviour += kControlBehaviorToggles ;
251
252 if ( icon )
253 {
254 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
255 behaviour + kControlContentPictHandle , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
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 ,
266 behaviour , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
267 }
268 m_macToolHandles.Add( m_macToolHandle ) ;
269 UMASetControlFontStyle( m_macToolHandle , &controlstyle ) ;
270 UMAEmbedControl( m_macToolHandle , m_macControl ) ;
271
272 x += (int)toolSize.x;
273 noButtons ++;
274 }
275 else
276 {
277 m_macToolHandles.Add( NULL ) ;
278 x += (int)toolSize.x / 4;
279 }
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
285 node = node->Next();
286 }
287
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
303 return TRUE;
304 }
305
306 void wxToolBar::SetToolBitmapSize(const wxSize& size)
307 {
308 m_defaultWidth = size.x; m_defaultHeight = size.y;
309 }
310
311 // The button size is bigger than the bitmap size
312 wxSize wxToolBar::GetToolSize() const
313 {
314 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
315 }
316
317 void 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 {
324 OnLeftClick( ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_index , ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->IsToggled() ) ;
325 }
326 }
327 }
328
329 void wxToolBar::SetRows(int nRows)
330 {
331 if ( nRows == m_maxRows )
332 {
333 // avoid resizing the frame uselessly
334 return;
335 }
336
337 m_maxRows = nRows;
338 }
339
340 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
341 {
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;
355 }
356
357 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
358 {
359 wxToolBarTool *tool = (wxToolBarTool *)t;
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 }
375 }
376
377 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
378 {
379 wxToolBarTool *tool = (wxToolBarTool *)t;
380
381 ControlHandle control = (ControlHandle) m_macToolHandles[ tool->m_index ] ;
382 ::SetControlValue( control , toggle ) ;
383 }
384
385 bool 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);
391
392 return TRUE;
393 }
394
395 void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle)
396 {
397 wxToolBarTool *tool = (wxToolBarTool *)t;
398 // TODO: set toggle state
399 }
400
401 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
402 {
403 return TRUE ;
404 }
405 #endif // wxUSE_TOOLBAR
406