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