]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/tbarbase.cpp
Accomodate changes in CountColours
[wxWidgets.git] / src / common / tbarbase.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/tbarbase.cpp
3// Purpose: wxToolBarBase implementation
4// Author: Julian Smart
5// Modified by: VZ at 11.12.99 (wxScrollableToolBar splitted off)
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "tbarbase.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/wx.h"
33#endif
34
35#include "wx/frame.h"
36
37// For ::UpdateWindow
38#ifdef __WXMSW__
39#include <windows.h>
40#endif
41
42#if wxUSE_TOOLBAR
43
44#include "wx/tbarbase.h"
45
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()
54#endif
55
56#include "wx/listimpl.cpp"
57
58WX_DEFINE_LIST(wxToolBarToolsList);
59
60// ============================================================================
61// implementation
62// ============================================================================
63
64// ----------------------------------------------------------------------------
65// wxToolBarToolBase
66// ----------------------------------------------------------------------------
67
68bool wxToolBarToolBase::Enable(bool enable)
69{
70 if ( m_enabled == enable )
71 return FALSE;
72
73 m_enabled = enable;
74
75 return TRUE;
76}
77
78bool wxToolBarToolBase::Toggle(bool toggle)
79{
80 wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
81
82 if ( m_toggled == toggle )
83 return FALSE;
84
85 m_toggled = toggle;
86
87 return TRUE;
88}
89
90bool wxToolBarToolBase::SetToggle(bool toggle)
91{
92 if ( m_isToggle == toggle )
93 return FALSE;
94
95 m_isToggle = toggle;
96
97 return TRUE;
98}
99
100bool wxToolBarToolBase::SetShortHelp(const wxString& help)
101{
102 if ( m_shortHelpString == help )
103 return FALSE;
104
105 m_shortHelpString = help;
106
107 return TRUE;
108}
109
110bool wxToolBarToolBase::SetLongHelp(const wxString& help)
111{
112 if ( m_longHelpString == help )
113 return FALSE;
114
115 m_longHelpString = help;
116
117 return TRUE;
118}
119
120wxToolBarToolBase::~wxToolBarToolBase()
121{
122}
123
124// ----------------------------------------------------------------------------
125// wxToolBarBase adding/deleting items
126// ----------------------------------------------------------------------------
127
128wxToolBarBase::wxToolBarBase()
129{
130 // the list owns the pointers
131 m_tools.DeleteContents(TRUE);
132
133 m_xMargin = m_yMargin = 0;
134
135 m_maxRows = m_maxCols = 0;
136}
137
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)
147{
148 return InsertTool(GetToolsCount(), id, bitmap, pushedBitmap,
149 toggle, clientData, helpString1, helpString2);
150}
151
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)
160{
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;
177}
178
179wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
180{
181 return InsertControl(GetToolsCount(), control);
182}
183
184wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
185{
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;
207}
208
209wxToolBarToolBase *wxToolBarBase::AddSeparator()
210{
211 return InsertSeparator(GetToolsCount());
212}
213
214wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
215{
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) )
225 {
226 delete tool;
227
228 return NULL;
229 }
230
231 m_tools.Insert(pos, tool);
232
233 return tool;
234}
235
236wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
237{
238 size_t pos = 0;
239 wxToolBarToolsList::Node *node;
240 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
241 {
242 if ( node->GetData()->GetId() == id )
243 break;
244
245 pos++;
246 }
247
248 if ( !node )
249 {
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;
253 }
254
255 wxToolBarToolBase *tool = node->GetData();
256 if ( !DoDeleteTool(pos, tool) )
257 {
258 return (wxToolBarToolBase *)NULL;
259 }
260
261 // the node would delete the data, so set it to NULL to avoid this
262 node->SetData(NULL);
263
264 m_tools.DeleteNode(node);
265
266 return tool;
267}
268
269bool wxToolBarBase::DeleteToolByPos(size_t pos)
270{
271 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
272 _T("invalid position in wxToolBar::DeleteToolByPos()") );
273
274 wxToolBarToolsList::Node *node = m_tools.Item(pos);
275
276 if ( !DoDeleteTool(pos, node->GetData()) )
277 {
278 return FALSE;
279 }
280
281 m_tools.DeleteNode(node);
282
283 return TRUE;
284}
285
286bool wxToolBarBase::DeleteTool(int id)
287{
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;
306}
307
308wxToolBarToolBase *wxToolBarBase::FindById(int id) const
309{
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;
325}
326
327void wxToolBarBase::ClearTools()
328{
329 m_tools.Clear();
330}
331
332bool wxToolBarBase::Realize()
333{
334 return TRUE;
335}
336
337wxToolBarBase::~wxToolBarBase()
338{
339}
340
341// ----------------------------------------------------------------------------
342// wxToolBarBase tools state
343// ----------------------------------------------------------------------------
344
345void wxToolBarBase::EnableTool(int id, bool enable)
346{
347 wxToolBarToolBase *tool = FindById(id);
348 if ( tool )
349 {
350 if ( tool->Enable(enable) )
351 {
352 DoEnableTool(tool, enable);
353 }
354 }
355}
356
357void wxToolBarBase::ToggleTool(int id, bool toggle)
358{
359 wxToolBarToolBase *tool = FindById(id);
360 if ( tool && tool->CanBeToggled() )
361 {
362 if ( tool->Toggle(toggle) )
363 {
364 DoToggleTool(tool, toggle);
365 }
366 }
367}
368
369void wxToolBarBase::SetToggle(int id, bool toggle)
370{
371 wxToolBarToolBase *tool = FindById(id);
372 if ( tool )
373 {
374 if ( tool->SetToggle(toggle) )
375 {
376 DoSetToggle(tool, toggle);
377 }
378 }
379}
380
381void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
382{
383 wxToolBarToolBase *tool = FindById(id);
384 if ( tool )
385 {
386 (void)tool->SetShortHelp(help);
387 }
388}
389
390void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
391{
392 wxToolBarToolBase *tool = FindById(id);
393 if ( tool )
394 {
395 (void)tool->SetLongHelp(help);
396 }
397}
398
399wxObject *wxToolBarBase::GetToolClientData(int id) const
400{
401 wxToolBarToolBase *tool = FindById(id);
402
403 return tool ? tool->GetClientData() : (wxObject *)NULL;
404}
405
406bool wxToolBarBase::GetToolState(int id) const
407{
408 wxToolBarToolBase *tool = FindById(id);
409 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
410
411 return tool->IsToggled();
412}
413
414bool wxToolBarBase::GetToolEnabled(int id) const
415{
416 wxToolBarToolBase *tool = FindById(id);
417 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
418
419 return tool->IsEnabled();
420}
421
422wxString wxToolBarBase::GetToolShortHelp(int id) const
423{
424 wxToolBarToolBase *tool = FindById(id);
425 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
426
427 return tool->GetShortHelp();
428}
429
430wxString wxToolBarBase::GetToolLongHelp(int id) const
431{
432 wxToolBarToolBase *tool = FindById(id);
433 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
434
435 return tool->GetLongHelp();
436}
437
438// ----------------------------------------------------------------------------
439// wxToolBarBase geometry
440// ----------------------------------------------------------------------------
441
442void wxToolBarBase::SetMargins(int x, int y)
443{
444 m_xMargin = x;
445 m_yMargin = y;
446}
447
448void wxToolBarBase::SetRows(int WXUNUSED(nRows))
449{
450 // nothing
451}
452
453// ----------------------------------------------------------------------------
454// event processing
455// ----------------------------------------------------------------------------
456
457// Only allow toggle if returns TRUE
458bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
459{
460 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
461 event.SetEventObject(this);
462 event.SetExtraLong((long) toggleDown);
463
464 // Send events to this toolbar instead (and thence up the window hierarchy)
465 GetEventHandler()->ProcessEvent(event);
466
467 return TRUE;
468}
469
470// Call when right button down.
471void wxToolBarBase::OnRightClick(int id,
472 long WXUNUSED(x),
473 long WXUNUSED(y))
474{
475 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
476 event.SetEventObject(this);
477 event.SetInt(id);
478
479 GetEventHandler()->ProcessEvent(event);
480}
481
482// Called when the mouse cursor enters a tool bitmap (no button pressed).
483// Argument is -1 if mouse is exiting the toolbar.
484// Note that for this event, the id of the window is used,
485// and the integer parameter of wxCommandEvent is used to retrieve
486// the tool id.
487void wxToolBarBase::OnMouseEnter(int id)
488{
489 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
490 event.SetEventObject(this);
491 event.SetInt(id);
492
493 (void)GetEventHandler()->ProcessEvent(event);
494
495 wxToolBarToolBase *tool = FindById(id);
496 if ( !tool || !tool->GetLongHelp() )
497 return;
498
499 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
500 if ( !frame )
501 return;
502
503 frame->SetStatusText(tool->GetLongHelp());
504}
505
506// ----------------------------------------------------------------------------
507// UI updates
508// ----------------------------------------------------------------------------
509
510void wxToolBarBase::OnIdle(wxIdleEvent& event)
511{
512 DoToolbarUpdates();
513
514 event.Skip();
515}
516
517// Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
518void wxToolBarBase::DoToolbarUpdates()
519{
520 wxEvtHandler* evtHandler = GetEventHandler();
521
522 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
523 node;
524 node = node->GetNext() )
525 {
526 int id = node->GetData()->GetId();
527
528 wxUpdateUIEvent event(id);
529 event.SetEventObject(this);
530
531 if ( evtHandler->ProcessEvent(event) )
532 {
533 if ( event.GetSetEnabled() )
534 EnableTool(id, event.GetEnabled());
535 if ( event.GetSetChecked() )
536 ToggleTool(id, event.GetChecked());
537#if 0
538 if ( event.GetSetText() )
539 // Set tooltip?
540#endif // 0
541 }
542 }
543}
544
545#endif // wxUSE_TOOLBAR