forked from ckw-mod/ckw-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.cpp
313 lines (272 loc) · 7.68 KB
/
selection.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
/*-----------------------------------------------------------------------------
* File: selection.cpp
*-----------------------------------------------------------------------------
* Copyright (c) 2004-2005 Kazuo Ishii <[email protected]>
* - original version
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*---------------------------------------------------------------------------*/
#include "ckw.h"
static int gSelectMode = 0;
static COORD gSelectPos = { -1, -1 }; // pick point
static SMALL_RECT gSelectRect = { -1, -1, -1, -1 }; // expanded selection area
static const wchar_t WORD_BREAK_CHARS[] = {
L' ', L'\t', L'\"', L'&', L'\'', L'(', L')', L'*',
L',', L';', L'<', L'=', L'>', L'?', L'@', L'[',
L'\\', L']', L'^', L'`', L'{', L'}', L'~',
0x3000,
0x3001,
0x3002,
/**/
0,
};
/*****************************************************************************/
#define SCRN_InvalidArea(x,y) \
(y < gCSI->srWindow.Top || \
y > gCSI->srWindow.Bottom || \
x < gCSI->srWindow.Left || \
x > gCSI->srWindow.Right)
#define SELECT_GetScrn(x,y) \
(gScreen + CSI_WndCols(gCSI) * (y - gCSI->srWindow.Top) + x)
static void __select_word_expand_left()
{
if(SCRN_InvalidArea(gSelectRect.Left, gSelectRect.Top))
return;
CHAR_INFO* base = SELECT_GetScrn(gSelectRect.Left, gSelectRect.Top);
CHAR_INFO* ptr = base;
int c = gSelectRect.Left;
for( ; c >= gCSI->srWindow.Left ; c--, ptr--) {
if(wcschr(WORD_BREAK_CHARS, ptr->Char.UnicodeChar)) {
c++;
break;
}
}
if(c < 0)
c = 0;
if(gSelectRect.Left > c)
gSelectRect.Left = c;
}
static void __select_word_expand_right()
{
if(SCRN_InvalidArea(gSelectRect.Right, gSelectRect.Bottom))
return;
CHAR_INFO* base = SELECT_GetScrn(gSelectRect.Right, gSelectRect.Bottom);
CHAR_INFO* ptr = base;
int c = gSelectRect.Right;
for( ; c <= gCSI->srWindow.Right ; c++, ptr++) {
if(wcschr(WORD_BREAK_CHARS, ptr->Char.UnicodeChar)) {
break;
}
}
if(gSelectRect.Right < c)
gSelectRect.Right = c;
}
static void __select_char_expand()
{
CHAR_INFO* base;
if(SCRN_InvalidArea(gSelectRect.Left, gSelectRect.Top)) {
}
else if(gSelectRect.Left-1 >= gCSI->srWindow.Left) {
base = SELECT_GetScrn(gSelectRect.Left, gSelectRect.Top);
if(base->Attributes & COMMON_LVB_TRAILING_BYTE)
gSelectRect.Left--;
}
if(SCRN_InvalidArea(gSelectRect.Right, gSelectRect.Bottom)) {
}
else {
base = SELECT_GetScrn(gSelectRect.Right, gSelectRect.Bottom);
if(base->Attributes & COMMON_LVB_TRAILING_BYTE)
gSelectRect.Right++;
}
}
inline void __select_expand()
{
if(gSelectMode == 0) {
__select_char_expand();
}
else if(gSelectMode == 1) {
__select_word_expand_left();
__select_word_expand_right();
}
else if(gSelectMode == 2) {
gSelectRect.Left = gCSI->srWindow.Left;
gSelectRect.Right = gCSI->srWindow.Right+1;
}
}
static void window_to_charpos(int& x, int& y)
{
x -= gBorderSize;
y -= gBorderSize;
if(x < 0) x = 0;
if(y < 0) y = 0;
x /= gFontW;
y /= gFontH;
x += gCSI->srWindow.Left;
y += gCSI->srWindow.Top;
if(x > gCSI->srWindow.Right) x = gCSI->srWindow.Right+1;
if(y > gCSI->srWindow.Bottom) y = gCSI->srWindow.Bottom;
}
/*****************************************************************************/
static inline bool __select_invalid()
{
return ( gSelectRect.Top > gSelectRect.Bottom ||
(gSelectRect.Top == gSelectRect.Bottom &&
gSelectRect.Left >= gSelectRect.Right) );
}
/*----------*/
BOOL selectionGetArea(SMALL_RECT& sr)
{
if( __select_invalid() )
return(FALSE);
sr = gSelectRect;
return(TRUE);
}
/*----------*/
void selectionClear(HWND hWnd)
{
if( __select_invalid() )
return;
gSelectRect.Left = gSelectRect.Right = \
gSelectRect.Top = gSelectRect.Bottom = 0;
InvalidateRect(hWnd, NULL, FALSE);
}
/*----------*/
wchar_t * selectionGetString()
{
if( __select_invalid() )
return(NULL);
int nb, y;
if(gSelectRect.Top == gSelectRect.Bottom) {
nb = gSelectRect.Right - gSelectRect.Left;
}
else {
nb = gCSI->srWindow.Right - gSelectRect.Left+1;
for(y = gSelectRect.Top+1 ; y <= gSelectRect.Bottom-1 ; y++)
nb += CSI_WndCols(gCSI);
nb += gSelectRect.Right - gCSI->srWindow.Left;
}
COORD size = { CSI_WndCols(gCSI), 1 };
CHAR_INFO* work = new CHAR_INFO[ size.X ];
wchar_t* buffer = new wchar_t[ nb +32 ];
wchar_t* wp = buffer;
COORD pos = { 0,0 };
SMALL_RECT sr = { gCSI->srWindow.Left, 0, gCSI->srWindow.Right, 0 };
*wp = 0;
if(gSelectRect.Top == gSelectRect.Bottom) {
sr.Top = sr.Bottom = gSelectRect.Top;
ReadConsoleOutput_Unicode(gStdOut, work, size, pos, &sr);
copyChar(wp, work, gSelectRect.Left, gSelectRect.Right-1, false);
}
else {
sr.Top = sr.Bottom = gSelectRect.Top;
ReadConsoleOutput_Unicode(gStdOut, work, size, pos, &sr);
copyChar(wp, work, gSelectRect.Left, gCSI->srWindow.Right);
for(y = gSelectRect.Top+1 ; y <= gSelectRect.Bottom-1 ; y++) {
sr.Top = sr.Bottom = y;
ReadConsoleOutput_Unicode(gStdOut, work, size, pos, &sr);
copyChar(wp, work, gCSI->srWindow.Left, gCSI->srWindow.Right);
}
sr.Top = sr.Bottom = gSelectRect.Bottom;
ReadConsoleOutput_Unicode(gStdOut, work, size, pos, &sr);
copyChar(wp, work, gCSI->srWindow.Left, gSelectRect.Right-1, false);
}
delete [] work;
return(buffer);
}
/*----------*/
void onLBtnDown(HWND hWnd, int x, int y)
{
static DWORD prev_time = 0;
static int prevX = -100;
static int prevY = -100;
{
/* calc click count */
DWORD now_time = GetTickCount();
DWORD stime;
if(prev_time > now_time)
stime = now_time + ~prev_time+1;
else
stime = now_time - prev_time;
if(stime <= GetDoubleClickTime()) {
int sx = (prevX > x) ? prevX-x : x-prevX;
int sy = (prevY > y) ? prevY-y : y-prevY;
if(sx <= GetSystemMetrics(SM_CXDOUBLECLK) &&
sy <= GetSystemMetrics(SM_CYDOUBLECLK)) {
if(++gSelectMode > 2)
gSelectMode = 0;
}
else {
gSelectMode = 0;
}
}
else {
gSelectMode = 0;
}
prev_time = now_time;
prevX = x;
prevY = y;
}
if(!gScreen || !gCSI)
return;
window_to_charpos(x, y);
SetCapture(hWnd);
gSelectPos.X = x;
gSelectPos.Y = y;
gSelectRect.Left = gSelectRect.Right = x;
gSelectRect.Top = gSelectRect.Bottom = y;
__select_expand();
InvalidateRect(hWnd, NULL, FALSE);
}
/*----------*/
void onLBtnUp(HWND hWnd, int x, int y)
{
if(hWnd != GetCapture())
return;
ReleaseCapture();
if(!gScreen || !gCSI)
return;
//window_to_charpos(x, y);
wchar_t* str = selectionGetString();
if(!str) return;
copyStringToClipboard( hWnd, str );
delete [] str;
}
/*----------*/
void onMouseMove(HWND hWnd, int x, int y)
{
if(hWnd != GetCapture())
return;
if(!gScreen || !gCSI)
return;
window_to_charpos(x, y);
SMALL_RECT bak = gSelectRect;
if(y < gSelectPos.Y || (y == gSelectPos.Y && x < gSelectPos.X)) {
gSelectRect.Left = x;
gSelectRect.Top = y;
gSelectRect.Right = gSelectPos.X;
gSelectRect.Bottom = gSelectPos.Y;
}
else {
gSelectRect.Left = gSelectPos.X;
gSelectRect.Top = gSelectPos.Y;
gSelectRect.Right = x;
gSelectRect.Bottom = y;
}
__select_expand();
if(memcmp(&bak, &gSelectRect, sizeof(bak))) {
InvalidateRect(hWnd, NULL, FALSE);
}
}
/* EOF */