Added LGPL standard comment, and copyright notices where necessary.
[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->lfWidth  == config->cell_width &&
262         lf->lfWeight == config->font_weight &&
263         !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
264         !lstrcmpW(lf->lfFaceName, config->face_name);
265 }
266
267 /******************************************************************
268  *              CopyFont
269  *
270  *
271  */
272 void WCUSER_CopyFont(struct config_data* config, const LOGFONT* lf)
273 {
274     config->cell_width  = lf->lfWidth;
275     config->cell_height = lf->lfHeight;
276     config->font_weight  = lf->lfWeight;
277     lstrcpyW(config->face_name, lf->lfFaceName);
278 }
279
280 /******************************************************************
281  *              WCUSER_InitFont
282  *
283  * create a hFont from the settings saved in registry...
284  * (called on init, assuming no font has been created before)
285  */
286 BOOL    WCUSER_InitFont(struct inner_data* data)
287 {
288     LOGFONT lf;
289         
290     lf.lfHeight        = -data->curcfg.cell_height;
291     lf.lfWidth         = data->curcfg.cell_width;
292     lf.lfEscapement    = 0;
293     lf.lfOrientation   = 0;
294     lf.lfWeight        = data->curcfg.font_weight;
295     lf.lfItalic        = FALSE;
296     lf.lfUnderline     = FALSE;
297     lf.lfStrikeOut     = FALSE;
298     lf.lfCharSet       = DEFAULT_CHARSET;
299     lf.lfOutPrecision  = OUT_DEFAULT_PRECIS;
300     lf.lfClipPrecision = CLIP_DEFAULT_PRECIS; 
301     lf.lfQuality       = DEFAULT_QUALITY;
302     lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
303     lstrcpy(lf.lfFaceName, data->curcfg.face_name);
304     PRIVATE(data)->hFont = CreateFontIndirect(&lf);
305     if (!PRIVATE(data)->hFont) return FALSE;
306
307     WCUSER_ComputePositions(data);
308     WCUSER_NewBitmap(data, TRUE);
309     InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
310     UpdateWindow(PRIVATE(data)->hWnd);
311     return TRUE;
312 }
313
314 /******************************************************************
315  *              WCUSER_SetFont
316  *
317  * sets logfont as the new font for the console
318  */
319 BOOL    WCUSER_SetFont(struct inner_data* data, const LOGFONT* logfont)
320 {
321     if (WCUSER_AreFontsEqual(&data->curcfg, logfont)) return TRUE;
322     if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
323     PRIVATE(data)->hFont = CreateFontIndirect(logfont);
324     if (!PRIVATE(data)->hFont) {Trace(0, "wrong font\n");return FALSE;}
325     WCUSER_CopyFont(&data->curcfg, logfont);
326
327     WCUSER_ComputePositions(data);
328     WCUSER_NewBitmap(data, TRUE);
329     InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
330     UpdateWindow(PRIVATE(data)->hWnd);
331     return TRUE;
332 }
333
334 /******************************************************************
335  *              WCUSER_GetCell
336  *
337  * Get a cell from the a relative coordinate in window (takes into
338  * account the scrolling)
339  */
340 static COORD    WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
341 {
342     COORD       c;
343
344     c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
345     c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
346
347     return c;
348 }
349
350 /******************************************************************
351  *              WCUSER_GetSelectionRect
352  *
353  * Get the selection rectangle
354  */
355 static void     WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
356 {
357     r->left   = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X)    ) * data->curcfg.cell_width;
358     r->top    = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y)    ) * data->curcfg.cell_height;
359     r->right  = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1) * data->curcfg.cell_width;
360     r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1) * data->curcfg.cell_height;
361 }
362
363 /******************************************************************
364  *              WCUSER_SetSelection
365  *
366  *
367  */
368 static void     WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
369 {
370     HDC         hDC;
371     RECT        r;
372
373     WCUSER_GetSelectionRect(data, &r);
374     hDC = hRefDC ? hRefDC : GetDC(PRIVATE(data)->hWnd);
375     if (hDC)
376     {
377         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
378             HideCaret(PRIVATE(data)->hWnd);
379         InvertRect(hDC, &r);
380         if (hDC != hRefDC)
381             ReleaseDC(PRIVATE(data)->hWnd, hDC);
382         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
383             ShowCaret(PRIVATE(data)->hWnd);
384     }
385 }
386
387 /******************************************************************
388  *              WCUSER_MoveSelection
389  *
390  *
391  */
392 static void     WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2, BOOL final)
393 {
394     RECT        r;
395     HDC         hDC;
396
397     WCUSER_GetSelectionRect(data, &r);
398     hDC = GetDC(PRIVATE(data)->hWnd);
399     if (hDC)
400     {
401         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
402             HideCaret(PRIVATE(data)->hWnd);
403         InvertRect(hDC, &r);
404     }
405     PRIVATE(data)->selectPt1 = c1;
406     PRIVATE(data)->selectPt2 = c2;
407     if (hDC)
408     {
409         WCUSER_GetSelectionRect(data, &r);
410         InvertRect(hDC, &r);
411         ReleaseDC(PRIVATE(data)->hWnd, hDC);
412         if (PRIVATE(data)->hWnd == GetFocus() && data->curcfg.cursor_visible)
413             ShowCaret(PRIVATE(data)->hWnd);
414     }
415     if (final)
416     {
417         ReleaseCapture();
418         PRIVATE(data)->has_selection = TRUE;
419     }
420 }
421
422 /******************************************************************
423  *              WCUSER_CopySelectionToClipboard
424  *
425  * Copies the current selection into the clipboard
426  */
427 static void     WCUSER_CopySelectionToClipboard(const struct inner_data* data)
428 {
429     HANDLE      hMem;
430     LPWSTR      p;
431     unsigned    w, h;
432
433     w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
434     h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
435
436     if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
437     EmptyClipboard();
438
439     hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h - 1) * sizeof(WCHAR));
440     if (hMem && (p = GlobalLock(hMem)))
441     {
442         COORD   c;
443         int     y;
444
445         c.X = data->curcfg.win_pos.X + min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
446         c.Y = data->curcfg.win_pos.Y + min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
447         
448         for (y = 0; y < h; y++, c.Y++)
449         {
450             ReadConsoleOutputCharacter(data->hConOut, &p[y * w], w - 1, c, NULL);
451             if (y < h - 1) p[y * w + w - 1] = '\n';
452         }
453         GlobalUnlock(hMem);
454         SetClipboardData(CF_UNICODETEXT, hMem);
455     }
456     CloseClipboard();
457 }
458
459 /******************************************************************
460  *              WCUSER_PasteFromClipboard
461  *
462  *
463  */
464 static void     WCUSER_PasteFromClipboard(struct inner_data* data)
465 {
466     HANDLE      h;
467     WCHAR*      ptr;
468
469     if (!OpenClipboard(PRIVATE(data)->hWnd)) return;
470     h = GetClipboardData(CF_UNICODETEXT);
471     if (h && (ptr = GlobalLock(h)))
472     {
473         int             i, len = GlobalSize(h) / sizeof(WCHAR);
474         INPUT_RECORD    ir[2];
475         DWORD           n;
476         SHORT           sh;
477
478         ir[0].EventType = KEY_EVENT;
479         ir[0].Event.KeyEvent.wRepeatCount = 0;
480         ir[0].Event.KeyEvent.dwControlKeyState = 0;
481         ir[0].Event.KeyEvent.bKeyDown = TRUE;
482
483         /* generate the corresponding input records */
484         for (i = 0; i < len; i++)
485         {
486             /* FIXME: the modifying keys are not generated (shift, ctrl...) */
487             sh = VkKeyScan(ptr[i]);
488             ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
489             ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(LOBYTE(sh), 0);
490             ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
491             
492             ir[1] = ir[0];
493             ir[1].Event.KeyEvent.bKeyDown = FALSE;
494             
495             WriteConsoleInput(data->hConIn, ir, 2, &n);
496         }
497         GlobalUnlock(h);
498     }
499     CloseClipboard();
500 }
501
502 /******************************************************************
503  *              Refresh
504  *
505  *
506  */
507 static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
508 {
509     if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
510     {
511         RECT    r;
512
513         r.left   = 0;
514         r.right  = data->curcfg.win_width * data->curcfg.cell_width;
515         r.top    = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
516         r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
517         InvalidateRect(PRIVATE(data)->hWnd, &r, FALSE);
518         WCUSER_FillMemDC(data, tp, bm);
519         UpdateWindow(PRIVATE(data)->hWnd);
520     }
521 }
522
523 /******************************************************************
524  *              WCUSER_Paint
525  *
526  *
527  */
528 static void     WCUSER_Paint(const struct inner_data* data)
529 {
530     PAINTSTRUCT         ps;
531
532     BeginPaint(PRIVATE(data)->hWnd, &ps);
533     BitBlt(ps.hdc, 0, 0, 
534            data->curcfg.win_width * data->curcfg.cell_width, 
535            data->curcfg.win_height * data->curcfg.cell_height,
536            PRIVATE(data)->hMemDC, 
537            data->curcfg.win_pos.X * data->curcfg.cell_width, 
538            data->curcfg.win_pos.Y * data->curcfg.cell_height,
539            SRCCOPY);
540     if (PRIVATE(data)->has_selection)
541         WCUSER_SetSelection(data, ps.hdc);
542     EndPaint(PRIVATE(data)->hWnd, &ps);
543 }
544
545 /******************************************************************
546  *              WCUSER_Scroll
547  *
548  *
549  */
550 static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
551 {
552     if (horz)
553     {
554         SetScrollPos(PRIVATE(data)->hWnd, SB_HORZ, pos, TRUE);
555         data->curcfg.win_pos.X = pos;
556         InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
557     }
558     else
559     {
560         SetScrollPos(PRIVATE(data)->hWnd, SB_VERT, pos, TRUE);
561         data->curcfg.win_pos.Y = pos;
562     }
563     InvalidateRect(PRIVATE(data)->hWnd, NULL, FALSE);
564 }
565
566 struct font_chooser {
567     struct inner_data*  data;
568     int                 done;
569 };
570
571 /******************************************************************
572  *              WCUSER_ValidateFontMetric
573  *
574  * Returns true if the font described in tm is usable as a font for the renderer
575  */
576 BOOL    WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRIC* tm)
577 {
578     return tm->tmMaxCharWidth * data->curcfg.win_width < GetSystemMetrics(SM_CXSCREEN) &&
579         tm->tmHeight * data->curcfg.win_height < GetSystemMetrics(SM_CYSCREEN) &&
580         !tm->tmItalic && !tm->tmUnderlined && !tm->tmStruckOut;
581 }
582
583 /******************************************************************
584  *              WCUSER_ValidateFont
585  *
586  * Returns true if the font family described in lf is usable as a font for the renderer
587  */
588 BOOL    WCUSER_ValidateFont(const struct inner_data* data, const LOGFONT* lf)
589 {
590     return (lf->lfPitchAndFamily & 3) == FIXED_PITCH && 
591         (lf->lfPitchAndFamily & 0xF0) == FF_MODERN &&
592         lf->lfCharSet != SYMBOL_CHARSET;
593 }
594
595 /******************************************************************
596  *              get_first_font_enum_2
597  *              get_first_font_enum
598  *
599  * Helper functions to get a decent font for the renderer
600  */
601 static int CALLBACK get_first_font_enum_2(const LOGFONT* lf, const TEXTMETRIC* tm, 
602                                           DWORD FontType, LPARAM lParam)
603 {
604     struct font_chooser*        fc = (struct font_chooser*)lParam;
605
606     if (WCUSER_ValidateFontMetric(fc->data, tm))
607     {
608         WCUSER_SetFont(fc->data, lf);
609         fc->done = 1;
610         return 0;
611     }
612     return 1;
613 }
614
615 static int CALLBACK get_first_font_enum(const LOGFONT* lf, const TEXTMETRIC* tm, 
616                                         DWORD FontType, LPARAM lParam)
617 {
618     struct font_chooser*        fc = (struct font_chooser*)lParam;
619
620     if (WCUSER_ValidateFont(fc->data, lf))
621     {
622         EnumFontFamilies(PRIVATE(fc->data)->hMemDC, lf->lfFaceName, get_first_font_enum_2, lParam);
623         return !fc->done; /* we just need the first matching one... */
624     }
625     return 1;
626 }
627
628 /******************************************************************
629  *              WCUSER_FillMenu
630  *
631  *
632  */
633 static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
634 {
635     HMENU               hSubMenu;
636     HINSTANCE           hInstance = GetModuleHandle(NULL);
637     WCHAR               buff[256];
638
639     if (!hMenu) return FALSE;
640
641     /* FIXME: error handling & memory cleanup */
642     hSubMenu = CreateMenu();
643     if (!hSubMenu) return FALSE;
644
645     LoadString(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(WCHAR));
646     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
647     LoadString(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(WCHAR));
648     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
649     LoadString(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(WCHAR));
650     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
651     LoadString(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(WCHAR));
652     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
653     LoadString(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(WCHAR));
654     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
655     LoadString(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(WCHAR));
656     InsertMenu(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);
657
658     if (sep) InsertMenu(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
659     LoadString(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(WCHAR));
660     InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
661     LoadString(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(WCHAR));
662     InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
663     LoadString(hInstance, IDS_PROPERTY, buff, sizeof(buff) / sizeof(WCHAR));
664     InsertMenu(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTY, buff);
665
666     return TRUE;
667 }
668
669 /******************************************************************
670  *              WCUSER_Create
671  *
672  * Creates the window for the rendering
673  */
674 static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCT lpcs)
675 {
676     struct inner_data*  data;
677     HMENU               hSysMenu;
678
679     data = lpcs->lpCreateParams;
680     SetWindowLong(hWnd, 0L, (DWORD)data);
681     PRIVATE(data)->hWnd = hWnd;
682
683     data->curcfg.cursor_size = 101; /* invalid value, will trigger a complete cleanup */
684
685     hSysMenu = GetSystemMenu(hWnd, FALSE);
686     if (!hSysMenu) return 0;
687     PRIVATE(data)->hPopMenu = CreatePopupMenu();
688     if (!PRIVATE(data)->hPopMenu) return 0;
689
690     WCUSER_FillMenu(hSysMenu, TRUE);
691     WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);
692
693     PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
694     if (!PRIVATE(data)->hMemDC) {Trace(0, "no mem dc\n");return 0;}
695
696     data->curcfg.quick_edit = FALSE;
697     return 0;
698 }
699
700 /******************************************************************
701  *              WCUSER_SetMenuDetails
702  *
703  * Grays / ungrays the menu items according to their state
704  */
705 static void     WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
706 {
707     if (!hMenu) {Trace(0, "Issue in getting menu bits\n");return;}
708
709     EnableMenuItem(hMenu, IDS_COPY, 
710                    MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
711     EnableMenuItem(hMenu, IDS_PASTE, 
712                    MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT) 
713                                  ? MF_ENABLED : MF_GRAYED));
714     EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
715     EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
716 }
717
718 /******************************************************************
719  *              CUSER_GetCtrlKeyState
720  *
721  * Get the console bit mask equivalent to the VK_ status in keyState
722  */
723 static DWORD    WCUSER_GetCtrlKeyState(BYTE* keyState)
724 {
725     DWORD               ret = 0;
726
727     GetKeyboardState(keyState);
728     if (keyState[VK_SHIFT]    & 0x80)   ret |= SHIFT_PRESSED;
729     if (keyState[VK_CONTROL]  & 0x80)   ret |= LEFT_CTRL_PRESSED; /* FIXME: gotta choose one */
730     if (keyState[VK_LCONTROL] & 0x80)   ret |= LEFT_CTRL_PRESSED;
731     if (keyState[VK_RCONTROL] & 0x80)   ret |= RIGHT_CTRL_PRESSED;
732     if (keyState[VK_LMENU]    & 0x80)   ret |= LEFT_ALT_PRESSED;
733     if (keyState[VK_RMENU]    & 0x80)   ret |= RIGHT_ALT_PRESSED;
734     if (keyState[VK_CAPITAL]  & 0x01)   ret |= CAPSLOCK_ON;         
735     if (keyState[VK_NUMLOCK]  & 0x01)   ret |= NUMLOCK_ON;
736     if (keyState[VK_SCROLL]   & 0x01)   ret |= SCROLLLOCK_ON;
737     
738     return ret;
739 }
740
741 /******************************************************************
742  *              WCUSER_HandleSelectionKey
743  *
744  * Handles keys while selecting an area
745  */
746 static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down, 
747                                       WPARAM wParam, LPARAM lParam)
748 {
749     BYTE        keyState[256];
750     DWORD       state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
751     COORD       c1, c2;
752
753     if (down) return;
754
755     switch (state)
756     {
757     case 0:
758         switch (wParam)
759         {
760         case VK_RETURN:
761             PRIVATE(data)->has_selection = FALSE;
762             WCUSER_SetSelection(data, 0);
763             WCUSER_CopySelectionToClipboard(data);
764             break;
765         case VK_RIGHT:
766             c1 = PRIVATE(data)->selectPt1;
767             c2 = PRIVATE(data)->selectPt2;
768             c1.X++; c2.X++;
769             if (c1.X < data->curcfg.sb_width && c2.X < data->curcfg.sb_width)
770             {
771                 WCUSER_MoveSelection(data, c1, c2, FALSE);
772             }
773             break;
774         case VK_LEFT:
775             c1 = PRIVATE(data)->selectPt1;
776             c2 = PRIVATE(data)->selectPt2;
777             c1.X--; c2.X--;
778             if (c1.X >= 0 && c2.X >= 0)
779             {
780                 WCUSER_MoveSelection(data, c1, c2, FALSE);
781             }
782             break;
783         case VK_UP:
784             c1 = PRIVATE(data)->selectPt1;
785             c2 = PRIVATE(data)->selectPt2;
786             c1.Y--; c2.Y--;
787             if (c1.Y >= 0 && c2.Y >= 0)
788             {
789                 WCUSER_MoveSelection(data, c1, c2, FALSE);
790             }
791             break;
792         case VK_DOWN:
793             c1 = PRIVATE(data)->selectPt1;
794             c2 = PRIVATE(data)->selectPt2;
795             c1.Y++; c2.Y++;
796             if (c1.X < data->curcfg.sb_height && c2.X < data->curcfg.sb_height)
797             {
798                 WCUSER_MoveSelection(data, c1, c2, FALSE);
799             }
800             break;
801         }
802         break;
803     case SHIFT_PRESSED:
804         switch (wParam)
805         {
806         case VK_RIGHT:
807             c1 = PRIVATE(data)->selectPt1;
808             c2 = PRIVATE(data)->selectPt2;
809             c2.X++;
810             if (c2.X < data->curcfg.sb_width)
811             {
812                 WCUSER_MoveSelection(data, c1, c2, FALSE);
813             }
814             break;
815         case VK_LEFT:
816             c1 = PRIVATE(data)->selectPt1;
817             c2 = PRIVATE(data)->selectPt2;
818             c2.X--;
819             if (c2.X >= c1.X)
820             {
821                 WCUSER_MoveSelection(data, c1, c2, FALSE);
822             }
823             break;
824         case VK_UP:
825             c1 = PRIVATE(data)->selectPt1;
826             c2 = PRIVATE(data)->selectPt2;
827             c2.Y--;
828             if (c2.Y >= c1.Y)
829             {
830                 WCUSER_MoveSelection(data, c1, c2, FALSE);
831             }
832             break;
833         case VK_DOWN:
834             c1 = PRIVATE(data)->selectPt1;
835             c2 = PRIVATE(data)->selectPt2;
836             c2.Y++;
837             if (c2.X < data->curcfg.sb_height)
838             {
839                 WCUSER_MoveSelection(data, c1, c2, FALSE);
840             }
841             break;
842         }
843         break;
844     }
845 }
846
847 /******************************************************************
848  *              WCUSER_GenerateKeyInputRecord
849  *
850  * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
851  */
852 static void    WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down, 
853                                              WPARAM wParam, LPARAM lParam, BOOL sys)
854 {
855     INPUT_RECORD        ir;
856     DWORD               n;
857     WCHAR               buf[2];
858     static      WCHAR   last; /* keep last char seen as feed for key up message */
859     BYTE                keyState[256];
860
861     ir.EventType = KEY_EVENT;
862     ir.Event.KeyEvent.bKeyDown = down;
863     ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
864     ir.Event.KeyEvent.wVirtualKeyCode = wParam;
865     
866     ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
867     
868     ir.Event.KeyEvent.uChar.UnicodeChar = 0;
869     ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
870     if (lParam & (1L << 24))            ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
871     if (sys)                            ir.Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED; /* FIXME: gotta choose one */
872
873     if (!(ir.Event.KeyEvent.dwControlKeyState & ENHANCED_KEY))
874     {
875         if (down)
876         {
877             switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
878             {
879             case 2:
880                 /* FIXME... should generate two events... */
881                 /* fall thru */
882             case 1:     
883                 last = buf[0];
884                 break;
885             default:
886                 last = 0;
887                 break;
888             }
889         }
890         ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME HACKY... and buggy 'coz it should be a stack, not a single value */
891         if (!down) last = 0;
892     }
893
894     WriteConsoleInput(data->hConIn, &ir, 1, &n);
895 }
896
897 /******************************************************************
898  *              WCUSER_GenerateMouseInputRecord
899  *
900  *
901  */
902 static void    WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c, 
903                                                WPARAM wParam, DWORD event)
904 {
905     INPUT_RECORD        ir;
906     BYTE                keyState[256];
907     DWORD               mode, n;
908
909     /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
910     if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
911         return;
912
913     ir.EventType = MOUSE_EVENT;
914     ir.Event.MouseEvent.dwMousePosition = c;
915     ir.Event.MouseEvent.dwButtonState = 0;
916     if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
917     if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
918     if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
919     ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
920     ir.Event.MouseEvent.dwEventFlags = event;
921
922     WriteConsoleInput(data->hConIn, &ir, 1, &n);
923 }
924
925 /******************************************************************
926  *              WCUSER_Proc
927  *
928  *
929  */
930 static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
931 {
932     struct inner_data*  data = (struct inner_data*)GetWindowLong(hWnd, 0);
933
934     switch (uMsg)
935     {
936     case WM_CREATE:
937         return WCUSER_Create(hWnd, (LPCREATESTRUCT)lParam);
938     case WM_DESTROY:
939         PRIVATE(data)->hWnd = 0;
940         PostQuitMessage(0);
941         break;
942     case WM_PAINT:
943         WCUSER_Paint(data);
944         break;
945     case WM_KEYDOWN:
946     case WM_KEYUP:
947         if (PRIVATE(data)->has_selection)
948             WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
949         else
950             WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam, FALSE);
951         break;
952     case WM_SYSKEYDOWN:
953     case WM_SYSKEYUP:
954         WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam, TRUE);
955         break;
956     case WM_LBUTTONDOWN:
957         if (data->curcfg.quick_edit)
958         {
959             if (PRIVATE(data)->has_selection)
960             {
961                 PRIVATE(data)->has_selection = FALSE;
962                 WCUSER_SetSelection(data, 0);
963             }
964             else
965             {
966                 PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
967                 SetCapture(PRIVATE(data)->hWnd);
968                 WCUSER_SetSelection(data, 0);
969                 PRIVATE(data)->has_selection = TRUE;
970             }
971         }
972         else 
973         {
974             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
975         }
976         break;
977     case WM_MOUSEMOVE:
978         if (data->curcfg.quick_edit)
979         {
980             if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection &&
981                 (wParam & MK_LBUTTON))
982             {
983                 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), FALSE);
984             }
985         }
986         else
987         {
988             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
989         }
990         break;
991     case WM_LBUTTONUP:
992         if (data->curcfg.quick_edit)
993         {
994             if (GetCapture() == PRIVATE(data)->hWnd && PRIVATE(data)->has_selection && 
995                 (wParam& MK_LBUTTON))
996             {
997                 WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam), TRUE);
998             }
999         }
1000         else
1001         {
1002             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1003         }
1004         break;
1005     case WM_RBUTTONDOWN:
1006         if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
1007         {
1008             RECT        r;
1009
1010             GetWindowRect(hWnd, &r);
1011             WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1012             TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN, 
1013                            r.left + LOWORD(lParam), r.top + HIWORD(lParam), 0, hWnd, NULL);
1014         }
1015         else
1016         {
1017             WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1018         }
1019         break;    
1020     case WM_RBUTTONUP:
1021         /* no need to track for rbutton up when opening the popup... the event will be
1022          * swallowed by TrackPopupMenu */
1023         WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1024         break;    
1025     case WM_MOUSEWHEEL:
1026         /* FIXME: should we scroll too ? */
1027         WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
1028         break;    
1029     case WM_SETFOCUS:
1030         if (data->curcfg.cursor_visible)
1031         {
1032             CreateCaret(PRIVATE(data)->hWnd, PRIVATE(data)->cursor_bitmap, 
1033                         data->curcfg.cell_width, data->curcfg.cell_height); 
1034             WCUSER_PosCursor(data);
1035         }
1036         break; 
1037     case WM_KILLFOCUS: 
1038         if (data->curcfg.cursor_visible)
1039             DestroyCaret(); 
1040         break;
1041     case WM_HSCROLL: 
1042         {
1043             int pos = data->curcfg.win_pos.X;
1044
1045             switch (LOWORD(wParam)) 
1046             { 
1047             case SB_PAGEUP:     pos -= 8;               break; 
1048             case SB_PAGEDOWN:   pos += 8;               break; 
1049             case SB_LINEUP:     pos--;                  break;
1050             case SB_LINEDOWN:   pos++;                  break;
1051             case SB_THUMBTRACK: pos = HIWORD(wParam);   break;
1052             default:                                    break;
1053             } 
1054             if (pos < 0) pos = 0;
1055             if (pos > data->curcfg.sb_width - data->curcfg.win_width) 
1056                 pos = data->curcfg.sb_width - data->curcfg.win_width;
1057             if (pos != data->curcfg.win_pos.X)
1058             {
1059                 ScrollWindow(hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, 
1060                              NULL, NULL);
1061                 data->curcfg.win_pos.X = pos;
1062                 SetScrollPos(hWnd, SB_HORZ, pos, TRUE); 
1063                 UpdateWindow(hWnd); 
1064                 WCUSER_PosCursor(data);
1065                 WINECON_NotifyWindowChange(data);
1066             }
1067         }
1068         break;
1069     case WM_VSCROLL: 
1070         {
1071             int pos = data->curcfg.win_pos.Y;
1072
1073             switch (LOWORD(wParam)) 
1074             { 
1075             case SB_PAGEUP:     pos -= 8;               break; 
1076             case SB_PAGEDOWN:   pos += 8;               break; 
1077             case SB_LINEUP:     pos--;                  break;
1078             case SB_LINEDOWN:   pos++;                  break;
1079             case SB_THUMBTRACK: pos = HIWORD(wParam);   break;
1080             default:                                    break;
1081             } 
1082             if (pos < 0) pos = 0;
1083             if (pos > data->curcfg.sb_height - data->curcfg.win_height) 
1084                 pos = data->curcfg.sb_height - data->curcfg.win_height;
1085             if (pos != data->curcfg.win_pos.Y)
1086             {
1087                 ScrollWindow(hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, 
1088                              NULL, NULL);
1089                 data->curcfg.win_pos.Y = pos;
1090                 SetScrollPos(hWnd, SB_VERT, pos, TRUE); 
1091                 UpdateWindow(hWnd); 
1092                 WCUSER_PosCursor(data);
1093                 WINECON_NotifyWindowChange(data);
1094             }
1095
1096         } 
1097     case WM_SYSCOMMAND:
1098         switch (wParam)
1099         {
1100         case IDS_DEFAULT:
1101             WCUSER_GetProperties(data, FALSE);
1102             break;
1103         case IDS_PROPERTY:
1104             WCUSER_GetProperties(data, TRUE);
1105             break;
1106         default: 
1107             return DefWindowProc(hWnd, uMsg, wParam, lParam);
1108         }
1109         break;
1110     case WM_COMMAND:
1111         switch (wParam)
1112         {
1113         case IDS_DEFAULT:
1114             WCUSER_GetProperties(data, FALSE);
1115             break;
1116         case IDS_PROPERTY:
1117             WCUSER_GetProperties(data, TRUE);
1118             break;
1119         case IDS_MARK:
1120             PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1121             PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
1122             WCUSER_SetSelection(data, 0);
1123             PRIVATE(data)->has_selection = TRUE;
1124             break;
1125         case IDS_COPY:
1126             if (PRIVATE(data)->has_selection)
1127             {
1128                 PRIVATE(data)->has_selection = FALSE;
1129                 WCUSER_SetSelection(data, 0);
1130                 WCUSER_CopySelectionToClipboard(data);
1131             }
1132             break;
1133         case IDS_PASTE:
1134             WCUSER_PasteFromClipboard(data);
1135             break;
1136         case IDS_SELECTALL:
1137             PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
1138             PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
1139             PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
1140             WCUSER_SetSelection(data, 0);
1141             PRIVATE(data)->has_selection = TRUE;
1142             break;
1143         case IDS_SCROLL:
1144         case IDS_SEARCH:
1145             Trace(0, "unhandled yet command: %x\n", wParam);
1146             break;
1147         default: 
1148             return DefWindowProc(hWnd, uMsg, wParam, lParam);
1149         }
1150         break;
1151     case WM_INITMENUPOPUP:
1152         if (!HIWORD(lParam))    return DefWindowProc(hWnd, uMsg, wParam, lParam);
1153         WCUSER_SetMenuDetails(data, GetSystemMenu(PRIVATE(data)->hWnd, FALSE));
1154         break;
1155     default:
1156         return DefWindowProc(hWnd, uMsg, wParam, lParam);
1157     }
1158     return 0;
1159 }
1160
1161 /******************************************************************
1162  *              WCUSER_DeleteBackend
1163  *
1164  *
1165  */
1166 void WCUSER_DeleteBackend(struct inner_data* data)
1167 {
1168     if (!PRIVATE(data)) return;
1169     if (PRIVATE(data)->hWnd)            DestroyWindow(PRIVATE(data)->hWnd);
1170     if (PRIVATE(data)->hFont)           DeleteObject(PRIVATE(data)->hFont);
1171     if (PRIVATE(data)->cursor_bitmap)   DeleteObject(PRIVATE(data)->cursor_bitmap);
1172     if (PRIVATE(data)->hMemDC)          DeleteDC(PRIVATE(data)->hMemDC);
1173     if (PRIVATE(data)->hBitmap)         DeleteObject(PRIVATE(data)->hBitmap);
1174     HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1175 }
1176
1177 /******************************************************************
1178  *              WCUSER_MainLoop
1179  *
1180  *
1181  */
1182 static int WCUSER_MainLoop(struct inner_data* data)
1183 {
1184     MSG         msg;
1185
1186     for (;;) 
1187     {
1188         switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1189         {
1190         case WAIT_OBJECT_0:
1191             if (!WINECON_GrabChanges(data))
1192                 PostQuitMessage(0);
1193             break;
1194         case WAIT_OBJECT_0+1:
1195             switch (GetMessage(&msg, 0, 0, 0))
1196             {
1197             case -1: /* the event handle became invalid, so exit */
1198                 return -1;
1199             case 0: /* WM_QUIT has been posted */
1200                 return 0;
1201             default:
1202                 DispatchMessage(&msg);
1203                 break;
1204             }
1205             break;
1206         default:
1207             Trace(0, "got pb\n");
1208             /* err */
1209             break;
1210         }
1211     }   
1212 }
1213
1214 /******************************************************************
1215  *              WCUSER_InitBackend
1216  *
1217  * Initialisation part II: creation of window.
1218  *
1219  */
1220 BOOL WCUSER_InitBackend(struct inner_data* data)
1221 {
1222     static WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1223
1224     WNDCLASS            wndclass;
1225
1226     data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1227     if (!data->private) return FALSE;
1228
1229     data->fnMainLoop = WCUSER_MainLoop;
1230     data->fnPosCursor = WCUSER_PosCursor;
1231     data->fnShapeCursor = WCUSER_ShapeCursor;
1232     data->fnComputePositions = WCUSER_ComputePositions;
1233     data->fnRefresh = WCUSER_Refresh;
1234     data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
1235     data->fnSetTitle = WCUSER_SetTitle;
1236     data->fnScroll = WCUSER_Scroll;
1237     data->fnDeleteBackend = WCUSER_DeleteBackend;
1238
1239     wndclass.style         = 0;
1240     wndclass.lpfnWndProc   = WCUSER_Proc;
1241     wndclass.cbClsExtra    = 0;
1242     wndclass.cbWndExtra    = sizeof(DWORD);
1243     wndclass.hInstance     = GetModuleHandle(NULL);
1244     wndclass.hIcon         = LoadIcon(0, IDI_WINLOGO);
1245     wndclass.hCursor       = LoadCursor(0, IDC_ARROW);
1246     wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
1247     wndclass.lpszMenuName  = NULL;
1248     wndclass.lpszClassName = wClassName;
1249   
1250     RegisterClass(&wndclass);
1251
1252     CreateWindow(wndclass.lpszClassName, NULL,
1253                  WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL, 
1254                  CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);   
1255     if (!PRIVATE(data)->hWnd) return FALSE;
1256
1257     /* force update of current data */
1258     if (!WINECON_GrabChanges(data)) return FALSE;
1259
1260     if (!WCUSER_InitFont(data))
1261     {
1262         struct font_chooser fc;
1263         /* try to find an acceptable font */
1264         fc.data = data;
1265         fc.done = 0;
1266         EnumFontFamilies(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
1267         return fc.done;
1268     }
1269     return TRUE;
1270 }
1271
1272
1273