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