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
, const wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, const long style
,
53 const int direction
, const 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
);
122 /* Old code which drew a line beneath the toolbar - not generally
125 GetClientSize( &w, &h );
126 wxPen * old_pen = dc.GetPen();
127 dc.SetPen( *white_pen );
128 dc.DrawLine(0,0,w,0);
129 dc.SetPen( *black_pen );
130 dc.DrawLine(0,(h-1),w,(h-1));
131 dc.SetPen( *old_pen );
137 void wxToolBarSimple::OnSize ( wxSizeEvent
& event
)
139 wxToolBarBase::OnSize(event
);
142 void wxToolBarSimple::OnKillFocus (wxFocusEvent
& event
)
144 OnMouseEnter(m_pressedTool
= m_currentTool
= -1);
147 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent
& event
)
150 event
.Position(&x
, &y
);
151 wxToolBarTool
*tool
= FindToolForPosition(x
, y
);
155 if (m_currentTool
> -1)
157 if (event
.LeftIsDown())
158 SpringUpButton(m_currentTool
);
165 if (!event
.IsButton())
167 if (tool
->m_index
!= m_currentTool
)
169 // If the left button is kept down and moved over buttons,
170 // press those buttons.
171 if (event
.LeftIsDown() && tool
->m_enabled
) {
172 SpringUpButton(m_currentTool
);
173 tool
->m_toggleState
= !tool
->m_toggleState
;
174 wxMemoryDC
*dc2
= new wxMemoryDC
;
176 DrawTool(dc
, *dc2
, tool
);
179 OnMouseEnter(tool
->m_index
);
180 m_currentTool
= tool
->m_index
;
185 // Left button pressed.
186 if (event
.LeftDown() && tool
->m_enabled
)
188 tool
->m_toggleState
= !tool
->m_toggleState
;
189 wxMemoryDC
*dc2
= new wxMemoryDC
;
191 DrawTool(dc
, *dc2
, tool
);
194 else if (event
.RightDown())
196 OnRightClick(tool
->m_index
, x
, y
);
198 // Left Button Released. Only this action confirms selection.
199 // If the button is enabled and it is not a toggle tool and it is
200 // in the pressed state, then raise the button and call OnLeftClick.
202 if (event
.LeftUp() && tool
->m_enabled
&&
203 (tool
->m_toggleState
|| tool
->m_isToggle
)){
204 if (!tool
->m_isToggle
)
205 tool
->m_toggleState
= FALSE
;
206 // Pass the OnLeftClick event to tool
207 if (!OnLeftClick(tool
->m_index
, tool
->m_toggleState
) && tool
->m_isToggle
)
208 // If it was a toggle, and OnLeftClick says No Toggle allowed,
209 // then change it back
210 tool
->m_toggleState
= !tool
->m_toggleState
;
212 wxMemoryDC
*dc2
= new wxMemoryDC
;
213 DrawTool(dc
, *dc2
, tool
);
218 void wxToolBarSimple::DrawTool(wxDC
& dc
, wxMemoryDC
& memDC
, wxToolBarTool
*tool
)
222 wxBitmap
*bitmap
= tool
->m_toggleState
? (& tool
->m_bitmap2
) : (& tool
->m_bitmap1
);
224 if (bitmap
&& bitmap
->Ok())
226 if (bitmap
->GetColourMap())
227 memDC
.SetPalette(*bitmap
->GetColourMap());
229 int ax
= (int)tool
->m_x
,
231 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
232 by
= (int)(tool
->m_y
+tool
->GetHeight());
234 memDC
.SelectObject(*bitmap
);
235 if (m_windowStyle
& wxTB_3DBUTTONS
)
237 dc
.SetClippingRegion(ax
, ay
, (bx
-ax
+1), (by
-ay
+1));
238 dc
.Blit((ax
+1), (ay
+1), (bx
-ax
-2), (by
-ay
-2), &memDC
, 0, 0);
239 wxPen
* old_pen
= dc
.GetPen();
240 dc
.SetPen( *white_pen
);
241 dc
.DrawLine(ax
,(by
-1),ax
,ay
);
242 dc
.DrawLine(ax
,ay
,(bx
-1),ay
);
243 dc
.SetPen( *dark_grey_pen
);
244 dc
.DrawLine((bx
-1),(ay
+1),(bx
-1),(by
-1));
245 dc
.DrawLine((bx
-1),(by
-1),(ax
+1),(by
-1));
246 dc
.SetPen( *black_pen
);
247 dc
.DrawLine(bx
,ay
,bx
,by
);
248 dc
.DrawLine(bx
,by
,ax
,by
);
249 dc
.SetPen( *old_pen
);
250 dc
.DestroyClippingRegion();
251 // Select bitmap out of the DC
255 dc
.Blit(tool
->m_x
, tool
->m_y
,
256 bitmap
->GetWidth(), bitmap
->GetHeight(),
259 memDC
.SelectObject(wxNullBitmap
);
260 memDC
.SetPalette(wxNullPalette
);
262 // No second bitmap, so draw a thick line around bitmap, or invert if mono
263 else if (tool
->m_toggleState
)
265 bool drawBorder
= FALSE
;
266 #ifdef __X__ // X doesn't invert properly on colour
267 drawBorder
= wxColourDisplay();
268 #else // Inversion works fine under Windows
274 memDC
.SelectObject(tool
->m_bitmap1
);
275 dc
.Blit(tool
->m_x
, tool
->m_y
, tool
->GetWidth(), tool
->GetHeight(),
276 &memDC
, 0, 0, wxSRC_INVERT
);
277 memDC
.SelectObject(wxNullBitmap
);
281 if (m_windowStyle
& wxTB_3DBUTTONS
)
283 int ax
= (int)tool
->m_x
,
285 bx
= (int)(tool
->m_x
+tool
->GetWidth()),
286 by
= (int)(tool
->m_y
+tool
->GetHeight());
288 memDC
.SelectObject(tool
->m_bitmap1
);
289 dc
.SetClippingRegion(ax
, ay
, (bx
-ax
+1), (by
-ay
+1));
290 dc
.Blit((ax
+2), (ay
+2), (bx
-ax
-2), (by
-ay
-2), &memDC
, 0, 0);
291 wxPen
* old_pen
= dc
.GetPen();
292 dc
.SetPen( *black_pen
);
293 dc
.DrawLine(ax
,(by
-1),ax
,ay
);
294 dc
.DrawLine(ax
,ay
,(bx
-1),ay
);
295 dc
.SetPen( *dark_grey_pen
);
296 dc
.DrawLine((ax
+1),(by
-2),(ax
+1),(ay
+1));
297 dc
.DrawLine((ax
+1),(ay
+1),(bx
-2),(ay
+1));
298 dc
.SetPen( *white_pen
);
299 dc
.DrawLine(bx
,ay
,bx
,by
);
300 dc
.DrawLine(bx
,by
,ax
,by
);
301 dc
.SetPen( *old_pen
);
302 dc
.DestroyClippingRegion();
303 memDC
.SelectObject(wxNullBitmap
);
309 long w
= tool
->m_bitmap1
.GetWidth();
310 long h
= tool
->m_bitmap1
.GetHeight();
312 memDC
.SelectObject(tool
->m_bitmap1
);
313 dc
.SetClippingRegion(tool
->m_x
, tool
->m_y
, w
, h
);
314 dc
.Blit(tool
->m_x
, tool
->m_y
, w
, h
,
316 dc
.SetPen(*thick_black_pen
);
317 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
318 dc
.DrawRectangle(x
, y
, w
-1, h
-1);
319 dc
.DestroyClippingRegion();
320 memDC
.SelectObject(wxNullBitmap
);
326 void wxToolBarSimple::ToggleTool(const int index
, const bool toggle
)
328 wxNode
*node
= m_tools
.Find((long)index
);
331 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
332 if (tool
&& tool
->m_isToggle
)
334 bool oldState
= tool
->m_toggleState
;
335 tool
->m_toggleState
= toggle
;
337 if (oldState
!= toggle
)
341 DrawTool(dc
, memDC
, tool
);
347 // Okay, so we've left the tool we're in ... we must check if
348 // the tool we're leaving was a 'sprung push button' and if so,
349 // spring it back to the up state.
351 void wxToolBarSimple::SpringUpButton(const int index
)
353 wxNode
*node
=m_tools
.Find((long)index
);
355 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
356 if (tool
&& !tool
->m_isToggle
&& tool
->m_toggleState
){
357 tool
->m_toggleState
= FALSE
;
360 DrawTool(dc
, memDC
, tool
);
362 else if (tool
&& tool
->m_isToggle
){
363 tool
->m_toggleState
= !tool
->m_toggleState
;
366 DrawTool(dc
, memDC
, tool
);