]>
Commit | Line | Data |
---|---|---|
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" |
2bda0e17 KB |
34 | #endif |
35 | ||
36 | #include "wx/msw/private.h" | |
37 | ||
edccf428 VZ |
38 | // ---------------------------------------------------------------------------- |
39 | // macros | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
2bda0e17 | 42 | #if !USE_SHARED_LIBRARY |
edccf428 | 43 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
2bda0e17 KB |
44 | #endif |
45 | ||
edccf428 VZ |
46 | // this macro tries to adjust the default button height to a reasonable value |
47 | // using the char height as the base | |
1c4a764c | 48 | #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) |
2bda0e17 | 49 | |
edccf428 VZ |
50 | // ============================================================================ |
51 | // implementation | |
52 | // ============================================================================ | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // creation/destruction | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | bool wxButton::Create(wxWindow *parent, | |
59 | wxWindowID id, | |
60 | const wxString& label, | |
61 | const wxPoint& pos, | |
62 | const wxSize& size, | |
63 | long style, | |
64 | const wxValidator& validator, | |
65 | const wxString& name) | |
2bda0e17 | 66 | { |
8d99be5f | 67 | if ( !CreateBase(parent, id, pos, size, style, validator, name) ) |
edccf428 VZ |
68 | return FALSE; |
69 | ||
70 | SetValidator(validator); | |
71 | ||
72 | parent->AddChild((wxButton *)this); | |
73 | ||
74 | m_backgroundColour = parent->GetBackgroundColour() ; | |
75 | m_foregroundColour = parent->GetForegroundColour() ; | |
76 | ||
77 | m_hWnd = (WXHWND)CreateWindowEx | |
78 | ( | |
79 | MakeExtendedStyle(m_windowStyle), | |
80 | _T("BUTTON"), | |
81 | label, | |
82 | WS_VISIBLE | WS_TABSTOP | WS_CHILD, | |
83 | 0, 0, 0, 0, | |
84 | GetWinHwnd(parent), | |
85 | (HMENU)m_windowId, | |
86 | wxGetInstance(), | |
87 | NULL | |
88 | ); | |
89 | ||
90 | // Subclass again for purposes of dialog editing mode | |
91 | SubclassWin(m_hWnd); | |
92 | ||
93 | SetFont(parent->GetFont()); | |
94 | ||
95 | SetSize(pos.x, pos.y, size.x, size.y); | |
96 | ||
2bda0e17 | 97 | return TRUE; |
2bda0e17 KB |
98 | } |
99 | ||
edccf428 | 100 | wxButton::~wxButton() |
2bda0e17 | 101 | { |
edccf428 VZ |
102 | wxPanel *panel = wxDynamicCast(GetParent(), wxPanel); |
103 | if ( panel ) | |
104 | { | |
105 | if ( panel->GetDefaultItem() == this ) | |
106 | { | |
107 | // don't leave the panel with invalid default item | |
108 | panel->SetDefaultItem(NULL); | |
109 | } | |
110 | } | |
2bda0e17 KB |
111 | } |
112 | ||
edccf428 VZ |
113 | // ---------------------------------------------------------------------------- |
114 | // size management including autosizing | |
115 | // ---------------------------------------------------------------------------- | |
116 | ||
4438caf4 | 117 | wxSize wxButton::DoGetBestSize() |
2bda0e17 | 118 | { |
4438caf4 VZ |
119 | wxString label = wxGetWindowText(GetHWND()); |
120 | int wBtn; | |
121 | GetTextExtent(label, &wBtn, NULL); | |
edccf428 | 122 | |
4438caf4 VZ |
123 | int wChar, hChar; |
124 | wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont()); | |
125 | ||
126 | // add a margin - the button is wider than just its label | |
127 | wBtn += 3*wChar; | |
128 | ||
129 | // the button height is proportional to the height of the font used | |
130 | int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar); | |
edccf428 | 131 | |
4438caf4 | 132 | return wxSize(wBtn, hBtn); |
2bda0e17 KB |
133 | } |
134 | ||
e1f36ff8 VZ |
135 | /* static */ |
136 | wxSize wxButton::GetDefaultSize() | |
137 | { | |
138 | // the base unit is the height of the system GUI font | |
139 | int wChar, hChar; | |
140 | wxGetCharSize(0, &wChar, &hChar, NULL); | |
141 | ||
142 | // the button height is proportional to the height of the font used | |
143 | int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar); | |
144 | ||
145 | // and the width/height ration is 75/23 | |
146 | return wxSize((75 * hBtn) / 23, hBtn); | |
147 | } | |
148 | ||
4438caf4 VZ |
149 | // ---------------------------------------------------------------------------- |
150 | // set this button as the default one in its panel | |
151 | // ---------------------------------------------------------------------------- | |
152 | ||
edccf428 | 153 | void wxButton::SetDefault() |
2bda0e17 | 154 | { |
edccf428 | 155 | wxWindow *parent = GetParent(); |
5d1d2d46 | 156 | wxButton *btnOldDefault = NULL; |
edccf428 VZ |
157 | wxPanel *panel = wxDynamicCast(parent, wxPanel); |
158 | if ( panel ) | |
5d1d2d46 VZ |
159 | { |
160 | btnOldDefault = panel->GetDefaultItem(); | |
edccf428 | 161 | panel->SetDefaultItem(this); |
5d1d2d46 | 162 | } |
2bda0e17 | 163 | |
edccf428 VZ |
164 | if ( parent ) |
165 | { | |
166 | SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L); | |
167 | } | |
8ed57d93 | 168 | |
0655ad29 VZ |
169 | // this doesn't work with bitmap buttons because it also removes the |
170 | // "ownerdrawn" style... | |
171 | if ( btnOldDefault && !wxDynamicCast(btnOldDefault, wxBitmapButton) ) | |
5d1d2d46 VZ |
172 | { |
173 | // remove the BS_DEFPUSHBUTTON style from the other button | |
174 | long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE); | |
175 | style &= ~BS_DEFPUSHBUTTON; | |
176 | SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L); | |
177 | } | |
178 | ||
179 | // set this button as the default | |
180 | long style = GetWindowLong(GetHwnd(), GWL_STYLE); | |
181 | style |= BS_DEFPUSHBUTTON; | |
182 | SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L); | |
2bda0e17 KB |
183 | } |
184 | ||
edccf428 VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // helpers | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | bool wxButton::SendClickEvent() | |
2bda0e17 | 190 | { |
edccf428 VZ |
191 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); |
192 | event.SetEventObject(this); | |
193 | ||
194 | return ProcessCommand(event); | |
2bda0e17 KB |
195 | } |
196 | ||
edccf428 | 197 | void wxButton::Command(wxCommandEvent & event) |
2bda0e17 | 198 | { |
edccf428 | 199 | ProcessCommand(event); |
2bda0e17 KB |
200 | } |
201 | ||
edccf428 VZ |
202 | // ---------------------------------------------------------------------------- |
203 | // event/message handlers | |
204 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 205 | |
edccf428 VZ |
206 | bool wxButton::MSWCommand(WXUINT param, WXWORD id) |
207 | { | |
208 | bool processed = FALSE; | |
57c0af52 | 209 | switch ( param ) |
edccf428 | 210 | { |
57c0af52 VZ |
211 | case 1: // 1 for accelerator |
212 | case BN_CLICKED: | |
213 | processed = SendClickEvent(); | |
214 | break; | |
edccf428 | 215 | } |
2bda0e17 | 216 | |
edccf428 | 217 | return processed; |
2bda0e17 KB |
218 | } |
219 | ||
edccf428 VZ |
220 | WXHBRUSH wxButton::OnCtlColor(WXHDC pDC, |
221 | WXHWND pWnd, | |
222 | WXUINT nCtlColor, | |
223 | WXUINT message, | |
224 | WXWPARAM wParam, | |
225 | WXLPARAM lParam) | |
2bda0e17 | 226 | { |
edccf428 | 227 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); |
2bda0e17 | 228 | |
edccf428 VZ |
229 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); |
230 | } | |
2bda0e17 | 231 |