]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
This is how wxPlotWindow would look like with the
[wxWidgets.git] / src / msw / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: control.cpp
3 // Purpose: wxControl class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "control.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/event.h"
25 #include "wx/app.h"
26 #include "wx/dcclient.h"
27 #include "wx/log.h"
28 #endif
29
30 #include "wx/control.h"
31
32 #include "wx/msw/private.h"
33
34 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
35 #include <commctrl.h>
36 #endif
37
38 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
39
40 BEGIN_EVENT_TABLE(wxControl, wxWindow)
41 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
42 END_EVENT_TABLE()
43
44 // Item members
45 wxControl::wxControl()
46 {
47 m_backgroundColour = *wxWHITE;
48 m_foregroundColour = *wxBLACK;
49
50 #if WXWIN_COMPATIBILITY
51 m_callback = 0;
52 #endif // WXWIN_COMPATIBILITY
53 }
54
55 wxControl::~wxControl()
56 {
57 m_isBeingDeleted = TRUE;
58 }
59
60
61 bool wxControl::Create(wxWindow *parent, wxWindowID id,
62 const wxPoint& pos,
63 const wxSize& size, long style,
64 const wxValidator& validator,
65 const wxString& name)
66 {
67 bool rval = wxWindow::Create(parent, id, pos, size, style, name);
68 if (rval) {
69 #if wxUSE_VALIDATORS
70 SetValidator(validator);
71 #endif
72 }
73 return rval;
74 }
75
76 bool wxControl::MSWCreateControl(const wxChar *classname,
77 WXDWORD style,
78 const wxPoint& pos,
79 const wxSize& size,
80 const wxString& label,
81 WXDWORD exstyle)
82 {
83 // want3D tells us whether or not the style specified a 3D border.
84 // If so, under WIN16 we can use Ctl3D to give it an appropriate style.
85 // Sometimes want3D is used to indicate that the non-extended style should have
86 // WS_BORDER.
87 bool want3D = TRUE;
88
89 // if no extended style given, determine it ourselves
90 if ( exstyle == (WXDWORD)-1 )
91 {
92 exstyle = GetExStyle(style, &want3D);
93 }
94
95 // all controls have these styles (wxWindows creates all controls visible
96 // by default)
97 style |= WS_CHILD | WS_VISIBLE;
98
99 m_hWnd = (WXHWND)::CreateWindowEx
100 (
101 exstyle, // extended style
102 classname, // the kind of control to create
103 label, // the window name
104 style, // the window style
105 pos.x, pos.y, // the window position
106 size.x, size.y, // and size
107 GetHwndOf(GetParent()), // parent
108 (HMENU)GetId(), // child id
109 wxGetInstance(), // app instance
110 NULL // creation parameters
111 );
112
113 if ( !m_hWnd )
114 {
115 wxLogDebug(wxT("Failed to create a control of class '%s'"), classname);
116 wxFAIL_MSG(_T("something is very wrong"));
117
118 return FALSE;
119 }
120
121 #if wxUSE_CTL3D
122 if ( want3D )
123 {
124 Ctl3dSubclassCtl(GetHwnd());
125 m_useCtl3D = TRUE;
126 }
127 #endif // wxUSE_CTL3D
128
129 // subclass again for purposes of dialog editing mode
130 SubclassWin(m_hWnd);
131
132 // controls use the same font and colours as their parent dialog by default
133 InheritAttributes();
134
135 return TRUE;
136 }
137
138 wxSize wxControl::DoGetBestSize() const
139 {
140 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
141 }
142
143 bool wxControl::ProcessCommand(wxCommandEvent& event)
144 {
145 #if WXWIN_COMPATIBILITY
146 if ( m_callback )
147 {
148 (void)(*m_callback)(*this, event);
149
150 return TRUE;
151 }
152 else
153 #endif // WXWIN_COMPATIBILITY
154
155 return GetEventHandler()->ProcessEvent(event);
156 }
157
158 #ifdef __WIN95__
159 bool wxControl::MSWOnNotify(int idCtrl,
160 WXLPARAM lParam,
161 WXLPARAM* result)
162 {
163 wxCommandEvent event(wxEVT_NULL, m_windowId);
164 wxEventType eventType = wxEVT_NULL;
165 NMHDR *hdr1 = (NMHDR*) lParam;
166 switch ( hdr1->code )
167 {
168 case NM_CLICK:
169 eventType = wxEVT_COMMAND_LEFT_CLICK;
170 break;
171
172 case NM_DBLCLK:
173 eventType = wxEVT_COMMAND_LEFT_DCLICK;
174 break;
175
176 case NM_RCLICK:
177 eventType = wxEVT_COMMAND_RIGHT_CLICK;
178 break;
179
180 case NM_RDBLCLK:
181 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
182 break;
183
184 case NM_SETFOCUS:
185 eventType = wxEVT_COMMAND_SET_FOCUS;
186 break;
187
188 case NM_KILLFOCUS:
189 eventType = wxEVT_COMMAND_KILL_FOCUS;
190 break;
191
192 case NM_RETURN:
193 eventType = wxEVT_COMMAND_ENTER;
194 break;
195
196 default:
197 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
198 }
199
200 event.SetEventType(eventType);
201 event.SetEventObject(this);
202
203 return GetEventHandler()->ProcessEvent(event);
204 }
205 #endif // Win95
206
207 void wxControl::OnEraseBackground(wxEraseEvent& event)
208 {
209 // notice that this 'dumb' implementation may cause flicker for some of the
210 // controls in which case they should intercept wxEraseEvent and process it
211 // themselves somehow
212
213 RECT rect;
214 ::GetClientRect(GetHwnd(), &rect);
215
216 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
217
218 HDC hdc = GetHdcOf((*event.GetDC()));
219 int mode = ::SetMapMode(hdc, MM_TEXT);
220
221 ::FillRect(hdc, &rect, hBrush);
222 ::DeleteObject(hBrush);
223 ::SetMapMode(hdc, mode);
224 }
225
226 WXHBRUSH wxControl::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
227 WXUINT message,
228 WXWPARAM wParam,
229 WXLPARAM lParam)
230 {
231 #if wxUSE_CTL3D
232 if ( m_useCtl3D )
233 {
234 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
235 return (WXHBRUSH) hbrush;
236 }
237 #endif // wxUSE_CTL3D
238
239 HDC hdc = (HDC)pDC;
240 if (GetParent()->GetTransparentBackground())
241 SetBkMode(hdc, TRANSPARENT);
242 else
243 SetBkMode(hdc, OPAQUE);
244
245 wxColour colBack = GetBackgroundColour();
246
247 ::SetBkColor(hdc, wxColourToRGB(colBack));
248 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
249
250 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
251
252 return (WXHBRUSH)brush->GetResourceHandle();
253 }
254
255 WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
256 {
257 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
258
259 // Even with extended styles, need to combine with WS_BORDER for them to
260 // look right.
261 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
262 style |= WS_BORDER;
263
264 return exStyle;
265 }
266
267 // ---------------------------------------------------------------------------
268 // global functions
269 // ---------------------------------------------------------------------------
270
271 // Call this repeatedly for several wnds to find the overall size
272 // of the widget.
273 // Call it initially with -1 for all values in rect.
274 // Keep calling for other widgets, and rect will be modified
275 // to calculate largest bounding rectangle.
276 void wxFindMaxSize(WXHWND wnd, RECT *rect)
277 {
278 int left = rect->left;
279 int right = rect->right;
280 int top = rect->top;
281 int bottom = rect->bottom;
282
283 GetWindowRect((HWND) wnd, rect);
284
285 if (left < 0)
286 return;
287
288 if (left < rect->left)
289 rect->left = left;
290
291 if (right > rect->right)
292 rect->right = right;
293
294 if (top < rect->top)
295 rect->top = top;
296
297 if (bottom > rect->bottom)
298 rect->bottom = bottom;
299 }
300