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
& WXUNUSED(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
& WXUNUSED(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())
216 if (bitmap
->GetPalette())
217 memDC
.SetPalette(*bitmap
->GetPalette());
220 int ax
= (int)tool
->m_x
,
222 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
223 by
= (int)(tool
->m_y
+tool
->GetHeight());
225 memDC
.SelectObject(*bitmap
);
226 if (m_windowStyle
& wxTB_3DBUTTONS
)
228 dc
.SetClippingRegion(ax
, ay
, (bx
-ax
+1), (by
-ay
+1));
229 dc
.Blit((ax
+1), (ay
+1), (bx
-ax
-2), (by
-ay
-2), &memDC
, 0, 0);
230 wxPen
* old_pen
= & dc
.GetPen();
231 dc
.SetPen( white_pen
);
232 dc
.DrawLine(ax
,(by
-1),ax
,ay
);
233 dc
.DrawLine(ax
,ay
,(bx
-1),ay
);
234 dc
.SetPen( dark_grey_pen
);
235 dc
.DrawLine((bx
-1),(ay
+1),(bx
-1),(by
-1));
236 dc
.DrawLine((bx
-1),(by
-1),(ax
+1),(by
-1));
237 dc
.SetPen( black_pen
);
238 dc
.DrawLine(bx
,ay
,bx
,by
);
239 dc
.DrawLine(bx
,by
,ax
,by
);
240 dc
.SetPen( *old_pen
);
241 dc
.DestroyClippingRegion();
242 // Select bitmap out of the DC
246 dc
.Blit(tool
->m_x
, tool
->m_y
,
247 bitmap
->GetWidth(), bitmap
->GetHeight(),
250 memDC
.SelectObject(wxNullBitmap
);
252 memDC
.SetPalette(wxNullPalette
);
255 // No second bitmap, so draw a thick line around bitmap, or invert if mono
256 else if (tool
->m_toggleState
)
258 bool drawBorder
= FALSE
;
259 #ifdef __X__ // X doesn't invert properly on colour
260 drawBorder
= wxColourDisplay();
261 #else // Inversion works fine under Windows
267 memDC
.SelectObject(tool
->m_bitmap1
);
268 dc
.Blit(tool
->m_x
, tool
->m_y
, tool
->GetWidth(), tool
->GetHeight(),
269 &memDC
, 0, 0, wxSRC_INVERT
);
270 memDC
.SelectObject(wxNullBitmap
);
274 if (m_windowStyle
& wxTB_3DBUTTONS
)
276 int ax
= (int)tool
->m_x
,
278 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
279 by
= (int)(tool
->m_y
+tool
->GetHeight());
281 memDC
.SelectObject(tool
->m_bitmap1
);
282 dc
.SetClippingRegion(ax
, ay
, (bx
-ax
+1), (by
-ay
+1));
283 dc
.Blit((ax
+2), (ay
+2), (bx
-ax
-2), (by
-ay
-2), &memDC
, 0, 0);
284 wxPen
* old_pen
= & dc
.GetPen();
285 dc
.SetPen( black_pen
);
286 dc
.DrawLine(ax
,(by
-1),ax
,ay
);
287 dc
.DrawLine(ax
,ay
,(bx
-1),ay
);
288 dc
.SetPen( dark_grey_pen
);
289 dc
.DrawLine((ax
+1),(by
-2),(ax
+1),(ay
+1));
290 dc
.DrawLine((ax
+1),(ay
+1),(bx
-2),(ay
+1));
291 dc
.SetPen( white_pen
);
292 dc
.DrawLine(bx
,ay
,bx
,by
);
293 dc
.DrawLine(bx
,by
,ax
,by
);
294 dc
.SetPen( *old_pen
);
295 dc
.DestroyClippingRegion();
296 memDC
.SelectObject(wxNullBitmap
);
302 long w
= tool
->m_bitmap1
.GetWidth();
303 long h
= tool
->m_bitmap1
.GetHeight();
304 wxPen
thick_black_pen("BLACK", 3, wxSOLID
);
306 memDC
.SelectObject(tool
->m_bitmap1
);
307 dc
.SetClippingRegion(tool
->m_x
, tool
->m_y
, w
, h
);
308 dc
.Blit(tool
->m_x
, tool
->m_y
, w
, h
,
310 dc
.SetPen(thick_black_pen
);
311 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
312 dc
.DrawRectangle(x
, y
, w
-1, h
-1);
313 dc
.DestroyClippingRegion();
314 memDC
.SelectObject(wxNullBitmap
);
320 void wxToolBarSimple::ToggleTool(int index
, bool toggle
)
322 wxNode
*node
= (wxNode
*) NULL
;
323 node
= m_tools
.Find((long)index
);
326 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
327 if (tool
&& tool
->m_isToggle
)
329 bool oldState
= tool
->m_toggleState
;
330 tool
->m_toggleState
= toggle
;
332 if (oldState
!= toggle
)
336 DrawTool(dc
, memDC
, tool
);
342 // Okay, so we've left the tool we're in ... we must check if
343 // the tool we're leaving was a 'sprung push button' and if so,
344 // spring it back to the up state.
346 void wxToolBarSimple::SpringUpButton(int index
)
348 wxNode
*node
= (wxNode
*) NULL
;
349 node
=m_tools
.Find((long)index
);
351 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
352 if (tool
&& !tool
->m_isToggle
&& tool
->m_toggleState
){
353 tool
->m_toggleState
= FALSE
;
356 DrawTool(dc
, memDC
, tool
);
358 else if (tool
&& tool
->m_isToggle
){
359 tool
->m_toggleState
= !tool
->m_toggleState
;
362 DrawTool(dc
, memDC
, tool
);
367 void wxToolBarSimple::LayoutTools(void)
369 m_currentRowsOrColumns
= 0;
372 int maxToolWidth
= 0;
373 int maxToolHeight
= 0;
377 // Find the maximum tool width and height
378 wxNode
*node
= m_tools
.First();
381 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
382 if (tool
->GetWidth() > maxToolWidth
)
383 maxToolWidth
= (int)tool
->GetWidth();
384 if (tool
->GetHeight() > maxToolHeight
)
385 maxToolHeight
= (int)tool
->GetHeight();
389 int separatorSize
= m_toolSeparation
;
391 node
= m_tools
.First();
394 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
395 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
397 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
399 if (m_currentRowsOrColumns
>= m_maxCols
)
400 m_lastY
+= separatorSize
;
402 m_lastX
+= separatorSize
;
406 if (m_currentRowsOrColumns
>= m_maxRows
)
407 m_lastX
+= separatorSize
;
409 m_lastY
+= separatorSize
;
412 else if (tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
)
414 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
416 if (m_currentRowsOrColumns
>= m_maxCols
)
418 m_currentRowsOrColumns
= 0;
420 m_lastY
+= maxToolHeight
+ m_toolPacking
;
422 tool
->m_x
= (long) (m_lastX
+ (maxToolWidth
- tool
->GetWidth())/2.0);
423 tool
->m_y
= (long) (m_lastY
+ (maxToolHeight
- tool
->GetHeight())/2.0);
425 m_lastX
+= maxToolWidth
+ m_toolPacking
;
429 if (m_currentRowsOrColumns
>= m_maxRows
)
431 m_currentRowsOrColumns
= 0;
432 m_lastX
+= (maxToolWidth
+ m_toolPacking
);
435 tool
->m_x
= (long) (m_lastX
+ (maxToolWidth
- tool
->GetWidth())/2.0);
436 tool
->m_y
= (long) (m_lastY
+ (maxToolHeight
- tool
->GetHeight())/2.0);
438 m_lastY
+= maxToolHeight
+ m_toolPacking
;
440 m_currentRowsOrColumns
++;
443 if (m_lastX
> m_maxWidth
)
444 m_maxWidth
= m_lastX
;
445 if (m_lastY
> m_maxHeight
)
446 m_maxHeight
= m_lastY
;
450 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
451 m_maxWidth
+= maxToolWidth
;
453 m_maxHeight
+= maxToolHeight
;
455 m_maxWidth
+= m_xMargin
;
456 m_maxHeight
+= m_yMargin
;