1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/button.cpp
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "univbutton.h"
24 #include "wx/wxprec.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/button.h"
36 #include "wx/validate.h"
37 #include "wx/settings.h"
40 #include "wx/univ/renderer.h"
41 #include "wx/univ/inphand.h"
42 #include "wx/univ/theme.h"
43 #include "wx/univ/colschem.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // default margins around the image
50 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
51 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
53 // ============================================================================
55 // ============================================================================
57 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
69 bool wxButton::Create(wxWindow
*parent
,
71 const wxBitmap
& bitmap
,
72 const wxString
&label
,
76 const wxValidator
& validator
,
79 // center label by default
80 if ( !(style
& wxALIGN_MASK
) )
82 style
|= wxALIGN_CENTRE_HORIZONTAL
| wxALIGN_CENTRE_VERTICAL
;
85 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
89 SetImageLabel(bitmap
);
90 // SetBestSize(size); -- called by SetImageLabel()
92 CreateInputHandler(wxINP_HANDLER_BUTTON
);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
106 wxSize
wxButtonBase::GetDefaultSize()
108 static wxSize s_sizeBtn
;
110 if ( s_sizeBtn
.x
== 0 )
114 // this corresponds more or less to wxMSW standard in Win32 theme (see
115 // wxWin32Renderer::AdjustSize())
116 // s_sizeBtn.x = 8*dc.GetCharWidth();
117 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
118 // Otto Wyss, Patch 664399
119 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
120 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
126 wxSize
wxButton::DoGetBestClientSize() const
128 wxClientDC
dc(wxConstCast(this, wxButton
));
129 wxCoord width
, height
;
130 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
134 // allocate extra space for the bitmap
135 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
136 if ( height
< heightBmp
)
139 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
142 // The default size should not be adjusted, so the code is moved into the
143 // renderer. This is conceptual wrong but currently the only solution.
144 // (Otto Wyss, Patch 664399)
147 // for compatibility with other ports, the buttons default size is never
148 // less than the standard one, but not when display not PDAs.
149 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
151 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
153 wxSize szDef = GetDefaultSize();
154 if ( width < szDef.x )
159 return wxSize(width
, height
);
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 void wxButton::DoDraw(wxControlRenderer
*renderer
)
168 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
170 renderer
->DrawButtonBorder();
173 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
176 bool wxButton::DoDrawBackground(wxDC
& dc
)
179 wxSize size
= GetSize();
181 rect
.height
= size
.y
;
183 if ( GetBackgroundBitmap().Ok() )
185 // get the bitmap and the flags
188 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
189 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
193 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
194 rect
, GetStateFlags());
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 void wxButton::Press()
214 void wxButton::Release()
224 void wxButton::Toggle()
233 // releasing button after it had been pressed generates a click event
238 void wxButton::Click()
240 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
241 InitCommandEvent(event
);
245 bool wxButton::PerformAction(const wxControlAction
& action
,
247 const wxString
& strArg
)
249 if ( action
== wxACTION_BUTTON_TOGGLE
)
251 else if ( action
== wxACTION_BUTTON_CLICK
)
253 else if ( action
== wxACTION_BUTTON_PRESS
)
255 else if ( action
== wxACTION_BUTTON_RELEASE
)
258 return wxControl::PerformAction(action
, numArg
, strArg
);
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
271 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
274 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
276 m_marginBmpX
= x
+ 2;
277 m_marginBmpY
= y
+ 2;
279 SetBestSize(wxDefaultSize
);
282 void wxButton::SetDefault()
287 // ============================================================================
288 // wxStdButtonInputHandler
289 // ============================================================================
291 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
292 : wxStdInputHandler(handler
)
295 m_winHasMouse
= false;
298 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
299 const wxKeyEvent
& event
,
302 int keycode
= event
.GetKeyCode();
303 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
305 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
310 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
313 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
314 const wxMouseEvent
& event
)
316 // the button has 2 states: pressed and normal with the following
317 // transitions between them:
319 // normal -> left down -> capture mouse and go to pressed state
320 // pressed -> left up inside -> generate click -> go to normal
321 // outside ------------------>
323 // the other mouse buttons are ignored
324 if ( event
.Button(1) )
326 if ( event
.LeftDown() || event
.LeftDClick() )
328 m_winCapture
= consumer
->GetInputWindow();
329 m_winCapture
->CaptureMouse();
330 m_winHasMouse
= true;
332 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
336 else if ( event
.LeftUp() )
340 m_winCapture
->ReleaseMouse();
346 // this will generate a click event
347 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
351 //else: the mouse was released outside the window, this doesn't
354 //else: don't do anything special about the double click
357 return wxStdInputHandler::HandleMouse(consumer
, event
);
360 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
361 const wxMouseEvent
& event
)
363 // we only have to do something when the mouse leaves/enters the pressed
364 // button and don't care about the other ones
365 if ( event
.GetEventObject() == m_winCapture
)
367 // leaving the button should remove its pressed state
368 if ( event
.Leaving() )
370 // remember that the mouse is now outside
371 m_winHasMouse
= false;
373 // we do have a pressed button, so release it
374 consumer
->GetInputWindow()->SetCurrent(false);
375 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
379 // and entering it back should make it pressed again if it had been
381 else if ( event
.Entering() )
383 // the mouse is (back) inside the button
384 m_winHasMouse
= true;
386 // we did have a pressed button which we released when leaving the
387 // window, press it again
388 consumer
->GetInputWindow()->SetCurrent(true);
389 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
395 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
398 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
399 const wxFocusEvent
& WXUNUSED(event
))
401 // buttons change appearance when they get/lose focus, so return true to
406 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
407 bool WXUNUSED(activated
))
409 // the default button changes appearance when the app is [de]activated, so
410 // return true to refresh
411 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
414 #endif // wxUSE_BUTTON