wineconsole: Try harder to get a scalable font.
[wine.git] / programs / wineconsole / user.c
bloba5155015eda2eeda4640fae7719b5e740281fabe
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 (WCUSER_SetFont(fc->data, &mlf))
408 struct config_data defcfg;
410 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
411 fc->done = 1;
412 /* since we've modified the current config with new font information,
413 * set this information as the new default.
415 WINECON_RegLoad(NULL, &defcfg);
416 defcfg.cell_width = fc->data->curcfg.cell_width;
417 defcfg.cell_height = fc->data->curcfg.cell_height;
418 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
419 /* Force also its writing back to the registry so that we can get it
420 * the next time.
422 WINECON_RegSave(&defcfg);
423 return 0;
426 return 1;
429 static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
430 DWORD FontType, LPARAM lParam)
432 struct font_chooser* fc = (struct font_chooser*)lParam;
434 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
435 if (WCUSER_ValidateFont(fc->data, lf, fc->pass))
437 EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
438 get_first_font_enum_2, lParam);
439 return !fc->done; /* we just need the first matching one... */
441 return 1;
444 /******************************************************************
445 * WCUSER_CopyFont
447 * get the relevant information from the font described in lf and store them
448 * in config
450 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
452 TEXTMETRICW tm;
453 HDC hDC;
454 HFONT hFont, hOldFont;
455 CPINFO cpinfo;
457 if (!(hDC = GetDC(hWnd))) return NULL;
458 if (!(hFont = CreateFontIndirectW(lf)))
460 ReleaseDC(hWnd, hDC);
461 return NULL;
463 hOldFont = SelectObject(hDC, hFont);
464 GetTextMetricsW(hDC, &tm);
465 SelectObject(hDC, hOldFont);
466 ReleaseDC(hWnd, hDC);
468 config->cell_width = tm.tmAveCharWidth;
469 config->cell_height = tm.tmHeight + tm.tmExternalLeading;
470 config->font_weight = tm.tmWeight;
471 lstrcpyW(config->face_name, lf->lfFaceName);
472 if (el) *el = tm.tmExternalLeading;
474 /* FIXME: use maximum width for DBCS codepages since some chars take two cells */
475 if (GetCPInfo( GetConsoleOutputCP(), &cpinfo ) && cpinfo.MaxCharSize > 1)
476 config->cell_width = tm.tmMaxCharWidth;
478 return hFont;
481 /******************************************************************
482 * WCUSER_FillLogFont
486 void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
488 lf->lfHeight = height;
489 lf->lfWidth = 0;
490 lf->lfEscapement = 0;
491 lf->lfOrientation = 0;
492 lf->lfWeight = weight;
493 lf->lfItalic = FALSE;
494 lf->lfUnderline = FALSE;
495 lf->lfStrikeOut = FALSE;
496 lf->lfCharSet = DEFAULT_CHARSET;
497 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
498 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
499 lf->lfQuality = DEFAULT_QUALITY;
500 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
501 lstrcpyW(lf->lfFaceName, name);
504 /******************************************************************
505 * WCUSER_SetFont
507 * sets logfont as the new font for the console
509 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
511 HFONT hFont;
512 LONG el;
514 if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
515 return TRUE;
517 hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
518 if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
520 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
521 PRIVATE(data)->hFont = hFont;
522 PRIVATE(data)->ext_leading = el;
524 WCUSER_ComputePositions(data);
525 WCUSER_NewBitmap(data);
526 InvalidateRect(data->hWnd, NULL, FALSE);
527 UpdateWindow(data->hWnd);
529 return TRUE;
532 /******************************************************************
533 * WCUSER_SetFontPmt
535 * Sets a new font for the console.
536 * In fact a wrapper for WCUSER_SetFont
538 static void WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
539 unsigned height, unsigned weight)
541 LOGFONTW lf;
542 struct font_chooser fc;
544 WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
545 wine_dbgstr_wn(font, -1), height, weight);
547 if (font[0] != '\0' && height != 0 && weight != 0)
549 WCUSER_FillLogFont(&lf, font, height, weight);
550 if (WCUSER_SetFont(data, &lf))
552 WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
553 return;
557 /* try to find an acceptable font */
558 WINE_WARN("Couldn't match the font from registry... trying to find one\n");
559 fc.data = data;
560 fc.done = FALSE;
561 for (fc.pass = 0; fc.pass <= 5; fc.pass++)
563 EnumFontFamiliesW(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
564 if (fc.done) return;
566 WINECON_Fatal("Couldn't find a decent font, aborting");
569 /******************************************************************
570 * WCUSER_GetCell
572 * Get a cell from a relative coordinate in window (takes into
573 * account the scrolling)
575 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
577 COORD c;
579 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
580 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
582 return c;
585 /******************************************************************
586 * WCUSER_GetSelectionRect
588 * Get the selection rectangle
590 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
592 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) - data->curcfg.win_pos.X) * data->curcfg.cell_width;
593 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
594 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
595 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
598 /******************************************************************
599 * WCUSER_SetSelection
603 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
605 HDC hDC;
606 RECT r;
608 WCUSER_GetSelectionRect(data, &r);
609 hDC = hRefDC ? hRefDC : GetDC(data->hWnd);
610 if (hDC)
612 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
613 HideCaret(data->hWnd);
614 InvertRect(hDC, &r);
615 if (hDC != hRefDC)
616 ReleaseDC(data->hWnd, hDC);
617 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
618 ShowCaret(data->hWnd);
622 /******************************************************************
623 * WCUSER_MoveSelection
627 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
629 RECT r;
630 HDC hDC;
632 if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
633 c2.X < 0 || c2.X >= data->curcfg.sb_width ||
634 c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
635 c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
636 return;
638 WCUSER_GetSelectionRect(data, &r);
639 hDC = GetDC(data->hWnd);
640 if (hDC)
642 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
643 HideCaret(data->hWnd);
644 InvertRect(hDC, &r);
646 PRIVATE(data)->selectPt1 = c1;
647 PRIVATE(data)->selectPt2 = c2;
648 if (hDC)
650 WCUSER_GetSelectionRect(data, &r);
651 InvertRect(hDC, &r);
652 ReleaseDC(data->hWnd, hDC);
653 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
654 ShowCaret(data->hWnd);
658 /******************************************************************
659 * WCUSER_CopySelectionToClipboard
661 * Copies the current selection into the clipboard
663 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
665 HANDLE hMem;
666 LPWSTR p;
667 unsigned w, h;
669 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
670 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
672 if (!OpenClipboard(data->hWnd)) return;
673 EmptyClipboard();
675 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
676 if (hMem && (p = GlobalLock(hMem)))
678 COORD c;
679 int y;
681 c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
682 c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
684 for (y = 0; y < h; y++, c.Y++)
686 LPWSTR end;
687 DWORD count;
689 ReadConsoleOutputCharacterW(data->hConOut, p, w - 1, c, &count);
691 /* strip spaces from the end of the line */
692 end = p + w - 1;
693 while (end > p && *(end - 1) == ' ')
694 end--;
695 *end = (y < h - 1) ? '\n' : '\0';
696 p = end + 1;
698 GlobalUnlock(hMem);
699 SetClipboardData(CF_UNICODETEXT, hMem);
701 CloseClipboard();
704 /******************************************************************
705 * WCUSER_PasteFromClipboard
709 static void WCUSER_PasteFromClipboard(struct inner_data* data)
711 HANDLE h;
712 WCHAR* ptr;
714 if (!OpenClipboard(data->hWnd)) return;
715 h = GetClipboardData(CF_UNICODETEXT);
716 if (h && (ptr = GlobalLock(h)))
718 int i, len = GlobalSize(h) / sizeof(WCHAR);
719 INPUT_RECORD ir[2];
720 DWORD n;
721 SHORT sh;
723 ir[0].EventType = KEY_EVENT;
724 ir[0].Event.KeyEvent.wRepeatCount = 0;
725 ir[0].Event.KeyEvent.dwControlKeyState = 0;
726 ir[0].Event.KeyEvent.bKeyDown = TRUE;
728 /* generate the corresponding input records */
729 for (i = 0; i < len; i++)
731 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
732 sh = VkKeyScanW(ptr[i]);
733 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
734 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(LOBYTE(sh), 0);
735 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
737 ir[1] = ir[0];
738 ir[1].Event.KeyEvent.bKeyDown = FALSE;
740 WriteConsoleInputW(data->hConIn, ir, 2, &n);
742 GlobalUnlock(h);
744 CloseClipboard();
747 /******************************************************************
748 * WCUSER_Refresh
752 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
754 WCUSER_FillMemDC(data, tp, bm);
755 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
757 RECT r;
759 r.left = 0;
760 r.right = data->curcfg.win_width * data->curcfg.cell_width;
761 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
762 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
763 InvalidateRect(data->hWnd, &r, FALSE);
764 UpdateWindow(data->hWnd);
768 /******************************************************************
769 * WCUSER_Paint
773 static void WCUSER_Paint(const struct inner_data* data)
775 PAINTSTRUCT ps;
777 if (data->in_set_config) return; /* in order to avoid some flicker */
778 BeginPaint(data->hWnd, &ps);
779 BitBlt(ps.hdc, 0, 0,
780 data->curcfg.win_width * data->curcfg.cell_width,
781 data->curcfg.win_height * data->curcfg.cell_height,
782 PRIVATE(data)->hMemDC,
783 data->curcfg.win_pos.X * data->curcfg.cell_width,
784 data->curcfg.win_pos.Y * data->curcfg.cell_height,
785 SRCCOPY);
786 if (PRIVATE(data)->has_selection)
787 WCUSER_SetSelection(data, ps.hdc);
788 EndPaint(data->hWnd, &ps);
791 /******************************************************************
792 * WCUSER_Scroll
796 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
798 if (horz)
800 ScrollWindow(data->hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, NULL, NULL);
801 SetScrollPos(data->hWnd, SB_HORZ, pos, TRUE);
802 data->curcfg.win_pos.X = pos;
804 else
806 ScrollWindow(data->hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, NULL, NULL);
807 SetScrollPos(data->hWnd, SB_VERT, pos, TRUE);
808 data->curcfg.win_pos.Y = pos;
810 InvalidateRect(data->hWnd, NULL, FALSE);
813 /******************************************************************
814 * WCUSER_FillMenu
818 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
820 HMENU hSubMenu;
821 HINSTANCE hInstance = GetModuleHandleW(NULL);
822 WCHAR buff[256];
824 if (!hMenu) return FALSE;
826 /* FIXME: error handling & memory cleanup */
827 hSubMenu = CreateMenu();
828 if (!hSubMenu) return FALSE;
830 LoadStringW(hInstance, IDS_MARK, buff, ARRAY_SIZE(buff));
831 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
832 LoadStringW(hInstance, IDS_COPY, buff, ARRAY_SIZE(buff));
833 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
834 LoadStringW(hInstance, IDS_PASTE, buff, ARRAY_SIZE(buff));
835 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
836 LoadStringW(hInstance, IDS_SELECTALL, buff, ARRAY_SIZE(buff));
837 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
838 LoadStringW(hInstance, IDS_SCROLL, buff, ARRAY_SIZE(buff));
839 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
840 LoadStringW(hInstance, IDS_SEARCH, buff, ARRAY_SIZE(buff));
841 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
843 if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
844 LoadStringW(hInstance, IDS_EDIT, buff, ARRAY_SIZE(buff));
845 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
846 LoadStringW(hInstance, IDS_DEFAULT, buff, ARRAY_SIZE(buff));
847 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
848 LoadStringW(hInstance, IDS_PROPERTIES, buff, ARRAY_SIZE(buff));
849 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
851 return TRUE;
854 /******************************************************************
855 * WCUSER_SetMenuDetails
857 * Grays / ungrays the menu items according to their state
859 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
861 if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
863 EnableMenuItem(hMenu, IDS_COPY,
864 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
865 EnableMenuItem(hMenu, IDS_PASTE,
866 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
867 ? MF_ENABLED : MF_GRAYED));
868 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
869 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
872 /******************************************************************
873 * WCUSER_Create
875 * Creates the window for the rendering
877 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
879 struct inner_data* data;
880 HMENU hSysMenu;
882 data = lpcs->lpCreateParams;
883 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
884 data->hWnd = hWnd;
886 hSysMenu = GetSystemMenu(hWnd, FALSE);
887 if (!hSysMenu) return 0;
888 PRIVATE(data)->hPopMenu = CreatePopupMenu();
889 if (!PRIVATE(data)->hPopMenu) return 0;
891 WCUSER_FillMenu(hSysMenu, TRUE);
892 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
894 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
895 if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
897 data->curcfg.quick_edit = FALSE;
898 return 0;
901 /******************************************************************
902 * WCUSER_GetCtrlKeyState
904 * Get the console bit mask equivalent to the VK_ status in keyState
906 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
908 DWORD ret = 0;
910 GetKeyboardState(keyState);
911 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
912 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
913 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
914 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
915 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
916 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
917 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
918 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
920 return ret;
923 /******************************************************************
924 * WCUSER_HandleSelectionKey
926 * Handles keys while selecting an area
928 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
929 WPARAM wParam, LPARAM lParam)
931 BYTE keyState[256];
932 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
933 COORD c1, c2;
935 if (!down) return;
937 switch (state)
939 case 0:
940 switch (wParam)
942 case VK_RETURN:
943 PRIVATE(data)->has_selection = FALSE;
944 WCUSER_SetSelection(data, 0);
945 WCUSER_CopySelectionToClipboard(data);
946 return;
947 case VK_RIGHT:
948 c1 = PRIVATE(data)->selectPt1;
949 c2 = PRIVATE(data)->selectPt2;
950 c1.X++; c2.X++;
951 WCUSER_MoveSelection(data, c1, c2);
952 return;
953 case VK_LEFT:
954 c1 = PRIVATE(data)->selectPt1;
955 c2 = PRIVATE(data)->selectPt2;
956 c1.X--; c2.X--;
957 WCUSER_MoveSelection(data, c1, c2);
958 return;
959 case VK_UP:
960 c1 = PRIVATE(data)->selectPt1;
961 c2 = PRIVATE(data)->selectPt2;
962 c1.Y--; c2.Y--;
963 WCUSER_MoveSelection(data, c1, c2);
964 return;
965 case VK_DOWN:
966 c1 = PRIVATE(data)->selectPt1;
967 c2 = PRIVATE(data)->selectPt2;
968 c1.Y++; c2.Y++;
969 WCUSER_MoveSelection(data, c1, c2);
970 return;
972 break;
973 case SHIFT_PRESSED:
974 switch (wParam)
976 case VK_RIGHT:
977 c1 = PRIVATE(data)->selectPt1;
978 c2 = PRIVATE(data)->selectPt2;
979 c2.X++;
980 WCUSER_MoveSelection(data, c1, c2);
981 return;
982 case VK_LEFT:
983 c1 = PRIVATE(data)->selectPt1;
984 c2 = PRIVATE(data)->selectPt2;
985 c2.X--;
986 WCUSER_MoveSelection(data, c1, c2);
987 return;
988 case VK_UP:
989 c1 = PRIVATE(data)->selectPt1;
990 c2 = PRIVATE(data)->selectPt2;
991 c2.Y--;
992 WCUSER_MoveSelection(data, c1, c2);
993 return;
994 case VK_DOWN:
995 c1 = PRIVATE(data)->selectPt1;
996 c2 = PRIVATE(data)->selectPt2;
997 c2.Y++;
998 WCUSER_MoveSelection(data, c1, c2);
999 return;
1001 break;
1004 if (wParam < VK_SPACE) /* Shift, Alt, Ctrl, Num Lock etc. */
1005 return;
1007 WCUSER_SetSelection(data, 0);
1008 PRIVATE(data)->has_selection = FALSE;
1011 /******************************************************************
1012 * WCUSER_GenerateKeyInputRecord
1014 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1016 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1017 WPARAM wParam, LPARAM lParam)
1019 INPUT_RECORD ir;
1020 DWORD n;
1021 WCHAR buf[2];
1022 static WCHAR last; /* keep last char seen as feed for key up message */
1023 BYTE keyState[256];
1025 ir.EventType = KEY_EVENT;
1026 ir.Event.KeyEvent.bKeyDown = down;
1027 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1028 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1030 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1032 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1033 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1034 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1036 if (down)
1038 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1040 case 2:
1041 /* FIXME... should generate two events... */
1042 /* fall through */
1043 case 1:
1044 last = buf[0];
1045 break;
1046 default:
1047 last = 0;
1048 break;
1051 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME: HACKY... and buggy because it should be a stack, not a single value */
1052 if (!down) last = 0;
1054 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1057 /******************************************************************
1058 * WCUSER_GenerateMouseInputRecord
1062 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1063 WPARAM wParam, DWORD event)
1065 INPUT_RECORD ir;
1066 BYTE keyState[256];
1067 DWORD mode, n;
1069 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1070 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1071 return;
1073 ir.EventType = MOUSE_EVENT;
1074 ir.Event.MouseEvent.dwMousePosition = c;
1075 ir.Event.MouseEvent.dwButtonState = 0;
1076 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1077 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1078 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1079 if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
1080 if (wParam & MK_SHIFT) ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
1081 if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1082 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1083 ir.Event.MouseEvent.dwEventFlags = event;
1085 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1088 /******************************************************************
1089 * WCUSER_Proc
1093 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1095 struct inner_data* data = (struct inner_data*)GetWindowLongPtrW(hWnd, 0);
1097 switch (uMsg)
1099 case WM_CREATE:
1100 return WCUSER_Create(hWnd, (LPCREATESTRUCTW)lParam);
1101 case WM_DESTROY:
1102 data->hWnd = 0;
1103 PostQuitMessage(0);
1104 break;
1105 case WM_PAINT:
1106 WCUSER_Paint(data);
1107 break;
1108 case WM_KEYDOWN:
1109 case WM_KEYUP:
1110 if (PRIVATE(data)->has_selection)
1111 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1112 else
1113 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam);
1114 break;
1115 case WM_SYSKEYDOWN:
1116 case WM_SYSKEYUP:
1117 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam);
1118 break;
1119 case WM_LBUTTONDOWN:
1120 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1122 if (PRIVATE(data)->has_selection)
1123 WCUSER_SetSelection(data, 0);
1125 if (data->curcfg.quick_edit && PRIVATE(data)->has_selection)
1127 PRIVATE(data)->has_selection = FALSE;
1129 else
1131 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1132 SetCapture(data->hWnd);
1133 WCUSER_SetSelection(data, 0);
1134 PRIVATE(data)->has_selection = TRUE;
1137 else
1139 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1141 break;
1142 case WM_MOUSEMOVE:
1143 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1145 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection &&
1146 (wParam & MK_LBUTTON))
1148 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1151 else
1153 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1155 break;
1156 case WM_LBUTTONUP:
1157 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1159 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection)
1161 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1162 ReleaseCapture();
1165 else
1167 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1169 break;
1170 case WM_RBUTTONDOWN:
1171 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1173 POINT pt;
1175 pt.x = (short)LOWORD(lParam);
1176 pt.y = (short)HIWORD(lParam);
1177 ClientToScreen(hWnd, &pt);
1178 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1179 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON,
1180 pt.x, pt.y, 0, hWnd, NULL);
1182 else
1184 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1186 break;
1187 case WM_RBUTTONUP:
1188 /* no need to track for rbutton up when opening the popup... the event will be
1189 * swallowed by TrackPopupMenu */
1190 case WM_MBUTTONDOWN:
1191 case WM_MBUTTONUP:
1192 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1193 break;
1194 case WM_LBUTTONDBLCLK:
1195 case WM_MBUTTONDBLCLK:
1196 case WM_RBUTTONDBLCLK:
1197 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
1198 break;
1199 case WM_SETFOCUS:
1200 if (data->curcfg.cursor_visible)
1202 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
1203 data->curcfg.cell_width, data->curcfg.cell_height);
1204 WCUSER_PosCursor(data);
1206 break;
1207 case WM_KILLFOCUS:
1208 if (data->curcfg.cursor_visible)
1209 DestroyCaret();
1210 break;
1211 case WM_HSCROLL:
1213 struct config_data cfg = data->curcfg;
1215 switch (LOWORD(wParam))
1217 case SB_PAGEUP: cfg.win_pos.X -= 8; break;
1218 case SB_PAGEDOWN: cfg.win_pos.X += 8; break;
1219 case SB_LINEUP: cfg.win_pos.X--; break;
1220 case SB_LINEDOWN: cfg.win_pos.X++; break;
1221 case SB_THUMBTRACK: cfg.win_pos.X = HIWORD(wParam); break;
1222 default: break;
1224 if (cfg.win_pos.X < 0) cfg.win_pos.X = 0;
1225 if (cfg.win_pos.X > data->curcfg.sb_width - data->curcfg.win_width)
1226 cfg.win_pos.X = data->curcfg.sb_width - data->curcfg.win_width;
1227 if (cfg.win_pos.X != data->curcfg.win_pos.X)
1229 WINECON_SetConfig(data, &cfg);
1232 break;
1233 case WM_MOUSEWHEEL:
1234 if (data->curcfg.sb_height <= data->curcfg.win_height)
1236 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1237 break;
1239 /* else fallthrough */
1240 case WM_VSCROLL:
1242 struct config_data cfg = data->curcfg;
1244 if (uMsg == WM_MOUSEWHEEL)
1246 UINT scrollLines = 3;
1247 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
1248 scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1249 cfg.win_pos.Y += scrollLines;
1250 } else {
1251 switch (LOWORD(wParam))
1253 case SB_PAGEUP: cfg.win_pos.Y -= 8; break;
1254 case SB_PAGEDOWN: cfg.win_pos.Y += 8; break;
1255 case SB_LINEUP: cfg.win_pos.Y--; break;
1256 case SB_LINEDOWN: cfg.win_pos.Y++; break;
1257 case SB_THUMBTRACK: cfg.win_pos.Y = HIWORD(wParam); break;
1258 default: break;
1262 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
1263 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
1264 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
1265 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
1267 WINECON_SetConfig(data, &cfg);
1270 break;
1271 case WM_SYSCOMMAND:
1272 switch (wParam)
1274 case IDS_DEFAULT:
1275 WCUSER_GetProperties(data, FALSE);
1276 break;
1277 case IDS_PROPERTIES:
1278 WCUSER_GetProperties(data, TRUE);
1279 break;
1280 default:
1281 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1283 break;
1284 case WM_COMMAND:
1285 switch (wParam)
1287 case IDS_DEFAULT:
1288 WCUSER_GetProperties(data, FALSE);
1289 break;
1290 case IDS_PROPERTIES:
1291 WCUSER_GetProperties(data, TRUE);
1292 break;
1293 case IDS_MARK:
1294 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1295 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1296 WCUSER_SetSelection(data, 0);
1297 PRIVATE(data)->has_selection = TRUE;
1298 break;
1299 case IDS_COPY:
1300 if (PRIVATE(data)->has_selection)
1302 PRIVATE(data)->has_selection = FALSE;
1303 WCUSER_SetSelection(data, 0);
1304 WCUSER_CopySelectionToClipboard(data);
1306 break;
1307 case IDS_PASTE:
1308 WCUSER_PasteFromClipboard(data);
1309 break;
1310 case IDS_SELECTALL:
1311 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1312 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1313 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1314 WCUSER_SetSelection(data, 0);
1315 PRIVATE(data)->has_selection = TRUE;
1316 break;
1317 case IDS_SCROLL:
1318 case IDS_SEARCH:
1319 WINE_FIXME("Unhandled yet command: %lx\n", wParam);
1320 break;
1321 default:
1322 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1324 break;
1325 case WM_INITMENUPOPUP:
1326 if (!HIWORD(lParam)) return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1327 WCUSER_SetMenuDetails(data, GetSystemMenu(data->hWnd, FALSE));
1328 break;
1329 case WM_SIZE:
1330 WINECON_ResizeWithContainer(data, LOWORD(lParam) / data->curcfg.cell_width,
1331 HIWORD(lParam) / data->curcfg.cell_height);
1332 break;
1333 default:
1334 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1336 return 0;
1339 /******************************************************************
1340 * WCUSER_DeleteBackend
1344 static void WCUSER_DeleteBackend(struct inner_data* data)
1346 if (!PRIVATE(data)) return;
1347 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1348 if (data->hWnd) DestroyWindow(data->hWnd);
1349 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1350 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1351 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1352 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1355 /******************************************************************
1356 * WCUSER_MainLoop
1360 static int WCUSER_MainLoop(struct inner_data* data)
1362 MSG msg;
1364 ShowWindow(data->hWnd, data->nCmdShow);
1365 while (!data->dying || !data->curcfg.exit_on_die)
1367 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1369 case WAIT_OBJECT_0:
1370 WINECON_GrabChanges(data);
1371 break;
1372 case WAIT_OBJECT_0+1:
1373 /* need to use PeekMessageW loop instead of simple GetMessage:
1374 * multiple messages might have arrived in between,
1375 * so GetMessage would lead to delayed processing */
1376 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
1378 if (msg.message == WM_QUIT) return 1;
1379 WINE_TRACE("dispatching msg %04x\n", msg.message);
1380 DispatchMessageW(&msg);
1382 break;
1383 default:
1384 WINE_ERR("got pb\n");
1385 /* err */
1386 break;
1389 PostQuitMessage(0);
1390 return 0;
1393 /******************************************************************
1394 * WCUSER_InitBackend
1396 * Initialisation part II: creation of window.
1399 enum init_return WCUSER_InitBackend(struct inner_data* data)
1401 static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1403 WNDCLASSW wndclass;
1404 CHARSETINFO ci;
1406 if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
1407 return init_failed;
1408 g_uiDefaultCharset = ci.ciCharset;
1409 WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1411 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1412 if (!data->private) return init_failed;
1414 data->fnMainLoop = WCUSER_MainLoop;
1415 data->fnPosCursor = WCUSER_PosCursor;
1416 data->fnShapeCursor = WCUSER_ShapeCursor;
1417 data->fnComputePositions = WCUSER_ComputePositions;
1418 data->fnRefresh = WCUSER_Refresh;
1419 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1420 data->fnSetTitle = WCUSER_SetTitle;
1421 data->fnSetFont = WCUSER_SetFontPmt;
1422 data->fnScroll = WCUSER_Scroll;
1423 data->fnDeleteBackend = WCUSER_DeleteBackend;
1425 wndclass.style = CS_DBLCLKS;
1426 wndclass.lpfnWndProc = WCUSER_Proc;
1427 wndclass.cbClsExtra = 0;
1428 wndclass.cbWndExtra = sizeof(DWORD_PTR);
1429 wndclass.hInstance = GetModuleHandleW(NULL);
1430 wndclass.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
1431 wndclass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1432 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1433 wndclass.lpszMenuName = NULL;
1434 wndclass.lpszClassName = wClassName;
1436 RegisterClassW(&wndclass);
1438 data->hWnd = CreateWindowW(wndclass.lpszClassName, NULL,
1439 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1440 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1441 if (!data->hWnd) return init_not_supported;
1443 return init_success;