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