push 6ad4febc802779d39881ae7cccabf16fa19e6741
[wine/hacks.git] / programs / wordpad / wordpad.c
blob754ae50a9cdd2bacd012f8c7f715b7127aa003eb
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 wszRichEditClass[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
56 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
58 static const WCHAR stringFormat[] = {'%','2','d','\0'};
60 static HWND hMainWnd;
61 static HWND hEditorWnd;
62 static HWND hFindWnd;
63 static HMENU hPopupMenu;
65 static UINT ID_FINDMSGSTRING;
67 static DWORD wordWrap[2];
68 static DWORD barState[2];
69 static WPARAM fileFormat = SF_RTF;
71 static WCHAR wszFileName[MAX_PATH];
72 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
73 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
74 static WCHAR wszSaveChanges[MAX_STRING_LEN];
75 static WCHAR units_cmW[MAX_STRING_LEN];
77 static char units_cmA[MAX_STRING_LEN];
79 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
81 /* Load string resources */
82 static void DoLoadStrings(void)
84 LPWSTR p = wszFilter;
85 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
86 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
87 static const WCHAR files_all[] = {'*','.','*','\0'};
89 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
91 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
92 p += lstrlenW(p) + 1;
93 lstrcpyW(p, files_rtf);
94 p += lstrlenW(p) + 1;
95 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
96 p += lstrlenW(p) + 1;
97 lstrcpyW(p, files_txt);
98 p += lstrlenW(p) + 1;
99 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
100 p += lstrlenW(p) + 1;
101 lstrcpyW(p, files_txt);
102 p += lstrlenW(p) + 1;
103 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
104 p += lstrlenW(p) + 1;
105 lstrcpyW(p, files_all);
106 p += lstrlenW(p) + 1;
107 *p = '\0';
109 p = wszDefaultFileName;
110 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
112 p = wszSaveChanges;
113 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
115 LoadStringA(hInstance, STRING_UNITS_CM, units_cmA, MAX_STRING_LEN);
116 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
119 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
121 TBBUTTON button;
123 ZeroMemory(&button, sizeof(button));
124 button.iBitmap = nImage;
125 button.idCommand = nCommand;
126 button.fsState = TBSTATE_ENABLED;
127 button.fsStyle = TBSTYLE_BUTTON;
128 button.dwData = 0;
129 button.iString = -1;
130 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
133 static void AddSeparator(HWND hwndToolBar)
135 TBBUTTON button;
137 ZeroMemory(&button, sizeof(button));
138 button.iBitmap = -1;
139 button.idCommand = 0;
140 button.fsState = 0;
141 button.fsStyle = TBSTYLE_SEP;
142 button.dwData = 0;
143 button.iString = -1;
144 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
147 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
149 HANDLE hFile = (HANDLE)cookie;
150 DWORD read;
152 if(!ReadFile(hFile, buffer, cb, &read, 0))
153 return 1;
155 *pcb = read;
157 return 0;
160 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
162 DWORD written;
163 int ret;
164 HANDLE hFile = (HANDLE)cookie;
166 ret = WriteFile(hFile, buffer, cb, &written, 0);
168 if(!ret || (cb != written))
169 return 1;
171 *pcb = cb;
173 return 0;
176 LPWSTR file_basename(LPWSTR path)
178 LPWSTR pos = path + lstrlenW(path);
180 while(pos > path)
182 if(*pos == '\\' || *pos == '/')
184 pos++;
185 break;
187 pos--;
189 return pos;
192 static void set_caption(LPCWSTR wszNewFileName)
194 static const WCHAR wszSeparator[] = {' ','-',' '};
195 WCHAR *wszCaption;
196 SIZE_T length = 0;
198 if(!wszNewFileName)
199 wszNewFileName = wszDefaultFileName;
200 else
201 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
203 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
204 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
206 if(!wszCaption)
207 return;
209 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
210 length += lstrlenW(wszNewFileName);
211 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
212 length += sizeof(wszSeparator) / sizeof(WCHAR);
213 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
215 SetWindowTextW(hMainWnd, wszCaption);
217 HeapFree(GetProcessHeap(), 0, wszCaption);
220 static BOOL validate_endptr(LPCSTR endptr, BOOL units)
222 if(!endptr)
223 return FALSE;
224 if(!*endptr)
225 return TRUE;
227 while(*endptr == ' ')
228 endptr++;
230 if(!units)
231 return *endptr == '\0';
233 /* FIXME: Allow other units and convert between them */
234 if(!lstrcmpA(endptr, units_cmA))
235 endptr += 2;
237 return *endptr == '\0';
240 static BOOL number_from_string(LPCWSTR string, float *num, BOOL units)
242 double ret;
243 char buffer[MAX_STRING_LEN];
244 char *endptr = buffer;
246 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN, NULL, NULL);
247 *num = 0;
248 errno = 0;
249 ret = strtod(buffer, &endptr);
251 if((ret == 0 && errno != 0) || endptr == buffer || !validate_endptr(endptr, units))
253 return FALSE;
254 } else
256 *num = (float)ret;
257 return TRUE;
261 static void set_size(float size)
263 CHARFORMAT2W fmt;
265 ZeroMemory(&fmt, sizeof(fmt));
266 fmt.cbSize = sizeof(fmt);
267 fmt.dwMask = CFM_SIZE;
268 fmt.yHeight = (int)(size * 20.0);
269 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
272 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
274 WCHAR sizeBuffer[MAX_STRING_LEN];
275 CHARFORMAT2W format;
277 ZeroMemory(&format, sizeof(format));
278 format.cbSize = sizeof(format);
279 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
281 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
282 if(lstrcmpW(sizeBuffer, wszNewFontSize))
284 float size = 0;
285 if(number_from_string((LPCWSTR) wszNewFontSize, &size, FALSE)
286 && size > 0)
288 set_size(size);
289 } else
291 SetWindowTextW(hwndSizeList, sizeBuffer);
292 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
293 wszAppTitle, MB_OK | MB_ICONINFORMATION);
298 static void add_size(HWND hSizeListWnd, unsigned size)
300 WCHAR buffer[3];
301 COMBOBOXEXITEMW cbItem;
302 cbItem.mask = CBEIF_TEXT;
303 cbItem.iItem = -1;
305 wsprintfW(buffer, stringFormat, size);
306 cbItem.pszText = (LPWSTR)buffer;
307 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
310 static void populate_size_list(HWND hSizeListWnd)
312 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
313 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
314 COMBOBOXEXITEMW cbFontItem;
315 CHARFORMAT2W fmt;
316 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
317 HDC hdc = GetDC(hMainWnd);
318 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
319 WCHAR buffer[3];
320 size_t i;
321 DWORD fontStyle;
323 ZeroMemory(&fmt, sizeof(fmt));
324 fmt.cbSize = sizeof(fmt);
325 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
327 cbFontItem.mask = CBEIF_LPARAM;
328 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
329 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
331 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
333 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
335 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
337 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
338 GetDeviceCaps(hdc, LOGPIXELSY)));
339 } else
341 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
342 add_size(hSizeListWnd, choices[i]);
345 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
346 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
349 static void update_size_list(void)
351 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
352 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
353 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
354 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
355 CHARFORMAT2W fmt;
357 ZeroMemory(&fmt, sizeof(fmt));
358 fmt.cbSize = sizeof(fmt);
360 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
362 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
363 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
365 if(lstrcmpW(fontSize, sizeBuffer))
366 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
369 static void update_font_list(void)
371 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
372 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
373 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
374 WCHAR fontName[MAX_STRING_LEN];
375 CHARFORMAT2W fmt;
377 ZeroMemory(&fmt, sizeof(fmt));
378 fmt.cbSize = sizeof(fmt);
380 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
381 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
383 if(lstrcmpW(fontName, fmt.szFaceName))
385 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
386 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
387 } else
389 update_size_list();
393 static void clear_formatting(void)
395 PARAFORMAT2 pf;
397 pf.cbSize = sizeof(pf);
398 pf.dwMask = PFM_ALIGNMENT;
399 pf.wAlignment = PFA_LEFT;
400 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
403 static int fileformat_number(WPARAM format)
405 int number = 0;
407 if(format == SF_TEXT)
409 number = 1;
410 } else if (format == (SF_TEXT | SF_UNICODE))
412 number = 2;
414 return number;
417 static WPARAM fileformat_flags(int format)
419 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
421 return flags[format];
424 static void set_font(LPCWSTR wszFaceName)
426 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
427 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
428 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
429 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
430 CHARFORMAT2W fmt;
432 ZeroMemory(&fmt, sizeof(fmt));
434 fmt.cbSize = sizeof(fmt);
435 fmt.dwMask = CFM_FACE;
437 lstrcpyW(fmt.szFaceName, wszFaceName);
439 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
441 populate_size_list(hSizeListWnd);
443 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
446 static void set_default_font(void)
448 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
449 'R','o','m','a','n',0};
450 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
451 CHARFORMAT2W fmt;
452 LPCWSTR font;
454 ZeroMemory(&fmt, sizeof(fmt));
456 fmt.cbSize = sizeof(fmt);
457 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
458 fmt.dwEffects = 0;
460 if(fileFormat & SF_RTF)
461 font = richTextFont;
462 else
463 font = plainTextFont;
465 lstrcpyW(fmt.szFaceName, font);
467 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
470 static void on_fontlist_modified(LPWSTR wszNewFaceName)
472 CHARFORMAT2W format;
473 ZeroMemory(&format, sizeof(format));
474 format.cbSize = sizeof(format);
475 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
477 if(lstrcmpW(format.szFaceName, wszNewFaceName))
478 set_font((LPCWSTR) wszNewFaceName);
481 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
483 COMBOBOXEXITEMW cbItem;
484 WCHAR buffer[MAX_PATH];
485 int fontHeight = 0;
487 cbItem.mask = CBEIF_TEXT;
488 cbItem.pszText = buffer;
489 cbItem.cchTextMax = MAX_STRING_LEN;
490 cbItem.iItem = 0;
492 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
494 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
495 cbItem.iItem++;
496 else
497 break;
499 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
500 lstrcpyW( cbItem.pszText, fontName );
502 cbItem.mask |= CBEIF_LPARAM;
503 if(fontType & RASTER_FONTTYPE)
504 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
506 cbItem.lParam = MAKELONG(fontType,fontHeight);
507 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
508 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
511 static void dialog_choose_font(void)
513 CHOOSEFONTW cf;
514 LOGFONTW lf;
515 CHARFORMAT2W fmt;
516 HDC hDC = GetDC(hMainWnd);
518 ZeroMemory(&cf, sizeof(cf));
519 cf.lStructSize = sizeof(cf);
520 cf.hwndOwner = hMainWnd;
521 cf.lpLogFont = &lf;
522 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
524 ZeroMemory(&fmt, sizeof(fmt));
525 fmt.cbSize = sizeof(fmt);
527 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
528 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
529 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) ? TRUE : FALSE;
530 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
531 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE;
532 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE;
533 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
534 cf.rgbColors = fmt.crTextColor;
536 if(ChooseFontW(&cf))
538 ZeroMemory(&fmt, sizeof(fmt));
539 fmt.cbSize = sizeof(fmt);
540 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
541 fmt.yHeight = cf.iPointSize * 2;
543 if(cf.nFontType & BOLD_FONTTYPE)
544 fmt.dwEffects |= CFE_BOLD;
545 if(cf.nFontType & ITALIC_FONTTYPE)
546 fmt.dwEffects |= CFE_ITALIC;
547 if(cf.lpLogFont->lfUnderline == TRUE)
548 fmt.dwEffects |= CFE_UNDERLINE;
549 if(cf.lpLogFont->lfStrikeOut == TRUE)
550 fmt.dwEffects |= CFE_STRIKEOUT;
552 fmt.crTextColor = cf.rgbColors;
554 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
555 set_font(cf.lpLogFont->lfFaceName);
560 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
561 DWORD FontType, LPARAM lParam)
563 HWND hListWnd = (HWND) lParam;
565 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
568 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
571 return 1;
574 static void populate_font_list(HWND hListWnd)
576 HDC hdc = GetDC(hMainWnd);
577 LOGFONTW fontinfo;
578 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
579 CHARFORMAT2W fmt;
581 fontinfo.lfCharSet = DEFAULT_CHARSET;
582 *fontinfo.lfFaceName = '\0';
583 fontinfo.lfPitchAndFamily = 0;
585 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
586 (LPARAM)hListWnd, 0);
588 ZeroMemory(&fmt, sizeof(fmt));
589 fmt.cbSize = sizeof(fmt);
590 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
591 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
594 static void update_window(void)
596 RECT rect;
598 GetClientRect(hMainWnd, &rect);
600 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
603 static BOOL is_bar_visible(int bandId)
605 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
608 static void store_bar_state(int bandId, BOOL show)
610 int formatIndex = reg_formatindex(fileFormat);
612 if(show)
613 barState[formatIndex] |= (1 << bandId);
614 else
615 barState[formatIndex] &= ~(1 << bandId);
618 static void set_toolbar_state(int bandId, BOOL show)
620 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
622 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
624 if(bandId == BANDID_TOOLBAR)
626 REBARBANDINFOW rbbinfo;
627 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
629 rbbinfo.cbSize = sizeof(rbbinfo);
630 rbbinfo.fMask = RBBIM_STYLE;
632 SendMessageW(hwndReBar, RB_GETBANDINFO, index, (LPARAM)&rbbinfo);
634 if(!show)
635 rbbinfo.fStyle &= ~RBBS_BREAK;
636 else
637 rbbinfo.fStyle |= RBBS_BREAK;
639 SendMessageW(hwndReBar, RB_SETBANDINFO, index, (LPARAM)&rbbinfo);
642 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
643 store_bar_state(bandId, show);
646 static void set_statusbar_state(BOOL show)
648 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
650 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
651 store_bar_state(BANDID_STATUSBAR, show);
654 static void set_bar_states(void)
656 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
657 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
658 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
659 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
660 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
661 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
663 update_window();
666 static void preview_exit(HWND hMainWnd)
668 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
669 HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_MAINMENU));
670 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
672 set_bar_states();
673 ShowWindow(hEditorWnd, TRUE);
675 close_preview(hMainWnd);
677 SetMenu(hMainWnd, hMenu);
678 registry_read_filelist(hMainWnd);
680 update_window();
683 static void set_fileformat(WPARAM format)
685 HICON hIcon;
686 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
687 fileFormat = format;
689 if(format & SF_TEXT)
690 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_TXT));
691 else
692 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_RTF));
694 SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
696 set_bar_states();
697 set_default_font();
698 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
701 static void ShowOpenError(DWORD Code)
703 LPWSTR Message;
705 switch(Code)
707 case ERROR_ACCESS_DENIED:
708 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
709 break;
711 default:
712 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
714 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
717 static void DoOpenFile(LPCWSTR szOpenFileName)
719 HANDLE hFile;
720 EDITSTREAM es;
721 char fileStart[5];
722 DWORD readOut;
723 WPARAM format = SF_TEXT;
725 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
726 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
727 if (hFile == INVALID_HANDLE_VALUE)
729 ShowOpenError(GetLastError());
730 return;
733 ReadFile(hFile, fileStart, 5, &readOut, NULL);
734 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
736 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
738 format = SF_TEXT | SF_UNICODE;
739 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
740 } else if(readOut >= 5)
742 static const char header[] = "{\\rtf";
743 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
745 if(!memcmp(header, fileStart, 5))
746 format = SF_RTF;
747 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
749 CloseHandle(hFile);
750 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED), wszAppTitle,
751 MB_OK | MB_ICONEXCLAMATION);
752 return;
756 es.dwCookie = (DWORD_PTR)hFile;
757 es.pfnCallback = stream_in;
759 clear_formatting();
760 set_fileformat(format);
761 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
763 CloseHandle(hFile);
765 SetFocus(hEditorWnd);
767 set_caption(szOpenFileName);
769 lstrcpyW(wszFileName, szOpenFileName);
770 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
771 registry_set_filelist(szOpenFileName, hMainWnd);
772 update_font_list();
775 static void ShowWriteError(DWORD Code)
777 LPWSTR Message;
779 switch(Code)
781 case ERROR_ACCESS_DENIED:
782 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
783 break;
785 default:
786 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
788 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
791 static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
793 HANDLE hFile;
794 EDITSTREAM stream;
795 LRESULT ret;
797 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
798 FILE_ATTRIBUTE_NORMAL, NULL);
800 if(hFile == INVALID_HANDLE_VALUE)
802 ShowWriteError(GetLastError());
803 return;
806 if(format == (SF_TEXT | SF_UNICODE))
808 static const BYTE unicode[] = {0xff,0xfe};
809 DWORD writeOut;
810 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
812 if(writeOut != sizeof(unicode))
814 CloseHandle(hFile);
815 return;
819 stream.dwCookie = (DWORD_PTR)hFile;
820 stream.pfnCallback = stream_out;
822 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
824 CloseHandle(hFile);
826 SetFocus(hEditorWnd);
828 if(!ret)
830 GETTEXTLENGTHEX gt;
831 gt.flags = GTL_DEFAULT;
832 gt.codepage = 1200;
834 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
835 return;
838 lstrcpyW(wszFileName, wszSaveFileName);
839 set_caption(wszFileName);
840 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
841 set_fileformat(format);
844 static void DialogSaveFile(void)
846 OPENFILENAMEW sfn;
848 WCHAR wszFile[MAX_PATH] = {'\0'};
849 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
851 ZeroMemory(&sfn, sizeof(sfn));
853 sfn.lStructSize = sizeof(sfn);
854 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
855 sfn.hwndOwner = hMainWnd;
856 sfn.lpstrFilter = wszFilter;
857 sfn.lpstrFile = wszFile;
858 sfn.nMaxFile = MAX_PATH;
859 sfn.lpstrDefExt = wszDefExt;
860 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
862 while(GetSaveFileNameW(&sfn))
864 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
866 if(MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
867 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
869 continue;
870 } else
872 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
873 break;
875 } else
877 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
878 break;
883 static BOOL prompt_save_changes(void)
885 if(!wszFileName[0])
887 GETTEXTLENGTHEX gt;
888 gt.flags = GTL_NUMCHARS;
889 gt.codepage = 1200;
890 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
891 return TRUE;
894 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
896 return TRUE;
897 } else
899 LPWSTR displayFileName;
900 WCHAR *text;
901 int ret;
903 if(!wszFileName[0])
904 displayFileName = wszDefaultFileName;
905 else
906 displayFileName = file_basename(wszFileName);
908 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
909 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
911 if(!text)
912 return FALSE;
914 wsprintfW(text, wszSaveChanges, displayFileName);
916 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
918 HeapFree(GetProcessHeap(), 0, text);
920 switch(ret)
922 case IDNO:
923 return TRUE;
925 case IDYES:
926 if(wszFileName[0])
927 DoSaveFile(wszFileName, fileFormat);
928 else
929 DialogSaveFile();
930 return TRUE;
932 default:
933 return FALSE;
938 static void DialogOpenFile(void)
940 OPENFILENAMEW ofn;
942 WCHAR wszFile[MAX_PATH] = {'\0'};
943 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
945 ZeroMemory(&ofn, sizeof(ofn));
947 ofn.lStructSize = sizeof(ofn);
948 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
949 ofn.hwndOwner = hMainWnd;
950 ofn.lpstrFilter = wszFilter;
951 ofn.lpstrFile = wszFile;
952 ofn.nMaxFile = MAX_PATH;
953 ofn.lpstrDefExt = wszDefExt;
954 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
956 if(GetOpenFileNameW(&ofn))
958 if(prompt_save_changes())
959 DoOpenFile(ofn.lpstrFile);
963 static void dialog_about(void)
965 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
966 HICON icon = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
967 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
970 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
972 switch(message)
974 case WM_INITDIALOG:
976 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
977 int wrap = -1;
978 char id[4];
979 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
981 sprintf(id, "%d\n", (int)ps->lParam);
982 SetWindowTextA(hIdWnd, id);
983 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
984 wrap = IDC_PAGEFMT_WN;
985 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
986 wrap = IDC_PAGEFMT_WW;
987 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
988 wrap = IDC_PAGEFMT_WM;
990 if(wrap != -1)
991 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
992 IDC_PAGEFMT_WM, wrap);
994 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
995 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
996 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
997 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
998 if(barState[ps->lParam] & (1 << BANDID_RULER))
999 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1000 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1001 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1003 break;
1005 case WM_COMMAND:
1006 switch(LOWORD(wParam))
1008 case IDC_PAGEFMT_WN:
1009 case IDC_PAGEFMT_WW:
1010 case IDC_PAGEFMT_WM:
1011 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1012 LOWORD(wParam));
1013 break;
1015 case IDC_PAGEFMT_TB:
1016 case IDC_PAGEFMT_FB:
1017 case IDC_PAGEFMT_RU:
1018 case IDC_PAGEFMT_SB:
1019 CheckDlgButton(hWnd, LOWORD(wParam),
1020 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1021 break;
1023 break;
1024 case WM_NOTIFY:
1026 LPNMHDR header = (LPNMHDR)lParam;
1027 if(header->code == PSN_APPLY)
1029 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1030 char sid[4];
1031 int id;
1033 GetWindowTextA(hIdWnd, sid, 4);
1034 id = atoi(sid);
1035 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1036 wordWrap[id] = ID_WORDWRAP_NONE;
1037 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1038 wordWrap[id] = ID_WORDWRAP_WINDOW;
1039 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1040 wordWrap[id] = ID_WORDWRAP_MARGIN;
1042 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1043 barState[id] |= (1 << BANDID_TOOLBAR);
1044 else
1045 barState[id] &= ~(1 << BANDID_TOOLBAR);
1047 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1048 barState[id] |= (1 << BANDID_FORMATBAR);
1049 else
1050 barState[id] &= ~(1 << BANDID_FORMATBAR);
1052 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1053 barState[id] |= (1 << BANDID_RULER);
1054 else
1055 barState[id] &= ~(1 << BANDID_RULER);
1057 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1058 barState[id] |= (1 << BANDID_STATUSBAR);
1059 else
1060 barState[id] &= ~(1 << BANDID_STATUSBAR);
1063 break;
1065 return FALSE;
1068 static void dialog_viewproperties(void)
1070 PROPSHEETPAGEW psp[2];
1071 PROPSHEETHEADERW psh;
1072 size_t i;
1073 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1074 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1076 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1077 psp[0].dwFlags = PSP_USETITLE;
1078 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1079 psp[0].pfnDlgProc = formatopts_proc;
1080 psp[0].hInstance = hInstance;
1081 psp[0].lParam = reg_formatindex(SF_TEXT);
1082 psp[0].pfnCallback = NULL;
1083 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1084 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1086 psp[i].dwSize = psp[0].dwSize;
1087 psp[i].dwFlags = psp[0].dwFlags;
1088 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1089 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1090 psp[i].hInstance = psp[0].hInstance;
1091 psp[i].lParam = reg_formatindex(SF_RTF);
1092 psp[i].pfnCallback = psp[0].pfnCallback;
1093 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1096 psh.dwSize = sizeof(psh);
1097 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1098 psh.hwndParent = hMainWnd;
1099 psh.hInstance = hInstance;
1100 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1101 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1102 U3(psh).ppsp = ppsp;
1103 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1105 if(fileFormat & SF_RTF)
1106 U2(psh).nStartPage = 1;
1107 else
1108 U2(psh).nStartPage = 0;
1109 PropertySheetW(&psh);
1110 set_bar_states();
1111 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1114 static void HandleCommandLine(LPWSTR cmdline)
1116 WCHAR delimiter;
1117 int opt_print = 0;
1119 /* skip white space */
1120 while (*cmdline == ' ') cmdline++;
1122 /* skip executable name */
1123 delimiter = (*cmdline == '"' ? '"' : ' ');
1125 if (*cmdline == delimiter) cmdline++;
1126 while (*cmdline && *cmdline != delimiter) cmdline++;
1127 if (*cmdline == delimiter) cmdline++;
1129 while (*cmdline)
1131 while (isspace(*cmdline)) cmdline++;
1133 if (*cmdline == '-' || *cmdline == '/')
1135 if (!cmdline[2] || isspace(cmdline[2]))
1137 switch (cmdline[1])
1139 case 'P':
1140 case 'p':
1141 opt_print = 1;
1142 cmdline += 2;
1143 continue;
1146 /* a filename starting by / */
1148 break;
1151 if (*cmdline)
1153 /* file name is passed on the command line */
1154 if (cmdline[0] == '"')
1156 cmdline++;
1157 cmdline[lstrlenW(cmdline) - 1] = 0;
1159 DoOpenFile(cmdline);
1160 InvalidateRect(hMainWnd, NULL, FALSE);
1163 if (opt_print)
1164 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1167 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1169 if(pFr->Flags & FR_DIALOGTERM)
1171 hFindWnd = 0;
1172 pFr->Flags = FR_FINDNEXT;
1173 return 0;
1176 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1178 DWORD flags = FR_DOWN;
1179 FINDTEXTW ft;
1180 static CHARRANGE cr;
1181 LRESULT end, ret;
1182 GETTEXTLENGTHEX gt;
1183 LRESULT length;
1184 int startPos;
1185 HMENU hMenu = GetMenu(hMainWnd);
1186 MENUITEMINFOW mi;
1188 mi.cbSize = sizeof(mi);
1189 mi.fMask = MIIM_DATA;
1190 mi.dwItemData = 1;
1191 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1193 gt.flags = GTL_NUMCHARS;
1194 gt.codepage = 1200;
1196 length = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
1198 if(pFr->lCustData == -1)
1200 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&startPos, (LPARAM)&end);
1201 cr.cpMin = startPos;
1202 pFr->lCustData = startPos;
1203 cr.cpMax = length;
1204 if(cr.cpMin == length)
1205 cr.cpMin = 0;
1206 } else
1208 startPos = pFr->lCustData;
1211 if(cr.cpMax > length)
1213 startPos = 0;
1214 cr.cpMin = 0;
1215 cr.cpMax = length;
1218 ft.chrg = cr;
1219 ft.lpstrText = pFr->lpstrFindWhat;
1221 if(pFr->Flags & FR_MATCHCASE)
1222 flags |= FR_MATCHCASE;
1223 if(pFr->Flags & FR_WHOLEWORD)
1224 flags |= FR_WHOLEWORD;
1226 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1228 if(ret == -1)
1230 if(cr.cpMax == length && cr.cpMax != startPos)
1232 ft.chrg.cpMin = cr.cpMin = 0;
1233 ft.chrg.cpMax = cr.cpMax = startPos;
1235 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1239 if(ret == -1)
1241 pFr->lCustData = -1;
1242 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED), wszAppTitle,
1243 MB_OK | MB_ICONASTERISK);
1244 } else
1246 end = ret + lstrlenW(pFr->lpstrFindWhat);
1247 cr.cpMin = end;
1248 SendMessageW(hEditorWnd, EM_SETSEL, (WPARAM)ret, (LPARAM)end);
1249 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1251 if(pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1252 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1254 if(pFr->Flags & FR_REPLACEALL)
1255 handle_findmsg(pFr);
1259 return 0;
1262 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1264 static WCHAR findBuffer[MAX_STRING_LEN];
1266 ZeroMemory(fr, sizeof(FINDREPLACEW));
1267 fr->lStructSize = sizeof(FINDREPLACEW);
1268 fr->hwndOwner = hMainWnd;
1269 fr->Flags = FR_HIDEUPDOWN;
1270 fr->lpstrFindWhat = findBuffer;
1271 fr->lCustData = -1;
1272 fr->wFindWhatLen = MAX_STRING_LEN*sizeof(WCHAR);
1274 if(replace)
1275 hFindWnd = ReplaceTextW(fr);
1276 else
1277 hFindWnd = FindTextW(fr);
1280 static int current_units_to_twips(float number)
1282 int twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1283 return twips;
1286 static void append_current_units(LPWSTR buffer)
1288 static const WCHAR space[] = {' ', 0};
1289 lstrcatW(buffer, space);
1290 lstrcatW(buffer, units_cmW);
1293 static void number_with_units(LPWSTR buffer, int number)
1295 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1296 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1298 wsprintfW(buffer, fmt, converted, units_cmW);
1301 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1303 COMBOBOXEXITEMW cbItem;
1304 COMBOBOXINFO cbInfo;
1305 HWND hCombo, hList;
1306 int idx, result;
1308 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1309 if (!hCombo)
1310 return FALSE;
1311 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1312 result = SendMessage(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1313 if (!result)
1314 return FALSE;
1315 hList = cbInfo.hwndList;
1316 idx = SendMessage(hList, LB_GETCURSEL, 0, 0);
1317 if (idx < 0)
1318 return FALSE;
1320 ZeroMemory(&cbItem, sizeof(cbItem));
1321 cbItem.mask = CBEIF_TEXT;
1322 cbItem.iItem = idx;
1323 cbItem.pszText = wszBuffer;
1324 cbItem.cchTextMax = bufferLength-1;
1325 result = SendMessageW(hComboEx, CBEM_GETITEM, 0, (LPARAM)&cbItem);
1327 return result != 0;
1330 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1332 switch(message)
1334 case WM_INITDIALOG:
1336 WCHAR buffer[MAX_STRING_LEN];
1337 SYSTEMTIME st;
1338 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1339 GetLocalTime(&st);
1341 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1342 MAX_STRING_LEN);
1343 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1344 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1345 MAX_STRING_LEN);
1346 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1347 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1348 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1350 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1352 break;
1354 case WM_COMMAND:
1355 switch(LOWORD(wParam))
1357 case IDOK:
1359 LRESULT index;
1360 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1362 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1364 if(index != LB_ERR)
1366 WCHAR buffer[MAX_STRING_LEN];
1367 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1368 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1371 /* Fall through */
1373 case IDCANCEL:
1374 EndDialog(hWnd, wParam);
1375 return TRUE;
1378 return FALSE;
1381 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1383 switch(message)
1385 case WM_INITDIALOG:
1387 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1388 WCHAR buffer[MAX_STRING_LEN];
1389 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1391 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, (LPWSTR)buffer, MAX_STRING_LEN);
1392 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1393 LoadStringW(hInstance, STRING_NEWFILE_TXT, (LPWSTR)buffer, MAX_STRING_LEN);
1394 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1395 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, (LPWSTR)buffer, MAX_STRING_LEN);
1396 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1398 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1400 break;
1402 case WM_COMMAND:
1403 switch(LOWORD(wParam))
1405 case IDOK:
1407 LRESULT index;
1408 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1409 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1411 if(index != LB_ERR)
1412 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1414 return TRUE;
1416 case IDCANCEL:
1417 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1418 return TRUE;
1421 return FALSE;
1424 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1426 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1428 switch(message)
1430 case WM_INITDIALOG:
1432 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd,
1433 GWLP_HINSTANCE);
1434 WCHAR buffer[MAX_STRING_LEN];
1435 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1436 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1437 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1438 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1439 PARAFORMAT2 pf;
1440 int index = 0;
1442 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1443 MAX_STRING_LEN);
1444 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1445 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1446 MAX_STRING_LEN);
1447 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1448 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1449 MAX_STRING_LEN);
1450 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1452 pf.cbSize = sizeof(pf);
1453 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1454 PFM_STARTINDENT;
1455 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1457 if(pf.wAlignment == PFA_RIGHT)
1458 index ++;
1459 else if(pf.wAlignment == PFA_CENTER)
1460 index += 2;
1462 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1464 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1465 SetWindowTextW(hLeftWnd, buffer);
1466 number_with_units(buffer, pf.dxRightIndent);
1467 SetWindowTextW(hRightWnd, buffer);
1468 number_with_units(buffer, -pf.dxOffset);
1469 SetWindowTextW(hFirstWnd, buffer);
1471 break;
1473 case WM_COMMAND:
1474 switch(LOWORD(wParam))
1476 case IDOK:
1478 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1479 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1480 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1481 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1482 WCHAR buffer[MAX_STRING_LEN];
1483 int index;
1484 float num;
1485 int ret = 0;
1486 PARAFORMAT pf;
1488 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1489 pf.wAlignment = ALIGNMENT_VALUES[index];
1491 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1492 if(number_from_string(buffer, &num, TRUE))
1493 ret++;
1494 pf.dxOffset = current_units_to_twips(num);
1495 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1496 if(number_from_string(buffer, &num, TRUE))
1497 ret++;
1498 pf.dxRightIndent = current_units_to_twips(num);
1499 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1500 if(number_from_string(buffer, &num, TRUE))
1501 ret++;
1502 pf.dxStartIndent = current_units_to_twips(num);
1504 if(ret != 3)
1506 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1507 wszAppTitle, MB_OK | MB_ICONASTERISK);
1508 return FALSE;
1509 } else
1511 if (pf.dxOffset + pf.dxStartIndent < 0
1512 && pf.dxStartIndent < 0)
1514 /* The first line is before the left edge, so
1515 * make sure it is at the left edge. */
1516 pf.dxOffset = -pf.dxStartIndent;
1517 } else if (pf.dxOffset < 0) {
1518 /* The second and following lines are before
1519 * the left edge, so set it to be at the left
1520 * edge, and adjust the first line since it
1521 * is relative to it. */
1522 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1523 pf.dxOffset = 0;
1525 /* Internally the dxStartIndent is the absolute
1526 * offset for the first line and dxOffset is
1527 * to it value as opposed how it is displayed with
1528 * the first line being the relative value.
1529 * These two lines make the adjustments. */
1530 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1531 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1533 pf.cbSize = sizeof(pf);
1534 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1535 PFM_STARTINDENT;
1536 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1539 /* Fall through */
1541 case IDCANCEL:
1542 EndDialog(hWnd, wParam);
1543 return TRUE;
1546 return FALSE;
1549 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1551 switch(message)
1553 case WM_INITDIALOG:
1555 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1556 PARAFORMAT pf;
1557 WCHAR buffer[MAX_STRING_LEN];
1558 int i;
1560 pf.cbSize = sizeof(pf);
1561 pf.dwMask = PFM_TABSTOPS;
1562 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1563 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1565 for(i = 0; i < pf.cTabCount; i++)
1567 number_with_units(buffer, pf.rgxTabs[i]);
1568 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1570 SetFocus(hTabWnd);
1572 break;
1574 case WM_COMMAND:
1575 switch(LOWORD(wParam))
1577 case IDC_TABSTOPS:
1579 HWND hTabWnd = (HWND)lParam;
1580 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1581 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1582 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1584 if(GetWindowTextLengthW(hTabWnd))
1585 EnableWindow(hAddWnd, TRUE);
1586 else
1587 EnableWindow(hAddWnd, FALSE);
1589 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1591 EnableWindow(hEmptyWnd, TRUE);
1593 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1594 EnableWindow(hDelWnd, FALSE);
1595 else
1596 EnableWindow(hDelWnd, TRUE);
1597 } else
1599 EnableWindow(hEmptyWnd, FALSE);
1602 break;
1604 case ID_TAB_ADD:
1606 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1607 WCHAR buffer[MAX_STRING_LEN];
1609 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1610 append_current_units(buffer);
1612 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1614 float number = 0;
1616 if(!number_from_string(buffer, &number, TRUE))
1618 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1619 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1620 } else
1622 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1623 SetWindowTextW(hTabWnd, 0);
1626 SetFocus(hTabWnd);
1628 break;
1630 case ID_TAB_DEL:
1632 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1633 LRESULT ret;
1634 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1635 if(ret != CB_ERR)
1636 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1638 break;
1640 case ID_TAB_EMPTY:
1642 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1643 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1644 SetFocus(hTabWnd);
1646 break;
1648 case IDOK:
1650 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1651 int i;
1652 WCHAR buffer[MAX_STRING_LEN];
1653 PARAFORMAT pf;
1654 float number;
1656 pf.cbSize = sizeof(pf);
1657 pf.dwMask = PFM_TABSTOPS;
1659 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1660 (LPARAM)&buffer) != CB_ERR &&
1661 i < MAX_TAB_STOPS; i++)
1663 number_from_string(buffer, &number, TRUE);
1664 pf.rgxTabs[i] = current_units_to_twips(number);
1666 pf.cTabCount = i;
1667 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1669 /* Fall through */
1670 case IDCANCEL:
1671 EndDialog(hWnd, wParam);
1672 return TRUE;
1675 return FALSE;
1678 static int context_menu(LPARAM lParam)
1680 int x = (int)(short)LOWORD(lParam);
1681 int y = (int)(short)HIWORD(lParam);
1682 HMENU hPop = GetSubMenu(hPopupMenu, 0);
1684 if(x == -1)
1686 int from = 0, to = 0;
1687 POINTL pt;
1688 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1689 SendMessageW(hEditorWnd, EM_POSFROMCHAR, (WPARAM)&pt, (LPARAM)to);
1690 ClientToScreen(hEditorWnd, (POINT*)&pt);
1691 x = pt.x;
1692 y = pt.y;
1695 TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
1696 x, y, 0, hMainWnd, 0);
1698 return 0;
1701 static LRESULT OnCreate( HWND hWnd )
1703 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1704 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
1705 HANDLE hDLL;
1706 TBADDBITMAP ab;
1707 int nStdBitmaps = 0;
1708 REBARINFO rbi;
1709 REBARBANDINFOW rbb;
1710 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1711 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1713 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1715 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1716 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1717 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1719 rbi.cbSize = sizeof(rbi);
1720 rbi.fMask = 0;
1721 rbi.himl = NULL;
1722 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1723 return -1;
1725 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
1726 IDC_TOOLBAR,
1727 1, hInstance, IDB_TOOLBAR,
1728 NULL, 0,
1729 24, 24, 16, 16, sizeof(TBBUTTON));
1731 ab.hInst = HINST_COMMCTRL;
1732 ab.nID = IDB_STD_SMALL_COLOR;
1733 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1735 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1736 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1737 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1738 AddSeparator(hToolBarWnd);
1739 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1740 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1741 AddSeparator(hToolBarWnd);
1742 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1743 AddSeparator(hToolBarWnd);
1744 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1745 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1746 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1747 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1748 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1749 AddSeparator(hToolBarWnd);
1750 AddButton(hToolBarWnd, 0, ID_DATETIME);
1752 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1754 rbb.cbSize = sizeof(rbb);
1755 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1756 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1757 rbb.cx = 0;
1758 rbb.hwndChild = hToolBarWnd;
1759 rbb.cxMinChild = 0;
1760 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1761 rbb.wID = BANDID_TOOLBAR;
1763 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1765 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1766 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1767 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1769 rbb.hwndChild = hFontListWnd;
1770 rbb.cx = 200;
1771 rbb.wID = BANDID_FONTLIST;
1773 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1775 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1776 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1777 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1779 rbb.hwndChild = hSizeListWnd;
1780 rbb.cx = 50;
1781 rbb.fStyle ^= RBBS_BREAK;
1782 rbb.wID = BANDID_SIZELIST;
1784 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1786 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1787 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
1788 IDC_FORMATBAR, 7, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1790 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1791 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1792 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1793 AddSeparator(hFormatBarWnd);
1794 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
1795 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
1796 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
1797 AddSeparator(hFormatBarWnd);
1798 AddButton(hFormatBarWnd, 6, ID_BULLET);
1800 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1802 rbb.hwndChild = hFormatBarWnd;
1803 rbb.wID = BANDID_FORMATBAR;
1805 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1807 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1808 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1811 rbb.hwndChild = hRulerWnd;
1812 rbb.wID = BANDID_RULER;
1813 rbb.fStyle |= RBBS_BREAK;
1815 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1817 hDLL = LoadLibraryW(wszRichEditDll);
1818 if(!hDLL)
1820 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1821 MB_OK | MB_ICONEXCLAMATION);
1822 PostQuitMessage(1);
1825 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
1826 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1827 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1828 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1830 if (!hEditorWnd)
1832 fprintf(stderr, "Error code %u\n", GetLastError());
1833 return -1;
1835 assert(hEditorWnd);
1837 SetFocus(hEditorWnd);
1838 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1840 set_default_font();
1842 populate_font_list(hFontListWnd);
1843 populate_size_list(hSizeListWnd);
1844 DoLoadStrings();
1845 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1847 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1849 registry_read_filelist(hWnd);
1850 registry_read_formatopts_all(barState, wordWrap);
1851 registry_read_options();
1852 DragAcceptFiles(hWnd, TRUE);
1854 return 0;
1857 static LRESULT OnUser( HWND hWnd )
1859 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1860 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1861 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1862 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1863 int from, to;
1864 CHARFORMAT2W fmt;
1865 PARAFORMAT2 pf;
1866 GETTEXTLENGTHEX gt;
1868 ZeroMemory(&fmt, sizeof(fmt));
1869 fmt.cbSize = sizeof(fmt);
1871 ZeroMemory(&pf, sizeof(pf));
1872 pf.cbSize = sizeof(pf);
1874 gt.flags = GTL_NUMCHARS;
1875 gt.codepage = 1200;
1877 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1878 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1880 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1882 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1883 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1884 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1885 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1886 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1887 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1888 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
1890 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
1891 (fmt.dwEffects & CFE_BOLD));
1892 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
1893 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
1894 (fmt.dwEffects & CFE_ITALIC));
1895 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
1896 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
1897 (fmt.dwEffects & CFE_UNDERLINE));
1898 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
1900 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1901 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
1902 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
1903 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
1905 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
1906 return 0;
1909 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
1911 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1912 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1913 NMHDR *pHdr = (NMHDR *)lParam;
1914 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
1915 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
1917 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
1919 if (pHdr->code == CBEN_ENDEDITW)
1921 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
1922 if(pHdr->hwndFrom == hwndFontList)
1924 on_fontlist_modified((LPWSTR)endEdit->szText);
1925 } else if (pHdr->hwndFrom == hwndSizeList)
1927 on_sizelist_modified(hwndFontList,(LPWSTR)endEdit->szText);
1930 return 0;
1933 if (pHdr->hwndFrom != hwndEditor)
1934 return 0;
1936 if (pHdr->code == EN_SELCHANGE)
1938 SELCHANGE *pSC = (SELCHANGE *)lParam;
1939 char buf[128];
1941 update_font_list();
1943 sprintf( buf,"selection = %d..%d, line count=%ld",
1944 pSC->chrg.cpMin, pSC->chrg.cpMax,
1945 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
1946 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
1947 SendMessage(hWnd, WM_USER, 0, 0);
1948 return 1;
1950 return 0;
1953 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
1955 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1956 static FINDREPLACEW findreplace;
1958 if ((HWND)lParam == hwndEditor)
1959 return 0;
1961 switch(LOWORD(wParam))
1964 case ID_FILE_EXIT:
1965 PostMessageW(hWnd, WM_CLOSE, 0, 0);
1966 break;
1968 case ID_FILE_NEW:
1970 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
1971 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
1972 newfile_proc);
1974 if(ret != ID_NEWFILE_ABORT)
1976 if(prompt_save_changes())
1978 SETTEXTEX st;
1980 set_caption(NULL);
1981 wszFileName[0] = '\0';
1983 clear_formatting();
1985 st.flags = ST_DEFAULT;
1986 st.codepage = 1200;
1987 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
1989 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1990 set_fileformat(ret);
1991 update_font_list();
1995 break;
1997 case ID_FILE_OPEN:
1998 DialogOpenFile();
1999 break;
2001 case ID_FILE_SAVE:
2002 if(wszFileName[0])
2004 DoSaveFile(wszFileName, fileFormat);
2005 break;
2007 /* Fall through */
2009 case ID_FILE_SAVEAS:
2010 DialogSaveFile();
2011 break;
2013 case ID_FILE_RECENT1:
2014 case ID_FILE_RECENT2:
2015 case ID_FILE_RECENT3:
2016 case ID_FILE_RECENT4:
2018 HMENU hMenu = GetMenu(hWnd);
2019 MENUITEMINFOW mi;
2021 mi.cbSize = sizeof(MENUITEMINFOW);
2022 mi.fMask = MIIM_DATA;
2023 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2024 DoOpenFile((LPWSTR)mi.dwItemData);
2026 break;
2028 case ID_FIND:
2029 dialog_find(&findreplace, FALSE);
2030 break;
2032 case ID_FIND_NEXT:
2033 handle_findmsg(&findreplace);
2034 break;
2036 case ID_REPLACE:
2037 dialog_find(&findreplace, TRUE);
2038 break;
2040 case ID_FONTSETTINGS:
2041 dialog_choose_font();
2042 break;
2044 case ID_PRINT:
2045 dialog_print(hWnd, wszFileName);
2046 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2047 break;
2049 case ID_PRINT_QUICK:
2050 print_quick(wszFileName);
2051 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2052 break;
2054 case ID_PREVIEW:
2056 int index = reg_formatindex(fileFormat);
2057 DWORD tmp = barState[index];
2058 barState[index] = 0;
2059 set_bar_states();
2060 barState[index] = tmp;
2061 ShowWindow(hEditorWnd, FALSE);
2063 init_preview(hWnd, wszFileName);
2065 SetMenu(hWnd, NULL);
2066 InvalidateRect(0, 0, TRUE);
2068 break;
2070 case ID_PRINTSETUP:
2071 dialog_printsetup(hWnd);
2072 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2073 break;
2075 case ID_FORMAT_BOLD:
2076 case ID_FORMAT_ITALIC:
2077 case ID_FORMAT_UNDERLINE:
2079 CHARFORMAT2W fmt;
2080 int effects = CFE_BOLD;
2082 ZeroMemory(&fmt, sizeof(fmt));
2083 fmt.cbSize = sizeof(fmt);
2084 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2086 fmt.dwMask = CFM_BOLD;
2088 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2090 effects = CFE_ITALIC;
2091 fmt.dwMask = CFM_ITALIC;
2092 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2094 effects = CFE_UNDERLINE;
2095 fmt.dwMask = CFM_UNDERLINE;
2098 fmt.dwEffects ^= effects;
2100 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2101 break;
2104 case ID_EDIT_CUT:
2105 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2106 break;
2108 case ID_EDIT_COPY:
2109 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2110 break;
2112 case ID_EDIT_PASTE:
2113 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2114 break;
2116 case ID_EDIT_CLEAR:
2117 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2118 break;
2120 case ID_EDIT_SELECTALL:
2122 CHARRANGE range = {0, -1};
2123 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2124 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2125 return 0;
2128 case ID_EDIT_GETTEXT:
2130 int nLen = GetWindowTextLengthW(hwndEditor);
2131 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2132 TEXTRANGEW tr;
2134 GetWindowTextW(hwndEditor, data, nLen+1);
2135 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2137 HeapFree( GetProcessHeap(), 0, data);
2138 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2139 tr.chrg.cpMin = 0;
2140 tr.chrg.cpMax = nLen;
2141 tr.lpstrText = data;
2142 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2143 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2144 HeapFree( GetProcessHeap(), 0, data );
2146 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2147 return 0;
2150 case ID_EDIT_CHARFORMAT:
2151 case ID_EDIT_DEFCHARFORMAT:
2153 CHARFORMAT2W cf;
2154 LRESULT i;
2155 ZeroMemory(&cf, sizeof(cf));
2156 cf.cbSize = sizeof(cf);
2157 cf.dwMask = 0;
2158 i = SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2159 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2160 return 0;
2163 case ID_EDIT_PARAFORMAT:
2165 PARAFORMAT2 pf;
2166 ZeroMemory(&pf, sizeof(pf));
2167 pf.cbSize = sizeof(pf);
2168 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2169 return 0;
2172 case ID_EDIT_SELECTIONINFO:
2174 CHARRANGE range = {0, -1};
2175 char buf[128];
2176 WCHAR *data = NULL;
2178 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2179 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2180 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2181 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2182 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2183 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2184 HeapFree( GetProcessHeap(), 0, data);
2185 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2186 return 0;
2189 case ID_EDIT_READONLY:
2191 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2192 if (nStyle & ES_READONLY)
2193 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2194 else
2195 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2196 return 0;
2199 case ID_EDIT_MODIFIED:
2200 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2201 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2202 else
2203 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2204 return 0;
2206 case ID_EDIT_UNDO:
2207 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2208 return 0;
2210 case ID_EDIT_REDO:
2211 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2212 return 0;
2214 case ID_BULLET:
2216 PARAFORMAT pf;
2218 pf.cbSize = sizeof(pf);
2219 pf.dwMask = PFM_NUMBERING;
2220 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2222 pf.dwMask |= PFM_OFFSET;
2224 if(pf.wNumbering == PFN_BULLET)
2226 pf.wNumbering = 0;
2227 pf.dxOffset = 0;
2228 } else
2230 pf.wNumbering = PFN_BULLET;
2231 pf.dxOffset = 720;
2234 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2236 break;
2238 case ID_ALIGN_LEFT:
2239 case ID_ALIGN_CENTER:
2240 case ID_ALIGN_RIGHT:
2242 PARAFORMAT2 pf;
2244 pf.cbSize = sizeof(pf);
2245 pf.dwMask = PFM_ALIGNMENT;
2246 switch(LOWORD(wParam)) {
2247 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2248 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2249 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2251 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2252 break;
2255 case ID_BACK_1:
2256 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2257 break;
2259 case ID_BACK_2:
2260 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2261 break;
2263 case ID_TOGGLE_TOOLBAR:
2264 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2265 update_window();
2266 break;
2268 case ID_TOGGLE_FORMATBAR:
2269 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2270 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2271 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2272 update_window();
2273 break;
2275 case ID_TOGGLE_STATUSBAR:
2276 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2277 update_window();
2278 break;
2280 case ID_TOGGLE_RULER:
2281 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2282 update_window();
2283 break;
2285 case ID_DATETIME:
2287 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2288 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2289 break;
2292 case ID_PARAFORMAT:
2294 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2295 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd,
2296 paraformat_proc);
2298 break;
2300 case ID_TABSTOPS:
2302 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2303 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2305 break;
2307 case ID_ABOUT:
2308 dialog_about();
2309 break;
2311 case ID_VIEWPROPERTIES:
2312 dialog_viewproperties();
2313 break;
2315 case IDC_FONTLIST:
2316 if (HIWORD(wParam) == CBN_SELENDOK)
2318 WCHAR buffer[LF_FACESIZE];
2319 HWND hwndFontList = (HWND)lParam;
2320 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2321 on_fontlist_modified(buffer);
2323 break;
2325 case IDC_SIZELIST:
2326 if (HIWORD(wParam) == CBN_SELENDOK)
2328 WCHAR buffer[MAX_STRING_LEN+1];
2329 HWND hwndSizeList = (HWND)lParam;
2330 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2331 on_sizelist_modified(hwndSizeList, buffer);
2333 break;
2335 default:
2336 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2337 break;
2339 return 0;
2342 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2344 HMENU hMenu = (HMENU)wParam;
2345 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2346 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2347 PARAFORMAT pf;
2348 int nAlignment = -1;
2349 int selFrom, selTo;
2350 GETTEXTLENGTHEX gt;
2351 LRESULT textLength;
2352 MENUITEMINFOW mi;
2354 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2355 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2356 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2358 pf.cbSize = sizeof(PARAFORMAT);
2359 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2360 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2361 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
2362 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2363 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
2364 if (pf.dwMask & PFM_ALIGNMENT)
2365 nAlignment = pf.wAlignment;
2366 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
2367 MF_CHECKED : MF_UNCHECKED);
2368 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
2369 MF_CHECKED : MF_UNCHECKED);
2370 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
2371 MF_CHECKED : MF_UNCHECKED);
2372 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
2373 MF_CHECKED : MF_UNCHECKED));
2374 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
2375 MF_ENABLED : MF_GRAYED);
2376 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
2377 MF_ENABLED : MF_GRAYED);
2379 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
2380 MF_CHECKED : MF_UNCHECKED);
2382 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
2383 MF_CHECKED : MF_UNCHECKED);
2385 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
2386 MF_CHECKED : MF_UNCHECKED);
2388 CheckMenuItem(hMenu, ID_TOGGLE_RULER, MF_BYCOMMAND|(is_bar_visible(BANDID_RULER)) ? MF_CHECKED : MF_UNCHECKED);
2390 gt.flags = GTL_NUMCHARS;
2391 gt.codepage = 1200;
2392 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2393 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2395 mi.cbSize = sizeof(mi);
2396 mi.fMask = MIIM_DATA;
2398 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2400 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
2401 MF_ENABLED : MF_GRAYED));
2403 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2405 return 0;
2408 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2410 int nStatusSize = 0;
2411 RECT rc;
2412 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2413 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2414 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2415 HWND hRulerWnd = GetDlgItem(hWnd, IDC_RULER);
2416 int rebarHeight = 0;
2418 if (hwndStatusBar)
2420 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2421 if (IsWindowVisible(hwndStatusBar))
2423 GetClientRect(hwndStatusBar, &rc);
2424 nStatusSize = rc.bottom - rc.top;
2425 } else
2427 nStatusSize = 0;
2430 if (hwndReBar)
2432 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2434 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2436 if (hwndEditor)
2438 GetClientRect(hWnd, &rc);
2439 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2442 redraw_ruler(hRulerWnd);
2444 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2447 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2449 if(msg == ID_FINDMSGSTRING)
2450 return handle_findmsg((LPFINDREPLACEW)lParam);
2452 switch(msg)
2454 case WM_CREATE:
2455 return OnCreate( hWnd );
2457 case WM_USER:
2458 return OnUser( hWnd );
2460 case WM_NOTIFY:
2461 return OnNotify( hWnd, lParam );
2463 case WM_COMMAND:
2464 if(preview_isactive())
2466 return preview_command( hWnd, wParam );
2469 return OnCommand( hWnd, wParam, lParam );
2471 case WM_DESTROY:
2472 PostQuitMessage(0);
2473 break;
2475 case WM_CLOSE:
2476 if(preview_isactive())
2478 preview_exit(hWnd);
2479 } else if(prompt_save_changes())
2481 registry_set_options(hMainWnd);
2482 registry_set_formatopts_all(barState);
2483 PostQuitMessage(0);
2485 break;
2487 case WM_ACTIVATE:
2488 if (LOWORD(wParam))
2489 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2490 return 0;
2492 case WM_INITMENUPOPUP:
2493 return OnInitPopupMenu( hWnd, wParam );
2495 case WM_SIZE:
2496 return OnSize( hWnd, wParam, lParam );
2498 case WM_CONTEXTMENU:
2499 if((HWND)wParam == hEditorWnd)
2500 return context_menu(lParam);
2501 else
2502 return DefWindowProcW(hWnd, msg, wParam, lParam);
2504 case WM_DROPFILES:
2506 WCHAR file[MAX_PATH];
2507 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2508 DragFinish((HDROP)wParam);
2510 if(prompt_save_changes())
2511 DoOpenFile(file);
2513 break;
2514 case WM_PAINT:
2515 if(preview_isactive())
2516 return print_preview(hWnd);
2517 else
2518 return DefWindowProcW(hWnd, msg, wParam, lParam);
2520 default:
2521 return DefWindowProcW(hWnd, msg, wParam, lParam);
2524 return 0;
2527 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2529 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2530 HACCEL hAccel;
2531 WNDCLASSW wc;
2532 MSG msg;
2533 RECT rc;
2534 UINT_PTR hPrevRulerProc;
2535 HWND hRulerWnd;
2536 POINTL EditPoint;
2537 DWORD bMaximized;
2538 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2539 'T','A','B','L','E','\0'};
2541 InitCommonControlsEx(&classes);
2543 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2545 wc.style = CS_HREDRAW | CS_VREDRAW;
2546 wc.lpfnWndProc = WndProc;
2547 wc.cbClsExtra = 0;
2548 wc.cbWndExtra = 4;
2549 wc.hInstance = hInstance;
2550 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2551 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2552 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2553 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2554 wc.lpszClassName = wszMainWndClass;
2555 RegisterClassW(&wc);
2557 registry_read_winrect(&rc);
2558 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2559 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2560 registry_read_maximized(&bMaximized);
2561 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2562 && bMaximized)
2563 nCmdShow = SW_SHOWMAXIMIZED;
2564 ShowWindow(hMainWnd, nCmdShow);
2566 set_caption(NULL);
2567 set_bar_states();
2568 set_fileformat(SF_RTF);
2569 hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
2570 get_default_printer_opts();
2571 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2573 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2574 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2575 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2576 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2578 HandleCommandLine(GetCommandLineW());
2580 while(GetMessageW(&msg,0,0,0))
2582 if (IsDialogMessage(hFindWnd, &msg))
2583 continue;
2585 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2586 continue;
2587 TranslateMessage(&msg);
2588 DispatchMessageW(&msg);
2589 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2590 SendMessageW(hMainWnd, WM_USER, 0, 0);
2593 return 0;