-
Notifications
You must be signed in to change notification settings - Fork 1
/
sex.cpp
481 lines (431 loc) · 13.3 KB
/
sex.cpp
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
** sex.c: Sex main window stuff.
** Copyright (C) 1998, Nullsoft Inc.
** Free for noncommercial use.
** http://www.nullsoft.com/
**/
#include "stdafx.h"
#include "resource.h"
static int moved=0;
static int config_w = 300;
static int config_h = 200;
static int config_x = 50;
static int config_y = 50;
static int config_border = 4;
static COLORREF config_color = RGB(255, 255, 0);
static COLORREF config_bcolor1 = RGB(150, 150, 150);
static COLORREF config_bcolor2 = 0;
static const TCHAR app_name[] = _T("Sex");
static TCHAR text_file[MAX_PATH] = _T("");
static TCHAR ini_file[MAX_PATH] = _T("");
static void config_read();
static void read_text();
static void write_text();
static void config_write();
static HMENU hmenu_main;
static HWND hwnd_rich;
static HWND hwnd_main;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPSTR lpszCmdParam, int nCmdShow);
static BOOL InitApplication(HINSTANCE hInstance);
static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInst*/, LPSTR /*lpszCmdParam*/, int nCmdShow)
{
MSG msg;
HACCEL hAccel = AtlLoadAccelerators(IDR_ACCELERATOR1);
if (!LoadLibrary(_T("RICHED32.DLL")))
{
MessageBox(nullptr, _T("Could not load RICHED32.DLL"), nullptr, MB_OK);
return (FALSE);
}
if (!InitApplication(hInstance))
{
MessageBox(nullptr, _T("Could not initialize application"), nullptr, MB_OK);
return (FALSE);
}
if (!InitInstance(hInstance, nCmdShow))
{
MessageBox(nullptr, _T("Could not create window"), nullptr, MB_OK);
return (FALSE);
}
// message loop
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(hwnd_main,hAccel,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} // while(GetMessage...
return static_cast<int>(msg.wParam);
} // WinMain
static BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style = CS_DBLCLKS|CS_VREDRAW|CS_HREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = nullptr;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = app_name;
if (!RegisterClass(&wc)) { return FALSE; }
return TRUE;
}
static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
const DWORD style = 0;
const DWORD exStyle = WS_EX_TOOLWINDOW;
HWND hwnd = CreateWindowEx(exStyle,app_name,app_name,style,0,0,1,1, nullptr, nullptr,hInstance, nullptr);
if (!hwnd) { return FALSE; }
if (nCmdShow == SW_SHOWMAXIMIZED)
{
ShowWindow(hwnd,SW_SHOWNORMAL);
}
else
{
ShowWindow(hwnd,nCmdShow);
}
return TRUE;
}
static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
static void OnDestroy(HWND hwnd);
static void OnClose(HWND hwnd);
static void OnActivate(HWND hwnd, UINT state, HWND hwndActDeact, BOOL fMinimized);
static UINT OnNCHitTest(HWND hwnd, int x, int y);
static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
static void OnSize(HWND hwnd, UINT state, int cx, int cy);
static void OnMove(HWND hwnd, int x, int y);
static void OnPaint(HWND hwnd);
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
HANDLE_MSG(hwnd,WM_CREATE,OnCreate);
HANDLE_MSG(hwnd,WM_DESTROY,OnDestroy);
HANDLE_MSG(hwnd,WM_CLOSE,OnClose);
HANDLE_MSG(hwnd,WM_PAINT,OnPaint);
HANDLE_MSG(hwnd,WM_MOVE,OnMove);
HANDLE_MSG(hwnd,WM_SIZE,OnSize);
HANDLE_MSG(hwnd,WM_COMMAND,OnCommand);
HANDLE_MSG(hwnd,WM_ACTIVATE,OnActivate);
HANDLE_MSG(hwnd,WM_NCHITTEST,OnNCHitTest);
case WM_USER:
switch (LOWORD(lParam))
{
case WM_LBUTTONDOWN:
SetForegroundWindow(hwnd);
break;
case WM_RBUTTONUP:
{
CPoint p;
GetCursorPos(&p);
SetForegroundWindow(hwnd);
TrackPopupMenu(hmenu_main,TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,p.x,p.y,0,hwnd_main, nullptr);
}
break;
}
return 0;
default: break;
}
return (DefWindowProc(hwnd, uMsg, wParam, lParam));
}
static WNDPROC Rich_OldWndProc;
LRESULT CALLBACK Rich_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg==WM_RBUTTONUP)
{
CPoint p;
GetCursorPos(&p);
TrackPopupMenu(hmenu_main,TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,p.x,p.y,0,hwnd_main, nullptr);
}
#pragma warning(suppress:5039) //this WNDPROC is from riched32.dll
return CallWindowProc(Rich_OldWndProc,hwnd,uMsg,wParam,lParam);
}
static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
HINSTANCE hInstance = lpCreateStruct->hInstance;
hwnd_main=hwnd;
hmenu_main=AtlLoadMenu(IDR_MENU1);
hmenu_main=GetSubMenu(hmenu_main,0);
config_read();
CWindow wnd(hwnd);
wnd.ModifyStyle(WS_CAPTION, 0);
wnd.SetWindowPos(nullptr, 0,0, 0,0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_DRAWFRAME|SWP_NOACTIVATE);
wnd.SetWindowPos(nullptr, config_x, config_y, config_w, config_h, SWP_NOACTIVATE|SWP_NOZORDER);
systray_add(hwnd,1024,AtlLoadIcon(IDI_ICON1),app_name);
hwnd_rich=CreateWindowEx(WS_EX_CLIENTEDGE, _T("RichEdit"),_T(""),
WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|WS_HSCROLL|WS_VSCROLL,
config_border,config_border,config_w-config_border*2,config_h-config_border*2,
hwnd, nullptr,hInstance, nullptr);
CWindow wnd_rich{hwnd_rich};
Rich_OldWndProc = reinterpret_cast<WNDPROC>(wnd_rich.GetWindowLongPtr(GWLP_WNDPROC));
wnd_rich.SetWindowLongPtr(GWLP_WNDPROC,reinterpret_cast<LONG_PTR>(Rich_WndProc));
if (!hwnd_rich)
{
wnd.MessageBox(_T("Error creating RichEdit control"),_T("Error"),MB_OK);
return 0;
}
wnd_rich.SendMessage(EM_SETBKGNDCOLOR,FALSE,static_cast<LPARAM>(config_color));
read_text();
return 1;
}
static void OnDestroy(HWND hwnd)
{
write_text();
config_write();
systray_del(hwnd,1024);
PostQuitMessage(0);
}
static void OnClose(HWND hwnd)
{
CWindow wnd{hwnd};
wnd.SetWindowPos(HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_DRAWFRAME | SWP_NOACTIVATE);
// DestroyWindow(hwnd);
}
static void OnActivate(HWND hwnd, UINT state, HWND /*hwndActDeact*/, BOOL /*fMinimized*/)
{
if (state==WA_INACTIVE)
{
if (moved)
{
moved=0;
config_write();
}
write_text();
SetPriorityClass(GetWindowInstance(hwnd),IDLE_PRIORITY_CLASS);
}
else
{
SetPriorityClass(GetWindowInstance(hwnd),NORMAL_PRIORITY_CLASS);
}
}
static UINT OnNCHitTest(HWND hwnd, int x, int y)
{
CWindow wnd(hwnd);
CRect r;
(void)wnd.GetClientRect(&r);
CPoint p(x, y);
(void)wnd.ScreenToClient(&p);
if (p.x <= config_border && p.y <= config_border*3) { return HTTOPLEFT; }
if (p.x <= config_border*3 && p.y <= config_border) { return HTTOPLEFT; }
if (p.x >= r.right-config_border && p.y >= r.bottom-config_border*3) { return HTBOTTOMRIGHT; }
if (p.x >= r.right-config_border*3 && p.y >= r.bottom-config_border) { return HTBOTTOMRIGHT; }
if (p.x >= r.right-config_border && p.y <= config_border*3) { return HTTOPRIGHT; }
if (p.x >= r.right-config_border*3 && p.y <= config_border) { return HTTOPRIGHT; }
if (p.x <= config_border && p.y >= r.bottom-config_border*3) { return HTBOTTOMLEFT; }
if (p.x <= config_border*3 && p.y >= r.bottom-config_border) { return HTBOTTOMLEFT; }
if (p.y <= config_border) { return HTCAPTION; }
if (p.x <= config_border) { return HTLEFT; }
if (p.y >= r.bottom-config_border) { return HTBOTTOM; }
if (p.x >= r.right-config_border) { return HTRIGHT; }
return HTCLIENT;
}
static void OnCommand(HWND hwnd, int id, HWND /*hwndCtl*/, UINT /*codeNotify*/)
{
CWindow wnd{hwnd};
CWindow wnd_rich{hwnd_rich};
switch (id)
{
case IDM_ABOUT:
wnd.MessageBox(_T("Sex v0.1\nCopyright (C) 1998, Nullsoft Inc."),_T("About sex"),MB_OK);
return;
case IDM_CLOSE:
wnd.SendMessage(WM_CLOSE,0,0);
return;
case IDM_QUIT:
wnd.DestroyWindow();
return;
case IDM_FONT:
{
LOGFONT lf={0,};
CHOOSEFONT cf={sizeof(cf),hwnd,nullptr,&lf,0,
CF_EFFECTS|CF_SCREENFONTS|CF_INITTOLOGFONTSTRUCT,
0,};
CHARFORMAT fmt={sizeof(fmt),};
wnd_rich.SendMessage(EM_GETCHARFORMAT,1,reinterpret_cast<LPARAM>(&fmt));
if (fmt.dwMask & CFM_FACE)
{
StringCchCopy(lf.lfFaceName, _countof(lf.lfFaceName), fmt.szFaceName);
}
else { lf.lfFaceName[0]=0; }
if (fmt.dwMask & CFM_SIZE)
{
lf.lfHeight=fmt.yHeight/15;
}
else { lf.lfHeight=0; }
if (fmt.dwMask & CFM_COLOR)
{
cf.rgbColors=fmt.crTextColor;
}
else { cf.rgbColors=0xffffff; }
lf.lfItalic=static_cast<BYTE>((fmt.dwEffects&CFE_ITALIC)?1:0);
lf.lfWeight=(fmt.dwEffects&CFE_BOLD)?FW_BOLD:FW_NORMAL;
lf.lfUnderline=static_cast<BYTE>((fmt.dwEffects&CFE_UNDERLINE)?1:0);
lf.lfStrikeOut=static_cast<BYTE>((fmt.dwEffects&CFE_STRIKEOUT)?1:0);
lf.lfCharSet=DEFAULT_CHARSET;
lf.lfOutPrecision=OUT_DEFAULT_PRECIS;
lf.lfClipPrecision=CLIP_DEFAULT_PRECIS;
lf.lfQuality=DEFAULT_QUALITY;
lf.lfPitchAndFamily=fmt.bPitchAndFamily;
if (ChooseFont(&cf))
{
fmt.dwMask=CFM_BOLD|CFM_COLOR|CFM_ITALIC|CFM_STRIKEOUT|CFM_UNDERLINE;
if (lf.lfFaceName[0]) { fmt.dwMask|=CFM_FACE; }
if (lf.lfHeight) { fmt.dwMask|=CFM_SIZE; }
fmt.dwEffects=0;
if (lf.lfItalic) { fmt.dwEffects |= CFE_ITALIC; }
if (lf.lfUnderline) { fmt.dwEffects |= CFE_UNDERLINE; }
if (lf.lfStrikeOut) { fmt.dwEffects |= CFE_STRIKEOUT; }
if (lf.lfWeight!=FW_NORMAL) { fmt.dwEffects |= CFE_BOLD; }
fmt.yHeight = cf.iPointSize*2;
fmt.crTextColor=cf.rgbColors;
fmt.bPitchAndFamily=lf.lfPitchAndFamily;
fmt.bCharSet = lf.lfCharSet;
StringCchCopy(fmt.szFaceName, _countof(fmt.szFaceName), lf.lfFaceName);
wnd_rich.SendMessage(EM_SETCHARFORMAT,SCF_SELECTION,reinterpret_cast<LPARAM>(&fmt));
}
}
return;
case IDM_BGCOLOR:
{
static COLORREF custcolors[16];
CHOOSECOLOR cs = {0};
cs.lStructSize = sizeof(cs);
cs.hwndOwner = hwnd;
cs.hInstance = nullptr;
cs.rgbResult=config_color;
cs.lpCustColors = custcolors;
cs.Flags = CC_RGBINIT|CC_FULLOPEN;
if (ChooseColor(&cs))
{
config_color=cs.rgbResult;
config_write();
wnd_rich.SendMessage(EM_SETBKGNDCOLOR,FALSE,static_cast<LPARAM>(config_color));
}
}
}
}
static void OnSize(HWND /*hwnd*/, UINT /*state*/, int cx, int cy)
{
moved=1;
SetWindowPos(hwnd_rich, nullptr, config_border,config_border, cx-config_border*2,cy-config_border*2, SWP_NOACTIVATE|SWP_NOZORDER);
}
static void OnMove(HWND /*hwnd*/, int /*x*/, int /*y*/)
{
moved=1;
}
static void OnPaint(HWND hwnd)
{
CRect r;
CPaintDC hdc(hwnd);
CWindow wnd(hwnd);
(void)wnd.GetClientRect(&r);
HPEN hPen=CreatePen(PS_SOLID,0,config_bcolor2);
LOGBRUSH lb={BS_SOLID,config_bcolor1};
HBRUSH hBrush=CreateBrushIndirect(&lb);
HPEN hOldPen=hdc.SelectPen(hPen);
HBRUSH hOldBrush=hdc.SelectBrush(hBrush);
hdc.Rectangle(&r);
hdc.SelectPen(hOldPen);
hdc.SelectBrush(hOldBrush);
DeleteObject(hPen);
DeleteObject(hBrush);
}
static int _r_i(LPTSTR name, int def)
{
return static_cast<int>(GetPrivateProfileInt(app_name,name,def,ini_file));
}
#define RI(x) (( x ) = _r_i(#x,( x )))
static void _w_i(LPTSTR name, int d)
{
TCHAR str[120];
StringCchPrintf(str, _countof(str), _T("%d"),d);
WritePrivateProfileString(app_name,name,str,ini_file);
}
#define WI(x) _w_i(#x,( x ))
static void _r_s(LPTSTR name, LPTSTR data, DWORD mlen)
{
TCHAR buf[2048];
StringCchCopy(buf,_countof(buf), data);
GetPrivateProfileString(app_name,name,buf,data,mlen,ini_file);
}
#define RS(x) (_r_s(#x,x,_countof(x)))
static void _w_s(LPTSTR name, LPTSTR data)
{
WritePrivateProfileString(app_name,name,data,ini_file);
}
#define WS(x) (_w_s(#x,x))
static void config_read()
{
GetModuleFileName(GetModuleHandle(nullptr),ini_file,_countof(ini_file));
StringCchCopy(text_file, _countof(text_file), ini_file);
PathRenameExtension(ini_file, _T(".ini"));
PathRenameExtension(text_file, _T(".rtf"));
RI(config_x);
RI(config_y);
RI(config_w);
RI(config_h);
RI(config_border);
RI(config_color);
RI(config_bcolor1);
RI(config_bcolor2);
}
static HANDLE esFile;
DWORD CALLBACK esCb(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
if (dwCookie == 1) // write
{
WriteFile(esFile,pbBuff,static_cast<DWORD>(cb),reinterpret_cast<DWORD*>(pcb), nullptr);
}
else // read
{
ReadFile(esFile,pbBuff,static_cast<DWORD>(cb),reinterpret_cast<DWORD*>(pcb), nullptr);
}
return 0;
}
static void read_text()
{
esFile=CreateFile(text_file,GENERIC_READ,0, nullptr,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, nullptr);
if (esFile != INVALID_HANDLE_VALUE)
{
EDITSTREAM es;
es.dwCookie=0;
es.pfnCallback=esCb;
SendMessage(hwnd_rich,EM_STREAMIN,SF_RTF,reinterpret_cast<LPARAM>(&es));
CloseHandle(esFile);
}
}
static void write_text()
{
esFile=CreateFile(text_file,GENERIC_WRITE,0, nullptr,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, nullptr);
if (esFile != INVALID_HANDLE_VALUE)
{
EDITSTREAM es;
es.dwCookie=1;
es.pfnCallback=esCb;
SendMessage(hwnd_rich,EM_STREAMOUT,SF_RTF,reinterpret_cast<LPARAM>(&es));
CloseHandle(esFile);
} else { MessageBox(hwnd_main,_T("Error writing .rtf"), _T("Error"),0); }
}
static void config_write()
{
CRect r;
CWindow wnd(hwnd_main);
(void)wnd.GetWindowRect(&r);
config_x=r.left;
config_y=r.top;
config_w=r.right-r.left;
config_h=r.bottom-r.top;
WI(config_x);
WI(config_y);
WI(config_w);
WI(config_h);
WI(config_border);
WI(config_color);
WI(config_bcolor1);
WI(config_bcolor2);
}