Added middle mouse event macros to wxListCtrl doc; added 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 // Created disabled-state bitmap on demand
94 if (!enable && !m_bitmap2.Ok())
95 {
96 wxImage in(m_bitmap1);
97 wxImage out(m_bitmap1);
98
99 wxCreateGreyedImage(in, out);
100
101 m_bitmap2 = out.ConvertToBitmap();
102 }
103 RefreshTool(tool);
104 }
105
106 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
107 {
108 wxToolBarTool *my_tool = (wxToolBarTool*) tool;
109
110 bool refresh = (my_tool->IsToggled() != toggle);
111
112 my_tool->m_isDown = toggle;
113
114 if (refresh)
115 RefreshTool( my_tool );
116 }
117
118 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool toggle)
119 {
120 }
121
122 wxToolBarToolBase *wxToolBar::CreateTool(int id,
123 const wxBitmap& bitmap1,
124 const wxBitmap& bitmap2,
125 bool toggle,
126 wxObject *clientData,
127 const wxString& shortHelpString,
128 const wxString& longHelpString)
129 {
130 return new wxToolBarTool( this, id, bitmap1, bitmap2, toggle,
131 clientData, shortHelpString, longHelpString);
132 }
133
134 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
135 {
136 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
137
138 return NULL;
139 }
140
141 void wxToolBar::RefreshTool( wxToolBarTool *tool )
142 {
143 wxRect rect( tool->m_x, tool->m_y, m_defaultWidth+6, m_defaultHeight+6 );
144 Refresh( TRUE, &rect );
145 }
146
147 void wxToolBar::DrawToolBarTool( wxToolBarTool *tool, wxDC &dc, bool down )
148 {
149 wxBitmap& bitmap = (tool->IsEnabled() || !tool->GetBitmap1().Ok()) ? tool->GetBitmap1() : tool->GetBitmap2() ;
150 if (down)
151 {
152 dc.DrawBitmap( bitmap, tool->m_x+4, tool->m_y+4, TRUE );
153
154 dc.SetPen( *wxGREY_PEN );
155 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
156 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
157
158 dc.SetPen( *wxBLACK_PEN );
159 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+1 );
160 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+1, tool->m_y+m_defaultHeight+4 );
161
162 dc.SetPen( *wxWHITE_PEN );
163 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
164 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
165 }
166 else
167 {
168 dc.DrawBitmap( bitmap, tool->m_x+3, tool->m_y+3, TRUE );
169
170 dc.SetPen( *wxWHITE_PEN );
171 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
172 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
173 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y, tool->m_x+m_defaultWidth+4, tool->m_y+2 );
174 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+4, tool->m_x+2, tool->m_y+m_defaultHeight+4 );
175
176 dc.SetPen( *wxBLACK_PEN );
177 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
178 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
179
180 dc.SetPen( *wxGREY_PEN );
181 dc.DrawLine( tool->m_x+1, tool->m_y+m_defaultHeight+4, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+4 );
182 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+m_defaultHeight+5 );
183 }
184 }
185
186 void wxToolBar::OnPaint(wxPaintEvent &event)
187 {
188 wxPaintDC dc(this);
189
190 wxSize clientSize = GetClientSize();
191 dc.SetPen( *wxBLACK_PEN );
192 dc.DrawLine( 0,0, clientSize.x,0 );
193
194 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
195 node;
196 node = node->GetNext() )
197 {
198 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
199
200 if (tool->GetId() == -1) continue;
201
202 DrawToolBarTool( tool, dc, tool->m_isDown );
203 }
204 }
205
206 bool wxToolBar::Realize()
207 {
208 if (!wxToolBarBase::Realize())
209 return FALSE;
210
211 int x;
212 int y;
213
214 if (GetWindowStyleFlag() & wxTB_VERTICAL)
215 {
216 x = m_xMargin;
217 y = 5;
218 }
219 else
220 {
221 y = m_yMargin;
222 x = 5;
223 }
224
225 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
226 node;
227 node = node->GetNext() )
228 {
229 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
230
231 if (GetWindowStyleFlag() & wxTB_VERTICAL)
232 {
233 if (tool->GetId() == -1)
234 {
235 y += 6;
236 continue;
237 }
238 tool->m_x = m_xMargin;
239 tool->m_y = y;
240 y += m_defaultHeight + 6;
241
242 // Calculate the maximum height or width (depending on style)
243 // so we know how to size the toolbar in Realize.
244 // We could get the size of the tool instead of the
245 // default bitmap size
246 if (m_maxWidth < (m_defaultWidth + 2*(m_xMargin + 2)))
247 m_maxWidth = (m_defaultWidth + 2*(m_xMargin + 2)) ;
248 }
249 else
250 {
251 if (tool->GetId() == -1)
252 {
253 x += 6;
254 continue;
255 }
256 tool->m_x = x;
257 tool->m_y = m_yMargin;
258 x += m_defaultWidth + 6;
259
260 // Calculate the maximum height or width (depending on style)
261 // so we know how to size the toolbar in Realize.
262 // We could get the size of the tool instead of the
263 // default bitmap size
264 if (m_maxHeight < (m_defaultHeight + 2*(m_yMargin + 2)))
265 m_maxHeight = (m_defaultHeight + 2*(m_yMargin + 2)) ;
266 }
267
268 }
269
270 wxSize sz = GetSize();
271 if (GetWindowStyleFlag() & wxTB_VERTICAL)
272 {
273 SetSize(m_maxWidth, sz.y);
274 }
275 else
276 {
277 SetSize(sz.x, m_maxHeight);
278 }
279
280 // Commenting out Robert's fix since the above should be
281 // more general
282 // SetSize( x+16, m_defaultHeight + 14 );
283
284 return TRUE;
285 }
286
287 void wxToolBar::OnMouse(wxMouseEvent &event)
288 {
289 wxToolBarTool *hit = NULL;
290 int x = event.GetX();
291 int y = event.GetY();
292
293 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
294 node;
295 node = node->GetNext() )
296 {
297 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
298
299 if ((x > tool->m_x) && (x < tool->m_x+m_defaultWidth+5) &&
300 (y > tool->m_y) && (y < tool->m_y+m_defaultHeight+5))
301 {
302 hit = tool;
303 break;
304 }
305 }
306
307 if (event.LeftDown() && (hit) && hit->Enabled())
308 {
309 CaptureMouse();
310 m_captured = hit;
311
312 m_captured->m_isDown = TRUE;
313 RefreshTool( m_captured );
314
315 return;
316 }
317
318 if (event.Dragging() && (m_captured))
319 {
320 bool is_down = (hit == m_captured);
321 if (is_down != m_captured->m_isDown)
322 {
323 m_captured->m_isDown = is_down;
324 RefreshTool( m_captured );
325 }
326
327 return;
328 }
329
330 if (event.LeftUp() && (m_captured))
331 {
332 ReleaseMouse();
333
334 m_captured->m_isDown = FALSE;
335 RefreshTool( m_captured );
336
337 if (hit == m_captured)
338 {
339 wxCommandEvent cevent( wxEVT_COMMAND_TOOL_CLICKED, m_captured->GetId() );
340 cevent.SetEventObject( this );
341 // cevent.SetExtraLong((long) toggleDown);
342 GetEventHandler()->ProcessEvent( cevent );
343 }
344
345 m_captured = NULL;
346
347 return;
348 }
349 }
350