Put tool disabling back in, but we need to debug wxCreateGreyedImage
[wxWidgets.git] / src / univ / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/toolbar.cpp
3 // Author: Robert Roebling
4 // Id: $Id$
5 // Copyright: (c) 2001 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #ifdef __GNUG__
18 #pragma implementation "univtoolbar.h"
19 #endif
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #ifndef WX_PRECOMP
29 #include "wx/utils.h"
30 #include "wx/app.h"
31 #endif
32
33 #include "wx/toolbar.h"
34 #include "wx/image.h"
35
36 //-----------------------------------------------------------------------------
37 // wxToolBar
38 //-----------------------------------------------------------------------------
39
40 BEGIN_EVENT_TABLE(wxToolBar,wxToolBarBase)
41 EVT_MOUSE_EVENTS( wxToolBar::OnMouse )
42 EVT_PAINT( wxToolBar::OnPaint )
43 EVT_SIZE( wxToolBar::OnSize )
44 END_EVENT_TABLE()
45
46 bool wxToolBar::Create( wxWindow *parent, wxWindowID id,
47 const wxPoint& pos, const wxSize& size,
48 long style, const wxString& name )
49 {
50 bool ret = wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name );
51
52 return ret;
53 }
54
55 void wxToolBar::Init()
56 {
57 // TODO: this is from tbarbase.cpp, but should be in
58 // wxToolbarBase::Init
59 // the list owns the pointers
60 m_tools.DeleteContents(TRUE);
61 m_xMargin = m_yMargin = 0;
62 m_maxRows = m_maxCols = 0;
63 // End TODO
64
65 m_maxWidth = 0;
66 m_maxHeight = 0;
67
68 m_captured = NULL;
69 SetToolBitmapSize( wxSize(16,15) );
70 }
71
72 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
73 {
74 return NULL;
75 }
76
77 void wxToolBar::SetToolShortHelp(int id, const wxString& helpString)
78 {
79 }
80
81 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *tool)
82 {
83 return TRUE;
84 }
85
86 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
87 {
88 return TRUE;
89 }
90
91 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
92 {
93 // Comment this out if you don't want the disabled look,
94 // which currently acts weirdly for the scissors icon
95 // in the toolbar sample. See src/common/tbarbase.cpp
96 // for the wxCreateGreyedImage function.
97 #if 1
98 // Created disabled-state bitmap on demand
99 if (!enable && !tool->GetBitmap2().Ok())
100 {
101 wxImage in(tool->GetBitmap1());
102 wxImage out(tool->GetBitmap1());
103
104 wxCreateGreyedImage(in, out);
105
106 tool->SetBitmap2(out.ConvertToBitmap());
107 }
108 RefreshTool((wxToolBarTool*) tool);
109 #endif
110 }
111
112 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
113 {
114 wxToolBarTool *my_tool = (wxToolBarTool*) tool;
115
116 bool refresh = (my_tool->IsToggled() != toggle);
117
118 my_tool->m_isDown = toggle;
119
120 if (refresh)
121 RefreshTool( my_tool );
122 }
123
124 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool toggle)
125 {
126 }
127
128 wxToolBarToolBase *wxToolBar::CreateTool(int id,
129 const wxBitmap& bitmap1,
130 const wxBitmap& bitmap2,
131 bool toggle,
132 wxObject *clientData,
133 const wxString& shortHelpString,
134 const wxString& longHelpString)
135 {
136 return new wxToolBarTool( this, id, bitmap1, bitmap2, toggle,
137 clientData, shortHelpString, longHelpString);
138 }
139
140 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
141 {
142 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
143
144 return NULL;
145 }
146
147 void wxToolBar::RefreshTool( wxToolBarTool *tool )
148 {
149 wxRect rect( tool->m_x, tool->m_y, m_defaultWidth+6, m_defaultHeight+6 );
150 Refresh( TRUE, &rect );
151 }
152
153 void wxToolBar::DrawToolBarTool( wxToolBarTool *tool, wxDC &dc, bool down )
154 {
155 const wxBitmap& bitmap = (tool->IsEnabled() || !tool->GetBitmap2().Ok()) ? tool->GetBitmap1() : tool->GetBitmap2() ;
156 if (down)
157 {
158 dc.DrawBitmap( bitmap, tool->m_x+4, tool->m_y+4, TRUE );
159
160 dc.SetPen( *wxGREY_PEN );
161 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
162 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
163
164 dc.SetPen( *wxBLACK_PEN );
165 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+1 );
166 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+1, tool->m_y+m_defaultHeight+4 );
167
168 dc.SetPen( *wxWHITE_PEN );
169 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
170 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
171 }
172 else
173 {
174 dc.DrawBitmap( bitmap, tool->m_x+3, tool->m_y+3, TRUE );
175
176 dc.SetPen( *wxWHITE_PEN );
177 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
178 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
179 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y, tool->m_x+m_defaultWidth+4, tool->m_y+2 );
180 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+4, tool->m_x+2, tool->m_y+m_defaultHeight+4 );
181
182 dc.SetPen( *wxBLACK_PEN );
183 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
184 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
185
186 dc.SetPen( *wxGREY_PEN );
187 dc.DrawLine( tool->m_x+1, tool->m_y+m_defaultHeight+4, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+4 );
188 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+m_defaultHeight+5 );
189 }
190 }
191
192 void wxToolBar::OnPaint(wxPaintEvent &event)
193 {
194 wxPaintDC dc(this);
195
196 wxSize clientSize = GetClientSize();
197 dc.SetPen( *wxBLACK_PEN );
198 dc.DrawLine( 0,0, clientSize.x,0 );
199
200 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
201 node;
202 node = node->GetNext() )
203 {
204 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
205
206 if (tool->GetId() == -1) continue;
207
208 DrawToolBarTool( tool, dc, tool->m_isDown );
209 }
210 }
211
212 bool wxToolBar::Realize()
213 {
214 if (!wxToolBarBase::Realize())
215 return FALSE;
216
217 int x;
218 int y;
219
220 if (GetWindowStyleFlag() & wxTB_VERTICAL)
221 {
222 x = m_xMargin;
223 y = 5;
224 }
225 else
226 {
227 y = m_yMargin;
228 x = 5;
229 }
230
231 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
232 node;
233 node = node->GetNext() )
234 {
235 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
236
237 if (GetWindowStyleFlag() & wxTB_VERTICAL)
238 {
239 if (tool->GetId() == -1)
240 {
241 y += 6;
242 continue;
243 }
244 tool->m_x = m_xMargin;
245 tool->m_y = y;
246 y += m_defaultHeight + 6;
247
248 // Calculate the maximum height or width (depending on style)
249 // so we know how to size the toolbar in Realize.
250 // We could get the size of the tool instead of the
251 // default bitmap size
252 if (m_maxWidth < (m_defaultWidth + 2*(m_xMargin + 2)))
253 m_maxWidth = (m_defaultWidth + 2*(m_xMargin + 2)) ;
254 }
255 else
256 {
257 if (tool->GetId() == -1)
258 {
259 x += 6;
260 continue;
261 }
262 tool->m_x = x;
263 tool->m_y = m_yMargin;
264 x += m_defaultWidth + 6;
265
266 // Calculate the maximum height or width (depending on style)
267 // so we know how to size the toolbar in Realize.
268 // We could get the size of the tool instead of the
269 // default bitmap size
270 if (m_maxHeight < (m_defaultHeight + 2*(m_yMargin + 2)))
271 m_maxHeight = (m_defaultHeight + 2*(m_yMargin + 2)) ;
272 }
273
274 }
275
276 wxSize sz = GetSize();
277 if (GetWindowStyleFlag() & wxTB_VERTICAL)
278 {
279 SetSize(m_maxWidth, sz.y);
280 }
281 else
282 {
283 SetSize(sz.x, m_maxHeight);
284 }
285
286 // Commenting out Robert's fix since the above should be
287 // more general
288 // SetSize( x+16, m_defaultHeight + 14 );
289
290 return TRUE;
291 }
292
293 void wxToolBar::OnMouse(wxMouseEvent &event)
294 {
295 wxToolBarTool *hit = NULL;
296 int x = event.GetX();
297 int y = event.GetY();
298
299 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
300 node;
301 node = node->GetNext() )
302 {
303 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
304
305 if ((x > tool->m_x) && (x < tool->m_x+m_defaultWidth+5) &&
306 (y > tool->m_y) && (y < tool->m_y+m_defaultHeight+5))
307 {
308 hit = tool;
309 break;
310 }
311 }
312
313 if (event.LeftDown() && (hit) && hit->IsEnabled())
314 {
315 CaptureMouse();
316 m_captured = hit;
317
318 m_captured->m_isDown = TRUE;
319 RefreshTool( m_captured );
320
321 return;
322 }
323
324 if (event.Dragging() && (m_captured))
325 {
326 bool is_down = (hit == m_captured);
327 if (is_down != m_captured->m_isDown)
328 {
329 m_captured->m_isDown = is_down;
330 RefreshTool( m_captured );
331 }
332
333 return;
334 }
335
336 if (event.LeftUp() && (m_captured))
337 {
338 ReleaseMouse();
339
340 m_captured->m_isDown = FALSE;
341 RefreshTool( m_captured );
342
343 if (hit == m_captured)
344 {
345 wxCommandEvent cevent( wxEVT_COMMAND_TOOL_CLICKED, m_captured->GetId() );
346 cevent.SetEventObject( this );
347 // cevent.SetExtraLong((long) toggleDown);
348 GetEventHandler()->ProcessEvent( cevent );
349 }
350
351 m_captured = NULL;
352
353 return;
354 }
355 }
356