hidclass.sys: Use the PDO instance id for the HID instance id.
[wine.git] / programs / wineconsole / user.c
blobeb68373e3cc58598e8f6c4cc475673678854df57
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))))
60 WINECON_Fatal("OOM\n");
61 dx = HeapAlloc( GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(*dx) );
63 hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
64 for (j = upd_tp; j <= upd_bm; j++)
66 cell = &data->cells[j * data->curcfg.sb_width];
67 for (i = 0; i < data->curcfg.sb_width; i++)
69 attr = cell[i].Attributes;
70 SetBkColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[(attr >> 4) & 0x0F]);
71 SetTextColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[attr & 0x0F]);
72 for (k = i; k < data->curcfg.sb_width && cell[k].Attributes == attr; k++)
74 line[k - i] = cell[k].Char.UnicodeChar;
75 dx[k - i] = data->curcfg.cell_width;
77 ExtTextOutW( PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
78 0, NULL, line, k - i, dx );
79 if (PRIVATE(data)->ext_leading &&
80 (hbr = CreateSolidBrush(data->curcfg.color_map[(attr >> 4) & 0x0F])))
82 r.left = i * data->curcfg.cell_width;
83 r.top = (j + 1) * data->curcfg.cell_height - PRIVATE(data)->ext_leading;
84 r.right = k * data->curcfg.cell_width;
85 r.bottom = (j + 1) * data->curcfg.cell_height;
86 FillRect(PRIVATE(data)->hMemDC, &r, hbr);
87 DeleteObject(hbr);
89 i = k - 1;
92 SelectObject(PRIVATE(data)->hMemDC, hOldFont);
93 HeapFree(GetProcessHeap(), 0, dx);
94 HeapFree(GetProcessHeap(), 0, line);
97 /******************************************************************
98 * WCUSER_NewBitmap
100 * Either the font geometry or the sb geometry has changed. we need
101 * to recreate the bitmap geometry.
103 static void WCUSER_NewBitmap(struct inner_data* data)
105 HDC hDC;
106 HBITMAP hnew, hold;
108 if (!data->curcfg.sb_width || !data->curcfg.sb_height ||
109 !PRIVATE(data)->hFont || !(hDC = GetDC(data->hWnd)))
110 return;
111 hnew = CreateCompatibleBitmap(hDC,
112 data->curcfg.sb_width * data->curcfg.cell_width,
113 data->curcfg.sb_height * data->curcfg.cell_height);
114 ReleaseDC(data->hWnd, hDC);
115 hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
117 if (PRIVATE(data)->hBitmap)
119 if (hold == PRIVATE(data)->hBitmap)
120 DeleteObject(PRIVATE(data)->hBitmap);
121 else
122 WINE_FIXME("leak\n");
124 PRIVATE(data)->hBitmap = hnew;
125 WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
128 /******************************************************************
129 * WCUSER_ResizeScreenBuffer
133 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
135 WCUSER_NewBitmap(data);
138 /******************************************************************
139 * WCUSER_PosCursor
141 * Set a new position for the cursor
143 static void WCUSER_PosCursor(const struct inner_data* data)
145 if (data->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
147 SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
148 (data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
149 ShowCaret(data->hWnd);
152 /******************************************************************
153 * WCUSER_ShapeCursor
155 * Sets a new shape for the cursor
157 static void WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
159 if (force || size != data->curcfg.cursor_size)
161 if (data->curcfg.cursor_visible && data->hWnd == GetFocus()) DestroyCaret();
162 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
163 PRIVATE(data)->cursor_bitmap = NULL;
164 if (size != 100)
166 int w16b; /* number of bytes per row, aligned on word size */
167 BYTE* ptr;
168 int i, j, nbl;
170 w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
171 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
172 if (!ptr) WINECON_Fatal("OOM");
173 nbl = max((data->curcfg.cell_height * size) / 100, 1);
174 for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
176 for (i = 0; i < data->curcfg.cell_width; i++)
178 ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
181 PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
182 data->curcfg.cell_height, 1, 1, ptr);
183 HeapFree(GetProcessHeap(), 0, ptr);
185 data->curcfg.cursor_size = size;
186 data->curcfg.cursor_visible = -1;
189 vis = vis != 0;
190 if (force || vis != data->curcfg.cursor_visible)
192 data->curcfg.cursor_visible = vis;
193 if (data->hWnd == GetFocus())
195 if (vis)
197 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
198 data->curcfg.cell_width, data->curcfg.cell_height);
199 WCUSER_PosCursor(data);
201 else
203 DestroyCaret();
207 WINECON_DumpConfig("crsr", &data->curcfg);
210 /******************************************************************
211 * WCUSER_ComputePositions
213 * Recomputes all the components (mainly scroll bars) positions
215 static void WCUSER_ComputePositions(struct inner_data* data)
217 RECT r;
218 int dx, dy;
220 /* compute window size from desired client size */
221 r.left = r.top = 0;
222 r.right = data->curcfg.win_width * data->curcfg.cell_width;
223 r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
225 if (IsRectEmpty(&r)) return;
227 AdjustWindowRect(&r, GetWindowLongW(data->hWnd, GWL_STYLE), FALSE);
229 dx = dy = 0;
230 if (data->curcfg.sb_width > data->curcfg.win_width)
232 dy = GetSystemMetrics(SM_CYHSCROLL);
233 SetScrollRange(data->hWnd, SB_HORZ, 0,
234 data->curcfg.sb_width - data->curcfg.win_width, FALSE);
235 SetScrollPos(data->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
236 ShowScrollBar(data->hWnd, SB_HORZ, TRUE);
238 else
240 ShowScrollBar(data->hWnd, SB_HORZ, FALSE);
243 if (data->curcfg.sb_height > data->curcfg.win_height)
245 dx = GetSystemMetrics(SM_CXVSCROLL);
246 SetScrollRange(data->hWnd, SB_VERT, 0,
247 data->curcfg.sb_height - data->curcfg.win_height, FALSE);
248 SetScrollPos(data->hWnd, SB_VERT, 0, FALSE); /* FIXME */
249 ShowScrollBar(data->hWnd, SB_VERT, TRUE);
251 else
253 ShowScrollBar(data->hWnd, SB_VERT, FALSE);
256 SetWindowPos(data->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
257 SWP_NOMOVE|SWP_NOZORDER);
258 WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
259 WCUSER_PosCursor(data);
262 /******************************************************************
263 * WCUSER_SetTitle
265 * Sets the title to the wine console
267 static void WCUSER_SetTitle(const struct inner_data* data)
269 WCHAR buffer[256];
271 if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
272 SetWindowTextW(data->hWnd, buffer);
275 void WCUSER_DumpLogFont(const char* pfx, const LOGFONTW* lf, DWORD ft)
277 WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
278 "\tlf.lfHeight=%d lf.lfWidth=%d lf.lfEscapement=%d lf.lfOrientation=%d\n"
279 "\tlf.lfWeight=%d lf.lfItalic=%u lf.lfUnderline=%u lf.lfStrikeOut=%u\n"
280 "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
281 "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
282 pfx,
283 (ft & RASTER_FONTTYPE) ? "raster" : "",
284 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
285 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
286 (ft & DEVICE_FONTTYPE) ? "|device" : "",
287 lf->lfHeight, lf->lfWidth, lf->lfEscapement, lf->lfOrientation,
288 lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
289 lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
290 wine_dbgstr_w(lf->lfFaceName));
293 void WCUSER_DumpTextMetric(const TEXTMETRICW* tm, DWORD ft)
295 WINE_TRACE_(wc_font)("%s%s%s%s\n"
296 "\ttmHeight=%d tmAscent=%d tmDescent=%d tmInternalLeading=%d tmExternalLeading=%d\n"
297 "\ttmAveCharWidth=%d tmMaxCharWidth=%d tmWeight=%d tmOverhang=%d\n"
298 "\ttmDigitizedAspectX=%d tmDigitizedAspectY=%d\n"
299 "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
300 "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
301 (ft & RASTER_FONTTYPE) ? "raster" : "",
302 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
303 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
304 (ft & DEVICE_FONTTYPE) ? "|device" : "",
305 tm->tmHeight, tm->tmAscent, tm->tmDescent, tm->tmInternalLeading, tm->tmExternalLeading, tm->tmAveCharWidth,
306 tm->tmMaxCharWidth, tm->tmWeight, tm->tmOverhang, tm->tmDigitizedAspectX, tm->tmDigitizedAspectY,
307 tm->tmFirstChar, tm->tmLastChar, tm->tmDefaultChar, tm->tmBreakChar, tm->tmItalic, tm->tmUnderlined, tm->tmStruckOut,
308 tm->tmPitchAndFamily, tm->tmCharSet);
311 /******************************************************************
312 * WCUSER_AreFontsEqual
316 static BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONTW* lf)
318 return lf->lfHeight == config->cell_height &&
319 lf->lfWeight == config->font_weight &&
320 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
321 !lstrcmpW(lf->lfFaceName, config->face_name);
324 struct font_chooser
326 struct inner_data* data;
327 int pass;
328 BOOL done;
331 /******************************************************************
332 * WCUSER_ValidateFontMetric
334 * Returns true if the font described in tm is usable as a font for the renderer
336 BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRICW* tm,
337 DWORD type, int pass)
339 switch (pass) /* we get increasingly lenient in later passes */
341 case 0:
342 if (type & RASTER_FONTTYPE)
344 if (tm->tmMaxCharWidth * data->curcfg.win_width >= GetSystemMetrics(SM_CXSCREEN) ||
345 tm->tmHeight * data->curcfg.win_height >= GetSystemMetrics(SM_CYSCREEN))
346 return FALSE;
348 /* fall through */
349 case 1:
350 if (tm->tmCharSet != DEFAULT_CHARSET && tm->tmCharSet != g_uiDefaultCharset) return FALSE;
351 /* fall through */
352 case 2:
353 if (tm->tmItalic || tm->tmUnderlined || tm->tmStruckOut) return FALSE;
354 break;
356 return TRUE;
359 /******************************************************************
360 * WCUSER_ValidateFont
362 * Returns true if the font family described in lf is usable as a font for the renderer
364 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONTW* lf, int pass)
366 switch (pass) /* we get increasingly lenient in later passes */
368 case 0:
369 case 1:
370 if (lf->lfCharSet != DEFAULT_CHARSET && lf->lfCharSet != g_uiDefaultCharset) return FALSE;
371 /* fall through */
372 case 2:
373 if ((lf->lfPitchAndFamily & 3) != FIXED_PITCH) return FALSE;
374 /* fall through */
375 case 3:
376 if (lf->lfFaceName[0] == '@') return FALSE;
377 break;
379 return TRUE;
382 /******************************************************************
383 * get_first_font_enum_2
384 * get_first_font_enum
386 * Helper functions to get a decent font for the renderer
388 static int CALLBACK get_first_font_enum_2(const LOGFONTW* lf, const TEXTMETRICW* tm,
389 DWORD FontType, LPARAM lParam)
391 struct font_chooser* fc = (struct font_chooser*)lParam;
393 WCUSER_DumpTextMetric(tm, FontType);
394 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType, fc->pass))
396 LOGFONTW mlf = *lf;
398 /* Use the default sizes for the font (this is needed, especially for
399 * TrueType fonts, so that we get a decent size, not the max size)
401 mlf.lfWidth = fc->data->curcfg.cell_width;
402 mlf.lfHeight = fc->data->curcfg.cell_height;
403 if (WCUSER_SetFont(fc->data, &mlf))
405 struct config_data defcfg;
407 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
408 fc->done = 1;
409 /* since we've modified the current config with new font information,
410 * set this information as the new default.
412 WINECON_RegLoad(NULL, &defcfg);
413 defcfg.cell_width = fc->data->curcfg.cell_width;
414 defcfg.cell_height = fc->data->curcfg.cell_height;
415 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
416 /* Force also its writing back to the registry so that we can get it
417 * the next time.
419 WINECON_RegSave(&defcfg);
420 return 0;
423 return 1;
426 static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
427 DWORD FontType, LPARAM lParam)
429 struct font_chooser* fc = (struct font_chooser*)lParam;
431 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
432 if (WCUSER_ValidateFont(fc->data, lf, fc->pass))
434 EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
435 get_first_font_enum_2, lParam);
436 return !fc->done; /* we just need the first matching one... */
438 return 1;
441 /******************************************************************
442 * WCUSER_CopyFont
444 * get the relevant information from the font described in lf and store them
445 * in config
447 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
449 TEXTMETRICW tm;
450 HDC hDC;
451 HFONT hFont, hOldFont;
453 if (!(hDC = GetDC(hWnd))) return NULL;
454 if (!(hFont = CreateFontIndirectW(lf)))
456 ReleaseDC(hWnd, hDC);
457 return NULL;
459 hOldFont = SelectObject(hDC, hFont);
460 GetTextMetricsW(hDC, &tm);
461 SelectObject(hDC, hOldFont);
462 ReleaseDC(hWnd, hDC);
464 config->cell_width = tm.tmMaxCharWidth;
465 config->cell_height = tm.tmHeight + tm.tmExternalLeading;
466 config->font_weight = tm.tmWeight;
467 lstrcpyW(config->face_name, lf->lfFaceName);
468 if (el) *el = tm.tmExternalLeading;
470 return hFont;
473 /******************************************************************
474 * WCUSER_FillLogFont
478 void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
480 lf->lfHeight = height;
481 lf->lfWidth = 0;
482 lf->lfEscapement = 0;
483 lf->lfOrientation = 0;
484 lf->lfWeight = weight;
485 lf->lfItalic = FALSE;
486 lf->lfUnderline = FALSE;
487 lf->lfStrikeOut = FALSE;
488 lf->lfCharSet = DEFAULT_CHARSET;
489 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
490 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
491 lf->lfQuality = DEFAULT_QUALITY;
492 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
493 lstrcpyW(lf->lfFaceName, name);
496 /******************************************************************
497 * WCUSER_SetFont
499 * sets logfont as the new font for the console
501 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
503 HFONT hFont;
504 LONG el;
506 if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
507 return TRUE;
509 hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
510 if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
512 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
513 PRIVATE(data)->hFont = hFont;
514 PRIVATE(data)->ext_leading = el;
516 WCUSER_ComputePositions(data);
517 WCUSER_NewBitmap(data);
518 InvalidateRect(data->hWnd, NULL, FALSE);
519 UpdateWindow(data->hWnd);
521 return TRUE;
524 /******************************************************************
525 * WCUSER_SetFontPmt
527 * Sets a new font for the console.
528 * In fact a wrapper for WCUSER_SetFont
530 static void WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
531 unsigned height, unsigned weight)
533 LOGFONTW lf;
534 struct font_chooser fc;
536 WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
537 wine_dbgstr_wn(font, -1), height, weight);
539 if (font[0] != '\0' && height != 0 && weight != 0)
541 WCUSER_FillLogFont(&lf, font, height, weight);
542 if (WCUSER_SetFont(data, &lf))
544 WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
545 return;
549 /* try to find an acceptable font */
550 WINE_WARN("Couldn't match the font from registry... trying to find one\n");
551 fc.data = data;
552 fc.done = FALSE;
553 for (fc.pass = 0; fc.pass <= 4; fc.pass++)
555 EnumFontFamiliesW(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
556 if (fc.done) return;
558 WINECON_Fatal("Couldn't find a decent font, aborting\n");
561 /******************************************************************
562 * WCUSER_GetCell
564 * Get a cell from a relative coordinate in window (takes into
565 * account the scrolling)
567 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
569 COORD c;
571 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
572 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
574 return c;
577 /******************************************************************
578 * WCUSER_GetSelectionRect
580 * Get the selection rectangle
582 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
584 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) - data->curcfg.win_pos.X) * data->curcfg.cell_width;
585 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
586 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
587 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
590 /******************************************************************
591 * WCUSER_SetSelection
595 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
597 HDC hDC;
598 RECT r;
600 WCUSER_GetSelectionRect(data, &r);
601 hDC = hRefDC ? hRefDC : GetDC(data->hWnd);
602 if (hDC)
604 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
605 HideCaret(data->hWnd);
606 InvertRect(hDC, &r);
607 if (hDC != hRefDC)
608 ReleaseDC(data->hWnd, hDC);
609 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
610 ShowCaret(data->hWnd);
614 /******************************************************************
615 * WCUSER_MoveSelection
619 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
621 RECT r;
622 HDC hDC;
624 if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
625 c2.X < 0 || c2.X >= data->curcfg.sb_width ||
626 c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
627 c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
628 return;
630 WCUSER_GetSelectionRect(data, &r);
631 hDC = GetDC(data->hWnd);
632 if (hDC)
634 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
635 HideCaret(data->hWnd);
636 InvertRect(hDC, &r);
638 PRIVATE(data)->selectPt1 = c1;
639 PRIVATE(data)->selectPt2 = c2;
640 if (hDC)
642 WCUSER_GetSelectionRect(data, &r);
643 InvertRect(hDC, &r);
644 ReleaseDC(data->hWnd, hDC);
645 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
646 ShowCaret(data->hWnd);
650 /******************************************************************
651 * WCUSER_CopySelectionToClipboard
653 * Copies the current selection into the clipboard
655 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
657 HANDLE hMem;
658 LPWSTR p;
659 unsigned w, h;
661 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
662 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
664 if (!OpenClipboard(data->hWnd)) return;
665 EmptyClipboard();
667 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
668 if (hMem && (p = GlobalLock(hMem)))
670 COORD c;
671 int y;
673 c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
674 c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
676 for (y = 0; y < h; y++, c.Y++)
678 LPWSTR end;
679 DWORD count;
681 ReadConsoleOutputCharacterW(data->hConOut, p, w - 1, c, &count);
683 /* strip spaces from the end of the line */
684 end = p + w - 1;
685 while (end > p && *(end - 1) == ' ')
686 end--;
687 *end = (y < h - 1) ? '\n' : '\0';
688 p = end + 1;
690 GlobalUnlock(hMem);
691 SetClipboardData(CF_UNICODETEXT, hMem);
693 CloseClipboard();
696 /******************************************************************
697 * WCUSER_PasteFromClipboard
701 static void WCUSER_PasteFromClipboard(struct inner_data* data)
703 HANDLE h;
704 WCHAR* ptr;
706 if (!OpenClipboard(data->hWnd)) return;
707 h = GetClipboardData(CF_UNICODETEXT);
708 if (h && (ptr = GlobalLock(h)))
710 int i, len = GlobalSize(h) / sizeof(WCHAR);
711 INPUT_RECORD ir[2];
712 DWORD n;
713 SHORT sh;
715 ir[0].EventType = KEY_EVENT;
716 ir[0].Event.KeyEvent.wRepeatCount = 0;
717 ir[0].Event.KeyEvent.dwControlKeyState = 0;
718 ir[0].Event.KeyEvent.bKeyDown = TRUE;
720 /* generate the corresponding input records */
721 for (i = 0; i < len; i++)
723 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
724 sh = VkKeyScanW(ptr[i]);
725 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
726 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(LOBYTE(sh), 0);
727 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
729 ir[1] = ir[0];
730 ir[1].Event.KeyEvent.bKeyDown = FALSE;
732 WriteConsoleInputW(data->hConIn, ir, 2, &n);
734 GlobalUnlock(h);
736 CloseClipboard();
739 /******************************************************************
740 * WCUSER_Refresh
744 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
746 WCUSER_FillMemDC(data, tp, bm);
747 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
749 RECT r;
751 r.left = 0;
752 r.right = data->curcfg.win_width * data->curcfg.cell_width;
753 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
754 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
755 InvalidateRect(data->hWnd, &r, FALSE);
756 UpdateWindow(data->hWnd);
760 /******************************************************************
761 * WCUSER_Paint
765 static void WCUSER_Paint(const struct inner_data* data)
767 PAINTSTRUCT ps;
769 if (data->in_set_config) return; /* in order to avoid some flicker */
770 BeginPaint(data->hWnd, &ps);
771 BitBlt(ps.hdc, 0, 0,
772 data->curcfg.win_width * data->curcfg.cell_width,
773 data->curcfg.win_height * data->curcfg.cell_height,
774 PRIVATE(data)->hMemDC,
775 data->curcfg.win_pos.X * data->curcfg.cell_width,
776 data->curcfg.win_pos.Y * data->curcfg.cell_height,
777 SRCCOPY);
778 if (PRIVATE(data)->has_selection)
779 WCUSER_SetSelection(data, ps.hdc);
780 EndPaint(data->hWnd, &ps);
783 /******************************************************************
784 * WCUSER_Scroll
788 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
790 if (horz)
792 ScrollWindow(data->hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, NULL, NULL);
793 SetScrollPos(data->hWnd, SB_HORZ, pos, TRUE);
794 data->curcfg.win_pos.X = pos;
796 else
798 ScrollWindow(data->hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, NULL, NULL);
799 SetScrollPos(data->hWnd, SB_VERT, pos, TRUE);
800 data->curcfg.win_pos.Y = pos;
802 InvalidateRect(data->hWnd, NULL, FALSE);
805 /******************************************************************
806 * WCUSER_FillMenu
810 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
812 HMENU hSubMenu;
813 HINSTANCE hInstance = GetModuleHandleW(NULL);
814 WCHAR buff[256];
816 if (!hMenu) return FALSE;
818 /* FIXME: error handling & memory cleanup */
819 hSubMenu = CreateMenu();
820 if (!hSubMenu) return FALSE;
822 LoadStringW(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(buff[0]));
823 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
824 LoadStringW(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(buff[0]));
825 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
826 LoadStringW(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(buff[0]));
827 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
828 LoadStringW(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(buff[0]));
829 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
830 LoadStringW(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(buff[0]));
831 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
832 LoadStringW(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(buff[0]));
833 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
835 if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
836 LoadStringW(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(buff[0]));
837 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
838 LoadStringW(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(buff[0]));
839 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
840 LoadStringW(hInstance, IDS_PROPERTIES, buff, sizeof(buff) / sizeof(buff[0]));
841 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
843 return TRUE;
846 /******************************************************************
847 * WCUSER_SetMenuDetails
849 * Grays / ungrays the menu items according to their state
851 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
853 if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
855 EnableMenuItem(hMenu, IDS_COPY,
856 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
857 EnableMenuItem(hMenu, IDS_PASTE,
858 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
859 ? MF_ENABLED : MF_GRAYED));
860 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
861 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
864 /******************************************************************
865 * WCUSER_Create
867 * Creates the window for the rendering
869 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
871 struct inner_data* data;
872 HMENU hSysMenu;
874 data = lpcs->lpCreateParams;
875 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
876 data->hWnd = hWnd;
878 hSysMenu = GetSystemMenu(hWnd, FALSE);
879 if (!hSysMenu) return 0;
880 PRIVATE(data)->hPopMenu = CreatePopupMenu();
881 if (!PRIVATE(data)->hPopMenu) return 0;
883 WCUSER_FillMenu(hSysMenu, TRUE);
884 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
886 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
887 if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
889 data->curcfg.quick_edit = FALSE;
890 return 0;
893 /******************************************************************
894 * WCUSER_GetCtrlKeyState
896 * Get the console bit mask equivalent to the VK_ status in keyState
898 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
900 DWORD ret = 0;
902 GetKeyboardState(keyState);
903 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
904 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
905 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
906 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
907 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
908 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
909 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
910 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
912 return ret;
915 /******************************************************************
916 * WCUSER_HandleSelectionKey
918 * Handles keys while selecting an area
920 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
921 WPARAM wParam, LPARAM lParam)
923 BYTE keyState[256];
924 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
925 COORD c1, c2;
927 if (!down) return;
929 switch (state)
931 case 0:
932 switch (wParam)
934 case VK_RETURN:
935 PRIVATE(data)->has_selection = FALSE;
936 WCUSER_SetSelection(data, 0);
937 WCUSER_CopySelectionToClipboard(data);
938 return;
939 case VK_RIGHT:
940 c1 = PRIVATE(data)->selectPt1;
941 c2 = PRIVATE(data)->selectPt2;
942 c1.X++; c2.X++;
943 WCUSER_MoveSelection(data, c1, c2);
944 return;
945 case VK_LEFT:
946 c1 = PRIVATE(data)->selectPt1;
947 c2 = PRIVATE(data)->selectPt2;
948 c1.X--; c2.X--;
949 WCUSER_MoveSelection(data, c1, c2);
950 return;
951 case VK_UP:
952 c1 = PRIVATE(data)->selectPt1;
953 c2 = PRIVATE(data)->selectPt2;
954 c1.Y--; c2.Y--;
955 WCUSER_MoveSelection(data, c1, c2);
956 return;
957 case VK_DOWN:
958 c1 = PRIVATE(data)->selectPt1;
959 c2 = PRIVATE(data)->selectPt2;
960 c1.Y++; c2.Y++;
961 WCUSER_MoveSelection(data, c1, c2);
962 return;
964 break;
965 case SHIFT_PRESSED:
966 switch (wParam)
968 case VK_RIGHT:
969 c1 = PRIVATE(data)->selectPt1;
970 c2 = PRIVATE(data)->selectPt2;
971 c2.X++;
972 WCUSER_MoveSelection(data, c1, c2);
973 return;
974 case VK_LEFT:
975 c1 = PRIVATE(data)->selectPt1;
976 c2 = PRIVATE(data)->selectPt2;
977 c2.X--;
978 WCUSER_MoveSelection(data, c1, c2);
979 return;
980 case VK_UP:
981 c1 = PRIVATE(data)->selectPt1;
982 c2 = PRIVATE(data)->selectPt2;
983 c2.Y--;
984 WCUSER_MoveSelection(data, c1, c2);
985 return;
986 case VK_DOWN:
987 c1 = PRIVATE(data)->selectPt1;
988 c2 = PRIVATE(data)->selectPt2;
989 c2.Y++;
990 WCUSER_MoveSelection(data, c1, c2);
991 return;
993 break;
996 if (wParam < VK_SPACE) /* Shift, Alt, Ctrl, Num Lock etc. */
997 return;
999 WCUSER_SetSelection(data, 0);
1000 PRIVATE(data)->has_selection = FALSE;
1003 /******************************************************************
1004 * WCUSER_GenerateKeyInputRecord
1006 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1008 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1009 WPARAM wParam, LPARAM lParam)
1011 INPUT_RECORD ir;
1012 DWORD n;
1013 WCHAR buf[2];
1014 static WCHAR last; /* keep last char seen as feed for key up message */
1015 BYTE keyState[256];
1017 ir.EventType = KEY_EVENT;
1018 ir.Event.KeyEvent.bKeyDown = down;
1019 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1020 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1022 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1024 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1025 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1026 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1028 if (down)
1030 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1032 case 2:
1033 /* FIXME... should generate two events... */
1034 /* fall through */
1035 case 1:
1036 last = buf[0];
1037 break;
1038 default:
1039 last = 0;
1040 break;
1043 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME: HACKY... and buggy because it should be a stack, not a single value */
1044 if (!down) last = 0;
1046 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1049 /******************************************************************
1050 * WCUSER_GenerateMouseInputRecord
1054 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1055 WPARAM wParam, DWORD event)
1057 INPUT_RECORD ir;
1058 BYTE keyState[256];
1059 DWORD mode, n;
1061 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1062 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1063 return;
1065 ir.EventType = MOUSE_EVENT;
1066 ir.Event.MouseEvent.dwMousePosition = c;
1067 ir.Event.MouseEvent.dwButtonState = 0;
1068 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1069 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1070 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1071 if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
1072 if (wParam & MK_SHIFT) ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
1073 if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1074 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1075 ir.Event.MouseEvent.dwEventFlags = event;
1077 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1080 /******************************************************************
1081 * WCUSER_Proc
1085 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1087 struct inner_data* data = (struct inner_data*)GetWindowLongPtrW(hWnd, 0);
1089 switch (uMsg)
1091 case WM_CREATE:
1092 return WCUSER_Create(hWnd, (LPCREATESTRUCTW)lParam);
1093 case WM_DESTROY:
1094 data->hWnd = 0;
1095 PostQuitMessage(0);
1096 break;
1097 case WM_PAINT:
1098 WCUSER_Paint(data);
1099 break;
1100 case WM_KEYDOWN:
1101 case WM_KEYUP:
1102 if (PRIVATE(data)->has_selection)
1103 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1104 else
1105 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam);
1106 break;
1107 case WM_SYSKEYDOWN:
1108 case WM_SYSKEYUP:
1109 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam);
1110 break;
1111 case WM_LBUTTONDOWN:
1112 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1114 if (PRIVATE(data)->has_selection)
1115 WCUSER_SetSelection(data, 0);
1117 if (data->curcfg.quick_edit && PRIVATE(data)->has_selection)
1119 PRIVATE(data)->has_selection = FALSE;
1121 else
1123 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1124 SetCapture(data->hWnd);
1125 WCUSER_SetSelection(data, 0);
1126 PRIVATE(data)->has_selection = TRUE;
1129 else
1131 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1133 break;
1134 case WM_MOUSEMOVE:
1135 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1137 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection &&
1138 (wParam & MK_LBUTTON))
1140 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1143 else
1145 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1147 break;
1148 case WM_LBUTTONUP:
1149 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1151 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection)
1153 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1154 ReleaseCapture();
1157 else
1159 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1161 break;
1162 case WM_RBUTTONDOWN:
1163 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1165 POINT pt;
1167 pt.x = (short)LOWORD(lParam);
1168 pt.y = (short)HIWORD(lParam);
1169 ClientToScreen(hWnd, &pt);
1170 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1171 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON,
1172 pt.x, pt.y, 0, hWnd, NULL);
1174 else
1176 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1178 break;
1179 case WM_RBUTTONUP:
1180 /* no need to track for rbutton up when opening the popup... the event will be
1181 * swallowed by TrackPopupMenu */
1182 case WM_MBUTTONDOWN:
1183 case WM_MBUTTONUP:
1184 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1185 break;
1186 case WM_LBUTTONDBLCLK:
1187 case WM_MBUTTONDBLCLK:
1188 case WM_RBUTTONDBLCLK:
1189 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
1190 break;
1191 case WM_SETFOCUS:
1192 if (data->curcfg.cursor_visible)
1194 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
1195 data->curcfg.cell_width, data->curcfg.cell_height);
1196 WCUSER_PosCursor(data);
1198 break;
1199 case WM_KILLFOCUS:
1200 if (data->curcfg.cursor_visible)
1201 DestroyCaret();
1202 break;
1203 case WM_HSCROLL:
1205 struct config_data cfg = data->curcfg;
1207 switch (LOWORD(wParam))
1209 case SB_PAGEUP: cfg.win_pos.X -= 8; break;
1210 case SB_PAGEDOWN: cfg.win_pos.X += 8; break;
1211 case SB_LINEUP: cfg.win_pos.X--; break;
1212 case SB_LINEDOWN: cfg.win_pos.X++; break;
1213 case SB_THUMBTRACK: cfg.win_pos.X = HIWORD(wParam); break;
1214 default: break;
1216 if (cfg.win_pos.X < 0) cfg.win_pos.X = 0;
1217 if (cfg.win_pos.X > data->curcfg.sb_width - data->curcfg.win_width)
1218 cfg.win_pos.X = data->curcfg.sb_width - data->curcfg.win_width;
1219 if (cfg.win_pos.X != data->curcfg.win_pos.X)
1221 WINECON_SetConfig(data, &cfg);
1224 break;
1225 case WM_MOUSEWHEEL:
1226 if (data->curcfg.sb_height <= data->curcfg.win_height)
1228 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1229 break;
1231 /* else fallthrough */
1232 case WM_VSCROLL:
1234 struct config_data cfg = data->curcfg;
1236 if (uMsg == WM_MOUSEWHEEL)
1238 UINT scrollLines = 3;
1239 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
1240 scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1241 cfg.win_pos.Y += scrollLines;
1242 } else {
1243 switch (LOWORD(wParam))
1245 case SB_PAGEUP: cfg.win_pos.Y -= 8; break;
1246 case SB_PAGEDOWN: cfg.win_pos.Y += 8; break;
1247 case SB_LINEUP: cfg.win_pos.Y--; break;
1248 case SB_LINEDOWN: cfg.win_pos.Y++; break;
1249 case SB_THUMBTRACK: cfg.win_pos.Y = HIWORD(wParam); break;
1250 default: break;
1254 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
1255 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
1256 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
1257 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
1259 WINECON_SetConfig(data, &cfg);
1262 break;
1263 case WM_SYSCOMMAND:
1264 switch (wParam)
1266 case IDS_DEFAULT:
1267 WCUSER_GetProperties(data, FALSE);
1268 break;
1269 case IDS_PROPERTIES:
1270 WCUSER_GetProperties(data, TRUE);
1271 break;
1272 default:
1273 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1275 break;
1276 case WM_COMMAND:
1277 switch (wParam)
1279 case IDS_DEFAULT:
1280 WCUSER_GetProperties(data, FALSE);
1281 break;
1282 case IDS_PROPERTIES:
1283 WCUSER_GetProperties(data, TRUE);
1284 break;
1285 case IDS_MARK:
1286 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1287 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1288 WCUSER_SetSelection(data, 0);
1289 PRIVATE(data)->has_selection = TRUE;
1290 break;
1291 case IDS_COPY:
1292 if (PRIVATE(data)->has_selection)
1294 PRIVATE(data)->has_selection = FALSE;
1295 WCUSER_SetSelection(data, 0);
1296 WCUSER_CopySelectionToClipboard(data);
1298 break;
1299 case IDS_PASTE:
1300 WCUSER_PasteFromClipboard(data);
1301 break;
1302 case IDS_SELECTALL:
1303 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1304 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1305 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1306 WCUSER_SetSelection(data, 0);
1307 PRIVATE(data)->has_selection = TRUE;
1308 break;
1309 case IDS_SCROLL:
1310 case IDS_SEARCH:
1311 WINE_FIXME("Unhandled yet command: %lx\n", wParam);
1312 break;
1313 default:
1314 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1316 break;
1317 case WM_INITMENUPOPUP:
1318 if (!HIWORD(lParam)) return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1319 WCUSER_SetMenuDetails(data, GetSystemMenu(data->hWnd, FALSE));
1320 break;
1321 case WM_SIZE:
1322 WINECON_ResizeWithContainer(data, LOWORD(lParam) / data->curcfg.cell_width,
1323 HIWORD(lParam) / data->curcfg.cell_height);
1324 break;
1325 default:
1326 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1328 return 0;
1331 /******************************************************************
1332 * WCUSER_DeleteBackend
1336 static void WCUSER_DeleteBackend(struct inner_data* data)
1338 if (!PRIVATE(data)) return;
1339 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1340 if (data->hWnd) DestroyWindow(data->hWnd);
1341 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1342 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1343 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1344 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1347 /******************************************************************
1348 * WCUSER_MainLoop
1352 static int WCUSER_MainLoop(struct inner_data* data)
1354 MSG msg;
1356 ShowWindow(data->hWnd, data->nCmdShow);
1357 while (!data->dying || !data->curcfg.exit_on_die)
1359 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1361 case WAIT_OBJECT_0:
1362 WINECON_GrabChanges(data);
1363 break;
1364 case WAIT_OBJECT_0+1:
1365 /* need to use PeekMessageW loop instead of simple GetMessage:
1366 * multiple messages might have arrived in between,
1367 * so GetMessage would lead to delayed processing */
1368 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
1370 if (msg.message == WM_QUIT) return 1;
1371 WINE_TRACE("dispatching msg %04x\n", msg.message);
1372 DispatchMessageW(&msg);
1374 break;
1375 default:
1376 WINE_ERR("got pb\n");
1377 /* err */
1378 break;
1381 PostQuitMessage(0);
1382 return 0;
1385 /******************************************************************
1386 * WCUSER_InitBackend
1388 * Initialisation part II: creation of window.
1391 enum init_return WCUSER_InitBackend(struct inner_data* data)
1393 static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1395 WNDCLASSW wndclass;
1396 CHARSETINFO ci;
1398 if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
1399 return init_failed;
1400 g_uiDefaultCharset = ci.ciCharset;
1401 WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1403 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1404 if (!data->private) return init_failed;
1406 data->fnMainLoop = WCUSER_MainLoop;
1407 data->fnPosCursor = WCUSER_PosCursor;
1408 data->fnShapeCursor = WCUSER_ShapeCursor;
1409 data->fnComputePositions = WCUSER_ComputePositions;
1410 data->fnRefresh = WCUSER_Refresh;
1411 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1412 data->fnSetTitle = WCUSER_SetTitle;
1413 data->fnSetFont = WCUSER_SetFontPmt;
1414 data->fnScroll = WCUSER_Scroll;
1415 data->fnDeleteBackend = WCUSER_DeleteBackend;
1417 wndclass.style = CS_DBLCLKS;
1418 wndclass.lpfnWndProc = WCUSER_Proc;
1419 wndclass.cbClsExtra = 0;
1420 wndclass.cbWndExtra = sizeof(DWORD_PTR);
1421 wndclass.hInstance = GetModuleHandleW(NULL);
1422 wndclass.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
1423 wndclass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1424 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1425 wndclass.lpszMenuName = NULL;
1426 wndclass.lpszClassName = wClassName;
1428 RegisterClassW(&wndclass);
1430 data->hWnd = CreateWindowW(wndclass.lpszClassName, NULL,
1431 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1432 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1433 if (!data->hWnd) return init_not_supported;
1435 return init_success;