1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolBarSimple
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbarsmpl.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/tbarsmpl.h"
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxToolBarSimple
, wxToolBarBase
)
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
)
42 wxToolBarSimple::wxToolBarSimple(void)
44 m_currentRowsOrColumns
= 0;
49 bool wxToolBarSimple::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
,
50 const wxString
& name
)
52 if ( ! wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
55 // Set it to grey (or other 3D face colour)
56 wxSystemSettings settings
;
57 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_3DFACE
));
59 if ( GetWindowStyleFlag() & wxTB_VERTICAL
)
60 { m_lastX
= 7; m_lastY
= 3; }
62 { m_lastX
= 3; m_lastY
= 7; }
63 m_maxWidth
= m_maxHeight
= 0;
64 m_pressedTool
= m_currentTool
= -1;
73 wxToolBarSimple::~wxToolBarSimple ()
77 void wxToolBarSimple::OnPaint (wxPaintEvent
& event
)
83 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
90 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
92 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
93 if (tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
)
94 DrawTool(dc
, mem_dc
, tool
);
100 void wxToolBarSimple::OnSize ( wxSizeEvent
& event
)
102 wxToolBarBase::OnSize(event
);
105 void wxToolBarSimple::OnKillFocus (wxFocusEvent
& event
)
107 OnMouseEnter(m_pressedTool
= m_currentTool
= -1);
110 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent
& event
)
113 event
.Position(&x
, &y
);
114 wxToolBarTool
*tool
= FindToolForPosition(x
, y
);
116 if (event
.LeftDown())
127 if (m_currentTool
> -1)
129 if (event
.LeftIsDown())
130 SpringUpButton(m_currentTool
);
137 if (!event
.IsButton())
139 if (tool
->m_index
!= m_currentTool
)
141 // If the left button is kept down and moved over buttons,
142 // press those buttons.
143 if (event
.LeftIsDown() && tool
->m_enabled
)
145 SpringUpButton(m_currentTool
);
146 tool
->m_toggleState
= !tool
->m_toggleState
;
147 wxMemoryDC
*dc2
= new wxMemoryDC
;
149 DrawTool(dc
, *dc2
, tool
);
152 m_currentTool
= tool
->m_index
;
153 OnMouseEnter(tool
->m_index
);
158 // Left button pressed.
159 if (event
.LeftDown() && tool
->m_enabled
)
161 if (tool
->m_isToggle
)
163 tool
->m_toggleState
= !tool
->m_toggleState
;
166 wxMemoryDC
*dc2
= new wxMemoryDC
;
168 DrawTool(dc
, *dc2
, tool
);
172 else if (event
.RightDown())
174 OnRightClick(tool
->m_index
, x
, y
);
177 // Left Button Released. Only this action confirms selection.
178 // If the button is enabled and it is not a toggle tool and it is
179 // in the pressed state, then raise the button and call OnLeftClick.
181 if (event
.LeftUp() && tool
->m_enabled
&&
182 (tool
->m_toggleState
|| tool
->m_isToggle
))
184 if (!tool
->m_isToggle
)
185 tool
->m_toggleState
= FALSE
;
187 // Pass the OnLeftClick event to tool
188 if (!OnLeftClick(tool
->m_index
, tool
->m_toggleState
) && tool
->m_isToggle
)
190 // If it was a toggle, and OnLeftClick says No Toggle allowed,
191 // then change it back
192 tool
->m_toggleState
= !tool
->m_toggleState
;
196 wxMemoryDC
*dc2
= new wxMemoryDC
;
197 DrawTool(dc
, *dc2
, tool
);
202 void wxToolBarSimple::DrawTool(wxDC
& dc
, wxMemoryDC
& memDC
, wxToolBarTool
*tool
)
206 wxPen
dark_grey_pen(wxColour( 85,85,85 ), 1, wxSOLID
);
207 wxPen
white_pen("WHITE", 1, wxSOLID
);
208 wxPen
black_pen("BLACK", 1, wxSOLID
);
210 wxBitmap
*bitmap
= tool
->m_toggleState
? (& tool
->m_bitmap2
) : (& tool
->m_bitmap1
);
212 if (bitmap
&& bitmap
->Ok())
214 if (bitmap
->GetPalette())
215 memDC
.SetPalette(*bitmap
->GetPalette());
217 int ax
= (int)tool
->m_x
,
219 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
220 by
= (int)(tool
->m_y
+tool
->GetHeight());
222 memDC
.SelectObject(*bitmap
);
223 if (m_windowStyle
& wxTB_3DBUTTONS
)
225 dc
.SetClippingRegion(ax
, ay
, (bx
-ax
+1), (by
-ay
+1));
226 dc
.Blit((ax
+1), (ay
+1), (bx
-ax
-2), (by
-ay
-2), &memDC
, 0, 0);
227 wxPen
* old_pen
= & dc
.GetPen();
228 dc
.SetPen( white_pen
);
229 dc
.DrawLine(ax
,(by
-1),ax
,ay
);
230 dc
.DrawLine(ax
,ay
,(bx
-1),ay
);
231 dc
.SetPen( dark_grey_pen
);
232 dc
.DrawLine((bx
-1),(ay
+1),(bx
-1),(by
-1));
233 dc
.DrawLine((bx
-1),(by
-1),(ax
+1),(by
-1));
234 dc
.SetPen( black_pen
);
235 dc
.DrawLine(bx
,ay
,bx
,by
);
236 dc
.DrawLine(bx
,by
,ax
,by
);
237 dc
.SetPen( *old_pen
);
238 dc
.DestroyClippingRegion();
239 // Select bitmap out of the DC
243 dc
.Blit(tool
->m_x
, tool
->m_y
,
244 bitmap
->GetWidth(), bitmap
->GetHeight(),
247 memDC
.SelectObject(wxNullBitmap
);
248 memDC
.SetPalette(wxNullPalette
);
250 // No second bitmap, so draw a thick line around bitmap, or invert if mono
251 else if (tool
->m_toggleState
)
253 bool drawBorder
= FALSE
;
254 #ifdef __X__ // X doesn't invert properly on colour
255 drawBorder
= wxColourDisplay();
256 #else // Inversion works fine under Windows
262 memDC
.SelectObject(tool
->m_bitmap1
);
263 dc
.Blit(tool
->m_x
, tool
->m_y
, tool
->GetWidth(), tool
->GetHeight(),
264 &memDC
, 0, 0, wxSRC_INVERT
);
265 memDC
.SelectObject(wxNullBitmap
);
269 if (m_windowStyle
& wxTB_3DBUTTONS
)
271 int ax
= (int)tool
->m_x
,
273 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
274 by
= (int)(tool
->m_y
+tool
->GetHeight());
276 memDC
.SelectObject(tool
->m_bitmap1
);
277 dc
.SetClippingRegion(ax
, ay
, (bx
-ax
+1), (by
-ay
+1));
278 dc
.Blit((ax
+2), (ay
+2), (bx
-ax
-2), (by
-ay
-2), &memDC
, 0, 0);
279 wxPen
* old_pen
= & dc
.GetPen();
280 dc
.SetPen( black_pen
);
281 dc
.DrawLine(ax
,(by
-1),ax
,ay
);
282 dc
.DrawLine(ax
,ay
,(bx
-1),ay
);
283 dc
.SetPen( dark_grey_pen
);
284 dc
.DrawLine((ax
+1),(by
-2),(ax
+1),(ay
+1));
285 dc
.DrawLine((ax
+1),(ay
+1),(bx
-2),(ay
+1));
286 dc
.SetPen( white_pen
);
287 dc
.DrawLine(bx
,ay
,bx
,by
);
288 dc
.DrawLine(bx
,by
,ax
,by
);
289 dc
.SetPen( *old_pen
);
290 dc
.DestroyClippingRegion();
291 memDC
.SelectObject(wxNullBitmap
);
297 long w
= tool
->m_bitmap1
.GetWidth();
298 long h
= tool
->m_bitmap1
.GetHeight();
299 wxPen
thick_black_pen("BLACK", 3, wxSOLID
);
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
,
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
);
315 void wxToolBarSimple::ToggleTool(int index
, bool toggle
)
317 wxNode
*node
= m_tools
.Find((long)index
);
320 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
321 if (tool
&& tool
->m_isToggle
)
323 bool oldState
= tool
->m_toggleState
;
324 tool
->m_toggleState
= toggle
;
326 if (oldState
!= toggle
)
330 DrawTool(dc
, memDC
, tool
);
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.
340 void wxToolBarSimple::SpringUpButton(int index
)
342 wxNode
*node
=m_tools
.Find((long)index
);
344 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
345 if (tool
&& !tool
->m_isToggle
&& tool
->m_toggleState
){
346 tool
->m_toggleState
= FALSE
;
349 DrawTool(dc
, memDC
, tool
);
351 else if (tool
&& tool
->m_isToggle
){
352 tool
->m_toggleState
= !tool
->m_toggleState
;
355 DrawTool(dc
, memDC
, tool
);
360 void wxToolBarSimple::Layout(void)
362 m_currentRowsOrColumns
= 0;
365 int maxToolWidth
= 0;
366 int maxToolHeight
= 0;
370 // Find the maximum tool width and height
371 wxNode
*node
= m_tools
.First();
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();
382 int separatorSize
= m_toolSeparation
;
384 node
= m_tools
.First();
387 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
388 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
390 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
392 if (m_currentRowsOrColumns
>= m_maxCols
)
393 m_lastY
+= separatorSize
;
395 m_lastX
+= separatorSize
;
399 if (m_currentRowsOrColumns
>= m_maxRows
)
400 m_lastX
+= separatorSize
;
402 m_lastY
+= separatorSize
;
405 else if (tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
)
407 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
409 if (m_currentRowsOrColumns
>= m_maxCols
)
411 m_currentRowsOrColumns
= 0;
413 m_lastY
+= maxToolHeight
+ m_toolPacking
;
415 tool
->m_x
= (long) (m_lastX
+ (maxToolWidth
- tool
->GetWidth())/2.0);
416 tool
->m_y
= (long) (m_lastY
+ (maxToolHeight
- tool
->GetHeight())/2.0);
418 m_lastX
+= maxToolWidth
+ m_toolPacking
;
422 if (m_currentRowsOrColumns
>= m_maxRows
)
424 m_currentRowsOrColumns
= 0;
425 m_lastX
+= (maxToolWidth
+ m_toolPacking
);
428 tool
->m_x
= (long) (m_lastX
+ (maxToolWidth
- tool
->GetWidth())/2.0);
429 tool
->m_y
= (long) (m_lastY
+ (maxToolHeight
- tool
->GetHeight())/2.0);
431 m_lastY
+= maxToolHeight
+ m_toolPacking
;
433 m_currentRowsOrColumns
++;
436 if (m_lastX
> m_maxWidth
)
437 m_maxWidth
= m_lastX
;
438 if (m_lastY
> m_maxHeight
)
439 m_maxHeight
= m_lastY
;
443 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
444 m_maxWidth
+= maxToolWidth
;
446 m_maxHeight
+= maxToolHeight
;
448 m_maxWidth
+= m_xMargin
;
449 m_maxHeight
+= m_yMargin
;