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;
69 SetCursor(*wxSTANDARD_CURSOR
);
74 wxToolBarSimple::~wxToolBarSimple ()
78 void wxToolBarSimple::OnPaint (wxPaintEvent
& event
)
84 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
91 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
93 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
94 if (tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
)
95 DrawTool(dc
, mem_dc
, tool
);
101 void wxToolBarSimple::OnSize ( wxSizeEvent
& event
)
103 wxToolBarBase::OnSize(event
);
106 void wxToolBarSimple::OnKillFocus (wxFocusEvent
& event
)
108 OnMouseEnter(m_pressedTool
= m_currentTool
= -1);
111 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent
& event
)
114 event
.Position(&x
, &y
);
115 wxToolBarTool
*tool
= FindToolForPosition(x
, y
);
117 if (event
.LeftDown())
128 if (m_currentTool
> -1)
130 if (event
.LeftIsDown())
131 SpringUpButton(m_currentTool
);
138 if (!event
.IsButton())
140 if (tool
->m_index
!= m_currentTool
)
142 // If the left button is kept down and moved over buttons,
143 // press those buttons.
144 if (event
.LeftIsDown() && tool
->m_enabled
)
146 SpringUpButton(m_currentTool
);
147 tool
->m_toggleState
= !tool
->m_toggleState
;
148 wxMemoryDC
*dc2
= new wxMemoryDC
;
150 DrawTool(dc
, *dc2
, tool
);
153 m_currentTool
= tool
->m_index
;
154 OnMouseEnter(tool
->m_index
);
159 // Left button pressed.
160 if (event
.LeftDown() && tool
->m_enabled
)
162 if (tool
->m_isToggle
)
164 tool
->m_toggleState
= !tool
->m_toggleState
;
167 wxMemoryDC
*dc2
= new wxMemoryDC
;
169 DrawTool(dc
, *dc2
, tool
);
173 else if (event
.RightDown())
175 OnRightClick(tool
->m_index
, x
, y
);
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.
182 if (event
.LeftUp() && tool
->m_enabled
&&
183 (tool
->m_toggleState
|| tool
->m_isToggle
))
185 if (!tool
->m_isToggle
)
186 tool
->m_toggleState
= FALSE
;
188 // Pass the OnLeftClick event to tool
189 if (!OnLeftClick(tool
->m_index
, tool
->m_toggleState
) && tool
->m_isToggle
)
191 // If it was a toggle, and OnLeftClick says No Toggle allowed,
192 // then change it back
193 tool
->m_toggleState
= !tool
->m_toggleState
;
197 wxMemoryDC
*dc2
= new wxMemoryDC
;
198 DrawTool(dc
, *dc2
, tool
);
203 void wxToolBarSimple::DrawTool(wxDC
& dc
, wxMemoryDC
& memDC
, wxToolBarTool
*tool
)
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
);
211 wxBitmap
*bitmap
= tool
->m_toggleState
? (& tool
->m_bitmap2
) : (& tool
->m_bitmap1
);
213 if (bitmap
&& bitmap
->Ok())
215 if (bitmap
->GetPalette())
216 memDC
.SetPalette(*bitmap
->GetPalette());
218 int ax
= (int)tool
->m_x
,
220 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
221 by
= (int)(tool
->m_y
+tool
->GetHeight());
223 memDC
.SelectObject(*bitmap
);
224 if (m_windowStyle
& wxTB_3DBUTTONS
)
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
244 dc
.Blit(tool
->m_x
, tool
->m_y
,
245 bitmap
->GetWidth(), bitmap
->GetHeight(),
248 memDC
.SelectObject(wxNullBitmap
);
249 memDC
.SetPalette(wxNullPalette
);
251 // No second bitmap, so draw a thick line around bitmap, or invert if mono
252 else if (tool
->m_toggleState
)
254 bool drawBorder
= FALSE
;
255 #ifdef __X__ // X doesn't invert properly on colour
256 drawBorder
= wxColourDisplay();
257 #else // Inversion works fine under Windows
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
);
270 if (m_windowStyle
& wxTB_3DBUTTONS
)
272 int ax
= (int)tool
->m_x
,
274 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
275 by
= (int)(tool
->m_y
+tool
->GetHeight());
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
);
298 long w
= tool
->m_bitmap1
.GetWidth();
299 long h
= tool
->m_bitmap1
.GetHeight();
300 wxPen
thick_black_pen("BLACK", 3, wxSOLID
);
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
,
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
);
316 void wxToolBarSimple::ToggleTool(int index
, bool toggle
)
318 wxNode
*node
= m_tools
.Find((long)index
);
321 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
322 if (tool
&& tool
->m_isToggle
)
324 bool oldState
= tool
->m_toggleState
;
325 tool
->m_toggleState
= toggle
;
327 if (oldState
!= toggle
)
331 DrawTool(dc
, memDC
, tool
);
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.
341 void wxToolBarSimple::SpringUpButton(int index
)
343 wxNode
*node
=m_tools
.Find((long)index
);
345 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
346 if (tool
&& !tool
->m_isToggle
&& tool
->m_toggleState
){
347 tool
->m_toggleState
= FALSE
;
350 DrawTool(dc
, memDC
, tool
);
352 else if (tool
&& tool
->m_isToggle
){
353 tool
->m_toggleState
= !tool
->m_toggleState
;
356 DrawTool(dc
, memDC
, tool
);
361 void wxToolBarSimple::Layout(void)
363 m_currentRowsOrColumns
= 0;
366 int maxToolWidth
= 0;
367 int maxToolHeight
= 0;
371 // Find the maximum tool width and height
372 wxNode
*node
= m_tools
.First();
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();
383 int separatorSize
= m_toolSeparation
;
385 node
= m_tools
.First();
388 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
389 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
391 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
393 if (m_currentRowsOrColumns
>= m_maxCols
)
394 m_lastY
+= separatorSize
;
396 m_lastX
+= separatorSize
;
400 if (m_currentRowsOrColumns
>= m_maxRows
)
401 m_lastX
+= separatorSize
;
403 m_lastY
+= separatorSize
;
406 else if (tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
)
408 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
410 if (m_currentRowsOrColumns
>= m_maxCols
)
412 m_currentRowsOrColumns
= 0;
414 m_lastY
+= maxToolHeight
+ m_toolPacking
;
416 tool
->m_x
= (long) (m_lastX
+ (maxToolWidth
- tool
->GetWidth())/2.0);
417 tool
->m_y
= (long) (m_lastY
+ (maxToolHeight
- tool
->GetHeight())/2.0);
419 m_lastX
+= maxToolWidth
+ m_toolPacking
;
423 if (m_currentRowsOrColumns
>= m_maxRows
)
425 m_currentRowsOrColumns
= 0;
426 m_lastX
+= (maxToolWidth
+ m_toolPacking
);
429 tool
->m_x
= (long) (m_lastX
+ (maxToolWidth
- tool
->GetWidth())/2.0);
430 tool
->m_y
= (long) (m_lastY
+ (maxToolHeight
- tool
->GetHeight())/2.0);
432 m_lastY
+= maxToolHeight
+ m_toolPacking
;
434 m_currentRowsOrColumns
++;
437 if (m_lastX
> m_maxWidth
)
438 m_maxWidth
= m_lastX
;
439 if (m_lastY
> m_maxHeight
)
440 m_maxHeight
= m_lastY
;
444 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
445 m_maxWidth
+= maxToolWidth
;
447 m_maxHeight
+= maxToolHeight
;
449 m_maxWidth
+= m_xMargin
;
450 m_maxHeight
+= m_yMargin
;