d3d10: Introduce a helper function to allocate arrays (AFL).
[wine.git] / programs / wordpad / wordpad.c
blobbb34f2e8e2f7c909b77ae65af673c37149c83707
1 /*
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
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <assert.h>
31 #include <windows.h>
32 #include <richedit.h>
33 #include <commctrl.h>
34 #include <commdlg.h>
35 #include <shellapi.h>
36 #include <math.h>
37 #include <errno.h>
39 #include "wine/unicode.h"
40 #include "wordpad.h"
42 #ifdef NONAMELESSUNION
43 # define U(x) (x).u
44 # define U2(x) (x).u2
45 # define U3(x) (x).u3
46 #else
47 # define U(x) (x)
48 # define U2(x) (x)
49 # define U3(x) (x)
50 #endif
52 /* use LoadString */
53 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
55 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
57 static const WCHAR stringFormat[] = {'%','2','d','\0'};
59 const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
60 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
62 static HWND hMainWnd;
63 static HWND hEditorWnd;
64 static HWND hFindWnd;
65 static HMENU hColorPopupMenu;
67 static UINT ID_FINDMSGSTRING;
69 static DWORD wordWrap[2];
70 static DWORD barState[2];
71 static WPARAM fileFormat = SF_RTF;
73 static WCHAR wszFileName[MAX_PATH];
74 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
75 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
76 static WCHAR wszSaveChanges[MAX_STRING_LEN];
77 static WCHAR units_cmW[MAX_STRING_LEN];
78 static WCHAR units_inW[MAX_STRING_LEN];
79 static WCHAR units_inchW[MAX_STRING_LEN];
80 static WCHAR units_ptW[MAX_STRING_LEN];
82 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
84 typedef enum
86 UNIT_CM,
87 UNIT_INCH,
88 UNIT_PT
89 } UNIT;
91 typedef struct
93 int endPos;
94 BOOL wrapped;
95 WCHAR findBuffer[128];
96 } FINDREPLACE_custom;
98 /* Load string resources */
99 static void DoLoadStrings(void)
101 LPWSTR p = wszFilter;
102 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
103 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
104 static const WCHAR files_all[] = {'*','.','*','\0'};
106 HINSTANCE hInstance = GetModuleHandleW(0);
108 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
109 p += lstrlenW(p) + 1;
110 lstrcpyW(p, files_rtf);
111 p += lstrlenW(p) + 1;
112 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
113 p += lstrlenW(p) + 1;
114 lstrcpyW(p, files_txt);
115 p += lstrlenW(p) + 1;
116 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
117 p += lstrlenW(p) + 1;
118 lstrcpyW(p, files_txt);
119 p += lstrlenW(p) + 1;
120 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
121 p += lstrlenW(p) + 1;
122 lstrcpyW(p, files_all);
123 p += lstrlenW(p) + 1;
124 *p = '\0';
126 p = wszDefaultFileName;
127 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
129 p = wszSaveChanges;
130 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
132 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
133 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
134 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
135 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
138 /* Show a message box with resource strings */
139 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
141 MSGBOXPARAMSW params;
143 params.cbSize = sizeof(params);
144 params.hwndOwner = hWnd;
145 params.hInstance = GetModuleHandleW(0);
146 params.lpszText = lpText;
147 params.lpszCaption = lpCaption;
148 params.dwStyle = uType;
149 params.lpszIcon = NULL;
150 params.dwContextHelpId = 0;
151 params.lpfnMsgBoxCallback = NULL;
152 params.dwLanguageId = 0;
153 return MessageBoxIndirectW(&params);
157 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
159 TBBUTTON button;
161 ZeroMemory(&button, sizeof(button));
162 button.iBitmap = nImage;
163 button.idCommand = nCommand;
164 button.fsState = TBSTATE_ENABLED;
165 button.fsStyle = BTNS_BUTTON;
166 button.dwData = 0;
167 button.iString = -1;
168 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
171 static void AddSeparator(HWND hwndToolBar)
173 TBBUTTON button;
175 ZeroMemory(&button, sizeof(button));
176 button.iBitmap = -1;
177 button.idCommand = 0;
178 button.fsState = 0;
179 button.fsStyle = BTNS_SEP;
180 button.dwData = 0;
181 button.iString = -1;
182 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
185 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
187 HANDLE hFile = (HANDLE)cookie;
188 DWORD read;
190 if(!ReadFile(hFile, buffer, cb, &read, 0))
191 return 1;
193 *pcb = read;
195 return 0;
198 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
200 DWORD written;
201 int ret;
202 HANDLE hFile = (HANDLE)cookie;
204 ret = WriteFile(hFile, buffer, cb, &written, 0);
206 if(!ret || (cb != written))
207 return 1;
209 *pcb = cb;
211 return 0;
214 LPWSTR file_basename(LPWSTR path)
216 LPWSTR pos = path + lstrlenW(path);
218 while(pos > path)
220 if(*pos == '\\' || *pos == '/')
222 pos++;
223 break;
225 pos--;
227 return pos;
230 static void set_caption(LPCWSTR wszNewFileName)
232 static const WCHAR wszSeparator[] = {' ','-',' '};
233 WCHAR *wszCaption;
234 SIZE_T length = 0;
236 if(!wszNewFileName)
237 wszNewFileName = wszDefaultFileName;
238 else
239 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
241 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
242 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
244 if(!wszCaption)
245 return;
247 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
248 length += lstrlenW(wszNewFileName);
249 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
250 length += sizeof(wszSeparator) / sizeof(WCHAR);
251 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
253 SetWindowTextW(hMainWnd, wszCaption);
255 HeapFree(GetProcessHeap(), 0, wszCaption);
258 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
260 if(punit != NULL)
261 *punit = UNIT_CM;
262 if(!endptr)
263 return FALSE;
264 if(!*endptr)
265 return TRUE;
267 while(*endptr == ' ')
268 endptr++;
270 if(punit == NULL)
271 return *endptr == '\0';
273 if(!lstrcmpW(endptr, units_cmW))
275 *punit = UNIT_CM;
276 endptr += lstrlenW(units_cmW);
278 else if (!lstrcmpW(endptr, units_inW))
280 *punit = UNIT_INCH;
281 endptr += lstrlenW(units_inW);
283 else if (!lstrcmpW(endptr, units_inchW))
285 *punit = UNIT_INCH;
286 endptr += lstrlenW(units_inchW);
288 else if (!lstrcmpW(endptr, units_ptW))
290 *punit = UNIT_PT;
291 endptr += lstrlenW(units_ptW);
294 return *endptr == '\0';
297 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
299 double ret;
300 WCHAR *endptr;
302 *num = 0;
303 errno = 0;
304 ret = wcstod(string, &endptr);
306 if (punit != NULL)
307 *punit = UNIT_CM;
308 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
310 return FALSE;
311 } else
313 *num = (float)ret;
314 return TRUE;
318 static void set_size(float size)
320 CHARFORMAT2W fmt;
322 ZeroMemory(&fmt, sizeof(fmt));
323 fmt.cbSize = sizeof(fmt);
324 fmt.dwMask = CFM_SIZE;
325 fmt.yHeight = (int)(size * 20.0);
326 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
329 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
331 WCHAR sizeBuffer[MAX_STRING_LEN];
332 CHARFORMAT2W format;
334 ZeroMemory(&format, sizeof(format));
335 format.cbSize = sizeof(format);
336 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
338 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
339 if(lstrcmpW(sizeBuffer, wszNewFontSize))
341 float size = 0;
342 if(number_from_string(wszNewFontSize, &size, NULL)
343 && size > 0)
345 set_size(size);
346 } else
348 SetWindowTextW(hwndSizeList, sizeBuffer);
349 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
350 wszAppTitle, MB_OK | MB_ICONINFORMATION);
355 static void add_size(HWND hSizeListWnd, unsigned size)
357 WCHAR buffer[3];
358 COMBOBOXEXITEMW cbItem;
359 cbItem.mask = CBEIF_TEXT;
360 cbItem.iItem = -1;
362 wsprintfW(buffer, stringFormat, size);
363 cbItem.pszText = buffer;
364 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
367 static void populate_size_list(HWND hSizeListWnd)
369 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
370 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
371 COMBOBOXEXITEMW cbFontItem;
372 CHARFORMAT2W fmt;
373 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
374 HDC hdc = GetDC(hMainWnd);
375 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
376 WCHAR buffer[3];
377 size_t i;
378 DWORD fontStyle;
380 ZeroMemory(&fmt, sizeof(fmt));
381 fmt.cbSize = sizeof(fmt);
382 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
384 cbFontItem.mask = CBEIF_LPARAM;
385 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
386 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
388 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
390 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
392 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
394 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
395 GetDeviceCaps(hdc, LOGPIXELSY)));
396 } else
398 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
399 add_size(hSizeListWnd, choices[i]);
402 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
403 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
406 static void update_size_list(void)
408 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
409 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
410 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
411 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
412 CHARFORMAT2W fmt;
414 ZeroMemory(&fmt, sizeof(fmt));
415 fmt.cbSize = sizeof(fmt);
417 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
419 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
420 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
422 if(lstrcmpW(fontSize, sizeBuffer))
423 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
426 static void update_font_list(void)
428 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
429 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
430 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
431 WCHAR fontName[MAX_STRING_LEN];
432 CHARFORMAT2W fmt;
434 ZeroMemory(&fmt, sizeof(fmt));
435 fmt.cbSize = sizeof(fmt);
437 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
438 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
440 if(lstrcmpW(fontName, fmt.szFaceName))
442 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
443 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
444 } else
446 update_size_list();
450 static void clear_formatting(void)
452 PARAFORMAT2 pf;
454 pf.cbSize = sizeof(pf);
455 pf.dwMask = PFM_ALIGNMENT;
456 pf.wAlignment = PFA_LEFT;
457 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
460 static int fileformat_number(WPARAM format)
462 int number = 0;
464 if(format == SF_TEXT)
466 number = 1;
467 } else if (format == (SF_TEXT | SF_UNICODE))
469 number = 2;
471 return number;
474 static WPARAM fileformat_flags(int format)
476 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
478 return flags[format];
481 static void set_font(LPCWSTR wszFaceName)
483 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
484 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
485 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
486 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
487 CHARFORMAT2W fmt;
489 ZeroMemory(&fmt, sizeof(fmt));
491 fmt.cbSize = sizeof(fmt);
492 fmt.dwMask = CFM_FACE;
494 lstrcpyW(fmt.szFaceName, wszFaceName);
496 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
498 populate_size_list(hSizeListWnd);
500 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
503 static void set_default_font(void)
505 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
506 'R','o','m','a','n',0};
507 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
508 CHARFORMAT2W fmt;
509 LPCWSTR font;
511 ZeroMemory(&fmt, sizeof(fmt));
513 fmt.cbSize = sizeof(fmt);
514 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
515 fmt.dwEffects = 0;
517 if(fileFormat & SF_RTF)
518 font = richTextFont;
519 else
520 font = plainTextFont;
522 lstrcpyW(fmt.szFaceName, font);
524 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
527 static void on_fontlist_modified(LPWSTR wszNewFaceName)
529 CHARFORMAT2W format;
530 ZeroMemory(&format, sizeof(format));
531 format.cbSize = sizeof(format);
532 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
534 if(lstrcmpW(format.szFaceName, wszNewFaceName))
535 set_font(wszNewFaceName);
538 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
540 COMBOBOXEXITEMW cbItem;
541 WCHAR buffer[MAX_PATH];
542 int fontHeight = 0;
544 cbItem.mask = CBEIF_TEXT;
545 cbItem.pszText = buffer;
546 cbItem.cchTextMax = MAX_STRING_LEN;
547 cbItem.iItem = 0;
549 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
551 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
552 cbItem.iItem++;
553 else
554 break;
556 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
557 lstrcpyW( cbItem.pszText, fontName );
559 cbItem.mask |= CBEIF_LPARAM;
560 if(fontType & RASTER_FONTTYPE)
561 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
563 cbItem.lParam = MAKELONG(fontType,fontHeight);
564 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
565 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
568 static void dialog_choose_font(void)
570 CHOOSEFONTW cf;
571 LOGFONTW lf;
572 CHARFORMAT2W fmt;
573 HDC hDC = GetDC(hMainWnd);
575 ZeroMemory(&cf, sizeof(cf));
576 cf.lStructSize = sizeof(cf);
577 cf.hwndOwner = hMainWnd;
578 cf.lpLogFont = &lf;
579 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_NOVERTFONTS;
581 ZeroMemory(&fmt, sizeof(fmt));
582 fmt.cbSize = sizeof(fmt);
584 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
585 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
586 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
587 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
588 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
589 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
590 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
591 cf.rgbColors = fmt.crTextColor;
593 if(ChooseFontW(&cf))
595 ZeroMemory(&fmt, sizeof(fmt));
596 fmt.cbSize = sizeof(fmt);
597 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
598 fmt.yHeight = cf.iPointSize * 2;
600 if(cf.nFontType & BOLD_FONTTYPE)
601 fmt.dwEffects |= CFE_BOLD;
602 if(cf.nFontType & ITALIC_FONTTYPE)
603 fmt.dwEffects |= CFE_ITALIC;
604 if(cf.lpLogFont->lfUnderline)
605 fmt.dwEffects |= CFE_UNDERLINE;
606 if(cf.lpLogFont->lfStrikeOut)
607 fmt.dwEffects |= CFE_STRIKEOUT;
609 fmt.crTextColor = cf.rgbColors;
611 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
612 set_font(cf.lpLogFont->lfFaceName);
617 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
618 DWORD FontType, LPARAM lParam)
620 HWND hListWnd = (HWND) lParam;
622 if (lpelfe->lfFaceName[0] == '@') return 1; /* ignore vertical fonts */
624 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
627 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
630 return 1;
633 static void populate_font_list(HWND hListWnd)
635 HDC hdc = GetDC(hMainWnd);
636 LOGFONTW fontinfo;
637 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
638 CHARFORMAT2W fmt;
640 fontinfo.lfCharSet = DEFAULT_CHARSET;
641 *fontinfo.lfFaceName = '\0';
642 fontinfo.lfPitchAndFamily = 0;
644 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
645 (LPARAM)hListWnd, 0);
647 ZeroMemory(&fmt, sizeof(fmt));
648 fmt.cbSize = sizeof(fmt);
649 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
650 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
653 static void update_window(void)
655 RECT rect;
657 GetClientRect(hMainWnd, &rect);
659 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
662 static BOOL is_bar_visible(int bandId)
664 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
667 static void store_bar_state(int bandId, BOOL show)
669 int formatIndex = reg_formatindex(fileFormat);
671 if(show)
672 barState[formatIndex] |= (1 << bandId);
673 else
674 barState[formatIndex] &= ~(1 << bandId);
677 static void set_toolbar_state(int bandId, BOOL show)
679 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
681 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
683 if(bandId == BANDID_TOOLBAR)
685 REBARBANDINFOW rbbinfo;
686 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
688 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
689 rbbinfo.fMask = RBBIM_STYLE;
691 SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
693 if(!show)
694 rbbinfo.fStyle &= ~RBBS_BREAK;
695 else
696 rbbinfo.fStyle |= RBBS_BREAK;
698 SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
701 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
702 store_bar_state(bandId, show);
705 static void set_statusbar_state(BOOL show)
707 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
709 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
710 store_bar_state(BANDID_STATUSBAR, show);
713 static void set_bar_states(void)
715 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
716 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
717 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
718 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
719 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
720 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
722 update_window();
725 static void preview_exit(HWND hMainWnd)
727 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
728 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
730 set_bar_states();
731 ShowWindow(hEditorWnd, TRUE);
733 close_preview(hMainWnd);
735 SetMenu(hMainWnd, hMenu);
736 registry_read_filelist(hMainWnd);
738 update_window();
741 static void set_fileformat(WPARAM format)
743 fileFormat = format;
745 set_bar_states();
746 set_default_font();
747 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
750 static void ShowOpenError(DWORD Code)
752 LPWSTR Message;
754 switch(Code)
756 case ERROR_ACCESS_DENIED:
757 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
758 break;
760 default:
761 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
763 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
766 static void DoOpenFile(LPCWSTR szOpenFileName)
768 HANDLE hFile;
769 EDITSTREAM es;
770 char fileStart[5];
771 DWORD readOut;
772 WPARAM format = SF_TEXT;
774 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
775 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
776 if (hFile == INVALID_HANDLE_VALUE)
778 ShowOpenError(GetLastError());
779 return;
782 ReadFile(hFile, fileStart, 5, &readOut, NULL);
783 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
785 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
787 format = SF_TEXT | SF_UNICODE;
788 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
789 } else if(readOut >= 5)
791 static const char header[] = "{\\rtf";
792 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
794 if(!memcmp(header, fileStart, 5))
795 format = SF_RTF;
796 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
798 CloseHandle(hFile);
799 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
800 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
801 return;
805 es.dwCookie = (DWORD_PTR)hFile;
806 es.pfnCallback = stream_in;
808 clear_formatting();
809 set_fileformat(format);
810 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
812 CloseHandle(hFile);
814 SetFocus(hEditorWnd);
816 set_caption(szOpenFileName);
818 lstrcpyW(wszFileName, szOpenFileName);
819 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
820 registry_set_filelist(szOpenFileName, hMainWnd);
821 update_font_list();
824 static void ShowWriteError(DWORD Code)
826 LPWSTR Message;
828 switch(Code)
830 case ERROR_ACCESS_DENIED:
831 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
832 break;
834 default:
835 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
837 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
840 static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
842 HANDLE hFile;
843 EDITSTREAM stream;
844 LRESULT ret;
846 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
847 FILE_ATTRIBUTE_NORMAL, NULL);
849 if(hFile == INVALID_HANDLE_VALUE)
851 ShowWriteError(GetLastError());
852 return FALSE;
855 if(format == (SF_TEXT | SF_UNICODE))
857 static const BYTE unicode[] = {0xff,0xfe};
858 DWORD writeOut;
859 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
861 if(writeOut != sizeof(unicode))
863 CloseHandle(hFile);
864 return FALSE;
868 stream.dwCookie = (DWORD_PTR)hFile;
869 stream.pfnCallback = stream_out;
871 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
873 CloseHandle(hFile);
875 SetFocus(hEditorWnd);
877 if(!ret)
879 GETTEXTLENGTHEX gt;
880 gt.flags = GTL_DEFAULT;
881 gt.codepage = 1200;
883 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
884 return FALSE;
887 lstrcpyW(wszFileName, wszSaveFileName);
888 set_caption(wszFileName);
889 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
890 set_fileformat(format);
892 return TRUE;
895 static BOOL DialogSaveFile(void)
897 OPENFILENAMEW sfn;
899 WCHAR wszFile[MAX_PATH] = {'\0'};
900 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
902 ZeroMemory(&sfn, sizeof(sfn));
904 sfn.lStructSize = sizeof(sfn);
905 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
906 sfn.hwndOwner = hMainWnd;
907 sfn.lpstrFilter = wszFilter;
908 sfn.lpstrFile = wszFile;
909 sfn.nMaxFile = MAX_PATH;
910 sfn.lpstrDefExt = wszDefExt;
911 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
913 while(GetSaveFileNameW(&sfn))
915 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
917 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
918 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
919 continue;
921 return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
923 return FALSE;
926 static BOOL prompt_save_changes(void)
928 if(!wszFileName[0])
930 GETTEXTLENGTHEX gt;
931 gt.flags = GTL_NUMCHARS;
932 gt.codepage = 1200;
933 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
934 return TRUE;
937 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
939 return TRUE;
940 } else
942 LPWSTR displayFileName;
943 WCHAR *text;
944 int ret;
946 if(!wszFileName[0])
947 displayFileName = wszDefaultFileName;
948 else
949 displayFileName = file_basename(wszFileName);
951 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
952 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
954 if(!text)
955 return FALSE;
957 wsprintfW(text, wszSaveChanges, displayFileName);
959 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
961 HeapFree(GetProcessHeap(), 0, text);
963 switch(ret)
965 case IDNO:
966 return TRUE;
968 case IDYES:
969 if(wszFileName[0])
970 return DoSaveFile(wszFileName, fileFormat);
971 return DialogSaveFile();
973 default:
974 return FALSE;
979 static void DialogOpenFile(void)
981 OPENFILENAMEW ofn;
983 WCHAR wszFile[MAX_PATH] = {'\0'};
984 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
986 ZeroMemory(&ofn, sizeof(ofn));
988 ofn.lStructSize = sizeof(ofn);
989 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
990 ofn.hwndOwner = hMainWnd;
991 ofn.lpstrFilter = wszFilter;
992 ofn.lpstrFile = wszFile;
993 ofn.nMaxFile = MAX_PATH;
994 ofn.lpstrDefExt = wszDefExt;
995 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
997 if(GetOpenFileNameW(&ofn))
999 if(prompt_save_changes())
1000 DoOpenFile(ofn.lpstrFile);
1004 static void dialog_about(void)
1006 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1007 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1010 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1012 switch(message)
1014 case WM_INITDIALOG:
1016 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1017 int wrap = -1;
1018 char id[4];
1019 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1021 sprintf(id, "%d\n", (int)ps->lParam);
1022 SetWindowTextA(hIdWnd, id);
1023 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1024 wrap = IDC_PAGEFMT_WN;
1025 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1026 wrap = IDC_PAGEFMT_WW;
1027 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1028 wrap = IDC_PAGEFMT_WM;
1030 if(wrap != -1)
1031 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1032 IDC_PAGEFMT_WM, wrap);
1034 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1035 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1036 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1037 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1038 if(barState[ps->lParam] & (1 << BANDID_RULER))
1039 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1040 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1041 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1043 break;
1045 case WM_COMMAND:
1046 switch(LOWORD(wParam))
1048 case IDC_PAGEFMT_WN:
1049 case IDC_PAGEFMT_WW:
1050 case IDC_PAGEFMT_WM:
1051 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1052 LOWORD(wParam));
1053 break;
1055 case IDC_PAGEFMT_TB:
1056 case IDC_PAGEFMT_FB:
1057 case IDC_PAGEFMT_RU:
1058 case IDC_PAGEFMT_SB:
1059 CheckDlgButton(hWnd, LOWORD(wParam),
1060 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1061 break;
1063 break;
1064 case WM_NOTIFY:
1066 LPNMHDR header = (LPNMHDR)lParam;
1067 if(header->code == PSN_APPLY)
1069 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1070 char sid[4];
1071 int id;
1073 GetWindowTextA(hIdWnd, sid, 4);
1074 id = atoi(sid);
1075 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1076 wordWrap[id] = ID_WORDWRAP_NONE;
1077 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1078 wordWrap[id] = ID_WORDWRAP_WINDOW;
1079 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1080 wordWrap[id] = ID_WORDWRAP_MARGIN;
1082 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1083 barState[id] |= (1 << BANDID_TOOLBAR);
1084 else
1085 barState[id] &= ~(1 << BANDID_TOOLBAR);
1087 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1088 barState[id] |= (1 << BANDID_FORMATBAR);
1089 else
1090 barState[id] &= ~(1 << BANDID_FORMATBAR);
1092 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1093 barState[id] |= (1 << BANDID_RULER);
1094 else
1095 barState[id] &= ~(1 << BANDID_RULER);
1097 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1098 barState[id] |= (1 << BANDID_STATUSBAR);
1099 else
1100 barState[id] &= ~(1 << BANDID_STATUSBAR);
1103 break;
1105 return FALSE;
1108 static void dialog_viewproperties(void)
1110 PROPSHEETPAGEW psp[2];
1111 PROPSHEETHEADERW psh;
1112 size_t i;
1113 HINSTANCE hInstance = GetModuleHandleW(0);
1114 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1116 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1117 psp[0].dwFlags = PSP_USETITLE;
1118 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1119 psp[0].pfnDlgProc = formatopts_proc;
1120 psp[0].hInstance = hInstance;
1121 psp[0].lParam = reg_formatindex(SF_TEXT);
1122 psp[0].pfnCallback = NULL;
1123 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1124 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1126 psp[i].dwSize = psp[0].dwSize;
1127 psp[i].dwFlags = psp[0].dwFlags;
1128 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1129 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1130 psp[i].hInstance = psp[0].hInstance;
1131 psp[i].lParam = reg_formatindex(SF_RTF);
1132 psp[i].pfnCallback = psp[0].pfnCallback;
1133 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1136 psh.dwSize = sizeof(psh);
1137 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1138 psh.hwndParent = hMainWnd;
1139 psh.hInstance = hInstance;
1140 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1141 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1142 U3(psh).ppsp = ppsp;
1143 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1145 if(fileFormat & SF_RTF)
1146 U2(psh).nStartPage = 1;
1147 else
1148 U2(psh).nStartPage = 0;
1149 PropertySheetW(&psh);
1150 set_bar_states();
1151 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1154 static void HandleCommandLine(LPWSTR cmdline)
1156 WCHAR delimiter;
1157 BOOL opt_print = FALSE;
1159 /* skip white space */
1160 while (*cmdline == ' ') cmdline++;
1162 /* skip executable name */
1163 delimiter = (*cmdline == '"' ? '"' : ' ');
1165 if (*cmdline == delimiter) cmdline++;
1166 while (*cmdline && *cmdline != delimiter) cmdline++;
1167 if (*cmdline == delimiter) cmdline++;
1169 while (*cmdline)
1171 while (isspace(*cmdline)) cmdline++;
1173 if (*cmdline == '-' || *cmdline == '/')
1175 if (!cmdline[2] || isspace(cmdline[2]))
1177 switch (cmdline[1])
1179 case 'P':
1180 case 'p':
1181 opt_print = TRUE;
1182 cmdline += 2;
1183 continue;
1186 /* a filename starting by / */
1188 break;
1191 if (*cmdline)
1193 /* file name is passed on the command line */
1194 if (cmdline[0] == '"')
1196 cmdline++;
1197 cmdline[lstrlenW(cmdline) - 1] = 0;
1199 DoOpenFile(cmdline);
1200 InvalidateRect(hMainWnd, NULL, FALSE);
1203 if (opt_print)
1204 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1207 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1209 if(pFr->Flags & FR_DIALOGTERM)
1211 hFindWnd = 0;
1212 pFr->Flags = FR_FINDNEXT;
1213 return 0;
1216 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1218 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1219 DWORD flags;
1220 FINDTEXTEXW ft;
1221 CHARRANGE sel;
1222 LRESULT ret = -1;
1223 HMENU hMenu = GetMenu(hMainWnd);
1224 MENUITEMINFOW mi;
1226 mi.cbSize = sizeof(mi);
1227 mi.fMask = MIIM_DATA;
1228 mi.dwItemData = 1;
1229 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1231 /* Make sure find field is saved. */
1232 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1234 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1235 sizeof(custom_data->findBuffer) / sizeof(WCHAR));
1236 pFr->lpstrFindWhat = custom_data->findBuffer;
1239 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1240 if(custom_data->endPos == -1) {
1241 custom_data->endPos = sel.cpMin;
1242 custom_data->wrapped = FALSE;
1245 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1246 ft.lpstrText = pFr->lpstrFindWhat;
1248 /* Only replace the existing selection if it is an exact match. */
1249 if (sel.cpMin != sel.cpMax &&
1250 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1252 ft.chrg = sel;
1253 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1254 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1255 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1256 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1260 /* Search from the start of the selection, but exclude the first character
1261 * from search if there is a selection. */
1262 ft.chrg.cpMin = sel.cpMin;
1263 if (sel.cpMin != sel.cpMax)
1264 ft.chrg.cpMin++;
1266 /* Search to the end, then wrap around and search from the start. */
1267 if (!custom_data->wrapped) {
1268 ft.chrg.cpMax = -1;
1269 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1270 if (ret == -1) {
1271 custom_data->wrapped = TRUE;
1272 ft.chrg.cpMin = 0;
1276 if (ret == -1) {
1277 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1278 if (ft.chrg.cpMax > ft.chrg.cpMin)
1279 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1282 if (ret == -1) {
1283 custom_data->endPos = -1;
1284 EnableWindow(hMainWnd, FALSE);
1285 MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1286 wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
1287 EnableWindow(hMainWnd, TRUE);
1288 } else {
1289 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1290 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1292 if (pFr->Flags & FR_REPLACEALL)
1293 return handle_findmsg(pFr);
1297 return 0;
1300 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1302 static WCHAR selBuffer[128];
1303 static WCHAR replaceBuffer[128];
1304 static FINDREPLACE_custom custom_data;
1305 static const WCHAR endl = '\r';
1306 FINDTEXTW ft;
1308 /* Allow only one search/replace dialog to open */
1309 if(hFindWnd != NULL)
1311 SetActiveWindow(hFindWnd);
1312 return;
1315 ZeroMemory(fr, sizeof(FINDREPLACEW));
1316 fr->lStructSize = sizeof(FINDREPLACEW);
1317 fr->hwndOwner = hMainWnd;
1318 fr->Flags = FR_HIDEUPDOWN;
1319 /* Find field is filled with the selected text if it is non-empty
1320 * and stays within the same paragraph, otherwise the previous
1321 * find field is used. */
1322 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1323 (LPARAM)&ft.chrg.cpMax);
1324 ft.lpstrText = &endl;
1325 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1326 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1328 /* Use a temporary buffer for the selected text so that the saved
1329 * find field is only overwritten when a find/replace is clicked. */
1330 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1331 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1332 fr->lpstrFindWhat = selBuffer;
1333 } else {
1334 fr->lpstrFindWhat = custom_data.findBuffer;
1336 fr->lpstrReplaceWith = replaceBuffer;
1337 custom_data.endPos = -1;
1338 custom_data.wrapped = FALSE;
1339 fr->lCustData = (LPARAM)&custom_data;
1340 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1341 fr->wReplaceWithLen = sizeof(replaceBuffer);
1343 if(replace)
1344 hFindWnd = ReplaceTextW(fr);
1345 else
1346 hFindWnd = FindTextW(fr);
1349 static int units_to_twips(UNIT unit, float number)
1351 int twips = 0;
1353 switch(unit)
1355 case UNIT_CM:
1356 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1357 break;
1359 case UNIT_INCH:
1360 twips = (int)(number * (float)TWIPS_PER_INCH);
1361 break;
1363 case UNIT_PT:
1364 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1365 break;
1368 return twips;
1371 static void append_current_units(LPWSTR buffer)
1373 static const WCHAR space[] = {' ', 0};
1374 lstrcatW(buffer, space);
1375 lstrcatW(buffer, units_cmW);
1378 static void number_with_units(LPWSTR buffer, int number)
1380 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1381 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1383 sprintfW(buffer, fmt, converted, units_cmW);
1386 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1388 COMBOBOXEXITEMW cbItem;
1389 COMBOBOXINFO cbInfo;
1390 HWND hCombo, hList;
1391 int idx, result;
1393 hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1394 if (!hCombo)
1395 return FALSE;
1396 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1397 result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1398 if (!result)
1399 return FALSE;
1400 hList = cbInfo.hwndList;
1401 idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
1402 if (idx < 0)
1403 return FALSE;
1405 ZeroMemory(&cbItem, sizeof(cbItem));
1406 cbItem.mask = CBEIF_TEXT;
1407 cbItem.iItem = idx;
1408 cbItem.pszText = wszBuffer;
1409 cbItem.cchTextMax = bufferLength-1;
1410 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1412 return result != 0;
1415 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1417 switch(message)
1419 case WM_INITDIALOG:
1421 WCHAR buffer[MAX_STRING_LEN];
1422 SYSTEMTIME st;
1423 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1424 GetLocalTime(&st);
1426 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1427 MAX_STRING_LEN);
1428 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1429 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1430 MAX_STRING_LEN);
1431 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1432 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1433 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1435 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1437 break;
1439 case WM_COMMAND:
1440 switch(LOWORD(wParam))
1442 case IDC_DATETIME:
1443 if (HIWORD(wParam) != LBN_DBLCLK)
1444 break;
1445 /* Fall through */
1447 case IDOK:
1449 LRESULT index;
1450 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1452 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1454 if(index != LB_ERR)
1456 WCHAR buffer[MAX_STRING_LEN];
1457 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1458 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1461 /* Fall through */
1463 case IDCANCEL:
1464 EndDialog(hWnd, wParam);
1465 return TRUE;
1468 return FALSE;
1471 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1473 switch(message)
1475 case WM_INITDIALOG:
1477 HINSTANCE hInstance = GetModuleHandleW(0);
1478 WCHAR buffer[MAX_STRING_LEN];
1479 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1481 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1482 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1483 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1484 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1485 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1486 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1488 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1490 break;
1492 case WM_COMMAND:
1493 switch(LOWORD(wParam))
1495 case IDOK:
1497 LRESULT index;
1498 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1499 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1501 if(index != LB_ERR)
1502 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1504 return TRUE;
1506 case IDCANCEL:
1507 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1508 return TRUE;
1511 return FALSE;
1514 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1516 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1518 switch(message)
1520 case WM_INITDIALOG:
1522 HINSTANCE hInstance = GetModuleHandleW(0);
1523 WCHAR buffer[MAX_STRING_LEN];
1524 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1525 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1526 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1527 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1528 PARAFORMAT2 pf;
1529 int index = 0;
1531 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1532 MAX_STRING_LEN);
1533 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1534 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1535 MAX_STRING_LEN);
1536 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1537 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1538 MAX_STRING_LEN);
1539 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1541 pf.cbSize = sizeof(pf);
1542 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1543 PFM_STARTINDENT;
1544 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1546 if(pf.wAlignment == PFA_RIGHT)
1547 index ++;
1548 else if(pf.wAlignment == PFA_CENTER)
1549 index += 2;
1551 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1553 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1554 SetWindowTextW(hLeftWnd, buffer);
1555 number_with_units(buffer, pf.dxRightIndent);
1556 SetWindowTextW(hRightWnd, buffer);
1557 number_with_units(buffer, -pf.dxOffset);
1558 SetWindowTextW(hFirstWnd, buffer);
1560 break;
1562 case WM_COMMAND:
1563 switch(LOWORD(wParam))
1565 case IDOK:
1567 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1568 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1569 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1570 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1571 WCHAR buffer[MAX_STRING_LEN];
1572 int index;
1573 float num;
1574 int ret = 0;
1575 PARAFORMAT pf;
1576 UNIT unit;
1578 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1579 pf.wAlignment = ALIGNMENT_VALUES[index];
1581 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1582 if(number_from_string(buffer, &num, &unit))
1583 ret++;
1584 pf.dxOffset = units_to_twips(unit, num);
1585 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1586 if(number_from_string(buffer, &num, &unit))
1587 ret++;
1588 pf.dxRightIndent = units_to_twips(unit, num);
1589 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1590 if(number_from_string(buffer, &num, &unit))
1591 ret++;
1592 pf.dxStartIndent = units_to_twips(unit, num);
1594 if(ret != 3)
1596 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1597 wszAppTitle, MB_OK | MB_ICONASTERISK);
1598 return FALSE;
1599 } else
1601 if (pf.dxOffset + pf.dxStartIndent < 0
1602 && pf.dxStartIndent < 0)
1604 /* The first line is before the left edge, so
1605 * make sure it is at the left edge. */
1606 pf.dxOffset = -pf.dxStartIndent;
1607 } else if (pf.dxOffset < 0) {
1608 /* The second and following lines are before
1609 * the left edge, so set it to be at the left
1610 * edge, and adjust the first line since it
1611 * is relative to it. */
1612 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1613 pf.dxOffset = 0;
1615 /* Internally the dxStartIndent is the absolute
1616 * offset for the first line and dxOffset is
1617 * to it value as opposed how it is displayed with
1618 * the first line being the relative value.
1619 * These two lines make the adjustments. */
1620 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1621 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1623 pf.cbSize = sizeof(pf);
1624 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1625 PFM_STARTINDENT;
1626 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1629 /* Fall through */
1631 case IDCANCEL:
1632 EndDialog(hWnd, wParam);
1633 return TRUE;
1636 return FALSE;
1639 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1641 switch(message)
1643 case WM_INITDIALOG:
1645 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1646 PARAFORMAT pf;
1647 WCHAR buffer[MAX_STRING_LEN];
1648 int i;
1650 pf.cbSize = sizeof(pf);
1651 pf.dwMask = PFM_TABSTOPS;
1652 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1653 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1655 for(i = 0; i < pf.cTabCount; i++)
1657 number_with_units(buffer, pf.rgxTabs[i]);
1658 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1660 SetFocus(hTabWnd);
1662 break;
1664 case WM_COMMAND:
1665 switch(LOWORD(wParam))
1667 case IDC_TABSTOPS:
1669 HWND hTabWnd = (HWND)lParam;
1670 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1671 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1672 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1674 if(GetWindowTextLengthW(hTabWnd))
1675 EnableWindow(hAddWnd, TRUE);
1676 else
1677 EnableWindow(hAddWnd, FALSE);
1679 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1681 EnableWindow(hEmptyWnd, TRUE);
1683 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1684 EnableWindow(hDelWnd, FALSE);
1685 else
1686 EnableWindow(hDelWnd, TRUE);
1687 } else
1689 EnableWindow(hEmptyWnd, FALSE);
1692 break;
1694 case ID_TAB_ADD:
1696 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1697 WCHAR buffer[MAX_STRING_LEN];
1698 UNIT unit;
1700 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1701 append_current_units(buffer);
1703 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1705 float number = 0;
1706 int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
1708 if(!number_from_string(buffer, &number, &unit))
1710 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1711 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1712 } else if (item_count >= MAX_TAB_STOPS) {
1713 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1714 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1715 } else {
1716 int i;
1717 float next_number = -1;
1718 int next_number_in_twips = -1;
1719 int insert_number = units_to_twips(unit, number);
1721 /* linear search for position to insert the string */
1722 for(i = 0; i < item_count; i++)
1724 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1725 number_from_string(buffer, &next_number, &unit);
1726 next_number_in_twips = units_to_twips(unit, next_number);
1727 if (insert_number <= next_number_in_twips)
1728 break;
1730 if (insert_number != next_number_in_twips)
1732 number_with_units(buffer, insert_number);
1733 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1734 SetWindowTextW(hTabWnd, 0);
1738 SetFocus(hTabWnd);
1740 break;
1742 case ID_TAB_DEL:
1744 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1745 LRESULT ret;
1746 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1747 if(ret != CB_ERR)
1748 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1750 break;
1752 case ID_TAB_EMPTY:
1754 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1755 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1756 SetFocus(hTabWnd);
1758 break;
1760 case IDOK:
1762 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1763 int i;
1764 WCHAR buffer[MAX_STRING_LEN];
1765 PARAFORMAT pf;
1766 float number;
1767 UNIT unit;
1769 pf.cbSize = sizeof(pf);
1770 pf.dwMask = PFM_TABSTOPS;
1772 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1773 (LPARAM)&buffer) != CB_ERR &&
1774 i < MAX_TAB_STOPS; i++)
1776 number_from_string(buffer, &number, &unit);
1777 pf.rgxTabs[i] = units_to_twips(unit, number);
1779 pf.cTabCount = i;
1780 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1782 /* Fall through */
1783 case IDCANCEL:
1784 EndDialog(hWnd, wParam);
1785 return TRUE;
1788 return FALSE;
1791 static LRESULT OnCreate( HWND hWnd )
1793 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1794 HINSTANCE hInstance = GetModuleHandleW(0);
1795 HANDLE hDLL;
1796 TBADDBITMAP ab;
1797 int nStdBitmaps = 0;
1798 REBARINFO rbi;
1799 REBARBANDINFOW rbb;
1800 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1801 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1803 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1805 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1806 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1807 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1809 rbi.cbSize = sizeof(rbi);
1810 rbi.fMask = 0;
1811 rbi.himl = NULL;
1812 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1813 return -1;
1815 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|BTNS_BUTTON,
1816 IDC_TOOLBAR,
1817 1, hInstance, IDB_TOOLBAR,
1818 NULL, 0,
1819 24, 24, 16, 16, sizeof(TBBUTTON));
1821 ab.hInst = HINST_COMMCTRL;
1822 ab.nID = IDB_STD_SMALL_COLOR;
1823 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1825 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1826 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1827 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1828 AddSeparator(hToolBarWnd);
1829 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1830 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1831 AddSeparator(hToolBarWnd);
1832 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1833 AddSeparator(hToolBarWnd);
1834 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1835 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1836 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1837 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1838 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1839 AddSeparator(hToolBarWnd);
1840 AddButton(hToolBarWnd, 0, ID_DATETIME);
1842 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1844 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1845 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1846 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1847 rbb.cx = 0;
1848 rbb.hwndChild = hToolBarWnd;
1849 rbb.cxMinChild = 0;
1850 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1851 rbb.wID = BANDID_TOOLBAR;
1853 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1855 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1856 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1857 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1859 rbb.hwndChild = hFontListWnd;
1860 rbb.cx = 200;
1861 rbb.wID = BANDID_FONTLIST;
1863 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1865 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1866 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1867 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1869 rbb.hwndChild = hSizeListWnd;
1870 rbb.cx = 50;
1871 rbb.fStyle ^= RBBS_BREAK;
1872 rbb.wID = BANDID_SIZELIST;
1874 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1876 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1877 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | BTNS_BUTTON,
1878 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1880 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1881 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1882 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1883 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1884 AddSeparator(hFormatBarWnd);
1885 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1886 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1887 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1888 AddSeparator(hFormatBarWnd);
1889 AddButton(hFormatBarWnd, 7, ID_BULLET);
1891 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1893 rbb.hwndChild = hFormatBarWnd;
1894 rbb.wID = BANDID_FORMATBAR;
1896 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1898 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1899 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1902 rbb.hwndChild = hRulerWnd;
1903 rbb.wID = BANDID_RULER;
1904 rbb.fStyle |= RBBS_BREAK;
1906 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1908 hDLL = LoadLibraryW(wszRichEditDll);
1909 if(!hDLL)
1911 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1912 MB_OK | MB_ICONEXCLAMATION);
1913 PostQuitMessage(1);
1916 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1917 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1918 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1919 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1921 if (!hEditorWnd)
1923 fprintf(stderr, "Error code %u\n", GetLastError());
1924 return -1;
1926 assert(hEditorWnd);
1928 setup_richedit_olecallback(hEditorWnd);
1929 SetFocus(hEditorWnd);
1930 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1932 set_default_font();
1934 populate_font_list(hFontListWnd);
1935 populate_size_list(hSizeListWnd);
1936 DoLoadStrings();
1937 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1939 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1941 registry_read_filelist(hWnd);
1942 registry_read_formatopts_all(barState, wordWrap);
1943 registry_read_options();
1944 DragAcceptFiles(hWnd, TRUE);
1946 return 0;
1949 static LRESULT OnUser( HWND hWnd )
1951 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1952 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1953 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1954 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1955 int from, to;
1956 CHARFORMAT2W fmt;
1957 PARAFORMAT2 pf;
1958 GETTEXTLENGTHEX gt;
1960 ZeroMemory(&fmt, sizeof(fmt));
1961 fmt.cbSize = sizeof(fmt);
1963 ZeroMemory(&pf, sizeof(pf));
1964 pf.cbSize = sizeof(pf);
1966 gt.flags = GTL_NUMCHARS;
1967 gt.codepage = 1200;
1969 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1970 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1972 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1974 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1975 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1976 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1977 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1978 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1979 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1980 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
1982 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
1983 (fmt.dwEffects & CFE_BOLD));
1984 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
1985 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
1986 (fmt.dwEffects & CFE_ITALIC));
1987 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
1988 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
1989 (fmt.dwEffects & CFE_UNDERLINE));
1990 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
1992 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1993 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
1994 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
1995 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
1997 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
1998 return 0;
2001 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2003 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2004 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2005 NMHDR *pHdr = (NMHDR *)lParam;
2006 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2007 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2009 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2011 if (pHdr->code == CBEN_ENDEDITW)
2013 NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
2014 if(pHdr->hwndFrom == hwndFontList)
2016 on_fontlist_modified(endEdit->szText);
2017 } else if (pHdr->hwndFrom == hwndSizeList)
2019 on_sizelist_modified(hwndSizeList,endEdit->szText);
2022 return 0;
2025 if (pHdr->hwndFrom != hwndEditor)
2026 return 0;
2028 if (pHdr->code == EN_SELCHANGE)
2030 SELCHANGE *pSC = (SELCHANGE *)lParam;
2031 char buf[128];
2033 update_font_list();
2035 sprintf( buf,"selection = %d..%d, line count=%ld",
2036 pSC->chrg.cpMin, pSC->chrg.cpMax,
2037 SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
2038 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2039 SendMessageW(hWnd, WM_USER, 0, 0);
2040 return 1;
2042 return 0;
2045 /* Copied from dlls/comdlg32/fontdlg.c */
2046 static const COLORREF textcolors[]=
2048 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2049 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2050 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2051 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2054 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2056 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2057 static FINDREPLACEW findreplace;
2059 if ((HWND)lParam == hwndEditor)
2060 return 0;
2062 switch(LOWORD(wParam))
2065 case ID_FILE_EXIT:
2066 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2067 break;
2069 case ID_FILE_NEW:
2071 HINSTANCE hInstance = GetModuleHandleW(0);
2072 int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
2074 if(ret != ID_NEWFILE_ABORT)
2076 if(prompt_save_changes())
2078 SETTEXTEX st;
2080 set_caption(NULL);
2081 wszFileName[0] = '\0';
2083 clear_formatting();
2085 st.flags = ST_DEFAULT;
2086 st.codepage = 1200;
2087 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2089 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2090 set_fileformat(ret);
2091 update_font_list();
2095 break;
2097 case ID_FILE_OPEN:
2098 DialogOpenFile();
2099 break;
2101 case ID_FILE_SAVE:
2102 if(wszFileName[0])
2104 DoSaveFile(wszFileName, fileFormat);
2105 break;
2107 /* Fall through */
2109 case ID_FILE_SAVEAS:
2110 DialogSaveFile();
2111 break;
2113 case ID_FILE_RECENT1:
2114 case ID_FILE_RECENT2:
2115 case ID_FILE_RECENT3:
2116 case ID_FILE_RECENT4:
2118 HMENU hMenu = GetMenu(hWnd);
2119 MENUITEMINFOW mi;
2121 mi.cbSize = sizeof(MENUITEMINFOW);
2122 mi.fMask = MIIM_DATA;
2123 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2124 DoOpenFile((LPWSTR)mi.dwItemData);
2126 break;
2128 case ID_FIND:
2129 dialog_find(&findreplace, FALSE);
2130 break;
2132 case ID_FIND_NEXT:
2133 handle_findmsg(&findreplace);
2134 break;
2136 case ID_REPLACE:
2137 dialog_find(&findreplace, TRUE);
2138 break;
2140 case ID_FONTSETTINGS:
2141 dialog_choose_font();
2142 break;
2144 case ID_PRINT:
2145 dialog_print(hWnd, wszFileName);
2146 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2147 break;
2149 case ID_PRINT_QUICK:
2150 print_quick(hMainWnd, wszFileName);
2151 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2152 break;
2154 case ID_PREVIEW:
2156 int index = reg_formatindex(fileFormat);
2157 DWORD tmp = barState[index];
2158 barState[index] = 1 << BANDID_STATUSBAR;
2159 set_bar_states();
2160 barState[index] = tmp;
2161 ShowWindow(hEditorWnd, FALSE);
2163 init_preview(hWnd, wszFileName);
2165 SetMenu(hWnd, NULL);
2166 InvalidateRect(0, 0, TRUE);
2168 break;
2170 case ID_PRINTSETUP:
2171 dialog_printsetup(hWnd);
2172 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2173 break;
2175 case ID_FORMAT_BOLD:
2176 case ID_FORMAT_ITALIC:
2177 case ID_FORMAT_UNDERLINE:
2179 CHARFORMAT2W fmt;
2180 int effects = CFE_BOLD;
2182 ZeroMemory(&fmt, sizeof(fmt));
2183 fmt.cbSize = sizeof(fmt);
2184 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2186 fmt.dwMask = CFM_BOLD;
2188 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2190 effects = CFE_ITALIC;
2191 fmt.dwMask = CFM_ITALIC;
2192 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2194 effects = CFE_UNDERLINE;
2195 fmt.dwMask = CFM_UNDERLINE;
2198 fmt.dwEffects ^= effects;
2200 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2201 break;
2204 case ID_FORMAT_COLOR:
2206 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2207 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2208 HMENU hPop;
2209 RECT itemrc;
2210 POINT pt;
2211 int mid;
2212 int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2214 SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2215 pt.x = itemrc.left;
2216 pt.y = itemrc.bottom;
2217 ClientToScreen(hFormatBarWnd, &pt);
2218 hPop = GetSubMenu(hColorPopupMenu, 0);
2219 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2220 TPM_RETURNCMD | TPM_NONOTIFY,
2221 pt.x, pt.y, 0, hWnd, 0);
2222 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2224 CHARFORMAT2W fmt;
2226 ZeroMemory(&fmt, sizeof(fmt));
2227 fmt.cbSize = sizeof(fmt);
2228 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2230 fmt.dwMask = CFM_COLOR;
2232 if (mid < ID_COLOR_AUTOMATIC) {
2233 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2234 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2235 } else {
2236 fmt.dwEffects |= CFE_AUTOCOLOR;
2239 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2241 break;
2244 case ID_EDIT_CUT:
2245 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2246 break;
2248 case ID_EDIT_COPY:
2249 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2250 break;
2252 case ID_EDIT_PASTE:
2253 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2254 break;
2256 case ID_EDIT_CLEAR:
2257 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2258 break;
2260 case ID_EDIT_SELECTALL:
2262 CHARRANGE range = {0, -1};
2263 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2264 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2265 return 0;
2268 case ID_EDIT_GETTEXT:
2270 int nLen = GetWindowTextLengthW(hwndEditor);
2271 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2272 TEXTRANGEW tr;
2274 GetWindowTextW(hwndEditor, data, nLen+1);
2275 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2277 HeapFree( GetProcessHeap(), 0, data);
2278 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2279 tr.chrg.cpMin = 0;
2280 tr.chrg.cpMax = nLen;
2281 tr.lpstrText = data;
2282 SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2283 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2284 HeapFree( GetProcessHeap(), 0, data );
2286 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2287 return 0;
2290 case ID_EDIT_CHARFORMAT:
2291 case ID_EDIT_DEFCHARFORMAT:
2293 CHARFORMAT2W cf;
2295 ZeroMemory(&cf, sizeof(cf));
2296 cf.cbSize = sizeof(cf);
2297 cf.dwMask = 0;
2298 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2299 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2300 return 0;
2303 case ID_EDIT_PARAFORMAT:
2305 PARAFORMAT2 pf;
2306 ZeroMemory(&pf, sizeof(pf));
2307 pf.cbSize = sizeof(pf);
2308 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2309 return 0;
2312 case ID_EDIT_SELECTIONINFO:
2314 CHARRANGE range = {0, -1};
2315 char buf[128];
2316 WCHAR *data = NULL;
2318 SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2319 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2320 SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2321 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2322 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2323 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2324 HeapFree( GetProcessHeap(), 0, data);
2325 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2326 return 0;
2329 case ID_EDIT_READONLY:
2331 LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
2332 if (nStyle & ES_READONLY)
2333 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2334 else
2335 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2336 return 0;
2339 case ID_EDIT_MODIFIED:
2340 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2341 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2342 else
2343 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2344 return 0;
2346 case ID_EDIT_UNDO:
2347 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2348 return 0;
2350 case ID_EDIT_REDO:
2351 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2352 return 0;
2354 case ID_BULLET:
2356 PARAFORMAT pf;
2358 pf.cbSize = sizeof(pf);
2359 pf.dwMask = PFM_NUMBERING;
2360 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2362 pf.dwMask |= PFM_OFFSET;
2364 if(pf.wNumbering == PFN_BULLET)
2366 pf.wNumbering = 0;
2367 pf.dxOffset = 0;
2368 } else
2370 pf.wNumbering = PFN_BULLET;
2371 pf.dxOffset = 720;
2374 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2376 break;
2378 case ID_ALIGN_LEFT:
2379 case ID_ALIGN_CENTER:
2380 case ID_ALIGN_RIGHT:
2382 PARAFORMAT2 pf;
2384 pf.cbSize = sizeof(pf);
2385 pf.dwMask = PFM_ALIGNMENT;
2386 switch(LOWORD(wParam)) {
2387 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2388 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2389 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2391 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2392 break;
2395 case ID_BACK_1:
2396 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2397 break;
2399 case ID_BACK_2:
2400 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2401 break;
2403 case ID_TOGGLE_TOOLBAR:
2404 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2405 update_window();
2406 break;
2408 case ID_TOGGLE_FORMATBAR:
2409 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2410 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2411 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2412 update_window();
2413 break;
2415 case ID_TOGGLE_STATUSBAR:
2416 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2417 update_window();
2418 break;
2420 case ID_TOGGLE_RULER:
2421 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2422 update_window();
2423 break;
2425 case ID_DATETIME:
2426 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2427 break;
2429 case ID_PARAFORMAT:
2430 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2431 break;
2433 case ID_TABSTOPS:
2434 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2435 break;
2437 case ID_ABOUT:
2438 dialog_about();
2439 break;
2441 case ID_VIEWPROPERTIES:
2442 dialog_viewproperties();
2443 break;
2445 case IDC_FONTLIST:
2446 if (HIWORD(wParam) == CBN_SELENDOK)
2448 WCHAR buffer[LF_FACESIZE];
2449 HWND hwndFontList = (HWND)lParam;
2450 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2451 on_fontlist_modified(buffer);
2453 break;
2455 case IDC_SIZELIST:
2456 if (HIWORD(wParam) == CBN_SELENDOK)
2458 WCHAR buffer[MAX_STRING_LEN+1];
2459 HWND hwndSizeList = (HWND)lParam;
2460 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2461 on_sizelist_modified(hwndSizeList, buffer);
2463 break;
2465 default:
2466 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2467 break;
2469 return 0;
2472 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2474 HMENU hMenu = (HMENU)wParam;
2475 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2476 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2477 PARAFORMAT pf;
2478 int nAlignment = -1;
2479 int selFrom, selTo;
2480 GETTEXTLENGTHEX gt;
2481 LRESULT textLength;
2482 MENUITEMINFOW mi;
2484 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2485 EnableMenuItem(hMenu, ID_EDIT_COPY, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2486 EnableMenuItem(hMenu, ID_EDIT_CUT, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2488 pf.cbSize = sizeof(PARAFORMAT);
2489 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2490 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2491 (GetWindowLongW(hwndEditor, GWL_STYLE) & ES_READONLY) ? MF_CHECKED : MF_UNCHECKED);
2492 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2493 SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED);
2494 if (pf.dwMask & PFM_ALIGNMENT)
2495 nAlignment = pf.wAlignment;
2496 CheckMenuItem(hMenu, ID_ALIGN_LEFT, (nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
2497 CheckMenuItem(hMenu, ID_ALIGN_CENTER, (nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
2498 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, (nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
2499 CheckMenuItem(hMenu, ID_BULLET, ((pf.wNumbering == PFN_BULLET) ? MF_CHECKED : MF_UNCHECKED));
2500 EnableMenuItem(hMenu, ID_EDIT_UNDO, SendMessageW(hwndEditor, EM_CANUNDO, 0, 0) ?
2501 MF_ENABLED : MF_GRAYED);
2502 EnableMenuItem(hMenu, ID_EDIT_REDO, SendMessageW(hwndEditor, EM_CANREDO, 0, 0) ?
2503 MF_ENABLED : MF_GRAYED);
2505 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, is_bar_visible(BANDID_TOOLBAR) ?
2506 MF_CHECKED : MF_UNCHECKED);
2508 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, is_bar_visible(BANDID_FORMATBAR) ?
2509 MF_CHECKED : MF_UNCHECKED);
2511 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, IsWindowVisible(hwndStatus) ?
2512 MF_CHECKED : MF_UNCHECKED);
2514 CheckMenuItem(hMenu, ID_TOGGLE_RULER, is_bar_visible(BANDID_RULER) ? MF_CHECKED : MF_UNCHECKED);
2516 gt.flags = GTL_NUMCHARS;
2517 gt.codepage = 1200;
2518 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2519 EnableMenuItem(hMenu, ID_FIND, textLength ? MF_ENABLED : MF_GRAYED);
2521 mi.cbSize = sizeof(mi);
2522 mi.fMask = MIIM_DATA;
2524 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2526 EnableMenuItem(hMenu, ID_FIND_NEXT, (textLength && mi.dwItemData) ? MF_ENABLED : MF_GRAYED);
2528 EnableMenuItem(hMenu, ID_REPLACE, textLength ? MF_ENABLED : MF_GRAYED);
2530 return 0;
2533 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2535 int nStatusSize = 0;
2536 RECT rc;
2537 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2538 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2539 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2540 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2541 int rebarHeight = 0;
2543 if (hwndStatusBar)
2545 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2546 if (IsWindowVisible(hwndStatusBar))
2548 GetClientRect(hwndStatusBar, &rc);
2549 nStatusSize = rc.bottom - rc.top;
2550 } else
2552 nStatusSize = 0;
2555 if (hwndReBar)
2557 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2559 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2561 if (hwndEditor)
2563 GetClientRect(hWnd, &rc);
2564 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2567 redraw_ruler(hRulerWnd);
2569 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2572 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2574 if(msg == ID_FINDMSGSTRING)
2575 return handle_findmsg((LPFINDREPLACEW)lParam);
2577 switch(msg)
2579 case WM_CREATE:
2580 return OnCreate( hWnd );
2582 case WM_USER:
2583 return OnUser( hWnd );
2585 case WM_NOTIFY:
2586 return OnNotify( hWnd, lParam );
2588 case WM_COMMAND:
2589 if(preview_isactive())
2591 return preview_command( hWnd, wParam );
2594 return OnCommand( hWnd, wParam, lParam );
2596 case WM_DESTROY:
2597 PostQuitMessage(0);
2598 break;
2600 case WM_CLOSE:
2601 if(preview_isactive())
2603 preview_exit(hWnd);
2604 } else if(prompt_save_changes())
2606 registry_set_options(hMainWnd);
2607 registry_set_formatopts_all(barState, wordWrap);
2608 PostQuitMessage(0);
2610 break;
2612 case WM_ACTIVATE:
2613 if (LOWORD(wParam))
2614 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2615 return 0;
2617 case WM_INITMENUPOPUP:
2618 return OnInitPopupMenu( hWnd, wParam );
2620 case WM_SIZE:
2621 return OnSize( hWnd, wParam, lParam );
2623 case WM_CONTEXTMENU:
2624 return DefWindowProcW(hWnd, msg, wParam, lParam);
2626 case WM_DROPFILES:
2628 WCHAR file[MAX_PATH];
2629 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2630 DragFinish((HDROP)wParam);
2632 if(prompt_save_changes())
2633 DoOpenFile(file);
2635 break;
2636 case WM_PAINT:
2637 if(!preview_isactive())
2638 return DefWindowProcW(hWnd, msg, wParam, lParam);
2640 default:
2641 return DefWindowProcW(hWnd, msg, wParam, lParam);
2644 return 0;
2647 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2649 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2650 HACCEL hAccel;
2651 WNDCLASSEXW wc;
2652 MSG msg;
2653 RECT rc;
2654 UINT_PTR hPrevRulerProc;
2655 HWND hRulerWnd;
2656 POINTL EditPoint;
2657 DWORD bMaximized;
2658 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2659 'T','A','B','L','E','\0'};
2661 InitCommonControlsEx(&classes);
2663 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2665 wc.cbSize = sizeof(wc);
2666 wc.style = 0;
2667 wc.lpfnWndProc = WndProc;
2668 wc.cbClsExtra = 0;
2669 wc.cbWndExtra = 4;
2670 wc.hInstance = hInstance;
2671 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2672 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2673 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2674 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2675 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2676 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2677 wc.lpszClassName = wszMainWndClass;
2678 RegisterClassExW(&wc);
2680 wc.style = 0;
2681 wc.lpfnWndProc = preview_proc;
2682 wc.cbClsExtra = 0;
2683 wc.cbWndExtra = 0;
2684 wc.hInstance = hInstance;
2685 wc.hIcon = NULL;
2686 wc.hIconSm = NULL;
2687 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2688 wc.hbrBackground = NULL;
2689 wc.lpszMenuName = NULL;
2690 wc.lpszClassName = wszPreviewWndClass;
2691 RegisterClassExW(&wc);
2693 registry_read_winrect(&rc);
2694 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2695 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2696 registry_read_maximized(&bMaximized);
2697 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2698 && bMaximized)
2699 nCmdShow = SW_SHOWMAXIMIZED;
2700 ShowWindow(hMainWnd, nCmdShow);
2702 set_caption(NULL);
2703 set_bar_states();
2704 set_fileformat(SF_RTF);
2705 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2706 get_default_printer_opts();
2707 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2709 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2710 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2711 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2712 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2714 HandleCommandLine(GetCommandLineW());
2716 while(GetMessageW(&msg,0,0,0))
2718 if (IsDialogMessageW(hFindWnd, &msg))
2719 continue;
2721 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2722 continue;
2723 TranslateMessage(&msg);
2724 DispatchMessageW(&msg);
2725 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2726 SendMessageW(hMainWnd, WM_USER, 0, 0);
2729 return 0;