]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
No significant change
[wxWidgets.git] / src / common / tbarbase.cpp
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
9 // Licence: wxWindows licence
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 #if wxUSE_TOOLBAR
32
33 #ifndef WX_PRECOMP
34 #include "wx/control.h"
35 #endif
36
37 #include "wx/frame.h"
38 #include "wx/image.h"
39 #include "wx/settings.h"
40
41 #include "wx/toolbar.h"
42
43 // ----------------------------------------------------------------------------
44 // wxWindows macros
45 // ----------------------------------------------------------------------------
46
47 IMPLEMENT_CLASS(wxToolBarBase, wxControl)
48
49 BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
50 END_EVENT_TABLE()
51
52 #include "wx/listimpl.cpp"
53
54 WX_DEFINE_LIST(wxToolBarToolsList);
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // wxToolBarToolBase
62 // ----------------------------------------------------------------------------
63
64 bool wxToolBarToolBase::Enable(bool enable)
65 {
66 if ( m_enabled == enable )
67 return FALSE;
68
69 m_enabled = enable;
70
71 return TRUE;
72 }
73
74 bool wxToolBarToolBase::Toggle(bool toggle)
75 {
76 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
77
78 if ( m_toggled == toggle )
79 return FALSE;
80
81 m_toggled = toggle;
82
83 return TRUE;
84 }
85
86 bool wxToolBarToolBase::SetToggle(bool toggle)
87 {
88 wxItemKind kind = toggle ? wxITEM_CHECK : wxITEM_NORMAL;
89 if ( m_kind == kind )
90 return FALSE;
91
92 m_kind = kind;
93
94 return TRUE;
95 }
96
97 bool wxToolBarToolBase::SetShortHelp(const wxString& help)
98 {
99 if ( m_shortHelpString == help )
100 return FALSE;
101
102 m_shortHelpString = help;
103
104 return TRUE;
105 }
106
107 bool wxToolBarToolBase::SetLongHelp(const wxString& help)
108 {
109 if ( m_longHelpString == help )
110 return FALSE;
111
112 m_longHelpString = help;
113
114 return TRUE;
115 }
116
117 wxToolBarToolBase::~wxToolBarToolBase()
118 {
119 }
120
121 // ----------------------------------------------------------------------------
122 // wxToolBarBase adding/deleting items
123 // ----------------------------------------------------------------------------
124
125 wxToolBarBase::wxToolBarBase()
126 {
127 // the list owns the pointers
128 m_xMargin = m_yMargin = 0;
129
130 m_maxRows = m_maxCols = 0;
131 }
132
133 wxToolBarToolBase *wxToolBarBase::DoAddTool(int id,
134 const wxString& label,
135 const wxBitmap& bitmap,
136 const wxBitmap& bmpDisabled,
137 wxItemKind kind,
138 const wxString& shortHelp,
139 const wxString& longHelp,
140 wxObject *clientData,
141 wxCoord WXUNUSED(xPos),
142 wxCoord WXUNUSED(yPos))
143 {
144 return InsertTool(GetToolsCount(), id, label, bitmap, bmpDisabled,
145 kind, shortHelp, longHelp, clientData);
146 }
147
148 wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
149 int id,
150 const wxString& label,
151 const wxBitmap& bitmap,
152 const wxBitmap& bmpDisabled,
153 wxItemKind kind,
154 const wxString& shortHelp,
155 const wxString& longHelp,
156 wxObject *clientData)
157 {
158 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
159 _T("invalid position in wxToolBar::InsertTool()") );
160
161 wxToolBarToolBase *tool = CreateTool(id, label, bitmap, bmpDisabled, kind,
162 clientData, shortHelp, longHelp);
163
164 if ( !InsertTool(pos, tool) )
165 {
166 delete tool;
167
168 return NULL;
169 }
170
171 return tool;
172 }
173
174 wxToolBarToolBase *wxToolBarBase::AddTool(wxToolBarToolBase *tool)
175 {
176 return InsertTool(GetToolsCount(), tool);
177 }
178
179 wxToolBarToolBase *
180 wxToolBarBase::InsertTool(size_t pos, wxToolBarToolBase *tool)
181 {
182 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
183 _T("invalid position in wxToolBar::InsertTool()") );
184
185 if ( !tool || !DoInsertTool(pos, tool) )
186 {
187 return NULL;
188 }
189
190 m_tools.Insert(pos, tool);
191
192 return tool;
193 }
194
195 wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
196 {
197 return InsertControl(GetToolsCount(), control);
198 }
199
200 wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
201 {
202 wxCHECK_MSG( control, (wxToolBarToolBase *)NULL,
203 _T("toolbar: can't insert NULL control") );
204
205 wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL,
206 _T("control must have toolbar as parent") );
207
208 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
209 _T("invalid position in wxToolBar::InsertControl()") );
210
211 wxToolBarToolBase *tool = CreateTool(control);
212
213 if ( !InsertTool(pos, tool) )
214 {
215 delete tool;
216
217 return NULL;
218 }
219
220 return tool;
221 }
222
223 wxControl *wxToolBarBase::FindControl( int id )
224 {
225 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
226 node;
227 node = node->GetNext() )
228 {
229 const wxToolBarToolBase * const tool = node->GetData();
230 if ( tool->IsControl() )
231 {
232 wxControl * const control = tool->GetControl();
233
234 if ( !control )
235 {
236 wxFAIL_MSG( _T("NULL control in toolbar?") );
237 }
238 else if ( control->GetId() == id )
239 {
240 // found
241 return control;
242 }
243 }
244 }
245
246 return NULL;
247 }
248
249 wxToolBarToolBase *wxToolBarBase::AddSeparator()
250 {
251 return InsertSeparator(GetToolsCount());
252 }
253
254 wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
255 {
256 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
257 _T("invalid position in wxToolBar::InsertSeparator()") );
258
259 wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR,
260 wxEmptyString,
261 wxNullBitmap, wxNullBitmap,
262 wxITEM_SEPARATOR, (wxObject *)NULL,
263 wxEmptyString, wxEmptyString);
264
265 if ( !tool || !DoInsertTool(pos, tool) )
266 {
267 delete tool;
268
269 return NULL;
270 }
271
272 m_tools.Insert(pos, tool);
273
274 return tool;
275 }
276
277 wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
278 {
279 size_t pos = 0;
280 wxToolBarToolsList::compatibility_iterator node;
281 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
282 {
283 if ( node->GetData()->GetId() == id )
284 break;
285
286 pos++;
287 }
288
289 if ( !node )
290 {
291 // don't give any error messages - sometimes we might call RemoveTool()
292 // without knowing whether the tool is or not in the toolbar
293 return (wxToolBarToolBase *)NULL;
294 }
295
296 wxToolBarToolBase *tool = node->GetData();
297 if ( !DoDeleteTool(pos, tool) )
298 {
299 return (wxToolBarToolBase *)NULL;
300 }
301
302 m_tools.Erase(node);
303
304 return tool;
305 }
306
307 bool wxToolBarBase::DeleteToolByPos(size_t pos)
308 {
309 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
310 _T("invalid position in wxToolBar::DeleteToolByPos()") );
311
312 wxToolBarToolsList::compatibility_iterator node = m_tools.Item(pos);
313
314 if ( !DoDeleteTool(pos, node->GetData()) )
315 {
316 return FALSE;
317 }
318
319 delete node->GetData();
320 m_tools.Erase(node);
321
322 return TRUE;
323 }
324
325 bool wxToolBarBase::DeleteTool(int id)
326 {
327 size_t pos = 0;
328 wxToolBarToolsList::compatibility_iterator node;
329 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
330 {
331 if ( node->GetData()->GetId() == id )
332 break;
333
334 pos++;
335 }
336
337 if ( !node || !DoDeleteTool(pos, node->GetData()) )
338 {
339 return FALSE;
340 }
341
342 delete node->GetData();
343 m_tools.Erase(node);
344
345 return TRUE;
346 }
347
348 wxToolBarToolBase *wxToolBarBase::FindById(int id) const
349 {
350 wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL;
351
352 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
353 node;
354 node = node->GetNext() )
355 {
356 tool = node->GetData();
357 if ( tool->GetId() == id )
358 {
359 // found
360 break;
361 }
362
363 tool = NULL;
364 }
365
366 return tool;
367 }
368
369 void wxToolBarBase::ClearTools()
370 {
371 WX_CLEAR_LIST(wxToolBarToolsList, m_tools);
372 }
373
374 bool wxToolBarBase::Realize()
375 {
376 return TRUE;
377 }
378
379 wxToolBarBase::~wxToolBarBase()
380 {
381 WX_CLEAR_LIST(wxToolBarToolsList, m_tools);
382 }
383
384 // ----------------------------------------------------------------------------
385 // wxToolBarBase tools state
386 // ----------------------------------------------------------------------------
387
388 void wxToolBarBase::EnableTool(int id, bool enable)
389 {
390 wxToolBarToolBase *tool = FindById(id);
391 if ( tool )
392 {
393 if ( tool->Enable(enable) )
394 {
395 DoEnableTool(tool, enable);
396 }
397 }
398 }
399
400 void wxToolBarBase::ToggleTool(int id, bool toggle)
401 {
402 wxToolBarToolBase *tool = FindById(id);
403 if ( tool && tool->CanBeToggled() )
404 {
405 if ( tool->Toggle(toggle) )
406 {
407 DoToggleTool(tool, toggle);
408 }
409 }
410 }
411
412 void wxToolBarBase::SetToggle(int id, bool toggle)
413 {
414 wxToolBarToolBase *tool = FindById(id);
415 if ( tool )
416 {
417 if ( tool->SetToggle(toggle) )
418 {
419 DoSetToggle(tool, toggle);
420 }
421 }
422 }
423
424 void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
425 {
426 wxToolBarToolBase *tool = FindById(id);
427 if ( tool )
428 {
429 (void)tool->SetShortHelp(help);
430 }
431 }
432
433 void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
434 {
435 wxToolBarToolBase *tool = FindById(id);
436 if ( tool )
437 {
438 (void)tool->SetLongHelp(help);
439 }
440 }
441
442 wxObject *wxToolBarBase::GetToolClientData(int id) const
443 {
444 wxToolBarToolBase *tool = FindById(id);
445
446 return tool ? tool->GetClientData() : (wxObject *)NULL;
447 }
448
449 void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
450 {
451 wxToolBarToolBase *tool = FindById(id);
452
453 wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") );
454
455 tool->SetClientData(clientData);
456 }
457
458 int wxToolBarBase::GetToolPos(int id) const
459 {
460 size_t pos = 0;
461 wxToolBarToolsList::compatibility_iterator node;
462
463 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
464 {
465 if ( node->GetData()->GetId() == id )
466 return pos;
467
468 pos++;
469 }
470
471 return wxNOT_FOUND;
472 }
473
474 bool wxToolBarBase::GetToolState(int id) const
475 {
476 wxToolBarToolBase *tool = FindById(id);
477 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
478
479 return tool->IsToggled();
480 }
481
482 bool wxToolBarBase::GetToolEnabled(int id) const
483 {
484 wxToolBarToolBase *tool = FindById(id);
485 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
486
487 return tool->IsEnabled();
488 }
489
490 wxString wxToolBarBase::GetToolShortHelp(int id) const
491 {
492 wxToolBarToolBase *tool = FindById(id);
493 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
494
495 return tool->GetShortHelp();
496 }
497
498 wxString wxToolBarBase::GetToolLongHelp(int id) const
499 {
500 wxToolBarToolBase *tool = FindById(id);
501 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
502
503 return tool->GetLongHelp();
504 }
505
506 // ----------------------------------------------------------------------------
507 // wxToolBarBase geometry
508 // ----------------------------------------------------------------------------
509
510 void wxToolBarBase::SetMargins(int x, int y)
511 {
512 m_xMargin = x;
513 m_yMargin = y;
514 }
515
516 void wxToolBarBase::SetRows(int WXUNUSED(nRows))
517 {
518 // nothing
519 }
520
521 // ----------------------------------------------------------------------------
522 // event processing
523 // ----------------------------------------------------------------------------
524
525 // Only allow toggle if returns TRUE
526 bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
527 {
528 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
529 event.SetEventObject(this);
530
531 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
532 event.SetInt((int)toggleDown);
533
534 // and SetExtraLong() for backwards compatibility
535 event.SetExtraLong((long)toggleDown);
536
537 // Send events to this toolbar instead (and thence up the window hierarchy)
538 GetEventHandler()->ProcessEvent(event);
539
540 return TRUE;
541 }
542
543 // Call when right button down.
544 void wxToolBarBase::OnRightClick(int id,
545 long WXUNUSED(x),
546 long WXUNUSED(y))
547 {
548 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
549 event.SetEventObject(this);
550 event.SetInt(id);
551
552 GetEventHandler()->ProcessEvent(event);
553 }
554
555 // Called when the mouse cursor enters a tool bitmap (no button pressed).
556 // Argument is -1 if mouse is exiting the toolbar.
557 // Note that for this event, the id of the window is used,
558 // and the integer parameter of wxCommandEvent is used to retrieve
559 // the tool id.
560 void wxToolBarBase::OnMouseEnter(int id)
561 {
562 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
563 event.SetEventObject(this);
564 event.SetInt(id);
565
566 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
567 if( frame )
568 {
569 wxToolBarToolBase* tool = id == -1 ? (wxToolBarToolBase*)0 : FindById(id);
570 wxString help = tool ? tool->GetLongHelp() : wxString();
571 frame->DoGiveHelp( help, id != -1 );
572 }
573
574 (void)GetEventHandler()->ProcessEvent(event);
575 }
576
577 // ----------------------------------------------------------------------------
578 // UI updates
579 // ----------------------------------------------------------------------------
580
581 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
582 void wxToolBarBase::UpdateWindowUI(long flags)
583 {
584 wxWindowBase::UpdateWindowUI(flags);
585
586 wxEvtHandler* evtHandler = GetEventHandler() ;
587
588 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
589 node;
590 node = node->GetNext() )
591 {
592 int id = node->GetData()->GetId();
593
594 wxUpdateUIEvent event(id);
595 event.SetEventObject(this);
596
597 if ( evtHandler->ProcessEvent(event) )
598 {
599 if ( event.GetSetEnabled() )
600 EnableTool(id, event.GetEnabled());
601 if ( event.GetSetChecked() )
602 ToggleTool(id, event.GetChecked());
603 #if 0
604 if ( event.GetSetText() )
605 // Set tooltip?
606 #endif // 0
607 }
608 }
609 }
610
611 // Helper function, used by wxCreateGreyedImage
612
613 static void wxGreyOutImage( const wxImage& src,
614 wxImage& dest,
615 const wxColour& darkCol,
616 const wxColour& lightCol,
617 const wxColour& bgCol )
618 {
619 // Second attempt, just making things monochrome
620 int width = src.GetWidth();
621 int height = src.GetHeight();
622
623 int redCur, greenCur, blueCur;
624 for ( int x = 0; x < width; x++ )
625 {
626 for ( int y = 1; y < height; y++ )
627 {
628 redCur = src.GetRed(x, y);
629 greenCur = src.GetGreen(x, y);
630 blueCur = src.GetBlue(x, y);
631
632 // Change light things to the background colour
633 if ( redCur >= (lightCol.Red() - 50) && greenCur >= (lightCol.Green() - 50) && blueCur >= (lightCol.Blue() - 50) )
634 {
635 dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
636 }
637 else if ( redCur == bgCol.Red() && greenCur == bgCol.Green() && blueCur == bgCol.Blue() )
638 {
639 // Leave the background colour as-is
640 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
641 }
642 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
643 {
644 // Change dark things to really dark
645 dest.SetRGB(x,y, darkCol.Red(), darkCol.Green(), darkCol.Blue());
646 }
647 }
648 }
649 }
650
651 /*
652 * Make a greyed-out image suitable for disabled buttons.
653 * This code is adapted from wxNewBitmapButton in FL.
654 */
655
656 bool wxCreateGreyedImage(const wxImage& in, wxImage& out)
657 {
658 out = in.Copy();
659
660 // assuming the pixels along the edges are of the background color
661 wxColour bgCol(in.GetRed(0, 0), in.GetGreen(0, 0), in.GetBlue(0, 0));
662
663 wxColour darkCol = wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) ;
664 wxColour lightCol = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT) ;
665
666 wxGreyOutImage(in, out, darkCol, lightCol, bgCol);
667
668 return TRUE;
669 }
670
671 #endif // wxUSE_TOOLBAR