]> git.saurik.com Git - wxWidgets.git/blame - src/common/tbarbase.cpp
added wxCAL_MONDAY/SUNDAY_FIRST flags and Ctrl-Home/Right/Left and Home/End keys
[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
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 }
322 }
323
324 return tool;
10b959e3
JS
325}
326
8a0681f9 327void wxToolBarBase::ClearTools()
10b959e3 328{
8a0681f9 329 m_tools.Clear();
10b959e3
JS
330}
331
8a0681f9 332bool wxToolBarBase::Realize()
10b959e3 333{
8a0681f9 334 return TRUE;
10b959e3
JS
335}
336
8a0681f9 337wxToolBarBase::~wxToolBarBase()
10b959e3 338{
10b959e3
JS
339}
340
8a0681f9
VZ
341// ----------------------------------------------------------------------------
342// wxToolBarBase tools state
343// ----------------------------------------------------------------------------
10b959e3 344
8a0681f9 345void wxToolBarBase::EnableTool(int id, bool enable)
10b959e3 346{
8a0681f9
VZ
347 wxToolBarToolBase *tool = FindById(id);
348 if ( tool )
10b959e3 349 {
8a0681f9
VZ
350 if ( tool->Enable(enable) )
351 {
352 DoEnableTool(tool, enable);
353 }
10b959e3 354 }
8a0681f9 355}
10b959e3 356
8a0681f9
VZ
357void wxToolBarBase::ToggleTool(int id, bool toggle)
358{
359 wxToolBarToolBase *tool = FindById(id);
360 if ( tool && tool->CanBeToggled() )
10b959e3 361 {
8a0681f9
VZ
362 if ( tool->Toggle(toggle) )
363 {
364 DoToggleTool(tool, toggle);
365 }
10b959e3 366 }
10b959e3
JS
367}
368
8a0681f9
VZ
369void wxToolBarBase::SetToggle(int id, bool toggle)
370{
371 wxToolBarToolBase *tool = FindById(id);
372 if ( tool )
10b959e3 373 {
8a0681f9
VZ
374 if ( tool->SetToggle(toggle) )
375 {
376 DoSetToggle(tool, toggle);
377 }
10b959e3 378 }
8a0681f9
VZ
379}
380
381void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
382{
383 wxToolBarToolBase *tool = FindById(id);
384 if ( tool )
10b959e3 385 {
8a0681f9 386 (void)tool->SetShortHelp(help);
10b959e3 387 }
8a0681f9
VZ
388}
389
390void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
391{
392 wxToolBarToolBase *tool = FindById(id);
393 if ( tool )
10b959e3 394 {
8a0681f9 395 (void)tool->SetLongHelp(help);
10b959e3 396 }
10b959e3
JS
397}
398
8a0681f9 399wxObject *wxToolBarBase::GetToolClientData(int id) const
10b959e3 400{
8a0681f9
VZ
401 wxToolBarToolBase *tool = FindById(id);
402
403 return tool ? tool->GetClientData() : (wxObject *)NULL;
10b959e3
JS
404}
405
6fd5fa4f
VZ
406void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
407{
408 wxToolBarToolBase *tool = FindById(id);
409
410 wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") );
411
412 tool->SetClientData(clientData);
413}
414
8a0681f9 415bool wxToolBarBase::GetToolState(int id) const
10b959e3 416{
8a0681f9
VZ
417 wxToolBarToolBase *tool = FindById(id);
418 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
419
420 return tool->IsToggled();
10b959e3
JS
421}
422
8a0681f9 423bool wxToolBarBase::GetToolEnabled(int id) const
10b959e3 424{
8a0681f9
VZ
425 wxToolBarToolBase *tool = FindById(id);
426 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
427
428 return tool->IsEnabled();
10b959e3
JS
429}
430
8a0681f9 431wxString wxToolBarBase::GetToolShortHelp(int id) const
10b959e3 432{
8a0681f9
VZ
433 wxToolBarToolBase *tool = FindById(id);
434 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
435
436 return tool->GetShortHelp();
10b959e3
JS
437}
438
8a0681f9 439wxString wxToolBarBase::GetToolLongHelp(int id) const
10b959e3 440{
8a0681f9
VZ
441 wxToolBarToolBase *tool = FindById(id);
442 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
10b959e3 443
8a0681f9 444 return tool->GetLongHelp();
10b959e3
JS
445}
446
8a0681f9
VZ
447// ----------------------------------------------------------------------------
448// wxToolBarBase geometry
449// ----------------------------------------------------------------------------
450
451void wxToolBarBase::SetMargins(int x, int y)
10b959e3 452{
8a0681f9
VZ
453 m_xMargin = x;
454 m_yMargin = y;
10b959e3
JS
455}
456
8a0681f9 457void wxToolBarBase::SetRows(int WXUNUSED(nRows))
10b959e3 458{
8a0681f9 459 // nothing
10b959e3
JS
460}
461
8a0681f9
VZ
462// ----------------------------------------------------------------------------
463// event processing
464// ----------------------------------------------------------------------------
465
466// Only allow toggle if returns TRUE
467bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
10b959e3 468{
8a0681f9
VZ
469 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
470 event.SetEventObject(this);
471 event.SetExtraLong((long) toggleDown);
472
473 // Send events to this toolbar instead (and thence up the window hierarchy)
474 GetEventHandler()->ProcessEvent(event);
475
476 return TRUE;
10b959e3
JS
477}
478
8a0681f9
VZ
479// Call when right button down.
480void wxToolBarBase::OnRightClick(int id,
481 long WXUNUSED(x),
482 long WXUNUSED(y))
10b959e3 483{
8a0681f9
VZ
484 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
485 event.SetEventObject(this);
486 event.SetInt(id);
487
488 GetEventHandler()->ProcessEvent(event);
489}
43d811ea 490
8a0681f9
VZ
491// Called when the mouse cursor enters a tool bitmap (no button pressed).
492// Argument is -1 if mouse is exiting the toolbar.
493// Note that for this event, the id of the window is used,
494// and the integer parameter of wxCommandEvent is used to retrieve
495// the tool id.
496void wxToolBarBase::OnMouseEnter(int id)
497{
498 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
499 event.SetEventObject(this);
500 event.SetInt(id);
501
502 (void)GetEventHandler()->ProcessEvent(event);
503
504 wxToolBarToolBase *tool = FindById(id);
505 if ( !tool || !tool->GetLongHelp() )
506 return;
507
508 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
509 if ( !frame )
510 return;
511
512 frame->SetStatusText(tool->GetLongHelp());
513}
514
515// ----------------------------------------------------------------------------
516// UI updates
517// ----------------------------------------------------------------------------
518
519void wxToolBarBase::OnIdle(wxIdleEvent& event)
520{
ac91b9d2 521 DoToolbarUpdates();
8a0681f9
VZ
522
523 event.Skip();
10b959e3
JS
524}
525
526// Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
ac91b9d2
VZ
527void wxToolBarBase::DoToolbarUpdates()
528{
8a0681f9 529 wxEvtHandler* evtHandler = GetEventHandler();
e702ff0f 530
8a0681f9
VZ
531 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
532 node;
533 node = node->GetNext() )
ac91b9d2 534 {
8a0681f9 535 int id = node->GetData()->GetId();
ac91b9d2 536
8a0681f9 537 wxUpdateUIEvent event(id);
ac91b9d2
VZ
538 event.SetEventObject(this);
539
8a0681f9 540 if ( evtHandler->ProcessEvent(event) )
ac91b9d2 541 {
8a0681f9
VZ
542 if ( event.GetSetEnabled() )
543 EnableTool(id, event.GetEnabled());
544 if ( event.GetSetChecked() )
545 ToggleTool(id, event.GetChecked());
546#if 0
547 if ( event.GetSetText() )
ac91b9d2 548 // Set tooltip?
8a0681f9 549#endif // 0
ac91b9d2 550 }
ac91b9d2 551 }
10b959e3
JS
552}
553
8a0681f9 554#endif // wxUSE_TOOLBAR