wldap32/tests: Add ber_printf/scanf tests.
[wine/zf.git] / programs / wordpad / wordpad.c
blobd48346e3c4a18b2b93a57251dc0c7cb1e8c3502b
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 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
243 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
245 if(!wszCaption)
246 return;
248 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
249 length += lstrlenW(wszNewFileName);
250 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
251 length += ARRAY_SIZE(wszSeparator);
252 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
254 SetWindowTextW(hMainWnd, wszCaption);
256 HeapFree(GetProcessHeap(), 0, wszCaption);
259 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
261 if(punit != NULL)
262 *punit = UNIT_CM;
263 if(!endptr)
264 return FALSE;
265 if(!*endptr)
266 return TRUE;
268 while(*endptr == ' ')
269 endptr++;
271 if(punit == NULL)
272 return *endptr == '\0';
274 if(!lstrcmpW(endptr, units_cmW))
276 *punit = UNIT_CM;
277 endptr += lstrlenW(units_cmW);
279 else if (!lstrcmpW(endptr, units_inW))
281 *punit = UNIT_INCH;
282 endptr += lstrlenW(units_inW);
284 else if (!lstrcmpW(endptr, units_inchW))
286 *punit = UNIT_INCH;
287 endptr += lstrlenW(units_inchW);
289 else if (!lstrcmpW(endptr, units_ptW))
291 *punit = UNIT_PT;
292 endptr += lstrlenW(units_ptW);
295 return *endptr == '\0';
298 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
300 double ret;
301 WCHAR *endptr;
303 *num = 0;
304 errno = 0;
305 ret = wcstod(string, &endptr);
307 if (punit != NULL)
308 *punit = UNIT_CM;
309 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
311 return FALSE;
312 } else
314 *num = (float)ret;
315 return TRUE;
319 static void set_size(float size)
321 CHARFORMAT2W fmt;
323 ZeroMemory(&fmt, sizeof(fmt));
324 fmt.cbSize = sizeof(fmt);
325 fmt.dwMask = CFM_SIZE;
326 fmt.yHeight = (int)(size * 20.0);
327 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
330 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
332 WCHAR sizeBuffer[MAX_STRING_LEN];
333 CHARFORMAT2W format;
335 ZeroMemory(&format, sizeof(format));
336 format.cbSize = sizeof(format);
337 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
339 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
340 if(lstrcmpW(sizeBuffer, wszNewFontSize))
342 float size = 0;
343 if(number_from_string(wszNewFontSize, &size, NULL)
344 && size > 0)
346 set_size(size);
347 } else
349 SetWindowTextW(hwndSizeList, sizeBuffer);
350 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
351 wszAppTitle, MB_OK | MB_ICONINFORMATION);
356 static void add_size(HWND hSizeListWnd, unsigned size)
358 WCHAR buffer[3];
359 COMBOBOXEXITEMW cbItem;
360 cbItem.mask = CBEIF_TEXT;
361 cbItem.iItem = -1;
363 wsprintfW(buffer, stringFormat, size);
364 cbItem.pszText = buffer;
365 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
368 static void populate_size_list(HWND hSizeListWnd)
370 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
371 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
372 COMBOBOXEXITEMW cbFontItem;
373 CHARFORMAT2W fmt;
374 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
375 HDC hdc = GetDC(hMainWnd);
376 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
377 WCHAR buffer[3];
378 size_t i;
379 DWORD fontStyle;
381 ZeroMemory(&fmt, sizeof(fmt));
382 fmt.cbSize = sizeof(fmt);
383 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
385 cbFontItem.mask = CBEIF_LPARAM;
386 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
387 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
389 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
391 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
393 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
395 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
396 GetDeviceCaps(hdc, LOGPIXELSY)));
397 } else
399 for(i = 0; i < ARRAY_SIZE(choices); i++)
400 add_size(hSizeListWnd, choices[i]);
403 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
404 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
407 static void update_size_list(void)
409 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
410 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
411 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
412 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
413 CHARFORMAT2W fmt;
415 ZeroMemory(&fmt, sizeof(fmt));
416 fmt.cbSize = sizeof(fmt);
418 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
420 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
421 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
423 if(lstrcmpW(fontSize, sizeBuffer))
424 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
427 static void update_font_list(void)
429 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
430 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
431 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
432 WCHAR fontName[MAX_STRING_LEN];
433 CHARFORMAT2W fmt;
435 ZeroMemory(&fmt, sizeof(fmt));
436 fmt.cbSize = sizeof(fmt);
438 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
439 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
441 if(lstrcmpW(fontName, fmt.szFaceName))
443 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
444 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
445 } else
447 update_size_list();
451 static void clear_formatting(void)
453 PARAFORMAT2 pf;
455 pf.cbSize = sizeof(pf);
456 pf.dwMask = PFM_ALIGNMENT;
457 pf.wAlignment = PFA_LEFT;
458 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
461 static int fileformat_number(WPARAM format)
463 int number = 0;
465 if(format == SF_TEXT)
467 number = 1;
468 } else if (format == (SF_TEXT | SF_UNICODE))
470 number = 2;
472 return number;
475 static WPARAM fileformat_flags(int format)
477 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
479 return flags[format];
482 static void set_font(LPCWSTR wszFaceName)
484 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
485 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
486 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
487 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
488 CHARFORMAT2W fmt;
490 ZeroMemory(&fmt, sizeof(fmt));
492 fmt.cbSize = sizeof(fmt);
493 fmt.dwMask = CFM_FACE;
495 lstrcpyW(fmt.szFaceName, wszFaceName);
497 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
499 populate_size_list(hSizeListWnd);
501 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
504 static void set_default_font(void)
506 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
507 'R','o','m','a','n',0};
508 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
509 CHARFORMAT2W fmt;
510 LPCWSTR font;
512 ZeroMemory(&fmt, sizeof(fmt));
514 fmt.cbSize = sizeof(fmt);
515 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
516 fmt.dwEffects = 0;
518 if(fileFormat & SF_RTF)
519 font = richTextFont;
520 else
521 font = plainTextFont;
523 lstrcpyW(fmt.szFaceName, font);
525 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
528 static void on_fontlist_modified(LPWSTR wszNewFaceName)
530 CHARFORMAT2W format;
531 ZeroMemory(&format, sizeof(format));
532 format.cbSize = sizeof(format);
533 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
535 if(lstrcmpW(format.szFaceName, wszNewFaceName))
536 set_font(wszNewFaceName);
539 static void dialog_choose_font(void)
541 CHOOSEFONTW cf;
542 LOGFONTW lf;
543 CHARFORMAT2W fmt;
544 HDC hDC = GetDC(hMainWnd);
546 ZeroMemory(&cf, sizeof(cf));
547 cf.lStructSize = sizeof(cf);
548 cf.hwndOwner = hMainWnd;
549 cf.lpLogFont = &lf;
550 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_NOVERTFONTS;
552 ZeroMemory(&fmt, sizeof(fmt));
553 fmt.cbSize = sizeof(fmt);
555 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
556 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
557 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
558 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
559 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
560 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
561 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
562 cf.rgbColors = fmt.crTextColor;
564 if(ChooseFontW(&cf))
566 ZeroMemory(&fmt, sizeof(fmt));
567 fmt.cbSize = sizeof(fmt);
568 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
569 fmt.yHeight = cf.iPointSize * 2;
571 if(cf.nFontType & BOLD_FONTTYPE)
572 fmt.dwEffects |= CFE_BOLD;
573 if(cf.nFontType & ITALIC_FONTTYPE)
574 fmt.dwEffects |= CFE_ITALIC;
575 if(cf.lpLogFont->lfUnderline)
576 fmt.dwEffects |= CFE_UNDERLINE;
577 if(cf.lpLogFont->lfStrikeOut)
578 fmt.dwEffects |= CFE_STRIKEOUT;
580 fmt.crTextColor = cf.rgbColors;
582 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
583 set_font(cf.lpLogFont->lfFaceName);
587 struct font_desc
589 WCHAR *name;
590 LPARAM lParam;
593 struct font_array
595 struct font_desc *fonts;
596 size_t count;
597 size_t capacity;
600 static BOOL array_reserve(void **elements, size_t *capacity, size_t count, size_t size)
602 size_t new_capacity, max_capacity;
603 void *new_elements;
605 if (count <= *capacity)
606 return TRUE;
608 max_capacity = ~(SIZE_T)0 / size;
609 if (count > max_capacity)
610 return FALSE;
612 new_capacity = max(4, *capacity);
613 while (new_capacity < count && new_capacity <= max_capacity / 2)
614 new_capacity *= 2;
615 if (new_capacity < count)
616 new_capacity = max_capacity;
618 new_elements = *elements ? HeapReAlloc(GetProcessHeap(), 0, *elements, new_capacity * size) :
619 HeapAlloc(GetProcessHeap(), 0, new_capacity * size);
620 if (!new_elements)
621 return FALSE;
623 *elements = new_elements;
624 *capacity = new_capacity;
626 return TRUE;
629 static void add_font(struct font_array *fonts, LPCWSTR fontName, DWORD fontType, const NEWTEXTMETRICEXW *ntmc)
631 int fontHeight = 0;
632 size_t idx;
634 if (!array_reserve((void **)&fonts->fonts, &fonts->capacity, fonts->count + 1, sizeof(*fonts->fonts)))
635 return;
637 if (fontType & RASTER_FONTTYPE)
638 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
640 idx = fonts->count;
641 fonts->fonts[idx].name = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
642 lstrcpyW( fonts->fonts[idx].name, fontName );
643 fonts->fonts[idx].lParam = MAKELONG(fontType, fontHeight);
645 fonts->count++;
648 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
649 DWORD FontType, LPARAM lParam)
651 struct font_array *fonts = (void *)lParam;
653 if (lpelfe->lfFaceName[0] == '@') return 1; /* ignore vertical fonts */
655 add_font(fonts, lpelfe->lfFaceName, FontType, (const NEWTEXTMETRICEXW *)lpntme);
657 return 1;
660 static int __cdecl fonts_desc_compare(const void *a, const void *b)
662 const struct font_desc *left = a, *right = b;
663 return lstrcmpiW(left->name, right->name);
666 static void populate_font_list(HWND hListWnd)
668 struct font_array font_array = { 0 };
669 HDC hdc = GetDC(hMainWnd);
670 LOGFONTW fontinfo;
671 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
672 CHARFORMAT2W fmt;
673 size_t i, j;
675 fontinfo.lfCharSet = DEFAULT_CHARSET;
676 *fontinfo.lfFaceName = '\0';
677 fontinfo.lfPitchAndFamily = 0;
679 /* Collect font names, sort, remove duplicates. */
680 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc, (LPARAM)&font_array, 0);
682 qsort(font_array.fonts, font_array.count, sizeof(*font_array.fonts), fonts_desc_compare);
684 for (i = 1, j = 0; i < font_array.count; ++i)
686 if (!lstrcmpiW(font_array.fonts[i].name, font_array.fonts[j].name))
688 HeapFree(GetProcessHeap(), 0, font_array.fonts[i].name);
689 font_array.fonts[i].name = NULL;
691 else if (++j != i)
693 font_array.fonts[j] = font_array.fonts[i];
694 font_array.fonts[i].name = NULL;
697 font_array.count = j + 1;
699 for (i = 0; i < font_array.count; ++i)
701 COMBOBOXEXITEMW cbitem = { 0 };
703 cbitem.mask = CBEIF_TEXT | CBEIF_LPARAM;
704 cbitem.pszText = font_array.fonts[i].name;
705 cbitem.iItem = -1;
706 cbitem.lParam = font_array.fonts[i].lParam;
708 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbitem);
710 HeapFree(GetProcessHeap(), 0, font_array.fonts[i].name);
712 HeapFree(GetProcessHeap(), 0, font_array.fonts);
714 ZeroMemory(&fmt, sizeof(fmt));
715 fmt.cbSize = sizeof(fmt);
716 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
717 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
720 static void update_window(void)
722 RECT rect;
724 GetClientRect(hMainWnd, &rect);
726 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
729 static BOOL is_bar_visible(int bandId)
731 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
734 static void store_bar_state(int bandId, BOOL show)
736 int formatIndex = reg_formatindex(fileFormat);
738 if(show)
739 barState[formatIndex] |= (1 << bandId);
740 else
741 barState[formatIndex] &= ~(1 << bandId);
744 static void set_toolbar_state(int bandId, BOOL show)
746 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
748 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
750 if(bandId == BANDID_TOOLBAR)
752 REBARBANDINFOW rbbinfo;
753 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
755 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
756 rbbinfo.fMask = RBBIM_STYLE;
758 SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
760 if(!show)
761 rbbinfo.fStyle &= ~RBBS_BREAK;
762 else
763 rbbinfo.fStyle |= RBBS_BREAK;
765 SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
768 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
769 store_bar_state(bandId, show);
772 static void set_statusbar_state(BOOL show)
774 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
776 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
777 store_bar_state(BANDID_STATUSBAR, show);
780 static void set_bar_states(void)
782 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
783 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
784 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
785 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
786 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
787 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
789 update_window();
792 static void preview_exit(HWND hMainWnd)
794 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
795 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
797 set_bar_states();
798 ShowWindow(hEditorWnd, TRUE);
800 close_preview(hMainWnd);
802 SetMenu(hMainWnd, hMenu);
803 registry_read_filelist(hMainWnd);
805 update_window();
808 static void set_fileformat(WPARAM format)
810 fileFormat = format;
812 set_bar_states();
813 set_default_font();
814 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
817 static void ShowOpenError(DWORD Code)
819 LPWSTR Message;
821 switch(Code)
823 case ERROR_ACCESS_DENIED:
824 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
825 break;
827 default:
828 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
830 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
833 static void DoOpenFile(LPCWSTR szOpenFileName)
835 HANDLE hFile;
836 EDITSTREAM es;
837 char fileStart[5];
838 DWORD readOut;
839 WPARAM format = SF_TEXT;
841 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
842 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
843 if (hFile == INVALID_HANDLE_VALUE)
845 ShowOpenError(GetLastError());
846 return;
849 ReadFile(hFile, fileStart, 5, &readOut, NULL);
850 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
852 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
854 format = SF_TEXT | SF_UNICODE;
855 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
856 } else if(readOut >= 5)
858 static const char header[] = "{\\rtf";
859 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
861 if(!memcmp(header, fileStart, 5))
862 format = SF_RTF;
863 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
865 CloseHandle(hFile);
866 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
867 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
868 return;
872 es.dwCookie = (DWORD_PTR)hFile;
873 es.pfnCallback = stream_in;
875 clear_formatting();
876 set_fileformat(format);
877 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
879 CloseHandle(hFile);
881 SetFocus(hEditorWnd);
883 set_caption(szOpenFileName);
885 lstrcpyW(wszFileName, szOpenFileName);
886 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
887 registry_set_filelist(szOpenFileName, hMainWnd);
888 update_font_list();
891 static void ShowWriteError(DWORD Code)
893 LPWSTR Message;
895 switch(Code)
897 case ERROR_ACCESS_DENIED:
898 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
899 break;
901 default:
902 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
904 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
907 static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
909 HANDLE hFile;
910 EDITSTREAM stream;
911 LRESULT ret;
913 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
914 FILE_ATTRIBUTE_NORMAL, NULL);
916 if(hFile == INVALID_HANDLE_VALUE)
918 ShowWriteError(GetLastError());
919 return FALSE;
922 if(format == (SF_TEXT | SF_UNICODE))
924 static const BYTE unicode[] = {0xff,0xfe};
925 DWORD writeOut;
926 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
928 if(writeOut != sizeof(unicode))
930 CloseHandle(hFile);
931 return FALSE;
935 stream.dwCookie = (DWORD_PTR)hFile;
936 stream.pfnCallback = stream_out;
938 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
940 CloseHandle(hFile);
942 SetFocus(hEditorWnd);
944 if(!ret)
946 GETTEXTLENGTHEX gt;
947 gt.flags = GTL_DEFAULT;
948 gt.codepage = 1200;
950 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
951 return FALSE;
954 lstrcpyW(wszFileName, wszSaveFileName);
955 set_caption(wszFileName);
956 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
957 set_fileformat(format);
959 return TRUE;
962 static BOOL DialogSaveFile(void)
964 OPENFILENAMEW sfn;
966 WCHAR wszFile[MAX_PATH] = {'\0'};
967 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
969 ZeroMemory(&sfn, sizeof(sfn));
971 sfn.lStructSize = sizeof(sfn);
972 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
973 sfn.hwndOwner = hMainWnd;
974 sfn.lpstrFilter = wszFilter;
975 sfn.lpstrFile = wszFile;
976 sfn.nMaxFile = MAX_PATH;
977 sfn.lpstrDefExt = wszDefExt;
978 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
980 while(GetSaveFileNameW(&sfn))
982 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
984 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
985 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
986 continue;
988 return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
990 return FALSE;
993 static BOOL prompt_save_changes(void)
995 if(!wszFileName[0])
997 GETTEXTLENGTHEX gt;
998 gt.flags = GTL_NUMCHARS;
999 gt.codepage = 1200;
1000 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
1001 return TRUE;
1004 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
1006 return TRUE;
1007 } else
1009 LPWSTR displayFileName;
1010 WCHAR *text;
1011 int ret;
1013 if(!wszFileName[0])
1014 displayFileName = wszDefaultFileName;
1015 else
1016 displayFileName = file_basename(wszFileName);
1018 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1019 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
1021 if(!text)
1022 return FALSE;
1024 wsprintfW(text, wszSaveChanges, displayFileName);
1026 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
1028 HeapFree(GetProcessHeap(), 0, text);
1030 switch(ret)
1032 case IDNO:
1033 return TRUE;
1035 case IDYES:
1036 if(wszFileName[0])
1037 return DoSaveFile(wszFileName, fileFormat);
1038 return DialogSaveFile();
1040 default:
1041 return FALSE;
1046 static void DialogOpenFile(void)
1048 OPENFILENAMEW ofn;
1050 WCHAR wszFile[MAX_PATH] = {'\0'};
1051 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
1053 ZeroMemory(&ofn, sizeof(ofn));
1055 ofn.lStructSize = sizeof(ofn);
1056 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
1057 ofn.hwndOwner = hMainWnd;
1058 ofn.lpstrFilter = wszFilter;
1059 ofn.lpstrFile = wszFile;
1060 ofn.nMaxFile = MAX_PATH;
1061 ofn.lpstrDefExt = wszDefExt;
1062 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
1064 if(GetOpenFileNameW(&ofn))
1066 if(prompt_save_changes())
1067 DoOpenFile(ofn.lpstrFile);
1071 static void dialog_about(void)
1073 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1074 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1077 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1079 switch(message)
1081 case WM_INITDIALOG:
1083 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1084 int wrap = -1;
1085 char id[4];
1086 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1088 sprintf(id, "%d\n", (int)ps->lParam);
1089 SetWindowTextA(hIdWnd, id);
1090 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1091 wrap = IDC_PAGEFMT_WN;
1092 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1093 wrap = IDC_PAGEFMT_WW;
1094 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1095 wrap = IDC_PAGEFMT_WM;
1097 if(wrap != -1)
1098 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1099 IDC_PAGEFMT_WM, wrap);
1101 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1102 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1103 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1104 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1105 if(barState[ps->lParam] & (1 << BANDID_RULER))
1106 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1107 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1108 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1110 break;
1112 case WM_COMMAND:
1113 switch(LOWORD(wParam))
1115 case IDC_PAGEFMT_WN:
1116 case IDC_PAGEFMT_WW:
1117 case IDC_PAGEFMT_WM:
1118 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1119 LOWORD(wParam));
1120 break;
1122 case IDC_PAGEFMT_TB:
1123 case IDC_PAGEFMT_FB:
1124 case IDC_PAGEFMT_RU:
1125 case IDC_PAGEFMT_SB:
1126 CheckDlgButton(hWnd, LOWORD(wParam),
1127 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1128 break;
1130 break;
1131 case WM_NOTIFY:
1133 LPNMHDR header = (LPNMHDR)lParam;
1134 if(header->code == PSN_APPLY)
1136 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1137 char sid[4];
1138 int id;
1140 GetWindowTextA(hIdWnd, sid, 4);
1141 id = atoi(sid);
1142 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1143 wordWrap[id] = ID_WORDWRAP_NONE;
1144 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1145 wordWrap[id] = ID_WORDWRAP_WINDOW;
1146 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1147 wordWrap[id] = ID_WORDWRAP_MARGIN;
1149 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1150 barState[id] |= (1 << BANDID_TOOLBAR);
1151 else
1152 barState[id] &= ~(1 << BANDID_TOOLBAR);
1154 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1155 barState[id] |= (1 << BANDID_FORMATBAR);
1156 else
1157 barState[id] &= ~(1 << BANDID_FORMATBAR);
1159 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1160 barState[id] |= (1 << BANDID_RULER);
1161 else
1162 barState[id] &= ~(1 << BANDID_RULER);
1164 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1165 barState[id] |= (1 << BANDID_STATUSBAR);
1166 else
1167 barState[id] &= ~(1 << BANDID_STATUSBAR);
1170 break;
1172 return FALSE;
1175 static void dialog_viewproperties(void)
1177 PROPSHEETPAGEW psp[2];
1178 PROPSHEETHEADERW psh;
1179 size_t i;
1180 HINSTANCE hInstance = GetModuleHandleW(0);
1181 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1183 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1184 psp[0].dwFlags = PSP_USETITLE;
1185 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1186 psp[0].pfnDlgProc = formatopts_proc;
1187 psp[0].hInstance = hInstance;
1188 psp[0].lParam = reg_formatindex(SF_TEXT);
1189 psp[0].pfnCallback = NULL;
1190 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1191 for(i = 1; i < ARRAY_SIZE(psp); i++)
1193 psp[i].dwSize = psp[0].dwSize;
1194 psp[i].dwFlags = psp[0].dwFlags;
1195 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1196 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1197 psp[i].hInstance = psp[0].hInstance;
1198 psp[i].lParam = reg_formatindex(SF_RTF);
1199 psp[i].pfnCallback = psp[0].pfnCallback;
1200 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1203 psh.dwSize = sizeof(psh);
1204 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1205 psh.hwndParent = hMainWnd;
1206 psh.hInstance = hInstance;
1207 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1208 psh.nPages = ARRAY_SIZE(psp);
1209 U3(psh).ppsp = ppsp;
1210 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1212 if(fileFormat & SF_RTF)
1213 U2(psh).nStartPage = 1;
1214 else
1215 U2(psh).nStartPage = 0;
1216 PropertySheetW(&psh);
1217 set_bar_states();
1218 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1221 static void HandleCommandLine(LPWSTR cmdline)
1223 WCHAR delimiter;
1224 BOOL opt_print = FALSE;
1226 /* skip white space */
1227 while (*cmdline == ' ') cmdline++;
1229 /* skip executable name */
1230 delimiter = (*cmdline == '"' ? '"' : ' ');
1232 if (*cmdline == delimiter) cmdline++;
1233 while (*cmdline && *cmdline != delimiter) cmdline++;
1234 if (*cmdline == delimiter) cmdline++;
1236 while (*cmdline)
1238 while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
1240 if (*cmdline == '-' || *cmdline == '/')
1242 if (!cmdline[2] || isspace(cmdline[2]))
1244 switch (cmdline[1])
1246 case 'P':
1247 case 'p':
1248 opt_print = TRUE;
1249 cmdline += 2;
1250 continue;
1253 /* a filename starting by / */
1255 break;
1258 if (*cmdline)
1260 /* file name is passed on the command line */
1261 if (cmdline[0] == '"')
1263 cmdline++;
1264 cmdline[lstrlenW(cmdline) - 1] = 0;
1266 DoOpenFile(cmdline);
1267 InvalidateRect(hMainWnd, NULL, FALSE);
1270 if (opt_print)
1271 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1274 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1276 if(pFr->Flags & FR_DIALOGTERM)
1278 hFindWnd = 0;
1279 pFr->Flags = FR_FINDNEXT;
1280 return 0;
1283 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1285 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1286 DWORD flags;
1287 FINDTEXTEXW ft;
1288 CHARRANGE sel;
1289 LRESULT ret = -1;
1290 HMENU hMenu = GetMenu(hMainWnd);
1291 MENUITEMINFOW mi;
1293 mi.cbSize = sizeof(mi);
1294 mi.fMask = MIIM_DATA;
1295 mi.dwItemData = 1;
1296 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1298 /* Make sure find field is saved. */
1299 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1301 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1302 ARRAY_SIZE(custom_data->findBuffer));
1303 pFr->lpstrFindWhat = custom_data->findBuffer;
1306 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1307 if(custom_data->endPos == -1) {
1308 custom_data->endPos = sel.cpMin;
1309 custom_data->wrapped = FALSE;
1312 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1313 ft.lpstrText = pFr->lpstrFindWhat;
1315 /* Only replace the existing selection if it is an exact match. */
1316 if (sel.cpMin != sel.cpMax &&
1317 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1319 ft.chrg = sel;
1320 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1321 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1322 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1323 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1327 /* Search from the start of the selection, but exclude the first character
1328 * from search if there is a selection. */
1329 ft.chrg.cpMin = sel.cpMin;
1330 if (sel.cpMin != sel.cpMax)
1331 ft.chrg.cpMin++;
1333 /* Search to the end, then wrap around and search from the start. */
1334 if (!custom_data->wrapped) {
1335 ft.chrg.cpMax = -1;
1336 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1337 if (ret == -1) {
1338 custom_data->wrapped = TRUE;
1339 ft.chrg.cpMin = 0;
1343 if (ret == -1) {
1344 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1345 if (ft.chrg.cpMax > ft.chrg.cpMin)
1346 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1349 if (ret == -1) {
1350 custom_data->endPos = -1;
1351 EnableWindow(hMainWnd, FALSE);
1352 MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1353 wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
1354 EnableWindow(hMainWnd, TRUE);
1355 } else {
1356 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1357 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1359 if (pFr->Flags & FR_REPLACEALL)
1360 return handle_findmsg(pFr);
1364 return 0;
1367 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1369 static WCHAR selBuffer[128];
1370 static WCHAR replaceBuffer[128];
1371 static FINDREPLACE_custom custom_data;
1372 static const WCHAR endl = '\r';
1373 FINDTEXTW ft;
1375 /* Allow only one search/replace dialog to open */
1376 if(hFindWnd != NULL)
1378 SetActiveWindow(hFindWnd);
1379 return;
1382 ZeroMemory(fr, sizeof(FINDREPLACEW));
1383 fr->lStructSize = sizeof(FINDREPLACEW);
1384 fr->hwndOwner = hMainWnd;
1385 fr->Flags = FR_HIDEUPDOWN;
1386 /* Find field is filled with the selected text if it is non-empty
1387 * and stays within the same paragraph, otherwise the previous
1388 * find field is used. */
1389 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1390 (LPARAM)&ft.chrg.cpMax);
1391 ft.lpstrText = &endl;
1392 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1393 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1395 /* Use a temporary buffer for the selected text so that the saved
1396 * find field is only overwritten when a find/replace is clicked. */
1397 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1398 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1399 fr->lpstrFindWhat = selBuffer;
1400 } else {
1401 fr->lpstrFindWhat = custom_data.findBuffer;
1403 fr->lpstrReplaceWith = replaceBuffer;
1404 custom_data.endPos = -1;
1405 custom_data.wrapped = FALSE;
1406 fr->lCustData = (LPARAM)&custom_data;
1407 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1408 fr->wReplaceWithLen = sizeof(replaceBuffer);
1410 if(replace)
1411 hFindWnd = ReplaceTextW(fr);
1412 else
1413 hFindWnd = FindTextW(fr);
1416 static int units_to_twips(UNIT unit, float number)
1418 int twips = 0;
1420 switch(unit)
1422 case UNIT_CM:
1423 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1424 break;
1426 case UNIT_INCH:
1427 twips = (int)(number * (float)TWIPS_PER_INCH);
1428 break;
1430 case UNIT_PT:
1431 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1432 break;
1435 return twips;
1438 static void append_current_units(LPWSTR buffer)
1440 static const WCHAR space[] = {' ', 0};
1441 lstrcatW(buffer, space);
1442 lstrcatW(buffer, units_cmW);
1445 static void number_with_units(LPWSTR buffer, int number)
1447 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1448 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1450 swprintf(buffer, MAX_STRING_LEN, fmt, converted, units_cmW);
1453 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1455 COMBOBOXEXITEMW cbItem;
1456 COMBOBOXINFO cbInfo;
1457 HWND hCombo, hList;
1458 int idx, result;
1460 hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1461 if (!hCombo)
1462 return FALSE;
1463 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1464 result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1465 if (!result)
1466 return FALSE;
1467 hList = cbInfo.hwndList;
1468 idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
1469 if (idx < 0)
1470 return FALSE;
1472 ZeroMemory(&cbItem, sizeof(cbItem));
1473 cbItem.mask = CBEIF_TEXT;
1474 cbItem.iItem = idx;
1475 cbItem.pszText = wszBuffer;
1476 cbItem.cchTextMax = bufferLength-1;
1477 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1479 return result != 0;
1482 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1484 switch(message)
1486 case WM_INITDIALOG:
1488 WCHAR buffer[MAX_STRING_LEN];
1489 SYSTEMTIME st;
1490 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1491 GetLocalTime(&st);
1493 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1494 MAX_STRING_LEN);
1495 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1496 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1497 MAX_STRING_LEN);
1498 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1499 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1500 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1502 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1504 break;
1506 case WM_COMMAND:
1507 switch(LOWORD(wParam))
1509 case IDC_DATETIME:
1510 if (HIWORD(wParam) != LBN_DBLCLK)
1511 break;
1512 /* Fall through */
1514 case IDOK:
1516 LRESULT index;
1517 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1519 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1521 if(index != LB_ERR)
1523 WCHAR buffer[MAX_STRING_LEN];
1524 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1525 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1528 /* Fall through */
1530 case IDCANCEL:
1531 EndDialog(hWnd, wParam);
1532 return TRUE;
1535 return FALSE;
1538 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1540 switch(message)
1542 case WM_INITDIALOG:
1544 HINSTANCE hInstance = GetModuleHandleW(0);
1545 WCHAR buffer[MAX_STRING_LEN];
1546 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1548 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1549 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1550 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1551 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1552 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1553 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1555 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1557 break;
1559 case WM_COMMAND:
1560 switch(LOWORD(wParam))
1562 case IDOK:
1564 LRESULT index;
1565 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1566 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1568 if(index != LB_ERR)
1569 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1571 return TRUE;
1573 case IDCANCEL:
1574 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1575 return TRUE;
1578 return FALSE;
1581 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1583 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1585 switch(message)
1587 case WM_INITDIALOG:
1589 HINSTANCE hInstance = GetModuleHandleW(0);
1590 WCHAR buffer[MAX_STRING_LEN];
1591 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1592 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1593 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1594 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1595 PARAFORMAT2 pf;
1596 int index = 0;
1598 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1599 MAX_STRING_LEN);
1600 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1601 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1602 MAX_STRING_LEN);
1603 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1604 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1605 MAX_STRING_LEN);
1606 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1608 pf.cbSize = sizeof(pf);
1609 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1610 PFM_STARTINDENT;
1611 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1613 if(pf.wAlignment == PFA_RIGHT)
1614 index ++;
1615 else if(pf.wAlignment == PFA_CENTER)
1616 index += 2;
1618 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1620 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1621 SetWindowTextW(hLeftWnd, buffer);
1622 number_with_units(buffer, pf.dxRightIndent);
1623 SetWindowTextW(hRightWnd, buffer);
1624 number_with_units(buffer, -pf.dxOffset);
1625 SetWindowTextW(hFirstWnd, buffer);
1627 break;
1629 case WM_COMMAND:
1630 switch(LOWORD(wParam))
1632 case IDOK:
1634 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1635 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1636 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1637 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1638 WCHAR buffer[MAX_STRING_LEN];
1639 int index;
1640 float num;
1641 int ret = 0;
1642 PARAFORMAT2 pf;
1643 UNIT unit;
1644 BOOL in_list = FALSE;
1646 pf.cbSize = sizeof(pf);
1647 pf.dwMask = PFM_NUMBERING;
1648 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1649 if ((pf.dwMask & PFM_NUMBERING) && pf.wNumbering)
1650 in_list = TRUE;
1652 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1653 pf.wAlignment = ALIGNMENT_VALUES[index];
1655 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1656 if(number_from_string(buffer, &num, &unit))
1657 ret++;
1658 pf.dxOffset = units_to_twips(unit, num);
1659 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1660 if(number_from_string(buffer, &num, &unit))
1661 ret++;
1662 pf.dxRightIndent = units_to_twips(unit, num);
1663 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1664 if(number_from_string(buffer, &num, &unit))
1665 ret++;
1666 pf.dxStartIndent = units_to_twips(unit, num);
1668 if(ret != 3)
1670 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1671 wszAppTitle, MB_OK | MB_ICONASTERISK);
1672 return FALSE;
1673 } else
1675 if (pf.dxOffset + pf.dxStartIndent < 0
1676 && pf.dxStartIndent < 0)
1678 /* The first line is before the left edge, so
1679 * make sure it is at the left edge. */
1680 pf.dxOffset = -pf.dxStartIndent;
1681 } else if (pf.dxOffset < 0) {
1682 /* The second and following lines are before
1683 * the left edge, so set it to be at the left
1684 * edge, and adjust the first line since it
1685 * is relative to it. */
1686 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1687 pf.dxOffset = 0;
1689 /* Internally the dxStartIndent is the absolute
1690 * offset for the first line and dxOffset is
1691 * to it value as opposed how it is displayed with
1692 * the first line being the relative value.
1693 * These two lines make the adjustments. */
1694 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1695 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1697 pf.cbSize = sizeof(pf);
1698 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1699 PFM_STARTINDENT;
1700 if (in_list)
1702 pf.wNumberingTab = max(pf.dxOffset, 0);
1703 pf.dwMask |= PFM_NUMBERINGTAB;
1706 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1709 /* Fall through */
1711 case IDCANCEL:
1712 EndDialog(hWnd, wParam);
1713 return TRUE;
1716 return FALSE;
1719 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1721 switch(message)
1723 case WM_INITDIALOG:
1725 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1726 PARAFORMAT pf;
1727 WCHAR buffer[MAX_STRING_LEN];
1728 int i;
1730 pf.cbSize = sizeof(pf);
1731 pf.dwMask = PFM_TABSTOPS;
1732 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1733 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1735 for(i = 0; i < pf.cTabCount; i++)
1737 number_with_units(buffer, pf.rgxTabs[i]);
1738 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1740 SetFocus(hTabWnd);
1742 break;
1744 case WM_COMMAND:
1745 switch(LOWORD(wParam))
1747 case IDC_TABSTOPS:
1749 HWND hTabWnd = (HWND)lParam;
1750 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1751 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1752 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1754 if(GetWindowTextLengthW(hTabWnd))
1755 EnableWindow(hAddWnd, TRUE);
1756 else
1757 EnableWindow(hAddWnd, FALSE);
1759 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1761 EnableWindow(hEmptyWnd, TRUE);
1763 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1764 EnableWindow(hDelWnd, FALSE);
1765 else
1766 EnableWindow(hDelWnd, TRUE);
1767 } else
1769 EnableWindow(hEmptyWnd, FALSE);
1772 break;
1774 case ID_TAB_ADD:
1776 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1777 WCHAR buffer[MAX_STRING_LEN];
1778 UNIT unit;
1780 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1781 append_current_units(buffer);
1783 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1785 float number = 0;
1786 int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
1788 if(!number_from_string(buffer, &number, &unit))
1790 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1791 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1792 } else if (item_count >= MAX_TAB_STOPS) {
1793 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1794 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1795 } else {
1796 int i;
1797 float next_number = -1;
1798 int next_number_in_twips = -1;
1799 int insert_number = units_to_twips(unit, number);
1801 /* linear search for position to insert the string */
1802 for(i = 0; i < item_count; i++)
1804 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1805 number_from_string(buffer, &next_number, &unit);
1806 next_number_in_twips = units_to_twips(unit, next_number);
1807 if (insert_number <= next_number_in_twips)
1808 break;
1810 if (insert_number != next_number_in_twips)
1812 number_with_units(buffer, insert_number);
1813 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1814 SetWindowTextW(hTabWnd, 0);
1818 SetFocus(hTabWnd);
1820 break;
1822 case ID_TAB_DEL:
1824 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1825 LRESULT ret;
1826 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1827 if(ret != CB_ERR)
1828 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1830 break;
1832 case ID_TAB_EMPTY:
1834 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1835 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1836 SetFocus(hTabWnd);
1838 break;
1840 case IDOK:
1842 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1843 int i;
1844 WCHAR buffer[MAX_STRING_LEN];
1845 PARAFORMAT pf;
1846 float number;
1847 UNIT unit;
1849 pf.cbSize = sizeof(pf);
1850 pf.dwMask = PFM_TABSTOPS;
1852 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1853 (LPARAM)&buffer) != CB_ERR &&
1854 i < MAX_TAB_STOPS; i++)
1856 number_from_string(buffer, &number, &unit);
1857 pf.rgxTabs[i] = units_to_twips(unit, number);
1859 pf.cTabCount = i;
1860 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1862 /* Fall through */
1863 case IDCANCEL:
1864 EndDialog(hWnd, wParam);
1865 return TRUE;
1868 return FALSE;
1871 static LRESULT OnCreate( HWND hWnd )
1873 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1874 HINSTANCE hInstance = GetModuleHandleW(0);
1875 HANDLE hDLL;
1876 TBADDBITMAP ab;
1877 int nStdBitmaps = 0;
1878 REBARINFO rbi;
1879 REBARBANDINFOW rbb;
1880 RECT rect;
1881 HFONT font;
1882 HDC hdc;
1883 SIZE name_sz, size_sz;
1884 int height;
1885 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1886 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1887 static const WCHAR font_text[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0}; /* a long font name */
1888 static const WCHAR size_text[] = {' ','0','0',0}; /* enough for two digits */
1890 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1892 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1893 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1894 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1896 rbi.cbSize = sizeof(rbi);
1897 rbi.fMask = 0;
1898 rbi.himl = NULL;
1899 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1900 return -1;
1902 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS,
1903 IDC_TOOLBAR,
1904 1, hInstance, IDB_TOOLBAR,
1905 NULL, 0,
1906 24, 24, 16, 16, sizeof(TBBUTTON));
1908 ab.hInst = HINST_COMMCTRL;
1909 ab.nID = IDB_STD_SMALL_COLOR;
1910 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1912 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1913 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1914 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1915 AddSeparator(hToolBarWnd);
1916 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1917 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1918 AddSeparator(hToolBarWnd);
1919 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1920 AddSeparator(hToolBarWnd);
1921 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1922 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1923 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1924 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1925 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1926 AddSeparator(hToolBarWnd);
1927 AddButton(hToolBarWnd, 0, ID_DATETIME);
1929 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1930 height = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1932 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1933 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1934 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1935 GetWindowRect(hFontListWnd, &rect);
1936 height = max(height, rect.bottom - rect.top);
1938 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1939 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1940 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1941 rbb.cx = 0;
1942 rbb.hwndChild = hToolBarWnd;
1943 rbb.cxMinChild = 0;
1944 rbb.cyChild = rbb.cyMinChild = height;
1945 rbb.wID = BANDID_TOOLBAR;
1947 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1949 font = (HFONT)SendMessageW(hFontListWnd, WM_GETFONT, 0, 0);
1950 hdc = GetDC(hFontListWnd);
1951 font = SelectObject(hdc, font);
1952 GetTextExtentPointW(hdc, font_text, ARRAY_SIZE(font_text) - 1, &name_sz);
1953 GetTextExtentPointW(hdc, size_text, ARRAY_SIZE(size_text) - 1, &size_sz);
1954 font = SelectObject(hdc, font);
1955 ReleaseDC(hFontListWnd, hdc);
1956 rbb.hwndChild = hFontListWnd;
1957 rbb.cx = MulDiv(name_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1958 rbb.wID = BANDID_FONTLIST;
1960 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1962 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1963 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1964 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1966 rbb.hwndChild = hSizeListWnd;
1967 rbb.cx = MulDiv(size_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1968 rbb.fStyle ^= RBBS_BREAK;
1969 rbb.wID = BANDID_SIZELIST;
1971 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1973 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1974 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS,
1975 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1977 SendMessageW(hFormatBarWnd, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
1979 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1980 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1981 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1982 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1983 AddSeparator(hFormatBarWnd);
1984 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1985 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1986 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1987 AddSeparator(hFormatBarWnd);
1988 AddButtonStyle(hFormatBarWnd, 7, ID_BULLETONOFF, BTNS_DROPDOWN);
1990 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1992 rbb.hwndChild = hFormatBarWnd;
1993 rbb.wID = BANDID_FORMATBAR;
1995 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1997 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1998 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
2001 rbb.hwndChild = hRulerWnd;
2002 rbb.wID = BANDID_RULER;
2003 rbb.fStyle |= RBBS_BREAK;
2005 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
2007 hDLL = LoadLibraryW(wszRichEditDll);
2008 if(!hDLL)
2010 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
2011 MB_OK | MB_ICONEXCLAMATION);
2012 PostQuitMessage(1);
2015 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
2016 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
2017 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
2018 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
2020 if (!hEditorWnd)
2022 fprintf(stderr, "Error code %u\n", GetLastError());
2023 return -1;
2025 assert(hEditorWnd);
2027 setup_richedit_olecallback(hEditorWnd);
2028 SetFocus(hEditorWnd);
2029 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
2031 set_default_font();
2033 populate_font_list(hFontListWnd);
2034 populate_size_list(hSizeListWnd);
2035 DoLoadStrings();
2036 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2038 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
2040 registry_read_filelist(hWnd);
2041 registry_read_formatopts_all(barState, wordWrap);
2042 registry_read_options();
2043 DragAcceptFiles(hWnd, TRUE);
2045 return 0;
2048 static LRESULT OnUser( HWND hWnd )
2050 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2051 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2052 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
2053 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2054 int from, to;
2055 CHARFORMAT2W fmt;
2056 PARAFORMAT2 pf;
2057 GETTEXTLENGTHEX gt;
2059 ZeroMemory(&fmt, sizeof(fmt));
2060 fmt.cbSize = sizeof(fmt);
2062 ZeroMemory(&pf, sizeof(pf));
2063 pf.cbSize = sizeof(pf);
2065 gt.flags = GTL_NUMCHARS;
2066 gt.codepage = 1200;
2068 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
2069 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
2071 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
2073 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2074 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
2075 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
2076 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
2077 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
2078 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
2079 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2081 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2082 (fmt.dwEffects & CFE_BOLD));
2083 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2084 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2085 (fmt.dwEffects & CFE_ITALIC));
2086 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2087 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2088 (fmt.dwEffects & CFE_UNDERLINE));
2089 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2091 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2092 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2093 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2094 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2096 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLETONOFF, pf.wNumbering != 0);
2097 return 0;
2100 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2102 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2103 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2104 NMHDR *pHdr = (NMHDR *)lParam;
2105 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2106 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2107 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2109 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2111 if (pHdr->code == CBEN_ENDEDITW)
2113 NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
2114 if(pHdr->hwndFrom == hwndFontList)
2116 on_fontlist_modified(endEdit->szText);
2117 } else if (pHdr->hwndFrom == hwndSizeList)
2119 on_sizelist_modified(hwndSizeList,endEdit->szText);
2122 return 0;
2125 if (pHdr->hwndFrom == hwndFormatBar)
2127 if (pHdr->code == TBN_DROPDOWN)
2129 NMTOOLBARW *tb_notify = (NMTOOLBARW *)lParam;
2130 HMENU menu = GetMenu( hWnd );
2131 MENUITEMINFOW info;
2132 TPMPARAMS params;
2133 RECT rc;
2135 if (!menu) return 0;
2136 info.cbSize = sizeof(info);
2137 info.fMask = MIIM_SUBMENU;
2138 GetMenuItemInfoW( menu, ID_LISTMENU, FALSE, &info );
2139 if (!info.hSubMenu) return 0;
2141 SendMessageW( tb_notify->hdr.hwndFrom, TB_GETRECT, (WPARAM)tb_notify->iItem, (LPARAM)&rc );
2142 MapWindowPoints( tb_notify->hdr.hwndFrom, HWND_DESKTOP, (LPPOINT)&rc, 2 );
2144 params.cbSize = sizeof(params);
2145 params.rcExclude = rc;
2146 TrackPopupMenuEx( info.hSubMenu,
2147 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
2148 rc.left, rc.bottom, hWnd, &params );
2151 return 0;
2154 if (pHdr->hwndFrom == hwndEditor)
2156 if (pHdr->code == EN_SELCHANGE)
2158 SELCHANGE *pSC = (SELCHANGE *)lParam;
2159 char buf[128];
2161 update_font_list();
2163 sprintf( buf,"selection = %d..%d, line count=%ld",
2164 pSC->chrg.cpMin, pSC->chrg.cpMax,
2165 SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
2166 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2167 SendMessageW(hWnd, WM_USER, 0, 0);
2168 return 1;
2171 return 0;
2174 /* Copied from dlls/comdlg32/fontdlg.c */
2175 static const COLORREF textcolors[]=
2177 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2178 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2179 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2180 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2183 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2185 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2186 static FINDREPLACEW findreplace;
2188 if ((HWND)lParam == hwndEditor)
2189 return 0;
2191 switch(LOWORD(wParam))
2194 case ID_FILE_EXIT:
2195 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2196 break;
2198 case ID_FILE_NEW:
2200 HINSTANCE hInstance = GetModuleHandleW(0);
2201 int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
2203 if(ret != ID_NEWFILE_ABORT)
2205 if(prompt_save_changes())
2207 SETTEXTEX st;
2209 set_caption(NULL);
2210 wszFileName[0] = '\0';
2212 clear_formatting();
2214 st.flags = ST_DEFAULT;
2215 st.codepage = 1200;
2216 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2218 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2219 set_fileformat(ret);
2220 update_font_list();
2224 break;
2226 case ID_FILE_OPEN:
2227 DialogOpenFile();
2228 break;
2230 case ID_FILE_SAVE:
2231 if(wszFileName[0])
2233 DoSaveFile(wszFileName, fileFormat);
2234 break;
2236 /* Fall through */
2238 case ID_FILE_SAVEAS:
2239 DialogSaveFile();
2240 break;
2242 case ID_FILE_RECENT1:
2243 case ID_FILE_RECENT2:
2244 case ID_FILE_RECENT3:
2245 case ID_FILE_RECENT4:
2247 HMENU hMenu = GetMenu(hWnd);
2248 MENUITEMINFOW mi;
2250 mi.cbSize = sizeof(MENUITEMINFOW);
2251 mi.fMask = MIIM_DATA;
2252 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2253 DoOpenFile((LPWSTR)mi.dwItemData);
2255 break;
2257 case ID_FIND:
2258 dialog_find(&findreplace, FALSE);
2259 break;
2261 case ID_FIND_NEXT:
2262 handle_findmsg(&findreplace);
2263 break;
2265 case ID_REPLACE:
2266 dialog_find(&findreplace, TRUE);
2267 break;
2269 case ID_FONTSETTINGS:
2270 dialog_choose_font();
2271 break;
2273 case ID_PRINT:
2274 dialog_print(hWnd, wszFileName);
2275 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2276 break;
2278 case ID_PRINT_QUICK:
2279 print_quick(hMainWnd, wszFileName);
2280 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2281 break;
2283 case ID_PREVIEW:
2285 int index = reg_formatindex(fileFormat);
2286 DWORD tmp = barState[index];
2287 barState[index] = 1 << BANDID_STATUSBAR;
2288 set_bar_states();
2289 barState[index] = tmp;
2290 ShowWindow(hEditorWnd, FALSE);
2292 init_preview(hWnd, wszFileName);
2294 SetMenu(hWnd, NULL);
2295 InvalidateRect(0, 0, TRUE);
2297 break;
2299 case ID_PRINTSETUP:
2300 dialog_printsetup(hWnd);
2301 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2302 break;
2304 case ID_FORMAT_BOLD:
2305 case ID_FORMAT_ITALIC:
2306 case ID_FORMAT_UNDERLINE:
2308 CHARFORMAT2W fmt;
2309 int effects = CFE_BOLD;
2311 ZeroMemory(&fmt, sizeof(fmt));
2312 fmt.cbSize = sizeof(fmt);
2313 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2315 fmt.dwMask = CFM_BOLD;
2317 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2319 effects = CFE_ITALIC;
2320 fmt.dwMask = CFM_ITALIC;
2321 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2323 effects = CFE_UNDERLINE;
2324 fmt.dwMask = CFM_UNDERLINE;
2327 fmt.dwEffects ^= effects;
2329 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2330 break;
2333 case ID_FORMAT_COLOR:
2335 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2336 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2337 HMENU hPop;
2338 RECT itemrc;
2339 POINT pt;
2340 int mid;
2341 int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2343 SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2344 pt.x = itemrc.left;
2345 pt.y = itemrc.bottom;
2346 ClientToScreen(hFormatBarWnd, &pt);
2347 hPop = GetSubMenu(hColorPopupMenu, 0);
2348 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2349 TPM_RETURNCMD | TPM_NONOTIFY,
2350 pt.x, pt.y, 0, hWnd, 0);
2351 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2353 CHARFORMAT2W fmt;
2355 ZeroMemory(&fmt, sizeof(fmt));
2356 fmt.cbSize = sizeof(fmt);
2357 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2359 fmt.dwMask = CFM_COLOR;
2361 if (mid < ID_COLOR_AUTOMATIC) {
2362 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2363 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2364 } else {
2365 fmt.dwEffects |= CFE_AUTOCOLOR;
2368 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2370 break;
2373 case ID_EDIT_CUT:
2374 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2375 break;
2377 case ID_EDIT_COPY:
2378 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2379 break;
2381 case ID_EDIT_PASTE:
2382 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2383 break;
2385 case ID_EDIT_CLEAR:
2386 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2387 break;
2389 case ID_EDIT_SELECTALL:
2391 CHARRANGE range = {0, -1};
2392 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2393 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2394 return 0;
2397 case ID_EDIT_GETTEXT:
2399 int nLen = GetWindowTextLengthW(hwndEditor);
2400 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2401 TEXTRANGEW tr;
2403 GetWindowTextW(hwndEditor, data, nLen+1);
2404 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2406 HeapFree( GetProcessHeap(), 0, data);
2407 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2408 tr.chrg.cpMin = 0;
2409 tr.chrg.cpMax = nLen;
2410 tr.lpstrText = data;
2411 SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2412 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2413 HeapFree( GetProcessHeap(), 0, data );
2415 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2416 return 0;
2419 case ID_EDIT_CHARFORMAT:
2420 case ID_EDIT_DEFCHARFORMAT:
2422 CHARFORMAT2W cf;
2424 ZeroMemory(&cf, sizeof(cf));
2425 cf.cbSize = sizeof(cf);
2426 cf.dwMask = 0;
2427 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2428 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2429 return 0;
2432 case ID_EDIT_PARAFORMAT:
2434 PARAFORMAT2 pf;
2435 ZeroMemory(&pf, sizeof(pf));
2436 pf.cbSize = sizeof(pf);
2437 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2438 return 0;
2441 case ID_EDIT_SELECTIONINFO:
2443 CHARRANGE range = {0, -1};
2444 char buf[128];
2445 WCHAR *data = NULL;
2447 SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2448 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2449 SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2450 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2451 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2452 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2453 HeapFree( GetProcessHeap(), 0, data);
2454 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2455 return 0;
2458 case ID_EDIT_READONLY:
2460 LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
2461 if (nStyle & ES_READONLY)
2462 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2463 else
2464 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2465 return 0;
2468 case ID_EDIT_MODIFIED:
2469 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2470 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2471 else
2472 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2473 return 0;
2475 case ID_EDIT_UNDO:
2476 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2477 return 0;
2479 case ID_EDIT_REDO:
2480 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2481 return 0;
2483 case ID_BULLETONOFF:
2484 case ID_BULLET:
2485 case ID_NUMBERING:
2486 case ID_LCLETTER:
2487 case ID_UCLETTER:
2488 case ID_LCROMAN:
2489 case ID_UCROMAN:
2491 PARAFORMAT2 pf;
2492 WORD new_number = LOWORD(wParam) - ID_BULLET + PFN_BULLET;
2493 pf.cbSize = sizeof(pf);
2494 pf.dwMask = PFM_NUMBERING;
2495 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2497 pf.dwMask = PFM_NUMBERING | PFM_NUMBERINGSTART | PFM_NUMBERINGSTYLE | PFM_NUMBERINGTAB | PFM_OFFSET | PFM_OFFSETINDENT;
2499 if(pf.wNumbering && ((pf.wNumbering == new_number) || (LOWORD(wParam) == ID_BULLETONOFF)))
2501 pf.wNumbering = 0;
2502 pf.wNumberingStart = 0;
2503 pf.wNumberingStyle = 0;
2504 pf.wNumberingTab = 0;
2505 pf.dxOffset = 0;
2506 pf.dxStartIndent = -360;
2507 } else
2509 pf.dxStartIndent = pf.wNumbering ? 0 : 360;
2511 if (LOWORD(wParam) == ID_BULLETONOFF)
2512 pf.wNumbering = last_bullet;
2513 else
2515 pf.wNumbering = new_number;
2516 last_bullet = pf.wNumbering;
2518 pf.wNumberingStart = 1;
2519 pf.wNumberingStyle = PFNS_PERIOD;
2520 pf.wNumberingTab = 360;
2521 pf.dxOffset = 360;
2524 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2526 break;
2528 case ID_ALIGN_LEFT:
2529 case ID_ALIGN_CENTER:
2530 case ID_ALIGN_RIGHT:
2532 PARAFORMAT2 pf;
2534 pf.cbSize = sizeof(pf);
2535 pf.dwMask = PFM_ALIGNMENT;
2536 switch(LOWORD(wParam)) {
2537 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2538 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2539 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2541 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2542 break;
2545 case ID_BACK_1:
2546 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2547 break;
2549 case ID_BACK_2:
2550 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2551 break;
2553 case ID_TOGGLE_TOOLBAR:
2554 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2555 update_window();
2556 break;
2558 case ID_TOGGLE_FORMATBAR:
2559 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2560 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2561 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2562 update_window();
2563 break;
2565 case ID_TOGGLE_STATUSBAR:
2566 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2567 update_window();
2568 break;
2570 case ID_TOGGLE_RULER:
2571 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2572 update_window();
2573 break;
2575 case ID_DATETIME:
2576 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2577 break;
2579 case ID_PARAFORMAT:
2580 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2581 break;
2583 case ID_TABSTOPS:
2584 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2585 break;
2587 case ID_ABOUT:
2588 dialog_about();
2589 break;
2591 case ID_VIEWPROPERTIES:
2592 dialog_viewproperties();
2593 break;
2595 case IDC_FONTLIST:
2596 if (HIWORD(wParam) == CBN_SELENDOK)
2598 WCHAR buffer[LF_FACESIZE];
2599 HWND hwndFontList = (HWND)lParam;
2600 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2601 on_fontlist_modified(buffer);
2603 break;
2605 case IDC_SIZELIST:
2606 if (HIWORD(wParam) == CBN_SELENDOK)
2608 WCHAR buffer[MAX_STRING_LEN+1];
2609 HWND hwndSizeList = (HWND)lParam;
2610 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2611 on_sizelist_modified(hwndSizeList, buffer);
2613 break;
2615 default:
2616 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2617 break;
2619 return 0;
2622 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2624 HMENU hMenu = (HMENU)wParam;
2625 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2626 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2627 PARAFORMAT pf;
2628 int nAlignment = -1;
2629 int selFrom, selTo;
2630 GETTEXTLENGTHEX gt;
2631 LRESULT textLength;
2632 MENUITEMINFOW mi;
2634 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2635 EnableMenuItem(hMenu, ID_EDIT_COPY, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2636 EnableMenuItem(hMenu, ID_EDIT_CUT, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2638 pf.cbSize = sizeof(PARAFORMAT);
2639 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2640 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2641 (GetWindowLongW(hwndEditor, GWL_STYLE) & ES_READONLY) ? MF_CHECKED : MF_UNCHECKED);
2642 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2643 SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED);
2644 if (pf.dwMask & PFM_ALIGNMENT)
2645 nAlignment = pf.wAlignment;
2646 CheckMenuItem(hMenu, ID_ALIGN_LEFT, (nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
2647 CheckMenuItem(hMenu, ID_ALIGN_CENTER, (nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
2648 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, (nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
2650 CheckMenuItem(hMenu, ID_BULLET, ((pf.wNumbering == PFN_BULLET) ? MF_CHECKED : MF_UNCHECKED));
2651 CheckMenuItem(hMenu, ID_NUMBERING, ((pf.wNumbering == PFN_ARABIC) ? MF_CHECKED : MF_UNCHECKED));
2652 CheckMenuItem(hMenu, ID_LCLETTER, ((pf.wNumbering == PFN_LCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2653 CheckMenuItem(hMenu, ID_UCLETTER, ((pf.wNumbering == PFN_UCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2654 CheckMenuItem(hMenu, ID_LCROMAN, ((pf.wNumbering == PFN_LCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2655 CheckMenuItem(hMenu, ID_UCROMAN, ((pf.wNumbering == PFN_UCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2657 EnableMenuItem(hMenu, ID_EDIT_UNDO, SendMessageW(hwndEditor, EM_CANUNDO, 0, 0) ?
2658 MF_ENABLED : MF_GRAYED);
2659 EnableMenuItem(hMenu, ID_EDIT_REDO, SendMessageW(hwndEditor, EM_CANREDO, 0, 0) ?
2660 MF_ENABLED : MF_GRAYED);
2662 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, is_bar_visible(BANDID_TOOLBAR) ?
2663 MF_CHECKED : MF_UNCHECKED);
2665 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, is_bar_visible(BANDID_FORMATBAR) ?
2666 MF_CHECKED : MF_UNCHECKED);
2668 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, IsWindowVisible(hwndStatus) ?
2669 MF_CHECKED : MF_UNCHECKED);
2671 CheckMenuItem(hMenu, ID_TOGGLE_RULER, is_bar_visible(BANDID_RULER) ? MF_CHECKED : MF_UNCHECKED);
2673 gt.flags = GTL_NUMCHARS;
2674 gt.codepage = 1200;
2675 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2676 EnableMenuItem(hMenu, ID_FIND, textLength ? MF_ENABLED : MF_GRAYED);
2678 mi.cbSize = sizeof(mi);
2679 mi.fMask = MIIM_DATA;
2681 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2683 EnableMenuItem(hMenu, ID_FIND_NEXT, (textLength && mi.dwItemData) ? MF_ENABLED : MF_GRAYED);
2685 EnableMenuItem(hMenu, ID_REPLACE, textLength ? MF_ENABLED : MF_GRAYED);
2687 return 0;
2690 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2692 int nStatusSize = 0;
2693 RECT rc;
2694 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2695 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2696 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2697 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2698 int rebarHeight = 0;
2700 if (hwndStatusBar)
2702 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2703 if (IsWindowVisible(hwndStatusBar))
2705 GetClientRect(hwndStatusBar, &rc);
2706 nStatusSize = rc.bottom - rc.top;
2707 } else
2709 nStatusSize = 0;
2712 if (hwndReBar)
2714 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2716 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2718 if (hwndEditor)
2720 GetClientRect(hWnd, &rc);
2721 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2724 redraw_ruler(hRulerWnd);
2726 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2729 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2731 if(msg == ID_FINDMSGSTRING)
2732 return handle_findmsg((LPFINDREPLACEW)lParam);
2734 switch(msg)
2736 case WM_CREATE:
2737 return OnCreate( hWnd );
2739 case WM_USER:
2740 return OnUser( hWnd );
2742 case WM_NOTIFY:
2743 return OnNotify( hWnd, lParam );
2745 case WM_COMMAND:
2746 if(preview_isactive())
2748 return preview_command( hWnd, wParam );
2751 return OnCommand( hWnd, wParam, lParam );
2753 case WM_DESTROY:
2754 PostQuitMessage(0);
2755 break;
2757 case WM_CLOSE:
2758 if(preview_isactive())
2760 preview_exit(hWnd);
2761 } else if(prompt_save_changes())
2763 registry_set_options(hMainWnd);
2764 registry_set_formatopts_all(barState, wordWrap);
2765 PostQuitMessage(0);
2767 break;
2769 case WM_ACTIVATE:
2770 if (LOWORD(wParam))
2771 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2772 return 0;
2774 case WM_INITMENUPOPUP:
2775 return OnInitPopupMenu( hWnd, wParam );
2777 case WM_SIZE:
2778 return OnSize( hWnd, wParam, lParam );
2780 case WM_CONTEXTMENU:
2781 return DefWindowProcW(hWnd, msg, wParam, lParam);
2783 case WM_DROPFILES:
2785 WCHAR file[MAX_PATH];
2786 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2787 DragFinish((HDROP)wParam);
2789 if(prompt_save_changes())
2790 DoOpenFile(file);
2792 break;
2793 case WM_PAINT:
2794 if(!preview_isactive())
2795 return DefWindowProcW(hWnd, msg, wParam, lParam);
2797 default:
2798 return DefWindowProcW(hWnd, msg, wParam, lParam);
2801 return 0;
2804 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2806 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2807 HACCEL hAccel;
2808 WNDCLASSEXW wc;
2809 MSG msg;
2810 RECT rc;
2811 UINT_PTR hPrevRulerProc;
2812 HWND hRulerWnd;
2813 POINTL EditPoint;
2814 DWORD bMaximized;
2815 MONITORINFO info;
2816 HMONITOR monitor;
2817 int x, y;
2818 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2819 'T','A','B','L','E','\0'};
2821 InitCommonControlsEx(&classes);
2823 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2825 wc.cbSize = sizeof(wc);
2826 wc.style = 0;
2827 wc.lpfnWndProc = WndProc;
2828 wc.cbClsExtra = 0;
2829 wc.cbWndExtra = 4;
2830 wc.hInstance = hInstance;
2831 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2832 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2833 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2834 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2835 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2836 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2837 wc.lpszClassName = wszMainWndClass;
2838 RegisterClassExW(&wc);
2840 wc.style = 0;
2841 wc.lpfnWndProc = preview_proc;
2842 wc.cbClsExtra = 0;
2843 wc.cbWndExtra = 0;
2844 wc.hInstance = hInstance;
2845 wc.hIcon = NULL;
2846 wc.hIconSm = NULL;
2847 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2848 wc.hbrBackground = NULL;
2849 wc.lpszMenuName = NULL;
2850 wc.lpszClassName = wszPreviewWndClass;
2851 RegisterClassExW(&wc);
2853 registry_read_winrect(&rc);
2854 monitor = MonitorFromRect(&rc, MONITOR_DEFAULTTOPRIMARY);
2855 info.cbSize = sizeof(info);
2856 GetMonitorInfoW(monitor, &info);
2858 x = rc.left;
2859 y = rc.top;
2860 IntersectRect(&info.rcWork, &info.rcWork, &rc);
2861 if (IsRectEmpty(&info.rcWork))
2862 x = y = CW_USEDEFAULT;
2864 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2865 x, y, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
2866 registry_read_maximized(&bMaximized);
2867 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2868 && bMaximized)
2869 nCmdShow = SW_SHOWMAXIMIZED;
2870 ShowWindow(hMainWnd, nCmdShow);
2872 set_caption(NULL);
2873 set_bar_states();
2874 set_fileformat(SF_RTF);
2875 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2876 get_default_printer_opts();
2877 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2879 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2880 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2881 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2882 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2884 HandleCommandLine(GetCommandLineW());
2886 while(GetMessageW(&msg,0,0,0))
2888 if (IsDialogMessageW(hFindWnd, &msg))
2889 continue;
2891 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2892 continue;
2893 TranslateMessage(&msg);
2894 DispatchMessageW(&msg);
2895 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2896 SendMessageW(hMainWnd, WM_USER, 0, 0);
2899 return 0;