]>
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" | |
2bda0e17 KB |
33 | #endif |
34 | ||
35 | #include "wx/msw/private.h" | |
36 | ||
edccf428 VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // macros | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
2bda0e17 | 41 | #if !USE_SHARED_LIBRARY |
edccf428 | 42 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
2bda0e17 KB |
43 | #endif |
44 | ||
edccf428 VZ |
45 | // this macro tries to adjust the default button height to a reasonable value |
46 | // using the char height as the base | |
1c4a764c | 47 | #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) |
2bda0e17 | 48 | |
edccf428 VZ |
49 | // ============================================================================ |
50 | // implementation | |
51 | // ============================================================================ | |
52 | ||
53 | // ---------------------------------------------------------------------------- | |
54 | // creation/destruction | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | bool wxButton::Create(wxWindow *parent, | |
58 | wxWindowID id, | |
59 | const wxString& label, | |
60 | const wxPoint& pos, | |
61 | const wxSize& size, | |
62 | long style, | |
63 | const wxValidator& validator, | |
64 | const wxString& name) | |
2bda0e17 | 65 | { |
edccf428 VZ |
66 | if ( !CreateBase(parent, id, pos, size, style, name) ) |
67 | return FALSE; | |
68 | ||
69 | SetValidator(validator); | |
70 | ||
71 | parent->AddChild((wxButton *)this); | |
72 | ||
73 | m_backgroundColour = parent->GetBackgroundColour() ; | |
74 | m_foregroundColour = parent->GetForegroundColour() ; | |
75 | ||
76 | m_hWnd = (WXHWND)CreateWindowEx | |
77 | ( | |
78 | MakeExtendedStyle(m_windowStyle), | |
79 | _T("BUTTON"), | |
80 | label, | |
81 | WS_VISIBLE | WS_TABSTOP | WS_CHILD, | |
82 | 0, 0, 0, 0, | |
83 | GetWinHwnd(parent), | |
84 | (HMENU)m_windowId, | |
85 | wxGetInstance(), | |
86 | NULL | |
87 | ); | |
88 | ||
89 | // Subclass again for purposes of dialog editing mode | |
90 | SubclassWin(m_hWnd); | |
91 | ||
92 | SetFont(parent->GetFont()); | |
93 | ||
94 | SetSize(pos.x, pos.y, size.x, size.y); | |
95 | ||
2bda0e17 | 96 | return TRUE; |
2bda0e17 KB |
97 | } |
98 | ||
edccf428 | 99 | wxButton::~wxButton() |
2bda0e17 | 100 | { |
edccf428 VZ |
101 | wxPanel *panel = wxDynamicCast(GetParent(), wxPanel); |
102 | if ( panel ) | |
103 | { | |
104 | if ( panel->GetDefaultItem() == this ) | |
105 | { | |
106 | // don't leave the panel with invalid default item | |
107 | panel->SetDefaultItem(NULL); | |
108 | } | |
109 | } | |
2bda0e17 KB |
110 | } |
111 | ||
edccf428 VZ |
112 | // ---------------------------------------------------------------------------- |
113 | // size management including autosizing | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
bfc6fde4 | 116 | void wxButton::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 | 117 | { |
edccf428 VZ |
118 | int currentX, currentY; |
119 | GetPosition(¤tX, ¤tY); | |
120 | int x1 = x; | |
121 | int y1 = y; | |
122 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
123 | x1 = currentX; | |
124 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
125 | y1 = currentY; | |
126 | ||
127 | AdjustForParentClientOrigin(x1, y1, sizeFlags); | |
128 | ||
129 | int actualWidth = width; | |
130 | int actualHeight = height; | |
131 | int ww, hh; | |
132 | GetSize(&ww, &hh); | |
133 | ||
134 | int current_width; | |
135 | int cyf; | |
136 | wxString buf = wxGetWindowText(GetHWND()); | |
137 | GetTextExtent(buf, ¤t_width, &cyf, NULL, NULL, &GetFont()); | |
138 | ||
139 | // If we're prepared to use the existing width, then... | |
140 | if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH)) | |
141 | { | |
142 | actualWidth = ww; | |
143 | } | |
144 | else if (width == -1) | |
145 | { | |
146 | int cx; | |
147 | int cy; | |
148 | wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont()); | |
149 | actualWidth = (int)(current_width + 3*cx) ; | |
150 | } | |
151 | ||
152 | // If we're prepared to use the existing height, then... | |
153 | if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT)) | |
154 | { | |
155 | actualHeight = hh; | |
156 | } | |
157 | else if (height == -1) | |
158 | { | |
159 | actualHeight = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cyf); | |
160 | } | |
161 | ||
162 | MoveWindow(GetHwnd(), x1, y1, actualWidth, actualHeight, TRUE); | |
2bda0e17 KB |
163 | } |
164 | ||
edccf428 | 165 | void wxButton::SetDefault() |
2bda0e17 | 166 | { |
edccf428 VZ |
167 | wxWindow *parent = GetParent(); |
168 | wxPanel *panel = wxDynamicCast(parent, wxPanel); | |
169 | if ( panel ) | |
170 | panel->SetDefaultItem(this); | |
2bda0e17 | 171 | |
edccf428 VZ |
172 | if ( parent ) |
173 | { | |
174 | SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L); | |
175 | } | |
8ed57d93 | 176 | |
edccf428 | 177 | SendMessage(GetHwnd(), BM_SETSTYLE, BS_DEFPUSHBUTTON, 1L); |
2bda0e17 KB |
178 | } |
179 | ||
edccf428 VZ |
180 | // ---------------------------------------------------------------------------- |
181 | // helpers | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | bool wxButton::SendClickEvent() | |
2bda0e17 | 185 | { |
edccf428 VZ |
186 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); |
187 | event.SetEventObject(this); | |
188 | ||
189 | return ProcessCommand(event); | |
2bda0e17 KB |
190 | } |
191 | ||
edccf428 | 192 | void wxButton::Command(wxCommandEvent & event) |
2bda0e17 | 193 | { |
edccf428 | 194 | ProcessCommand(event); |
2bda0e17 KB |
195 | } |
196 | ||
edccf428 VZ |
197 | // ---------------------------------------------------------------------------- |
198 | // event/message handlers | |
199 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 200 | |
edccf428 VZ |
201 | bool wxButton::MSWCommand(WXUINT param, WXWORD id) |
202 | { | |
203 | bool processed = FALSE; | |
204 | if ( param == BN_CLICKED || param == 1 ) // 1 for accelerator | |
205 | { | |
206 | processed = SendClickEvent(); | |
207 | } | |
2bda0e17 | 208 | |
edccf428 | 209 | return processed; |
2bda0e17 KB |
210 | } |
211 | ||
edccf428 VZ |
212 | WXHBRUSH wxButton::OnCtlColor(WXHDC pDC, |
213 | WXHWND pWnd, | |
214 | WXUINT nCtlColor, | |
215 | WXUINT message, | |
216 | WXWPARAM wParam, | |
217 | WXLPARAM lParam) | |
2bda0e17 | 218 | { |
edccf428 | 219 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); |
2bda0e17 | 220 | |
edccf428 VZ |
221 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); |
222 | } | |
2bda0e17 | 223 |