]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
corrected a bug in ConcatSelf() induced by the latest change
[wxWidgets.git] / src / common / tbarbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tbarbase.cpp
3 // Purpose: Toolbar base classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "tbarbase.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 // For ::UpdateWindow
28 #ifdef __WXMSW__
29 #include <windows.h>
30 #endif
31
32 #if USE_TOOLBAR
33
34 #include "wx/tbarbase.h"
35
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_ABSTRACT_CLASS(wxToolBarBase, wxControl)
38 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool, wxObject)
39
40 BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
41 EVT_SCROLL(wxToolBarBase::OnScroll)
42 EVT_SIZE(wxToolBarBase::OnSize)
43 EVT_IDLE(wxToolBarBase::OnIdle)
44 END_EVENT_TABLE()
45 #endif
46
47 // Keep a list of all toolbars created, so you can tell whether a toolbar
48 // is still valid: a tool may have quit the toolbar.
49 static wxList gs_ToolBars;
50
51 wxToolBarTool::wxToolBarTool(int theIndex,
52 const wxBitmap& theBitmap1, const wxBitmap& theBitmap2, bool toggle,
53 long xPos, long yPos, const wxString& helpS1, const wxString& helpS2)
54 {
55 m_toolStyle = wxTOOL_STYLE_BUTTON;
56 m_clientData = NULL;
57 m_index = theIndex;
58 m_isToggle = toggle;
59 m_toggleState = FALSE;
60 m_enabled = TRUE;
61 m_bitmap1 = theBitmap1;
62 m_bitmap2 = theBitmap2;
63 m_x = xPos;
64 m_y = yPos;
65 m_width = m_height = 0;
66 m_deleteSecondBitmap = FALSE;
67 if (m_bitmap1.Ok())
68 {
69 m_width = m_bitmap1.GetWidth()+2;
70 m_height = m_bitmap1.GetHeight()+2;
71 }
72 m_shortHelpString = helpS1;
73 m_longHelpString = helpS2;
74 }
75
76 wxToolBarTool::~wxToolBarTool(void)
77 {
78 /*
79 if (m_deleteSecondBitmap && m_bitmap2)
80 delete m_bitmap2;
81 */
82 }
83
84
85 // class wxToolBar
86
87 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER)
88 {
89 gs_ToolBars.Append(this);
90
91 m_maxRows = 1;
92 m_maxCols = 32000;
93 m_maxWidth = 0;
94 m_maxHeight = 0;
95 m_defaultWidth = 16;
96 m_defaultHeight = 15;
97 m_xMargin = 0;
98 m_yMargin = 0;
99 m_toolPacking = 1;
100 m_toolSeparation = 5;
101 m_currentTool = -1;
102
103 m_xScrollPixelsPerLine = 0;
104 m_yScrollPixelsPerLine = 0;
105 m_xScrollingEnabled = TRUE;
106 m_yScrollingEnabled = TRUE;
107 m_xScrollPosition = 0;
108 m_yScrollPosition = 0;
109 m_calcScrolledOffset = TRUE;
110 m_xScrollLines = 0;
111 m_yScrollLines = 0;
112 m_xScrollLinesPerPage = 0;
113 m_yScrollLinesPerPage = 0;
114 }
115
116 wxToolBarBase::~wxToolBarBase ()
117 {
118 gs_ToolBars.DeleteObject(this);
119
120 for ( wxNode *node = m_tools.First(); node; node = node->Next() )
121 {
122 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
123 delete tool;
124 }
125 }
126
127 // Only allow toggle if returns TRUE
128 bool wxToolBarBase::OnLeftClick(int toolIndex, bool toggleDown)
129 {
130 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, toolIndex);
131 event.SetEventObject(this);
132 event.SetExtraLong((long) toggleDown);
133
134 GetEventHandler()->ProcessEvent(event);
135
136 return TRUE;
137 }
138
139 // Call when right button down.
140 void wxToolBarBase::OnRightClick(int toolIndex, long x, long y)
141 {
142 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, toolIndex);
143 event.SetEventObject(this);
144 event.SetInt(toolIndex);
145
146 GetEventHandler()->ProcessEvent(event);
147 }
148
149 // Called when the mouse cursor enters a tool bitmap (no button pressed).
150 // Argument is -1 if mouse is exiting the toolbar.
151 // Note that for this event, the id of the window is used,
152 // and the integer parameter of wxCommandEvent is used to retrieve
153 // the tool id.
154 void wxToolBarBase::OnMouseEnter ( int toolIndex )
155 {
156 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
157 event.SetEventObject(this);
158 event.SetInt(toolIndex);
159
160 GetEventHandler()->ProcessEvent(event);
161 }
162
163 // If pushedBitmap is NULL, a reversed version of bitmap is
164 // created and used as the pushed/toggled image.
165 // If toggle is TRUE, the button toggles between the two states.
166 wxToolBarTool *wxToolBarBase::AddTool(int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap,
167 bool toggle, long xPos, long yPos, wxObject *clientData,
168 const wxString& helpString1, const wxString& helpString2)
169 {
170 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, pushedBitmap, toggle, xPos, yPos, helpString1, helpString2);
171 tool->m_clientData = clientData;
172
173 if (xPos > -1)
174 tool->m_x = xPos;
175 else
176 tool->m_x = m_xMargin;
177
178 if (yPos > -1)
179 tool->m_y = yPos;
180 else
181 tool->m_y = m_yMargin;
182
183 // Calculate reasonable max size in case Layout() not called
184 if ((tool->m_x + bitmap.GetWidth() + m_xMargin) > m_maxWidth)
185 m_maxWidth = (tool->m_x + bitmap.GetWidth() + m_xMargin);
186
187 if ((tool->m_y + bitmap.GetHeight() + m_yMargin) > m_maxHeight)
188 m_maxHeight = (tool->m_y + bitmap.GetHeight() + m_yMargin);
189
190 m_tools.Append((long)index, tool);
191 return tool;
192 }
193
194 void wxToolBarBase::AddSeparator ()
195 {
196 wxToolBarTool *tool = new wxToolBarTool;
197 tool->m_toolStyle = wxTOOL_STYLE_SEPARATOR;
198 m_tools.Append(tool);
199 }
200
201 void wxToolBarBase::ClearTools(void)
202 {
203 m_pressedTool = m_currentTool = -1;
204 wxNode *node = m_tools.First();
205 while (node)
206 {
207 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
208 wxNode *nextNode = node->Next();
209 delete tool;
210 delete node;
211 node = nextNode;
212 }
213 }
214
215 void wxToolBarBase::EnableTool(int index, bool enable)
216 {
217 wxNode *node = m_tools.Find((long)index);
218 if (node)
219 {
220 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
221 if (tool)
222 tool->m_enabled = enable;
223 }
224 }
225
226 void wxToolBarBase::ToggleTool(int index, bool toggle)
227 {
228 }
229
230 void wxToolBarBase::SetToggle(int index, bool value)
231 {
232 wxNode *node=m_tools.Find((long)index);
233 if (node){
234 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
235 tool->m_isToggle = value;
236 }
237 }
238
239 bool wxToolBarBase::GetToolState(int index) const
240 {
241 wxNode *node = m_tools.Find((long)index);
242 if (node)
243 {
244 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
245 if (tool)
246 {
247 return tool->m_toggleState;
248 }
249 else return FALSE;
250 }
251 else return FALSE;
252 }
253
254 bool wxToolBarBase::GetToolEnabled(int index) const
255 {
256 wxNode *node = m_tools.Find((long)index);
257 if (node)
258 {
259 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
260 if (tool)
261 {
262 return tool->m_enabled;
263 }
264 else return FALSE;
265 }
266 else return FALSE;
267 }
268
269 wxObject *wxToolBarBase::GetToolClientData(int index) const
270 {
271 wxNode *node = m_tools.Find((long)index);
272 if (node)
273 {
274 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
275 if (tool)
276 {
277 return tool->m_clientData;
278 }
279 else return NULL;
280 }
281 else return NULL;
282 }
283
284 void wxToolBarBase::SetToolShortHelp(int index, const wxString& helpString)
285 {
286 wxNode *node=m_tools.Find((long)index);
287 if (node)
288 {
289 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
290 tool->m_shortHelpString = helpString;
291 }
292 }
293
294 wxString wxToolBarBase::GetToolShortHelp(int index) const
295 {
296 wxNode *node=m_tools.Find((long)index);
297 if (node)
298 {
299 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
300 return tool->m_shortHelpString;
301 }
302 else
303 return wxString("");
304 }
305
306 void wxToolBarBase::SetToolLongHelp(int index, const wxString& helpString)
307 {
308 wxNode *node=m_tools.Find((long)index);
309 if (node)
310 {
311 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
312 tool->m_longHelpString = helpString;
313 }
314 }
315
316 wxString wxToolBarBase::GetToolLongHelp(int index) const
317 {
318 wxNode *node=m_tools.Find((long)index);
319 if (node)
320 {
321 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
322 return tool->m_longHelpString;
323 }
324 else
325 return wxString("");
326 }
327
328 wxToolBarTool *wxToolBarBase::FindToolForPosition(long x, long y) const
329 {
330 wxNode *node = m_tools.First();
331 while (node)
332 {
333 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
334 if ((x >= tool->m_x) && (y >= tool->m_y) &&
335 (x <= (tool->m_x + tool->GetWidth())) &&
336 (y <= (tool->m_y + tool->GetHeight())))
337 return tool;
338
339 node = node->Next();
340 }
341 return NULL;
342 }
343
344 wxSize wxToolBarBase::GetMaxSize ( void ) const
345 {
346 return wxSize(m_maxWidth, m_maxHeight);
347 }
348
349 // Okay, so we've left the tool we're in ... we must check if
350 // the tool we're leaving was a 'sprung push button' and if so,
351 // spring it back to the up state.
352 //
353 void wxToolBarBase::SetMargins(int x, int y)
354 {
355 m_xMargin = x;
356 m_yMargin = y;
357 }
358
359 void wxToolBarBase::SetToolPacking(int packing)
360 {
361 m_toolPacking = packing;
362 }
363
364 void wxToolBarBase::SetToolSeparation(int separation)
365 {
366 m_toolSeparation = separation;
367 }
368
369 void wxToolBarBase::Command(wxCommandEvent& event)
370 {
371 }
372
373 void wxToolBarBase::Layout(void)
374 {
375 }
376
377
378 // SCROLLING IMPLEMENTATION
379
380 /*
381 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
382 * noUnitsX/noUnitsY: : no. units per scrollbar
383 */
384 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
385 int noUnitsX, int noUnitsY,
386 int xPos, int yPos)
387 {
388 m_xScrollPixelsPerLine = pixelsPerUnitX;
389 m_yScrollPixelsPerLine = pixelsPerUnitY;
390 m_xScrollLines = noUnitsX;
391 m_yScrollLines = noUnitsY;
392
393 int w, h;
394 GetSize(&w, &h);
395
396 // Recalculate scroll bar range and position
397 if (m_xScrollLines > 0)
398 {
399 m_xScrollPosition = xPos;
400 SetScrollPos (wxHORIZONTAL, m_xScrollPosition, TRUE);
401 }
402 else
403 {
404 SetScrollbar(wxHORIZONTAL, 0, 0, 0, FALSE);
405 m_xScrollPosition = 0;
406 }
407
408 if (m_yScrollLines > 0)
409 {
410 m_yScrollPosition = yPos;
411 SetScrollPos (wxVERTICAL, m_yScrollPosition, TRUE);
412 }
413 else
414 {
415 SetScrollbar(wxVERTICAL, 0, 0, 0, FALSE);
416 m_yScrollPosition = 0;
417 }
418 AdjustScrollbars();
419 Refresh();
420 #ifdef __WXMSW__
421 ::UpdateWindow ((HWND) GetHWND());
422 #endif
423 }
424
425
426 void wxToolBarBase::OnScroll(wxScrollEvent& event)
427 {
428 int orient = event.GetOrientation();
429
430 int nScrollInc = CalcScrollInc(event);
431 if (nScrollInc == 0)
432 return;
433
434 if (orient == wxHORIZONTAL)
435 {
436 int newPos = m_xScrollPosition + nScrollInc;
437 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
438 }
439 else
440 {
441 int newPos = m_yScrollPosition + nScrollInc;
442 SetScrollPos(wxVERTICAL, newPos, TRUE );
443 }
444
445 if (orient == wxHORIZONTAL)
446 {
447 if (m_xScrollingEnabled)
448 ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, NULL);
449 else
450 Refresh();
451 }
452 else
453 {
454 if (m_yScrollingEnabled)
455 ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, NULL);
456 else
457 Refresh();
458 }
459
460 if (orient == wxHORIZONTAL)
461 {
462 m_xScrollPosition += nScrollInc;
463 }
464 else
465 {
466 m_yScrollPosition += nScrollInc;
467 }
468
469 }
470
471 int wxToolBarBase::CalcScrollInc(wxScrollEvent& event)
472 {
473 int pos = event.GetPosition();
474 int orient = event.GetOrientation();
475
476 int nScrollInc = 0;
477 switch (event.GetEventType())
478 {
479 case wxEVT_SCROLL_TOP:
480 {
481 if (orient == wxHORIZONTAL)
482 nScrollInc = - m_xScrollPosition;
483 else
484 nScrollInc = - m_yScrollPosition;
485 break;
486 }
487 case wxEVT_SCROLL_BOTTOM:
488 {
489 if (orient == wxHORIZONTAL)
490 nScrollInc = m_xScrollLines - m_xScrollPosition;
491 else
492 nScrollInc = m_yScrollLines - m_yScrollPosition;
493 break;
494 }
495 case wxEVT_SCROLL_LINEUP:
496 {
497 nScrollInc = -1;
498 break;
499 }
500 case wxEVT_SCROLL_LINEDOWN:
501 {
502 nScrollInc = 1;
503 break;
504 }
505 case wxEVT_SCROLL_PAGEUP:
506 {
507 if (orient == wxHORIZONTAL)
508 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
509 else
510 nScrollInc = -GetScrollPageSize(wxVERTICAL);
511 break;
512 }
513 case wxEVT_SCROLL_PAGEDOWN:
514 {
515 if (orient == wxHORIZONTAL)
516 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
517 else
518 nScrollInc = GetScrollPageSize(wxVERTICAL);
519 break;
520 }
521 case wxEVT_SCROLL_THUMBTRACK:
522 {
523 if (orient == wxHORIZONTAL)
524 nScrollInc = pos - m_xScrollPosition;
525 else
526 nScrollInc = pos - m_yScrollPosition;
527 break;
528 }
529 default:
530 {
531 break;
532 }
533 }
534 if (orient == wxHORIZONTAL)
535 {
536 int w, h;
537 GetClientSize(&w, &h);
538
539 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
540 int noPositions = (int) ( ((nMaxWidth - w)/(float)m_xScrollPixelsPerLine) + 0.5 );
541 if (noPositions < 0)
542 noPositions = 0;
543
544 if ( (m_xScrollPosition + nScrollInc) < 0 )
545 nScrollInc = -m_xScrollPosition; // As -ve as we can go
546 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
547 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
548
549 return nScrollInc;
550 }
551 else
552 {
553 int w, h;
554 GetClientSize(&w, &h);
555
556 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
557 int noPositions = (int) ( ((nMaxHeight - h)/(float)m_yScrollPixelsPerLine) + 0.5 );
558 if (noPositions < 0)
559 noPositions = 0;
560
561 if ( (m_yScrollPosition + nScrollInc) < 0 )
562 nScrollInc = -m_yScrollPosition; // As -ve as we can go
563 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
564 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
565
566 return nScrollInc;
567 }
568 }
569
570 // Adjust the scrollbars - new version.
571 void wxToolBarBase::AdjustScrollbars(void)
572 {
573 int w, h;
574 GetClientSize(&w, &h);
575
576 // Recalculate scroll bar range and position
577 if (m_xScrollLines > 0)
578 {
579 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
580 int newRange = (int) ( ((nMaxWidth)/(float)m_xScrollPixelsPerLine) + 0.5 );
581 if (newRange < 0)
582 newRange = 0;
583
584 m_xScrollPosition = wxMin(newRange, m_xScrollPosition);
585
586 // Calculate page size i.e. number of scroll units you get on the
587 // current client window
588 int noPagePositions = (int) ( (w/(float)m_xScrollPixelsPerLine) + 0.5 );
589 if (noPagePositions < 1)
590 noPagePositions = 1;
591
592 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, newRange);
593 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
594 }
595 if (m_yScrollLines > 0)
596 {
597 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
598 int newRange = (int) ( ((nMaxHeight)/(float)m_yScrollPixelsPerLine) + 0.5 );
599 if (newRange < 0)
600 newRange = 0;
601
602 m_yScrollPosition = wxMin(newRange, m_yScrollPosition);
603
604 // Calculate page size i.e. number of scroll units you get on the
605 // current client window
606 int noPagePositions = (int) ( (h/(float)m_yScrollPixelsPerLine) + 0.5 );
607 if (noPagePositions < 1)
608 noPagePositions = 1;
609
610 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, newRange);
611 SetScrollPageSize(wxVERTICAL, noPagePositions);
612 }
613 }
614
615 // Default OnSize resets scrollbars, if any
616 void wxToolBarBase::OnSize(wxSizeEvent& event)
617 {
618 #if USE_CONSTRAINTS
619 if (GetAutoLayout())
620 Layout();
621 #endif
622
623 AdjustScrollbars();
624 }
625
626 // Prepare the DC by translating it according to the current scroll position
627 void wxToolBarBase::PrepareDC(wxDC& dc)
628 {
629 dc.SetDeviceOrigin(- m_xScrollPosition * m_xScrollPixelsPerLine, - m_yScrollPosition * m_yScrollPixelsPerLine);
630 }
631
632 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
633 {
634 *x_unit = m_xScrollPixelsPerLine;
635 *y_unit = m_yScrollPixelsPerLine;
636 }
637
638 int wxToolBarBase::GetScrollPageSize(int orient) const
639 {
640 if ( orient == wxHORIZONTAL )
641 return m_xScrollLinesPerPage;
642 else
643 return m_yScrollLinesPerPage;
644 }
645
646 void wxToolBarBase::SetScrollPageSize(int orient, int pageSize)
647 {
648 if ( orient == wxHORIZONTAL )
649 m_xScrollLinesPerPage = pageSize;
650 else
651 m_yScrollLinesPerPage = pageSize;
652 }
653
654 /*
655 * Scroll to given position (scroll position, not pixel position)
656 */
657 void wxToolBarBase::Scroll (int x_pos, int y_pos)
658 {
659 int old_x, old_y;
660 ViewStart (&old_x, &old_y);
661 if (((x_pos == -1) || (x_pos == old_x)) && ((y_pos == -1) || (y_pos == old_y)))
662 return;
663
664 if (x_pos > -1)
665 {
666 m_xScrollPosition = x_pos;
667 SetScrollPos (wxHORIZONTAL, x_pos, TRUE);
668 }
669 if (y_pos > -1)
670 {
671 m_yScrollPosition = y_pos;
672 SetScrollPos (wxVERTICAL, y_pos, TRUE);
673 }
674 Refresh();
675 #ifdef __WXMSW__
676 UpdateWindow ((HWND) GetHWND());
677 #endif
678 }
679
680 void wxToolBarBase::EnableScrolling (bool x_scroll, bool y_scroll)
681 {
682 m_xScrollingEnabled = x_scroll;
683 m_yScrollingEnabled = y_scroll;
684 }
685
686 void wxToolBarBase::GetVirtualSize (int *x, int *y) const
687 {
688 *x = m_xScrollPixelsPerLine * m_xScrollLines;
689 *y = m_yScrollPixelsPerLine * m_yScrollLines;
690 }
691
692 // Where the current view starts from
693 void wxToolBarBase::ViewStart (int *x, int *y) const
694 {
695 *x = m_xScrollPosition;
696 *y = m_yScrollPosition;
697 }
698
699 void wxToolBarBase::OnIdle(wxIdleEvent& event)
700 {
701 wxWindow::OnIdle(event);
702
703 DoToolbarUpdates();
704 }
705
706 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
707 void wxToolBarBase::DoToolbarUpdates(void)
708 {
709 wxNode* node = GetTools().First();
710 while (node)
711 {
712 wxToolBarTool* tool = (wxToolBarTool* ) node->Data();
713
714 wxUpdateUIEvent event(tool->m_index);
715 event.SetEventObject(this);
716
717 if (GetEventHandler()->ProcessEvent(event))
718 {
719 if (event.GetSetEnabled())
720 EnableTool(tool->m_index, event.GetEnabled());
721 if (event.GetSetChecked())
722 ToggleTool(tool->m_index, event.GetChecked());
723 /*
724 if (event.GetSetText())
725 // Set tooltip?
726 */
727 }
728
729 node = node->Next();
730 }
731 }
732
733 #ifdef __WXMSW__
734 // Circumvent wxControl::MSWOnMouseMove which doesn't set the cursor.
735 void wxToolBarBase::MSWOnMouseMove(int x, int y, WXUINT flags)
736 {
737 wxWindow::MSWOnMouseMove(x, y, flags);
738 }
739 #endif
740
741 #endif