]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
*** empty log message ***
[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 #endif
35
36 #include "wx/msw/private.h"
37
38 // ----------------------------------------------------------------------------
39 // macros
40 // ----------------------------------------------------------------------------
41
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
44 #endif
45
46 // this macro tries to adjust the default button height to a reasonable value
47 // using the char height as the base
48 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
49
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)
66 {
67 if ( !CreateBase(parent, id, pos, size, style, name) )
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
97 return TRUE;
98 }
99
100 wxButton::~wxButton()
101 {
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 }
111 }
112
113 // ----------------------------------------------------------------------------
114 // size management including autosizing
115 // ----------------------------------------------------------------------------
116
117 wxSize wxButton::DoGetBestSize()
118 {
119 wxString label = wxGetWindowText(GetHWND());
120 int wBtn;
121 GetTextExtent(label, &wBtn, NULL);
122
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);
131
132 return wxSize(wBtn, hBtn);
133 }
134
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
149 // ----------------------------------------------------------------------------
150 // set this button as the default one in its panel
151 // ----------------------------------------------------------------------------
152
153 void wxButton::SetDefault()
154 {
155 wxWindow *parent = GetParent();
156 wxPanel *panel = wxDynamicCast(parent, wxPanel);
157 if ( panel )
158 panel->SetDefaultItem(this);
159
160 if ( parent )
161 {
162 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
163 }
164
165 SendMessage(GetHwnd(), BM_SETSTYLE, BS_DEFPUSHBUTTON, 1L);
166 }
167
168 // ----------------------------------------------------------------------------
169 // helpers
170 // ----------------------------------------------------------------------------
171
172 bool wxButton::SendClickEvent()
173 {
174 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
175 event.SetEventObject(this);
176
177 return ProcessCommand(event);
178 }
179
180 void wxButton::Command(wxCommandEvent & event)
181 {
182 ProcessCommand(event);
183 }
184
185 // ----------------------------------------------------------------------------
186 // event/message handlers
187 // ----------------------------------------------------------------------------
188
189 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
190 {
191 bool processed = FALSE;
192 switch ( param )
193 {
194 case 1: // 1 for accelerator
195 case BN_CLICKED:
196 processed = SendClickEvent();
197 break;
198 }
199
200 return processed;
201 }
202
203 WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
204 WXHWND pWnd,
205 WXUINT nCtlColor,
206 WXUINT message,
207 WXWPARAM wParam,
208 WXLPARAM lParam)
209 {
210 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
211
212 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
213 }
214