imm32: Use a stack allocated buffer in ImmTranslateMessage.
[wine.git] / programs / wordpad / wordpad.c
blob4a3aa2375bfea17b664523ace82d927e28b336fa
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
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <assert.h>
30 #include <windows.h>
31 #include <richedit.h>
32 #include <commctrl.h>
33 #include <commdlg.h>
34 #include <shellapi.h>
35 #include <math.h>
36 #include <errno.h>
38 #include "wordpad.h"
40 #ifdef NONAMELESSUNION
41 # define U(x) (x).u
42 # define U2(x) (x).u2
43 # define U3(x) (x).u3
44 #else
45 # define U(x) (x)
46 # define U2(x) (x)
47 # define U3(x) (x)
48 #endif
50 /* use LoadString */
51 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
53 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
55 static const WCHAR stringFormat[] = {'%','2','d','\0'};
57 const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
58 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
60 static HWND hMainWnd;
61 static HWND hEditorWnd;
62 static HWND hFindWnd;
63 static HMENU hColorPopupMenu;
65 static UINT ID_FINDMSGSTRING;
67 static DWORD wordWrap[2];
68 static DWORD barState[2];
69 static WPARAM fileFormat = SF_RTF;
71 static WCHAR wszFileName[MAX_PATH];
72 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
73 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
74 static WCHAR wszSaveChanges[MAX_STRING_LEN];
75 static WCHAR units_cmW[MAX_STRING_LEN];
76 static WCHAR units_inW[MAX_STRING_LEN];
77 static WCHAR units_inchW[MAX_STRING_LEN];
78 static WCHAR units_ptW[MAX_STRING_LEN];
80 static int last_bullet = PFN_BULLET;
82 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
84 typedef enum
86 UNIT_CM,
87 UNIT_INCH,
88 UNIT_PT
89 } UNIT;
91 typedef struct
93 int endPos;
94 BOOL wrapped;
95 WCHAR findBuffer[128];
96 } FINDREPLACE_custom;
98 /* Load string resources */
99 static void DoLoadStrings(void)
101 LPWSTR p = wszFilter;
102 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
103 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
104 static const WCHAR files_all[] = {'*','.','*','\0'};
106 HINSTANCE hInstance = GetModuleHandleW(0);
108 p += 1 + LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
109 lstrcpyW(p, files_rtf);
110 p += lstrlenW(p) + 1;
111 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
112 lstrcpyW(p, files_txt);
113 p += lstrlenW(p) + 1;
114 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
115 lstrcpyW(p, files_txt);
116 p += lstrlenW(p) + 1;
117 p += 1 + LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
118 lstrcpyW(p, files_all);
119 p += lstrlenW(p) + 1;
120 *p = '\0';
122 p = wszDefaultFileName;
123 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
125 p = wszSaveChanges;
126 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
128 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
129 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
130 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
131 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
134 /* Show a message box with resource strings */
135 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
137 MSGBOXPARAMSW params;
139 params.cbSize = sizeof(params);
140 params.hwndOwner = hWnd;
141 params.hInstance = GetModuleHandleW(0);
142 params.lpszText = lpText;
143 params.lpszCaption = lpCaption;
144 params.dwStyle = uType;
145 params.lpszIcon = NULL;
146 params.dwContextHelpId = 0;
147 params.lpfnMsgBoxCallback = NULL;
148 params.dwLanguageId = 0;
149 return MessageBoxIndirectW(&params);
153 static void AddButtonStyle(HWND hwndToolBar, int nImage, int nCommand, BYTE style)
155 TBBUTTON button;
157 ZeroMemory(&button, sizeof(button));
158 button.iBitmap = nImage;
159 button.idCommand = nCommand;
160 button.fsState = TBSTATE_ENABLED;
161 button.fsStyle = style;
162 button.dwData = 0;
163 button.iString = -1;
164 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
167 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
169 AddButtonStyle(hwndToolBar, nImage, nCommand, BTNS_BUTTON);
172 static void AddSeparator(HWND hwndToolBar)
174 TBBUTTON button;
176 ZeroMemory(&button, sizeof(button));
177 button.iBitmap = -1;
178 button.idCommand = 0;
179 button.fsState = 0;
180 button.fsStyle = BTNS_SEP;
181 button.dwData = 0;
182 button.iString = -1;
183 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
186 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
188 HANDLE hFile = (HANDLE)cookie;
189 DWORD read;
191 if(!ReadFile(hFile, buffer, cb, &read, 0))
192 return 1;
194 *pcb = read;
196 return 0;
199 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
201 DWORD written;
202 int ret;
203 HANDLE hFile = (HANDLE)cookie;
205 ret = WriteFile(hFile, buffer, cb, &written, 0);
207 if(!ret || (cb != written))
208 return 1;
210 *pcb = cb;
212 return 0;
215 LPWSTR file_basename(LPWSTR path)
217 LPWSTR pos = path + lstrlenW(path);
219 while(pos > path)
221 if(*pos == '\\' || *pos == '/')
223 pos++;
224 break;
226 pos--;
228 return pos;
231 static void set_caption(LPCWSTR wszNewFileName)
233 static const WCHAR wszSeparator[] = {' ','-',' '};
234 WCHAR *wszCaption;
235 SIZE_T length = 0;
237 if(!wszNewFileName)
238 wszNewFileName = wszDefaultFileName;
239 else
240 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
242 wszCaption = calloc(1, lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
244 if(!wszCaption)
245 return;
247 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
248 length += lstrlenW(wszNewFileName);
249 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
250 length += ARRAY_SIZE(wszSeparator);
251 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
253 SetWindowTextW(hMainWnd, wszCaption);
255 free(wszCaption);
258 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
260 if(punit != NULL)
261 *punit = UNIT_CM;
262 if(!endptr)
263 return FALSE;
264 if(!*endptr)
265 return TRUE;
267 while(*endptr == ' ')
268 endptr++;
270 if(punit == NULL)
271 return *endptr == '\0';
273 if(!lstrcmpW(endptr, units_cmW))
275 *punit = UNIT_CM;
276 endptr += lstrlenW(units_cmW);
278 else if (!lstrcmpW(endptr, units_inW))
280 *punit = UNIT_INCH;
281 endptr += lstrlenW(units_inW);
283 else if (!lstrcmpW(endptr, units_inchW))
285 *punit = UNIT_INCH;
286 endptr += lstrlenW(units_inchW);
288 else if (!lstrcmpW(endptr, units_ptW))
290 *punit = UNIT_PT;
291 endptr += lstrlenW(units_ptW);
294 return *endptr == '\0';
297 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
299 double ret;
300 WCHAR *endptr;
302 *num = 0;
303 errno = 0;
304 ret = wcstod(string, &endptr);
306 if (punit != NULL)
307 *punit = UNIT_CM;
308 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
310 return FALSE;
311 } else
313 *num = (float)ret;
314 return TRUE;
318 static void set_size(float size)
320 CHARFORMAT2W fmt;
322 ZeroMemory(&fmt, sizeof(fmt));
323 fmt.cbSize = sizeof(fmt);
324 fmt.dwMask = CFM_SIZE;
325 fmt.yHeight = (int)(size * 20.0);
326 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
329 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
331 WCHAR sizeBuffer[MAX_STRING_LEN];
332 CHARFORMAT2W format;
334 ZeroMemory(&format, sizeof(format));
335 format.cbSize = sizeof(format);
336 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
338 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
339 if(lstrcmpW(sizeBuffer, wszNewFontSize))
341 float size = 0;
342 if(number_from_string(wszNewFontSize, &size, NULL)
343 && size > 0)
345 set_size(size);
346 } else
348 SetWindowTextW(hwndSizeList, sizeBuffer);
349 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
350 wszAppTitle, MB_OK | MB_ICONINFORMATION);
355 static void add_size(HWND hSizeListWnd, unsigned size)
357 WCHAR buffer[3];
358 COMBOBOXEXITEMW cbItem;
359 cbItem.mask = CBEIF_TEXT;
360 cbItem.iItem = -1;
362 wsprintfW(buffer, stringFormat, size);
363 cbItem.pszText = buffer;
364 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
367 static void populate_size_list(HWND hSizeListWnd)
369 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
370 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
371 COMBOBOXEXITEMW cbFontItem;
372 CHARFORMAT2W fmt;
373 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
374 HDC hdc = GetDC(hMainWnd);
375 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
376 WCHAR buffer[3];
377 size_t i;
378 DWORD fontStyle;
380 ZeroMemory(&fmt, sizeof(fmt));
381 fmt.cbSize = sizeof(fmt);
382 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
384 cbFontItem.mask = CBEIF_LPARAM;
385 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
386 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
388 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
390 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
392 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
394 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
395 GetDeviceCaps(hdc, LOGPIXELSY)));
396 } else
398 for(i = 0; i < ARRAY_SIZE(choices); i++)
399 add_size(hSizeListWnd, choices[i]);
402 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
403 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
406 static void update_size_list(void)
408 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
409 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
410 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
411 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
412 CHARFORMAT2W fmt;
414 ZeroMemory(&fmt, sizeof(fmt));
415 fmt.cbSize = sizeof(fmt);
417 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
419 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
420 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
422 if(lstrcmpW(fontSize, sizeBuffer))
423 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
426 static void update_font_list(void)
428 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
429 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
430 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
431 WCHAR fontName[MAX_STRING_LEN];
432 CHARFORMAT2W fmt;
434 ZeroMemory(&fmt, sizeof(fmt));
435 fmt.cbSize = sizeof(fmt);
437 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
438 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
440 if(lstrcmpW(fontName, fmt.szFaceName))
442 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
443 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
444 } else
446 update_size_list();
450 static void clear_formatting(void)
452 PARAFORMAT2 pf;
454 pf.cbSize = sizeof(pf);
455 pf.dwMask = PFM_ALIGNMENT;
456 pf.wAlignment = PFA_LEFT;
457 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
460 static int fileformat_number(WPARAM format)
462 int number = 0;
464 if(format == SF_TEXT)
466 number = 1;
467 } else if (format == (SF_TEXT | SF_UNICODE))
469 number = 2;
471 return number;
474 static WPARAM fileformat_flags(int format)
476 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
478 return flags[format];
481 static void set_font(LPCWSTR wszFaceName)
483 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
484 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
485 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
486 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
487 CHARFORMAT2W fmt;
489 ZeroMemory(&fmt, sizeof(fmt));
491 fmt.cbSize = sizeof(fmt);
492 fmt.dwMask = CFM_FACE;
494 lstrcpyW(fmt.szFaceName, wszFaceName);
496 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
498 populate_size_list(hSizeListWnd);
500 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
503 static void set_default_font(void)
505 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
506 'R','o','m','a','n',0};
507 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
508 CHARFORMAT2W fmt;
509 LPCWSTR font;
511 ZeroMemory(&fmt, sizeof(fmt));
513 fmt.cbSize = sizeof(fmt);
514 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
515 fmt.dwEffects = 0;
517 if(fileFormat & SF_RTF)
518 font = richTextFont;
519 else
520 font = plainTextFont;
522 lstrcpyW(fmt.szFaceName, font);
524 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
527 static void on_fontlist_modified(LPWSTR wszNewFaceName)
529 CHARFORMAT2W format;
530 ZeroMemory(&format, sizeof(format));
531 format.cbSize = sizeof(format);
532 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
534 if(lstrcmpW(format.szFaceName, wszNewFaceName))
535 set_font(wszNewFaceName);
538 static void dialog_choose_font(void)
540 CHOOSEFONTW cf;
541 LOGFONTW lf;
542 CHARFORMAT2W fmt;
543 HDC hDC = GetDC(hMainWnd);
545 ZeroMemory(&cf, sizeof(cf));
546 cf.lStructSize = sizeof(cf);
547 cf.hwndOwner = hMainWnd;
548 cf.lpLogFont = &lf;
549 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_NOVERTFONTS;
551 ZeroMemory(&fmt, sizeof(fmt));
552 fmt.cbSize = sizeof(fmt);
554 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
555 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
556 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
557 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
558 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
559 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
560 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
561 cf.rgbColors = fmt.crTextColor;
563 if(ChooseFontW(&cf))
565 ZeroMemory(&fmt, sizeof(fmt));
566 fmt.cbSize = sizeof(fmt);
567 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
568 fmt.yHeight = cf.iPointSize * 2;
570 if(cf.nFontType & BOLD_FONTTYPE)
571 fmt.dwEffects |= CFE_BOLD;
572 if(cf.nFontType & ITALIC_FONTTYPE)
573 fmt.dwEffects |= CFE_ITALIC;
574 if(cf.lpLogFont->lfUnderline)
575 fmt.dwEffects |= CFE_UNDERLINE;
576 if(cf.lpLogFont->lfStrikeOut)
577 fmt.dwEffects |= CFE_STRIKEOUT;
579 fmt.crTextColor = cf.rgbColors;
581 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
582 set_font(cf.lpLogFont->lfFaceName);
586 struct font_desc
588 WCHAR *name;
589 LPARAM lParam;
592 struct font_array
594 struct font_desc *fonts;
595 size_t count;
596 size_t capacity;
599 static BOOL array_reserve(void **elements, size_t *capacity, size_t count, size_t size)
601 size_t new_capacity, max_capacity;
602 void *new_elements;
604 if (count <= *capacity)
605 return TRUE;
607 max_capacity = ~(SIZE_T)0 / size;
608 if (count > max_capacity)
609 return FALSE;
611 new_capacity = max(4, *capacity);
612 while (new_capacity < count && new_capacity <= max_capacity / 2)
613 new_capacity *= 2;
614 if (new_capacity < count)
615 new_capacity = max_capacity;
617 new_elements = *elements ? realloc(*elements, new_capacity * size) :
618 malloc(new_capacity * size);
619 if (!new_elements)
620 return FALSE;
622 *elements = new_elements;
623 *capacity = new_capacity;
625 return TRUE;
628 static void add_font(struct font_array *fonts, LPCWSTR fontName, DWORD fontType, const NEWTEXTMETRICEXW *ntmc)
630 int fontHeight = 0;
631 size_t idx;
633 if (!array_reserve((void **)&fonts->fonts, &fonts->capacity, fonts->count + 1, sizeof(*fonts->fonts)))
634 return;
636 if (fontType & RASTER_FONTTYPE)
637 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
639 idx = fonts->count;
640 fonts->fonts[idx].name = malloc((lstrlenW(fontName) + 1)*sizeof(WCHAR) );
641 lstrcpyW( fonts->fonts[idx].name, fontName );
642 fonts->fonts[idx].lParam = MAKELONG(fontType, fontHeight);
644 fonts->count++;
647 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
648 DWORD FontType, LPARAM lParam)
650 struct font_array *fonts = (void *)lParam;
652 if (lpelfe->lfFaceName[0] == '@') return 1; /* ignore vertical fonts */
654 add_font(fonts, lpelfe->lfFaceName, FontType, (const NEWTEXTMETRICEXW *)lpntme);
656 return 1;
659 static int __cdecl fonts_desc_compare(const void *a, const void *b)
661 const struct font_desc *left = a, *right = b;
662 return lstrcmpiW(left->name, right->name);
665 static void populate_font_list(HWND hListWnd)
667 struct font_array font_array = { 0 };
668 HDC hdc = GetDC(hMainWnd);
669 LOGFONTW fontinfo;
670 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
671 CHARFORMAT2W fmt;
672 size_t i, j;
674 fontinfo.lfCharSet = DEFAULT_CHARSET;
675 *fontinfo.lfFaceName = '\0';
676 fontinfo.lfPitchAndFamily = 0;
678 /* Collect font names, sort, remove duplicates. */
679 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc, (LPARAM)&font_array, 0);
681 qsort(font_array.fonts, font_array.count, sizeof(*font_array.fonts), fonts_desc_compare);
683 for (i = 1, j = 0; i < font_array.count; ++i)
685 if (!lstrcmpiW(font_array.fonts[i].name, font_array.fonts[j].name))
687 free(font_array.fonts[i].name);
688 font_array.fonts[i].name = NULL;
690 else if (++j != i)
692 font_array.fonts[j] = font_array.fonts[i];
693 font_array.fonts[i].name = NULL;
696 font_array.count = j + 1;
698 for (i = 0; i < font_array.count; ++i)
700 COMBOBOXEXITEMW cbitem = { 0 };
702 cbitem.mask = CBEIF_TEXT | CBEIF_LPARAM;
703 cbitem.pszText = font_array.fonts[i].name;
704 cbitem.iItem = -1;
705 cbitem.lParam = font_array.fonts[i].lParam;
707 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbitem);
709 free(font_array.fonts[i].name);
711 free(font_array.fonts);
713 ZeroMemory(&fmt, sizeof(fmt));
714 fmt.cbSize = sizeof(fmt);
715 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
716 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
719 static void update_window(void)
721 RECT rect;
723 GetClientRect(hMainWnd, &rect);
725 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
728 static BOOL is_bar_visible(int bandId)
730 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
733 static void store_bar_state(int bandId, BOOL show)
735 int formatIndex = reg_formatindex(fileFormat);
737 if(show)
738 barState[formatIndex] |= (1 << bandId);
739 else
740 barState[formatIndex] &= ~(1 << bandId);
743 static void set_toolbar_state(int bandId, BOOL show)
745 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
747 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
749 if(bandId == BANDID_TOOLBAR)
751 REBARBANDINFOW rbbinfo;
752 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
754 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
755 rbbinfo.fMask = RBBIM_STYLE;
757 SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
759 if(!show)
760 rbbinfo.fStyle &= ~RBBS_BREAK;
761 else
762 rbbinfo.fStyle |= RBBS_BREAK;
764 SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
767 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
768 store_bar_state(bandId, show);
771 static void set_statusbar_state(BOOL show)
773 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
775 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
776 store_bar_state(BANDID_STATUSBAR, show);
779 static void set_bar_states(void)
781 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
782 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
783 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
784 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
785 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
786 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
788 update_window();
791 static void preview_exit(HWND hMainWnd)
793 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
794 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
796 set_bar_states();
797 ShowWindow(hEditorWnd, TRUE);
799 close_preview(hMainWnd);
801 SetMenu(hMainWnd, hMenu);
802 registry_read_filelist(hMainWnd);
804 update_window();
807 static void set_fileformat(WPARAM format)
809 fileFormat = format;
811 set_bar_states();
812 set_default_font();
813 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
816 static void ShowOpenError(DWORD Code)
818 LPWSTR Message;
820 switch(Code)
822 case ERROR_ACCESS_DENIED:
823 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
824 break;
826 default:
827 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
829 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
832 static void DoOpenFile(LPCWSTR szOpenFileName)
834 HANDLE hFile;
835 EDITSTREAM es;
836 char fileStart[5];
837 DWORD readOut;
838 WPARAM format = SF_TEXT;
840 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
841 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
842 if (hFile == INVALID_HANDLE_VALUE)
844 ShowOpenError(GetLastError());
845 return;
848 ReadFile(hFile, fileStart, 5, &readOut, NULL);
849 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
851 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
853 format = SF_TEXT | SF_UNICODE;
854 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
855 } else if(readOut >= 5)
857 static const char header[] = "{\\rtf";
858 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
860 if(!memcmp(header, fileStart, 5))
861 format = SF_RTF;
862 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
864 CloseHandle(hFile);
865 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
866 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
867 return;
871 es.dwCookie = (DWORD_PTR)hFile;
872 es.pfnCallback = stream_in;
874 clear_formatting();
875 set_fileformat(format);
876 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
878 CloseHandle(hFile);
880 SetFocus(hEditorWnd);
882 set_caption(szOpenFileName);
884 lstrcpyW(wszFileName, szOpenFileName);
885 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
886 registry_set_filelist(szOpenFileName, hMainWnd);
887 update_font_list();
890 static void ShowWriteError(DWORD Code)
892 LPWSTR Message;
894 switch(Code)
896 case ERROR_ACCESS_DENIED:
897 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
898 break;
900 default:
901 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
903 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
906 static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
908 HANDLE hFile;
909 EDITSTREAM stream;
910 LRESULT ret;
912 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
913 FILE_ATTRIBUTE_NORMAL, NULL);
915 if(hFile == INVALID_HANDLE_VALUE)
917 ShowWriteError(GetLastError());
918 return FALSE;
921 if(format == (SF_TEXT | SF_UNICODE))
923 static const BYTE unicode[] = {0xff,0xfe};
924 DWORD writeOut;
925 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
927 if(writeOut != sizeof(unicode))
929 CloseHandle(hFile);
930 return FALSE;
934 stream.dwCookie = (DWORD_PTR)hFile;
935 stream.pfnCallback = stream_out;
937 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
939 CloseHandle(hFile);
941 SetFocus(hEditorWnd);
943 if(!ret)
945 GETTEXTLENGTHEX gt;
946 gt.flags = GTL_DEFAULT;
947 gt.codepage = 1200;
949 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
950 return FALSE;
953 lstrcpyW(wszFileName, wszSaveFileName);
954 set_caption(wszFileName);
955 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
956 set_fileformat(format);
958 return TRUE;
961 static BOOL DialogSaveFile(void)
963 OPENFILENAMEW sfn;
965 WCHAR wszFile[MAX_PATH] = {'\0'};
966 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
968 ZeroMemory(&sfn, sizeof(sfn));
970 sfn.lStructSize = sizeof(sfn);
971 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
972 sfn.hwndOwner = hMainWnd;
973 sfn.lpstrFilter = wszFilter;
974 sfn.lpstrFile = wszFile;
975 sfn.nMaxFile = MAX_PATH;
976 sfn.lpstrDefExt = wszDefExt;
977 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
979 while(GetSaveFileNameW(&sfn))
981 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
983 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
984 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
985 continue;
987 return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
989 return FALSE;
992 static BOOL prompt_save_changes(void)
994 if(!wszFileName[0])
996 GETTEXTLENGTHEX gt;
997 gt.flags = GTL_NUMCHARS;
998 gt.codepage = 1200;
999 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
1000 return TRUE;
1003 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
1005 return TRUE;
1006 } else
1008 LPWSTR displayFileName;
1009 WCHAR *text;
1010 int ret;
1012 if(!wszFileName[0])
1013 displayFileName = wszDefaultFileName;
1014 else
1015 displayFileName = file_basename(wszFileName);
1017 text = calloc(lstrlenW(displayFileName)+lstrlenW(wszSaveChanges), sizeof(WCHAR));
1019 if(!text)
1020 return FALSE;
1022 wsprintfW(text, wszSaveChanges, displayFileName);
1024 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
1026 free(text);
1028 switch(ret)
1030 case IDNO:
1031 return TRUE;
1033 case IDYES:
1034 if(wszFileName[0])
1035 return DoSaveFile(wszFileName, fileFormat);
1036 return DialogSaveFile();
1038 default:
1039 return FALSE;
1044 static void DialogOpenFile(void)
1046 OPENFILENAMEW ofn;
1048 WCHAR wszFile[MAX_PATH] = {'\0'};
1049 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
1051 ZeroMemory(&ofn, sizeof(ofn));
1053 ofn.lStructSize = sizeof(ofn);
1054 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
1055 ofn.hwndOwner = hMainWnd;
1056 ofn.lpstrFilter = wszFilter;
1057 ofn.lpstrFile = wszFile;
1058 ofn.nMaxFile = MAX_PATH;
1059 ofn.lpstrDefExt = wszDefExt;
1060 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
1062 if(GetOpenFileNameW(&ofn))
1064 if(prompt_save_changes())
1065 DoOpenFile(ofn.lpstrFile);
1069 static void dialog_about(void)
1071 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1072 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1075 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1077 switch(message)
1079 case WM_INITDIALOG:
1081 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1082 int wrap = -1;
1083 char id[4];
1084 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1086 sprintf(id, "%d\n", (int)ps->lParam);
1087 SetWindowTextA(hIdWnd, id);
1088 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1089 wrap = IDC_PAGEFMT_WN;
1090 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1091 wrap = IDC_PAGEFMT_WW;
1092 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1093 wrap = IDC_PAGEFMT_WM;
1095 if(wrap != -1)
1096 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1097 IDC_PAGEFMT_WM, wrap);
1099 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1100 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1101 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1102 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1103 if(barState[ps->lParam] & (1 << BANDID_RULER))
1104 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1105 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1106 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1108 break;
1110 case WM_COMMAND:
1111 switch(LOWORD(wParam))
1113 case IDC_PAGEFMT_WN:
1114 case IDC_PAGEFMT_WW:
1115 case IDC_PAGEFMT_WM:
1116 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1117 LOWORD(wParam));
1118 break;
1120 case IDC_PAGEFMT_TB:
1121 case IDC_PAGEFMT_FB:
1122 case IDC_PAGEFMT_RU:
1123 case IDC_PAGEFMT_SB:
1124 CheckDlgButton(hWnd, LOWORD(wParam),
1125 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1126 break;
1128 break;
1129 case WM_NOTIFY:
1131 LPNMHDR header = (LPNMHDR)lParam;
1132 if(header->code == PSN_APPLY)
1134 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1135 char sid[4];
1136 int id;
1138 GetWindowTextA(hIdWnd, sid, 4);
1139 id = atoi(sid);
1140 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1141 wordWrap[id] = ID_WORDWRAP_NONE;
1142 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1143 wordWrap[id] = ID_WORDWRAP_WINDOW;
1144 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1145 wordWrap[id] = ID_WORDWRAP_MARGIN;
1147 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1148 barState[id] |= (1 << BANDID_TOOLBAR);
1149 else
1150 barState[id] &= ~(1 << BANDID_TOOLBAR);
1152 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1153 barState[id] |= (1 << BANDID_FORMATBAR);
1154 else
1155 barState[id] &= ~(1 << BANDID_FORMATBAR);
1157 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1158 barState[id] |= (1 << BANDID_RULER);
1159 else
1160 barState[id] &= ~(1 << BANDID_RULER);
1162 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1163 barState[id] |= (1 << BANDID_STATUSBAR);
1164 else
1165 barState[id] &= ~(1 << BANDID_STATUSBAR);
1168 break;
1170 return FALSE;
1173 static void dialog_viewproperties(void)
1175 PROPSHEETPAGEW psp[2];
1176 PROPSHEETHEADERW psh;
1177 size_t i;
1178 HINSTANCE hInstance = GetModuleHandleW(0);
1179 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1181 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1182 psp[0].dwFlags = PSP_USETITLE;
1183 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1184 psp[0].pfnDlgProc = formatopts_proc;
1185 psp[0].hInstance = hInstance;
1186 psp[0].lParam = reg_formatindex(SF_TEXT);
1187 psp[0].pfnCallback = NULL;
1188 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1189 for(i = 1; i < ARRAY_SIZE(psp); i++)
1191 psp[i].dwSize = psp[0].dwSize;
1192 psp[i].dwFlags = psp[0].dwFlags;
1193 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1194 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1195 psp[i].hInstance = psp[0].hInstance;
1196 psp[i].lParam = reg_formatindex(SF_RTF);
1197 psp[i].pfnCallback = psp[0].pfnCallback;
1198 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1201 psh.dwSize = sizeof(psh);
1202 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1203 psh.hwndParent = hMainWnd;
1204 psh.hInstance = hInstance;
1205 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1206 psh.nPages = ARRAY_SIZE(psp);
1207 U3(psh).ppsp = ppsp;
1208 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1210 if(fileFormat & SF_RTF)
1211 U2(psh).nStartPage = 1;
1212 else
1213 U2(psh).nStartPage = 0;
1214 PropertySheetW(&psh);
1215 set_bar_states();
1216 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1219 static void HandleCommandLine(LPWSTR cmdline)
1221 WCHAR delimiter;
1222 BOOL opt_print = FALSE;
1224 /* skip white space */
1225 while (*cmdline == ' ') cmdline++;
1227 /* skip executable name */
1228 delimiter = (*cmdline == '"' ? '"' : ' ');
1230 if (*cmdline == delimiter) cmdline++;
1231 while (*cmdline && *cmdline != delimiter) cmdline++;
1232 if (*cmdline == delimiter) cmdline++;
1234 while (*cmdline)
1236 while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
1238 if (*cmdline == '-' || *cmdline == '/')
1240 if (!cmdline[2] || isspace(cmdline[2]))
1242 switch (cmdline[1])
1244 case 'P':
1245 case 'p':
1246 opt_print = TRUE;
1247 cmdline += 2;
1248 continue;
1251 /* a filename starting by / */
1253 break;
1256 if (*cmdline)
1258 /* file name is passed on the command line */
1259 if (cmdline[0] == '"')
1261 cmdline++;
1262 cmdline[lstrlenW(cmdline) - 1] = 0;
1264 DoOpenFile(cmdline);
1265 InvalidateRect(hMainWnd, NULL, FALSE);
1268 if (opt_print)
1269 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1272 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1274 if(pFr->Flags & FR_DIALOGTERM)
1276 hFindWnd = 0;
1277 pFr->Flags = FR_FINDNEXT;
1278 return 0;
1281 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1283 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1284 DWORD flags;
1285 FINDTEXTEXW ft;
1286 CHARRANGE sel;
1287 LRESULT ret = -1;
1288 HMENU hMenu = GetMenu(hMainWnd);
1289 MENUITEMINFOW mi;
1291 mi.cbSize = sizeof(mi);
1292 mi.fMask = MIIM_DATA;
1293 mi.dwItemData = 1;
1294 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1296 /* Make sure find field is saved. */
1297 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1299 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1300 ARRAY_SIZE(custom_data->findBuffer));
1301 pFr->lpstrFindWhat = custom_data->findBuffer;
1304 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1305 if(custom_data->endPos == -1) {
1306 custom_data->endPos = sel.cpMin;
1307 custom_data->wrapped = FALSE;
1310 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1311 ft.lpstrText = pFr->lpstrFindWhat;
1313 /* Only replace the existing selection if it is an exact match. */
1314 if (sel.cpMin != sel.cpMax &&
1315 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1317 ft.chrg = sel;
1318 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1319 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1320 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1321 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1325 /* Search from the start of the selection, but exclude the first character
1326 * from search if there is a selection. */
1327 ft.chrg.cpMin = sel.cpMin;
1328 if (sel.cpMin != sel.cpMax)
1329 ft.chrg.cpMin++;
1331 /* Search to the end, then wrap around and search from the start. */
1332 if (!custom_data->wrapped) {
1333 ft.chrg.cpMax = -1;
1334 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1335 if (ret == -1) {
1336 custom_data->wrapped = TRUE;
1337 ft.chrg.cpMin = 0;
1341 if (ret == -1) {
1342 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1343 if (ft.chrg.cpMax > ft.chrg.cpMin)
1344 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1347 if (ret == -1) {
1348 custom_data->endPos = -1;
1349 EnableWindow(hMainWnd, FALSE);
1350 MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1351 wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
1352 EnableWindow(hMainWnd, TRUE);
1353 } else {
1354 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1355 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1357 if (pFr->Flags & FR_REPLACEALL)
1358 return handle_findmsg(pFr);
1362 return 0;
1365 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1367 static WCHAR selBuffer[128];
1368 static WCHAR replaceBuffer[128];
1369 static FINDREPLACE_custom custom_data;
1370 static const WCHAR endl = '\r';
1371 FINDTEXTW ft;
1373 /* Allow only one search/replace dialog to open */
1374 if(hFindWnd != NULL)
1376 SetActiveWindow(hFindWnd);
1377 return;
1380 ZeroMemory(fr, sizeof(FINDREPLACEW));
1381 fr->lStructSize = sizeof(FINDREPLACEW);
1382 fr->hwndOwner = hMainWnd;
1383 fr->Flags = FR_HIDEUPDOWN;
1384 /* Find field is filled with the selected text if it is non-empty
1385 * and stays within the same paragraph, otherwise the previous
1386 * find field is used. */
1387 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1388 (LPARAM)&ft.chrg.cpMax);
1389 ft.lpstrText = &endl;
1390 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1391 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1393 /* Use a temporary buffer for the selected text so that the saved
1394 * find field is only overwritten when a find/replace is clicked. */
1395 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1396 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1397 fr->lpstrFindWhat = selBuffer;
1398 } else {
1399 fr->lpstrFindWhat = custom_data.findBuffer;
1401 fr->lpstrReplaceWith = replaceBuffer;
1402 custom_data.endPos = -1;
1403 custom_data.wrapped = FALSE;
1404 fr->lCustData = (LPARAM)&custom_data;
1405 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1406 fr->wReplaceWithLen = sizeof(replaceBuffer);
1408 if(replace)
1409 hFindWnd = ReplaceTextW(fr);
1410 else
1411 hFindWnd = FindTextW(fr);
1414 static int units_to_twips(UNIT unit, float number)
1416 int twips = 0;
1418 switch(unit)
1420 case UNIT_CM:
1421 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1422 break;
1424 case UNIT_INCH:
1425 twips = (int)(number * (float)TWIPS_PER_INCH);
1426 break;
1428 case UNIT_PT:
1429 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1430 break;
1433 return twips;
1436 static void append_current_units(LPWSTR buffer)
1438 static const WCHAR space[] = {' ', 0};
1439 lstrcatW(buffer, space);
1440 lstrcatW(buffer, units_cmW);
1443 static void number_with_units(LPWSTR buffer, int number)
1445 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1446 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1448 swprintf(buffer, MAX_STRING_LEN, fmt, converted, units_cmW);
1451 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1453 COMBOBOXEXITEMW cbItem;
1454 COMBOBOXINFO cbInfo;
1455 HWND hCombo, hList;
1456 int idx, result;
1458 hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1459 if (!hCombo)
1460 return FALSE;
1461 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1462 result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1463 if (!result)
1464 return FALSE;
1465 hList = cbInfo.hwndList;
1466 idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
1467 if (idx < 0)
1468 return FALSE;
1470 ZeroMemory(&cbItem, sizeof(cbItem));
1471 cbItem.mask = CBEIF_TEXT;
1472 cbItem.iItem = idx;
1473 cbItem.pszText = wszBuffer;
1474 cbItem.cchTextMax = bufferLength-1;
1475 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1477 return result != 0;
1480 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1482 switch(message)
1484 case WM_INITDIALOG:
1486 WCHAR buffer[MAX_STRING_LEN];
1487 SYSTEMTIME st;
1488 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1489 GetLocalTime(&st);
1491 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1492 MAX_STRING_LEN);
1493 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1494 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1495 MAX_STRING_LEN);
1496 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1497 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1498 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1500 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1502 break;
1504 case WM_COMMAND:
1505 switch(LOWORD(wParam))
1507 case IDC_DATETIME:
1508 if (HIWORD(wParam) != LBN_DBLCLK)
1509 break;
1510 /* Fall through */
1512 case IDOK:
1514 LRESULT index;
1515 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1517 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1519 if(index != LB_ERR)
1521 WCHAR buffer[MAX_STRING_LEN];
1522 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1523 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1526 /* Fall through */
1528 case IDCANCEL:
1529 EndDialog(hWnd, wParam);
1530 return TRUE;
1533 return FALSE;
1536 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1538 switch(message)
1540 case WM_INITDIALOG:
1542 HINSTANCE hInstance = GetModuleHandleW(0);
1543 WCHAR buffer[MAX_STRING_LEN];
1544 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1546 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1547 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1548 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1549 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1550 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1551 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1553 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1555 break;
1557 case WM_COMMAND:
1558 switch(LOWORD(wParam))
1560 case IDOK:
1562 LRESULT index;
1563 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1564 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1566 if(index != LB_ERR)
1567 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1569 return TRUE;
1571 case IDCANCEL:
1572 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1573 return TRUE;
1576 return FALSE;
1579 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1581 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1583 switch(message)
1585 case WM_INITDIALOG:
1587 HINSTANCE hInstance = GetModuleHandleW(0);
1588 WCHAR buffer[MAX_STRING_LEN];
1589 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1590 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1591 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1592 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1593 PARAFORMAT2 pf;
1594 int index = 0;
1596 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1597 MAX_STRING_LEN);
1598 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1599 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1600 MAX_STRING_LEN);
1601 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1602 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1603 MAX_STRING_LEN);
1604 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1606 pf.cbSize = sizeof(pf);
1607 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1608 PFM_STARTINDENT;
1609 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1611 if(pf.wAlignment == PFA_RIGHT)
1612 index ++;
1613 else if(pf.wAlignment == PFA_CENTER)
1614 index += 2;
1616 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1618 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1619 SetWindowTextW(hLeftWnd, buffer);
1620 number_with_units(buffer, pf.dxRightIndent);
1621 SetWindowTextW(hRightWnd, buffer);
1622 number_with_units(buffer, -pf.dxOffset);
1623 SetWindowTextW(hFirstWnd, buffer);
1625 break;
1627 case WM_COMMAND:
1628 switch(LOWORD(wParam))
1630 case IDOK:
1632 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1633 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1634 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1635 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1636 WCHAR buffer[MAX_STRING_LEN];
1637 int index;
1638 float num;
1639 int ret = 0;
1640 PARAFORMAT2 pf;
1641 UNIT unit;
1642 BOOL in_list = FALSE;
1644 pf.cbSize = sizeof(pf);
1645 pf.dwMask = PFM_NUMBERING;
1646 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1647 if ((pf.dwMask & PFM_NUMBERING) && pf.wNumbering)
1648 in_list = TRUE;
1650 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1651 pf.wAlignment = ALIGNMENT_VALUES[index];
1653 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1654 if(number_from_string(buffer, &num, &unit))
1655 ret++;
1656 pf.dxOffset = units_to_twips(unit, num);
1657 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1658 if(number_from_string(buffer, &num, &unit))
1659 ret++;
1660 pf.dxRightIndent = units_to_twips(unit, num);
1661 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1662 if(number_from_string(buffer, &num, &unit))
1663 ret++;
1664 pf.dxStartIndent = units_to_twips(unit, num);
1666 if(ret != 3)
1668 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1669 wszAppTitle, MB_OK | MB_ICONASTERISK);
1670 return FALSE;
1671 } else
1673 if (pf.dxOffset + pf.dxStartIndent < 0
1674 && pf.dxStartIndent < 0)
1676 /* The first line is before the left edge, so
1677 * make sure it is at the left edge. */
1678 pf.dxOffset = -pf.dxStartIndent;
1679 } else if (pf.dxOffset < 0) {
1680 /* The second and following lines are before
1681 * the left edge, so set it to be at the left
1682 * edge, and adjust the first line since it
1683 * is relative to it. */
1684 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1685 pf.dxOffset = 0;
1687 /* Internally the dxStartIndent is the absolute
1688 * offset for the first line and dxOffset is
1689 * to it value as opposed how it is displayed with
1690 * the first line being the relative value.
1691 * These two lines make the adjustments. */
1692 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1693 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1695 pf.cbSize = sizeof(pf);
1696 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1697 PFM_STARTINDENT;
1698 if (in_list)
1700 pf.wNumberingTab = max(pf.dxOffset, 0);
1701 pf.dwMask |= PFM_NUMBERINGTAB;
1704 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1707 /* Fall through */
1709 case IDCANCEL:
1710 EndDialog(hWnd, wParam);
1711 return TRUE;
1714 return FALSE;
1717 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1719 switch(message)
1721 case WM_INITDIALOG:
1723 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1724 PARAFORMAT pf;
1725 WCHAR buffer[MAX_STRING_LEN];
1726 int i;
1728 pf.cbSize = sizeof(pf);
1729 pf.dwMask = PFM_TABSTOPS;
1730 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1731 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1733 for(i = 0; i < pf.cTabCount; i++)
1735 number_with_units(buffer, pf.rgxTabs[i]);
1736 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1738 SetFocus(hTabWnd);
1740 break;
1742 case WM_COMMAND:
1743 switch(LOWORD(wParam))
1745 case IDC_TABSTOPS:
1747 HWND hTabWnd = (HWND)lParam;
1748 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1749 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1750 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1752 if(GetWindowTextLengthW(hTabWnd))
1753 EnableWindow(hAddWnd, TRUE);
1754 else
1755 EnableWindow(hAddWnd, FALSE);
1757 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1759 EnableWindow(hEmptyWnd, TRUE);
1761 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1762 EnableWindow(hDelWnd, FALSE);
1763 else
1764 EnableWindow(hDelWnd, TRUE);
1765 } else
1767 EnableWindow(hEmptyWnd, FALSE);
1770 break;
1772 case ID_TAB_ADD:
1774 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1775 WCHAR buffer[MAX_STRING_LEN];
1776 UNIT unit;
1778 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1779 append_current_units(buffer);
1781 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1783 float number = 0;
1784 int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
1786 if(!number_from_string(buffer, &number, &unit))
1788 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1789 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1790 } else if (item_count >= MAX_TAB_STOPS) {
1791 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1792 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1793 } else {
1794 int i;
1795 float next_number = -1;
1796 int next_number_in_twips = -1;
1797 int insert_number = units_to_twips(unit, number);
1799 /* linear search for position to insert the string */
1800 for(i = 0; i < item_count; i++)
1802 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1803 number_from_string(buffer, &next_number, &unit);
1804 next_number_in_twips = units_to_twips(unit, next_number);
1805 if (insert_number <= next_number_in_twips)
1806 break;
1808 if (insert_number != next_number_in_twips)
1810 number_with_units(buffer, insert_number);
1811 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1812 SetWindowTextW(hTabWnd, 0);
1816 SetFocus(hTabWnd);
1818 break;
1820 case ID_TAB_DEL:
1822 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1823 LRESULT ret;
1824 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1825 if(ret != CB_ERR)
1826 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1828 break;
1830 case ID_TAB_EMPTY:
1832 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1833 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1834 SetFocus(hTabWnd);
1836 break;
1838 case IDOK:
1840 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1841 int i;
1842 WCHAR buffer[MAX_STRING_LEN];
1843 PARAFORMAT pf;
1844 float number;
1845 UNIT unit;
1847 pf.cbSize = sizeof(pf);
1848 pf.dwMask = PFM_TABSTOPS;
1850 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1851 (LPARAM)&buffer) != CB_ERR &&
1852 i < MAX_TAB_STOPS; i++)
1854 number_from_string(buffer, &number, &unit);
1855 pf.rgxTabs[i] = units_to_twips(unit, number);
1857 pf.cTabCount = i;
1858 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1860 /* Fall through */
1861 case IDCANCEL:
1862 EndDialog(hWnd, wParam);
1863 return TRUE;
1866 return FALSE;
1869 static LRESULT OnCreate( HWND hWnd )
1871 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1872 HINSTANCE hInstance = GetModuleHandleW(0);
1873 HANDLE hDLL;
1874 TBADDBITMAP ab;
1875 int nStdBitmaps = 0;
1876 REBARINFO rbi;
1877 REBARBANDINFOW rbb;
1878 RECT rect;
1879 HFONT font;
1880 HDC hdc;
1881 SIZE name_sz, size_sz;
1882 int height, dpi;
1883 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1884 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1885 static const WCHAR font_text[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0}; /* a long font name */
1886 static const WCHAR size_text[] = {' ','0','0',0}; /* enough for two digits */
1888 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1890 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1891 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1892 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1894 rbi.cbSize = sizeof(rbi);
1895 rbi.fMask = 0;
1896 rbi.himl = NULL;
1897 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1898 return -1;
1900 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS,
1901 IDC_TOOLBAR,
1902 1, hInstance, IDB_TOOLBAR,
1903 NULL, 0,
1904 24, 24, 16, 16, sizeof(TBBUTTON));
1906 hdc = GetDC(hWnd);
1907 dpi = GetDeviceCaps(hdc, LOGPIXELSY);
1908 ReleaseDC(hWnd, hdc);
1910 ab.hInst = HINST_COMMCTRL;
1911 ab.nID = dpi >= 120 ? IDB_STD_LARGE_COLOR : IDB_STD_SMALL_COLOR;
1912 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1914 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1915 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1916 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1917 AddSeparator(hToolBarWnd);
1918 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1919 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1920 AddSeparator(hToolBarWnd);
1921 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1922 AddSeparator(hToolBarWnd);
1923 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1924 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1925 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1926 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1927 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1928 AddSeparator(hToolBarWnd);
1929 AddButton(hToolBarWnd, 0, ID_DATETIME);
1931 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1932 height = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1934 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1935 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1936 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1937 GetWindowRect(hFontListWnd, &rect);
1938 height = max(height, rect.bottom - rect.top);
1940 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1941 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1942 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1943 rbb.cx = 0;
1944 rbb.hwndChild = hToolBarWnd;
1945 rbb.cxMinChild = 0;
1946 rbb.cyChild = rbb.cyMinChild = height;
1947 rbb.wID = BANDID_TOOLBAR;
1949 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1951 font = (HFONT)SendMessageW(hFontListWnd, WM_GETFONT, 0, 0);
1952 hdc = GetDC(hFontListWnd);
1953 font = SelectObject(hdc, font);
1954 GetTextExtentPointW(hdc, font_text, ARRAY_SIZE(font_text) - 1, &name_sz);
1955 GetTextExtentPointW(hdc, size_text, ARRAY_SIZE(size_text) - 1, &size_sz);
1956 font = SelectObject(hdc, font);
1957 ReleaseDC(hFontListWnd, hdc);
1958 rbb.hwndChild = hFontListWnd;
1959 rbb.cx = MulDiv(name_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1960 rbb.wID = BANDID_FONTLIST;
1962 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1964 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1965 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1966 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1968 rbb.hwndChild = hSizeListWnd;
1969 rbb.cx = MulDiv(size_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1970 rbb.fStyle ^= RBBS_BREAK;
1971 rbb.wID = BANDID_SIZELIST;
1973 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1975 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1976 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS,
1977 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1979 SendMessageW(hFormatBarWnd, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
1981 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1982 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1983 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1984 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1985 AddSeparator(hFormatBarWnd);
1986 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1987 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1988 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1989 AddSeparator(hFormatBarWnd);
1990 AddButtonStyle(hFormatBarWnd, 7, ID_BULLETONOFF, BTNS_DROPDOWN);
1992 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1994 rbb.hwndChild = hFormatBarWnd;
1995 rbb.wID = BANDID_FORMATBAR;
1997 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1999 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
2000 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
2003 rbb.hwndChild = hRulerWnd;
2004 rbb.wID = BANDID_RULER;
2005 rbb.fStyle |= RBBS_BREAK;
2007 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
2009 hDLL = LoadLibraryW(wszRichEditDll);
2010 if(!hDLL)
2012 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
2013 MB_OK | MB_ICONEXCLAMATION);
2014 PostQuitMessage(1);
2017 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
2018 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
2019 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
2020 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
2022 if (!hEditorWnd)
2024 fprintf(stderr, "Error code %lu\n", GetLastError());
2025 return -1;
2027 assert(hEditorWnd);
2029 setup_richedit_olecallback(hEditorWnd);
2030 SetFocus(hEditorWnd);
2031 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
2033 set_default_font();
2035 populate_font_list(hFontListWnd);
2036 populate_size_list(hSizeListWnd);
2037 DoLoadStrings();
2038 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2040 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
2042 registry_read_filelist(hWnd);
2043 registry_read_formatopts_all(barState, wordWrap);
2044 registry_read_options();
2045 DragAcceptFiles(hWnd, TRUE);
2047 return 0;
2050 static LRESULT OnUser( HWND hWnd )
2052 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2053 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2054 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
2055 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2056 int from, to;
2057 CHARFORMAT2W fmt;
2058 PARAFORMAT2 pf;
2059 GETTEXTLENGTHEX gt;
2061 ZeroMemory(&fmt, sizeof(fmt));
2062 fmt.cbSize = sizeof(fmt);
2064 ZeroMemory(&pf, sizeof(pf));
2065 pf.cbSize = sizeof(pf);
2067 gt.flags = GTL_NUMCHARS;
2068 gt.codepage = 1200;
2070 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
2071 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
2073 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
2075 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2076 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
2077 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
2078 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
2079 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
2080 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
2081 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2083 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2084 (fmt.dwEffects & CFE_BOLD));
2085 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2086 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2087 (fmt.dwEffects & CFE_ITALIC));
2088 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2089 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2090 (fmt.dwEffects & CFE_UNDERLINE));
2091 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2093 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2094 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2095 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2096 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2098 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLETONOFF, pf.wNumbering != 0);
2099 return 0;
2102 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2104 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2105 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2106 NMHDR *pHdr = (NMHDR *)lParam;
2107 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2108 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2109 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2111 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2113 if (pHdr->code == CBEN_ENDEDITW)
2115 NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
2116 if(pHdr->hwndFrom == hwndFontList)
2118 on_fontlist_modified(endEdit->szText);
2119 } else if (pHdr->hwndFrom == hwndSizeList)
2121 on_sizelist_modified(hwndSizeList,endEdit->szText);
2124 return 0;
2127 if (pHdr->hwndFrom == hwndFormatBar)
2129 if (pHdr->code == TBN_DROPDOWN)
2131 NMTOOLBARW *tb_notify = (NMTOOLBARW *)lParam;
2132 HMENU menu = GetMenu( hWnd );
2133 MENUITEMINFOW info;
2134 TPMPARAMS params;
2135 RECT rc;
2137 if (!menu) return 0;
2138 info.cbSize = sizeof(info);
2139 info.fMask = MIIM_SUBMENU;
2140 GetMenuItemInfoW( menu, ID_LISTMENU, FALSE, &info );
2141 if (!info.hSubMenu) return 0;
2143 SendMessageW( tb_notify->hdr.hwndFrom, TB_GETRECT, (WPARAM)tb_notify->iItem, (LPARAM)&rc );
2144 MapWindowPoints( tb_notify->hdr.hwndFrom, HWND_DESKTOP, (LPPOINT)&rc, 2 );
2146 params.cbSize = sizeof(params);
2147 params.rcExclude = rc;
2148 TrackPopupMenuEx( info.hSubMenu,
2149 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
2150 rc.left, rc.bottom, hWnd, &params );
2153 return 0;
2156 if (pHdr->hwndFrom == hwndEditor)
2158 if (pHdr->code == EN_SELCHANGE)
2160 SELCHANGE *pSC = (SELCHANGE *)lParam;
2161 char buf[128];
2163 update_font_list();
2165 sprintf( buf,"selection = %ld..%ld, line count=%Id",
2166 pSC->chrg.cpMin, pSC->chrg.cpMax,
2167 SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
2168 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2169 SendMessageW(hWnd, WM_USER, 0, 0);
2170 return 1;
2173 return 0;
2176 /* Copied from dlls/comdlg32/fontdlg.c */
2177 static const COLORREF textcolors[]=
2179 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2180 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2181 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2182 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2185 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2187 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2188 static FINDREPLACEW findreplace;
2190 if ((HWND)lParam == hwndEditor)
2191 return 0;
2193 switch(LOWORD(wParam))
2196 case ID_FILE_EXIT:
2197 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2198 break;
2200 case ID_FILE_NEW:
2202 HINSTANCE hInstance = GetModuleHandleW(0);
2203 int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
2205 if(ret != ID_NEWFILE_ABORT)
2207 if(prompt_save_changes())
2209 SETTEXTEX st;
2211 set_caption(NULL);
2212 wszFileName[0] = '\0';
2214 clear_formatting();
2216 st.flags = ST_DEFAULT;
2217 st.codepage = 1200;
2218 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2220 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2221 set_fileformat(ret);
2222 update_font_list();
2226 break;
2228 case ID_FILE_OPEN:
2229 DialogOpenFile();
2230 break;
2232 case ID_FILE_SAVE:
2233 if(wszFileName[0])
2235 DoSaveFile(wszFileName, fileFormat);
2236 break;
2238 /* Fall through */
2240 case ID_FILE_SAVEAS:
2241 DialogSaveFile();
2242 break;
2244 case ID_FILE_RECENT1:
2245 case ID_FILE_RECENT2:
2246 case ID_FILE_RECENT3:
2247 case ID_FILE_RECENT4:
2249 HMENU hMenu = GetMenu(hWnd);
2250 MENUITEMINFOW mi;
2252 mi.cbSize = sizeof(MENUITEMINFOW);
2253 mi.fMask = MIIM_DATA;
2254 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2255 DoOpenFile((LPWSTR)mi.dwItemData);
2257 break;
2259 case ID_FIND:
2260 dialog_find(&findreplace, FALSE);
2261 break;
2263 case ID_FIND_NEXT:
2264 handle_findmsg(&findreplace);
2265 break;
2267 case ID_REPLACE:
2268 dialog_find(&findreplace, TRUE);
2269 break;
2271 case ID_FONTSETTINGS:
2272 dialog_choose_font();
2273 break;
2275 case ID_PRINT:
2276 dialog_print(hWnd, wszFileName);
2277 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2278 break;
2280 case ID_PRINT_QUICK:
2281 print_quick(hMainWnd, wszFileName);
2282 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2283 break;
2285 case ID_PREVIEW:
2287 int index = reg_formatindex(fileFormat);
2288 DWORD tmp = barState[index];
2289 barState[index] = 1 << BANDID_STATUSBAR;
2290 set_bar_states();
2291 barState[index] = tmp;
2292 ShowWindow(hEditorWnd, FALSE);
2294 init_preview(hWnd, wszFileName);
2296 SetMenu(hWnd, NULL);
2297 InvalidateRect(0, 0, TRUE);
2299 break;
2301 case ID_PRINTSETUP:
2302 dialog_printsetup(hWnd);
2303 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2304 break;
2306 case ID_FORMAT_BOLD:
2307 case ID_FORMAT_ITALIC:
2308 case ID_FORMAT_UNDERLINE:
2310 CHARFORMAT2W fmt;
2311 int effects = CFE_BOLD;
2313 ZeroMemory(&fmt, sizeof(fmt));
2314 fmt.cbSize = sizeof(fmt);
2315 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2317 fmt.dwMask = CFM_BOLD;
2319 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2321 effects = CFE_ITALIC;
2322 fmt.dwMask = CFM_ITALIC;
2323 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2325 effects = CFE_UNDERLINE;
2326 fmt.dwMask = CFM_UNDERLINE;
2329 fmt.dwEffects ^= effects;
2331 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2332 break;
2335 case ID_FORMAT_COLOR:
2337 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2338 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2339 HMENU hPop;
2340 RECT itemrc;
2341 POINT pt;
2342 int mid;
2343 int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2345 SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2346 pt.x = itemrc.left;
2347 pt.y = itemrc.bottom;
2348 ClientToScreen(hFormatBarWnd, &pt);
2349 hPop = GetSubMenu(hColorPopupMenu, 0);
2350 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2351 TPM_RETURNCMD | TPM_NONOTIFY,
2352 pt.x, pt.y, 0, hWnd, 0);
2353 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2355 CHARFORMAT2W fmt;
2357 ZeroMemory(&fmt, sizeof(fmt));
2358 fmt.cbSize = sizeof(fmt);
2359 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2361 fmt.dwMask = CFM_COLOR;
2363 if (mid < ID_COLOR_AUTOMATIC) {
2364 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2365 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2366 } else {
2367 fmt.dwEffects |= CFE_AUTOCOLOR;
2370 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2372 break;
2375 case ID_EDIT_CUT:
2376 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2377 break;
2379 case ID_EDIT_COPY:
2380 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2381 break;
2383 case ID_EDIT_PASTE:
2384 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2385 break;
2387 case ID_EDIT_CLEAR:
2388 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2389 break;
2391 case ID_EDIT_SELECTALL:
2393 CHARRANGE range = {0, -1};
2394 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2395 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2396 return 0;
2399 case ID_EDIT_GETTEXT:
2401 int nLen = GetWindowTextLengthW(hwndEditor);
2402 LPWSTR data = malloc((nLen+1)*sizeof(WCHAR) );
2403 TEXTRANGEW tr;
2405 GetWindowTextW(hwndEditor, data, nLen+1);
2406 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2408 free(data);
2409 data = malloc((nLen+1)*sizeof(WCHAR));
2410 tr.chrg.cpMin = 0;
2411 tr.chrg.cpMax = nLen;
2412 tr.lpstrText = data;
2413 SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2414 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2415 free(data);
2417 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2418 return 0;
2421 case ID_EDIT_CHARFORMAT:
2422 case ID_EDIT_DEFCHARFORMAT:
2424 CHARFORMAT2W cf;
2426 ZeroMemory(&cf, sizeof(cf));
2427 cf.cbSize = sizeof(cf);
2428 cf.dwMask = 0;
2429 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2430 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2431 return 0;
2434 case ID_EDIT_PARAFORMAT:
2436 PARAFORMAT2 pf;
2437 ZeroMemory(&pf, sizeof(pf));
2438 pf.cbSize = sizeof(pf);
2439 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2440 return 0;
2443 case ID_EDIT_SELECTIONINFO:
2445 CHARRANGE range = {0, -1};
2446 char buf[128];
2447 WCHAR *data = NULL;
2449 SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2450 data = malloc(sizeof(*data) * (range.cpMax-range.cpMin+1));
2451 SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2452 sprintf(buf, "Start = %ld, End = %ld", range.cpMin, range.cpMax);
2453 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2454 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2455 free(data);
2456 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2457 return 0;
2460 case ID_EDIT_READONLY:
2462 LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
2463 if (nStyle & ES_READONLY)
2464 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2465 else
2466 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2467 return 0;
2470 case ID_EDIT_MODIFIED:
2471 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2472 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2473 else
2474 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2475 return 0;
2477 case ID_EDIT_UNDO:
2478 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2479 return 0;
2481 case ID_EDIT_REDO:
2482 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2483 return 0;
2485 case ID_BULLETONOFF:
2486 case ID_BULLET:
2487 case ID_NUMBERING:
2488 case ID_LCLETTER:
2489 case ID_UCLETTER:
2490 case ID_LCROMAN:
2491 case ID_UCROMAN:
2493 PARAFORMAT2 pf;
2494 WORD new_number = LOWORD(wParam) - ID_BULLET + PFN_BULLET;
2495 pf.cbSize = sizeof(pf);
2496 pf.dwMask = PFM_NUMBERING;
2497 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2499 pf.dwMask = PFM_NUMBERING | PFM_NUMBERINGSTART | PFM_NUMBERINGSTYLE | PFM_NUMBERINGTAB | PFM_OFFSET | PFM_OFFSETINDENT;
2501 if(pf.wNumbering && ((pf.wNumbering == new_number) || (LOWORD(wParam) == ID_BULLETONOFF)))
2503 pf.wNumbering = 0;
2504 pf.wNumberingStart = 0;
2505 pf.wNumberingStyle = 0;
2506 pf.wNumberingTab = 0;
2507 pf.dxOffset = 0;
2508 pf.dxStartIndent = -360;
2509 } else
2511 pf.dxStartIndent = pf.wNumbering ? 0 : 360;
2513 if (LOWORD(wParam) == ID_BULLETONOFF)
2514 pf.wNumbering = last_bullet;
2515 else
2517 pf.wNumbering = new_number;
2518 last_bullet = pf.wNumbering;
2520 pf.wNumberingStart = 1;
2521 pf.wNumberingStyle = PFNS_PERIOD;
2522 pf.wNumberingTab = 360;
2523 pf.dxOffset = 360;
2526 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2528 break;
2530 case ID_ALIGN_LEFT:
2531 case ID_ALIGN_CENTER:
2532 case ID_ALIGN_RIGHT:
2534 PARAFORMAT2 pf;
2536 pf.cbSize = sizeof(pf);
2537 pf.dwMask = PFM_ALIGNMENT;
2538 switch(LOWORD(wParam)) {
2539 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2540 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2541 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2543 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2544 break;
2547 case ID_BACK_1:
2548 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2549 break;
2551 case ID_BACK_2:
2552 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2553 break;
2555 case ID_TOGGLE_TOOLBAR:
2556 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2557 update_window();
2558 break;
2560 case ID_TOGGLE_FORMATBAR:
2561 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2562 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2563 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2564 update_window();
2565 break;
2567 case ID_TOGGLE_STATUSBAR:
2568 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2569 update_window();
2570 break;
2572 case ID_TOGGLE_RULER:
2573 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2574 update_window();
2575 break;
2577 case ID_DATETIME:
2578 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2579 break;
2581 case ID_PARAFORMAT:
2582 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2583 break;
2585 case ID_TABSTOPS:
2586 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2587 break;
2589 case ID_ABOUT:
2590 dialog_about();
2591 break;
2593 case ID_VIEWPROPERTIES:
2594 dialog_viewproperties();
2595 break;
2597 case IDC_FONTLIST:
2598 if (HIWORD(wParam) == CBN_SELENDOK)
2600 WCHAR buffer[LF_FACESIZE];
2601 HWND hwndFontList = (HWND)lParam;
2602 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2603 on_fontlist_modified(buffer);
2605 break;
2607 case IDC_SIZELIST:
2608 if (HIWORD(wParam) == CBN_SELENDOK)
2610 WCHAR buffer[MAX_STRING_LEN+1];
2611 HWND hwndSizeList = (HWND)lParam;
2612 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2613 on_sizelist_modified(hwndSizeList, buffer);
2615 break;
2617 default:
2618 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2619 break;
2621 return 0;
2624 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2626 HMENU hMenu = (HMENU)wParam;
2627 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2628 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2629 PARAFORMAT pf;
2630 int nAlignment = -1;
2631 int selFrom, selTo;
2632 GETTEXTLENGTHEX gt;
2633 LRESULT textLength;
2634 MENUITEMINFOW mi;
2636 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2637 EnableMenuItem(hMenu, ID_EDIT_COPY, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2638 EnableMenuItem(hMenu, ID_EDIT_CUT, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2640 pf.cbSize = sizeof(PARAFORMAT);
2641 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2642 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2643 (GetWindowLongW(hwndEditor, GWL_STYLE) & ES_READONLY) ? MF_CHECKED : MF_UNCHECKED);
2644 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2645 SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED);
2646 if (pf.dwMask & PFM_ALIGNMENT)
2647 nAlignment = pf.wAlignment;
2648 CheckMenuItem(hMenu, ID_ALIGN_LEFT, (nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
2649 CheckMenuItem(hMenu, ID_ALIGN_CENTER, (nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
2650 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, (nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
2652 CheckMenuItem(hMenu, ID_BULLET, ((pf.wNumbering == PFN_BULLET) ? MF_CHECKED : MF_UNCHECKED));
2653 CheckMenuItem(hMenu, ID_NUMBERING, ((pf.wNumbering == PFN_ARABIC) ? MF_CHECKED : MF_UNCHECKED));
2654 CheckMenuItem(hMenu, ID_LCLETTER, ((pf.wNumbering == PFN_LCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2655 CheckMenuItem(hMenu, ID_UCLETTER, ((pf.wNumbering == PFN_UCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2656 CheckMenuItem(hMenu, ID_LCROMAN, ((pf.wNumbering == PFN_LCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2657 CheckMenuItem(hMenu, ID_UCROMAN, ((pf.wNumbering == PFN_UCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2659 EnableMenuItem(hMenu, ID_EDIT_UNDO, SendMessageW(hwndEditor, EM_CANUNDO, 0, 0) ?
2660 MF_ENABLED : MF_GRAYED);
2661 EnableMenuItem(hMenu, ID_EDIT_REDO, SendMessageW(hwndEditor, EM_CANREDO, 0, 0) ?
2662 MF_ENABLED : MF_GRAYED);
2664 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, is_bar_visible(BANDID_TOOLBAR) ?
2665 MF_CHECKED : MF_UNCHECKED);
2667 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, is_bar_visible(BANDID_FORMATBAR) ?
2668 MF_CHECKED : MF_UNCHECKED);
2670 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, IsWindowVisible(hwndStatus) ?
2671 MF_CHECKED : MF_UNCHECKED);
2673 CheckMenuItem(hMenu, ID_TOGGLE_RULER, is_bar_visible(BANDID_RULER) ? MF_CHECKED : MF_UNCHECKED);
2675 gt.flags = GTL_NUMCHARS;
2676 gt.codepage = 1200;
2677 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2678 EnableMenuItem(hMenu, ID_FIND, textLength ? MF_ENABLED : MF_GRAYED);
2680 mi.cbSize = sizeof(mi);
2681 mi.fMask = MIIM_DATA;
2683 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2685 EnableMenuItem(hMenu, ID_FIND_NEXT, (textLength && mi.dwItemData) ? MF_ENABLED : MF_GRAYED);
2687 EnableMenuItem(hMenu, ID_REPLACE, textLength ? MF_ENABLED : MF_GRAYED);
2689 return 0;
2692 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2694 int nStatusSize = 0;
2695 RECT rc;
2696 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2697 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2698 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2699 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2700 int rebarHeight = 0;
2702 if (hwndStatusBar)
2704 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2705 if (IsWindowVisible(hwndStatusBar))
2707 GetClientRect(hwndStatusBar, &rc);
2708 nStatusSize = rc.bottom - rc.top;
2709 } else
2711 nStatusSize = 0;
2714 if (hwndReBar)
2716 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2718 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2720 if (hwndEditor)
2722 GetClientRect(hWnd, &rc);
2723 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2726 redraw_ruler(hRulerWnd);
2728 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2731 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2733 if(msg == ID_FINDMSGSTRING)
2734 return handle_findmsg((LPFINDREPLACEW)lParam);
2736 switch(msg)
2738 case WM_CREATE:
2739 return OnCreate( hWnd );
2741 case WM_USER:
2742 return OnUser( hWnd );
2744 case WM_NOTIFY:
2745 return OnNotify( hWnd, lParam );
2747 case WM_COMMAND:
2748 if(preview_isactive())
2750 return preview_command( hWnd, wParam );
2753 return OnCommand( hWnd, wParam, lParam );
2755 case WM_DESTROY:
2756 PostQuitMessage(0);
2757 break;
2759 case WM_CLOSE:
2760 if(preview_isactive())
2762 preview_exit(hWnd);
2763 } else if(prompt_save_changes())
2765 registry_set_options(hMainWnd);
2766 registry_set_formatopts_all(barState, wordWrap);
2767 PostQuitMessage(0);
2769 break;
2771 case WM_ACTIVATE:
2772 if (LOWORD(wParam))
2773 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2774 return 0;
2776 case WM_INITMENUPOPUP:
2777 return OnInitPopupMenu( hWnd, wParam );
2779 case WM_SIZE:
2780 return OnSize( hWnd, wParam, lParam );
2782 case WM_CONTEXTMENU:
2783 return DefWindowProcW(hWnd, msg, wParam, lParam);
2785 case WM_DROPFILES:
2787 WCHAR file[MAX_PATH];
2788 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2789 DragFinish((HDROP)wParam);
2791 if(prompt_save_changes())
2792 DoOpenFile(file);
2794 break;
2795 case WM_PAINT:
2796 if(!preview_isactive())
2797 return DefWindowProcW(hWnd, msg, wParam, lParam);
2799 default:
2800 return DefWindowProcW(hWnd, msg, wParam, lParam);
2803 return 0;
2806 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2808 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2809 HACCEL hAccel;
2810 WNDCLASSEXW wc;
2811 MSG msg;
2812 RECT rc;
2813 UINT_PTR hPrevRulerProc;
2814 HWND hRulerWnd;
2815 POINTL EditPoint;
2816 DWORD bMaximized;
2817 MONITORINFO info;
2818 HMONITOR monitor;
2819 int x, y;
2820 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2821 'T','A','B','L','E','\0'};
2823 InitCommonControlsEx(&classes);
2825 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2827 wc.cbSize = sizeof(wc);
2828 wc.style = 0;
2829 wc.lpfnWndProc = WndProc;
2830 wc.cbClsExtra = 0;
2831 wc.cbWndExtra = 4;
2832 wc.hInstance = hInstance;
2833 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2834 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2835 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2836 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2837 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2838 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2839 wc.lpszClassName = wszMainWndClass;
2840 RegisterClassExW(&wc);
2842 wc.style = 0;
2843 wc.lpfnWndProc = preview_proc;
2844 wc.cbClsExtra = 0;
2845 wc.cbWndExtra = 0;
2846 wc.hInstance = hInstance;
2847 wc.hIcon = NULL;
2848 wc.hIconSm = NULL;
2849 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2850 wc.hbrBackground = NULL;
2851 wc.lpszMenuName = NULL;
2852 wc.lpszClassName = wszPreviewWndClass;
2853 RegisterClassExW(&wc);
2855 registry_read_winrect(&rc);
2856 monitor = MonitorFromRect(&rc, MONITOR_DEFAULTTOPRIMARY);
2857 info.cbSize = sizeof(info);
2858 GetMonitorInfoW(monitor, &info);
2860 x = rc.left;
2861 y = rc.top;
2862 IntersectRect(&info.rcWork, &info.rcWork, &rc);
2863 if (IsRectEmpty(&info.rcWork))
2864 x = y = CW_USEDEFAULT;
2866 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2867 x, y, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
2868 registry_read_maximized(&bMaximized);
2869 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2870 && bMaximized)
2871 nCmdShow = SW_SHOWMAXIMIZED;
2872 ShowWindow(hMainWnd, nCmdShow);
2874 set_caption(NULL);
2875 set_bar_states();
2876 set_fileformat(SF_RTF);
2877 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2878 get_default_printer_opts();
2879 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2881 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2882 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2883 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2884 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2886 HandleCommandLine(GetCommandLineW());
2888 while(GetMessageW(&msg,0,0,0))
2890 if (IsDialogMessageW(hFindWnd, &msg))
2891 continue;
2893 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2894 continue;
2895 TranslateMessage(&msg);
2896 DispatchMessageW(&msg);
2897 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2898 SendMessageW(hMainWnd, WM_USER, 0, 0);
2901 return 0;