]> git.saurik.com Git - wxWidgets.git/blob - src/msw/control.cpp
new sample
[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, WXDWORD style)
62 {
63 m_hWnd = (WXHWND)::CreateWindowEx
64 (
65 GetExStyle(style), // extended style
66 classname, // the kind of control to create
67 NULL, // the window name
68 style, // the window style
69 0, 0, 0, 0, // the window position and size
70 GetHwndOf(GetParent()), // parent
71 (HMENU)GetId(), // child id
72 wxGetInstance(), // app instance
73 NULL // creation parameters
74 );
75
76 if ( !m_hWnd )
77 {
78 #ifdef __WXDEBUG__
79 wxLogError(_T("Failed to create a control of class '%s'"), classname);
80 #endif // DEBUG
81
82 return FALSE;
83 }
84
85 // subclass again for purposes of dialog editing mode
86 SubclassWin(m_hWnd);
87
88 // controls use the same font and colours as their parent dialog by default
89 InheritAttributes();
90
91 return TRUE;
92 }
93
94 wxSize wxControl::DoGetBestSize()
95 {
96 return wxSize(DEFAULT_ITEM_WIDTH, DEFAULT_ITEM_HEIGHT);
97 }
98
99 bool wxControl::ProcessCommand(wxCommandEvent& event)
100 {
101 #if WXWIN_COMPATIBILITY
102 if ( m_callback )
103 {
104 (void)(*m_callback)(this, event);
105
106 return TRUE;
107 }
108 else
109 #endif // WXWIN_COMPATIBILITY
110
111 return GetEventHandler()->ProcessEvent(event);
112 }
113
114 #ifdef __WIN95__
115 bool wxControl::MSWOnNotify(int idCtrl,
116 WXLPARAM lParam,
117 WXLPARAM* result)
118 {
119 wxCommandEvent event(wxEVT_NULL, m_windowId);
120 wxEventType eventType = wxEVT_NULL;
121 NMHDR *hdr1 = (NMHDR*) lParam;
122 switch ( hdr1->code )
123 {
124 case NM_CLICK:
125 eventType = wxEVT_COMMAND_LEFT_CLICK;
126 break;
127
128 case NM_DBLCLK:
129 eventType = wxEVT_COMMAND_LEFT_DCLICK;
130 break;
131
132 case NM_RCLICK:
133 eventType = wxEVT_COMMAND_RIGHT_CLICK;
134 break;
135
136 case NM_RDBLCLK:
137 eventType = wxEVT_COMMAND_RIGHT_DCLICK;
138 break;
139
140 case NM_SETFOCUS:
141 eventType = wxEVT_COMMAND_SET_FOCUS;
142 break;
143
144 case NM_KILLFOCUS:
145 eventType = wxEVT_COMMAND_KILL_FOCUS;
146 break;
147
148 case NM_RETURN:
149 eventType = wxEVT_COMMAND_ENTER;
150 break;
151
152 default:
153 return wxWindow::MSWOnNotify(idCtrl, lParam, result);
154 }
155
156 event.SetEventType(eventType);
157 event.SetEventObject(this);
158
159 return GetEventHandler()->ProcessEvent(event);
160 }
161 #endif // Win95
162
163 void wxControl::OnEraseBackground(wxEraseEvent& event)
164 {
165 // In general, you don't want to erase the background of a control,
166 // or you'll get a flicker.
167 // TODO: move this 'null' function into each control that
168 // might flicker.
169
170 RECT rect;
171 ::GetClientRect((HWND) GetHWND(), &rect);
172
173 HBRUSH hBrush = ::CreateSolidBrush(PALETTERGB(GetBackgroundColour().Red(),
174 GetBackgroundColour().Green(),
175 GetBackgroundColour().Blue()));
176 int mode = ::SetMapMode((HDC) event.GetDC()->GetHDC(), MM_TEXT);
177
178 ::FillRect ((HDC) event.GetDC()->GetHDC(), &rect, hBrush);
179 ::DeleteObject(hBrush);
180 ::SetMapMode((HDC) event.GetDC()->GetHDC(), mode);
181 }
182
183 WXDWORD wxControl::GetExStyle(WXDWORD& style) const
184 {
185 bool want3D;
186 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
187
188 // Even with extended styles, need to combine with WS_BORDER
189 // for them to look right.
190 if ( want3D || wxStyleHasBorder(m_windowStyle) )
191 style |= WS_BORDER;
192
193 return exStyle;
194 }
195
196 // ---------------------------------------------------------------------------
197 // global functions
198 // ---------------------------------------------------------------------------
199
200 // Call this repeatedly for several wnds to find the overall size
201 // of the widget.
202 // Call it initially with -1 for all values in rect.
203 // Keep calling for other widgets, and rect will be modified
204 // to calculate largest bounding rectangle.
205 void wxFindMaxSize(WXHWND wnd, RECT *rect)
206 {
207 int left = rect->left;
208 int right = rect->right;
209 int top = rect->top;
210 int bottom = rect->bottom;
211
212 GetWindowRect((HWND) wnd, rect);
213
214 if (left < 0)
215 return;
216
217 if (left < rect->left)
218 rect->left = left;
219
220 if (right > rect->right)
221 rect->right = right;
222
223 if (top < rect->top)
224 rect->top = top;
225
226 if (bottom > rect->bottom)
227 rect->bottom = bottom;
228 }
229