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->lfWidth == config->cell_width &&
262 lf->lfWeight == config->font_weight &&
263 !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
264 !lstrcmpW(lf->lfFaceName, config->face_name);
267 /******************************************************************
272 void WCUSER_CopyFont(struct config_data* config, const LOGFONT* lf)
274 config->cell_width = lf->lfWidth;
275 config->cell_height = lf->lfHeight;
276 config->font_weight = lf->lfWeight;
277 lstrcpyW(config->face_name, lf->lfFaceName);
280 /******************************************************************
283 * create a hFont from the settings saved in registry...
284 * (called on init, assuming no font has been created before)
286 BOOL WCUSER_InitFont(struct inner_data* data)
290 lf.lfHeight = -data->curcfg.cell_height;
291 lf.lfWidth = data->curcfg.cell_width;
293 lf.lfOrientation = 0;
294 lf.lfWeight = data->curcfg.font_weight;
296 lf.lfUnderline = FALSE;
297 lf.lfStrikeOut = FALSE;
298 lf.lfCharSet = DEFAULT_CHARSET;
299 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
300 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
301 lf.lfQuality = DEFAULT_QUALITY;
302 lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
303 lstrcpy(lf.lfFaceName, data->curcfg.face_name);
304 PRIVATE(data)->hFont = CreateFontIndirect(&lf);
305 if (!PRIVATE(data)->hFont) return FALSE;
307 WCUSER_ComputePositions(data);
308 WCUSER_NewBitmap(data, TRUE);
309 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
310 UpdateWindow(PRIVATE(data)->hWnd);
314 /******************************************************************
317 * sets logfont as the new font for the console
319 BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONT* logfont)
321 if (WCUSER_AreFontsEqual(&data->curcfg, logfont)) return TRUE;
322 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
323 PRIVATE(data)->hFont = CreateFontIndirect(logfont);
324 if (!PRIVATE(data)->hFont) {Trace(0, "wrong font\n");return FALSE;}
325 WCUSER_CopyFont(&data->curcfg, logfont);
327 WCUSER_ComputePositions(data);
328 WCUSER_NewBitmap(data, TRUE);
329 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
330 UpdateWindow(PRIVATE(data)->hWnd);
334 /******************************************************************
337 * Get a cell from the a relative coordinate in window (takes into
338 * account the scrolling)
340 static COORD WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
344 c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
345 c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
350 /******************************************************************
351 * WCUSER_GetSelectionRect
353 * Get the selection rectangle
355 static void WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
357 r->left = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) ) * data->curcfg.cell_width;
358 r->top = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) ) * data->curcfg.cell_height;
359 r->right = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1) * data->curcfg.cell_width;
360 r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1) * data->curcfg.cell_height;
363 /******************************************************************
364 * WCUSER_SetSelection
368 static void WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
373 WCUSER_GetSelectionRect(data, &r);
374 hDC = hRefDC ? hRefDC : GetDC(PRIVATE(data)->hWnd);
377 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
378 HideCaret(PRIVATE(data)->hWnd);
381 ReleaseDC(PRIVATE(data)->hWnd, hDC);
382 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
383 ShowCaret(PRIVATE(data)->hWnd);
387 /******************************************************************
388 * WCUSER_MoveSelection
392 static void WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2, BOOL final)
397 WCUSER_GetSelectionRect(data, &r);
398 hDC = GetDC(PRIVATE(data)->hWnd);
401 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
402 HideCaret(PRIVATE(data)->hWnd);
405 PRIVATE(data)->selectPt1 = c1;
406 PRIVATE(data)->selectPt2 = c2;
409 WCUSER_GetSelectionRect(data, &r);
411 ReleaseDC(PRIVATE(data)->hWnd, hDC);
412 if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
413 ShowCaret(PRIVATE(data)->hWnd);
418 PRIVATE(data)->has_selection = TRUE;
422 /******************************************************************
423 * WCUSER_CopySelectionToClipboard
425 * Copies the current selection into the clipboard
427 static void WCUSER_CopySelectionToClipboard(const struct inner_data* data)
433 w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
434 h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
436 if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
439 hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h - 1) * sizeof(WCHAR));
440 if (hMem && (p = GlobalLock(hMem)))
445 c.X = data->curcfg.win_pos.X + min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
446 c.Y = data->curcfg.win_pos.Y + min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
448 for (y = 0; y < h; y++, c.Y++)
450 ReadConsoleOutputCharacter(data->hConOut, &p[y * w], w - 1, c, NULL);
451 if (y < h - 1) p[y * w + w - 1] = '\n';
454 SetClipboardData(CF_UNICODETEXT, hMem);
459 /******************************************************************
460 * WCUSER_PasteFromClipboard
464 static void WCUSER_PasteFromClipboard(struct inner_data* data)
469 if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
470 h = GetClipboardData(CF_UNICODETEXT);
471 if (h && (ptr = GlobalLock(h)))
473 int i, len = GlobalSize(h) / sizeof(WCHAR);
478 ir[0].EventType = KEY_EVENT;
479 ir[0].Event.KeyEvent.wRepeatCount = 0;
480 ir[0].Event.KeyEvent.dwControlKeyState = 0;
481 ir[0].Event.KeyEvent.bKeyDown = TRUE;
483 /* generate the corresponding input records */
484 for (i = 0; i < len; i++)
486 /* FIXME: the modifying keys are not generated (shift, ctrl...) */
487 sh = VkKeyScan(ptr[i]);
488 ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
489 ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(LOBYTE(sh), 0);
490 ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
493 ir[1].Event.KeyEvent.bKeyDown = FALSE;
495 WriteConsoleInput(data->hConIn, ir, 2, &n);
502 /******************************************************************
507 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
509 if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
514 r.right = data->curcfg.win_width * data->curcfg.cell_width;
515 r.top = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
516 r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
517 InvalidateRect(PRIVATE(data)->hWnd, &r, FALSE);
518 WCUSER_FillMemDC(data, tp, bm);
519 UpdateWindow(PRIVATE(data)->hWnd);
523 /******************************************************************
528 static void WCUSER_Paint(const struct inner_data* data)
532 BeginPaint(PRIVATE(data)->hWnd, &ps);
534 data->curcfg.win_width * data->curcfg.cell_width,
535 data->curcfg.win_height * data->curcfg.cell_height,
536 PRIVATE(data)->hMemDC,
537 data->curcfg.win_pos.X * data->curcfg.cell_width,
538 data->curcfg.win_pos.Y * data->curcfg.cell_height,
540 if (PRIVATE(data)->has_selection)
541 WCUSER_SetSelection(data, ps.hdc);
542 EndPaint(PRIVATE(data)->hWnd, &ps);
545 /******************************************************************
550 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
554 SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, pos, TRUE);
555 data->curcfg.win_pos.X = pos;
556 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
560 SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, pos, TRUE);
561 data->curcfg.win_pos.Y = pos;
563 InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
566 struct font_chooser {
567 struct inner_data* data;
571 /******************************************************************
572 * WCUSER_ValidateFontMetric
574 * Returns true if the font described in tm is usable as a font for the renderer
576 BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRIC* tm)
578 return tm->tmMaxCharWidth * data->curcfg.win_width < GetSystemMetrics(SM_CXSCREEN) &&
579 tm->tmHeight * data->curcfg.win_height < GetSystemMetrics(SM_CYSCREEN) &&
580 !tm->tmItalic && !tm->tmUnderlined && !tm->tmStruckOut;
583 /******************************************************************
584 * WCUSER_ValidateFont
586 * Returns true if the font family described in lf is usable as a font for the renderer
588 BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONT* lf)
590 return (lf->lfPitchAndFamily & 3) == FIXED_PITCH &&
591 (lf->lfPitchAndFamily & 0xF0) == FF_MODERN &&
592 lf->lfCharSet != SYMBOL_CHARSET;
595 /******************************************************************
596 * get_first_font_enum_2
597 * get_first_font_enum
599 * Helper functions to get a decent font for the renderer
601 static int CALLBACK get_first_font_enum_2(const LOGFONT* lf, const TEXTMETRIC* tm,
602 DWORD FontType, LPARAM lParam)
604 struct font_chooser* fc = (struct font_chooser*)lParam;
606 if (WCUSER_ValidateFontMetric(fc->data, tm))
608 WCUSER_SetFont(fc->data, lf);
615 static int CALLBACK get_first_font_enum(const LOGFONT* lf, const TEXTMETRIC* tm,
616 DWORD FontType, LPARAM lParam)
618 struct font_chooser* fc = (struct font_chooser*)lParam;
620 if (WCUSER_ValidateFont(fc->data, lf))
622 EnumFontFamilies(PRIVATE(fc->data)->hMemDC, lf->lfFaceName, get_first_font_enum_2, lParam);
623 return !fc->done; /* we just need the first matching one... */
628 /******************************************************************
633 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
636 HINSTANCE hInstance = GetModuleHandle(NULL);
639 if (!hMenu) return FALSE;
641 /* FIXME: error handling & memory cleanup */
642 hSubMenu = CreateMenu();
643 if (!hSubMenu) return FALSE;
645 LoadString(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(WCHAR));
646 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
647 LoadString(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(WCHAR));
648 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
649 LoadString(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(WCHAR));
650 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
651 LoadString(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(WCHAR));
652 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
653 LoadString(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(WCHAR));
654 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
655 LoadString(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(WCHAR));
656 InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
658 if (sep) InsertMenu(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
659 LoadString(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(WCHAR));
660 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
661 LoadString(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(WCHAR));
662 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
663 LoadString(hInstance, IDS_PROPERTY, buff, sizeof(buff) / sizeof(WCHAR));
664 InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTY, buff);
669 /******************************************************************
672 * Creates the window for the rendering
674 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCT lpcs)
676 struct inner_data* data;
679 data = lpcs->lpCreateParams;
680 SetWindowLong(hWnd, 0L, (DWORD)data);
681 PRIVATE(data)->hWnd = hWnd;
683 data->curcfg.cursor_size = 101; /* invalid value, will trigger a complete cleanup */
685 hSysMenu = GetSystemMenu(hWnd, FALSE);
686 if (!hSysMenu) return 0;
687 PRIVATE(data)->hPopMenu = CreatePopupMenu();
688 if (!PRIVATE(data)->hPopMenu) return 0;
690 WCUSER_FillMenu(hSysMenu, TRUE);
691 WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
693 PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
694 if (!PRIVATE(data)->hMemDC) {Trace(0, "no mem dc\n");return 0;}
696 data->curcfg.quick_edit = FALSE;
700 /******************************************************************
701 * WCUSER_SetMenuDetails
703 * Grays / ungrays the menu items according to their state
705 static void WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
707 if (!hMenu) {Trace(0, "Issue in getting menu bits\n");return;}
709 EnableMenuItem(hMenu, IDS_COPY,
710 MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
711 EnableMenuItem(hMenu, IDS_PASTE,
712 MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
713 ? MF_ENABLED : MF_GRAYED));
714 EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
715 EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
718 /******************************************************************
719 * CUSER_GetCtrlKeyState
721 * Get the console bit mask equivalent to the VK_ status in keyState
723 static DWORD WCUSER_GetCtrlKeyState(BYTE* keyState)
727 GetKeyboardState(keyState);
728 if (keyState[VK_SHIFT] & 0x80) ret |= SHIFT_PRESSED;
729 if (keyState[VK_CONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED; /* FIXME: gotta choose one */
730 if (keyState[VK_LCONTROL] & 0x80) ret |= LEFT_CTRL_PRESSED;
731 if (keyState[VK_RCONTROL] & 0x80) ret |= RIGHT_CTRL_PRESSED;
732 if (keyState[VK_LMENU] & 0x80) ret |= LEFT_ALT_PRESSED;
733 if (keyState[VK_RMENU] & 0x80) ret |= RIGHT_ALT_PRESSED;
734 if (keyState[VK_CAPITAL] & 0x01) ret |= CAPSLOCK_ON;
735 if (keyState[VK_NUMLOCK] & 0x01) ret |= NUMLOCK_ON;
736 if (keyState[VK_SCROLL] & 0x01) ret |= SCROLLLOCK_ON;
741 /******************************************************************
742 * WCUSER_HandleSelectionKey
744 * Handles keys while selecting an area
746 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
747 WPARAM wParam, LPARAM lParam)
750 DWORD state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
761 PRIVATE(data)->has_selection = FALSE;
762 WCUSER_SetSelection(data, 0);
763 WCUSER_CopySelectionToClipboard(data);
766 c1 = PRIVATE(data)->selectPt1;
767 c2 = PRIVATE(data)->selectPt2;
769 if (c1.X < data->curcfg.sb_width && c2.X < data->curcfg.sb_width)
771 WCUSER_MoveSelection(data, c1, c2, FALSE);
775 c1 = PRIVATE(data)->selectPt1;
776 c2 = PRIVATE(data)->selectPt2;
778 if (c1.X >= 0 && c2.X >= 0)
780 WCUSER_MoveSelection(data, c1, c2, FALSE);
784 c1 = PRIVATE(data)->selectPt1;
785 c2 = PRIVATE(data)->selectPt2;
787 if (c1.Y >= 0 && c2.Y >= 0)
789 WCUSER_MoveSelection(data, c1, c2, FALSE);
793 c1 = PRIVATE(data)->selectPt1;
794 c2 = PRIVATE(data)->selectPt2;
796 if (c1.X < data->curcfg.sb_height && c2.X < data->curcfg.sb_height)
798 WCUSER_MoveSelection(data, c1, c2, FALSE);
807 c1 = PRIVATE(data)->selectPt1;
808 c2 = PRIVATE(data)->selectPt2;
810 if (c2.X < data->curcfg.sb_width)
812 WCUSER_MoveSelection(data, c1, c2, FALSE);
816 c1 = PRIVATE(data)->selectPt1;
817 c2 = PRIVATE(data)->selectPt2;
821 WCUSER_MoveSelection(data, c1, c2, FALSE);
825 c1 = PRIVATE(data)->selectPt1;
826 c2 = PRIVATE(data)->selectPt2;
830 WCUSER_MoveSelection(data, c1, c2, FALSE);
834 c1 = PRIVATE(data)->selectPt1;
835 c2 = PRIVATE(data)->selectPt2;
837 if (c2.X < data->curcfg.sb_height)
839 WCUSER_MoveSelection(data, c1, c2, FALSE);
847 /******************************************************************
848 * WCUSER_GenerateKeyInputRecord
850 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
852 static void WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
853 WPARAM wParam, LPARAM lParam, BOOL sys)
858 static WCHAR last; /* keep last char seen as feed for key up message */
861 ir.EventType = KEY_EVENT;
862 ir.Event.KeyEvent.bKeyDown = down;
863 ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
864 ir.Event.KeyEvent.wVirtualKeyCode = wParam;
866 ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
868 ir.Event.KeyEvent.uChar.UnicodeChar = 0;
869 ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
870 if (lParam & (1L << 24)) ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
871 if (sys) ir.Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED; /* FIXME: gotta choose one */
873 if (!(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
877 switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
880 /* FIXME... should generate two events... */
890 ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME HACKY... and buggy 'coz it should be a stack, not a single value */
894 WriteConsoleInput(data->hConIn, &ir, 1, &n);
897 /******************************************************************
898 * WCUSER_GenerateMouseInputRecord
902 static void WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
903 WPARAM wParam, DWORD event)
909 /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
910 if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
913 ir.EventType = MOUSE_EVENT;
914 ir.Event.MouseEvent.dwMousePosition = c;
915 ir.Event.MouseEvent.dwButtonState = 0;
916 if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
917 if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
918 if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
919 ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
920 ir.Event.MouseEvent.dwEventFlags = event;
922 WriteConsoleInput(data->hConIn, &ir, 1, &n);
925 /******************************************************************
930 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
932 struct inner_data* data = (struct inner_data*)GetWindowLong(hWnd, 0);
937 return WCUSER_Create(hWnd, (LPCREATESTRUCT)lParam);
939 PRIVATE(data)->hWnd = 0;
947 if (PRIVATE(data)->has_selection)
948 WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
950 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam, FALSE);
954 WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam, TRUE);
957 if (data->curcfg.quick_edit)
959 if (PRIVATE(data)->has_selection)
961 PRIVATE(data)->has_selection = FALSE;
962 WCUSER_SetSelection(data, 0);
966 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
967 SetCapture(PRIVATE(data)->hWnd);
968 WCUSER_SetSelection(data, 0);
969 PRIVATE(data)->has_selection = TRUE;
974 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
978 if (data->curcfg.quick_edit)
980 if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
981 (wParam & MK_LBUTTON))
983 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), FALSE);
988 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
992 if (data->curcfg.quick_edit)
994 if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
995 (wParam& MK_LBUTTON))
997 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), TRUE);
1002 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1005 case WM_RBUTTONDOWN:
1006 if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1010 GetWindowRect(hWnd, &r);
1011 WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1012 TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN,
1013 r.left + LOWORD(lParam), r.top + HIWORD(lParam), 0, hWnd, NULL);
1017 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1021 /* no need to track for rbutton up when opening the popup... the event will be
1022 * swallowed by TrackPopupMenu */
1023 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1026 /* FIXME: should we scroll too ? */
1027 WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1030 if (data->curcfg.cursor_visible)
1032 CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap,
1033 data->curcfg.cell_width, data->curcfg.cell_height);
1034 WCUSER_PosCursor(data);
1038 if (data->curcfg.cursor_visible)
1043 int pos = data->curcfg.win_pos.X;
1045 switch (LOWORD(wParam))
1047 case SB_PAGEUP: pos -= 8; break;
1048 case SB_PAGEDOWN: pos += 8; break;
1049 case SB_LINEUP: pos--; break;
1050 case SB_LINEDOWN: pos++; break;
1051 case SB_THUMBTRACK: pos = HIWORD(wParam); break;
1054 if (pos < 0) pos = 0;
1055 if (pos > data->curcfg.sb_width - data->curcfg.win_width)
1056 pos = data->curcfg.sb_width - data->curcfg.win_width;
1057 if (pos != data->curcfg.win_pos.X)
1059 ScrollWindow(hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0,
1061 data->curcfg.win_pos.X = pos;
1062 SetScrollPos(hWnd, SB_HORZ, pos, TRUE);
1064 WCUSER_PosCursor(data);
1065 WINECON_NotifyWindowChange(data);
1071 int pos = data->curcfg.win_pos.Y;
1073 switch (LOWORD(wParam))
1075 case SB_PAGEUP: pos -= 8; break;
1076 case SB_PAGEDOWN: pos += 8; break;
1077 case SB_LINEUP: pos--; break;
1078 case SB_LINEDOWN: pos++; break;
1079 case SB_THUMBTRACK: pos = HIWORD(wParam); break;
1082 if (pos < 0) pos = 0;
1083 if (pos > data->curcfg.sb_height - data->curcfg.win_height)
1084 pos = data->curcfg.sb_height - data->curcfg.win_height;
1085 if (pos != data->curcfg.win_pos.Y)
1087 ScrollWindow(hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height,
1089 data->curcfg.win_pos.Y = pos;
1090 SetScrollPos(hWnd, SB_VERT, pos, TRUE);
1092 WCUSER_PosCursor(data);
1093 WINECON_NotifyWindowChange(data);
1101 WCUSER_GetProperties(data, FALSE);
1104 WCUSER_GetProperties(data, TRUE);
1107 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1114 WCUSER_GetProperties(data, FALSE);
1117 WCUSER_GetProperties(data, TRUE);
1120 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1121 PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1122 WCUSER_SetSelection(data, 0);
1123 PRIVATE(data)->has_selection = TRUE;
1126 if (PRIVATE(data)->has_selection)
1128 PRIVATE(data)->has_selection = FALSE;
1129 WCUSER_SetSelection(data, 0);
1130 WCUSER_CopySelectionToClipboard(data);
1134 WCUSER_PasteFromClipboard(data);
1137 PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1138 PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1139 PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1140 WCUSER_SetSelection(data, 0);
1141 PRIVATE(data)->has_selection = TRUE;
1145 Trace(0, "unhandled yet command: %x\n", wParam);
1148 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1151 case WM_INITMENUPOPUP:
1152 if (!HIWORD(lParam)) return DefWindowProc(hWnd, uMsg, wParam, lParam);
1153 WCUSER_SetMenuDetails(data, GetSystemMenu(PRIVATE(data)->hWnd, FALSE));
1156 return DefWindowProc(hWnd, uMsg, wParam, lParam);
1161 /******************************************************************
1162 * WCUSER_DeleteBackend
1166 void WCUSER_DeleteBackend(struct inner_data* data)
1168 if (!PRIVATE(data)) return;
1169 if (PRIVATE(data)->hWnd) DestroyWindow(PRIVATE(data)->hWnd);
1170 if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
1171 if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
1172 if (PRIVATE(data)->hMemDC) DeleteDC(PRIVATE(data)->hMemDC);
1173 if (PRIVATE(data)->hBitmap) DeleteObject(PRIVATE(data)->hBitmap);
1174 HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1177 /******************************************************************
1182 static int WCUSER_MainLoop(struct inner_data* data)
1188 switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1191 if (!WINECON_GrabChanges(data))
1194 case WAIT_OBJECT_0+1:
1195 switch (GetMessage(&msg, 0, 0, 0))
1197 case -1: /* the event handle became invalid, so exit */
1199 case 0: /* WM_QUIT has been posted */
1202 DispatchMessage(&msg);
1207 Trace(0, "got pb\n");
1214 /******************************************************************
1215 * WCUSER_InitBackend
1217 * Initialisation part II: creation of window.
1220 BOOL WCUSER_InitBackend(struct inner_data* data)
1222 static WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1226 data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1227 if (!data->private) return FALSE;
1229 data->fnMainLoop = WCUSER_MainLoop;
1230 data->fnPosCursor = WCUSER_PosCursor;
1231 data->fnShapeCursor = WCUSER_ShapeCursor;
1232 data->fnComputePositions = WCUSER_ComputePositions;
1233 data->fnRefresh = WCUSER_Refresh;
1234 data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1235 data->fnSetTitle = WCUSER_SetTitle;
1236 data->fnScroll = WCUSER_Scroll;
1237 data->fnDeleteBackend = WCUSER_DeleteBackend;
1240 wndclass.lpfnWndProc = WCUSER_Proc;
1241 wndclass.cbClsExtra = 0;
1242 wndclass.cbWndExtra = sizeof(DWORD);
1243 wndclass.hInstance = GetModuleHandle(NULL);
1244 wndclass.hIcon = LoadIcon(0, IDI_WINLOGO);
1245 wndclass.hCursor = LoadCursor(0, IDC_ARROW);
1246 wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1247 wndclass.lpszMenuName = NULL;
1248 wndclass.lpszClassName = wClassName;
1250 RegisterClass(&wndclass);
1252 CreateWindow(wndclass.lpszClassName, NULL,
1253 WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1254 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1255 if (!PRIVATE(data)->hWnd) return FALSE;
1257 /* force update of current data */
1258 if (!WINECON_GrabChanges(data)) return FALSE;
1260 if (!WCUSER_InitFont(data))
1262 struct font_chooser fc;
1263 /* try to find an acceptable font */
1266 EnumFontFamilies(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);