qcap/tests: Add media tests for the SmartTee filter.
[wine/multimedia.git] / programs / wineconsole / user.c
blob5c8c2c0281d4977ff6fa07333763d2778ef38eef
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;
59 /* no font has been set up yet, don't worry about filling the bitmap,
60 * we'll do it once a font is chosen
62 if (!PRIVATE(data)->hFont) return;
64 /* FIXME: could set up a mechanism to reuse the line between different
65 * calls to this function
67 if (!(line = HeapAlloc(GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(WCHAR))))
68 WINECON_Fatal("OOM\n");
70 hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
71 for (j = upd_tp; j <= upd_bm; j++)
73 cell = &data->cells[j * data->curcfg.sb_width];
74 for (i = 0; i < data->curcfg.sb_width; i++)
76 attr = cell[i].Attributes;
77 SetBkColor(PRIVATE(data)->hMemDC, WCUSER_ColorMap[(attr>>4)&0x0F]);
78 SetTextColor(PRIVATE(data)->hMemDC, WCUSER_ColorMap[attr&0x0F]);
79 for (k = i; k < data->curcfg.sb_width && cell[k].Attributes == attr; k++)
81 line[k - i] = cell[k].Char.UnicodeChar;
83 TextOutW(PRIVATE(data)->hMemDC, i * data->curcfg.cell_width,
84 j * data->curcfg.cell_height, line, k - i);
85 if (PRIVATE(data)->ext_leading &&
86 (hbr = CreateSolidBrush(WCUSER_ColorMap[(attr>>4)&0x0F])))
88 r.left = i * data->curcfg.cell_width;
89 r.top = (j + 1) * data->curcfg.cell_height - PRIVATE(data)->ext_leading;
90 r.right = k * data->curcfg.cell_width;
91 r.bottom = (j + 1) * data->curcfg.cell_height;
92 FillRect(PRIVATE(data)->hMemDC, &r, hbr);
93 DeleteObject(hbr);
95 i = k - 1;
98 SelectObject(PRIVATE(data)->hMemDC, hOldFont);
99 HeapFree(GetProcessHeap(), 0, line);
102 /******************************************************************
103 * WCUSER_NewBitmap
105 * Either the font geometry or the sb geometry has changed. we need
106 * to recreate the bitmap geometry.
108 static void WCUSER_NewBitmap(struct inner_data* data)
110 HDC hDC;
111 HBITMAP hnew, hold;
113 if (!data->curcfg.sb_width || !data->curcfg.sb_height ||
114 !PRIVATE(data)->hFont || !(hDC = GetDC(data->hWnd)))
115 return;
116 hnew = CreateCompatibleBitmap(hDC,
117 data->curcfg.sb_width * data->curcfg.cell_width,
118 data->curcfg.sb_height * data->curcfg.cell_height);
119 ReleaseDC(data->hWnd, hDC);
120 hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
122 if (PRIVATE(data)->hBitmap)
124 if (hold == PRIVATE(data)->hBitmap)
125 DeleteObject(PRIVATE(data)->hBitmap);
126 else
127 WINE_FIXME("leak\n");
129 PRIVATE(data)->hBitmap = hnew;
130 WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
133 /******************************************************************
134 * WCUSER_ResizeScreenBuffer
138 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
140 WCUSER_NewBitmap(data);
143 /******************************************************************
144 * WCUSER_PosCursor
146 * Set a new position for the cursor
148 static void WCUSER_PosCursor(const struct inner_data* data)
150 if (data->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
152 SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
153 (data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
154 ShowCaret(data->hWnd);
157 /******************************************************************
158 * WCUSER_ShapeCursor
160 * Sets a new shape for the cursor
162 static void WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
164 if (force || size != data->curcfg.cursor_size)
166 if (data->curcfg.cursor_visible && data->hWnd == GetFocus()) DestroyCaret();
167 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
168 PRIVATE(data)->cursor_bitmap = NULL;
169 if (size != 100)
171 int w16b; /* number of bytes per row, aligned on word size */
172 BYTE* ptr;
173 int i, j, nbl;
175 w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
176 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
177 if (!ptr) WINECON_Fatal("OOM");
178 nbl = max((data->curcfg.cell_height * size) / 100, 1);
179 for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
181 for (i = 0; i < data->curcfg.cell_width; i++)
183 ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
186 PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
187 data->curcfg.cell_height, 1, 1, ptr);
188 HeapFree(GetProcessHeap(), 0, ptr);
190 data->curcfg.cursor_size = size;
191 data->curcfg.cursor_visible = -1;
194 vis = vis != 0;
195 if (force || vis != data->curcfg.cursor_visible)
197 data->curcfg.cursor_visible = vis;
198 if (data->hWnd == GetFocus())
200 if (vis)
202 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
203 data->curcfg.cell_width, data->curcfg.cell_height);
204 WCUSER_PosCursor(data);
206 else
208 DestroyCaret();
212 WINECON_DumpConfig("crsr", &data->curcfg);
215 /******************************************************************
216 * WCUSER_ComputePositions
218 * Recomputes all the components (mainly scroll bars) positions
220 static void WCUSER_ComputePositions(struct inner_data* data)
222 RECT r;
223 int dx, dy;
225 /* compute window size from desired client size */
226 r.left = r.top = 0;
227 r.right = data->curcfg.win_width * data->curcfg.cell_width;
228 r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
230 if (IsRectEmpty(&r)) return;
232 AdjustWindowRect(&r, GetWindowLongW(data->hWnd, GWL_STYLE), FALSE);
234 dx = dy = 0;
235 if (data->curcfg.sb_width > data->curcfg.win_width)
237 dy = GetSystemMetrics(SM_CYHSCROLL);
238 SetScrollRange(data->hWnd, SB_HORZ, 0,
239 data->curcfg.sb_width - data->curcfg.win_width, FALSE);
240 SetScrollPos(data->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
241 ShowScrollBar(data->hWnd, SB_HORZ, TRUE);
243 else
245 ShowScrollBar(data->hWnd, SB_HORZ, FALSE);
248 if (data->curcfg.sb_height > data->curcfg.win_height)
250 dx = GetSystemMetrics(SM_CXVSCROLL);
251 SetScrollRange(data->hWnd, SB_VERT, 0,
252 data->curcfg.sb_height - data->curcfg.win_height, FALSE);
253 SetScrollPos(data->hWnd, SB_VERT, 0, FALSE); /* FIXME */
254 ShowScrollBar(data->hWnd, SB_VERT, TRUE);
256 else
258 ShowScrollBar(data->hWnd, SB_VERT, FALSE);
261 SetWindowPos(data->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
262 SWP_NOMOVE|SWP_NOZORDER);
263 WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
264 WCUSER_PosCursor(data);
267 /******************************************************************
268 * WCUSER_SetTitle
270 * Sets the title to the wine console
272 static void WCUSER_SetTitle(const struct inner_data* data)
274 WCHAR buffer[256];
276 if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
277 SetWindowTextW(data->hWnd, buffer);
280 void WCUSER_DumpLogFont(const char* pfx, const LOGFONTW* lf, DWORD ft)
282 WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
283 "\tlf.lfHeight=%d lf.lfWidth=%d lf.lfEscapement=%d lf.lfOrientation=%d\n"
284 "\tlf.lfWeight=%d lf.lfItalic=%u lf.lfUnderline=%u lf.lfStrikeOut=%u\n"
285 "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
286 "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
287 pfx,
288 (ft & RASTER_FONTTYPE) ? "raster" : "",
289 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
290 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
291 (ft & DEVICE_FONTTYPE) ? "|device" : "",
292 lf->lfHeight, lf->lfWidth, lf->lfEscapement, lf->lfOrientation,
293 lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
294 lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
295 wine_dbgstr_w(lf->lfFaceName));
298 void WCUSER_DumpTextMetric(const TEXTMETRICW* tm, DWORD ft)
300 WINE_TRACE_(wc_font)("%s%s%s%s\n"
301 "\ttmHeight=%d tmAscent=%d tmDescent=%d tmInternalLeading=%d tmExternalLeading=%d\n"
302 "\ttmAveCharWidth=%d tmMaxCharWidth=%d tmWeight=%d tmOverhang=%d\n"
303 "\ttmDigitizedAspectX=%d tmDigitizedAspectY=%d\n"
304 "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
305 "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
306 (ft & RASTER_FONTTYPE) ? "raster" : "",
307 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
308 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
309 (ft & DEVICE_FONTTYPE) ? "|device" : "",
310 tm->tmHeight, tm->tmAscent, tm->tmDescent, tm->tmInternalLeading, tm->tmExternalLeading, tm->tmAveCharWidth,
311 tm->tmMaxCharWidth, tm->tmWeight, tm->tmOverhang, tm->tmDigitizedAspectX, tm->tmDigitizedAspectY,
312 tm->tmFirstChar, tm->tmLastChar, tm->tmDefaultChar, tm->tmBreakChar, tm->tmItalic, tm->tmUnderlined, tm->tmStruckOut,
313 tm->tmPitchAndFamily, tm->tmCharSet);
316 /******************************************************************
317 * WCUSER_AreFontsEqual
321 static BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONTW* lf)
323 return lf->lfHeight == config->cell_height &&
324 lf->lfWeight == config->font_weight &&
325 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
326 !lstrcmpW(lf->lfFaceName, config->face_name);
329 struct font_chooser
331 struct inner_data* data;
332 int done;
335 /******************************************************************
336 * WCUSER_ValidateFontMetric
338 * Returns true if the font described in tm is usable as a font for the renderer
340 BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRICW* tm, DWORD fontType)
342 BOOL ret = TRUE;
344 if (fontType & RASTER_FONTTYPE)
345 ret = (tm->tmMaxCharWidth * data->curcfg.win_width < GetSystemMetrics(SM_CXSCREEN) &&
346 tm->tmHeight * data->curcfg.win_height < GetSystemMetrics(SM_CYSCREEN));
347 return ret && !tm->tmItalic && !tm->tmUnderlined && !tm->tmStruckOut &&
348 (tm->tmCharSet == DEFAULT_CHARSET || tm->tmCharSet == g_uiDefaultCharset);
351 /******************************************************************
352 * WCUSER_ValidateFont
354 * Returns true if the font family described in lf is usable as a font for the renderer
356 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONTW* lf)
358 return (lf->lfPitchAndFamily & 3) == FIXED_PITCH &&
359 /* (lf->lfPitchAndFamily & 0xF0) == FF_MODERN && */
360 lf->lfFaceName[0] != '@' &&
361 (lf->lfCharSet == DEFAULT_CHARSET || lf->lfCharSet == g_uiDefaultCharset);
364 /******************************************************************
365 * get_first_font_enum_2
366 * get_first_font_enum
368 * Helper functions to get a decent font for the renderer
370 static int CALLBACK get_first_font_enum_2(const LOGFONTW* lf, const TEXTMETRICW* tm,
371 DWORD FontType, LPARAM lParam)
373 struct font_chooser* fc = (struct font_chooser*)lParam;
375 WCUSER_DumpTextMetric(tm, FontType);
376 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType))
378 LOGFONTW mlf = *lf;
380 /* Use the default sizes for the font (this is needed, especially for
381 * TrueType fonts, so that we get a decent size, not the max size)
383 mlf.lfWidth = fc->data->curcfg.cell_width;
384 mlf.lfHeight = fc->data->curcfg.cell_height;
385 if (WCUSER_SetFont(fc->data, &mlf))
387 struct config_data defcfg;
389 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
390 fc->done = 1;
391 /* since we've modified the current config with new font information,
392 * set this information as the new default.
394 WINECON_RegLoad(NULL, &defcfg);
395 defcfg.cell_width = fc->data->curcfg.cell_width;
396 defcfg.cell_height = fc->data->curcfg.cell_height;
397 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
398 /* Force also its writing back to the registry so that we can get it
399 * the next time.
401 WINECON_RegSave(&defcfg);
402 return 0;
405 return 1;
408 static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
409 DWORD FontType, LPARAM lParam)
411 struct font_chooser* fc = (struct font_chooser*)lParam;
413 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
414 if (WCUSER_ValidateFont(fc->data, lf))
416 EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
417 get_first_font_enum_2, lParam);
418 return !fc->done; /* we just need the first matching one... */
420 return 1;
423 /******************************************************************
424 * WCUSER_CopyFont
426 * get the relevant information from the font described in lf and store them
427 * in config
429 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
431 TEXTMETRICW tm;
432 HDC hDC;
433 HFONT hFont, hOldFont;
434 int w, i, buf[256];
436 if (!(hDC = GetDC(hWnd))) return NULL;
437 if (!(hFont = CreateFontIndirectW(lf))) goto err1;
439 hOldFont = SelectObject(hDC, hFont);
440 GetTextMetricsW(hDC, &tm);
442 /* FIXME:
443 * the current freetype engine (at least 2.0.x with x <= 8) and its implementation
444 * in Wine don't return adequate values for fixed fonts
445 * In Windows, those fonts are expected to return the same value for
446 * - the average width
447 * - the largest width
448 * - the width of all characters in the font
449 * This isn't true in Wine. As a temporary workaround, we get as the width of the
450 * cell, the width of the first character in the font, after checking that all
451 * characters in the font have the same width (I hear paranoia coming)
452 * when this gets fixed, the code should be using tm.tmAveCharWidth
453 * or tm.tmMaxCharWidth as the cell width.
455 GetCharWidth32W(hDC, tm.tmFirstChar, tm.tmFirstChar, &w);
456 for (i = tm.tmFirstChar + 1; i <= tm.tmLastChar; i += sizeof(buf) / sizeof(buf[0]))
458 int j, k;
460 k = min(tm.tmLastChar - i, sizeof(buf) / sizeof(buf[0]) - 1);
461 GetCharWidth32W(hDC, i, i + k, buf);
462 for (j = 0; j <= k; j++)
464 if (buf[j] != w)
466 WINE_WARN("Non uniform cell width: [%d]=%d [%d]=%d\n"
467 "This may be caused by old freetype libraries, >= 2.0.8 is recommended\n",
468 i + j, buf[j], tm.tmFirstChar, w);
469 goto err;
473 SelectObject(hDC, hOldFont);
474 ReleaseDC(hWnd, hDC);
476 config->cell_width = w;
477 config->cell_height = tm.tmHeight + tm.tmExternalLeading;
478 config->font_weight = tm.tmWeight;
479 lstrcpyW(config->face_name, lf->lfFaceName);
480 if (el) *el = tm.tmExternalLeading;
482 return hFont;
483 err:
484 if (hDC && hOldFont) SelectObject(hDC, hOldFont);
485 if (hFont) DeleteObject(hFont);
486 err1:
487 if (hDC) ReleaseDC(hWnd, hDC);
489 return NULL;
492 /******************************************************************
493 * WCUSER_FillLogFont
497 void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
499 lf->lfHeight = height;
500 lf->lfWidth = 0;
501 lf->lfEscapement = 0;
502 lf->lfOrientation = 0;
503 lf->lfWeight = weight;
504 lf->lfItalic = FALSE;
505 lf->lfUnderline = FALSE;
506 lf->lfStrikeOut = FALSE;
507 lf->lfCharSet = DEFAULT_CHARSET;
508 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
509 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
510 lf->lfQuality = DEFAULT_QUALITY;
511 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
512 lstrcpyW(lf->lfFaceName, name);
515 /******************************************************************
516 * WCUSER_SetFont
518 * sets logfont as the new font for the console
520 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
522 HFONT hFont;
523 LONG el;
525 if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
526 return TRUE;
528 hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
529 if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
531 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
532 PRIVATE(data)->hFont = hFont;
533 PRIVATE(data)->ext_leading = el;
535 WCUSER_ComputePositions(data);
536 WCUSER_NewBitmap(data);
537 InvalidateRect(data->hWnd, NULL, FALSE);
538 UpdateWindow(data->hWnd);
540 return TRUE;
543 /******************************************************************
544 * WCUSER_SetFontPmt
546 * Sets a new font for the console.
547 * In fact a wrapper for WCUSER_SetFont
549 static void WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
550 unsigned height, unsigned weight)
552 LOGFONTW lf;
553 struct font_chooser fc;
555 WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
556 wine_dbgstr_wn(font, -1), height, weight);
558 if (font[0] != '\0' && height != 0 && weight != 0)
560 WCUSER_FillLogFont(&lf, font, height, weight);
561 if (WCUSER_SetFont(data, &lf))
563 WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
564 return;
568 /* try to find an acceptable font */
569 WINE_WARN("Couldn't match the font from registry... trying to find one\n");
570 fc.data = data;
571 fc.done = 0;
572 EnumFontFamiliesW(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
573 if (!fc.done) WINECON_Fatal("Couldn't find a decent font, aborting\n");
576 /******************************************************************
577 * WCUSER_GetCell
579 * Get a cell from a relative coordinate in window (takes into
580 * account the scrolling)
582 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
584 COORD c;
586 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
587 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
589 return c;
592 /******************************************************************
593 * WCUSER_GetSelectionRect
595 * Get the selection rectangle
597 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
599 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) - data->curcfg.win_pos.X) * data->curcfg.cell_width;
600 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
601 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
602 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
605 /******************************************************************
606 * WCUSER_SetSelection
610 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
612 HDC hDC;
613 RECT r;
615 WCUSER_GetSelectionRect(data, &r);
616 hDC = hRefDC ? hRefDC : GetDC(data->hWnd);
617 if (hDC)
619 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
620 HideCaret(data->hWnd);
621 InvertRect(hDC, &r);
622 if (hDC != hRefDC)
623 ReleaseDC(data->hWnd, hDC);
624 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
625 ShowCaret(data->hWnd);
629 /******************************************************************
630 * WCUSER_MoveSelection
634 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
636 RECT r;
637 HDC hDC;
639 if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
640 c2.X < 0 || c2.X >= data->curcfg.sb_width ||
641 c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
642 c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
643 return;
645 WCUSER_GetSelectionRect(data, &r);
646 hDC = GetDC(data->hWnd);
647 if (hDC)
649 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
650 HideCaret(data->hWnd);
651 InvertRect(hDC, &r);
653 PRIVATE(data)->selectPt1 = c1;
654 PRIVATE(data)->selectPt2 = c2;
655 if (hDC)
657 WCUSER_GetSelectionRect(data, &r);
658 InvertRect(hDC, &r);
659 ReleaseDC(data->hWnd, hDC);
660 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
661 ShowCaret(data->hWnd);
665 /******************************************************************
666 * WCUSER_CopySelectionToClipboard
668 * Copies the current selection into the clipboard
670 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
672 HANDLE hMem;
673 LPWSTR p;
674 unsigned w, h;
676 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
677 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
679 if (!OpenClipboard(data->hWnd)) return;
680 EmptyClipboard();
682 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
683 if (hMem && (p = GlobalLock(hMem)))
685 COORD c;
686 int y;
688 c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
689 c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
691 for (y = 0; y < h; y++, c.Y++)
693 LPWSTR end;
694 DWORD count;
696 ReadConsoleOutputCharacterW(data->hConOut, p, w - 1, c, &count);
698 /* strip spaces from the end of the line */
699 end = p + w - 1;
700 while (end > p && *(end - 1) == ' ')
701 end--;
702 *end = (y < h - 1) ? '\n' : '\0';
703 p = end + 1;
705 GlobalUnlock(hMem);
706 SetClipboardData(CF_UNICODETEXT, hMem);
708 CloseClipboard();
711 /******************************************************************
712 * WCUSER_PasteFromClipboard
716 static void WCUSER_PasteFromClipboard(struct inner_data* data)
718 HANDLE h;
719 WCHAR* ptr;
721 if (!OpenClipboard(data->hWnd)) return;
722 h = GetClipboardData(CF_UNICODETEXT);
723 if (h && (ptr = GlobalLock(h)))
725 int i, len = GlobalSize(h) / sizeof(WCHAR);
726 INPUT_RECORD ir[2];
727 DWORD n;
728 SHORT sh;
730 ir[0].EventType = KEY_EVENT;
731 ir[0].Event.KeyEvent.wRepeatCount = 0;
732 ir[0].Event.KeyEvent.dwControlKeyState = 0;
733 ir[0].Event.KeyEvent.bKeyDown = TRUE;
735 /* generate the corresponding input records */
736 for (i = 0; i < len; i++)
738 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
739 sh = VkKeyScanW(ptr[i]);
740 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
741 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(LOBYTE(sh), 0);
742 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
744 ir[1] = ir[0];
745 ir[1].Event.KeyEvent.bKeyDown = FALSE;
747 WriteConsoleInputW(data->hConIn, ir, 2, &n);
749 GlobalUnlock(h);
751 CloseClipboard();
754 /******************************************************************
755 * WCUSER_Refresh
759 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
761 WCUSER_FillMemDC(data, tp, bm);
762 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
764 RECT r;
766 r.left = 0;
767 r.right = data->curcfg.win_width * data->curcfg.cell_width;
768 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
769 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
770 InvalidateRect(data->hWnd, &r, FALSE);
771 UpdateWindow(data->hWnd);
775 /******************************************************************
776 * WCUSER_Paint
780 static void WCUSER_Paint(const struct inner_data* data)
782 PAINTSTRUCT ps;
784 if (data->in_set_config) return; /* in order to avoid some flicker */
785 BeginPaint(data->hWnd, &ps);
786 BitBlt(ps.hdc, 0, 0,
787 data->curcfg.win_width * data->curcfg.cell_width,
788 data->curcfg.win_height * data->curcfg.cell_height,
789 PRIVATE(data)->hMemDC,
790 data->curcfg.win_pos.X * data->curcfg.cell_width,
791 data->curcfg.win_pos.Y * data->curcfg.cell_height,
792 SRCCOPY);
793 if (PRIVATE(data)->has_selection)
794 WCUSER_SetSelection(data, ps.hdc);
795 EndPaint(data->hWnd, &ps);
798 /******************************************************************
799 * WCUSER_Scroll
803 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
805 if (horz)
807 ScrollWindow(data->hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, NULL, NULL);
808 SetScrollPos(data->hWnd, SB_HORZ, pos, TRUE);
809 data->curcfg.win_pos.X = pos;
811 else
813 ScrollWindow(data->hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, NULL, NULL);
814 SetScrollPos(data->hWnd, SB_VERT, pos, TRUE);
815 data->curcfg.win_pos.Y = pos;
817 InvalidateRect(data->hWnd, NULL, FALSE);
820 /******************************************************************
821 * WCUSER_FillMenu
825 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
827 HMENU hSubMenu;
828 HINSTANCE hInstance = GetModuleHandleW(NULL);
829 WCHAR buff[256];
831 if (!hMenu) return FALSE;
833 /* FIXME: error handling & memory cleanup */
834 hSubMenu = CreateMenu();
835 if (!hSubMenu) return FALSE;
837 LoadStringW(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(buff[0]));
838 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
839 LoadStringW(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(buff[0]));
840 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
841 LoadStringW(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(buff[0]));
842 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
843 LoadStringW(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(buff[0]));
844 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
845 LoadStringW(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(buff[0]));
846 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
847 LoadStringW(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(buff[0]));
848 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
850 if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
851 LoadStringW(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(buff[0]));
852 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
853 LoadStringW(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(buff[0]));
854 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
855 LoadStringW(hInstance, IDS_PROPERTIES, buff, sizeof(buff) / sizeof(buff[0]));
856 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
858 return TRUE;
861 /******************************************************************
862 * WCUSER_SetMenuDetails
864 * Grays / ungrays the menu items according to their state
866 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
868 if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
870 EnableMenuItem(hMenu, IDS_COPY,
871 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
872 EnableMenuItem(hMenu, IDS_PASTE,
873 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
874 ? MF_ENABLED : MF_GRAYED));
875 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
876 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
879 /******************************************************************
880 * WCUSER_Create
882 * Creates the window for the rendering
884 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
886 struct inner_data* data;
887 HMENU hSysMenu;
889 data = lpcs->lpCreateParams;
890 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
891 data->hWnd = hWnd;
893 hSysMenu = GetSystemMenu(hWnd, FALSE);
894 if (!hSysMenu) return 0;
895 PRIVATE(data)->hPopMenu = CreatePopupMenu();
896 if (!PRIVATE(data)->hPopMenu) return 0;
898 WCUSER_FillMenu(hSysMenu, TRUE);
899 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
901 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
902 if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
904 data->curcfg.quick_edit = FALSE;
905 return 0;
908 /******************************************************************
909 * WCUSER_GetCtrlKeyState
911 * Get the console bit mask equivalent to the VK_ status in keyState
913 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
915 DWORD ret = 0;
917 GetKeyboardState(keyState);
918 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
919 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
920 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
921 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
922 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
923 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
924 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
925 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
927 return ret;
930 /******************************************************************
931 * WCUSER_HandleSelectionKey
933 * Handles keys while selecting an area
935 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
936 WPARAM wParam, LPARAM lParam)
938 BYTE keyState[256];
939 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
940 COORD c1, c2;
942 if (!down) return;
944 switch (state)
946 case 0:
947 switch (wParam)
949 case VK_RETURN:
950 PRIVATE(data)->has_selection = FALSE;
951 WCUSER_SetSelection(data, 0);
952 WCUSER_CopySelectionToClipboard(data);
953 return;
954 case VK_RIGHT:
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_LEFT:
961 c1 = PRIVATE(data)->selectPt1;
962 c2 = PRIVATE(data)->selectPt2;
963 c1.X--; c2.X--;
964 WCUSER_MoveSelection(data, c1, c2);
965 return;
966 case VK_UP:
967 c1 = PRIVATE(data)->selectPt1;
968 c2 = PRIVATE(data)->selectPt2;
969 c1.Y--; c2.Y--;
970 WCUSER_MoveSelection(data, c1, c2);
971 return;
972 case VK_DOWN:
973 c1 = PRIVATE(data)->selectPt1;
974 c2 = PRIVATE(data)->selectPt2;
975 c1.Y++; c2.Y++;
976 WCUSER_MoveSelection(data, c1, c2);
977 return;
979 break;
980 case SHIFT_PRESSED:
981 switch (wParam)
983 case VK_RIGHT:
984 c1 = PRIVATE(data)->selectPt1;
985 c2 = PRIVATE(data)->selectPt2;
986 c2.X++;
987 WCUSER_MoveSelection(data, c1, c2);
988 return;
989 case VK_LEFT:
990 c1 = PRIVATE(data)->selectPt1;
991 c2 = PRIVATE(data)->selectPt2;
992 c2.X--;
993 WCUSER_MoveSelection(data, c1, c2);
994 return;
995 case VK_UP:
996 c1 = PRIVATE(data)->selectPt1;
997 c2 = PRIVATE(data)->selectPt2;
998 c2.Y--;
999 WCUSER_MoveSelection(data, c1, c2);
1000 return;
1001 case VK_DOWN:
1002 c1 = PRIVATE(data)->selectPt1;
1003 c2 = PRIVATE(data)->selectPt2;
1004 c2.Y++;
1005 WCUSER_MoveSelection(data, c1, c2);
1006 return;
1008 break;
1011 if (wParam < VK_SPACE) /* Shift, Alt, Ctrl, Num Lock etc. */
1012 return;
1014 WCUSER_SetSelection(data, 0);
1015 PRIVATE(data)->has_selection = FALSE;
1018 /******************************************************************
1019 * WCUSER_GenerateKeyInputRecord
1021 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1023 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1024 WPARAM wParam, LPARAM lParam)
1026 INPUT_RECORD ir;
1027 DWORD n;
1028 WCHAR buf[2];
1029 static WCHAR last; /* keep last char seen as feed for key up message */
1030 BYTE keyState[256];
1032 ir.EventType = KEY_EVENT;
1033 ir.Event.KeyEvent.bKeyDown = down;
1034 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1035 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1037 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1039 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1040 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1041 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1043 if (down)
1045 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1047 case 2:
1048 /* FIXME... should generate two events... */
1049 /* fall through */
1050 case 1:
1051 last = buf[0];
1052 break;
1053 default:
1054 last = 0;
1055 break;
1058 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME: HACKY... and buggy because it should be a stack, not a single value */
1059 if (!down) last = 0;
1061 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1064 /******************************************************************
1065 * WCUSER_GenerateMouseInputRecord
1069 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1070 WPARAM wParam, DWORD event)
1072 INPUT_RECORD ir;
1073 BYTE keyState[256];
1074 DWORD mode, n;
1076 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1077 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1078 return;
1080 ir.EventType = MOUSE_EVENT;
1081 ir.Event.MouseEvent.dwMousePosition = c;
1082 ir.Event.MouseEvent.dwButtonState = 0;
1083 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1084 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1085 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1086 if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
1087 if (wParam & MK_SHIFT) ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
1088 if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1089 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1090 ir.Event.MouseEvent.dwEventFlags = event;
1092 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1095 /******************************************************************
1096 * WCUSER_Proc
1100 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1102 struct inner_data* data = (struct inner_data*)GetWindowLongPtrW(hWnd, 0);
1104 switch (uMsg)
1106 case WM_CREATE:
1107 return WCUSER_Create(hWnd, (LPCREATESTRUCTW)lParam);
1108 case WM_DESTROY:
1109 data->hWnd = 0;
1110 PostQuitMessage(0);
1111 break;
1112 case WM_PAINT:
1113 WCUSER_Paint(data);
1114 break;
1115 case WM_KEYDOWN:
1116 case WM_KEYUP:
1117 if (PRIVATE(data)->has_selection)
1118 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1119 else
1120 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam);
1121 break;
1122 case WM_SYSKEYDOWN:
1123 case WM_SYSKEYUP:
1124 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam);
1125 break;
1126 case WM_LBUTTONDOWN:
1127 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1129 if (PRIVATE(data)->has_selection)
1130 WCUSER_SetSelection(data, 0);
1132 if (data->curcfg.quick_edit && PRIVATE(data)->has_selection)
1134 PRIVATE(data)->has_selection = FALSE;
1136 else
1138 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1139 SetCapture(data->hWnd);
1140 WCUSER_SetSelection(data, 0);
1141 PRIVATE(data)->has_selection = TRUE;
1144 else
1146 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1148 break;
1149 case WM_MOUSEMOVE:
1150 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1152 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection &&
1153 (wParam & MK_LBUTTON))
1155 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1158 else
1160 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1162 break;
1163 case WM_LBUTTONUP:
1164 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1166 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection)
1168 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1169 ReleaseCapture();
1172 else
1174 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1176 break;
1177 case WM_RBUTTONDOWN:
1178 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1180 POINT pt;
1182 pt.x = (short)LOWORD(lParam);
1183 pt.y = (short)HIWORD(lParam);
1184 ClientToScreen(hWnd, &pt);
1185 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1186 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON,
1187 pt.x, pt.y, 0, hWnd, NULL);
1189 else
1191 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1193 break;
1194 case WM_RBUTTONUP:
1195 /* no need to track for rbutton up when opening the popup... the event will be
1196 * swallowed by TrackPopupMenu */
1197 case WM_MBUTTONDOWN:
1198 case WM_MBUTTONUP:
1199 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1200 break;
1201 case WM_LBUTTONDBLCLK:
1202 case WM_MBUTTONDBLCLK:
1203 case WM_RBUTTONDBLCLK:
1204 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
1205 break;
1206 case WM_SETFOCUS:
1207 if (data->curcfg.cursor_visible)
1209 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
1210 data->curcfg.cell_width, data->curcfg.cell_height);
1211 WCUSER_PosCursor(data);
1213 break;
1214 case WM_KILLFOCUS:
1215 if (data->curcfg.cursor_visible)
1216 DestroyCaret();
1217 break;
1218 case WM_HSCROLL:
1220 struct config_data cfg = data->curcfg;
1222 switch (LOWORD(wParam))
1224 case SB_PAGEUP: cfg.win_pos.X -= 8; break;
1225 case SB_PAGEDOWN: cfg.win_pos.X += 8; break;
1226 case SB_LINEUP: cfg.win_pos.X--; break;
1227 case SB_LINEDOWN: cfg.win_pos.X++; break;
1228 case SB_THUMBTRACK: cfg.win_pos.X = HIWORD(wParam); break;
1229 default: break;
1231 if (cfg.win_pos.X < 0) cfg.win_pos.X = 0;
1232 if (cfg.win_pos.X > data->curcfg.sb_width - data->curcfg.win_width)
1233 cfg.win_pos.X = data->curcfg.sb_width - data->curcfg.win_width;
1234 if (cfg.win_pos.X != data->curcfg.win_pos.X)
1236 WINECON_SetConfig(data, &cfg);
1239 break;
1240 case WM_MOUSEWHEEL:
1241 if (data->curcfg.sb_height <= data->curcfg.win_height)
1243 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1244 break;
1246 /* else fallthrough */
1247 case WM_VSCROLL:
1249 struct config_data cfg = data->curcfg;
1251 if (uMsg == WM_MOUSEWHEEL)
1253 UINT scrollLines = 3;
1254 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
1255 scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1256 cfg.win_pos.Y += scrollLines;
1257 } else {
1258 switch (LOWORD(wParam))
1260 case SB_PAGEUP: cfg.win_pos.Y -= 8; break;
1261 case SB_PAGEDOWN: cfg.win_pos.Y += 8; break;
1262 case SB_LINEUP: cfg.win_pos.Y--; break;
1263 case SB_LINEDOWN: cfg.win_pos.Y++; break;
1264 case SB_THUMBTRACK: cfg.win_pos.Y = HIWORD(wParam); break;
1265 default: break;
1269 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
1270 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
1271 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
1272 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
1274 WINECON_SetConfig(data, &cfg);
1277 break;
1278 case WM_SYSCOMMAND:
1279 switch (wParam)
1281 case IDS_DEFAULT:
1282 WCUSER_GetProperties(data, FALSE);
1283 break;
1284 case IDS_PROPERTIES:
1285 WCUSER_GetProperties(data, TRUE);
1286 break;
1287 default:
1288 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1290 break;
1291 case WM_COMMAND:
1292 switch (wParam)
1294 case IDS_DEFAULT:
1295 WCUSER_GetProperties(data, FALSE);
1296 break;
1297 case IDS_PROPERTIES:
1298 WCUSER_GetProperties(data, TRUE);
1299 break;
1300 case IDS_MARK:
1301 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1302 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1303 WCUSER_SetSelection(data, 0);
1304 PRIVATE(data)->has_selection = TRUE;
1305 break;
1306 case IDS_COPY:
1307 if (PRIVATE(data)->has_selection)
1309 PRIVATE(data)->has_selection = FALSE;
1310 WCUSER_SetSelection(data, 0);
1311 WCUSER_CopySelectionToClipboard(data);
1313 break;
1314 case IDS_PASTE:
1315 WCUSER_PasteFromClipboard(data);
1316 break;
1317 case IDS_SELECTALL:
1318 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1319 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1320 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1321 WCUSER_SetSelection(data, 0);
1322 PRIVATE(data)->has_selection = TRUE;
1323 break;
1324 case IDS_SCROLL:
1325 case IDS_SEARCH:
1326 WINE_FIXME("Unhandled yet command: %lx\n", wParam);
1327 break;
1328 default:
1329 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1331 break;
1332 case WM_INITMENUPOPUP:
1333 if (!HIWORD(lParam)) return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1334 WCUSER_SetMenuDetails(data, GetSystemMenu(data->hWnd, FALSE));
1335 break;
1336 case WM_SIZE:
1337 WINECON_ResizeWithContainer(data, LOWORD(lParam) / data->curcfg.cell_width,
1338 HIWORD(lParam) / data->curcfg.cell_height);
1339 break;
1340 default:
1341 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1343 return 0;
1346 /******************************************************************
1347 * WCUSER_DeleteBackend
1351 static void WCUSER_DeleteBackend(struct inner_data* data)
1353 if (!PRIVATE(data)) return;
1354 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1355 if (data->hWnd) DestroyWindow(data->hWnd);
1356 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1357 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1358 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1359 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1362 /******************************************************************
1363 * WCUSER_MainLoop
1367 static int WCUSER_MainLoop(struct inner_data* data)
1369 MSG msg;
1371 ShowWindow(data->hWnd, data->nCmdShow);
1372 while (!data->dying || !data->curcfg.exit_on_die)
1374 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1376 case WAIT_OBJECT_0:
1377 WINECON_GrabChanges(data);
1378 break;
1379 case WAIT_OBJECT_0+1:
1380 /* need to use PeekMessageW loop instead of simple GetMessage:
1381 * multiple messages might have arrived in between,
1382 * so GetMessage would lead to delayed processing */
1383 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
1385 if (msg.message == WM_QUIT) return 0;
1386 WINE_TRACE("dispatching msg %04x\n", msg.message);
1387 DispatchMessageW(&msg);
1389 break;
1390 default:
1391 WINE_ERR("got pb\n");
1392 /* err */
1393 break;
1396 PostQuitMessage(0);
1397 return 0;
1400 /******************************************************************
1401 * WCUSER_InitBackend
1403 * Initialisation part II: creation of window.
1406 enum init_return WCUSER_InitBackend(struct inner_data* data)
1408 static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1410 WNDCLASSW wndclass;
1411 CHARSETINFO ci;
1413 if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
1414 return init_failed;
1415 g_uiDefaultCharset = ci.ciCharset;
1416 WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1418 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1419 if (!data->private) return init_failed;
1421 data->fnMainLoop = WCUSER_MainLoop;
1422 data->fnPosCursor = WCUSER_PosCursor;
1423 data->fnShapeCursor = WCUSER_ShapeCursor;
1424 data->fnComputePositions = WCUSER_ComputePositions;
1425 data->fnRefresh = WCUSER_Refresh;
1426 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1427 data->fnSetTitle = WCUSER_SetTitle;
1428 data->fnSetFont = WCUSER_SetFontPmt;
1429 data->fnScroll = WCUSER_Scroll;
1430 data->fnDeleteBackend = WCUSER_DeleteBackend;
1432 wndclass.style = CS_DBLCLKS;
1433 wndclass.lpfnWndProc = WCUSER_Proc;
1434 wndclass.cbClsExtra = 0;
1435 wndclass.cbWndExtra = sizeof(DWORD_PTR);
1436 wndclass.hInstance = GetModuleHandleW(NULL);
1437 wndclass.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
1438 wndclass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1439 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1440 wndclass.lpszMenuName = NULL;
1441 wndclass.lpszClassName = wClassName;
1443 RegisterClassW(&wndclass);
1445 data->hWnd = CreateWindowW(wndclass.lpszClassName, NULL,
1446 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1447 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1448 if (!data->hWnd) return init_not_supported;
1450 return init_success;