1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Toolbar base classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbarbase.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
36 #include "wx/tbarbase.h"
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_ABSTRACT_CLASS(wxToolBarBase
, wxControl
)
40 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
, wxObject
)
42 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
43 EVT_SCROLL(wxToolBarBase::OnScroll
)
44 EVT_SIZE(wxToolBarBase::OnSize
)
45 EVT_IDLE(wxToolBarBase::OnIdle
)
49 // Keep a list of all toolbars created, so you can tell whether a toolbar
50 // is still valid: a tool may have quit the toolbar.
51 static wxList gs_ToolBars
;
54 wxToolBarTool::wxToolBarTool(wxToolBar
*owner
, int theIndex
,
55 const wxBitmap
& theBitmap1
, const wxBitmap
& theBitmap2
,
56 bool toggle
, wxObject
*clientData
,
57 const wxString
& helpS1
, const wxString
& helpS2
,
60 wxToolBarTool::wxToolBarTool(int theIndex
,
61 const wxBitmap
& theBitmap1
, const wxBitmap
& theBitmap2
, bool toggle
,
62 long xPos
, long yPos
, const wxString
& helpS1
, const wxString
& helpS2
)
65 m_toolStyle
= wxTOOL_STYLE_BUTTON
;
69 m_clientData
= clientData
;
79 m_toggleState
= FALSE
;
81 m_bitmap1
= theBitmap1
;
82 m_bitmap2
= theBitmap2
;
83 m_width
= m_height
= 0;
84 m_deleteSecondBitmap
= FALSE
;
87 m_width
= m_bitmap1
.GetWidth()+2;
88 m_height
= m_bitmap1
.GetHeight()+2;
90 m_shortHelpString
= helpS1
;
91 m_longHelpString
= helpS2
;
94 wxToolBarTool::~wxToolBarTool()
97 if (m_deleteSecondBitmap && m_bitmap2)
105 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER
)
107 gs_ToolBars
.Append(this);
114 m_defaultHeight
= 15;
118 m_toolSeparation
= 5;
121 m_xScrollPixelsPerLine
= 0;
122 m_yScrollPixelsPerLine
= 0;
123 m_xScrollingEnabled
= TRUE
;
124 m_yScrollingEnabled
= TRUE
;
125 m_xScrollPosition
= 0;
126 m_yScrollPosition
= 0;
127 m_calcScrolledOffset
= TRUE
;
130 m_xScrollLinesPerPage
= 0;
131 m_yScrollLinesPerPage
= 0;
134 wxToolBarBase::~wxToolBarBase ()
136 gs_ToolBars
.DeleteObject(this);
138 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
140 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
145 // Only allow toggle if returns TRUE
146 bool wxToolBarBase::OnLeftClick(int toolIndex
, bool toggleDown
)
148 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
149 event
.SetEventObject(this);
150 event
.SetExtraLong((long) toggleDown
);
152 // First try sending the command to a window that has the focus, within a frame that
153 // also contains this toolbar.
154 wxFrame
* frame
= (wxFrame
*) NULL
;
155 wxWindow
* win
= this;
156 wxWindow
* focusWin
= (wxWindow
*) NULL
;
160 if (win
->IsKindOf(CLASSINFO(wxFrame
)))
162 frame
= (wxFrame
*) win
;
166 win
= win
->GetParent();
169 focusWin
= wxFindFocusDescendant(frame
);
171 if (focusWin
&& focusWin
->GetEventHandler()->ProcessEvent(event
))
174 // Send events to this toolbar instead (and thence up the window hierarchy)
175 GetEventHandler()->ProcessEvent(event
);
180 // Call when right button down.
181 void wxToolBarBase::OnRightClick(int toolIndex
,
185 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
186 event
.SetEventObject(this);
187 event
.SetInt(toolIndex
);
189 GetEventHandler()->ProcessEvent(event
);
192 // Called when the mouse cursor enters a tool bitmap (no button pressed).
193 // Argument is -1 if mouse is exiting the toolbar.
194 // Note that for this event, the id of the window is used,
195 // and the integer parameter of wxCommandEvent is used to retrieve
197 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
199 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
200 event
.SetEventObject(this);
201 event
.SetInt(toolIndex
);
203 GetEventHandler()->ProcessEvent(event
);
206 // If pushedBitmap is NULL, a reversed version of bitmap is
207 // created and used as the pushed/toggled image.
208 // If toggle is TRUE, the button toggles between the two states.
209 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
210 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
,
211 const wxString
& helpString1
, const wxString
& helpString2
)
214 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
215 (wxObject
*) NULL
, helpString1
, helpString2
);
217 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
219 tool
->m_clientData
= clientData
;
224 tool
->m_x
= m_xMargin
;
229 tool
->m_y
= m_yMargin
;
231 // Calculate reasonable max size in case Layout() not called
232 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
233 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
235 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
236 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
238 m_tools
.Append((long)index
, tool
);
242 void wxToolBarBase::AddSeparator ()
244 wxToolBarTool
*tool
= new wxToolBarTool
;
245 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
246 m_tools
.Append(-1, tool
);
249 void wxToolBarBase::ClearTools()
251 m_pressedTool
= m_currentTool
= -1;
252 wxNode
*node
= m_tools
.First();
255 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
256 wxNode
*nextNode
= node
->Next();
263 void wxToolBarBase::EnableTool(int index
, bool enable
)
265 wxNode
*node
= m_tools
.Find((long)index
);
268 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
270 tool
->m_enabled
= enable
;
274 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
275 bool WXUNUSED(toggle
))
279 void wxToolBarBase::SetToggle(int index
, bool value
)
281 wxNode
*node
=m_tools
.Find((long)index
);
283 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
284 tool
->m_isToggle
= value
;
288 bool wxToolBarBase::GetToolState(int index
) const
290 wxNode
*node
= m_tools
.Find((long)index
);
293 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
296 return tool
->m_toggleState
;
303 bool wxToolBarBase::GetToolEnabled(int index
) const
305 wxNode
*node
= m_tools
.Find((long)index
);
308 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
311 return tool
->m_enabled
;
318 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
320 wxNode
*node
= m_tools
.Find((long)index
);
323 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
326 return tool
->m_clientData
;
333 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
335 wxNode
*node
=m_tools
.Find((long)index
);
338 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
339 tool
->m_shortHelpString
= helpString
;
343 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
345 wxNode
*node
=m_tools
.Find((long)index
);
348 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
349 return tool
->m_shortHelpString
;
355 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
357 wxNode
*node
=m_tools
.Find((long)index
);
360 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
361 tool
->m_longHelpString
= helpString
;
365 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
367 wxNode
*node
=m_tools
.Find((long)index
);
370 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
371 return tool
->m_longHelpString
;
377 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
379 wxNode
*node
= m_tools
.First();
382 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
383 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
384 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
385 (y
<= (tool
->m_y
+ tool
->GetHeight())))
393 wxSize
wxToolBarBase::GetMaxSize ( void ) const
395 return wxSize(m_maxWidth
, m_maxHeight
);
398 // Okay, so we've left the tool we're in ... we must check if
399 // the tool we're leaving was a 'sprung push button' and if so,
400 // spring it back to the up state.
402 void wxToolBarBase::SetMargins(int x
, int y
)
408 void wxToolBarBase::SetToolPacking(int packing
)
410 m_toolPacking
= packing
;
413 void wxToolBarBase::SetToolSeparation(int separation
)
415 m_toolSeparation
= separation
;
418 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
422 void wxToolBarBase::Layout()
427 // SCROLLING IMPLEMENTATION
430 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
431 * noUnitsX/noUnitsY: : no. units per scrollbar
433 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
434 int noUnitsX
, int noUnitsY
,
437 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
438 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
439 m_xScrollLines
= noUnitsX
;
440 m_yScrollLines
= noUnitsY
;
445 // Recalculate scroll bar range and position
446 if (m_xScrollLines
> 0)
448 m_xScrollPosition
= xPos
;
449 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
453 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
454 m_xScrollPosition
= 0;
457 if (m_yScrollLines
> 0)
459 m_yScrollPosition
= yPos
;
460 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
464 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
465 m_yScrollPosition
= 0;
470 ::UpdateWindow ((HWND
) GetHWND());
475 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
477 int orient
= event
.GetOrientation();
479 int nScrollInc
= CalcScrollInc(event
);
483 if (orient
== wxHORIZONTAL
)
485 int newPos
= m_xScrollPosition
+ nScrollInc
;
486 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
490 int newPos
= m_yScrollPosition
+ nScrollInc
;
491 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
494 if (orient
== wxHORIZONTAL
)
496 if (m_xScrollingEnabled
)
497 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
503 if (m_yScrollingEnabled
)
504 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
509 if (orient
== wxHORIZONTAL
)
511 m_xScrollPosition
+= nScrollInc
;
515 m_yScrollPosition
+= nScrollInc
;
520 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
522 int pos
= event
.GetPosition();
523 int orient
= event
.GetOrientation();
526 switch (event
.GetEventType())
528 case wxEVT_SCROLL_TOP
:
530 if (orient
== wxHORIZONTAL
)
531 nScrollInc
= - m_xScrollPosition
;
533 nScrollInc
= - m_yScrollPosition
;
536 case wxEVT_SCROLL_BOTTOM
:
538 if (orient
== wxHORIZONTAL
)
539 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
541 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
544 case wxEVT_SCROLL_LINEUP
:
549 case wxEVT_SCROLL_LINEDOWN
:
554 case wxEVT_SCROLL_PAGEUP
:
556 if (orient
== wxHORIZONTAL
)
557 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
559 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
562 case wxEVT_SCROLL_PAGEDOWN
:
564 if (orient
== wxHORIZONTAL
)
565 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
567 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
570 case wxEVT_SCROLL_THUMBTRACK
:
572 if (orient
== wxHORIZONTAL
)
573 nScrollInc
= pos
- m_xScrollPosition
;
575 nScrollInc
= pos
- m_yScrollPosition
;
583 if (orient
== wxHORIZONTAL
)
586 GetClientSize(&w
, &h
);
588 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
589 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
593 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
594 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
595 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
596 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
603 GetClientSize(&w
, &h
);
605 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
606 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
610 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
611 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
612 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
613 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
619 // Adjust the scrollbars - new version.
620 void wxToolBarBase::AdjustScrollbars()
623 GetClientSize(&w
, &h
);
625 // Recalculate scroll bar range and position
626 if (m_xScrollLines
> 0)
628 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
629 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
633 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
635 // Calculate page size i.e. number of scroll units you get on the
636 // current client window
637 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
638 if (noPagePositions
< 1)
641 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
642 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
644 if (m_yScrollLines
> 0)
646 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
647 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
651 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
653 // Calculate page size i.e. number of scroll units you get on the
654 // current client window
655 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
656 if (noPagePositions
< 1)
659 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
660 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
664 // Default OnSize resets scrollbars, if any
665 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
667 #if wxUSE_CONSTRAINTS
675 // Prepare the DC by translating it according to the current scroll position
676 void wxToolBarBase::PrepareDC(wxDC
& dc
)
678 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
681 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
683 *x_unit
= m_xScrollPixelsPerLine
;
684 *y_unit
= m_yScrollPixelsPerLine
;
687 int wxToolBarBase::GetScrollPageSize(int orient
) const
689 if ( orient
== wxHORIZONTAL
)
690 return m_xScrollLinesPerPage
;
692 return m_yScrollLinesPerPage
;
695 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
697 if ( orient
== wxHORIZONTAL
)
698 m_xScrollLinesPerPage
= pageSize
;
700 m_yScrollLinesPerPage
= pageSize
;
704 * Scroll to given position (scroll position, not pixel position)
706 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
709 ViewStart (&old_x
, &old_y
);
710 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
715 m_xScrollPosition
= x_pos
;
716 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
720 m_yScrollPosition
= y_pos
;
721 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
725 UpdateWindow ((HWND
) GetHWND());
729 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
731 m_xScrollingEnabled
= x_scroll
;
732 m_yScrollingEnabled
= y_scroll
;
735 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
737 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
738 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
741 // Where the current view starts from
742 void wxToolBarBase::ViewStart (int *x
, int *y
) const
744 *x
= m_xScrollPosition
;
745 *y
= m_yScrollPosition
;
748 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
751 wxWindow::OnIdle(event
);
757 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
758 void wxToolBarBase::DoToolbarUpdates()
760 // First try sending the command to a window that has the focus, within a frame that
761 // also contains this toolbar.
762 wxFrame
* frame
= (wxFrame
*) NULL
;
763 wxWindow
* win
= this;
764 wxWindow
* focusWin
= (wxWindow
*) NULL
;
768 if (win
->IsKindOf(CLASSINFO(wxFrame
)))
770 frame
= (wxFrame
*) win
;
774 win
= win
->GetParent();
777 focusWin
= wxFindFocusDescendant(frame
);
780 wxEvtHandler
* evtHandler
= focusWin
? focusWin
->GetEventHandler() : GetEventHandler() ;
782 wxNode
* node
= GetTools().First();
785 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
787 wxUpdateUIEvent
event(tool
->m_index
);
788 event
.SetEventObject(this);
790 if (evtHandler
->ProcessEvent(event
))
792 if (event
.GetSetEnabled())
793 EnableTool(tool
->m_index
, event
.GetEnabled());
794 if (event
.GetSetChecked())
795 ToggleTool(tool
->m_index
, event
.GetChecked());
797 if (event.GetSetText())
807 // Circumvent wxControl::MSWOnMouseMove which doesn't set the cursor.
808 void wxToolBarBase::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
810 wxWindow::MSWOnMouseMove(x
, y
, flags
);