]> git.saurik.com Git - wxWidgets.git/blame - src/common/tbarbase.cpp
1. added wxAssertIsEqual() function to be used in wxASSERT()
[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
8a0681f9 5// Modified by: VZ at 11.12.99 (wxScrollableToolBar splitted off)
10b959e3
JS
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
1c383dba 9// Licence: wxWindows license
10b959e3
JS
10/////////////////////////////////////////////////////////////////////////////
11
8a0681f9
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
10b959e3 20#ifdef __GNUG__
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
31#ifndef WX_PRECOMP
8a0681f9 32 #include "wx/wx.h"
10b959e3
JS
33#endif
34
e702ff0f
JS
35#include "wx/frame.h"
36
10b959e3 37// For ::UpdateWindow
2049ba38 38#ifdef __WXMSW__
10b959e3
JS
39#include <windows.h>
40#endif
41
47d67540 42#if wxUSE_TOOLBAR
10b959e3
JS
43
44#include "wx/tbarbase.h"
45
8a0681f9
VZ
46// ----------------------------------------------------------------------------
47// wxWindows macros
48// ----------------------------------------------------------------------------
49
50#if !USE_SHARED_LIBRARY
51 BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
52 EVT_IDLE(wxToolBarBase::OnIdle)
53 END_EVENT_TABLE()
4fcd73bd 54#endif
10b959e3 55
12ed316d
JS
56IMPLEMENT_CLASS(wxToolBarBase, wxControl)
57
8a0681f9 58#include "wx/listimpl.cpp"
1c383dba 59
8a0681f9 60WX_DEFINE_LIST(wxToolBarToolsList);
10b959e3 61
8a0681f9
VZ
62// ============================================================================
63// implementation
64// ============================================================================
10b959e3 65
8a0681f9
VZ
66// ----------------------------------------------------------------------------
67// wxToolBarToolBase
68// ----------------------------------------------------------------------------
10b959e3 69
8a0681f9 70bool wxToolBarToolBase::Enable(bool enable)
10b959e3 71{
8a0681f9
VZ
72 if ( m_enabled == enable )
73 return FALSE;
10b959e3 74
8a0681f9 75 m_enabled = enable;
10b959e3 76
8a0681f9 77 return TRUE;
10b959e3
JS
78}
79
8a0681f9 80bool wxToolBarToolBase::Toggle(bool toggle)
10b959e3 81{
8a0681f9
VZ
82 wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
83
84 if ( m_toggled == toggle )
85 return FALSE;
10b959e3 86
8a0681f9
VZ
87 m_toggled = toggle;
88
89 return TRUE;
10b959e3
JS
90}
91
8a0681f9 92bool wxToolBarToolBase::SetToggle(bool toggle)
10b959e3 93{
8a0681f9
VZ
94 if ( m_isToggle == toggle )
95 return FALSE;
10b959e3 96
8a0681f9 97 m_isToggle = toggle;
10b959e3
JS
98
99 return TRUE;
100}
101
8a0681f9 102bool wxToolBarToolBase::SetShortHelp(const wxString& help)
10b959e3 103{
8a0681f9
VZ
104 if ( m_shortHelpString == help )
105 return FALSE;
10b959e3 106
8a0681f9
VZ
107 m_shortHelpString = help;
108
109 return TRUE;
10b959e3
JS
110}
111
8a0681f9 112bool wxToolBarToolBase::SetLongHelp(const wxString& help)
10b959e3 113{
8a0681f9
VZ
114 if ( m_longHelpString == help )
115 return FALSE;
10b959e3 116
8a0681f9
VZ
117 m_longHelpString = help;
118
119 return TRUE;
10b959e3
JS
120}
121
8a0681f9 122wxToolBarToolBase::~wxToolBarToolBase()
10b959e3 123{
8a0681f9 124}
10b959e3 125
8a0681f9
VZ
126// ----------------------------------------------------------------------------
127// wxToolBarBase adding/deleting items
128// ----------------------------------------------------------------------------
ac91b9d2 129
8a0681f9
VZ
130wxToolBarBase::wxToolBarBase()
131{
132 // the list owns the pointers
133 m_tools.DeleteContents(TRUE);
10b959e3 134
8a0681f9 135 m_xMargin = m_yMargin = 0;
10b959e3 136
8a0681f9 137 m_maxRows = m_maxCols = 0;
10b959e3
JS
138}
139
8a0681f9
VZ
140wxToolBarToolBase *wxToolBarBase::AddTool(int id,
141 const wxBitmap& bitmap,
142 const wxBitmap& pushedBitmap,
143 bool toggle,
144 wxCoord WXUNUSED(xPos),
145 wxCoord WXUNUSED(yPos),
146 wxObject *clientData,
147 const wxString& helpString1,
148 const wxString& helpString2)
10b959e3 149{
8a0681f9
VZ
150 return InsertTool(GetToolsCount(), id, bitmap, pushedBitmap,
151 toggle, clientData, helpString1, helpString2);
10b959e3
JS
152}
153
8a0681f9
VZ
154wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
155 int id,
156 const wxBitmap& bitmap,
157 const wxBitmap& pushedBitmap,
158 bool toggle,
159 wxObject *clientData,
160 const wxString& helpString1,
161 const wxString& helpString2)
10b959e3 162{
8a0681f9
VZ
163 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
164 _T("invalid position in wxToolBar::InsertTool()") );
165
166 wxToolBarToolBase *tool = CreateTool(id, bitmap, pushedBitmap, toggle,
167 clientData, helpString1, helpString2);
168
169 if ( !tool || !DoInsertTool(pos, tool) )
170 {
171 delete tool;
172
173 return NULL;
174 }
175
176 m_tools.Insert(pos, tool);
177
178 return tool;
10b959e3
JS
179}
180
8a0681f9 181wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
10b959e3 182{
8a0681f9 183 return InsertControl(GetToolsCount(), control);
10b959e3
JS
184}
185
8a0681f9 186wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
10b959e3 187{
8a0681f9
VZ
188 wxCHECK_MSG( control, (wxToolBarToolBase *)NULL,
189 _T("toolbar: can't insert NULL control") );
190
191 wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL,
192 _T("control must have toolbar as parent") );
193
194 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
195 _T("invalid position in wxToolBar::InsertControl()") );
196
197 wxToolBarToolBase *tool = CreateTool(control);
198
199 if ( !tool || !DoInsertTool(pos, tool) )
200 {
201 delete tool;
202
203 return NULL;
204 }
205
206 m_tools.Insert(pos, tool);
207
208 return tool;
10b959e3
JS
209}
210
8a0681f9 211wxToolBarToolBase *wxToolBarBase::AddSeparator()
10b959e3 212{
8a0681f9 213 return InsertSeparator(GetToolsCount());
10b959e3
JS
214}
215
8a0681f9 216wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
10b959e3 217{
8a0681f9
VZ
218 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
219 _T("invalid position in wxToolBar::InsertSeparator()") );
220
221 wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR,
222 wxNullBitmap, wxNullBitmap,
223 FALSE, (wxObject *)NULL,
224 wxEmptyString, wxEmptyString);
225
226 if ( !tool || !DoInsertTool(pos, tool) )
10b959e3 227 {
8a0681f9
VZ
228 delete tool;
229
230 return NULL;
10b959e3 231 }
8a0681f9
VZ
232
233 m_tools.Insert(pos, tool);
234
235 return tool;
10b959e3
JS
236}
237
8a0681f9 238wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
10b959e3 239{
8a0681f9
VZ
240 size_t pos = 0;
241 wxToolBarToolsList::Node *node;
242 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
10b959e3 243 {
8a0681f9
VZ
244 if ( node->GetData()->GetId() == id )
245 break;
246
247 pos++;
10b959e3 248 }
10b959e3 249
8a0681f9 250 if ( !node )
10b959e3 251 {
8a0681f9
VZ
252 // don't give any error messages - sometimes we might call RemoveTool()
253 // without knowing whether the tool is or not in the toolbar
254 return (wxToolBarToolBase *)NULL;
10b959e3 255 }
10b959e3 256
8a0681f9
VZ
257 wxToolBarToolBase *tool = node->GetData();
258 if ( !DoDeleteTool(pos, tool) )
259 {
260 return (wxToolBarToolBase *)NULL;
261 }
10b959e3 262
8a0681f9
VZ
263 // the node would delete the data, so set it to NULL to avoid this
264 node->SetData(NULL);
10b959e3 265
8a0681f9 266 m_tools.DeleteNode(node);
10b959e3 267
8a0681f9 268 return tool;
10b959e3
JS
269}
270
8a0681f9 271bool wxToolBarBase::DeleteToolByPos(size_t pos)
10b959e3 272{
8a0681f9
VZ
273 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
274 _T("invalid position in wxToolBar::DeleteToolByPos()") );
10b959e3 275
8a0681f9 276 wxToolBarToolsList::Node *node = m_tools.Item(pos);
10b959e3 277
8a0681f9
VZ
278 if ( !DoDeleteTool(pos, node->GetData()) )
279 {
280 return FALSE;
281 }
282
283 m_tools.DeleteNode(node);
284
285 return TRUE;
10b959e3
JS
286}
287
8a0681f9 288bool wxToolBarBase::DeleteTool(int id)
10b959e3 289{
8a0681f9
VZ
290 size_t pos = 0;
291 wxToolBarToolsList::Node *node;
292 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
293 {
294 if ( node->GetData()->GetId() == id )
295 break;
296
297 pos++;
298 }
299
300 if ( !node || !DoDeleteTool(pos, node->GetData()) )
301 {
302 return FALSE;
303 }
304
305 m_tools.DeleteNode(node);
306
307 return TRUE;
10b959e3
JS
308}
309
8a0681f9 310wxToolBarToolBase *wxToolBarBase::FindById(int id) const
10b959e3 311{
8a0681f9
VZ
312 wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL;
313
314 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
315 node;
316 node = node->GetNext() )
317 {
318 tool = node->GetData();
319 if ( tool->GetId() == id )
320 {
321 // found
322 break;
323 }
324 }
325
326 return tool;
10b959e3
JS
327}
328
8a0681f9 329void wxToolBarBase::ClearTools()
10b959e3 330{
8a0681f9 331 m_tools.Clear();
10b959e3
JS
332}
333
8a0681f9 334bool wxToolBarBase::Realize()
10b959e3 335{
8a0681f9 336 return TRUE;
10b959e3
JS
337}
338
8a0681f9 339wxToolBarBase::~wxToolBarBase()
10b959e3 340{
10b959e3
JS
341}
342
8a0681f9
VZ
343// ----------------------------------------------------------------------------
344// wxToolBarBase tools state
345// ----------------------------------------------------------------------------
10b959e3 346
8a0681f9 347void wxToolBarBase::EnableTool(int id, bool enable)
10b959e3 348{
8a0681f9
VZ
349 wxToolBarToolBase *tool = FindById(id);
350 if ( tool )
10b959e3 351 {
8a0681f9
VZ
352 if ( tool->Enable(enable) )
353 {
354 DoEnableTool(tool, enable);
355 }
10b959e3 356 }
8a0681f9 357}
10b959e3 358
8a0681f9
VZ
359void wxToolBarBase::ToggleTool(int id, bool toggle)
360{
361 wxToolBarToolBase *tool = FindById(id);
362 if ( tool && tool->CanBeToggled() )
10b959e3 363 {
8a0681f9
VZ
364 if ( tool->Toggle(toggle) )
365 {
366 DoToggleTool(tool, toggle);
367 }
10b959e3 368 }
10b959e3
JS
369}
370
8a0681f9
VZ
371void wxToolBarBase::SetToggle(int id, bool toggle)
372{
373 wxToolBarToolBase *tool = FindById(id);
374 if ( tool )
10b959e3 375 {
8a0681f9
VZ
376 if ( tool->SetToggle(toggle) )
377 {
378 DoSetToggle(tool, toggle);
379 }
10b959e3 380 }
8a0681f9
VZ
381}
382
383void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
384{
385 wxToolBarToolBase *tool = FindById(id);
386 if ( tool )
10b959e3 387 {
8a0681f9 388 (void)tool->SetShortHelp(help);
10b959e3 389 }
8a0681f9
VZ
390}
391
392void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
393{
394 wxToolBarToolBase *tool = FindById(id);
395 if ( tool )
10b959e3 396 {
8a0681f9 397 (void)tool->SetLongHelp(help);
10b959e3 398 }
10b959e3
JS
399}
400
8a0681f9 401wxObject *wxToolBarBase::GetToolClientData(int id) const
10b959e3 402{
8a0681f9
VZ
403 wxToolBarToolBase *tool = FindById(id);
404
405 return tool ? tool->GetClientData() : (wxObject *)NULL;
10b959e3
JS
406}
407
6fd5fa4f
VZ
408void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
409{
410 wxToolBarToolBase *tool = FindById(id);
411
412 wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") );
413
414 tool->SetClientData(clientData);
415}
416
8a0681f9 417bool wxToolBarBase::GetToolState(int id) const
10b959e3 418{
8a0681f9
VZ
419 wxToolBarToolBase *tool = FindById(id);
420 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
421
422 return tool->IsToggled();
10b959e3
JS
423}
424
8a0681f9 425bool wxToolBarBase::GetToolEnabled(int id) const
10b959e3 426{
8a0681f9
VZ
427 wxToolBarToolBase *tool = FindById(id);
428 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
429
430 return tool->IsEnabled();
10b959e3
JS
431}
432
8a0681f9 433wxString wxToolBarBase::GetToolShortHelp(int id) const
10b959e3 434{
8a0681f9
VZ
435 wxToolBarToolBase *tool = FindById(id);
436 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
437
438 return tool->GetShortHelp();
10b959e3
JS
439}
440
8a0681f9 441wxString wxToolBarBase::GetToolLongHelp(int id) const
10b959e3 442{
8a0681f9
VZ
443 wxToolBarToolBase *tool = FindById(id);
444 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
10b959e3 445
8a0681f9 446 return tool->GetLongHelp();
10b959e3
JS
447}
448
8a0681f9
VZ
449// ----------------------------------------------------------------------------
450// wxToolBarBase geometry
451// ----------------------------------------------------------------------------
452
453void wxToolBarBase::SetMargins(int x, int y)
10b959e3 454{
8a0681f9
VZ
455 m_xMargin = x;
456 m_yMargin = y;
10b959e3
JS
457}
458
8a0681f9 459void wxToolBarBase::SetRows(int WXUNUSED(nRows))
10b959e3 460{
8a0681f9 461 // nothing
10b959e3
JS
462}
463
8a0681f9
VZ
464// ----------------------------------------------------------------------------
465// event processing
466// ----------------------------------------------------------------------------
467
468// Only allow toggle if returns TRUE
469bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
10b959e3 470{
8a0681f9
VZ
471 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
472 event.SetEventObject(this);
6bec54e1
VZ
473
474 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
475 event.SetInt((int)toggleDown);
476
477 // and SetExtraLong() for backwards compatibility
478 event.SetExtraLong((long)toggleDown);
8a0681f9
VZ
479
480 // Send events to this toolbar instead (and thence up the window hierarchy)
481 GetEventHandler()->ProcessEvent(event);
482
483 return TRUE;
10b959e3
JS
484}
485
8a0681f9
VZ
486// Call when right button down.
487void wxToolBarBase::OnRightClick(int id,
488 long WXUNUSED(x),
489 long WXUNUSED(y))
10b959e3 490{
8a0681f9
VZ
491 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
492 event.SetEventObject(this);
493 event.SetInt(id);
494
495 GetEventHandler()->ProcessEvent(event);
496}
43d811ea 497
8a0681f9
VZ
498// Called when the mouse cursor enters a tool bitmap (no button pressed).
499// Argument is -1 if mouse is exiting the toolbar.
500// Note that for this event, the id of the window is used,
501// and the integer parameter of wxCommandEvent is used to retrieve
502// the tool id.
503void wxToolBarBase::OnMouseEnter(int id)
504{
505 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
506 event.SetEventObject(this);
507 event.SetInt(id);
508
509 (void)GetEventHandler()->ProcessEvent(event);
510
511 wxToolBarToolBase *tool = FindById(id);
512 if ( !tool || !tool->GetLongHelp() )
513 return;
514
515 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
516 if ( !frame )
517 return;
518
519 frame->SetStatusText(tool->GetLongHelp());
520}
521
522// ----------------------------------------------------------------------------
523// UI updates
524// ----------------------------------------------------------------------------
525
526void wxToolBarBase::OnIdle(wxIdleEvent& event)
527{
ac91b9d2 528 DoToolbarUpdates();
8a0681f9
VZ
529
530 event.Skip();
10b959e3
JS
531}
532
533// Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
ac91b9d2
VZ
534void wxToolBarBase::DoToolbarUpdates()
535{
e63fdcd6
JS
536 wxWindow* parent = this;
537 while (parent->GetParent())
538 parent = parent->GetParent();
539
f0e9f0d3 540#ifdef __WXMSW__
e63fdcd6 541 wxWindow* focusWin = wxFindFocusDescendant(parent);
f0e9f0d3
JS
542#else
543 wxWindow* focusWin = (wxWindow*) NULL;
544#endif
e63fdcd6
JS
545
546 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler() ;
e702ff0f 547
8a0681f9
VZ
548 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
549 node;
550 node = node->GetNext() )
ac91b9d2 551 {
8a0681f9 552 int id = node->GetData()->GetId();
ac91b9d2 553
8a0681f9 554 wxUpdateUIEvent event(id);
ac91b9d2
VZ
555 event.SetEventObject(this);
556
8a0681f9 557 if ( evtHandler->ProcessEvent(event) )
ac91b9d2 558 {
8a0681f9
VZ
559 if ( event.GetSetEnabled() )
560 EnableTool(id, event.GetEnabled());
561 if ( event.GetSetChecked() )
562 ToggleTool(id, event.GetChecked());
563#if 0
564 if ( event.GetSetText() )
ac91b9d2 565 // Set tooltip?
8a0681f9 566#endif // 0
ac91b9d2 567 }
ac91b9d2 568 }
10b959e3
JS
569}
570
8a0681f9 571#endif // wxUSE_TOOLBAR