Wineconsole is now able to handle non raster fonts (vector, true
[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 /* mapping console colors to RGB values */
26 COLORREF        WCUSER_ColorMap[16] = 
27 {
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),
32 };
33
34 /******************************************************************
35  *              WCUSER_FillMemDC
36  *
37  * Fills the Mem DC with current cells values
38  */
39 static void WCUSER_FillMemDC(const struct inner_data* data, int upd_tp, int upd_bm)
40 {
41     unsigned            i, j, k;
42     CHAR_INFO*          cell;
43     HFONT               hOldFont;
44     WORD                attr;
45     WCHAR*              line;
46
47     if (!(line = HeapAlloc(GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(WCHAR))))
48     {Trace(0, "OOM\n"); return;}
49
50     hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
51     for (j = upd_tp; j <= upd_bm; j++)
52     {
53         cell = &data->cells[j * data->curcfg.sb_width];
54         for (i = 0; i < data->curcfg.win_width; i++)
55         {
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++)
60             {
61                 line[k - i] = cell[k].Char.UnicodeChar;
62             }
63             TextOut(PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height, 
64                     line, k - i);
65             i = k - 1;
66         }
67     }
68     SelectObject(PRIVATE(data)->hMemDC, hOldFont);
69     HeapFree(GetProcessHeap(), 0, line);
70 }
71
72 /******************************************************************
73  *              WCUSER_NewBitmap
74  *
75  * Either the font geometry or the sb geometry has changed. we need to recreate the
76  * bitmap geometry
77  */
78 static void WCUSER_NewBitmap(struct inner_data* data, BOOL fill)
79 {
80     HDC         hDC;
81     HBITMAP     hnew, hold;
82
83     if (!data->curcfg.sb_width || !data->curcfg.sb_height || !(hDC = GetDC(PRIVATE(data)->hWnd))) 
84         return;
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);
90
91     if (PRIVATE(data)->hBitmap)
92     {
93         if (hold == PRIVATE(data)->hBitmap)
94             DeleteObject(PRIVATE(data)->hBitmap);
95         else
96             Trace(0, "leak\n");
97     }
98     PRIVATE(data)->hBitmap = hnew;
99     if (fill)
100         WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
101 }
102
103 /******************************************************************
104  *              WCUSER_ResizeScreenBuffer
105  *
106  *
107  */
108 static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
109 {
110     WCUSER_NewBitmap(data, FALSE);
111 }
112
113 /******************************************************************
114  *              WCUSER_PosCursor
115  *
116  * Set a new position for the cursor
117  */
118 static void     WCUSER_PosCursor(const struct inner_data* data)
119 {
120     if (PRIVATE(data)->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
121
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); 
125 }
126
127 /******************************************************************
128  *              WCUSER_ShapeCursor
129  *
130  * Sets a new shape for the cursor
131  */
132 void    WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
133 {
134     if (force || size != data->curcfg.cursor_size)
135     {
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;
139         if (size != 100)
140         {
141             int         w16b; /* number of byets per row, aligned on word size */
142             BYTE*       ptr;
143             int         i, j, nbl;
144
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++)
150             {
151                 for (i = 0; i < data->curcfg.cell_width; i++)
152                 {
153                     ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
154                 }
155             }
156             PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width, 
157                                                data->curcfg.cell_height, 1, 1, ptr);
158             HeapFree(GetProcessHeap(), 0, ptr);
159         }
160         data->curcfg.cursor_size = size;
161         data->curcfg.cursor_visible = -1;
162     }
163
164     vis = (vis) ? TRUE : FALSE;
165     if (force || vis != data->curcfg.cursor_visible)
166     {
167         data->curcfg.cursor_visible = vis;
168         if (PRIVATE(data)->hWnd == GetFocus())
169         {
170             if (vis)
171             {
172                 CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap, 
173                             data->curcfg.cell_width, data->curcfg.cell_height);
174                 WCUSER_PosCursor(data);
175             }
176             else
177             {
178                 DestroyCaret();
179             }
180         }
181     }
182 }
183
184 /******************************************************************
185  *              INECON_ComputePositions
186  *
187  * Recomputes all the components (mainly scroll bars) positions
188  */
189 void    WCUSER_ComputePositions(struct inner_data* data)
190 {
191     RECT                r;
192     int                 dx, dy;
193
194     /* compute window size from desired client size */
195     r.left = r.top = 0;
196     r.right = data->curcfg.win_width * data->curcfg.cell_width;
197     r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
198
199     if (IsRectEmpty(&r))
200     {
201         ShowWindow(PRIVATE(data)->hWnd, SW_HIDE);
202         return;
203     }
204
205     AdjustWindowRect(&r, GetWindowLong(PRIVATE(data)->hWnd, GWL_STYLE), FALSE);
206
207     dx = dy = 0;
208     if (data->curcfg.sb_width > data->curcfg.win_width)
209     {
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);
215     }
216     else
217     {
218         ShowScrollBar(PRIVATE(data)->hWnd, SB_HORZ, FALSE);
219     }
220
221     if (data->curcfg.sb_height > data->curcfg.win_height)
222     {
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);
228     }   
229     else
230     {
231         ShowScrollBar(PRIVATE(data)->hWnd, SB_VERT, FALSE);
232     }
233
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);
238 }
239
240 /******************************************************************
241  *              WCUSER_SetTitle
242  *
243  * Sets the title to the wine console
244  */
245 static void     WCUSER_SetTitle(const struct inner_data* data)
246 {
247     WCHAR       buffer[256];    
248
249     if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
250         SetWindowText(PRIVATE(data)->hWnd, buffer);
251 }
252
253 /******************************************************************
254  *              FontEqual
255  *
256  *
257  */
258 BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONT* lf)
259 {
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);
264 }
265
266 struct font_chooser {
267     struct inner_data*  data;
268     int                 done;
269 };
270
271 /******************************************************************
272  *              WCUSER_ValidateFontMetric
273  *
274  * Returns true if the font described in tm is usable as a font for the renderer
275  */
276 BOOL    WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRIC* tm, DWORD fontType)
277 {
278     BOOL        ret = TRUE;
279
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*/);
285 }
286
287 /******************************************************************
288  *              WCUSER_ValidateFont
289  *
290  * Returns true if the font family described in lf is usable as a font for the renderer
291  */
292 BOOL    WCUSER_ValidateFont(const struct inner_data* data, const LOGFONT* lf)
293 {
294     return (lf->lfPitchAndFamily & 3) == FIXED_PITCH && 
295         /* (lf->lfPitchAndFamily & 0xF0) == FF_MODERN && */
296         (lf->lfCharSet == DEFAULT_CHARSET || lf->lfCharSet == ANSI_CHARSET);
297 }
298
299 /******************************************************************
300  *              get_first_font_enum_2
301  *              get_first_font_enum
302  *
303  * Helper functions to get a decent font for the renderer
304  */
305 static int CALLBACK get_first_font_enum_2(const LOGFONT* lf, const TEXTMETRIC* tm, 
306                                           DWORD FontType, LPARAM lParam)
307 {
308     struct font_chooser*        fc = (struct font_chooser*)lParam;
309
310     if (WCUSER_ValidateFontMetric(fc->data, tm, FontType))
311     {
312         WCUSER_SetFont(fc->data, lf);
313         fc->done = 1;
314         return 0;
315     }
316     return 1;
317 }
318
319 static int CALLBACK get_first_font_enum(const LOGFONT* lf, const TEXTMETRIC* tm, 
320                                         DWORD FontType, LPARAM lParam)
321 {
322     struct font_chooser*        fc = (struct font_chooser*)lParam;
323
324     if (WCUSER_ValidateFont(fc->data, lf))
325     {
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... */
328     }
329     return 1;
330 }
331
332 /******************************************************************
333  *              CopyFont
334  *
335  * get the relevant information from the font described in lf and store them
336  * in config
337  */
338 HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONT* lf)
339 {
340     TEXTMETRIC  tm;
341     HDC         hDC;
342     HFONT       hFont, hOldFont;
343     int         w, i, buf[256];
344
345     if (!(hDC = GetDC(hWnd))) return (HFONT)0;
346     if (!(hFont = CreateFontIndirect(lf))) goto err1;
347
348     hOldFont = SelectObject(hDC, hFont);
349     GetTextMetrics(hDC, &tm);
350
351     /* FIXME:
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
362      * as the cell width.
363      */
364     GetCharWidth32(hDC, tm.tmFirstChar, tm.tmFirstChar, &w);
365     for (i = tm.tmFirstChar + 1; i <= tm.tmLastChar; i += sizeof(buf) / sizeof(buf[0]))
366     {
367         int j, l;
368             
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++) 
372         {
373             if (buf[j] != w)
374             {
375                 Trace(0, "Non uniform cell width: [%d]=%d [%d]=%d\n", 
376                       i + j, buf[j], tm.tmFirstChar, w);
377                 goto err;
378             }
379         }
380     }
381
382     SelectObject(hDC, hOldFont);
383     ReleaseDC(hWnd, hDC);
384
385     config->cell_width  = w;
386     config->cell_height = tm.tmHeight;
387     config->font_weight = tm.tmWeight;
388     lstrcpy(config->face_name, lf->lfFaceName);
389
390     return hFont;
391  err:
392     if (hDC && hOldFont) SelectObject(hDC, hOldFont);
393     if (hFont) DeleteObject(hFont);
394  err1:
395     if (hDC) ReleaseDC(hWnd, hDC);
396
397     return (HFONT)0;
398 }
399
400 /******************************************************************
401  *              WCUSER_FillLogFont
402  *
403  *
404  */
405 void    WCUSER_FillLogFont(LOGFONT* lf, const WCHAR* name, UINT height, UINT weight)
406 {
407     lf->lfHeight        = height;
408     lf->lfWidth         = 0;
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);
421 }
422
423 /******************************************************************
424  *              WCUSER_SetFont
425  *
426  * sets logfont as the new font for the console
427  */
428 BOOL    WCUSER_SetFont(struct inner_data* data, const LOGFONT* logfont)
429 {
430     if (WCUSER_AreFontsEqual(&data->curcfg, logfont)) return TRUE;
431     if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
432
433     PRIVATE(data)->hFont = WCUSER_CopyFont(&data->curcfg, PRIVATE(data)->hWnd, logfont);
434     if (!PRIVATE(data)->hFont) {Trace(0, "wrong font\n");return FALSE;}
435
436     WCUSER_ComputePositions(data);
437     WCUSER_NewBitmap(data, TRUE);
438     InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
439     UpdateWindow(PRIVATE(data)->hWnd);
440
441     return TRUE;
442 }
443
444 /******************************************************************
445  *              WCUSER_InitFont
446  *
447  * create a hFont from the settings saved in registry...
448  * (called on init, assuming no font has been created before)
449  */
450 static BOOL     WCUSER_InitFont(struct inner_data* data)
451 {
452     LOGFONT             lf;
453     struct font_chooser fc;
454
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;
459
460     if (WCUSER_SetFont(data, &lf)) return TRUE;
461
462     /* try to find an acceptable font */
463     Trace(0, "Couldn't match the font from registry... trying to find one\n");
464     fc.data = data;
465     fc.done = 0;
466     EnumFontFamilies(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
467     return fc.done;
468 }
469
470 /******************************************************************
471  *              WCUSER_GetCell
472  *
473  * Get a cell from the a relative coordinate in window (takes into
474  * account the scrolling)
475  */
476 static COORD    WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
477 {
478     COORD       c;
479
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;
482
483     return c;
484 }
485
486 /******************************************************************
487  *              WCUSER_GetSelectionRect
488  *
489  * Get the selection rectangle
490  */
491 static void     WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
492 {
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;
497 }
498
499 /******************************************************************
500  *              WCUSER_SetSelection
501  *
502  *
503  */
504 static void     WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
505 {
506     HDC         hDC;
507     RECT        r;
508
509     WCUSER_GetSelectionRect(data, &r);
510     hDC = hRefDC ? hRefDC : GetDC(PRIVATE(data)->hWnd);
511     if (hDC)
512     {
513         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
514             HideCaret(PRIVATE(data)->hWnd);
515         InvertRect(hDC, &r);
516         if (hDC != hRefDC)
517             ReleaseDC(PRIVATE(data)->hWnd, hDC);
518         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
519             ShowCaret(PRIVATE(data)->hWnd);
520     }
521 }
522
523 /******************************************************************
524  *              WCUSER_MoveSelection
525  *
526  *
527  */
528 static void     WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2, BOOL final)
529 {
530     RECT        r;
531     HDC         hDC;
532
533     WCUSER_GetSelectionRect(data, &r);
534     hDC = GetDC(PRIVATE(data)->hWnd);
535     if (hDC)
536     {
537         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
538             HideCaret(PRIVATE(data)->hWnd);
539         InvertRect(hDC, &r);
540     }
541     PRIVATE(data)->selectPt1 = c1;
542     PRIVATE(data)->selectPt2 = c2;
543     if (hDC)
544     {
545         WCUSER_GetSelectionRect(data, &r);
546         InvertRect(hDC, &r);
547         ReleaseDC(PRIVATE(data)->hWnd, hDC);
548         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
549             ShowCaret(PRIVATE(data)->hWnd);
550     }
551     if (final)
552     {
553         ReleaseCapture();
554         PRIVATE(data)->has_selection = TRUE;
555     }
556 }
557
558 /******************************************************************
559  *              WCUSER_CopySelectionToClipboard
560  *
561  * Copies the current selection into the clipboard
562  */
563 static void     WCUSER_CopySelectionToClipboard(const struct inner_data* data)
564 {
565     HANDLE      hMem;
566     LPWSTR      p;
567     unsigned    w, h;
568
569     w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
570     h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
571
572     if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
573     EmptyClipboard();
574
575     hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h - 1) * sizeof(WCHAR));
576     if (hMem && (p = GlobalLock(hMem)))
577     {
578         COORD   c;
579         int     y;
580
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);
583         
584         for (y = 0; y < h; y++, c.Y++)
585         {
586             ReadConsoleOutputCharacter(data->hConOut, &p[y * w], w - 1, c, NULL);
587             if (y < h - 1) p[y * w + w - 1] = '\n';
588         }
589         GlobalUnlock(hMem);
590         SetClipboardData(CF_UNICODETEXT, hMem);
591     }
592     CloseClipboard();
593 }
594
595 /******************************************************************
596  *              WCUSER_PasteFromClipboard
597  *
598  *
599  */
600 static void     WCUSER_PasteFromClipboard(struct inner_data* data)
601 {
602     HANDLE      h;
603     WCHAR*      ptr;
604
605     if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
606     h = GetClipboardData(CF_UNICODETEXT);
607     if (h && (ptr = GlobalLock(h)))
608     {
609         int             i, len = GlobalSize(h) / sizeof(WCHAR);
610         INPUT_RECORD    ir[2];
611         DWORD           n;
612         SHORT           sh;
613
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;
618
619         /* generate the corresponding input records */
620         for (i = 0; i < len; i++)
621         {
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];
627             
628             ir[1] = ir[0];
629             ir[1].Event.KeyEvent.bKeyDown = FALSE;
630             
631             WriteConsoleInput(data->hConIn, ir, 2, &n);
632         }
633         GlobalUnlock(h);
634     }
635     CloseClipboard();
636 }
637
638 /******************************************************************
639  *              Refresh
640  *
641  *
642  */
643 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
644 {
645     if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
646     {
647         RECT    r;
648
649         r.left   = 0;
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);
656     }
657 }
658
659 /******************************************************************
660  *              WCUSER_Paint
661  *
662  *
663  */
664 static void     WCUSER_Paint(const struct inner_data* data)
665 {
666     PAINTSTRUCT         ps;
667
668     BeginPaint(PRIVATE(data)->hWnd, &ps);
669     BitBlt(ps.hdc, 0, 0, 
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,
675            SRCCOPY);
676     if (PRIVATE(data)->has_selection)
677         WCUSER_SetSelection(data, ps.hdc);
678     EndPaint(PRIVATE(data)->hWnd, &ps);
679 }
680
681 /******************************************************************
682  *              WCUSER_Scroll
683  *
684  *
685  */
686 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
687 {
688     if (horz)
689     {
690         SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, pos, TRUE);
691         data->curcfg.win_pos.X = pos;
692         InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
693     }
694     else
695     {
696         SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, pos, TRUE);
697         data->curcfg.win_pos.Y = pos;
698     }
699     InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
700 }
701
702 /******************************************************************
703  *              WCUSER_FillMenu
704  *
705  *
706  */
707 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
708 {
709     HMENU               hSubMenu;
710     HINSTANCE           hInstance = GetModuleHandle(NULL);
711     WCHAR               buff[256];
712
713     if (!hMenu) return FALSE;
714
715     /* FIXME: error handling & memory cleanup */
716     hSubMenu = CreateMenu();
717     if (!hSubMenu) return FALSE;
718
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);
731
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);
739
740     return TRUE;
741 }
742
743 /******************************************************************
744  *              WCUSER_Create
745  *
746  * Creates the window for the rendering
747  */
748 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCT lpcs)
749 {
750     struct inner_data*  data;
751     HMENU               hSysMenu;
752
753     data = lpcs->lpCreateParams;
754     SetWindowLong(hWnd, 0L, (DWORD)data);
755     PRIVATE(data)->hWnd = hWnd;
756
757     data->curcfg.cursor_size = 101; /* invalid value, will trigger a complete cleanup */
758
759     hSysMenu = GetSystemMenu(hWnd, FALSE);
760     if (!hSysMenu) return 0;
761     PRIVATE(data)->hPopMenu = CreatePopupMenu();
762     if (!PRIVATE(data)->hPopMenu) return 0;
763
764     WCUSER_FillMenu(hSysMenu, TRUE);
765     WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
766
767     PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
768     if (!PRIVATE(data)->hMemDC) {Trace(0, "no mem dc\n");return 0;}
769
770     data->curcfg.quick_edit = FALSE;
771     return 0;
772 }
773
774 /******************************************************************
775  *              WCUSER_SetMenuDetails
776  *
777  * Grays / ungrays the menu items according to their state
778  */
779 static void     WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
780 {
781     if (!hMenu) {Trace(0, "Issue in getting menu bits\n");return;}
782
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);
790 }
791
792 /******************************************************************
793  *              CUSER_GetCtrlKeyState
794  *
795  * Get the console bit mask equivalent to the VK_ status in keyState
796  */
797 static DWORD    WCUSER_GetCtrlKeyState(BYTE* keyState)
798 {
799     DWORD               ret = 0;
800
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;
811     
812     return ret;
813 }
814
815 /******************************************************************
816  *              WCUSER_HandleSelectionKey
817  *
818  * Handles keys while selecting an area
819  */
820 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down, 
821                                       WPARAM wParam, LPARAM lParam)
822 {
823     BYTE        keyState[256];
824     DWORD       state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
825     COORD       c1, c2;
826
827     if (down) return;
828
829     switch (state)
830     {
831     case 0:
832         switch (wParam)
833         {
834         case VK_RETURN:
835             PRIVATE(data)->has_selection = FALSE;
836             WCUSER_SetSelection(data, 0);
837             WCUSER_CopySelectionToClipboard(data);
838             break;
839         case VK_RIGHT:
840             c1 = PRIVATE(data)->selectPt1;
841             c2 = PRIVATE(data)->selectPt2;
842             c1.X++; c2.X++;
843             if (c1.X < data->curcfg.sb_width && c2.X < data->curcfg.sb_width)
844             {
845                 WCUSER_MoveSelection(data, c1, c2, FALSE);
846             }
847             break;
848         case VK_LEFT:
849             c1 = PRIVATE(data)->selectPt1;
850             c2 = PRIVATE(data)->selectPt2;
851             c1.X--; c2.X--;
852             if (c1.X >= 0 && c2.X >= 0)
853             {
854                 WCUSER_MoveSelection(data, c1, c2, FALSE);
855             }
856             break;
857         case VK_UP:
858             c1 = PRIVATE(data)->selectPt1;
859             c2 = PRIVATE(data)->selectPt2;
860             c1.Y--; c2.Y--;
861             if (c1.Y >= 0 && c2.Y >= 0)
862             {
863                 WCUSER_MoveSelection(data, c1, c2, FALSE);
864             }
865             break;
866         case VK_DOWN:
867             c1 = PRIVATE(data)->selectPt1;
868             c2 = PRIVATE(data)->selectPt2;
869             c1.Y++; c2.Y++;
870             if (c1.X < data->curcfg.sb_height && c2.X < data->curcfg.sb_height)
871             {
872                 WCUSER_MoveSelection(data, c1, c2, FALSE);
873             }
874             break;
875         }
876         break;
877     case SHIFT_PRESSED:
878         switch (wParam)
879         {
880         case VK_RIGHT:
881             c1 = PRIVATE(data)->selectPt1;
882             c2 = PRIVATE(data)->selectPt2;
883             c2.X++;
884             if (c2.X < data->curcfg.sb_width)
885             {
886                 WCUSER_MoveSelection(data, c1, c2, FALSE);
887             }
888             break;
889         case VK_LEFT:
890             c1 = PRIVATE(data)->selectPt1;
891             c2 = PRIVATE(data)->selectPt2;
892             c2.X--;
893             if (c2.X >= c1.X)
894             {
895                 WCUSER_MoveSelection(data, c1, c2, FALSE);
896             }
897             break;
898         case VK_UP:
899             c1 = PRIVATE(data)->selectPt1;
900             c2 = PRIVATE(data)->selectPt2;
901             c2.Y--;
902             if (c2.Y >= c1.Y)
903             {
904                 WCUSER_MoveSelection(data, c1, c2, FALSE);
905             }
906             break;
907         case VK_DOWN:
908             c1 = PRIVATE(data)->selectPt1;
909             c2 = PRIVATE(data)->selectPt2;
910             c2.Y++;
911             if (c2.X < data->curcfg.sb_height)
912             {
913                 WCUSER_MoveSelection(data, c1, c2, FALSE);
914             }
915             break;
916         }
917         break;
918     }
919 }
920
921 /******************************************************************
922  *              WCUSER_GenerateKeyInputRecord
923  *
924  * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
925  */
926 static void    WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down, 
927                                              WPARAM wParam, LPARAM lParam, BOOL sys)
928 {
929     INPUT_RECORD        ir;
930     DWORD               n;
931     WCHAR               buf[2];
932     static      WCHAR   last; /* keep last char seen as feed for key up message */
933     BYTE                keyState[256];
934
935     ir.EventType = KEY_EVENT;
936     ir.Event.KeyEvent.bKeyDown = down;
937     ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
938     ir.Event.KeyEvent.wVirtualKeyCode = wParam;
939     
940     ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
941     
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 */
946
947     if (!(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
948     {
949         if (down)
950         {
951             switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
952             {
953             case 2:
954                 /* FIXME... should generate two events... */
955                 /* fall thru */
956             case 1:     
957                 last = buf[0];
958                 break;
959             default:
960                 last = 0;
961                 break;
962             }
963         }
964         ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME HACKY... and buggy 'coz it should be a stack, not a single value */
965         if (!down) last = 0;
966     }
967
968     WriteConsoleInput(data->hConIn, &ir, 1, &n);
969 }
970
971 /******************************************************************
972  *              WCUSER_GenerateMouseInputRecord
973  *
974  *
975  */
976 static void    WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c, 
977                                                WPARAM wParam, DWORD event)
978 {
979     INPUT_RECORD        ir;
980     BYTE                keyState[256];
981     DWORD               mode, n;
982
983     /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
984     if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
985         return;
986
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;
995
996     WriteConsoleInput(data->hConIn, &ir, 1, &n);
997 }
998
999 /******************************************************************
1000  *              WCUSER_Proc
1001  *
1002  *
1003  */
1004 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1005 {
1006     struct inner_data*  data = (struct inner_data*)GetWindowLong(hWnd, 0);
1007
1008     switch (uMsg)
1009     {
1010     case WM_CREATE:
1011         return WCUSER_Create(hWnd, (LPCREATESTRUCT)lParam);
1012     case WM_DESTROY:
1013         PRIVATE(data)->hWnd = 0;
1014         PostQuitMessage(0);
1015         break;
1016     case WM_PAINT:
1017         WCUSER_Paint(data);
1018         break;
1019     case WM_KEYDOWN:
1020     case WM_KEYUP:
1021         if (PRIVATE(data)->has_selection)
1022             WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
1023         else
1024             WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam, FALSE);
1025         break;
1026     case WM_SYSKEYDOWN:
1027     case WM_SYSKEYUP:
1028         WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam, TRUE);
1029         break;
1030     case WM_LBUTTONDOWN:
1031         if (data->curcfg.quick_edit)
1032         {
1033             if (PRIVATE(data)->has_selection)
1034             {
1035                 PRIVATE(data)->has_selection = FALSE;
1036                 WCUSER_SetSelection(data, 0);
1037             }
1038             else
1039             {
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;
1044             }
1045         }
1046         else 
1047         {
1048             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1049         }
1050         break;
1051     case WM_MOUSEMOVE:
1052         if (data->curcfg.quick_edit)
1053         {
1054             if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
1055                 (wParam & MK_LBUTTON))
1056             {
1057                 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), FALSE);
1058             }
1059         }
1060         else
1061         {
1062             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
1063         }
1064         break;
1065     case WM_LBUTTONUP:
1066         if (data->curcfg.quick_edit)
1067         {
1068             if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection && 
1069                 (wParam& MK_LBUTTON))
1070             {
1071                 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), TRUE);
1072             }
1073         }
1074         else
1075         {
1076             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1077         }
1078         break;
1079     case WM_RBUTTONDOWN:
1080         if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1081         {
1082             RECT        r;
1083
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);
1088         }
1089         else
1090         {
1091             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1092         }
1093         break;    
1094     case WM_RBUTTONUP:
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);
1098         break;    
1099     case WM_MOUSEWHEEL:
1100         /* FIXME: should we scroll too ? */
1101         WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1102         break;    
1103     case WM_SETFOCUS:
1104         if (data->curcfg.cursor_visible)
1105         {
1106             CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap, 
1107                         data->curcfg.cell_width, data->curcfg.cell_height); 
1108             WCUSER_PosCursor(data);
1109         }
1110         break; 
1111     case WM_KILLFOCUS: 
1112         if (data->curcfg.cursor_visible)
1113             DestroyCaret(); 
1114         break;
1115     case WM_HSCROLL: 
1116         {
1117             int pos = data->curcfg.win_pos.X;
1118
1119             switch (LOWORD(wParam)) 
1120             { 
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;
1126             default:                                    break;
1127             } 
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)
1132             {
1133                 ScrollWindow(hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, 
1134                              NULL, NULL);
1135                 data->curcfg.win_pos.X = pos;
1136                 SetScrollPos(hWnd, SB_HORZ, pos, TRUE); 
1137                 UpdateWindow(hWnd); 
1138                 WCUSER_PosCursor(data);
1139                 WINECON_NotifyWindowChange(data);
1140             }
1141         }
1142         break;
1143     case WM_VSCROLL: 
1144         {
1145             int pos = data->curcfg.win_pos.Y;
1146
1147             switch (LOWORD(wParam)) 
1148             { 
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;
1154             default:                                    break;
1155             } 
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)
1160             {
1161                 ScrollWindow(hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, 
1162                              NULL, NULL);
1163                 data->curcfg.win_pos.Y = pos;
1164                 SetScrollPos(hWnd, SB_VERT, pos, TRUE); 
1165                 UpdateWindow(hWnd); 
1166                 WCUSER_PosCursor(data);
1167                 WINECON_NotifyWindowChange(data);
1168             }
1169
1170         } 
1171     case WM_SYSCOMMAND:
1172         switch (wParam)
1173         {
1174         case IDS_DEFAULT:
1175             WCUSER_GetProperties(data, FALSE);
1176             break;
1177         case IDS_PROPERTY:
1178             WCUSER_GetProperties(data, TRUE);
1179             break;
1180         default: 
1181             return DefWindowProc(hWnd, uMsg, wParam, lParam);
1182         }
1183         break;
1184     case WM_COMMAND:
1185         switch (wParam)
1186         {
1187         case IDS_DEFAULT:
1188             WCUSER_GetProperties(data, FALSE);
1189             break;
1190         case IDS_PROPERTY:
1191             WCUSER_GetProperties(data, TRUE);
1192             break;
1193         case IDS_MARK:
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;
1198             break;
1199         case IDS_COPY:
1200             if (PRIVATE(data)->has_selection)
1201             {
1202                 PRIVATE(data)->has_selection = FALSE;
1203                 WCUSER_SetSelection(data, 0);
1204                 WCUSER_CopySelectionToClipboard(data);
1205             }
1206             break;
1207         case IDS_PASTE:
1208             WCUSER_PasteFromClipboard(data);
1209             break;
1210         case IDS_SELECTALL:
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;
1216             break;
1217         case IDS_SCROLL:
1218         case IDS_SEARCH:
1219             Trace(0, "unhandled yet command: %x\n", wParam);
1220             break;
1221         default: 
1222             return DefWindowProc(hWnd, uMsg, wParam, lParam);
1223         }
1224         break;
1225     case WM_INITMENUPOPUP:
1226         if (!HIWORD(lParam))    return DefWindowProc(hWnd, uMsg, wParam, lParam);
1227         WCUSER_SetMenuDetails(data, GetSystemMenu(PRIVATE(data)->hWnd, FALSE));
1228         break;
1229     default:
1230         return DefWindowProc(hWnd, uMsg, wParam, lParam);
1231     }
1232     return 0;
1233 }
1234
1235 /******************************************************************
1236  *              WCUSER_DeleteBackend
1237  *
1238  *
1239  */
1240 void WCUSER_DeleteBackend(struct inner_data* data)
1241 {
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));
1249 }
1250
1251 /******************************************************************
1252  *              WCUSER_MainLoop
1253  *
1254  *
1255  */
1256 static int WCUSER_MainLoop(struct inner_data* data)
1257 {
1258     MSG         msg;
1259
1260     for (;;) 
1261     {
1262         switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1263         {
1264         case WAIT_OBJECT_0:
1265             if (!WINECON_GrabChanges(data))
1266                 PostQuitMessage(0);
1267             break;
1268         case WAIT_OBJECT_0+1:
1269             switch (GetMessage(&msg, 0, 0, 0))
1270             {
1271             case -1: /* the event handle became invalid, so exit */
1272                 return -1;
1273             case 0: /* WM_QUIT has been posted */
1274                 return 0;
1275             default:
1276                 DispatchMessage(&msg);
1277                 break;
1278             }
1279             break;
1280         default:
1281             Trace(0, "got pb\n");
1282             /* err */
1283             break;
1284         }
1285     }   
1286 }
1287
1288 /******************************************************************
1289  *              WCUSER_InitBackend
1290  *
1291  * Initialisation part II: creation of window.
1292  *
1293  */
1294 BOOL WCUSER_InitBackend(struct inner_data* data)
1295 {
1296     static WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1297
1298     WNDCLASS            wndclass;
1299
1300     data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1301     if (!data->private) return FALSE;
1302
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;
1312
1313     wndclass.style         = 0;
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;
1323   
1324     RegisterClass(&wndclass);
1325
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;
1330
1331     /* force update of current data */
1332     if (!WINECON_GrabChanges(data)) return FALSE;
1333
1334     if (!WCUSER_InitFont(data)) return FALSE;
1335
1336     return TRUE;
1337 }
1338