]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarsmpl.cpp
wxBaseArray::Shrink() added
[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/wx.h"
25 #endif
26
27 #if USE_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 SetDefaultBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_3DFACE));
59
60 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
61 { m_lastX = 7; m_lastY = 3; }
62 else
63 { m_lastX = 3; m_lastY = 7; }
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;
70
71 return TRUE;
72 }
73
74 wxToolBarSimple::~wxToolBarSimple ()
75 {
76 }
77
78 void wxToolBarSimple::OnPaint (wxPaintEvent& 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& 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 if (bitmap->GetPalette())
216 memDC.SetPalette(*bitmap->GetPalette());
217
218 int ax = (int)tool->m_x,
219 ay = (int)tool->m_y,
220 bx = (int)(tool->m_x+tool->GetWidth()),
221 by = (int)(tool->m_y+tool->GetHeight());
222
223 memDC.SelectObject(*bitmap);
224 if (m_windowStyle & wxTB_3DBUTTONS)
225 {
226 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
227 dc.Blit((ax+1), (ay+1), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
228 wxPen * old_pen = dc.GetPen();
229 dc.SetPen( white_pen );
230 dc.DrawLine(ax,(by-1),ax,ay);
231 dc.DrawLine(ax,ay,(bx-1),ay);
232 dc.SetPen( dark_grey_pen );
233 dc.DrawLine((bx-1),(ay+1),(bx-1),(by-1));
234 dc.DrawLine((bx-1),(by-1),(ax+1),(by-1));
235 dc.SetPen( black_pen );
236 dc.DrawLine(bx,ay,bx,by);
237 dc.DrawLine(bx,by,ax,by);
238 dc.SetPen( *old_pen );
239 dc.DestroyClippingRegion();
240 // Select bitmap out of the DC
241 }
242 else
243 {
244 dc.Blit(tool->m_x, tool->m_y,
245 bitmap->GetWidth(), bitmap->GetHeight(),
246 &memDC, 0, 0);
247 }
248 memDC.SelectObject(wxNullBitmap);
249 memDC.SetPalette(wxNullPalette);
250 }
251 // No second bitmap, so draw a thick line around bitmap, or invert if mono
252 else if (tool->m_toggleState)
253 {
254 bool drawBorder = FALSE;
255 #ifdef __X__ // X doesn't invert properly on colour
256 drawBorder = wxColourDisplay();
257 #else // Inversion works fine under Windows
258 drawBorder = FALSE;
259 #endif
260
261 if (!drawBorder)
262 {
263 memDC.SelectObject(tool->m_bitmap1);
264 dc.Blit(tool->m_x, tool->m_y, tool->GetWidth(), tool->GetHeight(),
265 &memDC, 0, 0, wxSRC_INVERT);
266 memDC.SelectObject(wxNullBitmap);
267 }
268 else
269 {
270 if (m_windowStyle & wxTB_3DBUTTONS)
271 {
272 int ax = (int)tool->m_x,
273 ay = (int)tool->m_y,
274 bx = (int)(tool->m_x+tool->GetWidth()),
275 by = (int)(tool->m_y+tool->GetHeight());
276
277 memDC.SelectObject(tool->m_bitmap1);
278 dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1));
279 dc.Blit((ax+2), (ay+2), (bx-ax-2), (by-ay-2), &memDC, 0, 0);
280 wxPen * old_pen = dc.GetPen();
281 dc.SetPen( black_pen );
282 dc.DrawLine(ax,(by-1),ax,ay);
283 dc.DrawLine(ax,ay,(bx-1),ay);
284 dc.SetPen( dark_grey_pen );
285 dc.DrawLine((ax+1),(by-2),(ax+1),(ay+1));
286 dc.DrawLine((ax+1),(ay+1),(bx-2),(ay+1));
287 dc.SetPen( white_pen );
288 dc.DrawLine(bx,ay,bx,by);
289 dc.DrawLine(bx,by,ax,by);
290 dc.SetPen( *old_pen );
291 dc.DestroyClippingRegion();
292 memDC.SelectObject(wxNullBitmap);
293 }
294 else
295 {
296 long x = tool->m_x;
297 long y = tool->m_y;
298 long w = tool->m_bitmap1.GetWidth();
299 long h = tool->m_bitmap1.GetHeight();
300 wxPen thick_black_pen("BLACK", 3, wxSOLID);
301
302 memDC.SelectObject(tool->m_bitmap1);
303 dc.SetClippingRegion(tool->m_x, tool->m_y, w, h);
304 dc.Blit(tool->m_x, tool->m_y, w, h,
305 &memDC, 0, 0);
306 dc.SetPen(thick_black_pen);
307 dc.SetBrush(*wxTRANSPARENT_BRUSH);
308 dc.DrawRectangle(x, y, w-1, h-1);
309 dc.DestroyClippingRegion();
310 memDC.SelectObject(wxNullBitmap);
311 }
312 }
313 }
314 }
315
316 void wxToolBarSimple::ToggleTool(int index, bool toggle)
317 {
318 wxNode *node = m_tools.Find((long)index);
319 if (node)
320 {
321 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
322 if (tool && tool->m_isToggle)
323 {
324 bool oldState = tool->m_toggleState;
325 tool->m_toggleState = toggle;
326
327 if (oldState != toggle)
328 {
329 wxMemoryDC memDC;
330 wxClientDC dc(this);
331 DrawTool(dc, memDC, tool);
332 }
333 }
334 }
335 }
336
337 // Okay, so we've left the tool we're in ... we must check if
338 // the tool we're leaving was a 'sprung push button' and if so,
339 // spring it back to the up state.
340 //
341 void wxToolBarSimple::SpringUpButton(int index)
342 {
343 wxNode *node=m_tools.Find((long)index);
344 if (node) {
345 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
346 if (tool && !tool->m_isToggle && tool->m_toggleState){
347 tool->m_toggleState = FALSE;
348 wxMemoryDC memDC;
349 wxClientDC dc(this);
350 DrawTool(dc, memDC, tool);
351 }
352 else if (tool && tool->m_isToggle){
353 tool->m_toggleState = !tool->m_toggleState;
354 wxMemoryDC memDC;
355 wxClientDC dc(this);
356 DrawTool(dc, memDC, tool);
357 }
358 }
359 }
360
361 void wxToolBarSimple::Layout(void)
362 {
363 m_currentRowsOrColumns = 0;
364 m_lastX = m_xMargin;
365 m_lastY = m_yMargin;
366 int maxToolWidth = 0;
367 int maxToolHeight = 0;
368 m_maxWidth = 0;
369 m_maxHeight = 0;
370
371 // Find the maximum tool width and height
372 wxNode *node = m_tools.First();
373 while (node)
374 {
375 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
376 if (tool->GetWidth() > maxToolWidth)
377 maxToolWidth = (int)tool->GetWidth();
378 if (tool->GetHeight() > maxToolHeight)
379 maxToolHeight = (int)tool->GetHeight();
380 node = node->Next();
381 }
382
383 int separatorSize = m_toolSeparation;
384
385 node = m_tools.First();
386 while (node)
387 {
388 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
389 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
390 {
391 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
392 {
393 if (m_currentRowsOrColumns >= m_maxCols)
394 m_lastY += separatorSize;
395 else
396 m_lastX += separatorSize;
397 }
398 else
399 {
400 if (m_currentRowsOrColumns >= m_maxRows)
401 m_lastX += separatorSize;
402 else
403 m_lastY += separatorSize;
404 }
405 }
406 else if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON)
407 {
408 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
409 {
410 if (m_currentRowsOrColumns >= m_maxCols)
411 {
412 m_currentRowsOrColumns = 0;
413 m_lastX = m_xMargin;
414 m_lastY += maxToolHeight + m_toolPacking;
415 }
416 tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
417 tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
418
419 m_lastX += maxToolWidth + m_toolPacking;
420 }
421 else
422 {
423 if (m_currentRowsOrColumns >= m_maxRows)
424 {
425 m_currentRowsOrColumns = 0;
426 m_lastX += (maxToolWidth + m_toolPacking);
427 m_lastY = m_yMargin;
428 }
429 tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
430 tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
431
432 m_lastY += maxToolHeight + m_toolPacking;
433 }
434 m_currentRowsOrColumns ++;
435 }
436
437 if (m_lastX > m_maxWidth)
438 m_maxWidth = m_lastX;
439 if (m_lastY > m_maxHeight)
440 m_maxHeight = m_lastY;
441
442 node = node->Next();
443 }
444 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
445 m_maxWidth += maxToolWidth;
446 else
447 m_maxHeight += maxToolHeight;
448
449 m_maxWidth += m_xMargin;
450 m_maxHeight += m_yMargin;
451 }
452
453
454 #endif