Removed some more trailing whitespace.
[wine] / programs / wineconsole / user.c
1 /*
2  * a GUI application for displaying a console
3  *      USER32 back end
4  * Copyright 2001 Eric Pouech
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "winecon_user.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
28 WINE_DECLARE_DEBUG_CHANNEL(wc_font);
29
30 /* mapping console colors to RGB values */
31 COLORREF        WCUSER_ColorMap[16] =
32 {
33     RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x80), RGB(0x00, 0x80, 0x00), RGB(0x00, 0x80, 0x80),
34     RGB(0x80, 0x00, 0x00), RGB(0x80, 0x00, 0x80), RGB(0x80, 0x80, 0x00), RGB(0x80, 0x80, 0x80),
35     RGB(0xC0, 0xC0, 0xC0), RGB(0x00, 0x00, 0xFF), RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0xFF),
36     RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0xFF, 0x00), RGB(0xFF, 0xFF, 0xFF),
37 };
38
39 /******************************************************************
40  *              WCUSER_FillMemDC
41  *
42  * Fills the Mem DC with current cells values
43  */
44 static void WCUSER_FillMemDC(const struct inner_data* data, int upd_tp, int upd_bm)
45 {
46     unsigned            i, j, k;
47     CHAR_INFO*          cell;
48     HFONT               hOldFont;
49     WORD                attr;
50     WCHAR*              line;
51
52     /* no font has been set up yet, don't worry about filling the bitmap,
53      * we'll do it once a font is chosen
54      */
55     if (!PRIVATE(data)->hFont) return;
56
57     if (!(line = HeapAlloc(GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(WCHAR))))
58     {WINE_ERR("OOM\n"); return;}
59
60     hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
61     for (j = upd_tp; j <= upd_bm; j++)
62     {
63         cell = &data->cells[j * data->curcfg.sb_width];
64         for (i = 0; i < data->curcfg.sb_width; i++)
65         {
66             attr = cell[i].Attributes;
67             SetBkColor(PRIVATE(data)->hMemDC, WCUSER_ColorMap[(attr>>4)&0x0F]);
68             SetTextColor(PRIVATE(data)->hMemDC, WCUSER_ColorMap[attr&0x0F]);
69             for (k = i; k < data->curcfg.sb_width && cell[k].Attributes == attr; k++)
70             {
71                 line[k - i] = cell[k].Char.UnicodeChar;
72             }
73             TextOut(PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
74                     line, k - i);
75             i = k - 1;
76         }
77     }
78     SelectObject(PRIVATE(data)->hMemDC, hOldFont);
79     HeapFree(GetProcessHeap(), 0, line);
80 }
81
82 /******************************************************************
83  *              WCUSER_NewBitmap
84  *
85  * Either the font geometry or the sb geometry has changed. we need to recreate the
86  * bitmap geometry
87  */
88 static void WCUSER_NewBitmap(struct inner_data* data, BOOL fill)
89 {
90     HDC         hDC;
91     HBITMAP     hnew, hold;
92
93     if (!data->curcfg.sb_width || !data->curcfg.sb_height ||
94         !PRIVATE(data)->hFont || !(hDC = GetDC(PRIVATE(data)->hWnd)))
95         return;
96     hnew = CreateCompatibleBitmap(hDC,
97                                   data->curcfg.sb_width  * data->curcfg.cell_width,
98                                   data->curcfg.sb_height * data->curcfg.cell_height);
99     ReleaseDC(PRIVATE(data)->hWnd, hDC);
100     hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
101
102     if (PRIVATE(data)->hBitmap)
103     {
104         if (hold == PRIVATE(data)->hBitmap)
105             DeleteObject(PRIVATE(data)->hBitmap);
106         else
107             WINE_FIXME("leak\n");
108     }
109     PRIVATE(data)->hBitmap = hnew;
110     if (fill)
111         WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
112 }
113
114 /******************************************************************
115  *              WCUSER_ResizeScreenBuffer
116  *
117  *
118  */
119 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
120 {
121     WCUSER_NewBitmap(data, FALSE);
122 }
123
124 /******************************************************************
125  *              WCUSER_PosCursor
126  *
127  * Set a new position for the cursor
128  */
129 static void     WCUSER_PosCursor(const struct inner_data* data)
130 {
131     if (PRIVATE(data)->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
132
133     SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
134                 (data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
135     ShowCaret(PRIVATE(data)->hWnd);
136 }
137
138 /******************************************************************
139  *              WCUSER_ShapeCursor
140  *
141  * Sets a new shape for the cursor
142  */
143 void    WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
144 {
145     if (force || size != data->curcfg.cursor_size)
146     {
147         if (data->curcfg.cursor_visible && PRIVATE(data)->hWnd == GetFocus()) DestroyCaret();
148         if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
149         PRIVATE(data)->cursor_bitmap = (HBITMAP)0;
150         if (size != 100)
151         {
152             int         w16b; /* number of byets per row, aligned on word size */
153             BYTE*       ptr;
154             int         i, j, nbl;
155
156             w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
157             ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
158             if (!ptr) {WINE_ERR("OOM\n"); return;}
159             nbl = max((data->curcfg.cell_height * size) / 100, 1);
160             for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
161             {
162                 for (i = 0; i < data->curcfg.cell_width; i++)
163                 {
164                     ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
165                 }
166             }
167             PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
168                                                data->curcfg.cell_height, 1, 1, ptr);
169             HeapFree(GetProcessHeap(), 0, ptr);
170         }
171         data->curcfg.cursor_size = size;
172         data->curcfg.cursor_visible = -1;
173     }
174
175     vis = (vis) ? TRUE : FALSE;
176     if (force || vis != data->curcfg.cursor_visible)
177     {
178         data->curcfg.cursor_visible = vis;
179         if (PRIVATE(data)->hWnd == GetFocus())
180         {
181             if (vis)
182             {
183                 CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap,
184                             data->curcfg.cell_width, data->curcfg.cell_height);
185                 WCUSER_PosCursor(data);
186             }
187             else
188             {
189                 DestroyCaret();
190             }
191         }
192     }
193 }
194
195 /******************************************************************
196  *              INECON_ComputePositions
197  *
198  * Recomputes all the components (mainly scroll bars) positions
199  */
200 void    WCUSER_ComputePositions(struct inner_data* data)
201 {
202     RECT                r;
203     int                 dx, dy;
204
205     /* compute window size from desired client size */
206     r.left = r.top = 0;
207     r.right = data->curcfg.win_width * data->curcfg.cell_width;
208     r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
209
210     if (IsRectEmpty(&r))
211     {
212         ShowWindow(PRIVATE(data)->hWnd, SW_HIDE);
213         return;
214     }
215
216     AdjustWindowRect(&r, GetWindowLong(PRIVATE(data)->hWnd, GWL_STYLE), FALSE);
217
218     dx = dy = 0;
219     if (data->curcfg.sb_width > data->curcfg.win_width)
220     {
221         dy = GetSystemMetrics(SM_CYHSCROLL);
222         SetScrollRange(PRIVATE(data)->hWnd, SB_HORZ, 0,
223                        data->curcfg.sb_width - data->curcfg.win_width, FALSE);
224         SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
225         ShowScrollBar(PRIVATE(data)->hWnd, SB_HORZ, TRUE);
226     }
227     else
228     {
229         ShowScrollBar(PRIVATE(data)->hWnd, SB_HORZ, FALSE);
230     }
231
232     if (data->curcfg.sb_height > data->curcfg.win_height)
233     {
234         dx = GetSystemMetrics(SM_CXVSCROLL);
235         SetScrollRange(PRIVATE(data)->hWnd, SB_VERT, 0,
236                        data->curcfg.sb_height - data->curcfg.win_height, FALSE);
237         SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, 0, FALSE); /* FIXME */
238         ShowScrollBar(PRIVATE(data)->hWnd, SB_VERT, TRUE);
239     }
240     else
241     {
242         ShowScrollBar(PRIVATE(data)->hWnd, SB_VERT, FALSE);
243     }
244
245     SetWindowPos(PRIVATE(data)->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
246                  SWP_NOMOVE|SWP_NOZORDER|SWP_SHOWWINDOW);
247     WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
248     WCUSER_PosCursor(data);
249 }
250
251 /******************************************************************
252  *              WCUSER_SetTitle
253  *
254  * Sets the title to the wine console
255  */
256 static void     WCUSER_SetTitle(const struct inner_data* data)
257 {
258     WCHAR       buffer[256];
259
260     if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
261         SetWindowText(PRIVATE(data)->hWnd, buffer);
262 }
263
264 void WCUSER_DumpLogFont(const char* pfx, const LOGFONT* lf, DWORD ft)
265 {
266     WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
267                          "\tlf.lfHeight=%ld lf.lfWidth=%ld lf.lfEscapement=%ld lf.lfOrientation=%ld\n"
268                          "\tlf.lfWeight=%ld lf.lfItalic=%u lf.lfUnderline=%u lf.lfStrikeOut=%u\n"
269                          "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
270                          "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
271                          pfx,
272                          (ft & RASTER_FONTTYPE) ? "raster" : "",
273                          (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
274                          ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
275                          (ft & DEVICE_FONTTYPE) ? "|device" : "",
276                          lf->lfHeight, lf->lfWidth, lf->lfEscapement, lf->lfOrientation,
277                          lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
278                          lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
279                          wine_dbgstr_w(lf->lfFaceName));
280 }
281
282 void WCUSER_DumpTextMetric(const TEXTMETRIC* tm, DWORD ft)
283 {
284     WINE_TRACE_(wc_font)("%s%s%s%s\n"
285                          "\ttmHeight=%ld tmAscent=%ld tmDescent=%ld tmInternalLeading=%ld tmExternalLeading=%ld\n"
286                          "\ttmAveCharWidth=%ld tmMaxCharWidth=%ld tmWeight=%ld tmOverhang=%ld\n"
287                          "\ttmDigitizedAspectX=%ld tmDigitizedAspectY=%ld\n"
288                          "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
289                          "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
290                          (ft & RASTER_FONTTYPE) ? "raster" : "",
291                          (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
292                          ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
293                          (ft & DEVICE_FONTTYPE) ? "|device" : "",
294                          tm->tmHeight, tm->tmAscent, tm->tmDescent, tm->tmInternalLeading, tm->tmExternalLeading, tm->tmAveCharWidth,
295                          tm->tmMaxCharWidth, tm->tmWeight, tm->tmOverhang, tm->tmDigitizedAspectX, tm->tmDigitizedAspectY,
296                          tm->tmFirstChar, tm->tmLastChar, tm->tmDefaultChar, tm->tmBreakChar, tm->tmItalic, tm->tmUnderlined, tm->tmStruckOut,
297                          tm->tmPitchAndFamily, tm->tmCharSet);
298 }
299
300 /******************************************************************
301  *              FontEqual
302  *
303  *
304  */
305 BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONT* lf)
306 {
307     return lf->lfHeight == config->cell_height &&
308         lf->lfWeight == config->font_weight &&
309         !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
310         !lstrcmp(lf->lfFaceName, config->face_name);
311 }
312
313 struct font_chooser
314 {
315     struct inner_data*  data;
316     int                 done;
317 };
318
319 /******************************************************************
320  *              WCUSER_ValidateFontMetric
321  *
322  * Returns true if the font described in tm is usable as a font for the renderer
323  */
324 BOOL    WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRIC* tm, DWORD fontType)
325 {
326     BOOL        ret = TRUE;
327
328     if (fontType & RASTER_FONTTYPE)
329         ret = (tm->tmMaxCharWidth * data->curcfg.win_width < GetSystemMetrics(SM_CXSCREEN) &&
330                tm->tmHeight * data->curcfg.win_height < GetSystemMetrics(SM_CYSCREEN));
331     return ret && !tm->tmItalic && !tm->tmUnderlined && !tm->tmStruckOut &&
332         (tm->tmCharSet == DEFAULT_CHARSET || tm->tmCharSet == ANSI_CHARSET);
333 }
334
335 /******************************************************************
336  *              WCUSER_ValidateFont
337  *
338  * Returns true if the font family described in lf is usable as a font for the renderer
339  */
340 BOOL    WCUSER_ValidateFont(const struct inner_data* data, const LOGFONT* lf)
341 {
342     return (lf->lfPitchAndFamily & 3) == FIXED_PITCH &&
343         /* (lf->lfPitchAndFamily & 0xF0) == FF_MODERN && */
344         (lf->lfCharSet == DEFAULT_CHARSET || lf->lfCharSet == ANSI_CHARSET);
345 }
346
347 /******************************************************************
348  *              get_first_font_enum_2
349  *              get_first_font_enum
350  *
351  * Helper functions to get a decent font for the renderer
352  */
353 static int CALLBACK get_first_font_enum_2(const LOGFONT* lf, const TEXTMETRIC* tm,
354                                           DWORD FontType, LPARAM lParam)
355 {
356     struct font_chooser*        fc = (struct font_chooser*)lParam;
357
358     WCUSER_DumpTextMetric(tm, FontType);
359     if (WCUSER_ValidateFontMetric(fc->data, tm, FontType))
360     {
361         LOGFONT mlf = *lf;
362
363         /* Use the default sizes for the font (this is needed, especially for
364          * TrueType fonts, so that we get a decent size, not the max size)
365          */
366         mlf.lfWidth  = fc->data->curcfg.cell_width;
367         mlf.lfHeight = fc->data->curcfg.cell_height;
368         if (WCUSER_SetFont(fc->data, &mlf))
369         {
370             WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
371             fc->done = 1;
372             /* since we've modified the current config with new font information,
373              * set this information as the new default.
374              */
375             fc->data->defcfg.cell_width = fc->data->curcfg.cell_width;
376             fc->data->defcfg.cell_height = fc->data->curcfg.cell_height;
377             lstrcpyW(fc->data->defcfg.face_name, fc->data->curcfg.face_name);
378             /* Force also its writing back to the registry so that we can get it
379              * the next time.
380              */
381             WINECON_RegSave(&fc->data->defcfg);
382             return 0;
383         }
384     }
385     return 1;
386 }
387
388 static int CALLBACK get_first_font_enum(const LOGFONT* lf, const TEXTMETRIC* tm,
389                                         DWORD FontType, LPARAM lParam)
390 {
391     struct font_chooser*        fc = (struct font_chooser*)lParam;
392
393     WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
394     if (WCUSER_ValidateFont(fc->data, lf))
395     {
396         EnumFontFamilies(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
397                          get_first_font_enum_2, lParam);
398         return !fc->done; /* we just need the first matching one... */
399     }
400     return 1;
401 }
402
403 /******************************************************************
404  *              CopyFont
405  *
406  * get the relevant information from the font described in lf and store them
407  * in config
408  */
409 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONT* lf)
410 {
411     TEXTMETRIC  tm;
412     HDC         hDC;
413     HFONT       hFont, hOldFont;
414     int         w, i, buf[256];
415
416     if (!(hDC = GetDC(hWnd))) return (HFONT)0;
417     if (!(hFont = CreateFontIndirect(lf))) goto err1;
418
419     hOldFont = SelectObject(hDC, hFont);
420     GetTextMetrics(hDC, &tm);
421
422     /* FIXME:
423      * the current freetype engine (at least 2.0.x with x <= 8) and its implementation
424      * in Wine don't return adequate values for fixed fonts
425      * In Windows, those fonts are expectes to return the same value for
426      *  - the average width
427      *  - the largest width
428      *  - the width of all characters in the font
429      * This isn't true in Wine. As a temporary workaound, we get as the width of the
430      * cell, the width of the first character in the font, after checking that all
431      * characters in the font have the same width (I hear paranoïa coming)
432      * when this gets fixed, the should be using tm.tmAveCharWidth or tm.tmMaxCharWidth
433      * as the cell width.
434      */
435     GetCharWidth32(hDC, tm.tmFirstChar, tm.tmFirstChar, &w);
436     for (i = tm.tmFirstChar + 1; i <= tm.tmLastChar; i += sizeof(buf) / sizeof(buf[0]))
437     {
438         int j, l;
439
440         l = min(tm.tmLastChar - i, sizeof(buf) / sizeof(buf[0]) - 1);
441         GetCharWidth32(hDC, i, i + l, buf);
442         for (j = 0; j <= l; j++)
443         {
444             if (buf[j] != w)
445             {
446                 WINE_WARN("Non uniform cell width: [%d]=%d [%d]=%d\n"
447                           "This may be caused by old freetype libraries, >= 2.0.8 is recommended\n",
448                           i + j, buf[j], tm.tmFirstChar, w);
449                 goto err;
450             }
451         }
452     }
453
454     SelectObject(hDC, hOldFont);
455     ReleaseDC(hWnd, hDC);
456
457     config->cell_width  = w;
458     config->cell_height = tm.tmHeight;
459     config->font_weight = tm.tmWeight;
460     lstrcpy(config->face_name, lf->lfFaceName);
461
462     return hFont;
463  err:
464     if (hDC && hOldFont) SelectObject(hDC, hOldFont);
465     if (hFont) DeleteObject(hFont);
466  err1:
467     if (hDC) ReleaseDC(hWnd, hDC);
468
469     return (HFONT)0;
470 }
471
472 /******************************************************************
473  *              WCUSER_FillLogFont
474  *
475  *
476  */
477 void    WCUSER_FillLogFont(LOGFONT* lf, const WCHAR* name, UINT height, UINT weight)
478 {
479     lf->lfHeight        = height;
480     lf->lfWidth         = 0;
481     lf->lfEscapement    = 0;
482     lf->lfOrientation   = 0;
483     lf->lfWeight        = weight;
484     lf->lfItalic        = FALSE;
485     lf->lfUnderline     = FALSE;
486     lf->lfStrikeOut     = FALSE;
487     lf->lfCharSet       = DEFAULT_CHARSET;
488     lf->lfOutPrecision  = OUT_DEFAULT_PRECIS;
489     lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
490     lf->lfQuality       = DEFAULT_QUALITY;
491     lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
492     lstrcpy(lf->lfFaceName, name);
493 }
494
495 /******************************************************************
496  *              WCUSER_SetFont
497  *
498  * sets logfont as the new font for the console
499  */
500 BOOL    WCUSER_SetFont(struct inner_data* data, const LOGFONT* logfont)
501 {
502     HFONT       hFont;
503
504     if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
505         return TRUE;
506
507     hFont = WCUSER_CopyFont(&data->curcfg, PRIVATE(data)->hWnd, logfont);
508     if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}
509
510     if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
511     PRIVATE(data)->hFont = hFont;
512
513     WCUSER_ComputePositions(data);
514     WCUSER_NewBitmap(data, TRUE);
515     InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
516     UpdateWindow(PRIVATE(data)->hWnd);
517
518     return TRUE;
519 }
520
521 /******************************************************************
522  *              WCUSER_InitFont
523  *
524  * create a hFont from the settings saved in registry...
525  * (called on init, assuming no font has been created before)
526  */
527 static BOOL     WCUSER_InitFont(struct inner_data* data)
528 {
529     struct font_chooser fc;
530
531     WINE_TRACE_(wc_font)("=> %s\n", wine_dbgstr_wn(data->curcfg.face_name, -1));
532     if (data->curcfg.face_name[0] != '\0' &&
533         data->curcfg.cell_height != 0 &&
534         data->curcfg.font_weight != 0)
535     {
536         LOGFONT             lf;
537
538         WCUSER_FillLogFont(&lf, data->curcfg.face_name,
539                            data->curcfg.cell_height, data->curcfg.font_weight);
540         if (PRIVATE(data)->hFont != 0) WINE_FIXME("Oh strange\n");
541
542         if (WCUSER_SetFont(data, &lf))
543         {
544             WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
545             return TRUE;
546         }
547     }
548
549     /* try to find an acceptable font */
550     WINE_WARN("Couldn't match the font from registry... trying to find one\n");
551     fc.data = data;
552     fc.done = 0;
553     EnumFontFamilies(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
554     if (!fc.done) WINE_WARN("Couldn't find a decent font, aborting\n");
555     return fc.done;
556 }
557
558 /******************************************************************
559  *              WCUSER_GetCell
560  *
561  * Get a cell from the a relative coordinate in window (takes into
562  * account the scrolling)
563  */
564 static COORD    WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
565 {
566     COORD       c;
567
568     c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
569     c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
570
571     return c;
572 }
573
574 /******************************************************************
575  *              WCUSER_GetSelectionRect
576  *
577  * Get the selection rectangle
578  */
579 static void     WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
580 {
581     r->left   = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X)    ) * data->curcfg.cell_width;
582     r->top    = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y)    ) * data->curcfg.cell_height;
583     r->right  = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1) * data->curcfg.cell_width;
584     r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1) * data->curcfg.cell_height;
585 }
586
587 /******************************************************************
588  *              WCUSER_SetSelection
589  *
590  *
591  */
592 static void     WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
593 {
594     HDC         hDC;
595     RECT        r;
596
597     WCUSER_GetSelectionRect(data, &r);
598     hDC = hRefDC ? hRefDC : GetDC(PRIVATE(data)->hWnd);
599     if (hDC)
600     {
601         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
602             HideCaret(PRIVATE(data)->hWnd);
603         InvertRect(hDC, &r);
604         if (hDC != hRefDC)
605             ReleaseDC(PRIVATE(data)->hWnd, hDC);
606         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
607             ShowCaret(PRIVATE(data)->hWnd);
608     }
609 }
610
611 /******************************************************************
612  *              WCUSER_MoveSelection
613  *
614  *
615  */
616 static void     WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2, BOOL final)
617 {
618     RECT        r;
619     HDC         hDC;
620
621     WCUSER_GetSelectionRect(data, &r);
622     hDC = GetDC(PRIVATE(data)->hWnd);
623     if (hDC)
624     {
625         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
626             HideCaret(PRIVATE(data)->hWnd);
627         InvertRect(hDC, &r);
628     }
629     PRIVATE(data)->selectPt1 = c1;
630     PRIVATE(data)->selectPt2 = c2;
631     if (hDC)
632     {
633         WCUSER_GetSelectionRect(data, &r);
634         InvertRect(hDC, &r);
635         ReleaseDC(PRIVATE(data)->hWnd, hDC);
636         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
637             ShowCaret(PRIVATE(data)->hWnd);
638     }
639     if (final)
640     {
641         ReleaseCapture();
642         PRIVATE(data)->has_selection = TRUE;
643     }
644 }
645
646 /******************************************************************
647  *              WCUSER_CopySelectionToClipboard
648  *
649  * Copies the current selection into the clipboard
650  */
651 static void     WCUSER_CopySelectionToClipboard(const struct inner_data* data)
652 {
653     HANDLE      hMem;
654     LPWSTR      p;
655     unsigned    w, h;
656
657     w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
658     h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
659
660     if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
661     EmptyClipboard();
662
663     hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h - 1) * sizeof(WCHAR));
664     if (hMem && (p = GlobalLock(hMem)))
665     {
666         COORD   c;
667         int     y;
668
669         c.X = data->curcfg.win_pos.X + min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
670         c.Y = data->curcfg.win_pos.Y + min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
671
672         for (y = 0; y < h; y++, c.Y++)
673         {
674             ReadConsoleOutputCharacter(data->hConOut, &p[y * w], w - 1, c, NULL);
675             if (y < h - 1) p[y * w + w - 1] = '\n';
676         }
677         GlobalUnlock(hMem);
678         SetClipboardData(CF_UNICODETEXT, hMem);
679     }
680     CloseClipboard();
681 }
682
683 /******************************************************************
684  *              WCUSER_PasteFromClipboard
685  *
686  *
687  */
688 static void     WCUSER_PasteFromClipboard(struct inner_data* data)
689 {
690     HANDLE      h;
691     WCHAR*      ptr;
692
693     if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
694     h = GetClipboardData(CF_UNICODETEXT);
695     if (h && (ptr = GlobalLock(h)))
696     {
697         int             i, len = GlobalSize(h) / sizeof(WCHAR);
698         INPUT_RECORD    ir[2];
699         DWORD           n;
700         SHORT           sh;
701
702         ir[0].EventType = KEY_EVENT;
703         ir[0].Event.KeyEvent.wRepeatCount = 0;
704         ir[0].Event.KeyEvent.dwControlKeyState = 0;
705         ir[0].Event.KeyEvent.bKeyDown = TRUE;
706
707         /* generate the corresponding input records */
708         for (i = 0; i < len; i++)
709         {
710             /* FIXME: the modifying keys are not generated (shift, ctrl...) */
711             sh = VkKeyScan(ptr[i]);
712             ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
713             ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(LOBYTE(sh), 0);
714             ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
715
716             ir[1] = ir[0];
717             ir[1].Event.KeyEvent.bKeyDown = FALSE;
718
719             WriteConsoleInput(data->hConIn, ir, 2, &n);
720         }
721         GlobalUnlock(h);
722     }
723     CloseClipboard();
724 }
725
726 /******************************************************************
727  *              Refresh
728  *
729  *
730  */
731 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
732 {
733     WCUSER_FillMemDC(data, tp, bm);
734     if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
735     {
736         RECT    r;
737
738         r.left   = 0;
739         r.right  = data->curcfg.win_width * data->curcfg.cell_width;
740         r.top    = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
741         r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
742         InvalidateRect(PRIVATE(data)->hWnd, &r, FALSE);
743         UpdateWindow(PRIVATE(data)->hWnd);
744     }
745 }
746
747 /******************************************************************
748  *              WCUSER_Paint
749  *
750  *
751  */
752 static void     WCUSER_Paint(const struct inner_data* data)
753 {
754     PAINTSTRUCT         ps;
755
756     BeginPaint(PRIVATE(data)->hWnd, &ps);
757     BitBlt(ps.hdc, 0, 0,
758            data->curcfg.win_width * data->curcfg.cell_width,
759            data->curcfg.win_height * data->curcfg.cell_height,
760            PRIVATE(data)->hMemDC,
761            data->curcfg.win_pos.X * data->curcfg.cell_width,
762            data->curcfg.win_pos.Y * data->curcfg.cell_height,
763            SRCCOPY);
764     if (PRIVATE(data)->has_selection)
765         WCUSER_SetSelection(data, ps.hdc);
766     EndPaint(PRIVATE(data)->hWnd, &ps);
767 }
768
769 /******************************************************************
770  *              WCUSER_Scroll
771  *
772  *
773  */
774 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
775 {
776     if (horz)
777     {
778         SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, pos, TRUE);
779         data->curcfg.win_pos.X = pos;
780     }
781     else
782     {
783         SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, pos, TRUE);
784         data->curcfg.win_pos.Y = pos;
785     }
786     InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
787 }
788
789 /******************************************************************
790  *              WCUSER_FillMenu
791  *
792  *
793  */
794 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
795 {
796     HMENU               hSubMenu;
797     HINSTANCE           hInstance = GetModuleHandle(NULL);
798     WCHAR               buff[256];
799
800     if (!hMenu) return FALSE;
801
802     /* FIXME: error handling & memory cleanup */
803     hSubMenu = CreateMenu();
804     if (!hSubMenu) return FALSE;
805
806     LoadString(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(WCHAR));
807     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
808     LoadString(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(WCHAR));
809     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
810     LoadString(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(WCHAR));
811     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
812     LoadString(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(WCHAR));
813     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
814     LoadString(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(WCHAR));
815     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
816     LoadString(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(WCHAR));
817     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
818
819     if (sep) InsertMenu(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
820     LoadString(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(WCHAR));
821     InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
822     LoadString(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(WCHAR));
823     InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
824     LoadString(hInstance, IDS_PROPERTY, buff, sizeof(buff) / sizeof(WCHAR));
825     InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTY, buff);
826
827     return TRUE;
828 }
829
830 /******************************************************************
831  *              WCUSER_Create
832  *
833  * Creates the window for the rendering
834  */
835 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCT lpcs)
836 {
837     struct inner_data*  data;
838     HMENU               hSysMenu;
839
840     data = lpcs->lpCreateParams;
841     SetWindowLong(hWnd, 0L, (DWORD)data);
842     PRIVATE(data)->hWnd = hWnd;
843
844     data->curcfg.cursor_size = 101; /* invalid value, will trigger a complete cleanup */
845
846     hSysMenu = GetSystemMenu(hWnd, FALSE);
847     if (!hSysMenu) return 0;
848     PRIVATE(data)->hPopMenu = CreatePopupMenu();
849     if (!PRIVATE(data)->hPopMenu) return 0;
850
851     WCUSER_FillMenu(hSysMenu, TRUE);
852     WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
853
854     PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
855     if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
856
857     data->curcfg.quick_edit = FALSE;
858     return 0;
859 }
860
861 /******************************************************************
862  *              WCUSER_SetMenuDetails
863  *
864  * Grays / ungrays the menu items according to their state
865  */
866 static void     WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
867 {
868     if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}
869
870     EnableMenuItem(hMenu, IDS_COPY,
871                    MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
872     EnableMenuItem(hMenu, IDS_PASTE,
873                    MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
874                                  ? MF_ENABLED : MF_GRAYED));
875     EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
876     EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
877 }
878
879 /******************************************************************
880  *              CUSER_GetCtrlKeyState
881  *
882  * Get the console bit mask equivalent to the VK_ status in keyState
883  */
884 static DWORD    WCUSER_GetCtrlKeyState(BYTE* keyState)
885 {
886     DWORD               ret = 0;
887
888     GetKeyboardState(keyState);
889     if (keyState[VK_SHIFT]    & 0x80)   ret |= SHIFT_PRESSED;
890     if (keyState[VK_CONTROL]  & 0x80)   ret |= LEFT_CTRL_PRESSED; /* FIXME: gotta choose one */
891     if (keyState[VK_LCONTROL] & 0x80)   ret |= LEFT_CTRL_PRESSED;
892     if (keyState[VK_RCONTROL] & 0x80)   ret |= RIGHT_CTRL_PRESSED;
893     if (keyState[VK_LMENU]    & 0x80)   ret |= LEFT_ALT_PRESSED;
894     if (keyState[VK_RMENU]    & 0x80)   ret |= RIGHT_ALT_PRESSED;
895     if (keyState[VK_CAPITAL]  & 0x01)   ret |= CAPSLOCK_ON;
896     if (keyState[VK_NUMLOCK]  & 0x01)   ret |= NUMLOCK_ON;
897     if (keyState[VK_SCROLL]   & 0x01)   ret |= SCROLLLOCK_ON;
898
899     return ret;
900 }
901
902 /******************************************************************
903  *              WCUSER_HandleSelectionKey
904  *
905  * Handles keys while selecting an area
906  */
907 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
908                                       WPARAM wParam, LPARAM lParam)
909 {
910     BYTE        keyState[256];
911     DWORD       state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
912     COORD       c1, c2;
913
914     if (down) return;
915
916     switch (state)
917     {
918     case 0:
919         switch (wParam)
920         {
921         case VK_RETURN:
922             PRIVATE(data)->has_selection = FALSE;
923             WCUSER_SetSelection(data, 0);
924             WCUSER_CopySelectionToClipboard(data);
925             break;
926         case VK_RIGHT:
927             c1 = PRIVATE(data)->selectPt1;
928             c2 = PRIVATE(data)->selectPt2;
929             c1.X++; c2.X++;
930             if (c1.X < data->curcfg.sb_width && c2.X < data->curcfg.sb_width)
931             {
932                 WCUSER_MoveSelection(data, c1, c2, FALSE);
933             }
934             break;
935         case VK_LEFT:
936             c1 = PRIVATE(data)->selectPt1;
937             c2 = PRIVATE(data)->selectPt2;
938             c1.X--; c2.X--;
939             if (c1.X >= 0 && c2.X >= 0)
940             {
941                 WCUSER_MoveSelection(data, c1, c2, FALSE);
942             }
943             break;
944         case VK_UP:
945             c1 = PRIVATE(data)->selectPt1;
946             c2 = PRIVATE(data)->selectPt2;
947             c1.Y--; c2.Y--;
948             if (c1.Y >= 0 && c2.Y >= 0)
949             {
950                 WCUSER_MoveSelection(data, c1, c2, FALSE);
951             }
952             break;
953         case VK_DOWN:
954             c1 = PRIVATE(data)->selectPt1;
955             c2 = PRIVATE(data)->selectPt2;
956             c1.Y++; c2.Y++;
957             if (c1.X < data->curcfg.sb_height && c2.X < data->curcfg.sb_height)
958             {
959                 WCUSER_MoveSelection(data, c1, c2, FALSE);
960             }
961             break;
962         }
963         break;
964     case SHIFT_PRESSED:
965         switch (wParam)
966         {
967         case VK_RIGHT:
968             c1 = PRIVATE(data)->selectPt1;
969             c2 = PRIVATE(data)->selectPt2;
970             c2.X++;
971             if (c2.X < data->curcfg.sb_width)
972             {
973                 WCUSER_MoveSelection(data, c1, c2, FALSE);
974             }
975             break;
976         case VK_LEFT:
977             c1 = PRIVATE(data)->selectPt1;
978             c2 = PRIVATE(data)->selectPt2;
979             c2.X--;
980             if (c2.X >= c1.X)
981             {
982                 WCUSER_MoveSelection(data, c1, c2, FALSE);
983             }
984             break;
985         case VK_UP:
986             c1 = PRIVATE(data)->selectPt1;
987             c2 = PRIVATE(data)->selectPt2;
988             c2.Y--;
989             if (c2.Y >= c1.Y)
990             {
991                 WCUSER_MoveSelection(data, c1, c2, FALSE);
992             }
993             break;
994         case VK_DOWN:
995             c1 = PRIVATE(data)->selectPt1;
996             c2 = PRIVATE(data)->selectPt2;
997             c2.Y++;
998             if (c2.X < data->curcfg.sb_height)
999             {
1000                 WCUSER_MoveSelection(data, c1, c2, FALSE);
1001             }
1002             break;
1003         }
1004         break;
1005     }
1006 }
1007
1008 /******************************************************************
1009  *              WCUSER_GenerateKeyInputRecord
1010  *
1011  * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
1012  */
1013 static void    WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1014                                              WPARAM wParam, LPARAM lParam, BOOL sys)
1015 {
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];
1021
1022     ir.EventType = KEY_EVENT;
1023     ir.Event.KeyEvent.bKeyDown = down;
1024     ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
1025     ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1026
1027     ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1028
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;
1032     if (sys)                            ir.Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED; /* FIXME: gotta choose one */
1033
1034     if (!(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
1035     {
1036         if (down)
1037         {
1038             switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
1039             {
1040             case 2:
1041                 /* FIXME... should generate two events... */
1042                 /* fall thru */
1043             case 1:
1044                 last = buf[0];
1045                 break;
1046             default:
1047                 last = 0;
1048                 break;
1049             }
1050         }
1051         ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME HACKY... and buggy 'coz it should be a stack, not a single value */
1052         if (!down) last = 0;
1053     }
1054
1055     WriteConsoleInput(data->hConIn, &ir, 1, &n);
1056 }
1057
1058 /******************************************************************
1059  *              WCUSER_GenerateMouseInputRecord
1060  *
1061  *
1062  */
1063 static void    WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1064                                                WPARAM wParam, DWORD event)
1065 {
1066     INPUT_RECORD        ir;
1067     BYTE                keyState[256];
1068     DWORD               mode, n;
1069
1070     /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
1071     if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
1072         return;
1073
1074     ir.EventType = MOUSE_EVENT;
1075     ir.Event.MouseEvent.dwMousePosition = c;
1076     ir.Event.MouseEvent.dwButtonState = 0;
1077     if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
1078     if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
1079     if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1080     ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1081     ir.Event.MouseEvent.dwEventFlags = event;
1082
1083     WriteConsoleInput(data->hConIn, &ir, 1, &n);
1084 }
1085
1086 /******************************************************************
1087  *              WCUSER_Proc
1088  *
1089  *
1090  */
1091 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1092 {
1093     struct inner_data*  data = (struct inner_data*)GetWindowLong(hWnd, 0);
1094
1095     switch (uMsg)
1096     {
1097     case WM_CREATE:
1098         return WCUSER_Create(hWnd, (LPCREATESTRUCT)lParam);
1099     case WM_DESTROY:
1100         PRIVATE(data)->hWnd = 0;
1101         PostQuitMessage(0);
1102         break;
1103     case WM_PAINT:
1104         WCUSER_Paint(data);
1105         break;
1106     case WM_KEYDOWN:
1107     case WM_KEYUP:
1108         if (PRIVATE(data)->has_selection)
1109             WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1110         else
1111             WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam, FALSE);
1112         break;
1113     case WM_SYSKEYDOWN:
1114     case WM_SYSKEYUP:
1115         WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam, TRUE);
1116         break;
1117     case WM_LBUTTONDOWN:
1118         if (data->curcfg.quick_edit)
1119         {
1120             if (PRIVATE(data)->has_selection)
1121             {
1122                 PRIVATE(data)->has_selection = FALSE;
1123                 WCUSER_SetSelection(data, 0);
1124             }
1125             else
1126             {
1127                 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1128                 SetCapture(PRIVATE(data)->hWnd);
1129                 WCUSER_SetSelection(data, 0);
1130                 PRIVATE(data)->has_selection = TRUE;
1131             }
1132         }
1133         else
1134         {
1135             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1136         }
1137         break;
1138     case WM_MOUSEMOVE:
1139         if (data->curcfg.quick_edit)
1140         {
1141             if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
1142                 (wParam & MK_LBUTTON))
1143             {
1144                 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), FALSE);
1145             }
1146         }
1147         else
1148         {
1149             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1150         }
1151         break;
1152     case WM_LBUTTONUP:
1153         if (data->curcfg.quick_edit)
1154         {
1155             if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
1156                 (wParam& MK_LBUTTON))
1157             {
1158                 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), TRUE);
1159             }
1160         }
1161         else
1162         {
1163             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1164         }
1165         break;
1166     case WM_RBUTTONDOWN:
1167         if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1168         {
1169             RECT        r;
1170
1171             GetWindowRect(hWnd, &r);
1172             WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1173             TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN,
1174                            r.left + LOWORD(lParam), r.top + HIWORD(lParam), 0, hWnd, NULL);
1175         }
1176         else
1177         {
1178             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1179         }
1180         break;
1181     case WM_RBUTTONUP:
1182         /* no need to track for rbutton up when opening the popup... the event will be
1183          * swallowed by TrackPopupMenu */
1184         WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1185         break;
1186     case WM_MOUSEWHEEL:
1187         /* FIXME: should we scroll too ? */
1188         WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1189         break;
1190     case WM_SETFOCUS:
1191         if (data->curcfg.cursor_visible)
1192         {
1193             CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap,
1194                         data->curcfg.cell_width, data->curcfg.cell_height);
1195             WCUSER_PosCursor(data);
1196         }
1197         break;
1198     case WM_KILLFOCUS:
1199         if (data->curcfg.cursor_visible)
1200             DestroyCaret();
1201         break;
1202     case WM_HSCROLL:
1203         {
1204             int pos = data->curcfg.win_pos.X;
1205
1206             switch (LOWORD(wParam))
1207             {
1208             case SB_PAGEUP:     pos -= 8;               break;
1209             case SB_PAGEDOWN:   pos += 8;               break;
1210             case SB_LINEUP:     pos--;                  break;
1211             case SB_LINEDOWN:   pos++;                  break;
1212             case SB_THUMBTRACK: pos = HIWORD(wParam);   break;
1213             default:                                    break;
1214             }
1215             if (pos < 0) pos = 0;
1216             if (pos > data->curcfg.sb_width - data->curcfg.win_width)
1217                 pos = data->curcfg.sb_width - data->curcfg.win_width;
1218             if (pos != data->curcfg.win_pos.X)
1219             {
1220                 ScrollWindow(hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0,
1221                              NULL, NULL);
1222                 data->curcfg.win_pos.X = pos;
1223                 SetScrollPos(hWnd, SB_HORZ, pos, TRUE);
1224                 UpdateWindow(hWnd);
1225                 WCUSER_PosCursor(data);
1226                 WINECON_NotifyWindowChange(data);
1227             }
1228         }
1229         break;
1230     case WM_VSCROLL:
1231         {
1232             int pos = data->curcfg.win_pos.Y;
1233
1234             switch (LOWORD(wParam))
1235             {
1236             case SB_PAGEUP:     pos -= 8;               break;
1237             case SB_PAGEDOWN:   pos += 8;               break;
1238             case SB_LINEUP:     pos--;                  break;
1239             case SB_LINEDOWN:   pos++;                  break;
1240             case SB_THUMBTRACK: pos = HIWORD(wParam);   break;
1241             default:                                    break;
1242             }
1243             if (pos < 0) pos = 0;
1244             if (pos > data->curcfg.sb_height - data->curcfg.win_height)
1245                 pos = data->curcfg.sb_height - data->curcfg.win_height;
1246             if (pos != data->curcfg.win_pos.Y)
1247             {
1248                 ScrollWindow(hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height,
1249                              NULL, NULL);
1250                 data->curcfg.win_pos.Y = pos;
1251                 SetScrollPos(hWnd, SB_VERT, pos, TRUE);
1252                 UpdateWindow(hWnd);
1253                 WCUSER_PosCursor(data);
1254                 WINECON_NotifyWindowChange(data);
1255             }
1256
1257         }
1258     case WM_SYSCOMMAND:
1259         switch (wParam)
1260         {
1261         case IDS_DEFAULT:
1262             WCUSER_GetProperties(data, FALSE);
1263             break;
1264         case IDS_PROPERTY:
1265             WCUSER_GetProperties(data, TRUE);
1266             break;
1267         default:
1268             return DefWindowProc(hWnd, uMsg, wParam, lParam);
1269         }
1270         break;
1271     case WM_COMMAND:
1272         switch (wParam)
1273         {
1274         case IDS_DEFAULT:
1275             WCUSER_GetProperties(data, FALSE);
1276             break;
1277         case IDS_PROPERTY:
1278             WCUSER_GetProperties(data, TRUE);
1279             break;
1280         case IDS_MARK:
1281             PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1282             PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1283             WCUSER_SetSelection(data, 0);
1284             PRIVATE(data)->has_selection = TRUE;
1285             break;
1286         case IDS_COPY:
1287             if (PRIVATE(data)->has_selection)
1288             {
1289                 PRIVATE(data)->has_selection = FALSE;
1290                 WCUSER_SetSelection(data, 0);
1291                 WCUSER_CopySelectionToClipboard(data);
1292             }
1293             break;
1294         case IDS_PASTE:
1295             WCUSER_PasteFromClipboard(data);
1296             break;
1297         case IDS_SELECTALL:
1298             PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1299             PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1300             PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1301             WCUSER_SetSelection(data, 0);
1302             PRIVATE(data)->has_selection = TRUE;
1303             break;
1304         case IDS_SCROLL:
1305         case IDS_SEARCH:
1306             WINE_FIXME("Unhandled yet command: %x\n", wParam);
1307             break;
1308         default:
1309             return DefWindowProc(hWnd, uMsg, wParam, lParam);
1310         }
1311         break;
1312     case WM_INITMENUPOPUP:
1313         if (!HIWORD(lParam))    return DefWindowProc(hWnd, uMsg, wParam, lParam);
1314         WCUSER_SetMenuDetails(data, GetSystemMenu(PRIVATE(data)->hWnd, FALSE));
1315         break;
1316     default:
1317         return DefWindowProc(hWnd, uMsg, wParam, lParam);
1318     }
1319     return 0;
1320 }
1321
1322 /******************************************************************
1323  *              WCUSER_DeleteBackend
1324  *
1325  *
1326  */
1327 void WCUSER_DeleteBackend(struct inner_data* data)
1328 {
1329     if (!PRIVATE(data)) return;
1330     if (PRIVATE(data)->hMemDC)          DeleteDC(PRIVATE(data)->hMemDC);
1331     if (PRIVATE(data)->hWnd)            DestroyWindow(PRIVATE(data)->hWnd);
1332     if (PRIVATE(data)->hFont)           DeleteObject(PRIVATE(data)->hFont);
1333     if (PRIVATE(data)->cursor_bitmap)   DeleteObject(PRIVATE(data)->cursor_bitmap);
1334     if (PRIVATE(data)->hBitmap)         DeleteObject(PRIVATE(data)->hBitmap);
1335     HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1336 }
1337
1338 /******************************************************************
1339  *              WCUSER_MainLoop
1340  *
1341  *
1342  */
1343 static int WCUSER_MainLoop(struct inner_data* data)
1344 {
1345     MSG         msg;
1346
1347     for (;;)
1348     {
1349         switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1350         {
1351         case WAIT_OBJECT_0:
1352             if (!WINECON_GrabChanges(data) && data->curcfg.exit_on_die)
1353                 PostQuitMessage(0);
1354             break;
1355         case WAIT_OBJECT_0+1:
1356             switch (GetMessage(&msg, 0, 0, 0))
1357             {
1358             case -1: /* the event handle became invalid, so exit */
1359                 return -1;
1360             case 0: /* WM_QUIT has been posted */
1361                 return 0;
1362             default:
1363                 DispatchMessage(&msg);
1364                 break;
1365             }
1366             break;
1367         default:
1368             WINE_ERR("got pb\n");
1369             /* err */
1370             break;
1371         }
1372     }
1373 }
1374
1375 /******************************************************************
1376  *              WCUSER_InitBackend
1377  *
1378  * Initialisation part II: creation of window.
1379  *
1380  */
1381 BOOL WCUSER_InitBackend(struct inner_data* data)
1382 {
1383     static WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1384
1385     WNDCLASS            wndclass;
1386
1387     data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1388     if (!data->private) return FALSE;
1389
1390     data->fnMainLoop = WCUSER_MainLoop;
1391     data->fnPosCursor = WCUSER_PosCursor;
1392     data->fnShapeCursor = WCUSER_ShapeCursor;
1393     data->fnComputePositions = WCUSER_ComputePositions;
1394     data->fnRefresh = WCUSER_Refresh;
1395     data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1396     data->fnSetTitle = WCUSER_SetTitle;
1397     data->fnScroll = WCUSER_Scroll;
1398     data->fnDeleteBackend = WCUSER_DeleteBackend;
1399
1400     wndclass.style         = 0;
1401     wndclass.lpfnWndProc   = WCUSER_Proc;
1402     wndclass.cbClsExtra    = 0;
1403     wndclass.cbWndExtra    = sizeof(DWORD);
1404     wndclass.hInstance     = GetModuleHandle(NULL);
1405     wndclass.hIcon         = LoadIcon(0, IDI_WINLOGO);
1406     wndclass.hCursor       = LoadCursor(0, IDC_ARROW);
1407     wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1408     wndclass.lpszMenuName  = NULL;
1409     wndclass.lpszClassName = wClassName;
1410
1411     RegisterClass(&wndclass);
1412
1413     CreateWindow(wndclass.lpszClassName, NULL,
1414                  WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
1415                  CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1416     if (!PRIVATE(data)->hWnd) return FALSE;
1417
1418     /* force update of current data */
1419     if (!WINECON_GrabChanges(data)) return FALSE;
1420
1421     if (!WCUSER_InitFont(data)) return FALSE;
1422
1423     return TRUE;
1424 }
1425