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