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 // Send events to this toolbar instead (and thence up the window hierarchy)
153 GetEventHandler()->ProcessEvent(event
);
158 // Call when right button down.
159 void wxToolBarBase::OnRightClick(int toolIndex
,
163 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
164 event
.SetEventObject(this);
165 event
.SetInt(toolIndex
);
167 GetEventHandler()->ProcessEvent(event
);
170 // Called when the mouse cursor enters a tool bitmap (no button pressed).
171 // Argument is -1 if mouse is exiting the toolbar.
172 // Note that for this event, the id of the window is used,
173 // and the integer parameter of wxCommandEvent is used to retrieve
175 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
177 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
178 event
.SetEventObject(this);
179 event
.SetInt(toolIndex
);
181 GetEventHandler()->ProcessEvent(event
);
184 // If pushedBitmap is NULL, a reversed version of bitmap is
185 // created and used as the pushed/toggled image.
186 // If toggle is TRUE, the button toggles between the two states.
187 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
188 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
,
189 const wxString
& helpString1
, const wxString
& helpString2
)
192 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
193 (wxObject
*) NULL
, helpString1
, helpString2
);
195 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
197 tool
->m_clientData
= clientData
;
202 tool
->m_x
= m_xMargin
;
207 tool
->m_y
= m_yMargin
;
209 // Calculate reasonable max size in case Layout() not called
210 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
211 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
213 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
214 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
216 m_tools
.Append((long)index
, tool
);
220 void wxToolBarBase::AddSeparator ()
222 wxToolBarTool
*tool
= new wxToolBarTool
;
223 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
224 m_tools
.Append(-1, tool
);
227 void wxToolBarBase::ClearTools()
229 m_pressedTool
= m_currentTool
= -1;
230 wxNode
*node
= m_tools
.First();
233 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
234 wxNode
*nextNode
= node
->Next();
241 void wxToolBarBase::EnableTool(int index
, bool enable
)
243 wxNode
*node
= m_tools
.Find((long)index
);
246 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
248 tool
->m_enabled
= enable
;
252 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
253 bool WXUNUSED(toggle
))
257 void wxToolBarBase::SetToggle(int index
, bool value
)
259 wxNode
*node
=m_tools
.Find((long)index
);
261 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
262 tool
->m_isToggle
= value
;
266 bool wxToolBarBase::GetToolState(int index
) const
268 wxNode
*node
= m_tools
.Find((long)index
);
271 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
274 return tool
->m_toggleState
;
281 bool wxToolBarBase::GetToolEnabled(int index
) const
283 wxNode
*node
= m_tools
.Find((long)index
);
286 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
289 return tool
->m_enabled
;
296 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
298 wxNode
*node
= m_tools
.Find((long)index
);
301 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
304 return tool
->m_clientData
;
311 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
313 wxNode
*node
=m_tools
.Find((long)index
);
316 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
317 tool
->m_shortHelpString
= helpString
;
321 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
323 wxNode
*node
=m_tools
.Find((long)index
);
326 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
327 return tool
->m_shortHelpString
;
333 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
335 wxNode
*node
=m_tools
.Find((long)index
);
338 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
339 tool
->m_longHelpString
= helpString
;
343 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
345 wxNode
*node
=m_tools
.Find((long)index
);
348 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
349 return tool
->m_longHelpString
;
355 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
357 wxNode
*node
= m_tools
.First();
360 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
361 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
362 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
363 (y
<= (tool
->m_y
+ tool
->GetHeight())))
371 wxSize
wxToolBarBase::GetMaxSize ( void ) const
373 return wxSize(m_maxWidth
, m_maxHeight
);
376 // Okay, so we've left the tool we're in ... we must check if
377 // the tool we're leaving was a 'sprung push button' and if so,
378 // spring it back to the up state.
380 void wxToolBarBase::SetMargins(int x
, int y
)
386 void wxToolBarBase::SetToolPacking(int packing
)
388 m_toolPacking
= packing
;
391 void wxToolBarBase::SetToolSeparation(int separation
)
393 m_toolSeparation
= separation
;
396 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
400 void wxToolBarBase::Layout()
405 // SCROLLING IMPLEMENTATION
408 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
409 * noUnitsX/noUnitsY: : no. units per scrollbar
411 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
412 int noUnitsX
, int noUnitsY
,
415 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
416 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
417 m_xScrollLines
= noUnitsX
;
418 m_yScrollLines
= noUnitsY
;
423 // Recalculate scroll bar range and position
424 if (m_xScrollLines
> 0)
426 m_xScrollPosition
= xPos
;
427 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
431 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
432 m_xScrollPosition
= 0;
435 if (m_yScrollLines
> 0)
437 m_yScrollPosition
= yPos
;
438 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
442 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
443 m_yScrollPosition
= 0;
448 ::UpdateWindow ((HWND
) GetHWND());
453 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
455 int orient
= event
.GetOrientation();
457 int nScrollInc
= CalcScrollInc(event
);
461 if (orient
== wxHORIZONTAL
)
463 int newPos
= m_xScrollPosition
+ nScrollInc
;
464 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
468 int newPos
= m_yScrollPosition
+ nScrollInc
;
469 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
472 if (orient
== wxHORIZONTAL
)
474 if (m_xScrollingEnabled
)
475 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
481 if (m_yScrollingEnabled
)
482 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
487 if (orient
== wxHORIZONTAL
)
489 m_xScrollPosition
+= nScrollInc
;
493 m_yScrollPosition
+= nScrollInc
;
498 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
500 int pos
= event
.GetPosition();
501 int orient
= event
.GetOrientation();
504 switch (event
.GetEventType())
506 case wxEVT_SCROLL_TOP
:
508 if (orient
== wxHORIZONTAL
)
509 nScrollInc
= - m_xScrollPosition
;
511 nScrollInc
= - m_yScrollPosition
;
514 case wxEVT_SCROLL_BOTTOM
:
516 if (orient
== wxHORIZONTAL
)
517 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
519 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
522 case wxEVT_SCROLL_LINEUP
:
527 case wxEVT_SCROLL_LINEDOWN
:
532 case wxEVT_SCROLL_PAGEUP
:
534 if (orient
== wxHORIZONTAL
)
535 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
537 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
540 case wxEVT_SCROLL_PAGEDOWN
:
542 if (orient
== wxHORIZONTAL
)
543 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
545 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
548 case wxEVT_SCROLL_THUMBTRACK
:
550 if (orient
== wxHORIZONTAL
)
551 nScrollInc
= pos
- m_xScrollPosition
;
553 nScrollInc
= pos
- m_yScrollPosition
;
561 if (orient
== wxHORIZONTAL
)
564 GetClientSize(&w
, &h
);
566 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
567 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
571 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
572 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
573 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
574 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
581 GetClientSize(&w
, &h
);
583 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
584 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
588 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
589 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
590 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
591 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
597 // Adjust the scrollbars - new version.
598 void wxToolBarBase::AdjustScrollbars()
601 GetClientSize(&w
, &h
);
603 // Recalculate scroll bar range and position
604 if (m_xScrollLines
> 0)
606 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
607 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
611 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
613 // Calculate page size i.e. number of scroll units you get on the
614 // current client window
615 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
616 if (noPagePositions
< 1)
619 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
620 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
622 if (m_yScrollLines
> 0)
624 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
625 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
629 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
631 // Calculate page size i.e. number of scroll units you get on the
632 // current client window
633 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
634 if (noPagePositions
< 1)
637 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
638 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
642 // Default OnSize resets scrollbars, if any
643 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
645 #if wxUSE_CONSTRAINTS
653 // Prepare the DC by translating it according to the current scroll position
654 void wxToolBarBase::PrepareDC(wxDC
& dc
)
656 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
659 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
661 *x_unit
= m_xScrollPixelsPerLine
;
662 *y_unit
= m_yScrollPixelsPerLine
;
665 int wxToolBarBase::GetScrollPageSize(int orient
) const
667 if ( orient
== wxHORIZONTAL
)
668 return m_xScrollLinesPerPage
;
670 return m_yScrollLinesPerPage
;
673 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
675 if ( orient
== wxHORIZONTAL
)
676 m_xScrollLinesPerPage
= pageSize
;
678 m_yScrollLinesPerPage
= pageSize
;
682 * Scroll to given position (scroll position, not pixel position)
684 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
687 ViewStart (&old_x
, &old_y
);
688 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
693 m_xScrollPosition
= x_pos
;
694 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
698 m_yScrollPosition
= y_pos
;
699 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
703 UpdateWindow ((HWND
) GetHWND());
707 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
709 m_xScrollingEnabled
= x_scroll
;
710 m_yScrollingEnabled
= y_scroll
;
713 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
715 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
716 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
719 // Where the current view starts from
720 void wxToolBarBase::ViewStart (int *x
, int *y
) const
722 *x
= m_xScrollPosition
;
723 *y
= m_yScrollPosition
;
726 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
729 wxWindow::OnIdle(event
);
735 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
736 void wxToolBarBase::DoToolbarUpdates()
738 wxEvtHandler
* evtHandler
= GetEventHandler() ;
740 wxNode
* node
= GetTools().First();
743 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
745 wxUpdateUIEvent
event(tool
->m_index
);
746 event
.SetEventObject(this);
748 if (evtHandler
->ProcessEvent(event
))
750 if (event
.GetSetEnabled())
751 EnableTool(tool
->m_index
, event
.GetEnabled());
752 if (event
.GetSetChecked())
753 ToggleTool(tool
->m_index
, event
.GetChecked());
755 if (event.GetSetText())
765 // Circumvent wxControl::MSWOnMouseMove which doesn't set the cursor.
766 void wxToolBarBase::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
768 wxWindow::MSWOnMouseMove(x
, y
, flags
);