]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[wxWidgets.git] / src / msw / button.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: button.cpp
3// Purpose: wxButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
edccf428 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
edccf428
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
2bda0e17 19#ifdef __GNUG__
edccf428 20 #pragma implementation "button.h"
2bda0e17
KB
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
edccf428 27 #pragma hdrstop
2bda0e17
KB
28#endif
29
30#ifndef WX_PRECOMP
edccf428
VZ
31 #include "wx/button.h"
32 #include "wx/brush.h"
4e938f5b 33 #include "wx/panel.h"
8a4df159 34 #include "wx/bmpbuttn.h"
fb39c7ec
RR
35 #include "wx/settings.h"
36 #include "wx/dcscreen.h"
2bda0e17
KB
37#endif
38
39#include "wx/msw/private.h"
40
edccf428
VZ
41// ----------------------------------------------------------------------------
42// macros
43// ----------------------------------------------------------------------------
44
2bda0e17 45#if !USE_SHARED_LIBRARY
edccf428 46 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
2bda0e17
KB
47#endif
48
edccf428
VZ
49// this macro tries to adjust the default button height to a reasonable value
50// using the char height as the base
1c4a764c 51#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
2bda0e17 52
edccf428
VZ
53// ============================================================================
54// implementation
55// ============================================================================
56
57// ----------------------------------------------------------------------------
58// creation/destruction
59// ----------------------------------------------------------------------------
60
61bool wxButton::Create(wxWindow *parent,
62 wxWindowID id,
63 const wxString& label,
64 const wxPoint& pos,
65 const wxSize& size,
66 long style,
67 const wxValidator& validator,
68 const wxString& name)
2bda0e17 69{
8d99be5f 70 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
edccf428
VZ
71 return FALSE;
72
edccf428
VZ
73 parent->AddChild((wxButton *)this);
74
75 m_backgroundColour = parent->GetBackgroundColour() ;
76 m_foregroundColour = parent->GetForegroundColour() ;
77
78 m_hWnd = (WXHWND)CreateWindowEx
79 (
80 MakeExtendedStyle(m_windowStyle),
223d09f6 81 wxT("BUTTON"),
edccf428
VZ
82 label,
83 WS_VISIBLE | WS_TABSTOP | WS_CHILD,
a77aa9d6 84 0, 0, 0, 0,
edccf428
VZ
85 GetWinHwnd(parent),
86 (HMENU)m_windowId,
87 wxGetInstance(),
88 NULL
89 );
90
91 // Subclass again for purposes of dialog editing mode
92 SubclassWin(m_hWnd);
93
94 SetFont(parent->GetFont());
95
96 SetSize(pos.x, pos.y, size.x, size.y);
97
3f3cec48
RR
98 // bad hack added by Robert to make buttons at least
99 // 80 pixels wide. There are probably better ways...
100 // TODO. FIXME.
101 wxSize nsize( GetSize() );
102 if ((nsize.x < 80) || (nsize.y < 23))
103 {
678cd6de
VZ
104 if ((size.x == -1) && (nsize.x < 80))
105 nsize.x = 80;
106 if ((size.y == -1) && (nsize.y < 23))
107 nsize.y = 23;
3f3cec48
RR
108 SetSize( nsize );
109 }
110
2bda0e17 111 return TRUE;
2bda0e17
KB
112}
113
edccf428 114wxButton::~wxButton()
2bda0e17 115{
edccf428
VZ
116 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
117 if ( panel )
118 {
119 if ( panel->GetDefaultItem() == this )
120 {
121 // don't leave the panel with invalid default item
122 panel->SetDefaultItem(NULL);
123 }
124 }
2bda0e17
KB
125}
126
edccf428
VZ
127// ----------------------------------------------------------------------------
128// size management including autosizing
129// ----------------------------------------------------------------------------
130
4438caf4 131wxSize wxButton::DoGetBestSize()
2bda0e17 132{
4438caf4
VZ
133 wxString label = wxGetWindowText(GetHWND());
134 int wBtn;
135 GetTextExtent(label, &wBtn, NULL);
edccf428 136
4438caf4
VZ
137 int wChar, hChar;
138 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
139
140 // add a margin - the button is wider than just its label
141 wBtn += 3*wChar;
142
143 // the button height is proportional to the height of the font used
144 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
edccf428 145
4438caf4 146 return wxSize(wBtn, hBtn);
2bda0e17
KB
147}
148
e1f36ff8
VZ
149/* static */
150wxSize wxButton::GetDefaultSize()
151{
8c3c31d4 152 static wxSize s_sizeBtn;
e1f36ff8 153
8c3c31d4
VZ
154 if ( s_sizeBtn.x == 0 )
155 {
156 wxScreenDC dc;
157 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
158
159 // the size of a standard button in the dialog units is 50x14,
160 // translate this to pixels
161 // NB1: the multipliers come from the Windows convention
162 // NB2: the extra +1/+2 were needed to get the size be the same as the
163 // size of the buttons in the standard dialog - I don't know how
164 // this happens, but on my system this size is 75x23 in pixels and
165 // 23*8 isn't even divisible by 14... Would be nice to understand
166 // why these constants are needed though!
167 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
168 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
169 }
e1f36ff8 170
8c3c31d4 171 return s_sizeBtn;
e1f36ff8
VZ
172}
173
4438caf4
VZ
174// ----------------------------------------------------------------------------
175// set this button as the default one in its panel
176// ----------------------------------------------------------------------------
177
edccf428 178void wxButton::SetDefault()
2bda0e17 179{
edccf428 180 wxWindow *parent = GetParent();
5d1d2d46 181 wxButton *btnOldDefault = NULL;
edccf428
VZ
182 wxPanel *panel = wxDynamicCast(parent, wxPanel);
183 if ( panel )
5d1d2d46
VZ
184 {
185 btnOldDefault = panel->GetDefaultItem();
edccf428 186 panel->SetDefaultItem(this);
5d1d2d46 187 }
2bda0e17 188
edccf428
VZ
189 if ( parent )
190 {
191 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
192 }
8ed57d93 193
0655ad29
VZ
194 // this doesn't work with bitmap buttons because it also removes the
195 // "ownerdrawn" style...
196 if ( btnOldDefault && !wxDynamicCast(btnOldDefault, wxBitmapButton) )
5d1d2d46
VZ
197 {
198 // remove the BS_DEFPUSHBUTTON style from the other button
199 long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
200 style &= ~BS_DEFPUSHBUTTON;
201 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
202 }
203
204 // set this button as the default
205 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
206 style |= BS_DEFPUSHBUTTON;
207 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
2bda0e17
KB
208}
209
edccf428
VZ
210// ----------------------------------------------------------------------------
211// helpers
212// ----------------------------------------------------------------------------
213
214bool wxButton::SendClickEvent()
2bda0e17 215{
edccf428
VZ
216 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
217 event.SetEventObject(this);
218
219 return ProcessCommand(event);
2bda0e17
KB
220}
221
edccf428 222void wxButton::Command(wxCommandEvent & event)
2bda0e17 223{
edccf428 224 ProcessCommand(event);
2bda0e17
KB
225}
226
edccf428
VZ
227// ----------------------------------------------------------------------------
228// event/message handlers
229// ----------------------------------------------------------------------------
2bda0e17 230
edccf428
VZ
231bool wxButton::MSWCommand(WXUINT param, WXWORD id)
232{
233 bool processed = FALSE;
57c0af52 234 switch ( param )
edccf428 235 {
678cd6de 236 case 1: // means that the message came from an accelerator
57c0af52
VZ
237 case BN_CLICKED:
238 processed = SendClickEvent();
239 break;
edccf428 240 }
2bda0e17 241
edccf428 242 return processed;
2bda0e17
KB
243}
244
edccf428
VZ
245WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
246 WXHWND pWnd,
247 WXUINT nCtlColor,
248 WXUINT message,
249 WXWPARAM wParam,
250 WXLPARAM lParam)
2bda0e17 251{
87a1e308 252 const HDC& hdc = (HDC)pDC;
2bda0e17 253
87a1e308
VZ
254 const wxColour& colBack = GetBackgroundColour();
255 ::SetBkColor(hdc, RGB(colBack.Red(), colBack.Green(), colBack.Blue()));
256
257 const wxColour& colFor = GetForegroundColour();
258 ::SetTextColor(hdc, RGB(colFor.Red(), colFor.Green(), colFor.Blue()));
259
260 ::SetBkMode(hdc, OPAQUE);
261
262 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(colBack,
263 wxSOLID);
264 backgroundBrush->RealizeResource();
265 return (WXHBRUSH)backgroundBrush->GetResourceHandle();
edccf428 266}
2bda0e17 267
678cd6de
VZ
268long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
269{
270 // make sure that we won't have BS_DEFPUSHBUTTON style any more if the
271 // focus is being transfered to another button with the same parent -
272 // otherwise, we could finish with 2 default buttons inside one panel
273 if ( (nMsg == WM_KILLFOCUS) &&
274 (GetWindowLong(GetHwnd(), GWL_STYLE) & BS_DEFPUSHBUTTON) )
275 {
276 wxWindow *parent = GetParent();
277 wxWindow *win = wxFindWinFromHandle((WXHWND)wParam);
278 if ( win && win->GetParent() == parent )
279 {
280 wxPanel *panel = wxDynamicCast(parent, wxPanel);
281 if ( panel )
282 {
283 panel->SetDefaultItem(this);
284 }
285 // else: I don't know what to do - we'll still have the problem
286 // with multiple default buttons in a dialog...
287 }
288 }
289
290 // let the base class do all real processing
291 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
292}