urlmon: Fix CoInternetParse{Url,IUri} spec entry.
[wine.git] / programs / wineconsole / user.c
blob5cc405d5b708cde898291c13f1261119d7875001
1 /*
2 * a GUI application for displaying a console
3 * USER32 back end
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "winecon_user.h"
24 #include "winnls.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
29 WINE_DECLARE_DEBUG_CHANNEL(wc_font);
31 UINT g_uiDefaultCharset;
33 static BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* font);
35 /******************************************************************
36 * WCUSER_FillMemDC
38 * Fills the Mem DC with current cells values
40 static void WCUSER_FillMemDC(const struct inner_data* data, int upd_tp, int upd_bm)
42 unsigned i, j, k;
43 CHAR_INFO* cell;
44 HFONT hOldFont;
45 WORD attr;
46 WCHAR* line;
47 RECT r;
48 HBRUSH hbr;
49 INT *dx;
51 /* no font has been set up yet, don't worry about filling the bitmap,
52 * we'll do it once a font is chosen
54 if (!PRIVATE(data)->hFont) return;
56 /* FIXME: could set up a mechanism to reuse the line between different
57 * calls to this function
59 if (!(line = HeapAlloc(GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(WCHAR)))) return;
60 dx = HeapAlloc( GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(*dx) );
62 hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
63 for (j = upd_tp; j <= upd_bm; j++)
65 cell = &data->cells[j * data->curcfg.sb_width];
66 for (i = 0; i < data->curcfg.sb_width; i++)
68 attr = cell[i].Attributes;
69 SetBkColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[(attr >> 4) & 0x0F]);
70 SetTextColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[attr & 0x0F]);
71 for (k = i; k < data->curcfg.sb_width && cell[k].Attributes == attr; k++)
73 line[k - i] = cell[k].Char.UnicodeChar;
74 dx[k - i] = data->curcfg.cell_width;
76 ExtTextOutW( PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
77 0, NULL, line, k - i, dx );
78 if (PRIVATE(data)->ext_leading &&
79 (hbr = CreateSolidBrush(data->curcfg.color_map[(attr >> 4) & 0x0F])))
81 r.left = i * data->curcfg.cell_width;
82 r.top = (j + 1) * data->curcfg.cell_height - PRIVATE(data)->ext_leading;
83 r.right = k * data->curcfg.cell_width;
84 r.bottom = (j + 1) * data->curcfg.cell_height;
85 FillRect(PRIVATE(data)->hMemDC, &r, hbr);
86 DeleteObject(hbr);
88 i = k - 1;
91 SelectObject(PRIVATE(data)->hMemDC, hOldFont);
92 HeapFree(GetProcessHeap(), 0, dx);
93 HeapFree(GetProcessHeap(), 0, line);
96 /******************************************************************
97 * WCUSER_NewBitmap
99 * Either the font geometry or the sb geometry has changed. we need
100 * to recreate the bitmap geometry.
102 static void WCUSER_NewBitmap(struct inner_data* data)
104 HDC hDC;
105 HBITMAP hnew, hold;
107 if (!data->curcfg.sb_width || !data->curcfg.sb_height ||
108 !PRIVATE(data)->hFont || !(hDC = GetDC(data->hWnd)))
109 return;
110 hnew = CreateCompatibleBitmap(hDC,
111 data->curcfg.sb_width * data->curcfg.cell_width,
112 data->curcfg.sb_height * data->curcfg.cell_height);
113 ReleaseDC(data->hWnd, hDC);
114 hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
116 if (PRIVATE(data)->hBitmap)
118 if (hold == PRIVATE(data)->hBitmap)
119 DeleteObject(PRIVATE(data)->hBitmap);
120 else
121 WINE_FIXME("leak\n");
123 PRIVATE(data)->hBitmap = hnew;
124 WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
127 /******************************************************************
128 * WCUSER_ResizeScreenBuffer
132 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
134 WCUSER_NewBitmap(data);
137 /******************************************************************
138 * WCUSER_PosCursor
140 * Set a new position for the cursor
142 static void WCUSER_PosCursor(const struct inner_data* data)
144 if (data->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
146 SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
147 (data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
148 ShowCaret(data->hWnd);
151 /******************************************************************
152 * WCUSER_ShapeCursor
154 * Sets a new shape for the cursor
156 static void WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
158 if (force || size != data->curcfg.cursor_size)
160 if (data->curcfg.cursor_visible && data->hWnd == GetFocus()) DestroyCaret();
161 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
162 PRIVATE(data)->cursor_bitmap = NULL;
163 if (size != 100)
165 int w16b; /* number of bytes per row, aligned on word size */
166 BYTE* ptr;
167 int i, j, nbl;
169 w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
170 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
171 if (!ptr) return;
172 nbl = max((data->curcfg.cell_height * size) / 100, 1);
173 for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
175 for (i = 0; i < data->curcfg.cell_width; i++)
177 ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
180 PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
181 data->curcfg.cell_height, 1, 1, ptr);
182 HeapFree(GetProcessHeap(), 0, ptr);
184 data->curcfg.cursor_size = size;
185 data->curcfg.cursor_visible = -1;
188 vis = vis != 0;
189 if (force || vis != data->curcfg.cursor_visible)
191 data->curcfg.cursor_visible = vis;
192 if (data->hWnd == GetFocus())
194 if (vis)
196 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
197 data->curcfg.cell_width, data->curcfg.cell_height);
198 WCUSER_PosCursor(data);
200 else
202 DestroyCaret();
206 WINECON_DumpConfig("crsr", &data->curcfg);
209 /******************************************************************
210 * WCUSER_ComputePositions
212 * Recomputes all the components (mainly scroll bars) positions
214 static void WCUSER_ComputePositions(struct inner_data* data)
216 RECT r;
217 int dx, dy;
219 /* compute window size from desired client size */
220 r.left = r.top = 0;
221 r.right = data->curcfg.win_width * data->curcfg.cell_width;
222 r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
224 if (IsRectEmpty(&r)) return;
226 AdjustWindowRect(&r, GetWindowLongW(data->hWnd, GWL_STYLE), FALSE);
228 dx = dy = 0;
229 if (data->curcfg.sb_width > data->curcfg.win_width)
231 dy = GetSystemMetrics(SM_CYHSCROLL);
232 SetScrollRange(data->hWnd, SB_HORZ, 0,
233 data->curcfg.sb_width - data->curcfg.win_width, FALSE);
234 SetScrollPos(data->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
235 ShowScrollBar(data->hWnd, SB_HORZ, TRUE);
237 else
239 ShowScrollBar(data->hWnd, SB_HORZ, FALSE);
242 if (data->curcfg.sb_height > data->curcfg.win_height)
244 dx = GetSystemMetrics(SM_CXVSCROLL);
245 SetScrollRange(data->hWnd, SB_VERT, 0,
246 data->curcfg.sb_height - data->curcfg.win_height, FALSE);
247 SetScrollPos(data->hWnd, SB_VERT, 0, FALSE); /* FIXME */
248 ShowScrollBar(data->hWnd, SB_VERT, TRUE);
250 else
252 ShowScrollBar(data->hWnd, SB_VERT, FALSE);
255 SetWindowPos(data->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
256 SWP_NOMOVE|SWP_NOZORDER);
257 WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
258 WCUSER_PosCursor(data);
261 /******************************************************************
262 * WCUSER_SetTitle
264 * Sets the title to the wine console
266 static void WCUSER_SetTitle(const struct inner_data* data)
268 WCHAR buffer[256];
270 if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
271 SetWindowTextW(data->hWnd, buffer);
274 void WCUSER_DumpLogFont(const char* pfx, const LOGFONTW* lf, DWORD ft)
276 WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
277 "\tlf.lfHeight=%d lf.lfWidth=%d lf.lfEscapement=%d lf.lfOrientation=%d\n"
278 "\tlf.lfWeight=%d lf.lfItalic=%u lf.lfUnderline=%u lf.lfStrikeOut=%u\n"
279 "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
280 "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
281 pfx,
282 (ft & RASTER_FONTTYPE) ? "raster" : "",
283 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
284 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
285 (ft & DEVICE_FONTTYPE) ? "|device" : "",
286 lf->lfHeight, lf->lfWidth, lf->lfEscapement, lf->lfOrientation,
287 lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
288 lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
289 wine_dbgstr_w(lf->lfFaceName));
292 void WCUSER_DumpTextMetric(const TEXTMETRICW* tm, DWORD ft)
294 WINE_TRACE_(wc_font)("%s%s%s%s\n"
295 "\ttmHeight=%d tmAscent=%d tmDescent=%d tmInternalLeading=%d tmExternalLeading=%d\n"
296 "\ttmAveCharWidth=%d tmMaxCharWidth=%d tmWeight=%d tmOverhang=%d\n"
297 "\ttmDigitizedAspectX=%d tmDigitizedAspectY=%d\n"
298 "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
299 "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
300 (ft & RASTER_FONTTYPE) ? "raster" : "",
301 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
302 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
303 (ft & DEVICE_FONTTYPE) ? "|device" : "",
304 tm->tmHeight, tm->tmAscent, tm->tmDescent, tm->tmInternalLeading, tm->tmExternalLeading, tm->tmAveCharWidth,
305 tm->tmMaxCharWidth, tm->tmWeight, tm->tmOverhang, tm->tmDigitizedAspectX, tm->tmDigitizedAspectY,
306 tm->tmFirstChar, tm->tmLastChar, tm->tmDefaultChar, tm->tmBreakChar, tm->tmItalic, tm->tmUnderlined, tm->tmStruckOut,
307 tm->tmPitchAndFamily, tm->tmCharSet);
310 /******************************************************************
311 * WCUSER_AreFontsEqual
315 static BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONTW* lf)
317 return lf->lfHeight == config->cell_height &&
318 lf->lfWeight == config->font_weight &&
319 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
320 !lstrcmpW(lf->lfFaceName, config->face_name);
323 struct font_chooser
325 struct inner_data* data;
326 int pass;
327 BOOL done;
330 /******************************************************************
331 * WCUSER_ValidateFontMetric
333 * Returns true if the font described in tm is usable as a font for the renderer
335 BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRICW* tm,
336 DWORD type, int pass)
338 switch (pass) /* we get increasingly lenient in later passes */
340 case 0:
341 if (type & RASTER_FONTTYPE) return FALSE;
342 /* fall through */
343 case 1:
344 if (type & RASTER_FONTTYPE)
346 if (tm->tmMaxCharWidth * data->curcfg.win_width >= GetSystemMetrics(SM_CXSCREEN) ||
347 tm->tmHeight * data->curcfg.win_height >= GetSystemMetrics(SM_CYSCREEN))
348 return FALSE;
350 /* fall through */
351 case 2:
352 if (tm->tmCharSet != DEFAULT_CHARSET && tm->tmCharSet != g_uiDefaultCharset) return FALSE;
353 /* fall through */
354 case 3:
355 if (tm->tmItalic || tm->tmUnderlined || tm->tmStruckOut) return FALSE;
356 break;
358 return TRUE;
361 /******************************************************************
362 * WCUSER_ValidateFont
364 * Returns true if the font family described in lf is usable as a font for the renderer
366 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONTW* lf, int pass)
368 switch (pass) /* we get increasingly lenient in later passes */
370 case 0:
371 case 1:
372 case 2:
373 if (lf->lfCharSet != DEFAULT_CHARSET && lf->lfCharSet != g_uiDefaultCharset) return FALSE;
374 /* fall through */
375 case 3:
376 if ((lf->lfPitchAndFamily & 3) != FIXED_PITCH) return FALSE;
377 /* fall through */
378 case 4:
379 if (lf->lfFaceName[0] == '@') return FALSE;
380 break;
382 return TRUE;
385 /******************************************************************
386 * get_first_font_enum_2
387 * get_first_font_enum
389 * Helper functions to get a decent font for the renderer
391 static int CALLBACK get_first_font_enum_2(const LOGFONTW* lf, const TEXTMETRICW* tm,
392 DWORD FontType, LPARAM lParam)
394 struct font_chooser* fc = (struct font_chooser*)lParam;
396 WCUSER_DumpTextMetric(tm, FontType);
397 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType, fc->pass))
399 LOGFONTW mlf = *lf;
401 /* Use the default sizes for the font (this is needed, especially for
402 * TrueType fonts, so that we get a decent size, not the max size)
404 mlf.lfWidth = fc->data->curcfg.cell_width;
405 mlf.lfHeight = fc->data->curcfg.cell_height;
406 if (!mlf.lfHeight) mlf.lfHeight = MulDiv( 16, GetDpiForSystem(), USER_DEFAULT_SCREEN_DPI );
407 if (WCUSER_SetFont(fc->data, &mlf))
409 struct config_data defcfg;
411 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
412 fc->done = 1;
413 /* since we've modified the current config with new font information,
414 * set this information as the new default.
416 WINECON_RegLoad(NULL, &defcfg);
417 defcfg.cell_width = fc->data->curcfg.cell_width;
418 defcfg.cell_height = fc->data->curcfg.cell_height;
419 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
420 /* Force also its writing back to the registry so that we can get it
421 * the next time.
423 WINECON_RegSave(&defcfg);
424 return 0;
427 return 1;
430 static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
431 DWORD FontType, LPARAM lParam)
433 struct font_chooser* fc = (struct font_chooser*)lParam;
435 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
436 if (WCUSER_ValidateFont(fc->data, lf, fc->pass))
438 EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
439 get_first_font_enum_2, lParam);
440 return !fc->done; /* we just need the first matching one... */
442 return 1;
445 /******************************************************************
446 * WCUSER_CopyFont
448 * get the relevant information from the font described in lf and store them
449 * in config
451 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
453 TEXTMETRICW tm;
454 HDC hDC;
455 HFONT hFont, hOldFont;
456 CPINFO cpinfo;
458 if (!(hDC = GetDC(hWnd))) return NULL;
459 if (!(hFont = CreateFontIndirectW(lf)))
461 ReleaseDC(hWnd, hDC);
462 return NULL;
464 hOldFont = SelectObject(hDC, hFont);
465 GetTextMetricsW(hDC, &tm);
466 SelectObject(hDC, hOldFont);
467 ReleaseDC(hWnd, hDC);
469 config->cell_width = tm.tmAveCharWidth;
470 config->cell_height = tm.tmHeight + tm.tmExternalLeading;
471 config->font_weight = tm.tmWeight;
472 lstrcpyW(config->face_name, lf->lfFaceName);
473 if (el) *el = tm.tmExternalLeading;
475 /* FIXME: use maximum width for DBCS codepages since some chars take two cells */
476 if (GetCPInfo( GetConsoleOutputCP(), &cpinfo ) && cpinfo.MaxCharSize > 1)
477 config->cell_width = tm.tmMaxCharWidth;
479 return hFont;
482 /******************************************************************
483 * WCUSER_FillLogFont
487 void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
489 lf->lfHeight = height;
490 lf->lfWidth = 0;
491 lf->lfEscapement = 0;
492 lf->lfOrientation = 0;
493 lf->lfWeight = weight;
494 lf->lfItalic = FALSE;
495 lf->lfUnderline = FALSE;
496 lf->lfStrikeOut = FALSE;
497 lf->lfCharSet = DEFAULT_CHARSET;
498 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
499 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
500 lf->lfQuality = DEFAULT_QUALITY;
501 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
502 lstrcpyW(lf->lfFaceName, name);
505 /******************************************************************
506 * WCUSER_SetFont
508 * sets logfont as the new font for the console
510 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
512 HFONT hFont;
513 LONG el;
515 if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
516 return TRUE;
518 hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
519 if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
521 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
522 PRIVATE(data)->hFont = hFont;
523 PRIVATE(data)->ext_leading = el;
525 WCUSER_ComputePositions(data);
526 WCUSER_NewBitmap(data);
527 InvalidateRect(data->hWnd, NULL, FALSE);
528 UpdateWindow(data->hWnd);
530 return TRUE;
533 /******************************************************************
534 * WCUSER_SetFontPmt
536 * Sets a new font for the console.
537 * In fact a wrapper for WCUSER_SetFont
539 static void WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
540 unsigned height, unsigned weight)
542 LOGFONTW lf;
543 struct font_chooser fc;
545 WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
546 wine_dbgstr_wn(font, -1), height, weight);
548 if (font[0] != '\0' && height != 0 && weight != 0)
550 WCUSER_FillLogFont(&lf, font, height, weight);
551 if (WCUSER_SetFont(data, &lf))
553 WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
554 return;
558 /* try to find an acceptable font */
559 WINE_WARN("Couldn't match the font from registry... trying to find one\n");
560 fc.data = data;
561 fc.done = FALSE;
562 for (fc.pass = 0; fc.pass <= 5; fc.pass++)
564 EnumFontFamiliesW(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
565 if (fc.done) return;
567 WINECON_Fatal("Couldn't find a decent font, aborting");
570 /******************************************************************
571 * WCUSER_GetCell
573 * Get a cell from a relative coordinate in window (takes into
574 * account the scrolling)
576 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
578 COORD c;
580 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
581 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
583 return c;
586 /******************************************************************
587 * WCUSER_GetSelectionRect
589 * Get the selection rectangle
591 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
593 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) - data->curcfg.win_pos.X) * data->curcfg.cell_width;
594 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
595 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
596 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
599 /******************************************************************
600 * WCUSER_SetSelection
604 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
606 HDC hDC;
607 RECT r;
609 WCUSER_GetSelectionRect(data, &r);
610 hDC = hRefDC ? hRefDC : GetDC(data->hWnd);
611 if (hDC)
613 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
614 HideCaret(data->hWnd);
615 InvertRect(hDC, &r);
616 if (hDC != hRefDC)
617 ReleaseDC(data->hWnd, hDC);
618 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
619 ShowCaret(data->hWnd);
623 /******************************************************************
624 * WCUSER_MoveSelection
628 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
630 RECT r;
631 HDC hDC;
633 if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
634 c2.X < 0 || c2.X >= data->curcfg.sb_width ||
635 c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
636 c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
637 return;
639 WCUSER_GetSelectionRect(data, &r);
640 hDC = GetDC(data->hWnd);
641 if (hDC)
643 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
644 HideCaret(data->hWnd);
645 InvertRect(hDC, &r);
647 PRIVATE(data)->selectPt1 = c1;
648 PRIVATE(data)->selectPt2 = c2;
649 if (hDC)
651 WCUSER_GetSelectionRect(data, &r);
652 InvertRect(hDC, &r);
653 ReleaseDC(data->hWnd, hDC);
654 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
655 ShowCaret(data->hWnd);
659 /******************************************************************
660 * WCUSER_CopySelectionToClipboard
662 * Copies the current selection into the clipboard
664 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
666 HANDLE hMem;
667 LPWSTR p;
668 unsigned w, h;
670 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
671 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
673 if (!OpenClipboard(data->hWnd)) return;
674 EmptyClipboard();
676 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
677 if (hMem && (p = GlobalLock(hMem)))
679 COORD c;
680 int y;
682 c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
683 c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
685 for (y = 0; y < h; y++, c.Y++)
687 LPWSTR end;
688 DWORD count;
690 ReadConsoleOutputCharacterW(data->hConOut, p, w - 1, c, &count);
692 /* strip spaces from the end of the line */
693 end = p + w - 1;
694 while (end > p && *(end - 1) == ' ')
695 end--;
696 *end = (y < h - 1) ? '\n' : '\0';
697 p = end + 1;
699 GlobalUnlock(hMem);
700 SetClipboardData(CF_UNICODETEXT, hMem);
702 CloseClipboard();
705 /******************************************************************
706 * WCUSER_PasteFromClipboard
710 static void WCUSER_PasteFromClipboard(struct inner_data* data)
712 HANDLE h;
713 WCHAR* ptr;
715 if (!OpenClipboard(data->hWnd)) return;
716 h = GetClipboardData(CF_UNICODETEXT);
717 if (h && (ptr = GlobalLock(h)))
719 int i, len = GlobalSize(h) / sizeof(WCHAR);
720 INPUT_RECORD ir[2];
721 DWORD n;
722 SHORT sh;
724 ir[0].EventType = KEY_EVENT;
725 ir[0].Event.KeyEvent.wRepeatCount = 0;
726 ir[0].Event.KeyEvent.dwControlKeyState = 0;
727 ir[0].Event.KeyEvent.bKeyDown = TRUE;
729 /* generate the corresponding input records */
730 for (i = 0; i < len; i++)
732 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
733 sh = VkKeyScanW(ptr[i]);
734 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
735 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(LOBYTE(sh), 0);
736 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
738 ir[1] = ir[0];
739 ir[1].Event.KeyEvent.bKeyDown = FALSE;
741 WriteConsoleInputW(data->hConIn, ir, 2, &n);
743 GlobalUnlock(h);
745 CloseClipboard();
748 /******************************************************************
749 * WCUSER_Refresh
753 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
755 WCUSER_FillMemDC(data, tp, bm);
756 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
758 RECT r;
760 r.left = 0;
761 r.right = data->curcfg.win_width * data->curcfg.cell_width;
762 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
763 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
764 InvalidateRect(data->hWnd, &r, FALSE);
765 UpdateWindow(data->hWnd);
769 /******************************************************************
770 * WCUSER_Paint
774 static void WCUSER_Paint(const struct inner_data* data)
776 PAINTSTRUCT ps;
778 if (data->in_set_config) return; /* in order to avoid some flicker */
779 BeginPaint(data->hWnd, &ps);
780 BitBlt(ps.hdc, 0, 0,
781 data->curcfg.win_width * data->curcfg.cell_width,
782 data->curcfg.win_height * data->curcfg.cell_height,
783 PRIVATE(data)->hMemDC,
784 data->curcfg.win_pos.X * data->curcfg.cell_width,
785 data->curcfg.win_pos.Y * data->curcfg.cell_height,
786 SRCCOPY);
787 if (PRIVATE(data)->has_selection)
788 WCUSER_SetSelection(data, ps.hdc);
789 EndPaint(data->hWnd, &ps);
792 /******************************************************************
793 * WCUSER_Scroll
797 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
799 if (horz)
801 ScrollWindow(data->hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, NULL, NULL);
802 SetScrollPos(data->hWnd, SB_HORZ, pos, TRUE);
803 data->curcfg.win_pos.X = pos;
805 else
807 ScrollWindow(data->hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, NULL, NULL);
808 SetScrollPos(data->hWnd, SB_VERT, pos, TRUE);
809 data->curcfg.win_pos.Y = pos;
811 InvalidateRect(data->hWnd, NULL, FALSE);
814 /******************************************************************
815 * WCUSER_FillMenu
819 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
821 HMENU hSubMenu;
822 HINSTANCE hInstance = GetModuleHandleW(NULL);
823 WCHAR buff[256];
825 if (!hMenu) return FALSE;
827 /* FIXME: error handling & memory cleanup */
828 hSubMenu = CreateMenu();
829 if (!hSubMenu) return FALSE;
831 LoadStringW(hInstance, IDS_MARK, buff, ARRAY_SIZE(buff));
832 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
833 LoadStringW(hInstance, IDS_COPY, buff, ARRAY_SIZE(buff));
834 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
835 LoadStringW(hInstance, IDS_PASTE, buff, ARRAY_SIZE(buff));
836 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
837 LoadStringW(hInstance, IDS_SELECTALL, buff, ARRAY_SIZE(buff));
838 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
839 LoadStringW(hInstance, IDS_SCROLL, buff, ARRAY_SIZE(buff));
840 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
841 LoadStringW(hInstance, IDS_SEARCH, buff, ARRAY_SIZE(buff));
842 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
844 if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
845 LoadStringW(hInstance, IDS_EDIT, buff, ARRAY_SIZE(buff));
846 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
847 LoadStringW(hInstance, IDS_DEFAULT, buff, ARRAY_SIZE(buff));
848 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
849 LoadStringW(hInstance, IDS_PROPERTIES, buff, ARRAY_SIZE(buff));
850 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
852 return TRUE;
855 /******************************************************************
856 * WCUSER_SetMenuDetails
858 * Grays / ungrays the menu items according to their state
860 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
862 if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
864 EnableMenuItem(hMenu, IDS_COPY,
865 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
866 EnableMenuItem(hMenu, IDS_PASTE,
867 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
868 ? MF_ENABLED : MF_GRAYED));
869 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
870 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
873 /******************************************************************
874 * WCUSER_Create
876 * Creates the window for the rendering
878 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
880 struct inner_data* data;
881 HMENU hSysMenu;
883 data = lpcs->lpCreateParams;
884 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
885 data->hWnd = hWnd;
887 hSysMenu = GetSystemMenu(hWnd, FALSE);
888 if (!hSysMenu) return 0;
889 PRIVATE(data)->hPopMenu = CreatePopupMenu();
890 if (!PRIVATE(data)->hPopMenu) return 0;
892 WCUSER_FillMenu(hSysMenu, TRUE);
893 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
895 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
896 if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
898 data->curcfg.quick_edit = FALSE;
899 return 0;
902 /******************************************************************
903 * WCUSER_GetCtrlKeyState
905 * Get the console bit mask equivalent to the VK_ status in keyState
907 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
909 DWORD ret = 0;
911 GetKeyboardState(keyState);
912 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
913 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
914 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
915 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
916 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
917 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
918 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
919 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
921 return ret;
924 /******************************************************************
925 * WCUSER_HandleSelectionKey
927 * Handles keys while selecting an area
929 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
930 WPARAM wParam, LPARAM lParam)
932 BYTE keyState[256];
933 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
934 COORD c1, c2;
936 if (!down) return;
938 switch (state)
940 case 0:
941 switch (wParam)
943 case VK_RETURN:
944 PRIVATE(data)->has_selection = FALSE;
945 WCUSER_SetSelection(data, 0);
946 WCUSER_CopySelectionToClipboard(data);
947 return;
948 case VK_RIGHT:
949 c1 = PRIVATE(data)->selectPt1;
950 c2 = PRIVATE(data)->selectPt2;
951 c1.X++; c2.X++;
952 WCUSER_MoveSelection(data, c1, c2);
953 return;
954 case VK_LEFT:
955 c1 = PRIVATE(data)->selectPt1;
956 c2 = PRIVATE(data)->selectPt2;
957 c1.X--; c2.X--;
958 WCUSER_MoveSelection(data, c1, c2);
959 return;
960 case VK_UP:
961 c1 = PRIVATE(data)->selectPt1;
962 c2 = PRIVATE(data)->selectPt2;
963 c1.Y--; c2.Y--;
964 WCUSER_MoveSelection(data, c1, c2);
965 return;
966 case VK_DOWN:
967 c1 = PRIVATE(data)->selectPt1;
968 c2 = PRIVATE(data)->selectPt2;
969 c1.Y++; c2.Y++;
970 WCUSER_MoveSelection(data, c1, c2);
971 return;
973 break;
974 case SHIFT_PRESSED:
975 switch (wParam)
977 case VK_RIGHT:
978 c1 = PRIVATE(data)->selectPt1;
979 c2 = PRIVATE(data)->selectPt2;
980 c2.X++;
981 WCUSER_MoveSelection(data, c1, c2);
982 return;
983 case VK_LEFT:
984 c1 = PRIVATE(data)->selectPt1;
985 c2 = PRIVATE(data)->selectPt2;
986 c2.X--;
987 WCUSER_MoveSelection(data, c1, c2);
988 return;
989 case VK_UP:
990 c1 = PRIVATE(data)->selectPt1;
991 c2 = PRIVATE(data)->selectPt2;
992 c2.Y--;
993 WCUSER_MoveSelection(data, c1, c2);
994 return;
995 case VK_DOWN:
996 c1 = PRIVATE(data)->selectPt1;
997 c2 = PRIVATE(data)->selectPt2;
998 c2.Y++;
999 WCUSER_MoveSelection(data, c1, c2);
1000 return;
1002 break;
1005 if (wParam < VK_SPACE) /* Shift, Alt, Ctrl, Num Lock etc. */
1006 return;
1008 WCUSER_SetSelection(data, 0);
1009 PRIVATE(data)->has_selection = FALSE;
1012 /******************************************************************
1013 * WCUSER_GenerateKeyInputRecord
1015 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1017 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1018 WPARAM wParam, LPARAM lParam)
1020 INPUT_RECORD ir;
1021 DWORD n;
1022 WCHAR buf[2];
1023 static WCHAR last; /* keep last char seen as feed for key up message */
1024 BYTE keyState[256];
1026 ir.EventType = KEY_EVENT;
1027 ir.Event.KeyEvent.bKeyDown = down;
1028 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1029 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1031 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1033 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1034 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1035 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1037 if (down)
1039 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1041 case 2:
1042 /* FIXME... should generate two events... */
1043 /* fall through */
1044 case 1:
1045 last = buf[0];
1046 break;
1047 default:
1048 last = 0;
1049 break;
1052 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME: HACKY... and buggy because it should be a stack, not a single value */
1053 if (!down) last = 0;
1055 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1058 /******************************************************************
1059 * WCUSER_GenerateMouseInputRecord
1063 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1064 WPARAM wParam, DWORD event)
1066 INPUT_RECORD ir;
1067 BYTE keyState[256];
1068 DWORD mode, n;
1070 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1071 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1072 return;
1074 ir.EventType = MOUSE_EVENT;
1075 ir.Event.MouseEvent.dwMousePosition = c;
1076 ir.Event.MouseEvent.dwButtonState = 0;
1077 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1078 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1079 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1080 if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
1081 if (wParam & MK_SHIFT) ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
1082 if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1083 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1084 ir.Event.MouseEvent.dwEventFlags = event;
1086 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1089 /******************************************************************
1090 * WCUSER_Proc
1094 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1096 struct inner_data* data = (struct inner_data*)GetWindowLongPtrW(hWnd, 0);
1098 switch (uMsg)
1100 case WM_CREATE:
1101 return WCUSER_Create(hWnd, (LPCREATESTRUCTW)lParam);
1102 case WM_DESTROY:
1103 data->hWnd = 0;
1104 PostQuitMessage(0);
1105 break;
1106 case WM_PAINT:
1107 WCUSER_Paint(data);
1108 break;
1109 case WM_KEYDOWN:
1110 case WM_KEYUP:
1111 if (PRIVATE(data)->has_selection)
1112 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1113 else
1114 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam);
1115 break;
1116 case WM_SYSKEYDOWN:
1117 case WM_SYSKEYUP:
1118 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam);
1119 break;
1120 case WM_LBUTTONDOWN:
1121 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1123 if (PRIVATE(data)->has_selection)
1124 WCUSER_SetSelection(data, 0);
1126 if (data->curcfg.quick_edit && PRIVATE(data)->has_selection)
1128 PRIVATE(data)->has_selection = FALSE;
1130 else
1132 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1133 SetCapture(data->hWnd);
1134 WCUSER_SetSelection(data, 0);
1135 PRIVATE(data)->has_selection = TRUE;
1138 else
1140 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1142 break;
1143 case WM_MOUSEMOVE:
1144 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1146 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection &&
1147 (wParam & MK_LBUTTON))
1149 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1152 else
1154 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1156 break;
1157 case WM_LBUTTONUP:
1158 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1160 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection)
1162 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1163 ReleaseCapture();
1166 else
1168 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1170 break;
1171 case WM_RBUTTONDOWN:
1172 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1174 POINT pt;
1176 pt.x = (short)LOWORD(lParam);
1177 pt.y = (short)HIWORD(lParam);
1178 ClientToScreen(hWnd, &pt);
1179 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1180 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON,
1181 pt.x, pt.y, 0, hWnd, NULL);
1183 else
1185 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1187 break;
1188 case WM_RBUTTONUP:
1189 /* no need to track for rbutton up when opening the popup... the event will be
1190 * swallowed by TrackPopupMenu */
1191 case WM_MBUTTONDOWN:
1192 case WM_MBUTTONUP:
1193 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1194 break;
1195 case WM_LBUTTONDBLCLK:
1196 case WM_MBUTTONDBLCLK:
1197 case WM_RBUTTONDBLCLK:
1198 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
1199 break;
1200 case WM_SETFOCUS:
1201 if (data->curcfg.cursor_visible)
1203 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
1204 data->curcfg.cell_width, data->curcfg.cell_height);
1205 WCUSER_PosCursor(data);
1207 break;
1208 case WM_KILLFOCUS:
1209 if (data->curcfg.cursor_visible)
1210 DestroyCaret();
1211 break;
1212 case WM_HSCROLL:
1214 struct config_data cfg = data->curcfg;
1216 switch (LOWORD(wParam))
1218 case SB_PAGEUP: cfg.win_pos.X -= 8; break;
1219 case SB_PAGEDOWN: cfg.win_pos.X += 8; break;
1220 case SB_LINEUP: cfg.win_pos.X--; break;
1221 case SB_LINEDOWN: cfg.win_pos.X++; break;
1222 case SB_THUMBTRACK: cfg.win_pos.X = HIWORD(wParam); break;
1223 default: break;
1225 if (cfg.win_pos.X < 0) cfg.win_pos.X = 0;
1226 if (cfg.win_pos.X > data->curcfg.sb_width - data->curcfg.win_width)
1227 cfg.win_pos.X = data->curcfg.sb_width - data->curcfg.win_width;
1228 if (cfg.win_pos.X != data->curcfg.win_pos.X)
1230 WINECON_SetConfig(data, &cfg);
1233 break;
1234 case WM_MOUSEWHEEL:
1235 if (data->curcfg.sb_height <= data->curcfg.win_height)
1237 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1238 break;
1240 /* else fallthrough */
1241 case WM_VSCROLL:
1243 struct config_data cfg = data->curcfg;
1245 if (uMsg == WM_MOUSEWHEEL)
1247 UINT scrollLines = 3;
1248 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
1249 scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1250 cfg.win_pos.Y += scrollLines;
1251 } else {
1252 switch (LOWORD(wParam))
1254 case SB_PAGEUP: cfg.win_pos.Y -= 8; break;
1255 case SB_PAGEDOWN: cfg.win_pos.Y += 8; break;
1256 case SB_LINEUP: cfg.win_pos.Y--; break;
1257 case SB_LINEDOWN: cfg.win_pos.Y++; break;
1258 case SB_THUMBTRACK: cfg.win_pos.Y = HIWORD(wParam); break;
1259 default: break;
1263 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
1264 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
1265 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
1266 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
1268 WINECON_SetConfig(data, &cfg);
1271 break;
1272 case WM_SYSCOMMAND:
1273 switch (wParam)
1275 case IDS_DEFAULT:
1276 WCUSER_GetProperties(data, FALSE);
1277 break;
1278 case IDS_PROPERTIES:
1279 WCUSER_GetProperties(data, TRUE);
1280 break;
1281 default:
1282 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1284 break;
1285 case WM_COMMAND:
1286 switch (wParam)
1288 case IDS_DEFAULT:
1289 WCUSER_GetProperties(data, FALSE);
1290 break;
1291 case IDS_PROPERTIES:
1292 WCUSER_GetProperties(data, TRUE);
1293 break;
1294 case IDS_MARK:
1295 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1296 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1297 WCUSER_SetSelection(data, 0);
1298 PRIVATE(data)->has_selection = TRUE;
1299 break;
1300 case IDS_COPY:
1301 if (PRIVATE(data)->has_selection)
1303 PRIVATE(data)->has_selection = FALSE;
1304 WCUSER_SetSelection(data, 0);
1305 WCUSER_CopySelectionToClipboard(data);
1307 break;
1308 case IDS_PASTE:
1309 WCUSER_PasteFromClipboard(data);
1310 break;
1311 case IDS_SELECTALL:
1312 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1313 PRIVATE(data)->selectPt2.X = data->curcfg.sb_width - 1;
1314 PRIVATE(data)->selectPt2.Y = data->curcfg.sb_height - 1;
1315 WCUSER_SetSelection(data, 0);
1316 PRIVATE(data)->has_selection = TRUE;
1317 break;
1318 case IDS_SCROLL:
1319 case IDS_SEARCH:
1320 WINE_FIXME("Unhandled yet command: %lx\n", wParam);
1321 break;
1322 default:
1323 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1325 break;
1326 case WM_INITMENUPOPUP:
1327 if (!HIWORD(lParam)) return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1328 WCUSER_SetMenuDetails(data, GetSystemMenu(data->hWnd, FALSE));
1329 break;
1330 case WM_SIZE:
1331 WINECON_ResizeWithContainer(data, LOWORD(lParam) / data->curcfg.cell_width,
1332 HIWORD(lParam) / data->curcfg.cell_height);
1333 break;
1334 default:
1335 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1337 return 0;
1340 /******************************************************************
1341 * WCUSER_DeleteBackend
1345 static void WCUSER_DeleteBackend(struct inner_data* data)
1347 if (!PRIVATE(data)) return;
1348 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1349 if (data->hWnd) DestroyWindow(data->hWnd);
1350 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1351 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1352 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1353 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1356 /******************************************************************
1357 * WCUSER_MainLoop
1361 static int WCUSER_MainLoop(struct inner_data* data)
1363 MSG msg;
1365 ShowWindow(data->hWnd, data->nCmdShow);
1366 while (!data->dying || !data->curcfg.exit_on_die)
1368 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1370 case WAIT_OBJECT_0:
1371 WINECON_GrabChanges(data);
1372 break;
1373 case WAIT_OBJECT_0+1:
1374 /* need to use PeekMessageW loop instead of simple GetMessage:
1375 * multiple messages might have arrived in between,
1376 * so GetMessage would lead to delayed processing */
1377 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
1379 if (msg.message == WM_QUIT) return 1;
1380 WINE_TRACE("dispatching msg %04x\n", msg.message);
1381 DispatchMessageW(&msg);
1383 break;
1384 default:
1385 WINE_ERR("got pb\n");
1386 /* err */
1387 break;
1390 PostQuitMessage(0);
1391 return 0;
1394 /******************************************************************
1395 * WCUSER_InitBackend
1397 * Initialisation part II: creation of window.
1400 enum init_return WCUSER_InitBackend(struct inner_data* data)
1402 static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1404 WNDCLASSW wndclass;
1405 CHARSETINFO ci;
1407 if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
1408 return init_failed;
1409 g_uiDefaultCharset = ci.ciCharset;
1410 WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1412 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1413 if (!data->private) return init_failed;
1415 data->fnMainLoop = WCUSER_MainLoop;
1416 data->fnPosCursor = WCUSER_PosCursor;
1417 data->fnShapeCursor = WCUSER_ShapeCursor;
1418 data->fnComputePositions = WCUSER_ComputePositions;
1419 data->fnRefresh = WCUSER_Refresh;
1420 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1421 data->fnSetTitle = WCUSER_SetTitle;
1422 data->fnSetFont = WCUSER_SetFontPmt;
1423 data->fnScroll = WCUSER_Scroll;
1424 data->fnDeleteBackend = WCUSER_DeleteBackend;
1426 wndclass.style = CS_DBLCLKS;
1427 wndclass.lpfnWndProc = WCUSER_Proc;
1428 wndclass.cbClsExtra = 0;
1429 wndclass.cbWndExtra = sizeof(DWORD_PTR);
1430 wndclass.hInstance = GetModuleHandleW(NULL);
1431 wndclass.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
1432 wndclass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1433 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1434 wndclass.lpszMenuName = NULL;
1435 wndclass.lpszClassName = wClassName;
1437 RegisterClassW(&wndclass);
1439 data->hWnd = CreateWindowW(wndclass.lpszClassName, NULL,
1440 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1441 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1442 if (!data->hWnd) return init_not_supported;
1444 return init_success;