2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_IE 0x0400
41 #ifdef NONAMELESSUNION
52 static const WCHAR xszAppTitle
[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
54 static const WCHAR wszRichEditClass
[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
55 static const WCHAR wszMainWndClass
[] = {'W','O','R','D','P','A','D','T','O','P',0};
56 static const WCHAR wszAppTitle
[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
58 static const WCHAR stringFormat
[] = {'%','2','d','\0'};
61 static HWND hEditorWnd
;
63 static HMENU hPopupMenu
;
65 static UINT ID_FINDMSGSTRING
;
67 static DWORD wordWrap
[2];
68 static DWORD barState
[2];
69 static WPARAM fileFormat
= SF_RTF
;
71 static WCHAR wszFileName
[MAX_PATH
];
72 static WCHAR wszFilter
[MAX_STRING_LEN
*4+6*3+5];
73 static WCHAR wszDefaultFileName
[MAX_STRING_LEN
];
74 static WCHAR wszSaveChanges
[MAX_STRING_LEN
];
75 static WCHAR units_cmW
[MAX_STRING_LEN
];
77 static char units_cmA
[MAX_STRING_LEN
];
79 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
);
81 /* Load string resources */
82 static void DoLoadStrings(void)
85 static const WCHAR files_rtf
[] = {'*','.','r','t','f','\0'};
86 static const WCHAR files_txt
[] = {'*','.','t','x','t','\0'};
87 static const WCHAR files_all
[] = {'*','.','*','\0'};
89 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
91 LoadStringW(hInstance
, STRING_RICHTEXT_FILES_RTF
, p
, MAX_STRING_LEN
);
93 lstrcpyW(p
, files_rtf
);
95 LoadStringW(hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
);
97 lstrcpyW(p
, files_txt
);
99 LoadStringW(hInstance
, STRING_TEXT_FILES_UNICODE_TXT
, p
, MAX_STRING_LEN
);
100 p
+= lstrlenW(p
) + 1;
101 lstrcpyW(p
, files_txt
);
102 p
+= lstrlenW(p
) + 1;
103 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
104 p
+= lstrlenW(p
) + 1;
105 lstrcpyW(p
, files_all
);
106 p
+= lstrlenW(p
) + 1;
109 p
= wszDefaultFileName
;
110 LoadStringW(hInstance
, STRING_DEFAULT_FILENAME
, p
, MAX_STRING_LEN
);
113 LoadStringW(hInstance
, STRING_PROMPT_SAVE_CHANGES
, p
, MAX_STRING_LEN
);
115 LoadStringA(hInstance
, STRING_UNITS_CM
, units_cmA
, MAX_STRING_LEN
);
116 LoadStringW(hInstance
, STRING_UNITS_CM
, units_cmW
, MAX_STRING_LEN
);
119 static void AddButton(HWND hwndToolBar
, int nImage
, int nCommand
)
123 ZeroMemory(&button
, sizeof(button
));
124 button
.iBitmap
= nImage
;
125 button
.idCommand
= nCommand
;
126 button
.fsState
= TBSTATE_ENABLED
;
127 button
.fsStyle
= TBSTYLE_BUTTON
;
130 SendMessageW(hwndToolBar
, TB_ADDBUTTONSW
, 1, (LPARAM
)&button
);
133 static void AddSeparator(HWND hwndToolBar
)
137 ZeroMemory(&button
, sizeof(button
));
139 button
.idCommand
= 0;
141 button
.fsStyle
= TBSTYLE_SEP
;
144 SendMessageW(hwndToolBar
, TB_ADDBUTTONSW
, 1, (LPARAM
)&button
);
147 static DWORD CALLBACK
stream_in(DWORD_PTR cookie
, LPBYTE buffer
, LONG cb
, LONG
*pcb
)
149 HANDLE hFile
= (HANDLE
)cookie
;
152 if(!ReadFile(hFile
, buffer
, cb
, &read
, 0))
160 static DWORD CALLBACK
stream_out(DWORD_PTR cookie
, LPBYTE buffer
, LONG cb
, LONG
*pcb
)
164 HANDLE hFile
= (HANDLE
)cookie
;
166 ret
= WriteFile(hFile
, buffer
, cb
, &written
, 0);
168 if(!ret
|| (cb
!= written
))
176 LPWSTR
file_basename(LPWSTR path
)
178 LPWSTR pos
= path
+ lstrlenW(path
);
182 if(*pos
== '\\' || *pos
== '/')
192 static void set_caption(LPCWSTR wszNewFileName
)
194 static const WCHAR wszSeparator
[] = {' ','-',' '};
199 wszNewFileName
= wszDefaultFileName
;
201 wszNewFileName
= file_basename((LPWSTR
)wszNewFileName
);
203 wszCaption
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
204 lstrlenW(wszNewFileName
)*sizeof(WCHAR
)+sizeof(wszSeparator
)+sizeof(wszAppTitle
));
209 memcpy(wszCaption
, wszNewFileName
, lstrlenW(wszNewFileName
)*sizeof(WCHAR
));
210 length
+= lstrlenW(wszNewFileName
);
211 memcpy(wszCaption
+ length
, wszSeparator
, sizeof(wszSeparator
));
212 length
+= sizeof(wszSeparator
) / sizeof(WCHAR
);
213 memcpy(wszCaption
+ length
, wszAppTitle
, sizeof(wszAppTitle
));
215 SetWindowTextW(hMainWnd
, wszCaption
);
217 HeapFree(GetProcessHeap(), 0, wszCaption
);
220 static BOOL
validate_endptr(LPCSTR endptr
, BOOL units
)
227 while(*endptr
== ' ')
231 return *endptr
== '\0';
233 /* FIXME: Allow other units and convert between them */
234 if(!lstrcmpA(endptr
, units_cmA
))
237 return *endptr
== '\0';
240 static BOOL
number_from_string(LPCWSTR string
, float *num
, BOOL units
)
243 char buffer
[MAX_STRING_LEN
];
244 char *endptr
= buffer
;
246 WideCharToMultiByte(CP_ACP
, 0, string
, -1, buffer
, MAX_STRING_LEN
, NULL
, NULL
);
249 ret
= strtod(buffer
, &endptr
);
251 if((ret
== 0 && errno
!= 0) || endptr
== buffer
|| !validate_endptr(endptr
, units
))
261 static void set_size(float size
)
265 ZeroMemory(&fmt
, sizeof(fmt
));
266 fmt
.cbSize
= sizeof(fmt
);
267 fmt
.dwMask
= CFM_SIZE
;
268 fmt
.yHeight
= (int)(size
* 20.0);
269 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
272 static void add_size(HWND hSizeListWnd
, unsigned size
)
275 COMBOBOXEXITEMW cbItem
;
276 cbItem
.mask
= CBEIF_TEXT
;
279 wsprintfW(buffer
, stringFormat
, size
);
280 cbItem
.pszText
= (LPWSTR
)buffer
;
281 SendMessageW(hSizeListWnd
, CBEM_INSERTITEMW
, 0, (LPARAM
)&cbItem
);
284 static void populate_size_list(HWND hSizeListWnd
)
286 HWND hReBarWnd
= GetDlgItem(hMainWnd
, IDC_REBAR
);
287 HWND hFontListWnd
= GetDlgItem(hReBarWnd
, IDC_FONTLIST
);
288 COMBOBOXEXITEMW cbFontItem
;
290 HWND hListEditWnd
= (HWND
)SendMessageW(hSizeListWnd
, CBEM_GETEDITCONTROL
, 0, 0);
291 HDC hdc
= GetDC(hMainWnd
);
292 static const unsigned choices
[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
297 ZeroMemory(&fmt
, sizeof(fmt
));
298 fmt
.cbSize
= sizeof(fmt
);
299 SendMessageW(hEditorWnd
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
301 cbFontItem
.mask
= CBEIF_LPARAM
;
302 cbFontItem
.iItem
= SendMessageW(hFontListWnd
, CB_FINDSTRINGEXACT
, -1, (LPARAM
)fmt
.szFaceName
);
303 SendMessageW(hFontListWnd
, CBEM_GETITEMW
, 0, (LPARAM
)&cbFontItem
);
305 fontStyle
= (DWORD
)LOWORD(cbFontItem
.lParam
);
307 SendMessageW(hSizeListWnd
, CB_RESETCONTENT
, 0, 0);
309 if((fontStyle
& RASTER_FONTTYPE
) && cbFontItem
.iItem
)
311 add_size(hSizeListWnd
, (BYTE
)MulDiv(HIWORD(cbFontItem
.lParam
), 72,
312 GetDeviceCaps(hdc
, LOGPIXELSY
)));
315 for(i
= 0; i
< sizeof(choices
)/sizeof(choices
[0]); i
++)
316 add_size(hSizeListWnd
, choices
[i
]);
319 wsprintfW(buffer
, stringFormat
, fmt
.yHeight
/ 20);
320 SendMessageW(hListEditWnd
, WM_SETTEXT
, 0, (LPARAM
)buffer
);
323 static void update_size_list(void)
325 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
326 HWND hwndSizeList
= GetDlgItem(hReBar
, IDC_SIZELIST
);
327 HWND hwndSizeListEdit
= (HWND
)SendMessageW(hwndSizeList
, CBEM_GETEDITCONTROL
, 0, 0);
328 WCHAR fontSize
[MAX_STRING_LEN
], sizeBuffer
[MAX_STRING_LEN
];
331 ZeroMemory(&fmt
, sizeof(fmt
));
332 fmt
.cbSize
= sizeof(fmt
);
334 SendMessageW(hEditorWnd
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
336 SendMessageW(hwndSizeListEdit
, WM_GETTEXT
, MAX_PATH
, (LPARAM
)fontSize
);
337 wsprintfW(sizeBuffer
, stringFormat
, fmt
.yHeight
/ 20);
339 if(lstrcmpW(fontSize
, sizeBuffer
))
340 SendMessageW(hwndSizeListEdit
, WM_SETTEXT
, 0, (LPARAM
)sizeBuffer
);
343 static void update_font_list(void)
345 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
346 HWND hFontList
= GetDlgItem(hReBar
, IDC_FONTLIST
);
347 HWND hFontListEdit
= (HWND
)SendMessageW(hFontList
, CBEM_GETEDITCONTROL
, 0, 0);
348 WCHAR fontName
[MAX_STRING_LEN
];
351 ZeroMemory(&fmt
, sizeof(fmt
));
352 fmt
.cbSize
= sizeof(fmt
);
354 SendMessageW(hEditorWnd
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
355 if (!SendMessageW(hFontListEdit
, WM_GETTEXT
, MAX_PATH
, (LPARAM
)fontName
)) return;
357 if(lstrcmpW(fontName
, fmt
.szFaceName
))
359 SendMessageW(hFontListEdit
, WM_SETTEXT
, 0, (LPARAM
)fmt
.szFaceName
);
360 populate_size_list(GetDlgItem(hReBar
, IDC_SIZELIST
));
367 static void clear_formatting(void)
371 pf
.cbSize
= sizeof(pf
);
372 pf
.dwMask
= PFM_ALIGNMENT
;
373 pf
.wAlignment
= PFA_LEFT
;
374 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
377 static int fileformat_number(WPARAM format
)
381 if(format
== SF_TEXT
)
384 } else if (format
== (SF_TEXT
| SF_UNICODE
))
391 static WPARAM
fileformat_flags(int format
)
393 WPARAM flags
[] = { SF_RTF
, SF_TEXT
, SF_TEXT
| SF_UNICODE
};
395 return flags
[format
];
398 static void set_font(LPCWSTR wszFaceName
)
400 HWND hReBarWnd
= GetDlgItem(hMainWnd
, IDC_REBAR
);
401 HWND hSizeListWnd
= GetDlgItem(hReBarWnd
, IDC_SIZELIST
);
402 HWND hFontListWnd
= GetDlgItem(hReBarWnd
, IDC_FONTLIST
);
403 HWND hFontListEditWnd
= (HWND
)SendMessageW(hFontListWnd
, CBEM_GETEDITCONTROL
, 0, 0);
406 ZeroMemory(&fmt
, sizeof(fmt
));
408 fmt
.cbSize
= sizeof(fmt
);
409 fmt
.dwMask
= CFM_FACE
;
411 lstrcpyW(fmt
.szFaceName
, wszFaceName
);
413 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
415 populate_size_list(hSizeListWnd
);
417 SendMessageW(hFontListEditWnd
, WM_SETTEXT
, 0, (LPARAM
)(LPWSTR
)wszFaceName
);
420 static void set_default_font(void)
422 static const WCHAR richTextFont
[] = {'T','i','m','e','s',' ','N','e','w',' ',
423 'R','o','m','a','n',0};
424 static const WCHAR plainTextFont
[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
428 ZeroMemory(&fmt
, sizeof(fmt
));
430 fmt
.cbSize
= sizeof(fmt
);
431 fmt
.dwMask
= CFM_FACE
| CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINE
;
434 if(fileFormat
& SF_RTF
)
437 font
= plainTextFont
;
439 lstrcpyW(fmt
.szFaceName
, font
);
441 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
444 static void add_font(LPCWSTR fontName
, DWORD fontType
, HWND hListWnd
, NEWTEXTMETRICEXW
*ntmc
)
446 COMBOBOXEXITEMW cbItem
;
447 WCHAR buffer
[MAX_PATH
];
450 cbItem
.mask
= CBEIF_TEXT
;
451 cbItem
.pszText
= buffer
;
452 cbItem
.cchTextMax
= MAX_STRING_LEN
;
455 while(SendMessageW(hListWnd
, CBEM_GETITEMW
, 0, (LPARAM
)&cbItem
))
457 if(lstrcmpiW(cbItem
.pszText
, fontName
) <= 0)
462 cbItem
.pszText
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName
) + 1)*sizeof(WCHAR
) );
463 lstrcpyW( cbItem
.pszText
, fontName
);
465 cbItem
.mask
|= CBEIF_LPARAM
;
466 if(fontType
& RASTER_FONTTYPE
)
467 fontHeight
= ntmc
->ntmTm
.tmHeight
- ntmc
->ntmTm
.tmInternalLeading
;
469 cbItem
.lParam
= MAKELONG(fontType
,fontHeight
);
470 SendMessageW(hListWnd
, CBEM_INSERTITEMW
, 0, (LPARAM
)&cbItem
);
471 HeapFree( GetProcessHeap(), 0, cbItem
.pszText
);
474 static void dialog_choose_font(void)
479 HDC hDC
= GetDC(hMainWnd
);
481 ZeroMemory(&cf
, sizeof(cf
));
482 cf
.lStructSize
= sizeof(cf
);
483 cf
.hwndOwner
= hMainWnd
;
485 cf
.Flags
= CF_SCREENFONTS
| CF_NOSCRIPTSEL
| CF_INITTOLOGFONTSTRUCT
| CF_EFFECTS
;
487 ZeroMemory(&fmt
, sizeof(fmt
));
488 fmt
.cbSize
= sizeof(fmt
);
490 SendMessageW(hEditorWnd
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
491 lstrcpyW(cf
.lpLogFont
->lfFaceName
, fmt
.szFaceName
);
492 cf
.lpLogFont
->lfItalic
= (fmt
.dwEffects
& CFE_ITALIC
) ? TRUE
: FALSE
;
493 cf
.lpLogFont
->lfWeight
= (fmt
.dwEffects
& CFE_BOLD
) ? FW_BOLD
: FW_NORMAL
;
494 cf
.lpLogFont
->lfUnderline
= (fmt
.dwEffects
& CFE_UNDERLINE
) ? TRUE
: FALSE
;
495 cf
.lpLogFont
->lfStrikeOut
= (fmt
.dwEffects
& CFE_STRIKEOUT
) ? TRUE
: FALSE
;
496 cf
.lpLogFont
->lfHeight
= -MulDiv(fmt
.yHeight
/ 20, GetDeviceCaps(hDC
, LOGPIXELSY
), 72);
497 cf
.rgbColors
= fmt
.crTextColor
;
501 ZeroMemory(&fmt
, sizeof(fmt
));
502 fmt
.cbSize
= sizeof(fmt
);
503 fmt
.dwMask
= CFM_BOLD
| CFM_ITALIC
| CFM_SIZE
| CFM_UNDERLINE
| CFM_STRIKEOUT
| CFM_COLOR
;
504 fmt
.yHeight
= cf
.iPointSize
* 2;
506 if(cf
.nFontType
& BOLD_FONTTYPE
)
507 fmt
.dwEffects
|= CFE_BOLD
;
508 if(cf
.nFontType
& ITALIC_FONTTYPE
)
509 fmt
.dwEffects
|= CFE_ITALIC
;
510 if(cf
.lpLogFont
->lfUnderline
== TRUE
)
511 fmt
.dwEffects
|= CFE_UNDERLINE
;
512 if(cf
.lpLogFont
->lfStrikeOut
== TRUE
)
513 fmt
.dwEffects
|= CFE_STRIKEOUT
;
515 fmt
.crTextColor
= cf
.rgbColors
;
517 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
518 set_font(cf
.lpLogFont
->lfFaceName
);
523 int CALLBACK
enum_font_proc(const LOGFONTW
*lpelfe
, const TEXTMETRICW
*lpntme
,
524 DWORD FontType
, LPARAM lParam
)
526 HWND hListWnd
= (HWND
) lParam
;
528 if(SendMessageW(hListWnd
, CB_FINDSTRINGEXACT
, -1, (LPARAM
)lpelfe
->lfFaceName
) == CB_ERR
)
531 add_font((LPWSTR
)lpelfe
->lfFaceName
, FontType
, hListWnd
, (NEWTEXTMETRICEXW
*)lpntme
);
537 static void populate_font_list(HWND hListWnd
)
539 HDC hdc
= GetDC(hMainWnd
);
541 HWND hListEditWnd
= (HWND
)SendMessageW(hListWnd
, CBEM_GETEDITCONTROL
, 0, 0);
544 fontinfo
.lfCharSet
= DEFAULT_CHARSET
;
545 *fontinfo
.lfFaceName
= '\0';
546 fontinfo
.lfPitchAndFamily
= 0;
548 EnumFontFamiliesExW(hdc
, &fontinfo
, enum_font_proc
,
549 (LPARAM
)hListWnd
, 0);
551 ZeroMemory(&fmt
, sizeof(fmt
));
552 fmt
.cbSize
= sizeof(fmt
);
553 SendMessageW(hEditorWnd
, EM_GETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
554 SendMessageW(hListEditWnd
, WM_SETTEXT
, 0, (LPARAM
)fmt
.szFaceName
);
557 static void update_window(void)
561 GetWindowRect(hMainWnd
, &rect
);
563 (void) OnSize(hMainWnd
, SIZE_RESTORED
, MAKELONG(rect
.bottom
, rect
.right
));
566 static BOOL
is_bar_visible(int bandId
)
568 return barState
[reg_formatindex(fileFormat
)] & (1 << bandId
);
571 static void store_bar_state(int bandId
, BOOL show
)
573 int formatIndex
= reg_formatindex(fileFormat
);
576 barState
[formatIndex
] |= (1 << bandId
);
578 barState
[formatIndex
] &= ~(1 << bandId
);
581 static void set_toolbar_state(int bandId
, BOOL show
)
583 HWND hwndReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
585 SendMessageW(hwndReBar
, RB_SHOWBAND
, SendMessageW(hwndReBar
, RB_IDTOINDEX
, bandId
, 0), show
);
587 if(bandId
== BANDID_TOOLBAR
)
589 REBARBANDINFOW rbbinfo
;
590 int index
= SendMessageW(hwndReBar
, RB_IDTOINDEX
, BANDID_FONTLIST
, 0);
592 rbbinfo
.cbSize
= sizeof(rbbinfo
);
593 rbbinfo
.fMask
= RBBIM_STYLE
;
595 SendMessageW(hwndReBar
, RB_GETBANDINFO
, index
, (LPARAM
)&rbbinfo
);
598 rbbinfo
.fStyle
&= ~RBBS_BREAK
;
600 rbbinfo
.fStyle
|= RBBS_BREAK
;
602 SendMessageW(hwndReBar
, RB_SETBANDINFO
, index
, (LPARAM
)&rbbinfo
);
605 if(bandId
== BANDID_TOOLBAR
|| bandId
== BANDID_FORMATBAR
|| bandId
== BANDID_RULER
)
606 store_bar_state(bandId
, show
);
609 static void set_statusbar_state(BOOL show
)
611 HWND hStatusWnd
= GetDlgItem(hMainWnd
, IDC_STATUSBAR
);
613 ShowWindow(hStatusWnd
, show
? SW_SHOW
: SW_HIDE
);
614 store_bar_state(BANDID_STATUSBAR
, show
);
617 static void set_bar_states(void)
619 set_toolbar_state(BANDID_TOOLBAR
, is_bar_visible(BANDID_TOOLBAR
));
620 set_toolbar_state(BANDID_FONTLIST
, is_bar_visible(BANDID_FORMATBAR
));
621 set_toolbar_state(BANDID_SIZELIST
, is_bar_visible(BANDID_FORMATBAR
));
622 set_toolbar_state(BANDID_FORMATBAR
, is_bar_visible(BANDID_FORMATBAR
));
623 set_toolbar_state(BANDID_RULER
, is_bar_visible(BANDID_RULER
));
624 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR
));
629 static void preview_exit(HWND hMainWnd
)
631 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
632 HMENU hMenu
= LoadMenuW(hInstance
, MAKEINTRESOURCEW(IDM_MAINMENU
));
633 HWND hEditorWnd
= GetDlgItem(hMainWnd
, IDC_EDITOR
);
636 ShowWindow(hEditorWnd
, TRUE
);
638 close_preview(hMainWnd
);
640 SetMenu(hMainWnd
, hMenu
);
641 registry_read_filelist(hMainWnd
);
646 static void set_fileformat(WPARAM format
)
649 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
653 hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_TXT
));
655 hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_RTF
));
657 SendMessageW(hMainWnd
, WM_SETICON
, ICON_BIG
, (LPARAM
)hIcon
);
661 target_device(hMainWnd
, wordWrap
[reg_formatindex(fileFormat
)]);
664 static void DoOpenFile(LPCWSTR szOpenFileName
)
670 WPARAM format
= SF_TEXT
;
672 hFile
= CreateFileW(szOpenFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
673 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
674 if (hFile
== INVALID_HANDLE_VALUE
)
677 ReadFile(hFile
, fileStart
, 5, &readOut
, NULL
);
678 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
680 if(readOut
>= 2 && (BYTE
)fileStart
[0] == 0xff && (BYTE
)fileStart
[1] == 0xfe)
682 format
= SF_TEXT
| SF_UNICODE
;
683 SetFilePointer(hFile
, 2, NULL
, FILE_BEGIN
);
684 } else if(readOut
>= 5)
686 static const char header
[] = "{\\rtf";
687 static const BYTE STG_magic
[] = { 0xd0,0xcf,0x11,0xe0 };
689 if(!memcmp(header
, fileStart
, 5))
691 else if (!memcmp(STG_magic
, fileStart
, sizeof(STG_magic
)))
694 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED
), wszAppTitle
,
695 MB_OK
| MB_ICONEXCLAMATION
);
700 es
.dwCookie
= (DWORD_PTR
)hFile
;
701 es
.pfnCallback
= stream_in
;
704 set_fileformat(format
);
705 SendMessageW(hEditorWnd
, EM_STREAMIN
, format
, (LPARAM
)&es
);
709 SetFocus(hEditorWnd
);
711 set_caption(szOpenFileName
);
713 lstrcpyW(wszFileName
, szOpenFileName
);
714 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
715 registry_set_filelist(szOpenFileName
, hMainWnd
);
719 static void DoSaveFile(LPCWSTR wszSaveFileName
, WPARAM format
)
725 hFile
= CreateFileW(wszSaveFileName
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
726 FILE_ATTRIBUTE_NORMAL
, NULL
);
728 if(hFile
== INVALID_HANDLE_VALUE
)
731 if(format
== (SF_TEXT
| SF_UNICODE
))
733 static const BYTE unicode
[] = {0xff,0xfe};
735 WriteFile(hFile
, &unicode
, sizeof(unicode
), &writeOut
, 0);
737 if(writeOut
!= sizeof(unicode
))
741 stream
.dwCookie
= (DWORD_PTR
)hFile
;
742 stream
.pfnCallback
= stream_out
;
744 ret
= SendMessageW(hEditorWnd
, EM_STREAMOUT
, format
, (LPARAM
)&stream
);
748 SetFocus(hEditorWnd
);
753 gt
.flags
= GTL_DEFAULT
;
756 if(SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0))
760 lstrcpyW(wszFileName
, wszSaveFileName
);
761 set_caption(wszFileName
);
762 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
763 set_fileformat(format
);
766 static void DialogSaveFile(void)
770 WCHAR wszFile
[MAX_PATH
] = {'\0'};
771 static const WCHAR wszDefExt
[] = {'r','t','f','\0'};
773 ZeroMemory(&sfn
, sizeof(sfn
));
775 sfn
.lStructSize
= sizeof(sfn
);
776 sfn
.Flags
= OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
;
777 sfn
.hwndOwner
= hMainWnd
;
778 sfn
.lpstrFilter
= wszFilter
;
779 sfn
.lpstrFile
= wszFile
;
780 sfn
.nMaxFile
= MAX_PATH
;
781 sfn
.lpstrDefExt
= wszDefExt
;
782 sfn
.nFilterIndex
= fileformat_number(fileFormat
)+1;
784 while(GetSaveFileNameW(&sfn
))
786 if(fileformat_flags(sfn
.nFilterIndex
-1) != SF_RTF
)
788 if(MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING
),
789 wszAppTitle
, MB_YESNO
| MB_ICONEXCLAMATION
) != IDYES
)
794 DoSaveFile(sfn
.lpstrFile
, fileformat_flags(sfn
.nFilterIndex
-1));
799 DoSaveFile(sfn
.lpstrFile
, fileformat_flags(sfn
.nFilterIndex
-1));
805 static BOOL
prompt_save_changes(void)
810 gt
.flags
= GTL_NUMCHARS
;
812 if(!SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0))
816 if(!SendMessageW(hEditorWnd
, EM_GETMODIFY
, 0, 0))
821 LPWSTR displayFileName
;
826 displayFileName
= wszDefaultFileName
;
828 displayFileName
= file_basename(wszFileName
);
830 text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
831 (lstrlenW(displayFileName
)+lstrlenW(wszSaveChanges
))*sizeof(WCHAR
));
836 wsprintfW(text
, wszSaveChanges
, displayFileName
);
838 ret
= MessageBoxW(hMainWnd
, text
, wszAppTitle
, MB_YESNOCANCEL
| MB_ICONEXCLAMATION
);
840 HeapFree(GetProcessHeap(), 0, text
);
849 DoSaveFile(wszFileName
, fileFormat
);
860 static void DialogOpenFile(void)
864 WCHAR wszFile
[MAX_PATH
] = {'\0'};
865 static const WCHAR wszDefExt
[] = {'r','t','f','\0'};
867 ZeroMemory(&ofn
, sizeof(ofn
));
869 ofn
.lStructSize
= sizeof(ofn
);
870 ofn
.Flags
= OFN_HIDEREADONLY
| OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
;
871 ofn
.hwndOwner
= hMainWnd
;
872 ofn
.lpstrFilter
= wszFilter
;
873 ofn
.lpstrFile
= wszFile
;
874 ofn
.nMaxFile
= MAX_PATH
;
875 ofn
.lpstrDefExt
= wszDefExt
;
876 ofn
.nFilterIndex
= fileformat_number(fileFormat
)+1;
878 if(GetOpenFileNameW(&ofn
))
880 if(prompt_save_changes())
881 DoOpenFile(ofn
.lpstrFile
);
885 static void dialog_about(void)
887 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
888 HICON icon
= LoadImageW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
), IMAGE_ICON
, 48, 48, LR_SHARED
);
889 ShellAboutW(hMainWnd
, wszAppTitle
, 0, icon
);
892 static INT_PTR CALLBACK
formatopts_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
898 LPPROPSHEETPAGEW ps
= (LPPROPSHEETPAGEW
)lParam
;
901 HWND hIdWnd
= GetDlgItem(hWnd
, IDC_PAGEFMT_ID
);
903 sprintf(id
, "%d\n", (int)ps
->lParam
);
904 SetWindowTextA(hIdWnd
, id
);
905 if(wordWrap
[ps
->lParam
] == ID_WORDWRAP_WINDOW
)
906 wrap
= IDC_PAGEFMT_WW
;
907 else if(wordWrap
[ps
->lParam
] == ID_WORDWRAP_MARGIN
)
908 wrap
= IDC_PAGEFMT_WM
;
911 CheckRadioButton(hWnd
, IDC_PAGEFMT_WW
,
912 IDC_PAGEFMT_WM
, wrap
);
914 if(barState
[ps
->lParam
] & (1 << BANDID_TOOLBAR
))
915 CheckDlgButton(hWnd
, IDC_PAGEFMT_TB
, TRUE
);
916 if(barState
[ps
->lParam
] & (1 << BANDID_FORMATBAR
))
917 CheckDlgButton(hWnd
, IDC_PAGEFMT_FB
, TRUE
);
918 if(barState
[ps
->lParam
] & (1 << BANDID_RULER
))
919 CheckDlgButton(hWnd
, IDC_PAGEFMT_RU
, TRUE
);
920 if(barState
[ps
->lParam
] & (1 << BANDID_STATUSBAR
))
921 CheckDlgButton(hWnd
, IDC_PAGEFMT_SB
, TRUE
);
926 switch(LOWORD(wParam
))
930 CheckRadioButton(hWnd
, IDC_PAGEFMT_WW
, IDC_PAGEFMT_WM
,
938 CheckDlgButton(hWnd
, LOWORD(wParam
),
939 !IsDlgButtonChecked(hWnd
, LOWORD(wParam
)));
945 LPNMHDR header
= (LPNMHDR
)lParam
;
946 if(header
->code
== PSN_APPLY
)
948 HWND hIdWnd
= GetDlgItem(hWnd
, IDC_PAGEFMT_ID
);
952 GetWindowTextA(hIdWnd
, sid
, 4);
954 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_WW
))
955 wordWrap
[id
] = ID_WORDWRAP_WINDOW
;
956 else if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_WM
))
957 wordWrap
[id
] = ID_WORDWRAP_MARGIN
;
959 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_TB
))
960 barState
[id
] |= (1 << BANDID_TOOLBAR
);
962 barState
[id
] &= ~(1 << BANDID_TOOLBAR
);
964 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_FB
))
965 barState
[id
] |= (1 << BANDID_FORMATBAR
);
967 barState
[id
] &= ~(1 << BANDID_FORMATBAR
);
969 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_RU
))
970 barState
[id
] |= (1 << BANDID_RULER
);
972 barState
[id
] &= ~(1 << BANDID_RULER
);
974 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_SB
))
975 barState
[id
] |= (1 << BANDID_STATUSBAR
);
977 barState
[id
] &= ~(1 << BANDID_STATUSBAR
);
985 static void dialog_viewproperties(void)
987 PROPSHEETPAGEW psp
[2];
988 PROPSHEETHEADERW psh
;
990 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
991 LPCPROPSHEETPAGEW ppsp
= (LPCPROPSHEETPAGEW
)&psp
;
993 psp
[0].dwSize
= sizeof(PROPSHEETPAGEW
);
994 psp
[0].dwFlags
= PSP_USETITLE
;
995 U(psp
[0]).pszTemplate
= MAKEINTRESOURCEW(IDD_FORMATOPTS
);
996 psp
[0].pfnDlgProc
= formatopts_proc
;
997 psp
[0].hInstance
= hInstance
;
998 psp
[0].lParam
= reg_formatindex(SF_TEXT
);
999 psp
[0].pfnCallback
= NULL
;
1000 psp
[0].pszTitle
= MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT
);
1001 for(i
= 1; i
< sizeof(psp
)/sizeof(psp
[0]); i
++)
1003 psp
[i
].dwSize
= psp
[0].dwSize
;
1004 psp
[i
].dwFlags
= psp
[0].dwFlags
;
1005 U(psp
[i
]).pszTemplate
= U(psp
[0]).pszTemplate
;
1006 psp
[i
].pfnDlgProc
= psp
[0].pfnDlgProc
;
1007 psp
[i
].hInstance
= psp
[0].hInstance
;
1008 psp
[i
].lParam
= reg_formatindex(SF_RTF
);
1009 psp
[i
].pfnCallback
= psp
[0].pfnCallback
;
1010 psp
[i
].pszTitle
= MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT
);
1013 psh
.dwSize
= sizeof(psh
);
1014 psh
.dwFlags
= PSH_USEICONID
| PSH_PROPSHEETPAGE
| PSH_NOAPPLYNOW
;
1015 psh
.hwndParent
= hMainWnd
;
1016 psh
.hInstance
= hInstance
;
1017 psh
.pszCaption
= MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE
);
1018 psh
.nPages
= sizeof(psp
)/sizeof(psp
[0]);
1019 U3(psh
).ppsp
= ppsp
;
1020 U(psh
).pszIcon
= MAKEINTRESOURCEW(IDI_WORDPAD
);
1022 if(fileFormat
& SF_RTF
)
1023 U2(psh
).nStartPage
= 1;
1025 U2(psh
).nStartPage
= 0;
1026 PropertySheetW(&psh
);
1028 target_device(hMainWnd
, wordWrap
[reg_formatindex(fileFormat
)]);
1031 static void HandleCommandLine(LPWSTR cmdline
)
1036 /* skip white space */
1037 while (*cmdline
== ' ') cmdline
++;
1039 /* skip executable name */
1040 delimiter
= (*cmdline
== '"' ? '"' : ' ');
1042 if (*cmdline
== delimiter
) cmdline
++;
1043 while (*cmdline
&& *cmdline
!= delimiter
) cmdline
++;
1044 if (*cmdline
== delimiter
) cmdline
++;
1048 while (isspace(*cmdline
)) cmdline
++;
1050 if (*cmdline
== '-' || *cmdline
== '/')
1052 if (!cmdline
[2] || isspace(cmdline
[2]))
1063 /* a filename starting by / */
1070 /* file name is passed on the command line */
1071 if (cmdline
[0] == '"')
1074 cmdline
[lstrlenW(cmdline
) - 1] = 0;
1076 DoOpenFile(cmdline
);
1077 InvalidateRect(hMainWnd
, NULL
, FALSE
);
1081 MessageBox(hMainWnd
, "Printing not implemented", "WordPad", MB_OK
);
1084 static LRESULT
handle_findmsg(LPFINDREPLACEW pFr
)
1086 if(pFr
->Flags
& FR_DIALOGTERM
)
1089 pFr
->Flags
= FR_FINDNEXT
;
1093 if(pFr
->Flags
& FR_FINDNEXT
|| pFr
->Flags
& FR_REPLACE
|| pFr
->Flags
& FR_REPLACEALL
)
1095 DWORD flags
= FR_DOWN
;
1097 static CHARRANGE cr
;
1102 HMENU hMenu
= GetMenu(hMainWnd
);
1105 mi
.cbSize
= sizeof(mi
);
1106 mi
.fMask
= MIIM_DATA
;
1108 SetMenuItemInfoW(hMenu
, ID_FIND_NEXT
, FALSE
, &mi
);
1110 gt
.flags
= GTL_NUMCHARS
;
1113 length
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1115 if(pFr
->lCustData
== -1)
1117 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&startPos
, (LPARAM
)&end
);
1118 cr
.cpMin
= startPos
;
1119 pFr
->lCustData
= startPos
;
1121 if(cr
.cpMin
== length
)
1125 startPos
= pFr
->lCustData
;
1128 if(cr
.cpMax
> length
)
1136 ft
.lpstrText
= pFr
->lpstrFindWhat
;
1138 if(pFr
->Flags
& FR_MATCHCASE
)
1139 flags
|= FR_MATCHCASE
;
1140 if(pFr
->Flags
& FR_WHOLEWORD
)
1141 flags
|= FR_WHOLEWORD
;
1143 ret
= SendMessageW(hEditorWnd
, EM_FINDTEXTW
, (WPARAM
)flags
, (LPARAM
)&ft
);
1147 if(cr
.cpMax
== length
&& cr
.cpMax
!= startPos
)
1149 ft
.chrg
.cpMin
= cr
.cpMin
= 0;
1150 ft
.chrg
.cpMax
= cr
.cpMax
= startPos
;
1152 ret
= SendMessageW(hEditorWnd
, EM_FINDTEXTW
, (WPARAM
)flags
, (LPARAM
)&ft
);
1158 pFr
->lCustData
= -1;
1159 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED
), wszAppTitle
,
1160 MB_OK
| MB_ICONASTERISK
);
1163 end
= ret
+ lstrlenW(pFr
->lpstrFindWhat
);
1165 SendMessageW(hEditorWnd
, EM_SETSEL
, (WPARAM
)ret
, (LPARAM
)end
);
1166 SendMessageW(hEditorWnd
, EM_SCROLLCARET
, 0, 0);
1168 if(pFr
->Flags
& FR_REPLACE
|| pFr
->Flags
& FR_REPLACEALL
)
1169 SendMessageW(hEditorWnd
, EM_REPLACESEL
, TRUE
, (LPARAM
)pFr
->lpstrReplaceWith
);
1171 if(pFr
->Flags
& FR_REPLACEALL
)
1172 handle_findmsg(pFr
);
1179 static void dialog_find(LPFINDREPLACEW fr
, BOOL replace
)
1181 static WCHAR findBuffer
[MAX_STRING_LEN
];
1183 ZeroMemory(fr
, sizeof(FINDREPLACEW
));
1184 fr
->lStructSize
= sizeof(FINDREPLACEW
);
1185 fr
->hwndOwner
= hMainWnd
;
1186 fr
->Flags
= FR_HIDEUPDOWN
;
1187 fr
->lpstrFindWhat
= findBuffer
;
1189 fr
->wFindWhatLen
= MAX_STRING_LEN
*sizeof(WCHAR
);
1192 hFindWnd
= ReplaceTextW(fr
);
1194 hFindWnd
= FindTextW(fr
);
1197 static int current_units_to_twips(float number
)
1199 int twips
= (int)(number
* TWIPS_PER_CM
);
1203 static void append_current_units(LPWSTR buffer
)
1205 static const WCHAR space
[] = {' ', 0};
1206 lstrcatW(buffer
, space
);
1207 lstrcatW(buffer
, units_cmW
);
1210 static void number_with_units(LPWSTR buffer
, int number
)
1212 float converted
= (float)number
/ TWIPS_PER_CM
;
1213 char string
[MAX_STRING_LEN
];
1215 sprintf(string
, "%.2f ", converted
);
1216 lstrcatA(string
, units_cmA
);
1217 MultiByteToWideChar(CP_ACP
, 0, string
, -1, buffer
, MAX_STRING_LEN
);
1220 static INT_PTR CALLBACK
datetime_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1226 WCHAR buffer
[MAX_STRING_LEN
];
1228 HWND hListWnd
= GetDlgItem(hWnd
, IDC_DATETIME
);
1231 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &st
, 0, (LPWSTR
)&buffer
,
1233 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1234 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, 0, (LPWSTR
)&buffer
,
1236 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1237 GetTimeFormatW(LOCALE_USER_DEFAULT
, 0, &st
, 0, (LPWSTR
)&buffer
, MAX_STRING_LEN
);
1238 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1240 SendMessageW(hListWnd
, LB_SETSEL
, TRUE
, 0);
1245 switch(LOWORD(wParam
))
1250 HWND hListWnd
= GetDlgItem(hWnd
, IDC_DATETIME
);
1252 index
= SendMessageW(hListWnd
, LB_GETCURSEL
, 0, 0);
1256 WCHAR buffer
[MAX_STRING_LEN
];
1257 SendMessageW(hListWnd
, LB_GETTEXT
, index
, (LPARAM
)&buffer
);
1258 SendMessageW(hEditorWnd
, EM_REPLACESEL
, TRUE
, (LPARAM
)&buffer
);
1264 EndDialog(hWnd
, wParam
);
1271 static INT_PTR CALLBACK
newfile_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1277 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1278 WCHAR buffer
[MAX_STRING_LEN
];
1279 HWND hListWnd
= GetDlgItem(hWnd
, IDC_NEWFILE
);
1281 LoadStringW(hInstance
, STRING_NEWFILE_RICHTEXT
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1282 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1283 LoadStringW(hInstance
, STRING_NEWFILE_TXT
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1284 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1285 LoadStringW(hInstance
, STRING_NEWFILE_TXT_UNICODE
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1286 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1288 SendMessageW(hListWnd
, LB_SETSEL
, TRUE
, 0);
1293 switch(LOWORD(wParam
))
1298 HWND hListWnd
= GetDlgItem(hWnd
, IDC_NEWFILE
);
1299 index
= SendMessageW(hListWnd
, LB_GETCURSEL
, 0, 0);
1302 EndDialog(hWnd
, MAKELONG(fileformat_flags(index
),0));
1307 EndDialog(hWnd
, MAKELONG(ID_NEWFILE_ABORT
,0));
1314 static INT_PTR CALLBACK
paraformat_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1320 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
,
1322 WCHAR buffer
[MAX_STRING_LEN
];
1323 HWND hListWnd
= GetDlgItem(hWnd
, IDC_PARA_ALIGN
);
1324 HWND hLeftWnd
= GetDlgItem(hWnd
, IDC_PARA_LEFT
);
1325 HWND hRightWnd
= GetDlgItem(hWnd
, IDC_PARA_RIGHT
);
1326 HWND hFirstWnd
= GetDlgItem(hWnd
, IDC_PARA_FIRST
);
1330 LoadStringW(hInstance
, STRING_ALIGN_LEFT
, buffer
,
1332 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1333 LoadStringW(hInstance
, STRING_ALIGN_RIGHT
, buffer
,
1335 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1336 LoadStringW(hInstance
, STRING_ALIGN_CENTER
, buffer
,
1338 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1340 pf
.cbSize
= sizeof(pf
);
1341 pf
.dwMask
= PFM_ALIGNMENT
| PFM_OFFSET
| PFM_RIGHTINDENT
|
1343 SendMessageW(hEditorWnd
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
1345 if(pf
.wAlignment
== PFA_RIGHT
)
1347 else if(pf
.wAlignment
== PFA_CENTER
)
1350 SendMessageW(hListWnd
, CB_SETCURSEL
, index
, 0);
1352 number_with_units(buffer
, pf
.dxOffset
);
1353 SetWindowTextW(hLeftWnd
, buffer
);
1354 number_with_units(buffer
, pf
.dxRightIndent
);
1355 SetWindowTextW(hRightWnd
, buffer
);
1356 number_with_units(buffer
, pf
.dxStartIndent
- pf
.dxOffset
);
1357 SetWindowTextW(hFirstWnd
, buffer
);
1362 switch(LOWORD(wParam
))
1366 HWND hLeftWnd
= GetDlgItem(hWnd
, IDC_PARA_LEFT
);
1367 HWND hRightWnd
= GetDlgItem(hWnd
, IDC_PARA_RIGHT
);
1368 HWND hFirstWnd
= GetDlgItem(hWnd
, IDC_PARA_FIRST
);
1369 WCHAR buffer
[MAX_STRING_LEN
];
1374 GetWindowTextW(hLeftWnd
, buffer
, MAX_STRING_LEN
);
1375 if(number_from_string(buffer
, &num
, TRUE
))
1377 pf
.dxOffset
= current_units_to_twips(num
);
1378 GetWindowTextW(hRightWnd
, buffer
, MAX_STRING_LEN
);
1379 if(number_from_string(buffer
, &num
, TRUE
))
1381 pf
.dxRightIndent
= current_units_to_twips(num
);
1382 GetWindowTextW(hFirstWnd
, buffer
, MAX_STRING_LEN
);
1383 if(number_from_string(buffer
, &num
, TRUE
))
1385 pf
.dxStartIndent
= current_units_to_twips(num
);
1389 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_INVALID_NUMBER
),
1390 wszAppTitle
, MB_OK
| MB_ICONASTERISK
);
1394 pf
.dxStartIndent
= pf
.dxStartIndent
+ pf
.dxOffset
;
1395 pf
.cbSize
= sizeof(pf
);
1396 pf
.dwMask
= PFM_OFFSET
| PFM_OFFSETINDENT
| PFM_RIGHTINDENT
;
1397 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
1403 EndDialog(hWnd
, wParam
);
1410 static INT_PTR CALLBACK
tabstops_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1416 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
1418 WCHAR buffer
[MAX_STRING_LEN
];
1421 pf
.cbSize
= sizeof(pf
);
1422 pf
.dwMask
= PFM_TABSTOPS
;
1423 SendMessageW(hEditorWnd
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
1424 SendMessageW(hTabWnd
, CB_LIMITTEXT
, MAX_STRING_LEN
-1, 0);
1426 for(i
= 0; i
< pf
.cTabCount
; i
++)
1428 number_with_units(buffer
, pf
.rgxTabs
[i
]);
1429 SendMessageW(hTabWnd
, CB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1436 switch(LOWORD(wParam
))
1440 HWND hTabWnd
= (HWND
)lParam
;
1441 HWND hAddWnd
= GetDlgItem(hWnd
, ID_TAB_ADD
);
1442 HWND hDelWnd
= GetDlgItem(hWnd
, ID_TAB_DEL
);
1443 HWND hEmptyWnd
= GetDlgItem(hWnd
, ID_TAB_EMPTY
);
1445 if(GetWindowTextLengthW(hTabWnd
))
1446 EnableWindow(hAddWnd
, TRUE
);
1448 EnableWindow(hAddWnd
, FALSE
);
1450 if(SendMessageW(hTabWnd
, CB_GETCOUNT
, 0, 0))
1452 EnableWindow(hEmptyWnd
, TRUE
);
1454 if(SendMessageW(hTabWnd
, CB_GETCURSEL
, 0, 0) == CB_ERR
)
1455 EnableWindow(hDelWnd
, FALSE
);
1457 EnableWindow(hDelWnd
, TRUE
);
1460 EnableWindow(hEmptyWnd
, FALSE
);
1467 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
1468 WCHAR buffer
[MAX_STRING_LEN
];
1470 GetWindowTextW(hTabWnd
, buffer
, MAX_STRING_LEN
);
1471 append_current_units(buffer
);
1473 if(SendMessageW(hTabWnd
, CB_FINDSTRINGEXACT
, -1, (LPARAM
)&buffer
) == CB_ERR
)
1477 if(!number_from_string(buffer
, &number
, TRUE
))
1479 MessageBoxW(hWnd
, MAKEINTRESOURCEW(STRING_INVALID_NUMBER
),
1480 wszAppTitle
, MB_OK
| MB_ICONINFORMATION
);
1483 SendMessageW(hTabWnd
, CB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1484 SetWindowTextW(hTabWnd
, 0);
1493 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
1495 ret
= SendMessageW(hTabWnd
, CB_GETCURSEL
, 0, 0);
1497 SendMessageW(hTabWnd
, CB_DELETESTRING
, ret
, 0);
1503 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
1504 SendMessageW(hTabWnd
, CB_RESETCONTENT
, 0, 0);
1511 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
1513 WCHAR buffer
[MAX_STRING_LEN
];
1517 pf
.cbSize
= sizeof(pf
);
1518 pf
.dwMask
= PFM_TABSTOPS
;
1520 for(i
= 0; SendMessageW(hTabWnd
, CB_GETLBTEXT
, i
,
1521 (LPARAM
)&buffer
) != CB_ERR
&&
1522 i
< MAX_TAB_STOPS
; i
++)
1524 number_from_string(buffer
, &number
, TRUE
);
1525 pf
.rgxTabs
[i
] = current_units_to_twips(number
);
1528 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
1532 EndDialog(hWnd
, wParam
);
1539 static int context_menu(LPARAM lParam
)
1541 int x
= (int)(short)LOWORD(lParam
);
1542 int y
= (int)(short)HIWORD(lParam
);
1543 HMENU hPop
= GetSubMenu(hPopupMenu
, 0);
1547 int from
= 0, to
= 0;
1549 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
1550 SendMessageW(hEditorWnd
, EM_POSFROMCHAR
, (WPARAM
)&pt
, (LPARAM
)to
);
1551 ClientToScreen(hEditorWnd
, (POINT
*)&pt
);
1556 TrackPopupMenu(hPop
, TPM_LEFTALIGN
| TPM_TOPALIGN
| TPM_RIGHTBUTTON
,
1557 x
, y
, 0, hMainWnd
, 0);
1562 static LRESULT
OnCreate( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1564 HWND hToolBarWnd
, hFormatBarWnd
, hReBarWnd
, hFontListWnd
, hSizeListWnd
, hRulerWnd
;
1565 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
1568 int nStdBitmaps
= 0;
1571 static const WCHAR wszRichEditDll
[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1572 static const WCHAR wszRichEditText
[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1574 CreateStatusWindowW(CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
, wszRichEditText
, hWnd
, IDC_STATUSBAR
);
1576 hReBarWnd
= CreateWindowExW(WS_EX_TOOLWINDOW
, REBARCLASSNAMEW
, NULL
,
1577 CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
|WS_CLIPSIBLINGS
|WS_CLIPCHILDREN
|RBS_VARHEIGHT
|CCS_TOP
,
1578 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, hWnd
, (HMENU
)IDC_REBAR
, hInstance
, NULL
);
1580 rbi
.cbSize
= sizeof(rbi
);
1583 if(!SendMessageW(hReBarWnd
, RB_SETBARINFO
, 0, (LPARAM
)&rbi
))
1586 hToolBarWnd
= CreateToolbarEx(hReBarWnd
, CCS_NOPARENTALIGN
|CCS_NOMOVEY
|WS_VISIBLE
|WS_CHILD
|TBSTYLE_TOOLTIPS
|TBSTYLE_BUTTON
,
1588 1, hInstance
, IDB_TOOLBAR
,
1590 24, 24, 16, 16, sizeof(TBBUTTON
));
1592 ab
.hInst
= HINST_COMMCTRL
;
1593 ab
.nID
= IDB_STD_SMALL_COLOR
;
1594 nStdBitmaps
= SendMessageW(hToolBarWnd
, TB_ADDBITMAP
, 0, (LPARAM
)&ab
);
1596 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILENEW
, ID_FILE_NEW
);
1597 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILEOPEN
, ID_FILE_OPEN
);
1598 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILESAVE
, ID_FILE_SAVE
);
1599 AddSeparator(hToolBarWnd
);
1600 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINT
, ID_PRINT_QUICK
);
1601 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINTPRE
, ID_PREVIEW
);
1602 AddSeparator(hToolBarWnd
);
1603 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FIND
, ID_FIND
);
1604 AddSeparator(hToolBarWnd
);
1605 AddButton(hToolBarWnd
, nStdBitmaps
+STD_CUT
, ID_EDIT_CUT
);
1606 AddButton(hToolBarWnd
, nStdBitmaps
+STD_COPY
, ID_EDIT_COPY
);
1607 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PASTE
, ID_EDIT_PASTE
);
1608 AddButton(hToolBarWnd
, nStdBitmaps
+STD_UNDO
, ID_EDIT_UNDO
);
1609 AddButton(hToolBarWnd
, nStdBitmaps
+STD_REDOW
, ID_EDIT_REDO
);
1610 AddSeparator(hToolBarWnd
);
1611 AddButton(hToolBarWnd
, 0, ID_DATETIME
);
1613 SendMessageW(hToolBarWnd
, TB_AUTOSIZE
, 0, 0);
1615 rbb
.cbSize
= sizeof(rbb
);
1616 rbb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_CHILD
| RBBIM_STYLE
| RBBIM_ID
;
1617 rbb
.fStyle
= RBBS_CHILDEDGE
| RBBS_BREAK
| RBBS_NOGRIPPER
;
1619 rbb
.hwndChild
= hToolBarWnd
;
1621 rbb
.cyChild
= rbb
.cyMinChild
= HIWORD(SendMessageW(hToolBarWnd
, TB_GETBUTTONSIZE
, 0, 0));
1622 rbb
.wID
= BANDID_TOOLBAR
;
1624 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
1626 hFontListWnd
= CreateWindowExW(0, WC_COMBOBOXEXW
, NULL
,
1627 WS_BORDER
| WS_VISIBLE
| WS_CHILD
| CBS_DROPDOWN
| CBS_SORT
,
1628 0, 0, 200, 150, hReBarWnd
, (HMENU
)IDC_FONTLIST
, hInstance
, NULL
);
1630 rbb
.hwndChild
= hFontListWnd
;
1632 rbb
.wID
= BANDID_FONTLIST
;
1634 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
1636 hSizeListWnd
= CreateWindowExW(0, WC_COMBOBOXEXW
, NULL
,
1637 WS_BORDER
| WS_VISIBLE
| WS_CHILD
| CBS_DROPDOWN
,
1638 0, 0, 50, 150, hReBarWnd
, (HMENU
)IDC_SIZELIST
, hInstance
, NULL
);
1640 rbb
.hwndChild
= hSizeListWnd
;
1642 rbb
.fStyle
^= RBBS_BREAK
;
1643 rbb
.wID
= BANDID_SIZELIST
;
1645 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
1647 hFormatBarWnd
= CreateToolbarEx(hReBarWnd
,
1648 CCS_NOPARENTALIGN
| CCS_NOMOVEY
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
| TBSTYLE_BUTTON
,
1649 IDC_FORMATBAR
, 7, hInstance
, IDB_FORMATBAR
, NULL
, 0, 16, 16, 16, 16, sizeof(TBBUTTON
));
1651 AddButton(hFormatBarWnd
, 0, ID_FORMAT_BOLD
);
1652 AddButton(hFormatBarWnd
, 1, ID_FORMAT_ITALIC
);
1653 AddButton(hFormatBarWnd
, 2, ID_FORMAT_UNDERLINE
);
1654 AddSeparator(hFormatBarWnd
);
1655 AddButton(hFormatBarWnd
, 3, ID_ALIGN_LEFT
);
1656 AddButton(hFormatBarWnd
, 4, ID_ALIGN_CENTER
);
1657 AddButton(hFormatBarWnd
, 5, ID_ALIGN_RIGHT
);
1658 AddSeparator(hFormatBarWnd
);
1659 AddButton(hFormatBarWnd
, 6, ID_BULLET
);
1661 SendMessageW(hFormatBarWnd
, TB_AUTOSIZE
, 0, 0);
1663 rbb
.hwndChild
= hFormatBarWnd
;
1664 rbb
.wID
= BANDID_FORMATBAR
;
1666 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
1668 hRulerWnd
= CreateWindowExW(0, WC_STATICW
, NULL
, WS_VISIBLE
| WS_CHILD
,
1669 0, 0, 200, 10, hReBarWnd
, (HMENU
)IDC_RULER
, hInstance
, NULL
);
1672 rbb
.hwndChild
= hRulerWnd
;
1673 rbb
.wID
= BANDID_RULER
;
1674 rbb
.fStyle
|= RBBS_BREAK
;
1676 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
1678 hDLL
= LoadLibraryW(wszRichEditDll
);
1681 MessageBoxW(hWnd
, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED
), wszAppTitle
,
1682 MB_OK
| MB_ICONEXCLAMATION
);
1686 hEditorWnd
= CreateWindowExW(WS_EX_CLIENTEDGE
, wszRichEditClass
, NULL
,
1687 WS_CHILD
|WS_VISIBLE
|ES_SELECTIONBAR
|ES_MULTILINE
|ES_AUTOVSCROLL
1688 |ES_WANTRETURN
|WS_VSCROLL
|ES_NOHIDESEL
,
1689 0, 0, 1000, 100, hWnd
, (HMENU
)IDC_EDITOR
, hInstance
, NULL
);
1693 fprintf(stderr
, "Error code %u\n", GetLastError());
1698 SetFocus(hEditorWnd
);
1699 SendMessageW(hEditorWnd
, EM_SETEVENTMASK
, 0, ENM_SELCHANGE
);
1703 populate_font_list(hFontListWnd
);
1704 populate_size_list(hSizeListWnd
);
1706 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
1708 ID_FINDMSGSTRING
= RegisterWindowMessageW(FINDMSGSTRINGW
);
1710 registry_read_filelist(hWnd
);
1711 registry_read_formatopts_all(barState
, wordWrap
);
1712 registry_read_options();
1713 DragAcceptFiles(hWnd
, TRUE
);
1718 static LRESULT
OnUser( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1720 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
1721 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
1722 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
1723 HWND hwndFormatBar
= GetDlgItem(hwndReBar
, IDC_FORMATBAR
);
1729 ZeroMemory(&fmt
, sizeof(fmt
));
1730 fmt
.cbSize
= sizeof(fmt
);
1732 ZeroMemory(&pf
, sizeof(pf
));
1733 pf
.cbSize
= sizeof(pf
);
1735 gt
.flags
= GTL_NUMCHARS
;
1738 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_FIND
,
1739 SendMessageW(hwndEditor
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0) ? 1 : 0);
1741 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, TRUE
, (LPARAM
)&fmt
);
1743 SendMessageW(hwndEditor
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
1744 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_UNDO
,
1745 SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0));
1746 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_REDO
,
1747 SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0));
1748 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_CUT
, from
== to
? 0 : 1);
1749 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_COPY
, from
== to
? 0 : 1);
1751 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_BOLD
, (fmt
.dwMask
& CFM_BOLD
) &&
1752 (fmt
.dwEffects
& CFE_BOLD
));
1753 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_BOLD
, !(fmt
.dwMask
& CFM_BOLD
));
1754 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_ITALIC
, (fmt
.dwMask
& CFM_ITALIC
) &&
1755 (fmt
.dwEffects
& CFE_ITALIC
));
1756 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_ITALIC
, !(fmt
.dwMask
& CFM_ITALIC
));
1757 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_UNDERLINE
, (fmt
.dwMask
& CFM_UNDERLINE
) &&
1758 (fmt
.dwEffects
& CFE_UNDERLINE
));
1759 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_UNDERLINE
, !(fmt
.dwMask
& CFM_UNDERLINE
));
1761 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
1762 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_LEFT
, (pf
.wAlignment
== PFA_LEFT
));
1763 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_CENTER
, (pf
.wAlignment
== PFA_CENTER
));
1764 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_RIGHT
, (pf
.wAlignment
== PFA_RIGHT
));
1766 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_BULLET
, (pf
.wNumbering
& PFN_BULLET
));
1770 static LRESULT
OnNotify( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1772 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
1773 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
1774 NMHDR
*pHdr
= (NMHDR
*)lParam
;
1775 HWND hwndFontList
= GetDlgItem(hwndReBar
, IDC_FONTLIST
);
1776 HWND hwndSizeList
= GetDlgItem(hwndReBar
, IDC_SIZELIST
);
1777 WCHAR sizeBuffer
[MAX_PATH
];
1779 if (pHdr
->hwndFrom
== hwndFontList
|| pHdr
->hwndFrom
== hwndSizeList
)
1781 if (pHdr
->code
== CBEN_ENDEDITW
)
1783 CHARFORMAT2W format
;
1784 NMCBEENDEDIT
*endEdit
= (NMCBEENDEDIT
*)lParam
;
1786 ZeroMemory(&format
, sizeof(format
));
1787 format
.cbSize
= sizeof(format
);
1788 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&format
);
1790 if(pHdr
->hwndFrom
== hwndFontList
)
1792 if(lstrcmpW(format
.szFaceName
, (LPWSTR
)endEdit
->szText
))
1793 set_font((LPCWSTR
) endEdit
->szText
);
1794 } else if (pHdr
->hwndFrom
== hwndSizeList
)
1796 wsprintfW(sizeBuffer
, stringFormat
, format
.yHeight
/ 20);
1797 if(lstrcmpW(sizeBuffer
, (LPWSTR
)endEdit
->szText
))
1800 if(number_from_string((LPWSTR
)endEdit
->szText
, &size
, FALSE
)
1806 SetWindowTextW(hwndSizeList
, sizeBuffer
);
1807 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_INVALID_NUMBER
), wszAppTitle
, MB_OK
| MB_ICONINFORMATION
);
1815 if (pHdr
->hwndFrom
!= hwndEditor
)
1818 if (pHdr
->code
== EN_SELCHANGE
)
1820 SELCHANGE
*pSC
= (SELCHANGE
*)lParam
;
1825 sprintf( buf
,"selection = %d..%d, line count=%ld",
1826 pSC
->chrg
.cpMin
, pSC
->chrg
.cpMax
,
1827 SendMessage(hwndEditor
, EM_GETLINECOUNT
, 0, 0));
1828 SetWindowText(GetDlgItem(hWnd
, IDC_STATUSBAR
), buf
);
1829 SendMessage(hWnd
, WM_USER
, 0, 0);
1835 static LRESULT
OnCommand( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1837 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
1838 static FINDREPLACEW findreplace
;
1840 if ((HWND
)lParam
== hwndEditor
)
1843 switch(LOWORD(wParam
))
1847 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
1852 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
1853 int ret
= DialogBox(hInstance
, MAKEINTRESOURCE(IDD_NEWFILE
), hWnd
,
1856 if(ret
!= ID_NEWFILE_ABORT
)
1858 if(prompt_save_changes())
1863 wszFileName
[0] = '\0';
1867 st
.flags
= ST_DEFAULT
;
1869 SendMessageW(hEditorWnd
, EM_SETTEXTEX
, (WPARAM
)&st
, 0);
1871 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
1872 set_fileformat(ret
);
1886 DoSaveFile(wszFileName
, fileFormat
);
1891 case ID_FILE_SAVEAS
:
1895 case ID_FILE_RECENT1
:
1896 case ID_FILE_RECENT2
:
1897 case ID_FILE_RECENT3
:
1898 case ID_FILE_RECENT4
:
1900 HMENU hMenu
= GetMenu(hWnd
);
1903 mi
.cbSize
= sizeof(MENUITEMINFOW
);
1904 mi
.fMask
= MIIM_DATA
;
1905 if(GetMenuItemInfoW(hMenu
, LOWORD(wParam
), FALSE
, &mi
))
1906 DoOpenFile((LPWSTR
)mi
.dwItemData
);
1911 dialog_find(&findreplace
, FALSE
);
1915 handle_findmsg(&findreplace
);
1919 dialog_find(&findreplace
, TRUE
);
1922 case ID_FONTSETTINGS
:
1923 dialog_choose_font();
1927 dialog_print(hWnd
, wszFileName
);
1928 target_device(hMainWnd
, wordWrap
[reg_formatindex(fileFormat
)]);
1931 case ID_PRINT_QUICK
:
1932 print_quick(wszFileName
);
1933 target_device(hMainWnd
, wordWrap
[reg_formatindex(fileFormat
)]);
1938 int index
= reg_formatindex(fileFormat
);
1939 DWORD tmp
= barState
[index
];
1940 barState
[index
] = 0;
1942 barState
[index
] = tmp
;
1943 ShowWindow(hEditorWnd
, FALSE
);
1945 init_preview(hWnd
, wszFileName
);
1947 SetMenu(hWnd
, NULL
);
1948 InvalidateRect(0, 0, TRUE
);
1953 dialog_printsetup(hWnd
);
1954 target_device(hMainWnd
, wordWrap
[reg_formatindex(fileFormat
)]);
1957 case ID_FORMAT_BOLD
:
1958 case ID_FORMAT_ITALIC
:
1959 case ID_FORMAT_UNDERLINE
:
1962 int effects
= CFE_BOLD
;
1964 ZeroMemory(&fmt
, sizeof(fmt
));
1965 fmt
.cbSize
= sizeof(fmt
);
1966 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
1968 fmt
.dwMask
= CFM_BOLD
;
1970 if (LOWORD(wParam
) == ID_FORMAT_ITALIC
)
1972 effects
= CFE_ITALIC
;
1973 fmt
.dwMask
= CFM_ITALIC
;
1974 } else if (LOWORD(wParam
) == ID_FORMAT_UNDERLINE
)
1976 effects
= CFE_UNDERLINE
;
1977 fmt
.dwMask
= CFM_UNDERLINE
;
1980 fmt
.dwEffects
^= effects
;
1982 SendMessageW(hwndEditor
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
1987 PostMessageW(hwndEditor
, WM_CUT
, 0, 0);
1991 PostMessageW(hwndEditor
, WM_COPY
, 0, 0);
1995 PostMessageW(hwndEditor
, WM_PASTE
, 0, 0);
1999 PostMessageW(hwndEditor
, WM_CLEAR
, 0, 0);
2002 case ID_EDIT_SELECTALL
:
2004 CHARRANGE range
= {0, -1};
2005 SendMessageW(hwndEditor
, EM_EXSETSEL
, 0, (LPARAM
)&range
);
2006 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2010 case ID_EDIT_GETTEXT
:
2012 int nLen
= GetWindowTextLengthW(hwndEditor
);
2013 LPWSTR data
= HeapAlloc( GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
) );
2016 GetWindowTextW(hwndEditor
, data
, nLen
+1);
2017 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
2019 HeapFree( GetProcessHeap(), 0, data
);
2020 data
= HeapAlloc(GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
));
2022 tr
.chrg
.cpMax
= nLen
;
2023 tr
.lpstrText
= data
;
2024 SendMessage (hwndEditor
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
2025 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
2026 HeapFree( GetProcessHeap(), 0, data
);
2028 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2032 case ID_EDIT_CHARFORMAT
:
2033 case ID_EDIT_DEFCHARFORMAT
:
2037 ZeroMemory(&cf
, sizeof(cf
));
2038 cf
.cbSize
= sizeof(cf
);
2040 i
= SendMessageW(hwndEditor
, EM_GETCHARFORMAT
,
2041 LOWORD(wParam
) == ID_EDIT_CHARFORMAT
, (LPARAM
)&cf
);
2045 case ID_EDIT_PARAFORMAT
:
2048 ZeroMemory(&pf
, sizeof(pf
));
2049 pf
.cbSize
= sizeof(pf
);
2050 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2054 case ID_EDIT_SELECTIONINFO
:
2056 CHARRANGE range
= {0, -1};
2060 SendMessage(hwndEditor
, EM_EXGETSEL
, 0, (LPARAM
)&range
);
2061 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) * (range
.cpMax
-range
.cpMin
+1));
2062 SendMessage(hwndEditor
, EM_GETSELTEXT
, 0, (LPARAM
)data
);
2063 sprintf(buf
, "Start = %d, End = %d", range
.cpMin
, range
.cpMax
);
2064 MessageBoxA(hWnd
, buf
, "Editor", MB_OK
);
2065 MessageBoxW(hWnd
, data
, xszAppTitle
, MB_OK
);
2066 HeapFree( GetProcessHeap(), 0, data
);
2067 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2071 case ID_EDIT_READONLY
:
2073 long nStyle
= GetWindowLong(hwndEditor
, GWL_STYLE
);
2074 if (nStyle
& ES_READONLY
)
2075 SendMessageW(hwndEditor
, EM_SETREADONLY
, 0, 0);
2077 SendMessageW(hwndEditor
, EM_SETREADONLY
, 1, 0);
2081 case ID_EDIT_MODIFIED
:
2082 if (SendMessageW(hwndEditor
, EM_GETMODIFY
, 0, 0))
2083 SendMessageW(hwndEditor
, EM_SETMODIFY
, 0, 0);
2085 SendMessageW(hwndEditor
, EM_SETMODIFY
, 1, 0);
2089 SendMessageW(hwndEditor
, EM_UNDO
, 0, 0);
2093 SendMessageW(hwndEditor
, EM_REDO
, 0, 0);
2100 pf
.cbSize
= sizeof(pf
);
2101 pf
.dwMask
= PFM_NUMBERING
;
2102 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2104 pf
.dwMask
|= PFM_OFFSET
;
2106 if(pf
.wNumbering
== PFN_BULLET
)
2112 pf
.wNumbering
= PFN_BULLET
;
2116 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2121 case ID_ALIGN_CENTER
:
2122 case ID_ALIGN_RIGHT
:
2126 pf
.cbSize
= sizeof(pf
);
2127 pf
.dwMask
= PFM_ALIGNMENT
;
2128 switch(LOWORD(wParam
)) {
2129 case ID_ALIGN_LEFT
: pf
.wAlignment
= PFA_LEFT
; break;
2130 case ID_ALIGN_CENTER
: pf
.wAlignment
= PFA_CENTER
; break;
2131 case ID_ALIGN_RIGHT
: pf
.wAlignment
= PFA_RIGHT
; break;
2133 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2138 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 1, 0);
2142 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 0, RGB(255,255,192));
2145 case ID_TOGGLE_TOOLBAR
:
2146 set_toolbar_state(BANDID_TOOLBAR
, !is_bar_visible(BANDID_TOOLBAR
));
2150 case ID_TOGGLE_FORMATBAR
:
2151 set_toolbar_state(BANDID_FONTLIST
, !is_bar_visible(BANDID_FORMATBAR
));
2152 set_toolbar_state(BANDID_SIZELIST
, !is_bar_visible(BANDID_FORMATBAR
));
2153 set_toolbar_state(BANDID_FORMATBAR
, !is_bar_visible(BANDID_FORMATBAR
));
2157 case ID_TOGGLE_STATUSBAR
:
2158 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR
));
2162 case ID_TOGGLE_RULER
:
2163 set_toolbar_state(BANDID_RULER
, !is_bar_visible(BANDID_RULER
));
2169 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2170 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_DATETIME
), hWnd
, datetime_proc
);
2176 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2177 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_PARAFORMAT
), hWnd
,
2184 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2185 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_TABSTOPS
), hWnd
, tabstops_proc
);
2193 case ID_VIEWPROPERTIES
:
2194 dialog_viewproperties();
2198 SendMessageW(hwndEditor
, WM_COMMAND
, wParam
, lParam
);
2204 static LRESULT
OnInitPopupMenu( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2206 HMENU hMenu
= (HMENU
)wParam
;
2207 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2208 HWND hwndStatus
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
2210 int nAlignment
= -1;
2216 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&selFrom
, (LPARAM
)&selTo
);
2217 EnableMenuItem(hMenu
, ID_EDIT_COPY
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
2218 EnableMenuItem(hMenu
, ID_EDIT_CUT
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
2220 pf
.cbSize
= sizeof(PARAFORMAT
);
2221 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2222 CheckMenuItem(hMenu
, ID_EDIT_READONLY
,
2223 MF_BYCOMMAND
|(GetWindowLong(hwndEditor
, GWL_STYLE
)&ES_READONLY
? MF_CHECKED
: MF_UNCHECKED
));
2224 CheckMenuItem(hMenu
, ID_EDIT_MODIFIED
,
2225 MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0) ? MF_CHECKED
: MF_UNCHECKED
));
2226 if (pf
.dwMask
& PFM_ALIGNMENT
)
2227 nAlignment
= pf
.wAlignment
;
2228 CheckMenuItem(hMenu
, ID_ALIGN_LEFT
, MF_BYCOMMAND
|(nAlignment
== PFA_LEFT
) ?
2229 MF_CHECKED
: MF_UNCHECKED
);
2230 CheckMenuItem(hMenu
, ID_ALIGN_CENTER
, MF_BYCOMMAND
|(nAlignment
== PFA_CENTER
) ?
2231 MF_CHECKED
: MF_UNCHECKED
);
2232 CheckMenuItem(hMenu
, ID_ALIGN_RIGHT
, MF_BYCOMMAND
|(nAlignment
== PFA_RIGHT
) ?
2233 MF_CHECKED
: MF_UNCHECKED
);
2234 CheckMenuItem(hMenu
, ID_BULLET
, MF_BYCOMMAND
| ((pf
.wNumbering
== PFN_BULLET
) ?
2235 MF_CHECKED
: MF_UNCHECKED
));
2236 EnableMenuItem(hMenu
, ID_EDIT_UNDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0)) ?
2237 MF_ENABLED
: MF_GRAYED
);
2238 EnableMenuItem(hMenu
, ID_EDIT_REDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0)) ?
2239 MF_ENABLED
: MF_GRAYED
);
2241 CheckMenuItem(hMenu
, ID_TOGGLE_TOOLBAR
, MF_BYCOMMAND
|(is_bar_visible(BANDID_TOOLBAR
)) ?
2242 MF_CHECKED
: MF_UNCHECKED
);
2244 CheckMenuItem(hMenu
, ID_TOGGLE_FORMATBAR
, MF_BYCOMMAND
|(is_bar_visible(BANDID_FORMATBAR
)) ?
2245 MF_CHECKED
: MF_UNCHECKED
);
2247 CheckMenuItem(hMenu
, ID_TOGGLE_STATUSBAR
, MF_BYCOMMAND
|IsWindowVisible(hwndStatus
) ?
2248 MF_CHECKED
: MF_UNCHECKED
);
2250 CheckMenuItem(hMenu
, ID_TOGGLE_RULER
, MF_BYCOMMAND
|(is_bar_visible(BANDID_RULER
)) ? MF_CHECKED
: MF_UNCHECKED
);
2252 gt
.flags
= GTL_NUMCHARS
;
2254 textLength
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
2255 EnableMenuItem(hMenu
, ID_FIND
, MF_BYCOMMAND
|(textLength
? MF_ENABLED
: MF_GRAYED
));
2257 mi
.cbSize
= sizeof(mi
);
2258 mi
.fMask
= MIIM_DATA
;
2260 GetMenuItemInfoW(hMenu
, ID_FIND_NEXT
, FALSE
, &mi
);
2262 EnableMenuItem(hMenu
, ID_FIND_NEXT
, MF_BYCOMMAND
|((textLength
&& mi
.dwItemData
) ?
2263 MF_ENABLED
: MF_GRAYED
));
2265 EnableMenuItem(hMenu
, ID_REPLACE
, MF_BYCOMMAND
|(textLength
? MF_ENABLED
: MF_GRAYED
));
2270 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2272 int nStatusSize
= 0;
2274 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2275 HWND hwndStatusBar
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
2276 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
2277 HWND hRulerWnd
= GetDlgItem(hWnd
, IDC_RULER
);
2278 int rebarHeight
= 0;
2282 SendMessageW(hwndStatusBar
, WM_SIZE
, 0, 0);
2283 if (IsWindowVisible(hwndStatusBar
))
2285 GetClientRect(hwndStatusBar
, &rc
);
2286 nStatusSize
= rc
.bottom
- rc
.top
;
2294 rebarHeight
= SendMessageW(hwndReBar
, RB_GETBARHEIGHT
, 0, 0);
2296 MoveWindow(hwndReBar
, 0, 0, LOWORD(lParam
), rebarHeight
, TRUE
);
2300 GetClientRect(hWnd
, &rc
);
2301 MoveWindow(hwndEditor
, 0, rebarHeight
, rc
.right
, rc
.bottom
-nStatusSize
-rebarHeight
, TRUE
);
2304 redraw_ruler(hRulerWnd
);
2306 return DefWindowProcW(hWnd
, WM_SIZE
, wParam
, lParam
);
2309 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2311 if(msg
== ID_FINDMSGSTRING
)
2312 return handle_findmsg((LPFINDREPLACEW
)lParam
);
2317 return OnCreate( hWnd
, wParam
, lParam
);
2320 return OnUser( hWnd
, wParam
, lParam
);
2323 return OnNotify( hWnd
, wParam
, lParam
);
2326 if(preview_isactive())
2328 return preview_command( hWnd
, wParam
, lParam
);
2331 return OnCommand( hWnd
, wParam
, lParam
);
2338 if(preview_isactive())
2341 } else if(prompt_save_changes())
2343 registry_set_options(hMainWnd
);
2344 registry_set_formatopts_all(barState
);
2351 SetFocus(GetDlgItem(hWnd
, IDC_EDITOR
));
2354 case WM_INITMENUPOPUP
:
2355 return OnInitPopupMenu( hWnd
, wParam
, lParam
);
2358 return OnSize( hWnd
, wParam
, lParam
);
2360 case WM_CONTEXTMENU
:
2361 if((HWND
)wParam
== hEditorWnd
)
2362 return context_menu(lParam
);
2364 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2368 WCHAR file
[MAX_PATH
];
2369 DragQueryFileW((HDROP
)wParam
, 0, file
, MAX_PATH
);
2370 DragFinish((HDROP
)wParam
);
2372 if(prompt_save_changes())
2377 if(preview_isactive())
2378 return print_preview(hWnd
);
2380 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2383 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2389 int CALLBACK
WinMain(HINSTANCE hInstance
, HINSTANCE hOldInstance
, LPSTR szCmdParagraph
, int res
)
2391 INITCOMMONCONTROLSEX classes
= {8, ICC_BAR_CLASSES
|ICC_COOL_CLASSES
|ICC_USEREX_CLASSES
};
2396 UINT_PTR hPrevRulerProc
;
2399 static const WCHAR wszAccelTable
[] = {'M','A','I','N','A','C','C','E','L',
2400 'T','A','B','L','E','\0'};
2402 InitCommonControlsEx(&classes
);
2404 hAccel
= LoadAcceleratorsW(hInstance
, wszAccelTable
);
2406 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
2407 wc
.lpfnWndProc
= WndProc
;
2410 wc
.hInstance
= hInstance
;
2411 wc
.hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
2412 wc
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
2413 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2414 wc
.lpszMenuName
= MAKEINTRESOURCEW(IDM_MAINMENU
);
2415 wc
.lpszClassName
= wszMainWndClass
;
2416 RegisterClassW(&wc
);
2418 registry_read_winrect(&rc
);
2419 hMainWnd
= CreateWindowExW(0, wszMainWndClass
, wszAppTitle
, WS_CLIPCHILDREN
|WS_OVERLAPPEDWINDOW
,
2420 rc
.left
, rc
.top
, rc
.right
-rc
.left
, rc
.bottom
-rc
.top
, NULL
, NULL
, hInstance
, NULL
);
2421 ShowWindow(hMainWnd
, SW_SHOWDEFAULT
);
2425 set_fileformat(SF_RTF
);
2426 SendMessageW(hEditorWnd
, EM_EMPTYUNDOBUFFER
, 0, 0);
2427 hPopupMenu
= LoadMenuW(hInstance
, MAKEINTRESOURCEW(IDM_POPUP
));
2428 get_default_printer_opts();
2429 target_device(hMainWnd
, wordWrap
[reg_formatindex(fileFormat
)]);
2431 hRulerWnd
= GetDlgItem(GetDlgItem(hMainWnd
, IDC_REBAR
), IDC_RULER
);
2432 SendMessageW(GetDlgItem(hMainWnd
, IDC_EDITOR
), EM_POSFROMCHAR
, (WPARAM
)&EditPoint
, 0);
2433 hPrevRulerProc
= SetWindowLongPtrW(hRulerWnd
, GWLP_WNDPROC
, (UINT_PTR
)ruler_proc
);
2434 SendMessageW(hRulerWnd
, WM_USER
, (WPARAM
)&EditPoint
, hPrevRulerProc
);
2436 HandleCommandLine(GetCommandLineW());
2438 while(GetMessageW(&msg
,0,0,0))
2440 if (IsDialogMessage(hFindWnd
, &msg
))
2443 if (TranslateAcceleratorW(hMainWnd
, hAccel
, &msg
))
2445 TranslateMessage(&msg
);
2446 DispatchMessageW(&msg
);
2447 if (!PeekMessageW(&msg
, 0, 0, 0, PM_NOREMOVE
))
2448 SendMessageW(hMainWnd
, WM_USER
, 0, 0);