blob: 5f0ab5a0f36ff3b9297365c03873316f7ee5ef3f [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Win32Util.h
20
21// Miscellaneous but useful Win32 API utility functions & classes.
22// In particular, a set of classes which wrap GDI objects,
23// and some to handle palettes.
24
25#ifndef __RFB_WIN32_GDIUTIL_H__
26#define __RFB_WIN32_GDIUTIL_H__
27
28#include <rfb/ColourMap.h>
29#include <rfb/PixelFormat.h>
30#include <rfb/Rect.h>
31#include <rfb_win32/TCharArray.h>
32
33namespace rfb {
34
35 namespace win32 {
36
37 class LogicalPalette {
38 public:
39 LogicalPalette();
40 ~LogicalPalette();
41 void setEntries(int start, int count, const Colour* cols);
42 HPALETTE getHandle() {return palette;}
43 protected:
44 HPALETTE palette;
45 int numEntries;
46 };
47
48 class DeviceContext {
49 public:
50 DeviceContext() : dc(0) {}
51 virtual ~DeviceContext() {}
52 operator HDC() const {return dc;}
53 PixelFormat getPF() const;
54 static PixelFormat getPF(HDC dc);
55 protected:
56 HDC dc;
57 };
58
59 class WindowDC : public DeviceContext {
60 public:
61 WindowDC(HWND wnd);
62 virtual ~WindowDC();
63 protected:
64 HWND hwnd;
65 };
66
67 class CompatibleDC : public DeviceContext {
68 public:
69 CompatibleDC(HDC existing);
70 virtual ~CompatibleDC();
71 };
72
73 class BitmapDC : public CompatibleDC {
74 public:
75 BitmapDC(HDC hdc, HBITMAP hbitmap);
76 ~BitmapDC();
77 protected:
78 HBITMAP oldBitmap;
79 };
80
81 class CompatibleBitmap {
82 public:
83 CompatibleBitmap(HDC hdc, int width, int height);
84 virtual ~CompatibleBitmap();
85 operator HBITMAP() const {return hbmp;}
86 protected:
87 HBITMAP hbmp;
88 };
89
90 struct BitmapInfo {
91 BITMAPINFOHEADER bmiHeader;
92 union {
93 struct {
94 DWORD red;
95 DWORD green;
96 DWORD blue;
97 } mask;
98 RGBQUAD color[256];
99 };
100 };
101
102 inline void initMaxAndShift(DWORD mask, int* max, int* shift) {
103 for ((*shift) = 0; (mask & 1) == 0; (*shift)++) mask >>= 1;
104 (*max) = (rdr::U16)mask;
105 }
106
107 class PaletteSelector {
108 public:
109 PaletteSelector(HDC dc, HPALETTE pal);
110 ~PaletteSelector();
111 bool isRedrawRequired() {return redrawRequired;}
112 protected:
113 HPALETTE oldPal;
114 HDC device;
115 bool redrawRequired;
116 };
117
118 struct IconInfo : public ICONINFO {
119 IconInfo(HICON icon);
120 ~IconInfo();
121 };
122
123 struct ModuleFileName : public TCharArray {
124 ModuleFileName(HMODULE module=0);
125 };
126
127 struct FileVersionInfo : public TCharArray {
128 FileVersionInfo(const TCHAR* filename=0);
129 const TCHAR* getVerString(const TCHAR* name, DWORD langId = 0x080904b0);
130 };
131
132 bool splitPath(const TCHAR* path, TCHAR** dir, TCHAR** file);
133
134 class DynamicFnBase {
135 public:
136 DynamicFnBase(const TCHAR* dllName, const char* fnName);
137 ~DynamicFnBase();
138 bool isValid() const {return fnPtr != 0;}
139 protected:
140 void* fnPtr;
141 HMODULE dllHandle;
142 };
143
144 template<class T> class DynamicFn : public DynamicFnBase {
145 public:
146 DynamicFn(const TCHAR* dllName, const char* fnName) : DynamicFnBase(dllName, fnName) {}
147 T operator *() const {return (T)fnPtr;};
148 };
149
150 // Structure containing info on the monitor nearest the window.
151 // Copes with multi-monitor OSes and older ones.
152#if (WINVER >= 0x0500)
153 struct MonitorInfo : MONITORINFOEXA {
154 MonitorInfo(HWND hwnd);
155 };
156#else
157 struct MonitorInfo {
158 MonitorInfo(HWND hwnd);
159 DWORD cbSize;
160 RECT rcMonitor;
161 RECT rcWork;
162 DWORD dwFlags;
163 char szDevice[1]; // Always null...
164 };
165#endif
166 void moveToMonitor(HWND handle, const char* device);
167
168 class Handle {
169 public:
170 Handle(HANDLE h_=0) : h(h_) {}
171 ~Handle() {
172 if (h) CloseHandle(h);
173 }
174 operator HANDLE() {return h;}
175 HANDLE h;
176 };
177
178 // Center the window to a rectangle, or to a parent window.
179 // Optionally, resize the window to lay within the rect or parent window
180 // If the parent window is NULL then the working area if the window's
181 // current monitor is used instead.
182 void centerWindow(HWND handle, const RECT& r, bool clipToRect=false);
183 void centerWindow(HWND handle, HWND parent, bool clipToRect=false);
184
185 // MsgBox helper function. Define rfb::win32::AppName somewhere in your
186 // code and MsgBox will use its value in informational messages.
187 extern TStr AppName;
188 int MsgBox(HWND parent, const TCHAR* message, UINT flags);
189
190 // Get the computer name
191 struct ComputerName : TCharArray {
192 ComputerName() : TCharArray(MAX_COMPUTERNAME_LENGTH+1) {
193 ULONG namelength = MAX_COMPUTERNAME_LENGTH+1;
194 if (!GetComputerName(buf, &namelength))
195 _tcscpy(buf, _T(""));
196 }
197 };
198
199 // Allocate and/or manage LocalAlloc memory.
200 struct LocalMem {
201 LocalMem(int size) : ptr(LocalAlloc(LMEM_FIXED, size)) {
202 if (!ptr) throw rdr::SystemException("LocalAlloc", GetLastError());
203 }
204 LocalMem(void* p) : ptr(p) {}
205 ~LocalMem() {LocalFree(ptr);}
206 operator void*() {return ptr;}
207 void* takePtr() {
208 void* t = ptr; ptr = 0; return t;
209 }
210 void* ptr;
211 };
212
213 };
214
215};
216
217#endif