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