Commented out the bitmap-greying code until I have time to debug it
[wxWidgets.git] / src / univ / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/toolbar.cpp
3 // Author: Robert Roebling
4 // Id: $Id$
5 // Copyright: (c) 2001 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #ifdef __GNUG__
18 #pragma implementation "univtoolbar.h"
19 #endif
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #ifndef WX_PRECOMP
29 #include "wx/utils.h"
30 #include "wx/app.h"
31 #endif
32
33 #include "wx/toolbar.h"
34 #include "wx/image.h"
35
36 //-----------------------------------------------------------------------------
37 // wxToolBar
38 //-----------------------------------------------------------------------------
39
40 BEGIN_EVENT_TABLE(wxToolBar,wxToolBarBase)
41 EVT_MOUSE_EVENTS( wxToolBar::OnMouse )
42 EVT_PAINT( wxToolBar::OnPaint )
43 EVT_SIZE( wxToolBar::OnSize )
44 END_EVENT_TABLE()
45
46 bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
47 const wxPoint& pos, const wxSize& size,
48 long style, const wxString& name )
49 {
50 bool ret = wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name );
51
52 return ret;
53 }
54
55 void wxToolBar::Init()
56 {
57 // TODO: this is from tbarbase.cpp, but should be in
58 // wxToolbarBase::Init
59 // the list owns the pointers
60 m_tools.DeleteContents(TRUE);
61 m_xMargin = m_yMargin = 0;
62 m_maxRows = m_maxCols = 0;
63 // End TODO
64
65 m_maxWidth = 0;
66 m_maxHeight = 0;
67
68 m_captured = NULL;
69 SetToolBitmapSize( wxSize(16,15) );
70 }
71
72 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
73 {
74 return NULL;
75 }
76
77 void wxToolBar::SetToolShortHelp(int id, const wxString& helpString)
78 {
79 }
80
81 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *tool)
82 {
83 return TRUE;
84 }
85
86 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
87 {
88 return TRUE;
89 }
90
91 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
92 {
93 // TODO: figure out why it gives a BadMatch when
94 // creating the bitmap (out.ConvertToBitmap).
95 // Possibly we should ensure all depths match.
96 #if 0
97 // Created disabled-state bitmap on demand
98 if (!enable && !tool->GetBitmap2().Ok())
99 {
100 wxImage in(tool->GetBitmap1());
101 wxImage out(tool->GetBitmap1());
102
103 wxCreateGreyedImage(in, out);
104
105 tool->SetBitmap2(out.ConvertToBitmap());
106 }
107 RefreshTool((wxToolBarTool*) tool);
108 #endif
109 }
110
111 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
112 {
113 wxToolBarTool *my_tool = (wxToolBarTool*) tool;
114
115 bool refresh = (my_tool->IsToggled() != toggle);
116
117 my_tool->m_isDown = toggle;
118
119 if (refresh)
120 RefreshTool( my_tool );
121 }
122
123 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool toggle)
124 {
125 }
126
127 wxToolBarToolBase *wxToolBar::CreateTool(int id,
128 const wxBitmap& bitmap1,
129 const wxBitmap& bitmap2,
130 bool toggle,
131 wxObject *clientData,
132 const wxString& shortHelpString,
133 const wxString& longHelpString)
134 {
135 return new wxToolBarTool( this, id, bitmap1, bitmap2, toggle,
136 clientData, shortHelpString, longHelpString);
137 }
138
139 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
140 {
141 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
142
143 return NULL;
144 }
145
146 void wxToolBar::RefreshTool( wxToolBarTool *tool )
147 {
148 wxRect rect( tool->m_x, tool->m_y, m_defaultWidth+6, m_defaultHeight+6 );
149 Refresh( TRUE, &rect );
150 }
151
152 void wxToolBar::DrawToolBarTool( wxToolBarTool *tool, wxDC &dc, bool down )
153 {
154 const wxBitmap& bitmap = (tool->IsEnabled() || !tool->GetBitmap2().Ok()) ? tool->GetBitmap1() : tool->GetBitmap2() ;
155 if (down)
156 {
157 dc.DrawBitmap( bitmap, tool->m_x+4, tool->m_y+4, TRUE );
158
159 dc.SetPen( *wxGREY_PEN );
160 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
161 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
162
163 dc.SetPen( *wxBLACK_PEN );
164 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+1 );
165 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+1, tool->m_y+m_defaultHeight+4 );
166
167 dc.SetPen( *wxWHITE_PEN );
168 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
169 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
170 }
171 else
172 {
173 dc.DrawBitmap( bitmap, tool->m_x+3, tool->m_y+3, TRUE );
174
175 dc.SetPen( *wxWHITE_PEN );
176 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
177 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
178 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y, tool->m_x+m_defaultWidth+4, tool->m_y+2 );
179 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+4, tool->m_x+2, tool->m_y+m_defaultHeight+4 );
180
181 dc.SetPen( *wxBLACK_PEN );
182 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
183 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
184
185 dc.SetPen( *wxGREY_PEN );
186 dc.DrawLine( tool->m_x+1, tool->m_y+m_defaultHeight+4, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+4 );
187 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+m_defaultHeight+5 );
188 }
189 }
190
191 void wxToolBar::OnPaint(wxPaintEvent &event)
192 {
193 wxPaintDC dc(this);
194
195 wxSize clientSize = GetClientSize();
196 dc.SetPen( *wxBLACK_PEN );
197 dc.DrawLine( 0,0, clientSize.x,0 );
198
199 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
200 node;
201 node = node->GetNext() )
202 {
203 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
204
205 if (tool->GetId() == -1) continue;
206
207 DrawToolBarTool( tool, dc, tool->m_isDown );
208 }
209 }
210
211 bool wxToolBar::Realize()
212 {
213 if (!wxToolBarBase::Realize())
214 return FALSE;
215
216 int x;
217 int y;
218
219 if (GetWindowStyleFlag() & wxTB_VERTICAL)
220 {
221 x = m_xMargin;
222 y = 5;
223 }
224 else
225 {
226 y = m_yMargin;
227 x = 5;
228 }
229
230 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
231 node;
232 node = node->GetNext() )
233 {
234 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
235
236 if (GetWindowStyleFlag() & wxTB_VERTICAL)
237 {
238 if (tool->GetId() == -1)
239 {
240 y += 6;
241 continue;
242 }
243 tool->m_x = m_xMargin;
244 tool->m_y = y;
245 y += m_defaultHeight + 6;
246
247 // Calculate the maximum height or width (depending on style)
248 // so we know how to size the toolbar in Realize.
249 // We could get the size of the tool instead of the
250 // default bitmap size
251 if (m_maxWidth < (m_defaultWidth + 2*(m_xMargin + 2)))
252 m_maxWidth = (m_defaultWidth + 2*(m_xMargin + 2)) ;
253 }
254 else
255 {
256 if (tool->GetId() == -1)
257 {
258 x += 6;
259 continue;
260 }
261 tool->m_x = x;
262 tool->m_y = m_yMargin;
263 x += m_defaultWidth + 6;
264
265 // Calculate the maximum height or width (depending on style)
266 // so we know how to size the toolbar in Realize.
267 // We could get the size of the tool instead of the
268 // default bitmap size
269 if (m_maxHeight < (m_defaultHeight + 2*(m_yMargin + 2)))
270 m_maxHeight = (m_defaultHeight + 2*(m_yMargin + 2)) ;
271 }
272
273 }
274
275 wxSize sz = GetSize();
276 if (GetWindowStyleFlag() & wxTB_VERTICAL)
277 {
278 SetSize(m_maxWidth, sz.y);
279 }
280 else
281 {
282 SetSize(sz.x, m_maxHeight);
283 }
284
285 // Commenting out Robert's fix since the above should be
286 // more general
287 // SetSize( x+16, m_defaultHeight + 14 );
288
289 return TRUE;
290 }
291
292 void wxToolBar::OnMouse(wxMouseEvent &event)
293 {
294 wxToolBarTool *hit = NULL;
295 int x = event.GetX();
296 int y = event.GetY();
297
298 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
299 node;
300 node = node->GetNext() )
301 {
302 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
303
304 if ((x > tool->m_x) && (x < tool->m_x+m_defaultWidth+5) &&
305 (y > tool->m_y) && (y < tool->m_y+m_defaultHeight+5))
306 {
307 hit = tool;
308 break;
309 }
310 }
311
312 if (event.LeftDown() && (hit) && hit->IsEnabled())
313 {
314 CaptureMouse();
315 m_captured = hit;
316
317 m_captured->m_isDown = TRUE;
318 RefreshTool( m_captured );
319
320 return;
321 }
322
323 if (event.Dragging() && (m_captured))
324 {
325 bool is_down = (hit == m_captured);
326 if (is_down != m_captured->m_isDown)
327 {
328 m_captured->m_isDown = is_down;
329 RefreshTool( m_captured );
330 }
331
332 return;
333 }
334
335 if (event.LeftUp() && (m_captured))
336 {
337 ReleaseMouse();
338
339 m_captured->m_isDown = FALSE;
340 RefreshTool( m_captured );
341
342 if (hit == m_captured)
343 {
344 wxCommandEvent cevent( wxEVT_COMMAND_TOOL_CLICKED, m_captured->GetId() );
345 cevent.SetEventObject( this );
346 // cevent.SetExtraLong((long) toggleDown);
347 GetEventHandler()->ProcessEvent( cevent );
348 }
349
350 m_captured = NULL;
351
352 return;
353 }
354 }
355