2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007 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 #define MAX_STRING_LEN 255
43 #ifdef NONAMELESSUNION
54 static const WCHAR xszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
56 static const WCHAR wszRichEditClass[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
57 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
58 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
60 static const WCHAR key_recentfiles[] = {'R','e','c','e','n','t',' ','f','i','l','e',
61 ' ','l','i','s','t',0};
62 static const WCHAR key_options[] = {'O','p','t','i','o','n','s',0};
63 static const WCHAR key_rtf[] = {'R','T','F',0};
64 static const WCHAR key_text[] = {'T','e','x','t',0};
66 static const WCHAR var_file[] = {'F','i','l','e','%','d',0};
67 static const WCHAR var_framerect[] = {'F','r','a','m','e','R','e','c','t',0};
68 static const WCHAR var_barstate0[] = {'B','a','r','S','t','a','t','e','0',0};
69 static const WCHAR var_pagemargin[] = {'P','a','g','e','M','a','r','g','i','n',0};
71 static const WCHAR stringFormat[] = {'%','2','d','\0'};
74 static HWND hEditorWnd;
76 static HMENU hPopupMenu;
78 static UINT ID_FINDMSGSTRING;
80 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
81 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
82 static WCHAR wszSaveChanges[MAX_STRING_LEN];
83 static WCHAR wszPrintFilter[MAX_STRING_LEN*2+6+4+1];
84 static WCHAR units_cmW[MAX_STRING_LEN];
86 static char units_cmA[MAX_STRING_LEN];
88 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
90 /* Load string resources */
91 static void DoLoadStrings(void)
94 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
95 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
96 static const WCHAR files_all[] = {'*','.','*','\0'};
97 static const WCHAR files_prn[] = {'*','.','P','R','N',0};
98 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
100 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
101 p += lstrlenW(p) + 1;
102 lstrcpyW(p, files_rtf);
103 p += lstrlenW(p) + 1;
104 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
105 p += lstrlenW(p) + 1;
106 lstrcpyW(p, files_txt);
107 p += lstrlenW(p) + 1;
108 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
109 p += lstrlenW(p) + 1;
110 lstrcpyW(p, files_txt);
111 p += lstrlenW(p) + 1;
112 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
113 p += lstrlenW(p) + 1;
114 lstrcpyW(p, files_all);
115 p += lstrlenW(p) + 1;
119 LoadStringW(hInstance, STRING_PRINTER_FILES_PRN, p, MAX_STRING_LEN);
120 p += lstrlenW(p) + 1;
121 lstrcpyW(p, files_prn);
122 p += lstrlenW(p) + 1;
123 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
124 p += lstrlenW(p) + 1;
125 lstrcpyW(p, files_all);
126 p += lstrlenW(p) + 1;
129 p = wszDefaultFileName;
130 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
133 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
135 LoadStringA(hInstance, STRING_UNITS_CM, units_cmA, MAX_STRING_LEN);
136 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
139 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
143 ZeroMemory(&button, sizeof(button));
144 button.iBitmap = nImage;
145 button.idCommand = nCommand;
146 button.fsState = TBSTATE_ENABLED;
147 button.fsStyle = TBSTYLE_BUTTON;
150 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
153 static void AddTextButton(HWND hWnd, int string, int command, int id)
156 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
157 static const WCHAR button[] = {'B','U','T','T','O','N',0};
158 WCHAR text[MAX_STRING_LEN];
162 LoadStringW(hInstance, string, text, MAX_STRING_LEN);
163 hButton = CreateWindowW(button, text,
164 WS_VISIBLE | WS_CHILD, 5, 5, 100, 15,
165 hMainWnd, (HMENU)command, hInstance, NULL);
167 rb.cbSize = sizeof(rb);
168 rb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_CHILD | RBBIM_IDEALSIZE | RBBIM_ID;
169 rb.fStyle = RBBS_NOGRIPPER | RBBS_VARIABLEHEIGHT;
170 rb.hwndChild = hButton;
171 rb.cyChild = rb.cyMinChild = 22;
172 rb.cx = rb.cxMinChild = 90;
179 SendMessageW(hWnd, RB_INSERTBAND, -1, (LPARAM)&rb);
180 SetWindowPos(hButton, 0, 0, 0, 90, 22, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
183 static void AddSeparator(HWND hwndToolBar)
187 ZeroMemory(&button, sizeof(button));
189 button.idCommand = 0;
191 button.fsStyle = TBSTYLE_SEP;
194 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
197 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
199 HANDLE hFile = (HANDLE)cookie;
202 if(!ReadFile(hFile, buffer, cb, &read, 0))
210 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
214 HANDLE hFile = (HANDLE)cookie;
216 ret = WriteFile(hFile, buffer, cb, &written, 0);
218 if(!ret || (cb != written))
227 static LPWSTR file_basename(LPWSTR path)
229 LPWSTR pos = path + lstrlenW(path);
233 if(*pos == '\\' || *pos == '/')
243 static WCHAR wszFileName[MAX_PATH];
244 static WPARAM fileFormat = SF_RTF;
246 static void set_caption(LPCWSTR wszNewFileName)
248 static const WCHAR wszSeparator[] = {' ','-',' '};
253 wszNewFileName = wszDefaultFileName;
255 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
257 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
258 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
263 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
264 length += lstrlenW(wszNewFileName);
265 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
266 length += sizeof(wszSeparator) / sizeof(WCHAR);
267 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
269 SetWindowTextW(hMainWnd, wszCaption);
271 HeapFree(GetProcessHeap(), 0, wszCaption);
274 static LRESULT registry_get_handle(HKEY *hKey, LPDWORD action, LPCWSTR subKey)
277 static const WCHAR wszProgramKey[] = {'S','o','f','t','w','a','r','e','\\',
278 'M','i','c','r','o','s','o','f','t','\\',
279 'W','i','n','d','o','w','s','\\',
280 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
281 'A','p','p','l','e','t','s','\\',
282 'W','o','r','d','p','a','d',0};
283 LPWSTR key = (LPWSTR)wszProgramKey;
287 WCHAR backslash[] = {'\\',0};
288 key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
289 (lstrlenW(wszProgramKey)+lstrlenW(subKey)+lstrlenW(backslash)+1)
295 lstrcpyW(key, wszProgramKey);
296 lstrcatW(key, backslash);
297 lstrcatW(key, subKey);
302 ret = RegCreateKeyExW(HKEY_CURRENT_USER, key, 0, NULL, REG_OPTION_NON_VOLATILE,
303 KEY_READ | KEY_WRITE, NULL, hKey, action);
306 ret = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ | KEY_WRITE, hKey);
310 HeapFree(GetProcessHeap(), 0, key);
317 static void registry_set_options(void)
322 if(registry_get_handle(&hKey, &action, (LPWSTR)key_options) == ERROR_SUCCESS)
326 GetWindowRect(hMainWnd, &rc);
328 RegSetValueExW(hKey, var_framerect, 0, REG_BINARY, (LPBYTE)&rc, sizeof(RECT));
330 RegSetValueExW(hKey, var_pagemargin, 0, REG_BINARY, (LPBYTE)&margins, sizeof(RECT));
334 static RECT registry_read_winrect(void)
338 DWORD size = sizeof(RECT);
340 ZeroMemory(&rc, sizeof(RECT));
341 if(registry_get_handle(&hKey, 0, (LPWSTR)key_options) != ERROR_SUCCESS ||
342 RegQueryValueExW(hKey, var_framerect, 0, NULL, (LPBYTE)&rc, &size) !=
343 ERROR_SUCCESS || size != sizeof(RECT))
355 static void truncate_path(LPWSTR file, LPWSTR out, LPWSTR pos1, LPWSTR pos2)
357 static const WCHAR dots[] = {'.','.','.',0};
366 static void format_filelist_filename(LPWSTR file, LPWSTR out)
369 LPWSTR truncpos1, truncpos2;
370 WCHAR myDocs[MAX_STRING_LEN];
372 SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, (LPWSTR)&myDocs);
373 pos_basename = file_basename(file);
377 *(pos_basename-1) = 0;
378 if(!lstrcmpiW(file, myDocs) || (lstrlenW(pos_basename) > FILELIST_ENTRY_LENGTH))
380 truncpos1 = pos_basename;
381 *(pos_basename-1) = '\\';
385 BOOL morespace = FALSE;
387 *(pos_basename-1) = '\\';
389 for(pos = file; pos < pos_basename; pos++)
391 if(*pos == '\\' || *pos == '/')
395 if((pos - file + lstrlenW(pos_basename)) > FILELIST_ENTRY_LENGTH)
403 if((pos - file + lstrlenW(pos_basename)) > FILELIST_ENTRY_LENGTH)
412 for(pos = pos_basename; pos >= truncpos1; pos--)
414 if(*pos == '\\' || *pos == '/')
416 if((truncpos1 - file + lstrlenW(pos_basename) + pos_basename - pos) > FILELIST_ENTRY_LENGTH)
425 if(truncpos1 == pos_basename)
426 lstrcatW(out, pos_basename);
427 else if(truncpos1 == truncpos2 || !truncpos2)
430 truncate_path(file, out, truncpos1, truncpos2 ? truncpos2 : (pos_basename-1));
433 static void registry_read_filelist(HWND hMainWnd)
437 if(registry_get_handle(&hFileKey, 0, key_recentfiles) == ERROR_SUCCESS)
439 WCHAR itemText[MAX_PATH+3], buffer[MAX_PATH];
440 /* The menu item name is not the same as the file name, so we need to store
441 the file name here */
442 static WCHAR file1[MAX_PATH], file2[MAX_PATH], file3[MAX_PATH], file4[MAX_PATH];
443 WCHAR numFormat[] = {'&','%','d',' ',0};
444 LPWSTR pFile[] = {file1, file2, file3, file4};
445 DWORD pathSize = MAX_PATH*sizeof(WCHAR);
449 HMENU hMenu = GetMenu(hMainWnd);
451 mi.cbSize = sizeof(MENUITEMINFOW);
452 mi.fMask = MIIM_ID | MIIM_DATA | MIIM_STRING | MIIM_FTYPE;
453 mi.fType = MFT_STRING;
454 mi.dwTypeData = itemText;
455 mi.wID = ID_FILE_RECENT1;
457 RemoveMenu(hMenu, ID_FILE_RECENT_SEPARATOR, MF_BYCOMMAND);
458 for(i = 0; i < FILELIST_ENTRIES; i++)
460 wsprintfW(key, var_file, i+1);
461 RemoveMenu(hMenu, ID_FILE_RECENT1+i, MF_BYCOMMAND);
462 if(RegQueryValueExW(hFileKey, (LPWSTR)key, 0, NULL, (LPBYTE)pFile[i], &pathSize)
466 mi.dwItemData = (DWORD)pFile[i];
467 wsprintfW(itemText, numFormat, i+1);
469 lstrcpyW(buffer, pFile[i]);
471 format_filelist_filename(buffer, itemText);
473 InsertMenuItemW(hMenu, ID_FILE_EXIT, FALSE, &mi);
475 pathSize = MAX_PATH*sizeof(WCHAR);
477 mi.fType = MFT_SEPARATOR;
478 mi.fMask = MIIM_FTYPE | MIIM_ID;
479 InsertMenuItemW(hMenu, ID_FILE_EXIT, FALSE, &mi);
481 RegCloseKey(hFileKey);
485 static void registry_set_filelist(LPCWSTR newFile)
490 if(registry_get_handle(&hKey, &action, key_recentfiles) == ERROR_SUCCESS)
492 LPCWSTR pFiles[FILELIST_ENTRIES];
494 HMENU hMenu = GetMenu(hMainWnd);
498 mi.cbSize = sizeof(MENUITEMINFOW);
499 mi.fMask = MIIM_DATA;
501 for(i = 0; i < FILELIST_ENTRIES; i++)
504 for(i = 0; GetMenuItemInfoW(hMenu, ID_FILE_RECENT1+i, FALSE, &mi); i++)
505 pFiles[i] = (LPWSTR)mi.dwItemData;
507 if(lstrcmpiW(newFile, pFiles[0]))
509 for(i = 0; pFiles[i] && i < FILELIST_ENTRIES; i++)
511 if(!lstrcmpiW(pFiles[i], newFile))
514 for(j = 0; pFiles[j] && j < i; j++)
516 pFiles[i-j] = pFiles[i-j-1];
528 for(i = 0; pFiles[i] && i < FILELIST_ENTRIES-1; i++)
529 pFiles[FILELIST_ENTRIES-1-i] = pFiles[FILELIST_ENTRIES-2-i];
534 for(i = 0; pFiles[i] && i < FILELIST_ENTRIES; i++)
536 wsprintfW(buffer, var_file, i+1);
537 RegSetValueExW(hKey, (LPWSTR)&buffer, 0, REG_SZ, (LPBYTE)pFiles[i],
538 (lstrlenW(pFiles[i])+1)*sizeof(WCHAR));
543 registry_read_filelist(hMainWnd);
546 static BOOL validate_endptr(LPCSTR endptr, BOOL units)
548 if(!endptr || !*endptr)
551 while(*endptr == ' ')
555 return *endptr != '\0';
557 /* FIXME: Allow other units and convert between them */
558 if(!lstrcmpA(endptr, units_cmA))
561 return *endptr != '\0';
564 static BOOL number_from_string(LPCWSTR string, float *num, BOOL units)
567 char buffer[MAX_STRING_LEN];
568 char *endptr = buffer;
570 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN, NULL, NULL);
573 ret = strtod(buffer, &endptr);
575 if((ret == 0 && errno != 0) || endptr == buffer || validate_endptr(endptr, units))
585 static void set_size(float size)
589 ZeroMemory(&fmt, sizeof(fmt));
590 fmt.cbSize = sizeof(fmt);
591 fmt.dwMask = CFM_SIZE;
592 fmt.yHeight = (int)(size * 20.0);
593 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
596 static void add_size(HWND hSizeListWnd, unsigned size)
599 COMBOBOXEXITEMW cbItem;
600 cbItem.mask = CBEIF_TEXT;
603 wsprintfW(buffer, stringFormat, size);
604 cbItem.pszText = (LPWSTR)buffer;
605 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
608 static void populate_size_list(HWND hSizeListWnd)
610 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
611 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
612 COMBOBOXEXITEMW cbFontItem;
614 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
615 HDC hdc = GetDC(hMainWnd);
616 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
621 ZeroMemory(&fmt, sizeof(fmt));
622 fmt.cbSize = sizeof(fmt);
623 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
625 cbFontItem.mask = CBEIF_LPARAM;
626 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
627 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
629 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
631 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
633 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
635 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
636 GetDeviceCaps(hdc, LOGPIXELSY)));
639 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
640 add_size(hSizeListWnd, choices[i]);
643 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
644 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
647 static void update_size_list(void)
649 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
650 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
651 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
652 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
655 ZeroMemory(&fmt, sizeof(fmt));
656 fmt.cbSize = sizeof(fmt);
658 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
660 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
661 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
663 if(lstrcmpW(fontSize, sizeBuffer))
664 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
667 static void update_font_list(void)
669 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
670 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
671 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
672 WCHAR fontName[MAX_STRING_LEN];
675 ZeroMemory(&fmt, sizeof(fmt));
676 fmt.cbSize = sizeof(fmt);
678 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
679 SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName);
681 if(lstrcmpW(fontName, fmt.szFaceName))
683 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
684 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
691 static void clear_formatting(void)
695 pf.cbSize = sizeof(pf);
696 pf.dwMask = PFM_ALIGNMENT;
697 pf.wAlignment = PFA_LEFT;
698 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
701 static int reg_formatindex(WPARAM format)
703 return (format & SF_TEXT) ? 1 : 0;
706 static int fileformat_number(WPARAM format)
710 if(format == SF_TEXT)
713 } else if (format == (SF_TEXT | SF_UNICODE))
720 static WPARAM fileformat_flags(int format)
722 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
724 return flags[format];
727 static void set_font(LPCWSTR wszFaceName)
729 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
730 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
731 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
732 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
735 ZeroMemory(&fmt, sizeof(fmt));
737 fmt.cbSize = sizeof(fmt);
738 fmt.dwMask = CFM_FACE;
740 lstrcpyW(fmt.szFaceName, wszFaceName);
742 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
744 populate_size_list(hSizeListWnd);
746 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)(LPWSTR)wszFaceName);
749 static void set_default_font(void)
751 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
752 'R','o','m','a','n',0};
753 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
757 ZeroMemory(&fmt, sizeof(fmt));
759 fmt.cbSize = sizeof(fmt);
760 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
763 if(fileFormat & SF_RTF)
766 font = plainTextFont;
768 lstrcpyW(fmt.szFaceName, font);
770 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
773 static void add_font(LPWSTR fontName, DWORD fontType, HWND hListWnd, NEWTEXTMETRICEXW *ntmc)
775 COMBOBOXEXITEMW cbItem;
776 WCHAR buffer[MAX_PATH];
779 cbItem.mask = CBEIF_TEXT;
780 cbItem.pszText = buffer;
781 cbItem.cchTextMax = MAX_STRING_LEN;
784 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
786 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
791 cbItem.pszText = fontName;
793 cbItem.mask |= CBEIF_LPARAM;
794 if(fontType & RASTER_FONTTYPE)
795 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
797 cbItem.lParam = MAKELONG(fontType,fontHeight);
798 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
801 static void dialog_choose_font(void)
806 HDC hDC = GetDC(hMainWnd);
808 ZeroMemory(&cf, sizeof(cf));
809 cf.lStructSize = sizeof(cf);
810 cf.hwndOwner = hMainWnd;
812 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
814 ZeroMemory(&fmt, sizeof(fmt));
815 fmt.cbSize = sizeof(fmt);
817 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
818 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
819 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) ? TRUE : FALSE;
820 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
821 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE;
822 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE;
823 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
824 cf.rgbColors = fmt.crTextColor;
828 ZeroMemory(&fmt, sizeof(fmt));
829 fmt.cbSize = sizeof(fmt);
830 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
831 fmt.yHeight = cf.iPointSize * 2;
833 if(cf.nFontType & BOLD_FONTTYPE)
834 fmt.dwEffects |= CFE_BOLD;
835 if(cf.nFontType & ITALIC_FONTTYPE)
836 fmt.dwEffects |= CFE_ITALIC;
837 if(cf.lpLogFont->lfUnderline == TRUE)
838 fmt.dwEffects |= CFE_UNDERLINE;
839 if(cf.lpLogFont->lfStrikeOut == TRUE)
840 fmt.dwEffects |= CFE_STRIKEOUT;
842 fmt.crTextColor = cf.rgbColors;
844 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
845 set_font(cf.lpLogFont->lfFaceName);
850 int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
851 DWORD FontType, LPARAM lParam)
853 HWND hListWnd = (HWND) lParam;
855 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
858 add_font((LPWSTR)lpelfe->lfFaceName, FontType, hListWnd, (NEWTEXTMETRICEXW*)lpntme);
864 static void populate_font_list(HWND hListWnd)
866 HDC hdc = GetDC(hMainWnd);
868 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
871 fontinfo.lfCharSet = DEFAULT_CHARSET;
872 *fontinfo.lfFaceName = '\0';
873 fontinfo.lfPitchAndFamily = 0;
875 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
876 (LPARAM)hListWnd, 0);
878 ZeroMemory(&fmt, sizeof(fmt));
879 fmt.cbSize = sizeof(fmt);
880 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
881 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
884 static void update_window(void)
888 GetWindowRect(hMainWnd, &rect);
890 (void) OnSize(hMainWnd, SIZE_RESTORED, MAKELONG(rect.bottom, rect.right));
893 static DWORD barState[2];
894 static DWORD wordWrap[2];
896 static BOOL is_bar_visible(int bandId)
898 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
901 static void store_bar_state(int bandId, BOOL show)
903 int formatIndex = reg_formatindex(fileFormat);
906 barState[formatIndex] |= (1 << bandId);
908 barState[formatIndex] &= ~(1 << bandId);
911 static void set_toolbar_state(int bandId, BOOL show)
913 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
915 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
917 if(bandId == BANDID_TOOLBAR)
919 REBARBANDINFOW rbbinfo;
920 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
922 rbbinfo.cbSize = sizeof(rbbinfo);
923 rbbinfo.fMask = RBBIM_STYLE;
925 SendMessageW(hwndReBar, RB_GETBANDINFO, index, (LPARAM)&rbbinfo);
928 rbbinfo.fStyle &= ~RBBS_BREAK;
930 rbbinfo.fStyle |= RBBS_BREAK;
932 SendMessageW(hwndReBar, RB_SETBANDINFO, index, (LPARAM)&rbbinfo);
935 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR)
936 store_bar_state(bandId, show);
939 static void set_statusbar_state(BOOL show)
941 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
943 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
944 store_bar_state(BANDID_STATUSBAR, show);
947 static void set_bar_states(void)
949 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
950 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
951 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
952 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
953 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
958 static HGLOBAL devMode;
959 static HGLOBAL devNames;
961 static HDC make_dc(void)
963 if(devNames && devMode)
965 LPDEVNAMES dn = GlobalLock(devNames);
966 LPDEVMODEW dm = GlobalLock(devMode);
969 ret = CreateDCW((LPWSTR)dn + dn->wDriverOffset,
970 (LPWSTR)dn + dn->wDeviceOffset, 0, dm);
982 static LONG twips_to_pixels(int twips, int dpi)
984 float ret = ((float)twips / ((float)567 * 2.54)) * (float)dpi;
988 static LONG devunits_to_twips(int units, int dpi)
990 float ret = ((float)units / (float)dpi) * (float)567 * 2.54;
994 static LONG centmm_to_twips(int mm)
996 return MulDiv(mm, 567, 1000);
999 static LONG twips_to_centmm(int twips)
1001 return MulDiv(twips, 1000, 567);
1004 static RECT get_print_rect(HDC hdc)
1011 int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
1012 int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
1013 width = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALWIDTH), dpiX);
1014 height = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALHEIGHT), dpiY);
1017 width = centmm_to_twips(18500);
1018 height = centmm_to_twips(27000);
1021 rc.left = margins.left;
1022 rc.right = width - margins.right;
1023 rc.top = margins.top;
1024 rc.bottom = height - margins.bottom;
1029 static void target_device(void)
1031 HDC hdc = make_dc();
1033 int index = reg_formatindex(fileFormat);
1035 if(wordWrap[index] == ID_WORDWRAP_MARGIN)
1037 RECT rc = get_print_rect(hdc);
1043 HDC hMaindc = GetDC(hMainWnd);
1044 hdc = CreateCompatibleDC(hMaindc);
1045 ReleaseDC(hMainWnd, hMaindc);
1048 SendMessageW(hEditorWnd, EM_SETTARGETDEVICE, (WPARAM)hdc, width);
1053 static void set_fileformat(WPARAM format)
1056 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1057 fileFormat = format;
1059 if(format & SF_TEXT)
1060 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_TXT));
1062 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_RTF));
1064 SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
1071 static void DoOpenFile(LPCWSTR szOpenFileName)
1077 WPARAM format = SF_TEXT;
1079 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
1080 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1081 if (hFile == INVALID_HANDLE_VALUE)
1084 ReadFile(hFile, fileStart, 5, &readOut, NULL);
1085 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1087 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
1089 format = SF_TEXT | SF_UNICODE;
1090 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
1091 } else if(readOut >= 5)
1093 static const char header[] = "{\\rtf";
1094 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
1096 if(!memcmp(header, fileStart, 5))
1098 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
1101 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED), wszAppTitle,
1102 MB_OK | MB_ICONEXCLAMATION);
1107 es.dwCookie = (DWORD_PTR)hFile;
1108 es.pfnCallback = stream_in;
1111 set_fileformat(format);
1112 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
1116 SetFocus(hEditorWnd);
1118 set_caption(szOpenFileName);
1120 lstrcpyW(wszFileName, szOpenFileName);
1121 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1122 registry_set_filelist(szOpenFileName);
1126 static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
1132 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1133 FILE_ATTRIBUTE_NORMAL, NULL);
1135 if(hFile == INVALID_HANDLE_VALUE)
1138 if(format == (SF_TEXT | SF_UNICODE))
1140 static const BYTE unicode[] = {0xff,0xfe};
1142 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
1144 if(writeOut != sizeof(unicode))
1148 stream.dwCookie = (DWORD_PTR)hFile;
1149 stream.pfnCallback = stream_out;
1151 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
1155 SetFocus(hEditorWnd);
1160 gt.flags = GTL_DEFAULT;
1163 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0))
1167 lstrcpyW(wszFileName, wszSaveFileName);
1168 set_caption(wszFileName);
1169 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1170 set_fileformat(format);
1173 static void DialogSaveFile(void)
1177 WCHAR wszFile[MAX_PATH] = {'\0'};
1178 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
1180 ZeroMemory(&sfn, sizeof(sfn));
1182 sfn.lStructSize = sizeof(sfn);
1183 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
1184 sfn.hwndOwner = hMainWnd;
1185 sfn.lpstrFilter = wszFilter;
1186 sfn.lpstrFile = wszFile;
1187 sfn.nMaxFile = MAX_PATH;
1188 sfn.lpstrDefExt = wszDefExt;
1189 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
1191 while(GetSaveFileNameW(&sfn))
1193 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
1195 if(MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
1196 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
1201 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
1206 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
1212 static BOOL prompt_save_changes(void)
1217 gt.flags = GTL_NUMCHARS;
1219 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0))
1223 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
1228 LPWSTR displayFileName;
1233 displayFileName = wszDefaultFileName;
1235 displayFileName = file_basename(wszFileName);
1237 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1238 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
1243 wsprintfW(text, wszSaveChanges, displayFileName);
1245 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
1247 HeapFree(GetProcessHeap(), 0, text);
1256 DoSaveFile(wszFileName, fileFormat);
1267 static void DialogOpenFile(void)
1271 WCHAR wszFile[MAX_PATH] = {'\0'};
1272 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
1274 ZeroMemory(&ofn, sizeof(ofn));
1276 ofn.lStructSize = sizeof(ofn);
1277 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
1278 ofn.hwndOwner = hMainWnd;
1279 ofn.lpstrFilter = wszFilter;
1280 ofn.lpstrFile = wszFile;
1281 ofn.nMaxFile = MAX_PATH;
1282 ofn.lpstrDefExt = wszDefExt;
1283 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
1285 if(GetOpenFileNameW(&ofn))
1287 if(prompt_save_changes())
1288 DoOpenFile(ofn.lpstrFile);
1292 static LPWSTR dialog_print_to_file(void)
1295 static WCHAR file[MAX_PATH] = {'O','U','T','P','U','T','.','P','R','N',0};
1296 static const WCHAR defExt[] = {'P','R','N',0};
1298 ZeroMemory(&ofn, sizeof(ofn));
1300 ofn.lStructSize = sizeof(ofn);
1301 ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
1302 ofn.hwndOwner = hMainWnd;
1303 ofn.lpstrFilter = (LPWSTR)wszPrintFilter;
1304 ofn.lpstrFile = (LPWSTR)file;
1305 ofn.nMaxFile = MAX_PATH;
1306 ofn.lpstrDefExt = (LPWSTR)defExt;
1308 if(GetSaveFileNameW(&ofn))
1309 return (LPWSTR)file;
1314 static int get_num_pages(FORMATRANGE fr)
1321 fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE,
1324 while(fr.chrg.cpMin && fr.chrg.cpMin < fr.chrg.cpMax);
1329 static void char_from_pagenum(FORMATRANGE *fr, int page)
1333 for(i = 1; i <= page; i++)
1338 fr->chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)fr);
1342 static void print(LPPRINTDLGW pd)
1346 int printedPages = 0;
1349 fr.hdcTarget = pd->hDC;
1351 fr.rc = get_print_rect(fr.hdc);
1353 fr.rcPage.right = fr.rc.right + margins.right;
1355 fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
1357 ZeroMemory(&di, sizeof(di));
1358 di.cbSize = sizeof(di);
1359 di.lpszDocName = (LPWSTR)wszFileName;
1361 if(pd->Flags & PD_PRINTTOFILE)
1363 di.lpszOutput = dialog_print_to_file();
1368 if(pd->Flags & PD_SELECTION)
1370 SendMessageW(hEditorWnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);
1374 gt.flags = GTL_DEFAULT;
1377 fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
1379 if(pd->Flags & PD_PAGENUMS)
1380 char_from_pagenum(&fr, pd->nToPage);
1383 StartDocW(fr.hdc, &di);
1386 if(StartPage(fr.hdc) <= 0)
1389 fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
1391 if(EndPage(fr.hdc) <= 0)
1395 if((pd->Flags & PD_PAGENUMS) && (printedPages > (pd->nToPage - pd->nFromPage)))
1398 while(fr.chrg.cpMin && fr.chrg.cpMin < fr.chrg.cpMax);
1401 SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
1405 static void dialog_printsetup(void)
1409 ZeroMemory(&ps, sizeof(ps));
1410 ps.lStructSize = sizeof(ps);
1411 ps.hwndOwner = hMainWnd;
1412 ps.Flags = PSD_INHUNDREDTHSOFMILLIMETERS | PSD_MARGINS;
1413 ps.rtMargin.left = twips_to_centmm(margins.left);
1414 ps.rtMargin.right = twips_to_centmm(margins.right);
1415 ps.rtMargin.top = twips_to_centmm(margins.top);
1416 ps.rtMargin.bottom = twips_to_centmm(margins.bottom);
1417 ps.hDevMode = devMode;
1418 ps.hDevNames = devNames;
1420 if(PageSetupDlgW(&ps))
1422 margins.left = centmm_to_twips(ps.rtMargin.left);
1423 margins.right = centmm_to_twips(ps.rtMargin.right);
1424 margins.top = centmm_to_twips(ps.rtMargin.top);
1425 margins.bottom = centmm_to_twips(ps.rtMargin.bottom);
1426 devMode = ps.hDevMode;
1427 devNames = ps.hDevNames;
1432 static void get_default_printer_opts(void)
1435 ZeroMemory(&pd, sizeof(pd));
1437 ZeroMemory(&pd, sizeof(pd));
1438 pd.lStructSize = sizeof(pd);
1439 pd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
1440 pd.hwndOwner = hMainWnd;
1441 pd.hDevMode = devMode;
1445 devMode = pd.hDevMode;
1446 devNames = pd.hDevNames;
1449 static void print_quick(void)
1453 ZeroMemory(&pd, sizeof(pd));
1459 static void dialog_print(void)
1465 ZeroMemory(&pd, sizeof(pd));
1466 pd.lStructSize = sizeof(pd);
1467 pd.hwndOwner = hMainWnd;
1468 pd.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE;
1471 pd.hDevMode = devMode;
1472 pd.hDevNames = devNames;
1474 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1476 pd.Flags |= PD_NOSELECTION;
1480 devMode = pd.hDevMode;
1481 devNames = pd.hDevNames;
1486 typedef struct _previewinfo
1493 } previewinfo, *ppreviewinfo;
1495 static previewinfo preview;
1497 static void preview_bar_show(BOOL show)
1499 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
1506 AddTextButton(hReBar, STRING_PREVIEW_PRINT, ID_PRINT, BANDID_PREVIEW_BTN1);
1507 AddTextButton(hReBar, STRING_PREVIEW_NEXTPAGE, ID_PREVIEW_NEXTPAGE, BANDID_PREVIEW_BTN2);
1508 AddTextButton(hReBar, STRING_PREVIEW_PREVPAGE, ID_PREVIEW_PREVPAGE, BANDID_PREVIEW_BTN3);
1509 AddTextButton(hReBar, STRING_PREVIEW_CLOSE, ID_FILE_EXIT, BANDID_PREVIEW_BTN4);
1511 rb.cbSize = sizeof(rb);
1512 rb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_CHILD | RBBIM_IDEALSIZE | RBBIM_ID;
1513 rb.fStyle = RBBS_NOGRIPPER | RBBS_VARIABLEHEIGHT;
1514 rb.cyChild = rb.cyMinChild = 22;
1515 rb.cx = rb.cxMinChild = 90;
1517 rb.wID = BANDID_PREVIEW_BUFFER;
1519 SendMessageW(hReBar, RB_INSERTBAND, -1, (LPARAM)&rb);
1522 for(i = 0; i <= PREVIEW_BUTTONS; i++)
1523 SendMessageW(hReBar, RB_DELETEBAND, SendMessageW(hReBar, RB_IDTOINDEX, BANDID_PREVIEW_BTN1+i, 0), 0);
1527 static void preview_exit(void)
1529 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1530 HMENU hMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_MAINMENU));
1533 preview.window.right = 0;
1534 preview.window.bottom = 0;
1537 ShowWindow(hEditorWnd, TRUE);
1539 preview_bar_show(FALSE);
1541 SetMenu(hMainWnd, hMenu);
1542 registry_read_filelist(hMainWnd);
1547 static LRESULT print_preview(void)
1552 RECT window, background;
1553 HBITMAP hBitmapCapture, hBitmapScaled;
1554 int bmWidth, bmHeight, bmNewWidth, bmNewHeight;
1555 float ratioWidth, ratioHeight, ratio;
1556 int xOffset, yOffset;
1558 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
1561 hdc = BeginPaint(hMainWnd, &ps);
1562 GetClientRect(hMainWnd, &window);
1564 fr.hdcTarget = make_dc();
1565 fr.rc = get_print_rect(fr.hdcTarget);
1568 fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
1569 fr.rcPage.right = fr.rc.right + margins.right;
1571 bmWidth = twips_to_pixels(fr.rcPage.right, GetDeviceCaps(hdc, LOGPIXELSX));
1572 bmHeight = twips_to_pixels(fr.rcPage.bottom, GetDeviceCaps(hdc, LOGPIXELSY));
1574 hBitmapCapture = CreateCompatibleBitmap(hdc, bmWidth, bmHeight);
1580 preview.hdc = CreateCompatibleDC(hdc);
1581 fr.hdc = preview.hdc;
1582 gt.flags = GTL_DEFAULT;
1585 fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
1588 paper.right = bmWidth;
1590 paper.bottom = bmHeight;
1593 preview.pages = get_num_pages(fr);
1595 SelectObject(preview.hdc, hBitmapCapture);
1597 char_from_pagenum(&fr, preview.page);
1599 FillRect(preview.hdc, &paper, GetStockObject(WHITE_BRUSH));
1600 SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
1601 SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
1603 EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_PREVPAGE), preview.page > 1);
1604 EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_NEXTPAGE), preview.page < preview.pages);
1607 barheight = SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
1608 ratioWidth = ((float)window.right - 20.0) / (float)bmHeight;
1609 ratioHeight = ((float)window.bottom - 20.0 - (float)barheight) / (float)bmHeight;
1611 if(ratioWidth > ratioHeight)
1612 ratio = ratioHeight;
1616 bmNewWidth = (int)((float)bmWidth * ratio);
1617 bmNewHeight = (int)((float)bmHeight * ratio);
1618 hBitmapScaled = CreateCompatibleBitmap(hdc, bmNewWidth, bmNewHeight);
1620 xOffset = ((window.right - bmNewWidth) / 2);
1621 yOffset = ((window.bottom - bmNewHeight + barheight) / 2);
1623 if(window.right != preview.window.right || window.bottom != preview.window.bottom)
1625 DeleteDC(preview.hdcSized),
1626 preview.hdcSized = CreateCompatibleDC(hdc);
1627 SelectObject(preview.hdcSized, hBitmapScaled);
1629 StretchBlt(preview.hdcSized, 0, 0, bmNewWidth, bmNewHeight, preview.hdc, 0, 0, bmWidth, bmHeight, SRCCOPY);
1632 window.top = barheight;
1633 FillRect(hdc, &window, GetStockObject(GRAY_BRUSH));
1635 SelectObject(hdc, hBitmapScaled);
1637 background.left = xOffset - 2;
1638 background.right = xOffset + bmNewWidth + 2;
1639 background.top = yOffset - 2;
1640 background.bottom = yOffset + bmNewHeight + 2;
1642 FillRect(hdc, &background, GetStockObject(BLACK_BRUSH));
1644 BitBlt(hdc, xOffset, yOffset, bmNewWidth, bmNewHeight, preview.hdcSized, 0, 0, SRCCOPY);
1646 DeleteDC(fr.hdcTarget);
1647 preview.window = window;
1649 EndPaint(hMainWnd, &ps);
1654 static LRESULT preview_command(HWND hWnd, WPARAM wParam, LPARAM lParam)
1656 switch(LOWORD(wParam))
1659 PostMessageW(hMainWnd, WM_CLOSE, 0, 0);
1662 case ID_PREVIEW_NEXTPAGE:
1663 case ID_PREVIEW_PREVPAGE:
1665 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
1668 if(LOWORD(wParam) == ID_PREVIEW_NEXTPAGE)
1674 preview.window.right = 0;
1676 GetClientRect(hMainWnd, &rc);
1677 rc.top += SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
1678 InvalidateRect(hMainWnd, &rc, TRUE);
1692 static void dialog_about(void)
1694 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1695 HICON icon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
1696 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1699 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1705 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1708 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1710 sprintf(id, "%d\n", (int)ps->lParam);
1711 SetWindowTextA(hIdWnd, id);
1712 if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1713 wrap = IDC_PAGEFMT_WW;
1714 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1715 wrap = IDC_PAGEFMT_WM;
1718 CheckRadioButton(hWnd, IDC_PAGEFMT_WW,
1719 IDC_PAGEFMT_WM, wrap);
1721 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1722 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1723 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1724 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1725 if(barState[ps->lParam] & (BANDID_STATUSBAR))
1726 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1731 switch(LOWORD(wParam))
1733 case IDC_PAGEFMT_WW:
1734 case IDC_PAGEFMT_WM:
1735 CheckRadioButton(hWnd, IDC_PAGEFMT_WW, IDC_PAGEFMT_WM,
1739 case IDC_PAGEFMT_TB:
1740 case IDC_PAGEFMT_FB:
1741 case IDC_PAGEFMT_SB:
1742 CheckDlgButton(hWnd, LOWORD(wParam),
1743 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1749 LPNMHDR header = (LPNMHDR)lParam;
1750 if(header->code == PSN_APPLY)
1752 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1756 GetWindowTextA(hIdWnd, sid, 4);
1758 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1759 wordWrap[id] = ID_WORDWRAP_WINDOW;
1760 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1761 wordWrap[id] = ID_WORDWRAP_MARGIN;
1763 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1764 barState[id] |= (1 << BANDID_TOOLBAR);
1766 barState[id] &= ~(1 << BANDID_TOOLBAR);
1768 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1769 barState[id] |= (1 << BANDID_FORMATBAR);
1771 barState[id] &= ~(1 << BANDID_FORMATBAR);
1773 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1774 barState[id] |= (1 << BANDID_STATUSBAR);
1776 barState[id] &= ~(1 << BANDID_STATUSBAR);
1784 static void dialog_viewproperties(void)
1786 PROPSHEETPAGEW psp[2];
1787 PROPSHEETHEADERW psh;
1789 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1790 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1792 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1793 psp[0].dwFlags = PSP_USETITLE;
1794 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1795 psp[0].pfnDlgProc = formatopts_proc;
1796 psp[0].hInstance = hInstance;
1797 psp[0].lParam = reg_formatindex(SF_TEXT);
1798 psp[0].pfnCallback = NULL;
1799 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1800 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1802 psp[i].dwSize = psp[0].dwSize;
1803 psp[i].dwFlags = psp[0].dwFlags;
1804 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1805 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1806 psp[i].hInstance = psp[0].hInstance;
1807 psp[i].lParam = reg_formatindex(SF_RTF);
1808 psp[i].pfnCallback = psp[0].pfnCallback;
1809 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1812 psh.dwSize = sizeof(psh);
1813 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1814 psh.hwndParent = hMainWnd;
1815 psh.hInstance = hInstance;
1816 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1817 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1818 U3(psh).ppsp = ppsp;
1819 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1821 if(fileFormat & SF_RTF)
1822 U2(psh).nStartPage = 1;
1824 U2(psh).nStartPage = 0;
1825 PropertySheetW(&psh);
1830 static void HandleCommandLine(LPWSTR cmdline)
1835 /* skip white space */
1836 while (*cmdline == ' ') cmdline++;
1838 /* skip executable name */
1839 delimiter = (*cmdline == '"' ? '"' : ' ');
1841 if (*cmdline == delimiter) cmdline++;
1842 while (*cmdline && *cmdline != delimiter) cmdline++;
1843 if (*cmdline == delimiter) cmdline++;
1845 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
1849 if (*cmdline++ == ' ') continue;
1852 if (option) cmdline++;
1853 while (*cmdline == ' ') cmdline++;
1866 /* file name is passed on the command line */
1867 if (cmdline[0] == '"')
1870 cmdline[lstrlenW(cmdline) - 1] = 0;
1872 DoOpenFile(cmdline);
1873 InvalidateRect(hMainWnd, NULL, FALSE);
1877 MessageBox(hMainWnd, "Printing not implemented", "WordPad", MB_OK);
1880 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1882 if(pFr->Flags & FR_DIALOGTERM)
1885 pFr->Flags = FR_FINDNEXT;
1889 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1891 DWORD flags = FR_DOWN;
1893 static CHARRANGE cr;
1898 HMENU hMenu = GetMenu(hMainWnd);
1901 mi.cbSize = sizeof(mi);
1902 mi.fMask = MIIM_DATA;
1904 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1906 gt.flags = GTL_NUMCHARS;
1909 length = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
1911 if(pFr->lCustData == -1)
1913 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&startPos, (LPARAM)&end);
1914 cr.cpMin = startPos;
1915 pFr->lCustData = startPos;
1917 if(cr.cpMin == length)
1921 startPos = pFr->lCustData;
1924 if(cr.cpMax > length)
1932 ft.lpstrText = pFr->lpstrFindWhat;
1934 if(pFr->Flags & FR_MATCHCASE)
1935 flags |= FR_MATCHCASE;
1936 if(pFr->Flags & FR_WHOLEWORD)
1937 flags |= FR_WHOLEWORD;
1939 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1943 if(cr.cpMax == length && cr.cpMax != startPos)
1945 ft.chrg.cpMin = cr.cpMin = 0;
1946 ft.chrg.cpMax = cr.cpMax = startPos;
1948 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1954 pFr->lCustData = -1;
1955 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED), wszAppTitle,
1956 MB_OK | MB_ICONASTERISK);
1959 end = ret + lstrlenW(pFr->lpstrFindWhat);
1961 SendMessageW(hEditorWnd, EM_SETSEL, (WPARAM)ret, (LPARAM)end);
1962 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1964 if(pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1965 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1967 if(pFr->Flags & FR_REPLACEALL)
1968 handle_findmsg(pFr);
1975 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1977 static WCHAR findBuffer[MAX_STRING_LEN];
1979 ZeroMemory(fr, sizeof(FINDREPLACEW));
1980 fr->lStructSize = sizeof(FINDREPLACEW);
1981 fr->hwndOwner = hMainWnd;
1982 fr->Flags = FR_HIDEUPDOWN;
1983 fr->lpstrFindWhat = findBuffer;
1985 fr->wFindWhatLen = MAX_STRING_LEN*sizeof(WCHAR);
1988 hFindWnd = ReplaceTextW(fr);
1990 hFindWnd = FindTextW(fr);
1993 static void registry_read_options(void)
1996 DWORD size = sizeof(RECT);
1998 if(registry_get_handle(&hKey, 0, key_options) != ERROR_SUCCESS ||
1999 RegQueryValueExW(hKey, var_pagemargin, 0, NULL, (LPBYTE)&margins,
2000 &size) != ERROR_SUCCESS || size != sizeof(RECT))
2003 margins.bottom = 1417;
2004 margins.left = 1757;
2005 margins.right = 1757;
2011 static void registry_read_formatopts(int index, LPCWSTR key)
2015 BOOL fetched = FALSE;
2016 barState[index] = 0;
2017 wordWrap[index] = 0;
2019 if(registry_get_handle(&hKey, &action, key) != ERROR_SUCCESS)
2022 if(action == REG_OPENED_EXISTING_KEY)
2024 DWORD size = sizeof(DWORD);
2026 if(RegQueryValueExW(hKey, var_barstate0, 0, NULL, (LPBYTE)&barState[index],
2027 &size) == ERROR_SUCCESS)
2032 barState[index] = (1 << BANDID_TOOLBAR) | (1 << BANDID_FORMATBAR) | (1 << BANDID_RULER) | (1 << BANDID_STATUSBAR);
2034 if(index == reg_formatindex(SF_RTF))
2035 wordWrap[index] = ID_WORDWRAP_WINDOW;
2036 else if(index == reg_formatindex(SF_TEXT))
2037 wordWrap[index] = ID_WORDWRAP_WINDOW; /* FIXME: should be ID_WORDWRAP_NONE once we support it */
2042 static void registry_read_formatopts_all(void)
2044 registry_read_formatopts(reg_formatindex(SF_RTF), key_rtf);
2045 registry_read_formatopts(reg_formatindex(SF_TEXT), key_text);
2048 static void registry_set_formatopts(int index, LPCWSTR key)
2053 if(registry_get_handle(&hKey, &action, key) == ERROR_SUCCESS)
2055 RegSetValueExW(hKey, var_barstate0, 0, REG_DWORD, (LPBYTE)&barState[index],
2062 static void registry_set_formatopts_all(void)
2064 registry_set_formatopts(reg_formatindex(SF_RTF), key_rtf);
2065 registry_set_formatopts(reg_formatindex(SF_TEXT), key_text);
2068 static int current_units_to_twips(float number)
2070 int twips = (int)(number * 567);
2074 static void append_current_units(LPWSTR buffer)
2076 static const WCHAR space[] = {' '};
2077 lstrcatW(buffer, space);
2078 lstrcatW(buffer, units_cmW);
2081 static void number_with_units(LPWSTR buffer, int number)
2083 float converted = (float)number / 567;
2084 char string[MAX_STRING_LEN];
2086 sprintf(string, "%.2f ", converted);
2087 lstrcatA(string, units_cmA);
2088 MultiByteToWideChar(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN);
2091 BOOL CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2097 WCHAR buffer[MAX_STRING_LEN];
2099 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
2102 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
2104 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2105 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
2107 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2108 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
2109 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2111 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
2116 switch(LOWORD(wParam))
2121 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
2123 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
2127 WCHAR buffer[MAX_STRING_LEN];
2128 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
2129 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
2135 EndDialog(hWnd, wParam);
2142 BOOL CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2148 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
2149 WCHAR buffer[MAX_STRING_LEN];
2150 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
2152 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, (LPWSTR)buffer, MAX_STRING_LEN);
2153 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2154 LoadStringW(hInstance, STRING_NEWFILE_TXT, (LPWSTR)buffer, MAX_STRING_LEN);
2155 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2156 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, (LPWSTR)buffer, MAX_STRING_LEN);
2157 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2159 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
2164 switch(LOWORD(wParam))
2169 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
2170 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
2173 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
2178 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
2185 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2191 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd,
2193 WCHAR buffer[MAX_STRING_LEN];
2194 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
2195 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
2196 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
2197 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
2201 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
2203 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
2204 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
2206 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
2207 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
2209 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
2211 pf.cbSize = sizeof(pf);
2212 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
2214 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2216 if(pf.wAlignment == PFA_RIGHT)
2218 else if(pf.wAlignment == PFA_CENTER)
2221 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
2223 number_with_units(buffer, pf.dxOffset);
2224 SetWindowTextW(hLeftWnd, buffer);
2225 number_with_units(buffer, pf.dxRightIndent);
2226 SetWindowTextW(hRightWnd, buffer);
2227 number_with_units(buffer, pf.dxStartIndent - pf.dxOffset);
2228 SetWindowTextW(hFirstWnd, buffer);
2233 switch(LOWORD(wParam))
2237 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
2238 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
2239 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
2240 WCHAR buffer[MAX_STRING_LEN];
2245 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
2246 if(number_from_string(buffer, &num, TRUE))
2248 pf.dxOffset = current_units_to_twips(num);
2249 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
2250 if(number_from_string(buffer, &num, TRUE))
2252 pf.dxRightIndent = current_units_to_twips(num);
2253 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
2254 if(number_from_string(buffer, &num, TRUE))
2256 pf.dxStartIndent = current_units_to_twips(num);
2260 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
2261 wszAppTitle, MB_OK | MB_ICONASTERISK);
2265 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
2266 pf.cbSize = sizeof(pf);
2267 pf.dwMask = PFM_OFFSET | PFM_OFFSETINDENT | PFM_RIGHTINDENT;
2268 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2274 EndDialog(hWnd, wParam);
2281 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2287 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2289 WCHAR buffer[MAX_STRING_LEN];
2292 pf.cbSize = sizeof(pf);
2293 pf.dwMask = PFM_TABSTOPS;
2294 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2295 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
2297 for(i = 0; i < pf.cTabCount; i++)
2299 number_with_units(buffer, pf.rgxTabs[i]);
2300 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
2307 switch(LOWORD(wParam))
2311 HWND hTabWnd = (HWND)lParam;
2312 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
2313 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
2314 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
2316 if(GetWindowTextLengthW(hTabWnd))
2317 EnableWindow(hAddWnd, TRUE);
2319 EnableWindow(hAddWnd, FALSE);
2321 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
2323 EnableWindow(hEmptyWnd, TRUE);
2325 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
2326 EnableWindow(hDelWnd, FALSE);
2328 EnableWindow(hDelWnd, TRUE);
2331 EnableWindow(hEmptyWnd, FALSE);
2338 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2339 WCHAR buffer[MAX_STRING_LEN];
2341 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
2342 append_current_units(buffer);
2344 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
2348 if(!number_from_string(buffer, &number, TRUE))
2350 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
2351 wszAppTitle, MB_OK | MB_ICONINFORMATION);
2354 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
2355 SetWindowTextW(hTabWnd, 0);
2364 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2366 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
2368 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
2374 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2375 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
2382 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2384 WCHAR buffer[MAX_STRING_LEN];
2388 pf.cbSize = sizeof(pf);
2389 pf.dwMask = PFM_TABSTOPS;
2391 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
2392 (LPARAM)&buffer) != CB_ERR &&
2393 i < MAX_TAB_STOPS; i++)
2395 number_from_string(buffer, &number, TRUE);
2396 pf.rgxTabs[i] = current_units_to_twips(number);
2399 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2403 EndDialog(hWnd, wParam);
2410 static int context_menu(LPARAM lParam)
2412 int x = (int)(short)LOWORD(lParam);
2413 int y = (int)(short)HIWORD(lParam);
2414 HMENU hPop = GetSubMenu(hPopupMenu, 0);
2418 int from = 0, to = 0;
2420 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2421 SendMessageW(hEditorWnd, EM_POSFROMCHAR, (WPARAM)&pt, (LPARAM)to);
2422 ClientToScreen(hEditorWnd, (POINT*)&pt);
2427 TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
2428 x, y, 0, hMainWnd, 0);
2433 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
2435 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd;
2436 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2439 int nStdBitmaps = 0;
2442 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
2443 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
2445 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
2447 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
2448 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
2449 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
2451 rbi.cbSize = sizeof(rbi);
2454 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
2457 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
2459 1, hInstance, IDB_TOOLBAR,
2461 24, 24, 16, 16, sizeof(TBBUTTON));
2463 ab.hInst = HINST_COMMCTRL;
2464 ab.nID = IDB_STD_SMALL_COLOR;
2465 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
2467 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
2468 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
2469 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
2470 AddSeparator(hToolBarWnd);
2471 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
2472 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
2473 AddSeparator(hToolBarWnd);
2474 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
2475 AddSeparator(hToolBarWnd);
2476 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
2477 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
2478 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
2479 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
2480 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
2481 AddSeparator(hToolBarWnd);
2482 AddButton(hToolBarWnd, 0, ID_DATETIME);
2484 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
2486 rbb.cbSize = sizeof(rbb);
2487 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
2488 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
2490 rbb.hwndChild = hToolBarWnd;
2492 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
2493 rbb.wID = BANDID_TOOLBAR;
2495 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2497 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
2498 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
2499 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
2501 rbb.hwndChild = hFontListWnd;
2503 rbb.wID = BANDID_FONTLIST;
2505 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2507 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
2508 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
2509 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
2511 rbb.hwndChild = hSizeListWnd;
2513 rbb.fStyle ^= RBBS_BREAK;
2514 rbb.wID = BANDID_SIZELIST;
2516 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2518 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
2519 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
2520 IDC_FORMATBAR, 7, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
2522 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
2523 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
2524 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
2525 AddSeparator(hFormatBarWnd);
2526 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
2527 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
2528 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
2529 AddSeparator(hFormatBarWnd);
2530 AddButton(hFormatBarWnd, 6, ID_BULLET);
2532 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
2534 rbb.hwndChild = hFormatBarWnd;
2535 rbb.wID = BANDID_FORMATBAR;
2537 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2539 hDLL = LoadLibraryW(wszRichEditDll);
2542 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
2543 MB_OK | MB_ICONEXCLAMATION);
2547 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
2548 WS_CHILD|WS_VISIBLE|ECO_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
2549 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
2553 fprintf(stderr, "Error code %u\n", GetLastError());
2558 SetFocus(hEditorWnd);
2559 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
2563 populate_font_list(hFontListWnd);
2564 populate_size_list(hSizeListWnd);
2566 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2568 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
2570 registry_read_filelist(hWnd);
2571 registry_read_formatopts_all();
2572 registry_read_options();
2573 DragAcceptFiles(hWnd, TRUE);
2578 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
2580 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2581 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2582 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
2583 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2589 ZeroMemory(&fmt, sizeof(fmt));
2590 fmt.cbSize = sizeof(fmt);
2592 ZeroMemory(&pf, sizeof(pf));
2593 pf.cbSize = sizeof(pf);
2595 gt.flags = GTL_NUMCHARS;
2598 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
2599 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)>, 0) ? 1 : 0);
2601 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
2603 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2604 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
2605 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
2606 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
2607 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
2608 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
2609 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2611 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2612 (fmt.dwEffects & CFE_BOLD));
2613 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2614 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2615 (fmt.dwEffects & CFE_ITALIC));
2616 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2617 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2618 (fmt.dwEffects & CFE_UNDERLINE));
2619 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2621 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2622 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2623 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2624 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2626 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
2630 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
2632 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2633 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2634 NMHDR *pHdr = (NMHDR *)lParam;
2635 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2636 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2637 WCHAR sizeBuffer[MAX_PATH];
2639 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2641 if (pHdr->code == CBEN_ENDEDITW)
2643 CHARFORMAT2W format;
2644 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
2646 ZeroMemory(&format, sizeof(format));
2647 format.cbSize = sizeof(format);
2648 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
2650 if(pHdr->hwndFrom == hwndFontList)
2652 if(lstrcmpW(format.szFaceName, (LPWSTR)endEdit->szText))
2653 set_font((LPCWSTR) endEdit->szText);
2654 } else if (pHdr->hwndFrom == hwndSizeList)
2656 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
2657 if(lstrcmpW(sizeBuffer, (LPWSTR)endEdit->szText))
2660 if(number_from_string((LPWSTR)endEdit->szText, &size, FALSE))
2665 SetWindowTextW(hwndSizeList, sizeBuffer);
2666 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER), wszAppTitle, MB_OK | MB_ICONINFORMATION);
2674 if (pHdr->hwndFrom != hwndEditor)
2677 if (pHdr->code == EN_SELCHANGE)
2679 SELCHANGE *pSC = (SELCHANGE *)lParam;
2684 sprintf( buf,"selection = %d..%d, line count=%ld",
2685 pSC->chrg.cpMin, pSC->chrg.cpMax,
2686 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
2687 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2688 SendMessage(hWnd, WM_USER, 0, 0);
2694 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2696 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2697 static FINDREPLACEW findreplace;
2699 if ((HWND)lParam == hwndEditor)
2702 switch(LOWORD(wParam))
2706 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2711 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2712 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
2713 (DLGPROC)newfile_proc);
2715 if(ret != ID_NEWFILE_ABORT)
2717 if(prompt_save_changes())
2722 wszFileName[0] = '\0';
2726 st.flags = ST_DEFAULT;
2728 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2730 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2731 set_fileformat(ret);
2745 DoSaveFile(wszFileName, fileFormat);
2750 case ID_FILE_SAVEAS:
2754 case ID_FILE_RECENT1:
2755 case ID_FILE_RECENT2:
2756 case ID_FILE_RECENT3:
2757 case ID_FILE_RECENT4:
2759 HMENU hMenu = GetMenu(hWnd);
2762 mi.cbSize = sizeof(MENUITEMINFOW);
2763 mi.fMask = MIIM_DATA;
2764 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2765 DoOpenFile((LPWSTR)mi.dwItemData);
2770 dialog_find(&findreplace, FALSE);
2774 handle_findmsg(&findreplace);
2778 dialog_find(&findreplace, TRUE);
2781 case ID_FONTSETTINGS:
2782 dialog_choose_font();
2789 case ID_PRINT_QUICK:
2795 int index = reg_formatindex(fileFormat);
2796 DWORD tmp = barState[index];
2797 barState[index] = 0;
2799 barState[index] = tmp;
2800 ShowWindow(hEditorWnd, FALSE);
2801 preview_bar_show(TRUE);
2805 SetMenu(hWnd, NULL);
2806 InvalidateRect(0, 0, TRUE);
2811 dialog_printsetup();
2814 case ID_FORMAT_BOLD:
2815 case ID_FORMAT_ITALIC:
2816 case ID_FORMAT_UNDERLINE:
2819 int effects = CFE_BOLD;
2821 ZeroMemory(&fmt, sizeof(fmt));
2822 fmt.cbSize = sizeof(fmt);
2823 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2825 fmt.dwMask = CFM_BOLD;
2827 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2829 effects = CFE_ITALIC;
2830 fmt.dwMask = CFM_ITALIC;
2831 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2833 effects = CFE_UNDERLINE;
2834 fmt.dwMask = CFM_UNDERLINE;
2837 fmt.dwEffects ^= effects;
2839 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2844 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2848 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2852 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2856 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2859 case ID_EDIT_SELECTALL:
2861 CHARRANGE range = {0, -1};
2862 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2863 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2867 case ID_EDIT_GETTEXT:
2869 int nLen = GetWindowTextLengthW(hwndEditor);
2870 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2873 GetWindowTextW(hwndEditor, data, nLen+1);
2874 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
2876 HeapFree( GetProcessHeap(), 0, data);
2877 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2879 tr.chrg.cpMax = nLen;
2880 tr.lpstrText = data;
2881 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2882 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
2883 HeapFree( GetProcessHeap(), 0, data );
2885 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2889 case ID_EDIT_CHARFORMAT:
2890 case ID_EDIT_DEFCHARFORMAT:
2894 ZeroMemory(&cf, sizeof(cf));
2895 cf.cbSize = sizeof(cf);
2897 i = SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2898 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2902 case ID_EDIT_PARAFORMAT:
2905 ZeroMemory(&pf, sizeof(pf));
2906 pf.cbSize = sizeof(pf);
2907 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2911 case ID_EDIT_SELECTIONINFO:
2913 CHARRANGE range = {0, -1};
2917 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2918 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2919 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2920 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2921 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2922 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
2923 HeapFree( GetProcessHeap(), 0, data);
2924 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2928 case ID_EDIT_READONLY:
2930 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2931 if (nStyle & ES_READONLY)
2932 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2934 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2938 case ID_EDIT_MODIFIED:
2939 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2940 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2942 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2946 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2950 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2957 pf.cbSize = sizeof(pf);
2958 pf.dwMask = PFM_NUMBERING;
2959 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2961 pf.dwMask |= PFM_OFFSET;
2963 if(pf.wNumbering == PFN_BULLET)
2969 pf.wNumbering = PFN_BULLET;
2973 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2978 case ID_ALIGN_CENTER:
2979 case ID_ALIGN_RIGHT:
2983 pf.cbSize = sizeof(pf);
2984 pf.dwMask = PFM_ALIGNMENT;
2985 switch(LOWORD(wParam)) {
2986 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2987 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2988 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2990 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2995 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2999 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
3002 case ID_TOGGLE_TOOLBAR:
3003 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
3007 case ID_TOGGLE_FORMATBAR:
3008 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
3009 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
3010 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
3014 case ID_TOGGLE_STATUSBAR:
3015 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
3021 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
3022 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_DATETIME), hWnd, (DLGPROC)datetime_proc);
3028 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
3029 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd,
3036 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
3037 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
3045 case ID_VIEWPROPERTIES:
3046 dialog_viewproperties();
3050 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
3056 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
3058 HMENU hMenu = (HMENU)wParam;
3059 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
3060 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
3062 int nAlignment = -1;
3068 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
3069 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
3070 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
3072 pf.cbSize = sizeof(PARAFORMAT);
3073 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
3074 CheckMenuItem(hMenu, ID_EDIT_READONLY,
3075 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
3076 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
3077 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
3078 if (pf.dwMask & PFM_ALIGNMENT)
3079 nAlignment = pf.wAlignment;
3080 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
3081 MF_CHECKED : MF_UNCHECKED);
3082 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
3083 MF_CHECKED : MF_UNCHECKED);
3084 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
3085 MF_CHECKED : MF_UNCHECKED);
3086 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
3087 MF_CHECKED : MF_UNCHECKED));
3088 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
3089 MF_ENABLED : MF_GRAYED);
3090 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
3091 MF_ENABLED : MF_GRAYED);
3093 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
3094 MF_CHECKED : MF_UNCHECKED);
3096 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
3097 MF_CHECKED : MF_UNCHECKED);
3099 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
3100 MF_CHECKED : MF_UNCHECKED);
3102 gt.flags = GTL_NUMCHARS;
3104 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
3105 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
3107 mi.cbSize = sizeof(mi);
3108 mi.fMask = MIIM_DATA;
3110 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
3112 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
3113 MF_ENABLED : MF_GRAYED));
3115 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
3120 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
3122 int nStatusSize = 0;
3124 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
3125 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
3126 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
3127 int rebarHeight = 0;
3132 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
3133 if (IsWindowVisible(hwndStatusBar))
3135 GetClientRect(hwndStatusBar, &rc);
3136 nStatusSize = rc.bottom - rc.top;
3144 if(!is_bar_visible(BANDID_TOOLBAR))
3147 if(!is_bar_visible(BANDID_FORMATBAR))
3150 rebarHeight = rebarRows ? SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0) : 0;
3152 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
3156 GetClientRect(hWnd, &rc);
3157 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
3160 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
3163 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3165 if(msg == ID_FINDMSGSTRING)
3166 return handle_findmsg((LPFINDREPLACEW)lParam);
3171 return OnCreate( hWnd, wParam, lParam );
3174 return OnUser( hWnd, wParam, lParam );
3177 return OnNotify( hWnd, wParam, lParam );
3181 return preview_command( hWnd, wParam, lParam );
3183 return OnCommand( hWnd, wParam, lParam );
3193 } else if(prompt_save_changes())
3195 registry_set_options();
3196 registry_set_formatopts_all();
3203 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
3206 case WM_INITMENUPOPUP:
3207 return OnInitPopupMenu( hWnd, wParam, lParam );
3210 return OnSize( hWnd, wParam, lParam );
3212 case WM_CONTEXTMENU:
3213 if((HWND)wParam == hEditorWnd)
3214 return context_menu(lParam);
3216 return DefWindowProcW(hWnd, msg, wParam, lParam);
3220 WCHAR file[MAX_PATH];
3221 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
3222 DragFinish((HDROP)wParam);
3224 if(prompt_save_changes())
3230 return print_preview();
3232 return DefWindowProcW(hWnd, msg, wParam, lParam);
3235 return DefWindowProcW(hWnd, msg, wParam, lParam);
3241 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
3243 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
3248 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
3249 'T','A','B','L','E','\0'};
3251 InitCommonControlsEx(&classes);
3253 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
3255 wc.style = CS_HREDRAW | CS_VREDRAW;
3256 wc.lpfnWndProc = WndProc;
3259 wc.hInstance = hInstance;
3260 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
3261 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
3262 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3263 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
3264 wc.lpszClassName = wszMainWndClass;
3265 RegisterClassW(&wc);
3267 rc = registry_read_winrect();
3268 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
3269 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
3270 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
3274 set_fileformat(SF_RTF);
3275 hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
3276 get_default_printer_opts();
3279 HandleCommandLine(GetCommandLineW());
3281 while(GetMessageW(&msg,0,0,0))
3283 if (IsDialogMessage(hFindWnd, &msg))
3286 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
3288 TranslateMessage(&msg);
3289 DispatchMessageW(&msg);
3290 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
3291 SendMessageW(hMainWnd, WM_USER, 0, 0);