]> git.saurik.com Git - wxWidgets.git/blob - src/mac/toolbar.cpp
26f3a748cb6d47e1063ddb7751e473587f06cce4
[wxWidgets.git] / src / mac / 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 if ( icon )
249 {
250 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
251 kControlBehaviorOffsetContents + kControlContentPictHandle , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
252 ControlButtonContentInfo info ;
253
254 info.contentType = kControlContentPictHandle ;
255 info.u.picture = icon ;
256
257 UMASetControlData( m_macToolHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
258 }
259 else
260 {
261 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
262 kControlBehaviorOffsetContents , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
263 }
264 m_macToolHandles.Add( m_macToolHandle ) ;
265 UMASetControlFontStyle( m_macToolHandle , &controlstyle ) ;
266 UMAEmbedControl( m_macToolHandle , m_macControl ) ;
267
268 x += (int)toolSize.x;
269 noButtons ++;
270 }
271 else
272 {
273 m_macToolHandles.Add( NULL ) ;
274 x += (int)toolSize.x / 4;
275 }
276 if ( toolbarrect.left + x + kwxMacToolBarLeftMargin > m_maxWidth)
277 m_maxWidth = toolbarrect.left + x + kwxMacToolBarLeftMargin;
278 if (toolbarrect.top + kwxMacToolBarTopMargin + toolSize.y > m_maxHeight)
279 m_maxHeight = toolbarrect.top + kwxMacToolBarTopMargin ;
280
281 node = node->Next();
282 }
283
284 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
285 {
286 m_maxWidth = tw ; // +=toolSize.x;
287 m_maxHeight += toolSize.y;
288 m_maxHeight += m_yMargin;
289 }
290 else
291 {
292 m_maxHeight = th ;// += toolSize.y;
293 m_maxWidth += toolSize.x;
294 m_maxWidth += m_xMargin;
295 }
296
297 SetSize(m_maxWidth, m_maxHeight);
298
299 return TRUE;
300 }
301
302 void wxToolBar::SetToolBitmapSize(const wxSize& size)
303 {
304 m_defaultWidth = size.x; m_defaultHeight = size.y;
305 }
306
307 // The button size is bigger than the bitmap size
308 wxSize wxToolBar::GetToolSize() const
309 {
310 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
311 }
312
313 void wxToolBar::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
314 {
315 int index = 0 ;
316 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
317 {
318 if ( m_macToolHandles[index] == (void*) control )
319 {
320 OnLeftClick( ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_index , ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->IsToggled() ) ;
321 }
322 }
323 }
324
325 void wxToolBar::SetRows(int nRows)
326 {
327 if ( nRows == m_maxRows )
328 {
329 // avoid resizing the frame uselessly
330 return;
331 }
332
333 m_maxRows = nRows;
334 }
335
336 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
337 {
338 MacClientToRootWindow( &x , &y ) ;
339 Point pt = { x ,y } ;
340
341 int index = 0 ;
342 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
343 {
344 if ( PtInRect( pt , &(**(ControlHandle)(m_macToolHandles[index])).contrlRect) )
345 {
346 return (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ;
347 }
348 }
349
350 return (wxToolBarToolBase *)NULL;
351 }
352
353 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
354 {
355 wxToolBarTool *tool = (wxToolBarTool *)t;
356 }
357
358 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
359 {
360 wxToolBarTool *tool = (wxToolBarTool *)t;
361 // TODO: set toggle state
362 }
363
364 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
365 wxToolBarToolBase *tool)
366 {
367 // nothing special to do here - we really create the toolbar buttons in
368 // Realize() later
369 tool->Attach(this);
370
371 return TRUE;
372 }
373
374 void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle)
375 {
376 wxToolBarTool *tool = (wxToolBarTool *)t;
377 // TODO: set toggle state
378 }
379
380 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
381 {
382 return TRUE ;
383 }
384 #endif // wxUSE_TOOLBAR
385