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