]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
fixed somebody's poorly done StreamSize-->GetSize transition
[wxWidgets.git] / src / msw / button.cpp
CommitLineData
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"
8a4df159 34 #include "wx/bmpbuttn.h"
2bda0e17
KB
35#endif
36
37#include "wx/msw/private.h"
38
edccf428
VZ
39// ----------------------------------------------------------------------------
40// macros
41// ----------------------------------------------------------------------------
42
2bda0e17 43#if !USE_SHARED_LIBRARY
edccf428 44 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
2bda0e17
KB
45#endif
46
edccf428
VZ
47// this macro tries to adjust the default button height to a reasonable value
48// using the char height as the base
1c4a764c 49#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
2bda0e17 50
edccf428
VZ
51// ============================================================================
52// implementation
53// ============================================================================
54
55// ----------------------------------------------------------------------------
56// creation/destruction
57// ----------------------------------------------------------------------------
58
59bool 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)
2bda0e17 67{
8d99be5f 68 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
edccf428
VZ
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
2bda0e17 98 return TRUE;
2bda0e17
KB
99}
100
edccf428 101wxButton::~wxButton()
2bda0e17 102{
edccf428
VZ
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 }
2bda0e17
KB
112}
113
edccf428
VZ
114// ----------------------------------------------------------------------------
115// size management including autosizing
116// ----------------------------------------------------------------------------
117
4438caf4 118wxSize wxButton::DoGetBestSize()
2bda0e17 119{
4438caf4
VZ
120 wxString label = wxGetWindowText(GetHWND());
121 int wBtn;
122 GetTextExtent(label, &wBtn, NULL);
edccf428 123
4438caf4
VZ
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);
edccf428 132
4438caf4 133 return wxSize(wBtn, hBtn);
2bda0e17
KB
134}
135
e1f36ff8
VZ
136/* static */
137wxSize 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
4438caf4
VZ
150// ----------------------------------------------------------------------------
151// set this button as the default one in its panel
152// ----------------------------------------------------------------------------
153
edccf428 154void wxButton::SetDefault()
2bda0e17 155{
edccf428 156 wxWindow *parent = GetParent();
5d1d2d46 157 wxButton *btnOldDefault = NULL;
edccf428
VZ
158 wxPanel *panel = wxDynamicCast(parent, wxPanel);
159 if ( panel )
5d1d2d46
VZ
160 {
161 btnOldDefault = panel->GetDefaultItem();
edccf428 162 panel->SetDefaultItem(this);
5d1d2d46 163 }
2bda0e17 164
edccf428
VZ
165 if ( parent )
166 {
167 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
168 }
8ed57d93 169
0655ad29
VZ
170 // this doesn't work with bitmap buttons because it also removes the
171 // "ownerdrawn" style...
172 if ( btnOldDefault && !wxDynamicCast(btnOldDefault, wxBitmapButton) )
5d1d2d46
VZ
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);
2bda0e17
KB
184}
185
edccf428
VZ
186// ----------------------------------------------------------------------------
187// helpers
188// ----------------------------------------------------------------------------
189
190bool wxButton::SendClickEvent()
2bda0e17 191{
edccf428
VZ
192 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
193 event.SetEventObject(this);
194
195 return ProcessCommand(event);
2bda0e17
KB
196}
197
edccf428 198void wxButton::Command(wxCommandEvent & event)
2bda0e17 199{
edccf428 200 ProcessCommand(event);
2bda0e17
KB
201}
202
edccf428
VZ
203// ----------------------------------------------------------------------------
204// event/message handlers
205// ----------------------------------------------------------------------------
2bda0e17 206
edccf428
VZ
207bool wxButton::MSWCommand(WXUINT param, WXWORD id)
208{
209 bool processed = FALSE;
57c0af52 210 switch ( param )
edccf428 211 {
57c0af52
VZ
212 case 1: // 1 for accelerator
213 case BN_CLICKED:
214 processed = SendClickEvent();
215 break;
edccf428 216 }
2bda0e17 217
edccf428 218 return processed;
2bda0e17
KB
219}
220
edccf428
VZ
221WXHBRUSH wxButton::OnCtlColor(WXHDC pDC,
222 WXHWND pWnd,
223 WXUINT nCtlColor,
224 WXUINT message,
225 WXWPARAM wParam,
226 WXLPARAM lParam)
2bda0e17 227{
edccf428 228 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
2bda0e17 229
edccf428
VZ
230 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
231}
2bda0e17 232