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 // TODO: eliminate these; use system colours
43 static wxPen
* white_pen
= NULL
,
44 * dark_grey_pen
= NULL
,
48 wxToolBarSimple::wxToolBarSimple(void)
52 bool wxToolBarSimple::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long style
,
53 int direction
, int RowsOrColumns
, const wxString
& name
)
55 if ( ! wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
58 // Set it to grey (or other 3D face colour)
59 wxSystemSettings settings
;
60 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_3DFACE
));
61 SetDefaultBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_3DFACE
));
65 white_pen
= new wxPen
;
66 white_pen
->SetColour( "WHITE" );
68 if ( dark_grey_pen
== 0 )
70 dark_grey_pen
= new wxPen
;
71 dark_grey_pen
->SetColour( 85,85,85 );
75 black_pen
= new wxPen
;
76 black_pen
->SetColour( "BLACK" );
78 if ( thick_black_pen
== 0 )
80 thick_black_pen
= new wxPen("BLACK", 3, wxSOLID
);
82 m_tilingDirection
= direction
;
83 m_rowsOrColumns
= RowsOrColumns
;
84 if ( m_tilingDirection
== wxVERTICAL
)
85 { m_lastX
= 3; m_lastY
= 7; }
87 { m_lastX
= 7; m_lastY
= 3; }
88 m_maxWidth
= m_maxHeight
= 0;
89 m_pressedTool
= m_currentTool
= -1;
98 wxToolBarSimple::~wxToolBarSimple ()
102 void wxToolBarSimple::OnPaint (wxPaintEvent
& event
)
107 static int count
= 0;
108 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
115 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
117 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
118 if (tool
->m_toolStyle
== wxTOOL_STYLE_BUTTON
)
119 DrawTool(dc
, mem_dc
, tool
);
125 void wxToolBarSimple::OnSize ( wxSizeEvent
& event
)
127 wxToolBarBase::OnSize(event
);
130 void wxToolBarSimple::OnKillFocus (wxFocusEvent
& event
)
132 OnMouseEnter(m_pressedTool
= m_currentTool
= -1);
135 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent
& event
)
138 event
.Position(&x
, &y
);
139 wxToolBarTool
*tool
= FindToolForPosition(x
, y
);
143 if (m_currentTool
> -1)
145 if (event
.LeftIsDown())
146 SpringUpButton(m_currentTool
);
153 if (!event
.IsButton())
155 if (tool
->m_index
!= m_currentTool
)
157 // If the left button is kept down and moved over buttons,
158 // press those buttons.
159 if (event
.LeftIsDown() && tool
->m_enabled
) {
160 SpringUpButton(m_currentTool
);
161 tool
->m_toggleState
= !tool
->m_toggleState
;
162 wxMemoryDC
*dc2
= new wxMemoryDC
;
164 DrawTool(dc
, *dc2
, tool
);
167 OnMouseEnter(tool
->m_index
);
168 m_currentTool
= tool
->m_index
;
173 // Left button pressed.
174 if (event
.LeftDown() && tool
->m_enabled
)
176 tool
->m_toggleState
= !tool
->m_toggleState
;
177 wxMemoryDC
*dc2
= new wxMemoryDC
;
179 DrawTool(dc
, *dc2
, tool
);
182 else if (event
.RightDown())
184 OnRightClick(tool
->m_index
, x
, y
);
186 // Left Button Released. Only this action confirms selection.
187 // If the button is enabled and it is not a toggle tool and it is
188 // in the pressed state, then raise the button and call OnLeftClick.
190 if (event
.LeftUp() && tool
->m_enabled
&&
191 (tool
->m_toggleState
|| tool
->m_isToggle
)){
192 if (!tool
->m_isToggle
)
193 tool
->m_toggleState
= FALSE
;
194 // Pass the OnLeftClick event to tool
195 if (!OnLeftClick(tool
->m_index
, tool
->m_toggleState
) && tool
->m_isToggle
)
196 // If it was a toggle, and OnLeftClick says No Toggle allowed,
197 // then change it back
198 tool
->m_toggleState
= !tool
->m_toggleState
;
200 wxMemoryDC
*dc2
= new wxMemoryDC
;
201 DrawTool(dc
, *dc2
, tool
);
206 void wxToolBarSimple::DrawTool(wxDC
& dc
, wxMemoryDC
& memDC
, wxToolBarTool
*tool
)
210 wxBitmap
*bitmap
= tool
->m_toggleState
? (& tool
->m_bitmap2
) : (& tool
->m_bitmap1
);
212 if (bitmap
&& bitmap
->Ok())
214 if (bitmap
->GetColourMap())
215 memDC
.SetPalette(*bitmap
->GetColourMap());
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();
300 memDC
.SelectObject(tool
->m_bitmap1
);
301 dc
.SetClippingRegion(tool
->m_x
, tool
->m_y
, w
, h
);
302 dc
.Blit(tool
->m_x
, tool
->m_y
, w
, h
,
304 dc
.SetPen(*thick_black_pen
);
305 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
306 dc
.DrawRectangle(x
, y
, w
-1, h
-1);
307 dc
.DestroyClippingRegion();
308 memDC
.SelectObject(wxNullBitmap
);
314 void wxToolBarSimple::ToggleTool(int index
, bool toggle
)
316 wxNode
*node
= m_tools
.Find((long)index
);
319 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
320 if (tool
&& tool
->m_isToggle
)
322 bool oldState
= tool
->m_toggleState
;
323 tool
->m_toggleState
= toggle
;
325 if (oldState
!= toggle
)
329 DrawTool(dc
, memDC
, tool
);
335 // Okay, so we've left the tool we're in ... we must check if
336 // the tool we're leaving was a 'sprung push button' and if so,
337 // spring it back to the up state.
339 void wxToolBarSimple::SpringUpButton(int index
)
341 wxNode
*node
=m_tools
.Find((long)index
);
343 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
344 if (tool
&& !tool
->m_isToggle
&& tool
->m_toggleState
){
345 tool
->m_toggleState
= FALSE
;
348 DrawTool(dc
, memDC
, tool
);
350 else if (tool
&& tool
->m_isToggle
){
351 tool
->m_toggleState
= !tool
->m_toggleState
;
354 DrawTool(dc
, memDC
, tool
);