]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
added default ctor for wxDirDialog
[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 #if !USE_SHARED_LIBRARY
23 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
24
25 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
26 END_EVENT_TABLE()
27 #endif
28
29 #include <wx/mac/uma.h>
30
31 wxToolBar::wxToolBar()
32 {
33 m_maxWidth = -1;
34 m_maxHeight = -1;
35 m_defaultWidth = 24;
36 m_defaultHeight = 22;
37 // TODO
38 }
39
40 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
41 long style, const wxString& name)
42 {
43 m_maxWidth = m_maxHeight = 0;
44
45 m_defaultWidth = 24;
46 m_defaultHeight = 22;
47
48 int x = pos.x;
49 int y = pos.y;
50 int width = size.x;
51 int height = size.y;
52
53 if (width <= 0)
54 width = 100;
55 if (height <= 0)
56 height = 30;
57 if (x < 0)
58 x = 0;
59 if (y < 0)
60 y = 0;
61
62 Rect bounds ;
63 Str255 title ;
64
65 MacPreControlCreate( parent , id , "" , wxPoint( x , y ) , wxSize( width , height ) ,style, *((wxValidator*)NULL) , name , &bounds , title ) ;
66
67 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1,
68 kControlPlacardProc , (long) this ) ;
69 MacPostControlCreate() ;
70
71 return TRUE;
72 }
73
74 wxToolBar::~wxToolBar()
75 {
76 // TODO
77 }
78
79 PicHandle MakePict(GWorldPtr wp)
80 {
81 CGrafPtr origPort ;
82 GDHandle origDev ;
83
84 PicHandle pict; // this is the Picture we give back
85
86 RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ;
87
88 GetGWorld( &origPort , &origDev ) ;
89 SetGWorld( wp , NULL ) ;
90
91 pict = OpenPicture(&wp->portRect); // open a picture, this disables drawing
92 if(!pict)
93 return NULL;
94
95 RGBBackColor( &gray ) ;
96 EraseRect(&wp->portRect) ;
97 CopyBits((BitMap*)*wp->portPixMap, // src PixMap - we copy image over itself -
98 (BitMap*)*wp->portPixMap, // dst PixMap - no drawing occurs -
99 &wp->portRect, // srcRect - it will be recorded and compressed -
100 &wp->portRect, // dstRect - into the picture that is open -
101 srcCopy,NULL); // copyMode and no clip region
102
103 ClosePicture(); // We are done recording the picture
104 SetGWorld( origPort , origDev ) ;
105 return pict; // return our groovy pict handle
106 }
107
108 PicHandle MakePictWhite(GWorldPtr wp)
109 {
110 CGrafPtr origPort ;
111 GDHandle origDev ;
112
113 PicHandle pict; // this is the Picture we give back
114
115 RGBColor white = { 0xFFFF ,0xFFFF , 0xFFFF } ;
116
117 GetGWorld( &origPort , &origDev ) ;
118 SetGWorld( wp , NULL ) ;
119
120 pict = OpenPicture(&wp->portRect); // open a picture, this disables drawing
121 if(!pict)
122 return NULL;
123
124 RGBBackColor( &white ) ;
125 EraseRect(&wp->portRect) ;
126 CopyBits((BitMap*)*wp->portPixMap, // src PixMap - we copy image over itself -
127 (BitMap*)*wp->portPixMap, // dst PixMap - no drawing occurs -
128 &wp->portRect, // srcRect - it will be recorded and compressed -
129 &wp->portRect, // dstRect - into the picture that is open -
130 srcCopy,NULL); // copyMode and no clip region
131
132 ClosePicture(); // We are done recording the picture
133 SetGWorld( origPort , origDev ) ;
134 return pict; // return our groovy pict handle
135 }
136
137 const short kwxMacToolBarTopMargin = 2 ;
138 const short kwxMacToolBarLeftMargin = 2 ;
139
140 bool wxToolBar::CreateTools()
141 {
142 if (m_tools.Number() == 0)
143 return FALSE;
144
145 Rect toolbarrect = { m_y , m_x , m_y + m_height , m_x + m_width } ;
146 ControlFontStyleRec controlstyle ;
147 WindowPtr window = GetMacRootWindow() ;
148 controlstyle.flags = kControlUseFontMask ;
149 controlstyle.font = kControlFontSmallSystemFont ;
150
151 wxNode *node = m_tools.First();
152 int noButtons = 0;
153 int x = 0 ;
154 wxSize toolSize = GetToolSize() ;
155 int tw, th;
156 GetSize(& tw, & th);
157 while (node)
158 {
159 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
160 wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->m_bitmap1.GetRefData()) ;
161
162 if( tool->m_toolStyle != wxTOOL_STYLE_SEPARATOR )
163 {
164 Rect toolrect = { toolbarrect.top + kwxMacToolBarTopMargin , toolbarrect.left + x + kwxMacToolBarLeftMargin , 0 , 0 } ;
165 toolrect.right = toolrect.left + toolSize.x ;
166 toolrect.bottom = toolrect.top + toolSize.y ;
167
168 PicHandle icon = NULL ;
169 if ( bmap )
170 {
171 if ( bmap->m_bitmapType == kMacBitmapTypePict )
172 icon = bmap->m_hPict ;
173 else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
174 {
175 icon = MakePict( bmap->m_hBitmap ) ;
176 }
177 }
178
179 ControlHandle m_macToolHandle ;
180
181 if ( icon )
182 {
183 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
184 kControlBehaviorOffsetContents + kControlContentPictHandle , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
185 ControlButtonContentInfo info ;
186
187 info.contentType = kControlContentPictHandle ;
188 info.u.picture = icon ;
189
190 UMASetControlData( m_macToolHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
191 }
192 else
193 {
194 m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 ,
195 kControlBehaviorOffsetContents , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
196 }
197 m_macToolHandles.Add( m_macToolHandle ) ;
198 UMASetControlFontStyle( m_macToolHandle , &controlstyle ) ;
199 UMAEmbedControl( m_macToolHandle , m_macControl ) ;
200
201 x += (int)toolSize.x;
202 noButtons ++;
203 }
204 else
205 {
206 m_macToolHandles.Add( NULL ) ;
207 x += (int)toolSize.x / 4;
208 }
209 if ( toolbarrect.left + x + kwxMacToolBarLeftMargin > m_maxWidth)
210 m_maxWidth = toolbarrect.left + x + kwxMacToolBarLeftMargin;
211 if (toolbarrect.top + kwxMacToolBarTopMargin + toolSize.y > m_maxHeight)
212 m_maxHeight = toolbarrect.top + kwxMacToolBarTopMargin ;
213
214 node = node->Next();
215 }
216
217 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
218 {
219 m_maxWidth = tw ; // +=toolSize.x;
220 m_maxHeight += toolSize.y;
221 m_maxHeight += m_yMargin;
222 }
223 else
224 {
225 m_maxHeight = th ;// += toolSize.y;
226 m_maxWidth += toolSize.x;
227 m_maxWidth += m_xMargin;
228 }
229
230 SetSize(m_maxWidth, m_maxHeight);
231
232 return TRUE;
233 }
234
235 void wxToolBar::SetToolBitmapSize(const wxSize& size)
236 {
237 m_defaultWidth = size.x; m_defaultHeight = size.y;
238 }
239
240 wxSize wxToolBar::GetMaxSize() const
241 {
242 return wxSize(m_maxWidth, m_maxHeight);
243 }
244
245 // The button size is bigger than the bitmap size
246 wxSize wxToolBar::GetToolSize() const
247 {
248 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
249 }
250
251 void wxToolBar::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
252 {
253 int index = 0 ;
254 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
255 {
256 if ( m_macToolHandles[index] == (void*) control )
257 {
258 OnLeftClick( ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_index , ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_toggleState ) ;
259 }
260 }
261 }
262
263 void wxToolBar::EnableTool(int toolIndex, bool enable)
264 {
265 wxNode *node = m_tools.Find((long)toolIndex);
266 if (node)
267 {
268 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
269 tool->m_enabled = enable;
270 // TODO enable button
271 }
272 }
273
274 void wxToolBar::ToggleTool(int toolIndex, bool toggle)
275 {
276 wxNode *node = m_tools.Find((long)toolIndex);
277 if (node)
278 {
279 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
280 if (tool->m_isToggle)
281 {
282 tool->m_toggleState = toggle;
283 // TODO: set toggle state
284 }
285 }
286 }
287
288 void wxToolBar::ClearTools()
289 {
290 // TODO
291 wxToolBarBase::ClearTools();
292 }
293
294 // If pushedBitmap is NULL, a reversed version of bitmap is
295 // created and used as the pushed/toggled image.
296 // If toggle is TRUE, the button toggles between the two states.
297
298 wxToolBarTool *wxToolBar::AddTool(int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap,
299 bool toggle, long xPos, long yPos, wxObject *clientData, const wxString& helpString1, const wxString& helpString2)
300 {
301 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap, toggle, xPos, yPos, helpString1, helpString2);
302 tool->m_clientData = clientData;
303
304 if (xPos > -1)
305 tool->m_x = xPos;
306 else
307 tool->m_x = m_xMargin;
308
309 if (yPos > -1)
310 tool->m_y = yPos;
311 else
312 tool->m_y = m_yMargin;
313
314 tool->SetSize(GetToolSize().x, GetToolSize().y);
315
316 if ((tool->m_x + bitmap.GetWidth() + m_xMargin) > m_maxWidth)
317 m_maxWidth = (tool->m_x + tool->GetWidth() + m_xMargin);
318
319 if ((tool->m_y + bitmap.GetHeight() + m_yMargin) > m_maxHeight)
320 m_maxHeight = (tool->m_y + tool->GetHeight() + m_yMargin);
321
322 m_tools.Append((long)index, tool);
323 return tool;
324 }
325
326 #endif // wxUSE_TOOLBAR
327