]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
mbstowcs hack for CodeWarrior's MSL
[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
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"
2432b92d 25#include "wx/brush.h"
2bda0e17
KB
26#endif
27
28#include "wx/msw/private.h"
29
30#if !USE_SHARED_LIBRARY
31IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
32#endif
33
1c4a764c 34#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
2bda0e17
KB
35
36// Buttons
37
debe6624 38bool wxButton::MSWCommand(WXUINT param, WXWORD id)
2bda0e17 39{
567da5c6 40 if (param == BN_CLICKED || (param == 1)) // 1 for accelerator
2bda0e17
KB
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
debe6624 50bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label,
2bda0e17 51 const wxPoint& pos,
debe6624 52 const wxSize& size, long style,
2bda0e17
KB
53 const wxValidator& validator,
54 const wxString& name)
55{
56 SetName(name);
57 SetValidator(validator);
58
59 parent->AddChild((wxButton *)this);
fd71308f
JS
60 m_backgroundColour = parent->GetBackgroundColour() ;
61 m_foregroundColour = parent->GetForegroundColour() ;
2bda0e17
KB
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
1f112209 81#if wxUSE_CTL3D
2bda0e17
KB
82// if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
83// Ctl3dSubclassCtl(wx_button);
84#endif
85
86 m_hWnd = (WXHWND)wx_button;
87
88 // Subclass again for purposes of dialog editing mode
89 SubclassWin((WXHWND)wx_button);
90
c0ed460c 91 SetFont(parent->GetFont());
2bda0e17
KB
92
93 SetSize(x, y, width, height);
94 ShowWindow(wx_button, SW_SHOW);
95
96 return TRUE;
97}
98
debe6624 99void wxButton::SetSize(int x, int y, int width, int height, int sizeFlags)
2bda0e17
KB
100{
101 int currentX, currentY;
102 GetPosition(&currentX, &currentY);
103 int x1 = x;
104 int y1 = y;
105 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
106 x1 = currentX;
107 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
108 y1 = currentY;
109
81d66cf3
JS
110 AdjustForParentClientOrigin(x1, y1, sizeFlags);
111
2bda0e17
KB
112 int actualWidth = width;
113 int actualHeight = height;
114 int ww, hh;
115 GetSize(&ww, &hh);
116
debe6624
JS
117 int current_width;
118 int cyf;
2bda0e17
KB
119 char buf[300];
120 GetWindowText((HWND) GetHWND(), buf, 300);
ce3ed50d
JS
121 GetTextExtent(buf, &current_width, &cyf,NULL,NULL,
122 & this->GetFont());
2bda0e17
KB
123
124 // If we're prepared to use the existing width, then...
125 if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH))
126 actualWidth = ww;
127 else if (width == -1)
128 {
129 int cx;
130 int cy;
ce3ed50d 131 wxGetCharSize(GetHWND(), &cx, &cy, & this->GetFont());
2bda0e17
KB
132 actualWidth = (int)(current_width + 3*cx) ;
133 }
134
135 // If we're prepared to use the existing height, then...
136 if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT))
137 actualHeight = hh;
138 else if (height == -1)
139 {
1c4a764c 140 actualHeight = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cyf);
2bda0e17
KB
141 }
142
143 MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE);
2bda0e17
KB
144}
145
146void wxButton::SetDefault(void)
147{
148 wxWindow *parent = (wxWindow *)GetParent();
149 if (parent)
150 parent->SetDefaultItem(this);
151
152 if (parent)
153 {
8ed57d93 154 SendMessage((HWND)parent->GetHWND(), DM_SETDEFID, m_windowId, 0L);
2bda0e17 155 }
8ed57d93
VZ
156
157 SendMessage((HWND)GetHWND(), BM_SETSTYLE, BS_DEFPUSHBUTTON, 1L);
2bda0e17
KB
158}
159
160wxString wxButton::GetLabel(void) const
161{
162 GetWindowText((HWND) GetHWND(), wxBuffer, 300);
163 return wxString(wxBuffer);
164}
165
166void wxButton::SetLabel(const wxString& label)
167{
168 SetWindowText((HWND) GetHWND(), (const char *) label);
169}
170
debe6624 171WXHBRUSH wxButton::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
2bda0e17
KB
172 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
173{
174/*
175 WXHBRUSH hBrush = (WXHBRUSH) MSWDefWindowProc(message, wParam, lParam);
176// ::SetTextColor((HDC) pDC, GetSysColor(COLOR_BTNTEXT));
177 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(),
178 GetForegroundColour().Blue()));
179
180 return hBrush;
181*/
182 // This doesn't in fact seem to make any difference at all - buttons are always
183 // the same colour.
184 ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
185 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
186
187 wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID);
188
189 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
190 // has a zero usage count.
191 // NOT NOW; CHANGED.
192// backgroundBrush->RealizeResource();
193 return (WXHBRUSH) backgroundBrush->GetResourceHandle();
194}
195
196void wxButton::Command (wxCommandEvent & event)
197{
198 ProcessCommand (event);
199}
200
201