removed USE_SHARED_LIBRARY(IES)
[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 IMPLEMENT_DYNAMIC_CLASS(wxToolBarSimple, wxToolBarBase)
35
36 BEGIN_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)
41 END_EVENT_TABLE()
42
43 wxToolBarSimple::wxToolBarSimple(void)
44 {
45 m_currentRowsOrColumns = 0;
46 m_lastX = 0;
47 m_lastY = 0;
48 }
49
50 bool wxToolBarSimple::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
51 const wxString& name )
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));
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 SetCursor(*wxSTANDARD_CURSOR);
71
72 return TRUE;
73 }
74
75 wxToolBarSimple::~wxToolBarSimple ()
76 {
77 }
78
79 void wxToolBarSimple::OnPaint (wxPaintEvent& WXUNUSED(event))
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
99 count--;
100 }
101
102 void wxToolBarSimple::OnSize ( wxSizeEvent& event )
103 {
104 wxToolBarBase::OnSize(event);
105 }
106
107 void wxToolBarSimple::OnKillFocus (wxFocusEvent& WXUNUSED(event))
108 {
109 OnMouseEnter(m_pressedTool = m_currentTool = -1);
110 }
111
112 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent & event )
113 {
114 wxCoord x, y;
115 event.GetPosition(&x, &y);
116 wxToolBarTool *tool = FindToolForPosition(x, y);
117
118 if (event.LeftDown())
119 {
120 CaptureMouse();
121 }
122 if (event.LeftUp())
123 {
124 ReleaseMouse();
125 }
126
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.
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;
153 }
154 m_currentTool = tool->m_index;
155 OnMouseEnter(tool->m_index);
156 }
157 return;
158 }
159
160 // Left button pressed.
161 if (event.LeftDown() && tool->m_enabled)
162 {
163 if (tool->m_isToggle)
164 {
165 tool->m_toggleState = !tool->m_toggleState;
166 }
167
168 wxMemoryDC *dc2 = new wxMemoryDC;
169 wxClientDC dc(this);
170 DrawTool(dc, *dc2, tool);
171 delete dc2;
172
173 }
174 else if (event.RightDown())
175 {
176 OnRightClick(tool->m_index, x, y);
177 }
178
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 &&
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
197 wxClientDC dc(this);
198 wxMemoryDC *dc2 = new wxMemoryDC;
199 DrawTool(dc, *dc2, tool);
200 delete dc2;
201 }
202 }
203
204 void wxToolBarSimple::DrawTool(wxDC& dc, wxMemoryDC& memDC, wxToolBarTool *tool)
205 {
206 PrepareDC(dc);
207
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
212 wxBitmap *bitmap = tool->m_toggleState ? (& tool->m_bitmap2) : (& tool->m_bitmap1);
213
214 if (bitmap && bitmap->Ok())
215 {
216 #ifndef __WXGTK__
217 if (bitmap->GetPalette())
218 memDC.SetPalette(*bitmap->GetPalette());
219 #endif
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);
231 wxPen * old_pen = & dc.GetPen();
232 dc.SetPen( white_pen );
233 dc.DrawLine(ax,(by-1),ax,ay);
234 dc.DrawLine(ax,ay,(bx-1),ay);
235 dc.SetPen( dark_grey_pen );
236 dc.DrawLine((bx-1),(ay+1),(bx-1),(by-1));
237 dc.DrawLine((bx-1),(by-1),(ax+1),(by-1));
238 dc.SetPen( black_pen );
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);
252 #ifndef __WXGTK__
253 memDC.SetPalette(wxNullPalette);
254 #endif
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;
260 #ifdef __X__ // X doesn't invert properly on colour
261 drawBorder = wxColourDisplay();
262 #else // Inversion works fine under Windows
263 drawBorder = FALSE;
264 #endif
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);
285 wxPen * old_pen = & dc.GetPen();
286 dc.SetPen( black_pen );
287 dc.DrawLine(ax,(by-1),ax,ay);
288 dc.DrawLine(ax,ay,(bx-1),ay);
289 dc.SetPen( dark_grey_pen );
290 dc.DrawLine((ax+1),(by-2),(ax+1),(ay+1));
291 dc.DrawLine((ax+1),(ay+1),(bx-2),(ay+1));
292 dc.SetPen( white_pen );
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();
305 wxPen thick_black_pen("BLACK", 3, wxSOLID);
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);
311 dc.SetPen(thick_black_pen);
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
321 void wxToolBarSimple::ToggleTool(int index, bool toggle)
322 {
323 wxNode *node = (wxNode*) NULL;
324 node = m_tools.Find((long)index);
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 //
347 void wxToolBarSimple::SpringUpButton(int index)
348 {
349 wxNode *node = (wxNode*) NULL;
350 node=m_tools.Find((long)index);
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
368 void wxToolBarSimple::LayoutTools(void)
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
461 #endif