]> git.saurik.com Git - wxWidgets.git/blob - src/univ/toolbar.cpp
Added simplistic wxToolBar to wxUniversal. It
[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 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
169 node;
170 node = node->GetNext() )
171 {
172 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
173
174 if (tool->GetId() == -1) continue;
175
176 DrawToolBarTool( tool, dc, tool->m_isDown );
177 }
178 }
179
180 bool wxToolBar::Realize()
181 {
182 if (!wxToolBarBase::Realize())
183 return FALSE;
184
185 int x = 5;
186
187 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
188 node;
189 node = node->GetNext() )
190 {
191 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
192
193 if (tool->GetId() == -1)
194 {
195 x += 6;
196 continue;
197 }
198
199 tool->m_x = x;
200 tool->m_y = 4;
201 x += m_defaultWidth + 6;
202 }
203
204 return TRUE;
205 }
206
207 void wxToolBar::OnMouse(wxMouseEvent &event)
208 {
209 wxToolBarTool *hit = NULL;
210 int x = event.GetX();
211 int y = event.GetY();
212
213 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
214 node;
215 node = node->GetNext() )
216 {
217 wxToolBarTool *tool = (wxToolBarTool*) node->Data();
218
219 if ((x > tool->m_x) && (x < tool->m_x+m_defaultWidth+5) &&
220 (y > tool->m_y) && (y < tool->m_y+m_defaultHeight+5))
221 {
222 hit = tool;
223 break;
224 }
225 }
226
227 if (event.LeftDown() && (hit))
228 {
229 CaptureMouse();
230 m_captured = hit;
231
232 m_captured->m_isDown = TRUE;
233 RefreshTool( m_captured );
234
235 return;
236 }
237
238 if (event.Dragging() && (m_captured))
239 {
240 bool is_down = (hit == m_captured);
241 if (is_down != m_captured->m_isDown)
242 {
243 m_captured->m_isDown = is_down;
244 RefreshTool( m_captured );
245 }
246
247 return;
248 }
249
250 if (event.LeftUp() && (m_captured))
251 {
252 ReleaseMouse();
253
254 m_captured->m_isDown = FALSE;
255 RefreshTool( m_captured );
256
257 if (hit == m_captured)
258 {
259 wxCommandEvent cevent( wxEVT_COMMAND_TOOL_CLICKED, m_captured->GetId() );
260 cevent.SetEventObject( this );
261 // cevent.SetExtraLong((long) toggleDown);
262 GetEventHandler()->ProcessEvent( cevent );
263 }
264
265 m_captured = NULL;
266
267 return;
268 }
269 }
270