]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarsmpl.cpp
wxChoice/wxComboBox background colour change (to get back to where we were);
[wxWidgets.git] / src / common / tbarsmpl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tbarsmpl.cpp
3 // Purpose: wxToolBarSimple
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "tbarsmpl.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/settings.h"
25 #include "wx/window.h"
26 #include "wx/dcclient.h"
27 #include "wx/dcmemory.h"
28 #endif
29
30 #if wxUSE_TOOLBAR
31
32 #include "wx/tbarsmpl.h"
33
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_DYNAMIC_CLASS(wxToolBarSimple, wxToolBarBase)
36
37 BEGIN_EVENT_TABLE(wxToolBarSimple, wxToolBarBase)
38 EVT_SIZE(wxToolBarSimple::OnSize)
39 EVT_PAINT(wxToolBarSimple::OnPaint)
40 EVT_KILL_FOCUS(wxToolBarSimple::OnKillFocus)
41 EVT_MOUSE_EVENTS(wxToolBarSimple::OnMouseEvent)
42 END_EVENT_TABLE()
43 #endif
44
45 wxToolBarSimple::wxToolBarSimple(void)
46 {
47 m_currentRowsOrColumns = 0;
48 m_lastX = 0;
49 m_lastY = 0;
50 }
51
52 bool wxToolBarSimple::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
53 const wxString& name )
54 {
55 if ( ! wxWindow::Create(parent, id, pos, size, style, name) )
56 return FALSE;
57
58 // Set it to grey (or other 3D face colour)
59 wxSystemSettings settings;
60 SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_3DFACE));
61
62 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
63 { m_lastX = 7; m_lastY = 3; }
64 else
65 { m_lastX = 3; m_lastY = 7; }
66 m_maxWidth = m_maxHeight = 0;
67 m_pressedTool = m_currentTool = -1;
68 m_xMargin = 0;
69 m_yMargin = 0;
70 m_toolPacking = 1;
71 m_toolSeparation = 5;
72 SetCursor(*wxSTANDARD_CURSOR);
73
74 return TRUE;
75 }
76
77 wxToolBarSimple::~wxToolBarSimple ()
78 {
79 }
80
81 void wxToolBarSimple::OnPaint (wxPaintEvent& WXUNUSED(event))
82 {
83 wxPaintDC dc(this);
84 PrepareDC(dc);
85
86 static int count = 0;
87 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
88 if ( count > 0 )
89 return;
90 count++;
91
92 wxMemoryDC mem_dc;
93
94 for ( wxNode *node = m_tools.First(); node; node = node->Next() )
95 {
96 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
97 if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON)
98 DrawTool(dc, mem_dc, tool);
99 }
100
101 count--;
102 }
103
104 void wxToolBarSimple::OnSize ( wxSizeEvent& event )
105 {
106 wxToolBarBase::OnSize(event);
107 }
108
109 void wxToolBarSimple::OnKillFocus (wxFocusEvent& WXUNUSED(event))
110 {
111 OnMouseEnter(m_pressedTool = m_currentTool = -1);
112 }
113
114 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent & event )
115 {
116 wxCoord x, y;
117 event.GetPosition(&x, &y);
118 wxToolBarTool *tool = FindToolForPosition(x, y);
119
120 if (event.LeftDown())
121 {
122 CaptureMouse();
123 }
124 if (event.LeftUp())
125 {
126 ReleaseMouse();
127 }
128
129 if (!tool)
130 {
131 if (m_currentTool > -1)
132 {
133 if (event.LeftIsDown())
134 SpringUpButton(m_currentTool);
135 m_currentTool = -1;
136 OnMouseEnter(-1);
137 }
138 return;
139 }
140
141 if (!event.IsButton())
142 {
143 if (tool->m_index != m_currentTool)
144 {
145 // If the left button is kept down and moved over buttons,
146 // press those buttons.
147 if (event.LeftIsDown() && tool->m_enabled)
148 {
149 SpringUpButton(m_currentTool);
150 tool->m_toggleState = !tool->m_toggleState;
151 wxMemoryDC *dc2 = new wxMemoryDC;
152 wxClientDC dc(this);
153 DrawTool(dc, *dc2, tool);
154 delete dc2;
155 }
156 m_currentTool = tool->m_index;
157 OnMouseEnter(tool->m_index);
158 }
159 return;
160 }
161
162 // Left button pressed.
163 if (event.LeftDown() && tool->m_enabled)
164 {
165 if (tool->m_isToggle)
166 {
167 tool->m_toggleState = !tool->m_toggleState;
168 }
169
170 wxMemoryDC *dc2 = new wxMemoryDC;
171 wxClientDC dc(this);
172 DrawTool(dc, *dc2, tool);
173 delete dc2;
174
175 }
176 else if (event.RightDown())
177 {
178 OnRightClick(tool->m_index, x, y);
179 }
180
181 // Left Button Released. Only this action confirms selection.
182 // If the button is enabled and it is not a toggle tool and it is
183 // in the pressed state, then raise the button and call OnLeftClick.
184 //
185 if (event.LeftUp() && tool->m_enabled &&
186 (tool->m_toggleState || tool->m_isToggle))
187 {
188 if (!tool->m_isToggle)
189 tool->m_toggleState = FALSE;
190
191 // Pass the OnLeftClick event to tool
192 if (!OnLeftClick(tool->m_index, tool->m_toggleState) && tool->m_isToggle)
193 {
194 // If it was a toggle, and OnLeftClick says No Toggle allowed,
195 // then change it back
196 tool->m_toggleState = !tool->m_toggleState;
197 }
198
199 wxClientDC dc(this);
200 wxMemoryDC *dc2 = new wxMemoryDC;
201 DrawTool(dc, *dc2, tool);
202 delete dc2;
203 }
204 }
205
206 void wxToolBarSimple::DrawTool(wxDC& dc, wxMemoryDC& memDC, wxToolBarTool *tool)
207 {
208 PrepareDC(dc);
209
210 wxPen dark_grey_pen(wxColour( 85,85,85 ), 1, wxSOLID);
211 wxPen white_pen("WHITE", 1, wxSOLID);
212 wxPen black_pen("BLACK", 1, wxSOLID);
213
214 wxBitmap *bitmap = tool->m_toggleState ? (& tool->m_bitmap2) : (& tool->m_bitmap1);
215
216 if (bitmap && bitmap->Ok())
217 {
218 #ifndef __WXGTK__
219 if (bitmap->GetPalette())
220 memDC.SetPalette(*bitmap->GetPalette());
221 #endif
222
223 int ax = (int)tool->m_x,
224 ay = (int)tool->m_y,
225 bx = (int)(tool->m_x+tool->GetWidth()),
226 by = (int)(tool->m_y+tool->GetHeight());
227
228 memDC.SelectObject(*bitmap);
229 if (m_windowStyle & wxTB_3DBUTTONS)
230 {
231 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
232 dc.Blit((ax+1), (ay+1), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
233 wxPen * old_pen = & dc.GetPen();
234 dc.SetPen( white_pen );
235 dc.DrawLine(ax,(by-1),ax,ay);
236 dc.DrawLine(ax,ay,(bx-1),ay);
237 dc.SetPen( dark_grey_pen );
238 dc.DrawLine((bx-1),(ay+1),(bx-1),(by-1));
239 dc.DrawLine((bx-1),(by-1),(ax+1),(by-1));
240 dc.SetPen( black_pen );
241 dc.DrawLine(bx,ay,bx,by);
242 dc.DrawLine(bx,by,ax,by);
243 dc.SetPen( *old_pen );
244 dc.DestroyClippingRegion();
245 // Select bitmap out of the DC
246 }
247 else
248 {
249 dc.Blit(tool->m_x, tool->m_y,
250 bitmap->GetWidth(), bitmap->GetHeight(),
251 &memDC, 0, 0);
252 }
253 memDC.SelectObject(wxNullBitmap);
254 #ifndef __WXGTK__
255 memDC.SetPalette(wxNullPalette);
256 #endif
257 }
258 // No second bitmap, so draw a thick line around bitmap, or invert if mono
259 else if (tool->m_toggleState)
260 {
261 bool drawBorder = FALSE;
262 #ifdef __X__ // X doesn't invert properly on colour
263 drawBorder = wxColourDisplay();
264 #else // Inversion works fine under Windows
265 drawBorder = FALSE;
266 #endif
267
268 if (!drawBorder)
269 {
270 memDC.SelectObject(tool->m_bitmap1);
271 dc.Blit(tool->m_x, tool->m_y, tool->GetWidth(), tool->GetHeight(),
272 &memDC, 0, 0, wxSRC_INVERT);
273 memDC.SelectObject(wxNullBitmap);
274 }
275 else
276 {
277 if (m_windowStyle & wxTB_3DBUTTONS)
278 {
279 int ax = (int)tool->m_x,
280 ay = (int)tool->m_y,
281 bx = (int)(tool->m_x+tool->GetWidth()),
282 by = (int)(tool->m_y+tool->GetHeight());
283
284 memDC.SelectObject(tool->m_bitmap1);
285 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
286 dc.Blit((ax+2), (ay+2), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
287 wxPen * old_pen = & dc.GetPen();
288 dc.SetPen( black_pen );
289 dc.DrawLine(ax,(by-1),ax,ay);
290 dc.DrawLine(ax,ay,(bx-1),ay);
291 dc.SetPen( dark_grey_pen );
292 dc.DrawLine((ax+1),(by-2),(ax+1),(ay+1));
293 dc.DrawLine((ax+1),(ay+1),(bx-2),(ay+1));
294 dc.SetPen( white_pen );
295 dc.DrawLine(bx,ay,bx,by);
296 dc.DrawLine(bx,by,ax,by);
297 dc.SetPen( *old_pen );
298 dc.DestroyClippingRegion();
299 memDC.SelectObject(wxNullBitmap);
300 }
301 else
302 {
303 long x = tool->m_x;
304 long y = tool->m_y;
305 long w = tool->m_bitmap1.GetWidth();
306 long h = tool->m_bitmap1.GetHeight();
307 wxPen thick_black_pen("BLACK", 3, wxSOLID);
308
309 memDC.SelectObject(tool->m_bitmap1);
310 dc.SetClippingRegion(tool->m_x, tool->m_y, w, h);
311 dc.Blit(tool->m_x, tool->m_y, w, h,
312 &memDC, 0, 0);
313 dc.SetPen(thick_black_pen);
314 dc.SetBrush(*wxTRANSPARENT_BRUSH);
315 dc.DrawRectangle(x, y, w-1, h-1);
316 dc.DestroyClippingRegion();
317 memDC.SelectObject(wxNullBitmap);
318 }
319 }
320 }
321 }
322
323 void wxToolBarSimple::ToggleTool(int index, bool toggle)
324 {
325 wxNode *node = (wxNode*) NULL;
326 node = m_tools.Find((long)index);
327 if (node)
328 {
329 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
330 if (tool && tool->m_isToggle)
331 {
332 bool oldState = tool->m_toggleState;
333 tool->m_toggleState = toggle;
334
335 if (oldState != toggle)
336 {
337 wxMemoryDC memDC;
338 wxClientDC dc(this);
339 DrawTool(dc, memDC, tool);
340 }
341 }
342 }
343 }
344
345 // Okay, so we've left the tool we're in ... we must check if
346 // the tool we're leaving was a 'sprung push button' and if so,
347 // spring it back to the up state.
348 //
349 void wxToolBarSimple::SpringUpButton(int index)
350 {
351 wxNode *node = (wxNode*) NULL;
352 node=m_tools.Find((long)index);
353 if (node) {
354 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
355 if (tool && !tool->m_isToggle && tool->m_toggleState){
356 tool->m_toggleState = FALSE;
357 wxMemoryDC memDC;
358 wxClientDC dc(this);
359 DrawTool(dc, memDC, tool);
360 }
361 else if (tool && tool->m_isToggle){
362 tool->m_toggleState = !tool->m_toggleState;
363 wxMemoryDC memDC;
364 wxClientDC dc(this);
365 DrawTool(dc, memDC, tool);
366 }
367 }
368 }
369
370 void wxToolBarSimple::LayoutTools(void)
371 {
372 m_currentRowsOrColumns = 0;
373 m_lastX = m_xMargin;
374 m_lastY = m_yMargin;
375 int maxToolWidth = 0;
376 int maxToolHeight = 0;
377 m_maxWidth = 0;
378 m_maxHeight = 0;
379
380 // Find the maximum tool width and height
381 wxNode *node = m_tools.First();
382 while (node)
383 {
384 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
385 if (tool->GetWidth() > maxToolWidth)
386 maxToolWidth = (int)tool->GetWidth();
387 if (tool->GetHeight() > maxToolHeight)
388 maxToolHeight = (int)tool->GetHeight();
389 node = node->Next();
390 }
391
392 int separatorSize = m_toolSeparation;
393
394 node = m_tools.First();
395 while (node)
396 {
397 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
398 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
399 {
400 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
401 {
402 if (m_currentRowsOrColumns >= m_maxCols)
403 m_lastY += separatorSize;
404 else
405 m_lastX += separatorSize;
406 }
407 else
408 {
409 if (m_currentRowsOrColumns >= m_maxRows)
410 m_lastX += separatorSize;
411 else
412 m_lastY += separatorSize;
413 }
414 }
415 else if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON)
416 {
417 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
418 {
419 if (m_currentRowsOrColumns >= m_maxCols)
420 {
421 m_currentRowsOrColumns = 0;
422 m_lastX = m_xMargin;
423 m_lastY += maxToolHeight + m_toolPacking;
424 }
425 tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
426 tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
427
428 m_lastX += maxToolWidth + m_toolPacking;
429 }
430 else
431 {
432 if (m_currentRowsOrColumns >= m_maxRows)
433 {
434 m_currentRowsOrColumns = 0;
435 m_lastX += (maxToolWidth + m_toolPacking);
436 m_lastY = m_yMargin;
437 }
438 tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
439 tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
440
441 m_lastY += maxToolHeight + m_toolPacking;
442 }
443 m_currentRowsOrColumns ++;
444 }
445
446 if (m_lastX > m_maxWidth)
447 m_maxWidth = m_lastX;
448 if (m_lastY > m_maxHeight)
449 m_maxHeight = m_lastY;
450
451 node = node->Next();
452 }
453 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
454 m_maxWidth += maxToolWidth;
455 else
456 m_maxHeight += maxToolHeight;
457
458 m_maxWidth += m_xMargin;
459 m_maxHeight += m_yMargin;
460 }
461
462
463 #endif