2 * a GUI application for displaying a console
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "winecon_user.h"
25 /* mapping console colors to RGB values */
26 COLORREF WCUSER_ColorMap[16] =
28 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x80), RGB(0x00, 0x80, 0x00), RGB(0x00, 0x80, 0x80),
29 RGB(0x80, 0x00, 0x00), RGB(0x80, 0x00, 0x80), RGB(0x80, 0x80, 0x00), RGB(0x80, 0x80, 0x80),
30 RGB(0xC0, 0xC0, 0xC0), RGB(0x00, 0x00, 0xFF), RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0xFF),
31 RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0xFF, 0x00), RGB(0xFF, 0xFF, 0xFF),
34 /******************************************************************
37 * Fills the Mem DC with current cells values
39 static void WCUSER_FillMemDC(const struct inner_data* data, int upd_tp, int upd_bm)
47 if (!(line = HeapAlloc(GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(WCHAR))))
48 {Trace(0, "OOM\n"); return;}
50 hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
51 for (j = upd_tp; j <= upd_bm; j++)
53 cell = &data->cells[j * data->curcfg.sb_width];
54 for (i = 0; i < data->curcfg.win_width; i++)
56 attr = cell[i].Attributes;
57 SetBkColor(PRIVATE(data)->hMemDC,WCUSER_ColorMap[(attr>>4)&0x0F]);
58 SetTextColor(PRIVATE(data)->hMemDC, WCUSER_ColorMap[attr&0x0F]);
59 for (k = i; k < data->curcfg.win_width && cell[k].Attributes == attr; k++)
61 line[k - i] = cell[k].Char.UnicodeChar;
63 TextOut(PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
68 SelectObject(PRIVATE(data)->hMemDC, hOldFont);
69 HeapFree(GetProcessHeap(), 0, line);
72 /******************************************************************
75 * Either the font geometry or the sb geometry has changed. we need to recreate the
78 static void WCUSER_NewBitmap(struct inner_data* data, BOOL fill)
83 if (!data->curcfg.sb_width || !data->curcfg.sb_height || !(hDC = GetDC(PRIVATE(data)->hWnd)))
85 hnew = CreateCompatibleBitmap(hDC,
86 data->curcfg.sb_width * data->curcfg.cell_width,
87 data->curcfg.sb_height * data->curcfg.cell_height);
88 ReleaseDC(PRIVATE(data)->hWnd, hDC);
89 hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
91 if (PRIVATE(data)->hBitmap)
93 if (hold == PRIVATE(data)->hBitmap)
94 DeleteObject(PRIVATE(data)->hBitmap);
98 PRIVATE(data)->hBitmap = hnew;
100 WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
103 /******************************************************************
104 * WCUSER_ResizeScreenBuffer
108 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
110 WCUSER_NewBitmap(data, FALSE);
113 /******************************************************************
116 * Set a new position for the cursor
118 static void WCUSER_PosCursor(const struct inner_data* data)
120 if (PRIVATE(data)->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
122 SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
123 (data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
124 ShowCaret(PRIVATE(data)->hWnd);
127 /******************************************************************
130 * Sets a new shape for the cursor
132 void WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
134 if (force || size != data->curcfg.cursor_size)
136 if (data->curcfg.cursor_visible && PRIVATE(data)->hWnd == GetFocus()) DestroyCaret();
137 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
138 PRIVATE(data)->cursor_bitmap = (HBITMAP)0;
141 int w16b; /* number of byets per row, aligned on word size */
145 w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
146 ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
147 if (!ptr) {Trace(0, "OOM\n"); return;}
148 nbl = max((data->curcfg.cell_height * size) / 100, 1);
149 for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
151 for (i = 0; i < data->curcfg.cell_width; i++)
153 ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
156 PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
157 data->curcfg.cell_height, 1, 1, ptr);
158 HeapFree(GetProcessHeap(), 0, ptr);
160 data->curcfg.cursor_size = size;
161 data->curcfg.cursor_visible = -1;
164 vis = (vis) ? TRUE : FALSE;
165 if (force || vis != data->curcfg.cursor_visible)
167 data->curcfg.cursor_visible = vis;
168 if (PRIVATE(data)->hWnd == GetFocus())
172 CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap,
173 data->curcfg.cell_width, data->curcfg.cell_height);
174 WCUSER_PosCursor(data);
184 /******************************************************************
185 * INECON_ComputePositions
187 * Recomputes all the components (mainly scroll bars) positions
189 void WCUSER_ComputePositions(struct inner_data* data)
194 /* compute window size from desired client size */
196 r.right = data->curcfg.win_width * data->curcfg.cell_width;
197 r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
201 ShowWindow(PRIVATE(data)->hWnd, SW_HIDE);
205 AdjustWindowRect(&r, GetWindowLong(PRIVATE(data)->hWnd, GWL_STYLE), FALSE);
208 if (data->curcfg.sb_width > data->curcfg.win_width)
210 dy = GetSystemMetrics(SM_CYHSCROLL);
211 SetScrollRange(PRIVATE(data)->hWnd, SB_HORZ, 0,
212 data->curcfg.sb_width - data->curcfg.win_width, FALSE);
213 SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
214 ShowScrollBar(PRIVATE(data)->hWnd, SB_HORZ, TRUE);
218 ShowScrollBar(PRIVATE(data)->hWnd, SB_HORZ, FALSE);
221 if (data->curcfg.sb_height > data->curcfg.win_height)
223 dx = GetSystemMetrics(SM_CXVSCROLL);
224 SetScrollRange(PRIVATE(data)->hWnd, SB_VERT, 0,
225 data->curcfg.sb_height - data->curcfg.win_height, FALSE);
226 SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, 0, FALSE); /* FIXME */
227 ShowScrollBar(PRIVATE(data)->hWnd, SB_VERT, TRUE);
231 ShowScrollBar(PRIVATE(data)->hWnd, SB_VERT, FALSE);
234 SetWindowPos(PRIVATE(data)->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
235 SWP_NOMOVE|SWP_NOZORDER|SWP_SHOWWINDOW);
236 WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
237 WCUSER_PosCursor(data);
240 /******************************************************************
243 * Sets the title to the wine console
245 static void WCUSER_SetTitle(const struct inner_data* data)
249 if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
250 SetWindowText(PRIVATE(data)->hWnd, buffer);
253 /******************************************************************
258 BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONT* lf)
260 return lf->lfHeight == config->cell_height &&
261 lf->lfWeight == config->font_weight &&
262 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
263 !lstrcmp(lf->lfFaceName, config->face_name);
266 struct font_chooser {
267 struct inner_data* data;
271 /******************************************************************
272 * WCUSER_ValidateFontMetric
274 * Returns true if the font described in tm is usable as a font for the renderer
276 BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRIC* tm, DWORD fontType)
280 if (fontType & RASTER_FONTTYPE)
281 ret = (tm->tmMaxCharWidth * data->curcfg.win_width < GetSystemMetrics(SM_CXSCREEN) &&
282 tm->tmHeight * data->curcfg.win_height < GetSystemMetrics(SM_CYSCREEN));
283 return ret && !tm->tmItalic && !tm->tmUnderlined && !tm->tmStruckOut &&
284 (tm->tmCharSet == DEFAULT_CHARSET /*|| tm->tmCharSet == ANSI_CHARSET*/);
287 /******************************************************************
288 * WCUSER_ValidateFont
290 * Returns true if the font family described in lf is usable as a font for the renderer
292 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONT* lf)
294 return (lf->lfPitchAndFamily & 3) == FIXED_PITCH &&
295 /* (lf->lfPitchAndFamily & 0xF0) == FF_MODERN && */
296 (lf->lfCharSet == DEFAULT_CHARSET || lf->lfCharSet == ANSI_CHARSET);
299 /******************************************************************
300 * get_first_font_enum_2
301 * get_first_font_enum
303 * Helper functions to get a decent font for the renderer
305 static int CALLBACK get_first_font_enum_2(const LOGFONT* lf, const TEXTMETRIC* tm,
306 DWORD FontType, LPARAM lParam)
308 struct font_chooser* fc = (struct font_chooser*)lParam;
310 if (WCUSER_ValidateFontMetric(fc->data, tm, FontType))
312 WCUSER_SetFont(fc->data, lf);
319 static int CALLBACK get_first_font_enum(const LOGFONT* lf, const TEXTMETRIC* tm,
320 DWORD FontType, LPARAM lParam)
322 struct font_chooser* fc = (struct font_chooser*)lParam;
324 if (WCUSER_ValidateFont(fc->data, lf))
326 EnumFontFamilies(PRIVATE(fc->data)->hMemDC, lf->lfFaceName, get_first_font_enum_2, lParam);
327 return !fc->done; /* we just need the first matching one... */
332 /******************************************************************
335 * get the relevant information from the font described in lf and store them
338 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONT* lf)
342 HFONT hFont, hOldFont;
345 if (!(hDC = GetDC(hWnd))) return (HFONT)0;
346 if (!(hFont = CreateFontIndirect(lf))) goto err1;
348 hOldFont = SelectObject(hDC, hFont);
349 GetTextMetrics(hDC, &tm);
352 * the current freetype engine (at least 2.0.x with x <= 8) and its implementation
353 * in Wine don't return adequate values for fixed fonts
354 * In Windows, those fonts are expectes to return the same value for
355 * - the average width
356 * - the largest width
357 * - the width of all characters in the font
358 * This isn't true in Wine. As a temporary workaound, we get as the width of the
359 * cell, the width of the first character in the font, after checking that all
360 * characters in the font have the same width (I hear paranoïa coming)
361 * when this gets fixed, the should be using tm.tmAveCharWidth or tm.tmMaxCharWidth
364 GetCharWidth32(hDC, tm.tmFirstChar, tm.tmFirstChar, &w);
365 for (i = tm.tmFirstChar + 1; i <= tm.tmLastChar; i += sizeof(buf) / sizeof(buf[0]))
369 l = min(tm.tmLastChar - i, sizeof(buf) / sizeof(buf[0]) - 1);
370 GetCharWidth32(hDC, i, i + l, buf);
371 for (j = 0; j <= l; j++)
375 Trace(0, "Non uniform cell width: [%d]=%d [%d]=%d\n",
376 i + j, buf[j], tm.tmFirstChar, w);
382 SelectObject(hDC, hOldFont);
383 ReleaseDC(hWnd, hDC);
385 config->cell_width = w;
386 config->cell_height = tm.tmHeight;
387 config->font_weight = tm.tmWeight;
388 lstrcpy(config->face_name, lf->lfFaceName);
392 if (hDC && hOldFont) SelectObject(hDC, hOldFont);
393 if (hFont) DeleteObject(hFont);
395 if (hDC) ReleaseDC(hWnd, hDC);
400 /******************************************************************
405 void WCUSER_FillLogFont(LOGFONT* lf, const WCHAR* name, UINT height, UINT weight)
407 lf->lfHeight = height;
409 lf->lfEscapement = 0;
410 lf->lfOrientation = 0;
411 lf->lfWeight = weight;
412 lf->lfItalic = FALSE;
413 lf->lfUnderline = FALSE;
414 lf->lfStrikeOut = FALSE;
415 lf->lfCharSet = DEFAULT_CHARSET;
416 lf->lfOutPrecision = OUT_DEFAULT_PRECIS;
417 lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
418 lf->lfQuality = DEFAULT_QUALITY;
419 lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
420 lstrcpy(lf->lfFaceName, name);
423 /******************************************************************
426 * sets logfont as the new font for the console
428 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONT* logfont)
430 if (WCUSER_AreFontsEqual(&data->curcfg, logfont)) return TRUE;
431 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
433 PRIVATE(data)->hFont = WCUSER_CopyFont(&data->curcfg, PRIVATE(data)->hWnd, logfont);
434 if (!PRIVATE(data)->hFont) {Trace(0, "wrong font\n");return FALSE;}
436 WCUSER_ComputePositions(data);
437 WCUSER_NewBitmap(data, TRUE);
438 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
439 UpdateWindow(PRIVATE(data)->hWnd);
444 /******************************************************************
447 * create a hFont from the settings saved in registry...
448 * (called on init, assuming no font has been created before)
450 static BOOL WCUSER_InitFont(struct inner_data* data)
453 struct font_chooser fc;
455 WCUSER_FillLogFont(&lf, data->curcfg.face_name,
456 data->curcfg.cell_height, data->curcfg.font_weight);
457 data->curcfg.face_name[0] = 0;
458 data->curcfg.cell_height = data->curcfg.font_weight = 0;
460 if (WCUSER_SetFont(data, &lf)) return TRUE;
462 /* try to find an acceptable font */
463 Trace(0, "Couldn't match the font from registry... trying to find one\n");
466 EnumFontFamilies(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
470 /******************************************************************
473 * Get a cell from the a relative coordinate in window (takes into
474 * account the scrolling)
476 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
480 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
481 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
486 /******************************************************************
487 * WCUSER_GetSelectionRect
489 * Get the selection rectangle
491 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
493 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) ) * data->curcfg.cell_width;
494 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) ) * data->curcfg.cell_height;
495 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1) * data->curcfg.cell_width;
496 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1) * data->curcfg.cell_height;
499 /******************************************************************
500 * WCUSER_SetSelection
504 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
509 WCUSER_GetSelectionRect(data, &r);
510 hDC = hRefDC ? hRefDC : GetDC(PRIVATE(data)->hWnd);
513 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
514 HideCaret(PRIVATE(data)->hWnd);
517 ReleaseDC(PRIVATE(data)->hWnd, hDC);
518 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
519 ShowCaret(PRIVATE(data)->hWnd);
523 /******************************************************************
524 * WCUSER_MoveSelection
528 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2, BOOL final)
533 WCUSER_GetSelectionRect(data, &r);
534 hDC = GetDC(PRIVATE(data)->hWnd);
537 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
538 HideCaret(PRIVATE(data)->hWnd);
541 PRIVATE(data)->selectPt1 = c1;
542 PRIVATE(data)->selectPt2 = c2;
545 WCUSER_GetSelectionRect(data, &r);
547 ReleaseDC(PRIVATE(data)->hWnd, hDC);
548 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
549 ShowCaret(PRIVATE(data)->hWnd);
554 PRIVATE(data)->has_selection = TRUE;
558 /******************************************************************
559 * WCUSER_CopySelectionToClipboard
561 * Copies the current selection into the clipboard
563 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
569 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
570 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
572 if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
575 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h - 1) * sizeof(WCHAR));
576 if (hMem && (p = GlobalLock(hMem)))
581 c.X = data->curcfg.win_pos.X + min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
582 c.Y = data->curcfg.win_pos.Y + min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
584 for (y = 0; y < h; y++, c.Y++)
586 ReadConsoleOutputCharacter(data->hConOut, &p[y * w], w - 1, c, NULL);
587 if (y < h - 1) p[y * w + w - 1] = '\n';
590 SetClipboardData(CF_UNICODETEXT, hMem);
595 /******************************************************************
596 * WCUSER_PasteFromClipboard
600 static void WCUSER_PasteFromClipboard(struct inner_data* data)
605 if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
606 h = GetClipboardData(CF_UNICODETEXT);
607 if (h && (ptr = GlobalLock(h)))
609 int i, len = GlobalSize(h) / sizeof(WCHAR);
614 ir[0].EventType = KEY_EVENT;
615 ir[0].Event.KeyEvent.wRepeatCount = 0;
616 ir[0].Event.KeyEvent.dwControlKeyState = 0;
617 ir[0].Event.KeyEvent.bKeyDown = TRUE;
619 /* generate the corresponding input records */
620 for (i = 0; i < len; i++)
622 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
623 sh = VkKeyScan(ptr[i]);
624 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
625 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(LOBYTE(sh), 0);
626 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
629 ir[1].Event.KeyEvent.bKeyDown = FALSE;
631 WriteConsoleInput(data->hConIn, ir, 2, &n);
638 /******************************************************************
643 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
645 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
650 r.right = data->curcfg.win_width * data->curcfg.cell_width;
651 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
652 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
653 InvalidateRect(PRIVATE(data)->hWnd, &r, FALSE);
654 WCUSER_FillMemDC(data, tp, bm);
655 UpdateWindow(PRIVATE(data)->hWnd);
659 /******************************************************************
664 static void WCUSER_Paint(const struct inner_data* data)
668 BeginPaint(PRIVATE(data)->hWnd, &ps);
670 data->curcfg.win_width * data->curcfg.cell_width,
671 data->curcfg.win_height * data->curcfg.cell_height,
672 PRIVATE(data)->hMemDC,
673 data->curcfg.win_pos.X * data->curcfg.cell_width,
674 data->curcfg.win_pos.Y * data->curcfg.cell_height,
676 if (PRIVATE(data)->has_selection)
677 WCUSER_SetSelection(data, ps.hdc);
678 EndPaint(PRIVATE(data)->hWnd, &ps);
681 /******************************************************************
686 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
690 SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, pos, TRUE);
691 data->curcfg.win_pos.X = pos;
692 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
696 SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, pos, TRUE);
697 data->curcfg.win_pos.Y = pos;
699 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
702 /******************************************************************
707 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
710 HINSTANCE hInstance = GetModuleHandle(NULL);
713 if (!hMenu) return FALSE;
715 /* FIXME: error handling & memory cleanup */
716 hSubMenu = CreateMenu();
717 if (!hSubMenu) return FALSE;
719 LoadString(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(WCHAR));
720 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
721 LoadString(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(WCHAR));
722 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
723 LoadString(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(WCHAR));
724 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
725 LoadString(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(WCHAR));
726 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
727 LoadString(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(WCHAR));
728 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
729 LoadString(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(WCHAR));
730 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
732 if (sep) InsertMenu(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
733 LoadString(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(WCHAR));
734 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
735 LoadString(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(WCHAR));
736 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
737 LoadString(hInstance, IDS_PROPERTY, buff, sizeof(buff) / sizeof(WCHAR));
738 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTY, buff);
743 /******************************************************************
746 * Creates the window for the rendering
748 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCT lpcs)
750 struct inner_data* data;
753 data = lpcs->lpCreateParams;
754 SetWindowLong(hWnd, 0L, (DWORD)data);
755 PRIVATE(data)->hWnd = hWnd;
757 data->curcfg.cursor_size = 101; /* invalid value, will trigger a complete cleanup */
759 hSysMenu = GetSystemMenu(hWnd, FALSE);
760 if (!hSysMenu) return 0;
761 PRIVATE(data)->hPopMenu = CreatePopupMenu();
762 if (!PRIVATE(data)->hPopMenu) return 0;
764 WCUSER_FillMenu(hSysMenu, TRUE);
765 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
767 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
768 if (!PRIVATE(data)->hMemDC) {Trace(0, "no mem dc\n");return 0;}
770 data->curcfg.quick_edit = FALSE;
774 /******************************************************************
775 * WCUSER_SetMenuDetails
777 * Grays / ungrays the menu items according to their state
779 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
781 if (!hMenu) {Trace(0, "Issue in getting menu bits\n");return;}
783 EnableMenuItem(hMenu, IDS_COPY,
784 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
785 EnableMenuItem(hMenu, IDS_PASTE,
786 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
787 ? MF_ENABLED : MF_GRAYED));
788 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
789 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
792 /******************************************************************
793 * CUSER_GetCtrlKeyState
795 * Get the console bit mask equivalent to the VK_ status in keyState
797 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
801 GetKeyboardState(keyState);
802 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
803 if (keyState[VK_CONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED; /* FIXME: gotta choose one */
804 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
805 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
806 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
807 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
808 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
809 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
810 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
815 /******************************************************************
816 * WCUSER_HandleSelectionKey
818 * Handles keys while selecting an area
820 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
821 WPARAM wParam, LPARAM lParam)
824 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
835 PRIVATE(data)->has_selection = FALSE;
836 WCUSER_SetSelection(data, 0);
837 WCUSER_CopySelectionToClipboard(data);
840 c1 = PRIVATE(data)->selectPt1;
841 c2 = PRIVATE(data)->selectPt2;
843 if (c1.X < data->curcfg.sb_width && c2.X < data->curcfg.sb_width)
845 WCUSER_MoveSelection(data, c1, c2, FALSE);
849 c1 = PRIVATE(data)->selectPt1;
850 c2 = PRIVATE(data)->selectPt2;
852 if (c1.X >= 0 && c2.X >= 0)
854 WCUSER_MoveSelection(data, c1, c2, FALSE);
858 c1 = PRIVATE(data)->selectPt1;
859 c2 = PRIVATE(data)->selectPt2;
861 if (c1.Y >= 0 && c2.Y >= 0)
863 WCUSER_MoveSelection(data, c1, c2, FALSE);
867 c1 = PRIVATE(data)->selectPt1;
868 c2 = PRIVATE(data)->selectPt2;
870 if (c1.X < data->curcfg.sb_height && c2.X < data->curcfg.sb_height)
872 WCUSER_MoveSelection(data, c1, c2, FALSE);
881 c1 = PRIVATE(data)->selectPt1;
882 c2 = PRIVATE(data)->selectPt2;
884 if (c2.X < data->curcfg.sb_width)
886 WCUSER_MoveSelection(data, c1, c2, FALSE);
890 c1 = PRIVATE(data)->selectPt1;
891 c2 = PRIVATE(data)->selectPt2;
895 WCUSER_MoveSelection(data, c1, c2, FALSE);
899 c1 = PRIVATE(data)->selectPt1;
900 c2 = PRIVATE(data)->selectPt2;
904 WCUSER_MoveSelection(data, c1, c2, FALSE);
908 c1 = PRIVATE(data)->selectPt1;
909 c2 = PRIVATE(data)->selectPt2;
911 if (c2.X < data->curcfg.sb_height)
913 WCUSER_MoveSelection(data, c1, c2, FALSE);
921 /******************************************************************
922 * WCUSER_GenerateKeyInputRecord
924 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
926 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
927 WPARAM wParam, LPARAM lParam, BOOL sys)
932 static WCHAR last; /* keep last char seen as feed for key up message */
935 ir.EventType = KEY_EVENT;
936 ir.Event.KeyEvent.bKeyDown = down;
937 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
938 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
940 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
942 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
943 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
944 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
945 if (sys) ir.Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED; /* FIXME: gotta choose one */
947 if (!(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
951 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
954 /* FIXME... should generate two events... */
964 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME HACKY... and buggy 'coz it should be a stack, not a single value */
968 WriteConsoleInput(data->hConIn, &ir, 1, &n);
971 /******************************************************************
972 * WCUSER_GenerateMouseInputRecord
976 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
977 WPARAM wParam, DWORD event)
983 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
984 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
987 ir.EventType = MOUSE_EVENT;
988 ir.Event.MouseEvent.dwMousePosition = c;
989 ir.Event.MouseEvent.dwButtonState = 0;
990 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
991 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
992 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
993 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
994 ir.Event.MouseEvent.dwEventFlags = event;
996 WriteConsoleInput(data->hConIn, &ir, 1, &n);
999 /******************************************************************
1004 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1006 struct inner_data* data = (struct inner_data*)GetWindowLong(hWnd, 0);
1011 return WCUSER_Create(hWnd, (LPCREATESTRUCT)lParam);
1013 PRIVATE(data)->hWnd = 0;
1021 if (PRIVATE(data)->has_selection)
1022 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1024 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam, FALSE);
1028 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam, TRUE);
1030 case WM_LBUTTONDOWN:
1031 if (data->curcfg.quick_edit)
1033 if (PRIVATE(data)->has_selection)
1035 PRIVATE(data)->has_selection = FALSE;
1036 WCUSER_SetSelection(data, 0);
1040 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1041 SetCapture(PRIVATE(data)->hWnd);
1042 WCUSER_SetSelection(data, 0);
1043 PRIVATE(data)->has_selection = TRUE;
1048 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1052 if (data->curcfg.quick_edit)
1054 if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
1055 (wParam & MK_LBUTTON))
1057 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), FALSE);
1062 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1066 if (data->curcfg.quick_edit)
1068 if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
1069 (wParam& MK_LBUTTON))
1071 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), TRUE);
1076 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1079 case WM_RBUTTONDOWN:
1080 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1084 GetWindowRect(hWnd, &r);
1085 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1086 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN,
1087 r.left + LOWORD(lParam), r.top + HIWORD(lParam), 0, hWnd, NULL);
1091 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1095 /* no need to track for rbutton up when opening the popup... the event will be
1096 * swallowed by TrackPopupMenu */
1097 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1100 /* FIXME: should we scroll too ? */
1101 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1104 if (data->curcfg.cursor_visible)
1106 CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap,
1107 data->curcfg.cell_width, data->curcfg.cell_height);
1108 WCUSER_PosCursor(data);
1112 if (data->curcfg.cursor_visible)
1117 int pos = data->curcfg.win_pos.X;
1119 switch (LOWORD(wParam))
1121 case SB_PAGEUP: pos -= 8; break;
1122 case SB_PAGEDOWN: pos += 8; break;
1123 case SB_LINEUP: pos--; break;
1124 case SB_LINEDOWN: pos++; break;
1125 case SB_THUMBTRACK: pos = HIWORD(wParam); break;
1128 if (pos < 0) pos = 0;
1129 if (pos > data->curcfg.sb_width - data->curcfg.win_width)
1130 pos = data->curcfg.sb_width - data->curcfg.win_width;
1131 if (pos != data->curcfg.win_pos.X)
1133 ScrollWindow(hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0,
1135 data->curcfg.win_pos.X = pos;
1136 SetScrollPos(hWnd, SB_HORZ, pos, TRUE);
1138 WCUSER_PosCursor(data);
1139 WINECON_NotifyWindowChange(data);
1145 int pos = data->curcfg.win_pos.Y;
1147 switch (LOWORD(wParam))
1149 case SB_PAGEUP: pos -= 8; break;
1150 case SB_PAGEDOWN: pos += 8; break;
1151 case SB_LINEUP: pos--; break;
1152 case SB_LINEDOWN: pos++; break;
1153 case SB_THUMBTRACK: pos = HIWORD(wParam); break;
1156 if (pos < 0) pos = 0;
1157 if (pos > data->curcfg.sb_height - data->curcfg.win_height)
1158 pos = data->curcfg.sb_height - data->curcfg.win_height;
1159 if (pos != data->curcfg.win_pos.Y)
1161 ScrollWindow(hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height,
1163 data->curcfg.win_pos.Y = pos;
1164 SetScrollPos(hWnd, SB_VERT, pos, TRUE);
1166 WCUSER_PosCursor(data);
1167 WINECON_NotifyWindowChange(data);
1175 WCUSER_GetProperties(data, FALSE);
1178 WCUSER_GetProperties(data, TRUE);
1181 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1188 WCUSER_GetProperties(data, FALSE);
1191 WCUSER_GetProperties(data, TRUE);
1194 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1195 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1196 WCUSER_SetSelection(data, 0);
1197 PRIVATE(data)->has_selection = TRUE;
1200 if (PRIVATE(data)->has_selection)
1202 PRIVATE(data)->has_selection = FALSE;
1203 WCUSER_SetSelection(data, 0);
1204 WCUSER_CopySelectionToClipboard(data);
1208 WCUSER_PasteFromClipboard(data);
1211 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1212 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1213 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1214 WCUSER_SetSelection(data, 0);
1215 PRIVATE(data)->has_selection = TRUE;
1219 Trace(0, "unhandled yet command: %x\n", wParam);
1222 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1225 case WM_INITMENUPOPUP:
1226 if (!HIWORD(lParam)) return DefWindowProc(hWnd, uMsg, wParam, lParam);
1227 WCUSER_SetMenuDetails(data, GetSystemMenu(PRIVATE(data)->hWnd, FALSE));
1230 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1235 /******************************************************************
1236 * WCUSER_DeleteBackend
1240 void WCUSER_DeleteBackend(struct inner_data* data)
1242 if (!PRIVATE(data)) return;
1243 if (PRIVATE(data)->hWnd) DestroyWindow(PRIVATE(data)->hWnd);
1244 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1245 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1246 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1247 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1248 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1251 /******************************************************************
1256 static int WCUSER_MainLoop(struct inner_data* data)
1262 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1265 if (!WINECON_GrabChanges(data))
1268 case WAIT_OBJECT_0+1:
1269 switch (GetMessage(&msg, 0, 0, 0))
1271 case -1: /* the event handle became invalid, so exit */
1273 case 0: /* WM_QUIT has been posted */
1276 DispatchMessage(&msg);
1281 Trace(0, "got pb\n");
1288 /******************************************************************
1289 * WCUSER_InitBackend
1291 * Initialisation part II: creation of window.
1294 BOOL WCUSER_InitBackend(struct inner_data* data)
1296 static WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1300 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1301 if (!data->private) return FALSE;
1303 data->fnMainLoop = WCUSER_MainLoop;
1304 data->fnPosCursor = WCUSER_PosCursor;
1305 data->fnShapeCursor = WCUSER_ShapeCursor;
1306 data->fnComputePositions = WCUSER_ComputePositions;
1307 data->fnRefresh = WCUSER_Refresh;
1308 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1309 data->fnSetTitle = WCUSER_SetTitle;
1310 data->fnScroll = WCUSER_Scroll;
1311 data->fnDeleteBackend = WCUSER_DeleteBackend;
1314 wndclass.lpfnWndProc = WCUSER_Proc;
1315 wndclass.cbClsExtra = 0;
1316 wndclass.cbWndExtra = sizeof(DWORD);
1317 wndclass.hInstance = GetModuleHandle(NULL);
1318 wndclass.hIcon = LoadIcon(0, IDI_WINLOGO);
1319 wndclass.hCursor = LoadCursor(0, IDC_ARROW);
1320 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1321 wndclass.lpszMenuName = NULL;
1322 wndclass.lpszClassName = wClassName;
1324 RegisterClass(&wndclass);
1326 CreateWindow(wndclass.lpszClassName, NULL,
1327 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1328 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1329 if (!PRIVATE(data)->hWnd) return FALSE;
1331 /* force update of current data */
1332 if (!WINECON_GrabChanges(data)) return FALSE;
1334 if (!WCUSER_InitFont(data)) return FALSE;