]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
fixed an over-optimisation
[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 #ifdef __GNUG__
13 #pragma implementation "button.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/button.h"
25 #include "wx/brush.h"
26 #endif
27
28 #include "wx/msw/private.h"
29
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
32 #endif
33
34 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
35
36 // Buttons
37
38 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
39 {
40 if (param == BN_CLICKED || (param == 1)) // 1 for accelerator
41 {
42 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, id);
43 event.SetEventObject(this);
44 ProcessCommand(event);
45 return TRUE;
46 }
47 else return FALSE;
48 }
49
50 bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
51 const wxPoint& pos,
52 const wxSize& size, long style,
53 const wxValidator& validator,
54 const wxString& name)
55 {
56 SetName(name);
57 SetValidator(validator);
58
59 parent->AddChild((wxButton *)this);
60 m_backgroundColour = parent->GetBackgroundColour() ;
61 m_foregroundColour = parent->GetForegroundColour() ;
62
63 m_windowStyle = (long&)style;
64
65 int x = pos.x;
66 int y = pos.y;
67 int width = size.x;
68 int height = size.y;
69
70 if (id == -1)
71 m_windowId = NewControlId();
72 else
73 m_windowId = id;
74
75 DWORD exStyle = MakeExtendedStyle(m_windowStyle);
76 HWND wx_button =
77 CreateWindowEx(exStyle, "BUTTON", label, BS_PUSHBUTTON | WS_TABSTOP | WS_CHILD,
78 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
79 wxGetInstance(), NULL);
80
81 m_hWnd = (WXHWND)wx_button;
82
83 // Subclass again for purposes of dialog editing mode
84 SubclassWin((WXHWND)wx_button);
85
86 SetFont(parent->GetFont());
87
88 SetSize(x, y, width, height);
89 ShowWindow(wx_button, SW_SHOW);
90
91 return TRUE;
92 }
93
94 void wxButton::DoSetSize(int x, int y, int width, int height, int sizeFlags)
95 {
96 int currentX, currentY;
97 GetPosition(&currentX, &currentY);
98 int x1 = x;
99 int y1 = y;
100 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
101 x1 = currentX;
102 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
103 y1 = currentY;
104
105 AdjustForParentClientOrigin(x1, y1, sizeFlags);
106
107 int actualWidth = width;
108 int actualHeight = height;
109 int ww, hh;
110 GetSize(&ww, &hh);
111
112 int current_width;
113 int cyf;
114 char buf[300];
115 GetWindowText((HWND) GetHWND(), buf, 300);
116 GetTextExtent(buf, &current_width, &cyf,NULL,NULL,
117 & this->GetFont());
118
119 // If we're prepared to use the existing width, then...
120 if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH))
121 actualWidth = ww;
122 else if (width == -1)
123 {
124 int cx;
125 int cy;
126 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
127 actualWidth = (int)(current_width + 3*cx) ;
128 }
129
130 // If we're prepared to use the existing height, then...
131 if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT))
132 actualHeight = hh;
133 else if (height == -1)
134 {
135 actualHeight = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cyf);
136 }
137
138 MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE);
139 }
140
141 void wxButton::SetDefault(void)
142 {
143 wxWindow *parent = (wxWindow *)GetParent();
144 if (parent)
145 parent->SetDefaultItem(this);
146
147 if (parent)
148 {
149 SendMessage((HWND)parent->GetHWND(), DM_SETDEFID, m_windowId, 0L);
150 }
151
152 SendMessage((HWND)GetHWND(), BM_SETSTYLE, BS_DEFPUSHBUTTON, 1L);
153 }
154
155 wxString wxButton::GetLabel(void) const
156 {
157 GetWindowText((HWND) GetHWND(), wxBuffer, 300);
158 return wxString(wxBuffer);
159 }
160
161 void wxButton::SetLabel(const wxString& label)
162 {
163 SetWindowText((HWND) GetHWND(), (const char *) label);
164 }
165
166 WXHBRUSH wxButton::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
167 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
168 {
169 /*
170 WXHBRUSH hBrush = (WXHBRUSH) MSWDefWindowProc(message, wParam, lParam);
171 // ::SetTextColor((HDC) pDC, GetSysColor(COLOR_BTNTEXT));
172 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(),
173 GetForegroundColour().Blue()));
174
175 return hBrush;
176 */
177 // This doesn't in fact seem to make any difference at all - buttons are always
178 // the same colour.
179 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
180 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
181
182 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
183
184 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
185 // has a zero usage count.
186 // NOT NOW; CHANGED.
187 // backgroundBrush->RealizeResource();
188 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
189 }
190
191 void wxButton::Command (wxCommandEvent & event)
192 {
193 ProcessCommand (event);
194 }
195
196