]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/tbarbase.cpp
Various fixes for wxFilename.
[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
56IMPLEMENT_CLASS(wxToolBarBase, wxControl)
57
58#include "wx/listimpl.cpp"
59
60WX_DEFINE_LIST(wxToolBarToolsList);
61
62// ============================================================================
63// implementation
64// ============================================================================
65
66// ----------------------------------------------------------------------------
67// wxToolBarToolBase
68// ----------------------------------------------------------------------------
69
70bool wxToolBarToolBase::Enable(bool enable)
71{
72 if ( m_enabled == enable )
73 return FALSE;
74
75 m_enabled = enable;
76
77 return TRUE;
78}
79
80bool wxToolBarToolBase::Toggle(bool toggle)
81{
82 wxASSERT_MSG( m_isToggle, _T("can't toggle this tool") );
83
84 if ( m_toggled == toggle )
85 return FALSE;
86
87 m_toggled = toggle;
88
89 return TRUE;
90}
91
92bool wxToolBarToolBase::SetToggle(bool toggle)
93{
94 if ( m_isToggle == toggle )
95 return FALSE;
96
97 m_isToggle = toggle;
98
99 return TRUE;
100}
101
102bool wxToolBarToolBase::SetShortHelp(const wxString& help)
103{
104 if ( m_shortHelpString == help )
105 return FALSE;
106
107 m_shortHelpString = help;
108
109 return TRUE;
110}
111
112bool wxToolBarToolBase::SetLongHelp(const wxString& help)
113{
114 if ( m_longHelpString == help )
115 return FALSE;
116
117 m_longHelpString = help;
118
119 return TRUE;
120}
121
122wxToolBarToolBase::~wxToolBarToolBase()
123{
124}
125
126// ----------------------------------------------------------------------------
127// wxToolBarBase adding/deleting items
128// ----------------------------------------------------------------------------
129
130wxToolBarBase::wxToolBarBase()
131{
132 // the list owns the pointers
133 m_tools.DeleteContents(TRUE);
134
135 m_xMargin = m_yMargin = 0;
136
137 m_maxRows = m_maxCols = 0;
138}
139
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)
149{
150 return InsertTool(GetToolsCount(), id, bitmap, pushedBitmap,
151 toggle, clientData, helpString1, helpString2);
152}
153
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)
162{
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;
179}
180
181wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
182{
183 return InsertControl(GetToolsCount(), control);
184}
185
186wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
187{
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;
209}
210
211wxToolBarToolBase *wxToolBarBase::AddSeparator()
212{
213 return InsertSeparator(GetToolsCount());
214}
215
216wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
217{
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) )
227 {
228 delete tool;
229
230 return NULL;
231 }
232
233 m_tools.Insert(pos, tool);
234
235 return tool;
236}
237
238wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
239{
240 size_t pos = 0;
241 wxToolBarToolsList::Node *node;
242 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
243 {
244 if ( node->GetData()->GetId() == id )
245 break;
246
247 pos++;
248 }
249
250 if ( !node )
251 {
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;
255 }
256
257 wxToolBarToolBase *tool = node->GetData();
258 if ( !DoDeleteTool(pos, tool) )
259 {
260 return (wxToolBarToolBase *)NULL;
261 }
262
263 // the node would delete the data, so set it to NULL to avoid this
264 node->SetData(NULL);
265
266 m_tools.DeleteNode(node);
267
268 return tool;
269}
270
271bool wxToolBarBase::DeleteToolByPos(size_t pos)
272{
273 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
274 _T("invalid position in wxToolBar::DeleteToolByPos()") );
275
276 wxToolBarToolsList::Node *node = m_tools.Item(pos);
277
278 if ( !DoDeleteTool(pos, node->GetData()) )
279 {
280 return FALSE;
281 }
282
283 m_tools.DeleteNode(node);
284
285 return TRUE;
286}
287
288bool wxToolBarBase::DeleteTool(int id)
289{
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;
308}
309
310wxToolBarToolBase *wxToolBarBase::FindById(int id) const
311{
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;
327}
328
329void wxToolBarBase::ClearTools()
330{
331 m_tools.Clear();
332}
333
334bool wxToolBarBase::Realize()
335{
336 return TRUE;
337}
338
339wxToolBarBase::~wxToolBarBase()
340{
341}
342
343// ----------------------------------------------------------------------------
344// wxToolBarBase tools state
345// ----------------------------------------------------------------------------
346
347void wxToolBarBase::EnableTool(int id, bool enable)
348{
349 wxToolBarToolBase *tool = FindById(id);
350 if ( tool )
351 {
352 if ( tool->Enable(enable) )
353 {
354 DoEnableTool(tool, enable);
355 }
356 }
357}
358
359void wxToolBarBase::ToggleTool(int id, bool toggle)
360{
361 wxToolBarToolBase *tool = FindById(id);
362 if ( tool && tool->CanBeToggled() )
363 {
364 if ( tool->Toggle(toggle) )
365 {
366 DoToggleTool(tool, toggle);
367 }
368 }
369}
370
371void wxToolBarBase::SetToggle(int id, bool toggle)
372{
373 wxToolBarToolBase *tool = FindById(id);
374 if ( tool )
375 {
376 if ( tool->SetToggle(toggle) )
377 {
378 DoSetToggle(tool, toggle);
379 }
380 }
381}
382
383void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
384{
385 wxToolBarToolBase *tool = FindById(id);
386 if ( tool )
387 {
388 (void)tool->SetShortHelp(help);
389 }
390}
391
392void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
393{
394 wxToolBarToolBase *tool = FindById(id);
395 if ( tool )
396 {
397 (void)tool->SetLongHelp(help);
398 }
399}
400
401wxObject *wxToolBarBase::GetToolClientData(int id) const
402{
403 wxToolBarToolBase *tool = FindById(id);
404
405 return tool ? tool->GetClientData() : (wxObject *)NULL;
406}
407
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
417bool wxToolBarBase::GetToolState(int id) const
418{
419 wxToolBarToolBase *tool = FindById(id);
420 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
421
422 return tool->IsToggled();
423}
424
425bool wxToolBarBase::GetToolEnabled(int id) const
426{
427 wxToolBarToolBase *tool = FindById(id);
428 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
429
430 return tool->IsEnabled();
431}
432
433wxString wxToolBarBase::GetToolShortHelp(int id) const
434{
435 wxToolBarToolBase *tool = FindById(id);
436 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
437
438 return tool->GetShortHelp();
439}
440
441wxString wxToolBarBase::GetToolLongHelp(int id) const
442{
443 wxToolBarToolBase *tool = FindById(id);
444 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
445
446 return tool->GetLongHelp();
447}
448
449// ----------------------------------------------------------------------------
450// wxToolBarBase geometry
451// ----------------------------------------------------------------------------
452
453void wxToolBarBase::SetMargins(int x, int y)
454{
455 m_xMargin = x;
456 m_yMargin = y;
457}
458
459void wxToolBarBase::SetRows(int WXUNUSED(nRows))
460{
461 // nothing
462}
463
464// ----------------------------------------------------------------------------
465// event processing
466// ----------------------------------------------------------------------------
467
468// Only allow toggle if returns TRUE
469bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
470{
471 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
472 event.SetEventObject(this);
473 event.SetExtraLong((long) toggleDown);
474
475 // Send events to this toolbar instead (and thence up the window hierarchy)
476 GetEventHandler()->ProcessEvent(event);
477
478 return TRUE;
479}
480
481// Call when right button down.
482void wxToolBarBase::OnRightClick(int id,
483 long WXUNUSED(x),
484 long WXUNUSED(y))
485{
486 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
487 event.SetEventObject(this);
488 event.SetInt(id);
489
490 GetEventHandler()->ProcessEvent(event);
491}
492
493// Called when the mouse cursor enters a tool bitmap (no button pressed).
494// Argument is -1 if mouse is exiting the toolbar.
495// Note that for this event, the id of the window is used,
496// and the integer parameter of wxCommandEvent is used to retrieve
497// the tool id.
498void wxToolBarBase::OnMouseEnter(int id)
499{
500 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
501 event.SetEventObject(this);
502 event.SetInt(id);
503
504 (void)GetEventHandler()->ProcessEvent(event);
505
506 wxToolBarToolBase *tool = FindById(id);
507 if ( !tool || !tool->GetLongHelp() )
508 return;
509
510 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
511 if ( !frame )
512 return;
513
514 frame->SetStatusText(tool->GetLongHelp());
515}
516
517// ----------------------------------------------------------------------------
518// UI updates
519// ----------------------------------------------------------------------------
520
521void wxToolBarBase::OnIdle(wxIdleEvent& event)
522{
523 DoToolbarUpdates();
524
525 event.Skip();
526}
527
528// Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
529void wxToolBarBase::DoToolbarUpdates()
530{
531 wxWindow* parent = this;
532 while (parent->GetParent())
533 parent = parent->GetParent();
534
535#ifdef __WXMSW__
536 wxWindow* focusWin = wxFindFocusDescendant(parent);
537#else
538 wxWindow* focusWin = (wxWindow*) NULL;
539#endif
540
541 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler() ;
542
543 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
544 node;
545 node = node->GetNext() )
546 {
547 int id = node->GetData()->GetId();
548
549 wxUpdateUIEvent event(id);
550 event.SetEventObject(this);
551
552 if ( evtHandler->ProcessEvent(event) )
553 {
554 if ( event.GetSetEnabled() )
555 EnableTool(id, event.GetEnabled());
556 if ( event.GetSetChecked() )
557 ToggleTool(id, event.GetChecked());
558#if 0
559 if ( event.GetSetText() )
560 // Set tooltip?
561#endif // 0
562 }
563 }
564}
565
566#endif // wxUSE_TOOLBAR