user.c 47.1 KB
Newer Older
1 2 3 4
/*
 * a GUI application for displaying a console
 *	USER32 back end
 * Copyright 2001 Eric Pouech
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21
 */

#include <stdio.h>
22
#include <stdlib.h>
23
#include "winecon_user.h"
24
#include "winnls.h"
25

26 27 28 29 30
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
WINE_DECLARE_DEBUG_CHANNEL(wc_font);

31 32
UINT g_uiDefaultCharset;

33
static BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* font);
34

35 36 37 38 39 40 41 42 43 44 45 46
/******************************************************************
 *		WCUSER_FillMemDC
 *
 * Fills the Mem DC with current cells values
 */
static void WCUSER_FillMemDC(const struct inner_data* data, int upd_tp, int upd_bm)
{
    unsigned		i, j, k;
    CHAR_INFO*		cell;
    HFONT		hOldFont;
    WORD		attr;
    WCHAR*		line;
47 48
    RECT                r;
    HBRUSH              hbr;
49
    INT *dx;
50

51
    /* no font has been set up yet, don't worry about filling the bitmap,
52 53 54 55
     * we'll do it once a font is chosen
     */
    if (!PRIVATE(data)->hFont) return;

56 57 58
    /* FIXME: could set up a mechanism to reuse the line between different
     * calls to this function
     */
59
    if (!(line = HeapAlloc(GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(WCHAR))))
60
        WINECON_Fatal("OOM\n");
61
    dx = HeapAlloc( GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(*dx) );
62

63
    hOldFont = SelectObject(PRIVATE(data)->hMemDC, PRIVATE(data)->hFont);
64 65
    for (j = upd_tp; j <= upd_bm; j++)
    {
66
	cell = &data->cells[j * data->curcfg.sb_width];
67
	for (i = 0; i < data->curcfg.sb_width; i++)
68 69
	{
	    attr = cell[i].Attributes;
70 71
	    SetBkColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[(attr >> 4) & 0x0F]);
	    SetTextColor(PRIVATE(data)->hMemDC, data->curcfg.color_map[attr & 0x0F]);
72
	    for (k = i; k < data->curcfg.sb_width && cell[k].Attributes == attr; k++)
73 74
	    {
		line[k - i] = cell[k].Char.UnicodeChar;
75
                dx[k - i] = data->curcfg.cell_width;
76
	    }
77 78 79
            ExtTextOutW( PRIVATE(data)->hMemDC, i * data->curcfg.cell_width, j * data->curcfg.cell_height,
                         0, NULL, line, k - i, dx );
            if (PRIVATE(data)->ext_leading &&
80
                (hbr = CreateSolidBrush(data->curcfg.color_map[(attr >> 4) & 0x0F])))
81 82 83 84 85 86 87 88
            {
                r.left   = i * data->curcfg.cell_width;
                r.top    = (j + 1) * data->curcfg.cell_height - PRIVATE(data)->ext_leading;
                r.right  = k * data->curcfg.cell_width;
                r.bottom = (j + 1) * data->curcfg.cell_height;
                FillRect(PRIVATE(data)->hMemDC, &r, hbr);
                DeleteObject(hbr);
            }
89 90 91
	    i = k - 1;
	}
    }
92
    SelectObject(PRIVATE(data)->hMemDC, hOldFont);
93
    HeapFree(GetProcessHeap(), 0, dx);
94 95 96 97 98 99
    HeapFree(GetProcessHeap(), 0, line);
}

/******************************************************************
 *		WCUSER_NewBitmap
 *
100 101
 * Either the font geometry or the sb geometry has changed. we need
 * to recreate the bitmap geometry.
102
 */
103
static void WCUSER_NewBitmap(struct inner_data* data)
104
{
105
    HDC         hDC;
106 107
    HBITMAP	hnew, hold;

108
    if (!data->curcfg.sb_width || !data->curcfg.sb_height ||
109
        !PRIVATE(data)->hFont || !(hDC = GetDC(data->hWnd)))
110
        return;
111 112
    hnew = CreateCompatibleBitmap(hDC,
				  data->curcfg.sb_width  * data->curcfg.cell_width,
113
				  data->curcfg.sb_height * data->curcfg.cell_height);
114
    ReleaseDC(data->hWnd, hDC);
115
    hold = SelectObject(PRIVATE(data)->hMemDC, hnew);
116

117
    if (PRIVATE(data)->hBitmap)
118
    {
119 120
	if (hold == PRIVATE(data)->hBitmap)
	    DeleteObject(PRIVATE(data)->hBitmap);
121
	else
122
	    WINE_FIXME("leak\n");
123
    }
124
    PRIVATE(data)->hBitmap = hnew;
125
    WCUSER_FillMemDC(data, 0, data->curcfg.sb_height - 1);
126 127 128 129 130 131 132 133 134
}

/******************************************************************
 *		WCUSER_ResizeScreenBuffer
 *
 *
 */
static void WCUSER_ResizeScreenBuffer(struct inner_data* data)
{
135
    WCUSER_NewBitmap(data);
136 137 138 139 140 141 142 143 144
}

/******************************************************************
 *		WCUSER_PosCursor
 *
 * Set a new position for the cursor
 */
static void	WCUSER_PosCursor(const struct inner_data* data)
{
145
    if (data->hWnd != GetFocus() || !data->curcfg.cursor_visible) return;
146

147
    SetCaretPos((data->cursor.X - data->curcfg.win_pos.X) * data->curcfg.cell_width,
148
		(data->cursor.Y - data->curcfg.win_pos.Y) * data->curcfg.cell_height);
149
    ShowCaret(data->hWnd);
150 151 152 153 154 155 156
}

/******************************************************************
 *		WCUSER_ShapeCursor
 *
 * Sets a new shape for the cursor
 */
157
static void	WCUSER_ShapeCursor(struct inner_data* data, int size, int vis, BOOL force)
158
{
159
    if (force || size != data->curcfg.cursor_size)
160
    {
161
	if (data->curcfg.cursor_visible && data->hWnd == GetFocus()) DestroyCaret();
162
	if (PRIVATE(data)->cursor_bitmap) DeleteObject(PRIVATE(data)->cursor_bitmap);
163
	PRIVATE(data)->cursor_bitmap = NULL;
164 165
	if (size != 100)
	{
166
	    int		w16b; /* number of bytes per row, aligned on word size */
167 168 169
	    BYTE*	ptr;
	    int		i, j, nbl;

170 171
	    w16b = ((data->curcfg.cell_width + 15) & ~15) / 8;
	    ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w16b * data->curcfg.cell_height);
172
	    if (!ptr) WINECON_Fatal("OOM");
173 174
	    nbl = max((data->curcfg.cell_height * size) / 100, 1);
	    for (j = data->curcfg.cell_height - nbl; j < data->curcfg.cell_height; j++)
175
	    {
176
		for (i = 0; i < data->curcfg.cell_width; i++)
177 178 179 180
		{
		    ptr[w16b * j + (i / 8)] |= 0x80 >> (i & 7);
		}
	    }
181
	    PRIVATE(data)->cursor_bitmap = CreateBitmap(data->curcfg.cell_width,
182
                                               data->curcfg.cell_height, 1, 1, ptr);
183 184
	    HeapFree(GetProcessHeap(), 0, ptr);
	}
185 186
	data->curcfg.cursor_size = size;
	data->curcfg.cursor_visible = -1;
187 188
    }

189
    vis = vis != 0;
190
    if (force || vis != data->curcfg.cursor_visible)
191
    {
192
	data->curcfg.cursor_visible = vis;
193
	if (data->hWnd == GetFocus())
194 195 196
	{
	    if (vis)
	    {
197
		CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
198
                            data->curcfg.cell_width, data->curcfg.cell_height);
199 200 201 202 203 204 205 206
		WCUSER_PosCursor(data);
	    }
	    else
	    {
		DestroyCaret();
	    }
	}
    }
207
    WINECON_DumpConfig("crsr", &data->curcfg);
208 209 210
}

/******************************************************************
211
 *		WCUSER_ComputePositions
212 213 214
 *
 * Recomputes all the components (mainly scroll bars) positions
 */
215
static void	WCUSER_ComputePositions(struct inner_data* data)
216 217 218 219 220 221
{
    RECT		r;
    int			dx, dy;

    /* compute window size from desired client size */
    r.left = r.top = 0;
222 223
    r.right = data->curcfg.win_width * data->curcfg.cell_width;
    r.bottom = data->curcfg.win_height * data->curcfg.cell_height;
224

225
    if (IsRectEmpty(&r)) return;
226

227
    AdjustWindowRect(&r, GetWindowLongW(data->hWnd, GWL_STYLE), FALSE);
228 229

    dx = dy = 0;
230
    if (data->curcfg.sb_width > data->curcfg.win_width)
231 232
    {
	dy = GetSystemMetrics(SM_CYHSCROLL);
233
	SetScrollRange(data->hWnd, SB_HORZ, 0,
234
                       data->curcfg.sb_width - data->curcfg.win_width, FALSE);
235 236
	SetScrollPos(data->hWnd, SB_HORZ, 0, FALSE); /* FIXME */
	ShowScrollBar(data->hWnd, SB_HORZ, TRUE);
237 238 239
    }
    else
    {
240
	ShowScrollBar(data->hWnd, SB_HORZ, FALSE);
241 242
    }

243
    if (data->curcfg.sb_height > data->curcfg.win_height)
244 245
    {
	dx = GetSystemMetrics(SM_CXVSCROLL);
246
	SetScrollRange(data->hWnd, SB_VERT, 0,
247
                       data->curcfg.sb_height - data->curcfg.win_height, FALSE);
248 249
	SetScrollPos(data->hWnd, SB_VERT, 0, FALSE); /* FIXME */
	ShowScrollBar(data->hWnd, SB_VERT, TRUE);
250
    }
251 252
    else
    {
253
	ShowScrollBar(data->hWnd, SB_VERT, FALSE);
254 255
    }

256
    SetWindowPos(data->hWnd, 0, 0, 0, r.right - r.left + dx, r.bottom - r.top + dy,
257
		 SWP_NOMOVE|SWP_NOZORDER);
258
    WCUSER_ShapeCursor(data, data->curcfg.cursor_size, data->curcfg.cursor_visible, TRUE);
259 260 261 262 263 264 265 266 267 268
    WCUSER_PosCursor(data);
}

/******************************************************************
 *		WCUSER_SetTitle
 *
 * Sets the title to the wine console
 */
static void	WCUSER_SetTitle(const struct inner_data* data)
{
269
    WCHAR	buffer[256];
270 271

    if (WINECON_GetConsoleTitle(data->hConIn, buffer, sizeof(buffer)))
272
        SetWindowTextW(data->hWnd, buffer);
273 274
}

275
void WCUSER_DumpLogFont(const char* pfx, const LOGFONTW* lf, DWORD ft)
276 277
{
    WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
278 279
                         "\tlf.lfHeight=%d lf.lfWidth=%d lf.lfEscapement=%d lf.lfOrientation=%d\n"
                         "\tlf.lfWeight=%d lf.lfItalic=%u lf.lfUnderline=%u lf.lfStrikeOut=%u\n"
280 281 282
                         "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
                         "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
                         pfx,
283 284 285 286 287
                         (ft & RASTER_FONTTYPE) ? "raster" : "",
                         (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
                         ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
                         (ft & DEVICE_FONTTYPE) ? "|device" : "",
                         lf->lfHeight, lf->lfWidth, lf->lfEscapement, lf->lfOrientation,
288
                         lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
289
                         lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
290 291 292
                         wine_dbgstr_w(lf->lfFaceName));
}

293
void WCUSER_DumpTextMetric(const TEXTMETRICW* tm, DWORD ft)
294 295
{
    WINE_TRACE_(wc_font)("%s%s%s%s\n"
296 297 298
                         "\ttmHeight=%d tmAscent=%d tmDescent=%d tmInternalLeading=%d tmExternalLeading=%d\n"
                         "\ttmAveCharWidth=%d tmMaxCharWidth=%d tmWeight=%d tmOverhang=%d\n"
                         "\ttmDigitizedAspectX=%d tmDigitizedAspectY=%d\n"
299 300
                         "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
                         "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
301 302 303 304 305 306 307
                         (ft & RASTER_FONTTYPE) ? "raster" : "",
                         (ft & TRUETYPE_FONTTYPE) ? "truetype" : "",
                         ((ft & (RASTER_FONTTYPE|TRUETYPE_FONTTYPE)) == 0) ? "vector" : "",
                         (ft & DEVICE_FONTTYPE) ? "|device" : "",
                         tm->tmHeight, tm->tmAscent, tm->tmDescent, tm->tmInternalLeading, tm->tmExternalLeading, tm->tmAveCharWidth,
                         tm->tmMaxCharWidth, tm->tmWeight, tm->tmOverhang, tm->tmDigitizedAspectX, tm->tmDigitizedAspectY,
                         tm->tmFirstChar, tm->tmLastChar, tm->tmDefaultChar, tm->tmBreakChar, tm->tmItalic, tm->tmUnderlined, tm->tmStruckOut,
308 309 310
                         tm->tmPitchAndFamily, tm->tmCharSet);
}

311
/******************************************************************
312
 *		WCUSER_AreFontsEqual
313 314 315
 *
 *
 */
316
static BOOL WCUSER_AreFontsEqual(const struct config_data* config, const LOGFONTW* lf)
317 318 319 320
{
    return lf->lfHeight == config->cell_height &&
        lf->lfWeight == config->font_weight &&
        !lf->lfItalic && !lf->lfUnderline && !lf->lfStrikeOut &&
321
        !lstrcmpW(lf->lfFaceName, config->face_name);
322 323
}

324
struct font_chooser
325
{
326
    struct inner_data*	data;
327
    int                 pass;
328
    BOOL                done;
329 330
};

331
/******************************************************************
332
 *		WCUSER_ValidateFontMetric
333
 *
334 335
 * Returns true if the font described in tm is usable as a font for the renderer
 */
336
BOOL WCUSER_ValidateFontMetric(const struct inner_data* data, const TEXTMETRICW* tm,
337
                               DWORD type, int pass)
338
{
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    switch (pass)  /* we get increasingly lenient in later passes */
    {
    case 0:
        if (type & RASTER_FONTTYPE)
        {
            if (tm->tmMaxCharWidth * data->curcfg.win_width >= GetSystemMetrics(SM_CXSCREEN) ||
                tm->tmHeight * data->curcfg.win_height >= GetSystemMetrics(SM_CYSCREEN))
                return FALSE;
        }
        /* fall through */
    case 1:
        if (tm->tmCharSet != DEFAULT_CHARSET && tm->tmCharSet != g_uiDefaultCharset) return FALSE;
        /* fall through */
    case 2:
        if (tm->tmItalic || tm->tmUnderlined || tm->tmStruckOut) return FALSE;
        break;
    }
    return TRUE;
357 358 359 360
}

/******************************************************************
 *		WCUSER_ValidateFont
361
 *
362
 * Returns true if the font family described in lf is usable as a font for the renderer
363
 */
364
BOOL WCUSER_ValidateFont(const struct inner_data* data, const LOGFONTW* lf, int pass)
365
{
366 367 368 369 370 371 372 373 374 375 376 377 378 379
    switch (pass)  /* we get increasingly lenient in later passes */
    {
    case 0:
    case 1:
        if (lf->lfCharSet != DEFAULT_CHARSET && lf->lfCharSet != g_uiDefaultCharset) return FALSE;
        /* fall through */
    case 2:
        if ((lf->lfPitchAndFamily & 3) != FIXED_PITCH) return FALSE;
        /* fall through */
    case 3:
        if (lf->lfFaceName[0] == '@') return FALSE;
        break;
    }
    return TRUE;
380 381 382
}

/******************************************************************
383 384
 *		get_first_font_enum_2
 *		get_first_font_enum
385
 *
386
 * Helper functions to get a decent font for the renderer
387
 */
388 389
static int CALLBACK get_first_font_enum_2(const LOGFONTW* lf, const TEXTMETRICW* tm,
                                          DWORD FontType, LPARAM lParam)
390
{
391
    struct font_chooser*	fc = (struct font_chooser*)lParam;
392

393
    WCUSER_DumpTextMetric(tm, FontType);
394
    if (WCUSER_ValidateFontMetric(fc->data, tm, FontType, fc->pass))
395
    {
396
        LOGFONTW mlf = *lf;
397 398

        /* Use the default sizes for the font (this is needed, especially for
399
         * TrueType fonts, so that we get a decent size, not the max size)
400
         */
401 402 403 404
        mlf.lfWidth  = fc->data->curcfg.cell_width;
        mlf.lfHeight = fc->data->curcfg.cell_height;
        if (WCUSER_SetFont(fc->data, &mlf))
        {
405 406
            struct      config_data     defcfg;

407 408
            WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
            fc->done = 1;
409
            /* since we've modified the current config with new font information,
410 411
             * set this information as the new default.
             */
412 413 414 415
            WINECON_RegLoad(NULL, &defcfg);
            defcfg.cell_width = fc->data->curcfg.cell_width;
            defcfg.cell_height = fc->data->curcfg.cell_height;
            lstrcpyW(defcfg.face_name, fc->data->curcfg.face_name);
416 417 418
            /* Force also its writing back to the registry so that we can get it
             * the next time.
             */
419
            WINECON_RegSave(&defcfg);
420 421
            return 0;
        }
422 423 424 425
    }
    return 1;
}

426 427
static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
                                        DWORD FontType, LPARAM lParam)
428 429 430
{
    struct font_chooser*	fc = (struct font_chooser*)lParam;

431
    WCUSER_DumpLogFont("InitFamily: ", lf, FontType);
432
    if (WCUSER_ValidateFont(fc->data, lf, fc->pass))
433
    {
434 435
        EnumFontFamiliesW(PRIVATE(fc->data)->hMemDC, lf->lfFaceName,
                          get_first_font_enum_2, lParam);
436 437 438 439 440 441
	return !fc->done; /* we just need the first matching one... */
    }
    return 1;
}

/******************************************************************
442
 *		WCUSER_CopyFont
443 444 445 446
 *
 * get the relevant information from the font described in lf and store them
 * in config
 */
447
HFONT WCUSER_CopyFont(struct config_data* config, HWND hWnd, const LOGFONTW* lf, LONG* el)
448
{
449
    TEXTMETRICW tm;
450 451
    HDC         hDC;
    HFONT       hFont, hOldFont;
452
    CPINFO cpinfo;
453

454
    if (!(hDC = GetDC(hWnd))) return NULL;
455
    if (!(hFont = CreateFontIndirectW(lf)))
456
    {
457 458
        ReleaseDC(hWnd, hDC);
        return NULL;
459
    }
460 461
    hOldFont = SelectObject(hDC, hFont);
    GetTextMetricsW(hDC, &tm);
462 463 464
    SelectObject(hDC, hOldFont);
    ReleaseDC(hWnd, hDC);

465
    config->cell_width  = tm.tmAveCharWidth;
Eric Pouech's avatar
Eric Pouech committed
466
    config->cell_height = tm.tmHeight + tm.tmExternalLeading;
467
    config->font_weight = tm.tmWeight;
468
    lstrcpyW(config->face_name, lf->lfFaceName);
469
    if (el) *el = tm.tmExternalLeading;
470

471 472 473 474
    /* FIXME: use maximum width for DBCS codepages since some chars take two cells */
    if (GetCPInfo( GetConsoleOutputCP(), &cpinfo ) && cpinfo.MaxCharSize > 1)
        config->cell_width  = tm.tmMaxCharWidth;

475 476 477 478 479 480 481 482
    return hFont;
}

/******************************************************************
 *		WCUSER_FillLogFont
 *
 *
 */
483
void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
484 485 486 487 488 489 490 491 492 493 494
{
    lf->lfHeight        = height;
    lf->lfWidth         = 0;
    lf->lfEscapement    = 0;
    lf->lfOrientation   = 0;
    lf->lfWeight        = weight;
    lf->lfItalic        = FALSE;
    lf->lfUnderline     = FALSE;
    lf->lfStrikeOut     = FALSE;
    lf->lfCharSet       = DEFAULT_CHARSET;
    lf->lfOutPrecision  = OUT_DEFAULT_PRECIS;
495
    lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
496 497
    lf->lfQuality       = DEFAULT_QUALITY;
    lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
498
    lstrcpyW(lf->lfFaceName, name);
499 500 501 502 503 504 505
}

/******************************************************************
 *		WCUSER_SetFont
 *
 * sets logfont as the new font for the console
 */
506
BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
507
{
508
    HFONT       hFont;
509
    LONG        el;
510

511
    if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
512
        return TRUE;
513

514
    hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
515 516 517 518
    if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}

    if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
    PRIVATE(data)->hFont = hFont;
519
    PRIVATE(data)->ext_leading = el;
520

521
    WCUSER_ComputePositions(data);
522
    WCUSER_NewBitmap(data);
523 524
    InvalidateRect(data->hWnd, NULL, FALSE);
    UpdateWindow(data->hWnd);
525

526 527 528
    return TRUE;
}

529
/******************************************************************
530
 *		WCUSER_SetFontPmt
531
 *
532 533
 * Sets a new font for the console.
 * In fact a wrapper for WCUSER_SetFont
534
 */
535 536
static void     WCUSER_SetFontPmt(struct inner_data* data, const WCHAR* font,
                                  unsigned height, unsigned weight)
537
{
538
    LOGFONTW            lf;
539 540
    struct font_chooser fc;

541 542
    WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
                         wine_dbgstr_wn(font, -1), height, weight);
543

544 545 546
    if (font[0] != '\0' && height != 0 && weight != 0)
    {
        WCUSER_FillLogFont(&lf, font, height, weight);
547
        if (WCUSER_SetFont(data, &lf))
548 549
        {
            WCUSER_DumpLogFont("InitReuses: ", &lf, 0);
550
            return;
551
        }
552
    }
553 554

    /* try to find an acceptable font */
555
    WINE_WARN("Couldn't match the font from registry... trying to find one\n");
556
    fc.data = data;
557
    fc.done = FALSE;
558 559 560 561 562 563
    for (fc.pass = 0; fc.pass <= 4; fc.pass++)
    {
        EnumFontFamiliesW(PRIVATE(data)->hMemDC, NULL, get_first_font_enum, (LPARAM)&fc);
        if (fc.done) return;
    }
    WINECON_Fatal("Couldn't find a decent font, aborting\n");
564 565
}

566 567 568
/******************************************************************
 *		WCUSER_GetCell
 *
569
 * Get a cell from a relative coordinate in window (takes into
570 571 572 573 574 575
 * account the scrolling)
 */
static COORD	WCUSER_GetCell(const struct inner_data* data, LPARAM lParam)
{
    COORD	c;

576 577
    c.X = data->curcfg.win_pos.X + (short)LOWORD(lParam) / data->curcfg.cell_width;
    c.Y = data->curcfg.win_pos.Y + (short)HIWORD(lParam) / data->curcfg.cell_height;
578 579 580 581 582 583 584 585 586 587 588

    return c;
}

/******************************************************************
 *		WCUSER_GetSelectionRect
 *
 * Get the selection rectangle
 */
static void	WCUSER_GetSelectionRect(const struct inner_data* data, LPRECT r)
{
Eric Pouech's avatar
Eric Pouech committed
589 590 591 592
    r->left   = (min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X)     - data->curcfg.win_pos.X) * data->curcfg.cell_width;
    r->top    = (min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y)     - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
    r->right  = (max(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X) + 1 - data->curcfg.win_pos.X) * data->curcfg.cell_width;
    r->bottom = (max(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y) + 1 - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
593 594 595 596 597 598 599 600 601 602 603 604 605
}

/******************************************************************
 *		WCUSER_SetSelection
 *
 *
 */
static void	WCUSER_SetSelection(const struct inner_data* data, HDC hRefDC)
{
    HDC		hDC;
    RECT	r;

    WCUSER_GetSelectionRect(data, &r);
606
    hDC = hRefDC ? hRefDC : GetDC(data->hWnd);
607 608
    if (hDC)
    {
609 610
	if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
	    HideCaret(data->hWnd);
611 612
	InvertRect(hDC, &r);
	if (hDC != hRefDC)
613 614 615
	    ReleaseDC(data->hWnd, hDC);
	if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
	    ShowCaret(data->hWnd);
616 617 618 619 620 621 622 623
    }
}

/******************************************************************
 *		WCUSER_MoveSelection
 *
 *
 */
624
static void	WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
625 626 627 628
{
    RECT	r;
    HDC		hDC;

629 630 631 632 633 634
    if (c1.X < 0 || c1.X >= data->curcfg.sb_width ||
        c2.X < 0 || c2.X >= data->curcfg.sb_width ||
        c1.Y < 0 || c1.Y >= data->curcfg.sb_height ||
        c2.Y < 0 || c2.Y >= data->curcfg.sb_height)
        return;

635
    WCUSER_GetSelectionRect(data, &r);
636
    hDC = GetDC(data->hWnd);
637 638
    if (hDC)
    {
639 640
	if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
	    HideCaret(data->hWnd);
641 642
	InvertRect(hDC, &r);
    }
643 644
    PRIVATE(data)->selectPt1 = c1;
    PRIVATE(data)->selectPt2 = c2;
645 646 647 648
    if (hDC)
    {
	WCUSER_GetSelectionRect(data, &r);
	InvertRect(hDC, &r);
649 650 651
	ReleaseDC(data->hWnd, hDC);
	if (data->hWnd == GetFocus() && data->curcfg.cursor_visible)
	    ShowCaret(data->hWnd);
652 653 654 655 656 657 658 659 660 661 662 663 664 665
    }
}

/******************************************************************
 *		WCUSER_CopySelectionToClipboard
 *
 * Copies the current selection into the clipboard
 */
static void	WCUSER_CopySelectionToClipboard(const struct inner_data* data)
{
    HANDLE	hMem;
    LPWSTR	p;
    unsigned	w, h;

666 667
    w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
    h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
668

669
    if (!OpenClipboard(data->hWnd)) return;
670 671
    EmptyClipboard();

Eric Pouech's avatar
Eric Pouech committed
672
    hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
673 674 675 676 677
    if (hMem && (p = GlobalLock(hMem)))
    {
	COORD	c;
	int	y;

Eric Pouech's avatar
Eric Pouech committed
678 679
	c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
	c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
680

681 682
	for (y = 0; y < h; y++, c.Y++)
	{
683
	    LPWSTR end;
684 685 686
	    DWORD count;

	    ReadConsoleOutputCharacterW(data->hConOut, p, w - 1, c, &count);
687

688 689 690 691 692 693
	    /* strip spaces from the end of the line */
	    end = p + w - 1;
	    while (end > p && *(end - 1) == ' ')
	        end--;
	    *end = (y < h - 1) ? '\n' : '\0';
	    p = end + 1;
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
	}
	GlobalUnlock(hMem);
	SetClipboardData(CF_UNICODETEXT, hMem);
    }
    CloseClipboard();
}

/******************************************************************
 *		WCUSER_PasteFromClipboard
 *
 *
 */
static void	WCUSER_PasteFromClipboard(struct inner_data* data)
{
    HANDLE	h;
    WCHAR*	ptr;

711
    if (!OpenClipboard(data->hWnd)) return;
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    h = GetClipboardData(CF_UNICODETEXT);
    if (h && (ptr = GlobalLock(h)))
    {
	int		i, len = GlobalSize(h) / sizeof(WCHAR);
	INPUT_RECORD	ir[2];
	DWORD		n;
	SHORT		sh;

	ir[0].EventType = KEY_EVENT;
	ir[0].Event.KeyEvent.wRepeatCount = 0;
	ir[0].Event.KeyEvent.dwControlKeyState = 0;
	ir[0].Event.KeyEvent.bKeyDown = TRUE;

	/* generate the corresponding input records */
	for (i = 0; i < len; i++)
	{
	    /* FIXME: the modifying keys are not generated (shift, ctrl...) */
729
            sh = VkKeyScanW(ptr[i]);
730
	    ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
731
            ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(LOBYTE(sh), 0);
732
	    ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
733

734 735
	    ir[1] = ir[0];
	    ir[1].Event.KeyEvent.bKeyDown = FALSE;
736

737
            WriteConsoleInputW(data->hConIn, ir, 2, &n);
738 739 740 741 742 743
	}
	GlobalUnlock(h);
    }
    CloseClipboard();
}

744
/******************************************************************
745
 *		WCUSER_Refresh
746 747 748
 *
 *
 */
749 750
static void WCUSER_Refresh(const struct inner_data* data, int tp, int bm)
{
751
    WCUSER_FillMemDC(data, tp, bm);
752
    if (data->curcfg.win_pos.Y <= bm && data->curcfg.win_pos.Y + data->curcfg.win_height >= tp)
753 754 755 756
    {
	RECT	r;

	r.left   = 0;
757 758 759
	r.right  = data->curcfg.win_width * data->curcfg.cell_width;
	r.top    = (tp - data->curcfg.win_pos.Y) * data->curcfg.cell_height;
	r.bottom = (bm - data->curcfg.win_pos.Y + 1) * data->curcfg.cell_height;
760 761
	InvalidateRect(data->hWnd, &r, FALSE);
	UpdateWindow(data->hWnd);
762 763 764 765 766 767 768 769 770 771 772 773
    }
}

/******************************************************************
 *		WCUSER_Paint
 *
 *
 */
static void	WCUSER_Paint(const struct inner_data* data)
{
    PAINTSTRUCT		ps;

774
    if (data->in_set_config) return; /* in order to avoid some flicker */
775
    BeginPaint(data->hWnd, &ps);
776 777
    BitBlt(ps.hdc, 0, 0,
           data->curcfg.win_width * data->curcfg.cell_width,
778
           data->curcfg.win_height * data->curcfg.cell_height,
779 780
	   PRIVATE(data)->hMemDC,
           data->curcfg.win_pos.X * data->curcfg.cell_width,
781
           data->curcfg.win_pos.Y * data->curcfg.cell_height,
782
	   SRCCOPY);
783
    if (PRIVATE(data)->has_selection)
784
	WCUSER_SetSelection(data, ps.hdc);
785
    EndPaint(data->hWnd, &ps);
786 787 788 789 790 791 792 793 794 795 796
}

/******************************************************************
 *		WCUSER_Scroll
 *
 *
 */
static void WCUSER_Scroll(struct inner_data* data, int pos, BOOL horz)
{
    if (horz)
    {
797
        ScrollWindow(data->hWnd, (data->curcfg.win_pos.X - pos) * data->curcfg.cell_width, 0, NULL, NULL);
798
	SetScrollPos(data->hWnd, SB_HORZ, pos, TRUE);
799
	data->curcfg.win_pos.X = pos;
800 801 802
    }
    else
    {
803
        ScrollWindow(data->hWnd, 0, (data->curcfg.win_pos.Y - pos) * data->curcfg.cell_height, NULL, NULL);
804
	SetScrollPos(data->hWnd, SB_VERT, pos, TRUE);
805
	data->curcfg.win_pos.Y = pos;
806
    }
807
    InvalidateRect(data->hWnd, NULL, FALSE);
808 809 810
}

/******************************************************************
811 812
 *		WCUSER_FillMenu
 *
813 814
 *
 */
815
static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
816
{
817 818 819
    HMENU     hSubMenu;
    HINSTANCE hInstance = GetModuleHandleW(NULL);
    WCHAR     buff[256];
820

821
    if (!hMenu) return FALSE;
822 823 824

    /* FIXME: error handling & memory cleanup */
    hSubMenu = CreateMenu();
825
    if (!hSubMenu) return FALSE;
826

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    LoadStringW(hInstance, IDS_MARK, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
    LoadStringW(hInstance, IDS_COPY, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
    LoadStringW(hInstance, IDS_PASTE, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
    LoadStringW(hInstance, IDS_SELECTALL, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
    LoadStringW(hInstance, IDS_SCROLL, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
    LoadStringW(hInstance, IDS_SEARCH, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);

    if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
    LoadStringW(hInstance, IDS_EDIT, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
    LoadStringW(hInstance, IDS_DEFAULT, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
    LoadStringW(hInstance, IDS_PROPERTIES, buff, sizeof(buff) / sizeof(buff[0]));
    InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
847

848 849 850
    return TRUE;
}

851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
/******************************************************************
 *		WCUSER_SetMenuDetails
 *
 * Grays / ungrays the menu items according to their state
 */
static void	WCUSER_SetMenuDetails(const struct inner_data* data, HMENU hMenu)
{
    if (!hMenu) {WINE_ERR("Issue in getting menu bits\n");return;}

    EnableMenuItem(hMenu, IDS_COPY,
                   MF_BYCOMMAND|(PRIVATE(data)->has_selection ? MF_ENABLED : MF_GRAYED));
    EnableMenuItem(hMenu, IDS_PASTE,
		   MF_BYCOMMAND|(IsClipboardFormatAvailable(CF_UNICODETEXT)
				 ? MF_ENABLED : MF_GRAYED));
    EnableMenuItem(hMenu, IDS_SCROLL, MF_BYCOMMAND|MF_GRAYED);
    EnableMenuItem(hMenu, IDS_SEARCH, MF_BYCOMMAND|MF_GRAYED);
}

869 870 871 872 873
/******************************************************************
 *		WCUSER_Create
 *
 * Creates the window for the rendering
 */
874
static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
875 876 877 878 879
{
    struct inner_data*	data;
    HMENU		hSysMenu;

    data = lpcs->lpCreateParams;
880
    SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
881
    data->hWnd = hWnd;
882 883 884 885 886 887 888 889 890 891

    hSysMenu = GetSystemMenu(hWnd, FALSE);
    if (!hSysMenu) return 0;
    PRIVATE(data)->hPopMenu = CreatePopupMenu();
    if (!PRIVATE(data)->hPopMenu) return 0;

    WCUSER_FillMenu(hSysMenu, TRUE);
    WCUSER_FillMenu(PRIVATE(data)->hPopMenu, FALSE);

    PRIVATE(data)->hMemDC = CreateCompatibleDC(0);
892
    if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
893

894
    data->curcfg.quick_edit = FALSE;
895 896 897
    return 0;
}

898
/******************************************************************
899
 *		WCUSER_GetCtrlKeyState
900 901 902 903 904 905 906 907 908 909 910 911 912
 *
 * Get the console bit mask equivalent to the VK_ status in keyState
 */
static DWORD    WCUSER_GetCtrlKeyState(BYTE* keyState)
{
    DWORD               ret = 0;

    GetKeyboardState(keyState);
    if (keyState[VK_SHIFT]    & 0x80)	ret |= SHIFT_PRESSED;
    if (keyState[VK_LCONTROL] & 0x80)	ret |= LEFT_CTRL_PRESSED;
    if (keyState[VK_RCONTROL] & 0x80)	ret |= RIGHT_CTRL_PRESSED;
    if (keyState[VK_LMENU]    & 0x80)	ret |= LEFT_ALT_PRESSED;
    if (keyState[VK_RMENU]    & 0x80)	ret |= RIGHT_ALT_PRESSED;
913
    if (keyState[VK_CAPITAL]  & 0x01)	ret |= CAPSLOCK_ON;
914 915
    if (keyState[VK_NUMLOCK]  & 0x01)	ret |= NUMLOCK_ON;
    if (keyState[VK_SCROLL]   & 0x01)	ret |= SCROLLLOCK_ON;
916

917 918 919 920 921 922 923 924
    return ret;
}

/******************************************************************
 *		WCUSER_HandleSelectionKey
 *
 * Handles keys while selecting an area
 */
925
static void WCUSER_HandleSelectionKey(struct inner_data* data, BOOL down,
926 927 928 929 930 931
                                      WPARAM wParam, LPARAM lParam)
{
    BYTE	keyState[256];
    DWORD       state = WCUSER_GetCtrlKeyState(keyState) & ~(CAPSLOCK_ON|NUMLOCK_ON|SCROLLLOCK_ON);
    COORD       c1, c2;

932
    if (!down) return;
933 934 935 936 937 938 939 940 941 942

    switch (state)
    {
    case 0:
        switch (wParam)
        {
        case VK_RETURN:
            PRIVATE(data)->has_selection = FALSE;
            WCUSER_SetSelection(data, 0);
            WCUSER_CopySelectionToClipboard(data);
943
            return;
944 945 946 947
        case VK_RIGHT:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c1.X++; c2.X++;
948
            WCUSER_MoveSelection(data, c1, c2);
949
            return;
950 951 952 953
        case VK_LEFT:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c1.X--; c2.X--;
954
            WCUSER_MoveSelection(data, c1, c2);
955
            return;
956 957 958 959
        case VK_UP:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c1.Y--; c2.Y--;
960
            WCUSER_MoveSelection(data, c1, c2);
961
            return;
962 963 964 965
        case VK_DOWN:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c1.Y++; c2.Y++;
966
            WCUSER_MoveSelection(data, c1, c2);
967
            return;
968 969 970 971 972 973 974 975 976
        }
        break;
    case SHIFT_PRESSED:
        switch (wParam)
        {
        case VK_RIGHT:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c2.X++;
977
            WCUSER_MoveSelection(data, c1, c2);
978
            return;
979 980 981 982
        case VK_LEFT:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c2.X--;
983
            WCUSER_MoveSelection(data, c1, c2);
984
            return;
985 986 987 988
        case VK_UP:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c2.Y--;
989
            WCUSER_MoveSelection(data, c1, c2);
990
            return;
991 992 993 994
        case VK_DOWN:
            c1 = PRIVATE(data)->selectPt1;
            c2 = PRIVATE(data)->selectPt2;
            c2.Y++;
995
            WCUSER_MoveSelection(data, c1, c2);
996
            return;
997 998 999
        }
        break;
    }
1000 1001 1002 1003 1004 1005

    if (wParam < VK_SPACE)  /* Shift, Alt, Ctrl, Num Lock etc. */
        return;
    
    WCUSER_SetSelection(data, 0);    
    PRIVATE(data)->has_selection = FALSE;
1006 1007
}

1008
/******************************************************************
1009
 *		WCUSER_GenerateKeyInputRecord
1010 1011 1012
 *
 * generates input_record from windows WM_KEYUP/WM_KEYDOWN messages
 */
1013
static void    WCUSER_GenerateKeyInputRecord(struct inner_data* data, BOOL down,
1014
                                             WPARAM wParam, LPARAM lParam)
1015 1016 1017 1018 1019
{
    INPUT_RECORD	ir;
    DWORD		n;
    WCHAR		buf[2];
    static	WCHAR	last; /* keep last char seen as feed for key up message */
1020
    BYTE		keyState[256];
1021 1022 1023 1024 1025

    ir.EventType = KEY_EVENT;
    ir.Event.KeyEvent.bKeyDown = down;
    ir.Event.KeyEvent.wRepeatCount = LOWORD(lParam);
    ir.Event.KeyEvent.wVirtualKeyCode = wParam;
1026

1027
    ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1028

1029
    ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1030
    ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1031
    if (lParam & (1L << 24))		ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1032

1033
    if (down)
1034
    {
1035 1036 1037 1038
        switch (ToUnicode(wParam, HIWORD(lParam), keyState, buf, 2, 0))
        {
        case 2:
            /* FIXME... should generate two events... */
1039
            /* fall through */
1040 1041 1042 1043 1044 1045 1046
        case 1:
            last = buf[0];
            break;
        default:
            last = 0;
            break;
        }
1047
    }
1048 1049
    ir.Event.KeyEvent.uChar.UnicodeChar = last; /* FIXME: HACKY... and buggy because it should be a stack, not a single value */
    if (!down) last = 0;
1050

1051
    WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1052 1053
}

1054 1055 1056 1057 1058
/******************************************************************
 *		WCUSER_GenerateMouseInputRecord
 *
 *
 */
1059
static void    WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
                                               WPARAM wParam, DWORD event)
{
    INPUT_RECORD	ir;
    BYTE		keyState[256];
    DWORD               mode, n;

    /* MOUSE_EVENTs shouldn't be sent unless ENABLE_MOUSE_INPUT is active */
    if (!GetConsoleMode(data->hConIn, &mode) || !(mode & ENABLE_MOUSE_INPUT))
        return;

    ir.EventType = MOUSE_EVENT;
    ir.Event.MouseEvent.dwMousePosition = c;
    ir.Event.MouseEvent.dwButtonState = 0;
    if (wParam & MK_LBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_1ST_BUTTON_PRESSED;
    if (wParam & MK_MBUTTON) ir.Event.MouseEvent.dwButtonState |= FROM_LEFT_2ND_BUTTON_PRESSED;
    if (wParam & MK_RBUTTON) ir.Event.MouseEvent.dwButtonState |= RIGHTMOST_BUTTON_PRESSED;
1076 1077 1078
    if (wParam & MK_CONTROL) ir.Event.MouseEvent.dwButtonState |= LEFT_CTRL_PRESSED;
    if (wParam & MK_SHIFT)   ir.Event.MouseEvent.dwButtonState |= SHIFT_PRESSED;
    if (event == MOUSE_WHEELED) ir.Event.MouseEvent.dwButtonState |= wParam & 0xFFFF0000;
1079 1080 1081
    ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
    ir.Event.MouseEvent.dwEventFlags = event;

1082
    WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1083 1084
}

1085 1086 1087 1088 1089 1090 1091
/******************************************************************
 *		WCUSER_Proc
 *
 *
 */
static LRESULT CALLBACK WCUSER_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
1092
    struct inner_data* data = (struct inner_data*)GetWindowLongPtrW(hWnd, 0);
1093 1094 1095 1096

    switch (uMsg)
    {
    case WM_CREATE:
1097
        return WCUSER_Create(hWnd, (LPCREATESTRUCTW)lParam);
1098
    case WM_DESTROY:
1099
	data->hWnd = 0;
1100 1101 1102 1103 1104 1105 1106
	PostQuitMessage(0);
	break;
    case WM_PAINT:
	WCUSER_Paint(data);
	break;
    case WM_KEYDOWN:
    case WM_KEYUP:
1107 1108 1109
        if (PRIVATE(data)->has_selection)
            WCUSER_HandleSelectionKey(data, uMsg == WM_KEYDOWN, wParam, lParam);
        else
1110
            WCUSER_GenerateKeyInputRecord(data, uMsg == WM_KEYDOWN, wParam, lParam);
1111 1112 1113
	break;
    case WM_SYSKEYDOWN:
    case WM_SYSKEYUP:
1114
	WCUSER_GenerateKeyInputRecord(data, uMsg == WM_SYSKEYDOWN, wParam, lParam);
1115 1116
	break;
    case WM_LBUTTONDOWN:
1117
        if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1118 1119
        {
            if (PRIVATE(data)->has_selection)
1120 1121 1122
                WCUSER_SetSelection(data, 0);
            
            if (data->curcfg.quick_edit && PRIVATE(data)->has_selection)
1123 1124 1125 1126 1127 1128
            {
                PRIVATE(data)->has_selection = FALSE;
            }
            else
            {
                PRIVATE(data)->selectPt1 = PRIVATE(data)->selectPt2 = WCUSER_GetCell(data, lParam);
1129
                SetCapture(data->hWnd);
1130 1131 1132 1133
                WCUSER_SetSelection(data, 0);
                PRIVATE(data)->has_selection = TRUE;
            }
        }
1134
        else
1135 1136 1137
        {
            WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
        }
1138 1139
	break;
    case WM_MOUSEMOVE:
1140
        if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1141
        {
1142
            if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection &&
1143 1144
                (wParam & MK_LBUTTON))
            {
1145
                WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
1146 1147 1148 1149 1150 1151
            }
        }
        else
        {
            WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, MOUSE_MOVED);
        }
1152 1153
	break;
    case WM_LBUTTONUP:
1154
        if (data->curcfg.quick_edit || PRIVATE(data)->has_selection)
1155
        {
1156
            if (GetCapture() == data->hWnd && PRIVATE(data)->has_selection)
1157
            {
1158 1159
                WCUSER_MoveSelection(data, PRIVATE(data)->selectPt1, WCUSER_GetCell(data, lParam));
                ReleaseCapture();
1160 1161 1162 1163 1164 1165
            }
        }
        else
        {
            WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
        }
1166
	break;
1167 1168 1169
    case WM_RBUTTONDOWN:
        if ((wParam & (MK_CONTROL|MK_SHIFT)) == data->curcfg.menu_mask)
        {
1170
            POINT       pt;
1171

1172 1173
            pt.x = (short)LOWORD(lParam);
            pt.y = (short)HIWORD(lParam);
1174
            ClientToScreen(hWnd, &pt);
1175
            WCUSER_SetMenuDetails(data, PRIVATE(data)->hPopMenu);
1176
            TrackPopupMenu(PRIVATE(data)->hPopMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON,
1177
                           pt.x, pt.y, 0, hWnd, NULL);
1178 1179 1180 1181 1182
        }
        else
        {
            WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
        }
1183
	break;
1184 1185 1186
    case WM_RBUTTONUP:
        /* no need to track for rbutton up when opening the popup... the event will be
         * swallowed by TrackPopupMenu */
1187 1188
    case WM_MBUTTONDOWN:
    case WM_MBUTTONUP:
1189
        WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, 0);
1190
	break;
1191 1192 1193 1194 1195
    case WM_LBUTTONDBLCLK:
    case WM_MBUTTONDBLCLK:
    case WM_RBUTTONDBLCLK:
        WCUSER_GenerateMouseInputRecord(data, WCUSER_GetCell(data, lParam), wParam, DOUBLE_CLICK);
        break;
1196
    case WM_SETFOCUS:
1197
	if (data->curcfg.cursor_visible)
1198
	{
1199
	    CreateCaret(data->hWnd, PRIVATE(data)->cursor_bitmap,
1200
                        data->curcfg.cell_width, data->curcfg.cell_height);
1201 1202
	    WCUSER_PosCursor(data);
	}
1203 1204
        break;
    case WM_KILLFOCUS:
1205
	if (data->curcfg.cursor_visible)
1206
	    DestroyCaret();
1207
	break;
1208
    case WM_HSCROLL:
1209
        {
1210
            struct config_data  cfg = data->curcfg;
1211

1212 1213
            switch (LOWORD(wParam))
            {
1214 1215 1216 1217 1218 1219
            case SB_PAGEUP: 	cfg.win_pos.X -= 8; 		break;
            case SB_PAGEDOWN: 	cfg.win_pos.X += 8; 		break;
            case SB_LINEUP: 	cfg.win_pos.X--;		break;
            case SB_LINEDOWN: 	cfg.win_pos.X++; 		break;
            case SB_THUMBTRACK: cfg.win_pos.X = HIWORD(wParam);	break;
            default: 					        break;
1220
            }
1221 1222 1223 1224
            if (cfg.win_pos.X < 0) cfg.win_pos.X = 0;
            if (cfg.win_pos.X > data->curcfg.sb_width - data->curcfg.win_width)
                cfg.win_pos.X = data->curcfg.sb_width - data->curcfg.win_width;
            if (cfg.win_pos.X != data->curcfg.win_pos.X)
1225
            {
1226
                WINECON_SetConfig(data, &cfg);
1227 1228
            }
        }
1229
	break;
1230 1231 1232 1233 1234 1235 1236
    case WM_MOUSEWHEEL:
        if (data->curcfg.sb_height <= data->curcfg.win_height)
        {
            WCUSER_GenerateMouseInputRecord(data,  WCUSER_GetCell(data, lParam), wParam, MOUSE_WHEELED);
            break;
        }
        /* else fallthrough */
1237
    case WM_VSCROLL:
1238
        {
1239
	    struct config_data  cfg = data->curcfg;
1240

1241 1242 1243 1244 1245
            if (uMsg == WM_MOUSEWHEEL)
            {
                UINT scrollLines = 3;
                SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
                scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1246
                cfg.win_pos.Y += scrollLines;
1247 1248 1249
            } else {
                switch (LOWORD(wParam))
                {
1250 1251 1252 1253 1254 1255
                case SB_PAGEUP:     cfg.win_pos.Y -= 8; 		break;
                case SB_PAGEDOWN:   cfg.win_pos.Y += 8; 		break;
                case SB_LINEUP:     cfg.win_pos.Y--;			break;
                case SB_LINEDOWN:   cfg.win_pos.Y++;	 		break;
                case SB_THUMBTRACK: cfg.win_pos.Y = HIWORD(wParam);	break;
                default: 					        break;
1256 1257 1258
                }
            }

1259 1260 1261 1262
	    if (cfg.win_pos.Y < 0) cfg.win_pos.Y = 0;
	    if (cfg.win_pos.Y > data->curcfg.sb_height - data->curcfg.win_height)
                cfg.win_pos.Y = data->curcfg.sb_height - data->curcfg.win_height;
	    if (cfg.win_pos.Y != data->curcfg.win_pos.Y)
1263
	    {
1264
                WINECON_SetConfig(data, &cfg);
1265
	    }
1266
        }
1267
        break;
1268 1269 1270 1271
    case WM_SYSCOMMAND:
	switch (wParam)
	{
	case IDS_DEFAULT:
1272
	    WCUSER_GetProperties(data, FALSE);
1273
	    break;
1274
	case IDS_PROPERTIES:
1275
	    WCUSER_GetProperties(data, TRUE);
1276
	    break;
1277
	default:
1278
            return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1279 1280 1281 1282 1283
	}
	break;
    case WM_COMMAND:
	switch (wParam)
	{
1284 1285 1286
	case IDS_DEFAULT:
	    WCUSER_GetProperties(data, FALSE);
	    break;
1287
	case IDS_PROPERTIES:
1288 1289
	    WCUSER_GetProperties(data, TRUE);
	    break;
1290
	case IDS_MARK:
1291 1292 1293 1294 1295
            PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
            PRIVATE(data)->selectPt2.X = PRIVATE(data)->selectPt2.Y = 0;
            WCUSER_SetSelection(data, 0);
            PRIVATE(data)->has_selection = TRUE;
	    break;
1296
	case IDS_COPY:
1297 1298 1299 1300 1301 1302
            if (PRIVATE(data)->has_selection)
            {
                PRIVATE(data)->has_selection = FALSE;
                WCUSER_SetSelection(data, 0);
                WCUSER_CopySelectionToClipboard(data);
            }
1303 1304 1305 1306 1307
	    break;
	case IDS_PASTE:
	    WCUSER_PasteFromClipboard(data);
	    break;
	case IDS_SELECTALL:
1308 1309 1310 1311 1312
            PRIVATE(data)->selectPt1.X = PRIVATE(data)->selectPt1.Y = 0;
            PRIVATE(data)->selectPt2.X = (data->curcfg.sb_width - 1) * data->curcfg.cell_width;
            PRIVATE(data)->selectPt2.Y = (data->curcfg.sb_height - 1) * data->curcfg.cell_height;
            WCUSER_SetSelection(data, 0);
            PRIVATE(data)->has_selection = TRUE;
1313 1314 1315
	    break;
	case IDS_SCROLL:
	case IDS_SEARCH:
1316
	    WINE_FIXME("Unhandled yet command: %lx\n", wParam);
1317
	    break;
1318
	default:
1319
            return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1320 1321 1322
	}
	break;
    case WM_INITMENUPOPUP:
1323
        if (!HIWORD(lParam)) return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1324
	WCUSER_SetMenuDetails(data, GetSystemMenu(data->hWnd, FALSE));
1325
	break;
1326 1327 1328 1329
    case WM_SIZE:
        WINECON_ResizeWithContainer(data, LOWORD(lParam) / data->curcfg.cell_width,
                                    HIWORD(lParam) / data->curcfg.cell_height);
        break;
1330
    default:
1331
        return DefWindowProcW(hWnd, uMsg, wParam, lParam);
1332 1333 1334 1335 1336 1337 1338 1339 1340
    }
    return 0;
}

/******************************************************************
 *		WCUSER_DeleteBackend
 *
 *
 */
1341
static void WCUSER_DeleteBackend(struct inner_data* data)
1342
{
1343
    if (!PRIVATE(data)) return;
1344
    if (PRIVATE(data)->hMemDC)		DeleteDC(PRIVATE(data)->hMemDC);
1345
    if (data->hWnd)		        DestroyWindow(data->hWnd);
1346 1347 1348 1349
    if (PRIVATE(data)->hFont)		DeleteObject(PRIVATE(data)->hFont);
    if (PRIVATE(data)->cursor_bitmap)	DeleteObject(PRIVATE(data)->cursor_bitmap);
    if (PRIVATE(data)->hBitmap)		DeleteObject(PRIVATE(data)->hBitmap);
    HeapFree(GetProcessHeap(), 0, PRIVATE(data));
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
}

/******************************************************************
 *		WCUSER_MainLoop
 *
 *
 */
static int WCUSER_MainLoop(struct inner_data* data)
{
    MSG		msg;

1361
    ShowWindow(data->hWnd, data->nCmdShow);
1362
    while (!data->dying || !data->curcfg.exit_on_die)
1363
    {
1364
	switch (MsgWaitForMultipleObjects(1, &data->hSynchro, FALSE, INFINITE, QS_ALLINPUT))
1365 1366
	{
	case WAIT_OBJECT_0:
1367
	    WINECON_GrabChanges(data);
1368 1369
	    break;
	case WAIT_OBJECT_0+1:
1370
            /* need to use PeekMessageW loop instead of simple GetMessage:
1371 1372
	     * multiple messages might have arrived in between,
	     * so GetMessage would lead to delayed processing */
1373
            while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
1374
	    {
1375
                if (msg.message == WM_QUIT) return 1;
1376
                WINE_TRACE("dispatching msg %04x\n", msg.message);
1377
                DispatchMessageW(&msg);
1378 1379 1380
	    }
	    break;
	default:
1381
	    WINE_ERR("got pb\n");
1382 1383 1384
	    /* err */
	    break;
	}
1385
    }
1386 1387
    PostQuitMessage(0);
    return 0;
1388 1389 1390 1391 1392 1393 1394 1395
}

/******************************************************************
 *		WCUSER_InitBackend
 *
 * Initialisation part II: creation of window.
 *
 */
1396
enum init_return WCUSER_InitBackend(struct inner_data* data)
1397
{
1398
    static const WCHAR wClassName[] = {'W','i','n','e','C','o','n','s','o','l','e','C','l','a','s','s',0};
1399

1400 1401 1402
    WNDCLASSW   wndclass;
    CHARSETINFO ci;

1403 1404 1405 1406
    if (!TranslateCharsetInfo((DWORD *)(INT_PTR)GetACP(), &ci, TCI_SRCCODEPAGE))
        return init_failed;
    g_uiDefaultCharset = ci.ciCharset;
    WINE_TRACE_(wc_font)("Code page %d => Default charset: %d\n", GetACP(), g_uiDefaultCharset);
1407

1408
    data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1409
    if (!data->private) return init_failed;
1410 1411 1412 1413 1414 1415 1416 1417

    data->fnMainLoop = WCUSER_MainLoop;
    data->fnPosCursor = WCUSER_PosCursor;
    data->fnShapeCursor = WCUSER_ShapeCursor;
    data->fnComputePositions = WCUSER_ComputePositions;
    data->fnRefresh = WCUSER_Refresh;
    data->fnResizeScreenBuffer = WCUSER_ResizeScreenBuffer;
    data->fnSetTitle = WCUSER_SetTitle;
1418
    data->fnSetFont = WCUSER_SetFontPmt;
1419 1420 1421
    data->fnScroll = WCUSER_Scroll;
    data->fnDeleteBackend = WCUSER_DeleteBackend;

1422
    wndclass.style         = CS_DBLCLKS;
1423 1424
    wndclass.lpfnWndProc   = WCUSER_Proc;
    wndclass.cbClsExtra    = 0;
1425
    wndclass.cbWndExtra    = sizeof(DWORD_PTR);
1426 1427 1428
    wndclass.hInstance     = GetModuleHandleW(NULL);
    wndclass.hIcon         = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
    wndclass.hCursor       = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
1429 1430 1431
    wndclass.hbrBackground = GetStockObject(BLACK_BRUSH);
    wndclass.lpszMenuName  = NULL;
    wndclass.lpszClassName = wClassName;
1432

1433
    RegisterClassW(&wndclass);
1434

1435 1436 1437
    data->hWnd = CreateWindowW(wndclass.lpszClassName, NULL,
                               WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_HSCROLL|WS_VSCROLL,
                               CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, 0, 0, wndclass.hInstance, data);
1438
    if (!data->hWnd) return init_not_supported;
1439

1440
    return init_success;
1441
}