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"
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
)
93 // Comment this out if you don't want the disabled look,
94 // which currently acts weirdly for the scissors icon
95 // in the toolbar sample. See src/common/tbarbase.cpp
96 // for the wxCreateGreyedImage function.
98 // Created disabled-state bitmap on demand
99 if (!enable
&& !tool
->GetBitmap2().Ok())
101 wxImage
in(tool
->GetBitmap1());
102 wxImage
out(tool
->GetBitmap1());
104 wxCreateGreyedImage(in
, out
);
106 tool
->SetBitmap2(out
.ConvertToBitmap());
108 RefreshTool((wxToolBarTool
*) tool
);
112 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
114 wxToolBarTool
*my_tool
= (wxToolBarTool
*) tool
;
116 bool refresh
= (my_tool
->IsToggled() != toggle
);
118 my_tool
->m_isDown
= toggle
;
121 RefreshTool( my_tool
);
124 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
)
128 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
129 const wxBitmap
& bitmap1
,
130 const wxBitmap
& bitmap2
,
132 wxObject
*clientData
,
133 const wxString
& shortHelpString
,
134 const wxString
& longHelpString
)
136 return new wxToolBarTool( this, id
, bitmap1
, bitmap2
, toggle
,
137 clientData
, shortHelpString
, longHelpString
);
140 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
142 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
147 void wxToolBar::RefreshTool( wxToolBarTool
*tool
)
149 wxRect
rect( tool
->m_x
, tool
->m_y
, m_defaultWidth
+6, m_defaultHeight
+6 );
150 Refresh( TRUE
, &rect
);
153 void wxToolBar::DrawToolBarTool( wxToolBarTool
*tool
, wxDC
&dc
, bool down
)
155 const wxBitmap
& bitmap
= (tool
->IsEnabled() || !tool
->GetBitmap2().Ok()) ? tool
->GetBitmap1() : tool
->GetBitmap2() ;
158 dc
.DrawBitmap( bitmap
, tool
->m_x
+4, tool
->m_y
+4, TRUE
);
160 dc
.SetPen( *wxGREY_PEN
);
161 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
162 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
164 dc
.SetPen( *wxBLACK_PEN
);
165 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1 );
166 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4 );
168 dc
.SetPen( *wxWHITE_PEN
);
169 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
170 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
174 dc
.DrawBitmap( bitmap
, tool
->m_x
+3, tool
->m_y
+3, TRUE
);
176 dc
.SetPen( *wxWHITE_PEN
);
177 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
178 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
179 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+2 );
180 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+2, tool
->m_y
+m_defaultHeight
+4 );
182 dc
.SetPen( *wxBLACK_PEN
);
183 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
184 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
186 dc
.SetPen( *wxGREY_PEN
);
187 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+4 );
188 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+m_defaultHeight
+5 );
192 void wxToolBar::OnPaint(wxPaintEvent
&event
)
196 wxSize clientSize
= GetClientSize();
197 dc
.SetPen( *wxBLACK_PEN
);
198 dc
.DrawLine( 0,0, clientSize
.x
,0 );
200 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
202 node
= node
->GetNext() )
204 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
206 if (tool
->GetId() == -1) continue;
208 DrawToolBarTool( tool
, dc
, tool
->m_isDown
);
212 bool wxToolBar::Realize()
214 if (!wxToolBarBase::Realize())
220 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
231 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
233 node
= node
->GetNext() )
235 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
237 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
239 if (tool
->GetId() == -1)
244 tool
->m_x
= m_xMargin
;
246 y
+= m_defaultHeight
+ 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_maxWidth
< (m_defaultWidth
+ 2*(m_xMargin
+ 2)))
253 m_maxWidth
= (m_defaultWidth
+ 2*(m_xMargin
+ 2)) ;
257 if (tool
->GetId() == -1)
263 tool
->m_y
= m_yMargin
;
264 x
+= m_defaultWidth
+ 6;
266 // Calculate the maximum height or width (depending on style)
267 // so we know how to size the toolbar in Realize.
268 // We could get the size of the tool instead of the
269 // default bitmap size
270 if (m_maxHeight
< (m_defaultHeight
+ 2*(m_yMargin
+ 2)))
271 m_maxHeight
= (m_defaultHeight
+ 2*(m_yMargin
+ 2)) ;
276 wxSize sz
= GetSize();
277 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
279 SetSize(m_maxWidth
, sz
.y
);
283 SetSize(sz
.x
, m_maxHeight
);
286 // Commenting out Robert's fix since the above should be
288 // SetSize( x+16, m_defaultHeight + 14 );
293 void wxToolBar::OnMouse(wxMouseEvent
&event
)
295 wxToolBarTool
*hit
= NULL
;
296 int x
= event
.GetX();
297 int y
= event
.GetY();
299 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
301 node
= node
->GetNext() )
303 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
305 if ((x
> tool
->m_x
) && (x
< tool
->m_x
+m_defaultWidth
+5) &&
306 (y
> tool
->m_y
) && (y
< tool
->m_y
+m_defaultHeight
+5))
313 if (event
.LeftDown() && (hit
) && hit
->IsEnabled())
318 m_captured
->m_isDown
= TRUE
;
319 RefreshTool( m_captured
);
324 if (event
.Dragging() && (m_captured
))
326 bool is_down
= (hit
== m_captured
);
327 if (is_down
!= m_captured
->m_isDown
)
329 m_captured
->m_isDown
= is_down
;
330 RefreshTool( m_captured
);
336 if (event
.LeftUp() && (m_captured
))
340 m_captured
->m_isDown
= FALSE
;
341 RefreshTool( m_captured
);
343 if (hit
== m_captured
)
345 wxCommandEvent
cevent( wxEVT_COMMAND_TOOL_CLICKED
, m_captured
->GetId() );
346 cevent
.SetEventObject( this );
347 // cevent.SetExtraLong((long) toggleDown);
348 GetEventHandler()->ProcessEvent( cevent
);