Release 1.1.37.
[wine/gsoc-2012-control.git] / programs / wordpad / wordpad.c
blobed43ce5c618e3a158c71eea057bfbaf75ac3553b
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 static HWND hMainWnd;
60 static HWND hEditorWnd;
61 static HWND hFindWnd;
62 static HMENU hPopupMenu;
64 static UINT ID_FINDMSGSTRING;
66 static DWORD wordWrap[2];
67 static DWORD barState[2];
68 static WPARAM fileFormat = SF_RTF;
70 static WCHAR wszFileName[MAX_PATH];
71 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
72 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
73 static WCHAR wszSaveChanges[MAX_STRING_LEN];
74 static WCHAR units_cmW[MAX_STRING_LEN];
76 static char units_cmA[MAX_STRING_LEN];
78 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
80 /* Load string resources */
81 static void DoLoadStrings(void)
83 LPWSTR p = wszFilter;
84 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
85 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
86 static const WCHAR files_all[] = {'*','.','*','\0'};
88 HINSTANCE hInstance = GetModuleHandleW(0);
90 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
91 p += lstrlenW(p) + 1;
92 lstrcpyW(p, files_rtf);
93 p += lstrlenW(p) + 1;
94 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
95 p += lstrlenW(p) + 1;
96 lstrcpyW(p, files_txt);
97 p += lstrlenW(p) + 1;
98 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
99 p += lstrlenW(p) + 1;
100 lstrcpyW(p, files_txt);
101 p += lstrlenW(p) + 1;
102 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
103 p += lstrlenW(p) + 1;
104 lstrcpyW(p, files_all);
105 p += lstrlenW(p) + 1;
106 *p = '\0';
108 p = wszDefaultFileName;
109 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
111 p = wszSaveChanges;
112 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
114 LoadStringA(hInstance, STRING_UNITS_CM, units_cmA, MAX_STRING_LEN);
115 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
118 /* Show a message box with resource strings */
119 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
121 MSGBOXPARAMSW params;
123 params.cbSize = sizeof(params);
124 params.hwndOwner = hWnd;
125 params.hInstance = GetModuleHandleW(0);
126 params.lpszText = lpText;
127 params.lpszCaption = lpCaption;
128 params.dwStyle = uType;
129 params.lpszIcon = NULL;
130 params.dwContextHelpId = 0;
131 params.lpfnMsgBoxCallback = NULL;
132 params.dwLanguageId = 0;
133 return MessageBoxIndirectW(&params);
137 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
139 TBBUTTON button;
141 ZeroMemory(&button, sizeof(button));
142 button.iBitmap = nImage;
143 button.idCommand = nCommand;
144 button.fsState = TBSTATE_ENABLED;
145 button.fsStyle = TBSTYLE_BUTTON;
146 button.dwData = 0;
147 button.iString = -1;
148 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
151 static void AddSeparator(HWND hwndToolBar)
153 TBBUTTON button;
155 ZeroMemory(&button, sizeof(button));
156 button.iBitmap = -1;
157 button.idCommand = 0;
158 button.fsState = 0;
159 button.fsStyle = TBSTYLE_SEP;
160 button.dwData = 0;
161 button.iString = -1;
162 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
165 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
167 HANDLE hFile = (HANDLE)cookie;
168 DWORD read;
170 if(!ReadFile(hFile, buffer, cb, &read, 0))
171 return 1;
173 *pcb = read;
175 return 0;
178 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
180 DWORD written;
181 int ret;
182 HANDLE hFile = (HANDLE)cookie;
184 ret = WriteFile(hFile, buffer, cb, &written, 0);
186 if(!ret || (cb != written))
187 return 1;
189 *pcb = cb;
191 return 0;
194 LPWSTR file_basename(LPWSTR path)
196 LPWSTR pos = path + lstrlenW(path);
198 while(pos > path)
200 if(*pos == '\\' || *pos == '/')
202 pos++;
203 break;
205 pos--;
207 return pos;
210 static void set_caption(LPCWSTR wszNewFileName)
212 static const WCHAR wszSeparator[] = {' ','-',' '};
213 WCHAR *wszCaption;
214 SIZE_T length = 0;
216 if(!wszNewFileName)
217 wszNewFileName = wszDefaultFileName;
218 else
219 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
221 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
222 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
224 if(!wszCaption)
225 return;
227 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
228 length += lstrlenW(wszNewFileName);
229 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
230 length += sizeof(wszSeparator) / sizeof(WCHAR);
231 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
233 SetWindowTextW(hMainWnd, wszCaption);
235 HeapFree(GetProcessHeap(), 0, wszCaption);
238 static BOOL validate_endptr(LPCSTR endptr, BOOL units)
240 if(!endptr)
241 return FALSE;
242 if(!*endptr)
243 return TRUE;
245 while(*endptr == ' ')
246 endptr++;
248 if(!units)
249 return *endptr == '\0';
251 /* FIXME: Allow other units and convert between them */
252 if(!lstrcmpA(endptr, units_cmA))
253 endptr += 2;
255 return *endptr == '\0';
258 static BOOL number_from_string(LPCWSTR string, float *num, BOOL units)
260 double ret;
261 char buffer[MAX_STRING_LEN];
262 char *endptr = buffer;
264 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN, NULL, NULL);
265 *num = 0;
266 errno = 0;
267 ret = strtod(buffer, &endptr);
269 if((ret == 0 && errno != 0) || endptr == buffer || !validate_endptr(endptr, units))
271 return FALSE;
272 } else
274 *num = (float)ret;
275 return TRUE;
279 static void set_size(float size)
281 CHARFORMAT2W fmt;
283 ZeroMemory(&fmt, sizeof(fmt));
284 fmt.cbSize = sizeof(fmt);
285 fmt.dwMask = CFM_SIZE;
286 fmt.yHeight = (int)(size * 20.0);
287 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
290 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
292 WCHAR sizeBuffer[MAX_STRING_LEN];
293 CHARFORMAT2W format;
295 ZeroMemory(&format, sizeof(format));
296 format.cbSize = sizeof(format);
297 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
299 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
300 if(lstrcmpW(sizeBuffer, wszNewFontSize))
302 float size = 0;
303 if(number_from_string(wszNewFontSize, &size, FALSE)
304 && size > 0)
306 set_size(size);
307 } else
309 SetWindowTextW(hwndSizeList, sizeBuffer);
310 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
311 wszAppTitle, MB_OK | MB_ICONINFORMATION);
316 static void add_size(HWND hSizeListWnd, unsigned size)
318 WCHAR buffer[3];
319 COMBOBOXEXITEMW cbItem;
320 cbItem.mask = CBEIF_TEXT;
321 cbItem.iItem = -1;
323 wsprintfW(buffer, stringFormat, size);
324 cbItem.pszText = buffer;
325 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
328 static void populate_size_list(HWND hSizeListWnd)
330 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
331 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
332 COMBOBOXEXITEMW cbFontItem;
333 CHARFORMAT2W fmt;
334 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
335 HDC hdc = GetDC(hMainWnd);
336 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
337 WCHAR buffer[3];
338 size_t i;
339 DWORD fontStyle;
341 ZeroMemory(&fmt, sizeof(fmt));
342 fmt.cbSize = sizeof(fmt);
343 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
345 cbFontItem.mask = CBEIF_LPARAM;
346 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
347 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
349 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
351 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
353 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
355 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
356 GetDeviceCaps(hdc, LOGPIXELSY)));
357 } else
359 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
360 add_size(hSizeListWnd, choices[i]);
363 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
364 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
367 static void update_size_list(void)
369 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
370 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
371 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
372 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
373 CHARFORMAT2W fmt;
375 ZeroMemory(&fmt, sizeof(fmt));
376 fmt.cbSize = sizeof(fmt);
378 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
380 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
381 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
383 if(lstrcmpW(fontSize, sizeBuffer))
384 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
387 static void update_font_list(void)
389 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
390 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
391 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
392 WCHAR fontName[MAX_STRING_LEN];
393 CHARFORMAT2W fmt;
395 ZeroMemory(&fmt, sizeof(fmt));
396 fmt.cbSize = sizeof(fmt);
398 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
399 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
401 if(lstrcmpW(fontName, fmt.szFaceName))
403 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
404 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
405 } else
407 update_size_list();
411 static void clear_formatting(void)
413 PARAFORMAT2 pf;
415 pf.cbSize = sizeof(pf);
416 pf.dwMask = PFM_ALIGNMENT;
417 pf.wAlignment = PFA_LEFT;
418 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
421 static int fileformat_number(WPARAM format)
423 int number = 0;
425 if(format == SF_TEXT)
427 number = 1;
428 } else if (format == (SF_TEXT | SF_UNICODE))
430 number = 2;
432 return number;
435 static WPARAM fileformat_flags(int format)
437 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
439 return flags[format];
442 static void set_font(LPCWSTR wszFaceName)
444 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
445 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
446 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
447 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
448 CHARFORMAT2W fmt;
450 ZeroMemory(&fmt, sizeof(fmt));
452 fmt.cbSize = sizeof(fmt);
453 fmt.dwMask = CFM_FACE;
455 lstrcpyW(fmt.szFaceName, wszFaceName);
457 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
459 populate_size_list(hSizeListWnd);
461 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
464 static void set_default_font(void)
466 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
467 'R','o','m','a','n',0};
468 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
469 CHARFORMAT2W fmt;
470 LPCWSTR font;
472 ZeroMemory(&fmt, sizeof(fmt));
474 fmt.cbSize = sizeof(fmt);
475 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
476 fmt.dwEffects = 0;
478 if(fileFormat & SF_RTF)
479 font = richTextFont;
480 else
481 font = plainTextFont;
483 lstrcpyW(fmt.szFaceName, font);
485 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
488 static void on_fontlist_modified(LPWSTR wszNewFaceName)
490 CHARFORMAT2W format;
491 ZeroMemory(&format, sizeof(format));
492 format.cbSize = sizeof(format);
493 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
495 if(lstrcmpW(format.szFaceName, wszNewFaceName))
496 set_font(wszNewFaceName);
499 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
501 COMBOBOXEXITEMW cbItem;
502 WCHAR buffer[MAX_PATH];
503 int fontHeight = 0;
505 cbItem.mask = CBEIF_TEXT;
506 cbItem.pszText = buffer;
507 cbItem.cchTextMax = MAX_STRING_LEN;
508 cbItem.iItem = 0;
510 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
512 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
513 cbItem.iItem++;
514 else
515 break;
517 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
518 lstrcpyW( cbItem.pszText, fontName );
520 cbItem.mask |= CBEIF_LPARAM;
521 if(fontType & RASTER_FONTTYPE)
522 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
524 cbItem.lParam = MAKELONG(fontType,fontHeight);
525 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
526 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
529 static void dialog_choose_font(void)
531 CHOOSEFONTW cf;
532 LOGFONTW lf;
533 CHARFORMAT2W fmt;
534 HDC hDC = GetDC(hMainWnd);
536 ZeroMemory(&cf, sizeof(cf));
537 cf.lStructSize = sizeof(cf);
538 cf.hwndOwner = hMainWnd;
539 cf.lpLogFont = &lf;
540 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
542 ZeroMemory(&fmt, sizeof(fmt));
543 fmt.cbSize = sizeof(fmt);
545 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
546 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
547 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) ? TRUE : FALSE;
548 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
549 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE;
550 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE;
551 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
552 cf.rgbColors = fmt.crTextColor;
554 if(ChooseFontW(&cf))
556 ZeroMemory(&fmt, sizeof(fmt));
557 fmt.cbSize = sizeof(fmt);
558 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
559 fmt.yHeight = cf.iPointSize * 2;
561 if(cf.nFontType & BOLD_FONTTYPE)
562 fmt.dwEffects |= CFE_BOLD;
563 if(cf.nFontType & ITALIC_FONTTYPE)
564 fmt.dwEffects |= CFE_ITALIC;
565 if(cf.lpLogFont->lfUnderline == TRUE)
566 fmt.dwEffects |= CFE_UNDERLINE;
567 if(cf.lpLogFont->lfStrikeOut == TRUE)
568 fmt.dwEffects |= CFE_STRIKEOUT;
570 fmt.crTextColor = cf.rgbColors;
572 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
573 set_font(cf.lpLogFont->lfFaceName);
578 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
579 DWORD FontType, LPARAM lParam)
581 HWND hListWnd = (HWND) lParam;
583 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
586 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
589 return 1;
592 static void populate_font_list(HWND hListWnd)
594 HDC hdc = GetDC(hMainWnd);
595 LOGFONTW fontinfo;
596 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
597 CHARFORMAT2W fmt;
599 fontinfo.lfCharSet = DEFAULT_CHARSET;
600 *fontinfo.lfFaceName = '\0';
601 fontinfo.lfPitchAndFamily = 0;
603 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
604 (LPARAM)hListWnd, 0);
606 ZeroMemory(&fmt, sizeof(fmt));
607 fmt.cbSize = sizeof(fmt);
608 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
609 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
612 static void update_window(void)
614 RECT rect;
616 GetClientRect(hMainWnd, &rect);
618 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
621 static BOOL is_bar_visible(int bandId)
623 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
626 static void store_bar_state(int bandId, BOOL show)
628 int formatIndex = reg_formatindex(fileFormat);
630 if(show)
631 barState[formatIndex] |= (1 << bandId);
632 else
633 barState[formatIndex] &= ~(1 << bandId);
636 static void set_toolbar_state(int bandId, BOOL show)
638 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
640 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
642 if(bandId == BANDID_TOOLBAR)
644 REBARBANDINFOW rbbinfo;
645 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
647 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
648 rbbinfo.fMask = RBBIM_STYLE;
650 SendMessageW(hwndReBar, RB_GETBANDINFO, index, (LPARAM)&rbbinfo);
652 if(!show)
653 rbbinfo.fStyle &= ~RBBS_BREAK;
654 else
655 rbbinfo.fStyle |= RBBS_BREAK;
657 SendMessageW(hwndReBar, RB_SETBANDINFO, index, (LPARAM)&rbbinfo);
660 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
661 store_bar_state(bandId, show);
664 static void set_statusbar_state(BOOL show)
666 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
668 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
669 store_bar_state(BANDID_STATUSBAR, show);
672 static void set_bar_states(void)
674 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
675 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
676 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
677 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
678 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
679 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
681 update_window();
684 static void preview_exit(HWND hMainWnd)
686 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
687 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
689 set_bar_states();
690 ShowWindow(hEditorWnd, TRUE);
692 close_preview(hMainWnd);
694 SetMenu(hMainWnd, hMenu);
695 registry_read_filelist(hMainWnd);
697 update_window();
700 static void set_fileformat(WPARAM format)
702 HICON hIcon;
703 HINSTANCE hInstance = GetModuleHandleW(0);
704 fileFormat = format;
706 if(format & SF_TEXT)
707 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_TXT));
708 else
709 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_RTF));
711 SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
713 set_bar_states();
714 set_default_font();
715 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
718 static void ShowOpenError(DWORD Code)
720 LPWSTR Message;
722 switch(Code)
724 case ERROR_ACCESS_DENIED:
725 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
726 break;
728 default:
729 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
731 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
734 static void DoOpenFile(LPCWSTR szOpenFileName)
736 HANDLE hFile;
737 EDITSTREAM es;
738 char fileStart[5];
739 DWORD readOut;
740 WPARAM format = SF_TEXT;
742 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
743 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
744 if (hFile == INVALID_HANDLE_VALUE)
746 ShowOpenError(GetLastError());
747 return;
750 ReadFile(hFile, fileStart, 5, &readOut, NULL);
751 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
753 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
755 format = SF_TEXT | SF_UNICODE;
756 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
757 } else if(readOut >= 5)
759 static const char header[] = "{\\rtf";
760 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
762 if(!memcmp(header, fileStart, 5))
763 format = SF_RTF;
764 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
766 CloseHandle(hFile);
767 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
768 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
769 return;
773 es.dwCookie = (DWORD_PTR)hFile;
774 es.pfnCallback = stream_in;
776 clear_formatting();
777 set_fileformat(format);
778 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
780 CloseHandle(hFile);
782 SetFocus(hEditorWnd);
784 set_caption(szOpenFileName);
786 lstrcpyW(wszFileName, szOpenFileName);
787 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
788 registry_set_filelist(szOpenFileName, hMainWnd);
789 update_font_list();
792 static void ShowWriteError(DWORD Code)
794 LPWSTR Message;
796 switch(Code)
798 case ERROR_ACCESS_DENIED:
799 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
800 break;
802 default:
803 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
805 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
808 static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
810 HANDLE hFile;
811 EDITSTREAM stream;
812 LRESULT ret;
814 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
815 FILE_ATTRIBUTE_NORMAL, NULL);
817 if(hFile == INVALID_HANDLE_VALUE)
819 ShowWriteError(GetLastError());
820 return;
823 if(format == (SF_TEXT | SF_UNICODE))
825 static const BYTE unicode[] = {0xff,0xfe};
826 DWORD writeOut;
827 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
829 if(writeOut != sizeof(unicode))
831 CloseHandle(hFile);
832 return;
836 stream.dwCookie = (DWORD_PTR)hFile;
837 stream.pfnCallback = stream_out;
839 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
841 CloseHandle(hFile);
843 SetFocus(hEditorWnd);
845 if(!ret)
847 GETTEXTLENGTHEX gt;
848 gt.flags = GTL_DEFAULT;
849 gt.codepage = 1200;
851 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
852 return;
855 lstrcpyW(wszFileName, wszSaveFileName);
856 set_caption(wszFileName);
857 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
858 set_fileformat(format);
861 static void DialogSaveFile(void)
863 OPENFILENAMEW sfn;
865 WCHAR wszFile[MAX_PATH] = {'\0'};
866 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
868 ZeroMemory(&sfn, sizeof(sfn));
870 sfn.lStructSize = sizeof(sfn);
871 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
872 sfn.hwndOwner = hMainWnd;
873 sfn.lpstrFilter = wszFilter;
874 sfn.lpstrFile = wszFile;
875 sfn.nMaxFile = MAX_PATH;
876 sfn.lpstrDefExt = wszDefExt;
877 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
879 while(GetSaveFileNameW(&sfn))
881 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
883 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
884 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
886 continue;
887 } else
889 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
890 break;
892 } else
894 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
895 break;
900 static BOOL prompt_save_changes(void)
902 if(!wszFileName[0])
904 GETTEXTLENGTHEX gt;
905 gt.flags = GTL_NUMCHARS;
906 gt.codepage = 1200;
907 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
908 return TRUE;
911 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
913 return TRUE;
914 } else
916 LPWSTR displayFileName;
917 WCHAR *text;
918 int ret;
920 if(!wszFileName[0])
921 displayFileName = wszDefaultFileName;
922 else
923 displayFileName = file_basename(wszFileName);
925 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
926 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
928 if(!text)
929 return FALSE;
931 wsprintfW(text, wszSaveChanges, displayFileName);
933 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
935 HeapFree(GetProcessHeap(), 0, text);
937 switch(ret)
939 case IDNO:
940 return TRUE;
942 case IDYES:
943 if(wszFileName[0])
944 DoSaveFile(wszFileName, fileFormat);
945 else
946 DialogSaveFile();
947 return TRUE;
949 default:
950 return FALSE;
955 static void DialogOpenFile(void)
957 OPENFILENAMEW ofn;
959 WCHAR wszFile[MAX_PATH] = {'\0'};
960 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
962 ZeroMemory(&ofn, sizeof(ofn));
964 ofn.lStructSize = sizeof(ofn);
965 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
966 ofn.hwndOwner = hMainWnd;
967 ofn.lpstrFilter = wszFilter;
968 ofn.lpstrFile = wszFile;
969 ofn.nMaxFile = MAX_PATH;
970 ofn.lpstrDefExt = wszDefExt;
971 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
973 if(GetOpenFileNameW(&ofn))
975 if(prompt_save_changes())
976 DoOpenFile(ofn.lpstrFile);
980 static void dialog_about(void)
982 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
983 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
986 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
988 switch(message)
990 case WM_INITDIALOG:
992 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
993 int wrap = -1;
994 char id[4];
995 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
997 sprintf(id, "%d\n", (int)ps->lParam);
998 SetWindowTextA(hIdWnd, id);
999 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1000 wrap = IDC_PAGEFMT_WN;
1001 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1002 wrap = IDC_PAGEFMT_WW;
1003 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1004 wrap = IDC_PAGEFMT_WM;
1006 if(wrap != -1)
1007 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1008 IDC_PAGEFMT_WM, wrap);
1010 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1011 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1012 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1013 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1014 if(barState[ps->lParam] & (1 << BANDID_RULER))
1015 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1016 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1017 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1019 break;
1021 case WM_COMMAND:
1022 switch(LOWORD(wParam))
1024 case IDC_PAGEFMT_WN:
1025 case IDC_PAGEFMT_WW:
1026 case IDC_PAGEFMT_WM:
1027 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1028 LOWORD(wParam));
1029 break;
1031 case IDC_PAGEFMT_TB:
1032 case IDC_PAGEFMT_FB:
1033 case IDC_PAGEFMT_RU:
1034 case IDC_PAGEFMT_SB:
1035 CheckDlgButton(hWnd, LOWORD(wParam),
1036 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1037 break;
1039 break;
1040 case WM_NOTIFY:
1042 LPNMHDR header = (LPNMHDR)lParam;
1043 if(header->code == PSN_APPLY)
1045 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1046 char sid[4];
1047 int id;
1049 GetWindowTextA(hIdWnd, sid, 4);
1050 id = atoi(sid);
1051 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1052 wordWrap[id] = ID_WORDWRAP_NONE;
1053 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1054 wordWrap[id] = ID_WORDWRAP_WINDOW;
1055 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1056 wordWrap[id] = ID_WORDWRAP_MARGIN;
1058 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1059 barState[id] |= (1 << BANDID_TOOLBAR);
1060 else
1061 barState[id] &= ~(1 << BANDID_TOOLBAR);
1063 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1064 barState[id] |= (1 << BANDID_FORMATBAR);
1065 else
1066 barState[id] &= ~(1 << BANDID_FORMATBAR);
1068 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1069 barState[id] |= (1 << BANDID_RULER);
1070 else
1071 barState[id] &= ~(1 << BANDID_RULER);
1073 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1074 barState[id] |= (1 << BANDID_STATUSBAR);
1075 else
1076 barState[id] &= ~(1 << BANDID_STATUSBAR);
1079 break;
1081 return FALSE;
1084 static void dialog_viewproperties(void)
1086 PROPSHEETPAGEW psp[2];
1087 PROPSHEETHEADERW psh;
1088 size_t i;
1089 HINSTANCE hInstance = GetModuleHandleW(0);
1090 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1092 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1093 psp[0].dwFlags = PSP_USETITLE;
1094 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1095 psp[0].pfnDlgProc = formatopts_proc;
1096 psp[0].hInstance = hInstance;
1097 psp[0].lParam = reg_formatindex(SF_TEXT);
1098 psp[0].pfnCallback = NULL;
1099 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1100 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1102 psp[i].dwSize = psp[0].dwSize;
1103 psp[i].dwFlags = psp[0].dwFlags;
1104 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1105 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1106 psp[i].hInstance = psp[0].hInstance;
1107 psp[i].lParam = reg_formatindex(SF_RTF);
1108 psp[i].pfnCallback = psp[0].pfnCallback;
1109 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1112 psh.dwSize = sizeof(psh);
1113 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1114 psh.hwndParent = hMainWnd;
1115 psh.hInstance = hInstance;
1116 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1117 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1118 U3(psh).ppsp = ppsp;
1119 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1121 if(fileFormat & SF_RTF)
1122 U2(psh).nStartPage = 1;
1123 else
1124 U2(psh).nStartPage = 0;
1125 PropertySheetW(&psh);
1126 set_bar_states();
1127 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1130 static void HandleCommandLine(LPWSTR cmdline)
1132 WCHAR delimiter;
1133 int opt_print = 0;
1135 /* skip white space */
1136 while (*cmdline == ' ') cmdline++;
1138 /* skip executable name */
1139 delimiter = (*cmdline == '"' ? '"' : ' ');
1141 if (*cmdline == delimiter) cmdline++;
1142 while (*cmdline && *cmdline != delimiter) cmdline++;
1143 if (*cmdline == delimiter) cmdline++;
1145 while (*cmdline)
1147 while (isspace(*cmdline)) cmdline++;
1149 if (*cmdline == '-' || *cmdline == '/')
1151 if (!cmdline[2] || isspace(cmdline[2]))
1153 switch (cmdline[1])
1155 case 'P':
1156 case 'p':
1157 opt_print = 1;
1158 cmdline += 2;
1159 continue;
1162 /* a filename starting by / */
1164 break;
1167 if (*cmdline)
1169 /* file name is passed on the command line */
1170 if (cmdline[0] == '"')
1172 cmdline++;
1173 cmdline[lstrlenW(cmdline) - 1] = 0;
1175 DoOpenFile(cmdline);
1176 InvalidateRect(hMainWnd, NULL, FALSE);
1179 if (opt_print)
1180 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1183 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1185 if(pFr->Flags & FR_DIALOGTERM)
1187 hFindWnd = 0;
1188 pFr->Flags = FR_FINDNEXT;
1189 return 0;
1192 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1194 DWORD flags = FR_DOWN;
1195 FINDTEXTW ft;
1196 static CHARRANGE cr;
1197 LRESULT end, ret;
1198 GETTEXTLENGTHEX gt;
1199 LRESULT length;
1200 int startPos;
1201 HMENU hMenu = GetMenu(hMainWnd);
1202 MENUITEMINFOW mi;
1204 mi.cbSize = sizeof(mi);
1205 mi.fMask = MIIM_DATA;
1206 mi.dwItemData = 1;
1207 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1209 gt.flags = GTL_NUMCHARS;
1210 gt.codepage = 1200;
1212 length = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
1214 if(pFr->lCustData == -1)
1216 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&startPos, (LPARAM)&end);
1217 cr.cpMin = startPos;
1218 pFr->lCustData = startPos;
1219 cr.cpMax = length;
1220 if(cr.cpMin == length)
1221 cr.cpMin = 0;
1222 } else
1224 startPos = pFr->lCustData;
1227 if(cr.cpMax > length)
1229 startPos = 0;
1230 cr.cpMin = 0;
1231 cr.cpMax = length;
1234 ft.chrg = cr;
1235 ft.lpstrText = pFr->lpstrFindWhat;
1237 if(pFr->Flags & FR_MATCHCASE)
1238 flags |= FR_MATCHCASE;
1239 if(pFr->Flags & FR_WHOLEWORD)
1240 flags |= FR_WHOLEWORD;
1242 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, flags, (LPARAM)&ft);
1244 if(ret == -1)
1246 if(cr.cpMax == length && cr.cpMax != startPos)
1248 ft.chrg.cpMin = cr.cpMin = 0;
1249 ft.chrg.cpMax = cr.cpMax = startPos;
1251 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, flags, (LPARAM)&ft);
1255 if(ret == -1)
1257 pFr->lCustData = -1;
1258 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED), wszAppTitle,
1259 MB_OK | MB_ICONASTERISK);
1260 } else
1262 end = ret + lstrlenW(pFr->lpstrFindWhat);
1263 cr.cpMin = end;
1264 SendMessageW(hEditorWnd, EM_SETSEL, ret, end);
1265 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1267 if(pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1268 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1270 if(pFr->Flags & FR_REPLACEALL)
1271 handle_findmsg(pFr);
1275 return 0;
1278 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1280 static WCHAR findBuffer[MAX_STRING_LEN];
1282 /* Allow only one search/replace dialog to open */
1283 if(hFindWnd != NULL)
1285 SetActiveWindow(hFindWnd);
1286 return;
1289 ZeroMemory(fr, sizeof(FINDREPLACEW));
1290 fr->lStructSize = sizeof(FINDREPLACEW);
1291 fr->hwndOwner = hMainWnd;
1292 fr->Flags = FR_HIDEUPDOWN;
1293 fr->lpstrFindWhat = findBuffer;
1294 fr->lCustData = -1;
1295 fr->wFindWhatLen = MAX_STRING_LEN*sizeof(WCHAR);
1297 if(replace)
1298 hFindWnd = ReplaceTextW(fr);
1299 else
1300 hFindWnd = FindTextW(fr);
1303 static int current_units_to_twips(float number)
1305 int twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1306 return twips;
1309 static void append_current_units(LPWSTR buffer)
1311 static const WCHAR space[] = {' ', 0};
1312 lstrcatW(buffer, space);
1313 lstrcatW(buffer, units_cmW);
1316 static void number_with_units(LPWSTR buffer, int number)
1318 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1319 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1321 sprintfW(buffer, fmt, converted, units_cmW);
1324 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1326 COMBOBOXEXITEMW cbItem;
1327 COMBOBOXINFO cbInfo;
1328 HWND hCombo, hList;
1329 int idx, result;
1331 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1332 if (!hCombo)
1333 return FALSE;
1334 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1335 result = SendMessage(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1336 if (!result)
1337 return FALSE;
1338 hList = cbInfo.hwndList;
1339 idx = SendMessage(hList, LB_GETCURSEL, 0, 0);
1340 if (idx < 0)
1341 return FALSE;
1343 ZeroMemory(&cbItem, sizeof(cbItem));
1344 cbItem.mask = CBEIF_TEXT;
1345 cbItem.iItem = idx;
1346 cbItem.pszText = wszBuffer;
1347 cbItem.cchTextMax = bufferLength-1;
1348 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1350 return result != 0;
1353 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1355 switch(message)
1357 case WM_INITDIALOG:
1359 WCHAR buffer[MAX_STRING_LEN];
1360 SYSTEMTIME st;
1361 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1362 GetLocalTime(&st);
1364 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1365 MAX_STRING_LEN);
1366 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1367 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1368 MAX_STRING_LEN);
1369 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1370 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1371 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1373 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1375 break;
1377 case WM_COMMAND:
1378 switch(LOWORD(wParam))
1380 case IDC_DATETIME:
1381 if (HIWORD(wParam) != LBN_DBLCLK)
1382 break;
1383 /* Fall through */
1385 case IDOK:
1387 LRESULT index;
1388 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1390 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1392 if(index != LB_ERR)
1394 WCHAR buffer[MAX_STRING_LEN];
1395 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1396 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1399 /* Fall through */
1401 case IDCANCEL:
1402 EndDialog(hWnd, wParam);
1403 return TRUE;
1406 return FALSE;
1409 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1411 switch(message)
1413 case WM_INITDIALOG:
1415 HINSTANCE hInstance = GetModuleHandleW(0);
1416 WCHAR buffer[MAX_STRING_LEN];
1417 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1419 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1420 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1421 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1422 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1423 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1424 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1426 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1428 break;
1430 case WM_COMMAND:
1431 switch(LOWORD(wParam))
1433 case IDOK:
1435 LRESULT index;
1436 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1437 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1439 if(index != LB_ERR)
1440 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1442 return TRUE;
1444 case IDCANCEL:
1445 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1446 return TRUE;
1449 return FALSE;
1452 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1454 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1456 switch(message)
1458 case WM_INITDIALOG:
1460 HINSTANCE hInstance = GetModuleHandleW(0);
1461 WCHAR buffer[MAX_STRING_LEN];
1462 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1463 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1464 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1465 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1466 PARAFORMAT2 pf;
1467 int index = 0;
1469 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1470 MAX_STRING_LEN);
1471 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1472 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1473 MAX_STRING_LEN);
1474 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1475 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1476 MAX_STRING_LEN);
1477 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1479 pf.cbSize = sizeof(pf);
1480 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1481 PFM_STARTINDENT;
1482 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1484 if(pf.wAlignment == PFA_RIGHT)
1485 index ++;
1486 else if(pf.wAlignment == PFA_CENTER)
1487 index += 2;
1489 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1491 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1492 SetWindowTextW(hLeftWnd, buffer);
1493 number_with_units(buffer, pf.dxRightIndent);
1494 SetWindowTextW(hRightWnd, buffer);
1495 number_with_units(buffer, -pf.dxOffset);
1496 SetWindowTextW(hFirstWnd, buffer);
1498 break;
1500 case WM_COMMAND:
1501 switch(LOWORD(wParam))
1503 case IDOK:
1505 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1506 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1507 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1508 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1509 WCHAR buffer[MAX_STRING_LEN];
1510 int index;
1511 float num;
1512 int ret = 0;
1513 PARAFORMAT pf;
1515 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1516 pf.wAlignment = ALIGNMENT_VALUES[index];
1518 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1519 if(number_from_string(buffer, &num, TRUE))
1520 ret++;
1521 pf.dxOffset = current_units_to_twips(num);
1522 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1523 if(number_from_string(buffer, &num, TRUE))
1524 ret++;
1525 pf.dxRightIndent = current_units_to_twips(num);
1526 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1527 if(number_from_string(buffer, &num, TRUE))
1528 ret++;
1529 pf.dxStartIndent = current_units_to_twips(num);
1531 if(ret != 3)
1533 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1534 wszAppTitle, MB_OK | MB_ICONASTERISK);
1535 return FALSE;
1536 } else
1538 if (pf.dxOffset + pf.dxStartIndent < 0
1539 && pf.dxStartIndent < 0)
1541 /* The first line is before the left edge, so
1542 * make sure it is at the left edge. */
1543 pf.dxOffset = -pf.dxStartIndent;
1544 } else if (pf.dxOffset < 0) {
1545 /* The second and following lines are before
1546 * the left edge, so set it to be at the left
1547 * edge, and adjust the first line since it
1548 * is relative to it. */
1549 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1550 pf.dxOffset = 0;
1552 /* Internally the dxStartIndent is the absolute
1553 * offset for the first line and dxOffset is
1554 * to it value as opposed how it is displayed with
1555 * the first line being the relative value.
1556 * These two lines make the adjustments. */
1557 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1558 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1560 pf.cbSize = sizeof(pf);
1561 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1562 PFM_STARTINDENT;
1563 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1566 /* Fall through */
1568 case IDCANCEL:
1569 EndDialog(hWnd, wParam);
1570 return TRUE;
1573 return FALSE;
1576 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1578 switch(message)
1580 case WM_INITDIALOG:
1582 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1583 PARAFORMAT pf;
1584 WCHAR buffer[MAX_STRING_LEN];
1585 int i;
1587 pf.cbSize = sizeof(pf);
1588 pf.dwMask = PFM_TABSTOPS;
1589 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1590 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1592 for(i = 0; i < pf.cTabCount; i++)
1594 number_with_units(buffer, pf.rgxTabs[i]);
1595 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1597 SetFocus(hTabWnd);
1599 break;
1601 case WM_COMMAND:
1602 switch(LOWORD(wParam))
1604 case IDC_TABSTOPS:
1606 HWND hTabWnd = (HWND)lParam;
1607 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1608 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1609 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1611 if(GetWindowTextLengthW(hTabWnd))
1612 EnableWindow(hAddWnd, TRUE);
1613 else
1614 EnableWindow(hAddWnd, FALSE);
1616 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1618 EnableWindow(hEmptyWnd, TRUE);
1620 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1621 EnableWindow(hDelWnd, FALSE);
1622 else
1623 EnableWindow(hDelWnd, TRUE);
1624 } else
1626 EnableWindow(hEmptyWnd, FALSE);
1629 break;
1631 case ID_TAB_ADD:
1633 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1634 WCHAR buffer[MAX_STRING_LEN];
1636 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1637 append_current_units(buffer);
1639 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1641 float number = 0;
1642 int item_count = SendMessage(hTabWnd, CB_GETCOUNT, 0, 0);
1644 if(!number_from_string(buffer, &number, TRUE))
1646 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1647 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1648 } else if (item_count >= MAX_TAB_STOPS) {
1649 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1650 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1651 } else {
1652 int i;
1653 float next_number = -1;
1654 int next_number_in_twips = -1;
1655 int insert_number = current_units_to_twips(number);
1657 /* linear search for position to insert the string */
1658 for(i = 0; i < item_count; i++)
1660 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1661 number_from_string(buffer, &next_number, TRUE);
1662 next_number_in_twips = current_units_to_twips(next_number);
1663 if (insert_number <= next_number_in_twips)
1664 break;
1666 if (insert_number != next_number_in_twips)
1668 number_with_units(buffer, insert_number);
1669 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1670 SetWindowTextW(hTabWnd, 0);
1674 SetFocus(hTabWnd);
1676 break;
1678 case ID_TAB_DEL:
1680 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1681 LRESULT ret;
1682 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1683 if(ret != CB_ERR)
1684 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1686 break;
1688 case ID_TAB_EMPTY:
1690 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1691 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1692 SetFocus(hTabWnd);
1694 break;
1696 case IDOK:
1698 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1699 int i;
1700 WCHAR buffer[MAX_STRING_LEN];
1701 PARAFORMAT pf;
1702 float number;
1704 pf.cbSize = sizeof(pf);
1705 pf.dwMask = PFM_TABSTOPS;
1707 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1708 (LPARAM)&buffer) != CB_ERR &&
1709 i < MAX_TAB_STOPS; i++)
1711 number_from_string(buffer, &number, TRUE);
1712 pf.rgxTabs[i] = current_units_to_twips(number);
1714 pf.cTabCount = i;
1715 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1717 /* Fall through */
1718 case IDCANCEL:
1719 EndDialog(hWnd, wParam);
1720 return TRUE;
1723 return FALSE;
1726 static int context_menu(LPARAM lParam)
1728 int x = (int)(short)LOWORD(lParam);
1729 int y = (int)(short)HIWORD(lParam);
1730 HMENU hPop = GetSubMenu(hPopupMenu, 0);
1732 if(x == -1)
1734 int from = 0, to = 0;
1735 POINTL pt;
1736 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1737 SendMessageW(hEditorWnd, EM_POSFROMCHAR, (WPARAM)&pt, to);
1738 ClientToScreen(hEditorWnd, (POINT*)&pt);
1739 x = pt.x;
1740 y = pt.y;
1743 TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
1744 x, y, 0, hMainWnd, 0);
1746 return 0;
1749 static LRESULT OnCreate( HWND hWnd )
1751 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1752 HINSTANCE hInstance = GetModuleHandleW(0);
1753 HANDLE hDLL;
1754 TBADDBITMAP ab;
1755 int nStdBitmaps = 0;
1756 REBARINFO rbi;
1757 REBARBANDINFOW rbb;
1758 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1759 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1761 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1763 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1764 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1765 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1767 rbi.cbSize = sizeof(rbi);
1768 rbi.fMask = 0;
1769 rbi.himl = NULL;
1770 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1771 return -1;
1773 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
1774 IDC_TOOLBAR,
1775 1, hInstance, IDB_TOOLBAR,
1776 NULL, 0,
1777 24, 24, 16, 16, sizeof(TBBUTTON));
1779 ab.hInst = HINST_COMMCTRL;
1780 ab.nID = IDB_STD_SMALL_COLOR;
1781 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1783 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1784 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1785 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1786 AddSeparator(hToolBarWnd);
1787 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1788 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1789 AddSeparator(hToolBarWnd);
1790 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1791 AddSeparator(hToolBarWnd);
1792 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1793 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1794 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1795 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1796 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1797 AddSeparator(hToolBarWnd);
1798 AddButton(hToolBarWnd, 0, ID_DATETIME);
1800 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1802 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1803 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1804 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1805 rbb.cx = 0;
1806 rbb.hwndChild = hToolBarWnd;
1807 rbb.cxMinChild = 0;
1808 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1809 rbb.wID = BANDID_TOOLBAR;
1811 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1813 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1814 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1815 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1817 rbb.hwndChild = hFontListWnd;
1818 rbb.cx = 200;
1819 rbb.wID = BANDID_FONTLIST;
1821 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1823 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1824 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1825 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1827 rbb.hwndChild = hSizeListWnd;
1828 rbb.cx = 50;
1829 rbb.fStyle ^= RBBS_BREAK;
1830 rbb.wID = BANDID_SIZELIST;
1832 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1834 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1835 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
1836 IDC_FORMATBAR, 7, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1838 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1839 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1840 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1841 AddSeparator(hFormatBarWnd);
1842 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
1843 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
1844 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
1845 AddSeparator(hFormatBarWnd);
1846 AddButton(hFormatBarWnd, 6, ID_BULLET);
1848 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1850 rbb.hwndChild = hFormatBarWnd;
1851 rbb.wID = BANDID_FORMATBAR;
1853 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1855 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1856 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1859 rbb.hwndChild = hRulerWnd;
1860 rbb.wID = BANDID_RULER;
1861 rbb.fStyle |= RBBS_BREAK;
1863 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1865 hDLL = LoadLibraryW(wszRichEditDll);
1866 if(!hDLL)
1868 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1869 MB_OK | MB_ICONEXCLAMATION);
1870 PostQuitMessage(1);
1873 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1874 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1875 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1876 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1878 if (!hEditorWnd)
1880 fprintf(stderr, "Error code %u\n", GetLastError());
1881 return -1;
1883 assert(hEditorWnd);
1885 SetFocus(hEditorWnd);
1886 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1888 set_default_font();
1890 populate_font_list(hFontListWnd);
1891 populate_size_list(hSizeListWnd);
1892 DoLoadStrings();
1893 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1895 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1897 registry_read_filelist(hWnd);
1898 registry_read_formatopts_all(barState, wordWrap);
1899 registry_read_options();
1900 DragAcceptFiles(hWnd, TRUE);
1902 return 0;
1905 static LRESULT OnUser( HWND hWnd )
1907 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1908 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1909 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1910 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1911 int from, to;
1912 CHARFORMAT2W fmt;
1913 PARAFORMAT2 pf;
1914 GETTEXTLENGTHEX gt;
1916 ZeroMemory(&fmt, sizeof(fmt));
1917 fmt.cbSize = sizeof(fmt);
1919 ZeroMemory(&pf, sizeof(pf));
1920 pf.cbSize = sizeof(pf);
1922 gt.flags = GTL_NUMCHARS;
1923 gt.codepage = 1200;
1925 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1926 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1928 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1930 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1931 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1932 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1933 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1934 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1935 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1936 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
1938 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
1939 (fmt.dwEffects & CFE_BOLD));
1940 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
1941 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
1942 (fmt.dwEffects & CFE_ITALIC));
1943 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
1944 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
1945 (fmt.dwEffects & CFE_UNDERLINE));
1946 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
1948 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1949 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
1950 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
1951 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
1953 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
1954 return 0;
1957 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
1959 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1960 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1961 NMHDR *pHdr = (NMHDR *)lParam;
1962 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
1963 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
1965 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
1967 if (pHdr->code == CBEN_ENDEDITW)
1969 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
1970 if(pHdr->hwndFrom == hwndFontList)
1972 on_fontlist_modified((LPWSTR)endEdit->szText);
1973 } else if (pHdr->hwndFrom == hwndSizeList)
1975 on_sizelist_modified(hwndFontList,(LPWSTR)endEdit->szText);
1978 return 0;
1981 if (pHdr->hwndFrom != hwndEditor)
1982 return 0;
1984 if (pHdr->code == EN_SELCHANGE)
1986 SELCHANGE *pSC = (SELCHANGE *)lParam;
1987 char buf[128];
1989 update_font_list();
1991 sprintf( buf,"selection = %d..%d, line count=%ld",
1992 pSC->chrg.cpMin, pSC->chrg.cpMax,
1993 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
1994 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
1995 SendMessage(hWnd, WM_USER, 0, 0);
1996 return 1;
1998 return 0;
2001 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2003 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2004 static FINDREPLACEW findreplace;
2006 if ((HWND)lParam == hwndEditor)
2007 return 0;
2009 switch(LOWORD(wParam))
2012 case ID_FILE_EXIT:
2013 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2014 break;
2016 case ID_FILE_NEW:
2018 HINSTANCE hInstance = GetModuleHandleW(0);
2019 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
2020 newfile_proc);
2022 if(ret != ID_NEWFILE_ABORT)
2024 if(prompt_save_changes())
2026 SETTEXTEX st;
2028 set_caption(NULL);
2029 wszFileName[0] = '\0';
2031 clear_formatting();
2033 st.flags = ST_DEFAULT;
2034 st.codepage = 1200;
2035 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2037 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2038 set_fileformat(ret);
2039 update_font_list();
2043 break;
2045 case ID_FILE_OPEN:
2046 DialogOpenFile();
2047 break;
2049 case ID_FILE_SAVE:
2050 if(wszFileName[0])
2052 DoSaveFile(wszFileName, fileFormat);
2053 break;
2055 /* Fall through */
2057 case ID_FILE_SAVEAS:
2058 DialogSaveFile();
2059 break;
2061 case ID_FILE_RECENT1:
2062 case ID_FILE_RECENT2:
2063 case ID_FILE_RECENT3:
2064 case ID_FILE_RECENT4:
2066 HMENU hMenu = GetMenu(hWnd);
2067 MENUITEMINFOW mi;
2069 mi.cbSize = sizeof(MENUITEMINFOW);
2070 mi.fMask = MIIM_DATA;
2071 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2072 DoOpenFile((LPWSTR)mi.dwItemData);
2074 break;
2076 case ID_FIND:
2077 dialog_find(&findreplace, FALSE);
2078 break;
2080 case ID_FIND_NEXT:
2081 handle_findmsg(&findreplace);
2082 break;
2084 case ID_REPLACE:
2085 dialog_find(&findreplace, TRUE);
2086 break;
2088 case ID_FONTSETTINGS:
2089 dialog_choose_font();
2090 break;
2092 case ID_PRINT:
2093 dialog_print(hWnd, wszFileName);
2094 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2095 break;
2097 case ID_PRINT_QUICK:
2098 print_quick(wszFileName);
2099 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2100 break;
2102 case ID_PREVIEW:
2104 int index = reg_formatindex(fileFormat);
2105 DWORD tmp = barState[index];
2106 barState[index] = 0;
2107 set_bar_states();
2108 barState[index] = tmp;
2109 ShowWindow(hEditorWnd, FALSE);
2111 init_preview(hWnd, wszFileName);
2113 SetMenu(hWnd, NULL);
2114 InvalidateRect(0, 0, TRUE);
2116 break;
2118 case ID_PRINTSETUP:
2119 dialog_printsetup(hWnd);
2120 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2121 break;
2123 case ID_FORMAT_BOLD:
2124 case ID_FORMAT_ITALIC:
2125 case ID_FORMAT_UNDERLINE:
2127 CHARFORMAT2W fmt;
2128 int effects = CFE_BOLD;
2130 ZeroMemory(&fmt, sizeof(fmt));
2131 fmt.cbSize = sizeof(fmt);
2132 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2134 fmt.dwMask = CFM_BOLD;
2136 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2138 effects = CFE_ITALIC;
2139 fmt.dwMask = CFM_ITALIC;
2140 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2142 effects = CFE_UNDERLINE;
2143 fmt.dwMask = CFM_UNDERLINE;
2146 fmt.dwEffects ^= effects;
2148 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2149 break;
2152 case ID_EDIT_CUT:
2153 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2154 break;
2156 case ID_EDIT_COPY:
2157 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2158 break;
2160 case ID_EDIT_PASTE:
2161 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2162 break;
2164 case ID_EDIT_CLEAR:
2165 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2166 break;
2168 case ID_EDIT_SELECTALL:
2170 CHARRANGE range = {0, -1};
2171 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2172 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2173 return 0;
2176 case ID_EDIT_GETTEXT:
2178 int nLen = GetWindowTextLengthW(hwndEditor);
2179 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2180 TEXTRANGEW tr;
2182 GetWindowTextW(hwndEditor, data, nLen+1);
2183 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2185 HeapFree( GetProcessHeap(), 0, data);
2186 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2187 tr.chrg.cpMin = 0;
2188 tr.chrg.cpMax = nLen;
2189 tr.lpstrText = data;
2190 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2191 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2192 HeapFree( GetProcessHeap(), 0, data );
2194 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2195 return 0;
2198 case ID_EDIT_CHARFORMAT:
2199 case ID_EDIT_DEFCHARFORMAT:
2201 CHARFORMAT2W cf;
2202 LRESULT i;
2203 ZeroMemory(&cf, sizeof(cf));
2204 cf.cbSize = sizeof(cf);
2205 cf.dwMask = 0;
2206 i = SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2207 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2208 return 0;
2211 case ID_EDIT_PARAFORMAT:
2213 PARAFORMAT2 pf;
2214 ZeroMemory(&pf, sizeof(pf));
2215 pf.cbSize = sizeof(pf);
2216 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2217 return 0;
2220 case ID_EDIT_SELECTIONINFO:
2222 CHARRANGE range = {0, -1};
2223 char buf[128];
2224 WCHAR *data = NULL;
2226 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2227 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2228 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2229 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2230 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2231 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2232 HeapFree( GetProcessHeap(), 0, data);
2233 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2234 return 0;
2237 case ID_EDIT_READONLY:
2239 LONG nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2240 if (nStyle & ES_READONLY)
2241 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2242 else
2243 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2244 return 0;
2247 case ID_EDIT_MODIFIED:
2248 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2249 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2250 else
2251 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2252 return 0;
2254 case ID_EDIT_UNDO:
2255 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2256 return 0;
2258 case ID_EDIT_REDO:
2259 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2260 return 0;
2262 case ID_BULLET:
2264 PARAFORMAT pf;
2266 pf.cbSize = sizeof(pf);
2267 pf.dwMask = PFM_NUMBERING;
2268 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2270 pf.dwMask |= PFM_OFFSET;
2272 if(pf.wNumbering == PFN_BULLET)
2274 pf.wNumbering = 0;
2275 pf.dxOffset = 0;
2276 } else
2278 pf.wNumbering = PFN_BULLET;
2279 pf.dxOffset = 720;
2282 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2284 break;
2286 case ID_ALIGN_LEFT:
2287 case ID_ALIGN_CENTER:
2288 case ID_ALIGN_RIGHT:
2290 PARAFORMAT2 pf;
2292 pf.cbSize = sizeof(pf);
2293 pf.dwMask = PFM_ALIGNMENT;
2294 switch(LOWORD(wParam)) {
2295 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2296 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2297 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2299 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2300 break;
2303 case ID_BACK_1:
2304 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2305 break;
2307 case ID_BACK_2:
2308 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2309 break;
2311 case ID_TOGGLE_TOOLBAR:
2312 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2313 update_window();
2314 break;
2316 case ID_TOGGLE_FORMATBAR:
2317 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2318 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2319 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2320 update_window();
2321 break;
2323 case ID_TOGGLE_STATUSBAR:
2324 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2325 update_window();
2326 break;
2328 case ID_TOGGLE_RULER:
2329 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2330 update_window();
2331 break;
2333 case ID_DATETIME:
2334 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2335 break;
2337 case ID_PARAFORMAT:
2338 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2339 break;
2341 case ID_TABSTOPS:
2342 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, tabstops_proc);
2343 break;
2345 case ID_ABOUT:
2346 dialog_about();
2347 break;
2349 case ID_VIEWPROPERTIES:
2350 dialog_viewproperties();
2351 break;
2353 case IDC_FONTLIST:
2354 if (HIWORD(wParam) == CBN_SELENDOK)
2356 WCHAR buffer[LF_FACESIZE];
2357 HWND hwndFontList = (HWND)lParam;
2358 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2359 on_fontlist_modified(buffer);
2361 break;
2363 case IDC_SIZELIST:
2364 if (HIWORD(wParam) == CBN_SELENDOK)
2366 WCHAR buffer[MAX_STRING_LEN+1];
2367 HWND hwndSizeList = (HWND)lParam;
2368 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2369 on_sizelist_modified(hwndSizeList, buffer);
2371 break;
2373 default:
2374 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2375 break;
2377 return 0;
2380 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2382 HMENU hMenu = (HMENU)wParam;
2383 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2384 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2385 PARAFORMAT pf;
2386 int nAlignment = -1;
2387 int selFrom, selTo;
2388 GETTEXTLENGTHEX gt;
2389 LRESULT textLength;
2390 MENUITEMINFOW mi;
2392 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2393 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2394 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2396 pf.cbSize = sizeof(PARAFORMAT);
2397 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2398 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2399 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
2400 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2401 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
2402 if (pf.dwMask & PFM_ALIGNMENT)
2403 nAlignment = pf.wAlignment;
2404 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
2405 MF_CHECKED : MF_UNCHECKED);
2406 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
2407 MF_CHECKED : MF_UNCHECKED);
2408 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
2409 MF_CHECKED : MF_UNCHECKED);
2410 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
2411 MF_CHECKED : MF_UNCHECKED));
2412 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
2413 MF_ENABLED : MF_GRAYED);
2414 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
2415 MF_ENABLED : MF_GRAYED);
2417 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
2418 MF_CHECKED : MF_UNCHECKED);
2420 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
2421 MF_CHECKED : MF_UNCHECKED);
2423 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
2424 MF_CHECKED : MF_UNCHECKED);
2426 CheckMenuItem(hMenu, ID_TOGGLE_RULER, MF_BYCOMMAND|(is_bar_visible(BANDID_RULER)) ? MF_CHECKED : MF_UNCHECKED);
2428 gt.flags = GTL_NUMCHARS;
2429 gt.codepage = 1200;
2430 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2431 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2433 mi.cbSize = sizeof(mi);
2434 mi.fMask = MIIM_DATA;
2436 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2438 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
2439 MF_ENABLED : MF_GRAYED));
2441 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2443 return 0;
2446 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2448 int nStatusSize = 0;
2449 RECT rc;
2450 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2451 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2452 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2453 HWND hRulerWnd = GetDlgItem(hWnd, IDC_RULER);
2454 int rebarHeight = 0;
2456 if (hwndStatusBar)
2458 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2459 if (IsWindowVisible(hwndStatusBar))
2461 GetClientRect(hwndStatusBar, &rc);
2462 nStatusSize = rc.bottom - rc.top;
2463 } else
2465 nStatusSize = 0;
2468 if (hwndReBar)
2470 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2472 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2474 if (hwndEditor)
2476 GetClientRect(hWnd, &rc);
2477 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2480 redraw_ruler(hRulerWnd);
2482 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2485 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2487 if(msg == ID_FINDMSGSTRING)
2488 return handle_findmsg((LPFINDREPLACEW)lParam);
2490 switch(msg)
2492 case WM_CREATE:
2493 return OnCreate( hWnd );
2495 case WM_USER:
2496 return OnUser( hWnd );
2498 case WM_NOTIFY:
2499 return OnNotify( hWnd, lParam );
2501 case WM_COMMAND:
2502 if(preview_isactive())
2504 return preview_command( hWnd, wParam );
2507 return OnCommand( hWnd, wParam, lParam );
2509 case WM_DESTROY:
2510 PostQuitMessage(0);
2511 break;
2513 case WM_CLOSE:
2514 if(preview_isactive())
2516 preview_exit(hWnd);
2517 } else if(prompt_save_changes())
2519 registry_set_options(hMainWnd);
2520 registry_set_formatopts_all(barState);
2521 PostQuitMessage(0);
2523 break;
2525 case WM_ACTIVATE:
2526 if (LOWORD(wParam))
2527 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2528 return 0;
2530 case WM_INITMENUPOPUP:
2531 return OnInitPopupMenu( hWnd, wParam );
2533 case WM_SIZE:
2534 return OnSize( hWnd, wParam, lParam );
2536 case WM_CONTEXTMENU:
2537 if((HWND)wParam == hEditorWnd)
2538 return context_menu(lParam);
2539 else
2540 return DefWindowProcW(hWnd, msg, wParam, lParam);
2542 case WM_DROPFILES:
2544 WCHAR file[MAX_PATH];
2545 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2546 DragFinish((HDROP)wParam);
2548 if(prompt_save_changes())
2549 DoOpenFile(file);
2551 break;
2552 case WM_PAINT:
2553 if(preview_isactive())
2554 return print_preview(hWnd);
2555 else
2556 return DefWindowProcW(hWnd, msg, wParam, lParam);
2558 default:
2559 return DefWindowProcW(hWnd, msg, wParam, lParam);
2562 return 0;
2565 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2567 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2568 HACCEL hAccel;
2569 WNDCLASSW wc;
2570 MSG msg;
2571 RECT rc;
2572 UINT_PTR hPrevRulerProc;
2573 HWND hRulerWnd;
2574 POINTL EditPoint;
2575 DWORD bMaximized;
2576 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2577 'T','A','B','L','E','\0'};
2579 InitCommonControlsEx(&classes);
2581 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2583 wc.style = CS_HREDRAW | CS_VREDRAW;
2584 wc.lpfnWndProc = WndProc;
2585 wc.cbClsExtra = 0;
2586 wc.cbWndExtra = 4;
2587 wc.hInstance = hInstance;
2588 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2589 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2590 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2591 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2592 wc.lpszClassName = wszMainWndClass;
2593 RegisterClassW(&wc);
2595 registry_read_winrect(&rc);
2596 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2597 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2598 registry_read_maximized(&bMaximized);
2599 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2600 && bMaximized)
2601 nCmdShow = SW_SHOWMAXIMIZED;
2602 ShowWindow(hMainWnd, nCmdShow);
2604 set_caption(NULL);
2605 set_bar_states();
2606 set_fileformat(SF_RTF);
2607 hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
2608 get_default_printer_opts();
2609 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2611 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2612 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2613 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2614 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2616 HandleCommandLine(GetCommandLineW());
2618 while(GetMessageW(&msg,0,0,0))
2620 if (IsDialogMessage(hFindWnd, &msg))
2621 continue;
2623 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2624 continue;
2625 TranslateMessage(&msg);
2626 DispatchMessageW(&msg);
2627 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2628 SendMessageW(hMainWnd, WM_USER, 0, 0);
2631 return 0;