Release 2.3.
[wine.git] / programs / wineconsole / user.c
blob316c01114b1f9bbbba0eac1e39bc466e8ffc86f1
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))))
60 WINECON_Fatal("OOM\n");
61 dx = HeapAlloc( GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(*dx) );
63 hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
64 for (j = upd_tp; j <= upd_bm; j++)
66 cell = &data->cells[j * data->curcfg.sb_width];
67 for (i = 0; i < data->curcfg.sb_width; i++)
69 attr = cell[i].Attributes;
70 SetBkColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[(attr >> 4) & 0x0F]);
71 SetTextColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[attr & 0x0F]);
72 for (k = i; k < data->curcfg.sb_width && cell[k].Attributes == attr; k++)
74 line[k - i] = cell[k].Char.UnicodeChar;
75 dx[k - i] = data->curcfg.cell_width;
77 ExtTextOutW( PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
78 0, NULL, line, k - i, dx );
79 if (PRIVATE(data)->ext_leading &&
80 (hbr = CreateSolidBrush(data->curcfg.color_map[(attr >> 4) & 0x0F])))
82 r.left = i * data->curcfg.cell_width;
83 r.top = (j + 1) * data->curcfg.cell_height - PRIVATE(data)->ext_leading;
84 r.right = k * data->curcfg.cell_width;
85 r.bottom = (j + 1) * data->curcfg.cell_height;
86 FillRect(PRIVATE(data)->hMemDC, &r, hbr);
87 DeleteObject(hbr);
89 i = k - 1;
92 SelectObject(PRIVATE(data)->hMemDC, hOldFont);
93 HeapFree(GetProcessHeap(), 0, dx);
94 HeapFree(GetProcessHeap(), 0, line);
97 /******************************************************************
98 * WCUSER_NewBitmap
100 * Either the font geometry or the sb geometry has changed. we need
101 * to recreate the bitmap geometry.
103 static void WCUSER_NewBitmap(struct inner_data* data)
105 HDC hDC;
106 HBITMAP hnew, hold;
108 if (!data->curcfg.sb_width || !data->curcfg.sb_height ||
109 !PRIVATE(data)->hFont || !(hDC = GetDC(data->hWnd)))
110 return;
111 hnew = CreateCompatibleBitmap(hDC,
112 data->curcfg.sb_width * data->curcfg.cell_width,
113 data->curcfg.sb_height * data->curcfg.cell_height);
114 ReleaseDC(data->hWnd, hDC);
115 hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
117 if (PRIVATE(data)->hBitmap)
119 if (hold == PRIVATE(data)->hBitmap)
120 DeleteObject(PRIVATE(data)->hBitmap);
121 else
122 WINE_FIXME("leak\n");
124 PRIVATE(data)->hBitmap = hnew;
125 WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
128 /******************************************************************
129 * WCUSER_ResizeScreenBuffer
133 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
135 WCUSER_NewBitmap(data);
138 /******************************************************************
139 * WCUSER_PosCursor
141 * Set a new position for the cursor
143 static void WCUSER_PosCursor(const struct inner_data* data)
145 if (data->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
147 SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
148 (data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
149 ShowCaret(data->hWnd);
152 /******************************************************************
153 * WCUSER_ShapeCursor
155 * Sets a new shape for the cursor
157 static void WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
159 if (force || size != data->curcfg.cursor_size)
161 if (data->curcfg.cursor_visible && data->hWnd == GetFocus()) DestroyCaret();
162 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
163 PRIVATE(data)->cursor_bitmap = NULL;
164 if (size != 100)
166 int w16b; /* number of bytes per row, aligned on word size */
167 BYTE* ptr;
168 int i, j, nbl;
170 w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
171 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
172 if (!ptr) WINECON_Fatal("OOM");
173 nbl = max((data->curcfg.cell_height * size) / 100, 1);
174 for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
176 for (i = 0; i < data->curcfg.cell_width; i++)
178 ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
181 PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
182 data->curcfg.cell_height, 1, 1, ptr);
183 HeapFree(GetProcessHeap(), 0, ptr);
185 data->curcfg.cursor_size = size;
186 data->curcfg.cursor_visible = -1;
189 vis = vis != 0;
190 if (force || vis != data->curcfg.cursor_visible)
192 data->curcfg.cursor_visible = vis;
193 if (data->hWnd == GetFocus())
195 if (vis)
197 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
198 data->curcfg.cell_width, data->curcfg.cell_height);
199 WCUSER_PosCursor(data);
201 else
203 DestroyCaret();
207 WINECON_DumpConfig("crsr", &data->curcfg);
210 /******************************************************************
211 * WCUSER_ComputePositions
213 * Recomputes all the components (mainly scroll bars) positions
215 static void WCUSER_ComputePositions(struct inner_data* data)
217 RECT r;
218 int dx, dy;
220 /* compute window size from desired client size */
221 r.left = r.top = 0;
222 r.right = data->curcfg.win_width * data->curcfg.cell_width;
223 r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
225 if (IsRectEmpty(&r)) return;
227 AdjustWindowRect(&r, GetWindowLongW(data->hWnd, GWL_STYLE), FALSE);
229 dx = dy = 0;
230 if (data->curcfg.sb_width > data->curcfg.win_width)
232 dy = GetSystemMetrics(SM_CYHSCROLL);
233 SetScrollRange(data->hWnd, SB_HORZ, 0,
234 data->curcfg.sb_width - data->curcfg.win_width, FALSE);
235 SetScrollPos(data->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
236 ShowScrollBar(data->hWnd, SB_HORZ, TRUE);
238 else
240 ShowScrollBar(data->hWnd, SB_HORZ, FALSE);
243 if (data->curcfg.sb_height > data->curcfg.win_height)
245 dx = GetSystemMetrics(SM_CXVSCROLL);
246 SetScrollRange(data->hWnd, SB_VERT, 0,
247 data->curcfg.sb_height - data->curcfg.win_height, FALSE);
248 SetScrollPos(data->hWnd, SB_VERT, 0, FALSE); /* FIXME */
249 ShowScrollBar(data->hWnd, SB_VERT, TRUE);
251 else
253 ShowScrollBar(data->hWnd, SB_VERT, FALSE);
256 SetWindowPos(data->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
257 SWP_NOMOVE|SWP_NOZORDER);
258 WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
259 WCUSER_PosCursor(data);
262 /******************************************************************
263 * WCUSER_SetTitle
265 * Sets the title to the wine console
267 static void WCUSER_SetTitle(const struct inner_data* data)
269 WCHAR buffer[256];
271 if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
272 SetWindowTextW(data->hWnd, buffer);
275 void WCUSER_DumpLogFont(const char* pfx, const LOGFONTW* lf, DWORD ft)
277 WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
278 "\tlf.lfHeight=%d lf.lfWidth=%d lf.lfEscapement=%d lf.lfOrientation=%d\n"
279 "\tlf.lfWeight=%d lf.lfItalic=%u lf.lfUnderline=%u lf.lfStrikeOut=%u\n"
280 "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
281 "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
282 pfx,
283 (ft & RASTER_FONTTYPE) ? "raster" : "",
284 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
285 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
286 (ft & DEVICE_FONTTYPE) ? "|device" : "",
287 lf->lfHeight, lf->lfWidth, lf->lfEscapement, lf->lfOrientation,
288 lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
289 lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
290 wine_dbgstr_w(lf->lfFaceName));
293 void WCUSER_DumpTextMetric(const TEXTMETRICW* tm, DWORD ft)
295 WINE_TRACE_(wc_font)("%s%s%s%s\n"
296 "\ttmHeight=%d tmAscent=%d tmDescent=%d tmInternalLeading=%d tmExternalLeading=%d\n"
297 "\ttmAveCharWidth=%d tmMaxCharWidth=%d tmWeight=%d tmOverhang=%d\n"
298 "\ttmDigitizedAspectX=%d tmDigitizedAspectY=%d\n"
299 "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
300 "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
301 (ft & RASTER_FONTTYPE) ? "raster" : "",
302 (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
303 ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
304 (ft & DEVICE_FONTTYPE) ? "|device" : "",
305 tm->tmHeight, tm->tmAscent, tm->tmDescent, tm->tmInternalLeading, tm->tmExternalLeading, tm->tmAveCharWidth,
306 tm->tmMaxCharWidth, tm->tmWeight, tm->tmOverhang, tm->tmDigitizedAspectX, tm->tmDigitizedAspectY,
307 tm->tmFirstChar, tm->tmLastChar, tm->tmDefaultChar, tm->tmBreakChar, tm->tmItalic, tm->tmUnderlined, tm->tmStruckOut,
308 tm->tmPitchAndFamily, tm->tmCharSet);
311 /******************************************************************
312 * WCUSER_AreFontsEqual
316 static BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONTW* lf)
318 return lf->lfHeight == config->cell_height &&
319 lf->lfWeight == config->font_weight &&
320 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
321 !lstrcmpW(lf->lfFaceName, config->face_name);
324 struct font_chooser
326 struct inner_data* data;
327 int pass;
328 BOOL done;
331 /******************************************************************
332 * WCUSER_ValidateFontMetric
334 * Returns true if the font described in tm is usable as a font for the renderer
336 BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRICW* tm,
337 DWORD type, int pass)
339 switch (pass) /* we get increasingly lenient in later passes */
341 case 0:
342 if (type & RASTER_FONTTYPE)
344 if (tm->tmMaxCharWidth * data->curcfg.win_width >= GetSystemMetrics(SM_CXSCREEN) ||
345 tm->tmHeight * data->curcfg.win_height >= GetSystemMetrics(SM_CYSCREEN))
346 return FALSE;
348 /* fall through */
349 case 1:
350 if (tm->tmCharSet != DEFAULT_CHARSET && tm->tmCharSet != g_uiDefaultCharset) return FALSE;
351 /* fall through */
352 case 2:
353 if (tm->tmItalic || tm->tmUnderlined || tm->tmStruckOut) return FALSE;
354 break;
356 return TRUE;
359 /******************************************************************
360 * WCUSER_ValidateFont
362 * Returns true if the font family described in lf is usable as a font for the renderer
364 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONTW* lf, int pass)
366 switch (pass) /* we get increasingly lenient in later passes */
368 case 0:
369 case 1:
370 if (lf->lfCharSet != DEFAULT_CHARSET && lf->lfCharSet != g_uiDefaultCharset) return FALSE;
371 /* fall through */
372 case 2:
373 if ((lf->lfPitchAndFamily & 3) != FIXED_PITCH) return FALSE;
374 /* fall through */
375 case 3:
376 if (lf->lfFaceName[0] == '@') return FALSE;
377 break;
379 return TRUE;
382 /******************************************************************
383 * get_first_font_enum_2
384 * get_first_font_enum
386 * Helper functions to get a decent font for the renderer
388 static int CALLBACK get_first_font_enum_2(const LOGFONTW* lf, const TEXTMETRICW* tm,
389 DWORD FontType, LPARAM lParam)
391 struct font_chooser* fc = (struct font_chooser*)lParam;
393 WCUSER_DumpTextMetric(tm, FontType);
394 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType, fc->pass))
396 LOGFONTW mlf = *lf;
398 /* Use the default sizes for the font (this is needed, especially for
399 * TrueType fonts, so that we get a decent size, not the max size)
401 mlf.lfWidth = fc->data->curcfg.cell_width;
402 mlf.lfHeight = fc->data->curcfg.cell_height;
403 if (WCUSER_SetFont(fc->data, &mlf))
405 struct config_data defcfg;
407 WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
408 fc->done = 1;
409 /* since we've modified the current config with new font information,
410 * set this information as the new default.
412 WINECON_RegLoad(NULL, &defcfg);
413 defcfg.cell_width = fc->data->curcfg.cell_width;
414 defcfg.cell_height = fc->data->curcfg.cell_height;
415 lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
416 /* Force also its writing back to the registry so that we can get it
417 * the next time.
419 WINECON_RegSave(&defcfg);
420 return 0;
423 return 1;
426 static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
427 DWORD FontType, LPARAM lParam)
429 struct font_chooser* fc = (struct font_chooser*)lParam;
431 WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
432 if (WCUSER_ValidateFont(fc->data, lf, fc->pass))
434 EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
435 get_first_font_enum_2, lParam);
436 return !fc->done; /* we just need the first matching one... */
438 return 1;
441 /******************************************************************
442 * WCUSER_CopyFont
444 * get the relevant information from the font described in lf and store them
445 * in config
447 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
449 TEXTMETRICW tm;
450 HDC hDC;
451 HFONT hFont, hOldFont;
452 CPINFO cpinfo;
454 if (!(hDC = GetDC(hWnd))) return NULL;
455 if (!(hFont = CreateFontIndirectW(lf)))
457 ReleaseDC(hWnd, hDC);
458 return NULL;
460 hOldFont = SelectObject(hDC, hFont);
461 GetTextMetricsW(hDC, &tm);
462 SelectObject(hDC, hOldFont);
463 ReleaseDC(hWnd, hDC);
465 config->cell_width = tm.tmAveCharWidth;
466 config->cell_height = tm.tmHeight + tm.tmExternalLeading;
467 config->font_weight = tm.tmWeight;
468 lstrcpyW(config->face_name, lf->lfFaceName);
469 if (el) *el = tm.tmExternalLeading;
471 /* FIXME: use maximum width for DBCS codepages since some chars take two cells */
472 if (GetCPInfo( GetConsoleOutputCP(), &cpinfo ) && cpinfo.MaxCharSize > 1)
473 config->cell_width = tm.tmMaxCharWidth;
475 return hFont;
478 /******************************************************************
479 * WCUSER_FillLogFont
483 void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
485 lf->lfHeight = height;
486 lf->lfWidth = 0;
487 lf->lfEscapement = 0;
488 lf->lfOrientation = 0;
489 lf->lfWeight = weight;
490 lf->lfItalic = FALSE;
491 lf->lfUnderline = FALSE;
492 lf->lfStrikeOut = FALSE;
493 lf->lfCharSet = DEFAULT_CHARSET;
494 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
495 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
496 lf->lfQuality = DEFAULT_QUALITY;
497 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
498 lstrcpyW(lf->lfFaceName, name);
501 /******************************************************************
502 * WCUSER_SetFont
504 * sets logfont as the new font for the console
506 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
508 HFONT hFont;
509 LONG el;
511 if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
512 return TRUE;
514 hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
515 if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
517 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
518 PRIVATE(data)->hFont = hFont;
519 PRIVATE(data)->ext_leading = el;
521 WCUSER_ComputePositions(data);
522 WCUSER_NewBitmap(data);
523 InvalidateRect(data->hWnd, NULL, FALSE);
524 UpdateWindow(data->hWnd);
526 return TRUE;
529 /******************************************************************
530 * WCUSER_SetFontPmt
532 * Sets a new font for the console.
533 * In fact a wrapper for WCUSER_SetFont
535 static void WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
536 unsigned height, unsigned weight)
538 LOGFONTW lf;
539 struct font_chooser fc;
541 WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
542 wine_dbgstr_wn(font, -1), height, weight);
544 if (font[0] != '\0' && height != 0 && weight != 0)
546 WCUSER_FillLogFont(&lf, font, height, weight);
547 if (WCUSER_SetFont(data, &lf))
549 WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
550 return;
554 /* try to find an acceptable font */
555 WINE_WARN("Couldn't match the font from registry... trying to find one\n");
556 fc.data = data;
557 fc.done = FALSE;
558 for (fc.pass = 0; fc.pass <= 4; fc.pass++)
560 EnumFontFamiliesW(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
561 if (fc.done) return;
563 WINECON_Fatal("Couldn't find a decent font, aborting\n");
566 /******************************************************************
567 * WCUSER_GetCell
569 * Get a cell from a relative coordinate in window (takes into
570 * account the scrolling)
572 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
574 COORD c;
576 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
577 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
579 return c;
582 /******************************************************************
583 * WCUSER_GetSelectionRect
585 * Get the selection rectangle
587 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
589 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) - data->curcfg.win_pos.X) * data->curcfg.cell_width;
590 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
591 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
592 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
595 /******************************************************************
596 * WCUSER_SetSelection
600 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
602 HDC hDC;
603 RECT r;
605 WCUSER_GetSelectionRect(data, &r);
606 hDC = hRefDC ? hRefDC : GetDC(data->hWnd);
607 if (hDC)
609 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
610 HideCaret(data->hWnd);
611 InvertRect(hDC, &r);
612 if (hDC != hRefDC)
613 ReleaseDC(data->hWnd, hDC);
614 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
615 ShowCaret(data->hWnd);
619 /******************************************************************
620 * WCUSER_MoveSelection
624 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
626 RECT r;
627 HDC hDC;
629 if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
630 c2.X < 0 || c2.X >= data->curcfg.sb_width ||
631 c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
632 c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
633 return;
635 WCUSER_GetSelectionRect(data, &r);
636 hDC = GetDC(data->hWnd);
637 if (hDC)
639 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
640 HideCaret(data->hWnd);
641 InvertRect(hDC, &r);
643 PRIVATE(data)->selectPt1 = c1;
644 PRIVATE(data)->selectPt2 = c2;
645 if (hDC)
647 WCUSER_GetSelectionRect(data, &r);
648 InvertRect(hDC, &r);
649 ReleaseDC(data->hWnd, hDC);
650 if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
651 ShowCaret(data->hWnd);
655 /******************************************************************
656 * WCUSER_CopySelectionToClipboard
658 * Copies the current selection into the clipboard
660 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
662 HANDLE hMem;
663 LPWSTR p;
664 unsigned w, h;
666 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
667 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
669 if (!OpenClipboard(data->hWnd)) return;
670 EmptyClipboard();
672 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
673 if (hMem && (p = GlobalLock(hMem)))
675 COORD c;
676 int y;
678 c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
679 c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
681 for (y = 0; y < h; y++, c.Y++)
683 LPWSTR end;
684 DWORD count;
686 ReadConsoleOutputCharacterW(data->hConOut, p, w - 1, c, &count);
688 /* strip spaces from the end of the line */
689 end = p + w - 1;
690 while (end > p && *(end - 1) == ' ')
691 end--;
692 *end = (y < h - 1) ? '\n' : '\0';
693 p = end + 1;
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(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 = VkKeyScanW(ptr[i]);
730 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
731 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(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 WriteConsoleInputW(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(data->hWnd, &r, FALSE);
761 UpdateWindow(data->hWnd);
765 /******************************************************************
766 * WCUSER_Paint
770 static void WCUSER_Paint(const struct inner_data* data)
772 PAINTSTRUCT ps;
774 if (data->in_set_config) return; /* in order to avoid some flicker */
775 BeginPaint(data->hWnd, &ps);
776 BitBlt(ps.hdc, 0, 0,
777 data->curcfg.win_width * data->curcfg.cell_width,
778 data->curcfg.win_height * data->curcfg.cell_height,
779 PRIVATE(data)->hMemDC,
780 data->curcfg.win_pos.X * data->curcfg.cell_width,
781 data->curcfg.win_pos.Y * data->curcfg.cell_height,
782 SRCCOPY);
783 if (PRIVATE(data)->has_selection)
784 WCUSER_SetSelection(data, ps.hdc);
785 EndPaint(data->hWnd, &ps);
788 /******************************************************************
789 * WCUSER_Scroll
793 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
795 if (horz)
797 ScrollWindow(data->hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, NULL, NULL);
798 SetScrollPos(data->hWnd, SB_HORZ, pos, TRUE);
799 data->curcfg.win_pos.X = pos;
801 else
803 ScrollWindow(data->hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, NULL, NULL);
804 SetScrollPos(data->hWnd, SB_VERT, pos, TRUE);
805 data->curcfg.win_pos.Y = pos;
807 InvalidateRect(data->hWnd, NULL, FALSE);
810 /******************************************************************
811 * WCUSER_FillMenu
815 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
817 HMENU hSubMenu;
818 HINSTANCE hInstance = GetModuleHandleW(NULL);
819 WCHAR buff[256];
821 if (!hMenu) return FALSE;
823 /* FIXME: error handling & memory cleanup */
824 hSubMenu = CreateMenu();
825 if (!hSubMenu) return FALSE;
827 LoadStringW(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(buff[0]));
828 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
829 LoadStringW(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(buff[0]));
830 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
831 LoadStringW(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(buff[0]));
832 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
833 LoadStringW(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(buff[0]));
834 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
835 LoadStringW(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(buff[0]));
836 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
837 LoadStringW(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(buff[0]));
838 InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
840 if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
841 LoadStringW(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(buff[0]));
842 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
843 LoadStringW(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(buff[0]));
844 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
845 LoadStringW(hInstance, IDS_PROPERTIES, buff, sizeof(buff) / sizeof(buff[0]));
846 InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
848 return TRUE;
851 /******************************************************************
852 * WCUSER_SetMenuDetails
854 * Grays / ungrays the menu items according to their state
856 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
858 if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
860 EnableMenuItem(hMenu, IDS_COPY,
861 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
862 EnableMenuItem(hMenu, IDS_PASTE,
863 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
864 ? MF_ENABLED : MF_GRAYED));
865 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
866 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
869 /******************************************************************
870 * WCUSER_Create
872 * Creates the window for the rendering
874 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
876 struct inner_data* data;
877 HMENU hSysMenu;
879 data = lpcs->lpCreateParams;
880 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
881 data->hWnd = hWnd;
883 hSysMenu = GetSystemMenu(hWnd, FALSE);
884 if (!hSysMenu) return 0;
885 PRIVATE(data)->hPopMenu = CreatePopupMenu();
886 if (!PRIVATE(data)->hPopMenu) return 0;
888 WCUSER_FillMenu(hSysMenu, TRUE);
889 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
891 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
892 if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
894 data->curcfg.quick_edit = FALSE;
895 return 0;
898 /******************************************************************
899 * WCUSER_GetCtrlKeyState
901 * Get the console bit mask equivalent to the VK_ status in keyState
903 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
905 DWORD ret = 0;
907 GetKeyboardState(keyState);
908 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
909 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
910 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
911 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
912 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
913 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
914 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
915 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
917 return ret;
920 /******************************************************************
921 * WCUSER_HandleSelectionKey
923 * Handles keys while selecting an area
925 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
926 WPARAM wParam, LPARAM lParam)
928 BYTE keyState[256];
929 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
930 COORD c1, c2;
932 if (!down) return;
934 switch (state)
936 case 0:
937 switch (wParam)
939 case VK_RETURN:
940 PRIVATE(data)->has_selection = FALSE;
941 WCUSER_SetSelection(data, 0);
942 WCUSER_CopySelectionToClipboard(data);
943 return;
944 case VK_RIGHT:
945 c1 = PRIVATE(data)->selectPt1;
946 c2 = PRIVATE(data)->selectPt2;
947 c1.X++; c2.X++;
948 WCUSER_MoveSelection(data, c1, c2);
949 return;
950 case VK_LEFT:
951 c1 = PRIVATE(data)->selectPt1;
952 c2 = PRIVATE(data)->selectPt2;
953 c1.X--; c2.X--;
954 WCUSER_MoveSelection(data, c1, c2);
955 return;
956 case VK_UP:
957 c1 = PRIVATE(data)->selectPt1;
958 c2 = PRIVATE(data)->selectPt2;
959 c1.Y--; c2.Y--;
960 WCUSER_MoveSelection(data, c1, c2);
961 return;
962 case VK_DOWN:
963 c1 = PRIVATE(data)->selectPt1;
964 c2 = PRIVATE(data)->selectPt2;
965 c1.Y++; c2.Y++;
966 WCUSER_MoveSelection(data, c1, c2);
967 return;
969 break;
970 case SHIFT_PRESSED:
971 switch (wParam)
973 case VK_RIGHT:
974 c1 = PRIVATE(data)->selectPt1;
975 c2 = PRIVATE(data)->selectPt2;
976 c2.X++;
977 WCUSER_MoveSelection(data, c1, c2);
978 return;
979 case VK_LEFT:
980 c1 = PRIVATE(data)->selectPt1;
981 c2 = PRIVATE(data)->selectPt2;
982 c2.X--;
983 WCUSER_MoveSelection(data, c1, c2);
984 return;
985 case VK_UP:
986 c1 = PRIVATE(data)->selectPt1;
987 c2 = PRIVATE(data)->selectPt2;
988 c2.Y--;
989 WCUSER_MoveSelection(data, c1, c2);
990 return;
991 case VK_DOWN:
992 c1 = PRIVATE(data)->selectPt1;
993 c2 = PRIVATE(data)->selectPt2;
994 c2.Y++;
995 WCUSER_MoveSelection(data, c1, c2);
996 return;
998 break;
1001 if (wParam < VK_SPACE) /* Shift, Alt, Ctrl, Num Lock etc. */
1002 return;
1004 WCUSER_SetSelection(data, 0);
1005 PRIVATE(data)->has_selection = FALSE;
1008 /******************************************************************
1009 * WCUSER_GenerateKeyInputRecord
1011 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1013 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1014 WPARAM wParam, LPARAM lParam)
1016 INPUT_RECORD ir;
1017 DWORD n;
1018 WCHAR buf[2];
1019 static WCHAR last; /* keep last char seen as feed for key up message */
1020 BYTE keyState[256];
1022 ir.EventType = KEY_EVENT;
1023 ir.Event.KeyEvent.bKeyDown = down;
1024 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1025 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1027 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1029 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1030 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1031 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1033 if (down)
1035 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1037 case 2:
1038 /* FIXME... should generate two events... */
1039 /* fall through */
1040 case 1:
1041 last = buf[0];
1042 break;
1043 default:
1044 last = 0;
1045 break;
1048 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME: HACKY... and buggy because it should be a stack, not a single value */
1049 if (!down) last = 0;
1051 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1054 /******************************************************************
1055 * WCUSER_GenerateMouseInputRecord
1059 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1060 WPARAM wParam, DWORD event)
1062 INPUT_RECORD ir;
1063 BYTE keyState[256];
1064 DWORD mode, n;
1066 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1067 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1068 return;
1070 ir.EventType = MOUSE_EVENT;
1071 ir.Event.MouseEvent.dwMousePosition = c;
1072 ir.Event.MouseEvent.dwButtonState = 0;
1073 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1074 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1075 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1076 if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
1077 if (wParam & MK_SHIFT) ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
1078 if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1079 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1080 ir.Event.MouseEvent.dwEventFlags = event;
1082 WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1085 /******************************************************************
1086 * WCUSER_Proc
1090 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1092 struct inner_data* data = (struct inner_data*)GetWindowLongPtrW(hWnd, 0);
1094 switch (uMsg)
1096 case WM_CREATE:
1097 return WCUSER_Create(hWnd, (LPCREATESTRUCTW)lParam);
1098 case WM_DESTROY:
1099 data->hWnd = 0;
1100 PostQuitMessage(0);
1101 break;
1102 case WM_PAINT:
1103 WCUSER_Paint(data);
1104 break;
1105 case WM_KEYDOWN:
1106 case WM_KEYUP:
1107 if (PRIVATE(data)->has_selection)
1108 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1109 else
1110 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam);
1111 break;
1112 case WM_SYSKEYDOWN:
1113 case WM_SYSKEYUP:
1114 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam);
1115 break;
1116 case WM_LBUTTONDOWN:
1117 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1119 if (PRIVATE(data)->has_selection)
1120 WCUSER_SetSelection(data, 0);
1122 if (data->curcfg.quick_edit && PRIVATE(data)->has_selection)
1124 PRIVATE(data)->has_selection = FALSE;
1126 else
1128 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1129 SetCapture(data->hWnd);
1130 WCUSER_SetSelection(data, 0);
1131 PRIVATE(data)->has_selection = TRUE;
1134 else
1136 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1138 break;
1139 case WM_MOUSEMOVE:
1140 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1142 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection &&
1143 (wParam & MK_LBUTTON))
1145 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1148 else
1150 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1152 break;
1153 case WM_LBUTTONUP:
1154 if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1156 if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection)
1158 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1159 ReleaseCapture();
1162 else
1164 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1166 break;
1167 case WM_RBUTTONDOWN:
1168 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1170 POINT pt;
1172 pt.x = (short)LOWORD(lParam);
1173 pt.y = (short)HIWORD(lParam);
1174 ClientToScreen(hWnd, &pt);
1175 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1176 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON,
1177 pt.x, pt.y, 0, hWnd, NULL);
1179 else
1181 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1183 break;
1184 case WM_RBUTTONUP:
1185 /* no need to track for rbutton up when opening the popup... the event will be
1186 * swallowed by TrackPopupMenu */
1187 case WM_MBUTTONDOWN:
1188 case WM_MBUTTONUP:
1189 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1190 break;
1191 case WM_LBUTTONDBLCLK:
1192 case WM_MBUTTONDBLCLK:
1193 case WM_RBUTTONDBLCLK:
1194 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
1195 break;
1196 case WM_SETFOCUS:
1197 if (data->curcfg.cursor_visible)
1199 CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
1200 data->curcfg.cell_width, data->curcfg.cell_height);
1201 WCUSER_PosCursor(data);
1203 break;
1204 case WM_KILLFOCUS:
1205 if (data->curcfg.cursor_visible)
1206 DestroyCaret();
1207 break;
1208 case WM_HSCROLL:
1210 struct config_data cfg = data->curcfg;
1212 switch (LOWORD(wParam))
1214 case SB_PAGEUP: cfg.win_pos.X -= 8; break;
1215 case SB_PAGEDOWN: cfg.win_pos.X += 8; break;
1216 case SB_LINEUP: cfg.win_pos.X--; break;
1217 case SB_LINEDOWN: cfg.win_pos.X++; break;
1218 case SB_THUMBTRACK: cfg.win_pos.X = HIWORD(wParam); break;
1219 default: break;
1221 if (cfg.win_pos.X < 0) cfg.win_pos.X = 0;
1222 if (cfg.win_pos.X > data->curcfg.sb_width - data->curcfg.win_width)
1223 cfg.win_pos.X = data->curcfg.sb_width - data->curcfg.win_width;
1224 if (cfg.win_pos.X != data->curcfg.win_pos.X)
1226 WINECON_SetConfig(data, &cfg);
1229 break;
1230 case WM_MOUSEWHEEL:
1231 if (data->curcfg.sb_height <= data->curcfg.win_height)
1233 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1234 break;
1236 /* else fallthrough */
1237 case WM_VSCROLL:
1239 struct config_data cfg = data->curcfg;
1241 if (uMsg == WM_MOUSEWHEEL)
1243 UINT scrollLines = 3;
1244 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
1245 scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1246 cfg.win_pos.Y += scrollLines;
1247 } else {
1248 switch (LOWORD(wParam))
1250 case SB_PAGEUP: cfg.win_pos.Y -= 8; break;
1251 case SB_PAGEDOWN: cfg.win_pos.Y += 8; break;
1252 case SB_LINEUP: cfg.win_pos.Y--; break;
1253 case SB_LINEDOWN: cfg.win_pos.Y++; break;
1254 case SB_THUMBTRACK: cfg.win_pos.Y = HIWORD(wParam); break;
1255 default: break;
1259 if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
1260 if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
1261 cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
1262 if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
1264 WINECON_SetConfig(data, &cfg);
1267 break;
1268 case WM_SYSCOMMAND:
1269 switch (wParam)
1271 case IDS_DEFAULT:
1272 WCUSER_GetProperties(data, FALSE);
1273 break;
1274 case IDS_PROPERTIES:
1275 WCUSER_GetProperties(data, TRUE);
1276 break;
1277 default:
1278 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1280 break;
1281 case WM_COMMAND:
1282 switch (wParam)
1284 case IDS_DEFAULT:
1285 WCUSER_GetProperties(data, FALSE);
1286 break;
1287 case IDS_PROPERTIES:
1288 WCUSER_GetProperties(data, TRUE);
1289 break;
1290 case IDS_MARK:
1291 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1292 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1293 WCUSER_SetSelection(data, 0);
1294 PRIVATE(data)->has_selection = TRUE;
1295 break;
1296 case IDS_COPY:
1297 if (PRIVATE(data)->has_selection)
1299 PRIVATE(data)->has_selection = FALSE;
1300 WCUSER_SetSelection(data, 0);
1301 WCUSER_CopySelectionToClipboard(data);
1303 break;
1304 case IDS_PASTE:
1305 WCUSER_PasteFromClipboard(data);
1306 break;
1307 case IDS_SELECTALL:
1308 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1309 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1310 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1311 WCUSER_SetSelection(data, 0);
1312 PRIVATE(data)->has_selection = TRUE;
1313 break;
1314 case IDS_SCROLL:
1315 case IDS_SEARCH:
1316 WINE_FIXME("Unhandled yet command: %lx\n", wParam);
1317 break;
1318 default:
1319 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1321 break;
1322 case WM_INITMENUPOPUP:
1323 if (!HIWORD(lParam)) return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1324 WCUSER_SetMenuDetails(data, GetSystemMenu(data->hWnd, FALSE));
1325 break;
1326 case WM_SIZE:
1327 WINECON_ResizeWithContainer(data, LOWORD(lParam) / data->curcfg.cell_width,
1328 HIWORD(lParam) / data->curcfg.cell_height);
1329 break;
1330 default:
1331 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1333 return 0;
1336 /******************************************************************
1337 * WCUSER_DeleteBackend
1341 static void WCUSER_DeleteBackend(struct inner_data* data)
1343 if (!PRIVATE(data)) return;
1344 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1345 if (data->hWnd) DestroyWindow(data->hWnd);
1346 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1347 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1348 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1349 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1352 /******************************************************************
1353 * WCUSER_MainLoop
1357 static int WCUSER_MainLoop(struct inner_data* data)
1359 MSG msg;
1361 ShowWindow(data->hWnd, data->nCmdShow);
1362 while (!data->dying || !data->curcfg.exit_on_die)
1364 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1366 case WAIT_OBJECT_0:
1367 WINECON_GrabChanges(data);
1368 break;
1369 case WAIT_OBJECT_0+1:
1370 /* need to use PeekMessageW loop instead of simple GetMessage:
1371 * multiple messages might have arrived in between,
1372 * so GetMessage would lead to delayed processing */
1373 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
1375 if (msg.message == WM_QUIT) return 1;
1376 WINE_TRACE("dispatching msg %04x\n", msg.message);
1377 DispatchMessageW(&msg);
1379 break;
1380 default:
1381 WINE_ERR("got pb\n");
1382 /* err */
1383 break;
1386 PostQuitMessage(0);
1387 return 0;
1390 /******************************************************************
1391 * WCUSER_InitBackend
1393 * Initialisation part II: creation of window.
1396 enum init_return WCUSER_InitBackend(struct inner_data* data)
1398 static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1400 WNDCLASSW wndclass;
1401 CHARSETINFO ci;
1403 if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
1404 return init_failed;
1405 g_uiDefaultCharset = ci.ciCharset;
1406 WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1408 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1409 if (!data->private) return init_failed;
1411 data->fnMainLoop = WCUSER_MainLoop;
1412 data->fnPosCursor = WCUSER_PosCursor;
1413 data->fnShapeCursor = WCUSER_ShapeCursor;
1414 data->fnComputePositions = WCUSER_ComputePositions;
1415 data->fnRefresh = WCUSER_Refresh;
1416 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1417 data->fnSetTitle = WCUSER_SetTitle;
1418 data->fnSetFont = WCUSER_SetFontPmt;
1419 data->fnScroll = WCUSER_Scroll;
1420 data->fnDeleteBackend = WCUSER_DeleteBackend;
1422 wndclass.style = CS_DBLCLKS;
1423 wndclass.lpfnWndProc = WCUSER_Proc;
1424 wndclass.cbClsExtra = 0;
1425 wndclass.cbWndExtra = sizeof(DWORD_PTR);
1426 wndclass.hInstance = GetModuleHandleW(NULL);
1427 wndclass.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
1428 wndclass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1429 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1430 wndclass.lpszMenuName = NULL;
1431 wndclass.lpszClassName = wClassName;
1433 RegisterClassW(&wndclass);
1435 data->hWnd = CreateWindowW(wndclass.lpszClassName, NULL,
1436 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1437 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1438 if (!data->hWnd) return init_not_supported;
1440 return init_success;