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