1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/toolbar.cpp
3 // Author: Robert Roebling
5 // Copyright: (c) 2001 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
18 #pragma implementation "univtoolbar.h"
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
33 #include "wx/toolbar.h"
34 #include "wx/validate.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 BEGIN_EVENT_TABLE(wxToolBar
,wxToolBarBase
)
41 EVT_MOUSE_EVENTS( wxToolBar::OnMouse
)
42 EVT_PAINT( wxToolBar::OnPaint
)
43 EVT_SIZE( wxToolBar::OnSize
)
46 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
47 const wxPoint
& pos
, const wxSize
& size
,
48 long style
, const wxString
& name
)
50 bool ret
= wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
55 void wxToolBar::Init()
57 // TODO: this is from tbarbase.cpp, but should be in
58 // wxToolbarBase::Init
59 // the list owns the pointers
60 m_tools
.DeleteContents(TRUE
);
61 m_xMargin
= m_yMargin
= 0;
62 m_maxRows
= m_maxCols
= 0;
69 SetToolBitmapSize( wxSize(16,15) );
72 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
77 void wxToolBar::SetToolShortHelp(int id
, const wxString
& helpString
)
81 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
)
86 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
91 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
95 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
97 wxToolBarTool
*my_tool
= (wxToolBarTool
*) tool
;
99 bool refresh
= (my_tool
->IsToggled() != toggle
);
101 my_tool
->m_isDown
= toggle
;
104 RefreshTool( my_tool
);
107 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
)
111 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
112 const wxBitmap
& bitmap1
,
113 const wxBitmap
& bitmap2
,
115 wxObject
*clientData
,
116 const wxString
& shortHelpString
,
117 const wxString
& longHelpString
)
119 return new wxToolBarTool( this, id
, bitmap1
, bitmap2
, toggle
,
120 clientData
, shortHelpString
, longHelpString
);
123 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
125 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
130 void wxToolBar::RefreshTool( wxToolBarTool
*tool
)
132 wxRect
rect( tool
->m_x
, tool
->m_y
, m_defaultWidth
+6, m_defaultHeight
+6 );
133 Refresh( TRUE
, &rect
);
136 void wxToolBar::DrawToolBarTool( wxToolBarTool
*tool
, wxDC
&dc
, bool down
)
140 dc
.DrawBitmap( tool
->GetBitmap1(), tool
->m_x
+4, tool
->m_y
+4, TRUE
);
142 dc
.SetPen( *wxGREY_PEN
);
143 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
144 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
146 dc
.SetPen( *wxBLACK_PEN
);
147 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1 );
148 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4 );
150 dc
.SetPen( *wxWHITE_PEN
);
151 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
152 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
156 dc
.DrawBitmap( tool
->GetBitmap1(), tool
->m_x
+3, tool
->m_y
+3, TRUE
);
158 dc
.SetPen( *wxWHITE_PEN
);
159 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
160 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
161 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+2 );
162 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+2, tool
->m_y
+m_defaultHeight
+4 );
164 dc
.SetPen( *wxBLACK_PEN
);
165 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
166 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
168 dc
.SetPen( *wxGREY_PEN
);
169 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+4 );
170 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+m_defaultHeight
+5 );
174 void wxToolBar::OnPaint(wxPaintEvent
&event
)
178 wxSize clientSize
= GetClientSize();
179 dc
.SetPen( *wxBLACK_PEN
);
180 dc
.DrawLine( 0,0, clientSize
.x
,0 );
182 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
184 node
= node
->GetNext() )
186 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
188 if (tool
->GetId() == -1) continue;
190 DrawToolBarTool( tool
, dc
, tool
->m_isDown
);
194 bool wxToolBar::Realize()
196 if (!wxToolBarBase::Realize())
202 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
213 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
215 node
= node
->GetNext() )
217 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
219 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
221 if (tool
->GetId() == -1)
226 tool
->m_x
= m_xMargin
;
228 y
+= m_defaultHeight
+ 6;
230 // Calculate the maximum height or width (depending on style)
231 // so we know how to size the toolbar in Realize.
232 // We could get the size of the tool instead of the
233 // default bitmap size
234 if (m_maxWidth
< (m_defaultWidth
+ 2*(m_xMargin
+ 2)))
235 m_maxWidth
= (m_defaultWidth
+ 2*(m_xMargin
+ 2)) ;
239 if (tool
->GetId() == -1)
245 tool
->m_y
= m_yMargin
;
246 x
+= m_defaultWidth
+ 6;
248 // Calculate the maximum height or width (depending on style)
249 // so we know how to size the toolbar in Realize.
250 // We could get the size of the tool instead of the
251 // default bitmap size
252 if (m_maxHeight
< (m_defaultHeight
+ 2*(m_yMargin
+ 2)))
253 m_maxHeight
= (m_defaultHeight
+ 2*(m_yMargin
+ 2)) ;
258 wxSize sz
= GetSize();
259 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
261 SetSize(m_maxWidth
, sz
.y
);
265 SetSize(sz
.x
, m_maxHeight
);
268 SetSize( x
+16, m_defaultHeight
+ 14 );
273 void wxToolBar::OnMouse(wxMouseEvent
&event
)
275 wxToolBarTool
*hit
= NULL
;
276 int x
= event
.GetX();
277 int y
= event
.GetY();
279 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
281 node
= node
->GetNext() )
283 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
285 if ((x
> tool
->m_x
) && (x
< tool
->m_x
+m_defaultWidth
+5) &&
286 (y
> tool
->m_y
) && (y
< tool
->m_y
+m_defaultHeight
+5))
293 if (event
.LeftDown() && (hit
))
298 m_captured
->m_isDown
= TRUE
;
299 RefreshTool( m_captured
);
304 if (event
.Dragging() && (m_captured
))
306 bool is_down
= (hit
== m_captured
);
307 if (is_down
!= m_captured
->m_isDown
)
309 m_captured
->m_isDown
= is_down
;
310 RefreshTool( m_captured
);
316 if (event
.LeftUp() && (m_captured
))
320 m_captured
->m_isDown
= FALSE
;
321 RefreshTool( m_captured
);
323 if (hit
== m_captured
)
325 wxCommandEvent
cevent( wxEVT_COMMAND_TOOL_CLICKED
, m_captured
->GetId() );
326 cevent
.SetEventObject( this );
327 // cevent.SetExtraLong((long) toggleDown);
328 GetEventHandler()->ProcessEvent( cevent
);