]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
Now its possible to run wxHTML without wxSockets
[wxWidgets.git] / src / msw / button.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19 #ifdef __GNUG__
20 #pragma implementation "button.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/button.h"
32 #include "wx/brush.h"
33 #include "wx/panel.h"
34 #include "wx/bmpbuttn.h"
35 #endif
36
37 #include "wx/msw/private.h"
38
39 // ----------------------------------------------------------------------------
40 // macros
41 // ----------------------------------------------------------------------------
42
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
45 #endif
46
47 // this macro tries to adjust the default button height to a reasonable value
48 // using the char height as the base
49 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
50
51 // ============================================================================
52 // implementation
53 // ============================================================================
54
55 // ----------------------------------------------------------------------------
56 // creation/destruction
57 // ----------------------------------------------------------------------------
58
59 bool wxButton::Create(wxWindow *parent,
60 wxWindowID id,
61 const wxString& label,
62 const wxPoint& pos,
63 const wxSize& size,
64 long style,
65 const wxValidator& validator,
66 const wxString& name)
67 {
68 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
69 return FALSE;
70
71 SetValidator(validator);
72
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),
81 _T("BUTTON"),
82 label,
83 WS_VISIBLE | WS_TABSTOP | WS_CHILD,
84 0, 0, 0, 0,
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
98 return TRUE;
99 }
100
101 wxButton::~wxButton()
102 {
103 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
104 if ( panel )
105 {
106 if ( panel->GetDefaultItem() == this )
107 {
108 // don't leave the panel with invalid default item
109 panel->SetDefaultItem(NULL);
110 }
111 }
112 }
113
114 // ----------------------------------------------------------------------------
115 // size management including autosizing
116 // ----------------------------------------------------------------------------
117
118 wxSize wxButton::DoGetBestSize()
119 {
120 wxString label = wxGetWindowText(GetHWND());
121 int wBtn;
122 GetTextExtent(label, &wBtn, NULL);
123
124 int wChar, hChar;
125 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
126
127 // add a margin - the button is wider than just its label
128 wBtn += 3*wChar;
129
130 // the button height is proportional to the height of the font used
131 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
132
133 return wxSize(wBtn, hBtn);
134 }
135
136 /* static */
137 wxSize wxButton::GetDefaultSize()
138 {
139 // the base unit is the height of the system GUI font
140 int wChar, hChar;
141 wxGetCharSize(0, &wChar, &hChar, NULL);
142
143 // the button height is proportional to the height of the font used
144 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
145
146 // and the width/height ration is 75/23
147 return wxSize((75 * hBtn) / 23, hBtn);
148 }
149
150 // ----------------------------------------------------------------------------
151 // set this button as the default one in its panel
152 // ----------------------------------------------------------------------------
153
154 void wxButton::SetDefault()
155 {
156 wxWindow *parent = GetParent();
157 wxButton *btnOldDefault = NULL;
158 wxPanel *panel = wxDynamicCast(parent, wxPanel);
159 if ( panel )
160 {
161 btnOldDefault = panel->GetDefaultItem();
162 panel->SetDefaultItem(this);
163 }
164
165 if ( parent )
166 {
167 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
168 }
169
170 // this doesn't work with bitmap buttons because it also removes the
171 // "ownerdrawn" style...
172 if ( btnOldDefault && !wxDynamicCast(btnOldDefault, wxBitmapButton) )
173 {
174 // remove the BS_DEFPUSHBUTTON style from the other button
175 long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
176 style &= ~BS_DEFPUSHBUTTON;
177 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
178 }
179
180 // set this button as the default
181 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
182 style |= BS_DEFPUSHBUTTON;
183 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
184 }
185
186 // ----------------------------------------------------------------------------
187 // helpers
188 // ----------------------------------------------------------------------------
189
190 bool wxButton::SendClickEvent()
191 {
192 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
193 event.SetEventObject(this);
194
195 return ProcessCommand(event);
196 }
197
198 void wxButton::Command(wxCommandEvent & event)
199 {
200 ProcessCommand(event);
201 }
202
203 // ----------------------------------------------------------------------------
204 // event/message handlers
205 // ----------------------------------------------------------------------------
206
207 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
208 {
209 bool processed = FALSE;
210 switch ( param )
211 {
212 case 1: // 1 for accelerator
213 case BN_CLICKED:
214 processed = SendClickEvent();
215 break;
216 }
217
218 return processed;
219 }
220
221 WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
222 WXHWND pWnd,
223 WXUINT nCtlColor,
224 WXUINT message,
225 WXWPARAM wParam,
226 WXLPARAM lParam)
227 {
228 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
229
230 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
231 }
232