]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
wxButton::GetDefaultSize() added for MSW and documented
[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 #endif
34
35 #include "wx/msw/private.h"
36
37 // ----------------------------------------------------------------------------
38 // macros
39 // ----------------------------------------------------------------------------
40
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
43 #endif
44
45 // this macro tries to adjust the default button height to a reasonable value
46 // using the char height as the base
47 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
48
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)
65 {
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
96 return TRUE;
97 }
98
99 wxButton::~wxButton()
100 {
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 }
110 }
111
112 // ----------------------------------------------------------------------------
113 // size management including autosizing
114 // ----------------------------------------------------------------------------
115
116 wxSize wxButton::DoGetBestSize()
117 {
118 wxString label = wxGetWindowText(GetHWND());
119 int wBtn;
120 GetTextExtent(label, &wBtn, NULL);
121
122 int wChar, hChar;
123 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
124
125 // add a margin - the button is wider than just its label
126 wBtn += 3*wChar;
127
128 // the button height is proportional to the height of the font used
129 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
130
131 return wxSize(wBtn, hBtn);
132 }
133
134 /* static */
135 wxSize wxButton::GetDefaultSize()
136 {
137 // the base unit is the height of the system GUI font
138 int wChar, hChar;
139 wxGetCharSize(0, &wChar, &hChar, NULL);
140
141 // the button height is proportional to the height of the font used
142 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
143
144 // and the width/height ration is 75/23
145 return wxSize((75 * hBtn) / 23, hBtn);
146 }
147
148 // ----------------------------------------------------------------------------
149 // set this button as the default one in its panel
150 // ----------------------------------------------------------------------------
151
152 void wxButton::SetDefault()
153 {
154 wxWindow *parent = GetParent();
155 wxPanel *panel = wxDynamicCast(parent, wxPanel);
156 if ( panel )
157 panel->SetDefaultItem(this);
158
159 if ( parent )
160 {
161 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
162 }
163
164 SendMessage(GetHwnd(), BM_SETSTYLE, BS_DEFPUSHBUTTON, 1L);
165 }
166
167 // ----------------------------------------------------------------------------
168 // helpers
169 // ----------------------------------------------------------------------------
170
171 bool wxButton::SendClickEvent()
172 {
173 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
174 event.SetEventObject(this);
175
176 return ProcessCommand(event);
177 }
178
179 void wxButton::Command(wxCommandEvent & event)
180 {
181 ProcessCommand(event);
182 }
183
184 // ----------------------------------------------------------------------------
185 // event/message handlers
186 // ----------------------------------------------------------------------------
187
188 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
189 {
190 bool processed = FALSE;
191 switch ( param )
192 {
193 case 1: // 1 for accelerator
194 case BN_CLICKED:
195 processed = SendClickEvent();
196 break;
197 }
198
199 return processed;
200 }
201
202 WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
203 WXHWND pWnd,
204 WXUINT nCtlColor,
205 WXUINT message,
206 WXWPARAM wParam,
207 WXLPARAM lParam)
208 {
209 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
210
211 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
212 }
213