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