]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
NO C++ COMMENTS IN THIS FILE PLEASE
[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 #endif
28
29 #include "wx/control.h"
30
31 #include "wx/msw/private.h"
32
33 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
34 #include <commctrl.h>
35 #endif
36
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
39
40 BEGIN_EVENT_TABLE(wxControl, wxWindow)
41 EVT_ERASE_BACKGROUND(wxControl::OnEraseBackground)
42 END_EVENT_TABLE()
43 #endif
44
45 // Item members
46 wxControl::wxControl()
47 {
48 m_backgroundColour = *wxWHITE;
49 m_foregroundColour = *wxBLACK;
50
51 #if WXWIN_COMPATIBILITY
52 m_callback = 0;
53 #endif // WXWIN_COMPATIBILITY
54 }
55
56 wxControl::~wxControl()
57 {
58 m_isBeingDeleted = TRUE;
59 }
60
61 bool wxControl::MSWCreateControl(const wxChar *classname,
62 WXDWORD style,
63 const wxPoint& pos,
64 const wxSize& size,
65 const wxString& label,
66 WXDWORD exstyle)
67 {
68 // VZ: if someone could put a comment here explaining what exactly this is
69 // needed for, it would be nice...
70 bool want3D;
71
72 // if no extended style given, determine it ourselves
73 if ( exstyle == (WXDWORD)-1 )
74 {
75 exstyle = GetExStyle(style, &want3D);
76 }
77
78 // all controls have these childs (wxWindows creates all controls visible
79 // by default)
80 style |= WS_CHILD | WS_VISIBLE;
81
82 m_hWnd = (WXHWND)::CreateWindowEx
83 (
84 exstyle, // extended style
85 classname, // the kind of control to create
86 label, // the window name
87 style, // the window style
88 pos.x, pos.y, // the window position
89 size.x, size.y, // and size
90 GetHwndOf(GetParent()), // parent
91 (HMENU)GetId(), // child id
92 wxGetInstance(), // app instance
93 NULL // creation parameters
94 );
95
96 if ( !m_hWnd )
97 {
98 #ifdef __WXDEBUG__
99 wxLogError(wxT("Failed to create a control of class '%s'"), classname);
100 #endif // DEBUG
101
102 return FALSE;
103 }
104
105 #if wxUSE_CTL3D
106 if ( want3D )
107 {
108 Ctl3dSubclassCtl(GetHwnd());
109 m_useCtl3D = TRUE;
110 }
111 #endif // wxUSE_CTL3D
112
113 // subclass again for purposes of dialog editing mode
114 SubclassWin(m_hWnd);
115
116 // controls use the same font and colours as their parent dialog by default
117 InheritAttributes();
118
119 return TRUE;
120 }
121
122 wxSize wxControl::DoGetBestSize()
123 {
124 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
125 }
126
127 bool wxControl::ProcessCommand(wxCommandEvent& event)
128 {
129 #if WXWIN_COMPATIBILITY
130 if ( m_callback )
131 {
132 (void)(*m_callback)(this, event);
133
134 return TRUE;
135 }
136 else
137 #endif // WXWIN_COMPATIBILITY
138
139 return GetEventHandler()->ProcessEvent(event);
140 }
141
142 #ifdef __WIN95__
143 bool wxControl::MSWOnNotify(int idCtrl,
144 WXLPARAM lParam,
145 WXLPARAM* result)
146 {
147 wxCommandEvent event(wxEVT_NULL, m_windowId);
148 wxEventType eventType = wxEVT_NULL;
149 NMHDR *hdr1 = (NMHDR*) lParam;
150 switch ( hdr1->code )
151 {
152 case NM_CLICK:
153 eventType = wxEVT_COMMAND_LEFT_CLICK;
154 break;
155
156 case NM_DBLCLK:
157 eventType = wxEVT_COMMAND_LEFT_DCLICK;
158 break;
159
160 case NM_RCLICK:
161 eventType = wxEVT_COMMAND_RIGHT_CLICK;
162 break;
163
164 case NM_RDBLCLK:
165 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
166 break;
167
168 case NM_SETFOCUS:
169 eventType = wxEVT_COMMAND_SET_FOCUS;
170 break;
171
172 case NM_KILLFOCUS:
173 eventType = wxEVT_COMMAND_KILL_FOCUS;
174 break;
175
176 case NM_RETURN:
177 eventType = wxEVT_COMMAND_ENTER;
178 break;
179
180 default:
181 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
182 }
183
184 event.SetEventType(eventType);
185 event.SetEventObject(this);
186
187 return GetEventHandler()->ProcessEvent(event);
188 }
189 #endif // Win95
190
191 void wxControl::OnEraseBackground(wxEraseEvent& event)
192 {
193 // In general, you don't want to erase the background of a control,
194 // or you'll get a flicker.
195 // TODO: move this 'null' function into each control that
196 // might flicker.
197
198 RECT rect;
199 ::GetClientRect(GetHwnd(), &rect);
200
201 HBRUSH hBrush = ::CreateSolidBrush(wxColourToRGB(GetBackgroundColour()));
202
203 HDC hdc = GetHdcOf((*event.GetDC()));
204 int mode = ::SetMapMode(hdc, MM_TEXT);
205
206 ::FillRect(hdc, &rect, hBrush);
207 ::DeleteObject(hBrush);
208 ::SetMapMode(hdc, mode);
209 }
210
211 WXDWORD wxControl::GetExStyle(WXDWORD& style, bool *want3D) const
212 {
213 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, want3D);
214
215 // Even with extended styles, need to combine with WS_BORDER for them to
216 // look right.
217 if ( *want3D || wxStyleHasBorder(m_windowStyle) )
218 style |= WS_BORDER;
219
220 return exStyle;
221 }
222
223 // ---------------------------------------------------------------------------
224 // global functions
225 // ---------------------------------------------------------------------------
226
227 // Call this repeatedly for several wnds to find the overall size
228 // of the widget.
229 // Call it initially with -1 for all values in rect.
230 // Keep calling for other widgets, and rect will be modified
231 // to calculate largest bounding rectangle.
232 void wxFindMaxSize(WXHWND wnd, RECT *rect)
233 {
234 int left = rect->left;
235 int right = rect->right;
236 int top = rect->top;
237 int bottom = rect->bottom;
238
239 GetWindowRect((HWND) wnd, rect);
240
241 if (left < 0)
242 return;
243
244 if (left < rect->left)
245 rect->left = left;
246
247 if (right > rect->right)
248 rect->right = right;
249
250 if (top < rect->top)
251 rect->top = top;
252
253 if (bottom > rect->bottom)
254 rect->bottom = bottom;
255 }
256