unicode: Use a standard two-level mapping table for the digit map.
[wine.git] / programs / wordpad / wordpad.c
blob579291be804b22deb8560711ca1decbb305e8bb2
1 /*
2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_IE 0x0400
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <assert.h>
31 #include <windows.h>
32 #include <richedit.h>
33 #include <commctrl.h>
34 #include <commdlg.h>
35 #include <shellapi.h>
36 #include <math.h>
37 #include <errno.h>
39 #include "wordpad.h"
41 #ifdef NONAMELESSUNION
42 # define U(x) (x).u
43 # define U2(x) (x).u2
44 # define U3(x) (x).u3
45 #else
46 # define U(x) (x)
47 # define U2(x) (x)
48 # define U3(x) (x)
49 #endif
51 /* use LoadString */
52 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
54 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
56 static const WCHAR stringFormat[] = {'%','2','d','\0'};
58 const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
59 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
61 static HWND hMainWnd;
62 static HWND hEditorWnd;
63 static HWND hFindWnd;
64 static HMENU hColorPopupMenu;
66 static UINT ID_FINDMSGSTRING;
68 static DWORD wordWrap[2];
69 static DWORD barState[2];
70 static WPARAM fileFormat = SF_RTF;
72 static WCHAR wszFileName[MAX_PATH];
73 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
74 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
75 static WCHAR wszSaveChanges[MAX_STRING_LEN];
76 static WCHAR units_cmW[MAX_STRING_LEN];
77 static WCHAR units_inW[MAX_STRING_LEN];
78 static WCHAR units_inchW[MAX_STRING_LEN];
79 static WCHAR units_ptW[MAX_STRING_LEN];
81 static int last_bullet = PFN_BULLET;
83 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
85 typedef enum
87 UNIT_CM,
88 UNIT_INCH,
89 UNIT_PT
90 } UNIT;
92 typedef struct
94 int endPos;
95 BOOL wrapped;
96 WCHAR findBuffer[128];
97 } FINDREPLACE_custom;
99 /* Load string resources */
100 static void DoLoadStrings(void)
102 LPWSTR p = wszFilter;
103 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
104 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
105 static const WCHAR files_all[] = {'*','.','*','\0'};
107 HINSTANCE hInstance = GetModuleHandleW(0);
109 p += 1 + LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
110 lstrcpyW(p, files_rtf);
111 p += lstrlenW(p) + 1;
112 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
113 lstrcpyW(p, files_txt);
114 p += lstrlenW(p) + 1;
115 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
116 lstrcpyW(p, files_txt);
117 p += lstrlenW(p) + 1;
118 p += 1 + LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
119 lstrcpyW(p, files_all);
120 p += lstrlenW(p) + 1;
121 *p = '\0';
123 p = wszDefaultFileName;
124 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
126 p = wszSaveChanges;
127 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
129 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
130 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
131 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
132 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
135 /* Show a message box with resource strings */
136 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
138 MSGBOXPARAMSW params;
140 params.cbSize = sizeof(params);
141 params.hwndOwner = hWnd;
142 params.hInstance = GetModuleHandleW(0);
143 params.lpszText = lpText;
144 params.lpszCaption = lpCaption;
145 params.dwStyle = uType;
146 params.lpszIcon = NULL;
147 params.dwContextHelpId = 0;
148 params.lpfnMsgBoxCallback = NULL;
149 params.dwLanguageId = 0;
150 return MessageBoxIndirectW(&params);
154 static void AddButtonStyle(HWND hwndToolBar, int nImage, int nCommand, BYTE style)
156 TBBUTTON button;
158 ZeroMemory(&button, sizeof(button));
159 button.iBitmap = nImage;
160 button.idCommand = nCommand;
161 button.fsState = TBSTATE_ENABLED;
162 button.fsStyle = style;
163 button.dwData = 0;
164 button.iString = -1;
165 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
168 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
170 AddButtonStyle(hwndToolBar, nImage, nCommand, BTNS_BUTTON);
173 static void AddSeparator(HWND hwndToolBar)
175 TBBUTTON button;
177 ZeroMemory(&button, sizeof(button));
178 button.iBitmap = -1;
179 button.idCommand = 0;
180 button.fsState = 0;
181 button.fsStyle = BTNS_SEP;
182 button.dwData = 0;
183 button.iString = -1;
184 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
187 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
189 HANDLE hFile = (HANDLE)cookie;
190 DWORD read;
192 if(!ReadFile(hFile, buffer, cb, &read, 0))
193 return 1;
195 *pcb = read;
197 return 0;
200 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
202 DWORD written;
203 int ret;
204 HANDLE hFile = (HANDLE)cookie;
206 ret = WriteFile(hFile, buffer, cb, &written, 0);
208 if(!ret || (cb != written))
209 return 1;
211 *pcb = cb;
213 return 0;
216 LPWSTR file_basename(LPWSTR path)
218 LPWSTR pos = path + lstrlenW(path);
220 while(pos > path)
222 if(*pos == '\\' || *pos == '/')
224 pos++;
225 break;
227 pos--;
229 return pos;
232 static void set_caption(LPCWSTR wszNewFileName)
234 static const WCHAR wszSeparator[] = {' ','-',' '};
235 WCHAR *wszCaption;
236 SIZE_T length = 0;
238 if(!wszNewFileName)
239 wszNewFileName = wszDefaultFileName;
240 else
241 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
243 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
244 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
246 if(!wszCaption)
247 return;
249 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
250 length += lstrlenW(wszNewFileName);
251 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
252 length += ARRAY_SIZE(wszSeparator);
253 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
255 SetWindowTextW(hMainWnd, wszCaption);
257 HeapFree(GetProcessHeap(), 0, wszCaption);
260 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
262 if(punit != NULL)
263 *punit = UNIT_CM;
264 if(!endptr)
265 return FALSE;
266 if(!*endptr)
267 return TRUE;
269 while(*endptr == ' ')
270 endptr++;
272 if(punit == NULL)
273 return *endptr == '\0';
275 if(!lstrcmpW(endptr, units_cmW))
277 *punit = UNIT_CM;
278 endptr += lstrlenW(units_cmW);
280 else if (!lstrcmpW(endptr, units_inW))
282 *punit = UNIT_INCH;
283 endptr += lstrlenW(units_inW);
285 else if (!lstrcmpW(endptr, units_inchW))
287 *punit = UNIT_INCH;
288 endptr += lstrlenW(units_inchW);
290 else if (!lstrcmpW(endptr, units_ptW))
292 *punit = UNIT_PT;
293 endptr += lstrlenW(units_ptW);
296 return *endptr == '\0';
299 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
301 double ret;
302 WCHAR *endptr;
304 *num = 0;
305 errno = 0;
306 ret = wcstod(string, &endptr);
308 if (punit != NULL)
309 *punit = UNIT_CM;
310 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
312 return FALSE;
313 } else
315 *num = (float)ret;
316 return TRUE;
320 static void set_size(float size)
322 CHARFORMAT2W fmt;
324 ZeroMemory(&fmt, sizeof(fmt));
325 fmt.cbSize = sizeof(fmt);
326 fmt.dwMask = CFM_SIZE;
327 fmt.yHeight = (int)(size * 20.0);
328 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
331 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
333 WCHAR sizeBuffer[MAX_STRING_LEN];
334 CHARFORMAT2W format;
336 ZeroMemory(&format, sizeof(format));
337 format.cbSize = sizeof(format);
338 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
340 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
341 if(lstrcmpW(sizeBuffer, wszNewFontSize))
343 float size = 0;
344 if(number_from_string(wszNewFontSize, &size, NULL)
345 && size > 0)
347 set_size(size);
348 } else
350 SetWindowTextW(hwndSizeList, sizeBuffer);
351 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
352 wszAppTitle, MB_OK | MB_ICONINFORMATION);
357 static void add_size(HWND hSizeListWnd, unsigned size)
359 WCHAR buffer[3];
360 COMBOBOXEXITEMW cbItem;
361 cbItem.mask = CBEIF_TEXT;
362 cbItem.iItem = -1;
364 wsprintfW(buffer, stringFormat, size);
365 cbItem.pszText = buffer;
366 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
369 static void populate_size_list(HWND hSizeListWnd)
371 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
372 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
373 COMBOBOXEXITEMW cbFontItem;
374 CHARFORMAT2W fmt;
375 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
376 HDC hdc = GetDC(hMainWnd);
377 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
378 WCHAR buffer[3];
379 size_t i;
380 DWORD fontStyle;
382 ZeroMemory(&fmt, sizeof(fmt));
383 fmt.cbSize = sizeof(fmt);
384 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
386 cbFontItem.mask = CBEIF_LPARAM;
387 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
388 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
390 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
392 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
394 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
396 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
397 GetDeviceCaps(hdc, LOGPIXELSY)));
398 } else
400 for(i = 0; i < ARRAY_SIZE(choices); i++)
401 add_size(hSizeListWnd, choices[i]);
404 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
405 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
408 static void update_size_list(void)
410 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
411 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
412 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
413 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
414 CHARFORMAT2W fmt;
416 ZeroMemory(&fmt, sizeof(fmt));
417 fmt.cbSize = sizeof(fmt);
419 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
421 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
422 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
424 if(lstrcmpW(fontSize, sizeBuffer))
425 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
428 static void update_font_list(void)
430 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
431 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
432 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
433 WCHAR fontName[MAX_STRING_LEN];
434 CHARFORMAT2W fmt;
436 ZeroMemory(&fmt, sizeof(fmt));
437 fmt.cbSize = sizeof(fmt);
439 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
440 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
442 if(lstrcmpW(fontName, fmt.szFaceName))
444 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
445 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
446 } else
448 update_size_list();
452 static void clear_formatting(void)
454 PARAFORMAT2 pf;
456 pf.cbSize = sizeof(pf);
457 pf.dwMask = PFM_ALIGNMENT;
458 pf.wAlignment = PFA_LEFT;
459 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
462 static int fileformat_number(WPARAM format)
464 int number = 0;
466 if(format == SF_TEXT)
468 number = 1;
469 } else if (format == (SF_TEXT | SF_UNICODE))
471 number = 2;
473 return number;
476 static WPARAM fileformat_flags(int format)
478 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
480 return flags[format];
483 static void set_font(LPCWSTR wszFaceName)
485 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
486 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
487 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
488 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
489 CHARFORMAT2W fmt;
491 ZeroMemory(&fmt, sizeof(fmt));
493 fmt.cbSize = sizeof(fmt);
494 fmt.dwMask = CFM_FACE;
496 lstrcpyW(fmt.szFaceName, wszFaceName);
498 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
500 populate_size_list(hSizeListWnd);
502 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
505 static void set_default_font(void)
507 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
508 'R','o','m','a','n',0};
509 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
510 CHARFORMAT2W fmt;
511 LPCWSTR font;
513 ZeroMemory(&fmt, sizeof(fmt));
515 fmt.cbSize = sizeof(fmt);
516 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
517 fmt.dwEffects = 0;
519 if(fileFormat & SF_RTF)
520 font = richTextFont;
521 else
522 font = plainTextFont;
524 lstrcpyW(fmt.szFaceName, font);
526 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
529 static void on_fontlist_modified(LPWSTR wszNewFaceName)
531 CHARFORMAT2W format;
532 ZeroMemory(&format, sizeof(format));
533 format.cbSize = sizeof(format);
534 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
536 if(lstrcmpW(format.szFaceName, wszNewFaceName))
537 set_font(wszNewFaceName);
540 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
542 COMBOBOXEXITEMW cbItem;
543 WCHAR buffer[MAX_PATH];
544 int fontHeight = 0;
546 cbItem.mask = CBEIF_TEXT;
547 cbItem.pszText = buffer;
548 cbItem.cchTextMax = MAX_STRING_LEN;
549 cbItem.iItem = 0;
551 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
553 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
554 cbItem.iItem++;
555 else
556 break;
558 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
559 lstrcpyW( cbItem.pszText, fontName );
561 cbItem.mask |= CBEIF_LPARAM;
562 if(fontType & RASTER_FONTTYPE)
563 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
565 cbItem.lParam = MAKELONG(fontType,fontHeight);
566 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
567 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
570 static void dialog_choose_font(void)
572 CHOOSEFONTW cf;
573 LOGFONTW lf;
574 CHARFORMAT2W fmt;
575 HDC hDC = GetDC(hMainWnd);
577 ZeroMemory(&cf, sizeof(cf));
578 cf.lStructSize = sizeof(cf);
579 cf.hwndOwner = hMainWnd;
580 cf.lpLogFont = &lf;
581 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_NOVERTFONTS;
583 ZeroMemory(&fmt, sizeof(fmt));
584 fmt.cbSize = sizeof(fmt);
586 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
587 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
588 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
589 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
590 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
591 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
592 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
593 cf.rgbColors = fmt.crTextColor;
595 if(ChooseFontW(&cf))
597 ZeroMemory(&fmt, sizeof(fmt));
598 fmt.cbSize = sizeof(fmt);
599 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
600 fmt.yHeight = cf.iPointSize * 2;
602 if(cf.nFontType & BOLD_FONTTYPE)
603 fmt.dwEffects |= CFE_BOLD;
604 if(cf.nFontType & ITALIC_FONTTYPE)
605 fmt.dwEffects |= CFE_ITALIC;
606 if(cf.lpLogFont->lfUnderline)
607 fmt.dwEffects |= CFE_UNDERLINE;
608 if(cf.lpLogFont->lfStrikeOut)
609 fmt.dwEffects |= CFE_STRIKEOUT;
611 fmt.crTextColor = cf.rgbColors;
613 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
614 set_font(cf.lpLogFont->lfFaceName);
619 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
620 DWORD FontType, LPARAM lParam)
622 HWND hListWnd = (HWND) lParam;
624 if (lpelfe->lfFaceName[0] == '@') return 1; /* ignore vertical fonts */
626 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
629 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
632 return 1;
635 static void populate_font_list(HWND hListWnd)
637 HDC hdc = GetDC(hMainWnd);
638 LOGFONTW fontinfo;
639 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
640 CHARFORMAT2W fmt;
642 fontinfo.lfCharSet = DEFAULT_CHARSET;
643 *fontinfo.lfFaceName = '\0';
644 fontinfo.lfPitchAndFamily = 0;
646 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
647 (LPARAM)hListWnd, 0);
649 ZeroMemory(&fmt, sizeof(fmt));
650 fmt.cbSize = sizeof(fmt);
651 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
652 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
655 static void update_window(void)
657 RECT rect;
659 GetClientRect(hMainWnd, &rect);
661 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
664 static BOOL is_bar_visible(int bandId)
666 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
669 static void store_bar_state(int bandId, BOOL show)
671 int formatIndex = reg_formatindex(fileFormat);
673 if(show)
674 barState[formatIndex] |= (1 << bandId);
675 else
676 barState[formatIndex] &= ~(1 << bandId);
679 static void set_toolbar_state(int bandId, BOOL show)
681 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
683 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
685 if(bandId == BANDID_TOOLBAR)
687 REBARBANDINFOW rbbinfo;
688 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
690 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
691 rbbinfo.fMask = RBBIM_STYLE;
693 SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
695 if(!show)
696 rbbinfo.fStyle &= ~RBBS_BREAK;
697 else
698 rbbinfo.fStyle |= RBBS_BREAK;
700 SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
703 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
704 store_bar_state(bandId, show);
707 static void set_statusbar_state(BOOL show)
709 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
711 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
712 store_bar_state(BANDID_STATUSBAR, show);
715 static void set_bar_states(void)
717 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
718 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
719 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
720 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
721 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
722 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
724 update_window();
727 static void preview_exit(HWND hMainWnd)
729 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
730 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
732 set_bar_states();
733 ShowWindow(hEditorWnd, TRUE);
735 close_preview(hMainWnd);
737 SetMenu(hMainWnd, hMenu);
738 registry_read_filelist(hMainWnd);
740 update_window();
743 static void set_fileformat(WPARAM format)
745 fileFormat = format;
747 set_bar_states();
748 set_default_font();
749 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
752 static void ShowOpenError(DWORD Code)
754 LPWSTR Message;
756 switch(Code)
758 case ERROR_ACCESS_DENIED:
759 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
760 break;
762 default:
763 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
765 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
768 static void DoOpenFile(LPCWSTR szOpenFileName)
770 HANDLE hFile;
771 EDITSTREAM es;
772 char fileStart[5];
773 DWORD readOut;
774 WPARAM format = SF_TEXT;
776 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
777 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
778 if (hFile == INVALID_HANDLE_VALUE)
780 ShowOpenError(GetLastError());
781 return;
784 ReadFile(hFile, fileStart, 5, &readOut, NULL);
785 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
787 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
789 format = SF_TEXT | SF_UNICODE;
790 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
791 } else if(readOut >= 5)
793 static const char header[] = "{\\rtf";
794 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
796 if(!memcmp(header, fileStart, 5))
797 format = SF_RTF;
798 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
800 CloseHandle(hFile);
801 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
802 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
803 return;
807 es.dwCookie = (DWORD_PTR)hFile;
808 es.pfnCallback = stream_in;
810 clear_formatting();
811 set_fileformat(format);
812 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
814 CloseHandle(hFile);
816 SetFocus(hEditorWnd);
818 set_caption(szOpenFileName);
820 lstrcpyW(wszFileName, szOpenFileName);
821 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
822 registry_set_filelist(szOpenFileName, hMainWnd);
823 update_font_list();
826 static void ShowWriteError(DWORD Code)
828 LPWSTR Message;
830 switch(Code)
832 case ERROR_ACCESS_DENIED:
833 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
834 break;
836 default:
837 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
839 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
842 static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
844 HANDLE hFile;
845 EDITSTREAM stream;
846 LRESULT ret;
848 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
849 FILE_ATTRIBUTE_NORMAL, NULL);
851 if(hFile == INVALID_HANDLE_VALUE)
853 ShowWriteError(GetLastError());
854 return FALSE;
857 if(format == (SF_TEXT | SF_UNICODE))
859 static const BYTE unicode[] = {0xff,0xfe};
860 DWORD writeOut;
861 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
863 if(writeOut != sizeof(unicode))
865 CloseHandle(hFile);
866 return FALSE;
870 stream.dwCookie = (DWORD_PTR)hFile;
871 stream.pfnCallback = stream_out;
873 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
875 CloseHandle(hFile);
877 SetFocus(hEditorWnd);
879 if(!ret)
881 GETTEXTLENGTHEX gt;
882 gt.flags = GTL_DEFAULT;
883 gt.codepage = 1200;
885 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
886 return FALSE;
889 lstrcpyW(wszFileName, wszSaveFileName);
890 set_caption(wszFileName);
891 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
892 set_fileformat(format);
894 return TRUE;
897 static BOOL DialogSaveFile(void)
899 OPENFILENAMEW sfn;
901 WCHAR wszFile[MAX_PATH] = {'\0'};
902 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
904 ZeroMemory(&sfn, sizeof(sfn));
906 sfn.lStructSize = sizeof(sfn);
907 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
908 sfn.hwndOwner = hMainWnd;
909 sfn.lpstrFilter = wszFilter;
910 sfn.lpstrFile = wszFile;
911 sfn.nMaxFile = MAX_PATH;
912 sfn.lpstrDefExt = wszDefExt;
913 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
915 while(GetSaveFileNameW(&sfn))
917 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
919 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
920 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
921 continue;
923 return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
925 return FALSE;
928 static BOOL prompt_save_changes(void)
930 if(!wszFileName[0])
932 GETTEXTLENGTHEX gt;
933 gt.flags = GTL_NUMCHARS;
934 gt.codepage = 1200;
935 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
936 return TRUE;
939 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
941 return TRUE;
942 } else
944 LPWSTR displayFileName;
945 WCHAR *text;
946 int ret;
948 if(!wszFileName[0])
949 displayFileName = wszDefaultFileName;
950 else
951 displayFileName = file_basename(wszFileName);
953 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
954 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
956 if(!text)
957 return FALSE;
959 wsprintfW(text, wszSaveChanges, displayFileName);
961 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
963 HeapFree(GetProcessHeap(), 0, text);
965 switch(ret)
967 case IDNO:
968 return TRUE;
970 case IDYES:
971 if(wszFileName[0])
972 return DoSaveFile(wszFileName, fileFormat);
973 return DialogSaveFile();
975 default:
976 return FALSE;
981 static void DialogOpenFile(void)
983 OPENFILENAMEW ofn;
985 WCHAR wszFile[MAX_PATH] = {'\0'};
986 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
988 ZeroMemory(&ofn, sizeof(ofn));
990 ofn.lStructSize = sizeof(ofn);
991 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
992 ofn.hwndOwner = hMainWnd;
993 ofn.lpstrFilter = wszFilter;
994 ofn.lpstrFile = wszFile;
995 ofn.nMaxFile = MAX_PATH;
996 ofn.lpstrDefExt = wszDefExt;
997 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
999 if(GetOpenFileNameW(&ofn))
1001 if(prompt_save_changes())
1002 DoOpenFile(ofn.lpstrFile);
1006 static void dialog_about(void)
1008 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1009 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1012 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1014 switch(message)
1016 case WM_INITDIALOG:
1018 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1019 int wrap = -1;
1020 char id[4];
1021 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1023 sprintf(id, "%d\n", (int)ps->lParam);
1024 SetWindowTextA(hIdWnd, id);
1025 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1026 wrap = IDC_PAGEFMT_WN;
1027 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1028 wrap = IDC_PAGEFMT_WW;
1029 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1030 wrap = IDC_PAGEFMT_WM;
1032 if(wrap != -1)
1033 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1034 IDC_PAGEFMT_WM, wrap);
1036 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1037 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1038 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1039 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1040 if(barState[ps->lParam] & (1 << BANDID_RULER))
1041 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1042 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1043 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1045 break;
1047 case WM_COMMAND:
1048 switch(LOWORD(wParam))
1050 case IDC_PAGEFMT_WN:
1051 case IDC_PAGEFMT_WW:
1052 case IDC_PAGEFMT_WM:
1053 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1054 LOWORD(wParam));
1055 break;
1057 case IDC_PAGEFMT_TB:
1058 case IDC_PAGEFMT_FB:
1059 case IDC_PAGEFMT_RU:
1060 case IDC_PAGEFMT_SB:
1061 CheckDlgButton(hWnd, LOWORD(wParam),
1062 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1063 break;
1065 break;
1066 case WM_NOTIFY:
1068 LPNMHDR header = (LPNMHDR)lParam;
1069 if(header->code == PSN_APPLY)
1071 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1072 char sid[4];
1073 int id;
1075 GetWindowTextA(hIdWnd, sid, 4);
1076 id = atoi(sid);
1077 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1078 wordWrap[id] = ID_WORDWRAP_NONE;
1079 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1080 wordWrap[id] = ID_WORDWRAP_WINDOW;
1081 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1082 wordWrap[id] = ID_WORDWRAP_MARGIN;
1084 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1085 barState[id] |= (1 << BANDID_TOOLBAR);
1086 else
1087 barState[id] &= ~(1 << BANDID_TOOLBAR);
1089 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1090 barState[id] |= (1 << BANDID_FORMATBAR);
1091 else
1092 barState[id] &= ~(1 << BANDID_FORMATBAR);
1094 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1095 barState[id] |= (1 << BANDID_RULER);
1096 else
1097 barState[id] &= ~(1 << BANDID_RULER);
1099 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1100 barState[id] |= (1 << BANDID_STATUSBAR);
1101 else
1102 barState[id] &= ~(1 << BANDID_STATUSBAR);
1105 break;
1107 return FALSE;
1110 static void dialog_viewproperties(void)
1112 PROPSHEETPAGEW psp[2];
1113 PROPSHEETHEADERW psh;
1114 size_t i;
1115 HINSTANCE hInstance = GetModuleHandleW(0);
1116 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1118 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1119 psp[0].dwFlags = PSP_USETITLE;
1120 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1121 psp[0].pfnDlgProc = formatopts_proc;
1122 psp[0].hInstance = hInstance;
1123 psp[0].lParam = reg_formatindex(SF_TEXT);
1124 psp[0].pfnCallback = NULL;
1125 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1126 for(i = 1; i < ARRAY_SIZE(psp); i++)
1128 psp[i].dwSize = psp[0].dwSize;
1129 psp[i].dwFlags = psp[0].dwFlags;
1130 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1131 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1132 psp[i].hInstance = psp[0].hInstance;
1133 psp[i].lParam = reg_formatindex(SF_RTF);
1134 psp[i].pfnCallback = psp[0].pfnCallback;
1135 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1138 psh.dwSize = sizeof(psh);
1139 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1140 psh.hwndParent = hMainWnd;
1141 psh.hInstance = hInstance;
1142 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1143 psh.nPages = ARRAY_SIZE(psp);
1144 U3(psh).ppsp = ppsp;
1145 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1147 if(fileFormat & SF_RTF)
1148 U2(psh).nStartPage = 1;
1149 else
1150 U2(psh).nStartPage = 0;
1151 PropertySheetW(&psh);
1152 set_bar_states();
1153 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1156 static void HandleCommandLine(LPWSTR cmdline)
1158 WCHAR delimiter;
1159 BOOL opt_print = FALSE;
1161 /* skip white space */
1162 while (*cmdline == ' ') cmdline++;
1164 /* skip executable name */
1165 delimiter = (*cmdline == '"' ? '"' : ' ');
1167 if (*cmdline == delimiter) cmdline++;
1168 while (*cmdline && *cmdline != delimiter) cmdline++;
1169 if (*cmdline == delimiter) cmdline++;
1171 while (*cmdline)
1173 while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
1175 if (*cmdline == '-' || *cmdline == '/')
1177 if (!cmdline[2] || isspace(cmdline[2]))
1179 switch (cmdline[1])
1181 case 'P':
1182 case 'p':
1183 opt_print = TRUE;
1184 cmdline += 2;
1185 continue;
1188 /* a filename starting by / */
1190 break;
1193 if (*cmdline)
1195 /* file name is passed on the command line */
1196 if (cmdline[0] == '"')
1198 cmdline++;
1199 cmdline[lstrlenW(cmdline) - 1] = 0;
1201 DoOpenFile(cmdline);
1202 InvalidateRect(hMainWnd, NULL, FALSE);
1205 if (opt_print)
1206 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1209 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1211 if(pFr->Flags & FR_DIALOGTERM)
1213 hFindWnd = 0;
1214 pFr->Flags = FR_FINDNEXT;
1215 return 0;
1218 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1220 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1221 DWORD flags;
1222 FINDTEXTEXW ft;
1223 CHARRANGE sel;
1224 LRESULT ret = -1;
1225 HMENU hMenu = GetMenu(hMainWnd);
1226 MENUITEMINFOW mi;
1228 mi.cbSize = sizeof(mi);
1229 mi.fMask = MIIM_DATA;
1230 mi.dwItemData = 1;
1231 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1233 /* Make sure find field is saved. */
1234 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1236 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1237 ARRAY_SIZE(custom_data->findBuffer));
1238 pFr->lpstrFindWhat = custom_data->findBuffer;
1241 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1242 if(custom_data->endPos == -1) {
1243 custom_data->endPos = sel.cpMin;
1244 custom_data->wrapped = FALSE;
1247 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1248 ft.lpstrText = pFr->lpstrFindWhat;
1250 /* Only replace the existing selection if it is an exact match. */
1251 if (sel.cpMin != sel.cpMax &&
1252 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1254 ft.chrg = sel;
1255 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1256 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1257 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1258 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1262 /* Search from the start of the selection, but exclude the first character
1263 * from search if there is a selection. */
1264 ft.chrg.cpMin = sel.cpMin;
1265 if (sel.cpMin != sel.cpMax)
1266 ft.chrg.cpMin++;
1268 /* Search to the end, then wrap around and search from the start. */
1269 if (!custom_data->wrapped) {
1270 ft.chrg.cpMax = -1;
1271 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1272 if (ret == -1) {
1273 custom_data->wrapped = TRUE;
1274 ft.chrg.cpMin = 0;
1278 if (ret == -1) {
1279 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1280 if (ft.chrg.cpMax > ft.chrg.cpMin)
1281 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1284 if (ret == -1) {
1285 custom_data->endPos = -1;
1286 EnableWindow(hMainWnd, FALSE);
1287 MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1288 wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
1289 EnableWindow(hMainWnd, TRUE);
1290 } else {
1291 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1292 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1294 if (pFr->Flags & FR_REPLACEALL)
1295 return handle_findmsg(pFr);
1299 return 0;
1302 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1304 static WCHAR selBuffer[128];
1305 static WCHAR replaceBuffer[128];
1306 static FINDREPLACE_custom custom_data;
1307 static const WCHAR endl = '\r';
1308 FINDTEXTW ft;
1310 /* Allow only one search/replace dialog to open */
1311 if(hFindWnd != NULL)
1313 SetActiveWindow(hFindWnd);
1314 return;
1317 ZeroMemory(fr, sizeof(FINDREPLACEW));
1318 fr->lStructSize = sizeof(FINDREPLACEW);
1319 fr->hwndOwner = hMainWnd;
1320 fr->Flags = FR_HIDEUPDOWN;
1321 /* Find field is filled with the selected text if it is non-empty
1322 * and stays within the same paragraph, otherwise the previous
1323 * find field is used. */
1324 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1325 (LPARAM)&ft.chrg.cpMax);
1326 ft.lpstrText = &endl;
1327 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1328 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1330 /* Use a temporary buffer for the selected text so that the saved
1331 * find field is only overwritten when a find/replace is clicked. */
1332 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1333 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1334 fr->lpstrFindWhat = selBuffer;
1335 } else {
1336 fr->lpstrFindWhat = custom_data.findBuffer;
1338 fr->lpstrReplaceWith = replaceBuffer;
1339 custom_data.endPos = -1;
1340 custom_data.wrapped = FALSE;
1341 fr->lCustData = (LPARAM)&custom_data;
1342 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1343 fr->wReplaceWithLen = sizeof(replaceBuffer);
1345 if(replace)
1346 hFindWnd = ReplaceTextW(fr);
1347 else
1348 hFindWnd = FindTextW(fr);
1351 static int units_to_twips(UNIT unit, float number)
1353 int twips = 0;
1355 switch(unit)
1357 case UNIT_CM:
1358 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1359 break;
1361 case UNIT_INCH:
1362 twips = (int)(number * (float)TWIPS_PER_INCH);
1363 break;
1365 case UNIT_PT:
1366 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1367 break;
1370 return twips;
1373 static void append_current_units(LPWSTR buffer)
1375 static const WCHAR space[] = {' ', 0};
1376 lstrcatW(buffer, space);
1377 lstrcatW(buffer, units_cmW);
1380 static void number_with_units(LPWSTR buffer, int number)
1382 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1383 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1385 swprintf(buffer, MAX_STRING_LEN, fmt, converted, units_cmW);
1388 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1390 COMBOBOXEXITEMW cbItem;
1391 COMBOBOXINFO cbInfo;
1392 HWND hCombo, hList;
1393 int idx, result;
1395 hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1396 if (!hCombo)
1397 return FALSE;
1398 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1399 result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1400 if (!result)
1401 return FALSE;
1402 hList = cbInfo.hwndList;
1403 idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
1404 if (idx < 0)
1405 return FALSE;
1407 ZeroMemory(&cbItem, sizeof(cbItem));
1408 cbItem.mask = CBEIF_TEXT;
1409 cbItem.iItem = idx;
1410 cbItem.pszText = wszBuffer;
1411 cbItem.cchTextMax = bufferLength-1;
1412 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1414 return result != 0;
1417 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1419 switch(message)
1421 case WM_INITDIALOG:
1423 WCHAR buffer[MAX_STRING_LEN];
1424 SYSTEMTIME st;
1425 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1426 GetLocalTime(&st);
1428 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1429 MAX_STRING_LEN);
1430 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1431 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1432 MAX_STRING_LEN);
1433 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1434 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1435 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1437 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1439 break;
1441 case WM_COMMAND:
1442 switch(LOWORD(wParam))
1444 case IDC_DATETIME:
1445 if (HIWORD(wParam) != LBN_DBLCLK)
1446 break;
1447 /* Fall through */
1449 case IDOK:
1451 LRESULT index;
1452 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1454 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1456 if(index != LB_ERR)
1458 WCHAR buffer[MAX_STRING_LEN];
1459 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1460 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1463 /* Fall through */
1465 case IDCANCEL:
1466 EndDialog(hWnd, wParam);
1467 return TRUE;
1470 return FALSE;
1473 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1475 switch(message)
1477 case WM_INITDIALOG:
1479 HINSTANCE hInstance = GetModuleHandleW(0);
1480 WCHAR buffer[MAX_STRING_LEN];
1481 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1483 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1484 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1485 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1486 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1487 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1488 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1490 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1492 break;
1494 case WM_COMMAND:
1495 switch(LOWORD(wParam))
1497 case IDOK:
1499 LRESULT index;
1500 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1501 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1503 if(index != LB_ERR)
1504 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1506 return TRUE;
1508 case IDCANCEL:
1509 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1510 return TRUE;
1513 return FALSE;
1516 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1518 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1520 switch(message)
1522 case WM_INITDIALOG:
1524 HINSTANCE hInstance = GetModuleHandleW(0);
1525 WCHAR buffer[MAX_STRING_LEN];
1526 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1527 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1528 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1529 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1530 PARAFORMAT2 pf;
1531 int index = 0;
1533 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1534 MAX_STRING_LEN);
1535 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1536 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1537 MAX_STRING_LEN);
1538 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1539 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1540 MAX_STRING_LEN);
1541 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1543 pf.cbSize = sizeof(pf);
1544 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1545 PFM_STARTINDENT;
1546 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1548 if(pf.wAlignment == PFA_RIGHT)
1549 index ++;
1550 else if(pf.wAlignment == PFA_CENTER)
1551 index += 2;
1553 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1555 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1556 SetWindowTextW(hLeftWnd, buffer);
1557 number_with_units(buffer, pf.dxRightIndent);
1558 SetWindowTextW(hRightWnd, buffer);
1559 number_with_units(buffer, -pf.dxOffset);
1560 SetWindowTextW(hFirstWnd, buffer);
1562 break;
1564 case WM_COMMAND:
1565 switch(LOWORD(wParam))
1567 case IDOK:
1569 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1570 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1571 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1572 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1573 WCHAR buffer[MAX_STRING_LEN];
1574 int index;
1575 float num;
1576 int ret = 0;
1577 PARAFORMAT2 pf;
1578 UNIT unit;
1579 BOOL in_list = FALSE;
1581 pf.cbSize = sizeof(pf);
1582 pf.dwMask = PFM_NUMBERING;
1583 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1584 if ((pf.dwMask & PFM_NUMBERING) && pf.wNumbering)
1585 in_list = TRUE;
1587 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1588 pf.wAlignment = ALIGNMENT_VALUES[index];
1590 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1591 if(number_from_string(buffer, &num, &unit))
1592 ret++;
1593 pf.dxOffset = units_to_twips(unit, num);
1594 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1595 if(number_from_string(buffer, &num, &unit))
1596 ret++;
1597 pf.dxRightIndent = units_to_twips(unit, num);
1598 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1599 if(number_from_string(buffer, &num, &unit))
1600 ret++;
1601 pf.dxStartIndent = units_to_twips(unit, num);
1603 if(ret != 3)
1605 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1606 wszAppTitle, MB_OK | MB_ICONASTERISK);
1607 return FALSE;
1608 } else
1610 if (pf.dxOffset + pf.dxStartIndent < 0
1611 && pf.dxStartIndent < 0)
1613 /* The first line is before the left edge, so
1614 * make sure it is at the left edge. */
1615 pf.dxOffset = -pf.dxStartIndent;
1616 } else if (pf.dxOffset < 0) {
1617 /* The second and following lines are before
1618 * the left edge, so set it to be at the left
1619 * edge, and adjust the first line since it
1620 * is relative to it. */
1621 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1622 pf.dxOffset = 0;
1624 /* Internally the dxStartIndent is the absolute
1625 * offset for the first line and dxOffset is
1626 * to it value as opposed how it is displayed with
1627 * the first line being the relative value.
1628 * These two lines make the adjustments. */
1629 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1630 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1632 pf.cbSize = sizeof(pf);
1633 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1634 PFM_STARTINDENT;
1635 if (in_list)
1637 pf.wNumberingTab = max(pf.dxOffset, 0);
1638 pf.dwMask |= PFM_NUMBERINGTAB;
1641 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1644 /* Fall through */
1646 case IDCANCEL:
1647 EndDialog(hWnd, wParam);
1648 return TRUE;
1651 return FALSE;
1654 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1656 switch(message)
1658 case WM_INITDIALOG:
1660 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1661 PARAFORMAT pf;
1662 WCHAR buffer[MAX_STRING_LEN];
1663 int i;
1665 pf.cbSize = sizeof(pf);
1666 pf.dwMask = PFM_TABSTOPS;
1667 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1668 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1670 for(i = 0; i < pf.cTabCount; i++)
1672 number_with_units(buffer, pf.rgxTabs[i]);
1673 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1675 SetFocus(hTabWnd);
1677 break;
1679 case WM_COMMAND:
1680 switch(LOWORD(wParam))
1682 case IDC_TABSTOPS:
1684 HWND hTabWnd = (HWND)lParam;
1685 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1686 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1687 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1689 if(GetWindowTextLengthW(hTabWnd))
1690 EnableWindow(hAddWnd, TRUE);
1691 else
1692 EnableWindow(hAddWnd, FALSE);
1694 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1696 EnableWindow(hEmptyWnd, TRUE);
1698 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1699 EnableWindow(hDelWnd, FALSE);
1700 else
1701 EnableWindow(hDelWnd, TRUE);
1702 } else
1704 EnableWindow(hEmptyWnd, FALSE);
1707 break;
1709 case ID_TAB_ADD:
1711 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1712 WCHAR buffer[MAX_STRING_LEN];
1713 UNIT unit;
1715 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1716 append_current_units(buffer);
1718 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1720 float number = 0;
1721 int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
1723 if(!number_from_string(buffer, &number, &unit))
1725 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1726 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1727 } else if (item_count >= MAX_TAB_STOPS) {
1728 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1729 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1730 } else {
1731 int i;
1732 float next_number = -1;
1733 int next_number_in_twips = -1;
1734 int insert_number = units_to_twips(unit, number);
1736 /* linear search for position to insert the string */
1737 for(i = 0; i < item_count; i++)
1739 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1740 number_from_string(buffer, &next_number, &unit);
1741 next_number_in_twips = units_to_twips(unit, next_number);
1742 if (insert_number <= next_number_in_twips)
1743 break;
1745 if (insert_number != next_number_in_twips)
1747 number_with_units(buffer, insert_number);
1748 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1749 SetWindowTextW(hTabWnd, 0);
1753 SetFocus(hTabWnd);
1755 break;
1757 case ID_TAB_DEL:
1759 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1760 LRESULT ret;
1761 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1762 if(ret != CB_ERR)
1763 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1765 break;
1767 case ID_TAB_EMPTY:
1769 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1770 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1771 SetFocus(hTabWnd);
1773 break;
1775 case IDOK:
1777 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1778 int i;
1779 WCHAR buffer[MAX_STRING_LEN];
1780 PARAFORMAT pf;
1781 float number;
1782 UNIT unit;
1784 pf.cbSize = sizeof(pf);
1785 pf.dwMask = PFM_TABSTOPS;
1787 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1788 (LPARAM)&buffer) != CB_ERR &&
1789 i < MAX_TAB_STOPS; i++)
1791 number_from_string(buffer, &number, &unit);
1792 pf.rgxTabs[i] = units_to_twips(unit, number);
1794 pf.cTabCount = i;
1795 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1797 /* Fall through */
1798 case IDCANCEL:
1799 EndDialog(hWnd, wParam);
1800 return TRUE;
1803 return FALSE;
1806 static LRESULT OnCreate( HWND hWnd )
1808 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1809 HINSTANCE hInstance = GetModuleHandleW(0);
1810 HANDLE hDLL;
1811 TBADDBITMAP ab;
1812 int nStdBitmaps = 0;
1813 REBARINFO rbi;
1814 REBARBANDINFOW rbb;
1815 RECT rect;
1816 HFONT font;
1817 HDC hdc;
1818 SIZE name_sz, size_sz;
1819 int height;
1820 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1821 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1822 static const WCHAR font_text[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0}; /* a long font name */
1823 static const WCHAR size_text[] = {' ','0','0',0}; /* enough for two digits */
1825 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1827 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1828 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1829 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1831 rbi.cbSize = sizeof(rbi);
1832 rbi.fMask = 0;
1833 rbi.himl = NULL;
1834 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1835 return -1;
1837 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS,
1838 IDC_TOOLBAR,
1839 1, hInstance, IDB_TOOLBAR,
1840 NULL, 0,
1841 24, 24, 16, 16, sizeof(TBBUTTON));
1843 ab.hInst = HINST_COMMCTRL;
1844 ab.nID = IDB_STD_SMALL_COLOR;
1845 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1847 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1848 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1849 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1850 AddSeparator(hToolBarWnd);
1851 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1852 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1853 AddSeparator(hToolBarWnd);
1854 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1855 AddSeparator(hToolBarWnd);
1856 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1857 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1858 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1859 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1860 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1861 AddSeparator(hToolBarWnd);
1862 AddButton(hToolBarWnd, 0, ID_DATETIME);
1864 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1865 height = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1867 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1868 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1869 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1870 GetWindowRect(hFontListWnd, &rect);
1871 height = max(height, rect.bottom - rect.top);
1873 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1874 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1875 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1876 rbb.cx = 0;
1877 rbb.hwndChild = hToolBarWnd;
1878 rbb.cxMinChild = 0;
1879 rbb.cyChild = rbb.cyMinChild = height;
1880 rbb.wID = BANDID_TOOLBAR;
1882 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1884 font = (HFONT)SendMessageW(hFontListWnd, WM_GETFONT, 0, 0);
1885 hdc = GetDC(hFontListWnd);
1886 font = SelectObject(hdc, font);
1887 GetTextExtentPointW(hdc, font_text, ARRAY_SIZE(font_text) - 1, &name_sz);
1888 GetTextExtentPointW(hdc, size_text, ARRAY_SIZE(size_text) - 1, &size_sz);
1889 font = SelectObject(hdc, font);
1890 ReleaseDC(hFontListWnd, hdc);
1891 rbb.hwndChild = hFontListWnd;
1892 rbb.cx = MulDiv(name_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1893 rbb.wID = BANDID_FONTLIST;
1895 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1897 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1898 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1899 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1901 rbb.hwndChild = hSizeListWnd;
1902 rbb.cx = MulDiv(size_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1903 rbb.fStyle ^= RBBS_BREAK;
1904 rbb.wID = BANDID_SIZELIST;
1906 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1908 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1909 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS,
1910 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1912 SendMessageW(hFormatBarWnd, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
1914 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1915 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1916 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1917 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1918 AddSeparator(hFormatBarWnd);
1919 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1920 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1921 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1922 AddSeparator(hFormatBarWnd);
1923 AddButtonStyle(hFormatBarWnd, 7, ID_BULLETONOFF, BTNS_DROPDOWN);
1925 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1927 rbb.hwndChild = hFormatBarWnd;
1928 rbb.wID = BANDID_FORMATBAR;
1930 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1932 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1933 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1936 rbb.hwndChild = hRulerWnd;
1937 rbb.wID = BANDID_RULER;
1938 rbb.fStyle |= RBBS_BREAK;
1940 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1942 hDLL = LoadLibraryW(wszRichEditDll);
1943 if(!hDLL)
1945 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1946 MB_OK | MB_ICONEXCLAMATION);
1947 PostQuitMessage(1);
1950 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1951 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1952 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1953 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1955 if (!hEditorWnd)
1957 fprintf(stderr, "Error code %u\n", GetLastError());
1958 return -1;
1960 assert(hEditorWnd);
1962 setup_richedit_olecallback(hEditorWnd);
1963 SetFocus(hEditorWnd);
1964 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1966 set_default_font();
1968 populate_font_list(hFontListWnd);
1969 populate_size_list(hSizeListWnd);
1970 DoLoadStrings();
1971 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1973 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1975 registry_read_filelist(hWnd);
1976 registry_read_formatopts_all(barState, wordWrap);
1977 registry_read_options();
1978 DragAcceptFiles(hWnd, TRUE);
1980 return 0;
1983 static LRESULT OnUser( HWND hWnd )
1985 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1986 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1987 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1988 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1989 int from, to;
1990 CHARFORMAT2W fmt;
1991 PARAFORMAT2 pf;
1992 GETTEXTLENGTHEX gt;
1994 ZeroMemory(&fmt, sizeof(fmt));
1995 fmt.cbSize = sizeof(fmt);
1997 ZeroMemory(&pf, sizeof(pf));
1998 pf.cbSize = sizeof(pf);
2000 gt.flags = GTL_NUMCHARS;
2001 gt.codepage = 1200;
2003 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
2004 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
2006 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
2008 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2009 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
2010 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
2011 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
2012 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
2013 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
2014 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2016 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2017 (fmt.dwEffects & CFE_BOLD));
2018 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2019 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2020 (fmt.dwEffects & CFE_ITALIC));
2021 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2022 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2023 (fmt.dwEffects & CFE_UNDERLINE));
2024 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2026 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2027 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2028 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2029 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2031 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLETONOFF, pf.wNumbering != 0);
2032 return 0;
2035 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2037 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2038 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2039 NMHDR *pHdr = (NMHDR *)lParam;
2040 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2041 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2042 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2044 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2046 if (pHdr->code == CBEN_ENDEDITW)
2048 NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
2049 if(pHdr->hwndFrom == hwndFontList)
2051 on_fontlist_modified(endEdit->szText);
2052 } else if (pHdr->hwndFrom == hwndSizeList)
2054 on_sizelist_modified(hwndSizeList,endEdit->szText);
2057 return 0;
2060 if (pHdr->hwndFrom == hwndFormatBar)
2062 if (pHdr->code == TBN_DROPDOWN)
2064 NMTOOLBARW *tb_notify = (NMTOOLBARW *)lParam;
2065 HMENU menu = GetMenu( hWnd );
2066 MENUITEMINFOW info;
2067 TPMPARAMS params;
2068 RECT rc;
2070 if (!menu) return 0;
2071 info.cbSize = sizeof(info);
2072 info.fMask = MIIM_SUBMENU;
2073 GetMenuItemInfoW( menu, ID_LISTMENU, FALSE, &info );
2074 if (!info.hSubMenu) return 0;
2076 SendMessageW( tb_notify->hdr.hwndFrom, TB_GETRECT, (WPARAM)tb_notify->iItem, (LPARAM)&rc );
2077 MapWindowPoints( tb_notify->hdr.hwndFrom, HWND_DESKTOP, (LPPOINT)&rc, 2 );
2079 params.cbSize = sizeof(params);
2080 params.rcExclude = rc;
2081 TrackPopupMenuEx( info.hSubMenu,
2082 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
2083 rc.left, rc.bottom, hWnd, &params );
2086 return 0;
2089 if (pHdr->hwndFrom == hwndEditor)
2091 if (pHdr->code == EN_SELCHANGE)
2093 SELCHANGE *pSC = (SELCHANGE *)lParam;
2094 char buf[128];
2096 update_font_list();
2098 sprintf( buf,"selection = %d..%d, line count=%ld",
2099 pSC->chrg.cpMin, pSC->chrg.cpMax,
2100 SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
2101 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2102 SendMessageW(hWnd, WM_USER, 0, 0);
2103 return 1;
2106 return 0;
2109 /* Copied from dlls/comdlg32/fontdlg.c */
2110 static const COLORREF textcolors[]=
2112 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2113 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2114 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2115 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2118 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2120 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2121 static FINDREPLACEW findreplace;
2123 if ((HWND)lParam == hwndEditor)
2124 return 0;
2126 switch(LOWORD(wParam))
2129 case ID_FILE_EXIT:
2130 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2131 break;
2133 case ID_FILE_NEW:
2135 HINSTANCE hInstance = GetModuleHandleW(0);
2136 int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
2138 if(ret != ID_NEWFILE_ABORT)
2140 if(prompt_save_changes())
2142 SETTEXTEX st;
2144 set_caption(NULL);
2145 wszFileName[0] = '\0';
2147 clear_formatting();
2149 st.flags = ST_DEFAULT;
2150 st.codepage = 1200;
2151 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2153 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2154 set_fileformat(ret);
2155 update_font_list();
2159 break;
2161 case ID_FILE_OPEN:
2162 DialogOpenFile();
2163 break;
2165 case ID_FILE_SAVE:
2166 if(wszFileName[0])
2168 DoSaveFile(wszFileName, fileFormat);
2169 break;
2171 /* Fall through */
2173 case ID_FILE_SAVEAS:
2174 DialogSaveFile();
2175 break;
2177 case ID_FILE_RECENT1:
2178 case ID_FILE_RECENT2:
2179 case ID_FILE_RECENT3:
2180 case ID_FILE_RECENT4:
2182 HMENU hMenu = GetMenu(hWnd);
2183 MENUITEMINFOW mi;
2185 mi.cbSize = sizeof(MENUITEMINFOW);
2186 mi.fMask = MIIM_DATA;
2187 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2188 DoOpenFile((LPWSTR)mi.dwItemData);
2190 break;
2192 case ID_FIND:
2193 dialog_find(&findreplace, FALSE);
2194 break;
2196 case ID_FIND_NEXT:
2197 handle_findmsg(&findreplace);
2198 break;
2200 case ID_REPLACE:
2201 dialog_find(&findreplace, TRUE);
2202 break;
2204 case ID_FONTSETTINGS:
2205 dialog_choose_font();
2206 break;
2208 case ID_PRINT:
2209 dialog_print(hWnd, wszFileName);
2210 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2211 break;
2213 case ID_PRINT_QUICK:
2214 print_quick(hMainWnd, wszFileName);
2215 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2216 break;
2218 case ID_PREVIEW:
2220 int index = reg_formatindex(fileFormat);
2221 DWORD tmp = barState[index];
2222 barState[index] = 1 << BANDID_STATUSBAR;
2223 set_bar_states();
2224 barState[index] = tmp;
2225 ShowWindow(hEditorWnd, FALSE);
2227 init_preview(hWnd, wszFileName);
2229 SetMenu(hWnd, NULL);
2230 InvalidateRect(0, 0, TRUE);
2232 break;
2234 case ID_PRINTSETUP:
2235 dialog_printsetup(hWnd);
2236 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2237 break;
2239 case ID_FORMAT_BOLD:
2240 case ID_FORMAT_ITALIC:
2241 case ID_FORMAT_UNDERLINE:
2243 CHARFORMAT2W fmt;
2244 int effects = CFE_BOLD;
2246 ZeroMemory(&fmt, sizeof(fmt));
2247 fmt.cbSize = sizeof(fmt);
2248 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2250 fmt.dwMask = CFM_BOLD;
2252 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2254 effects = CFE_ITALIC;
2255 fmt.dwMask = CFM_ITALIC;
2256 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2258 effects = CFE_UNDERLINE;
2259 fmt.dwMask = CFM_UNDERLINE;
2262 fmt.dwEffects ^= effects;
2264 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2265 break;
2268 case ID_FORMAT_COLOR:
2270 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2271 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2272 HMENU hPop;
2273 RECT itemrc;
2274 POINT pt;
2275 int mid;
2276 int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2278 SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2279 pt.x = itemrc.left;
2280 pt.y = itemrc.bottom;
2281 ClientToScreen(hFormatBarWnd, &pt);
2282 hPop = GetSubMenu(hColorPopupMenu, 0);
2283 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2284 TPM_RETURNCMD | TPM_NONOTIFY,
2285 pt.x, pt.y, 0, hWnd, 0);
2286 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2288 CHARFORMAT2W fmt;
2290 ZeroMemory(&fmt, sizeof(fmt));
2291 fmt.cbSize = sizeof(fmt);
2292 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2294 fmt.dwMask = CFM_COLOR;
2296 if (mid < ID_COLOR_AUTOMATIC) {
2297 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2298 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2299 } else {
2300 fmt.dwEffects |= CFE_AUTOCOLOR;
2303 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2305 break;
2308 case ID_EDIT_CUT:
2309 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2310 break;
2312 case ID_EDIT_COPY:
2313 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2314 break;
2316 case ID_EDIT_PASTE:
2317 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2318 break;
2320 case ID_EDIT_CLEAR:
2321 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2322 break;
2324 case ID_EDIT_SELECTALL:
2326 CHARRANGE range = {0, -1};
2327 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2328 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2329 return 0;
2332 case ID_EDIT_GETTEXT:
2334 int nLen = GetWindowTextLengthW(hwndEditor);
2335 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2336 TEXTRANGEW tr;
2338 GetWindowTextW(hwndEditor, data, nLen+1);
2339 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2341 HeapFree( GetProcessHeap(), 0, data);
2342 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2343 tr.chrg.cpMin = 0;
2344 tr.chrg.cpMax = nLen;
2345 tr.lpstrText = data;
2346 SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2347 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2348 HeapFree( GetProcessHeap(), 0, data );
2350 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2351 return 0;
2354 case ID_EDIT_CHARFORMAT:
2355 case ID_EDIT_DEFCHARFORMAT:
2357 CHARFORMAT2W cf;
2359 ZeroMemory(&cf, sizeof(cf));
2360 cf.cbSize = sizeof(cf);
2361 cf.dwMask = 0;
2362 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2363 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2364 return 0;
2367 case ID_EDIT_PARAFORMAT:
2369 PARAFORMAT2 pf;
2370 ZeroMemory(&pf, sizeof(pf));
2371 pf.cbSize = sizeof(pf);
2372 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2373 return 0;
2376 case ID_EDIT_SELECTIONINFO:
2378 CHARRANGE range = {0, -1};
2379 char buf[128];
2380 WCHAR *data = NULL;
2382 SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2383 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2384 SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2385 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2386 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2387 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2388 HeapFree( GetProcessHeap(), 0, data);
2389 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2390 return 0;
2393 case ID_EDIT_READONLY:
2395 LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
2396 if (nStyle & ES_READONLY)
2397 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2398 else
2399 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2400 return 0;
2403 case ID_EDIT_MODIFIED:
2404 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2405 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2406 else
2407 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2408 return 0;
2410 case ID_EDIT_UNDO:
2411 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2412 return 0;
2414 case ID_EDIT_REDO:
2415 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2416 return 0;
2418 case ID_BULLETONOFF:
2419 case ID_BULLET:
2420 case ID_NUMBERING:
2421 case ID_LCLETTER:
2422 case ID_UCLETTER:
2423 case ID_LCROMAN:
2424 case ID_UCROMAN:
2426 PARAFORMAT2 pf;
2427 WORD new_number = LOWORD(wParam) - ID_BULLET + PFN_BULLET;
2428 pf.cbSize = sizeof(pf);
2429 pf.dwMask = PFM_NUMBERING;
2430 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2432 pf.dwMask = PFM_NUMBERING | PFM_NUMBERINGSTART | PFM_NUMBERINGSTYLE | PFM_NUMBERINGTAB | PFM_OFFSET | PFM_OFFSETINDENT;
2434 if(pf.wNumbering && ((pf.wNumbering == new_number) || (LOWORD(wParam) == ID_BULLETONOFF)))
2436 pf.wNumbering = 0;
2437 pf.wNumberingStart = 0;
2438 pf.wNumberingStyle = 0;
2439 pf.wNumberingTab = 0;
2440 pf.dxOffset = 0;
2441 pf.dxStartIndent = -360;
2442 } else
2444 pf.dxStartIndent = pf.wNumbering ? 0 : 360;
2446 if (LOWORD(wParam) == ID_BULLETONOFF)
2447 pf.wNumbering = last_bullet;
2448 else
2450 pf.wNumbering = new_number;
2451 last_bullet = pf.wNumbering;
2453 pf.wNumberingStart = 1;
2454 pf.wNumberingStyle = PFNS_PERIOD;
2455 pf.wNumberingTab = 360;
2456 pf.dxOffset = 360;
2459 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2461 break;
2463 case ID_ALIGN_LEFT:
2464 case ID_ALIGN_CENTER:
2465 case ID_ALIGN_RIGHT:
2467 PARAFORMAT2 pf;
2469 pf.cbSize = sizeof(pf);
2470 pf.dwMask = PFM_ALIGNMENT;
2471 switch(LOWORD(wParam)) {
2472 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2473 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2474 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2476 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2477 break;
2480 case ID_BACK_1:
2481 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2482 break;
2484 case ID_BACK_2:
2485 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2486 break;
2488 case ID_TOGGLE_TOOLBAR:
2489 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2490 update_window();
2491 break;
2493 case ID_TOGGLE_FORMATBAR:
2494 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2495 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2496 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2497 update_window();
2498 break;
2500 case ID_TOGGLE_STATUSBAR:
2501 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2502 update_window();
2503 break;
2505 case ID_TOGGLE_RULER:
2506 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2507 update_window();
2508 break;
2510 case ID_DATETIME:
2511 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2512 break;
2514 case ID_PARAFORMAT:
2515 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2516 break;
2518 case ID_TABSTOPS:
2519 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2520 break;
2522 case ID_ABOUT:
2523 dialog_about();
2524 break;
2526 case ID_VIEWPROPERTIES:
2527 dialog_viewproperties();
2528 break;
2530 case IDC_FONTLIST:
2531 if (HIWORD(wParam) == CBN_SELENDOK)
2533 WCHAR buffer[LF_FACESIZE];
2534 HWND hwndFontList = (HWND)lParam;
2535 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2536 on_fontlist_modified(buffer);
2538 break;
2540 case IDC_SIZELIST:
2541 if (HIWORD(wParam) == CBN_SELENDOK)
2543 WCHAR buffer[MAX_STRING_LEN+1];
2544 HWND hwndSizeList = (HWND)lParam;
2545 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2546 on_sizelist_modified(hwndSizeList, buffer);
2548 break;
2550 default:
2551 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2552 break;
2554 return 0;
2557 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2559 HMENU hMenu = (HMENU)wParam;
2560 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2561 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2562 PARAFORMAT pf;
2563 int nAlignment = -1;
2564 int selFrom, selTo;
2565 GETTEXTLENGTHEX gt;
2566 LRESULT textLength;
2567 MENUITEMINFOW mi;
2569 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2570 EnableMenuItem(hMenu, ID_EDIT_COPY, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2571 EnableMenuItem(hMenu, ID_EDIT_CUT, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2573 pf.cbSize = sizeof(PARAFORMAT);
2574 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2575 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2576 (GetWindowLongW(hwndEditor, GWL_STYLE) & ES_READONLY) ? MF_CHECKED : MF_UNCHECKED);
2577 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2578 SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED);
2579 if (pf.dwMask & PFM_ALIGNMENT)
2580 nAlignment = pf.wAlignment;
2581 CheckMenuItem(hMenu, ID_ALIGN_LEFT, (nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
2582 CheckMenuItem(hMenu, ID_ALIGN_CENTER, (nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
2583 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, (nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
2585 CheckMenuItem(hMenu, ID_BULLET, ((pf.wNumbering == PFN_BULLET) ? MF_CHECKED : MF_UNCHECKED));
2586 CheckMenuItem(hMenu, ID_NUMBERING, ((pf.wNumbering == PFN_ARABIC) ? MF_CHECKED : MF_UNCHECKED));
2587 CheckMenuItem(hMenu, ID_LCLETTER, ((pf.wNumbering == PFN_LCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2588 CheckMenuItem(hMenu, ID_UCLETTER, ((pf.wNumbering == PFN_UCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2589 CheckMenuItem(hMenu, ID_LCROMAN, ((pf.wNumbering == PFN_LCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2590 CheckMenuItem(hMenu, ID_UCROMAN, ((pf.wNumbering == PFN_UCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2592 EnableMenuItem(hMenu, ID_EDIT_UNDO, SendMessageW(hwndEditor, EM_CANUNDO, 0, 0) ?
2593 MF_ENABLED : MF_GRAYED);
2594 EnableMenuItem(hMenu, ID_EDIT_REDO, SendMessageW(hwndEditor, EM_CANREDO, 0, 0) ?
2595 MF_ENABLED : MF_GRAYED);
2597 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, is_bar_visible(BANDID_TOOLBAR) ?
2598 MF_CHECKED : MF_UNCHECKED);
2600 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, is_bar_visible(BANDID_FORMATBAR) ?
2601 MF_CHECKED : MF_UNCHECKED);
2603 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, IsWindowVisible(hwndStatus) ?
2604 MF_CHECKED : MF_UNCHECKED);
2606 CheckMenuItem(hMenu, ID_TOGGLE_RULER, is_bar_visible(BANDID_RULER) ? MF_CHECKED : MF_UNCHECKED);
2608 gt.flags = GTL_NUMCHARS;
2609 gt.codepage = 1200;
2610 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2611 EnableMenuItem(hMenu, ID_FIND, textLength ? MF_ENABLED : MF_GRAYED);
2613 mi.cbSize = sizeof(mi);
2614 mi.fMask = MIIM_DATA;
2616 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2618 EnableMenuItem(hMenu, ID_FIND_NEXT, (textLength && mi.dwItemData) ? MF_ENABLED : MF_GRAYED);
2620 EnableMenuItem(hMenu, ID_REPLACE, textLength ? MF_ENABLED : MF_GRAYED);
2622 return 0;
2625 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2627 int nStatusSize = 0;
2628 RECT rc;
2629 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2630 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2631 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2632 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2633 int rebarHeight = 0;
2635 if (hwndStatusBar)
2637 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2638 if (IsWindowVisible(hwndStatusBar))
2640 GetClientRect(hwndStatusBar, &rc);
2641 nStatusSize = rc.bottom - rc.top;
2642 } else
2644 nStatusSize = 0;
2647 if (hwndReBar)
2649 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2651 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2653 if (hwndEditor)
2655 GetClientRect(hWnd, &rc);
2656 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2659 redraw_ruler(hRulerWnd);
2661 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2664 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2666 if(msg == ID_FINDMSGSTRING)
2667 return handle_findmsg((LPFINDREPLACEW)lParam);
2669 switch(msg)
2671 case WM_CREATE:
2672 return OnCreate( hWnd );
2674 case WM_USER:
2675 return OnUser( hWnd );
2677 case WM_NOTIFY:
2678 return OnNotify( hWnd, lParam );
2680 case WM_COMMAND:
2681 if(preview_isactive())
2683 return preview_command( hWnd, wParam );
2686 return OnCommand( hWnd, wParam, lParam );
2688 case WM_DESTROY:
2689 PostQuitMessage(0);
2690 break;
2692 case WM_CLOSE:
2693 if(preview_isactive())
2695 preview_exit(hWnd);
2696 } else if(prompt_save_changes())
2698 registry_set_options(hMainWnd);
2699 registry_set_formatopts_all(barState, wordWrap);
2700 PostQuitMessage(0);
2702 break;
2704 case WM_ACTIVATE:
2705 if (LOWORD(wParam))
2706 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2707 return 0;
2709 case WM_INITMENUPOPUP:
2710 return OnInitPopupMenu( hWnd, wParam );
2712 case WM_SIZE:
2713 return OnSize( hWnd, wParam, lParam );
2715 case WM_CONTEXTMENU:
2716 return DefWindowProcW(hWnd, msg, wParam, lParam);
2718 case WM_DROPFILES:
2720 WCHAR file[MAX_PATH];
2721 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2722 DragFinish((HDROP)wParam);
2724 if(prompt_save_changes())
2725 DoOpenFile(file);
2727 break;
2728 case WM_PAINT:
2729 if(!preview_isactive())
2730 return DefWindowProcW(hWnd, msg, wParam, lParam);
2732 default:
2733 return DefWindowProcW(hWnd, msg, wParam, lParam);
2736 return 0;
2739 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2741 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2742 HACCEL hAccel;
2743 WNDCLASSEXW wc;
2744 MSG msg;
2745 RECT rc;
2746 UINT_PTR hPrevRulerProc;
2747 HWND hRulerWnd;
2748 POINTL EditPoint;
2749 DWORD bMaximized;
2750 MONITORINFO info;
2751 HMONITOR monitor;
2752 int x, y;
2753 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2754 'T','A','B','L','E','\0'};
2756 InitCommonControlsEx(&classes);
2758 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2760 wc.cbSize = sizeof(wc);
2761 wc.style = 0;
2762 wc.lpfnWndProc = WndProc;
2763 wc.cbClsExtra = 0;
2764 wc.cbWndExtra = 4;
2765 wc.hInstance = hInstance;
2766 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2767 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2768 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2769 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2770 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2771 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2772 wc.lpszClassName = wszMainWndClass;
2773 RegisterClassExW(&wc);
2775 wc.style = 0;
2776 wc.lpfnWndProc = preview_proc;
2777 wc.cbClsExtra = 0;
2778 wc.cbWndExtra = 0;
2779 wc.hInstance = hInstance;
2780 wc.hIcon = NULL;
2781 wc.hIconSm = NULL;
2782 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2783 wc.hbrBackground = NULL;
2784 wc.lpszMenuName = NULL;
2785 wc.lpszClassName = wszPreviewWndClass;
2786 RegisterClassExW(&wc);
2788 registry_read_winrect(&rc);
2789 monitor = MonitorFromRect(&rc, MONITOR_DEFAULTTOPRIMARY);
2790 info.cbSize = sizeof(info);
2791 GetMonitorInfoW(monitor, &info);
2793 x = rc.left;
2794 y = rc.top;
2795 IntersectRect(&info.rcWork, &info.rcWork, &rc);
2796 if (IsRectEmpty(&info.rcWork))
2797 x = y = CW_USEDEFAULT;
2799 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2800 x, y, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
2801 registry_read_maximized(&bMaximized);
2802 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2803 && bMaximized)
2804 nCmdShow = SW_SHOWMAXIMIZED;
2805 ShowWindow(hMainWnd, nCmdShow);
2807 set_caption(NULL);
2808 set_bar_states();
2809 set_fileformat(SF_RTF);
2810 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2811 get_default_printer_opts();
2812 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2814 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2815 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2816 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2817 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2819 HandleCommandLine(GetCommandLineW());
2821 while(GetMessageW(&msg,0,0,0))
2823 if (IsDialogMessageW(hFindWnd, &msg))
2824 continue;
2826 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2827 continue;
2828 TranslateMessage(&msg);
2829 DispatchMessageW(&msg);
2830 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2831 SendMessageW(hMainWnd, WM_USER, 0, 0);
2834 return 0;