dsound: Allow inactive devices to change the primary format.
[wine.git] / programs / wineconsole / user.c
blob40a0757f177aac9b5c326ba689835f5d0c9689c3
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 /* mapping console colors to RGB values */
34 const COLORREF WCUSER_ColorMap[16] =
36 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x80), RGB(0x00, 0x80, 0x00), RGB(0x00, 0x80, 0x80),
37 RGB(0x80, 0x00, 0x00), RGB(0x80, 0x00, 0x80), RGB(0x80, 0x80, 0x00), RGB(0xC0, 0xC0, 0xC0),
38 RGB(0x80, 0x80, 0x80), RGB(0x00, 0x00, 0xFF), RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0xFF),
39 RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0xFF, 0x00), RGB(0xFF, 0xFF, 0xFF),
42 static BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* font);
44 /******************************************************************
45 * WCUSER_FillMemDC
47 * Fills the Mem DC with current cells values
49 static void WCUSER_FillMemDC(const struct inner_data* data, int upd_tp, int upd_bm)
51 unsigned i, j, k;
52 CHAR_INFO* cell;
53 HFONT hOldFont;
54 WORD attr;
55 WCHAR* line;
56 RECT r;
57 HBRUSH hbr;
58 INT *dx;
60 /* no font has been set up yet, don't worry about filling the bitmap,
61 * we'll do it once a font is chosen
63 if (!PRIVATE(data)->hFont) return;
65 /* FIXME: could set up a mechanism to reuse the line between different
66 * calls to this function
68 if (!(line = HeapAlloc(GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(WCHAR))))
69 WINECON_Fatal("OOM\n");
70 dx = HeapAlloc( GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(*dx) );
72 hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
73 for (j = upd_tp; j <= upd_bm; j++)
75 cell = &data->cells[j * data->curcfg.sb_width];
76 for (i = 0; i < data->curcfg.sb_width; i++)
78 attr = cell[i].Attributes;
79 SetBkColor(PRIVATE(data)->hMemDC, WCUSER_ColorMap[(attr>>4)&0x0F]);
80 SetTextColor(PRIVATE(data)->hMemDC, WCUSER_ColorMap[attr&0x0F]);
81 for (k = i; k < data->curcfg.sb_width && cell[k].Attributes == attr; k++)
83 line[k - i] = cell[k].Char.UnicodeChar;
84 dx[k - i] = data->curcfg.cell_width;
86 ExtTextOutW( PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
87 0, NULL, line, k - i, dx );
88 if (PRIVATE(data)->ext_leading &&
89 (hbr = CreateSolidBrush(WCUSER_ColorMap[(attr>>4)&0x0F])))
91 r.left = i * data->curcfg.cell_width;
92 r.top = (j + 1) * data->curcfg.cell_height - PRIVATE(data)->ext_leading;
93 r.right = k * data->curcfg.cell_width;
94 r.bottom = (j + 1) * data->curcfg.cell_height;
95 FillRect(PRIVATE(data)->hMemDC, &r, hbr);
96 DeleteObject(hbr);
98 i = k - 1;
101 SelectObject(PRIVATE(data)->hMemDC, hOldFont);
102 HeapFree(GetProcessHeap(), 0, dx);
103 HeapFree(GetProcessHeap(), 0, line);
106 /******************************************************************
107 * WCUSER_NewBitmap
109 * Either the font geometry or the sb geometry has changed. we need
110 * to recreate the bitmap geometry.
112 static void WCUSER_NewBitmap(struct inner_data* data)
114 HDC hDC;
115 HBITMAP hnew, hold;
117 if (!data->curcfg.sb_width || !data->curcfg.sb_height ||
118 !PRIVATE(data)->hFont || !(hDC = GetDC(data->hWnd)))
119 return;
120 hnew = CreateCompatibleBitmap(hDC,
121 data->curcfg.sb_width * data->curcfg.cell_width,
122 data->curcfg.sb_height * data->curcfg.cell_height);
123 ReleaseDC(data->hWnd, hDC);
124 hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
126 if (PRIVATE(data)->hBitmap)
128 if (hold == PRIVATE(data)->hBitmap)
129 DeleteObject(PRIVATE(data)->hBitmap);
130 else
131 WINE_FIXME("leak\n");
133 PRIVATE(data)->hBitmap = hnew;
134 WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
137 /******************************************************************
138 * WCUSER_ResizeScreenBuffer
142 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
144 WCUSER_NewBitmap(data);
147 /******************************************************************
148 * WCUSER_PosCursor
150 * Set a new position for the cursor
152 static void WCUSER_PosCursor(const struct inner_data* data)
154 if (data->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
156 SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
157 (data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
158 ShowCaret(data->hWnd);
161 /******************************************************************
162 * WCUSER_ShapeCursor
164 * Sets a new shape for the cursor
166 static void WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
168 if (force || size != data->curcfg.cursor_size)
170 if (data->curcfg.cursor_visible && data->hWnd == GetFocus()) DestroyCaret();
171 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
172 PRIVATE(data)->cursor_bitmap = NULL;
173 if (size != 100)
175 int w16b; /* number of bytes per row, aligned on word size */
176 BYTE* ptr;
177 int i, j, nbl;
179 w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
180 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
181 if (!ptr) WINECON_Fatal("OOM");
182 nbl = max((data->curcfg.cell_height * size) / 100, 1);
183 for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
185 for (i = 0; i < data->curcfg.cell_width; i++)
187 ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
190 PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
191 data->curcfg.cell_height, 1, 1, ptr);
192 HeapFree(GetProcessHeap(), 0, ptr);
194 data->curcfg.cursor_size = size;
195 data->curcfg.cursor_visible = -1;
198 vis = vis != 0;
199 if (force || vis != data->curcfg.cursor_visible)
201 data->curcfg.cursor_visible = vis;
202 if (data->hWnd == GetFocus())
204 if (vis)
206 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
207 data->curcfg.cell_width, data->curcfg.cell_height);
208 WCUSER_PosCursor(data);
210 else
212 DestroyCaret();
216 WINECON_DumpConfig("crsr", &data->curcfg);
219 /******************************************************************
220 * WCUSER_ComputePositions
222 * Recomputes all the components (mainly scroll bars) positions
224 static void WCUSER_ComputePositions(struct inner_data* data)
226 RECT r;
227 int dx, dy;
229 /* compute window size from desired client size */
230 r.left = r.top = 0;
231 r.right = data->curcfg.win_width * data->curcfg.cell_width;
232 r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
234 if (IsRectEmpty(&r)) return;
236 AdjustWindowRect(&r, GetWindowLongW(data->hWnd, GWL_STYLE), FALSE);
238 dx = dy = 0;
239 if (data->curcfg.sb_width > data->curcfg.win_width)
241 dy = GetSystemMetrics(SM_CYHSCROLL);
242 SetScrollRange(data->hWnd, SB_HORZ, 0,
243 data->curcfg.sb_width - data->curcfg.win_width, FALSE);
244 SetScrollPos(data->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
245 ShowScrollBar(data->hWnd, SB_HORZ, TRUE);
247 else
249 ShowScrollBar(data->hWnd, SB_HORZ, FALSE);
252 if (data->curcfg.sb_height > data->curcfg.win_height)
254 dx = GetSystemMetrics(SM_CXVSCROLL);
255 SetScrollRange(data->hWnd, SB_VERT, 0,
256 data->curcfg.sb_height - data->curcfg.win_height, FALSE);
257 SetScrollPos(data->hWnd, SB_VERT, 0, FALSE); /* FIXME */
258 ShowScrollBar(data->hWnd, SB_VERT, TRUE);
260 else
262 ShowScrollBar(data->hWnd, SB_VERT, FALSE);
265 SetWindowPos(data->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
266 SWP_NOMOVE|SWP_NOZORDER);
267 WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
268 WCUSER_PosCursor(data);
271 /******************************************************************
272 * WCUSER_SetTitle
274 * Sets the title to the wine console
276 static void WCUSER_SetTitle(const struct inner_data* data)
278 WCHAR buffer[256];
280 if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
281 SetWindowTextW(data->hWnd, buffer);
284 void WCUSER_DumpLogFont(const char* pfx, const LOGFONTW* lf, DWORD ft)
286 WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
287 "\tlf.lfHeight=%d lf.lfWidth=%d lf.lfEscapement=%d lf.lfOrientation=%d\n"
288 "\tlf.lfWeight=%d lf.lfItalic=%u lf.lfUnderline=%u lf.lfStrikeOut=%u\n"
289 "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
290 "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
291 pfx,
292 (ft & RASTER_FONTTYPE) ? "raster" : "",
293 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
294 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
295 (ft & DEVICE_FONTTYPE) ? "|device" : "",
296 lf->lfHeight, lf->lfWidth, lf->lfEscapement, lf->lfOrientation,
297 lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
298 lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
299 wine_dbgstr_w(lf->lfFaceName));
302 void WCUSER_DumpTextMetric(const TEXTMETRICW* tm, DWORD ft)
304 WINE_TRACE_(wc_font)("%s%s%s%s\n"
305 "\ttmHeight=%d tmAscent=%d tmDescent=%d tmInternalLeading=%d tmExternalLeading=%d\n"
306 "\ttmAveCharWidth=%d tmMaxCharWidth=%d tmWeight=%d tmOverhang=%d\n"
307 "\ttmDigitizedAspectX=%d tmDigitizedAspectY=%d\n"
308 "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
309 "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
310 (ft & RASTER_FONTTYPE) ? "raster" : "",
311 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
312 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
313 (ft & DEVICE_FONTTYPE) ? "|device" : "",
314 tm->tmHeight, tm->tmAscent, tm->tmDescent, tm->tmInternalLeading, tm->tmExternalLeading, tm->tmAveCharWidth,
315 tm->tmMaxCharWidth, tm->tmWeight, tm->tmOverhang, tm->tmDigitizedAspectX, tm->tmDigitizedAspectY,
316 tm->tmFirstChar, tm->tmLastChar, tm->tmDefaultChar, tm->tmBreakChar, tm->tmItalic, tm->tmUnderlined, tm->tmStruckOut,
317 tm->tmPitchAndFamily, tm->tmCharSet);
320 /******************************************************************
321 * WCUSER_AreFontsEqual
325 static BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONTW* lf)
327 return lf->lfHeight == config->cell_height &&
328 lf->lfWeight == config->font_weight &&
329 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
330 !lstrcmpW(lf->lfFaceName, config->face_name);
333 struct font_chooser
335 struct inner_data* data;
336 int pass;
337 BOOL done;
340 /******************************************************************
341 * WCUSER_ValidateFontMetric
343 * Returns true if the font described in tm is usable as a font for the renderer
345 BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRICW* tm,
346 DWORD type, int pass)
348 switch (pass) /* we get increasingly lenient in later passes */
350 case 0:
351 if (type & RASTER_FONTTYPE)
353 if (tm->tmMaxCharWidth * data->curcfg.win_width >= GetSystemMetrics(SM_CXSCREEN) ||
354 tm->tmHeight * data->curcfg.win_height >= GetSystemMetrics(SM_CYSCREEN))
355 return FALSE;
357 /* fall through */
358 case 1:
359 if (tm->tmCharSet != DEFAULT_CHARSET && tm->tmCharSet != g_uiDefaultCharset) return FALSE;
360 /* fall through */
361 case 2:
362 if (tm->tmItalic || tm->tmUnderlined || tm->tmStruckOut) return FALSE;
363 break;
365 return TRUE;
368 /******************************************************************
369 * WCUSER_ValidateFont
371 * Returns true if the font family described in lf is usable as a font for the renderer
373 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONTW* lf, int pass)
375 switch (pass) /* we get increasingly lenient in later passes */
377 case 0:
378 case 1:
379 if (lf->lfCharSet != DEFAULT_CHARSET && lf->lfCharSet != g_uiDefaultCharset) return FALSE;
380 /* fall through */
381 case 2:
382 if ((lf->lfPitchAndFamily & 3) != FIXED_PITCH) return FALSE;
383 /* fall through */
384 case 3:
385 if (lf->lfFaceName[0] == '@') return FALSE;
386 break;
388 return TRUE;
391 /******************************************************************
392 * get_first_font_enum_2
393 * get_first_font_enum
395 * Helper functions to get a decent font for the renderer
397 static int CALLBACK get_first_font_enum_2(const LOGFONTW* lf, const TEXTMETRICW* tm,
398 DWORD FontType, LPARAM lParam)
400 struct font_chooser* fc = (struct font_chooser*)lParam;
402 WCUSER_DumpTextMetric(tm, FontType);
403 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType, fc->pass))
405 LOGFONTW mlf = *lf;
407 /* Use the default sizes for the font (this is needed, especially for
408 * TrueType fonts, so that we get a decent size, not the max size)
410 mlf.lfWidth = fc->data->curcfg.cell_width;
411 mlf.lfHeight = fc->data->curcfg.cell_height;
412 if (WCUSER_SetFont(fc->data, &mlf))
414 struct config_data defcfg;
416 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
417 fc->done = 1;
418 /* since we've modified the current config with new font information,
419 * set this information as the new default.
421 WINECON_RegLoad(NULL, &defcfg);
422 defcfg.cell_width = fc->data->curcfg.cell_width;
423 defcfg.cell_height = fc->data->curcfg.cell_height;
424 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
425 /* Force also its writing back to the registry so that we can get it
426 * the next time.
428 WINECON_RegSave(&defcfg);
429 return 0;
432 return 1;
435 static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
436 DWORD FontType, LPARAM lParam)
438 struct font_chooser* fc = (struct font_chooser*)lParam;
440 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
441 if (WCUSER_ValidateFont(fc->data, lf, fc->pass))
443 EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
444 get_first_font_enum_2, lParam);
445 return !fc->done; /* we just need the first matching one... */
447 return 1;
450 /******************************************************************
451 * WCUSER_CopyFont
453 * get the relevant information from the font described in lf and store them
454 * in config
456 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
458 TEXTMETRICW tm;
459 HDC hDC;
460 HFONT hFont, hOldFont;
462 if (!(hDC = GetDC(hWnd))) return NULL;
463 if (!(hFont = CreateFontIndirectW(lf)))
465 ReleaseDC(hWnd, hDC);
466 return NULL;
468 hOldFont = SelectObject(hDC, hFont);
469 GetTextMetricsW(hDC, &tm);
470 SelectObject(hDC, hOldFont);
471 ReleaseDC(hWnd, hDC);
473 config->cell_width = tm.tmMaxCharWidth;
474 config->cell_height = tm.tmHeight + tm.tmExternalLeading;
475 config->font_weight = tm.tmWeight;
476 lstrcpyW(config->face_name, lf->lfFaceName);
477 if (el) *el = tm.tmExternalLeading;
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 <= 4; 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\n");
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, sizeof(buff) / sizeof(buff[0]));
832 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
833 LoadStringW(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(buff[0]));
834 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
835 LoadStringW(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(buff[0]));
836 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
837 LoadStringW(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(buff[0]));
838 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
839 LoadStringW(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(buff[0]));
840 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
841 LoadStringW(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(buff[0]));
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, sizeof(buff) / sizeof(buff[0]));
846 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
847 LoadStringW(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(buff[0]));
848 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
849 LoadStringW(hInstance, IDS_PROPERTIES, buff, sizeof(buff) / sizeof(buff[0]));
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) * data->curcfg.cell_width;
1314 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
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;