]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/private.h
added WinStruct<>
[wxWidgets.git] / include / wx / msw / private.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: private.h
3 // Purpose: Private declarations: as this header is only included by
4 // wxWindows itself, it may contain identifiers which don't start
5 // with "wx".
6 // Author: Julian Smart
7 // Modified by:
8 // Created: 01/02/97
9 // RCS-ID: $Id$
10 // Copyright: (c) Julian Smart
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #ifndef _WX_PRIVATE_H_
15 #define _WX_PRIVATE_H_
16
17 #ifndef STRICT
18 #define STRICT 1
19 #endif
20
21 #include <windows.h>
22
23
24 #if defined (__WXWINCE__)
25 #include <wingdi.h> // RGB, COLORREF
26 #include <winuser.h> // Global Namespaces ::GetKeyState, ::GetWindowRect
27 #endif
28
29
30 #ifdef __WXMICROWIN__
31 // Extra prototypes and symbols not defined by MicroWindows
32 #include "wx/msw/microwin.h"
33 #endif
34
35 // undefine conflicting symbols which were defined in windows.h
36 #include "wx/msw/winundef.h"
37
38 // Include fixes for MSLU:
39 #include "wx/msw/mslu.h"
40
41 #include "wx/log.h"
42
43 class WXDLLEXPORT wxFont;
44 class WXDLLEXPORT wxWindow;
45
46 // ---------------------------------------------------------------------------
47 // private constants
48 // ---------------------------------------------------------------------------
49
50 // Conversion
51 static const double METRIC_CONVERSION_CONSTANT = 0.0393700787;
52
53 // Scaling factors for various unit conversions
54 static const double mm2inches = (METRIC_CONVERSION_CONSTANT);
55 static const double inches2mm = (1/METRIC_CONVERSION_CONSTANT);
56
57 static const double mm2twips = (METRIC_CONVERSION_CONSTANT*1440);
58 static const double twips2mm = (1/(METRIC_CONVERSION_CONSTANT*1440));
59
60 static const double mm2pt = (METRIC_CONVERSION_CONSTANT*72);
61 static const double pt2mm = (1/(METRIC_CONVERSION_CONSTANT*72));
62
63 // ---------------------------------------------------------------------------
64 // standard icons from the resources
65 // ---------------------------------------------------------------------------
66
67 #if wxUSE_GUI
68
69 WXDLLEXPORT_DATA(extern HICON) wxSTD_FRAME_ICON;
70 WXDLLEXPORT_DATA(extern HICON) wxSTD_MDIPARENTFRAME_ICON;
71 WXDLLEXPORT_DATA(extern HICON) wxSTD_MDICHILDFRAME_ICON;
72 WXDLLEXPORT_DATA(extern HICON) wxDEFAULT_FRAME_ICON;
73 WXDLLEXPORT_DATA(extern HICON) wxDEFAULT_MDIPARENTFRAME_ICON;
74 WXDLLEXPORT_DATA(extern HICON) wxDEFAULT_MDICHILDFRAME_ICON;
75 WXDLLEXPORT_DATA(extern HFONT) wxSTATUS_LINE_FONT;
76
77 #endif // wxUSE_GUI
78
79 // ---------------------------------------------------------------------------
80 // define things missing from some compilers' headers
81 // ---------------------------------------------------------------------------
82
83 #if defined(__GNUWIN32__) && !wxUSE_NORLANDER_HEADERS
84 #ifndef ZeroMemory
85 inline void ZeroMemory(void *buf, size_t len) { memset(buf, 0, len); }
86 #endif
87 #endif // old mingw32
88
89 // this defines a CASTWNDPROC macro which casts a pointer to the type of a
90 // window proc
91 #if defined(__WXWINCE__)
92 typedef FARPROC WndProcCast;
93 #elif defined(STRICT) || defined(__GNUC__)
94 typedef WNDPROC WndProcCast;
95 #else
96 typedef FARPROC WndProcCast;
97 #endif
98
99
100 #define CASTWNDPROC (WndProcCast)
101
102 // ---------------------------------------------------------------------------
103 // some stuff for old Windows versions (FIXME: what does it do here??)
104 // ---------------------------------------------------------------------------
105
106 #if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
107 #define APIENTRY FAR PASCAL
108 #endif
109
110 #ifdef __WIN32__
111 #define _EXPORT
112 #else
113 #define _EXPORT _export
114 #endif
115
116 #ifndef __WIN32__
117 typedef signed short int SHORT;
118 #endif
119
120 #if !defined(__WIN32__) // 3.x uses FARPROC for dialogs
121 #ifndef STRICT
122 #define DLGPROC FARPROC
123 #endif
124 #endif
125
126 #if wxUSE_PENWIN
127 WXDLLEXPORT void wxRegisterPenWin();
128 WXDLLEXPORT void wxCleanUpPenWin();
129 WXDLLEXPORT void wxEnablePenAppHooks (bool hook);
130 #endif // wxUSE_PENWIN
131
132 #if wxUSE_ITSY_BITSY
133 #define IBS_HORZCAPTION 0x4000L
134 #define IBS_VERTCAPTION 0x8000L
135
136 UINT WINAPI ibGetCaptionSize( HWND hWnd ) ;
137 UINT WINAPI ibSetCaptionSize( HWND hWnd, UINT nSize ) ;
138 LRESULT WINAPI ibDefWindowProc( HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam ) ;
139 VOID WINAPI ibAdjustWindowRect( HWND hWnd, LPRECT lprc ) ;
140 #endif // wxUSE_ITSY_BITSY
141
142 #if wxUSE_CTL3D
143 #include "wx/msw/ctl3d/ctl3d.h"
144 #endif // wxUSE_CTL3D
145
146 /*
147 * Decide what window classes we're going to use
148 * for this combination of CTl3D/FAFA settings
149 */
150
151 #define STATIC_CLASS wxT("STATIC")
152 #define STATIC_FLAGS (SS_LEFT|WS_CHILD|WS_VISIBLE)
153 #define CHECK_CLASS wxT("BUTTON")
154 #define CHECK_FLAGS (BS_AUTOCHECKBOX|WS_TABSTOP|WS_CHILD)
155 #define CHECK_IS_FAFA FALSE
156 #define RADIO_CLASS wxT("BUTTON")
157 #define RADIO_FLAGS (BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE)
158 #define RADIO_SIZE 20
159 #define RADIO_IS_FAFA FALSE
160 #define PURE_WINDOWS
161 #define GROUP_CLASS wxT("BUTTON")
162 #define GROUP_FLAGS (BS_GROUPBOX|WS_CHILD|WS_VISIBLE)
163
164 /*
165 #define BITCHECK_FLAGS (FB_BITMAP|FC_BUTTONDRAW|FC_DEFAULT|WS_VISIBLE)
166 #define BITRADIO_FLAGS (FC_BUTTONDRAW|FB_BITMAP|FC_RADIO|WS_CHILD|WS_VISIBLE)
167 */
168
169 // ---------------------------------------------------------------------------
170 // misc macros
171 // ---------------------------------------------------------------------------
172
173 #define MEANING_CHARACTER '0'
174 #define DEFAULT_ITEM_WIDTH 100
175 #define DEFAULT_ITEM_HEIGHT 80
176
177 // Scale font to get edit control height
178 //#define EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) (3*(cy)/2)
179 #define EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) (cy+8)
180
181 // Generic subclass proc, for panel item moving/sizing and intercept
182 // EDIT control VK_RETURN messages
183 extern LONG APIENTRY _EXPORT
184 wxSubclassedGenericControlProc(WXHWND hWnd, WXUINT message, WXWPARAM wParam, WXLPARAM lParam);
185
186 // ---------------------------------------------------------------------------
187 // useful macros and functions
188 // ---------------------------------------------------------------------------
189
190 // a wrapper macro for ZeroMemory()
191 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
192 #define wxZeroMemory(obj) ::ZeroMemory(&obj, sizeof(obj))
193 #else
194 #define wxZeroMemory(obj) memset((void*) & obj, 0, sizeof(obj))
195 #endif
196
197 #if wxUSE_GUI
198
199 #include <wx/gdicmn.h>
200 #include <wx/colour.h>
201
202 // make conversion from wxColour and COLORREF a bit less painful
203 inline COLORREF wxColourToRGB(const wxColour& c)
204 {
205 return RGB(c.Red(), c.Green(), c.Blue());
206 }
207
208 inline COLORREF wxColourToPalRGB(const wxColour& c)
209 {
210 return PALETTERGB(c.Red(), c.Green(), c.Blue());
211 }
212
213 inline wxColour wxRGBToColour(COLORREF rgb)
214 {
215 return wxColour(GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));
216 }
217
218 inline void wxRGBToColour(wxColour& c, COLORREF rgb)
219 {
220 c.Set(GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));
221 }
222
223 // get the standard colour map for some standard colours - see comment in this
224 // function to understand why is it needed and when should it be used
225 //
226 // it returns a wxCOLORMAP (can't use COLORMAP itself here as comctl32.dll
227 // might be not included/available) array of size wxSTD_COLOUR_MAX
228 //
229 // NB: if you change these colours, update wxBITMAP_STD_COLOURS in the
230 // resources as well: it must have the same number of pixels!
231 enum wxSTD_COLOUR
232 {
233 wxSTD_COL_BTNTEXT,
234 wxSTD_COL_BTNSHADOW,
235 wxSTD_COL_BTNFACE,
236 wxSTD_COL_BTNHIGHLIGHT,
237 wxSTD_COL_MAX,
238 };
239
240 struct WXDLLEXPORT wxCOLORMAP
241 {
242 COLORREF from, to;
243 };
244
245 // this function is implemented in src/msw/window.cpp
246 extern wxCOLORMAP *wxGetStdColourMap();
247
248 // copy Windows RECT to our wxRect
249 inline void wxCopyRECTToRect(const RECT& r, wxRect& rect)
250 {
251 rect.y = r.top;
252 rect.x = r.left;
253 rect.width = r.right - r.left;
254 rect.height = r.bottom - r.top;
255 }
256
257 // translations between HIMETRIC units (which OLE likes) and pixels (which are
258 // liked by all the others) - implemented in msw/utilsexc.cpp
259 extern void HIMETRICToPixel(LONG *x, LONG *y);
260 extern void PixelToHIMETRIC(LONG *x, LONG *y);
261
262 // Windows convention of the mask is opposed to the wxWindows one, so we need
263 // to invert the mask each time we pass one/get one to/from Windows
264 extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w = 0, int h = 0);
265
266 // Creates an icon or cursor depending from a bitmap
267 //
268 // The bitmap must be valid and it should have a mask. If it doesn't, a default
269 // mask is created using light grey as the transparent colour.
270 extern HICON wxBitmapToHICON(const wxBitmap& bmp);
271
272 // Same requirments as above apply and the bitmap must also have the correct
273 // size.
274 extern
275 HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY);
276
277 // get (x, y) from DWORD - notice that HI/LOWORD can *not* be used because they
278 // will fail on system with multiple monitors where the coords may be negative
279 //
280 // these macros are standard now (Win98) but some older headers don't have them
281 #ifndef GET_X_LPARAM
282 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
283 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
284 #endif // GET_X_LPARAM
285
286 // get the current state of SHIFT/CTRL keys
287 inline bool wxIsShiftDown()
288 {
289 return (::GetKeyState(VK_SHIFT) & 0x100) != 0;
290 }
291
292 inline bool wxIsCtrlDown()
293 {
294 return (::GetKeyState(VK_CONTROL) & 0x100) != 0;
295 }
296
297 // wrapper around GetWindowRect() and GetClientRect() APIs doing error checking
298 // for Win32
299 inline RECT wxGetWindowRect(HWND hwnd)
300 {
301 RECT rect;
302 #ifdef __WIN16__
303 ::GetWindowRect(hwnd, &rect);
304 #else // Win32
305 if ( !::GetWindowRect(hwnd, &rect) )
306 {
307 wxLogLastError(_T("GetWindowRect"));
308 }
309 #endif // Win16/32
310
311 return rect;
312 }
313
314 inline RECT wxGetClientRect(HWND hwnd)
315 {
316 RECT rect;
317 #ifdef __WIN16__
318 ::GetClientRect(hwnd, &rect);
319 #else // Win32
320 if ( !::GetClientRect(hwnd, &rect) )
321 {
322 wxLogLastError(_T("GetClientRect"));
323 }
324 #endif // Win16/32
325
326 return rect;
327 }
328
329 // ---------------------------------------------------------------------------
330 // small helper classes
331 // ---------------------------------------------------------------------------
332
333 // a template to make initializing Windows styructs less painful: it zeroes all
334 // the struct fields and also sets cbSize member to the correct value (and so
335 // can be only used with structures which have this member...)
336 template <class T>
337 struct WinStruct : public T
338 {
339 WinStruct()
340 {
341 ::ZeroMemory(this, cbSize = sizeof(T));
342 }
343 };
344
345
346 // create an instance of this class and use it as the HDC for screen, will
347 // automatically release the DC going out of scope
348 class ScreenHDC
349 {
350 public:
351 ScreenHDC() { m_hdc = ::GetDC(NULL); }
352 ~ScreenHDC() { ::ReleaseDC(NULL, m_hdc); }
353
354 operator HDC() const { return m_hdc; }
355
356 private:
357 HDC m_hdc;
358
359 DECLARE_NO_COPY_CLASS(ScreenHDC)
360 };
361
362 // the same as ScreenHDC but for memory DCs: creates the HDC compatible with
363 // the given one (screen by default) in ctor and destroys it in dtor
364 class MemoryHDC
365 {
366 public:
367 MemoryHDC(HDC hdc = 0) { m_hdc = ::CreateCompatibleDC(hdc); }
368 ~MemoryHDC() { ::DeleteDC(m_hdc); }
369
370 operator HDC() const { return m_hdc; }
371
372 private:
373 HDC m_hdc;
374
375 DECLARE_NO_COPY_CLASS(MemoryHDC)
376 };
377
378 // a class which selects a GDI object into a DC in its ctor and deselects in
379 // dtor
380 class SelectInHDC
381 {
382 public:
383 SelectInHDC(HDC hdc, HGDIOBJ hgdiobj) : m_hdc(hdc)
384 { m_hgdiobj = ::SelectObject(hdc, hgdiobj); }
385
386 ~SelectInHDC() { ::SelectObject(m_hdc, m_hgdiobj); }
387
388 // return true if the object was successfully selected
389 operator bool() const { return m_hgdiobj != 0; }
390
391 private:
392 HDC m_hdc;
393 HGDIOBJ m_hgdiobj;
394
395 DECLARE_NO_COPY_CLASS(SelectInHDC)
396 };
397
398 // when working with global pointers (which is unfortunately still necessary
399 // sometimes, e.g. for clipboard) it is important to unlock them exactly as
400 // many times as we lock them which just asks for using a "smart lock" class
401 class GlobalPtr
402 {
403 public:
404 GlobalPtr(HGLOBAL hGlobal) : m_hGlobal(hGlobal)
405 {
406 m_ptr = ::GlobalLock(hGlobal);
407 if ( !m_ptr )
408 {
409 wxLogLastError(_T("GlobalLock"));
410 }
411 }
412
413 ~GlobalPtr()
414 {
415 if ( !::GlobalUnlock(m_hGlobal) )
416 {
417 #ifdef __WXDEBUG__
418 // this might happen simply because the block became unlocked
419 DWORD dwLastError = ::GetLastError();
420 if ( dwLastError != NO_ERROR )
421 {
422 wxLogApiError(_T("GlobalUnlock"), dwLastError);
423 }
424 #endif // __WXDEBUG__
425 }
426 }
427
428 operator void *() const { return m_ptr; }
429
430 private:
431 HGLOBAL m_hGlobal;
432 void *m_ptr;
433
434 DECLARE_NO_COPY_CLASS(GlobalPtr)
435 };
436
437 // ---------------------------------------------------------------------------
438 // macros to make casting between WXFOO and FOO a bit easier: the GetFoo()
439 // returns Foo cast to the Windows type for oruselves, while GetFooOf() takes
440 // an argument which should be a pointer or reference to the object of the
441 // corresponding class (this depends on the macro)
442 // ---------------------------------------------------------------------------
443
444 #define GetHwnd() ((HWND)GetHWND())
445 #define GetHwndOf(win) ((HWND)((win)->GetHWND()))
446 // old name
447 #define GetWinHwnd GetHwndOf
448
449 #define GetHdc() ((HDC)GetHDC())
450 #define GetHdcOf(dc) ((HDC)(dc).GetHDC())
451
452 #define GetHbitmap() ((HBITMAP)GetHBITMAP())
453 #define GetHbitmapOf(bmp) ((HBITMAP)(bmp).GetHBITMAP())
454
455 #define GetHicon() ((HICON)GetHICON())
456 #define GetHiconOf(icon) ((HICON)(icon).GetHICON())
457
458 #define GetHaccel() ((HACCEL)GetHACCEL())
459 #define GetHaccelOf(table) ((HACCEL)((table).GetHACCEL()))
460
461 #define GetHmenu() ((HMENU)GetHMenu())
462 #define GetHmenuOf(menu) ((HMENU)menu->GetHMenu())
463
464 #define GetHcursor() ((HCURSOR)GetHCURSOR())
465 #define GetHcursorOf(cursor) ((HCURSOR)(cursor).GetHCURSOR())
466
467 #define GetHfont() ((HFONT)GetHFONT())
468 #define GetHfontOf(font) ((HFONT)(font).GetHFONT())
469
470 #define GetHpalette() ((HPALETTE)GetHPALETTE())
471 #define GetHpaletteOf(pal) ((HPALETTE)(pal).GetHPALETTE())
472
473 #define GetHrgn() ((HRGN)GetHRGN())
474 #define GetHrgnOf(rgn) ((HRGN)(rgn).GetHRGN())
475
476 #endif // wxUSE_GUI
477
478 // ---------------------------------------------------------------------------
479 // global data
480 // ---------------------------------------------------------------------------
481
482 WXDLLEXPORT_DATA(extern wxChar*) wxBuffer;
483
484 WXDLLEXPORT_DATA(extern HINSTANCE) wxhInstance;
485
486 // ---------------------------------------------------------------------------
487 // global functions
488 // ---------------------------------------------------------------------------
489
490 extern "C"
491 {
492 WXDLLEXPORT HINSTANCE wxGetInstance();
493 }
494
495 WXDLLEXPORT void wxSetInstance(HINSTANCE hInst);
496
497 #if wxUSE_GUI
498
499 // cursor stuff
500 extern HCURSOR wxGetCurrentBusyCursor(); // from msw/utils.cpp
501 extern const wxCursor *wxGetGlobalCursor(); // from msw/cursor.cpp
502
503 WXDLLEXPORT void wxGetCharSize(WXHWND wnd, int *x, int *y, const wxFont *the_font);
504 WXDLLEXPORT void wxFillLogFont(LOGFONT *logFont, const wxFont *font);
505 WXDLLEXPORT wxFont wxCreateFontFromLogFont(const LOGFONT *logFont);
506 WXDLLEXPORT wxFontEncoding wxGetFontEncFromCharSet(int charset);
507
508 WXDLLEXPORT void wxSliderEvent(WXHWND control, WXWORD wParam, WXWORD pos);
509 WXDLLEXPORT void wxScrollBarEvent(WXHWND hbar, WXWORD wParam, WXWORD pos);
510
511 // Find maximum size of window/rectangle
512 WXDLLEXPORT extern void wxFindMaxSize(WXHWND hwnd, RECT *rect);
513
514 // Safely get the window text (i.e. without using fixed size buffer)
515 WXDLLEXPORT extern wxString wxGetWindowText(WXHWND hWnd);
516
517 // get the window class name
518 WXDLLEXPORT extern wxString wxGetWindowClass(WXHWND hWnd);
519
520 // get the window id (should be unsigned, hence this is not wxWindowID which
521 // is, for mainly historical reasons, signed)
522 WXDLLEXPORT extern WXWORD wxGetWindowId(WXHWND hWnd);
523
524 // check if hWnd's WNDPROC is wndProc. Return true if yes, false if they are
525 // different
526 WXDLLEXPORT extern bool wxCheckWindowWndProc(WXHWND hWnd, WXFARPROC wndProc);
527
528 // Does this window style specify any border?
529 inline bool wxStyleHasBorder(long style)
530 {
531 return (style & (wxSIMPLE_BORDER | wxRAISED_BORDER |
532 wxSUNKEN_BORDER | wxDOUBLE_BORDER)) != 0;
533 }
534
535 // ----------------------------------------------------------------------------
536 // functions mapping HWND to wxWindow
537 // ----------------------------------------------------------------------------
538
539 // this function simply checks whether the given hWnd corresponds to a wxWindow
540 // and returns either that window if it does or NULL otherwise
541 WXDLLEXPORT extern wxWindow* wxFindWinFromHandle(WXHWND hWnd);
542
543 // find the window for HWND which is part of some wxWindow, i.e. unlike
544 // wxFindWinFromHandle() above it will also work for "sub controls" of a
545 // wxWindow.
546 //
547 // returns the wxWindow corresponding to the given HWND or NULL.
548 WXDLLEXPORT extern wxWindow *wxGetWindowFromHWND(WXHWND hwnd);
549
550
551 // Get the size of an icon
552 WXDLLEXPORT extern wxSize wxGetHiconSize(HICON hicon);
553
554 #endif // wxUSE_GUI
555
556 #endif
557 // _WX_PRIVATE_H_