New code for greying out.
[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 EVT_ENTER_WINDOW( wxToolBar::OnEnter )
45 EVT_LEAVE_WINDOW( wxToolBar::OnLeave )
46 END_EVENT_TABLE()
47
48 bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
49 const wxPoint& pos, const wxSize& size,
50 long style, const wxString& name )
51 {
52 bool ret = wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name );
53
54 return ret;
55 }
56
57 void wxToolBar::Init()
58 {
59 // TODO: this is from tbarbase.cpp, but should be in
60 // wxToolbarBase::Init
61 // the list owns the pointers
62 m_tools.DeleteContents(TRUE);
63 m_xMargin = m_yMargin = 0;
64 m_maxRows = m_maxCols = 0;
65 // End TODO
66
67 m_maxWidth = 0;
68 m_maxHeight = 0;
69
70 m_captured = NULL;
71 m_underMouse = NULL;
72
73 SetToolBitmapSize( wxSize(16,15) );
74 }
75
76 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
77 {
78 return NULL;
79 }
80
81 void wxToolBar::SetToolShortHelp(int id, const wxString& helpString)
82 {
83 }
84
85 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *tool)
86 {
87 return TRUE;
88 }
89
90 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
91 {
92 return TRUE;
93 }
94
95 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
96 {
97 // Comment this out if you don't want the disabled look,
98 // which currently acts weirdly for the scissors icon
99 // in the toolbar sample. See src/common/tbarbase.cpp
100 // for the wxCreateGreyedImage function.
101 #if 1
102 // Created disabled-state bitmap on demand
103 if (!enable && !tool->GetBitmap2().Ok())
104 {
105 wxImage image( tool->GetBitmap1() );
106
107 unsigned char bg_red = 180;
108 unsigned char bg_green = 180;
109 unsigned char bg_blue = 180;
110
111 unsigned char mask_red = image.GetMaskRed();
112 unsigned char mask_green = image.GetMaskGreen();
113 unsigned char mask_blue = image.GetMaskBlue();
114
115 bool has_mask = image.HasMask();
116
117 int x,y;
118 for (y = 0; y < image.GetHeight(); y++)
119 {
120 for (x = 0; x < image.GetWidth(); x++)
121 {
122 unsigned char red = image.GetRed(x,y);
123 unsigned char green = image.GetGreen(x,y);
124 unsigned char blue = image.GetBlue(x,y);
125 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
126 {
127 red = (((wxInt32) red - bg_red) >> 1) + bg_red;
128 green = (((wxInt32) green - bg_green) >> 1) + bg_green;
129 blue = (((wxInt32) blue - bg_blue) >> 1) + bg_blue;
130 image.SetRGB( x, y, red, green, blue );
131 }
132 }
133 }
134
135 for (y = 0; y < image.GetHeight(); y++)
136 {
137 for (x = y % 2; x < image.GetWidth(); x += 2)
138 {
139 unsigned char red = image.GetRed(x,y);
140 unsigned char green = image.GetGreen(x,y);
141 unsigned char blue = image.GetBlue(x,y);
142 if (!has_mask || red != mask_red || green != mask_green || blue != mask_blue)
143 {
144 red = (((wxInt32) red - bg_red) >> 1) + bg_red;
145 green = (((wxInt32) green - bg_green) >> 1) + bg_green;
146 blue = (((wxInt32) blue - bg_blue) >> 1) + bg_blue;
147 image.SetRGB( x, y, red, green, blue );
148 }
149 }
150 }
151
152 tool->SetBitmap2( image.ConvertToBitmap() );
153 }
154
155 RefreshTool((wxToolBarTool*) tool);
156 #endif
157 }
158
159 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
160 {
161 wxToolBarTool *my_tool = (wxToolBarTool*) tool;
162
163 bool refresh = (my_tool->IsToggled() != toggle);
164
165 my_tool->m_isDown = toggle;
166
167 if (refresh)
168 RefreshTool( my_tool );
169 }
170
171 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool toggle)
172 {
173 }
174
175 wxToolBarToolBase *wxToolBar::CreateTool(int id,
176 const wxBitmap& bitmap1,
177 const wxBitmap& bitmap2,
178 bool toggle,
179 wxObject *clientData,
180 const wxString& shortHelpString,
181 const wxString& longHelpString)
182 {
183 return new wxToolBarTool( this, id, bitmap1, bitmap2, toggle,
184 clientData, shortHelpString, longHelpString);
185 }
186
187 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
188 {
189 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
190
191 return NULL;
192 }
193
194 void wxToolBar::RefreshTool( wxToolBarTool *tool )
195 {
196 wxRect rect( tool->m_x, tool->m_y, m_defaultWidth+6, m_defaultHeight+6 );
197 Refresh( TRUE, &rect );
198 }
199
200 void wxToolBar::DrawToolBarTool( wxToolBarTool *tool, wxDC &dc, bool down )
201 {
202 const wxBitmap& bitmap = (tool->IsEnabled() || !tool->GetBitmap2().Ok()) ? tool->GetBitmap1() : tool->GetBitmap2() ;
203
204 if (HasFlag(wxTB_FLAT) && (tool != m_underMouse))
205 {
206 if (down)
207 dc.DrawBitmap( bitmap, tool->m_x+4, tool->m_y+4, TRUE );
208 else
209 dc.DrawBitmap( bitmap, tool->m_x+3, tool->m_y+3, TRUE );
210
211 return;
212 }
213
214 if (down)
215 {
216 dc.DrawBitmap( bitmap, tool->m_x+4, tool->m_y+4, TRUE );
217
218 dc.SetPen( *wxGREY_PEN );
219 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
220 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
221
222 dc.SetPen( *wxBLACK_PEN );
223 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+1 );
224 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+1, tool->m_y+m_defaultHeight+4 );
225
226 dc.SetPen( *wxWHITE_PEN );
227 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
228 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
229 }
230 else
231 {
232 dc.DrawBitmap( bitmap, tool->m_x+3, tool->m_y+3, TRUE );
233
234 dc.SetPen( *wxWHITE_PEN );
235 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
236 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
237 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y, tool->m_x+m_defaultWidth+4, tool->m_y+2 );
238 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+4, tool->m_x+2, tool->m_y+m_defaultHeight+4 );
239
240 dc.SetPen( *wxBLACK_PEN );
241 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
242 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
243
244 dc.SetPen( *wxGREY_PEN );
245 dc.DrawLine( tool->m_x+1, tool->m_y+m_defaultHeight+4, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+4 );
246 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+m_defaultHeight+5 );
247 }
248 }
249
250 void wxToolBar::OnPaint(wxPaintEvent &event)
251 {
252 wxPaintDC dc(this);
253
254 wxSize clientSize = GetClientSize();
255 dc.SetPen( *wxBLACK_PEN );
256 dc.DrawLine( 0,0, clientSize.x,0 );
257
258 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
259 node;
260 node = node->GetNext() )
261 {
262 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
263
264 if (tool->GetId() == -1) continue;
265
266 DrawToolBarTool( tool, dc, tool->m_isDown );
267 }
268 }
269
270 bool wxToolBar::Realize()
271 {
272 if (!wxToolBarBase::Realize())
273 return FALSE;
274
275 int x;
276 int y;
277
278 if (GetWindowStyleFlag() & wxTB_VERTICAL)
279 {
280 m_xMargin += 1; // Cannot help it, but otherwise it look ugly
281
282 x = m_xMargin;
283 y = 5;
284 }
285 else
286 {
287 m_yMargin += 1; // Cannot help it, but otherwise it look ugly
288
289 y = m_yMargin;
290 x = 5;
291 }
292
293 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
294 node;
295 node = node->GetNext() )
296 {
297 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
298
299 if (GetWindowStyleFlag() & wxTB_VERTICAL)
300 {
301 if (tool->GetId() == -1)
302 {
303 y += 6;
304 continue;
305 }
306 tool->m_x = m_xMargin;
307 tool->m_y = y;
308 y += m_defaultHeight + 6;
309
310 // Calculate the maximum height or width (depending on style)
311 // so we know how to size the toolbar in Realize.
312 // We could get the size of the tool instead of the
313 // default bitmap size
314 if (m_maxWidth < (m_defaultWidth + 2*(m_xMargin + 2)))
315 m_maxWidth = (m_defaultWidth + 2*(m_xMargin + 2)) ;
316 }
317 else
318 {
319 if (tool->GetId() == -1)
320 {
321 x += 6;
322 continue;
323 }
324 tool->m_x = x;
325 tool->m_y = m_yMargin;
326 x += m_defaultWidth + 6;
327
328 // Calculate the maximum height or width (depending on style)
329 // so we know how to size the toolbar in Realize.
330 // We could get the size of the tool instead of the
331 // default bitmap size
332 if (m_maxHeight < (m_defaultHeight + 2*(m_yMargin + 2)))
333 m_maxHeight = (m_defaultHeight + 2*(m_yMargin + 2)) ;
334 }
335
336 }
337
338 wxSize sz = GetSize();
339 if (GetWindowStyleFlag() & wxTB_VERTICAL)
340 {
341 SetSize(m_maxWidth, sz.y);
342 }
343 else
344 {
345 SetSize(sz.x, m_maxHeight);
346 }
347
348 return TRUE;
349 }
350
351 void wxToolBar::OnLeave(wxMouseEvent &event)
352 {
353 if (HasFlag( wxTB_FLAT ))
354 {
355 wxToolBarTool *oldMouseUnder = m_underMouse;
356 if (m_underMouse)
357 {
358 m_underMouse = NULL;
359 RefreshTool( oldMouseUnder );
360 }
361 }
362 }
363
364 void wxToolBar::OnEnter(wxMouseEvent &event)
365 {
366 }
367
368 void wxToolBar::OnMouse(wxMouseEvent &event)
369 {
370 wxToolBarTool *hit = NULL;
371 int x = event.GetX();
372 int y = event.GetY();
373
374 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
375 node;
376 node = node->GetNext() )
377 {
378 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
379
380 if ((x > tool->m_x) && (x < tool->m_x+m_defaultWidth+5) &&
381 (y > tool->m_y) && (y < tool->m_y+m_defaultHeight+5))
382 {
383 hit = tool;
384 break;
385 }
386 }
387
388 wxToolBarTool *oldMouseUnder = m_underMouse;
389 if (HasFlag( wxTB_FLAT))
390 {
391 if (m_underMouse && (m_underMouse != hit))
392 {
393 m_underMouse = NULL;
394 RefreshTool( oldMouseUnder );
395 }
396 m_underMouse = hit;
397 }
398
399 if (event.LeftDown() && (hit) && hit->IsEnabled())
400 {
401 CaptureMouse();
402 m_captured = hit;
403
404 m_captured->m_isDown = TRUE;
405 RefreshTool( m_captured );
406
407 return;
408 }
409
410 if (event.Dragging() && (m_captured))
411 {
412 bool is_down = (hit == m_captured);
413 if (is_down != m_captured->m_isDown)
414 {
415 m_captured->m_isDown = is_down;
416 RefreshTool( m_captured );
417 }
418
419 return;
420 }
421
422 if (event.LeftUp() && (m_captured))
423 {
424 ReleaseMouse();
425
426 m_captured->m_isDown = FALSE;
427
428 // Bad trick...
429 m_underMouse = NULL;
430
431 RefreshTool( m_captured );
432
433 if (hit == m_captured)
434 {
435 wxCommandEvent cevent( wxEVT_COMMAND_TOOL_CLICKED, m_captured->GetId() );
436 cevent.SetEventObject( this );
437 // cevent.SetExtraLong((long) toggleDown);
438 GetEventHandler()->ProcessEvent( cevent );
439 }
440
441 m_captured = NULL;
442
443 return;
444 }
445
446 if (HasFlag(wxTB_FLAT))
447 {
448 if (m_underMouse && (m_underMouse != oldMouseUnder))
449 RefreshTool( m_underMouse );
450 }
451 }
452