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
44 static const WCHAR xszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
45 static const WCHAR xszMainMenu[] = {'M','A','I','N','M','E','N','U',0};
47 static const WCHAR wszRichEditClass[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
48 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
49 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
51 static const WCHAR key_recentfiles[] = {'R','e','c','e','n','t',' ','f','i','l','e',
52 ' ','l','i','s','t',0};
53 static const WCHAR key_options[] = {'O','p','t','i','o','n','s',0};
54 static const WCHAR key_rtf[] = {'R','T','F',0};
55 static const WCHAR key_text[] = {'T','e','x','t',0};
57 static const WCHAR var_file[] = {'F','i','l','e','%','d',0};
58 static const WCHAR var_framerect[] = {'F','r','a','m','e','R','e','c','t',0};
59 static const WCHAR var_barstate0[] = {'B','a','r','S','t','a','t','e','0',0};
60 static const WCHAR var_pagemargin[] = {'P','a','g','e','M','a','r','g','i','n',0};
62 static const WCHAR stringFormat[] = {'%','2','d','\0'};
65 static HWND hEditorWnd;
67 static HMENU hPopupMenu;
69 static UINT ID_FINDMSGSTRING;
71 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
72 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
73 static WCHAR wszSaveChanges[MAX_STRING_LEN];
74 static WCHAR wszPrintFilter[MAX_STRING_LEN*2+6+4+1];
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'};
88 static const WCHAR files_prn[] = {'*','.','P','R','N',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;
110 LoadStringW(hInstance, STRING_PRINTER_FILES_PRN, p, MAX_STRING_LEN);
111 p += lstrlenW(p) + 1;
112 lstrcpyW(p, files_prn);
113 p += lstrlenW(p) + 1;
114 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
115 p += lstrlenW(p) + 1;
116 lstrcpyW(p, files_all);
117 p += lstrlenW(p) + 1;
120 p = wszDefaultFileName;
121 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
124 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
126 LoadStringA(hInstance, STRING_UNITS_CM, units_cmA, MAX_STRING_LEN);
127 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
130 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
134 ZeroMemory(&button, sizeof(button));
135 button.iBitmap = nImage;
136 button.idCommand = nCommand;
137 button.fsState = TBSTATE_ENABLED;
138 button.fsStyle = TBSTYLE_BUTTON;
141 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
144 static void AddTextButton(HWND hWnd, int string, int command, int id)
147 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
148 static const WCHAR button[] = {'B','U','T','T','O','N',0};
149 WCHAR text[MAX_STRING_LEN];
153 LoadStringW(hInstance, string, text, MAX_STRING_LEN);
154 hButton = CreateWindowW(button, text,
155 WS_VISIBLE | WS_CHILD, 5, 5, 100, 15,
156 hMainWnd, (HMENU)command, hInstance, NULL);
158 rb.cbSize = sizeof(rb);
159 rb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_CHILD | RBBIM_IDEALSIZE | RBBIM_ID;
160 rb.fStyle = RBBS_NOGRIPPER | RBBS_VARIABLEHEIGHT;
161 rb.hwndChild = hButton;
162 rb.cyChild = rb.cyMinChild = 22;
163 rb.cx = rb.cxMinChild = 90;
170 SendMessageW(hWnd, RB_INSERTBAND, -1, (LPARAM)&rb);
171 SetWindowPos(hButton, 0, 0, 0, 90, 22, SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
174 static void AddSeparator(HWND hwndToolBar)
178 ZeroMemory(&button, sizeof(button));
180 button.idCommand = 0;
182 button.fsStyle = TBSTYLE_SEP;
185 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
188 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
190 HANDLE hFile = (HANDLE)cookie;
193 if(!ReadFile(hFile, buffer, cb, &read, 0))
201 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
205 HANDLE hFile = (HANDLE)cookie;
207 ret = WriteFile(hFile, buffer, cb, &written, 0);
209 if(!ret || (cb != written))
218 static LPWSTR file_basename(LPWSTR path)
220 LPWSTR pos = path + lstrlenW(path);
224 if(*pos == '\\' || *pos == '/')
234 static WCHAR wszFileName[MAX_PATH];
235 static WPARAM fileFormat = SF_RTF;
237 static void set_caption(LPCWSTR wszNewFileName)
239 static const WCHAR wszSeparator[] = {' ','-',' '};
244 wszNewFileName = wszDefaultFileName;
246 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
248 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
249 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
254 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
255 length += lstrlenW(wszNewFileName);
256 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
257 length += sizeof(wszSeparator) / sizeof(WCHAR);
258 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
260 SetWindowTextW(hMainWnd, wszCaption);
262 HeapFree(GetProcessHeap(), 0, wszCaption);
265 static LRESULT registry_get_handle(HKEY *hKey, LPDWORD action, LPCWSTR subKey)
268 static const WCHAR wszProgramKey[] = {'S','o','f','t','w','a','r','e','\\',
269 'M','i','c','r','o','s','o','f','t','\\',
270 'W','i','n','d','o','w','s','\\',
271 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
272 'A','p','p','l','e','t','s','\\',
273 'W','o','r','d','p','a','d',0};
274 LPWSTR key = (LPWSTR)wszProgramKey;
278 WCHAR backslash[] = {'\\',0};
279 key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
280 (lstrlenW(wszProgramKey)+lstrlenW(subKey)+lstrlenW(backslash)+1)
286 lstrcpyW(key, wszProgramKey);
287 lstrcatW(key, backslash);
288 lstrcatW(key, subKey);
293 ret = RegCreateKeyExW(HKEY_CURRENT_USER, key, 0, NULL, REG_OPTION_NON_VOLATILE,
294 KEY_READ | KEY_WRITE, NULL, hKey, action);
297 ret = RegOpenKeyExW(HKEY_CURRENT_USER, key, 0, KEY_READ | KEY_WRITE, hKey);
301 HeapFree(GetProcessHeap(), 0, key);
308 static void registry_set_options(void)
313 if(registry_get_handle(&hKey, &action, (LPWSTR)key_options) == ERROR_SUCCESS)
317 GetWindowRect(hMainWnd, &rc);
319 RegSetValueExW(hKey, var_framerect, 0, REG_BINARY, (LPBYTE)&rc, sizeof(RECT));
321 RegSetValueExW(hKey, var_pagemargin, 0, REG_BINARY, (LPBYTE)&margins, sizeof(RECT));
325 static RECT registry_read_winrect(void)
329 DWORD size = sizeof(RECT);
331 ZeroMemory(&rc, sizeof(RECT));
332 if(registry_get_handle(&hKey, 0, (LPWSTR)key_options) != ERROR_SUCCESS ||
333 RegQueryValueExW(hKey, var_framerect, 0, NULL, (LPBYTE)&rc, &size) !=
334 ERROR_SUCCESS || size != sizeof(RECT))
346 static void truncate_path(LPWSTR file, LPWSTR out, LPWSTR pos1, LPWSTR pos2)
348 static const WCHAR dots[] = {'.','.','.',0};
357 static void format_filelist_filename(LPWSTR file, LPWSTR out)
360 LPWSTR truncpos1, truncpos2;
361 WCHAR myDocs[MAX_STRING_LEN];
363 SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, (LPWSTR)&myDocs);
364 pos_basename = file_basename(file);
368 *(pos_basename-1) = 0;
369 if(!lstrcmpiW(file, myDocs) || (lstrlenW(pos_basename) > FILELIST_ENTRY_LENGTH))
371 truncpos1 = pos_basename;
372 *(pos_basename-1) = '\\';
376 BOOL morespace = FALSE;
378 *(pos_basename-1) = '\\';
380 for(pos = file; pos < pos_basename; pos++)
382 if(*pos == '\\' || *pos == '/')
386 if((pos - file + lstrlenW(pos_basename)) > FILELIST_ENTRY_LENGTH)
394 if((pos - file + lstrlenW(pos_basename)) > FILELIST_ENTRY_LENGTH)
403 for(pos = pos_basename; pos >= truncpos1; pos--)
405 if(*pos == '\\' || *pos == '/')
407 if((truncpos1 - file + lstrlenW(pos_basename) + pos_basename - pos) > FILELIST_ENTRY_LENGTH)
416 if(truncpos1 == pos_basename)
417 lstrcatW(out, pos_basename);
418 else if(truncpos1 == truncpos2 || !truncpos2)
421 truncate_path(file, out, truncpos1, truncpos2 ? truncpos2 : (pos_basename-1));
424 static void registry_read_filelist(HWND hMainWnd)
428 if(registry_get_handle(&hFileKey, 0, key_recentfiles) == ERROR_SUCCESS)
430 WCHAR itemText[MAX_PATH+3], buffer[MAX_PATH];
431 /* The menu item name is not the same as the file name, so we need to store
432 the file name here */
433 static WCHAR file1[MAX_PATH], file2[MAX_PATH], file3[MAX_PATH], file4[MAX_PATH];
434 WCHAR numFormat[] = {'&','%','d',' ',0};
435 LPWSTR pFile[] = {file1, file2, file3, file4};
436 DWORD pathSize = MAX_PATH*sizeof(WCHAR);
440 HMENU hMenu = GetMenu(hMainWnd);
442 mi.cbSize = sizeof(MENUITEMINFOW);
443 mi.fMask = MIIM_ID | MIIM_DATA | MIIM_STRING | MIIM_FTYPE;
444 mi.fType = MFT_STRING;
445 mi.dwTypeData = itemText;
446 mi.wID = ID_FILE_RECENT1;
448 RemoveMenu(hMenu, ID_FILE_RECENT_SEPARATOR, MF_BYCOMMAND);
449 for(i = 0; i < FILELIST_ENTRIES; i++)
451 wsprintfW(key, var_file, i+1);
452 RemoveMenu(hMenu, ID_FILE_RECENT1+i, MF_BYCOMMAND);
453 if(RegQueryValueExW(hFileKey, (LPWSTR)key, 0, NULL, (LPBYTE)pFile[i], &pathSize)
457 mi.dwItemData = (DWORD)pFile[i];
458 wsprintfW(itemText, numFormat, i+1);
460 lstrcpyW(buffer, pFile[i]);
462 format_filelist_filename(buffer, itemText);
464 InsertMenuItemW(hMenu, ID_FILE_EXIT, FALSE, &mi);
466 pathSize = MAX_PATH*sizeof(WCHAR);
468 mi.fType = MFT_SEPARATOR;
469 mi.fMask = MIIM_FTYPE | MIIM_ID;
470 InsertMenuItemW(hMenu, ID_FILE_EXIT, FALSE, &mi);
472 RegCloseKey(hFileKey);
476 static void registry_set_filelist(LPCWSTR newFile)
481 if(registry_get_handle(&hKey, &action, key_recentfiles) == ERROR_SUCCESS)
483 LPCWSTR pFiles[FILELIST_ENTRIES];
485 HMENU hMenu = GetMenu(hMainWnd);
489 mi.cbSize = sizeof(MENUITEMINFOW);
490 mi.fMask = MIIM_DATA;
492 for(i = 0; i < FILELIST_ENTRIES; i++)
495 for(i = 0; GetMenuItemInfoW(hMenu, ID_FILE_RECENT1+i, FALSE, &mi); i++)
496 pFiles[i] = (LPWSTR)mi.dwItemData;
498 if(lstrcmpiW(newFile, pFiles[0]))
500 for(i = 0; pFiles[i] && i < FILELIST_ENTRIES; i++)
502 if(!lstrcmpiW(pFiles[i], newFile))
505 for(j = 0; pFiles[j] && j < i; j++)
507 pFiles[i-j] = pFiles[i-j-1];
519 for(i = 0; pFiles[i] && i < FILELIST_ENTRIES-1; i++)
520 pFiles[FILELIST_ENTRIES-1-i] = pFiles[FILELIST_ENTRIES-2-i];
525 for(i = 0; pFiles[i] && i < FILELIST_ENTRIES; i++)
527 wsprintfW(buffer, var_file, i+1);
528 RegSetValueExW(hKey, (LPWSTR)&buffer, 0, REG_SZ, (LPBYTE)pFiles[i],
529 (lstrlenW(pFiles[i])+1)*sizeof(WCHAR));
534 registry_read_filelist(hMainWnd);
537 static BOOL validate_endptr(LPCSTR endptr, BOOL units)
539 if(!endptr || !*endptr)
542 while(*endptr == ' ')
546 return *endptr != '\0';
548 /* FIXME: Allow other units and convert between them */
549 if(!lstrcmpA(endptr, units_cmA))
552 return *endptr != '\0';
555 static BOOL number_from_string(LPCWSTR string, float *num, BOOL units)
558 char buffer[MAX_STRING_LEN];
559 char *endptr = buffer;
561 WideCharToMultiByte(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN, NULL, NULL);
564 ret = strtod(buffer, &endptr);
566 if((ret == 0 && errno != 0) || endptr == buffer || validate_endptr(endptr, units))
576 static void set_size(float size)
580 ZeroMemory(&fmt, sizeof(fmt));
581 fmt.cbSize = sizeof(fmt);
582 fmt.dwMask = CFM_SIZE;
583 fmt.yHeight = (int)(size * 20.0);
584 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
587 static void add_size(HWND hSizeListWnd, unsigned size)
590 COMBOBOXEXITEMW cbItem;
591 cbItem.mask = CBEIF_TEXT;
594 wsprintfW(buffer, stringFormat, size);
595 cbItem.pszText = (LPWSTR)buffer;
596 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
599 static void populate_size_list(HWND hSizeListWnd)
601 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
602 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
603 COMBOBOXEXITEMW cbFontItem;
605 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
606 HDC hdc = GetDC(hMainWnd);
607 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
612 ZeroMemory(&fmt, sizeof(fmt));
613 fmt.cbSize = sizeof(fmt);
614 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
616 cbFontItem.mask = CBEIF_LPARAM;
617 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
618 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
620 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
622 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
624 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
626 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
627 GetDeviceCaps(hdc, LOGPIXELSY)));
630 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
631 add_size(hSizeListWnd, choices[i]);
634 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
635 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
638 static void update_size_list(void)
640 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
641 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
642 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
643 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
646 ZeroMemory(&fmt, sizeof(fmt));
647 fmt.cbSize = sizeof(fmt);
649 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
651 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
652 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
654 if(lstrcmpW(fontSize, sizeBuffer))
655 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
658 static void update_font_list(void)
660 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
661 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
662 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
663 WCHAR fontName[MAX_STRING_LEN];
666 ZeroMemory(&fmt, sizeof(fmt));
667 fmt.cbSize = sizeof(fmt);
669 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
670 SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName);
672 if(lstrcmpW(fontName, fmt.szFaceName))
674 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
675 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
682 static void clear_formatting(void)
686 pf.cbSize = sizeof(pf);
687 pf.dwMask = PFM_ALIGNMENT;
688 pf.wAlignment = PFA_LEFT;
689 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
692 static int reg_formatindex(WPARAM format)
694 return (format & SF_TEXT) ? 1 : 0;
697 static int fileformat_number(WPARAM format)
701 if(format == SF_TEXT)
704 } else if (format == (SF_TEXT | SF_UNICODE))
711 static WPARAM fileformat_flags(int format)
713 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
715 return flags[format];
718 static void set_font(LPCWSTR wszFaceName)
720 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
721 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
722 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
723 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
726 ZeroMemory(&fmt, sizeof(fmt));
728 fmt.cbSize = sizeof(fmt);
729 fmt.dwMask = CFM_FACE;
731 lstrcpyW(fmt.szFaceName, wszFaceName);
733 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
735 populate_size_list(hSizeListWnd);
737 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)(LPWSTR)wszFaceName);
740 static void set_default_font(void)
742 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
743 'R','o','m','a','n',0};
744 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
748 ZeroMemory(&fmt, sizeof(fmt));
750 fmt.cbSize = sizeof(fmt);
751 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
754 if(fileFormat & SF_RTF)
757 font = plainTextFont;
759 lstrcpyW(fmt.szFaceName, font);
761 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
764 static void add_font(LPWSTR fontName, DWORD fontType, HWND hListWnd, NEWTEXTMETRICEXW *ntmc)
766 COMBOBOXEXITEMW cbItem;
767 WCHAR buffer[MAX_PATH];
770 cbItem.mask = CBEIF_TEXT;
771 cbItem.pszText = buffer;
772 cbItem.cchTextMax = MAX_STRING_LEN;
775 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
777 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
782 cbItem.pszText = fontName;
784 cbItem.mask |= CBEIF_LPARAM;
785 if(fontType & RASTER_FONTTYPE)
786 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
788 cbItem.lParam = MAKELONG(fontType,fontHeight);
789 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
792 static void dialog_choose_font(void)
797 HDC hDC = GetDC(hMainWnd);
799 ZeroMemory(&cf, sizeof(cf));
800 cf.lStructSize = sizeof(cf);
801 cf.hwndOwner = hMainWnd;
803 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT;
805 ZeroMemory(&fmt, sizeof(fmt));
806 fmt.cbSize = sizeof(fmt);
808 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
809 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
810 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) ? TRUE : FALSE;
811 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
812 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
816 ZeroMemory(&fmt, sizeof(fmt));
817 fmt.cbSize = sizeof(fmt);
818 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE;
819 fmt.yHeight = cf.iPointSize * 2;
821 if(cf.nFontType & BOLD_FONTTYPE)
822 fmt.dwEffects |= CFE_BOLD;
823 if(cf.nFontType & ITALIC_FONTTYPE)
824 fmt.dwEffects |= CFE_ITALIC;
826 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
827 set_font(cf.lpLogFont->lfFaceName);
832 int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
833 DWORD FontType, LPARAM lParam)
835 HWND hListWnd = (HWND) lParam;
837 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
840 add_font((LPWSTR)lpelfe->lfFaceName, FontType, hListWnd, (NEWTEXTMETRICEXW*)lpntme);
846 static void populate_font_list(HWND hListWnd)
848 HDC hdc = GetDC(hMainWnd);
850 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
853 fontinfo.lfCharSet = DEFAULT_CHARSET;
854 *fontinfo.lfFaceName = '\0';
855 fontinfo.lfPitchAndFamily = 0;
857 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
858 (LPARAM)hListWnd, 0);
860 ZeroMemory(&fmt, sizeof(fmt));
861 fmt.cbSize = sizeof(fmt);
862 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
863 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
866 static void update_window(void)
870 GetWindowRect(hMainWnd, &rect);
872 (void) OnSize(hMainWnd, SIZE_RESTORED, MAKELONG(rect.bottom, rect.right));
875 static DWORD barState[2];
876 static DWORD wordWrap[2];
878 static BOOL is_bar_visible(int bandId)
880 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
883 static void store_bar_state(int bandId, BOOL show)
885 int formatIndex = reg_formatindex(fileFormat);
888 barState[formatIndex] |= (1 << bandId);
890 barState[formatIndex] &= ~(1 << bandId);
893 static void set_toolbar_state(int bandId, BOOL show)
895 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
897 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
899 if(bandId == BANDID_TOOLBAR)
901 REBARBANDINFOW rbbinfo;
902 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
904 rbbinfo.cbSize = sizeof(rbbinfo);
905 rbbinfo.fMask = RBBIM_STYLE;
907 SendMessageW(hwndReBar, RB_GETBANDINFO, index, (LPARAM)&rbbinfo);
910 rbbinfo.fStyle &= ~RBBS_BREAK;
912 rbbinfo.fStyle |= RBBS_BREAK;
914 SendMessageW(hwndReBar, RB_SETBANDINFO, index, (LPARAM)&rbbinfo);
917 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR)
918 store_bar_state(bandId, show);
921 static void set_statusbar_state(BOOL show)
923 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
925 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
926 store_bar_state(BANDID_STATUSBAR, show);
929 static void set_bar_states(void)
931 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
932 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
933 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
934 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
935 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
940 static HGLOBAL devMode;
941 static HGLOBAL devNames;
943 static HDC make_dc(void)
945 if(devNames && devMode)
947 LPDEVNAMES dn = GlobalLock(devNames);
948 LPDEVMODEW dm = GlobalLock(devMode);
951 ret = CreateDCW((LPWSTR)dn + dn->wDriverOffset,
952 (LPWSTR)dn + dn->wDeviceOffset, 0, dm);
964 static LONG twips_to_pixels(int twips, int dpi)
966 float ret = ((float)twips / ((float)567 * 2.54)) * (float)dpi;
970 static LONG devunits_to_twips(int units, int dpi)
972 float ret = ((float)units / (float)dpi) * (float)567 * 2.54;
976 static LONG centmm_to_twips(int mm)
978 return MulDiv(mm, 567, 1000);
981 static LONG twips_to_centmm(int twips)
983 return MulDiv(twips, 1000, 567);
986 static RECT get_print_rect(HDC hdc)
993 int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
994 int dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
995 width = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALWIDTH), dpiX);
996 height = devunits_to_twips(GetDeviceCaps(hdc, PHYSICALHEIGHT), dpiY);
999 width = centmm_to_twips(18500);
1000 height = centmm_to_twips(27000);
1003 rc.left = margins.left;
1004 rc.right = width - margins.right;
1005 rc.top = margins.top;
1006 rc.bottom = height - margins.bottom;
1011 static void target_device(void)
1013 HDC hdc = make_dc();
1015 int index = reg_formatindex(fileFormat);
1017 if(wordWrap[index] == ID_WORDWRAP_MARGIN)
1019 RECT rc = get_print_rect(hdc);
1025 HDC hMaindc = GetDC(hMainWnd);
1026 hdc = CreateCompatibleDC(hMaindc);
1027 ReleaseDC(hMainWnd, hMaindc);
1030 SendMessageW(hEditorWnd, EM_SETTARGETDEVICE, (WPARAM)hdc, width);
1035 static void set_fileformat(WPARAM format)
1038 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1039 fileFormat = format;
1041 if(format & SF_TEXT)
1042 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_TXT));
1044 hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_RTF));
1046 SendMessageW(hMainWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
1053 static void DoOpenFile(LPCWSTR szOpenFileName)
1059 WPARAM format = SF_TEXT;
1061 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
1062 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1063 if (hFile == INVALID_HANDLE_VALUE)
1066 ReadFile(hFile, fileStart, 5, &readOut, NULL);
1067 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1069 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
1071 format = SF_TEXT | SF_UNICODE;
1072 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
1073 } else if(readOut >= 5)
1075 static const char header[] = "{\\rtf";
1076 if(!memcmp(header, fileStart, 5))
1080 es.dwCookie = (DWORD_PTR)hFile;
1081 es.pfnCallback = stream_in;
1084 set_fileformat(format);
1085 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
1089 SetFocus(hEditorWnd);
1091 set_caption(szOpenFileName);
1093 lstrcpyW(wszFileName, szOpenFileName);
1094 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1095 registry_set_filelist(szOpenFileName);
1099 static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
1105 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
1106 FILE_ATTRIBUTE_NORMAL, NULL);
1108 if(hFile == INVALID_HANDLE_VALUE)
1111 if(format == (SF_TEXT | SF_UNICODE))
1113 static const BYTE unicode[] = {0xff,0xfe};
1115 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
1117 if(writeOut != sizeof(unicode))
1121 stream.dwCookie = (DWORD_PTR)hFile;
1122 stream.pfnCallback = stream_out;
1124 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
1128 SetFocus(hEditorWnd);
1133 gt.flags = GTL_DEFAULT;
1136 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0))
1140 lstrcpyW(wszFileName, wszSaveFileName);
1141 set_caption(wszFileName);
1142 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1143 set_fileformat(format);
1146 static void DialogSaveFile(void)
1150 WCHAR wszFile[MAX_PATH] = {'\0'};
1151 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
1153 ZeroMemory(&sfn, sizeof(sfn));
1155 sfn.lStructSize = sizeof(sfn);
1156 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
1157 sfn.hwndOwner = hMainWnd;
1158 sfn.lpstrFilter = wszFilter;
1159 sfn.lpstrFile = wszFile;
1160 sfn.nMaxFile = MAX_PATH;
1161 sfn.lpstrDefExt = wszDefExt;
1162 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
1164 while(GetSaveFileNameW(&sfn))
1166 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
1168 if(MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
1169 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
1174 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
1179 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
1185 static BOOL prompt_save_changes(void)
1190 gt.flags = GTL_NUMCHARS;
1192 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0))
1196 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
1201 LPWSTR displayFileName;
1206 displayFileName = wszDefaultFileName;
1208 displayFileName = file_basename(wszFileName);
1210 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1211 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
1216 wsprintfW(text, wszSaveChanges, displayFileName);
1218 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
1220 HeapFree(GetProcessHeap(), 0, text);
1229 DoSaveFile(wszFileName, fileFormat);
1240 static void DialogOpenFile(void)
1244 WCHAR wszFile[MAX_PATH] = {'\0'};
1245 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
1247 ZeroMemory(&ofn, sizeof(ofn));
1249 ofn.lStructSize = sizeof(ofn);
1250 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
1251 ofn.hwndOwner = hMainWnd;
1252 ofn.lpstrFilter = wszFilter;
1253 ofn.lpstrFile = wszFile;
1254 ofn.nMaxFile = MAX_PATH;
1255 ofn.lpstrDefExt = wszDefExt;
1256 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
1258 if(GetOpenFileNameW(&ofn))
1260 if(prompt_save_changes())
1261 DoOpenFile(ofn.lpstrFile);
1265 static LPWSTR dialog_print_to_file(void)
1268 static WCHAR file[MAX_PATH] = {'O','U','T','P','U','T','.','P','R','N',0};
1269 static const WCHAR defExt[] = {'P','R','N',0};
1271 ZeroMemory(&ofn, sizeof(ofn));
1273 ofn.lStructSize = sizeof(ofn);
1274 ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
1275 ofn.hwndOwner = hMainWnd;
1276 ofn.lpstrFilter = (LPWSTR)wszPrintFilter;
1277 ofn.lpstrFile = (LPWSTR)file;
1278 ofn.nMaxFile = MAX_PATH;
1279 ofn.lpstrDefExt = (LPWSTR)defExt;
1281 if(GetSaveFileNameW(&ofn))
1282 return (LPWSTR)file;
1287 static int get_num_pages(FORMATRANGE fr)
1294 fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE,
1296 } while(fr.chrg.cpMin < fr.chrg.cpMax);
1301 static void char_from_pagenum(FORMATRANGE *fr, int page)
1305 for(i = 1; i <= page; i++)
1310 fr->chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)fr);
1314 static void print(LPPRINTDLGW pd)
1318 int printedPages = 0;
1321 fr.hdcTarget = pd->hDC;
1323 fr.rc = get_print_rect(fr.hdc);
1325 fr.rcPage.right = fr.rc.right + margins.right;
1327 fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
1329 ZeroMemory(&di, sizeof(di));
1330 di.cbSize = sizeof(di);
1331 di.lpszDocName = (LPWSTR)wszFileName;
1333 if(pd->Flags & PD_PRINTTOFILE)
1335 di.lpszOutput = dialog_print_to_file();
1340 if(pd->Flags & PD_SELECTION)
1342 SendMessageW(hEditorWnd, EM_EXGETSEL, 0, (LPARAM)&fr.chrg);
1346 gt.flags = GTL_DEFAULT;
1349 fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
1351 if(pd->Flags & PD_PAGENUMS)
1352 char_from_pagenum(&fr, pd->nToPage);
1355 StartDocW(fr.hdc, &di);
1358 if(StartPage(fr.hdc) <= 0)
1361 fr.chrg.cpMin = SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
1363 if(EndPage(fr.hdc) <= 0)
1367 if((pd->Flags & PD_PAGENUMS) && (printedPages > (pd->nToPage - pd->nFromPage)))
1370 while(fr.chrg.cpMin < fr.chrg.cpMax);
1373 SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
1377 static void dialog_printsetup(void)
1381 ZeroMemory(&ps, sizeof(ps));
1382 ps.lStructSize = sizeof(ps);
1383 ps.hwndOwner = hMainWnd;
1384 ps.Flags = PSD_INHUNDREDTHSOFMILLIMETERS | PSD_MARGINS;
1385 ps.rtMargin.left = twips_to_centmm(margins.left);
1386 ps.rtMargin.right = twips_to_centmm(margins.right);
1387 ps.rtMargin.top = twips_to_centmm(margins.top);
1388 ps.rtMargin.bottom = twips_to_centmm(margins.bottom);
1389 ps.hDevMode = devMode;
1390 ps.hDevNames = devNames;
1392 if(PageSetupDlgW(&ps))
1394 margins.left = centmm_to_twips(ps.rtMargin.left);
1395 margins.right = centmm_to_twips(ps.rtMargin.right);
1396 margins.top = centmm_to_twips(ps.rtMargin.top);
1397 margins.bottom = centmm_to_twips(ps.rtMargin.bottom);
1398 devMode = ps.hDevMode;
1399 devNames = ps.hDevNames;
1404 static void get_default_printer_opts(void)
1407 ZeroMemory(&pd, sizeof(pd));
1409 ZeroMemory(&pd, sizeof(pd));
1410 pd.lStructSize = sizeof(pd);
1411 pd.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
1412 pd.hwndOwner = hMainWnd;
1413 pd.hDevMode = devMode;
1417 devMode = pd.hDevMode;
1418 devNames = pd.hDevNames;
1421 static void print_quick(void)
1425 ZeroMemory(&pd, sizeof(pd));
1431 static void dialog_print(void)
1437 ZeroMemory(&pd, sizeof(pd));
1438 pd.lStructSize = sizeof(pd);
1439 pd.hwndOwner = hMainWnd;
1440 pd.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE;
1443 pd.hDevMode = devMode;
1444 pd.hDevNames = devNames;
1446 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1448 pd.Flags |= PD_NOSELECTION;
1452 devMode = pd.hDevMode;
1453 devNames = pd.hDevNames;
1458 typedef struct _previewinfo
1465 } previewinfo, *ppreviewinfo;
1467 static previewinfo preview;
1469 static void preview_bar_show(BOOL show)
1471 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
1476 AddTextButton(hReBar, STRING_PREVIEW_PRINT, ID_PRINT, BANDID_PREVIEW_BTN1);
1477 AddTextButton(hReBar, STRING_PREVIEW_NEXTPAGE, ID_PREVIEW_NEXTPAGE, BANDID_PREVIEW_BTN2);
1478 AddTextButton(hReBar, STRING_PREVIEW_PREVPAGE, ID_PREVIEW_PREVPAGE, BANDID_PREVIEW_BTN3);
1479 AddTextButton(hReBar, STRING_PREVIEW_CLOSE, ID_FILE_EXIT, BANDID_PREVIEW_BTN4);
1482 for(i = 0; i < PREVIEW_BUTTONS; i++)
1483 SendMessageW(hReBar, RB_DELETEBAND, SendMessageW(hReBar, RB_IDTOINDEX, BANDID_PREVIEW_BTN1+i, 0), 0);
1487 static void preview_exit(void)
1489 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1490 HMENU hMenu = LoadMenuW(hInstance, xszMainMenu);
1493 preview.window.right = 0;
1494 preview.window.bottom = 0;
1497 ShowWindow(hEditorWnd, TRUE);
1499 preview_bar_show(FALSE);
1501 SetMenu(hMainWnd, hMenu);
1502 registry_read_filelist(hMainWnd);
1507 static LRESULT print_preview(void)
1512 RECT window, background;
1513 HBITMAP hBitmapCapture, hBitmapScaled;
1514 int bmWidth, bmHeight, bmNewWidth, bmNewHeight;
1515 float ratioWidth, ratioHeight, ratio;
1516 int xOffset, yOffset;
1518 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
1521 hdc = BeginPaint(hMainWnd, &ps);
1522 GetClientRect(hMainWnd, &window);
1524 fr.hdcTarget = make_dc();
1525 fr.rc = get_print_rect(fr.hdcTarget);
1528 fr.rcPage.bottom = fr.rc.bottom + margins.bottom;
1529 fr.rcPage.right = fr.rc.right + margins.right;
1531 bmWidth = twips_to_pixels(fr.rcPage.right, GetDeviceCaps(hdc, LOGPIXELSX));
1532 bmHeight = twips_to_pixels(fr.rcPage.bottom, GetDeviceCaps(hdc, LOGPIXELSY));
1534 hBitmapCapture = CreateCompatibleBitmap(hdc, bmWidth, bmHeight);
1540 preview.hdc = CreateCompatibleDC(hdc);
1541 fr.hdc = preview.hdc;
1542 gt.flags = GTL_DEFAULT;
1545 fr.chrg.cpMax = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
1548 paper.right = bmWidth;
1550 paper.bottom = bmHeight;
1553 preview.pages = get_num_pages(fr);
1555 SelectObject(preview.hdc, hBitmapCapture);
1557 char_from_pagenum(&fr, preview.page);
1559 FillRect(preview.hdc, &paper, GetStockObject(WHITE_BRUSH));
1560 SendMessageW(hEditorWnd, EM_FORMATRANGE, TRUE, (LPARAM)&fr);
1561 SendMessageW(hEditorWnd, EM_FORMATRANGE, FALSE, 0);
1563 EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_PREVPAGE), preview.page > 1);
1564 EnableWindow(GetDlgItem(hReBar, ID_PREVIEW_NEXTPAGE), preview.page < preview.pages);
1567 barheight = SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
1568 ratioWidth = ((float)window.right - 20.0) / (float)bmHeight;
1569 ratioHeight = ((float)window.bottom - 20.0 - (float)barheight) / (float)bmHeight;
1571 if(ratioWidth > ratioHeight)
1572 ratio = ratioHeight;
1576 bmNewWidth = (int)((float)bmWidth * ratio);
1577 bmNewHeight = (int)((float)bmHeight * ratio);
1578 hBitmapScaled = CreateCompatibleBitmap(hdc, bmNewWidth, bmNewHeight);
1580 xOffset = ((window.right - bmNewWidth) / 2);
1581 yOffset = ((window.bottom - bmNewHeight + barheight) / 2);
1583 if(window.right != preview.window.right || window.bottom != preview.window.bottom)
1585 DeleteDC(preview.hdcSized),
1586 preview.hdcSized = CreateCompatibleDC(hdc);
1587 SelectObject(preview.hdcSized, hBitmapScaled);
1589 StretchBlt(preview.hdcSized, 0, 0, bmNewWidth, bmNewHeight, preview.hdc, 0, 0, bmWidth, bmHeight, SRCCOPY);
1592 window.top = barheight;
1593 FillRect(hdc, &window, GetStockObject(GRAY_BRUSH));
1595 SelectObject(hdc, hBitmapScaled);
1597 background.left = xOffset - 2;
1598 background.right = xOffset + bmNewWidth + 2;
1599 background.top = yOffset - 2;
1600 background.bottom = yOffset + bmNewHeight + 2;
1602 FillRect(hdc, &background, GetStockObject(BLACK_BRUSH));
1604 BitBlt(hdc, xOffset, yOffset, bmNewWidth, bmNewHeight, preview.hdcSized, 0, 0, SRCCOPY);
1606 DeleteDC(fr.hdcTarget);
1607 preview.window = window;
1609 EndPaint(hMainWnd, &ps);
1614 static LRESULT preview_command(HWND hWnd, WPARAM wParam, LPARAM lParam)
1616 switch(LOWORD(wParam))
1619 PostMessageW(hMainWnd, WM_CLOSE, 0, 0);
1622 case ID_PREVIEW_NEXTPAGE:
1623 case ID_PREVIEW_PREVPAGE:
1625 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
1628 if(LOWORD(wParam) == ID_PREVIEW_NEXTPAGE)
1634 preview.window.right = 0;
1636 GetClientRect(hMainWnd, &rc);
1637 rc.top += SendMessageW(hReBar, RB_GETBARHEIGHT, 0, 0);
1638 InvalidateRect(hMainWnd, &rc, TRUE);
1652 static void dialog_about(void)
1654 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1655 HICON icon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
1656 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1659 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1665 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1668 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1670 sprintf(id, "%d\n", (int)ps->lParam);
1671 SetWindowTextA(hIdWnd, id);
1672 if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1673 wrap = IDC_PAGEFMT_WW;
1674 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1675 wrap = IDC_PAGEFMT_WM;
1678 CheckRadioButton(hWnd, IDC_PAGEFMT_WW,
1679 IDC_PAGEFMT_WM, wrap);
1681 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1682 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1683 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1684 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1685 if(barState[ps->lParam] & (BANDID_STATUSBAR))
1686 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1691 switch(LOWORD(wParam))
1693 case IDC_PAGEFMT_WW:
1694 case IDC_PAGEFMT_WM:
1695 CheckRadioButton(hWnd, IDC_PAGEFMT_WW, IDC_PAGEFMT_WM,
1699 case IDC_PAGEFMT_TB:
1700 case IDC_PAGEFMT_FB:
1701 case IDC_PAGEFMT_SB:
1702 CheckDlgButton(hWnd, LOWORD(wParam),
1703 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1709 LPNMHDR header = (LPNMHDR)lParam;
1710 if(header->code == PSN_APPLY)
1712 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1716 GetWindowTextA(hIdWnd, sid, 4);
1718 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1719 wordWrap[id] = ID_WORDWRAP_WINDOW;
1720 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1721 wordWrap[id] = ID_WORDWRAP_MARGIN;
1723 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1724 barState[id] |= (1 << BANDID_TOOLBAR);
1726 barState[id] &= ~(1 << BANDID_TOOLBAR);
1728 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1729 barState[id] |= (1 << BANDID_FORMATBAR);
1731 barState[id] &= ~(1 << BANDID_FORMATBAR);
1733 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1734 barState[id] |= (1 << BANDID_STATUSBAR);
1736 barState[id] &= ~(1 << BANDID_STATUSBAR);
1744 static void dialog_viewproperties(void)
1746 PROPSHEETPAGEW psp[2];
1747 PROPSHEETHEADERW psh;
1749 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
1750 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1752 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1753 psp[0].dwFlags = PSP_USETITLE;
1754 psp[0].pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1755 psp[0].pfnDlgProc = formatopts_proc;
1756 psp[0].hInstance = hInstance;
1757 psp[0].lParam = reg_formatindex(SF_TEXT);
1758 psp[0].pfnCallback = NULL;
1759 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1760 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1762 psp[i].dwSize = psp[0].dwSize;
1763 psp[i].dwFlags = psp[0].dwFlags;
1764 psp[i].pszTemplate = psp[0].pszTemplate;
1765 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1766 psp[i].hInstance = psp[0].hInstance;
1767 psp[i].lParam = reg_formatindex(SF_RTF);
1768 psp[i].pfnCallback = psp[0].pfnCallback;
1769 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1772 psh.dwSize = sizeof(psh);
1773 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1774 psh.hwndParent = hMainWnd;
1775 psh.hInstance = hInstance;
1776 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1777 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1779 psh.pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1781 if(fileFormat & SF_RTF)
1785 PropertySheetW(&psh);
1790 static void HandleCommandLine(LPWSTR cmdline)
1795 /* skip white space */
1796 while (*cmdline == ' ') cmdline++;
1798 /* skip executable name */
1799 delimiter = (*cmdline == '"' ? '"' : ' ');
1801 if (*cmdline == delimiter) cmdline++;
1802 while (*cmdline && *cmdline != delimiter) cmdline++;
1803 if (*cmdline == delimiter) cmdline++;
1805 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
1809 if (*cmdline++ == ' ') continue;
1812 if (option) cmdline++;
1813 while (*cmdline == ' ') cmdline++;
1826 /* file name is passed on the command line */
1827 if (cmdline[0] == '"')
1830 cmdline[lstrlenW(cmdline) - 1] = 0;
1832 DoOpenFile(cmdline);
1833 InvalidateRect(hMainWnd, NULL, FALSE);
1837 MessageBox(hMainWnd, "Printing not implemented", "WordPad", MB_OK);
1840 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1842 if(pFr->Flags & FR_DIALOGTERM)
1845 pFr->Flags = FR_FINDNEXT;
1847 } else if(pFr->Flags & FR_FINDNEXT)
1849 DWORD flags = FR_DOWN;
1851 static CHARRANGE cr;
1856 HMENU hMenu = GetMenu(hMainWnd);
1859 mi.cbSize = sizeof(mi);
1860 mi.fMask = MIIM_DATA;
1862 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1864 gt.flags = GTL_NUMCHARS;
1867 length = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
1869 if(pFr->lCustData == -1)
1871 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&startPos, (LPARAM)&end);
1872 cr.cpMin = startPos;
1873 pFr->lCustData = startPos;
1875 if(cr.cpMin == length)
1879 startPos = pFr->lCustData;
1882 if(cr.cpMax > length)
1890 ft.lpstrText = pFr->lpstrFindWhat;
1892 if(pFr->Flags & FR_MATCHCASE)
1893 flags |= FR_MATCHCASE;
1894 if(pFr->Flags & FR_WHOLEWORD)
1895 flags |= FR_WHOLEWORD;
1897 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1901 if(cr.cpMax == length && cr.cpMax != startPos)
1903 ft.chrg.cpMin = cr.cpMin = 0;
1904 ft.chrg.cpMax = cr.cpMax = startPos;
1906 ret = SendMessageW(hEditorWnd, EM_FINDTEXTW, (WPARAM)flags, (LPARAM)&ft);
1912 pFr->lCustData = -1;
1913 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED), wszAppTitle,
1914 MB_OK | MB_ICONASTERISK);
1917 end = ret + lstrlenW(pFr->lpstrFindWhat);
1919 SendMessageW(hEditorWnd, EM_SETSEL, (WPARAM)ret, (LPARAM)end);
1920 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1927 static void dialog_find(LPFINDREPLACEW fr)
1929 static WCHAR findBuffer[MAX_STRING_LEN];
1931 ZeroMemory(fr, sizeof(FINDREPLACEW));
1932 fr->lStructSize = sizeof(FINDREPLACEW);
1933 fr->hwndOwner = hMainWnd;
1934 fr->Flags = FR_HIDEUPDOWN;
1935 fr->lpstrFindWhat = findBuffer;
1937 fr->wFindWhatLen = MAX_STRING_LEN*sizeof(WCHAR);
1939 hFindWnd = FindTextW(fr);
1942 static void registry_read_options(void)
1945 DWORD size = sizeof(RECT);
1947 if(registry_get_handle(&hKey, 0, key_options) != ERROR_SUCCESS ||
1948 RegQueryValueExW(hKey, var_pagemargin, 0, NULL, (LPBYTE)&margins,
1949 &size) != ERROR_SUCCESS || size != sizeof(RECT))
1952 margins.bottom = 1417;
1953 margins.left = 1757;
1954 margins.right = 1757;
1960 static void registry_read_formatopts(int index, LPCWSTR key)
1964 BOOL fetched = FALSE;
1965 barState[index] = 0;
1966 wordWrap[index] = 0;
1968 if(registry_get_handle(&hKey, &action, key) != ERROR_SUCCESS)
1971 if(action == REG_OPENED_EXISTING_KEY)
1973 DWORD size = sizeof(DWORD);
1975 if(RegQueryValueExW(hKey, var_barstate0, 0, NULL, (LPBYTE)&barState[index],
1976 &size) == ERROR_SUCCESS)
1981 barState[index] = (1 << BANDID_TOOLBAR) | (1 << BANDID_FORMATBAR) | (1 << BANDID_RULER) | (1 << BANDID_STATUSBAR);
1983 if(index == reg_formatindex(SF_RTF))
1984 wordWrap[index] = ID_WORDWRAP_WINDOW;
1985 else if(index == reg_formatindex(SF_TEXT))
1986 wordWrap[index] = ID_WORDWRAP_WINDOW; /* FIXME: should be ID_WORDWRAP_NONE once we support it */
1991 static void registry_read_formatopts_all(void)
1993 registry_read_formatopts(reg_formatindex(SF_RTF), key_rtf);
1994 registry_read_formatopts(reg_formatindex(SF_TEXT), key_text);
1997 static void registry_set_formatopts(int index, LPCWSTR key)
2002 if(registry_get_handle(&hKey, &action, key) == ERROR_SUCCESS)
2004 RegSetValueExW(hKey, var_barstate0, 0, REG_DWORD, (LPBYTE)&barState[index],
2011 static void registry_set_formatopts_all(void)
2013 registry_set_formatopts(reg_formatindex(SF_RTF), key_rtf);
2014 registry_set_formatopts(reg_formatindex(SF_TEXT), key_text);
2017 static int current_units_to_twips(float number)
2019 int twips = (int)(number * 567);
2023 static void append_current_units(LPWSTR buffer)
2025 static const WCHAR space[] = {' '};
2026 lstrcatW(buffer, space);
2027 lstrcatW(buffer, units_cmW);
2030 static void number_with_units(LPWSTR buffer, int number)
2032 float converted = (float)number / 567;
2033 char string[MAX_STRING_LEN];
2035 sprintf(string, "%.2f ", converted);
2036 lstrcatA(string, units_cmA);
2037 MultiByteToWideChar(CP_ACP, 0, string, -1, buffer, MAX_STRING_LEN);
2040 BOOL CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2046 WCHAR buffer[MAX_STRING_LEN];
2048 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
2051 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
2053 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2054 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
2056 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2057 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
2058 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2060 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
2065 switch(LOWORD(wParam))
2070 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
2072 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
2076 WCHAR buffer[MAX_STRING_LEN];
2077 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
2078 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
2084 EndDialog(hWnd, wParam);
2091 BOOL CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2097 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
2098 WCHAR buffer[MAX_STRING_LEN];
2099 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
2101 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, (LPWSTR)buffer, MAX_STRING_LEN);
2102 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2103 LoadStringW(hInstance, STRING_NEWFILE_TXT, (LPWSTR)buffer, MAX_STRING_LEN);
2104 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2105 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, (LPWSTR)buffer, MAX_STRING_LEN);
2106 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
2108 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
2113 switch(LOWORD(wParam))
2118 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
2119 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
2122 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
2127 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
2134 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2140 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd,
2142 WCHAR buffer[MAX_STRING_LEN];
2143 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
2144 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
2145 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
2146 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
2150 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
2152 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
2153 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
2155 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
2156 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
2158 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
2160 pf.cbSize = sizeof(pf);
2161 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
2163 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2165 if(pf.wAlignment == PFA_RIGHT)
2167 else if(pf.wAlignment == PFA_CENTER)
2170 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
2172 number_with_units(buffer, pf.dxOffset);
2173 SetWindowTextW(hLeftWnd, buffer);
2174 number_with_units(buffer, pf.dxRightIndent);
2175 SetWindowTextW(hRightWnd, buffer);
2176 number_with_units(buffer, pf.dxStartIndent - pf.dxOffset);
2177 SetWindowTextW(hFirstWnd, buffer);
2182 switch(LOWORD(wParam))
2186 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
2187 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
2188 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
2189 WCHAR buffer[MAX_STRING_LEN];
2194 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
2195 if(number_from_string(buffer, &num, TRUE))
2197 pf.dxOffset = current_units_to_twips(num);
2198 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
2199 if(number_from_string(buffer, &num, TRUE))
2201 pf.dxRightIndent = current_units_to_twips(num);
2202 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
2203 if(number_from_string(buffer, &num, TRUE))
2205 pf.dxStartIndent = current_units_to_twips(num);
2209 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
2210 wszAppTitle, MB_OK | MB_ICONASTERISK);
2214 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
2215 pf.cbSize = sizeof(pf);
2216 pf.dwMask = PFM_OFFSET | PFM_OFFSETINDENT | PFM_RIGHTINDENT;
2217 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2223 EndDialog(hWnd, wParam);
2230 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2236 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2238 WCHAR buffer[MAX_STRING_LEN];
2241 pf.cbSize = sizeof(pf);
2242 pf.dwMask = PFM_TABSTOPS;
2243 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2244 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
2246 for(i = 0; i < pf.cTabCount; i++)
2248 number_with_units(buffer, pf.rgxTabs[i]);
2249 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
2256 switch(LOWORD(wParam))
2260 HWND hTabWnd = (HWND)lParam;
2261 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
2262 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
2263 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
2265 if(GetWindowTextLengthW(hTabWnd))
2266 EnableWindow(hAddWnd, TRUE);
2268 EnableWindow(hAddWnd, FALSE);
2270 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
2272 EnableWindow(hEmptyWnd, TRUE);
2274 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
2275 EnableWindow(hDelWnd, FALSE);
2277 EnableWindow(hDelWnd, TRUE);
2280 EnableWindow(hEmptyWnd, FALSE);
2287 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2288 WCHAR buffer[MAX_STRING_LEN];
2290 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
2291 append_current_units(buffer);
2293 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
2297 if(!number_from_string(buffer, &number, TRUE))
2299 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
2300 wszAppTitle, MB_OK | MB_ICONINFORMATION);
2303 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
2304 SetWindowTextW(hTabWnd, 0);
2313 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2315 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
2317 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
2323 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2324 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
2331 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
2333 WCHAR buffer[MAX_STRING_LEN];
2337 pf.cbSize = sizeof(pf);
2338 pf.dwMask = PFM_TABSTOPS;
2340 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
2341 (LPARAM)&buffer) != CB_ERR &&
2342 i < MAX_TAB_STOPS; i++)
2344 number_from_string(buffer, &number, TRUE);
2345 pf.rgxTabs[i] = current_units_to_twips(number);
2348 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2352 EndDialog(hWnd, wParam);
2359 static int context_menu(LPARAM lParam)
2361 int x = (int)(short)LOWORD(lParam);
2362 int y = (int)(short)HIWORD(lParam);
2363 HMENU hPop = GetSubMenu(hPopupMenu, 0);
2367 int from = 0, to = 0;
2369 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2370 SendMessageW(hEditorWnd, EM_POSFROMCHAR, (WPARAM)&pt, (LPARAM)to);
2371 ClientToScreen(hEditorWnd, (POINT*)&pt);
2376 TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
2377 x, y, 0, hMainWnd, 0);
2382 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
2384 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd;
2385 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2388 int nStdBitmaps = 0;
2391 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
2392 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
2394 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
2396 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
2397 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
2398 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
2400 rbi.cbSize = sizeof(rbi);
2403 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
2406 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
2408 1, hInstance, IDB_TOOLBAR,
2410 24, 24, 16, 16, sizeof(TBBUTTON));
2412 ab.hInst = HINST_COMMCTRL;
2413 ab.nID = IDB_STD_SMALL_COLOR;
2414 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
2416 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
2417 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
2418 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
2419 AddSeparator(hToolBarWnd);
2420 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
2421 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
2422 AddSeparator(hToolBarWnd);
2423 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
2424 AddSeparator(hToolBarWnd);
2425 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
2426 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
2427 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
2428 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
2429 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
2430 AddSeparator(hToolBarWnd);
2431 AddButton(hToolBarWnd, 0, ID_DATETIME);
2433 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
2435 rbb.cbSize = sizeof(rbb);
2436 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
2437 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
2439 rbb.hwndChild = hToolBarWnd;
2441 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
2442 rbb.wID = BANDID_TOOLBAR;
2444 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2446 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
2447 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
2448 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
2450 rbb.hwndChild = hFontListWnd;
2452 rbb.wID = BANDID_FONTLIST;
2454 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2456 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
2457 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
2458 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
2460 rbb.hwndChild = hSizeListWnd;
2462 rbb.fStyle ^= RBBS_BREAK;
2463 rbb.wID = BANDID_SIZELIST;
2465 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2467 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
2468 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
2469 IDC_FORMATBAR, 7, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
2471 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
2472 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
2473 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
2474 AddSeparator(hFormatBarWnd);
2475 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
2476 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
2477 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
2478 AddSeparator(hFormatBarWnd);
2479 AddButton(hFormatBarWnd, 6, ID_BULLET);
2481 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
2483 rbb.hwndChild = hFormatBarWnd;
2484 rbb.wID = BANDID_FORMATBAR;
2486 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
2488 hDLL = LoadLibraryW(wszRichEditDll);
2491 MessageBoxW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
2492 MB_OK | MB_ICONEXCLAMATION);
2496 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
2497 WS_CHILD|WS_VISIBLE|ECO_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
2498 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
2502 fprintf(stderr, "Error code %u\n", GetLastError());
2507 SetFocus(hEditorWnd);
2508 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
2512 populate_font_list(hFontListWnd);
2513 populate_size_list(hSizeListWnd);
2515 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2517 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
2519 registry_read_filelist(hWnd);
2520 registry_read_formatopts_all();
2521 registry_read_options();
2522 DragAcceptFiles(hWnd, TRUE);
2527 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
2529 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2530 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2531 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
2532 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2538 ZeroMemory(&fmt, sizeof(fmt));
2539 fmt.cbSize = sizeof(fmt);
2541 ZeroMemory(&pf, sizeof(pf));
2542 pf.cbSize = sizeof(pf);
2544 gt.flags = GTL_NUMCHARS;
2547 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
2548 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)>, 0) ? 1 : 0);
2550 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
2552 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2553 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
2554 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
2555 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
2556 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
2557 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
2558 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2560 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2561 (fmt.dwEffects & CFE_BOLD));
2562 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2563 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2564 (fmt.dwEffects & CFE_ITALIC));
2565 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2566 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2567 (fmt.dwEffects & CFE_UNDERLINE));
2568 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2570 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2571 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2572 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2573 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2575 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
2579 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
2581 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2582 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2583 NMHDR *pHdr = (NMHDR *)lParam;
2584 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2585 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2586 WCHAR sizeBuffer[MAX_PATH];
2588 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2590 if (pHdr->code == CBEN_ENDEDITW)
2592 CHARFORMAT2W format;
2593 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
2595 ZeroMemory(&format, sizeof(format));
2596 format.cbSize = sizeof(format);
2597 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
2599 if(pHdr->hwndFrom == hwndFontList)
2601 if(lstrcmpW(format.szFaceName, (LPWSTR)endEdit->szText))
2602 set_font((LPCWSTR) endEdit->szText);
2603 } else if (pHdr->hwndFrom == hwndSizeList)
2605 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
2606 if(lstrcmpW(sizeBuffer, (LPWSTR)endEdit->szText))
2609 if(number_from_string((LPWSTR)endEdit->szText, &size, FALSE))
2614 SetWindowTextW(hwndSizeList, sizeBuffer);
2615 MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER), wszAppTitle, MB_OK | MB_ICONINFORMATION);
2623 if (pHdr->hwndFrom != hwndEditor)
2626 if (pHdr->code == EN_SELCHANGE)
2628 SELCHANGE *pSC = (SELCHANGE *)lParam;
2633 sprintf( buf,"selection = %d..%d, line count=%ld",
2634 pSC->chrg.cpMin, pSC->chrg.cpMax,
2635 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
2636 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2637 SendMessage(hWnd, WM_USER, 0, 0);
2643 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2645 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2646 static FINDREPLACEW findreplace;
2648 if ((HWND)lParam == hwndEditor)
2651 switch(LOWORD(wParam))
2655 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2660 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2661 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
2662 (DLGPROC)newfile_proc);
2664 if(ret != ID_NEWFILE_ABORT)
2666 if(prompt_save_changes())
2671 wszFileName[0] = '\0';
2675 st.flags = ST_DEFAULT;
2677 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2679 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2680 set_fileformat(ret);
2694 DoSaveFile(wszFileName, fileFormat);
2699 case ID_FILE_SAVEAS:
2703 case ID_FILE_RECENT1:
2704 case ID_FILE_RECENT2:
2705 case ID_FILE_RECENT3:
2706 case ID_FILE_RECENT4:
2708 HMENU hMenu = GetMenu(hWnd);
2711 mi.cbSize = sizeof(MENUITEMINFOW);
2712 mi.fMask = MIIM_DATA;
2713 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2714 DoOpenFile((LPWSTR)mi.dwItemData);
2719 dialog_find(&findreplace);
2723 handle_findmsg(&findreplace);
2726 case ID_FONTSETTINGS:
2727 dialog_choose_font();
2734 case ID_PRINT_QUICK:
2740 int index = reg_formatindex(fileFormat);
2741 DWORD tmp = barState[index];
2742 barState[index] = 0;
2744 barState[index] = tmp;
2745 ShowWindow(hEditorWnd, FALSE);
2746 preview_bar_show(TRUE);
2750 SetMenu(hWnd, NULL);
2751 InvalidateRect(0, 0, TRUE);
2756 dialog_printsetup();
2759 case ID_FORMAT_BOLD:
2760 case ID_FORMAT_ITALIC:
2761 case ID_FORMAT_UNDERLINE:
2764 int mask = CFM_BOLD;
2765 if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
2766 if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
2768 ZeroMemory(&fmt, sizeof(fmt));
2769 fmt.cbSize = sizeof(fmt);
2770 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2771 if (!(fmt.dwMask&mask))
2772 fmt.dwEffects |= mask;
2774 fmt.dwEffects ^= mask;
2776 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2781 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2785 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2789 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2793 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2796 case ID_EDIT_SELECTALL:
2798 CHARRANGE range = {0, -1};
2799 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2800 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2804 case ID_EDIT_GETTEXT:
2806 int nLen = GetWindowTextLengthW(hwndEditor);
2807 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2810 GetWindowTextW(hwndEditor, data, nLen+1);
2811 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
2813 HeapFree( GetProcessHeap(), 0, data);
2814 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2816 tr.chrg.cpMax = nLen;
2817 tr.lpstrText = data;
2818 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2819 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
2820 HeapFree( GetProcessHeap(), 0, data );
2822 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2826 case ID_EDIT_CHARFORMAT:
2827 case ID_EDIT_DEFCHARFORMAT:
2831 ZeroMemory(&cf, sizeof(cf));
2832 cf.cbSize = sizeof(cf);
2834 i = SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2835 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2839 case ID_EDIT_PARAFORMAT:
2842 ZeroMemory(&pf, sizeof(pf));
2843 pf.cbSize = sizeof(pf);
2844 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2848 case ID_EDIT_SELECTIONINFO:
2850 CHARRANGE range = {0, -1};
2854 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2855 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2856 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2857 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2858 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2859 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
2860 HeapFree( GetProcessHeap(), 0, data);
2861 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2865 case ID_EDIT_READONLY:
2867 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2868 if (nStyle & ES_READONLY)
2869 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2871 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2875 case ID_EDIT_MODIFIED:
2876 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2877 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2879 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2883 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2887 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2894 pf.cbSize = sizeof(pf);
2895 pf.dwMask = PFM_NUMBERING;
2896 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2898 pf.dwMask |= PFM_OFFSET;
2900 if(pf.wNumbering == PFN_BULLET)
2906 pf.wNumbering = PFN_BULLET;
2910 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2915 case ID_ALIGN_CENTER:
2916 case ID_ALIGN_RIGHT:
2920 pf.cbSize = sizeof(pf);
2921 pf.dwMask = PFM_ALIGNMENT;
2922 switch(LOWORD(wParam)) {
2923 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2924 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2925 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2927 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2932 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2936 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2939 case ID_TOGGLE_TOOLBAR:
2940 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2944 case ID_TOGGLE_FORMATBAR:
2945 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2946 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2947 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2951 case ID_TOGGLE_STATUSBAR:
2952 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2958 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2959 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_DATETIME), hWnd, (DLGPROC)datetime_proc);
2965 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2966 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd,
2973 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
2974 DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2982 case ID_VIEWPROPERTIES:
2983 dialog_viewproperties();
2987 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2993 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
2995 HMENU hMenu = (HMENU)wParam;
2996 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2997 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2999 int nAlignment = -1;
3005 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
3006 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
3007 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
3009 pf.cbSize = sizeof(PARAFORMAT);
3010 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
3011 CheckMenuItem(hMenu, ID_EDIT_READONLY,
3012 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
3013 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
3014 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
3015 if (pf.dwMask & PFM_ALIGNMENT)
3016 nAlignment = pf.wAlignment;
3017 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
3018 MF_CHECKED : MF_UNCHECKED);
3019 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
3020 MF_CHECKED : MF_UNCHECKED);
3021 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
3022 MF_CHECKED : MF_UNCHECKED);
3023 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
3024 MF_CHECKED : MF_UNCHECKED));
3025 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
3026 MF_ENABLED : MF_GRAYED);
3027 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
3028 MF_ENABLED : MF_GRAYED);
3030 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
3031 MF_CHECKED : MF_UNCHECKED);
3033 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
3034 MF_CHECKED : MF_UNCHECKED);
3036 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
3037 MF_CHECKED : MF_UNCHECKED);
3039 gt.flags = GTL_NUMCHARS;
3041 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)>, 0);
3042 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
3044 mi.cbSize = sizeof(mi);
3045 mi.fMask = MIIM_DATA;
3047 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
3049 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
3050 MF_ENABLED : MF_GRAYED));
3054 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
3056 int nStatusSize = 0;
3058 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
3059 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
3060 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
3061 int rebarHeight = 0;
3066 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
3067 if (IsWindowVisible(hwndStatusBar))
3069 GetClientRect(hwndStatusBar, &rc);
3070 nStatusSize = rc.bottom - rc.top;
3078 if(!is_bar_visible(BANDID_TOOLBAR))
3081 if(!is_bar_visible(BANDID_FORMATBAR))
3084 rebarHeight = rebarRows ? SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0) : 0;
3086 rc.top = rc.left = 0;
3087 rc.bottom = rebarHeight;
3088 rc.right = LOWORD(lParam);
3089 SendMessageW(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
3093 GetClientRect(hWnd, &rc);
3094 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
3097 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
3100 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3102 if(msg == ID_FINDMSGSTRING)
3103 return handle_findmsg((LPFINDREPLACEW)lParam);
3108 return OnCreate( hWnd, wParam, lParam );
3111 return OnUser( hWnd, wParam, lParam );
3114 return OnNotify( hWnd, wParam, lParam );
3118 return preview_command( hWnd, wParam, lParam );
3120 return OnCommand( hWnd, wParam, lParam );
3130 } else if(prompt_save_changes())
3132 registry_set_options();
3133 registry_set_formatopts_all();
3140 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
3143 case WM_INITMENUPOPUP:
3144 return OnInitPopupMenu( hWnd, wParam, lParam );
3147 return OnSize( hWnd, wParam, lParam );
3149 case WM_CONTEXTMENU:
3150 if((HWND)wParam == hEditorWnd)
3151 return context_menu(lParam);
3153 return DefWindowProcW(hWnd, msg, wParam, lParam);
3157 WCHAR file[MAX_PATH];
3158 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
3159 DragFinish((HDROP)wParam);
3161 if(prompt_save_changes())
3167 return print_preview();
3169 return DefWindowProcW(hWnd, msg, wParam, lParam);
3172 return DefWindowProcW(hWnd, msg, wParam, lParam);
3178 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
3180 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
3185 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
3186 'T','A','B','L','E','\0'};
3188 InitCommonControlsEx(&classes);
3190 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
3192 wc.style = CS_HREDRAW | CS_VREDRAW;
3193 wc.lpfnWndProc = WndProc;
3196 wc.hInstance = hInstance;
3197 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
3198 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
3199 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3200 wc.lpszMenuName = xszMainMenu;
3201 wc.lpszClassName = wszMainWndClass;
3202 RegisterClassW(&wc);
3204 rc = registry_read_winrect();
3205 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
3206 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
3207 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
3211 set_fileformat(SF_RTF);
3212 hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
3213 get_default_printer_opts();
3216 HandleCommandLine(GetCommandLineW());
3218 while(GetMessageW(&msg,0,0,0))
3220 if (IsDialogMessage(hFindWnd, &msg))
3223 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
3225 TranslateMessage(&msg);
3226 DispatchMessageW(&msg);
3227 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
3228 SendMessageW(hMainWnd, WM_USER, 0, 0);