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)))) return;
60
    dx = HeapAlloc( GetProcessHeap(), 0, data->curcfg.sb_width * sizeof(*dx) );
61

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

274
void WCUSER_DumpLogFont(const char* pfx, const LOGFONTW* lf, DWORD ft)
275 276
{
    WINE_TRACE_(wc_font)("%s %s%s%s%s\n"
277 278
                         "\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"
279 280 281
                         "\tlf.lfCharSet=%u lf.lfOutPrecision=%u lf.lfClipPrecision=%u lf.lfQuality=%u\n"
                         "\tlf->lfPitchAndFamily=%u lf.lfFaceName=%s\n",
                         pfx,
282 283 284 285 286
                         (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,
287
                         lf->lfWeight, lf->lfItalic, lf->lfUnderline, lf->lfStrikeOut, lf->lfCharSet,
288
                         lf->lfOutPrecision, lf->lfClipPrecision, lf->lfQuality, lf->lfPitchAndFamily,
289 290 291
                         wine_dbgstr_w(lf->lfFaceName));
}

292
void WCUSER_DumpTextMetric(const TEXTMETRICW* tm, DWORD ft)
293 294
{
    WINE_TRACE_(wc_font)("%s%s%s%s\n"
295 296 297
                         "\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"
298 299
                         "\ttmFirstChar=%d tmLastChar=%d tmDefaultChar=%d tmBreakChar=%d\n"
                         "\ttmItalic=%u tmUnderlined=%u tmStruckOut=%u tmPitchAndFamily=%u tmCharSet=%u\n",
300 301 302 303 304 305 306
                         (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,
307 308 309
                         tm->tmPitchAndFamily, tm->tmCharSet);
}

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

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

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

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

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

396
    WCUSER_DumpTextMetric(tm, FontType);
397
    if (WCUSER_ValidateFontMetric(fc->data, tm, FontType, fc->pass))
398
    {
399
        LOGFONTW mlf = *lf;
400 401

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

411 412
            WCUSER_DumpLogFont("InitChoosing: ", &mlf, FontType);
            fc->done = 1;
413
            /* since we've modified the current config with new font information,
414 415
             * set this information as the new default.
             */
416 417 418 419
            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);
420 421 422
            /* Force also its writing back to the registry so that we can get it
             * the next time.
             */
423
            WINECON_RegSave(&defcfg);
424 425
            return 0;
        }
426 427 428 429
    }
    return 1;
}

430 431
static int CALLBACK get_first_font_enum(const LOGFONTW* lf, const TEXTMETRICW* tm,
                                        DWORD FontType, LPARAM lParam)
432 433 434
{
    struct font_chooser*	fc = (struct font_chooser*)lParam;

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

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

458
    if (!(hDC = GetDC(hWnd))) return NULL;
459
    if (!(hFont = CreateFontIndirectW(lf)))
460
    {
461 462
        ReleaseDC(hWnd, hDC);
        return NULL;
463
    }
464 465
    hOldFont = SelectObject(hDC, hFont);
    GetTextMetricsW(hDC, &tm);
466 467 468
    SelectObject(hDC, hOldFont);
    ReleaseDC(hWnd, hDC);

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

475 476 477 478
    /* 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;

479 480 481 482 483 484 485 486
    return hFont;
}

/******************************************************************
 *		WCUSER_FillLogFont
 *
 *
 */
487
void WCUSER_FillLogFont(LOGFONTW* lf, const WCHAR* name, UINT height, UINT weight)
488 489 490 491 492 493 494 495 496 497 498
{
    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;
499
    lf->lfClipPrecision = CLIP_DEFAULT_PRECIS;
500 501
    lf->lfQuality       = DEFAULT_QUALITY;
    lf->lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
502
    lstrcpyW(lf->lfFaceName, name);
503 504 505 506 507 508 509
}

/******************************************************************
 *		WCUSER_SetFont
 *
 * sets logfont as the new font for the console
 */
510
BOOL WCUSER_SetFont(struct inner_data* data, const LOGFONTW* logfont)
511
{
512
    HFONT       hFont;
513
    LONG        el;
514

515
    if (PRIVATE(data)->hFont != 0 && WCUSER_AreFontsEqual(&data->curcfg, logfont))
516
        return TRUE;
517

518
    hFont = WCUSER_CopyFont(&data->curcfg, data->hWnd, logfont, &el);
519 520 521 522
    if (!hFont) {WINE_ERR("wrong font\n"); return FALSE;}

    if (PRIVATE(data)->hFont) DeleteObject(PRIVATE(data)->hFont);
    PRIVATE(data)->hFont = hFont;
523
    PRIVATE(data)->ext_leading = el;
524

525
    WCUSER_ComputePositions(data);
526
    WCUSER_NewBitmap(data);
527 528
    InvalidateRect(data->hWnd, NULL, FALSE);
    UpdateWindow(data->hWnd);
529

530 531 532
    return TRUE;
}

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

545 546
    WINE_TRACE_(wc_font)("=> %s h=%u w=%u\n",
                         wine_dbgstr_wn(font, -1), height, weight);
547

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

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

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

580 581
    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;
582 583 584 585 586 587 588 589 590 591 592

    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
593 594 595 596
    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;
597 598 599 600 601 602 603 604 605 606 607 608 609
}

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

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

/******************************************************************
 *		WCUSER_MoveSelection
 *
 *
 */
628
static void	WCUSER_MoveSelection(struct inner_data* data, COORD c1, COORD c2)
629 630 631 632
{
    RECT	r;
    HDC		hDC;

633 634 635 636 637 638
    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;

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

/******************************************************************
 *		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;

670 671
    w = abs(PRIVATE(data)->selectPt1.X - PRIVATE(data)->selectPt2.X) + 2;
    h = abs(PRIVATE(data)->selectPt1.Y - PRIVATE(data)->selectPt2.Y) + 1;
672

673
    if (!OpenClipboard(data->hWnd)) return;
674 675
    EmptyClipboard();

Eric Pouech's avatar
Eric Pouech committed
676
    hMem = GlobalAlloc(GMEM_MOVEABLE, (w * h) * sizeof(WCHAR));
677 678 679 680 681
    if (hMem && (p = GlobalLock(hMem)))
    {
	COORD	c;
	int	y;

Eric Pouech's avatar
Eric Pouech committed
682 683
	c.X = min(PRIVATE(data)->selectPt1.X, PRIVATE(data)->selectPt2.X);
	c.Y = min(PRIVATE(data)->selectPt1.Y, PRIVATE(data)->selectPt2.Y);
684

685 686
	for (y = 0; y < h; y++, c.Y++)
	{
687
	    LPWSTR end;
688 689 690
	    DWORD count;

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

692 693 694 695 696 697
	    /* 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;
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
	}
	GlobalUnlock(hMem);
	SetClipboardData(CF_UNICODETEXT, hMem);
    }
    CloseClipboard();
}

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

715
    if (!OpenClipboard(data->hWnd)) return;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    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...) */
733
            sh = VkKeyScanW(ptr[i]);
734
	    ir[0].Event.KeyEvent.wVirtualKeyCode = LOBYTE(sh);
735
            ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKeyW(LOBYTE(sh), 0);
736
	    ir[0].Event.KeyEvent.uChar.UnicodeChar = ptr[i];
737

738 739
	    ir[1] = ir[0];
	    ir[1].Event.KeyEvent.bKeyDown = FALSE;
740

741
            WriteConsoleInputW(data->hConIn, ir, 2, &n);
742 743 744 745 746 747
	}
	GlobalUnlock(h);
    }
    CloseClipboard();
}

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

	r.left   = 0;
761 762 763
	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;
764 765
	InvalidateRect(data->hWnd, &r, FALSE);
	UpdateWindow(data->hWnd);
766 767 768 769 770 771 772 773 774 775 776 777
    }
}

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

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

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

/******************************************************************
815 816
 *		WCUSER_FillMenu
 *
817 818
 *
 */
819
static BOOL WCUSER_FillMenu(HMENU hMenu, BOOL sep)
820
{
821 822 823
    HMENU     hSubMenu;
    HINSTANCE hInstance = GetModuleHandleW(NULL);
    WCHAR     buff[256];
824

825
    if (!hMenu) return FALSE;
826 827 828

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

831
    LoadStringW(hInstance, IDS_MARK, buff, ARRAY_SIZE(buff));
832
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_MARK, buff);
833
    LoadStringW(hInstance, IDS_COPY, buff, ARRAY_SIZE(buff));
834
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_COPY, buff);
835
    LoadStringW(hInstance, IDS_PASTE, buff, ARRAY_SIZE(buff));
836
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PASTE, buff);
837
    LoadStringW(hInstance, IDS_SELECTALL, buff, ARRAY_SIZE(buff));
838
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SELECTALL, buff);
839
    LoadStringW(hInstance, IDS_SCROLL, buff, ARRAY_SIZE(buff));
840
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SCROLL, buff);
841
    LoadStringW(hInstance, IDS_SEARCH, buff, ARRAY_SIZE(buff));
842 843 844
    InsertMenuW(hSubMenu, -1, MF_BYPOSITION|MF_STRING, IDS_SEARCH, buff);

    if (sep) InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_SEPARATOR, 0, NULL);
845
    LoadStringW(hInstance, IDS_EDIT, buff, ARRAY_SIZE(buff));
846
    InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING|MF_POPUP, (UINT_PTR)hSubMenu, buff);
847
    LoadStringW(hInstance, IDS_DEFAULT, buff, ARRAY_SIZE(buff));
848
    InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_DEFAULT, buff);
849
    LoadStringW(hInstance, IDS_PROPERTIES, buff, ARRAY_SIZE(buff));
850
    InsertMenuW(hMenu, -1, MF_BYPOSITION|MF_STRING, IDS_PROPERTIES, buff);
851

852 853 854
    return TRUE;
}

855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
/******************************************************************
 *		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);
}

873 874 875 876 877
/******************************************************************
 *		WCUSER_Create
 *
 * Creates the window for the rendering
 */
878
static LRESULT WCUSER_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
879 880 881 882 883
{
    struct inner_data*	data;
    HMENU		hSysMenu;

    data = lpcs->lpCreateParams;
884
    SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)data);
885
    data->hWnd = hWnd;
886 887 888 889 890 891 892 893 894 895

    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);
896
    if (!PRIVATE(data)->hMemDC) {WINE_ERR("no mem dc\n");return 0;}
897

898
    data->curcfg.quick_edit = FALSE;
899 900 901
    return 0;
}

902
/******************************************************************
903
 *		WCUSER_GetCtrlKeyState
904 905 906 907 908 909 910 911 912 913 914 915 916
 *
 * 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;
917
    if (keyState[VK_CAPITAL]  & 0x01)	ret |= CAPSLOCK_ON;
918 919
    if (keyState[VK_NUMLOCK]  & 0x01)	ret |= NUMLOCK_ON;
    if (keyState[VK_SCROLL]   & 0x01)	ret |= SCROLLLOCK_ON;
920

921 922 923 924 925 926 927 928
    return ret;
}

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

936
    if (!down) return;
937 938 939 940 941 942 943 944 945 946

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

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

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

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

1031
    ir.Event.KeyEvent.wVirtualScanCode = HIWORD(lParam) & 0xFF;
1032

1033
    ir.Event.KeyEvent.uChar.UnicodeChar = 0;
1034
    ir.Event.KeyEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
1035
    if (lParam & (1L << 24))		ir.Event.KeyEvent.dwControlKeyState |= ENHANCED_KEY;
1036

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

1055
    WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1056 1057
}

1058 1059 1060 1061 1062
/******************************************************************
 *		WCUSER_GenerateMouseInputRecord
 *
 *
 */
1063
static void    WCUSER_GenerateMouseInputRecord(struct inner_data* data, COORD c,
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
                                               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;
1080 1081 1082
    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;
1083 1084 1085
    ir.Event.MouseEvent.dwControlKeyState = WCUSER_GetCtrlKeyState(keyState);
    ir.Event.MouseEvent.dwEventFlags = event;

1086
    WriteConsoleInputW(data->hConIn, &ir, 1, &n);
1087 1088
}

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

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

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

1216 1217
            switch (LOWORD(wParam))
            {
1218 1219 1220 1221 1222 1223
            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;
1224
            }
1225 1226 1227 1228
            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)
1229
            {
1230
                WINECON_SetConfig(data, &cfg);
1231 1232
            }
        }
1233
	break;
1234 1235 1236 1237 1238 1239 1240
    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 */
1241
    case WM_VSCROLL:
1242
        {
1243
	    struct config_data  cfg = data->curcfg;
1244

1245 1246 1247 1248 1249
            if (uMsg == WM_MOUSEWHEEL)
            {
                UINT scrollLines = 3;
                SystemParametersInfoW(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, 0);
                scrollLines *= -GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
1250
                cfg.win_pos.Y += scrollLines;
1251 1252 1253
            } else {
                switch (LOWORD(wParam))
                {
1254 1255 1256 1257 1258 1259
                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;
1260 1261 1262
                }
            }

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

/******************************************************************
 *		WCUSER_DeleteBackend
 *
 *
 */
1345
static void WCUSER_DeleteBackend(struct inner_data* data)
1346
{
1347
    if (!PRIVATE(data)) return;
1348
    if (PRIVATE(data)->hMemDC)		DeleteDC(PRIVATE(data)->hMemDC);
1349
    if (data->hWnd)		        DestroyWindow(data->hWnd);
1350 1351 1352 1353
    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));
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
}

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

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

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

1404 1405 1406
    WNDCLASSW   wndclass;
    CHARSETINFO ci;

1407 1408 1409 1410
    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);
1411

1412
    data->private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct inner_data_user));
1413
    if (!data->private) return init_failed;
1414 1415 1416 1417 1418 1419 1420 1421

    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;
1422
    data->fnSetFont = WCUSER_SetFontPmt;
1423 1424 1425
    data->fnScroll = WCUSER_Scroll;
    data->fnDeleteBackend = WCUSER_DeleteBackend;

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

1437
    RegisterClassW(&wndclass);
1438

1439 1440 1441
    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);
1442
    if (!data->hWnd) return init_not_supported;
1443

1444
    return init_success;
1445
}