]>
Commit | Line | Data |
---|---|---|
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 |
22 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase) |
23 | ||
24 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
25 | END_EVENT_TABLE() | |
e9576ca5 | 26 | |
519cb848 SC |
27 | #include <wx/mac/uma.h> |
28 | ||
37e2cb08 SC |
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() | |
e9576ca5 SC |
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 | { | |
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 | ||
141 | wxToolBar::~wxToolBar() | |
142 | { | |
143 | // TODO | |
144 | } | |
145 | ||
519cb848 SC |
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 | ||
37e2cb08 | 207 | bool 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 | ||
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 | ||
7c551d95 | 268 | x += (int)toolSize.x; |
519cb848 SC |
269 | noButtons ++; |
270 | } | |
271 | else | |
272 | { | |
273 | m_macToolHandles.Add( NULL ) ; | |
7c551d95 | 274 | x += (int)toolSize.x / 4; |
519cb848 | 275 | } |
7c551d95 SC |
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 | ||
519cb848 SC |
281 | node = node->Next(); |
282 | } | |
283 | ||
7c551d95 SC |
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 | ||
519cb848 | 299 | return TRUE; |
e9576ca5 SC |
300 | } |
301 | ||
302 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
303 | { | |
304 | m_defaultWidth = size.x; m_defaultHeight = size.y; | |
e9576ca5 SC |
305 | } |
306 | ||
e9576ca5 SC |
307 | // The button size is bigger than the bitmap size |
308 | wxSize wxToolBar::GetToolSize() const | |
309 | { | |
e9576ca5 SC |
310 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); |
311 | } | |
312 | ||
519cb848 SC |
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 | { | |
37e2cb08 | 320 | OnLeftClick( ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_index , ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->IsToggled() ) ; |
519cb848 SC |
321 | } |
322 | } | |
323 | } | |
324 | ||
37e2cb08 | 325 | void wxToolBar::SetRows(int nRows) |
e9576ca5 | 326 | { |
37e2cb08 | 327 | if ( nRows == m_maxRows ) |
e9576ca5 | 328 | { |
37e2cb08 SC |
329 | // avoid resizing the frame uselessly |
330 | return; | |
e9576ca5 | 331 | } |
37e2cb08 SC |
332 | |
333 | m_maxRows = nRows; | |
e9576ca5 SC |
334 | } |
335 | ||
37e2cb08 | 336 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
e9576ca5 | 337 | { |
37e2cb08 SC |
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; | |
e9576ca5 SC |
351 | } |
352 | ||
37e2cb08 | 353 | void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) |
e9576ca5 | 354 | { |
37e2cb08 | 355 | wxToolBarTool *tool = (wxToolBarTool *)t; |
e9576ca5 SC |
356 | } |
357 | ||
37e2cb08 | 358 | void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) |
e9576ca5 | 359 | { |
37e2cb08 SC |
360 | wxToolBarTool *tool = (wxToolBarTool *)t; |
361 | // TODO: set toggle state | |
362 | } | |
7c551d95 | 363 | |
37e2cb08 SC |
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); | |
7c551d95 | 370 | |
37e2cb08 SC |
371 | return TRUE; |
372 | } | |
e9576ca5 | 373 | |
37e2cb08 SC |
374 | void wxToolBar::DoSetToggle(wxToolBarToolBase *t, bool toggle) |
375 | { | |
376 | wxToolBarTool *tool = (wxToolBarTool *)t; | |
377 | // TODO: set toggle state | |
e9576ca5 SC |
378 | } |
379 | ||
37e2cb08 SC |
380 | bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool) |
381 | { | |
382 | return TRUE ; | |
383 | } | |
519cb848 SC |
384 | #endif // wxUSE_TOOLBAR |
385 |