]> git.saurik.com Git - wxWidgets.git/blame - src/common/tbarbase.cpp
reverted wxMGL-specific code (didn't mean to commit this...)
[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
1e6feb95
VZ
31#if wxUSE_TOOLBAR
32
10b959e3 33#ifndef WX_PRECOMP
f538710f 34 #include "wx/control.h"
10b959e3
JS
35#endif
36
e702ff0f
JS
37#include "wx/frame.h"
38
10b959e3 39// For ::UpdateWindow
2049ba38 40#ifdef __WXMSW__
10b959e3
JS
41#include <windows.h>
42#endif
43
10b959e3
JS
44#include "wx/tbarbase.h"
45
8a0681f9
VZ
46// ----------------------------------------------------------------------------
47// wxWindows macros
48// ----------------------------------------------------------------------------
49
12ed316d
JS
50IMPLEMENT_CLASS(wxToolBarBase, wxControl)
51
1e6feb95
VZ
52BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
53 EVT_IDLE(wxToolBarBase::OnIdle)
54END_EVENT_TABLE()
55
8a0681f9 56#include "wx/listimpl.cpp"
1c383dba 57
8a0681f9 58WX_DEFINE_LIST(wxToolBarToolsList);
10b959e3 59
8a0681f9
VZ
60// ============================================================================
61// implementation
62// ============================================================================
10b959e3 63
8a0681f9
VZ
64// ----------------------------------------------------------------------------
65// wxToolBarToolBase
66// ----------------------------------------------------------------------------
10b959e3 67
8a0681f9 68bool wxToolBarToolBase::Enable(bool enable)
10b959e3 69{
8a0681f9
VZ
70 if ( m_enabled == enable )
71 return FALSE;
10b959e3 72
8a0681f9 73 m_enabled = enable;
10b959e3 74
8a0681f9 75 return TRUE;
10b959e3
JS
76}
77
8a0681f9 78bool wxToolBarToolBase::Toggle(bool toggle)
10b959e3 79{
8a0681f9
VZ
80 wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
81
82 if ( m_toggled == toggle )
83 return FALSE;
10b959e3 84
8a0681f9
VZ
85 m_toggled = toggle;
86
87 return TRUE;
10b959e3
JS
88}
89
8a0681f9 90bool wxToolBarToolBase::SetToggle(bool toggle)
10b959e3 91{
8a0681f9
VZ
92 if ( m_isToggle == toggle )
93 return FALSE;
10b959e3 94
8a0681f9 95 m_isToggle = toggle;
10b959e3
JS
96
97 return TRUE;
98}
99
8a0681f9 100bool wxToolBarToolBase::SetShortHelp(const wxString& help)
10b959e3 101{
8a0681f9
VZ
102 if ( m_shortHelpString == help )
103 return FALSE;
10b959e3 104
8a0681f9
VZ
105 m_shortHelpString = help;
106
107 return TRUE;
10b959e3
JS
108}
109
8a0681f9 110bool wxToolBarToolBase::SetLongHelp(const wxString& help)
10b959e3 111{
8a0681f9
VZ
112 if ( m_longHelpString == help )
113 return FALSE;
10b959e3 114
8a0681f9
VZ
115 m_longHelpString = help;
116
117 return TRUE;
10b959e3
JS
118}
119
8a0681f9 120wxToolBarToolBase::~wxToolBarToolBase()
10b959e3 121{
8a0681f9 122}
10b959e3 123
8a0681f9
VZ
124// ----------------------------------------------------------------------------
125// wxToolBarBase adding/deleting items
126// ----------------------------------------------------------------------------
ac91b9d2 127
8a0681f9
VZ
128wxToolBarBase::wxToolBarBase()
129{
130 // the list owns the pointers
131 m_tools.DeleteContents(TRUE);
10b959e3 132
8a0681f9 133 m_xMargin = m_yMargin = 0;
10b959e3 134
8a0681f9 135 m_maxRows = m_maxCols = 0;
10b959e3
JS
136}
137
8a0681f9
VZ
138wxToolBarToolBase *wxToolBarBase::AddTool(int id,
139 const wxBitmap& bitmap,
140 const wxBitmap& pushedBitmap,
141 bool toggle,
142 wxCoord WXUNUSED(xPos),
143 wxCoord WXUNUSED(yPos),
144 wxObject *clientData,
145 const wxString& helpString1,
146 const wxString& helpString2)
10b959e3 147{
8a0681f9
VZ
148 return InsertTool(GetToolsCount(), id, bitmap, pushedBitmap,
149 toggle, clientData, helpString1, helpString2);
10b959e3
JS
150}
151
8a0681f9
VZ
152wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
153 int id,
154 const wxBitmap& bitmap,
155 const wxBitmap& pushedBitmap,
156 bool toggle,
157 wxObject *clientData,
158 const wxString& helpString1,
159 const wxString& helpString2)
10b959e3 160{
8a0681f9
VZ
161 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
162 _T("invalid position in wxToolBar::InsertTool()") );
163
164 wxToolBarToolBase *tool = CreateTool(id, bitmap, pushedBitmap, toggle,
165 clientData, helpString1, helpString2);
166
167 if ( !tool || !DoInsertTool(pos, tool) )
168 {
169 delete tool;
170
171 return NULL;
172 }
173
174 m_tools.Insert(pos, tool);
175
176 return tool;
10b959e3
JS
177}
178
8a0681f9 179wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
10b959e3 180{
8a0681f9 181 return InsertControl(GetToolsCount(), control);
10b959e3
JS
182}
183
8a0681f9 184wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
10b959e3 185{
8a0681f9
VZ
186 wxCHECK_MSG( control, (wxToolBarToolBase *)NULL,
187 _T("toolbar: can't insert NULL control") );
188
189 wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL,
190 _T("control must have toolbar as parent") );
191
192 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
193 _T("invalid position in wxToolBar::InsertControl()") );
194
195 wxToolBarToolBase *tool = CreateTool(control);
196
197 if ( !tool || !DoInsertTool(pos, tool) )
198 {
199 delete tool;
200
201 return NULL;
202 }
203
204 m_tools.Insert(pos, tool);
205
206 return tool;
10b959e3
JS
207}
208
8a0681f9 209wxToolBarToolBase *wxToolBarBase::AddSeparator()
10b959e3 210{
8a0681f9 211 return InsertSeparator(GetToolsCount());
10b959e3
JS
212}
213
8a0681f9 214wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
10b959e3 215{
8a0681f9
VZ
216 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
217 _T("invalid position in wxToolBar::InsertSeparator()") );
218
219 wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR,
220 wxNullBitmap, wxNullBitmap,
221 FALSE, (wxObject *)NULL,
222 wxEmptyString, wxEmptyString);
223
224 if ( !tool || !DoInsertTool(pos, tool) )
10b959e3 225 {
8a0681f9
VZ
226 delete tool;
227
228 return NULL;
10b959e3 229 }
8a0681f9
VZ
230
231 m_tools.Insert(pos, tool);
232
233 return tool;
10b959e3
JS
234}
235
8a0681f9 236wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
10b959e3 237{
8a0681f9
VZ
238 size_t pos = 0;
239 wxToolBarToolsList::Node *node;
240 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
10b959e3 241 {
8a0681f9
VZ
242 if ( node->GetData()->GetId() == id )
243 break;
244
245 pos++;
10b959e3 246 }
10b959e3 247
8a0681f9 248 if ( !node )
10b959e3 249 {
8a0681f9
VZ
250 // don't give any error messages - sometimes we might call RemoveTool()
251 // without knowing whether the tool is or not in the toolbar
252 return (wxToolBarToolBase *)NULL;
10b959e3 253 }
10b959e3 254
8a0681f9
VZ
255 wxToolBarToolBase *tool = node->GetData();
256 if ( !DoDeleteTool(pos, tool) )
257 {
258 return (wxToolBarToolBase *)NULL;
259 }
10b959e3 260
8a0681f9
VZ
261 // the node would delete the data, so set it to NULL to avoid this
262 node->SetData(NULL);
10b959e3 263
8a0681f9 264 m_tools.DeleteNode(node);
10b959e3 265
8a0681f9 266 return tool;
10b959e3
JS
267}
268
8a0681f9 269bool wxToolBarBase::DeleteToolByPos(size_t pos)
10b959e3 270{
8a0681f9
VZ
271 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
272 _T("invalid position in wxToolBar::DeleteToolByPos()") );
10b959e3 273
8a0681f9 274 wxToolBarToolsList::Node *node = m_tools.Item(pos);
10b959e3 275
8a0681f9
VZ
276 if ( !DoDeleteTool(pos, node->GetData()) )
277 {
278 return FALSE;
279 }
280
281 m_tools.DeleteNode(node);
282
283 return TRUE;
10b959e3
JS
284}
285
8a0681f9 286bool wxToolBarBase::DeleteTool(int id)
10b959e3 287{
8a0681f9
VZ
288 size_t pos = 0;
289 wxToolBarToolsList::Node *node;
290 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
291 {
292 if ( node->GetData()->GetId() == id )
293 break;
294
295 pos++;
296 }
297
298 if ( !node || !DoDeleteTool(pos, node->GetData()) )
299 {
300 return FALSE;
301 }
302
303 m_tools.DeleteNode(node);
304
305 return TRUE;
10b959e3
JS
306}
307
8a0681f9 308wxToolBarToolBase *wxToolBarBase::FindById(int id) const
10b959e3 309{
8a0681f9
VZ
310 wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL;
311
312 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
313 node;
314 node = node->GetNext() )
315 {
316 tool = node->GetData();
317 if ( tool->GetId() == id )
318 {
319 // found
320 break;
321 }
d9739886
VZ
322
323 tool = NULL;
8a0681f9
VZ
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