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()
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
,
160 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
161 event
.SetEventObject(this);
162 event
.SetInt(toolIndex
);
164 GetEventHandler()->ProcessEvent(event
);
167 // Called when the mouse cursor enters a tool bitmap (no button pressed).
168 // Argument is -1 if mouse is exiting the toolbar.
169 // Note that for this event, the id of the window is used,
170 // and the integer parameter of wxCommandEvent is used to retrieve
172 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
174 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
175 event
.SetEventObject(this);
176 event
.SetInt(toolIndex
);
178 GetEventHandler()->ProcessEvent(event
);
181 // If pushedBitmap is NULL, a reversed version of bitmap is
182 // created and used as the pushed/toggled image.
183 // If toggle is TRUE, the button toggles between the two states.
184 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
185 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
,
186 const wxString
& helpString1
, const wxString
& helpString2
)
189 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
190 (wxObject
*) NULL
, helpString1
, helpString2
);
192 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
194 tool
->m_clientData
= clientData
;
199 tool
->m_x
= m_xMargin
;
204 tool
->m_y
= m_yMargin
;
206 // Calculate reasonable max size in case Layout() not called
207 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
208 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
210 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
211 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
213 m_tools
.Append((long)index
, tool
);
217 void wxToolBarBase::AddSeparator ()
219 wxToolBarTool
*tool
= new wxToolBarTool
;
220 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
221 m_tools
.Append(-1, tool
);
224 void wxToolBarBase::ClearTools()
226 m_pressedTool
= m_currentTool
= -1;
227 wxNode
*node
= m_tools
.First();
230 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
231 wxNode
*nextNode
= node
->Next();
238 void wxToolBarBase::EnableTool(int index
, bool enable
)
240 wxNode
*node
= m_tools
.Find((long)index
);
243 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
245 tool
->m_enabled
= enable
;
249 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
250 bool WXUNUSED(toggle
))
254 void wxToolBarBase::SetToggle(int index
, bool value
)
256 wxNode
*node
=m_tools
.Find((long)index
);
258 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
259 tool
->m_isToggle
= value
;
263 bool wxToolBarBase::GetToolState(int index
) const
265 wxNode
*node
= m_tools
.Find((long)index
);
268 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
271 return tool
->m_toggleState
;
278 bool wxToolBarBase::GetToolEnabled(int index
) const
280 wxNode
*node
= m_tools
.Find((long)index
);
283 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
286 return tool
->m_enabled
;
293 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
295 wxNode
*node
= m_tools
.Find((long)index
);
298 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
301 return tool
->m_clientData
;
308 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
310 wxNode
*node
=m_tools
.Find((long)index
);
313 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
314 tool
->m_shortHelpString
= helpString
;
318 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
320 wxNode
*node
=m_tools
.Find((long)index
);
323 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
324 return tool
->m_shortHelpString
;
330 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
332 wxNode
*node
=m_tools
.Find((long)index
);
335 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
336 tool
->m_longHelpString
= helpString
;
340 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
342 wxNode
*node
=m_tools
.Find((long)index
);
345 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
346 return tool
->m_longHelpString
;
352 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
354 wxNode
*node
= m_tools
.First();
357 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
358 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
359 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
360 (y
<= (tool
->m_y
+ tool
->GetHeight())))
368 wxSize
wxToolBarBase::GetMaxSize ( void ) const
370 return wxSize(m_maxWidth
, m_maxHeight
);
373 // Okay, so we've left the tool we're in ... we must check if
374 // the tool we're leaving was a 'sprung push button' and if so,
375 // spring it back to the up state.
377 void wxToolBarBase::SetMargins(int x
, int y
)
383 void wxToolBarBase::SetToolPacking(int packing
)
385 m_toolPacking
= packing
;
388 void wxToolBarBase::SetToolSeparation(int separation
)
390 m_toolSeparation
= separation
;
393 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
397 void wxToolBarBase::Layout()
402 // SCROLLING IMPLEMENTATION
405 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
406 * noUnitsX/noUnitsY: : no. units per scrollbar
408 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
409 int noUnitsX
, int noUnitsY
,
412 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
413 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
414 m_xScrollLines
= noUnitsX
;
415 m_yScrollLines
= noUnitsY
;
420 // Recalculate scroll bar range and position
421 if (m_xScrollLines
> 0)
423 m_xScrollPosition
= xPos
;
424 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
428 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
429 m_xScrollPosition
= 0;
432 if (m_yScrollLines
> 0)
434 m_yScrollPosition
= yPos
;
435 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
439 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
440 m_yScrollPosition
= 0;
445 ::UpdateWindow ((HWND
) GetHWND());
450 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
452 int orient
= event
.GetOrientation();
454 int nScrollInc
= CalcScrollInc(event
);
458 if (orient
== wxHORIZONTAL
)
460 int newPos
= m_xScrollPosition
+ nScrollInc
;
461 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
465 int newPos
= m_yScrollPosition
+ nScrollInc
;
466 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
469 if (orient
== wxHORIZONTAL
)
471 if (m_xScrollingEnabled
)
472 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
478 if (m_yScrollingEnabled
)
479 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
484 if (orient
== wxHORIZONTAL
)
486 m_xScrollPosition
+= nScrollInc
;
490 m_yScrollPosition
+= nScrollInc
;
495 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
497 int pos
= event
.GetPosition();
498 int orient
= event
.GetOrientation();
501 switch (event
.GetEventType())
503 case wxEVT_SCROLL_TOP
:
505 if (orient
== wxHORIZONTAL
)
506 nScrollInc
= - m_xScrollPosition
;
508 nScrollInc
= - m_yScrollPosition
;
511 case wxEVT_SCROLL_BOTTOM
:
513 if (orient
== wxHORIZONTAL
)
514 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
516 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
519 case wxEVT_SCROLL_LINEUP
:
524 case wxEVT_SCROLL_LINEDOWN
:
529 case wxEVT_SCROLL_PAGEUP
:
531 if (orient
== wxHORIZONTAL
)
532 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
534 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
537 case wxEVT_SCROLL_PAGEDOWN
:
539 if (orient
== wxHORIZONTAL
)
540 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
542 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
545 case wxEVT_SCROLL_THUMBTRACK
:
547 if (orient
== wxHORIZONTAL
)
548 nScrollInc
= pos
- m_xScrollPosition
;
550 nScrollInc
= pos
- m_yScrollPosition
;
558 if (orient
== wxHORIZONTAL
)
561 GetClientSize(&w
, &h
);
563 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
564 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
568 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
569 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
570 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
571 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
578 GetClientSize(&w
, &h
);
580 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
581 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
585 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
586 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
587 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
588 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
594 // Adjust the scrollbars - new version.
595 void wxToolBarBase::AdjustScrollbars()
598 GetClientSize(&w
, &h
);
600 // Recalculate scroll bar range and position
601 if (m_xScrollLines
> 0)
603 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
604 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
608 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
610 // Calculate page size i.e. number of scroll units you get on the
611 // current client window
612 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
613 if (noPagePositions
< 1)
616 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
617 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
619 if (m_yScrollLines
> 0)
621 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
622 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
626 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
628 // Calculate page size i.e. number of scroll units you get on the
629 // current client window
630 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
631 if (noPagePositions
< 1)
634 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
635 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
639 // Default OnSize resets scrollbars, if any
640 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
642 #if wxUSE_CONSTRAINTS
650 // Prepare the DC by translating it according to the current scroll position
651 void wxToolBarBase::PrepareDC(wxDC
& dc
)
653 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
656 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
658 *x_unit
= m_xScrollPixelsPerLine
;
659 *y_unit
= m_yScrollPixelsPerLine
;
662 int wxToolBarBase::GetScrollPageSize(int orient
) const
664 if ( orient
== wxHORIZONTAL
)
665 return m_xScrollLinesPerPage
;
667 return m_yScrollLinesPerPage
;
670 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
672 if ( orient
== wxHORIZONTAL
)
673 m_xScrollLinesPerPage
= pageSize
;
675 m_yScrollLinesPerPage
= pageSize
;
679 * Scroll to given position (scroll position, not pixel position)
681 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
684 ViewStart (&old_x
, &old_y
);
685 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
690 m_xScrollPosition
= x_pos
;
691 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
695 m_yScrollPosition
= y_pos
;
696 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
700 UpdateWindow ((HWND
) GetHWND());
704 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
706 m_xScrollingEnabled
= x_scroll
;
707 m_yScrollingEnabled
= y_scroll
;
710 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
712 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
713 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
716 // Where the current view starts from
717 void wxToolBarBase::ViewStart (int *x
, int *y
) const
719 *x
= m_xScrollPosition
;
720 *y
= m_yScrollPosition
;
723 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
726 wxWindow::OnIdle(event
);
732 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
733 void wxToolBarBase::DoToolbarUpdates()
735 wxNode
* node
= GetTools().First();
738 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
740 wxUpdateUIEvent
event(tool
->m_index
);
741 event
.SetEventObject(this);
743 if (GetEventHandler()->ProcessEvent(event
))
745 if (event
.GetSetEnabled())
746 EnableTool(tool
->m_index
, event
.GetEnabled());
747 if (event
.GetSetChecked())
748 ToggleTool(tool
->m_index
, event
.GetChecked());
750 if (event.GetSetText())
760 // Circumvent wxControl::MSWOnMouseMove which doesn't set the cursor.
761 void wxToolBarBase::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
763 wxWindow::MSWOnMouseMove(x
, y
, flags
);