]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarsmpl.cpp
wxFrame::CreateToolBar() stuff
[wxWidgets.git] / src / common / tbarsmpl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tbarsmpl.cpp
3 // Purpose: wxToolBarSimple
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "tbarsmpl.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx.h"
25 #endif
26
27 #if USE_TOOLBAR
28
29 #include "wx/tbarsmpl.h"
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxToolBarSimple, wxToolBarBase)
33
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)
39 END_EVENT_TABLE()
40 #endif
41
42 // TODO: eliminate these; use system colours
43 static wxPen * white_pen = NULL,
44 * dark_grey_pen = NULL,
45 * black_pen = NULL,
46 * thick_black_pen;
47
48 wxToolBarSimple::wxToolBarSimple(void)
49 {
50 }
51
52 bool wxToolBarSimple::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style,
53 int direction, int RowsOrColumns, const wxString& name )
54 {
55 if ( ! wxWindow::Create(parent, id, pos, size, style, name) )
56 return FALSE;
57
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));
62
63 if ( white_pen == 0 )
64 {
65 white_pen = new wxPen;
66 white_pen->SetColour( "WHITE" );
67 }
68 if ( dark_grey_pen == 0 )
69 {
70 dark_grey_pen = new wxPen;
71 dark_grey_pen->SetColour( 85,85,85 );
72 }
73 if ( black_pen == 0 )
74 {
75 black_pen = new wxPen;
76 black_pen->SetColour( "BLACK" );
77 }
78 if ( thick_black_pen == 0 )
79 {
80 thick_black_pen = new wxPen("BLACK", 3, wxSOLID);
81 }
82 m_tilingDirection = direction;
83 m_rowsOrColumns = RowsOrColumns;
84 if ( m_tilingDirection == wxVERTICAL )
85 { m_lastX = 3; m_lastY = 7; }
86 else
87 { m_lastX = 7; m_lastY = 3; }
88 m_maxWidth = m_maxHeight = 0;
89 m_pressedTool = m_currentTool = -1;
90 m_xMargin = 0;
91 m_yMargin = 0;
92 m_toolPacking = 1;
93 m_toolSeparation = 5;
94
95 return TRUE;
96 }
97
98 wxToolBarSimple::~wxToolBarSimple ()
99 {
100 }
101
102 void wxToolBarSimple::OnPaint (wxPaintEvent& event)
103 {
104 wxPaintDC dc(this);
105 PrepareDC(dc);
106
107 static int count = 0;
108 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
109 if ( count > 0 )
110 return;
111 count++;
112
113 wxMemoryDC mem_dc;
114
115 for ( wxNode *node = m_tools.First(); node; node = node->Next() )
116 {
117 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
118 if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON)
119 DrawTool(dc, mem_dc, tool);
120 }
121
122 count--;
123 }
124
125 void wxToolBarSimple::OnSize ( wxSizeEvent& event )
126 {
127 wxToolBarBase::OnSize(event);
128 }
129
130 void wxToolBarSimple::OnKillFocus (wxFocusEvent& event)
131 {
132 OnMouseEnter(m_pressedTool = m_currentTool = -1);
133 }
134
135 void wxToolBarSimple::OnMouseEvent ( wxMouseEvent & event )
136 {
137 long x, y;
138 event.Position(&x, &y);
139 wxToolBarTool *tool = FindToolForPosition(x, y);
140
141 if (!tool)
142 {
143 if (m_currentTool > -1)
144 {
145 if (event.LeftIsDown())
146 SpringUpButton(m_currentTool);
147 m_currentTool = -1;
148 OnMouseEnter(-1);
149 }
150 return;
151 }
152
153 if (!event.IsButton())
154 {
155 if (tool->m_index != m_currentTool)
156 {
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;
163 wxClientDC dc(this);
164 DrawTool(dc, *dc2, tool);
165 delete dc2;
166 }
167 OnMouseEnter(tool->m_index);
168 m_currentTool = tool->m_index;
169 }
170 return;
171 }
172
173 // Left button pressed.
174 if (event.LeftDown() && tool->m_enabled)
175 {
176 tool->m_toggleState = !tool->m_toggleState;
177 wxMemoryDC *dc2 = new wxMemoryDC;
178 wxClientDC dc(this);
179 DrawTool(dc, *dc2, tool);
180 delete dc2;
181 }
182 else if (event.RightDown())
183 {
184 OnRightClick(tool->m_index, x, y);
185 }
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.
189 //
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;
199 wxClientDC dc(this);
200 wxMemoryDC *dc2 = new wxMemoryDC;
201 DrawTool(dc, *dc2, tool);
202 delete dc2;
203 }
204 }
205
206 void wxToolBarSimple::DrawTool(wxDC& dc, wxMemoryDC& memDC, wxToolBarTool *tool)
207 {
208 PrepareDC(dc);
209
210 wxBitmap *bitmap = tool->m_toggleState ? (& tool->m_bitmap2) : (& tool->m_bitmap1);
211
212 if (bitmap && bitmap->Ok())
213 {
214 if (bitmap->GetColourMap())
215 memDC.SetPalette(*bitmap->GetColourMap());
216
217 int ax = (int)tool->m_x,
218 ay = (int)tool->m_y,
219 bx = (int)(tool->m_x+tool->GetWidth()),
220 by = (int)(tool->m_y+tool->GetHeight());
221
222 memDC.SelectObject(*bitmap);
223 if (m_windowStyle & wxTB_3DBUTTONS)
224 {
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
240 }
241 else
242 {
243 dc.Blit(tool->m_x, tool->m_y,
244 bitmap->GetWidth(), bitmap->GetHeight(),
245 &memDC, 0, 0);
246 }
247 memDC.SelectObject(wxNullBitmap);
248 memDC.SetPalette(wxNullPalette);
249 }
250 // No second bitmap, so draw a thick line around bitmap, or invert if mono
251 else if (tool->m_toggleState)
252 {
253 bool drawBorder = FALSE;
254 #ifdef __X__ // X doesn't invert properly on colour
255 drawBorder = wxColourDisplay();
256 #else // Inversion works fine under Windows
257 drawBorder = FALSE;
258 #endif
259
260 if (!drawBorder)
261 {
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);
266 }
267 else
268 {
269 if (m_windowStyle & wxTB_3DBUTTONS)
270 {
271 int ax = (int)tool->m_x,
272 ay = (int)tool->m_y,
273 bx = (int)(tool->m_x+tool->GetWidth()),
274 by = (int)(tool->m_y+tool->GetHeight());
275
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);
292 }
293 else
294 {
295 long x = tool->m_x;
296 long y = tool->m_y;
297 long w = tool->m_bitmap1.GetWidth();
298 long h = tool->m_bitmap1.GetHeight();
299
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,
303 &memDC, 0, 0);
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);
309 }
310 }
311 }
312 }
313
314 void wxToolBarSimple::ToggleTool(int index, bool toggle)
315 {
316 wxNode *node = m_tools.Find((long)index);
317 if (node)
318 {
319 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
320 if (tool && tool->m_isToggle)
321 {
322 bool oldState = tool->m_toggleState;
323 tool->m_toggleState = toggle;
324
325 if (oldState != toggle)
326 {
327 wxMemoryDC memDC;
328 wxClientDC dc(this);
329 DrawTool(dc, memDC, tool);
330 }
331 }
332 }
333 }
334
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.
338 //
339 void wxToolBarSimple::SpringUpButton(int index)
340 {
341 wxNode *node=m_tools.Find((long)index);
342 if (node) {
343 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
344 if (tool && !tool->m_isToggle && tool->m_toggleState){
345 tool->m_toggleState = FALSE;
346 wxMemoryDC memDC;
347 wxClientDC dc(this);
348 DrawTool(dc, memDC, tool);
349 }
350 else if (tool && tool->m_isToggle){
351 tool->m_toggleState = !tool->m_toggleState;
352 wxMemoryDC memDC;
353 wxClientDC dc(this);
354 DrawTool(dc, memDC, tool);
355 }
356 }
357 }
358
359 #endif