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"
34 #include "wx/tbarbase.h"
36 #if !USE_SHARED_LIBRARY
37 IMPLEMENT_ABSTRACT_CLASS(wxToolBarBase
, wxControl
)
38 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
, wxObject
)
40 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
41 EVT_SCROLL(wxToolBarBase::OnScroll
)
42 EVT_SIZE(wxToolBarBase::OnSize
)
43 EVT_IDLE(wxToolBarBase::OnIdle
)
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
;
52 wxToolBarTool::wxToolBarTool(wxToolBar
*owner
, int theIndex
,
53 const wxBitmap
& theBitmap1
, const wxBitmap
& theBitmap2
,
54 bool toggle
, wxObject
*clientData
,
55 const wxString
& helpS1
, const wxString
& helpS2
,
58 wxToolBarTool::wxToolBarTool(int theIndex
,
59 const wxBitmap
& theBitmap1
, const wxBitmap
& theBitmap2
, bool toggle
,
60 long xPos
, long yPos
, const wxString
& helpS1
, const wxString
& helpS2
)
63 m_toolStyle
= wxTOOL_STYLE_BUTTON
;
67 m_clientData
= clientData
;
77 m_toggleState
= FALSE
;
79 m_bitmap1
= theBitmap1
;
80 m_bitmap2
= theBitmap2
;
81 m_width
= m_height
= 0;
82 m_deleteSecondBitmap
= FALSE
;
85 m_width
= m_bitmap1
.GetWidth()+2;
86 m_height
= m_bitmap1
.GetHeight()+2;
88 m_shortHelpString
= helpS1
;
89 m_longHelpString
= helpS2
;
92 wxToolBarTool::~wxToolBarTool(void)
95 if (m_deleteSecondBitmap && m_bitmap2)
103 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER
)
105 gs_ToolBars
.Append(this);
112 m_defaultHeight
= 15;
116 m_toolSeparation
= 5;
119 m_xScrollPixelsPerLine
= 0;
120 m_yScrollPixelsPerLine
= 0;
121 m_xScrollingEnabled
= TRUE
;
122 m_yScrollingEnabled
= TRUE
;
123 m_xScrollPosition
= 0;
124 m_yScrollPosition
= 0;
125 m_calcScrolledOffset
= TRUE
;
128 m_xScrollLinesPerPage
= 0;
129 m_yScrollLinesPerPage
= 0;
132 wxToolBarBase::~wxToolBarBase ()
134 gs_ToolBars
.DeleteObject(this);
136 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
138 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
143 // Only allow toggle if returns TRUE
144 bool wxToolBarBase::OnLeftClick(int toolIndex
, bool toggleDown
)
146 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
147 event
.SetEventObject(this);
148 event
.SetExtraLong((long) toggleDown
);
150 GetEventHandler()->ProcessEvent(event
);
155 // Call when right button down.
156 void wxToolBarBase::OnRightClick(int toolIndex
, long x
, long y
)
158 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
159 event
.SetEventObject(this);
160 event
.SetInt(toolIndex
);
162 GetEventHandler()->ProcessEvent(event
);
165 // Called when the mouse cursor enters a tool bitmap (no button pressed).
166 // Argument is -1 if mouse is exiting the toolbar.
167 // Note that for this event, the id of the window is used,
168 // and the integer parameter of wxCommandEvent is used to retrieve
170 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
172 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
173 event
.SetEventObject(this);
174 event
.SetInt(toolIndex
);
176 GetEventHandler()->ProcessEvent(event
);
179 // If pushedBitmap is NULL, a reversed version of bitmap is
180 // created and used as the pushed/toggled image.
181 // If toggle is TRUE, the button toggles between the two states.
182 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
183 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
,
184 const wxString
& helpString1
, const wxString
& helpString2
)
187 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
188 (wxObject
*) NULL
, helpString1
, helpString2
);
190 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
192 tool
->m_clientData
= clientData
;
197 tool
->m_x
= m_xMargin
;
202 tool
->m_y
= m_yMargin
;
204 // Calculate reasonable max size in case Layout() not called
205 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
206 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
208 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
209 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
211 m_tools
.Append((long)index
, tool
);
215 void wxToolBarBase::AddSeparator ()
217 wxToolBarTool
*tool
= new wxToolBarTool
;
218 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
219 m_tools
.Append(-1, tool
);
222 void wxToolBarBase::ClearTools(void)
224 m_pressedTool
= m_currentTool
= -1;
225 wxNode
*node
= m_tools
.First();
228 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
229 wxNode
*nextNode
= node
->Next();
236 void wxToolBarBase::EnableTool(int index
, bool enable
)
238 wxNode
*node
= m_tools
.Find((long)index
);
241 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
243 tool
->m_enabled
= enable
;
247 void wxToolBarBase::ToggleTool(int index
, bool toggle
)
251 void wxToolBarBase::SetToggle(int index
, bool value
)
253 wxNode
*node
=m_tools
.Find((long)index
);
255 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
256 tool
->m_isToggle
= value
;
260 bool wxToolBarBase::GetToolState(int index
) const
262 wxNode
*node
= m_tools
.Find((long)index
);
265 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
268 return tool
->m_toggleState
;
275 bool wxToolBarBase::GetToolEnabled(int index
) const
277 wxNode
*node
= m_tools
.Find((long)index
);
280 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
283 return tool
->m_enabled
;
290 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
292 wxNode
*node
= m_tools
.Find((long)index
);
295 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
298 return tool
->m_clientData
;
305 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
307 wxNode
*node
=m_tools
.Find((long)index
);
310 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
311 tool
->m_shortHelpString
= helpString
;
315 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
317 wxNode
*node
=m_tools
.Find((long)index
);
320 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
321 return tool
->m_shortHelpString
;
327 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
329 wxNode
*node
=m_tools
.Find((long)index
);
332 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
333 tool
->m_longHelpString
= helpString
;
337 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
339 wxNode
*node
=m_tools
.Find((long)index
);
342 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
343 return tool
->m_longHelpString
;
349 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
351 wxNode
*node
= m_tools
.First();
354 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
355 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
356 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
357 (y
<= (tool
->m_y
+ tool
->GetHeight())))
365 wxSize
wxToolBarBase::GetMaxSize ( void ) const
367 return wxSize(m_maxWidth
, m_maxHeight
);
370 // Okay, so we've left the tool we're in ... we must check if
371 // the tool we're leaving was a 'sprung push button' and if so,
372 // spring it back to the up state.
374 void wxToolBarBase::SetMargins(int x
, int y
)
380 void wxToolBarBase::SetToolPacking(int packing
)
382 m_toolPacking
= packing
;
385 void wxToolBarBase::SetToolSeparation(int separation
)
387 m_toolSeparation
= separation
;
390 void wxToolBarBase::Command(wxCommandEvent
& event
)
394 void wxToolBarBase::Layout(void)
399 // SCROLLING IMPLEMENTATION
402 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
403 * noUnitsX/noUnitsY: : no. units per scrollbar
405 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
406 int noUnitsX
, int noUnitsY
,
409 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
410 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
411 m_xScrollLines
= noUnitsX
;
412 m_yScrollLines
= noUnitsY
;
417 // Recalculate scroll bar range and position
418 if (m_xScrollLines
> 0)
420 m_xScrollPosition
= xPos
;
421 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
425 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
426 m_xScrollPosition
= 0;
429 if (m_yScrollLines
> 0)
431 m_yScrollPosition
= yPos
;
432 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
436 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
437 m_yScrollPosition
= 0;
442 ::UpdateWindow ((HWND
) GetHWND());
447 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
449 int orient
= event
.GetOrientation();
451 int nScrollInc
= CalcScrollInc(event
);
455 if (orient
== wxHORIZONTAL
)
457 int newPos
= m_xScrollPosition
+ nScrollInc
;
458 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
462 int newPos
= m_yScrollPosition
+ nScrollInc
;
463 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
466 if (orient
== wxHORIZONTAL
)
468 if (m_xScrollingEnabled
)
469 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
475 if (m_yScrollingEnabled
)
476 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
481 if (orient
== wxHORIZONTAL
)
483 m_xScrollPosition
+= nScrollInc
;
487 m_yScrollPosition
+= nScrollInc
;
492 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
494 int pos
= event
.GetPosition();
495 int orient
= event
.GetOrientation();
498 switch (event
.GetEventType())
500 case wxEVT_SCROLL_TOP
:
502 if (orient
== wxHORIZONTAL
)
503 nScrollInc
= - m_xScrollPosition
;
505 nScrollInc
= - m_yScrollPosition
;
508 case wxEVT_SCROLL_BOTTOM
:
510 if (orient
== wxHORIZONTAL
)
511 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
513 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
516 case wxEVT_SCROLL_LINEUP
:
521 case wxEVT_SCROLL_LINEDOWN
:
526 case wxEVT_SCROLL_PAGEUP
:
528 if (orient
== wxHORIZONTAL
)
529 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
531 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
534 case wxEVT_SCROLL_PAGEDOWN
:
536 if (orient
== wxHORIZONTAL
)
537 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
539 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
542 case wxEVT_SCROLL_THUMBTRACK
:
544 if (orient
== wxHORIZONTAL
)
545 nScrollInc
= pos
- m_xScrollPosition
;
547 nScrollInc
= pos
- m_yScrollPosition
;
555 if (orient
== wxHORIZONTAL
)
558 GetClientSize(&w
, &h
);
560 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
561 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
565 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
566 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
567 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
568 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
575 GetClientSize(&w
, &h
);
577 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
578 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
582 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
583 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
584 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
585 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
591 // Adjust the scrollbars - new version.
592 void wxToolBarBase::AdjustScrollbars(void)
595 GetClientSize(&w
, &h
);
597 // Recalculate scroll bar range and position
598 if (m_xScrollLines
> 0)
600 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
601 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
605 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
607 // Calculate page size i.e. number of scroll units you get on the
608 // current client window
609 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
610 if (noPagePositions
< 1)
613 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
614 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
616 if (m_yScrollLines
> 0)
618 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
619 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
623 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
625 // Calculate page size i.e. number of scroll units you get on the
626 // current client window
627 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
628 if (noPagePositions
< 1)
631 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
632 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
636 // Default OnSize resets scrollbars, if any
637 void wxToolBarBase::OnSize(wxSizeEvent
& event
)
639 #if wxUSE_CONSTRAINTS
647 // Prepare the DC by translating it according to the current scroll position
648 void wxToolBarBase::PrepareDC(wxDC
& dc
)
650 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
653 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
655 *x_unit
= m_xScrollPixelsPerLine
;
656 *y_unit
= m_yScrollPixelsPerLine
;
659 int wxToolBarBase::GetScrollPageSize(int orient
) const
661 if ( orient
== wxHORIZONTAL
)
662 return m_xScrollLinesPerPage
;
664 return m_yScrollLinesPerPage
;
667 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
669 if ( orient
== wxHORIZONTAL
)
670 m_xScrollLinesPerPage
= pageSize
;
672 m_yScrollLinesPerPage
= pageSize
;
676 * Scroll to given position (scroll position, not pixel position)
678 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
681 ViewStart (&old_x
, &old_y
);
682 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
687 m_xScrollPosition
= x_pos
;
688 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
692 m_yScrollPosition
= y_pos
;
693 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
697 UpdateWindow ((HWND
) GetHWND());
701 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
703 m_xScrollingEnabled
= x_scroll
;
704 m_yScrollingEnabled
= y_scroll
;
707 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
709 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
710 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
713 // Where the current view starts from
714 void wxToolBarBase::ViewStart (int *x
, int *y
) const
716 *x
= m_xScrollPosition
;
717 *y
= m_yScrollPosition
;
720 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
723 wxWindow::OnIdle(event
);
729 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
730 void wxToolBarBase::DoToolbarUpdates(void)
732 wxNode
* node
= GetTools().First();
735 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
737 wxUpdateUIEvent
event(tool
->m_index
);
738 event
.SetEventObject(this);
740 if (GetEventHandler()->ProcessEvent(event
))
742 if (event
.GetSetEnabled())
743 EnableTool(tool
->m_index
, event
.GetEnabled());
744 if (event
.GetSetChecked())
745 ToggleTool(tool
->m_index
, event
.GetChecked());
747 if (event.GetSetText())
757 // Circumvent wxControl::MSWOnMouseMove which doesn't set the cursor.
758 void wxToolBarBase::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
760 wxWindow::MSWOnMouseMove(x
, y
, flags
);