forked from stephaniewm55/WinVi-Minimalist-Text-Hex-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mouse.c
382 lines (357 loc) · 11 KB
/
Mouse.c
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
/********************************************************\
|* *|
|* Program: WinVi *|
|* Author: Raphael Molle *|
|* Compiler: several *|
|* --------------------- -------------------------- *|
|* © Copyright 1994-2011 by Raphael Molle, Berlin *|
|* © Copyleft by GPL See file COPYING for more *|
|* info about terms of use *|
|* *|
\********************************************************/
/* revision log:
* 3-Dec-2000 first publication of this source file
* 29-Sep-2002 ShowMatch for EBCDIC
* 14-Nov-2002 prevent mouse positioning after Ctrl+Alt+V
* 1-Jan-2009 can now be compiled with VC++ 2008 Express
* 9-Jan-2011 bugfix: mouse drag above or below the text in long lines
*/
#include <windows.h>
#include <string.h>
#include <wchar.h>
#include "winvi.h"
#include "page.h"
#include "paint.h"
#include "status.h"
#include "toolbar.h"
#if !defined(WS_EX_TOOLWINDOW)
#define WS_EX_TOOLWINDOW 0x00000080L
#endif
extern LONG xOffset;
extern BOOL EscapeInput;
BOOL Selecting = FALSE, LButtonPushed = FALSE;
UINT TimerRunning = 0;
POSITION SelectAnchor;
ULONG SelectAnchorCount;
POSITION PrevMousePos;
LONG PrevMouseCount;
static void MouseSelect(void)
{ long CurrCount;
if (!ComparePos(&CurrPos, &PrevMousePos)) return;
CurrCount = CountBytes(&CurrPos);
if (ComparePos(&CurrPos, &PrevMousePos) > 0) {
long Amount = CurrCount - PrevMouseCount;
InvalidateArea(&PrevMousePos, Amount, 1);
} else {
long Amount = PrevMouseCount - CurrCount;
InvalidateArea(&CurrPos, Amount, 1);
}
if (ComparePos(&CurrPos, &SelectAnchor) > 0) {
SelectStart = SelectAnchor;
SelectCount = CurrCount - SelectAnchorCount;
SelectBytePos = SelectAnchorCount;
} else {
SelectStart = CurrPos;
SelectCount = SelectAnchorCount - CurrCount;
SelectBytePos = CurrCount;
}
UpdateWindow(hwndMain);
CheckClipboard();
UpdateWindow(hwndMain);
PrevMousePos = CurrPos;
PrevMouseCount = CurrCount;
}
void MouseInTextArea(UINT uMsg, WPARAM wPar, int Line, int xPixel, int yPixel,
BOOL Activated)
{ BOOL Down;
PARAM_NOT_USED(wPar);
PARAM_NOT_USED(yPixel);
if (TimerRunning) {
KillTimer(hwndMain, 100);
TimerRunning = 0;
}
if (Activated || uMsg==WM_LBUTTONUP) {
if (LButtonPushed) {
LButtonPushed = Selecting = FALSE;
ReleaseCapture();
}
return;
}
if (EscapeInput) return;
if (LineInfo == NULL /*Windows NT 3.51 initial state*/) return;
while (LineInfo[Line].Flags & LIF_EOF && Line) --Line;
Down = uMsg==WM_LBUTTONDOWN || uMsg==WM_LBUTTONDBLCLK;
if (Down || LButtonPushed) {
BOOL DoCapture = TRUE, ShiftPressed;
if (Disabled) {
MessageBeep(MB_OK);
return;
}
ShiftPressed = Mode==InsertMode && GetKeyState(VK_SHIFT)<0;
if (ShiftPressed && !SelectCount) {
SelectStart = SelectAnchor = CurrPos;
SelectBytePos = SelectAnchorCount = CountBytes(&CurrPos);
}
Indenting = FALSE;
CurrPos = LineInfo[Line].Pos;
CurrCol.PixelOffset = xPixel+xOffset;
if (Mode==InsertMode && uMsg!=WM_LBUTTONDBLCLK)
CurrCol.PixelOffset += AveCharWidth >> 1;
AdvanceToCurr(&CurrPos,/*Line,*/ (Mode==InsertMode || Mode==ReplaceMode)
&& uMsg!=WM_LBUTTONDBLCLK
? Mode==InsertMode ? 3 : 1 : 0);
if (Down) {
if (hwndCmd) {
char Buf[4];
if (GetWindowText(hwndCmd, Buf, sizeof(Buf))==1 && Buf[0]==':')
/*"empty" command window (status line), remove...*/
PostMessage(hwndMain, WM_USER+1234, 0, 0);
}
Selecting = Mode==InsertMode;
if (ShiftPressed) ExtendSelection();
else {
if (SelectCount) {
Unselect();
UpdateWindow(hwndMain);
}
if (Selecting) {
SelectStart = CurrPos;
if (uMsg == WM_LBUTTONDBLCLK) {
/*select word...*/
int c, f = CharFlags(CharAt(&CurrPos));
if (!(f & 1)) {
do {
c = GoBackAndChar(&SelectStart);
} while (!((f ^ CharFlags(c)) & 0x39));
if (c != C_EOF) CharAndAdvance(&SelectStart);
do {
c = AdvanceAndChar(&CurrPos);
} while (!((f ^ CharFlags(c)) & 0x39));
SelectCount = CountBytes(&CurrPos) -
CountBytes(&SelectStart);
InvalidateArea(&SelectStart, SelectCount, 0);
CheckClipboard();
DoCapture = FALSE;
}
}
SelectAnchor = SelectStart;
SelectAnchorCount = SelectBytePos = CountBytes(&SelectStart);
}
}
if (DoCapture) {
LButtonPushed = TRUE;
SetCapture(hwndMain);
PrevMousePos = CurrPos;
PrevMouseCount = CountBytes(&CurrPos);
}
} else if (Selecting) MouseSelect();
NewPosition(&CurrPos);
if (!DoCapture) {
/*last line may have scrolled, reselect again (11-May-98)...*/
InvalidateArea(&SelectStart, SelectCount, 0);
/*word selected, cursor is not mouse position (7-Oct-2000)...*/
GetXPos(&CurrCol);
}
ShowEditCaret();
if (Mode == CommandMode) {
INT c = CharAt(&CurrPos);
switch (c) {
case '(': case '[': case '{':
case ')': case ']': case '}':
ShowMatch(0);
}
}
}
}
void MouseSelectText(UINT uMsg, WPARAM wPar, LONG xPixel, BOOL BelowText)
{
PARAM_NOT_USED(wPar);
if (LButtonPushed) {
if (uMsg == WM_LBUTTONUP) {
LButtonPushed = FALSE;
ReleaseCapture();
if (TimerRunning) {
KillTimer(hwndMain, 100);
TimerRunning = 0;
}
} else {
int i;
static BOOL SaveBelowText;
static LONG SaveXPixel;
UINT Flags = 0x402;
if (uMsg == WM_TIMER) {
BelowText = SaveBelowText;
xPixel = SaveXPixel;
} else {
SaveBelowText = BelowText;
SaveXPixel = xPixel;
}
if (Selecting) Flags |= 16;
if (Mode == InsertMode) Flags |= 1;
if (BelowText) {
if (!(LineInfo[CrsrLines].Flags & LIF_EOF))
Position(1, CTRL('e'), Flags);
i = CrsrLines-1;
} else {
Position(1, CTRL('y'), Flags);
i = 0;
}
if (Selecting) {
PrevMousePos = CurrPos;
PrevMouseCount = CountBytes(&CurrPos);
}
while (LineInfo[i].Flags & LIF_EOF && i) --i;
CurrPos = LineInfo[i].Pos;
CurrCol.PixelOffset = xPixel+xOffset;
if (Mode == InsertMode) CurrCol.PixelOffset += AveCharWidth >> 1;
AdvanceToCurr(&CurrPos, /*i,*/ (Mode==InsertMode ? 3 : 0)
| (Mode==ReplaceMode ? 1 : 0));
NewPosition(&CurrPos);
ShowEditCaret();
if (Selecting) MouseSelect();
if (!TimerRunning)
TimerRunning = SetTimer(hwndMain, 100, 10, NULL);
}
}
}
static HWND hwndStatusTip;
VOID RemoveStatusTip(VOID)
{
if (hwndStatusTip) {
DestroyWindow(hwndStatusTip);
hwndStatusTip = NULL;
ReleaseCapture();
}
}
BOOL MouseHandle(UINT uMsg, WPARAM wPar, LPARAM lPar)
{ int Activated = MouseActivated;
MouseActivated = FALSE;
if (MouseForToolbar(uMsg, wPar, lPar)) {
/*nothing*/
} else if (!Activated && !LButtonPushed
&& (short)HIWORD(lPar) >= ClientRect.bottom-StatusHeight) {
extern BOOL SizeGrip;
if ((short)LOWORD(lPar) >= ClientRect.right-16 && SizeGrip) {
if (uMsg != WM_LBUTTONDOWN) {
SwitchCursor(SWC_SIZEGRIP);
return (FALSE);
}
SendMessage(hwndMain, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, lPar);
} else {
if ( (short)LOWORD(lPar) < StatusFields[0].x+StatusFields[0].Width
&& (short)LOWORD(lPar) >= StatusFields[0].x+1
&& (short)HIWORD(lPar) >= ClientRect.bottom-StatusHeight+5
&& (short)HIWORD(lPar) < ClientRect.bottom-StatusHeight+5
+StatusTextHeight) {
if (uMsg == WM_LBUTTONDOWN) {
if (!hwndCmd && !EscapeInput) {
RemoveStatusTip();
CommandEdit(':');
}
} else if (uMsg == WM_RBUTTONDOWN) {
POINT p;
GetCursorPos(&p);
StatusContextMenu(p.x, p.y);
} else if (GetFocus() == hwndMain) {
/*check for status tip to display...*/
if (StatusFields[0].TextExtent+6 > StatusFields[0].Width) {
if (!hwndStatusTip && StatusFields[0].Text &&
*StatusFields[0].Text) {
POINT pt;
pt.x = StatusFields[0].x;
pt.y = ClientRect.bottom-StatusHeight + 5;
ClientToScreen(hwndMain, &pt);
hwndStatusTip =
CreateWindowW(L"WinVi - ToolTip",
StatusFields[0].Text,
WS_BORDER | WS_POPUP,
pt.x, pt.y,
StatusFields[0].TextExtent+7,
StatusTextHeight,
0 /*no parent*/, NULL,
hInst, NULL
);
SetWindowLong(hwndStatusTip, 0*sizeof(LONG),
(LONG)StatusStyle
[StatusFields[0].Style].Color);
SetWindowLong(hwndStatusTip, 1*sizeof(LONG),
GetSysColor(COLOR_BTNFACE));
SetWindowLong(hwndStatusTip, 2*sizeof(LONG),
(UINT)StatusFont);
SetWindowLong(hwndStatusTip, 3*sizeof(LONG), -1);
SetWindowLong(hwndStatusTip, GWL_EXSTYLE,
GetWindowLong(hwndStatusTip, GWL_EXSTYLE)
| WS_EX_TOOLWINDOW);
SetCapture(hwndMain);
ShowWindow(hwndStatusTip, SW_SHOWNA);
}
}
}
} else RemoveStatusTip();
}
} else {
RemoveStatusTip();
if ((short)HIWORD(lPar) >= nToolbarHeight + YTEXT_OFFSET &&
(short)HIWORD(lPar) < ClientRect.bottom-StatusHeight-YTEXT_OFFSET) {
int Line = ((short)HIWORD(lPar) - nToolbarHeight - YTEXT_OFFSET)
/ TextHeight;
SwitchCursor(SWC_TEXTAREA);
MouseInTextArea(uMsg, wPar, Line,
(short)LOWORD(lPar) - StartX(FALSE),
StatusHeight + YTEXT_OFFSET + Line*TextHeight,
Activated);
return (TRUE);
} else if (LButtonPushed) {
MouseSelectText(uMsg, wPar, (short)LOWORD(lPar) - StartX(FALSE),
(short)HIWORD(lPar) >= nToolbarHeight+YTEXT_OFFSET);
return (TRUE);
}
}
SwitchCursor(SWC_STATUS_OR_TOOLBAR);
return (FALSE);
}
BOOL StatusContextMenuCommand(WORD wCommand)
{ HGLOBAL hMem;
PWSTR pMem, pSrc;
int nLen;
if (wCommand == 910) {
pSrc = StatusFields[0].Text;
if (*pSrc == ' ') ++pSrc;
nLen = wcslen(pSrc);
} else if (wCommand == 911) {
pSrc = wcschr(StatusFields[0].Text, 0253);
if (pSrc++ == NULL || (pMem = wcsrchr(pSrc, 0273)) == NULL)
return (FALSE);
nLen = pMem - pSrc;
} else return (FALSE);
if (!OpenClipboard(hwndMain)) {
ErrorBox(MB_ICONEXCLAMATION, 302);
return (FALSE);
}
if (!EmptyClipboard()) {
CloseClipboard();
ErrorBox(MB_ICONEXCLAMATION, 303);
return (FALSE);
}
hMem = GlobalAlloc(GMEM_MOVEABLE, 2*nLen + 2);
if (!hMem) {
CloseClipboard();
ErrorBox(MB_ICONEXCLAMATION, 304);
return (FALSE);
}
pMem = GlobalLock(hMem);
if (pMem == NULL) {
CloseClipboard();
GlobalFree(hMem);
ErrorBox(MB_ICONEXCLAMATION, 305);
return (FALSE);
}
_fmemcpy(pMem, pSrc, 2*nLen);
pMem[nLen] = '\0';
/*unlock and publish...*/
GlobalUnlock(hMem);
SetClipboardData(CF_UNICODETEXT, hMem);
CloseClipboard();
CheckClipboard();
return (TRUE);
}