]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarsmpl.cpp
Made NOPCH overridable
[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.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 // TODO: eliminate these; use system colours
43 static wxPen * white_pen = NULL,
44 * dark_grey_pen = NULL,
45 * black_pen = NULL,
46 * thick_black_pen;
47
48 wxToolBarSimple::wxToolBarSimple(void)
49 {
50 m_currentRowsOrColumns = 0;
51 m_lastX = 0;
52 m_lastY = 0;
53 }
54
55 bool wxToolBarSimple::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
56 const wxString& name )
57 {
58 if ( ! wxWindow::Create(parent, id, pos, size, style, name) )
59 return FALSE;
60
61 // Set it to grey (or other 3D face colour)
62 wxSystemSettings settings;
63 SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_3DFACE));
64 SetDefaultBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_3DFACE));
65
66 if ( white_pen == 0 )
67 {
68 white_pen = new wxPen;
69 white_pen->SetColour( "WHITE" );
70 }
71 if ( dark_grey_pen == 0 )
72 {
73 dark_grey_pen = new wxPen;
74 dark_grey_pen->SetColour( 85,85,85 );
75 }
76 if ( black_pen == 0 )
77 {
78 black_pen = new wxPen;
79 black_pen->SetColour( "BLACK" );
80 }
81 if ( thick_black_pen == 0 )
82 {
83 thick_black_pen = new wxPen("BLACK", 3, wxSOLID);
84 }
85 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
86 { m_lastX = 7; m_lastY = 3; }
87 else
88 { m_lastX = 3; m_lastY = 7; }
89 m_maxWidth = m_maxHeight = 0;
90 m_pressedTool = m_currentTool = -1;
91 m_xMargin = 0;
92 m_yMargin = 0;
93 m_toolPacking = 1;
94 m_toolSeparation = 5;
95
96 return TRUE;
97 }
98
99 wxToolBarSimple::~wxToolBarSimple ()
100 {
101 }
102
103 void wxToolBarSimple::OnPaint (wxPaintEvent& event)
104 {
105 wxPaintDC dc(this);
106 PrepareDC(dc);
107
108 static int count = 0;
109 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
110 if ( count > 0 )
111 return;
112 count++;
113
114 wxMemoryDC mem_dc;
115
116 for ( wxNode *node = m_tools.First(); node; node = node->Next() )
117 {
118 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
119 if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON)
120 DrawTool(dc, mem_dc, tool);
121 }
122
123 count--;
124 }
125
126 void wxToolBarSimple::OnSize ( wxSizeEvent& event )
127 {
128 wxToolBarBase::OnSize(event);
129 }
130
131 void wxToolBarSimple::OnKillFocus (wxFocusEvent& event)
132 {
133 OnMouseEnter(m_pressedTool = m_currentTool = -1);
134 }
135
136 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent & event )
137 {
138 long x, y;
139 event.Position(&x, &y);
140 wxToolBarTool *tool = FindToolForPosition(x, y);
141
142 if (!tool)
143 {
144 if (m_currentTool > -1)
145 {
146 if (event.LeftIsDown())
147 SpringUpButton(m_currentTool);
148 m_currentTool = -1;
149 OnMouseEnter(-1);
150 }
151 return;
152 }
153
154 if (!event.IsButton())
155 {
156 if (tool->m_index != m_currentTool)
157 {
158 // If the left button is kept down and moved over buttons,
159 // press those buttons.
160 if (event.LeftIsDown() && tool->m_enabled) {
161 SpringUpButton(m_currentTool);
162 tool->m_toggleState = !tool->m_toggleState;
163 wxMemoryDC *dc2 = new wxMemoryDC;
164 wxClientDC dc(this);
165 DrawTool(dc, *dc2, tool);
166 delete dc2;
167 }
168 OnMouseEnter(tool->m_index);
169 m_currentTool = tool->m_index;
170 }
171 return;
172 }
173
174 // Left button pressed.
175 if (event.LeftDown() && tool->m_enabled)
176 {
177 tool->m_toggleState = !tool->m_toggleState;
178 wxMemoryDC *dc2 = new wxMemoryDC;
179 wxClientDC dc(this);
180 DrawTool(dc, *dc2, tool);
181 delete dc2;
182 }
183 else if (event.RightDown())
184 {
185 OnRightClick(tool->m_index, x, y);
186 }
187 // Left Button Released. Only this action confirms selection.
188 // If the button is enabled and it is not a toggle tool and it is
189 // in the pressed state, then raise the button and call OnLeftClick.
190 //
191 if (event.LeftUp() && tool->m_enabled &&
192 (tool->m_toggleState || tool->m_isToggle)){
193 if (!tool->m_isToggle)
194 tool->m_toggleState = FALSE;
195 // Pass the OnLeftClick event to tool
196 if (!OnLeftClick(tool->m_index, tool->m_toggleState) && tool->m_isToggle)
197 // If it was a toggle, and OnLeftClick says No Toggle allowed,
198 // then change it back
199 tool->m_toggleState = !tool->m_toggleState;
200 wxClientDC dc(this);
201 wxMemoryDC *dc2 = new wxMemoryDC;
202 DrawTool(dc, *dc2, tool);
203 delete dc2;
204 }
205 }
206
207 void wxToolBarSimple::DrawTool(wxDC& dc, wxMemoryDC& memDC, wxToolBarTool *tool)
208 {
209 PrepareDC(dc);
210
211 wxBitmap *bitmap = tool->m_toggleState ? (& tool->m_bitmap2) : (& tool->m_bitmap1);
212
213 if (bitmap && bitmap->Ok())
214 {
215 if (bitmap->GetColourMap())
216 memDC.SetPalette(*bitmap->GetColourMap());
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
301 memDC.SelectObject(tool->m_bitmap1);
302 dc.SetClippingRegion(tool->m_x, tool->m_y, w, h);
303 dc.Blit(tool->m_x, tool->m_y, w, h,
304 &memDC, 0, 0);
305 dc.SetPen(*thick_black_pen);
306 dc.SetBrush(*wxTRANSPARENT_BRUSH);
307 dc.DrawRectangle(x, y, w-1, h-1);
308 dc.DestroyClippingRegion();
309 memDC.SelectObject(wxNullBitmap);
310 }
311 }
312 }
313 }
314
315 void wxToolBarSimple::ToggleTool(int index, bool toggle)
316 {
317 wxNode *node = m_tools.Find((long)index);
318 if (node)
319 {
320 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
321 if (tool && tool->m_isToggle)
322 {
323 bool oldState = tool->m_toggleState;
324 tool->m_toggleState = toggle;
325
326 if (oldState != toggle)
327 {
328 wxMemoryDC memDC;
329 wxClientDC dc(this);
330 DrawTool(dc, memDC, tool);
331 }
332 }
333 }
334 }
335
336 // Okay, so we've left the tool we're in ... we must check if
337 // the tool we're leaving was a 'sprung push button' and if so,
338 // spring it back to the up state.
339 //
340 void wxToolBarSimple::SpringUpButton(int index)
341 {
342 wxNode *node=m_tools.Find((long)index);
343 if (node) {
344 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
345 if (tool && !tool->m_isToggle && tool->m_toggleState){
346 tool->m_toggleState = FALSE;
347 wxMemoryDC memDC;
348 wxClientDC dc(this);
349 DrawTool(dc, memDC, tool);
350 }
351 else if (tool && tool->m_isToggle){
352 tool->m_toggleState = !tool->m_toggleState;
353 wxMemoryDC memDC;
354 wxClientDC dc(this);
355 DrawTool(dc, memDC, tool);
356 }
357 }
358 }
359
360 void wxToolBarSimple::Layout(void)
361 {
362 m_currentRowsOrColumns = 0;
363 m_lastX = m_xMargin;
364 m_lastY = m_yMargin;
365 int maxToolWidth = 0;
366 int maxToolHeight = 0;
367 m_maxWidth = 0;
368 m_maxHeight = 0;
369
370 // Find the maximum tool width and height
371 wxNode *node = m_tools.First();
372 while (node)
373 {
374 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
375 if (tool->GetWidth() > maxToolWidth)
376 maxToolWidth = (int)tool->GetWidth();
377 if (tool->GetHeight() > maxToolHeight)
378 maxToolHeight = (int)tool->GetHeight();
379 node = node->Next();
380 }
381
382 int separatorSize = m_toolSeparation;
383
384 node = m_tools.First();
385 while (node)
386 {
387 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
388 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
389 {
390 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
391 {
392 if (m_currentRowsOrColumns >= m_maxCols)
393 m_lastY += separatorSize;
394 else
395 m_lastX += separatorSize;
396 }
397 else
398 {
399 if (m_currentRowsOrColumns >= m_maxRows)
400 m_lastX += separatorSize;
401 else
402 m_lastY += separatorSize;
403 }
404 }
405 else if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON)
406 {
407 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
408 {
409 if (m_currentRowsOrColumns >= m_maxCols)
410 {
411 m_currentRowsOrColumns = 0;
412 m_lastX = m_xMargin;
413 m_lastY += maxToolHeight + m_toolPacking;
414 }
415 tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
416 tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
417
418 m_lastX += maxToolWidth + m_toolPacking;
419 }
420 else
421 {
422 if (m_currentRowsOrColumns >= m_maxRows)
423 {
424 m_currentRowsOrColumns = 0;
425 m_lastX += (maxToolWidth + m_toolPacking);
426 m_lastY = m_yMargin;
427 }
428 tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0);
429 tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0);
430
431 m_lastY += maxToolHeight + m_toolPacking;
432 }
433 m_currentRowsOrColumns ++;
434 }
435
436 if (m_lastX > m_maxWidth)
437 m_maxWidth = m_lastX;
438 if (m_lastY > m_maxHeight)
439 m_maxHeight = m_lastY;
440
441 node = node->Next();
442 }
443 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
444 m_maxWidth += maxToolWidth;
445 else
446 m_maxHeight += maxToolHeight;
447
448 m_maxWidth += m_xMargin;
449 m_maxHeight += m_yMargin;
450 }
451
452
453 #endif