dialog.c 36.3 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 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
22 23
 */

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

Alexandre Julliard's avatar
Alexandre Julliard committed
31 32
#include "main.h"
#include "dialog.h"
33

34
#define SPACES_IN_TAB 8
35
#define PRINT_LEN_MAX 500
36

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

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

41 42 43 44 45 46 47
/* 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]);
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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;

        default:
        {
            CPINFOEXW cpi;
            GetCPInfoExW((enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP, 0, &cpi);
            lstrcpynW(buffer, cpi.CodePageName, length);
            break;
        }
    }
}

70
VOID ShowLastError(void)
71 72 73 74
{
    DWORD error = GetLastError();
    if (error != NO_ERROR)
    {
75 76
        LPWSTR lpMsgBuf;
        WCHAR szTitle[MAX_STRING_LEN];
77

78
        LoadStringW(Globals.hInstance, STRING_ERROR, szTitle, ARRAY_SIZE(szTitle));
79
        FormatMessageW(
80
            FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
81
            NULL, error, 0, (LPWSTR)&lpMsgBuf, 0, NULL);
82
        MessageBoxW(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
83 84 85 86 87 88
        LocalFree(lpMsgBuf);
    }
}

/**
 * Sets the caption of the main window according to Globals.szFileTitle:
89 90
 *    Untitled - Notepad        if no file is open
 *    filename - Notepad        if a file is given
91
 */
92
void UpdateWindowCaption(void)
93 94
{
  WCHAR szCaption[MAX_STRING_LEN];
95 96
  WCHAR szNotepad[MAX_STRING_LEN];
  static const WCHAR hyphenW[] = { ' ','-',' ',0 };
97

98
  if (Globals.szFileTitle[0] != '\0')
99
      lstrcpyW(szCaption, Globals.szFileTitle);
100
  else
101
      LoadStringW(Globals.hInstance, STRING_UNTITLED, szCaption, ARRAY_SIZE(szCaption));
102

103
  LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
104 105
  lstrcatW(szCaption, hyphenW);
  lstrcatW(szCaption, szNotepad);
106

107
  SetWindowTextW(Globals.hMainWnd, szCaption);
108 109
}

110
int DIALOG_StringMsgBox(HWND hParent, int formatId, LPCWSTR szString, DWORD dwFlags)
111 112 113
{
   WCHAR szMessage[MAX_STRING_LEN];
   WCHAR szResource[MAX_STRING_LEN];
114 115

   /* Load and format szMessage */
116 117
   LoadStringW(Globals.hInstance, formatId, szResource, ARRAY_SIZE(szResource));
   wnsprintfW(szMessage, ARRAY_SIZE(szMessage), szResource, szString);
118

119
   /* Load szCaption */
120
   if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
121
     LoadStringW(Globals.hInstance, STRING_ERROR,  szResource, ARRAY_SIZE(szResource));
122
   else
123
     LoadStringW(Globals.hInstance, STRING_NOTEPAD,  szResource, ARRAY_SIZE(szResource));
124 125

   /* Display Modal Dialog */
126 127
   if (hParent == NULL)
     hParent = Globals.hMainWnd;
128
   return MessageBoxW(hParent, szMessage, szResource, dwFlags);
129 130 131 132 133
}

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

136 137 138
static int AlertFileNotSaved(LPCWSTR szFileName)
{
   WCHAR szUntitled[MAX_STRING_LEN];
Alexandre Julliard's avatar
Alexandre Julliard committed
139

140
   LoadStringW(Globals.hInstance, STRING_UNTITLED, szUntitled, ARRAY_SIZE(szUntitled));
141 142
   return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, szFileName[0] ? szFileName : szUntitled,
     MB_ICONQUESTION|MB_YESNOCANCEL);
143 144
}

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
static int AlertUnicodeCharactersLost(LPCWSTR szFileName)
{
    WCHAR szMsgFormat[MAX_STRING_LEN];
    WCHAR szEnc[MAX_STRING_LEN];
    WCHAR szMsg[ARRAY_SIZE(szMsgFormat) + MAX_PATH + ARRAY_SIZE(szEnc)];
    WCHAR szCaption[MAX_STRING_LEN];

    LoadStringW(Globals.hInstance, STRING_LOSS_OF_UNICODE_CHARACTERS,
                szMsgFormat, ARRAY_SIZE(szMsgFormat));
    load_encoding_name(ENCODING_ANSI, szEnc, ARRAY_SIZE(szEnc));
    wnsprintfW(szMsg, ARRAY_SIZE(szMsg), szMsgFormat, szFileName, szEnc);
    LoadStringW(Globals.hInstance, STRING_NOTEPAD, szCaption,
                ARRAY_SIZE(szCaption));
    return MessageBoxW(Globals.hMainWnd, szMsg, szCaption,
                       MB_OKCANCEL|MB_ICONEXCLAMATION);
}

162 163 164 165
/**
 * Returns:
 *   TRUE  - if file exists
 *   FALSE - if file does not exist
Alexandre Julliard's avatar
Alexandre Julliard committed
166
 */
167 168
BOOL FileExists(LPCWSTR szFilename)
{
169
   WIN32_FIND_DATAW entry;
170
   HANDLE hFile;
171

172
   hFile = FindFirstFileW(szFilename, &entry);
173
   FindClose(hFile);
174

175
   return (hFile != INVALID_HANDLE_VALUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
176 177
}

178 179 180 181 182 183 184
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;
}
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
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)
202
{
203 204
    int lenW;
    WCHAR* textW;
205 206
    HANDLE hFile;
    DWORD dwNumWrite;
207
    PVOID pBytes;
208
    DWORD size;
209

210 211 212 213 214 215
    /* 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();
216
        return SAVE_FAILED;
217 218 219 220
    }
    textW[0] = (WCHAR) 0xfeff;
    lenW = GetWindowTextW(Globals.hEdit, textW+1, lenW) + 1;

221
    switch (enc)
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    {
    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);
239
            return SAVE_FAILED;
240 241 242 243 244 245
        }
        WideCharToMultiByte(CP_UTF8, 0, textW, lenW, pBytes, size, NULL, NULL);
        HeapFree(GetProcessHeap(), 0, textW);
        break;

    default:
246 247 248 249 250 251 252
        if (is_conversion_to_ansi_lossy(textW+1, lenW-1)
            && AlertUnicodeCharactersLost(szFileName) == IDCANCEL)
        {
            HeapFree(GetProcessHeap(), 0, textW);
            return SHOW_SAVEAS_DIALOG;
        }

253 254 255 256 257 258
        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);
259
            return SAVE_FAILED;
260 261 262 263 264 265
        }
        WideCharToMultiByte(CP_ACP, 0, textW+1, lenW-1, pBytes, size, NULL, NULL);
        HeapFree(GetProcessHeap(), 0, textW);
        break;
    }

266
    hFile = CreateFileW(szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
267 268 269 270
                       NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
        ShowLastError();
271
        HeapFree(GetProcessHeap(), 0, pBytes);
272
        return SAVE_FAILED;
273
    }
274
    if (!WriteFile(hFile, pBytes, size, &dwNumWrite, NULL))
275 276
    {
        ShowLastError();
277 278
        CloseHandle(hFile);
        HeapFree(GetProcessHeap(), 0, pBytes);
279
        return SAVE_FAILED;
280
    }
281
    SetEndOfFile(hFile);
282
    CloseHandle(hFile);
283 284 285
    HeapFree(GetProcessHeap(), 0, pBytes);

    SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
286
    return SAVED_OK;
287
}
Alexandre Julliard's avatar
Alexandre Julliard committed
288

289 290 291 292 293
/**
 * Returns:
 *   TRUE  - User agreed to close (both save/don't save)
 *   FALSE - User cancelled close by selecting "Cancel"
 */
294 295
BOOL DoCloseFile(void)
{
296
    int nResult;
297
    static const WCHAR empty_strW[] = { 0 };
298

299
    if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0))
300
    {
301 302 303
        /* prompt user to save changes */
        nResult = AlertFileNotSaved(Globals.szFileName);
        switch (nResult) {
304
            case IDYES:     return DIALOG_FileSave();
Alexandre Julliard's avatar
Alexandre Julliard committed
305

306
            case IDNO:      break;
Alexandre Julliard's avatar
Alexandre Julliard committed
307

308
            case IDCANCEL:  return(FALSE);
309

310 311 312
            default:        return(FALSE);
        } /* switch */
    } /* if */
313

314
    SetFileNameAndEncoding(empty_strW, ENCODING_ANSI);
315 316

    UpdateWindowCaption();
317
    return(TRUE);
Alexandre Julliard's avatar
Alexandre Julliard committed
318 319
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
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;
    }
}

340
void DoOpenFile(LPCWSTR szFileName, ENCODING enc)
341
{
342
    static const WCHAR dotlog[] = { '.','L','O','G',0 };
343 344 345 346
    HANDLE hFile;
    LPSTR pTemp;
    DWORD size;
    DWORD dwNumRead;
347 348
    int lenW;
    WCHAR* textW;
349
    int i;
350
    WCHAR log[5];
351

352
    /* Close any files and prompt to save changes */
353 354 355
    if (!DoCloseFile())
	return;

356 357
    hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
358
    if(hFile == INVALID_HANDLE_VALUE)
359
    {
360
	AlertFileNotFound(szFileName);
361 362
	return;
    }
363

364
    size = GetFileSize(hFile, NULL);
365
    if (size == INVALID_FILE_SIZE)
366 367 368 369 370
    {
	CloseHandle(hFile);
	ShowLastError();
	return;
    }
371

372 373
    /* Extra memory for (WCHAR)'\0'-termination. */
    pTemp = HeapAlloc(GetProcessHeap(), 0, size+2);
374 375 376 377 378 379 380 381 382 383 384 385 386
    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
387
    }
388 389 390

    CloseHandle(hFile);

391 392
    size = dwNumRead;

393 394 395 396 397 398 399 400 401 402 403 404
    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;
    }
405 406 407 408 409

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

413 414 415
        /* fall through */

    case ENCODING_UTF16LE:
416 417
        textW = (LPWSTR)pTemp;
        lenW  = size/sizeof(WCHAR);
418 419 420
        break;

    default:
421 422 423 424 425 426 427 428 429 430 431 432 433 434
        {
            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;
        }
435 436
    }

437 438 439 440 441 442
    /* 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] = ' ';
443
    textW[lenW] = '\0';
444

445 446 447 448 449 450
    if (lenW >= 1 && textW[0] == 0xfeff)
        SetWindowTextW(Globals.hEdit, textW+1);
    else
        SetWindowTextW(Globals.hEdit, textW);

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

452 453
    SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
    SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
454
    SetFocus(Globals.hEdit);
455
    
456
    /*  If the file starts with .LOG, add a time/date at the end and set cursor after */
457
    if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog))
458 459
    {
	static const WCHAR lfW[] = { '\r','\n',0 };
460
        SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1);
461
        SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
462
	DIALOG_EditTimeDate();
463
        SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW);
464
    }
465

466
    SetFileNameAndEncoding(szFileName, enc);
467
    UpdateWindowCaption();
Alexandre Julliard's avatar
Alexandre Julliard committed
468 469
}

Alexandre Julliard's avatar
Alexandre Julliard committed
470 471
VOID DIALOG_FileNew(VOID)
{
472 473
    static const WCHAR empty_strW[] = { 0 };

474
    /* Close any files and prompt to save changes */
Alexandre Julliard's avatar
Alexandre Julliard committed
475
    if (DoCloseFile()) {
476
        SetWindowTextW(Globals.hEdit, empty_strW);
477
        SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
478
        SetFocus(Globals.hEdit);
Alexandre Julliard's avatar
Alexandre Julliard committed
479
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
480 481
}

482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 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 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
/* 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);
    }
}

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
573 574
VOID DIALOG_FileOpen(VOID)
{
575
    OPENFILENAMEW openfilename;
576 577 578 579
    WCHAR szPath[MAX_PATH];
    WCHAR szDir[MAX_PATH];
    static const WCHAR szDefaultExt[] = { 't','x','t',0 };
    static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
580 581 582

    ZeroMemory(&openfilename, sizeof(openfilename));

583
    GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
584
    lstrcpyW(szPath, txt_files);
585 586 587 588 589 590

    openfilename.lStructSize       = sizeof(openfilename);
    openfilename.hwndOwner         = Globals.hMainWnd;
    openfilename.hInstance         = Globals.hInstance;
    openfilename.lpstrFilter       = Globals.szFilter;
    openfilename.lpstrFile         = szPath;
591
    openfilename.nMaxFile          = ARRAY_SIZE(szPath);
592
    openfilename.lpstrInitialDir   = szDir;
593 594 595 596 597
    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);
598 599
    openfilename.lpstrDefExt       = szDefaultExt;

600 601
    Globals.encOfnCombo = ENCODING_ANSI;
    Globals.bOfnIsOpenDialog = TRUE;
602

603
    if (GetOpenFileNameW(&openfilename))
604
        DoOpenFile(openfilename.lpstrFile, Globals.encOfnCombo);
Alexandre Julliard's avatar
Alexandre Julliard committed
605 606
}

607
/* Return FALSE to cancel close */
608
BOOL DIALOG_FileSave(VOID)
Alexandre Julliard's avatar
Alexandre Julliard committed
609
{
610
    if (Globals.szFileName[0] == '\0')
611
        return DIALOG_FileSaveAs();
612
    else
613 614 615 616 617 618 619 620
    {
        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
621 622
}

623
BOOL DIALOG_FileSaveAs(VOID)
Alexandre Julliard's avatar
Alexandre Julliard committed
624
{
625
    OPENFILENAMEW saveas;
626 627 628 629
    WCHAR szPath[MAX_PATH];
    WCHAR szDir[MAX_PATH];
    static const WCHAR szDefaultExt[] = { 't','x','t',0 };
    static const WCHAR txt_files[] = { '*','.','t','x','t',0 };
630 631 632

    ZeroMemory(&saveas, sizeof(saveas));

633
    GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir);
634
    lstrcpyW(szPath, txt_files);
635

636
    saveas.lStructSize       = sizeof(OPENFILENAMEW);
637 638 639 640
    saveas.hwndOwner         = Globals.hMainWnd;
    saveas.hInstance         = Globals.hInstance;
    saveas.lpstrFilter       = Globals.szFilter;
    saveas.lpstrFile         = szPath;
641
    saveas.nMaxFile          = ARRAY_SIZE(szPath);
642
    saveas.lpstrInitialDir   = szDir;
643 644 645 646 647
    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);
648 649
    saveas.lpstrDefExt       = szDefaultExt;

650 651 652 653
    /* Preset encoding to what file was opened/saved last with. */
    Globals.encOfnCombo = Globals.encFile;
    Globals.bOfnIsOpenDialog = FALSE;

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
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;
670
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
671 672
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686
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 */
687
        GetTextExtentPoint32W(hdc, text, lstrlenW(text), &szMetric);
688
        if (dopage)
689 690 691
            ExtTextOutW(hdc, (rc->left + rc->right - szMetric.cx) / 2,
                        header ? rc->top : rc->bottom - szMetric.cy,
                        ETO_CLIPPED, rc, text, lstrlenW(text), NULL);
692 693 694 695 696 697 698 699
        return 1;
    }
    return 0;
}

static BOOL notepad_print_page(HDC hdc, RECT *rc, BOOL dopage, int page, LPTEXTINFO tInfo)
{
    int b, y;
700
    TEXTMETRICW tm;
701 702 703 704 705 706 707 708
    SIZE szMetrics;

    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 };
709
            MessageBoxW(Globals.hMainWnd, failedW, errorW, MB_ICONEXCLAMATION);
710 711 712 713
            return FALSE;
        }
    }

714
    GetTextMetricsW(hdc, &tm);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    y = rc->top + notepad_print_header(hdc, rc, dopage, TRUE, page, Globals.szFileName) * tm.tmHeight;
    b = rc->bottom - 2 * notepad_print_header(hdc, rc, FALSE, FALSE, page, Globals.szFooter) * tm.tmHeight;

    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)
        {
750
            GetTextExtentExPointW(hdc, tInfo->lptr, tInfo->len, rc->right - rc->left, &n, NULL, &szMetrics);
751 752 753 754 755 756 757 758 759 760 761 762
            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)
763
            ExtTextOutW(hdc, rc->left, y, ETO_CLIPPED, rc, tInfo->lptr, n, NULL);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791

        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);

    notepad_print_header(hdc, rc, dopage, FALSE, page, Globals.szFooter);
    if (dopage)
    {
        EndPage(hdc);
    }
    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
792 793
VOID DIALOG_FilePrint(VOID)
{
794 795
    DOCINFOW di;
    PRINTDLGW printer;
796
    int page, dopage, copy;
797
    LOGFONTW lfFont;
798
    HFONT hTextFont, old_font = 0;
799
    DWORD size;
800 801
    BOOL ret = FALSE;
    RECT rc;
802
    LPWSTR pTemp;
803
    TEXTINFO tInfo;
804
    WCHAR cTemp[PRINT_LEN_MAX];
805

806 807 808 809
    /* Get Current Settings */
    ZeroMemory(&printer, sizeof(printer));
    printer.lStructSize           = sizeof(printer);
    printer.hwndOwner             = Globals.hMainWnd;
810 811
    printer.hDevMode              = Globals.hDevMode;
    printer.hDevNames             = Globals.hDevNames;
812
    printer.hInstance             = Globals.hInstance;
813

814
    /* Set some default flags */
815
    printer.Flags                 = PD_RETURNDC | PD_NOSELECTION;
816
    printer.nFromPage             = 0;
817 818
    printer.nMinPage              = 1;
    /* we really need to calculate number of pages to set nMaxPage and nToPage */
819 820
    printer.nToPage               = 0;
    printer.nMaxPage              = -1;
821 822 823
    /* Let commdlg manage copy settings */
    printer.nCopies               = (WORD)PD_USEDEVMODECOPIES;

824
    if (!PrintDlgW(&printer)) return;
825

826 827 828
    Globals.hDevMode = printer.hDevMode;
    Globals.hDevNames = printer.hDevNames;

829
    SetMapMode(printer.hDC, MM_TEXT);
830

831
    /* initialize DOCINFO */
832
    di.cbSize = sizeof(DOCINFOW);
833 834 835
    di.lpszDocName = Globals.szFileTitle;
    di.lpszOutput = NULL;
    di.lpszDatatype = NULL;
836 837 838
    di.fwType = 0; 

    /* Get the file text */
839
    size = GetWindowTextLengthW(Globals.hEdit) + 1;
840
    pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
841 842
    if (!pTemp)
    {
843 844 845
       DeleteDC(printer.hDC);
       ShowLastError();
       return;
846
    }
847
    size = GetWindowTextW(Globals.hEdit, pTemp, size);
848

849
    if (StartDocW(printer.hDC, &di) > 0)
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    {
        /* 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;
866
        hTextFont = CreateFontIndirectW(&lfFont);
867 868 869 870 871 872 873 874 875 876 877
        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;

878
            do {
879 880 881 882 883 884 885 886 887 888 889
                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;
890
                }
891 892 893 894 895 896
                else
                    dopage = 1;

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

898 899 900 901 902 903
            if (!ret) break;
        }
        EndDoc(printer.hDC);
        SelectObject(printer.hDC, old_font);
        DeleteObject(hTextFont);
    }
904
    DeleteDC(printer.hDC);
905
    HeapFree(GetProcessHeap(), 0, pTemp);
Alexandre Julliard's avatar
Alexandre Julliard committed
906 907 908 909
}

VOID DIALOG_FilePrinterSetup(VOID)
{
910
    PRINTDLGW printer;
911

912 913 914
    ZeroMemory(&printer, sizeof(printer));
    printer.lStructSize         = sizeof(printer);
    printer.hwndOwner           = Globals.hMainWnd;
915 916
    printer.hDevMode            = Globals.hDevMode;
    printer.hDevNames           = Globals.hDevNames;
917 918 919
    printer.hInstance           = Globals.hInstance;
    printer.Flags               = PD_PRINTSETUP;
    printer.nCopies             = 1;
920

921
    PrintDlgW(&printer);
922 923 924

    Globals.hDevMode = printer.hDevMode;
    Globals.hDevNames = printer.hDevNames;
Alexandre Julliard's avatar
Alexandre Julliard committed
925 926 927 928
}

VOID DIALOG_FileExit(VOID)
{
929
    PostMessageW(Globals.hMainWnd, WM_CLOSE, 0, 0l);
Alexandre Julliard's avatar
Alexandre Julliard committed
930 931 932 933
}

VOID DIALOG_EditUndo(VOID)
{
934
    SendMessageW(Globals.hEdit, EM_UNDO, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
935 936 937 938
}

VOID DIALOG_EditCut(VOID)
{
939
    SendMessageW(Globals.hEdit, WM_CUT, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
940 941 942 943
}

VOID DIALOG_EditCopy(VOID)
{
944
    SendMessageW(Globals.hEdit, WM_COPY, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
945 946 947 948
}

VOID DIALOG_EditPaste(VOID)
{
949
    SendMessageW(Globals.hEdit, WM_PASTE, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
950 951 952 953
}

VOID DIALOG_EditDelete(VOID)
{
954
    SendMessageW(Globals.hEdit, WM_CLEAR, 0, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
955 956 957 958
}

VOID DIALOG_EditSelectAll(VOID)
{
959
    SendMessageW(Globals.hEdit, EM_SETSEL, 0, -1);
Alexandre Julliard's avatar
Alexandre Julliard committed
960 961 962 963
}

VOID DIALOG_EditTimeDate(VOID)
{
964
    SYSTEMTIME   st;
965 966
    WCHAR        szDate[MAX_STRING_LEN];
    static const WCHAR spaceW[] = { ' ',0 };
967

968
    GetLocalTime(&st);
969

970
    GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, NULL, szDate, MAX_STRING_LEN);
971
    SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
972

973
    SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)spaceW);
974

975
    GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
976
    SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szDate);
Alexandre Julliard's avatar
Alexandre Julliard committed
977 978 979 980
}

VOID DIALOG_EditWrap(VOID)
{
981
    BOOL modify = FALSE;
Lauri Tulmin's avatar
Lauri Tulmin committed
982 983 984 985 986 987 988
    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;

989
    size = GetWindowTextLengthW(Globals.hEdit) + 1;
Lauri Tulmin's avatar
Lauri Tulmin committed
990 991 992 993 994 995
    pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
    if (!pTemp)
    {
        ShowLastError();
        return;
    }
996
    GetWindowTextW(Globals.hEdit, pTemp, size);
997
    modify = SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0);
Lauri Tulmin's avatar
Lauri Tulmin committed
998 999 1000
    DestroyWindow(Globals.hEdit);
    GetClientRect(Globals.hMainWnd, &rc);
    if( Globals.bWrapLongLines ) dwStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
1001
    Globals.hEdit = CreateWindowExW(WS_EX_CLIENTEDGE, editW, NULL, dwStyle,
Lauri Tulmin's avatar
Lauri Tulmin committed
1002 1003
                         0, 0, rc.right, rc.bottom, Globals.hMainWnd,
                         NULL, Globals.hInstance, NULL);
1004
    SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
Lauri Tulmin's avatar
Lauri Tulmin committed
1005
    SetWindowTextW(Globals.hEdit, pTemp);
1006
    SendMessageW(Globals.hEdit, EM_SETMODIFY, modify, 0);
Lauri Tulmin's avatar
Lauri Tulmin committed
1007 1008 1009
    SetFocus(Globals.hEdit);
    HeapFree(GetProcessHeap(), 0, pTemp);
    
1010 1011 1012
    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
1013 1014
}

1015 1016
VOID DIALOG_SelectFont(VOID)
{
1017 1018
    CHOOSEFONTW cf;
    LOGFONTW lf=Globals.lfFont;
1019 1020 1021 1022 1023

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

1026
    if( ChooseFontW(&cf) )
1027 1028 1029
    {
        HFONT currfont=Globals.hFont;

1030
        Globals.hFont=CreateFontIndirectW( &lf );
1031
        Globals.lfFont=lf;
1032
        SendMessageW( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, TRUE );
1033 1034 1035 1036 1037
        if( currfont!=NULL )
            DeleteObject( currfont );
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1038 1039
VOID DIALOG_Search(VOID)
{
1040 1041 1042 1043 1044 1045 1046
        /* Allow only one search/replace dialog to open */
        if(Globals.hFindReplaceDlg != NULL)
        {
            SetActiveWindow(Globals.hFindReplaceDlg);
            return;
        }

1047
        ZeroMemory(&Globals.find, sizeof(Globals.find));
1048 1049 1050
        Globals.find.lStructSize      = sizeof(Globals.find);
        Globals.find.hwndOwner        = Globals.hMainWnd;
        Globals.find.hInstance        = Globals.hInstance;
1051
        Globals.find.lpstrFindWhat    = Globals.szFindText;
1052
        Globals.find.wFindWhatLen     = ARRAY_SIZE(Globals.szFindText);
1053
        Globals.find.Flags            = FR_DOWN|FR_HIDEWHOLEWORD;
1054 1055 1056

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

1058
        Globals.hFindReplaceDlg = FindTextW(&Globals.find);
1059
        assert(Globals.hFindReplaceDlg !=0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1060 1061 1062 1063
}

VOID DIALOG_SearchNext(VOID)
{
1064 1065 1066 1067
    if (Globals.lastFind.lpstrFindWhat == NULL)
        DIALOG_Search();
    else                /* use the last find data */
        NOTEPAD_DoFind(&Globals.lastFind);
Alexandre Julliard's avatar
Alexandre Julliard committed
1068 1069
}

1070 1071
VOID DIALOG_Replace(VOID)
{
1072 1073 1074 1075 1076 1077 1078
        /* Allow only one search/replace dialog to open */
        if(Globals.hFindReplaceDlg != NULL)
        {
            SetActiveWindow(Globals.hFindReplaceDlg);
            return;
        }

1079 1080 1081 1082 1083
        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;
1084
        Globals.find.wFindWhatLen     = ARRAY_SIZE(Globals.szFindText);
1085
        Globals.find.lpstrReplaceWith = Globals.szReplaceText;
1086
        Globals.find.wReplaceWithLen  = ARRAY_SIZE(Globals.szReplaceText);
1087 1088 1089 1090 1091
        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    */

1092
        Globals.hFindReplaceDlg = ReplaceTextW(&Globals.find);
1093 1094 1095
        assert(Globals.hFindReplaceDlg !=0);
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1096 1097
VOID DIALOG_HelpContents(VOID)
{
1098
    WinHelpW(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1099 1100 1101 1102
}

VOID DIALOG_HelpSearch(VOID)
{
1103
        /* Search Help */
Alexandre Julliard's avatar
Alexandre Julliard committed
1104 1105 1106 1107
}

VOID DIALOG_HelpHelp(VOID)
{
1108
    WinHelpW(Globals.hMainWnd, helpfileW, HELP_HELPONHELP, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1109 1110
}

1111
VOID DIALOG_HelpAboutNotepad(VOID)
Alexandre Julliard's avatar
Alexandre Julliard committed
1112
{
1113
    static const WCHAR notepadW[] = { 'W','i','n','e',' ','N','o','t','e','p','a','d',0 };
1114
    WCHAR szNotepad[MAX_STRING_LEN];
1115 1116
    HICON icon = LoadImageW(Globals.hInstance, MAKEINTRESOURCEW(IDI_NOTEPAD),
                            IMAGE_ICON, 48, 48, LR_SHARED);
1117

1118
    LoadStringW(Globals.hInstance, STRING_NOTEPAD, szNotepad, ARRAY_SIZE(szNotepad));
1119
    ShellAboutW(Globals.hMainWnd, szNotepad, notepadW, icon);
Alexandre Julliard's avatar
Alexandre Julliard committed
1120 1121
}

1122

Alexandre Julliard's avatar
Alexandre Julliard committed
1123 1124
/***********************************************************************
 *
1125
 *           DIALOG_FilePageSetup
Alexandre Julliard's avatar
Alexandre Julliard committed
1126
 */
1127
VOID DIALOG_FilePageSetup(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
1128
{
1129 1130
    DialogBoxW(Globals.hInstance, MAKEINTRESOURCEW(DIALOG_PAGESETUP),
               Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
Alexandre Julliard's avatar
Alexandre Julliard committed
1131 1132 1133 1134 1135 1136 1137 1138
}


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

1139
static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1140
{
1141 1142

   switch (msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
1143 1144 1145
    {
    case WM_COMMAND:
      switch (wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1146 1147
        {
        case IDOK:
1148
          /* save user input and close dialog */
1149 1150
          GetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader, ARRAY_SIZE(Globals.szHeader));
          GetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter, ARRAY_SIZE(Globals.szFooter));
1151 1152 1153 1154 1155

          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
1156 1157 1158 1159
          EndDialog(hDlg, IDOK);
          return TRUE;

        case IDCANCEL:
1160
          /* discard user input and close dialog */
Alexandre Julliard's avatar
Alexandre Julliard committed
1161 1162
          EndDialog(hDlg, IDCANCEL);
          return TRUE;
1163 1164

        case IDHELP:
1165
        {
1166
          /* FIXME: Bring this to work */
1167 1168
          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 };
1169
          MessageBoxW(Globals.hMainWnd, sorryW, helpW, MB_ICONEXCLAMATION);
1170
          return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1171
        }
1172 1173 1174 1175

	default:
	    break;
        }
1176 1177 1178
      break;

    case WM_INITDIALOG:
1179
       /* fetch last user input prior to display dialog */
1180 1181
       SetDlgItemTextW(hDlg, IDC_PAGESETUP_HEADERVALUE, Globals.szHeader);
       SetDlgItemTextW(hDlg, IDC_PAGESETUP_FOOTERVALUE, Globals.szFooter);
1182 1183 1184 1185
       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);
1186
       break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1187
    }
1188

Alexandre Julliard's avatar
Alexandre Julliard committed
1189 1190
  return FALSE;
}