dialog.c 38.7 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
2
 *  Notepad (dialog.c)
Alexandre Julliard's avatar
Alexandre Julliard committed
3
 *
4
 *  Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5
 *  Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
6
 *  Copyright 2002 Andriy Palamarchuk
7
 *  Copyright 2007 Rolf Kalbermatter
8
 *  Copyright 2010 Vitaly Perov
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
23 24
 */

25
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <stdio.h>
27
#include <windows.h>
28
#include <shellapi.h>
29
#include <commdlg.h>
30
#include <shlwapi.h>
31
#include <winternl.h>
32

Alexandre Julliard's avatar
Alexandre Julliard committed
33 34
#include "main.h"
#include "dialog.h"
35

36
#define SPACES_IN_TAB 8
37
#define PRINT_LEN_MAX 500
38

39
static const WCHAR helpfileW[] = { 'n','o','t','e','p','a','d','.','h','l','p',0 };
Alexandre Julliard's avatar
Alexandre Julliard committed
40

41
static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
42

43 44 45 46 47 48 49
/* Swap bytes of WCHAR buffer (big-endian <-> little-endian). */
static inline void byteswap_wide_string(LPWSTR str, UINT num)
{
    UINT i;
    for (i = 0; i < num; i++) str[i] = RtlUshortByteSwap(str[i]);
}

50 51 52 53 54 55 56 57 58 59 60 61
static void load_encoding_name(ENCODING enc, WCHAR* buffer, int length)
{
    switch (enc)
    {
        case ENCODING_UTF16LE:
            LoadStringW(Globals.hInstance, STRING_UNICODE_LE, buffer, length);
            break;

        case ENCODING_UTF16BE:
            LoadStringW(Globals.hInstance, STRING_UNICODE_BE, buffer, length);
            break;

62 63 64 65 66
        case ENCODING_UTF8:
            LoadStringW(Globals.hInstance, STRING_UTF8, buffer, length);
            break;

        case ENCODING_ANSI:
67 68
        {
            CPINFOEXW cpi;
69
            GetCPInfoExW(CP_ACP, 0, &cpi);
70 71 72
            lstrcpynW(buffer, cpi.CodePageName, length);
            break;
        }
73 74 75 76

        default:
            assert(0 && "bad encoding in load_encoding_name");
            break;
77 78 79
    }
}

80
VOID ShowLastError(void)
81 82 83 84
{
    DWORD error = GetLastError();
    if (error != NO_ERROR)
    {
85 86
        LPWSTR lpMsgBuf;
        WCHAR szTitle[MAX_STRING_LEN];
87

88
        LoadStringW(Globals.hInstance, STRING_ERROR, szTitle, ARRAY_SIZE(szTitle));
89
        FormatMessageW(
90
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
91
            NULL, error, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
92
        MessageBoxW(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
93 94 95 96 97 98
        LocalFree(lpMsgBuf);
    }
}

/**
 * Sets the caption of the main window according to Globals.szFileTitle:
99 100
 *    Untitled - Notepad        if no file is open
 *    filename - Notepad        if a file is given
101
 */
102
void UpdateWindowCaption(void)
103
{
104
  static const WCHAR hyphenW[] = { ' ','-',' ',0 };
105 106
  WCHAR szNotepad[64];
  WCHAR szCaption[ARRAY_SIZE(Globals.szFileTitle) + ARRAY_SIZE(hyphenW) + ARRAY_SIZE(szNotepad)];
107

108
  if (Globals.szFileTitle[0] != '\0')
109
      lstrcpyW(szCaption, Globals.szFileTitle);
110
  else
111
      LoadStringW(Globals.hInstance, STRING_UNTITLED, szCaption, ARRAY_SIZE(szCaption));
112

113
  LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
114 115
  lstrcatW(szCaption, hyphenW);
  lstrcatW(szCaption, szNotepad);
116

117
  SetWindowTextW(Globals.hMainWnd, szCaption);
118 119
}

120
int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
121 122 123
{
   WCHAR szMessage[MAX_STRING_LEN];
   WCHAR szResource[MAX_STRING_LEN];
124 125

   /* Load and format szMessage */
126 127
   LoadStringW(Globals.hInstance, formatId, szResource, ARRAY_SIZE(szResource));
   wnsprintfW(szMessage, ARRAY_SIZE(szMessage), szResource, szString);
128

129
   /* Load szCaption */
130
   if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
131
     LoadStringW(Globals.hInstance, STRING_ERROR,  szResource, ARRAY_SIZE(szResource));
132
   else
133
     LoadStringW(Globals.hInstance, STRING_NOTEPAD,  szResource, ARRAY_SIZE(szResource));
134 135

   /* Display Modal Dialog */
136 137
   if (hParent == NULL)
     hParent = Globals.hMainWnd;
138
   return MessageBoxW(hParent, szMessage, szResource, dwFlags);
139 140 141 142 143
}

static void AlertFileNotFound(LPCWSTR szFileName)
{
   DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, szFileName, MB_ICONEXCLAMATION|MB_OK);
Alexandre Julliard's avatar
Alexandre Julliard committed
144 145
}

146 147 148
static int AlertFileNotSaved(LPCWSTR szFileName)
{
   WCHAR szUntitled[MAX_STRING_LEN];
Alexandre Julliard's avatar
Alexandre Julliard committed
149

150
   LoadStringW(Globals.hInstance, STRING_UNTITLED, szUntitled, ARRAY_SIZE(szUntitled));
151 152
   return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
     MB_ICONQUESTION|MB_YESNOCANCEL);
153 154
}

155 156
static int AlertUnicodeCharactersLost(LPCWSTR szFileName)
{
157
    WCHAR szCaption[MAX_STRING_LEN];
158 159
    WCHAR szMsgFormat[MAX_STRING_LEN];
    WCHAR szEnc[MAX_STRING_LEN];
160 161 162
    WCHAR* szMsg;
    DWORD_PTR args[2];
    int rc;
163

164 165
    LoadStringW(Globals.hInstance, STRING_NOTEPAD, szCaption,
                ARRAY_SIZE(szCaption));
166 167 168
    LoadStringW(Globals.hInstance, STRING_LOSS_OF_UNICODE_CHARACTERS,
                szMsgFormat, ARRAY_SIZE(szMsgFormat));
    load_encoding_name(ENCODING_ANSI, szEnc, ARRAY_SIZE(szEnc));
169 170 171 172 173 174 175
    args[0] = (DWORD_PTR)szFileName;
    args[1] = (DWORD_PTR)szEnc;
    FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_ARGUMENT_ARRAY, szMsgFormat, 0, 0, (LPWSTR)&szMsg, 0, (__ms_va_list*)args);
    rc = MessageBoxW(Globals.hMainWnd, szMsg, szCaption,
                     MB_OKCANCEL|MB_ICONEXCLAMATION);
    LocalFree(szMsg);
    return rc;
176 177
}

178 179 180 181
/**
 * Returns:
 *   TRUE  - if file exists
 *   FALSE - if file does not exist
Alexandre Julliard's avatar
Alexandre Julliard committed
182
 */
183 184
BOOL FileExists(LPCWSTR szFilename)
{
185
   WIN32_FIND_DATAW entry;
186
   HANDLE hFile;
187

188
   hFile = FindFirstFileW(szFilename, &entry);
189
   FindClose(hFile);
190

191
   return (hFile != INVALID_HANDLE_VALUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
192 193
}

194 195 196 197 198 199 200
static inline BOOL is_conversion_to_ansi_lossy(LPCWSTR textW, int lenW)
{
    BOOL ret = FALSE;
    WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, textW, lenW, NULL, 0,
                        NULL, &ret);
    return ret;
}
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
typedef enum
{
    SAVED_OK,
    SAVE_FAILED,
    SHOW_SAVEAS_DIALOG
} SAVE_STATUS;

/* szFileName is the filename to save under; enc is the encoding to use.
 *
 * If the function succeeds, it returns SAVED_OK.
 * If the function fails, it returns SAVE_FAILED.
 * If Unicode data could be lost due to conversion to a non-Unicode character
 * set, a warning is displayed. The user can continue (and the function carries
 * on), or cancel (and the function returns SHOW_SAVEAS_DIALOG).
 */
static SAVE_STATUS DoSaveFile(LPCWSTR szFileName, ENCODING enc)
218
{
219 220
    int lenW;
    WCHAR* textW;
221 222
    HANDLE hFile;
    DWORD dwNumWrite;
223
    PVOID pBytes;
224
    DWORD size;
225

226 227 228 229 230 231
    /* lenW includes the byte-order mark, but not the \0. */
    lenW = GetWindowTextLengthW(Globals.hEdit) + 1;
    textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
    if (!textW)
    {
        ShowLastError();
232
        return SAVE_FAILED;
233 234 235 236
    }
    textW[0] = (WCHAR) 0xfeff;
    lenW = GetWindowTextW(Globals.hEdit, textW+1, lenW) + 1;

237
    switch (enc)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    {
    case ENCODING_UTF16BE:
        byteswap_wide_string(textW, lenW);
        /* fall through */

    case ENCODING_UTF16LE:
        size = lenW * sizeof(WCHAR);
        pBytes = textW;
        break;

    case ENCODING_UTF8:
        size = WideCharToMultiByte(CP_UTF8, 0, textW, lenW, NULL, 0, NULL, NULL);
        pBytes = HeapAlloc(GetProcessHeap(), 0, size);
        if (!pBytes)
        {
            ShowLastError();
            HeapFree(GetProcessHeap(), 0, textW);
255
            return SAVE_FAILED;
256 257 258 259 260 261
        }
        WideCharToMultiByte(CP_UTF8, 0, textW, lenW, pBytes, size, NULL, NULL);
        HeapFree(GetProcessHeap(), 0, textW);
        break;

    default:
262 263 264 265 266 267 268
        if (is_conversion_to_ansi_lossy(textW+1, lenW-1)
            && AlertUnicodeCharactersLost(szFileName) == IDCANCEL)
        {
            HeapFree(GetProcessHeap(), 0, textW);
            return SHOW_SAVEAS_DIALOG;
        }

269 270 271 272 273 274
        size = WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, NULL, 0, NULL, NULL);
        pBytes = HeapAlloc(GetProcessHeap(), 0, size);
        if (!pBytes)
        {
            ShowLastError();
            HeapFree(GetProcessHeap(), 0, textW);
275
            return SAVE_FAILED;
276 277 278 279 280 281
        }
        WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, pBytes, size, NULL, NULL);
        HeapFree(GetProcessHeap(), 0, textW);
        break;
    }

282
    hFile = CreateFileW(szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
283 284 285 286
                       NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
        ShowLastError();
287
        HeapFree(GetProcessHeap(), 0, pBytes);
288
        return SAVE_FAILED;
289
    }
290
    if (!WriteFile(hFile, pBytes, size, &dwNumWrite, NULL))
291 292
    {
        ShowLastError();
293 294
        CloseHandle(hFile);
        HeapFree(GetProcessHeap(), 0, pBytes);
295
        return SAVE_FAILED;
296
    }
297
    SetEndOfFile(hFile);
298
    CloseHandle(hFile);
299 300 301
    HeapFree(GetProcessHeap(), 0, pBytes);

    SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
302
    return SAVED_OK;
303
}
Alexandre Julliard's avatar
Alexandre Julliard committed
304

305 306 307 308 309
/**
 * Returns:
 *   TRUE  - User agreed to close (both save/don't save)
 *   FALSE - User cancelled close by selecting "Cancel"
 */
310 311
BOOL DoCloseFile(void)
{
312
    int nResult;
313
    static const WCHAR empty_strW[] = { 0 };
314

315 316 317
    nResult=GetWindowTextLengthW(Globals.hEdit);
    if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0) &&
        (nResult || Globals.szFileName[0]))
318
    {
319 320 321
        /* prompt user to save changes */
        nResult = AlertFileNotSaved(Globals.szFileName);
        switch (nResult) {
322
            case IDYES:     return DIALOG_FileSave();
Alexandre Julliard's avatar
Alexandre Julliard committed
323

324
            case IDNO:      break;
Alexandre Julliard's avatar
Alexandre Julliard committed
325

326
            case IDCANCEL:  return(FALSE);
327

328 329 330
            default:        return(FALSE);
        } /* switch */
    } /* if */
331

332
    SetFileNameAndEncoding(empty_strW, ENCODING_ANSI);
333 334

    UpdateWindowCaption();
335
    return(TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
336 337
}

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
static inline ENCODING detect_encoding_of_buffer(const void* buffer, int size)
{
    static const char bom_utf8[] = { 0xef, 0xbb, 0xbf };
    if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
        return ENCODING_UTF8;
    else
    {
        int flags = IS_TEXT_UNICODE_SIGNATURE |
                    IS_TEXT_UNICODE_REVERSE_SIGNATURE |
                    IS_TEXT_UNICODE_ODD_LENGTH;
        IsTextUnicode(buffer, size, &flags);
        if (flags & IS_TEXT_UNICODE_SIGNATURE)
            return ENCODING_UTF16LE;
        else if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
            return ENCODING_UTF16BE;
        else
            return ENCODING_ANSI;
    }
}

358
void DoOpenFile(LPCWSTR szFileName, ENCODING enc)
359
{
360
    static const WCHAR dotlog[] = { '.','L','O','G',0 };
361 362 363 364
    HANDLE hFile;
    LPSTR pTemp;
    DWORD size;
    DWORD dwNumRead;
365 366
    int lenW;
    WCHAR* textW;
367
    int i;
368
    WCHAR log[5];
369

370
    /* Close any files and prompt to save changes */
371 372 373
    if (!DoCloseFile())
	return;

374
    hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
375
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
376
    if(hFile == INVALID_HANDLE_VALUE)
377
    {
378
	AlertFileNotFound(szFileName);
379 380
	return;
    }
381

382
    size = GetFileSize(hFile, NULL);
383
    if (size == INVALID_FILE_SIZE)
384 385 386 387 388
    {
	CloseHandle(hFile);
	ShowLastError();
	return;
    }
389

390 391
    /* Extra memory for (WCHAR)'\0'-termination. */
    pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
392 393 394 395 396 397 398 399 400 401 402 403 404
    if (!pTemp)
    {
	CloseHandle(hFile);
	ShowLastError();
	return;
    }

    if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
    {
	CloseHandle(hFile);
	HeapFree(GetProcessHeap(), 0, pTemp);
	ShowLastError();
	return;
Alexandre Julliard's avatar
Alexandre Julliard committed
405
    }
406 407 408

    CloseHandle(hFile);

409 410
    size = dwNumRead;

411 412 413 414 415 416 417 418 419 420 421 422
    if (enc == ENCODING_AUTO)
        enc = detect_encoding_of_buffer(pTemp, size);
    else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE))
    {
        /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM,
         * override the selection (like native Notepad).
         */
        if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe)
            enc = ENCODING_UTF16LE;
        else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff)
            enc = ENCODING_UTF16BE;
    }
423 424 425 426 427

    switch (enc)
    {
    case ENCODING_UTF16BE:
        byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR));
428 429 430
        /* Forget whether the file is BE or LE, like native Notepad. */
        enc = ENCODING_UTF16LE;

431 432 433
        /* fall through */

    case ENCODING_UTF16LE:
434 435
        textW = (LPWSTR)pTemp;
        lenW  = size/sizeof(WCHAR);
436 437 438
        break;

    default:
439 440 441 442 443 444 445 446 447 448 449 450 451 452
        {
            int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP;
            lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0);
            textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR));
            if (!textW)
            {
                ShowLastError();
                HeapFree(GetProcessHeap(), 0, pTemp);
                return;
            }
            MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW);
            HeapFree(GetProcessHeap(), 0, pTemp);
            break;
        }
453 454
    }

455 456 457 458 459 460
    /* Replace '\0's with spaces. Other than creating a custom control that
     * can deal with '\0' characters, it's the best that can be done.
     */
    for (i = 0; i < lenW; i++)
        if (textW[i] == '\0')
            textW[i] = ' ';
461
    textW[lenW] = '\0';
462

463 464 465 466 467 468
    if (lenW >= 1 && textW[0] == 0xfeff)
        SetWindowTextW(Globals.hEdit, textW+1);
    else
        SetWindowTextW(Globals.hEdit, textW);

    HeapFree(GetProcessHeap(), 0, textW);
469

470 471
    SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
    SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
472
    SetFocus(Globals.hEdit);
473
    
474
    /*  If the file starts with .LOG, add a time/date at the end and set cursor after */
475
    if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
476 477
    {
	static const WCHAR lfW[] = { '\r','\n',0 };
478
        SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
479
        SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
480
	DIALOG_EditTimeDate();
481
        SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
482
    }
483

484
    SetFileNameAndEncoding(szFileName, enc);
485
    UpdateWindowCaption();
Alexandre Julliard's avatar
Alexandre Julliard committed
486 487
}

Alexandre Julliard's avatar
Alexandre Julliard committed
488 489
VOID DIALOG_FileNew(VOID)
{
490 491
    static const WCHAR empty_strW[] = { 0 };

492
    /* Close any files and prompt to save changes */
Alexandre Julliard's avatar
Alexandre Julliard committed
493
    if (DoCloseFile()) {
494
        SetWindowTextW(Globals.hEdit, empty_strW);
495
        SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
496
        SetFocus(Globals.hEdit);
Alexandre Julliard's avatar
Alexandre Julliard committed
497
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
498 499
}

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
/* Used to detect encoding of files selected in Open dialog.
 * Returns ENCODING_AUTO if file can't be read, etc.
 */
static ENCODING detect_encoding_of_file(LPCWSTR szFileName)
{
    DWORD size;
    HANDLE hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                               OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
        return ENCODING_AUTO;
    size = GetFileSize(hFile, NULL);
    if (size == INVALID_FILE_SIZE)
    {
        CloseHandle(hFile);
        return ENCODING_AUTO;
    }
    else
    {
        DWORD dwNumRead;
        BYTE buffer[MAX_STRING_LEN];
        if (!ReadFile(hFile, buffer, min(size, sizeof(buffer)), &dwNumRead, NULL))
        {
            CloseHandle(hFile);
            return ENCODING_AUTO;
        }
        CloseHandle(hFile);
        return detect_encoding_of_buffer(buffer, dwNumRead);
    }
}

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
static LPWSTR dialog_print_to_file(HWND hMainWnd)
{
    OPENFILENAMEW ofn;
    static WCHAR file[MAX_PATH] = {'o','u','t','p','u','t','.','p','r','n',0};
    static const WCHAR defExt[] = {'p','r','n',0};

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(ofn);
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    ofn.hwndOwner = hMainWnd;
    ofn.lpstrFile = file;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrDefExt = defExt;

    if(GetSaveFileNameW(&ofn))
        return file;
    else
        return FALSE;
}
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
static UINT_PTR CALLBACK OfnHookProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static HWND hEncCombo;

    switch (uMsg)
    {
    case WM_INITDIALOG:
        {
            ENCODING enc;
            hEncCombo = GetDlgItem(hdlg, IDC_OFN_ENCCOMBO);
            for (enc = MIN_ENCODING; enc <= MAX_ENCODING; enc++)
            {
                WCHAR szEnc[MAX_STRING_LEN];
                load_encoding_name(enc, szEnc, ARRAY_SIZE(szEnc));
                SendMessageW(hEncCombo, CB_ADDSTRING, 0, (LPARAM)szEnc);
            }
            SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)Globals.encOfnCombo, 0);
        }
        break;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDC_OFN_ENCCOMBO &&
            HIWORD(wParam) == CBN_SELCHANGE)
        {
            int index = SendMessageW(hEncCombo, CB_GETCURSEL, 0, 0);
            Globals.encOfnCombo = index==CB_ERR ? ENCODING_ANSI : (ENCODING)index;
        }

        break;

    case WM_NOTIFY:
        switch (((OFNOTIFYW*)lParam)->hdr.code)
        {
            case CDN_SELCHANGE:
                if (Globals.bOfnIsOpenDialog)
                {
                    /* Check the start of the selected file for a BOM. */
                    ENCODING enc;
                    WCHAR szFileName[MAX_PATH];
                    SendMessageW(GetParent(hdlg), CDM_GETFILEPATH,
                                 ARRAY_SIZE(szFileName), (LPARAM)szFileName);
                    enc = detect_encoding_of_file(szFileName);
                    if (enc != ENCODING_AUTO)
                    {
                        Globals.encOfnCombo = enc;
                        SendMessageW(hEncCombo, CB_SETCURSEL, (WPARAM)enc, 0);
                    }
                }
                break;

            default:
                break;
        }
        break;

    default:
        break;
    }
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
611 612
VOID DIALOG_FileOpen(VOID)
{
613
    OPENFILENAMEW openfilename;
614 615 616
    WCHAR szPath[MAX_PATH];
    static const WCHAR szDefaultExt[] = { 't','x','t',0 };
    static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
617 618 619

    ZeroMemory(&openfilename, sizeof(openfilename));

620
    lstrcpyW(szPath, txt_files);
621 622 623 624 625 626

    openfilename.lStructSize       = sizeof(openfilename);
    openfilename.hwndOwner         = Globals.hMainWnd;
    openfilename.hInstance         = Globals.hInstance;
    openfilename.lpstrFilter       = Globals.szFilter;
    openfilename.lpstrFile         = szPath;
627
    openfilename.nMaxFile          = ARRAY_SIZE(szPath);
628 629 630 631 632
    openfilename.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
                         OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
                         OFN_HIDEREADONLY | OFN_ENABLESIZING;
    openfilename.lpfnHook          = OfnHookProc;
    openfilename.lpTemplateName    = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
633 634
    openfilename.lpstrDefExt       = szDefaultExt;

635 636
    Globals.encOfnCombo = ENCODING_ANSI;
    Globals.bOfnIsOpenDialog = TRUE;
637

638
    if (GetOpenFileNameW(&openfilename))
639
        DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
Alexandre Julliard's avatar
Alexandre Julliard committed
640 641
}

642
/* Return FALSE to cancel close */
643
BOOL DIALOG_FileSave(VOID)
Alexandre Julliard's avatar
Alexandre Julliard committed
644
{
645
    if (Globals.szFileName[0] == '\0')
646
        return DIALOG_FileSaveAs();
647
    else
648 649 650 651 652 653 654 655
    {
        switch (DoSaveFile(Globals.szFileName, Globals.encFile))
        {
            case SAVED_OK:           return TRUE;
            case SHOW_SAVEAS_DIALOG: return DIALOG_FileSaveAs();
            default:                 return FALSE;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
656 657
}

658
BOOL DIALOG_FileSaveAs(VOID)
Alexandre Julliard's avatar
Alexandre Julliard committed
659
{
660
    OPENFILENAMEW saveas;
661 662 663
    WCHAR szPath[MAX_PATH];
    static const WCHAR szDefaultExt[] = { 't','x','t',0 };
    static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
664 665 666

    ZeroMemory(&saveas, sizeof(saveas));

667
    lstrcpyW(szPath, txt_files);
668

669
    saveas.lStructSize       = sizeof(OPENFILENAMEW);
670 671 672 673
    saveas.hwndOwner         = Globals.hMainWnd;
    saveas.hInstance         = Globals.hInstance;
    saveas.lpstrFilter       = Globals.szFilter;
    saveas.lpstrFile         = szPath;
674
    saveas.nMaxFile          = ARRAY_SIZE(szPath);
675 676 677 678 679
    saveas.Flags          = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER |
                            OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
                            OFN_HIDEREADONLY | OFN_ENABLESIZING;
    saveas.lpfnHook          = OfnHookProc;
    saveas.lpTemplateName    = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE);
680 681
    saveas.lpstrDefExt       = szDefaultExt;

682 683 684 685
    /* Preset encoding to what file was opened/saved last with. */
    Globals.encOfnCombo = Globals.encFile;
    Globals.bOfnIsOpenDialog = FALSE;

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
retry:
    if (!GetSaveFileNameW(&saveas))
        return FALSE;

    switch (DoSaveFile(szPath, Globals.encOfnCombo))
    {
        case SAVED_OK:
            SetFileNameAndEncoding(szPath, Globals.encOfnCombo);
            UpdateWindowCaption();
            return TRUE;

        case SHOW_SAVEAS_DIALOG:
            goto retry;

        default:
            return FALSE;
702
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
703 704
}

705 706 707 708 709 710 711 712 713 714 715 716 717 718
typedef struct {
    LPWSTR mptr;
    LPWSTR mend;
    LPWSTR lptr;
    DWORD len;
} TEXTINFO, *LPTEXTINFO;

static int notepad_print_header(HDC hdc, RECT *rc, BOOL dopage, BOOL header, int page, LPWSTR text)
{
    SIZE szMetric;

    if (*text)
    {
        /* Write the header or footer */
719
        GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
720
        if (dopage)
721 722 723
            ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
                        header ? rc->top : rc->bottom - szMetric.cy,
                        ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
724 725 726 727 728
        return 1;
    }
    return 0;
}

729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
static WCHAR *expand_header_vars(WCHAR *pattern, int page)
{
    int length = 0;
    int i;
    BOOL inside = FALSE;
    WCHAR *buffer = NULL;

    for (i = 0; pattern[i]; i++)
    {
        if (inside)
        {
            if (pattern[i] == '&')
                length++;
            else if (pattern[i] == 'p')
                length += 11;
            inside = FALSE;
        }
        else if (pattern[i] == '&')
            inside = TRUE;
        else
            length++;
    }

    buffer = HeapAlloc(GetProcessHeap(), 0, (length + 1) * sizeof(WCHAR));
    if (buffer)
    {
        int j = 0;
        inside = FALSE;
        for (i = 0; pattern[i]; i++)
        {
            if (inside)
            {
                if (pattern[i] == '&')
                    buffer[j++] = '&';
                else if (pattern[i] == 'p')
                {
                    static const WCHAR percent_dW[] = {'%','d',0};
                    j += wnsprintfW(&buffer[j], 11, percent_dW, page);
                }
                inside = FALSE;
            }
            else if (pattern[i] == '&')
                inside = TRUE;
            else
                buffer[j++] = pattern[i];
        }
        buffer[j++] = 0;
    }
    return buffer;
}

780 781 782
static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
{
    int b, y;
783
    TEXTMETRICW tm;
784
    SIZE szMetrics;
785 786 787 788 789
    WCHAR *footer_text = NULL;

    footer_text = expand_header_vars(Globals.szFooter, page);
    if (footer_text == NULL)
        return FALSE;
790 791 792 793 794 795 796

    if (dopage)
    {
        if (StartPage(hdc) <= 0)
        {
            static const WCHAR failedW[] = { 'S','t','a','r','t','P','a','g','e',' ','f','a','i','l','e','d',0 };
            static const WCHAR errorW[] = { 'P','r','i','n','t',' ','E','r','r','o','r',0 };
797
            MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
798
            HeapFree(GetProcessHeap(), 0, footer_text);
799 800 801 802
            return FALSE;
        }
    }

803
    GetTextMetricsW(hdc, &tm);
804
    y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
805
    b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, footer_text) * tm.tmHeight;
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

    do {
        INT m, n;

        if (!tInfo->len)
        {
            /* find the end of the line */
            while (tInfo->mptr < tInfo->mend && *tInfo->mptr != '\n' && *tInfo->mptr != '\r')
            {
                if (*tInfo->mptr == '\t')
                {
                    /* replace tabs with spaces */
                    for (m = 0; m < SPACES_IN_TAB; m++)
                    {
                        if (tInfo->len < PRINT_LEN_MAX)
                            tInfo->lptr[tInfo->len++] = ' ';
                        else if (Globals.bWrapLongLines)
                            break;
                    }
                }
                else if (tInfo->len < PRINT_LEN_MAX)
                    tInfo->lptr[tInfo->len++] = *tInfo->mptr;

                if (tInfo->len >= PRINT_LEN_MAX && Globals.bWrapLongLines)
                     break;

                tInfo->mptr++;
            }
        }

        /* Find out how much we should print if line wrapping is enabled */
        if (Globals.bWrapLongLines)
        {
839
            GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
840 841 842 843 844 845 846 847 848 849 850 851
            if (n < tInfo->len && tInfo->lptr[n] != ' ')
            {
                m = n;
                /* Don't wrap words unless it's a single word over the entire line */
                while (m  && tInfo->lptr[m] != ' ') m--;
                if (m > 0) n = m + 1;
            }
        }
        else
            n = tInfo->len;

        if (dopage)
852
            ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872

        tInfo->len -= n;

        if (tInfo->len)
        {
            memcpy(tInfo->lptr, tInfo->lptr + n, tInfo->len * sizeof(WCHAR));
            y += tm.tmHeight + tm.tmExternalLeading;
        }
        else
        {
            /* find the next line */
            while (tInfo->mptr < tInfo->mend && y < b && (*tInfo->mptr == '\n' || *tInfo->mptr == '\r'))
            {
                if (*tInfo->mptr == '\n')
                    y += tm.tmHeight + tm.tmExternalLeading;
                tInfo->mptr++;
            }
        }
    } while (tInfo->mptr < tInfo->mend && y < b);

873
    notepad_print_header(hdc, rc, dopage, FALSE, page, footer_text);
874 875 876 877
    if (dopage)
    {
        EndPage(hdc);
    }
878
    HeapFree(GetProcessHeap(), 0, footer_text);
879 880 881
    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
882 883
VOID DIALOG_FilePrint(VOID)
{
884 885
    DOCINFOW di;
    PRINTDLGW printer;
886
    int page, dopage, copy;
887
    LOGFONTW lfFont;
888
    HFONT hTextFont, old_font = 0;
889
    DWORD size;
890 891
    BOOL ret = FALSE;
    RECT rc;
892
    LPWSTR pTemp;
893
    TEXTINFO tInfo;
894
    WCHAR cTemp[PRINT_LEN_MAX];
895

896 897 898 899
    /* Get Current Settings */
    ZeroMemory(&printer, sizeof(printer));
    printer.lStructSize           = sizeof(printer);
    printer.hwndOwner             = Globals.hMainWnd;
900 901
    printer.hDevMode              = Globals.hDevMode;
    printer.hDevNames             = Globals.hDevNames;
902
    printer.hInstance             = Globals.hInstance;
903

904
    /* Set some default flags */
905
    printer.Flags                 = PD_RETURNDC | PD_NOSELECTION;
906
    printer.nFromPage             = 0;
907 908
    printer.nMinPage              = 1;
    /* we really need to calculate number of pages to set nMaxPage and nToPage */
909 910
    printer.nToPage               = 0;
    printer.nMaxPage              = -1;
911 912 913
    /* Let commdlg manage copy settings */
    printer.nCopies               = (WORD)PD_USEDEVMODECOPIES;

914
    if (!PrintDlgW(&printer)) return;
915

916 917 918
    Globals.hDevMode = printer.hDevMode;
    Globals.hDevNames = printer.hDevNames;

919
    SetMapMode(printer.hDC, MM_TEXT);
920

921
    /* initialize DOCINFO */
922
    di.cbSize = sizeof(DOCINFOW);
923 924 925
    di.lpszDocName = Globals.szFileTitle;
    di.lpszOutput = NULL;
    di.lpszDatatype = NULL;
926 927
    di.fwType = 0; 

928 929 930 931 932 933 934
    if(printer.Flags & PD_PRINTTOFILE)
    {
        di.lpszOutput = dialog_print_to_file(printer.hwndOwner);
        if(!di.lpszOutput)
            return;
    }

935
    /* Get the file text */
936
    size = GetWindowTextLengthW(Globals.hEdit) + 1;
937
    pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
938 939
    if (!pTemp)
    {
940 941 942
       DeleteDC(printer.hDC);
       ShowLastError();
       return;
943
    }
944
    size = GetWindowTextW(Globals.hEdit, pTemp, size);
945

946
    if (StartDocW(printer.hDC, &di) > 0)
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    {
        /* Get the page margins in pixels. */
        rc.top =    MulDiv(Globals.iMarginTop, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540) -
                    GetDeviceCaps(printer.hDC, PHYSICALOFFSETY);
        rc.bottom = GetDeviceCaps(printer.hDC, PHYSICALHEIGHT) -
                    MulDiv(Globals.iMarginBottom, GetDeviceCaps(printer.hDC, LOGPIXELSY), 2540);
        rc.left =   MulDiv(Globals.iMarginLeft, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540) -
                    GetDeviceCaps(printer.hDC, PHYSICALOFFSETX);
        rc.right =  GetDeviceCaps(printer.hDC, PHYSICALWIDTH) -
                    MulDiv(Globals.iMarginRight, GetDeviceCaps(printer.hDC, LOGPIXELSX), 2540);

        /* Create a font for the printer resolution */
        lfFont = Globals.lfFont;
        lfFont.lfHeight = MulDiv(lfFont.lfHeight, GetDeviceCaps(printer.hDC, LOGPIXELSY), get_dpi());
        /* Make the font a bit lighter */
        lfFont.lfWeight -= 100;
963
        hTextFont = CreateFontIndirectW(&lfFont);
964 965 966 967 968 969 970 971 972 973 974
        old_font = SelectObject(printer.hDC, hTextFont);

        for (copy = 1; copy <= printer.nCopies; copy++)
        {
            page = 1;

            tInfo.mptr = pTemp;
            tInfo.mend = pTemp + size;
            tInfo.lptr = cTemp;
            tInfo.len = 0;

975
            do {
976 977 978 979 980 981 982 983 984 985 986
                if (printer.Flags & PD_PAGENUMS)
                {
                    /* a specific range of pages is selected, so
                     * skip pages that are not to be printed
                     */
                    if (page > printer.nToPage)
                        break;
                    else if (page >= printer.nFromPage)
                        dopage = 1;
                    else
                        dopage = 0;
987
                }
988 989 990 991 992 993
                else
                    dopage = 1;

                ret = notepad_print_page(printer.hDC, &rc, dopage, page, &tInfo);
                page++;
            } while (ret && tInfo.mptr < tInfo.mend);
994

995 996 997 998 999 1000
            if (!ret) break;
        }
        EndDoc(printer.hDC);
        SelectObject(printer.hDC, old_font);
        DeleteObject(hTextFont);
    }
1001
    DeleteDC(printer.hDC);
1002
    HeapFree(GetProcessHeap(), 0, pTemp);
Alexandre Julliard's avatar
Alexandre Julliard committed
1003 1004 1005 1006
}

VOID DIALOG_FilePrinterSetup(VOID)
{
1007
    PRINTDLGW printer;
1008

1009 1010 1011
    ZeroMemory(&printer, sizeof(printer));
    printer.lStructSize         = sizeof(printer);
    printer.hwndOwner           = Globals.hMainWnd;
1012 1013
    printer.hDevMode            = Globals.hDevMode;
    printer.hDevNames           = Globals.hDevNames;
1014 1015 1016
    printer.hInstance           = Globals.hInstance;
    printer.Flags               = PD_PRINTSETUP;
    printer.nCopies             = 1;
1017

1018
    PrintDlgW(&printer);
1019 1020 1021

    Globals.hDevMode = printer.hDevMode;
    Globals.hDevNames = printer.hDevNames;
Alexandre Julliard's avatar
Alexandre Julliard committed
1022 1023 1024 1025
}

VOID DIALOG_FileExit(VOID)
{
1026
    PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
Alexandre Julliard's avatar
Alexandre Julliard committed
1027 1028 1029 1030
}

VOID DIALOG_EditUndo(VOID)
{
1031
    SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1032 1033 1034 1035
}

VOID DIALOG_EditCut(VOID)
{
1036
    SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1037 1038 1039 1040
}

VOID DIALOG_EditCopy(VOID)
{
1041
    SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1042 1043 1044 1045
}

VOID DIALOG_EditPaste(VOID)
{
1046
    SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1047 1048 1049 1050
}

VOID DIALOG_EditDelete(VOID)
{
1051
    SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1052 1053 1054 1055
}

VOID DIALOG_EditSelectAll(VOID)
{
1056
    SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
Alexandre Julliard's avatar
Alexandre Julliard committed
1057 1058 1059 1060
}

VOID DIALOG_EditTimeDate(VOID)
{
1061
    SYSTEMTIME   st;
1062 1063
    WCHAR        szDate[MAX_STRING_LEN];
    static const WCHAR spaceW[] = { ' ',0 };
1064

1065
    GetLocalTime(&st);
1066

1067
    GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
1068
    SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
1069

1070
    SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
1071

1072
    GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
1073
    SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
Alexandre Julliard's avatar
Alexandre Julliard committed
1074 1075 1076 1077
}

VOID DIALOG_EditWrap(VOID)
{
1078
    BOOL modify = FALSE;
Lauri Tulmin's avatar
Lauri Tulmin committed
1079 1080 1081 1082 1083 1084 1085
    static const WCHAR editW[] = { 'e','d','i','t',0 };
    DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
                    ES_AUTOVSCROLL | ES_MULTILINE;
    RECT rc;
    DWORD size;
    LPWSTR pTemp;

1086
    size = GetWindowTextLengthW(Globals.hEdit) + 1;
Lauri Tulmin's avatar
Lauri Tulmin committed
1087 1088 1089 1090 1091 1092
    pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
    if (!pTemp)
    {
        ShowLastError();
        return;
    }
1093
    GetWindowTextW(Globals.hEdit, pTemp, size);
1094
    modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
Lauri Tulmin's avatar
Lauri Tulmin committed
1095 1096 1097
    DestroyWindow(Globals.hEdit);
    GetClientRect(Globals.hMainWnd, &rc);
    if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
1098
    Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
Lauri Tulmin's avatar
Lauri Tulmin committed
1099 1100
                         0, 0, rc.right, rc.bottom, Globals.hMainWnd,
                         NULL, Globals.hInstance, NULL);
1101
    SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
Lauri Tulmin's avatar
Lauri Tulmin committed
1102
    SetWindowTextW(Globals.hEdit, pTemp);
1103
    SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
Lauri Tulmin's avatar
Lauri Tulmin committed
1104 1105 1106
    SetFocus(Globals.hEdit);
    HeapFree(GetProcessHeap(), 0, pTemp);
    
1107 1108 1109
    Globals.bWrapLongLines = !Globals.bWrapLongLines;
    CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
        MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
Alexandre Julliard's avatar
Alexandre Julliard committed
1110 1111
}

1112 1113
VOID DIALOG_SelectFont(VOID)
{
1114 1115
    CHOOSEFONTW cf;
    LOGFONTW lf=Globals.lfFont;
1116 1117 1118 1119 1120

    ZeroMemory( &cf, sizeof(cf) );
    cf.lStructSize=sizeof(cf);
    cf.hwndOwner=Globals.hMainWnd;
    cf.lpLogFont=&lf;
1121
    cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_NOVERTFONTS;
1122

1123
    if( ChooseFontW(&cf) )
1124 1125 1126
    {
        HFONT currfont=Globals.hFont;

1127
        Globals.hFont=CreateFontIndirectW( &lf );
1128
        Globals.lfFont=lf;
1129
        SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
1130 1131 1132 1133 1134
        if( currfont!=NULL )
            DeleteObject( currfont );
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1135 1136
VOID DIALOG_Search(VOID)
{
1137 1138 1139 1140 1141 1142 1143
        /* Allow only one search/replace dialog to open */
        if(Globals.hFindReplaceDlg != NULL)
        {
            SetActiveWindow(Globals.hFindReplaceDlg);
            return;
        }

1144
        ZeroMemory(&Globals.find, sizeof(Globals.find));
1145 1146 1147
        Globals.find.lStructSize      = sizeof(Globals.find);
        Globals.find.hwndOwner        = Globals.hMainWnd;
        Globals.find.hInstance        = Globals.hInstance;
1148
        Globals.find.lpstrFindWhat    = Globals.szFindText;
1149
        Globals.find.wFindWhatLen     = ARRAY_SIZE(Globals.szFindText);
1150
        Globals.find.Flags            = FR_DOWN|FR_HIDEWHOLEWORD;
1151 1152 1153

        /* We only need to create the modal FindReplace dialog which will */
        /* notify us of incoming events using hMainWnd Window Messages    */
1154

1155
        Globals.hFindReplaceDlg = FindTextW(&Globals.find);
1156
        assert(Globals.hFindReplaceDlg !=0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1157 1158 1159 1160
}

VOID DIALOG_SearchNext(VOID)
{
1161 1162 1163 1164
    if (Globals.lastFind.lpstrFindWhat == NULL)
        DIALOG_Search();
    else                /* use the last find data */
        NOTEPAD_DoFind(&Globals.lastFind);
Alexandre Julliard's avatar
Alexandre Julliard committed
1165 1166
}

1167 1168
VOID DIALOG_Replace(VOID)
{
1169 1170 1171 1172 1173 1174 1175
        /* Allow only one search/replace dialog to open */
        if(Globals.hFindReplaceDlg != NULL)
        {
            SetActiveWindow(Globals.hFindReplaceDlg);
            return;
        }

1176 1177 1178 1179 1180
        ZeroMemory(&Globals.find, sizeof(Globals.find));
        Globals.find.lStructSize      = sizeof(Globals.find);
        Globals.find.hwndOwner        = Globals.hMainWnd;
        Globals.find.hInstance        = Globals.hInstance;
        Globals.find.lpstrFindWhat    = Globals.szFindText;
1181
        Globals.find.wFindWhatLen     = ARRAY_SIZE(Globals.szFindText);
1182
        Globals.find.lpstrReplaceWith = Globals.szReplaceText;
1183
        Globals.find.wReplaceWithLen  = ARRAY_SIZE(Globals.szReplaceText);
1184 1185 1186 1187 1188
        Globals.find.Flags            = FR_DOWN|FR_HIDEWHOLEWORD;

        /* We only need to create the modal FindReplace dialog which will */
        /* notify us of incoming events using hMainWnd Window Messages    */

1189
        Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
1190 1191 1192
        assert(Globals.hFindReplaceDlg !=0);
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1193 1194
VOID DIALOG_HelpContents(VOID)
{
1195
    WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1196 1197
}

1198
VOID DIALOG_HelpAboutNotepad(VOID)
Alexandre Julliard's avatar
Alexandre Julliard committed
1199
{
1200
    static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
1201
    WCHAR szNotepad[MAX_STRING_LEN];
1202 1203
    HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
                            IMAGE_ICON, 48, 48, LR_SHARED);
1204

1205
    LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
1206
    ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
Alexandre Julliard's avatar
Alexandre Julliard committed
1207 1208
}

1209

Alexandre Julliard's avatar
Alexandre Julliard committed
1210 1211
/***********************************************************************
 *
1212
 *           DIALOG_FilePageSetup
Alexandre Julliard's avatar
Alexandre Julliard committed
1213
 */
1214
VOID DIALOG_FilePageSetup(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
1215
{
1216 1217
    DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
               Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
Alexandre Julliard's avatar
Alexandre Julliard committed
1218 1219 1220 1221 1222 1223 1224 1225
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *           DIALOG_PAGESETUP_DlgProc
 */

1226
static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1227
{
1228 1229

   switch (msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
1230 1231 1232
    {
    case WM_COMMAND:
      switch (wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1233 1234
        {
        case IDOK:
1235
          /* save user input and close dialog */
1236 1237
          GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
          GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
1238 1239 1240 1241 1242

          Globals.iMarginTop = GetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, NULL, FALSE) * 100;
          Globals.iMarginBottom = GetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, NULL, FALSE) * 100;
          Globals.iMarginLeft = GetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, NULL, FALSE) * 100;
          Globals.iMarginRight = GetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, NULL, FALSE) * 100;
Alexandre Julliard's avatar
Alexandre Julliard committed
1243 1244 1245 1246
          EndDialog(hDlg, IDOK);
          return TRUE;

        case IDCANCEL:
1247
          /* discard user input and close dialog */
Alexandre Julliard's avatar
Alexandre Julliard committed
1248 1249
          EndDialog(hDlg, IDCANCEL);
          return TRUE;
1250 1251

        case IDHELP:
1252
        {
1253
          /* FIXME: Bring this to work */
1254 1255
          static const WCHAR sorryW[] = { 'S','o','r','r','y',',',' ','n','o',' ','h','e','l','p',' ','a','v','a','i','l','a','b','l','e',0 };
          static const WCHAR helpW[] = { 'H','e','l','p',0 };
1256
          MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
1257
          return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1258
        }
1259 1260 1261 1262

	default:
	    break;
        }
1263 1264 1265
      break;

    case WM_INITDIALOG:
1266
       /* fetch last user input prior to display dialog */
1267 1268
       SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
       SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
1269 1270 1271 1272
       SetDlgItemInt(hDlg, IDC_PAGESETUP_TOPVALUE, Globals.iMarginTop / 100, FALSE);
       SetDlgItemInt(hDlg, IDC_PAGESETUP_BOTTOMVALUE, Globals.iMarginBottom / 100, FALSE);
       SetDlgItemInt(hDlg, IDC_PAGESETUP_LEFTVALUE, Globals.iMarginLeft / 100, FALSE);
       SetDlgItemInt(hDlg, IDC_PAGESETUP_RIGHTVALUE, Globals.iMarginRight / 100, FALSE);
1273
       break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1274
    }
1275

Alexandre Julliard's avatar
Alexandre Julliard committed
1276 1277
  return FALSE;
}