ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / programs / wineconsole / user.c
blob03d0ea18e66b4c444d3cbe981076442325500907
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)
343 if (tm->tmMaxCharWidth * data->curcfg.win_width >= GetSystemMetrics(SM_CXSCREEN) ||
344 tm->tmHeight * data->curcfg.win_height >= GetSystemMetrics(SM_CYSCREEN))
345 return FALSE;
347 /* fall through */
348 case 1:
349 if (tm->tmCharSet != DEFAULT_CHARSET && tm->tmCharSet != g_uiDefaultCharset) return FALSE;
350 /* fall through */
351 case 2:
352 if (tm->tmItalic || tm->tmUnderlined || tm->tmStruckOut) return FALSE;
353 break;
355 return TRUE;
358 /******************************************************************
359 * WCUSER_ValidateFont
361 * Returns true if the font family described in lf is usable as a font for the renderer
363 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONTW* lf, int pass)
365 switch (pass) /* we get increasingly lenient in later passes */
367 case 0:
368 case 1:
369 if (lf->lfCharSet != DEFAULT_CHARSET && lf->lfCharSet != g_uiDefaultCharset) return FALSE;
370 /* fall through */
371 case 2:
372 if ((lf->lfPitchAndFamily & 3) != FIXED_PITCH) return FALSE;
373 /* fall through */
374 case 3:
375 if (lf->lfFaceName[0] == '@') return FALSE;
376 break;
378 return TRUE;
381 /******************************************************************
382 * get_first_font_enum_2
383 * get_first_font_enum
385 * Helper functions to get a decent font for the renderer
387 static int CALLBACK get_first_font_enum_2(const LOGFONTW* lf, const TEXTMETRICW* tm,
388 DWORD FontType, LPARAM lParam)
390 struct font_chooser* fc = (struct font_chooser*)lParam;
392 WCUSER_DumpTextMetric(tm, FontType);
393 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType, fc->pass))
395 LOGFONTW mlf = *lf;
397 /* Use the default sizes for the font (this is needed, especially for
398 * TrueType fonts, so that we get a decent size, not the max size)
400 mlf.lfWidth = fc->data->curcfg.cell_width;
401 mlf.lfHeight = fc->data->curcfg.cell_height;
402 if (WCUSER_SetFont(fc->data, &mlf))
404 struct config_data defcfg;
406 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
407 fc->done = 1;
408 /* since we've modified the current config with new font information,
409 * set this information as the new default.
411 WINECON_RegLoad(NULL, &defcfg);
412 defcfg.cell_width = fc->data->curcfg.cell_width;
413 defcfg.cell_height = fc->data->curcfg.cell_height;
414 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
415 /* Force also its writing back to the registry so that we can get it
416 * the next time.
418 WINECON_RegSave(&defcfg);
419 return 0;
422 return 1;
425 static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
426 DWORD FontType, LPARAM lParam)
428 struct font_chooser* fc = (struct font_chooser*)lParam;
430 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
431 if (WCUSER_ValidateFont(fc->data, lf, fc->pass))
433 EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
434 get_first_font_enum_2, lParam);
435 return !fc->done; /* we just need the first matching one... */
437 return 1;
440 /******************************************************************
441 * WCUSER_CopyFont
443 * get the relevant information from the font described in lf and store them
444 * in config
446 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
448 TEXTMETRICW tm;
449 HDC hDC;
450 HFONT hFont, hOldFont;
451 CPINFO cpinfo;
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.tmAveCharWidth;
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 /* FIXME: use maximum width for DBCS codepages since some chars take two cells */
471 if (GetCPInfo( GetConsoleOutputCP(), &cpinfo ) && cpinfo.MaxCharSize > 1)
472 config->cell_width = tm.tmMaxCharWidth;
474 return hFont;
477 /******************************************************************
478 * WCUSER_FillLogFont
482 void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
484 lf->lfHeight = height;
485 lf->lfWidth = 0;
486 lf->lfEscapement = 0;
487 lf->lfOrientation = 0;
488 lf->lfWeight = weight;
489 lf->lfItalic = FALSE;
490 lf->lfUnderline = FALSE;
491 lf->lfStrikeOut = FALSE;
492 lf->lfCharSet = DEFAULT_CHARSET;
493 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
494 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
495 lf->lfQuality = DEFAULT_QUALITY;
496 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
497 lstrcpyW(lf->lfFaceName, name);
500 /******************************************************************
501 * WCUSER_SetFont
503 * sets logfont as the new font for the console
505 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
507 HFONT hFont;
508 LONG el;
510 if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
511 return TRUE;
513 hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
514 if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
516 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
517 PRIVATE(data)->hFont = hFont;
518 PRIVATE(data)->ext_leading = el;
520 WCUSER_ComputePositions(data);
521 WCUSER_NewBitmap(data);
522 InvalidateRect(data->hWnd, NULL, FALSE);
523 UpdateWindow(data->hWnd);
525 return TRUE;
528 /******************************************************************
529 * WCUSER_SetFontPmt
531 * Sets a new font for the console.
532 * In fact a wrapper for WCUSER_SetFont
534 static void WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
535 unsigned height, unsigned weight)
537 LOGFONTW lf;
538 struct font_chooser fc;
540 WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
541 wine_dbgstr_wn(font, -1), height, weight);
543 if (font[0] != '\0' && height != 0 && weight != 0)
545 WCUSER_FillLogFont(&lf, font, height, weight);
546 if (WCUSER_SetFont(data, &lf))
548 WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
549 return;
553 /* try to find an acceptable font */
554 WINE_WARN("Couldn't match the font from registry... trying to find one\n");
555 fc.data = data;
556 fc.done = FALSE;
557 for (fc.pass = 0; fc.pass <= 4; fc.pass++)
559 EnumFontFamiliesW(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
560 if (fc.done) return;
562 WINECON_Fatal("Couldn't find a decent font, aborting");
565 /******************************************************************
566 * WCUSER_GetCell
568 * Get a cell from a relative coordinate in window (takes into
569 * account the scrolling)
571 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
573 COORD c;
575 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
576 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
578 return c;
581 /******************************************************************
582 * WCUSER_GetSelectionRect
584 * Get the selection rectangle
586 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
588 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) - data->curcfg.win_pos.X) * data->curcfg.cell_width;
589 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
590 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
591 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
594 /******************************************************************
595 * WCUSER_SetSelection
599 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
601 HDC hDC;
602 RECT r;
604 WCUSER_GetSelectionRect(data, &r);
605 hDC = hRefDC ? hRefDC : GetDC(data->hWnd);
606 if (hDC)
608 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
609 HideCaret(data->hWnd);
610 InvertRect(hDC, &r);
611 if (hDC != hRefDC)
612 ReleaseDC(data->hWnd, hDC);
613 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
614 ShowCaret(data->hWnd);
618 /******************************************************************
619 * WCUSER_MoveSelection
623 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
625 RECT r;
626 HDC hDC;
628 if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
629 c2.X < 0 || c2.X >= data->curcfg.sb_width ||
630 c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
631 c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
632 return;
634 WCUSER_GetSelectionRect(data, &r);
635 hDC = GetDC(data->hWnd);
636 if (hDC)
638 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
639 HideCaret(data->hWnd);
640 InvertRect(hDC, &r);
642 PRIVATE(data)->selectPt1 = c1;
643 PRIVATE(data)->selectPt2 = c2;
644 if (hDC)
646 WCUSER_GetSelectionRect(data, &r);
647 InvertRect(hDC, &r);
648 ReleaseDC(data->hWnd, hDC);
649 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
650 ShowCaret(data->hWnd);
654 /******************************************************************
655 * WCUSER_CopySelectionToClipboard
657 * Copies the current selection into the clipboard
659 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
661 HANDLE hMem;
662 LPWSTR p;
663 unsigned w, h;
665 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
666 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
668 if (!OpenClipboard(data->hWnd)) return;
669 EmptyClipboard();
671 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
672 if (hMem && (p = GlobalLock(hMem)))
674 COORD c;
675 int y;
677 c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
678 c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
680 for (y = 0; y < h; y++, c.Y++)
682 LPWSTR end;
683 DWORD count;
685 ReadConsoleOutputCharacterW(data->hConOut, p, w - 1, c, &count);
687 /* strip spaces from the end of the line */
688 end = p + w - 1;
689 while (end > p && *(end - 1) == ' ')
690 end--;
691 *end = (y < h - 1) ? '\n' : '\0';
692 p = end + 1;
694 GlobalUnlock(hMem);
695 SetClipboardData(CF_UNICODETEXT, hMem);
697 CloseClipboard();
700 /******************************************************************
701 * WCUSER_PasteFromClipboard
705 static void WCUSER_PasteFromClipboard(struct inner_data* data)
707 HANDLE h;
708 WCHAR* ptr;
710 if (!OpenClipboard(data->hWnd)) return;
711 h = GetClipboardData(CF_UNICODETEXT);
712 if (h && (ptr = GlobalLock(h)))
714 int i, len = GlobalSize(h) / sizeof(WCHAR);
715 INPUT_RECORD ir[2];
716 DWORD n;
717 SHORT sh;
719 ir[0].EventType = KEY_EVENT;
720 ir[0].Event.KeyEvent.wRepeatCount = 0;
721 ir[0].Event.KeyEvent.dwControlKeyState = 0;
722 ir[0].Event.KeyEvent.bKeyDown = TRUE;
724 /* generate the corresponding input records */
725 for (i = 0; i < len; i++)
727 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
728 sh = VkKeyScanW(ptr[i]);
729 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
730 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(LOBYTE(sh), 0);
731 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
733 ir[1] = ir[0];
734 ir[1].Event.KeyEvent.bKeyDown = FALSE;
736 WriteConsoleInputW(data->hConIn, ir, 2, &n);
738 GlobalUnlock(h);
740 CloseClipboard();
743 /******************************************************************
744 * WCUSER_Refresh
748 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
750 WCUSER_FillMemDC(data, tp, bm);
751 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
753 RECT r;
755 r.left = 0;
756 r.right = data->curcfg.win_width * data->curcfg.cell_width;
757 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
758 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
759 InvalidateRect(data->hWnd, &r, FALSE);
760 UpdateWindow(data->hWnd);
764 /******************************************************************
765 * WCUSER_Paint
769 static void WCUSER_Paint(const struct inner_data* data)
771 PAINTSTRUCT ps;
773 if (data->in_set_config) return; /* in order to avoid some flicker */
774 BeginPaint(data->hWnd, &ps);
775 BitBlt(ps.hdc, 0, 0,
776 data->curcfg.win_width * data->curcfg.cell_width,
777 data->curcfg.win_height * data->curcfg.cell_height,
778 PRIVATE(data)->hMemDC,
779 data->curcfg.win_pos.X * data->curcfg.cell_width,
780 data->curcfg.win_pos.Y * data->curcfg.cell_height,
781 SRCCOPY);
782 if (PRIVATE(data)->has_selection)
783 WCUSER_SetSelection(data, ps.hdc);
784 EndPaint(data->hWnd, &ps);
787 /******************************************************************
788 * WCUSER_Scroll
792 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
794 if (horz)
796 ScrollWindow(data->hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, NULL, NULL);
797 SetScrollPos(data->hWnd, SB_HORZ, pos, TRUE);
798 data->curcfg.win_pos.X = pos;
800 else
802 ScrollWindow(data->hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, NULL, NULL);
803 SetScrollPos(data->hWnd, SB_VERT, pos, TRUE);
804 data->curcfg.win_pos.Y = pos;
806 InvalidateRect(data->hWnd, NULL, FALSE);
809 /******************************************************************
810 * WCUSER_FillMenu
814 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
816 HMENU hSubMenu;
817 HINSTANCE hInstance = GetModuleHandleW(NULL);
818 WCHAR buff[256];
820 if (!hMenu) return FALSE;
822 /* FIXME: error handling & memory cleanup */
823 hSubMenu = CreateMenu();
824 if (!hSubMenu) return FALSE;
826 LoadStringW(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(buff[0]));
827 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
828 LoadStringW(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(buff[0]));
829 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
830 LoadStringW(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(buff[0]));
831 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
832 LoadStringW(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(buff[0]));
833 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
834 LoadStringW(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(buff[0]));
835 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
836 LoadStringW(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(buff[0]));
837 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
839 if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
840 LoadStringW(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(buff[0]));
841 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
842 LoadStringW(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(buff[0]));
843 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
844 LoadStringW(hInstance, IDS_PROPERTIES, buff, sizeof(buff) / sizeof(buff[0]));
845 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
847 return TRUE;
850 /******************************************************************
851 * WCUSER_SetMenuDetails
853 * Grays / ungrays the menu items according to their state
855 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
857 if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
859 EnableMenuItem(hMenu, IDS_COPY,
860 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
861 EnableMenuItem(hMenu, IDS_PASTE,
862 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
863 ? MF_ENABLED : MF_GRAYED));
864 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
865 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
868 /******************************************************************
869 * WCUSER_Create
871 * Creates the window for the rendering
873 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
875 struct inner_data* data;
876 HMENU hSysMenu;
878 data = lpcs->lpCreateParams;
879 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
880 data->hWnd = hWnd;
882 hSysMenu = GetSystemMenu(hWnd, FALSE);
883 if (!hSysMenu) return 0;
884 PRIVATE(data)->hPopMenu = CreatePopupMenu();
885 if (!PRIVATE(data)->hPopMenu) return 0;
887 WCUSER_FillMenu(hSysMenu, TRUE);
888 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
890 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
891 if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
893 data->curcfg.quick_edit = FALSE;
894 return 0;
897 /******************************************************************
898 * WCUSER_GetCtrlKeyState
900 * Get the console bit mask equivalent to the VK_ status in keyState
902 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
904 DWORD ret = 0;
906 GetKeyboardState(keyState);
907 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
908 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
909 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
910 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
911 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
912 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
913 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
914 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
916 return ret;
919 /******************************************************************
920 * WCUSER_HandleSelectionKey
922 * Handles keys while selecting an area
924 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
925 WPARAM wParam, LPARAM lParam)
927 BYTE keyState[256];
928 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
929 COORD c1, c2;
931 if (!down) return;
933 switch (state)
935 case 0:
936 switch (wParam)
938 case VK_RETURN:
939 PRIVATE(data)->has_selection = FALSE;
940 WCUSER_SetSelection(data, 0);
941 WCUSER_CopySelectionToClipboard(data);
942 return;
943 case VK_RIGHT:
944 c1 = PRIVATE(data)->selectPt1;
945 c2 = PRIVATE(data)->selectPt2;
946 c1.X++; c2.X++;
947 WCUSER_MoveSelection(data, c1, c2);
948 return;
949 case VK_LEFT:
950 c1 = PRIVATE(data)->selectPt1;
951 c2 = PRIVATE(data)->selectPt2;
952 c1.X--; c2.X--;
953 WCUSER_MoveSelection(data, c1, c2);
954 return;
955 case VK_UP:
956 c1 = PRIVATE(data)->selectPt1;
957 c2 = PRIVATE(data)->selectPt2;
958 c1.Y--; c2.Y--;
959 WCUSER_MoveSelection(data, c1, c2);
960 return;
961 case VK_DOWN:
962 c1 = PRIVATE(data)->selectPt1;
963 c2 = PRIVATE(data)->selectPt2;
964 c1.Y++; c2.Y++;
965 WCUSER_MoveSelection(data, c1, c2);
966 return;
968 break;
969 case SHIFT_PRESSED:
970 switch (wParam)
972 case VK_RIGHT:
973 c1 = PRIVATE(data)->selectPt1;
974 c2 = PRIVATE(data)->selectPt2;
975 c2.X++;
976 WCUSER_MoveSelection(data, c1, c2);
977 return;
978 case VK_LEFT:
979 c1 = PRIVATE(data)->selectPt1;
980 c2 = PRIVATE(data)->selectPt2;
981 c2.X--;
982 WCUSER_MoveSelection(data, c1, c2);
983 return;
984 case VK_UP:
985 c1 = PRIVATE(data)->selectPt1;
986 c2 = PRIVATE(data)->selectPt2;
987 c2.Y--;
988 WCUSER_MoveSelection(data, c1, c2);
989 return;
990 case VK_DOWN:
991 c1 = PRIVATE(data)->selectPt1;
992 c2 = PRIVATE(data)->selectPt2;
993 c2.Y++;
994 WCUSER_MoveSelection(data, c1, c2);
995 return;
997 break;
1000 if (wParam < VK_SPACE) /* Shift, Alt, Ctrl, Num Lock etc. */
1001 return;
1003 WCUSER_SetSelection(data, 0);
1004 PRIVATE(data)->has_selection = FALSE;
1007 /******************************************************************
1008 * WCUSER_GenerateKeyInputRecord
1010 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1012 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1013 WPARAM wParam, LPARAM lParam)
1015 INPUT_RECORD ir;
1016 DWORD n;
1017 WCHAR buf[2];
1018 static WCHAR last; /* keep last char seen as feed for key up message */
1019 BYTE keyState[256];
1021 ir.EventType = KEY_EVENT;
1022 ir.Event.KeyEvent.bKeyDown = down;
1023 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1024 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1026 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1028 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1029 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1030 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1032 if (down)
1034 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1036 case 2:
1037 /* FIXME... should generate two events... */
1038 /* fall through */
1039 case 1:
1040 last = buf[0];
1041 break;
1042 default:
1043 last = 0;
1044 break;
1047 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME: HACKY... and buggy because it should be a stack, not a single value */
1048 if (!down) last = 0;
1050 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1053 /******************************************************************
1054 * WCUSER_GenerateMouseInputRecord
1058 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1059 WPARAM wParam, DWORD event)
1061 INPUT_RECORD ir;
1062 BYTE keyState[256];
1063 DWORD mode, n;
1065 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1066 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1067 return;
1069 ir.EventType = MOUSE_EVENT;
1070 ir.Event.MouseEvent.dwMousePosition = c;
1071 ir.Event.MouseEvent.dwButtonState = 0;
1072 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1073 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1074 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1075 if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
1076 if (wParam & MK_SHIFT) ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
1077 if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1078 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1079 ir.Event.MouseEvent.dwEventFlags = event;
1081 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1084 /******************************************************************
1085 * WCUSER_Proc
1089 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1091 struct inner_data* data = (struct inner_data*)GetWindowLongPtrW(hWnd, 0);
1093 switch (uMsg)
1095 case WM_CREATE:
1096 return WCUSER_Create(hWnd, (LPCREATESTRUCTW)lParam);
1097 case WM_DESTROY:
1098 data->hWnd = 0;
1099 PostQuitMessage(0);
1100 break;
1101 case WM_PAINT:
1102 WCUSER_Paint(data);
1103 break;
1104 case WM_KEYDOWN:
1105 case WM_KEYUP:
1106 if (PRIVATE(data)->has_selection)
1107 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1108 else
1109 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam);
1110 break;
1111 case WM_SYSKEYDOWN:
1112 case WM_SYSKEYUP:
1113 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam);
1114 break;
1115 case WM_LBUTTONDOWN:
1116 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1118 if (PRIVATE(data)->has_selection)
1119 WCUSER_SetSelection(data, 0);
1121 if (data->curcfg.quick_edit && PRIVATE(data)->has_selection)
1123 PRIVATE(data)->has_selection = FALSE;
1125 else
1127 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1128 SetCapture(data->hWnd);
1129 WCUSER_SetSelection(data, 0);
1130 PRIVATE(data)->has_selection = TRUE;
1133 else
1135 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1137 break;
1138 case WM_MOUSEMOVE:
1139 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1141 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection &&
1142 (wParam & MK_LBUTTON))
1144 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1147 else
1149 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1151 break;
1152 case WM_LBUTTONUP:
1153 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1155 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection)
1157 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1158 ReleaseCapture();
1161 else
1163 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1165 break;
1166 case WM_RBUTTONDOWN:
1167 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1169 POINT pt;
1171 pt.x = (short)LOWORD(lParam);
1172 pt.y = (short)HIWORD(lParam);
1173 ClientToScreen(hWnd, &pt);
1174 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1175 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON,
1176 pt.x, pt.y, 0, hWnd, NULL);
1178 else
1180 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1182 break;
1183 case WM_RBUTTONUP:
1184 /* no need to track for rbutton up when opening the popup... the event will be
1185 * swallowed by TrackPopupMenu */
1186 case WM_MBUTTONDOWN:
1187 case WM_MBUTTONUP:
1188 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1189 break;
1190 case WM_LBUTTONDBLCLK:
1191 case WM_MBUTTONDBLCLK:
1192 case WM_RBUTTONDBLCLK:
1193 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
1194 break;
1195 case WM_SETFOCUS:
1196 if (data->curcfg.cursor_visible)
1198 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
1199 data->curcfg.cell_width, data->curcfg.cell_height);
1200 WCUSER_PosCursor(data);
1202 break;
1203 case WM_KILLFOCUS:
1204 if (data->curcfg.cursor_visible)
1205 DestroyCaret();
1206 break;
1207 case WM_HSCROLL:
1209 struct config_data cfg = data->curcfg;
1211 switch (LOWORD(wParam))
1213 case SB_PAGEUP: cfg.win_pos.X -= 8; break;
1214 case SB_PAGEDOWN: cfg.win_pos.X += 8; break;
1215 case SB_LINEUP: cfg.win_pos.X--; break;
1216 case SB_LINEDOWN: cfg.win_pos.X++; break;
1217 case SB_THUMBTRACK: cfg.win_pos.X = HIWORD(wParam); break;
1218 default: break;
1220 if (cfg.win_pos.X < 0) cfg.win_pos.X = 0;
1221 if (cfg.win_pos.X > data->curcfg.sb_width - data->curcfg.win_width)
1222 cfg.win_pos.X = data->curcfg.sb_width - data->curcfg.win_width;
1223 if (cfg.win_pos.X != data->curcfg.win_pos.X)
1225 WINECON_SetConfig(data, &cfg);
1228 break;
1229 case WM_MOUSEWHEEL:
1230 if (data->curcfg.sb_height <= data->curcfg.win_height)
1232 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1233 break;
1235 /* else fallthrough */
1236 case WM_VSCROLL:
1238 struct config_data cfg = data->curcfg;
1240 if (uMsg == WM_MOUSEWHEEL)
1242 UINT scrollLines = 3;
1243 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
1244 scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1245 cfg.win_pos.Y += scrollLines;
1246 } else {
1247 switch (LOWORD(wParam))
1249 case SB_PAGEUP: cfg.win_pos.Y -= 8; break;
1250 case SB_PAGEDOWN: cfg.win_pos.Y += 8; break;
1251 case SB_LINEUP: cfg.win_pos.Y--; break;
1252 case SB_LINEDOWN: cfg.win_pos.Y++; break;
1253 case SB_THUMBTRACK: cfg.win_pos.Y = HIWORD(wParam); break;
1254 default: break;
1258 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
1259 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
1260 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
1261 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
1263 WINECON_SetConfig(data, &cfg);
1266 break;
1267 case WM_SYSCOMMAND:
1268 switch (wParam)
1270 case IDS_DEFAULT:
1271 WCUSER_GetProperties(data, FALSE);
1272 break;
1273 case IDS_PROPERTIES:
1274 WCUSER_GetProperties(data, TRUE);
1275 break;
1276 default:
1277 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1279 break;
1280 case WM_COMMAND:
1281 switch (wParam)
1283 case IDS_DEFAULT:
1284 WCUSER_GetProperties(data, FALSE);
1285 break;
1286 case IDS_PROPERTIES:
1287 WCUSER_GetProperties(data, TRUE);
1288 break;
1289 case IDS_MARK:
1290 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1291 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1292 WCUSER_SetSelection(data, 0);
1293 PRIVATE(data)->has_selection = TRUE;
1294 break;
1295 case IDS_COPY:
1296 if (PRIVATE(data)->has_selection)
1298 PRIVATE(data)->has_selection = FALSE;
1299 WCUSER_SetSelection(data, 0);
1300 WCUSER_CopySelectionToClipboard(data);
1302 break;
1303 case IDS_PASTE:
1304 WCUSER_PasteFromClipboard(data);
1305 break;
1306 case IDS_SELECTALL:
1307 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1308 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1309 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1310 WCUSER_SetSelection(data, 0);
1311 PRIVATE(data)->has_selection = TRUE;
1312 break;
1313 case IDS_SCROLL:
1314 case IDS_SEARCH:
1315 WINE_FIXME("Unhandled yet command: %lx\n", wParam);
1316 break;
1317 default:
1318 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1320 break;
1321 case WM_INITMENUPOPUP:
1322 if (!HIWORD(lParam)) return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1323 WCUSER_SetMenuDetails(data, GetSystemMenu(data->hWnd, FALSE));
1324 break;
1325 case WM_SIZE:
1326 WINECON_ResizeWithContainer(data, LOWORD(lParam) / data->curcfg.cell_width,
1327 HIWORD(lParam) / data->curcfg.cell_height);
1328 break;
1329 default:
1330 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1332 return 0;
1335 /******************************************************************
1336 * WCUSER_DeleteBackend
1340 static void WCUSER_DeleteBackend(struct inner_data* data)
1342 if (!PRIVATE(data)) return;
1343 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1344 if (data->hWnd) DestroyWindow(data->hWnd);
1345 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1346 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1347 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1348 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1351 /******************************************************************
1352 * WCUSER_MainLoop
1356 static int WCUSER_MainLoop(struct inner_data* data)
1358 MSG msg;
1360 ShowWindow(data->hWnd, data->nCmdShow);
1361 while (!data->dying || !data->curcfg.exit_on_die)
1363 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1365 case WAIT_OBJECT_0:
1366 WINECON_GrabChanges(data);
1367 break;
1368 case WAIT_OBJECT_0+1:
1369 /* need to use PeekMessageW loop instead of simple GetMessage:
1370 * multiple messages might have arrived in between,
1371 * so GetMessage would lead to delayed processing */
1372 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
1374 if (msg.message == WM_QUIT) return 1;
1375 WINE_TRACE("dispatching msg %04x\n", msg.message);
1376 DispatchMessageW(&msg);
1378 break;
1379 default:
1380 WINE_ERR("got pb\n");
1381 /* err */
1382 break;
1385 PostQuitMessage(0);
1386 return 0;
1389 /******************************************************************
1390 * WCUSER_InitBackend
1392 * Initialisation part II: creation of window.
1395 enum init_return WCUSER_InitBackend(struct inner_data* data)
1397 static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1399 WNDCLASSW wndclass;
1400 CHARSETINFO ci;
1402 if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
1403 return init_failed;
1404 g_uiDefaultCharset = ci.ciCharset;
1405 WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1407 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1408 if (!data->private) return init_failed;
1410 data->fnMainLoop = WCUSER_MainLoop;
1411 data->fnPosCursor = WCUSER_PosCursor;
1412 data->fnShapeCursor = WCUSER_ShapeCursor;
1413 data->fnComputePositions = WCUSER_ComputePositions;
1414 data->fnRefresh = WCUSER_Refresh;
1415 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1416 data->fnSetTitle = WCUSER_SetTitle;
1417 data->fnSetFont = WCUSER_SetFontPmt;
1418 data->fnScroll = WCUSER_Scroll;
1419 data->fnDeleteBackend = WCUSER_DeleteBackend;
1421 wndclass.style = CS_DBLCLKS;
1422 wndclass.lpfnWndProc = WCUSER_Proc;
1423 wndclass.cbClsExtra = 0;
1424 wndclass.cbWndExtra = sizeof(DWORD_PTR);
1425 wndclass.hInstance = GetModuleHandleW(NULL);
1426 wndclass.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
1427 wndclass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1428 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1429 wndclass.lpszMenuName = NULL;
1430 wndclass.lpszClassName = wClassName;
1432 RegisterClassW(&wndclass);
1434 data->hWnd = CreateWindowW(wndclass.lpszClassName, NULL,
1435 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1436 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1437 if (!data->hWnd) return init_not_supported;
1439 return init_success;