2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_IE 0x0400
41 #ifdef NONAMELESSUNION
52 static const WCHAR xszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
54 static const WCHAR wszRichEditClass[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
55 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
56 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
58 static const WCHAR stringFormat[] = {'%','2','d','\0'};
61 static HWND hEditorWnd;
63 static HMENU hPopupMenu;
65 static UINT ID_FINDMSGSTRING;
67 static DWORD wordWrap[2];
68 static DWORD barState[2];
69 static WPARAM fileFormat = SF_RTF;
71 static WCHAR wszFileName[MAX_PATH];
72 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
73 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
74 static WCHAR wszSaveChanges[MAX_STRING_LEN];
75 static WCHAR units_cmW[MAX_STRING_LEN];
77 static char units_cmA[MAX_STRING_LEN];
79 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
81 /* Load string resources */
82 static void DoLoadStrings(void)
85 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
86 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
87 static const WCHAR files_all[] = {'*','.','*','\0'};
89 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
91 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
93 lstrcpyW(p, files_rtf);
95 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
97 lstrcpyW(p, files_txt);
99 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
100 p += lstrlenW(p) + 1;
101 lstrcpyW(p, files_txt);
102 p += lstrlenW(p) + 1;
103 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
104 p += lstrlenW(p) + 1;
105 lstrcpyW(p, files_all);
106 p += lstrlenW(p) + 1;
109 p = wszDefaultFileName;
110 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
113 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
115 LoadStringA(hInstance, STRING_UNITS_CM, units_cmA, MAX_STRING_LEN);
116 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
119 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
123 ZeroMemory(&button, sizeof(button));
124 button.iBitmap = nImage;
125 button.idCommand = nCommand;
126 button.fsState = TBSTATE_ENABLED;
127 button.fsStyle = TBSTYLE_BUTTON;
130 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
133 static void AddSeparator(HWND hwndToolBar)
137 ZeroMemory(&button, sizeof(button));
139 button.idCommand = 0;
141 button.fsStyle = TBSTYLE_SEP;
144 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
147 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
149 HANDLE hFile = (HANDLE)cookie;
152 if(!ReadFile(hFile, buffer, cb, &read, 0))
160 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
164 HANDLE hFile = (HANDLE)cookie;
166 ret = WriteFile(hFile, buffer, cb, &written, 0);
168 if(!ret || (cb != written))
176 LPWSTR file_basename(LPWSTR path)
178 LPWSTR pos = path + lstrlenW(path);
182 if(*pos == '\\' || *pos == '/')
192 static void set_caption(LPCWSTR wszNewFileName)
194 static const WCHAR wszSeparator[] = {' ','-',' '};
199 wszNewFileName = wszDefaultFileName;
201 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
203 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
204 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
209 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
210 length += lstrlenW(wszNewFileName);
211 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
212 length += sizeof(wszSeparator) / sizeof(WCHAR);
213 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
215 SetWindowTextW(hMainWnd, wszCaption);
217 HeapFree(GetProcessHeap(), 0, wszCaption);
220 static BOOL validate_endptr(LPCSTR endptr, BOOL units)
222 if(!endptr || !*endptr)
225 while(*endptr == ' ')
229 return *endptr != '\0';
231 /* FIXME: Allow other units and convert between them */
232 if(!lstrcmpA(endptr, units_cmA))
235 return *endptr != '\0';
238 static BOOL number_from_string(LPCWSTR string, float *num, BOOL units)
241 char buffer[MAX_STRING_LEN];
242 char *endptr = buffer;
244 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN, NULL, NULL);
247 ret = strtod(buffer, &endptr);
249 if((ret == 0 && errno != 0) || endptr == buffer || validate_endptr(endptr, units))
259 static void set_size(float size)
263 ZeroMemory(&fmt, sizeof(fmt));
264 fmt.cbSize = sizeof(fmt);
265 fmt.dwMask = CFM_SIZE;
266 fmt.yHeight = (int)(size * 20.0);
267 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
270 static void add_size(HWND hSizeListWnd, unsigned size)
273 COMBOBOXEXITEMW cbItem;
274 cbItem.mask = CBEIF_TEXT;
277 wsprintfW(buffer, stringFormat, size);
278 cbItem.pszText = (LPWSTR)buffer;
279 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
282 static void populate_size_list(HWND hSizeListWnd)
284 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
285 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
286 COMBOBOXEXITEMW cbFontItem;
288 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
289 HDC hdc = GetDC(hMainWnd);
290 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
295 ZeroMemory(&fmt, sizeof(fmt));
296 fmt.cbSize = sizeof(fmt);
297 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
299 cbFontItem.mask = CBEIF_LPARAM;
300 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
301 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
303 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
305 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
307 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
309 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
310 GetDeviceCaps(hdc, LOGPIXELSY)));
313 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
314 add_size(hSizeListWnd, choices[i]);
317 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
318 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
321 static void update_size_list(void)
323 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
324 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
325 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
326 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
329 ZeroMemory(&fmt, sizeof(fmt));
330 fmt.cbSize = sizeof(fmt);
332 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
334 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
335 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
337 if(lstrcmpW(fontSize, sizeBuffer))
338 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
341 static void update_font_list(void)
343 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
344 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
345 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
346 WCHAR fontName[MAX_STRING_LEN];
349 ZeroMemory(&fmt, sizeof(fmt));
350 fmt.cbSize = sizeof(fmt);
352 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
353 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
355 if(lstrcmpW(fontName, fmt.szFaceName))
357 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
358 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
365 static void clear_formatting(void)
369 pf.cbSize = sizeof(pf);
370 pf.dwMask = PFM_ALIGNMENT;
371 pf.wAlignment = PFA_LEFT;
372 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
375 static int fileformat_number(WPARAM format)
379 if(format == SF_TEXT)
382 } else if (format == (SF_TEXT | SF_UNICODE))
389 static WPARAM fileformat_flags(int format)
391 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
393 return flags[format];
396 static void set_font(LPCWSTR wszFaceName)
398 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
399 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
400 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
401 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
404 ZeroMemory(&fmt, sizeof(fmt));
406 fmt.cbSize = sizeof(fmt);
407 fmt.dwMask = CFM_FACE;
409 lstrcpyW(fmt.szFaceName, wszFaceName);
411 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
413 populate_size_list(hSizeListWnd);
415 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)(LPWSTR)wszFaceName);
418 static void set_default_font(void)
420 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
421 'R','o','m','a','n',0};
422 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
426 ZeroMemory(&fmt, sizeof(fmt));
428 fmt.cbSize = sizeof(fmt);
429 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
432 if(fileFormat & SF_RTF)
435 font = plainTextFont;
437 lstrcpyW(fmt.szFaceName, font);
439 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
442 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, NEWTEXTMETRICEXW *ntmc)
444 COMBOBOXEXITEMW cbItem;
445 WCHAR buffer[MAX_PATH];
448 cbItem.mask = CBEIF_TEXT;
449 cbItem.pszText = buffer;
450 cbItem.cchTextMax = MAX_STRING_LEN;
453 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
455 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
460 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
461 lstrcpyW( cbItem.pszText, fontName );
463 cbItem.mask |= CBEIF_LPARAM;
464 if(fontType & RASTER_FONTTYPE)
465 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
467 cbItem.lParam = MAKELONG(fontType,fontHeight);
468 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
469 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
472 static void dialog_choose_font(void)
477 HDC hDC = GetDC(hMainWnd);
479 ZeroMemory(&cf, sizeof(cf));
480 cf.lStructSize = sizeof(cf);
481 cf.hwndOwner = hMainWnd;
483 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
485 ZeroMemory(&fmt, sizeof(fmt));
486 fmt.cbSize = sizeof(fmt);
488 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
489 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
490 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) ? TRUE : FALSE;
491 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
492 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE;
493 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE;
494 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
495 cf.rgbColors = fmt.crTextColor;
499 ZeroMemory(&fmt, sizeof(fmt));
500 fmt.cbSize = sizeof(fmt);
501 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
502 fmt.yHeight = cf.iPointSize * 2;
504 if(cf.nFontType & BOLD_FONTTYPE)
505 fmt.dwEffects |= CFE_BOLD;
506 if(cf.nFontType & ITALIC_FONTTYPE)
507 fmt.dwEffects |= CFE_ITALIC;
508 if(cf.lpLogFont->lfUnderline == TRUE)
509 fmt.dwEffects |= CFE_UNDERLINE;
510 if(cf.lpLogFont->lfStrikeOut == TRUE)
511 fmt.dwEffects |= CFE_STRIKEOUT;
513 fmt.crTextColor = cf.rgbColors;
515 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
516 set_font(cf.lpLogFont->lfFaceName);
521 int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
522 DWORD FontType, LPARAM lParam)
524 HWND hListWnd = (HWND) lParam;
526 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
529 add_font((LPWSTR)lpelfe->lfFaceName, FontType, hListWnd, (NEWTEXTMETRICEXW*)lpntme);
535 static void populate_font_list(HWND hListWnd)
537 HDC hdc = GetDC(hMainWnd);
539 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
542 fontinfo.lfCharSet = DEFAULT_CHARSET;
543 *fontinfo.lfFaceName = '\0';
544 fontinfo.lfPitchAndFamily = 0;
546 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
547 (LPARAM)hListWnd, 0);
549 ZeroMemory(&fmt, sizeof(fmt));
550 fmt.cbSize = sizeof(fmt);
551 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
552 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
555 static void update_window(void)
559 GetWindowRect(hMainWnd, &rect);
561 (void) OnSize(hMainWnd, SIZE_RESTORED, MAKELONG(rect.bottom, rect.right));
564 static BOOL is_bar_visible(int bandId)
566 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
569 static void store_bar_state(int bandId, BOOL show)
571 int formatIndex = reg_formatindex(fileFormat);
574 barState[formatIndex] |= (1 << bandId);
576 barState[formatIndex] &= ~(1 << bandId);
579 static void set_toolbar_state(int bandId, BOOL show)
581 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
583 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
585 if(bandId == BANDID_TOOLBAR)
587 REBARBANDINFOW rbbinfo;
588 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
590 rbbinfo.cbSize = sizeof(rbbinfo);
591 rbbinfo.fMask = RBBIM_STYLE;
593 SendMessageW(hwndReBar, RB_GETBANDINFO, index, (LPARAM)&rbbinfo);
596 rbbinfo.fStyle &= ~RBBS_BREAK;
598 rbbinfo.fStyle |= RBBS_BREAK;
600 SendMessageW(hwndReBar, RB_SETBANDINFO, index, (LPARAM)&rbbinfo);
603 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
604 store_bar_state(bandId, show);
607 static void set_statusbar_state(BOOL show)
609 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
611 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
612 store_bar_state(BANDID_STATUSBAR, show);
615 static void set_bar_states(void)
617 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
618 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
619 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
620 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
621 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
622 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
627 static void preview_exit(HWND hMainWnd)
629 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
630 HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_MAINMENU));
631 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
634 ShowWindow(hEditorWnd, TRUE);
636 close_preview(hMainWnd);
638 SetMenu(hMainWnd, hMenu);
639 registry_read_filelist(hMainWnd);
644 static void set_fileformat(WPARAM format)
647 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
651 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_TXT));
653 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_RTF));
655 SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
659 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
662 static void DoOpenFile(LPCWSTR szOpenFileName)
668 WPARAM format = SF_TEXT;
670 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
671 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
672 if (hFile == INVALID_HANDLE_VALUE)
675 ReadFile(hFile, fileStart, 5, &readOut, NULL);
676 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
678 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
680 format = SF_TEXT | SF_UNICODE;
681 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
682 } else if(readOut >= 5)
684 static const char header[] = "{\\rtf";
685 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
687 if(!memcmp(header, fileStart, 5))
689 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
692 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED), wszAppTitle,
693 MB_OK | MB_ICONEXCLAMATION);
698 es.dwCookie = (DWORD_PTR)hFile;
699 es.pfnCallback = stream_in;
702 set_fileformat(format);
703 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
707 SetFocus(hEditorWnd);
709 set_caption(szOpenFileName);
711 lstrcpyW(wszFileName, szOpenFileName);
712 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
713 registry_set_filelist(szOpenFileName, hMainWnd);
717 static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
723 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
724 FILE_ATTRIBUTE_NORMAL, NULL);
726 if(hFile == INVALID_HANDLE_VALUE)
729 if(format == (SF_TEXT | SF_UNICODE))
731 static const BYTE unicode[] = {0xff,0xfe};
733 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
735 if(writeOut != sizeof(unicode))
739 stream.dwCookie = (DWORD_PTR)hFile;
740 stream.pfnCallback = stream_out;
742 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
746 SetFocus(hEditorWnd);
751 gt.flags = GTL_DEFAULT;
754 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0))
758 lstrcpyW(wszFileName, wszSaveFileName);
759 set_caption(wszFileName);
760 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
761 set_fileformat(format);
764 static void DialogSaveFile(void)
768 WCHAR wszFile[MAX_PATH] = {'\0'};
769 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
771 ZeroMemory(&sfn, sizeof(sfn));
773 sfn.lStructSize = sizeof(sfn);
774 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
775 sfn.hwndOwner = hMainWnd;
776 sfn.lpstrFilter = wszFilter;
777 sfn.lpstrFile = wszFile;
778 sfn.nMaxFile = MAX_PATH;
779 sfn.lpstrDefExt = wszDefExt;
780 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
782 while(GetSaveFileNameW(&sfn))
784 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
786 if(MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
787 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
792 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
797 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
803 static BOOL prompt_save_changes(void)
808 gt.flags = GTL_NUMCHARS;
810 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0))
814 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
819 LPWSTR displayFileName;
824 displayFileName = wszDefaultFileName;
826 displayFileName = file_basename(wszFileName);
828 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
829 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
834 wsprintfW(text, wszSaveChanges, displayFileName);
836 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
838 HeapFree(GetProcessHeap(), 0, text);
847 DoSaveFile(wszFileName, fileFormat);
858 static void DialogOpenFile(void)
862 WCHAR wszFile[MAX_PATH] = {'\0'};
863 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
865 ZeroMemory(&ofn, sizeof(ofn));
867 ofn.lStructSize = sizeof(ofn);
868 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
869 ofn.hwndOwner = hMainWnd;
870 ofn.lpstrFilter = wszFilter;
871 ofn.lpstrFile = wszFile;
872 ofn.nMaxFile = MAX_PATH;
873 ofn.lpstrDefExt = wszDefExt;
874 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
876 if(GetOpenFileNameW(&ofn))
878 if(prompt_save_changes())
879 DoOpenFile(ofn.lpstrFile);
883 static void dialog_about(void)
885 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
886 HICON icon = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
887 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
890 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
896 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
899 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
901 sprintf(id, "%d\n", (int)ps->lParam);
902 SetWindowTextA(hIdWnd, id);
903 if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
904 wrap = IDC_PAGEFMT_WW;
905 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
906 wrap = IDC_PAGEFMT_WM;
909 CheckRadioButton(hWnd, IDC_PAGEFMT_WW,
910 IDC_PAGEFMT_WM, wrap);
912 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
913 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
914 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
915 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
916 if(barState[ps->lParam] & (1 << BANDID_RULER))
917 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
918 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
919 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
924 switch(LOWORD(wParam))
928 CheckRadioButton(hWnd, IDC_PAGEFMT_WW, IDC_PAGEFMT_WM,
936 CheckDlgButton(hWnd, LOWORD(wParam),
937 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
943 LPNMHDR header = (LPNMHDR)lParam;
944 if(header->code == PSN_APPLY)
946 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
950 GetWindowTextA(hIdWnd, sid, 4);
952 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
953 wordWrap[id] = ID_WORDWRAP_WINDOW;
954 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
955 wordWrap[id] = ID_WORDWRAP_MARGIN;
957 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
958 barState[id] |= (1 << BANDID_TOOLBAR);
960 barState[id] &= ~(1 << BANDID_TOOLBAR);
962 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
963 barState[id] |= (1 << BANDID_FORMATBAR);
965 barState[id] &= ~(1 << BANDID_FORMATBAR);
967 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
968 barState[id] |= (1 << BANDID_RULER);
970 barState[id] &= ~(1 << BANDID_RULER);
972 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
973 barState[id] |= (1 << BANDID_STATUSBAR);
975 barState[id] &= ~(1 << BANDID_STATUSBAR);
983 static void dialog_viewproperties(void)
985 PROPSHEETPAGEW psp[2];
986 PROPSHEETHEADERW psh;
988 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
989 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
991 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
992 psp[0].dwFlags = PSP_USETITLE;
993 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
994 psp[0].pfnDlgProc = formatopts_proc;
995 psp[0].hInstance = hInstance;
996 psp[0].lParam = reg_formatindex(SF_TEXT);
997 psp[0].pfnCallback = NULL;
998 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
999 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1001 psp[i].dwSize = psp[0].dwSize;
1002 psp[i].dwFlags = psp[0].dwFlags;
1003 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1004 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1005 psp[i].hInstance = psp[0].hInstance;
1006 psp[i].lParam = reg_formatindex(SF_RTF);
1007 psp[i].pfnCallback = psp[0].pfnCallback;
1008 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1011 psh.dwSize = sizeof(psh);
1012 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1013 psh.hwndParent = hMainWnd;
1014 psh.hInstance = hInstance;
1015 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1016 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1017 U3(psh).ppsp = ppsp;
1018 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1020 if(fileFormat & SF_RTF)
1021 U2(psh).nStartPage = 1;
1023 U2(psh).nStartPage = 0;
1024 PropertySheetW(&psh);
1026 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1029 static void HandleCommandLine(LPWSTR cmdline)
1034 /* skip white space */
1035 while (*cmdline == ' ') cmdline++;
1037 /* skip executable name */
1038 delimiter = (*cmdline == '"' ? '"' : ' ');
1040 if (*cmdline == delimiter) cmdline++;
1041 while (*cmdline && *cmdline != delimiter) cmdline++;
1042 if (*cmdline == delimiter) cmdline++;
1046 while (isspace(*cmdline)) cmdline++;
1048 if (*cmdline == '-' || *cmdline == '/')
1050 if (!cmdline[2] || isspace(cmdline[2]))
1061 /* a filename starting by / */
1068 /* file name is passed on the command line */
1069 if (cmdline[0] == '"')
1072 cmdline[lstrlenW(cmdline) - 1] = 0;
1074 DoOpenFile(cmdline);
1075 InvalidateRect(hMainWnd, NULL, FALSE);
1079 MessageBox(hMainWnd, "Printing not implemented", "WordPad", MB_OK);
1082 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1084 if(pFr->Flags & FR_DIALOGTERM)
1087 pFr->Flags = FR_FINDNEXT;
1091 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1093 DWORD flags = FR_DOWN;
1095 static CHARRANGE cr;
1100 HMENU hMenu = GetMenu(hMainWnd);
1103 mi.cbSize = sizeof(mi);
1104 mi.fMask = MIIM_DATA;
1106 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1108 gt.flags = GTL_NUMCHARS;
1111 length = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
1113 if(pFr->lCustData == -1)
1115 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&startPos, (LPARAM)&end);
1116 cr.cpMin = startPos;
1117 pFr->lCustData = startPos;
1119 if(cr.cpMin == length)
1123 startPos = pFr->lCustData;
1126 if(cr.cpMax > length)
1134 ft.lpstrText = pFr->lpstrFindWhat;
1136 if(pFr->Flags & FR_MATCHCASE)
1137 flags |= FR_MATCHCASE;
1138 if(pFr->Flags & FR_WHOLEWORD)
1139 flags |= FR_WHOLEWORD;
1141 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1145 if(cr.cpMax == length && cr.cpMax != startPos)
1147 ft.chrg.cpMin = cr.cpMin = 0;
1148 ft.chrg.cpMax = cr.cpMax = startPos;
1150 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1156 pFr->lCustData = -1;
1157 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED), wszAppTitle,
1158 MB_OK | MB_ICONASTERISK);
1161 end = ret + lstrlenW(pFr->lpstrFindWhat);
1163 SendMessageW(hEditorWnd, EM_SETSEL, (WPARAM)ret, (LPARAM)end);
1164 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1166 if(pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1167 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1169 if(pFr->Flags & FR_REPLACEALL)
1170 handle_findmsg(pFr);
1177 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1179 static WCHAR findBuffer[MAX_STRING_LEN];
1181 ZeroMemory(fr, sizeof(FINDREPLACEW));
1182 fr->lStructSize = sizeof(FINDREPLACEW);
1183 fr->hwndOwner = hMainWnd;
1184 fr->Flags = FR_HIDEUPDOWN;
1185 fr->lpstrFindWhat = findBuffer;
1187 fr->wFindWhatLen = MAX_STRING_LEN*sizeof(WCHAR);
1190 hFindWnd = ReplaceTextW(fr);
1192 hFindWnd = FindTextW(fr);
1195 static int current_units_to_twips(float number)
1197 int twips = (int)(number * TWIPS_PER_CM);
1201 static void append_current_units(LPWSTR buffer)
1203 static const WCHAR space[] = {' '};
1204 lstrcatW(buffer, space);
1205 lstrcatW(buffer, units_cmW);
1208 static void number_with_units(LPWSTR buffer, int number)
1210 float converted = (float)number / TWIPS_PER_CM;
1211 char string[MAX_STRING_LEN];
1213 sprintf(string, "%.2f ", converted);
1214 lstrcatA(string, units_cmA);
1215 MultiByteToWideChar(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN);
1218 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1224 WCHAR buffer[MAX_STRING_LEN];
1226 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1229 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1231 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1232 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1234 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1235 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1236 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1238 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1243 switch(LOWORD(wParam))
1248 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1250 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1254 WCHAR buffer[MAX_STRING_LEN];
1255 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1256 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1262 EndDialog(hWnd, wParam);
1269 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1275 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1276 WCHAR buffer[MAX_STRING_LEN];
1277 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1279 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, (LPWSTR)buffer, MAX_STRING_LEN);
1280 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1281 LoadStringW(hInstance, STRING_NEWFILE_TXT, (LPWSTR)buffer, MAX_STRING_LEN);
1282 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1283 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, (LPWSTR)buffer, MAX_STRING_LEN);
1284 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1286 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1291 switch(LOWORD(wParam))
1296 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1297 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1300 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1305 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1312 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1318 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd,
1320 WCHAR buffer[MAX_STRING_LEN];
1321 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1322 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1323 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1324 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1328 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1330 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1331 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1333 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1334 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1336 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1338 pf.cbSize = sizeof(pf);
1339 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1341 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1343 if(pf.wAlignment == PFA_RIGHT)
1345 else if(pf.wAlignment == PFA_CENTER)
1348 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1350 number_with_units(buffer, pf.dxOffset);
1351 SetWindowTextW(hLeftWnd, buffer);
1352 number_with_units(buffer, pf.dxRightIndent);
1353 SetWindowTextW(hRightWnd, buffer);
1354 number_with_units(buffer, pf.dxStartIndent - pf.dxOffset);
1355 SetWindowTextW(hFirstWnd, buffer);
1360 switch(LOWORD(wParam))
1364 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1365 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1366 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1367 WCHAR buffer[MAX_STRING_LEN];
1372 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1373 if(number_from_string(buffer, &num, TRUE))
1375 pf.dxOffset = current_units_to_twips(num);
1376 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1377 if(number_from_string(buffer, &num, TRUE))
1379 pf.dxRightIndent = current_units_to_twips(num);
1380 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1381 if(number_from_string(buffer, &num, TRUE))
1383 pf.dxStartIndent = current_units_to_twips(num);
1387 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1388 wszAppTitle, MB_OK | MB_ICONASTERISK);
1392 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1393 pf.cbSize = sizeof(pf);
1394 pf.dwMask = PFM_OFFSET | PFM_OFFSETINDENT | PFM_RIGHTINDENT;
1395 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1401 EndDialog(hWnd, wParam);
1408 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1414 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1416 WCHAR buffer[MAX_STRING_LEN];
1419 pf.cbSize = sizeof(pf);
1420 pf.dwMask = PFM_TABSTOPS;
1421 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1422 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1424 for(i = 0; i < pf.cTabCount; i++)
1426 number_with_units(buffer, pf.rgxTabs[i]);
1427 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1434 switch(LOWORD(wParam))
1438 HWND hTabWnd = (HWND)lParam;
1439 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1440 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1441 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1443 if(GetWindowTextLengthW(hTabWnd))
1444 EnableWindow(hAddWnd, TRUE);
1446 EnableWindow(hAddWnd, FALSE);
1448 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1450 EnableWindow(hEmptyWnd, TRUE);
1452 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1453 EnableWindow(hDelWnd, FALSE);
1455 EnableWindow(hDelWnd, TRUE);
1458 EnableWindow(hEmptyWnd, FALSE);
1465 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1466 WCHAR buffer[MAX_STRING_LEN];
1468 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1469 append_current_units(buffer);
1471 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1475 if(!number_from_string(buffer, &number, TRUE))
1477 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1478 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1481 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1482 SetWindowTextW(hTabWnd, 0);
1491 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1493 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1495 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1501 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1502 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1509 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1511 WCHAR buffer[MAX_STRING_LEN];
1515 pf.cbSize = sizeof(pf);
1516 pf.dwMask = PFM_TABSTOPS;
1518 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1519 (LPARAM)&buffer) != CB_ERR &&
1520 i < MAX_TAB_STOPS; i++)
1522 number_from_string(buffer, &number, TRUE);
1523 pf.rgxTabs[i] = current_units_to_twips(number);
1526 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1530 EndDialog(hWnd, wParam);
1537 static int context_menu(LPARAM lParam)
1539 int x = (int)(short)LOWORD(lParam);
1540 int y = (int)(short)HIWORD(lParam);
1541 HMENU hPop = GetSubMenu(hPopupMenu, 0);
1545 int from = 0, to = 0;
1547 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1548 SendMessageW(hEditorWnd, EM_POSFROMCHAR, (WPARAM)&pt, (LPARAM)to);
1549 ClientToScreen(hEditorWnd, (POINT*)&pt);
1554 TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
1555 x, y, 0, hMainWnd, 0);
1560 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
1562 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1563 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
1566 int nStdBitmaps = 0;
1569 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1570 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1572 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1574 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1575 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1576 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1578 rbi.cbSize = sizeof(rbi);
1581 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1584 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
1586 1, hInstance, IDB_TOOLBAR,
1588 24, 24, 16, 16, sizeof(TBBUTTON));
1590 ab.hInst = HINST_COMMCTRL;
1591 ab.nID = IDB_STD_SMALL_COLOR;
1592 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1594 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1595 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1596 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1597 AddSeparator(hToolBarWnd);
1598 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1599 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1600 AddSeparator(hToolBarWnd);
1601 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1602 AddSeparator(hToolBarWnd);
1603 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1604 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1605 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1606 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1607 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1608 AddSeparator(hToolBarWnd);
1609 AddButton(hToolBarWnd, 0, ID_DATETIME);
1611 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1613 rbb.cbSize = sizeof(rbb);
1614 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1615 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1617 rbb.hwndChild = hToolBarWnd;
1619 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1620 rbb.wID = BANDID_TOOLBAR;
1622 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1624 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1625 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1626 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1628 rbb.hwndChild = hFontListWnd;
1630 rbb.wID = BANDID_FONTLIST;
1632 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1634 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1635 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1636 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1638 rbb.hwndChild = hSizeListWnd;
1640 rbb.fStyle ^= RBBS_BREAK;
1641 rbb.wID = BANDID_SIZELIST;
1643 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1645 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1646 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
1647 IDC_FORMATBAR, 7, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1649 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1650 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1651 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1652 AddSeparator(hFormatBarWnd);
1653 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
1654 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
1655 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
1656 AddSeparator(hFormatBarWnd);
1657 AddButton(hFormatBarWnd, 6, ID_BULLET);
1659 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1661 rbb.hwndChild = hFormatBarWnd;
1662 rbb.wID = BANDID_FORMATBAR;
1664 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1666 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1667 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1670 rbb.hwndChild = hRulerWnd;
1671 rbb.wID = BANDID_RULER;
1672 rbb.fStyle |= RBBS_BREAK;
1674 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1676 hDLL = LoadLibraryW(wszRichEditDll);
1679 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1680 MB_OK | MB_ICONEXCLAMATION);
1684 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
1685 WS_CHILD|WS_VISIBLE|ECO_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
1686 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1690 fprintf(stderr, "Error code %u\n", GetLastError());
1695 SetFocus(hEditorWnd);
1696 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1700 populate_font_list(hFontListWnd);
1701 populate_size_list(hSizeListWnd);
1703 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1705 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1707 registry_read_filelist(hWnd);
1708 registry_read_formatopts_all(barState, wordWrap);
1709 registry_read_options();
1710 DragAcceptFiles(hWnd, TRUE);
1715 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
1717 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1718 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1719 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1720 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1726 ZeroMemory(&fmt, sizeof(fmt));
1727 fmt.cbSize = sizeof(fmt);
1729 ZeroMemory(&pf, sizeof(pf));
1730 pf.cbSize = sizeof(pf);
1732 gt.flags = GTL_NUMCHARS;
1735 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1736 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)>, 0) ? 1 : 0);
1738 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1740 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1741 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1742 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1743 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1744 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1745 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1746 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
1748 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
1749 (fmt.dwEffects & CFE_BOLD));
1750 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
1751 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
1752 (fmt.dwEffects & CFE_ITALIC));
1753 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
1754 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
1755 (fmt.dwEffects & CFE_UNDERLINE));
1756 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
1758 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1759 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
1760 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
1761 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
1763 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
1767 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
1769 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1770 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1771 NMHDR *pHdr = (NMHDR *)lParam;
1772 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
1773 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
1774 WCHAR sizeBuffer[MAX_PATH];
1776 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
1778 if (pHdr->code == CBEN_ENDEDITW)
1780 CHARFORMAT2W format;
1781 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
1783 ZeroMemory(&format, sizeof(format));
1784 format.cbSize = sizeof(format);
1785 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
1787 if(pHdr->hwndFrom == hwndFontList)
1789 if(lstrcmpW(format.szFaceName, (LPWSTR)endEdit->szText))
1790 set_font((LPCWSTR) endEdit->szText);
1791 } else if (pHdr->hwndFrom == hwndSizeList)
1793 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
1794 if(lstrcmpW(sizeBuffer, (LPWSTR)endEdit->szText))
1797 if(number_from_string((LPWSTR)endEdit->szText, &size, FALSE))
1802 SetWindowTextW(hwndSizeList, sizeBuffer);
1803 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER), wszAppTitle, MB_OK | MB_ICONINFORMATION);
1811 if (pHdr->hwndFrom != hwndEditor)
1814 if (pHdr->code == EN_SELCHANGE)
1816 SELCHANGE *pSC = (SELCHANGE *)lParam;
1821 sprintf( buf,"selection = %d..%d, line count=%ld",
1822 pSC->chrg.cpMin, pSC->chrg.cpMax,
1823 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
1824 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
1825 SendMessage(hWnd, WM_USER, 0, 0);
1831 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
1833 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1834 static FINDREPLACEW findreplace;
1836 if ((HWND)lParam == hwndEditor)
1839 switch(LOWORD(wParam))
1843 PostMessageW(hWnd, WM_CLOSE, 0, 0);
1848 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
1849 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
1852 if(ret != ID_NEWFILE_ABORT)
1854 if(prompt_save_changes())
1859 wszFileName[0] = '\0';
1863 st.flags = ST_DEFAULT;
1865 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
1867 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1868 set_fileformat(ret);
1882 DoSaveFile(wszFileName, fileFormat);
1887 case ID_FILE_SAVEAS:
1891 case ID_FILE_RECENT1:
1892 case ID_FILE_RECENT2:
1893 case ID_FILE_RECENT3:
1894 case ID_FILE_RECENT4:
1896 HMENU hMenu = GetMenu(hWnd);
1899 mi.cbSize = sizeof(MENUITEMINFOW);
1900 mi.fMask = MIIM_DATA;
1901 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
1902 DoOpenFile((LPWSTR)mi.dwItemData);
1907 dialog_find(&findreplace, FALSE);
1911 handle_findmsg(&findreplace);
1915 dialog_find(&findreplace, TRUE);
1918 case ID_FONTSETTINGS:
1919 dialog_choose_font();
1923 dialog_print(hWnd, wszFileName);
1924 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1927 case ID_PRINT_QUICK:
1928 print_quick(wszFileName);
1929 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1934 int index = reg_formatindex(fileFormat);
1935 DWORD tmp = barState[index];
1936 barState[index] = 0;
1938 barState[index] = tmp;
1939 ShowWindow(hEditorWnd, FALSE);
1941 init_preview(hWnd, wszFileName);
1943 SetMenu(hWnd, NULL);
1944 InvalidateRect(0, 0, TRUE);
1949 dialog_printsetup(hWnd);
1950 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1953 case ID_FORMAT_BOLD:
1954 case ID_FORMAT_ITALIC:
1955 case ID_FORMAT_UNDERLINE:
1958 int effects = CFE_BOLD;
1960 ZeroMemory(&fmt, sizeof(fmt));
1961 fmt.cbSize = sizeof(fmt);
1962 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
1964 fmt.dwMask = CFM_BOLD;
1966 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
1968 effects = CFE_ITALIC;
1969 fmt.dwMask = CFM_ITALIC;
1970 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
1972 effects = CFE_UNDERLINE;
1973 fmt.dwMask = CFM_UNDERLINE;
1976 fmt.dwEffects ^= effects;
1978 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
1983 PostMessageW(hwndEditor, WM_CUT, 0, 0);
1987 PostMessageW(hwndEditor, WM_COPY, 0, 0);
1991 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
1995 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
1998 case ID_EDIT_SELECTALL:
2000 CHARRANGE range = {0, -1};
2001 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2002 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2006 case ID_EDIT_GETTEXT:
2008 int nLen = GetWindowTextLengthW(hwndEditor);
2009 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2012 GetWindowTextW(hwndEditor, data, nLen+1);
2013 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
2015 HeapFree( GetProcessHeap(), 0, data);
2016 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2018 tr.chrg.cpMax = nLen;
2019 tr.lpstrText = data;
2020 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2021 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
2022 HeapFree( GetProcessHeap(), 0, data );
2024 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2028 case ID_EDIT_CHARFORMAT:
2029 case ID_EDIT_DEFCHARFORMAT:
2033 ZeroMemory(&cf, sizeof(cf));
2034 cf.cbSize = sizeof(cf);
2036 i = SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2037 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2041 case ID_EDIT_PARAFORMAT:
2044 ZeroMemory(&pf, sizeof(pf));
2045 pf.cbSize = sizeof(pf);
2046 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2050 case ID_EDIT_SELECTIONINFO:
2052 CHARRANGE range = {0, -1};
2056 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2057 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2058 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2059 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2060 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2061 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
2062 HeapFree( GetProcessHeap(), 0, data);
2063 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2067 case ID_EDIT_READONLY:
2069 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2070 if (nStyle & ES_READONLY)
2071 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2073 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2077 case ID_EDIT_MODIFIED:
2078 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2079 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2081 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2085 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2089 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2096 pf.cbSize = sizeof(pf);
2097 pf.dwMask = PFM_NUMBERING;
2098 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2100 pf.dwMask |= PFM_OFFSET;
2102 if(pf.wNumbering == PFN_BULLET)
2108 pf.wNumbering = PFN_BULLET;
2112 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2117 case ID_ALIGN_CENTER:
2118 case ID_ALIGN_RIGHT:
2122 pf.cbSize = sizeof(pf);
2123 pf.dwMask = PFM_ALIGNMENT;
2124 switch(LOWORD(wParam)) {
2125 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2126 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2127 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2129 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2134 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2138 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2141 case ID_TOGGLE_TOOLBAR:
2142 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2146 case ID_TOGGLE_FORMATBAR:
2147 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2148 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2149 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2153 case ID_TOGGLE_STATUSBAR:
2154 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2158 case ID_TOGGLE_RULER:
2159 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2165 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2166 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2172 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2173 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd,
2180 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2181 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2189 case ID_VIEWPROPERTIES:
2190 dialog_viewproperties();
2194 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2200 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
2202 HMENU hMenu = (HMENU)wParam;
2203 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2204 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2206 int nAlignment = -1;
2212 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2213 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2214 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2216 pf.cbSize = sizeof(PARAFORMAT);
2217 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2218 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2219 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
2220 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2221 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
2222 if (pf.dwMask & PFM_ALIGNMENT)
2223 nAlignment = pf.wAlignment;
2224 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
2225 MF_CHECKED : MF_UNCHECKED);
2226 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
2227 MF_CHECKED : MF_UNCHECKED);
2228 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
2229 MF_CHECKED : MF_UNCHECKED);
2230 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
2231 MF_CHECKED : MF_UNCHECKED));
2232 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
2233 MF_ENABLED : MF_GRAYED);
2234 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
2235 MF_ENABLED : MF_GRAYED);
2237 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
2238 MF_CHECKED : MF_UNCHECKED);
2240 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
2241 MF_CHECKED : MF_UNCHECKED);
2243 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
2244 MF_CHECKED : MF_UNCHECKED);
2246 CheckMenuItem(hMenu, ID_TOGGLE_RULER, MF_BYCOMMAND|(is_bar_visible(BANDID_RULER)) ? MF_CHECKED : MF_UNCHECKED);
2248 gt.flags = GTL_NUMCHARS;
2250 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
2251 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2253 mi.cbSize = sizeof(mi);
2254 mi.fMask = MIIM_DATA;
2256 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2258 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
2259 MF_ENABLED : MF_GRAYED));
2261 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2266 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2268 int nStatusSize = 0;
2270 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2271 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2272 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2273 HWND hRulerWnd = GetDlgItem(hWnd, IDC_RULER);
2274 int rebarHeight = 0;
2278 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2279 if (IsWindowVisible(hwndStatusBar))
2281 GetClientRect(hwndStatusBar, &rc);
2282 nStatusSize = rc.bottom - rc.top;
2290 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2292 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2296 GetClientRect(hWnd, &rc);
2297 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2300 redraw_ruler(hRulerWnd);
2302 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2305 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2307 if(msg == ID_FINDMSGSTRING)
2308 return handle_findmsg((LPFINDREPLACEW)lParam);
2313 return OnCreate( hWnd, wParam, lParam );
2316 return OnUser( hWnd, wParam, lParam );
2319 return OnNotify( hWnd, wParam, lParam );
2322 if(preview_isactive())
2324 return preview_command( hWnd, wParam, lParam );
2327 return OnCommand( hWnd, wParam, lParam );
2334 if(preview_isactive())
2337 } else if(prompt_save_changes())
2339 registry_set_options(hMainWnd);
2340 registry_set_formatopts_all(barState);
2347 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2350 case WM_INITMENUPOPUP:
2351 return OnInitPopupMenu( hWnd, wParam, lParam );
2354 return OnSize( hWnd, wParam, lParam );
2356 case WM_CONTEXTMENU:
2357 if((HWND)wParam == hEditorWnd)
2358 return context_menu(lParam);
2360 return DefWindowProcW(hWnd, msg, wParam, lParam);
2364 WCHAR file[MAX_PATH];
2365 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2366 DragFinish((HDROP)wParam);
2368 if(prompt_save_changes())
2373 if(preview_isactive())
2374 return print_preview(hWnd);
2376 return DefWindowProcW(hWnd, msg, wParam, lParam);
2379 return DefWindowProcW(hWnd, msg, wParam, lParam);
2385 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
2387 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2392 UINT_PTR hPrevRulerProc;
2395 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2396 'T','A','B','L','E','\0'};
2398 InitCommonControlsEx(&classes);
2400 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2402 wc.style = CS_HREDRAW | CS_VREDRAW;
2403 wc.lpfnWndProc = WndProc;
2406 wc.hInstance = hInstance;
2407 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2408 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2409 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2410 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2411 wc.lpszClassName = wszMainWndClass;
2412 RegisterClassW(&wc);
2414 registry_read_winrect(&rc);
2415 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2416 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2417 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
2421 set_fileformat(SF_RTF);
2422 hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
2423 get_default_printer_opts();
2424 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2426 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2427 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2428 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2429 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2431 HandleCommandLine(GetCommandLineW());
2433 while(GetMessageW(&msg,0,0,0))
2435 if (IsDialogMessage(hFindWnd, &msg))
2438 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2440 TranslateMessage(&msg);
2441 DispatchMessageW(&msg);
2442 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2443 SendMessageW(hMainWnd, WM_USER, 0, 0);