[a76d99]: win32 / GUIWin.cxx  Maximize  Restore  History

Download this file

306 lines (272 with data), 7.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SciTE - Scintilla based Text Editor
/** @file GUIWin.cxx
** Interface to platform GUI facilities.
** Split off from Scintilla's Platform.h to avoid SciTE depending on implementation of Scintilla.
**/
// Copyright 1998-2010 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <stdio.h>
#include <time.h>
#include <string>
#include <vector>
#ifdef __MINGW_H
#define _WIN32_IE 0x0400
#endif
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#ifdef _MSC_VER
// windows.h, et al, use a lot of nameless struct/unions - can't fix it, so allow it
#pragma warning(disable: 4201)
#endif
#include <windows.h>
#ifdef _MSC_VER
// okay, that's done, don't allow it in our code
#pragma warning(default: 4201)
#pragma warning(disable: 4244)
#endif
#include "Scintilla.h"
#include "GUI.h"
namespace GUI {
enum { SURROGATE_LEAD_FIRST = 0xD800 };
enum { SURROGATE_TRAIL_FIRST = 0xDC00 };
enum { SURROGATE_TRAIL_LAST = 0xDFFF };
static unsigned int UTF8Length(const wchar_t *uptr, size_t tlen) {
unsigned int len = 0;
for (size_t i = 0; i < tlen && uptr[i];) {
unsigned int uch = uptr[i];
if (uch < 0x80) {
len++;
} else if (uch < 0x800) {
len += 2;
} else if ((uch >= SURROGATE_LEAD_FIRST) &&
(uch <= SURROGATE_TRAIL_LAST)) {
len += 4;
i++;
} else {
len += 3;
}
i++;
}
return len;
}
static void UTF8FromUTF16(const wchar_t *uptr, size_t tlen, char *putf, size_t len) {
int k = 0;
for (size_t i = 0; i < tlen && uptr[i];) {
unsigned int uch = uptr[i];
if (uch < 0x80) {
putf[k++] = static_cast<char>(uch);
} else if (uch < 0x800) {
putf[k++] = static_cast<char>(0xC0 | (uch >> 6));
putf[k++] = static_cast<char>(0x80 | (uch & 0x3f));
} else if ((uch >= SURROGATE_LEAD_FIRST) &&
(uch <= SURROGATE_TRAIL_LAST)) {
// Half a surrogate pair
i++;
unsigned int xch = 0x10000 + ((uch & 0x3ff) << 10) + (uptr[i] & 0x3ff);
putf[k++] = static_cast<char>(0xF0 | (xch >> 18));
putf[k++] = static_cast<char>(0x80 | ((xch >> 12) & 0x3f));
putf[k++] = static_cast<char>(0x80 | ((xch >> 6) & 0x3f));
putf[k++] = static_cast<char>(0x80 | (xch & 0x3f));
} else {
putf[k++] = static_cast<char>(0xE0 | (uch >> 12));
putf[k++] = static_cast<char>(0x80 | ((uch >> 6) & 0x3f));
putf[k++] = static_cast<char>(0x80 | (uch & 0x3f));
}
i++;
}
putf[len] = '\0';
}
static size_t UTF16Length(const char *s, unsigned int len) {
size_t ulen = 0;
size_t charLen;
for (size_t i=0; i<len;) {
unsigned char ch = static_cast<unsigned char>(s[i]);
if (ch < 0x80) {
charLen = 1;
} else if (ch < 0x80 + 0x40 + 0x20) {
charLen = 2;
} else if (ch < 0x80 + 0x40 + 0x20 + 0x10) {
charLen = 3;
} else {
charLen = 4;
ulen++;
}
i += charLen;
ulen++;
}
return ulen;
}
static size_t UTF16FromUTF8(const char *s, size_t len, gui_char *tbuf, size_t tlen) {
size_t ui=0;
const unsigned char *us = reinterpret_cast<const unsigned char *>(s);
size_t i=0;
while ((i<len) && (ui<tlen)) {
unsigned char ch = us[i++];
if (ch < 0x80) {
tbuf[ui] = ch;
} else if (ch < 0x80 + 0x40 + 0x20) {
tbuf[ui] = static_cast<wchar_t>((ch & 0x1F) << 6);
ch = us[i++];
tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
} else if (ch < 0x80 + 0x40 + 0x20 + 0x10) {
tbuf[ui] = static_cast<wchar_t>((ch & 0xF) << 12);
ch = us[i++];
tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + ((ch & 0x7F) << 6));
ch = us[i++];
tbuf[ui] = static_cast<wchar_t>(tbuf[ui] + (ch & 0x7F));
} else {
// Outside the BMP so need two surrogates
int val = (ch & 0x7) << 18;
ch = us[i++];
val += (ch & 0x3F) << 12;
ch = us[i++];
val += (ch & 0x3F) << 6;
ch = us[i++];
val += (ch & 0x3F);
tbuf[ui] = static_cast<wchar_t>(((val - 0x10000) >> 10) + SURROGATE_LEAD_FIRST);
ui++;
tbuf[ui] = static_cast<wchar_t>((val & 0x3ff) + SURROGATE_TRAIL_FIRST);
}
ui++;
}
return ui;
}
gui_string StringFromUTF8(const char *s) {
if (!s) {
return gui_string();
}
size_t sLen = strlen(s);
size_t wideLen = UTF16Length(s, static_cast<int>(sLen));
std::vector<gui_char> vgc(wideLen + 1);
size_t outLen = UTF16FromUTF8(s, sLen, &vgc[0], wideLen);
vgc[outLen] = 0;
return gui_string(&vgc[0], outLen);
}
std::string UTF8FromString(const gui_string &s) {
size_t sLen = s.size();
size_t narrowLen = UTF8Length(s.c_str(), sLen);
std::vector<char> vc(narrowLen + 1);
UTF8FromUTF16(s.c_str(), sLen, &vc[0], narrowLen);
return std::string(&vc[0], narrowLen);
}
gui_string StringFromInteger(long i) {
char number[32];
sprintf(number, "%0ld", i);
gui_char gnumber[32];
size_t n=0;
while (number[n]) {
gnumber[n] = static_cast<gui_char>(number[n]);
n++;
}
gnumber[n] = 0;
return gui_string(gnumber);
}
void Window::Destroy() {
if (wid)
::DestroyWindow(reinterpret_cast<HWND>(wid));
wid = 0;
}
bool Window::HasFocus() {
return ::GetFocus() == wid;
}
Rectangle Window::GetPosition() {
RECT rc;
::GetWindowRect(reinterpret_cast<HWND>(wid), &rc);
return Rectangle(rc.left, rc.top, rc.right, rc.bottom);
}
void Window::SetPosition(Rectangle rc) {
::SetWindowPos(reinterpret_cast<HWND>(wid),
0, rc.left, rc.top, rc.Width(), rc.Height(), SWP_NOZORDER|SWP_NOACTIVATE);
}
Rectangle Window::GetClientPosition() {
RECT rc={0,0,0,0};
if (wid)
::GetClientRect(reinterpret_cast<HWND>(wid), &rc);
return Rectangle(rc.left, rc.top, rc.right, rc.bottom);
}
void Window::Show(bool show) {
if (show)
::ShowWindow(reinterpret_cast<HWND>(wid), SW_SHOWNOACTIVATE);
else
::ShowWindow(reinterpret_cast<HWND>(wid), SW_HIDE);
}
void Window::InvalidateAll() {
::InvalidateRect(reinterpret_cast<HWND>(wid), NULL, FALSE);
}
void Window::SetTitle(const gui_char *s) {
::SetWindowTextW(reinterpret_cast<HWND>(wid), s);
}
void Menu::CreatePopUp() {
Destroy();
mid = ::CreatePopupMenu();
}
void Menu::Destroy() {
if (mid)
::DestroyMenu(reinterpret_cast<HMENU>(mid));
mid = 0;
}
void Menu::Show(Point pt, Window &w) {
::TrackPopupMenu(reinterpret_cast<HMENU>(mid),
0, pt.x - 4, pt.y, 0,
reinterpret_cast<HWND>(w.GetID()), NULL);
Destroy();
}
static bool initialisedET = false;
static bool usePerformanceCounter = false;
static LARGE_INTEGER frequency;
ElapsedTime::ElapsedTime() {
if (!initialisedET) {
usePerformanceCounter = ::QueryPerformanceFrequency(&frequency) != 0;
initialisedET = true;
}
if (usePerformanceCounter) {
LARGE_INTEGER timeVal;
::QueryPerformanceCounter(&timeVal);
bigBit = timeVal.HighPart;
littleBit = timeVal.LowPart;
} else {
bigBit = clock();
littleBit = 0;
}
}
double ElapsedTime::Duration(bool reset) {
double result;
long endBigBit;
long endLittleBit;
if (usePerformanceCounter) {
LARGE_INTEGER lEnd;
::QueryPerformanceCounter(&lEnd);
endBigBit = lEnd.HighPart;
endLittleBit = lEnd.LowPart;
LARGE_INTEGER lBegin;
lBegin.HighPart = bigBit;
lBegin.LowPart = littleBit;
double elapsed = lEnd.QuadPart - lBegin.QuadPart;
result = elapsed / static_cast<double>(frequency.QuadPart);
} else {
endBigBit = clock();
endLittleBit = 0;
double elapsed = endBigBit - bigBit;
result = elapsed / CLOCKS_PER_SEC;
}
if (reset) {
bigBit = endBigBit;
littleBit = endLittleBit;
}
return result;
}
sptr_t ScintillaWindow::Send(unsigned int msg, uptr_t wParam, sptr_t lParam) {
return ::SendMessage(reinterpret_cast<HWND>(GetID()), msg, wParam, lParam);
}
sptr_t ScintillaWindow::SendPointer(unsigned int msg, uptr_t wParam, void *lParam) {
return ::SendMessage(reinterpret_cast<HWND>(GetID()), msg, wParam, reinterpret_cast<LPARAM>(lParam));
}
bool IsDBCSLeadByte(int codePage, char ch) {
if (SC_CP_UTF8 == codePage)
// For lexing, all characters >= 0x80 are treated the
// same so none is considered a lead byte.
return false;
else
return ::IsDBCSLeadByteEx(codePage, ch) != 0;
}
}