msi: Select the first item in the SelectionTree control.
[wine.git] / programs / wineconsole / user.c
blobfffd40fc8a43ec663d68631d37da5d768cb9a917
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 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(0x80, 0x80, 0x80),
38 RGB(0xC0, 0xC0, 0xC0), 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 LOGFONT* 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 TextOut(PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
84 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(PRIVATE(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(PRIVATE(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 (PRIVATE(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(PRIVATE(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 && PRIVATE(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) ? TRUE : FALSE;
195 if (force || vis != data->curcfg.cursor_visible)
197 data->curcfg.cursor_visible = vis;
198 if (PRIVATE(data)->hWnd == GetFocus())
200 if (vis)
202 CreateCaret(PRIVATE(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, GetWindowLong(PRIVATE(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(PRIVATE(data)->hWnd, SB_HORZ, 0,
239 data->curcfg.sb_width - data->curcfg.win_width, FALSE);
240 SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
241 ShowScrollBar(PRIVATE(data)->hWnd, SB_HORZ, TRUE);
243 else
245 ShowScrollBar(PRIVATE(data)->hWnd, SB_HORZ, FALSE);
248 if (data->curcfg.sb_height > data->curcfg.win_height)
250 dx = GetSystemMetrics(SM_CXVSCROLL);
251 SetScrollRange(PRIVATE(data)->hWnd, SB_VERT, 0,
252 data->curcfg.sb_height - data->curcfg.win_height, FALSE);
253 SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, 0, FALSE); /* FIXME */
254 ShowScrollBar(PRIVATE(data)->hWnd, SB_VERT, TRUE);
256 else
258 ShowScrollBar(PRIVATE(data)->hWnd, SB_VERT, FALSE);
261 SetWindowPos(PRIVATE(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 SetWindowText(PRIVATE(data)->hWnd, buffer);
280 void WCUSER_DumpLogFont(const char* pfx, const LOGFONT* 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 TEXTMETRIC* 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 BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONT* lf)
323 return lf->lfHeight == config->cell_height &&
324 lf->lfWeight == config->font_weight &&
325 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
326 !lstrcmp(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 TEXTMETRIC* 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 LOGFONT* lf)
358 return (lf->lfPitchAndFamily & 3) == FIXED_PITCH &&
359 /* (lf->lfPitchAndFamily & 0xF0) == FF_MODERN && */
360 (lf->lfCharSet == DEFAULT_CHARSET || lf->lfCharSet == g_uiDefaultCharset);
363 /******************************************************************
364 * get_first_font_enum_2
365 * get_first_font_enum
367 * Helper functions to get a decent font for the renderer
369 static int CALLBACK get_first_font_enum_2(const LOGFONT* lf, const TEXTMETRIC* tm,
370 DWORD FontType, LPARAM lParam)
372 struct font_chooser* fc = (struct font_chooser*)lParam;
374 WCUSER_DumpTextMetric(tm, FontType);
375 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType))
377 LOGFONT mlf = *lf;
379 /* Use the default sizes for the font (this is needed, especially for
380 * TrueType fonts, so that we get a decent size, not the max size)
382 mlf.lfWidth = fc->data->curcfg.cell_width;
383 mlf.lfHeight = fc->data->curcfg.cell_height;
384 if (WCUSER_SetFont(fc->data, &mlf))
386 struct config_data defcfg;
388 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
389 fc->done = 1;
390 /* since we've modified the current config with new font information,
391 * set this information as the new default.
393 WINECON_RegLoad(NULL, &defcfg);
394 defcfg.cell_width = fc->data->curcfg.cell_width;
395 defcfg.cell_height = fc->data->curcfg.cell_height;
396 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
397 /* Force also its writing back to the registry so that we can get it
398 * the next time.
400 WINECON_RegSave(&defcfg);
401 return 0;
404 return 1;
407 static int CALLBACK get_first_font_enum(const LOGFONT* lf, const TEXTMETRIC* tm,
408 DWORD FontType, LPARAM lParam)
410 struct font_chooser* fc = (struct font_chooser*)lParam;
412 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
413 if (WCUSER_ValidateFont(fc->data, lf))
415 EnumFontFamilies(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
416 get_first_font_enum_2, lParam);
417 return !fc->done; /* we just need the first matching one... */
419 return 1;
422 /******************************************************************
423 * WCUSER_CopyFont
425 * get the relevant information from the font described in lf and store them
426 * in config
428 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONT* lf, LONG* el)
430 TEXTMETRIC tm;
431 HDC hDC;
432 HFONT hFont, hOldFont;
433 int w, i, buf[256];
435 if (!(hDC = GetDC(hWnd))) return NULL;
436 if (!(hFont = CreateFontIndirect(lf))) goto err1;
438 hOldFont = SelectObject(hDC, hFont);
439 GetTextMetrics(hDC, &tm);
441 /* FIXME:
442 * the current freetype engine (at least 2.0.x with x <= 8) and its implementation
443 * in Wine don't return adequate values for fixed fonts
444 * In Windows, those fonts are expected to return the same value for
445 * - the average width
446 * - the largest width
447 * - the width of all characters in the font
448 * This isn't true in Wine. As a temporary workaround, we get as the width of the
449 * cell, the width of the first character in the font, after checking that all
450 * characters in the font have the same width (I hear paranoïa coming)
451 * when this gets fixed, the code should be using tm.tmAveCharWidth
452 * or tm.tmMaxCharWidth as the cell width.
454 GetCharWidth32(hDC, tm.tmFirstChar, tm.tmFirstChar, &w);
455 for (i = tm.tmFirstChar + 1; i <= tm.tmLastChar; i += sizeof(buf) / sizeof(buf[0]))
457 int j, k;
459 k = min(tm.tmLastChar - i, sizeof(buf) / sizeof(buf[0]) - 1);
460 GetCharWidth32(hDC, i, i + k, buf);
461 for (j = 0; j <= k; j++)
463 if (buf[j] != w)
465 WINE_WARN("Non uniform cell width: [%d]=%d [%d]=%d\n"
466 "This may be caused by old freetype libraries, >= 2.0.8 is recommended\n",
467 i + j, buf[j], tm.tmFirstChar, w);
468 goto err;
472 SelectObject(hDC, hOldFont);
473 ReleaseDC(hWnd, hDC);
475 config->cell_width = w;
476 config->cell_height = tm.tmHeight + tm.tmExternalLeading;
477 config->font_weight = tm.tmWeight;
478 lstrcpy(config->face_name, lf->lfFaceName);
479 if (el) *el = tm.tmExternalLeading;
481 return hFont;
482 err:
483 if (hDC && hOldFont) SelectObject(hDC, hOldFont);
484 if (hFont) DeleteObject(hFont);
485 err1:
486 if (hDC) ReleaseDC(hWnd, hDC);
488 return NULL;
491 /******************************************************************
492 * WCUSER_FillLogFont
496 void WCUSER_FillLogFont(LOGFONT* lf, const WCHAR* name, UINT height, UINT weight)
498 lf->lfHeight = height;
499 lf->lfWidth = 0;
500 lf->lfEscapement = 0;
501 lf->lfOrientation = 0;
502 lf->lfWeight = weight;
503 lf->lfItalic = FALSE;
504 lf->lfUnderline = FALSE;
505 lf->lfStrikeOut = FALSE;
506 lf->lfCharSet = DEFAULT_CHARSET;
507 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
508 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
509 lf->lfQuality = DEFAULT_QUALITY;
510 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
511 lstrcpy(lf->lfFaceName, name);
514 /******************************************************************
515 * WCUSER_SetFont
517 * sets logfont as the new font for the console
519 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONT* logfont)
521 HFONT hFont;
522 LONG el;
524 if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
525 return TRUE;
527 hFont = WCUSER_CopyFont(&data->curcfg, PRIVATE(data)->hWnd, logfont, &el);
528 if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
530 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
531 PRIVATE(data)->hFont = hFont;
532 PRIVATE(data)->ext_leading = el;
534 WCUSER_ComputePositions(data);
535 WCUSER_NewBitmap(data);
536 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
537 UpdateWindow(PRIVATE(data)->hWnd);
539 return TRUE;
542 /******************************************************************
543 * WCUSER_SetFontPmt
545 * Sets a new font for the console.
546 * In fact a wrapper for WCUSER_SetFont
548 static void WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
549 unsigned height, unsigned weight)
551 LOGFONT lf;
552 struct font_chooser fc;
554 WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
555 wine_dbgstr_wn(font, -1), height, weight);
557 if (font[0] != '\0' && height != 0 && weight != 0)
559 WCUSER_FillLogFont(&lf, font, height, weight);
560 if (WCUSER_SetFont(data, &lf))
562 WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
563 return;
567 /* try to find an acceptable font */
568 WINE_WARN("Couldn't match the font from registry... trying to find one\n");
569 fc.data = data;
570 fc.done = 0;
571 EnumFontFamilies(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
572 if (!fc.done) WINECON_Fatal("Couldn't find a decent font, aborting\n");
575 /******************************************************************
576 * WCUSER_GetCell
578 * Get a cell from a relative coordinate in window (takes into
579 * account the scrolling)
581 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
583 COORD c;
585 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
586 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
588 return c;
591 /******************************************************************
592 * WCUSER_GetSelectionRect
594 * Get the selection rectangle
596 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
598 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) - data->curcfg.win_pos.X) * data->curcfg.cell_width;
599 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
600 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
601 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
604 /******************************************************************
605 * WCUSER_SetSelection
609 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
611 HDC hDC;
612 RECT r;
614 WCUSER_GetSelectionRect(data, &r);
615 hDC = hRefDC ? hRefDC : GetDC(PRIVATE(data)->hWnd);
616 if (hDC)
618 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
619 HideCaret(PRIVATE(data)->hWnd);
620 InvertRect(hDC, &r);
621 if (hDC != hRefDC)
622 ReleaseDC(PRIVATE(data)->hWnd, hDC);
623 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
624 ShowCaret(PRIVATE(data)->hWnd);
628 /******************************************************************
629 * WCUSER_MoveSelection
633 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
635 RECT r;
636 HDC hDC;
638 if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
639 c2.X < 0 || c2.X >= data->curcfg.sb_width ||
640 c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
641 c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
642 return;
644 WCUSER_GetSelectionRect(data, &r);
645 hDC = GetDC(PRIVATE(data)->hWnd);
646 if (hDC)
648 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
649 HideCaret(PRIVATE(data)->hWnd);
650 InvertRect(hDC, &r);
652 PRIVATE(data)->selectPt1 = c1;
653 PRIVATE(data)->selectPt2 = c2;
654 if (hDC)
656 WCUSER_GetSelectionRect(data, &r);
657 InvertRect(hDC, &r);
658 ReleaseDC(PRIVATE(data)->hWnd, hDC);
659 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
660 ShowCaret(PRIVATE(data)->hWnd);
664 /******************************************************************
665 * WCUSER_CopySelectionToClipboard
667 * Copies the current selection into the clipboard
669 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
671 HANDLE hMem;
672 LPWSTR p;
673 unsigned w, h;
675 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
676 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
678 if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
679 EmptyClipboard();
681 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
682 if (hMem && (p = GlobalLock(hMem)))
684 COORD c;
685 int y;
687 c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
688 c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
690 for (y = 0; y < h; y++, c.Y++)
692 ReadConsoleOutputCharacter(data->hConOut, &p[y * w], w - 1, c, NULL);
693 p[y * w + w - 1] = (y < h - 1) ? '\n' : '\0';
695 GlobalUnlock(hMem);
696 SetClipboardData(CF_UNICODETEXT, hMem);
698 CloseClipboard();
701 /******************************************************************
702 * WCUSER_PasteFromClipboard
706 static void WCUSER_PasteFromClipboard(struct inner_data* data)
708 HANDLE h;
709 WCHAR* ptr;
711 if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
712 h = GetClipboardData(CF_UNICODETEXT);
713 if (h && (ptr = GlobalLock(h)))
715 int i, len = GlobalSize(h) / sizeof(WCHAR);
716 INPUT_RECORD ir[2];
717 DWORD n;
718 SHORT sh;
720 ir[0].EventType = KEY_EVENT;
721 ir[0].Event.KeyEvent.wRepeatCount = 0;
722 ir[0].Event.KeyEvent.dwControlKeyState = 0;
723 ir[0].Event.KeyEvent.bKeyDown = TRUE;
725 /* generate the corresponding input records */
726 for (i = 0; i < len; i++)
728 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
729 sh = VkKeyScan(ptr[i]);
730 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
731 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(LOBYTE(sh), 0);
732 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
734 ir[1] = ir[0];
735 ir[1].Event.KeyEvent.bKeyDown = FALSE;
737 WriteConsoleInput(data->hConIn, ir, 2, &n);
739 GlobalUnlock(h);
741 CloseClipboard();
744 /******************************************************************
745 * WCUSER_Refresh
749 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
751 WCUSER_FillMemDC(data, tp, bm);
752 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
754 RECT r;
756 r.left = 0;
757 r.right = data->curcfg.win_width * data->curcfg.cell_width;
758 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
759 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
760 InvalidateRect(PRIVATE(data)->hWnd, &r, FALSE);
761 UpdateWindow(PRIVATE(data)->hWnd);
765 /******************************************************************
766 * WCUSER_Paint
770 static void WCUSER_Paint(const struct inner_data* data)
772 PAINTSTRUCT ps;
774 BeginPaint(PRIVATE(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(PRIVATE(data)->hWnd, &ps);
787 /******************************************************************
788 * WCUSER_Scroll
792 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
794 if (horz)
796 SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, pos, TRUE);
797 data->curcfg.win_pos.X = pos;
799 else
801 SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, pos, TRUE);
802 data->curcfg.win_pos.Y = pos;
804 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
807 /******************************************************************
808 * WCUSER_FillMenu
812 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
814 HMENU hSubMenu;
815 HINSTANCE hInstance = GetModuleHandle(NULL);
816 WCHAR buff[256];
818 if (!hMenu) return FALSE;
820 /* FIXME: error handling & memory cleanup */
821 hSubMenu = CreateMenu();
822 if (!hSubMenu) return FALSE;
824 LoadString(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(WCHAR));
825 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
826 LoadString(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(WCHAR));
827 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
828 LoadString(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(WCHAR));
829 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
830 LoadString(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(WCHAR));
831 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
832 LoadString(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(WCHAR));
833 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
834 LoadString(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(WCHAR));
835 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
837 if (sep) InsertMenu(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
838 LoadString(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(WCHAR));
839 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
840 LoadString(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(WCHAR));
841 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
842 LoadString(hInstance, IDS_PROPERTIES, buff, sizeof(buff) / sizeof(WCHAR));
843 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
845 return TRUE;
848 /******************************************************************
849 * WCUSER_SetMenuDetails
851 * Grays / ungrays the menu items according to their state
853 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
855 if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
857 EnableMenuItem(hMenu, IDS_COPY,
858 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
859 EnableMenuItem(hMenu, IDS_PASTE,
860 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
861 ? MF_ENABLED : MF_GRAYED));
862 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
863 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
866 /******************************************************************
867 * WCUSER_Create
869 * Creates the window for the rendering
871 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCT lpcs)
873 struct inner_data* data;
874 HMENU hSysMenu;
876 data = lpcs->lpCreateParams;
877 SetWindowLongPtr(hWnd, 0L, (DWORD_PTR)data);
878 PRIVATE(data)->hWnd = hWnd;
880 hSysMenu = GetSystemMenu(hWnd, FALSE);
881 if (!hSysMenu) return 0;
882 PRIVATE(data)->hPopMenu = CreatePopupMenu();
883 if (!PRIVATE(data)->hPopMenu) return 0;
885 WCUSER_FillMenu(hSysMenu, TRUE);
886 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
888 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
889 if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
891 data->curcfg.quick_edit = FALSE;
892 return 0;
895 /******************************************************************
896 * WCUSER_GetCtrlKeyState
898 * Get the console bit mask equivalent to the VK_ status in keyState
900 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
902 DWORD ret = 0;
904 GetKeyboardState(keyState);
905 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
906 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
907 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
908 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
909 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
910 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
911 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
912 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
914 return ret;
917 /******************************************************************
918 * WCUSER_HandleSelectionKey
920 * Handles keys while selecting an area
922 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
923 WPARAM wParam, LPARAM lParam)
925 BYTE keyState[256];
926 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
927 COORD c1, c2;
929 if (down) return;
931 switch (state)
933 case 0:
934 switch (wParam)
936 case VK_RETURN:
937 PRIVATE(data)->has_selection = FALSE;
938 WCUSER_SetSelection(data, 0);
939 WCUSER_CopySelectionToClipboard(data);
940 break;
941 case VK_RIGHT:
942 c1 = PRIVATE(data)->selectPt1;
943 c2 = PRIVATE(data)->selectPt2;
944 c1.X++; c2.X++;
945 WCUSER_MoveSelection(data, c1, c2);
946 break;
947 case VK_LEFT:
948 c1 = PRIVATE(data)->selectPt1;
949 c2 = PRIVATE(data)->selectPt2;
950 c1.X--; c2.X--;
951 WCUSER_MoveSelection(data, c1, c2);
952 break;
953 case VK_UP:
954 c1 = PRIVATE(data)->selectPt1;
955 c2 = PRIVATE(data)->selectPt2;
956 c1.Y--; c2.Y--;
957 WCUSER_MoveSelection(data, c1, c2);
958 break;
959 case VK_DOWN:
960 c1 = PRIVATE(data)->selectPt1;
961 c2 = PRIVATE(data)->selectPt2;
962 c1.Y++; c2.Y++;
963 WCUSER_MoveSelection(data, c1, c2);
964 break;
966 break;
967 case SHIFT_PRESSED:
968 switch (wParam)
970 case VK_RIGHT:
971 c1 = PRIVATE(data)->selectPt1;
972 c2 = PRIVATE(data)->selectPt2;
973 c2.X++;
974 WCUSER_MoveSelection(data, c1, c2);
975 break;
976 case VK_LEFT:
977 c1 = PRIVATE(data)->selectPt1;
978 c2 = PRIVATE(data)->selectPt2;
979 c2.X--;
980 WCUSER_MoveSelection(data, c1, c2);
981 break;
982 case VK_UP:
983 c1 = PRIVATE(data)->selectPt1;
984 c2 = PRIVATE(data)->selectPt2;
985 c2.Y--;
986 WCUSER_MoveSelection(data, c1, c2);
987 break;
988 case VK_DOWN:
989 c1 = PRIVATE(data)->selectPt1;
990 c2 = PRIVATE(data)->selectPt2;
991 c2.Y++;
992 WCUSER_MoveSelection(data, c1, c2);
993 break;
995 break;
999 /******************************************************************
1000 * WCUSER_GenerateKeyInputRecord
1002 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1004 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1005 WPARAM wParam, LPARAM lParam, BOOL sys)
1007 INPUT_RECORD ir;
1008 DWORD n;
1009 WCHAR buf[2];
1010 static WCHAR last; /* keep last char seen as feed for key up message */
1011 BYTE keyState[256];
1013 ir.EventType = KEY_EVENT;
1014 ir.Event.KeyEvent.bKeyDown = down;
1015 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1016 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1018 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1020 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1021 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1022 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1023 if (sys) ir.Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED; /* FIXME: gotta choose one */
1025 if (!(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1027 if (down)
1029 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1031 case 2:
1032 /* FIXME... should generate two events... */
1033 /* fall thru */
1034 case 1:
1035 last = buf[0];
1036 break;
1037 default:
1038 last = 0;
1039 break;
1042 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME HACKY... and buggy 'coz it should be a stack, not a single value */
1043 if (!down) last = 0;
1046 WriteConsoleInput(data->hConIn, &ir, 1, &n);
1049 /******************************************************************
1050 * WCUSER_GenerateMouseInputRecord
1054 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1055 WPARAM wParam, DWORD event)
1057 INPUT_RECORD ir;
1058 BYTE keyState[256];
1059 DWORD mode, n;
1061 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1062 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1063 return;
1065 ir.EventType = MOUSE_EVENT;
1066 ir.Event.MouseEvent.dwMousePosition = c;
1067 ir.Event.MouseEvent.dwButtonState = 0;
1068 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1069 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1070 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1071 if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
1072 if (wParam & MK_SHIFT) ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
1073 if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1074 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1075 ir.Event.MouseEvent.dwEventFlags = event;
1077 WriteConsoleInput(data->hConIn, &ir, 1, &n);
1080 /******************************************************************
1081 * WCUSER_Proc
1085 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1087 struct inner_data* data = (struct inner_data*)GetWindowLongPtr(hWnd, 0);
1089 switch (uMsg)
1091 case WM_CREATE:
1092 return WCUSER_Create(hWnd, (LPCREATESTRUCT)lParam);
1093 case WM_DESTROY:
1094 PRIVATE(data)->hWnd = 0;
1095 PostQuitMessage(0);
1096 break;
1097 case WM_PAINT:
1098 WCUSER_Paint(data);
1099 break;
1100 case WM_KEYDOWN:
1101 case WM_KEYUP:
1102 if (PRIVATE(data)->has_selection)
1103 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1104 else
1105 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam, FALSE);
1106 break;
1107 case WM_SYSKEYDOWN:
1108 case WM_SYSKEYUP:
1109 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam, TRUE);
1110 break;
1111 case WM_LBUTTONDOWN:
1112 if (data->curcfg.quick_edit)
1114 if (PRIVATE(data)->has_selection)
1116 PRIVATE(data)->has_selection = FALSE;
1117 WCUSER_SetSelection(data, 0);
1119 else
1121 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1122 SetCapture(PRIVATE(data)->hWnd);
1123 WCUSER_SetSelection(data, 0);
1124 PRIVATE(data)->has_selection = TRUE;
1127 else
1129 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1131 break;
1132 case WM_MOUSEMOVE:
1133 if (data->curcfg.quick_edit)
1135 if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
1136 (wParam & MK_LBUTTON))
1138 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1141 else
1143 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1145 break;
1146 case WM_LBUTTONUP:
1147 if (data->curcfg.quick_edit)
1149 if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
1150 (wParam& MK_LBUTTON))
1152 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1153 ReleaseCapture();
1154 PRIVATE(data)->has_selection = FALSE;
1157 else
1159 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1161 break;
1162 case WM_RBUTTONDOWN:
1163 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1165 RECT r;
1167 GetWindowRect(hWnd, &r);
1168 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1169 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN,
1170 r.left + LOWORD(lParam), r.top + HIWORD(lParam), 0, hWnd, NULL);
1172 else
1174 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1176 break;
1177 case WM_RBUTTONUP:
1178 /* no need to track for rbutton up when opening the popup... the event will be
1179 * swallowed by TrackPopupMenu */
1180 case WM_MBUTTONDOWN:
1181 case WM_MBUTTONUP:
1182 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1183 break;
1184 case WM_LBUTTONDBLCLK:
1185 case WM_MBUTTONDBLCLK:
1186 case WM_RBUTTONDBLCLK:
1187 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
1188 break;
1189 case WM_MOUSEWHEEL:
1190 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1191 break;
1192 case WM_SETFOCUS:
1193 if (data->curcfg.cursor_visible)
1195 CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap,
1196 data->curcfg.cell_width, data->curcfg.cell_height);
1197 WCUSER_PosCursor(data);
1199 break;
1200 case WM_KILLFOCUS:
1201 if (data->curcfg.cursor_visible)
1202 DestroyCaret();
1203 break;
1204 case WM_HSCROLL:
1206 int pos = data->curcfg.win_pos.X;
1208 switch (LOWORD(wParam))
1210 case SB_PAGEUP: pos -= 8; break;
1211 case SB_PAGEDOWN: pos += 8; break;
1212 case SB_LINEUP: pos--; break;
1213 case SB_LINEDOWN: pos++; break;
1214 case SB_THUMBTRACK: pos = HIWORD(wParam); break;
1215 default: break;
1217 if (pos < 0) pos = 0;
1218 if (pos > data->curcfg.sb_width - data->curcfg.win_width)
1219 pos = data->curcfg.sb_width - data->curcfg.win_width;
1220 if (pos != data->curcfg.win_pos.X)
1222 ScrollWindow(hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0,
1223 NULL, NULL);
1224 data->curcfg.win_pos.X = pos;
1225 SetScrollPos(hWnd, SB_HORZ, pos, TRUE);
1226 UpdateWindow(hWnd);
1227 WCUSER_PosCursor(data);
1228 WINECON_NotifyWindowChange(data);
1231 break;
1232 case WM_VSCROLL:
1234 int pos = data->curcfg.win_pos.Y;
1236 switch (LOWORD(wParam))
1238 case SB_PAGEUP: pos -= 8; break;
1239 case SB_PAGEDOWN: pos += 8; break;
1240 case SB_LINEUP: pos--; break;
1241 case SB_LINEDOWN: pos++; break;
1242 case SB_THUMBTRACK: pos = HIWORD(wParam); break;
1243 default: break;
1245 if (pos < 0) pos = 0;
1246 if (pos > data->curcfg.sb_height - data->curcfg.win_height)
1247 pos = data->curcfg.sb_height - data->curcfg.win_height;
1248 if (pos != data->curcfg.win_pos.Y)
1250 ScrollWindow(hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height,
1251 NULL, NULL);
1252 data->curcfg.win_pos.Y = pos;
1253 SetScrollPos(hWnd, SB_VERT, pos, TRUE);
1254 UpdateWindow(hWnd);
1255 WCUSER_PosCursor(data);
1256 WINECON_NotifyWindowChange(data);
1260 case WM_SYSCOMMAND:
1261 switch (wParam)
1263 case IDS_DEFAULT:
1264 WCUSER_GetProperties(data, FALSE);
1265 break;
1266 case IDS_PROPERTIES:
1267 WCUSER_GetProperties(data, TRUE);
1268 break;
1269 default:
1270 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1272 break;
1273 case WM_COMMAND:
1274 switch (wParam)
1276 case IDS_DEFAULT:
1277 WCUSER_GetProperties(data, FALSE);
1278 break;
1279 case IDS_PROPERTIES:
1280 WCUSER_GetProperties(data, TRUE);
1281 break;
1282 case IDS_MARK:
1283 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1284 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1285 WCUSER_SetSelection(data, 0);
1286 PRIVATE(data)->has_selection = TRUE;
1287 break;
1288 case IDS_COPY:
1289 if (PRIVATE(data)->has_selection)
1291 PRIVATE(data)->has_selection = FALSE;
1292 WCUSER_SetSelection(data, 0);
1293 WCUSER_CopySelectionToClipboard(data);
1295 break;
1296 case IDS_PASTE:
1297 WCUSER_PasteFromClipboard(data);
1298 break;
1299 case IDS_SELECTALL:
1300 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1301 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1302 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1303 WCUSER_SetSelection(data, 0);
1304 PRIVATE(data)->has_selection = TRUE;
1305 break;
1306 case IDS_SCROLL:
1307 case IDS_SEARCH:
1308 WINE_FIXME("Unhandled yet command: %x\n", wParam);
1309 break;
1310 default:
1311 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1313 break;
1314 case WM_INITMENUPOPUP:
1315 if (!HIWORD(lParam)) return DefWindowProc(hWnd, uMsg, wParam, lParam);
1316 WCUSER_SetMenuDetails(data, GetSystemMenu(PRIVATE(data)->hWnd, FALSE));
1317 break;
1318 default:
1319 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1321 return 0;
1324 /******************************************************************
1325 * WCUSER_DeleteBackend
1329 static void WCUSER_DeleteBackend(struct inner_data* data)
1331 if (!PRIVATE(data)) return;
1332 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1333 if (PRIVATE(data)->hWnd) DestroyWindow(PRIVATE(data)->hWnd);
1334 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1335 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1336 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1337 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1340 /******************************************************************
1341 * WCUSER_MainLoop
1345 static int WCUSER_MainLoop(struct inner_data* data)
1347 MSG msg;
1349 ShowWindow(PRIVATE(data)->hWnd, data->nCmdShow);
1350 for (;;)
1352 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1354 case WAIT_OBJECT_0:
1355 if (!WINECON_GrabChanges(data) && data->curcfg.exit_on_die)
1356 PostQuitMessage(0);
1357 break;
1358 case WAIT_OBJECT_0+1:
1359 /* need to use PeekMessage loop instead of simple GetMessage:
1360 * multiple messages might have arrived in between,
1361 * so GetMessage would lead to delayed processing */
1362 while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
1364 if (msg.message == WM_QUIT) return 0;
1365 WINE_TRACE("dispatching msg %04x\n", msg.message);
1366 DispatchMessage(&msg);
1368 break;
1369 default:
1370 WINE_ERR("got pb\n");
1371 /* err */
1372 break;
1377 /******************************************************************
1378 * WCUSER_InitBackend
1380 * Initialisation part II: creation of window.
1383 enum init_return WCUSER_InitBackend(struct inner_data* data)
1385 static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1387 WNDCLASS wndclass;
1388 CHARSETINFO ci;
1390 if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
1391 return init_failed;
1392 g_uiDefaultCharset = ci.ciCharset;
1393 WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1395 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1396 if (!data->private) return init_failed;
1398 data->fnMainLoop = WCUSER_MainLoop;
1399 data->fnPosCursor = WCUSER_PosCursor;
1400 data->fnShapeCursor = WCUSER_ShapeCursor;
1401 data->fnComputePositions = WCUSER_ComputePositions;
1402 data->fnRefresh = WCUSER_Refresh;
1403 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1404 data->fnSetTitle = WCUSER_SetTitle;
1405 data->fnSetFont = WCUSER_SetFontPmt;
1406 data->fnScroll = WCUSER_Scroll;
1407 data->fnDeleteBackend = WCUSER_DeleteBackend;
1409 wndclass.style = CS_DBLCLKS;
1410 wndclass.lpfnWndProc = WCUSER_Proc;
1411 wndclass.cbClsExtra = 0;
1412 wndclass.cbWndExtra = sizeof(DWORD_PTR);
1413 wndclass.hInstance = GetModuleHandle(NULL);
1414 wndclass.hIcon = LoadIcon(0, IDI_WINLOGO);
1415 wndclass.hCursor = LoadCursor(0, IDC_ARROW);
1416 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1417 wndclass.lpszMenuName = NULL;
1418 wndclass.lpszClassName = wClassName;
1420 RegisterClass(&wndclass);
1422 CreateWindow(wndclass.lpszClassName, NULL,
1423 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1424 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1425 if (!PRIVATE(data)->hWnd) return init_failed;
1427 return init_success;