Corrections to position and size for toolbar.
[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/validate.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 m_captured = NULL;
58
59 SetToolBitmapSize( wxSize(16,15) );
60 }
61
62 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
63 {
64 return NULL;
65 }
66
67 void wxToolBar::SetToolShortHelp(int id, const wxString& helpString)
68 {
69 }
70
71 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *tool)
72 {
73 return TRUE;
74 }
75
76 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *tool)
77 {
78 return TRUE;
79 }
80
81 void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable)
82 {
83 }
84
85 void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool toggle)
86 {
87 wxToolBarTool *my_tool = (wxToolBarTool*) tool;
88
89 bool refresh = (my_tool->IsToggled() != toggle);
90
91 my_tool->m_isDown = toggle;
92
93 if (refresh)
94 RefreshTool( my_tool );
95 }
96
97 void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool toggle)
98 {
99 }
100
101 wxToolBarToolBase *wxToolBar::CreateTool(int id,
102 const wxBitmap& bitmap1,
103 const wxBitmap& bitmap2,
104 bool toggle,
105 wxObject *clientData,
106 const wxString& shortHelpString,
107 const wxString& longHelpString)
108 {
109 return new wxToolBarTool( this, id, bitmap1, bitmap2, toggle,
110 clientData, shortHelpString, longHelpString);
111 }
112
113 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
114 {
115 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
116
117 return NULL;
118 }
119
120 void wxToolBar::RefreshTool( wxToolBarTool *tool )
121 {
122 wxRect rect( tool->m_x, tool->m_y, m_defaultWidth+6, m_defaultHeight+6 );
123 Refresh( TRUE, &rect );
124 }
125
126 void wxToolBar::DrawToolBarTool( wxToolBarTool *tool, wxDC &dc, bool down )
127 {
128 if (down)
129 {
130 dc.DrawBitmap( tool->GetBitmap1(), tool->m_x+4, tool->m_y+4, TRUE );
131
132 dc.SetPen( *wxGREY_PEN );
133 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
134 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
135
136 dc.SetPen( *wxBLACK_PEN );
137 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+1 );
138 dc.DrawLine( tool->m_x+1, tool->m_y+1, tool->m_x+1, tool->m_y+m_defaultHeight+4 );
139
140 dc.SetPen( *wxWHITE_PEN );
141 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
142 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
143 }
144 else
145 {
146 dc.DrawBitmap( tool->GetBitmap1(), tool->m_x+3, tool->m_y+3, TRUE );
147
148 dc.SetPen( *wxWHITE_PEN );
149 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y );
150 dc.DrawLine( tool->m_x, tool->m_y, tool->m_x, tool->m_y+m_defaultHeight+5 );
151 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y, tool->m_x+m_defaultWidth+4, tool->m_y+2 );
152 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+4, tool->m_x+2, tool->m_y+m_defaultHeight+4 );
153
154 dc.SetPen( *wxBLACK_PEN );
155 dc.DrawLine( tool->m_x, tool->m_y+m_defaultHeight+5, tool->m_x+m_defaultWidth+6, tool->m_y+m_defaultHeight+5 );
156 dc.DrawLine( tool->m_x+m_defaultWidth+5, tool->m_y, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+6 );
157
158 dc.SetPen( *wxGREY_PEN );
159 dc.DrawLine( tool->m_x+1, tool->m_y+m_defaultHeight+4, tool->m_x+m_defaultWidth+5, tool->m_y+m_defaultHeight+4 );
160 dc.DrawLine( tool->m_x+m_defaultWidth+4, tool->m_y+1, tool->m_x+m_defaultWidth+4, tool->m_y+m_defaultHeight+5 );
161 }
162 }
163
164 void wxToolBar::OnPaint(wxPaintEvent &event)
165 {
166 wxPaintDC dc(this);
167
168 wxSize clientSize = GetClientSize();
169 dc.SetPen( *wxBLACK_PEN );
170 dc.DrawLine( 0,0, clientSize.x,0 );
171
172 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
173 node;
174 node = node->GetNext() )
175 {
176 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
177
178 if (tool->GetId() == -1) continue;
179
180 DrawToolBarTool( tool, dc, tool->m_isDown );
181 }
182 }
183
184 bool wxToolBar::Realize()
185 {
186 if (!wxToolBarBase::Realize())
187 return FALSE;
188
189 int x = 5;
190
191 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
192 node;
193 node = node->GetNext() )
194 {
195 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
196
197 if (tool->GetId() == -1)
198 {
199 x += 6;
200 continue;
201 }
202
203 tool->m_x = x;
204 tool->m_y = 4;
205 x += m_defaultWidth + 6;
206 }
207
208 SetSize( x+16, m_defaultHeight + 14 );
209
210 return TRUE;
211 }
212
213 void wxToolBar::OnMouse(wxMouseEvent &event)
214 {
215 wxToolBarTool *hit = NULL;
216 int x = event.GetX();
217 int y = event.GetY();
218
219 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
220 node;
221 node = node->GetNext() )
222 {
223 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
224
225 if ((x > tool->m_x) && (x < tool->m_x+m_defaultWidth+5) &&
226 (y > tool->m_y) && (y < tool->m_y+m_defaultHeight+5))
227 {
228 hit = tool;
229 break;
230 }
231 }
232
233 if (event.LeftDown() && (hit))
234 {
235 CaptureMouse();
236 m_captured = hit;
237
238 m_captured->m_isDown = TRUE;
239 RefreshTool( m_captured );
240
241 return;
242 }
243
244 if (event.Dragging() && (m_captured))
245 {
246 bool is_down = (hit == m_captured);
247 if (is_down != m_captured->m_isDown)
248 {
249 m_captured->m_isDown = is_down;
250 RefreshTool( m_captured );
251 }
252
253 return;
254 }
255
256 if (event.LeftUp() && (m_captured))
257 {
258 ReleaseMouse();
259
260 m_captured->m_isDown = FALSE;
261 RefreshTool( m_captured );
262
263 if (hit == m_captured)
264 {
265 wxCommandEvent cevent( wxEVT_COMMAND_TOOL_CLICKED, m_captured->GetId() );
266 cevent.SetEventObject( this );
267 // cevent.SetExtraLong((long) toggleDown);
268 GetEventHandler()->ProcessEvent( cevent );
269 }
270
271 m_captured = NULL;
272
273 return;
274 }
275 }
276