mshtml: Fix Portuguese (Brazilian) translation.
[wine/multimedia.git] / programs / wordpad / wordpad.c
blob0e6566df74c9b9225ef0260364bb68d6399b9e60
1 /*
2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_IE 0x0400
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <assert.h>
31 #include <windows.h>
32 #include <richedit.h>
33 #include <commctrl.h>
34 #include <commdlg.h>
35 #include <shellapi.h>
36 #include <math.h>
37 #include <errno.h>
39 #include "wine/unicode.h"
40 #include "wordpad.h"
42 #ifdef NONAMELESSUNION
43 # define U(x) (x).u
44 # define U2(x) (x).u2
45 # define U3(x) (x).u3
46 #else
47 # define U(x) (x)
48 # define U2(x) (x)
49 # define U3(x) (x)
50 #endif
52 /* use LoadString */
53 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
55 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
57 static const WCHAR stringFormat[] = {'%','2','d','\0'};
59 const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
60 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
62 static HWND hMainWnd;
63 static HWND hEditorWnd;
64 static HWND hFindWnd;
65 static HMENU hPopupMenu;
66 static HMENU hColorPopupMenu;
68 static UINT ID_FINDMSGSTRING;
70 static DWORD wordWrap[2];
71 static DWORD barState[2];
72 static WPARAM fileFormat = SF_RTF;
74 static WCHAR wszFileName[MAX_PATH];
75 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
76 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
77 static WCHAR wszSaveChanges[MAX_STRING_LEN];
78 static WCHAR units_cmW[MAX_STRING_LEN];
79 static WCHAR units_inW[MAX_STRING_LEN];
80 static WCHAR units_inchW[MAX_STRING_LEN];
81 static WCHAR units_ptW[MAX_STRING_LEN];
83 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
85 typedef enum
87 UNIT_CM,
88 UNIT_INCH,
89 UNIT_PT
90 } UNIT;
92 /* Load string resources */
93 static void DoLoadStrings(void)
95 LPWSTR p = wszFilter;
96 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
97 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
98 static const WCHAR files_all[] = {'*','.','*','\0'};
100 HINSTANCE hInstance = GetModuleHandleW(0);
102 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
103 p += lstrlenW(p) + 1;
104 lstrcpyW(p, files_rtf);
105 p += lstrlenW(p) + 1;
106 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
107 p += lstrlenW(p) + 1;
108 lstrcpyW(p, files_txt);
109 p += lstrlenW(p) + 1;
110 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
111 p += lstrlenW(p) + 1;
112 lstrcpyW(p, files_txt);
113 p += lstrlenW(p) + 1;
114 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
115 p += lstrlenW(p) + 1;
116 lstrcpyW(p, files_all);
117 p += lstrlenW(p) + 1;
118 *p = '\0';
120 p = wszDefaultFileName;
121 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
123 p = wszSaveChanges;
124 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
126 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
127 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
128 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
129 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
132 /* Show a message box with resource strings */
133 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
135 MSGBOXPARAMSW params;
137 params.cbSize = sizeof(params);
138 params.hwndOwner = hWnd;
139 params.hInstance = GetModuleHandleW(0);
140 params.lpszText = lpText;
141 params.lpszCaption = lpCaption;
142 params.dwStyle = uType;
143 params.lpszIcon = NULL;
144 params.dwContextHelpId = 0;
145 params.lpfnMsgBoxCallback = NULL;
146 params.dwLanguageId = 0;
147 return MessageBoxIndirectW(&params);
151 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
153 TBBUTTON button;
155 ZeroMemory(&button, sizeof(button));
156 button.iBitmap = nImage;
157 button.idCommand = nCommand;
158 button.fsState = TBSTATE_ENABLED;
159 button.fsStyle = TBSTYLE_BUTTON;
160 button.dwData = 0;
161 button.iString = -1;
162 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
165 static void AddSeparator(HWND hwndToolBar)
167 TBBUTTON button;
169 ZeroMemory(&button, sizeof(button));
170 button.iBitmap = -1;
171 button.idCommand = 0;
172 button.fsState = 0;
173 button.fsStyle = TBSTYLE_SEP;
174 button.dwData = 0;
175 button.iString = -1;
176 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
179 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
181 HANDLE hFile = (HANDLE)cookie;
182 DWORD read;
184 if(!ReadFile(hFile, buffer, cb, &read, 0))
185 return 1;
187 *pcb = read;
189 return 0;
192 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
194 DWORD written;
195 int ret;
196 HANDLE hFile = (HANDLE)cookie;
198 ret = WriteFile(hFile, buffer, cb, &written, 0);
200 if(!ret || (cb != written))
201 return 1;
203 *pcb = cb;
205 return 0;
208 LPWSTR file_basename(LPWSTR path)
210 LPWSTR pos = path + lstrlenW(path);
212 while(pos > path)
214 if(*pos == '\\' || *pos == '/')
216 pos++;
217 break;
219 pos--;
221 return pos;
224 static void set_caption(LPCWSTR wszNewFileName)
226 static const WCHAR wszSeparator[] = {' ','-',' '};
227 WCHAR *wszCaption;
228 SIZE_T length = 0;
230 if(!wszNewFileName)
231 wszNewFileName = wszDefaultFileName;
232 else
233 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
235 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
236 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
238 if(!wszCaption)
239 return;
241 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
242 length += lstrlenW(wszNewFileName);
243 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
244 length += sizeof(wszSeparator) / sizeof(WCHAR);
245 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
247 SetWindowTextW(hMainWnd, wszCaption);
249 HeapFree(GetProcessHeap(), 0, wszCaption);
252 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
254 if(punit != NULL)
255 *punit = UNIT_CM;
256 if(!endptr)
257 return FALSE;
258 if(!*endptr)
259 return TRUE;
261 while(*endptr == ' ')
262 endptr++;
264 if(punit == NULL)
265 return *endptr == '\0';
267 if(!lstrcmpW(endptr, units_cmW))
269 *punit = UNIT_CM;
270 endptr += lstrlenW(units_cmW);
272 else if (!lstrcmpW(endptr, units_inW))
274 *punit = UNIT_INCH;
275 endptr += lstrlenW(units_inW);
277 else if (!lstrcmpW(endptr, units_inchW))
279 *punit = UNIT_INCH;
280 endptr += lstrlenW(units_inchW);
282 else if (!lstrcmpW(endptr, units_ptW))
284 *punit = UNIT_PT;
285 endptr += lstrlenW(units_ptW);
288 return *endptr == '\0';
291 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
293 double ret;
294 WCHAR *endptr;
296 *num = 0;
297 errno = 0;
298 ret = wcstod(string, &endptr);
300 if (punit != NULL)
301 *punit = UNIT_CM;
302 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
304 return FALSE;
305 } else
307 *num = (float)ret;
308 return TRUE;
312 static void set_size(float size)
314 CHARFORMAT2W fmt;
316 ZeroMemory(&fmt, sizeof(fmt));
317 fmt.cbSize = sizeof(fmt);
318 fmt.dwMask = CFM_SIZE;
319 fmt.yHeight = (int)(size * 20.0);
320 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
323 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
325 WCHAR sizeBuffer[MAX_STRING_LEN];
326 CHARFORMAT2W format;
328 ZeroMemory(&format, sizeof(format));
329 format.cbSize = sizeof(format);
330 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
332 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
333 if(lstrcmpW(sizeBuffer, wszNewFontSize))
335 float size = 0;
336 if(number_from_string(wszNewFontSize, &size, NULL)
337 && size > 0)
339 set_size(size);
340 } else
342 SetWindowTextW(hwndSizeList, sizeBuffer);
343 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
344 wszAppTitle, MB_OK | MB_ICONINFORMATION);
349 static void add_size(HWND hSizeListWnd, unsigned size)
351 WCHAR buffer[3];
352 COMBOBOXEXITEMW cbItem;
353 cbItem.mask = CBEIF_TEXT;
354 cbItem.iItem = -1;
356 wsprintfW(buffer, stringFormat, size);
357 cbItem.pszText = buffer;
358 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
361 static void populate_size_list(HWND hSizeListWnd)
363 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
364 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
365 COMBOBOXEXITEMW cbFontItem;
366 CHARFORMAT2W fmt;
367 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
368 HDC hdc = GetDC(hMainWnd);
369 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
370 WCHAR buffer[3];
371 size_t i;
372 DWORD fontStyle;
374 ZeroMemory(&fmt, sizeof(fmt));
375 fmt.cbSize = sizeof(fmt);
376 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
378 cbFontItem.mask = CBEIF_LPARAM;
379 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
380 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
382 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
384 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
386 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
388 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
389 GetDeviceCaps(hdc, LOGPIXELSY)));
390 } else
392 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
393 add_size(hSizeListWnd, choices[i]);
396 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
397 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
400 static void update_size_list(void)
402 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
403 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
404 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
405 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
406 CHARFORMAT2W fmt;
408 ZeroMemory(&fmt, sizeof(fmt));
409 fmt.cbSize = sizeof(fmt);
411 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
413 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
414 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
416 if(lstrcmpW(fontSize, sizeBuffer))
417 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
420 static void update_font_list(void)
422 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
423 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
424 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
425 WCHAR fontName[MAX_STRING_LEN];
426 CHARFORMAT2W fmt;
428 ZeroMemory(&fmt, sizeof(fmt));
429 fmt.cbSize = sizeof(fmt);
431 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
432 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
434 if(lstrcmpW(fontName, fmt.szFaceName))
436 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
437 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
438 } else
440 update_size_list();
444 static void clear_formatting(void)
446 PARAFORMAT2 pf;
448 pf.cbSize = sizeof(pf);
449 pf.dwMask = PFM_ALIGNMENT;
450 pf.wAlignment = PFA_LEFT;
451 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
454 static int fileformat_number(WPARAM format)
456 int number = 0;
458 if(format == SF_TEXT)
460 number = 1;
461 } else if (format == (SF_TEXT | SF_UNICODE))
463 number = 2;
465 return number;
468 static WPARAM fileformat_flags(int format)
470 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
472 return flags[format];
475 static void set_font(LPCWSTR wszFaceName)
477 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
478 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
479 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
480 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
481 CHARFORMAT2W fmt;
483 ZeroMemory(&fmt, sizeof(fmt));
485 fmt.cbSize = sizeof(fmt);
486 fmt.dwMask = CFM_FACE;
488 lstrcpyW(fmt.szFaceName, wszFaceName);
490 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
492 populate_size_list(hSizeListWnd);
494 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
497 static void set_default_font(void)
499 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
500 'R','o','m','a','n',0};
501 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
502 CHARFORMAT2W fmt;
503 LPCWSTR font;
505 ZeroMemory(&fmt, sizeof(fmt));
507 fmt.cbSize = sizeof(fmt);
508 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
509 fmt.dwEffects = 0;
511 if(fileFormat & SF_RTF)
512 font = richTextFont;
513 else
514 font = plainTextFont;
516 lstrcpyW(fmt.szFaceName, font);
518 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
521 static void on_fontlist_modified(LPWSTR wszNewFaceName)
523 CHARFORMAT2W format;
524 ZeroMemory(&format, sizeof(format));
525 format.cbSize = sizeof(format);
526 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
528 if(lstrcmpW(format.szFaceName, wszNewFaceName))
529 set_font(wszNewFaceName);
532 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
534 COMBOBOXEXITEMW cbItem;
535 WCHAR buffer[MAX_PATH];
536 int fontHeight = 0;
538 cbItem.mask = CBEIF_TEXT;
539 cbItem.pszText = buffer;
540 cbItem.cchTextMax = MAX_STRING_LEN;
541 cbItem.iItem = 0;
543 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
545 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
546 cbItem.iItem++;
547 else
548 break;
550 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
551 lstrcpyW( cbItem.pszText, fontName );
553 cbItem.mask |= CBEIF_LPARAM;
554 if(fontType & RASTER_FONTTYPE)
555 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
557 cbItem.lParam = MAKELONG(fontType,fontHeight);
558 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
559 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
562 static void dialog_choose_font(void)
564 CHOOSEFONTW cf;
565 LOGFONTW lf;
566 CHARFORMAT2W fmt;
567 HDC hDC = GetDC(hMainWnd);
569 ZeroMemory(&cf, sizeof(cf));
570 cf.lStructSize = sizeof(cf);
571 cf.hwndOwner = hMainWnd;
572 cf.lpLogFont = &lf;
573 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
575 ZeroMemory(&fmt, sizeof(fmt));
576 fmt.cbSize = sizeof(fmt);
578 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
579 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
580 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) ? TRUE : FALSE;
581 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
582 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE;
583 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE;
584 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
585 cf.rgbColors = fmt.crTextColor;
587 if(ChooseFontW(&cf))
589 ZeroMemory(&fmt, sizeof(fmt));
590 fmt.cbSize = sizeof(fmt);
591 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
592 fmt.yHeight = cf.iPointSize * 2;
594 if(cf.nFontType & BOLD_FONTTYPE)
595 fmt.dwEffects |= CFE_BOLD;
596 if(cf.nFontType & ITALIC_FONTTYPE)
597 fmt.dwEffects |= CFE_ITALIC;
598 if(cf.lpLogFont->lfUnderline == TRUE)
599 fmt.dwEffects |= CFE_UNDERLINE;
600 if(cf.lpLogFont->lfStrikeOut == TRUE)
601 fmt.dwEffects |= CFE_STRIKEOUT;
603 fmt.crTextColor = cf.rgbColors;
605 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
606 set_font(cf.lpLogFont->lfFaceName);
611 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
612 DWORD FontType, LPARAM lParam)
614 HWND hListWnd = (HWND) lParam;
616 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
619 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
622 return 1;
625 static void populate_font_list(HWND hListWnd)
627 HDC hdc = GetDC(hMainWnd);
628 LOGFONTW fontinfo;
629 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
630 CHARFORMAT2W fmt;
632 fontinfo.lfCharSet = DEFAULT_CHARSET;
633 *fontinfo.lfFaceName = '\0';
634 fontinfo.lfPitchAndFamily = 0;
636 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
637 (LPARAM)hListWnd, 0);
639 ZeroMemory(&fmt, sizeof(fmt));
640 fmt.cbSize = sizeof(fmt);
641 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
642 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
645 static void update_window(void)
647 RECT rect;
649 GetClientRect(hMainWnd, &rect);
651 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
654 static BOOL is_bar_visible(int bandId)
656 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
659 static void store_bar_state(int bandId, BOOL show)
661 int formatIndex = reg_formatindex(fileFormat);
663 if(show)
664 barState[formatIndex] |= (1 << bandId);
665 else
666 barState[formatIndex] &= ~(1 << bandId);
669 static void set_toolbar_state(int bandId, BOOL show)
671 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
673 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
675 if(bandId == BANDID_TOOLBAR)
677 REBARBANDINFOW rbbinfo;
678 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
680 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
681 rbbinfo.fMask = RBBIM_STYLE;
683 SendMessageW(hwndReBar, RB_GETBANDINFO, index, (LPARAM)&rbbinfo);
685 if(!show)
686 rbbinfo.fStyle &= ~RBBS_BREAK;
687 else
688 rbbinfo.fStyle |= RBBS_BREAK;
690 SendMessageW(hwndReBar, RB_SETBANDINFO, index, (LPARAM)&rbbinfo);
693 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
694 store_bar_state(bandId, show);
697 static void set_statusbar_state(BOOL show)
699 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
701 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
702 store_bar_state(BANDID_STATUSBAR, show);
705 static void set_bar_states(void)
707 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
708 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
709 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
710 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
711 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
712 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
714 update_window();
717 static void preview_exit(HWND hMainWnd)
719 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
720 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
722 set_bar_states();
723 ShowWindow(hEditorWnd, TRUE);
725 close_preview(hMainWnd);
727 SetMenu(hMainWnd, hMenu);
728 registry_read_filelist(hMainWnd);
730 update_window();
733 static void set_fileformat(WPARAM format)
735 fileFormat = format;
737 set_bar_states();
738 set_default_font();
739 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
742 static void ShowOpenError(DWORD Code)
744 LPWSTR Message;
746 switch(Code)
748 case ERROR_ACCESS_DENIED:
749 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
750 break;
752 default:
753 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
755 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
758 static void DoOpenFile(LPCWSTR szOpenFileName)
760 HANDLE hFile;
761 EDITSTREAM es;
762 char fileStart[5];
763 DWORD readOut;
764 WPARAM format = SF_TEXT;
766 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
767 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
768 if (hFile == INVALID_HANDLE_VALUE)
770 ShowOpenError(GetLastError());
771 return;
774 ReadFile(hFile, fileStart, 5, &readOut, NULL);
775 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
777 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
779 format = SF_TEXT | SF_UNICODE;
780 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
781 } else if(readOut >= 5)
783 static const char header[] = "{\\rtf";
784 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
786 if(!memcmp(header, fileStart, 5))
787 format = SF_RTF;
788 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
790 CloseHandle(hFile);
791 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
792 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
793 return;
797 es.dwCookie = (DWORD_PTR)hFile;
798 es.pfnCallback = stream_in;
800 clear_formatting();
801 set_fileformat(format);
802 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
804 CloseHandle(hFile);
806 SetFocus(hEditorWnd);
808 set_caption(szOpenFileName);
810 lstrcpyW(wszFileName, szOpenFileName);
811 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
812 registry_set_filelist(szOpenFileName, hMainWnd);
813 update_font_list();
816 static void ShowWriteError(DWORD Code)
818 LPWSTR Message;
820 switch(Code)
822 case ERROR_ACCESS_DENIED:
823 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
824 break;
826 default:
827 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
829 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
832 static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
834 HANDLE hFile;
835 EDITSTREAM stream;
836 LRESULT ret;
838 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
839 FILE_ATTRIBUTE_NORMAL, NULL);
841 if(hFile == INVALID_HANDLE_VALUE)
843 ShowWriteError(GetLastError());
844 return;
847 if(format == (SF_TEXT | SF_UNICODE))
849 static const BYTE unicode[] = {0xff,0xfe};
850 DWORD writeOut;
851 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
853 if(writeOut != sizeof(unicode))
855 CloseHandle(hFile);
856 return;
860 stream.dwCookie = (DWORD_PTR)hFile;
861 stream.pfnCallback = stream_out;
863 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
865 CloseHandle(hFile);
867 SetFocus(hEditorWnd);
869 if(!ret)
871 GETTEXTLENGTHEX gt;
872 gt.flags = GTL_DEFAULT;
873 gt.codepage = 1200;
875 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
876 return;
879 lstrcpyW(wszFileName, wszSaveFileName);
880 set_caption(wszFileName);
881 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
882 set_fileformat(format);
885 static void DialogSaveFile(void)
887 OPENFILENAMEW sfn;
889 WCHAR wszFile[MAX_PATH] = {'\0'};
890 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
892 ZeroMemory(&sfn, sizeof(sfn));
894 sfn.lStructSize = sizeof(sfn);
895 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
896 sfn.hwndOwner = hMainWnd;
897 sfn.lpstrFilter = wszFilter;
898 sfn.lpstrFile = wszFile;
899 sfn.nMaxFile = MAX_PATH;
900 sfn.lpstrDefExt = wszDefExt;
901 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
903 while(GetSaveFileNameW(&sfn))
905 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
907 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
908 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
910 continue;
911 } else
913 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
914 break;
916 } else
918 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
919 break;
924 static BOOL prompt_save_changes(void)
926 if(!wszFileName[0])
928 GETTEXTLENGTHEX gt;
929 gt.flags = GTL_NUMCHARS;
930 gt.codepage = 1200;
931 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
932 return TRUE;
935 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
937 return TRUE;
938 } else
940 LPWSTR displayFileName;
941 WCHAR *text;
942 int ret;
944 if(!wszFileName[0])
945 displayFileName = wszDefaultFileName;
946 else
947 displayFileName = file_basename(wszFileName);
949 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
950 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
952 if(!text)
953 return FALSE;
955 wsprintfW(text, wszSaveChanges, displayFileName);
957 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
959 HeapFree(GetProcessHeap(), 0, text);
961 switch(ret)
963 case IDNO:
964 return TRUE;
966 case IDYES:
967 if(wszFileName[0])
968 DoSaveFile(wszFileName, fileFormat);
969 else
970 DialogSaveFile();
971 return TRUE;
973 default:
974 return FALSE;
979 static void DialogOpenFile(void)
981 OPENFILENAMEW ofn;
983 WCHAR wszFile[MAX_PATH] = {'\0'};
984 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
986 ZeroMemory(&ofn, sizeof(ofn));
988 ofn.lStructSize = sizeof(ofn);
989 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
990 ofn.hwndOwner = hMainWnd;
991 ofn.lpstrFilter = wszFilter;
992 ofn.lpstrFile = wszFile;
993 ofn.nMaxFile = MAX_PATH;
994 ofn.lpstrDefExt = wszDefExt;
995 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
997 if(GetOpenFileNameW(&ofn))
999 if(prompt_save_changes())
1000 DoOpenFile(ofn.lpstrFile);
1004 static void dialog_about(void)
1006 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1007 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1010 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1012 switch(message)
1014 case WM_INITDIALOG:
1016 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1017 int wrap = -1;
1018 char id[4];
1019 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1021 sprintf(id, "%d\n", (int)ps->lParam);
1022 SetWindowTextA(hIdWnd, id);
1023 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1024 wrap = IDC_PAGEFMT_WN;
1025 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1026 wrap = IDC_PAGEFMT_WW;
1027 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1028 wrap = IDC_PAGEFMT_WM;
1030 if(wrap != -1)
1031 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1032 IDC_PAGEFMT_WM, wrap);
1034 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1035 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1036 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1037 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1038 if(barState[ps->lParam] & (1 << BANDID_RULER))
1039 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1040 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1041 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1043 break;
1045 case WM_COMMAND:
1046 switch(LOWORD(wParam))
1048 case IDC_PAGEFMT_WN:
1049 case IDC_PAGEFMT_WW:
1050 case IDC_PAGEFMT_WM:
1051 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1052 LOWORD(wParam));
1053 break;
1055 case IDC_PAGEFMT_TB:
1056 case IDC_PAGEFMT_FB:
1057 case IDC_PAGEFMT_RU:
1058 case IDC_PAGEFMT_SB:
1059 CheckDlgButton(hWnd, LOWORD(wParam),
1060 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1061 break;
1063 break;
1064 case WM_NOTIFY:
1066 LPNMHDR header = (LPNMHDR)lParam;
1067 if(header->code == PSN_APPLY)
1069 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1070 char sid[4];
1071 int id;
1073 GetWindowTextA(hIdWnd, sid, 4);
1074 id = atoi(sid);
1075 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1076 wordWrap[id] = ID_WORDWRAP_NONE;
1077 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1078 wordWrap[id] = ID_WORDWRAP_WINDOW;
1079 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1080 wordWrap[id] = ID_WORDWRAP_MARGIN;
1082 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1083 barState[id] |= (1 << BANDID_TOOLBAR);
1084 else
1085 barState[id] &= ~(1 << BANDID_TOOLBAR);
1087 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1088 barState[id] |= (1 << BANDID_FORMATBAR);
1089 else
1090 barState[id] &= ~(1 << BANDID_FORMATBAR);
1092 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1093 barState[id] |= (1 << BANDID_RULER);
1094 else
1095 barState[id] &= ~(1 << BANDID_RULER);
1097 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1098 barState[id] |= (1 << BANDID_STATUSBAR);
1099 else
1100 barState[id] &= ~(1 << BANDID_STATUSBAR);
1103 break;
1105 return FALSE;
1108 static void dialog_viewproperties(void)
1110 PROPSHEETPAGEW psp[2];
1111 PROPSHEETHEADERW psh;
1112 size_t i;
1113 HINSTANCE hInstance = GetModuleHandleW(0);
1114 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1116 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1117 psp[0].dwFlags = PSP_USETITLE;
1118 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1119 psp[0].pfnDlgProc = formatopts_proc;
1120 psp[0].hInstance = hInstance;
1121 psp[0].lParam = reg_formatindex(SF_TEXT);
1122 psp[0].pfnCallback = NULL;
1123 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1124 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1126 psp[i].dwSize = psp[0].dwSize;
1127 psp[i].dwFlags = psp[0].dwFlags;
1128 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1129 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1130 psp[i].hInstance = psp[0].hInstance;
1131 psp[i].lParam = reg_formatindex(SF_RTF);
1132 psp[i].pfnCallback = psp[0].pfnCallback;
1133 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1136 psh.dwSize = sizeof(psh);
1137 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1138 psh.hwndParent = hMainWnd;
1139 psh.hInstance = hInstance;
1140 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1141 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1142 U3(psh).ppsp = ppsp;
1143 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1145 if(fileFormat & SF_RTF)
1146 U2(psh).nStartPage = 1;
1147 else
1148 U2(psh).nStartPage = 0;
1149 PropertySheetW(&psh);
1150 set_bar_states();
1151 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1154 static void HandleCommandLine(LPWSTR cmdline)
1156 WCHAR delimiter;
1157 int opt_print = 0;
1159 /* skip white space */
1160 while (*cmdline == ' ') cmdline++;
1162 /* skip executable name */
1163 delimiter = (*cmdline == '"' ? '"' : ' ');
1165 if (*cmdline == delimiter) cmdline++;
1166 while (*cmdline && *cmdline != delimiter) cmdline++;
1167 if (*cmdline == delimiter) cmdline++;
1169 while (*cmdline)
1171 while (isspace(*cmdline)) cmdline++;
1173 if (*cmdline == '-' || *cmdline == '/')
1175 if (!cmdline[2] || isspace(cmdline[2]))
1177 switch (cmdline[1])
1179 case 'P':
1180 case 'p':
1181 opt_print = 1;
1182 cmdline += 2;
1183 continue;
1186 /* a filename starting by / */
1188 break;
1191 if (*cmdline)
1193 /* file name is passed on the command line */
1194 if (cmdline[0] == '"')
1196 cmdline++;
1197 cmdline[lstrlenW(cmdline) - 1] = 0;
1199 DoOpenFile(cmdline);
1200 InvalidateRect(hMainWnd, NULL, FALSE);
1203 if (opt_print)
1204 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1207 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1209 if(pFr->Flags & FR_DIALOGTERM)
1211 hFindWnd = 0;
1212 pFr->Flags = FR_FINDNEXT;
1213 return 0;
1216 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1218 DWORD flags = FR_DOWN;
1219 FINDTEXTW ft;
1220 static CHARRANGE cr;
1221 LRESULT end, ret;
1222 GETTEXTLENGTHEX gt;
1223 LRESULT length;
1224 int startPos;
1225 HMENU hMenu = GetMenu(hMainWnd);
1226 MENUITEMINFOW mi;
1228 mi.cbSize = sizeof(mi);
1229 mi.fMask = MIIM_DATA;
1230 mi.dwItemData = 1;
1231 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1233 gt.flags = GTL_NUMCHARS;
1234 gt.codepage = 1200;
1236 length = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
1238 if(pFr->lCustData == -1)
1240 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&startPos, (LPARAM)&end);
1241 cr.cpMin = startPos;
1242 pFr->lCustData = startPos;
1243 cr.cpMax = length;
1244 if(cr.cpMin == length)
1245 cr.cpMin = 0;
1246 } else
1248 startPos = pFr->lCustData;
1251 if(cr.cpMax > length)
1253 startPos = 0;
1254 cr.cpMin = 0;
1255 cr.cpMax = length;
1258 ft.chrg = cr;
1259 ft.lpstrText = pFr->lpstrFindWhat;
1261 if(pFr->Flags & FR_MATCHCASE)
1262 flags |= FR_MATCHCASE;
1263 if(pFr->Flags & FR_WHOLEWORD)
1264 flags |= FR_WHOLEWORD;
1266 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, flags, (LPARAM)&ft);
1268 if(ret == -1)
1270 if(cr.cpMax == length && cr.cpMax != startPos)
1272 ft.chrg.cpMin = cr.cpMin = 0;
1273 ft.chrg.cpMax = cr.cpMax = startPos;
1275 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, flags, (LPARAM)&ft);
1279 if(ret == -1)
1281 pFr->lCustData = -1;
1282 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED), wszAppTitle,
1283 MB_OK | MB_ICONASTERISK);
1284 } else
1286 end = ret + lstrlenW(pFr->lpstrFindWhat);
1287 cr.cpMin = end;
1288 SendMessageW(hEditorWnd, EM_SETSEL, ret, end);
1289 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1291 if(pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1292 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1294 if(pFr->Flags & FR_REPLACEALL)
1295 handle_findmsg(pFr);
1299 return 0;
1302 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1304 static WCHAR findBuffer[MAX_STRING_LEN];
1305 static WCHAR replaceBuffer[MAX_STRING_LEN];
1307 /* Allow only one search/replace dialog to open */
1308 if(hFindWnd != NULL)
1310 SetActiveWindow(hFindWnd);
1311 return;
1314 ZeroMemory(fr, sizeof(FINDREPLACEW));
1315 fr->lStructSize = sizeof(FINDREPLACEW);
1316 fr->hwndOwner = hMainWnd;
1317 fr->Flags = FR_HIDEUPDOWN;
1318 fr->lpstrFindWhat = findBuffer;
1319 fr->lpstrReplaceWith = replaceBuffer;
1320 fr->lCustData = -1;
1321 fr->wFindWhatLen = sizeof(findBuffer);
1322 fr->wReplaceWithLen = sizeof(replaceBuffer);
1324 if(replace)
1325 hFindWnd = ReplaceTextW(fr);
1326 else
1327 hFindWnd = FindTextW(fr);
1330 static int units_to_twips(UNIT unit, float number)
1332 int twips = 0;
1334 switch(unit)
1336 case UNIT_CM:
1337 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1338 break;
1340 case UNIT_INCH:
1341 twips = (int)(number * (float)TWIPS_PER_INCH);
1342 break;
1344 case UNIT_PT:
1345 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1346 break;
1349 return twips;
1352 static void append_current_units(LPWSTR buffer)
1354 static const WCHAR space[] = {' ', 0};
1355 lstrcatW(buffer, space);
1356 lstrcatW(buffer, units_cmW);
1359 static void number_with_units(LPWSTR buffer, int number)
1361 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1362 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1364 sprintfW(buffer, fmt, converted, units_cmW);
1367 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1369 COMBOBOXEXITEMW cbItem;
1370 COMBOBOXINFO cbInfo;
1371 HWND hCombo, hList;
1372 int idx, result;
1374 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1375 if (!hCombo)
1376 return FALSE;
1377 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1378 result = SendMessage(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1379 if (!result)
1380 return FALSE;
1381 hList = cbInfo.hwndList;
1382 idx = SendMessage(hList, LB_GETCURSEL, 0, 0);
1383 if (idx < 0)
1384 return FALSE;
1386 ZeroMemory(&cbItem, sizeof(cbItem));
1387 cbItem.mask = CBEIF_TEXT;
1388 cbItem.iItem = idx;
1389 cbItem.pszText = wszBuffer;
1390 cbItem.cchTextMax = bufferLength-1;
1391 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1393 return result != 0;
1396 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1398 switch(message)
1400 case WM_INITDIALOG:
1402 WCHAR buffer[MAX_STRING_LEN];
1403 SYSTEMTIME st;
1404 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1405 GetLocalTime(&st);
1407 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1408 MAX_STRING_LEN);
1409 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1410 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1411 MAX_STRING_LEN);
1412 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1413 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1414 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1416 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1418 break;
1420 case WM_COMMAND:
1421 switch(LOWORD(wParam))
1423 case IDC_DATETIME:
1424 if (HIWORD(wParam) != LBN_DBLCLK)
1425 break;
1426 /* Fall through */
1428 case IDOK:
1430 LRESULT index;
1431 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1433 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1435 if(index != LB_ERR)
1437 WCHAR buffer[MAX_STRING_LEN];
1438 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1439 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1442 /* Fall through */
1444 case IDCANCEL:
1445 EndDialog(hWnd, wParam);
1446 return TRUE;
1449 return FALSE;
1452 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1454 switch(message)
1456 case WM_INITDIALOG:
1458 HINSTANCE hInstance = GetModuleHandleW(0);
1459 WCHAR buffer[MAX_STRING_LEN];
1460 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1462 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1463 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1464 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1465 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1466 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1467 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1469 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1471 break;
1473 case WM_COMMAND:
1474 switch(LOWORD(wParam))
1476 case IDOK:
1478 LRESULT index;
1479 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1480 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1482 if(index != LB_ERR)
1483 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1485 return TRUE;
1487 case IDCANCEL:
1488 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1489 return TRUE;
1492 return FALSE;
1495 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1497 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1499 switch(message)
1501 case WM_INITDIALOG:
1503 HINSTANCE hInstance = GetModuleHandleW(0);
1504 WCHAR buffer[MAX_STRING_LEN];
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 PARAFORMAT2 pf;
1510 int index = 0;
1512 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1513 MAX_STRING_LEN);
1514 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1515 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1516 MAX_STRING_LEN);
1517 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1518 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1519 MAX_STRING_LEN);
1520 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1522 pf.cbSize = sizeof(pf);
1523 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1524 PFM_STARTINDENT;
1525 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1527 if(pf.wAlignment == PFA_RIGHT)
1528 index ++;
1529 else if(pf.wAlignment == PFA_CENTER)
1530 index += 2;
1532 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1534 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1535 SetWindowTextW(hLeftWnd, buffer);
1536 number_with_units(buffer, pf.dxRightIndent);
1537 SetWindowTextW(hRightWnd, buffer);
1538 number_with_units(buffer, -pf.dxOffset);
1539 SetWindowTextW(hFirstWnd, buffer);
1541 break;
1543 case WM_COMMAND:
1544 switch(LOWORD(wParam))
1546 case IDOK:
1548 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1549 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1550 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1551 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1552 WCHAR buffer[MAX_STRING_LEN];
1553 int index;
1554 float num;
1555 int ret = 0;
1556 PARAFORMAT pf;
1557 UNIT unit;
1559 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1560 pf.wAlignment = ALIGNMENT_VALUES[index];
1562 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1563 if(number_from_string(buffer, &num, &unit))
1564 ret++;
1565 pf.dxOffset = units_to_twips(unit, num);
1566 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1567 if(number_from_string(buffer, &num, &unit))
1568 ret++;
1569 pf.dxRightIndent = units_to_twips(unit, num);
1570 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1571 if(number_from_string(buffer, &num, &unit))
1572 ret++;
1573 pf.dxStartIndent = units_to_twips(unit, num);
1575 if(ret != 3)
1577 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1578 wszAppTitle, MB_OK | MB_ICONASTERISK);
1579 return FALSE;
1580 } else
1582 if (pf.dxOffset + pf.dxStartIndent < 0
1583 && pf.dxStartIndent < 0)
1585 /* The first line is before the left edge, so
1586 * make sure it is at the left edge. */
1587 pf.dxOffset = -pf.dxStartIndent;
1588 } else if (pf.dxOffset < 0) {
1589 /* The second and following lines are before
1590 * the left edge, so set it to be at the left
1591 * edge, and adjust the first line since it
1592 * is relative to it. */
1593 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1594 pf.dxOffset = 0;
1596 /* Internally the dxStartIndent is the absolute
1597 * offset for the first line and dxOffset is
1598 * to it value as opposed how it is displayed with
1599 * the first line being the relative value.
1600 * These two lines make the adjustments. */
1601 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1602 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1604 pf.cbSize = sizeof(pf);
1605 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1606 PFM_STARTINDENT;
1607 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1610 /* Fall through */
1612 case IDCANCEL:
1613 EndDialog(hWnd, wParam);
1614 return TRUE;
1617 return FALSE;
1620 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1622 switch(message)
1624 case WM_INITDIALOG:
1626 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1627 PARAFORMAT pf;
1628 WCHAR buffer[MAX_STRING_LEN];
1629 int i;
1631 pf.cbSize = sizeof(pf);
1632 pf.dwMask = PFM_TABSTOPS;
1633 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1634 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1636 for(i = 0; i < pf.cTabCount; i++)
1638 number_with_units(buffer, pf.rgxTabs[i]);
1639 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1641 SetFocus(hTabWnd);
1643 break;
1645 case WM_COMMAND:
1646 switch(LOWORD(wParam))
1648 case IDC_TABSTOPS:
1650 HWND hTabWnd = (HWND)lParam;
1651 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1652 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1653 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1655 if(GetWindowTextLengthW(hTabWnd))
1656 EnableWindow(hAddWnd, TRUE);
1657 else
1658 EnableWindow(hAddWnd, FALSE);
1660 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1662 EnableWindow(hEmptyWnd, TRUE);
1664 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1665 EnableWindow(hDelWnd, FALSE);
1666 else
1667 EnableWindow(hDelWnd, TRUE);
1668 } else
1670 EnableWindow(hEmptyWnd, FALSE);
1673 break;
1675 case ID_TAB_ADD:
1677 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1678 WCHAR buffer[MAX_STRING_LEN];
1679 UNIT unit;
1681 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1682 append_current_units(buffer);
1684 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1686 float number = 0;
1687 int item_count = SendMessage(hTabWnd, CB_GETCOUNT, 0, 0);
1689 if(!number_from_string(buffer, &number, &unit))
1691 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1692 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1693 } else if (item_count >= MAX_TAB_STOPS) {
1694 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1695 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1696 } else {
1697 int i;
1698 float next_number = -1;
1699 int next_number_in_twips = -1;
1700 int insert_number = units_to_twips(unit, number);
1702 /* linear search for position to insert the string */
1703 for(i = 0; i < item_count; i++)
1705 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1706 number_from_string(buffer, &next_number, &unit);
1707 next_number_in_twips = units_to_twips(unit, next_number);
1708 if (insert_number <= next_number_in_twips)
1709 break;
1711 if (insert_number != next_number_in_twips)
1713 number_with_units(buffer, insert_number);
1714 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1715 SetWindowTextW(hTabWnd, 0);
1719 SetFocus(hTabWnd);
1721 break;
1723 case ID_TAB_DEL:
1725 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1726 LRESULT ret;
1727 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1728 if(ret != CB_ERR)
1729 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1731 break;
1733 case ID_TAB_EMPTY:
1735 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1736 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1737 SetFocus(hTabWnd);
1739 break;
1741 case IDOK:
1743 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1744 int i;
1745 WCHAR buffer[MAX_STRING_LEN];
1746 PARAFORMAT pf;
1747 float number;
1748 UNIT unit;
1750 pf.cbSize = sizeof(pf);
1751 pf.dwMask = PFM_TABSTOPS;
1753 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1754 (LPARAM)&buffer) != CB_ERR &&
1755 i < MAX_TAB_STOPS; i++)
1757 number_from_string(buffer, &number, &unit);
1758 pf.rgxTabs[i] = units_to_twips(unit, number);
1760 pf.cTabCount = i;
1761 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1763 /* Fall through */
1764 case IDCANCEL:
1765 EndDialog(hWnd, wParam);
1766 return TRUE;
1769 return FALSE;
1772 static int context_menu(LPARAM lParam)
1774 int x = (int)(short)LOWORD(lParam);
1775 int y = (int)(short)HIWORD(lParam);
1776 HMENU hPop = GetSubMenu(hPopupMenu, 0);
1778 if(x == -1)
1780 int from = 0, to = 0;
1781 POINTL pt;
1782 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1783 SendMessageW(hEditorWnd, EM_POSFROMCHAR, (WPARAM)&pt, to);
1784 ClientToScreen(hEditorWnd, (POINT*)&pt);
1785 x = pt.x;
1786 y = pt.y;
1789 TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
1790 x, y, 0, hMainWnd, 0);
1792 return 0;
1795 static LRESULT OnCreate( HWND hWnd )
1797 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1798 HINSTANCE hInstance = GetModuleHandleW(0);
1799 HANDLE hDLL;
1800 TBADDBITMAP ab;
1801 int nStdBitmaps = 0;
1802 REBARINFO rbi;
1803 REBARBANDINFOW rbb;
1804 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1805 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1807 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1809 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1810 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1811 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1813 rbi.cbSize = sizeof(rbi);
1814 rbi.fMask = 0;
1815 rbi.himl = NULL;
1816 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1817 return -1;
1819 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
1820 IDC_TOOLBAR,
1821 1, hInstance, IDB_TOOLBAR,
1822 NULL, 0,
1823 24, 24, 16, 16, sizeof(TBBUTTON));
1825 ab.hInst = HINST_COMMCTRL;
1826 ab.nID = IDB_STD_SMALL_COLOR;
1827 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1829 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1830 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1831 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1832 AddSeparator(hToolBarWnd);
1833 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1834 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1835 AddSeparator(hToolBarWnd);
1836 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1837 AddSeparator(hToolBarWnd);
1838 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1839 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1840 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1841 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1842 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1843 AddSeparator(hToolBarWnd);
1844 AddButton(hToolBarWnd, 0, ID_DATETIME);
1846 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1848 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1849 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1850 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1851 rbb.cx = 0;
1852 rbb.hwndChild = hToolBarWnd;
1853 rbb.cxMinChild = 0;
1854 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1855 rbb.wID = BANDID_TOOLBAR;
1857 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1859 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1860 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1861 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1863 rbb.hwndChild = hFontListWnd;
1864 rbb.cx = 200;
1865 rbb.wID = BANDID_FONTLIST;
1867 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1869 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1870 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1871 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1873 rbb.hwndChild = hSizeListWnd;
1874 rbb.cx = 50;
1875 rbb.fStyle ^= RBBS_BREAK;
1876 rbb.wID = BANDID_SIZELIST;
1878 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1880 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1881 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
1882 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1884 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1885 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1886 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1887 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1888 AddSeparator(hFormatBarWnd);
1889 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1890 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1891 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1892 AddSeparator(hFormatBarWnd);
1893 AddButton(hFormatBarWnd, 7, ID_BULLET);
1895 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1897 rbb.hwndChild = hFormatBarWnd;
1898 rbb.wID = BANDID_FORMATBAR;
1900 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1902 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1903 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1906 rbb.hwndChild = hRulerWnd;
1907 rbb.wID = BANDID_RULER;
1908 rbb.fStyle |= RBBS_BREAK;
1910 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1912 hDLL = LoadLibraryW(wszRichEditDll);
1913 if(!hDLL)
1915 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1916 MB_OK | MB_ICONEXCLAMATION);
1917 PostQuitMessage(1);
1920 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1921 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1922 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1923 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1925 if (!hEditorWnd)
1927 fprintf(stderr, "Error code %u\n", GetLastError());
1928 return -1;
1930 assert(hEditorWnd);
1932 SetFocus(hEditorWnd);
1933 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1935 set_default_font();
1937 populate_font_list(hFontListWnd);
1938 populate_size_list(hSizeListWnd);
1939 DoLoadStrings();
1940 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1942 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1944 registry_read_filelist(hWnd);
1945 registry_read_formatopts_all(barState, wordWrap);
1946 registry_read_options();
1947 DragAcceptFiles(hWnd, TRUE);
1949 return 0;
1952 static LRESULT OnUser( HWND hWnd )
1954 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1955 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1956 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1957 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1958 int from, to;
1959 CHARFORMAT2W fmt;
1960 PARAFORMAT2 pf;
1961 GETTEXTLENGTHEX gt;
1963 ZeroMemory(&fmt, sizeof(fmt));
1964 fmt.cbSize = sizeof(fmt);
1966 ZeroMemory(&pf, sizeof(pf));
1967 pf.cbSize = sizeof(pf);
1969 gt.flags = GTL_NUMCHARS;
1970 gt.codepage = 1200;
1972 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1973 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1975 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1977 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1978 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1979 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1980 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1981 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1982 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1983 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
1985 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
1986 (fmt.dwEffects & CFE_BOLD));
1987 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
1988 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
1989 (fmt.dwEffects & CFE_ITALIC));
1990 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
1991 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
1992 (fmt.dwEffects & CFE_UNDERLINE));
1993 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
1995 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1996 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
1997 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
1998 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2000 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
2001 return 0;
2004 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2006 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2007 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2008 NMHDR *pHdr = (NMHDR *)lParam;
2009 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2010 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2012 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2014 if (pHdr->code == CBEN_ENDEDITW)
2016 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
2017 if(pHdr->hwndFrom == hwndFontList)
2019 on_fontlist_modified((LPWSTR)endEdit->szText);
2020 } else if (pHdr->hwndFrom == hwndSizeList)
2022 on_sizelist_modified(hwndFontList,(LPWSTR)endEdit->szText);
2025 return 0;
2028 if (pHdr->hwndFrom != hwndEditor)
2029 return 0;
2031 if (pHdr->code == EN_SELCHANGE)
2033 SELCHANGE *pSC = (SELCHANGE *)lParam;
2034 char buf[128];
2036 update_font_list();
2038 sprintf( buf,"selection = %d..%d, line count=%ld",
2039 pSC->chrg.cpMin, pSC->chrg.cpMax,
2040 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
2041 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2042 SendMessage(hWnd, WM_USER, 0, 0);
2043 return 1;
2045 return 0;
2048 /* Copied from dlls/comdlg32/fontdlg.c */
2049 static const COLORREF textcolors[]=
2051 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2052 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2053 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2054 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2057 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2059 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2060 static FINDREPLACEW findreplace;
2062 if ((HWND)lParam == hwndEditor)
2063 return 0;
2065 switch(LOWORD(wParam))
2068 case ID_FILE_EXIT:
2069 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2070 break;
2072 case ID_FILE_NEW:
2074 HINSTANCE hInstance = GetModuleHandleW(0);
2075 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
2076 newfile_proc);
2078 if(ret != ID_NEWFILE_ABORT)
2080 if(prompt_save_changes())
2082 SETTEXTEX st;
2084 set_caption(NULL);
2085 wszFileName[0] = '\0';
2087 clear_formatting();
2089 st.flags = ST_DEFAULT;
2090 st.codepage = 1200;
2091 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2093 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2094 set_fileformat(ret);
2095 update_font_list();
2099 break;
2101 case ID_FILE_OPEN:
2102 DialogOpenFile();
2103 break;
2105 case ID_FILE_SAVE:
2106 if(wszFileName[0])
2108 DoSaveFile(wszFileName, fileFormat);
2109 break;
2111 /* Fall through */
2113 case ID_FILE_SAVEAS:
2114 DialogSaveFile();
2115 break;
2117 case ID_FILE_RECENT1:
2118 case ID_FILE_RECENT2:
2119 case ID_FILE_RECENT3:
2120 case ID_FILE_RECENT4:
2122 HMENU hMenu = GetMenu(hWnd);
2123 MENUITEMINFOW mi;
2125 mi.cbSize = sizeof(MENUITEMINFOW);
2126 mi.fMask = MIIM_DATA;
2127 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2128 DoOpenFile((LPWSTR)mi.dwItemData);
2130 break;
2132 case ID_FIND:
2133 dialog_find(&findreplace, FALSE);
2134 break;
2136 case ID_FIND_NEXT:
2137 handle_findmsg(&findreplace);
2138 break;
2140 case ID_REPLACE:
2141 dialog_find(&findreplace, TRUE);
2142 break;
2144 case ID_FONTSETTINGS:
2145 dialog_choose_font();
2146 break;
2148 case ID_PRINT:
2149 dialog_print(hWnd, wszFileName);
2150 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2151 break;
2153 case ID_PRINT_QUICK:
2154 print_quick(hMainWnd, wszFileName);
2155 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2156 break;
2158 case ID_PREVIEW:
2160 int index = reg_formatindex(fileFormat);
2161 DWORD tmp = barState[index];
2162 barState[index] = 1 << BANDID_STATUSBAR;
2163 set_bar_states();
2164 barState[index] = tmp;
2165 ShowWindow(hEditorWnd, FALSE);
2167 init_preview(hWnd, wszFileName);
2169 SetMenu(hWnd, NULL);
2170 InvalidateRect(0, 0, TRUE);
2172 break;
2174 case ID_PRINTSETUP:
2175 dialog_printsetup(hWnd);
2176 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2177 break;
2179 case ID_FORMAT_BOLD:
2180 case ID_FORMAT_ITALIC:
2181 case ID_FORMAT_UNDERLINE:
2183 CHARFORMAT2W fmt;
2184 int effects = CFE_BOLD;
2186 ZeroMemory(&fmt, sizeof(fmt));
2187 fmt.cbSize = sizeof(fmt);
2188 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2190 fmt.dwMask = CFM_BOLD;
2192 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2194 effects = CFE_ITALIC;
2195 fmt.dwMask = CFM_ITALIC;
2196 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2198 effects = CFE_UNDERLINE;
2199 fmt.dwMask = CFM_UNDERLINE;
2202 fmt.dwEffects ^= effects;
2204 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2205 break;
2208 case ID_FORMAT_COLOR:
2210 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2211 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2212 HMENU hPop;
2213 RECT itemrc;
2214 POINT pt;
2215 int mid;
2216 int itemidx = SendMessage(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2218 SendMessage(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2219 pt.x = itemrc.left;
2220 pt.y = itemrc.bottom;
2221 ClientToScreen(hFormatBarWnd, &pt);
2222 hPop = GetSubMenu(hColorPopupMenu, 0);
2223 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2224 TPM_RETURNCMD | TPM_NONOTIFY,
2225 pt.x, pt.y, 0, hWnd, 0);
2226 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2228 CHARFORMAT2W fmt;
2230 ZeroMemory(&fmt, sizeof(fmt));
2231 fmt.cbSize = sizeof(fmt);
2232 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2234 fmt.dwMask = CFM_COLOR;
2236 if (mid < ID_COLOR_AUTOMATIC) {
2237 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2238 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2239 } else {
2240 fmt.dwEffects |= CFE_AUTOCOLOR;
2243 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2245 break;
2248 case ID_EDIT_CUT:
2249 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2250 break;
2252 case ID_EDIT_COPY:
2253 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2254 break;
2256 case ID_EDIT_PASTE:
2257 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2258 break;
2260 case ID_EDIT_CLEAR:
2261 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2262 break;
2264 case ID_EDIT_SELECTALL:
2266 CHARRANGE range = {0, -1};
2267 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2268 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2269 return 0;
2272 case ID_EDIT_GETTEXT:
2274 int nLen = GetWindowTextLengthW(hwndEditor);
2275 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2276 TEXTRANGEW tr;
2278 GetWindowTextW(hwndEditor, data, nLen+1);
2279 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2281 HeapFree( GetProcessHeap(), 0, data);
2282 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2283 tr.chrg.cpMin = 0;
2284 tr.chrg.cpMax = nLen;
2285 tr.lpstrText = data;
2286 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2287 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2288 HeapFree( GetProcessHeap(), 0, data );
2290 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2291 return 0;
2294 case ID_EDIT_CHARFORMAT:
2295 case ID_EDIT_DEFCHARFORMAT:
2297 CHARFORMAT2W cf;
2299 ZeroMemory(&cf, sizeof(cf));
2300 cf.cbSize = sizeof(cf);
2301 cf.dwMask = 0;
2302 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2303 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2304 return 0;
2307 case ID_EDIT_PARAFORMAT:
2309 PARAFORMAT2 pf;
2310 ZeroMemory(&pf, sizeof(pf));
2311 pf.cbSize = sizeof(pf);
2312 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2313 return 0;
2316 case ID_EDIT_SELECTIONINFO:
2318 CHARRANGE range = {0, -1};
2319 char buf[128];
2320 WCHAR *data = NULL;
2322 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2323 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2324 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2325 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2326 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2327 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2328 HeapFree( GetProcessHeap(), 0, data);
2329 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2330 return 0;
2333 case ID_EDIT_READONLY:
2335 LONG nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2336 if (nStyle & ES_READONLY)
2337 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2338 else
2339 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2340 return 0;
2343 case ID_EDIT_MODIFIED:
2344 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2345 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2346 else
2347 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2348 return 0;
2350 case ID_EDIT_UNDO:
2351 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2352 return 0;
2354 case ID_EDIT_REDO:
2355 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2356 return 0;
2358 case ID_BULLET:
2360 PARAFORMAT pf;
2362 pf.cbSize = sizeof(pf);
2363 pf.dwMask = PFM_NUMBERING;
2364 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2366 pf.dwMask |= PFM_OFFSET;
2368 if(pf.wNumbering == PFN_BULLET)
2370 pf.wNumbering = 0;
2371 pf.dxOffset = 0;
2372 } else
2374 pf.wNumbering = PFN_BULLET;
2375 pf.dxOffset = 720;
2378 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2380 break;
2382 case ID_ALIGN_LEFT:
2383 case ID_ALIGN_CENTER:
2384 case ID_ALIGN_RIGHT:
2386 PARAFORMAT2 pf;
2388 pf.cbSize = sizeof(pf);
2389 pf.dwMask = PFM_ALIGNMENT;
2390 switch(LOWORD(wParam)) {
2391 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2392 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2393 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2395 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2396 break;
2399 case ID_BACK_1:
2400 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2401 break;
2403 case ID_BACK_2:
2404 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2405 break;
2407 case ID_TOGGLE_TOOLBAR:
2408 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2409 update_window();
2410 break;
2412 case ID_TOGGLE_FORMATBAR:
2413 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2414 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2415 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2416 update_window();
2417 break;
2419 case ID_TOGGLE_STATUSBAR:
2420 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2421 update_window();
2422 break;
2424 case ID_TOGGLE_RULER:
2425 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2426 update_window();
2427 break;
2429 case ID_DATETIME:
2430 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2431 break;
2433 case ID_PARAFORMAT:
2434 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2435 break;
2437 case ID_TABSTOPS:
2438 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2439 break;
2441 case ID_ABOUT:
2442 dialog_about();
2443 break;
2445 case ID_VIEWPROPERTIES:
2446 dialog_viewproperties();
2447 break;
2449 case IDC_FONTLIST:
2450 if (HIWORD(wParam) == CBN_SELENDOK)
2452 WCHAR buffer[LF_FACESIZE];
2453 HWND hwndFontList = (HWND)lParam;
2454 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2455 on_fontlist_modified(buffer);
2457 break;
2459 case IDC_SIZELIST:
2460 if (HIWORD(wParam) == CBN_SELENDOK)
2462 WCHAR buffer[MAX_STRING_LEN+1];
2463 HWND hwndSizeList = (HWND)lParam;
2464 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2465 on_sizelist_modified(hwndSizeList, buffer);
2467 break;
2469 default:
2470 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2471 break;
2473 return 0;
2476 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2478 HMENU hMenu = (HMENU)wParam;
2479 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2480 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2481 PARAFORMAT pf;
2482 int nAlignment = -1;
2483 int selFrom, selTo;
2484 GETTEXTLENGTHEX gt;
2485 LRESULT textLength;
2486 MENUITEMINFOW mi;
2488 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2489 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2490 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2492 pf.cbSize = sizeof(PARAFORMAT);
2493 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2494 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2495 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
2496 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2497 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
2498 if (pf.dwMask & PFM_ALIGNMENT)
2499 nAlignment = pf.wAlignment;
2500 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
2501 MF_CHECKED : MF_UNCHECKED);
2502 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
2503 MF_CHECKED : MF_UNCHECKED);
2504 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
2505 MF_CHECKED : MF_UNCHECKED);
2506 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
2507 MF_CHECKED : MF_UNCHECKED));
2508 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
2509 MF_ENABLED : MF_GRAYED);
2510 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
2511 MF_ENABLED : MF_GRAYED);
2513 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
2514 MF_CHECKED : MF_UNCHECKED);
2516 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
2517 MF_CHECKED : MF_UNCHECKED);
2519 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
2520 MF_CHECKED : MF_UNCHECKED);
2522 CheckMenuItem(hMenu, ID_TOGGLE_RULER, MF_BYCOMMAND|(is_bar_visible(BANDID_RULER)) ? MF_CHECKED : MF_UNCHECKED);
2524 gt.flags = GTL_NUMCHARS;
2525 gt.codepage = 1200;
2526 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2527 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2529 mi.cbSize = sizeof(mi);
2530 mi.fMask = MIIM_DATA;
2532 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2534 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
2535 MF_ENABLED : MF_GRAYED));
2537 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2539 return 0;
2542 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2544 int nStatusSize = 0;
2545 RECT rc;
2546 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2547 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2548 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2549 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2550 int rebarHeight = 0;
2552 if (hwndStatusBar)
2554 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2555 if (IsWindowVisible(hwndStatusBar))
2557 GetClientRect(hwndStatusBar, &rc);
2558 nStatusSize = rc.bottom - rc.top;
2559 } else
2561 nStatusSize = 0;
2564 if (hwndReBar)
2566 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2568 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2570 if (hwndEditor)
2572 GetClientRect(hWnd, &rc);
2573 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2576 redraw_ruler(hRulerWnd);
2578 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2581 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2583 if(msg == ID_FINDMSGSTRING)
2584 return handle_findmsg((LPFINDREPLACEW)lParam);
2586 switch(msg)
2588 case WM_CREATE:
2589 return OnCreate( hWnd );
2591 case WM_USER:
2592 return OnUser( hWnd );
2594 case WM_NOTIFY:
2595 return OnNotify( hWnd, lParam );
2597 case WM_COMMAND:
2598 if(preview_isactive())
2600 return preview_command( hWnd, wParam );
2603 return OnCommand( hWnd, wParam, lParam );
2605 case WM_DESTROY:
2606 PostQuitMessage(0);
2607 break;
2609 case WM_CLOSE:
2610 if(preview_isactive())
2612 preview_exit(hWnd);
2613 } else if(prompt_save_changes())
2615 registry_set_options(hMainWnd);
2616 registry_set_formatopts_all(barState);
2617 PostQuitMessage(0);
2619 break;
2621 case WM_ACTIVATE:
2622 if (LOWORD(wParam))
2623 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2624 return 0;
2626 case WM_INITMENUPOPUP:
2627 return OnInitPopupMenu( hWnd, wParam );
2629 case WM_SIZE:
2630 return OnSize( hWnd, wParam, lParam );
2632 case WM_CONTEXTMENU:
2633 if((HWND)wParam == hEditorWnd)
2634 return context_menu(lParam);
2635 else
2636 return DefWindowProcW(hWnd, msg, wParam, lParam);
2638 case WM_DROPFILES:
2640 WCHAR file[MAX_PATH];
2641 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2642 DragFinish((HDROP)wParam);
2644 if(prompt_save_changes())
2645 DoOpenFile(file);
2647 break;
2648 case WM_PAINT:
2649 if(!preview_isactive())
2650 return DefWindowProcW(hWnd, msg, wParam, lParam);
2652 default:
2653 return DefWindowProcW(hWnd, msg, wParam, lParam);
2656 return 0;
2659 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2661 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2662 HACCEL hAccel;
2663 WNDCLASSEXW wc;
2664 MSG msg;
2665 RECT rc;
2666 UINT_PTR hPrevRulerProc;
2667 HWND hRulerWnd;
2668 POINTL EditPoint;
2669 DWORD bMaximized;
2670 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2671 'T','A','B','L','E','\0'};
2673 InitCommonControlsEx(&classes);
2675 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2677 wc.cbSize = sizeof(wc);
2678 wc.style = CS_HREDRAW | CS_VREDRAW;
2679 wc.lpfnWndProc = WndProc;
2680 wc.cbClsExtra = 0;
2681 wc.cbWndExtra = 4;
2682 wc.hInstance = hInstance;
2683 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2684 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2685 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2686 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2687 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2688 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2689 wc.lpszClassName = wszMainWndClass;
2690 RegisterClassExW(&wc);
2692 wc.style = CS_HREDRAW | CS_VREDRAW;
2693 wc.lpfnWndProc = preview_proc;
2694 wc.cbClsExtra = 0;
2695 wc.cbWndExtra = 0;
2696 wc.hInstance = hInstance;
2697 wc.hIcon = NULL;
2698 wc.hIconSm = NULL;
2699 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2700 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2701 wc.lpszMenuName = NULL;
2702 wc.lpszClassName = wszPreviewWndClass;
2703 RegisterClassExW(&wc);
2705 registry_read_winrect(&rc);
2706 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2707 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2708 registry_read_maximized(&bMaximized);
2709 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2710 && bMaximized)
2711 nCmdShow = SW_SHOWMAXIMIZED;
2712 ShowWindow(hMainWnd, nCmdShow);
2714 set_caption(NULL);
2715 set_bar_states();
2716 set_fileformat(SF_RTF);
2717 hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
2718 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2719 get_default_printer_opts();
2720 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2722 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2723 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2724 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2725 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2727 HandleCommandLine(GetCommandLineW());
2729 while(GetMessageW(&msg,0,0,0))
2731 if (IsDialogMessage(hFindWnd, &msg))
2732 continue;
2734 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2735 continue;
2736 TranslateMessage(&msg);
2737 DispatchMessageW(&msg);
2738 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2739 SendMessageW(hMainWnd, WM_USER, 0, 0);
2742 return 0;