]>
Commit | Line | Data |
---|---|---|
10b959e3 | 1 | ///////////////////////////////////////////////////////////////////////////// |
76b49cf4 | 2 | // Name: src/common/tbarbase.cpp |
8a0681f9 | 3 | // Purpose: wxToolBarBase implementation |
10b959e3 | 4 | // Author: Julian Smart |
3103e8a9 | 5 | // Modified by: VZ at 11.12.99 (wxScrollableToolBar split off) |
10b959e3 JS |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
55d99c7a | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
10b959e3 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8a0681f9 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
10b959e3 JS |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
8a0681f9 | 24 | #pragma hdrstop |
10b959e3 JS |
25 | #endif |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_TOOLBAR |
28 | ||
76b49cf4 WS |
29 | #include "wx/toolbar.h" |
30 | ||
10b959e3 | 31 | #ifndef WX_PRECOMP |
f538710f | 32 | #include "wx/control.h" |
76b49cf4 | 33 | #include "wx/frame.h" |
9eddec69 | 34 | #include "wx/settings.h" |
9ac43913 VZ |
35 | #if WXWIN_COMPATIBILITY_2_8 |
36 | #include "wx/image.h" | |
37 | #endif // WXWIN_COMPATIBILITY_2_8 | |
a9a0ceca | 38 | #include "wx/menu.h" |
155ecd4c | 39 | #endif |
e702ff0f | 40 | |
8a0681f9 | 41 | // ---------------------------------------------------------------------------- |
77ffb593 | 42 | // wxWidgets macros |
8a0681f9 VZ |
43 | // ---------------------------------------------------------------------------- |
44 | ||
1e6feb95 | 45 | BEGIN_EVENT_TABLE(wxToolBarBase, wxControl) |
1e6feb95 VZ |
46 | END_EVENT_TABLE() |
47 | ||
8a0681f9 | 48 | #include "wx/listimpl.cpp" |
1c383dba | 49 | |
259c43f6 | 50 | WX_DEFINE_LIST(wxToolBarToolsList) |
10b959e3 | 51 | |
8a0681f9 VZ |
52 | // ============================================================================ |
53 | // implementation | |
54 | // ============================================================================ | |
10b959e3 | 55 | |
8a0681f9 VZ |
56 | // ---------------------------------------------------------------------------- |
57 | // wxToolBarToolBase | |
58 | // ---------------------------------------------------------------------------- | |
10b959e3 | 59 | |
cb719f2e | 60 | IMPLEMENT_DYNAMIC_CLASS(wxToolBarToolBase, wxObject) |
d6071228 | 61 | |
a9a0ceca VZ |
62 | wxToolBarToolBase::~wxToolBarToolBase() |
63 | { | |
64 | delete m_dropdownMenu; | |
1be45608 VZ |
65 | if ( IsControl() ) |
66 | GetControl()->Destroy(); | |
a9a0ceca VZ |
67 | } |
68 | ||
69 | ||
8a0681f9 | 70 | bool wxToolBarToolBase::Enable(bool enable) |
10b959e3 | 71 | { |
ae291463 JS |
72 | if ( m_enabled == enable ) |
73 | return false; | |
74 | ||
8a0681f9 | 75 | m_enabled = enable; |
10b959e3 | 76 | |
331c9f56 | 77 | return true; |
10b959e3 JS |
78 | } |
79 | ||
8a0681f9 | 80 | bool wxToolBarToolBase::Toggle(bool toggle) |
10b959e3 | 81 | { |
e76c0b5f | 82 | wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") ); |
8a0681f9 | 83 | |
ae291463 JS |
84 | if ( m_toggled == toggle ) |
85 | return false; | |
86 | ||
8a0681f9 VZ |
87 | m_toggled = toggle; |
88 | ||
331c9f56 | 89 | return true; |
10b959e3 JS |
90 | } |
91 | ||
8a0681f9 | 92 | bool wxToolBarToolBase::SetToggle(bool toggle) |
10b959e3 | 93 | { |
e76c0b5f | 94 | wxItemKind kind = toggle ? wxITEM_CHECK : wxITEM_NORMAL; |
ae291463 JS |
95 | if ( m_kind == kind ) |
96 | return false; | |
10b959e3 | 97 | |
e76c0b5f | 98 | m_kind = kind; |
10b959e3 | 99 | |
331c9f56 | 100 | return true; |
10b959e3 JS |
101 | } |
102 | ||
8a0681f9 | 103 | bool wxToolBarToolBase::SetShortHelp(const wxString& help) |
10b959e3 | 104 | { |
ae291463 JS |
105 | if ( m_shortHelpString == help ) |
106 | return false; | |
107 | ||
8a0681f9 VZ |
108 | m_shortHelpString = help; |
109 | ||
331c9f56 | 110 | return true; |
10b959e3 JS |
111 | } |
112 | ||
8a0681f9 | 113 | bool wxToolBarToolBase::SetLongHelp(const wxString& help) |
10b959e3 | 114 | { |
ae291463 JS |
115 | if ( m_longHelpString == help ) |
116 | return false; | |
117 | ||
8a0681f9 VZ |
118 | m_longHelpString = help; |
119 | ||
331c9f56 | 120 | return true; |
10b959e3 JS |
121 | } |
122 | ||
a9a0ceca VZ |
123 | |
124 | void wxToolBarToolBase::SetDropdownMenu(wxMenu* menu) | |
125 | { | |
126 | delete m_dropdownMenu; | |
127 | m_dropdownMenu = menu; | |
128 | } | |
129 | ||
25af884d | 130 | |
8a0681f9 VZ |
131 | // ---------------------------------------------------------------------------- |
132 | // wxToolBarBase adding/deleting items | |
133 | // ---------------------------------------------------------------------------- | |
ac91b9d2 | 134 | |
8a0681f9 VZ |
135 | wxToolBarBase::wxToolBarBase() |
136 | { | |
137 | // the list owns the pointers | |
8a0681f9 | 138 | m_xMargin = m_yMargin = 0; |
8a0681f9 | 139 | m_maxRows = m_maxCols = 0; |
7b3eaf8f RR |
140 | m_toolPacking = m_toolSeparation = 0; |
141 | m_defaultWidth = 16; | |
142 | m_defaultHeight = 15; | |
10b959e3 JS |
143 | } |
144 | ||
d408730c VZ |
145 | void wxToolBarBase::FixupStyle() |
146 | { | |
147 | if ( !HasFlag(wxTB_TOP | wxTB_LEFT | wxTB_RIGHT | wxTB_BOTTOM) ) | |
148 | { | |
149 | // this is the default | |
150 | m_windowStyle |= wxTB_TOP; | |
151 | } | |
152 | } | |
153 | ||
e76c0b5f VZ |
154 | wxToolBarToolBase *wxToolBarBase::DoAddTool(int id, |
155 | const wxString& label, | |
156 | const wxBitmap& bitmap, | |
157 | const wxBitmap& bmpDisabled, | |
158 | wxItemKind kind, | |
159 | const wxString& shortHelp, | |
160 | const wxString& longHelp, | |
161 | wxObject *clientData, | |
162 | wxCoord WXUNUSED(xPos), | |
163 | wxCoord WXUNUSED(yPos)) | |
10b959e3 | 164 | { |
9f884528 | 165 | InvalidateBestSize(); |
e76c0b5f VZ |
166 | return InsertTool(GetToolsCount(), id, label, bitmap, bmpDisabled, |
167 | kind, shortHelp, longHelp, clientData); | |
10b959e3 JS |
168 | } |
169 | ||
8a0681f9 VZ |
170 | wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos, |
171 | int id, | |
e76c0b5f | 172 | const wxString& label, |
8a0681f9 | 173 | const wxBitmap& bitmap, |
e76c0b5f VZ |
174 | const wxBitmap& bmpDisabled, |
175 | wxItemKind kind, | |
176 | const wxString& shortHelp, | |
177 | const wxString& longHelp, | |
178 | wxObject *clientData) | |
10b959e3 | 179 | { |
8a0681f9 VZ |
180 | wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL, |
181 | _T("invalid position in wxToolBar::InsertTool()") ); | |
182 | ||
e76c0b5f VZ |
183 | wxToolBarToolBase *tool = CreateTool(id, label, bitmap, bmpDisabled, kind, |
184 | clientData, shortHelp, longHelp); | |
8a0681f9 | 185 | |
dd91da4e | 186 | if ( !InsertTool(pos, tool) ) |
8a0681f9 VZ |
187 | { |
188 | delete tool; | |
189 | ||
190 | return NULL; | |
191 | } | |
192 | ||
dd91da4e VZ |
193 | return tool; |
194 | } | |
195 | ||
196 | wxToolBarToolBase *wxToolBarBase::AddTool(wxToolBarToolBase *tool) | |
197 | { | |
198 | return InsertTool(GetToolsCount(), tool); | |
199 | } | |
200 | ||
201 | wxToolBarToolBase * | |
202 | wxToolBarBase::InsertTool(size_t pos, wxToolBarToolBase *tool) | |
203 | { | |
204 | wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL, | |
205 | _T("invalid position in wxToolBar::InsertTool()") ); | |
206 | ||
207 | if ( !tool || !DoInsertTool(pos, tool) ) | |
208 | { | |
209 | return NULL; | |
210 | } | |
211 | ||
8a0681f9 | 212 | m_tools.Insert(pos, tool); |
1be45608 | 213 | tool->Attach(this); |
8a0681f9 VZ |
214 | |
215 | return tool; | |
10b959e3 JS |
216 | } |
217 | ||
cdb11cb9 VZ |
218 | wxToolBarToolBase * |
219 | wxToolBarBase::AddControl(wxControl *control, const wxString& label) | |
10b959e3 | 220 | { |
cdb11cb9 | 221 | return InsertControl(GetToolsCount(), control, label); |
10b959e3 JS |
222 | } |
223 | ||
cdb11cb9 VZ |
224 | wxToolBarToolBase * |
225 | wxToolBarBase::InsertControl(size_t pos, | |
226 | wxControl *control, | |
227 | const wxString& label) | |
10b959e3 | 228 | { |
8a0681f9 VZ |
229 | wxCHECK_MSG( control, (wxToolBarToolBase *)NULL, |
230 | _T("toolbar: can't insert NULL control") ); | |
231 | ||
232 | wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL, | |
233 | _T("control must have toolbar as parent") ); | |
234 | ||
235 | wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL, | |
236 | _T("invalid position in wxToolBar::InsertControl()") ); | |
237 | ||
cdb11cb9 | 238 | wxToolBarToolBase *tool = CreateTool(control, label); |
8a0681f9 | 239 | |
dd91da4e | 240 | if ( !InsertTool(pos, tool) ) |
8a0681f9 VZ |
241 | { |
242 | delete tool; | |
243 | ||
244 | return NULL; | |
245 | } | |
246 | ||
8a0681f9 | 247 | return tool; |
10b959e3 JS |
248 | } |
249 | ||
fba2d5e6 RR |
250 | wxControl *wxToolBarBase::FindControl( int id ) |
251 | { | |
222ed1d6 | 252 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
fba2d5e6 RR |
253 | node; |
254 | node = node->GetNext() ) | |
255 | { | |
652ab153 VZ |
256 | const wxToolBarToolBase * const tool = node->GetData(); |
257 | if ( tool->IsControl() ) | |
fba2d5e6 | 258 | { |
652ab153 VZ |
259 | wxControl * const control = tool->GetControl(); |
260 | ||
261 | if ( !control ) | |
262 | { | |
263 | wxFAIL_MSG( _T("NULL control in toolbar?") ); | |
264 | } | |
265 | else if ( control->GetId() == id ) | |
266 | { | |
267 | // found | |
fba2d5e6 | 268 | return control; |
652ab153 | 269 | } |
fba2d5e6 RR |
270 | } |
271 | } | |
272 | ||
273 | return NULL; | |
274 | } | |
275 | ||
8a0681f9 | 276 | wxToolBarToolBase *wxToolBarBase::AddSeparator() |
10b959e3 | 277 | { |
8a0681f9 | 278 | return InsertSeparator(GetToolsCount()); |
10b959e3 JS |
279 | } |
280 | ||
8a0681f9 | 281 | wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos) |
10b959e3 | 282 | { |
8a0681f9 VZ |
283 | wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL, |
284 | _T("invalid position in wxToolBar::InsertSeparator()") ); | |
285 | ||
286 | wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR, | |
e76c0b5f | 287 | wxEmptyString, |
8a0681f9 | 288 | wxNullBitmap, wxNullBitmap, |
e76c0b5f | 289 | wxITEM_SEPARATOR, (wxObject *)NULL, |
8a0681f9 VZ |
290 | wxEmptyString, wxEmptyString); |
291 | ||
292 | if ( !tool || !DoInsertTool(pos, tool) ) | |
10b959e3 | 293 | { |
8a0681f9 VZ |
294 | delete tool; |
295 | ||
296 | return NULL; | |
10b959e3 | 297 | } |
8a0681f9 VZ |
298 | |
299 | m_tools.Insert(pos, tool); | |
300 | ||
301 | return tool; | |
10b959e3 JS |
302 | } |
303 | ||
8a0681f9 | 304 | wxToolBarToolBase *wxToolBarBase::RemoveTool(int id) |
10b959e3 | 305 | { |
8a0681f9 | 306 | size_t pos = 0; |
222ed1d6 | 307 | wxToolBarToolsList::compatibility_iterator node; |
8a0681f9 | 308 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
10b959e3 | 309 | { |
8a0681f9 VZ |
310 | if ( node->GetData()->GetId() == id ) |
311 | break; | |
312 | ||
313 | pos++; | |
10b959e3 | 314 | } |
10b959e3 | 315 | |
8a0681f9 | 316 | if ( !node ) |
10b959e3 | 317 | { |
8a0681f9 VZ |
318 | // don't give any error messages - sometimes we might call RemoveTool() |
319 | // without knowing whether the tool is or not in the toolbar | |
1be45608 | 320 | return NULL; |
10b959e3 | 321 | } |
10b959e3 | 322 | |
8a0681f9 | 323 | wxToolBarToolBase *tool = node->GetData(); |
1be45608 VZ |
324 | wxCHECK_MSG( tool, NULL, "NULL tool in the tools list?" ); |
325 | ||
8a0681f9 | 326 | if ( !DoDeleteTool(pos, tool) ) |
1be45608 | 327 | return NULL; |
10b959e3 | 328 | |
222ed1d6 | 329 | m_tools.Erase(node); |
10b959e3 | 330 | |
1be45608 VZ |
331 | tool->Detach(); |
332 | ||
8a0681f9 | 333 | return tool; |
10b959e3 JS |
334 | } |
335 | ||
8a0681f9 | 336 | bool wxToolBarBase::DeleteToolByPos(size_t pos) |
10b959e3 | 337 | { |
331c9f56 | 338 | wxCHECK_MSG( pos < GetToolsCount(), false, |
8a0681f9 | 339 | _T("invalid position in wxToolBar::DeleteToolByPos()") ); |
10b959e3 | 340 | |
222ed1d6 | 341 | wxToolBarToolsList::compatibility_iterator node = m_tools.Item(pos); |
10b959e3 | 342 | |
8a0681f9 VZ |
343 | if ( !DoDeleteTool(pos, node->GetData()) ) |
344 | { | |
331c9f56 | 345 | return false; |
8a0681f9 VZ |
346 | } |
347 | ||
222ed1d6 MB |
348 | delete node->GetData(); |
349 | m_tools.Erase(node); | |
8a0681f9 | 350 | |
331c9f56 | 351 | return true; |
10b959e3 JS |
352 | } |
353 | ||
8a0681f9 | 354 | bool wxToolBarBase::DeleteTool(int id) |
10b959e3 | 355 | { |
8a0681f9 | 356 | size_t pos = 0; |
222ed1d6 | 357 | wxToolBarToolsList::compatibility_iterator node; |
8a0681f9 VZ |
358 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
359 | { | |
360 | if ( node->GetData()->GetId() == id ) | |
361 | break; | |
362 | ||
363 | pos++; | |
364 | } | |
365 | ||
366 | if ( !node || !DoDeleteTool(pos, node->GetData()) ) | |
367 | { | |
331c9f56 | 368 | return false; |
8a0681f9 VZ |
369 | } |
370 | ||
222ed1d6 MB |
371 | delete node->GetData(); |
372 | m_tools.Erase(node); | |
8a0681f9 | 373 | |
331c9f56 | 374 | return true; |
10b959e3 JS |
375 | } |
376 | ||
8a0681f9 | 377 | wxToolBarToolBase *wxToolBarBase::FindById(int id) const |
10b959e3 | 378 | { |
8a0681f9 VZ |
379 | wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL; |
380 | ||
222ed1d6 | 381 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
8a0681f9 VZ |
382 | node; |
383 | node = node->GetNext() ) | |
384 | { | |
385 | tool = node->GetData(); | |
386 | if ( tool->GetId() == id ) | |
387 | { | |
388 | // found | |
389 | break; | |
390 | } | |
d9739886 VZ |
391 | |
392 | tool = NULL; | |
8a0681f9 VZ |
393 | } |
394 | ||
395 | return tool; | |
10b959e3 JS |
396 | } |
397 | ||
331c9f56 VZ |
398 | void wxToolBarBase::UnToggleRadioGroup(wxToolBarToolBase *tool) |
399 | { | |
400 | wxCHECK_RET( tool, _T("NULL tool in wxToolBarTool::UnToggleRadioGroup") ); | |
401 | ||
402 | if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO ) | |
403 | return; | |
404 | ||
405 | wxToolBarToolsList::compatibility_iterator node = m_tools.Find(tool); | |
406 | wxCHECK_RET( node, _T("invalid tool in wxToolBarTool::UnToggleRadioGroup") ); | |
407 | ||
408 | wxToolBarToolsList::compatibility_iterator nodeNext = node->GetNext(); | |
409 | while ( nodeNext ) | |
410 | { | |
4e115ed2 | 411 | wxToolBarToolBase *toolNext = nodeNext->GetData(); |
331c9f56 | 412 | |
4e115ed2 | 413 | if ( !toolNext->IsButton() || toolNext->GetKind() != wxITEM_RADIO ) |
331c9f56 VZ |
414 | break; |
415 | ||
4e115ed2 | 416 | if ( toolNext->Toggle(false) ) |
214b9484 | 417 | { |
4e115ed2 | 418 | DoToggleTool(toolNext, false); |
214b9484 | 419 | } |
331c9f56 VZ |
420 | |
421 | nodeNext = nodeNext->GetNext(); | |
422 | } | |
423 | ||
424 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); | |
425 | while ( nodePrev ) | |
426 | { | |
4e115ed2 | 427 | wxToolBarToolBase *toolNext = nodePrev->GetData(); |
331c9f56 | 428 | |
4e115ed2 | 429 | if ( !toolNext->IsButton() || toolNext->GetKind() != wxITEM_RADIO ) |
331c9f56 VZ |
430 | break; |
431 | ||
4e115ed2 | 432 | if ( toolNext->Toggle(false) ) |
214b9484 | 433 | { |
4e115ed2 | 434 | DoToggleTool(toolNext, false); |
214b9484 | 435 | } |
331c9f56 VZ |
436 | |
437 | nodePrev = nodePrev->GetPrevious(); | |
438 | } | |
439 | } | |
440 | ||
8a0681f9 | 441 | void wxToolBarBase::ClearTools() |
10b959e3 | 442 | { |
a3e9caa2 RD |
443 | while ( GetToolsCount() ) |
444 | { | |
445 | DeleteToolByPos(0); | |
446 | } | |
10b959e3 JS |
447 | } |
448 | ||
8a0681f9 | 449 | bool wxToolBarBase::Realize() |
10b959e3 | 450 | { |
331c9f56 | 451 | return true; |
10b959e3 JS |
452 | } |
453 | ||
8a0681f9 | 454 | wxToolBarBase::~wxToolBarBase() |
10b959e3 | 455 | { |
222ed1d6 | 456 | WX_CLEAR_LIST(wxToolBarToolsList, m_tools); |
2ab82214 VZ |
457 | |
458 | // notify the frame that it doesn't have a tool bar any longer to avoid | |
459 | // dangling pointers | |
b2e3be1c | 460 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
2ab82214 VZ |
461 | if ( frame && frame->GetToolBar() == this ) |
462 | { | |
463 | frame->SetToolBar(NULL); | |
464 | } | |
10b959e3 JS |
465 | } |
466 | ||
8a0681f9 VZ |
467 | // ---------------------------------------------------------------------------- |
468 | // wxToolBarBase tools state | |
469 | // ---------------------------------------------------------------------------- | |
10b959e3 | 470 | |
8a0681f9 | 471 | void wxToolBarBase::EnableTool(int id, bool enable) |
10b959e3 | 472 | { |
8a0681f9 VZ |
473 | wxToolBarToolBase *tool = FindById(id); |
474 | if ( tool ) | |
10b959e3 | 475 | { |
8a0681f9 VZ |
476 | if ( tool->Enable(enable) ) |
477 | { | |
478 | DoEnableTool(tool, enable); | |
479 | } | |
10b959e3 | 480 | } |
8a0681f9 | 481 | } |
10b959e3 | 482 | |
8a0681f9 VZ |
483 | void wxToolBarBase::ToggleTool(int id, bool toggle) |
484 | { | |
485 | wxToolBarToolBase *tool = FindById(id); | |
486 | if ( tool && tool->CanBeToggled() ) | |
10b959e3 | 487 | { |
8a0681f9 VZ |
488 | if ( tool->Toggle(toggle) ) |
489 | { | |
331c9f56 | 490 | UnToggleRadioGroup(tool); |
8a0681f9 VZ |
491 | DoToggleTool(tool, toggle); |
492 | } | |
10b959e3 | 493 | } |
10b959e3 JS |
494 | } |
495 | ||
8a0681f9 VZ |
496 | void wxToolBarBase::SetToggle(int id, bool toggle) |
497 | { | |
498 | wxToolBarToolBase *tool = FindById(id); | |
499 | if ( tool ) | |
10b959e3 | 500 | { |
8a0681f9 VZ |
501 | if ( tool->SetToggle(toggle) ) |
502 | { | |
503 | DoSetToggle(tool, toggle); | |
504 | } | |
10b959e3 | 505 | } |
8a0681f9 VZ |
506 | } |
507 | ||
508 | void wxToolBarBase::SetToolShortHelp(int id, const wxString& help) | |
509 | { | |
510 | wxToolBarToolBase *tool = FindById(id); | |
511 | if ( tool ) | |
10b959e3 | 512 | { |
8a0681f9 | 513 | (void)tool->SetShortHelp(help); |
10b959e3 | 514 | } |
8a0681f9 VZ |
515 | } |
516 | ||
517 | void wxToolBarBase::SetToolLongHelp(int id, const wxString& help) | |
518 | { | |
519 | wxToolBarToolBase *tool = FindById(id); | |
520 | if ( tool ) | |
10b959e3 | 521 | { |
8a0681f9 | 522 | (void)tool->SetLongHelp(help); |
10b959e3 | 523 | } |
10b959e3 JS |
524 | } |
525 | ||
8a0681f9 | 526 | wxObject *wxToolBarBase::GetToolClientData(int id) const |
10b959e3 | 527 | { |
8a0681f9 VZ |
528 | wxToolBarToolBase *tool = FindById(id); |
529 | ||
530 | return tool ? tool->GetClientData() : (wxObject *)NULL; | |
10b959e3 JS |
531 | } |
532 | ||
6fd5fa4f VZ |
533 | void wxToolBarBase::SetToolClientData(int id, wxObject *clientData) |
534 | { | |
535 | wxToolBarToolBase *tool = FindById(id); | |
536 | ||
537 | wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") ); | |
538 | ||
539 | tool->SetClientData(clientData); | |
540 | } | |
541 | ||
e6c96a7c JS |
542 | int wxToolBarBase::GetToolPos(int id) const |
543 | { | |
544 | size_t pos = 0; | |
222ed1d6 | 545 | wxToolBarToolsList::compatibility_iterator node; |
e6c96a7c JS |
546 | |
547 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
548 | { | |
549 | if ( node->GetData()->GetId() == id ) | |
550 | return pos; | |
551 | ||
552 | pos++; | |
553 | } | |
554 | ||
555 | return wxNOT_FOUND; | |
556 | } | |
557 | ||
8a0681f9 | 558 | bool wxToolBarBase::GetToolState(int id) const |
10b959e3 | 559 | { |
8a0681f9 | 560 | wxToolBarToolBase *tool = FindById(id); |
331c9f56 | 561 | wxCHECK_MSG( tool, false, _T("no such tool") ); |
8a0681f9 VZ |
562 | |
563 | return tool->IsToggled(); | |
10b959e3 JS |
564 | } |
565 | ||
8a0681f9 | 566 | bool wxToolBarBase::GetToolEnabled(int id) const |
10b959e3 | 567 | { |
8a0681f9 | 568 | wxToolBarToolBase *tool = FindById(id); |
331c9f56 | 569 | wxCHECK_MSG( tool, false, _T("no such tool") ); |
8a0681f9 VZ |
570 | |
571 | return tool->IsEnabled(); | |
10b959e3 JS |
572 | } |
573 | ||
8a0681f9 | 574 | wxString wxToolBarBase::GetToolShortHelp(int id) const |
10b959e3 | 575 | { |
8a0681f9 | 576 | wxToolBarToolBase *tool = FindById(id); |
525d8583 | 577 | wxCHECK_MSG( tool, wxEmptyString, _T("no such tool") ); |
8a0681f9 VZ |
578 | |
579 | return tool->GetShortHelp(); | |
10b959e3 JS |
580 | } |
581 | ||
8a0681f9 | 582 | wxString wxToolBarBase::GetToolLongHelp(int id) const |
10b959e3 | 583 | { |
8a0681f9 | 584 | wxToolBarToolBase *tool = FindById(id); |
525d8583 | 585 | wxCHECK_MSG( tool, wxEmptyString, _T("no such tool") ); |
10b959e3 | 586 | |
8a0681f9 | 587 | return tool->GetLongHelp(); |
10b959e3 JS |
588 | } |
589 | ||
8a0681f9 VZ |
590 | // ---------------------------------------------------------------------------- |
591 | // wxToolBarBase geometry | |
592 | // ---------------------------------------------------------------------------- | |
593 | ||
594 | void wxToolBarBase::SetMargins(int x, int y) | |
10b959e3 | 595 | { |
8a0681f9 VZ |
596 | m_xMargin = x; |
597 | m_yMargin = y; | |
10b959e3 JS |
598 | } |
599 | ||
8a0681f9 | 600 | void wxToolBarBase::SetRows(int WXUNUSED(nRows)) |
10b959e3 | 601 | { |
8a0681f9 | 602 | // nothing |
10b959e3 JS |
603 | } |
604 | ||
25af884d FM |
605 | bool wxToolBarBase::IsVertical() const |
606 | { | |
607 | return HasFlag(wxTB_LEFT | wxTB_RIGHT); | |
608 | } | |
609 | ||
610 | ||
8a0681f9 VZ |
611 | // ---------------------------------------------------------------------------- |
612 | // event processing | |
613 | // ---------------------------------------------------------------------------- | |
614 | ||
331c9f56 | 615 | // Only allow toggle if returns true |
8a0681f9 | 616 | bool wxToolBarBase::OnLeftClick(int id, bool toggleDown) |
10b959e3 | 617 | { |
8a0681f9 VZ |
618 | wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id); |
619 | event.SetEventObject(this); | |
6bec54e1 VZ |
620 | |
621 | // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown | |
622 | event.SetInt((int)toggleDown); | |
623 | ||
624 | // and SetExtraLong() for backwards compatibility | |
625 | event.SetExtraLong((long)toggleDown); | |
8a0681f9 VZ |
626 | |
627 | // Send events to this toolbar instead (and thence up the window hierarchy) | |
628 | GetEventHandler()->ProcessEvent(event); | |
629 | ||
331c9f56 | 630 | return true; |
10b959e3 JS |
631 | } |
632 | ||
8a0681f9 VZ |
633 | // Call when right button down. |
634 | void wxToolBarBase::OnRightClick(int id, | |
635 | long WXUNUSED(x), | |
636 | long WXUNUSED(y)) | |
10b959e3 | 637 | { |
8a0681f9 VZ |
638 | wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id); |
639 | event.SetEventObject(this); | |
640 | event.SetInt(id); | |
641 | ||
642 | GetEventHandler()->ProcessEvent(event); | |
643 | } | |
43d811ea | 644 | |
8a0681f9 | 645 | // Called when the mouse cursor enters a tool bitmap (no button pressed). |
cb719f2e | 646 | // Argument is wxID_ANY if mouse is exiting the toolbar. |
8a0681f9 VZ |
647 | // Note that for this event, the id of the window is used, |
648 | // and the integer parameter of wxCommandEvent is used to retrieve | |
649 | // the tool id. | |
650 | void wxToolBarBase::OnMouseEnter(int id) | |
651 | { | |
652 | wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId()); | |
653 | event.SetEventObject(this); | |
654 | event.SetInt(id); | |
655 | ||
8a0681f9 | 656 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
6d99eb3e | 657 | if ( frame ) |
66ce9e06 | 658 | { |
326a37f1 | 659 | wxString help; |
fa36fe36 VZ |
660 | if ( id != wxID_ANY ) |
661 | { | |
662 | const wxToolBarToolBase * const tool = FindById(id); | |
663 | if ( tool ) | |
664 | help = tool->GetLongHelp(); | |
665 | } | |
666 | ||
6d99eb3e VZ |
667 | // call DoGiveHelp() even if help string is empty to avoid showing the |
668 | // help for the previously selected tool when another one is selected | |
669 | frame->DoGiveHelp(help, id != wxID_ANY); | |
66ce9e06 VZ |
670 | } |
671 | ||
1f361cdd | 672 | (void)GetEventHandler()->ProcessEvent(event); |
8a0681f9 VZ |
673 | } |
674 | ||
675 | // ---------------------------------------------------------------------------- | |
676 | // UI updates | |
677 | // ---------------------------------------------------------------------------- | |
678 | ||
10b959e3 | 679 | // Do the toolbar button updates (check for EVT_UPDATE_UI handlers) |
e39af974 | 680 | void wxToolBarBase::UpdateWindowUI(long flags) |
ac91b9d2 | 681 | { |
e39af974 JS |
682 | wxWindowBase::UpdateWindowUI(flags); |
683 | ||
632e5b63 VZ |
684 | // don't waste time updating state of tools in a hidden toolbar |
685 | if ( !IsShown() ) | |
686 | return; | |
687 | ||
fe5d86ed RR |
688 | // There is no sense in updating the toolbar UI |
689 | // if the parent window is about to get destroyed | |
5ce61d9f RR |
690 | wxWindow *tlw = wxGetTopLevelParent( this ); |
691 | if (tlw && wxPendingDelete.Member( tlw )) | |
fe5d86ed RR |
692 | return; |
693 | ||
65b17727 | 694 | wxEvtHandler* evtHandler = GetEventHandler() ; |
e702ff0f | 695 | |
222ed1d6 | 696 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
8a0681f9 VZ |
697 | node; |
698 | node = node->GetNext() ) | |
ac91b9d2 | 699 | { |
74c80fe4 VZ |
700 | wxToolBarToolBase * const tool = node->GetData(); |
701 | if ( tool->IsSeparator() ) | |
702 | continue; | |
703 | ||
704 | int id = tool->GetId(); | |
ac91b9d2 | 705 | |
8a0681f9 | 706 | wxUpdateUIEvent event(id); |
ac91b9d2 VZ |
707 | event.SetEventObject(this); |
708 | ||
8a0681f9 | 709 | if ( evtHandler->ProcessEvent(event) ) |
ac91b9d2 | 710 | { |
8a0681f9 VZ |
711 | if ( event.GetSetEnabled() ) |
712 | EnableTool(id, event.GetEnabled()); | |
713 | if ( event.GetSetChecked() ) | |
714 | ToggleTool(id, event.GetChecked()); | |
715 | #if 0 | |
716 | if ( event.GetSetText() ) | |
ac91b9d2 | 717 | // Set tooltip? |
8a0681f9 | 718 | #endif // 0 |
ac91b9d2 | 719 | } |
ac91b9d2 | 720 | } |
10b959e3 JS |
721 | } |
722 | ||
a9a0ceca VZ |
723 | bool wxToolBarBase::SetDropdownMenu(int toolid, wxMenu* menu) |
724 | { | |
725 | wxToolBarToolBase * const tool = FindById(toolid); | |
726 | wxCHECK_MSG( tool, false, _T("invalid tool id") ); | |
727 | ||
728 | wxCHECK_MSG( tool->GetKind() == wxITEM_DROPDOWN, false, | |
729 | _T("menu can be only associated with drop down tools") ); | |
730 | ||
731 | tool->SetDropdownMenu(menu); | |
732 | ||
733 | return true; | |
734 | } | |
735 | ||
9ac43913 | 736 | #if WXWIN_COMPATIBILITY_2_8 |
c229e50d | 737 | |
9ac43913 | 738 | bool wxCreateGreyedImage(const wxImage& in, wxImage& out) |
c229e50d | 739 | { |
9ac43913 VZ |
740 | #if wxUSE_IMAGE |
741 | out = in.ConvertToGreyscale(); | |
742 | if ( out.Ok() ) | |
743 | return true; | |
744 | #endif // wxUSE_IMAGE | |
9ac43913 | 745 | return false; |
c229e50d JS |
746 | } |
747 | ||
9ac43913 | 748 | #endif // WXWIN_COMPATIBILITY_2_8 |
434c6c9f | 749 | |
8a0681f9 | 750 | #endif // wxUSE_TOOLBAR |