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"
44 #include "wx/stockitem.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // default margins around the image
51 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
52 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
54 // ============================================================================
56 // ============================================================================
58 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
70 bool wxButton::Create(wxWindow
*parent
,
72 const wxBitmap
& bitmap
,
77 const wxValidator
& validator
,
81 if (label
.empty() && wxIsStockID(id
))
82 label
= wxGetStockLabel(id
);
84 // center label by default
85 if ( !(style
& wxALIGN_MASK
) )
87 style
|= wxALIGN_CENTRE_HORIZONTAL
| wxALIGN_CENTRE_VERTICAL
;
90 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
94 SetImageLabel(bitmap
);
95 // SetBestSize(size); -- called by SetImageLabel()
97 CreateInputHandler(wxINP_HANDLER_BUTTON
);
102 wxButton::~wxButton()
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
111 wxSize
wxButtonBase::GetDefaultSize()
113 static wxSize s_sizeBtn
;
115 if ( s_sizeBtn
.x
== 0 )
119 // this corresponds more or less to wxMSW standard in Win32 theme (see
120 // wxWin32Renderer::AdjustSize())
121 // s_sizeBtn.x = 8*dc.GetCharWidth();
122 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
123 // Otto Wyss, Patch 664399
124 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
125 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
131 wxSize
wxButton::DoGetBestClientSize() const
133 wxClientDC
dc(wxConstCast(this, wxButton
));
134 wxCoord width
, height
;
135 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
139 // allocate extra space for the bitmap
140 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
141 if ( height
< heightBmp
)
144 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
147 // The default size should not be adjusted, so the code is moved into the
148 // renderer. This is conceptual wrong but currently the only solution.
149 // (Otto Wyss, Patch 664399)
152 // for compatibility with other ports, the buttons default size is never
153 // less than the standard one, but not when display not PDAs.
154 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
156 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
158 wxSize szDef = GetDefaultSize();
159 if ( width < szDef.x )
164 return wxSize(width
, height
);
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 void wxButton::DoDraw(wxControlRenderer
*renderer
)
173 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
175 renderer
->DrawButtonBorder();
178 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
181 bool wxButton::DoDrawBackground(wxDC
& dc
)
184 wxSize size
= GetSize();
186 rect
.height
= size
.y
;
188 if ( GetBackgroundBitmap().Ok() )
190 // get the bitmap and the flags
193 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
194 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
198 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
199 rect
, GetStateFlags());
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 void wxButton::Press()
219 void wxButton::Release()
229 void wxButton::Toggle()
238 // releasing button after it had been pressed generates a click event
243 void wxButton::Click()
245 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
246 InitCommandEvent(event
);
250 bool wxButton::PerformAction(const wxControlAction
& action
,
252 const wxString
& strArg
)
254 if ( action
== wxACTION_BUTTON_TOGGLE
)
256 else if ( action
== wxACTION_BUTTON_CLICK
)
258 else if ( action
== wxACTION_BUTTON_PRESS
)
260 else if ( action
== wxACTION_BUTTON_RELEASE
)
263 return wxControl::PerformAction(action
, numArg
, strArg
);
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
276 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
279 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
281 m_marginBmpX
= x
+ 2;
282 m_marginBmpY
= y
+ 2;
284 SetBestSize(wxDefaultSize
);
287 void wxButton::SetDefault()
292 // ============================================================================
293 // wxStdButtonInputHandler
294 // ============================================================================
296 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
297 : wxStdInputHandler(handler
)
300 m_winHasMouse
= false;
303 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
304 const wxKeyEvent
& event
,
307 int keycode
= event
.GetKeyCode();
308 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
310 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
315 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
318 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
319 const wxMouseEvent
& event
)
321 // the button has 2 states: pressed and normal with the following
322 // transitions between them:
324 // normal -> left down -> capture mouse and go to pressed state
325 // pressed -> left up inside -> generate click -> go to normal
326 // outside ------------------>
328 // the other mouse buttons are ignored
329 if ( event
.Button(1) )
331 if ( event
.LeftDown() || event
.LeftDClick() )
333 m_winCapture
= consumer
->GetInputWindow();
334 m_winCapture
->CaptureMouse();
335 m_winHasMouse
= true;
337 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
341 else if ( event
.LeftUp() )
345 m_winCapture
->ReleaseMouse();
351 // this will generate a click event
352 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
356 //else: the mouse was released outside the window, this doesn't
359 //else: don't do anything special about the double click
362 return wxStdInputHandler::HandleMouse(consumer
, event
);
365 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
366 const wxMouseEvent
& event
)
368 // we only have to do something when the mouse leaves/enters the pressed
369 // button and don't care about the other ones
370 if ( event
.GetEventObject() == m_winCapture
)
372 // leaving the button should remove its pressed state
373 if ( event
.Leaving() )
375 // remember that the mouse is now outside
376 m_winHasMouse
= false;
378 // we do have a pressed button, so release it
379 consumer
->GetInputWindow()->SetCurrent(false);
380 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
384 // and entering it back should make it pressed again if it had been
386 else if ( event
.Entering() )
388 // the mouse is (back) inside the button
389 m_winHasMouse
= true;
391 // we did have a pressed button which we released when leaving the
392 // window, press it again
393 consumer
->GetInputWindow()->SetCurrent(true);
394 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
400 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
403 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
404 const wxFocusEvent
& WXUNUSED(event
))
406 // buttons change appearance when they get/lose focus, so return true to
411 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
412 bool WXUNUSED(activated
))
414 // the default button changes appearance when the app is [de]activated, so
415 // return true to refresh
416 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
419 #endif // wxUSE_BUTTON