wordpad: Use return value of LoadStringW() instead of calling lstrlenW().
[wine.git] / programs / wordpad / wordpad.c
blob89bcac1d222692d2232ec44125b374cab5d5c8fe
1 /*
2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_IE 0x0400
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <assert.h>
31 #include <windows.h>
32 #include <richedit.h>
33 #include <commctrl.h>
34 #include <commdlg.h>
35 #include <shellapi.h>
36 #include <math.h>
37 #include <errno.h>
39 #include "wine/unicode.h"
40 #include "wordpad.h"
42 #ifdef NONAMELESSUNION
43 # define U(x) (x).u
44 # define U2(x) (x).u2
45 # define U3(x) (x).u3
46 #else
47 # define U(x) (x)
48 # define U2(x) (x)
49 # define U3(x) (x)
50 #endif
52 /* use LoadString */
53 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
55 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
57 static const WCHAR stringFormat[] = {'%','2','d','\0'};
59 const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
60 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
62 static HWND hMainWnd;
63 static HWND hEditorWnd;
64 static HWND hFindWnd;
65 static HMENU hColorPopupMenu;
67 static UINT ID_FINDMSGSTRING;
69 static DWORD wordWrap[2];
70 static DWORD barState[2];
71 static WPARAM fileFormat = SF_RTF;
73 static WCHAR wszFileName[MAX_PATH];
74 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
75 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
76 static WCHAR wszSaveChanges[MAX_STRING_LEN];
77 static WCHAR units_cmW[MAX_STRING_LEN];
78 static WCHAR units_inW[MAX_STRING_LEN];
79 static WCHAR units_inchW[MAX_STRING_LEN];
80 static WCHAR units_ptW[MAX_STRING_LEN];
82 static int last_bullet = PFN_BULLET;
84 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
86 typedef enum
88 UNIT_CM,
89 UNIT_INCH,
90 UNIT_PT
91 } UNIT;
93 typedef struct
95 int endPos;
96 BOOL wrapped;
97 WCHAR findBuffer[128];
98 } FINDREPLACE_custom;
100 /* Load string resources */
101 static void DoLoadStrings(void)
103 LPWSTR p = wszFilter;
104 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
105 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
106 static const WCHAR files_all[] = {'*','.','*','\0'};
108 HINSTANCE hInstance = GetModuleHandleW(0);
110 p += 1 + LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
111 lstrcpyW(p, files_rtf);
112 p += lstrlenW(p) + 1;
113 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
114 lstrcpyW(p, files_txt);
115 p += lstrlenW(p) + 1;
116 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
117 lstrcpyW(p, files_txt);
118 p += lstrlenW(p) + 1;
119 p += 1 + LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
120 lstrcpyW(p, files_all);
121 p += lstrlenW(p) + 1;
122 *p = '\0';
124 p = wszDefaultFileName;
125 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
127 p = wszSaveChanges;
128 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
130 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
131 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
132 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
133 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
136 /* Show a message box with resource strings */
137 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
139 MSGBOXPARAMSW params;
141 params.cbSize = sizeof(params);
142 params.hwndOwner = hWnd;
143 params.hInstance = GetModuleHandleW(0);
144 params.lpszText = lpText;
145 params.lpszCaption = lpCaption;
146 params.dwStyle = uType;
147 params.lpszIcon = NULL;
148 params.dwContextHelpId = 0;
149 params.lpfnMsgBoxCallback = NULL;
150 params.dwLanguageId = 0;
151 return MessageBoxIndirectW(&params);
155 static void AddButtonStyle(HWND hwndToolBar, int nImage, int nCommand, BYTE style)
157 TBBUTTON button;
159 ZeroMemory(&button, sizeof(button));
160 button.iBitmap = nImage;
161 button.idCommand = nCommand;
162 button.fsState = TBSTATE_ENABLED;
163 button.fsStyle = style;
164 button.dwData = 0;
165 button.iString = -1;
166 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
169 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
171 AddButtonStyle(hwndToolBar, nImage, nCommand, BTNS_BUTTON);
174 static void AddSeparator(HWND hwndToolBar)
176 TBBUTTON button;
178 ZeroMemory(&button, sizeof(button));
179 button.iBitmap = -1;
180 button.idCommand = 0;
181 button.fsState = 0;
182 button.fsStyle = BTNS_SEP;
183 button.dwData = 0;
184 button.iString = -1;
185 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
188 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
190 HANDLE hFile = (HANDLE)cookie;
191 DWORD read;
193 if(!ReadFile(hFile, buffer, cb, &read, 0))
194 return 1;
196 *pcb = read;
198 return 0;
201 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
203 DWORD written;
204 int ret;
205 HANDLE hFile = (HANDLE)cookie;
207 ret = WriteFile(hFile, buffer, cb, &written, 0);
209 if(!ret || (cb != written))
210 return 1;
212 *pcb = cb;
214 return 0;
217 LPWSTR file_basename(LPWSTR path)
219 LPWSTR pos = path + lstrlenW(path);
221 while(pos > path)
223 if(*pos == '\\' || *pos == '/')
225 pos++;
226 break;
228 pos--;
230 return pos;
233 static void set_caption(LPCWSTR wszNewFileName)
235 static const WCHAR wszSeparator[] = {' ','-',' '};
236 WCHAR *wszCaption;
237 SIZE_T length = 0;
239 if(!wszNewFileName)
240 wszNewFileName = wszDefaultFileName;
241 else
242 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
244 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
245 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
247 if(!wszCaption)
248 return;
250 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
251 length += lstrlenW(wszNewFileName);
252 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
253 length += sizeof(wszSeparator) / sizeof(WCHAR);
254 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
256 SetWindowTextW(hMainWnd, wszCaption);
258 HeapFree(GetProcessHeap(), 0, wszCaption);
261 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
263 if(punit != NULL)
264 *punit = UNIT_CM;
265 if(!endptr)
266 return FALSE;
267 if(!*endptr)
268 return TRUE;
270 while(*endptr == ' ')
271 endptr++;
273 if(punit == NULL)
274 return *endptr == '\0';
276 if(!lstrcmpW(endptr, units_cmW))
278 *punit = UNIT_CM;
279 endptr += lstrlenW(units_cmW);
281 else if (!lstrcmpW(endptr, units_inW))
283 *punit = UNIT_INCH;
284 endptr += lstrlenW(units_inW);
286 else if (!lstrcmpW(endptr, units_inchW))
288 *punit = UNIT_INCH;
289 endptr += lstrlenW(units_inchW);
291 else if (!lstrcmpW(endptr, units_ptW))
293 *punit = UNIT_PT;
294 endptr += lstrlenW(units_ptW);
297 return *endptr == '\0';
300 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
302 double ret;
303 WCHAR *endptr;
305 *num = 0;
306 errno = 0;
307 ret = wcstod(string, &endptr);
309 if (punit != NULL)
310 *punit = UNIT_CM;
311 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
313 return FALSE;
314 } else
316 *num = (float)ret;
317 return TRUE;
321 static void set_size(float size)
323 CHARFORMAT2W fmt;
325 ZeroMemory(&fmt, sizeof(fmt));
326 fmt.cbSize = sizeof(fmt);
327 fmt.dwMask = CFM_SIZE;
328 fmt.yHeight = (int)(size * 20.0);
329 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
332 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
334 WCHAR sizeBuffer[MAX_STRING_LEN];
335 CHARFORMAT2W format;
337 ZeroMemory(&format, sizeof(format));
338 format.cbSize = sizeof(format);
339 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
341 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
342 if(lstrcmpW(sizeBuffer, wszNewFontSize))
344 float size = 0;
345 if(number_from_string(wszNewFontSize, &size, NULL)
346 && size > 0)
348 set_size(size);
349 } else
351 SetWindowTextW(hwndSizeList, sizeBuffer);
352 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
353 wszAppTitle, MB_OK | MB_ICONINFORMATION);
358 static void add_size(HWND hSizeListWnd, unsigned size)
360 WCHAR buffer[3];
361 COMBOBOXEXITEMW cbItem;
362 cbItem.mask = CBEIF_TEXT;
363 cbItem.iItem = -1;
365 wsprintfW(buffer, stringFormat, size);
366 cbItem.pszText = buffer;
367 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
370 static void populate_size_list(HWND hSizeListWnd)
372 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
373 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
374 COMBOBOXEXITEMW cbFontItem;
375 CHARFORMAT2W fmt;
376 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
377 HDC hdc = GetDC(hMainWnd);
378 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
379 WCHAR buffer[3];
380 size_t i;
381 DWORD fontStyle;
383 ZeroMemory(&fmt, sizeof(fmt));
384 fmt.cbSize = sizeof(fmt);
385 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
387 cbFontItem.mask = CBEIF_LPARAM;
388 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
389 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
391 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
393 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
395 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
397 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
398 GetDeviceCaps(hdc, LOGPIXELSY)));
399 } else
401 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
402 add_size(hSizeListWnd, choices[i]);
405 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
406 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
409 static void update_size_list(void)
411 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
412 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
413 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
414 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
415 CHARFORMAT2W fmt;
417 ZeroMemory(&fmt, sizeof(fmt));
418 fmt.cbSize = sizeof(fmt);
420 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
422 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
423 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
425 if(lstrcmpW(fontSize, sizeBuffer))
426 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
429 static void update_font_list(void)
431 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
432 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
433 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
434 WCHAR fontName[MAX_STRING_LEN];
435 CHARFORMAT2W fmt;
437 ZeroMemory(&fmt, sizeof(fmt));
438 fmt.cbSize = sizeof(fmt);
440 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
441 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
443 if(lstrcmpW(fontName, fmt.szFaceName))
445 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
446 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
447 } else
449 update_size_list();
453 static void clear_formatting(void)
455 PARAFORMAT2 pf;
457 pf.cbSize = sizeof(pf);
458 pf.dwMask = PFM_ALIGNMENT;
459 pf.wAlignment = PFA_LEFT;
460 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
463 static int fileformat_number(WPARAM format)
465 int number = 0;
467 if(format == SF_TEXT)
469 number = 1;
470 } else if (format == (SF_TEXT | SF_UNICODE))
472 number = 2;
474 return number;
477 static WPARAM fileformat_flags(int format)
479 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
481 return flags[format];
484 static void set_font(LPCWSTR wszFaceName)
486 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
487 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
488 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
489 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
490 CHARFORMAT2W fmt;
492 ZeroMemory(&fmt, sizeof(fmt));
494 fmt.cbSize = sizeof(fmt);
495 fmt.dwMask = CFM_FACE;
497 lstrcpyW(fmt.szFaceName, wszFaceName);
499 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
501 populate_size_list(hSizeListWnd);
503 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
506 static void set_default_font(void)
508 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
509 'R','o','m','a','n',0};
510 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
511 CHARFORMAT2W fmt;
512 LPCWSTR font;
514 ZeroMemory(&fmt, sizeof(fmt));
516 fmt.cbSize = sizeof(fmt);
517 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
518 fmt.dwEffects = 0;
520 if(fileFormat & SF_RTF)
521 font = richTextFont;
522 else
523 font = plainTextFont;
525 lstrcpyW(fmt.szFaceName, font);
527 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
530 static void on_fontlist_modified(LPWSTR wszNewFaceName)
532 CHARFORMAT2W format;
533 ZeroMemory(&format, sizeof(format));
534 format.cbSize = sizeof(format);
535 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
537 if(lstrcmpW(format.szFaceName, wszNewFaceName))
538 set_font(wszNewFaceName);
541 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
543 COMBOBOXEXITEMW cbItem;
544 WCHAR buffer[MAX_PATH];
545 int fontHeight = 0;
547 cbItem.mask = CBEIF_TEXT;
548 cbItem.pszText = buffer;
549 cbItem.cchTextMax = MAX_STRING_LEN;
550 cbItem.iItem = 0;
552 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
554 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
555 cbItem.iItem++;
556 else
557 break;
559 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
560 lstrcpyW( cbItem.pszText, fontName );
562 cbItem.mask |= CBEIF_LPARAM;
563 if(fontType & RASTER_FONTTYPE)
564 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
566 cbItem.lParam = MAKELONG(fontType,fontHeight);
567 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
568 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
571 static void dialog_choose_font(void)
573 CHOOSEFONTW cf;
574 LOGFONTW lf;
575 CHARFORMAT2W fmt;
576 HDC hDC = GetDC(hMainWnd);
578 ZeroMemory(&cf, sizeof(cf));
579 cf.lStructSize = sizeof(cf);
580 cf.hwndOwner = hMainWnd;
581 cf.lpLogFont = &lf;
582 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_NOVERTFONTS;
584 ZeroMemory(&fmt, sizeof(fmt));
585 fmt.cbSize = sizeof(fmt);
587 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
588 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
589 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
590 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
591 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
592 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
593 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
594 cf.rgbColors = fmt.crTextColor;
596 if(ChooseFontW(&cf))
598 ZeroMemory(&fmt, sizeof(fmt));
599 fmt.cbSize = sizeof(fmt);
600 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
601 fmt.yHeight = cf.iPointSize * 2;
603 if(cf.nFontType & BOLD_FONTTYPE)
604 fmt.dwEffects |= CFE_BOLD;
605 if(cf.nFontType & ITALIC_FONTTYPE)
606 fmt.dwEffects |= CFE_ITALIC;
607 if(cf.lpLogFont->lfUnderline)
608 fmt.dwEffects |= CFE_UNDERLINE;
609 if(cf.lpLogFont->lfStrikeOut)
610 fmt.dwEffects |= CFE_STRIKEOUT;
612 fmt.crTextColor = cf.rgbColors;
614 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
615 set_font(cf.lpLogFont->lfFaceName);
620 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
621 DWORD FontType, LPARAM lParam)
623 HWND hListWnd = (HWND) lParam;
625 if (lpelfe->lfFaceName[0] == '@') return 1; /* ignore vertical fonts */
627 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
630 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
633 return 1;
636 static void populate_font_list(HWND hListWnd)
638 HDC hdc = GetDC(hMainWnd);
639 LOGFONTW fontinfo;
640 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
641 CHARFORMAT2W fmt;
643 fontinfo.lfCharSet = DEFAULT_CHARSET;
644 *fontinfo.lfFaceName = '\0';
645 fontinfo.lfPitchAndFamily = 0;
647 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
648 (LPARAM)hListWnd, 0);
650 ZeroMemory(&fmt, sizeof(fmt));
651 fmt.cbSize = sizeof(fmt);
652 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
653 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
656 static void update_window(void)
658 RECT rect;
660 GetClientRect(hMainWnd, &rect);
662 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
665 static BOOL is_bar_visible(int bandId)
667 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
670 static void store_bar_state(int bandId, BOOL show)
672 int formatIndex = reg_formatindex(fileFormat);
674 if(show)
675 barState[formatIndex] |= (1 << bandId);
676 else
677 barState[formatIndex] &= ~(1 << bandId);
680 static void set_toolbar_state(int bandId, BOOL show)
682 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
684 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
686 if(bandId == BANDID_TOOLBAR)
688 REBARBANDINFOW rbbinfo;
689 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
691 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
692 rbbinfo.fMask = RBBIM_STYLE;
694 SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
696 if(!show)
697 rbbinfo.fStyle &= ~RBBS_BREAK;
698 else
699 rbbinfo.fStyle |= RBBS_BREAK;
701 SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
704 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
705 store_bar_state(bandId, show);
708 static void set_statusbar_state(BOOL show)
710 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
712 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
713 store_bar_state(BANDID_STATUSBAR, show);
716 static void set_bar_states(void)
718 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
719 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
720 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
721 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
722 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
723 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
725 update_window();
728 static void preview_exit(HWND hMainWnd)
730 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
731 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
733 set_bar_states();
734 ShowWindow(hEditorWnd, TRUE);
736 close_preview(hMainWnd);
738 SetMenu(hMainWnd, hMenu);
739 registry_read_filelist(hMainWnd);
741 update_window();
744 static void set_fileformat(WPARAM format)
746 fileFormat = format;
748 set_bar_states();
749 set_default_font();
750 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
753 static void ShowOpenError(DWORD Code)
755 LPWSTR Message;
757 switch(Code)
759 case ERROR_ACCESS_DENIED:
760 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
761 break;
763 default:
764 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
766 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
769 static void DoOpenFile(LPCWSTR szOpenFileName)
771 HANDLE hFile;
772 EDITSTREAM es;
773 char fileStart[5];
774 DWORD readOut;
775 WPARAM format = SF_TEXT;
777 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
778 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
779 if (hFile == INVALID_HANDLE_VALUE)
781 ShowOpenError(GetLastError());
782 return;
785 ReadFile(hFile, fileStart, 5, &readOut, NULL);
786 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
788 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
790 format = SF_TEXT | SF_UNICODE;
791 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
792 } else if(readOut >= 5)
794 static const char header[] = "{\\rtf";
795 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
797 if(!memcmp(header, fileStart, 5))
798 format = SF_RTF;
799 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
801 CloseHandle(hFile);
802 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
803 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
804 return;
808 es.dwCookie = (DWORD_PTR)hFile;
809 es.pfnCallback = stream_in;
811 clear_formatting();
812 set_fileformat(format);
813 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
815 CloseHandle(hFile);
817 SetFocus(hEditorWnd);
819 set_caption(szOpenFileName);
821 lstrcpyW(wszFileName, szOpenFileName);
822 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
823 registry_set_filelist(szOpenFileName, hMainWnd);
824 update_font_list();
827 static void ShowWriteError(DWORD Code)
829 LPWSTR Message;
831 switch(Code)
833 case ERROR_ACCESS_DENIED:
834 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
835 break;
837 default:
838 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
840 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
843 static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
845 HANDLE hFile;
846 EDITSTREAM stream;
847 LRESULT ret;
849 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
850 FILE_ATTRIBUTE_NORMAL, NULL);
852 if(hFile == INVALID_HANDLE_VALUE)
854 ShowWriteError(GetLastError());
855 return FALSE;
858 if(format == (SF_TEXT | SF_UNICODE))
860 static const BYTE unicode[] = {0xff,0xfe};
861 DWORD writeOut;
862 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
864 if(writeOut != sizeof(unicode))
866 CloseHandle(hFile);
867 return FALSE;
871 stream.dwCookie = (DWORD_PTR)hFile;
872 stream.pfnCallback = stream_out;
874 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
876 CloseHandle(hFile);
878 SetFocus(hEditorWnd);
880 if(!ret)
882 GETTEXTLENGTHEX gt;
883 gt.flags = GTL_DEFAULT;
884 gt.codepage = 1200;
886 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
887 return FALSE;
890 lstrcpyW(wszFileName, wszSaveFileName);
891 set_caption(wszFileName);
892 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
893 set_fileformat(format);
895 return TRUE;
898 static BOOL DialogSaveFile(void)
900 OPENFILENAMEW sfn;
902 WCHAR wszFile[MAX_PATH] = {'\0'};
903 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
905 ZeroMemory(&sfn, sizeof(sfn));
907 sfn.lStructSize = sizeof(sfn);
908 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
909 sfn.hwndOwner = hMainWnd;
910 sfn.lpstrFilter = wszFilter;
911 sfn.lpstrFile = wszFile;
912 sfn.nMaxFile = MAX_PATH;
913 sfn.lpstrDefExt = wszDefExt;
914 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
916 while(GetSaveFileNameW(&sfn))
918 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
920 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
921 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
922 continue;
924 return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
926 return FALSE;
929 static BOOL prompt_save_changes(void)
931 if(!wszFileName[0])
933 GETTEXTLENGTHEX gt;
934 gt.flags = GTL_NUMCHARS;
935 gt.codepage = 1200;
936 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
937 return TRUE;
940 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
942 return TRUE;
943 } else
945 LPWSTR displayFileName;
946 WCHAR *text;
947 int ret;
949 if(!wszFileName[0])
950 displayFileName = wszDefaultFileName;
951 else
952 displayFileName = file_basename(wszFileName);
954 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
955 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
957 if(!text)
958 return FALSE;
960 wsprintfW(text, wszSaveChanges, displayFileName);
962 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
964 HeapFree(GetProcessHeap(), 0, text);
966 switch(ret)
968 case IDNO:
969 return TRUE;
971 case IDYES:
972 if(wszFileName[0])
973 return DoSaveFile(wszFileName, fileFormat);
974 return DialogSaveFile();
976 default:
977 return FALSE;
982 static void DialogOpenFile(void)
984 OPENFILENAMEW ofn;
986 WCHAR wszFile[MAX_PATH] = {'\0'};
987 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
989 ZeroMemory(&ofn, sizeof(ofn));
991 ofn.lStructSize = sizeof(ofn);
992 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
993 ofn.hwndOwner = hMainWnd;
994 ofn.lpstrFilter = wszFilter;
995 ofn.lpstrFile = wszFile;
996 ofn.nMaxFile = MAX_PATH;
997 ofn.lpstrDefExt = wszDefExt;
998 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
1000 if(GetOpenFileNameW(&ofn))
1002 if(prompt_save_changes())
1003 DoOpenFile(ofn.lpstrFile);
1007 static void dialog_about(void)
1009 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1010 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1013 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1015 switch(message)
1017 case WM_INITDIALOG:
1019 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1020 int wrap = -1;
1021 char id[4];
1022 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1024 sprintf(id, "%d\n", (int)ps->lParam);
1025 SetWindowTextA(hIdWnd, id);
1026 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1027 wrap = IDC_PAGEFMT_WN;
1028 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1029 wrap = IDC_PAGEFMT_WW;
1030 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1031 wrap = IDC_PAGEFMT_WM;
1033 if(wrap != -1)
1034 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1035 IDC_PAGEFMT_WM, wrap);
1037 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1038 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1039 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1040 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1041 if(barState[ps->lParam] & (1 << BANDID_RULER))
1042 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1043 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1044 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1046 break;
1048 case WM_COMMAND:
1049 switch(LOWORD(wParam))
1051 case IDC_PAGEFMT_WN:
1052 case IDC_PAGEFMT_WW:
1053 case IDC_PAGEFMT_WM:
1054 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1055 LOWORD(wParam));
1056 break;
1058 case IDC_PAGEFMT_TB:
1059 case IDC_PAGEFMT_FB:
1060 case IDC_PAGEFMT_RU:
1061 case IDC_PAGEFMT_SB:
1062 CheckDlgButton(hWnd, LOWORD(wParam),
1063 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1064 break;
1066 break;
1067 case WM_NOTIFY:
1069 LPNMHDR header = (LPNMHDR)lParam;
1070 if(header->code == PSN_APPLY)
1072 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1073 char sid[4];
1074 int id;
1076 GetWindowTextA(hIdWnd, sid, 4);
1077 id = atoi(sid);
1078 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1079 wordWrap[id] = ID_WORDWRAP_NONE;
1080 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1081 wordWrap[id] = ID_WORDWRAP_WINDOW;
1082 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1083 wordWrap[id] = ID_WORDWRAP_MARGIN;
1085 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1086 barState[id] |= (1 << BANDID_TOOLBAR);
1087 else
1088 barState[id] &= ~(1 << BANDID_TOOLBAR);
1090 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1091 barState[id] |= (1 << BANDID_FORMATBAR);
1092 else
1093 barState[id] &= ~(1 << BANDID_FORMATBAR);
1095 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1096 barState[id] |= (1 << BANDID_RULER);
1097 else
1098 barState[id] &= ~(1 << BANDID_RULER);
1100 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1101 barState[id] |= (1 << BANDID_STATUSBAR);
1102 else
1103 barState[id] &= ~(1 << BANDID_STATUSBAR);
1106 break;
1108 return FALSE;
1111 static void dialog_viewproperties(void)
1113 PROPSHEETPAGEW psp[2];
1114 PROPSHEETHEADERW psh;
1115 size_t i;
1116 HINSTANCE hInstance = GetModuleHandleW(0);
1117 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1119 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1120 psp[0].dwFlags = PSP_USETITLE;
1121 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1122 psp[0].pfnDlgProc = formatopts_proc;
1123 psp[0].hInstance = hInstance;
1124 psp[0].lParam = reg_formatindex(SF_TEXT);
1125 psp[0].pfnCallback = NULL;
1126 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1127 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1129 psp[i].dwSize = psp[0].dwSize;
1130 psp[i].dwFlags = psp[0].dwFlags;
1131 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1132 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1133 psp[i].hInstance = psp[0].hInstance;
1134 psp[i].lParam = reg_formatindex(SF_RTF);
1135 psp[i].pfnCallback = psp[0].pfnCallback;
1136 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1139 psh.dwSize = sizeof(psh);
1140 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1141 psh.hwndParent = hMainWnd;
1142 psh.hInstance = hInstance;
1143 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1144 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1145 U3(psh).ppsp = ppsp;
1146 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1148 if(fileFormat & SF_RTF)
1149 U2(psh).nStartPage = 1;
1150 else
1151 U2(psh).nStartPage = 0;
1152 PropertySheetW(&psh);
1153 set_bar_states();
1154 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1157 static void HandleCommandLine(LPWSTR cmdline)
1159 WCHAR delimiter;
1160 BOOL opt_print = FALSE;
1162 /* skip white space */
1163 while (*cmdline == ' ') cmdline++;
1165 /* skip executable name */
1166 delimiter = (*cmdline == '"' ? '"' : ' ');
1168 if (*cmdline == delimiter) cmdline++;
1169 while (*cmdline && *cmdline != delimiter) cmdline++;
1170 if (*cmdline == delimiter) cmdline++;
1172 while (*cmdline)
1174 while (isspace(*cmdline)) cmdline++;
1176 if (*cmdline == '-' || *cmdline == '/')
1178 if (!cmdline[2] || isspace(cmdline[2]))
1180 switch (cmdline[1])
1182 case 'P':
1183 case 'p':
1184 opt_print = TRUE;
1185 cmdline += 2;
1186 continue;
1189 /* a filename starting by / */
1191 break;
1194 if (*cmdline)
1196 /* file name is passed on the command line */
1197 if (cmdline[0] == '"')
1199 cmdline++;
1200 cmdline[lstrlenW(cmdline) - 1] = 0;
1202 DoOpenFile(cmdline);
1203 InvalidateRect(hMainWnd, NULL, FALSE);
1206 if (opt_print)
1207 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1210 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1212 if(pFr->Flags & FR_DIALOGTERM)
1214 hFindWnd = 0;
1215 pFr->Flags = FR_FINDNEXT;
1216 return 0;
1219 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1221 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1222 DWORD flags;
1223 FINDTEXTEXW ft;
1224 CHARRANGE sel;
1225 LRESULT ret = -1;
1226 HMENU hMenu = GetMenu(hMainWnd);
1227 MENUITEMINFOW mi;
1229 mi.cbSize = sizeof(mi);
1230 mi.fMask = MIIM_DATA;
1231 mi.dwItemData = 1;
1232 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1234 /* Make sure find field is saved. */
1235 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1237 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1238 sizeof(custom_data->findBuffer) / sizeof(WCHAR));
1239 pFr->lpstrFindWhat = custom_data->findBuffer;
1242 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1243 if(custom_data->endPos == -1) {
1244 custom_data->endPos = sel.cpMin;
1245 custom_data->wrapped = FALSE;
1248 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1249 ft.lpstrText = pFr->lpstrFindWhat;
1251 /* Only replace the existing selection if it is an exact match. */
1252 if (sel.cpMin != sel.cpMax &&
1253 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1255 ft.chrg = sel;
1256 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1257 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1258 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1259 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1263 /* Search from the start of the selection, but exclude the first character
1264 * from search if there is a selection. */
1265 ft.chrg.cpMin = sel.cpMin;
1266 if (sel.cpMin != sel.cpMax)
1267 ft.chrg.cpMin++;
1269 /* Search to the end, then wrap around and search from the start. */
1270 if (!custom_data->wrapped) {
1271 ft.chrg.cpMax = -1;
1272 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1273 if (ret == -1) {
1274 custom_data->wrapped = TRUE;
1275 ft.chrg.cpMin = 0;
1279 if (ret == -1) {
1280 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1281 if (ft.chrg.cpMax > ft.chrg.cpMin)
1282 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1285 if (ret == -1) {
1286 custom_data->endPos = -1;
1287 EnableWindow(hMainWnd, FALSE);
1288 MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1289 wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
1290 EnableWindow(hMainWnd, TRUE);
1291 } else {
1292 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1293 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1295 if (pFr->Flags & FR_REPLACEALL)
1296 return handle_findmsg(pFr);
1300 return 0;
1303 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1305 static WCHAR selBuffer[128];
1306 static WCHAR replaceBuffer[128];
1307 static FINDREPLACE_custom custom_data;
1308 static const WCHAR endl = '\r';
1309 FINDTEXTW ft;
1311 /* Allow only one search/replace dialog to open */
1312 if(hFindWnd != NULL)
1314 SetActiveWindow(hFindWnd);
1315 return;
1318 ZeroMemory(fr, sizeof(FINDREPLACEW));
1319 fr->lStructSize = sizeof(FINDREPLACEW);
1320 fr->hwndOwner = hMainWnd;
1321 fr->Flags = FR_HIDEUPDOWN;
1322 /* Find field is filled with the selected text if it is non-empty
1323 * and stays within the same paragraph, otherwise the previous
1324 * find field is used. */
1325 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1326 (LPARAM)&ft.chrg.cpMax);
1327 ft.lpstrText = &endl;
1328 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1329 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1331 /* Use a temporary buffer for the selected text so that the saved
1332 * find field is only overwritten when a find/replace is clicked. */
1333 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1334 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1335 fr->lpstrFindWhat = selBuffer;
1336 } else {
1337 fr->lpstrFindWhat = custom_data.findBuffer;
1339 fr->lpstrReplaceWith = replaceBuffer;
1340 custom_data.endPos = -1;
1341 custom_data.wrapped = FALSE;
1342 fr->lCustData = (LPARAM)&custom_data;
1343 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1344 fr->wReplaceWithLen = sizeof(replaceBuffer);
1346 if(replace)
1347 hFindWnd = ReplaceTextW(fr);
1348 else
1349 hFindWnd = FindTextW(fr);
1352 static int units_to_twips(UNIT unit, float number)
1354 int twips = 0;
1356 switch(unit)
1358 case UNIT_CM:
1359 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1360 break;
1362 case UNIT_INCH:
1363 twips = (int)(number * (float)TWIPS_PER_INCH);
1364 break;
1366 case UNIT_PT:
1367 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1368 break;
1371 return twips;
1374 static void append_current_units(LPWSTR buffer)
1376 static const WCHAR space[] = {' ', 0};
1377 lstrcatW(buffer, space);
1378 lstrcatW(buffer, units_cmW);
1381 static void number_with_units(LPWSTR buffer, int number)
1383 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1384 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1386 sprintfW(buffer, fmt, converted, units_cmW);
1389 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1391 COMBOBOXEXITEMW cbItem;
1392 COMBOBOXINFO cbInfo;
1393 HWND hCombo, hList;
1394 int idx, result;
1396 hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1397 if (!hCombo)
1398 return FALSE;
1399 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1400 result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1401 if (!result)
1402 return FALSE;
1403 hList = cbInfo.hwndList;
1404 idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
1405 if (idx < 0)
1406 return FALSE;
1408 ZeroMemory(&cbItem, sizeof(cbItem));
1409 cbItem.mask = CBEIF_TEXT;
1410 cbItem.iItem = idx;
1411 cbItem.pszText = wszBuffer;
1412 cbItem.cchTextMax = bufferLength-1;
1413 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1415 return result != 0;
1418 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1420 switch(message)
1422 case WM_INITDIALOG:
1424 WCHAR buffer[MAX_STRING_LEN];
1425 SYSTEMTIME st;
1426 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1427 GetLocalTime(&st);
1429 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1430 MAX_STRING_LEN);
1431 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1432 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1433 MAX_STRING_LEN);
1434 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1435 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1436 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1438 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1440 break;
1442 case WM_COMMAND:
1443 switch(LOWORD(wParam))
1445 case IDC_DATETIME:
1446 if (HIWORD(wParam) != LBN_DBLCLK)
1447 break;
1448 /* Fall through */
1450 case IDOK:
1452 LRESULT index;
1453 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1455 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1457 if(index != LB_ERR)
1459 WCHAR buffer[MAX_STRING_LEN];
1460 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1461 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1464 /* Fall through */
1466 case IDCANCEL:
1467 EndDialog(hWnd, wParam);
1468 return TRUE;
1471 return FALSE;
1474 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1476 switch(message)
1478 case WM_INITDIALOG:
1480 HINSTANCE hInstance = GetModuleHandleW(0);
1481 WCHAR buffer[MAX_STRING_LEN];
1482 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1484 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1485 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1486 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1487 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1488 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1489 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1491 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1493 break;
1495 case WM_COMMAND:
1496 switch(LOWORD(wParam))
1498 case IDOK:
1500 LRESULT index;
1501 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1502 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1504 if(index != LB_ERR)
1505 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1507 return TRUE;
1509 case IDCANCEL:
1510 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1511 return TRUE;
1514 return FALSE;
1517 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1519 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1521 switch(message)
1523 case WM_INITDIALOG:
1525 HINSTANCE hInstance = GetModuleHandleW(0);
1526 WCHAR buffer[MAX_STRING_LEN];
1527 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1528 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1529 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1530 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1531 PARAFORMAT2 pf;
1532 int index = 0;
1534 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1535 MAX_STRING_LEN);
1536 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1537 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1538 MAX_STRING_LEN);
1539 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1540 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1541 MAX_STRING_LEN);
1542 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1544 pf.cbSize = sizeof(pf);
1545 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1546 PFM_STARTINDENT;
1547 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1549 if(pf.wAlignment == PFA_RIGHT)
1550 index ++;
1551 else if(pf.wAlignment == PFA_CENTER)
1552 index += 2;
1554 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1556 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1557 SetWindowTextW(hLeftWnd, buffer);
1558 number_with_units(buffer, pf.dxRightIndent);
1559 SetWindowTextW(hRightWnd, buffer);
1560 number_with_units(buffer, -pf.dxOffset);
1561 SetWindowTextW(hFirstWnd, buffer);
1563 break;
1565 case WM_COMMAND:
1566 switch(LOWORD(wParam))
1568 case IDOK:
1570 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1571 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1572 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1573 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1574 WCHAR buffer[MAX_STRING_LEN];
1575 int index;
1576 float num;
1577 int ret = 0;
1578 PARAFORMAT2 pf;
1579 UNIT unit;
1580 BOOL in_list = FALSE;
1582 pf.cbSize = sizeof(pf);
1583 pf.dwMask = PFM_NUMBERING;
1584 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1585 if ((pf.dwMask & PFM_NUMBERING) && pf.wNumbering)
1586 in_list = TRUE;
1588 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1589 pf.wAlignment = ALIGNMENT_VALUES[index];
1591 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1592 if(number_from_string(buffer, &num, &unit))
1593 ret++;
1594 pf.dxOffset = units_to_twips(unit, num);
1595 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1596 if(number_from_string(buffer, &num, &unit))
1597 ret++;
1598 pf.dxRightIndent = units_to_twips(unit, num);
1599 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1600 if(number_from_string(buffer, &num, &unit))
1601 ret++;
1602 pf.dxStartIndent = units_to_twips(unit, num);
1604 if(ret != 3)
1606 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1607 wszAppTitle, MB_OK | MB_ICONASTERISK);
1608 return FALSE;
1609 } else
1611 if (pf.dxOffset + pf.dxStartIndent < 0
1612 && pf.dxStartIndent < 0)
1614 /* The first line is before the left edge, so
1615 * make sure it is at the left edge. */
1616 pf.dxOffset = -pf.dxStartIndent;
1617 } else if (pf.dxOffset < 0) {
1618 /* The second and following lines are before
1619 * the left edge, so set it to be at the left
1620 * edge, and adjust the first line since it
1621 * is relative to it. */
1622 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1623 pf.dxOffset = 0;
1625 /* Internally the dxStartIndent is the absolute
1626 * offset for the first line and dxOffset is
1627 * to it value as opposed how it is displayed with
1628 * the first line being the relative value.
1629 * These two lines make the adjustments. */
1630 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1631 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1633 pf.cbSize = sizeof(pf);
1634 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1635 PFM_STARTINDENT;
1636 if (in_list)
1638 pf.wNumberingTab = max(pf.dxOffset, 0);
1639 pf.dwMask |= PFM_NUMBERINGTAB;
1642 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1645 /* Fall through */
1647 case IDCANCEL:
1648 EndDialog(hWnd, wParam);
1649 return TRUE;
1652 return FALSE;
1655 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1657 switch(message)
1659 case WM_INITDIALOG:
1661 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1662 PARAFORMAT pf;
1663 WCHAR buffer[MAX_STRING_LEN];
1664 int i;
1666 pf.cbSize = sizeof(pf);
1667 pf.dwMask = PFM_TABSTOPS;
1668 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1669 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1671 for(i = 0; i < pf.cTabCount; i++)
1673 number_with_units(buffer, pf.rgxTabs[i]);
1674 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1676 SetFocus(hTabWnd);
1678 break;
1680 case WM_COMMAND:
1681 switch(LOWORD(wParam))
1683 case IDC_TABSTOPS:
1685 HWND hTabWnd = (HWND)lParam;
1686 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1687 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1688 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1690 if(GetWindowTextLengthW(hTabWnd))
1691 EnableWindow(hAddWnd, TRUE);
1692 else
1693 EnableWindow(hAddWnd, FALSE);
1695 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1697 EnableWindow(hEmptyWnd, TRUE);
1699 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1700 EnableWindow(hDelWnd, FALSE);
1701 else
1702 EnableWindow(hDelWnd, TRUE);
1703 } else
1705 EnableWindow(hEmptyWnd, FALSE);
1708 break;
1710 case ID_TAB_ADD:
1712 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1713 WCHAR buffer[MAX_STRING_LEN];
1714 UNIT unit;
1716 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1717 append_current_units(buffer);
1719 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1721 float number = 0;
1722 int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
1724 if(!number_from_string(buffer, &number, &unit))
1726 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1727 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1728 } else if (item_count >= MAX_TAB_STOPS) {
1729 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1730 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1731 } else {
1732 int i;
1733 float next_number = -1;
1734 int next_number_in_twips = -1;
1735 int insert_number = units_to_twips(unit, number);
1737 /* linear search for position to insert the string */
1738 for(i = 0; i < item_count; i++)
1740 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1741 number_from_string(buffer, &next_number, &unit);
1742 next_number_in_twips = units_to_twips(unit, next_number);
1743 if (insert_number <= next_number_in_twips)
1744 break;
1746 if (insert_number != next_number_in_twips)
1748 number_with_units(buffer, insert_number);
1749 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1750 SetWindowTextW(hTabWnd, 0);
1754 SetFocus(hTabWnd);
1756 break;
1758 case ID_TAB_DEL:
1760 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1761 LRESULT ret;
1762 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1763 if(ret != CB_ERR)
1764 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1766 break;
1768 case ID_TAB_EMPTY:
1770 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1771 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1772 SetFocus(hTabWnd);
1774 break;
1776 case IDOK:
1778 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1779 int i;
1780 WCHAR buffer[MAX_STRING_LEN];
1781 PARAFORMAT pf;
1782 float number;
1783 UNIT unit;
1785 pf.cbSize = sizeof(pf);
1786 pf.dwMask = PFM_TABSTOPS;
1788 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1789 (LPARAM)&buffer) != CB_ERR &&
1790 i < MAX_TAB_STOPS; i++)
1792 number_from_string(buffer, &number, &unit);
1793 pf.rgxTabs[i] = units_to_twips(unit, number);
1795 pf.cTabCount = i;
1796 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1798 /* Fall through */
1799 case IDCANCEL:
1800 EndDialog(hWnd, wParam);
1801 return TRUE;
1804 return FALSE;
1807 static LRESULT OnCreate( HWND hWnd )
1809 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1810 HINSTANCE hInstance = GetModuleHandleW(0);
1811 HANDLE hDLL;
1812 TBADDBITMAP ab;
1813 int nStdBitmaps = 0;
1814 REBARINFO rbi;
1815 REBARBANDINFOW rbb;
1816 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1817 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1819 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1821 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1822 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1823 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1825 rbi.cbSize = sizeof(rbi);
1826 rbi.fMask = 0;
1827 rbi.himl = NULL;
1828 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1829 return -1;
1831 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS,
1832 IDC_TOOLBAR,
1833 1, hInstance, IDB_TOOLBAR,
1834 NULL, 0,
1835 24, 24, 16, 16, sizeof(TBBUTTON));
1837 ab.hInst = HINST_COMMCTRL;
1838 ab.nID = IDB_STD_SMALL_COLOR;
1839 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1841 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1842 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1843 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1844 AddSeparator(hToolBarWnd);
1845 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1846 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1847 AddSeparator(hToolBarWnd);
1848 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1849 AddSeparator(hToolBarWnd);
1850 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1851 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1852 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1853 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1854 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1855 AddSeparator(hToolBarWnd);
1856 AddButton(hToolBarWnd, 0, ID_DATETIME);
1858 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1860 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1861 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1862 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1863 rbb.cx = 0;
1864 rbb.hwndChild = hToolBarWnd;
1865 rbb.cxMinChild = 0;
1866 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1867 rbb.wID = BANDID_TOOLBAR;
1869 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1871 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1872 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1873 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1875 rbb.hwndChild = hFontListWnd;
1876 rbb.cx = 200;
1877 rbb.wID = BANDID_FONTLIST;
1879 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1881 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1882 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1883 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1885 rbb.hwndChild = hSizeListWnd;
1886 rbb.cx = 50;
1887 rbb.fStyle ^= RBBS_BREAK;
1888 rbb.wID = BANDID_SIZELIST;
1890 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1892 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1893 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS,
1894 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1896 SendMessageW(hFormatBarWnd, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
1898 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1899 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1900 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1901 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1902 AddSeparator(hFormatBarWnd);
1903 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1904 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1905 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1906 AddSeparator(hFormatBarWnd);
1907 AddButtonStyle(hFormatBarWnd, 7, ID_BULLETONOFF, BTNS_DROPDOWN);
1909 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1911 rbb.hwndChild = hFormatBarWnd;
1912 rbb.wID = BANDID_FORMATBAR;
1914 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1916 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1917 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1920 rbb.hwndChild = hRulerWnd;
1921 rbb.wID = BANDID_RULER;
1922 rbb.fStyle |= RBBS_BREAK;
1924 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1926 hDLL = LoadLibraryW(wszRichEditDll);
1927 if(!hDLL)
1929 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1930 MB_OK | MB_ICONEXCLAMATION);
1931 PostQuitMessage(1);
1934 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1935 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1936 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1937 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1939 if (!hEditorWnd)
1941 fprintf(stderr, "Error code %u\n", GetLastError());
1942 return -1;
1944 assert(hEditorWnd);
1946 setup_richedit_olecallback(hEditorWnd);
1947 SetFocus(hEditorWnd);
1948 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1950 set_default_font();
1952 populate_font_list(hFontListWnd);
1953 populate_size_list(hSizeListWnd);
1954 DoLoadStrings();
1955 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1957 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1959 registry_read_filelist(hWnd);
1960 registry_read_formatopts_all(barState, wordWrap);
1961 registry_read_options();
1962 DragAcceptFiles(hWnd, TRUE);
1964 return 0;
1967 static LRESULT OnUser( HWND hWnd )
1969 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1970 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1971 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1972 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1973 int from, to;
1974 CHARFORMAT2W fmt;
1975 PARAFORMAT2 pf;
1976 GETTEXTLENGTHEX gt;
1978 ZeroMemory(&fmt, sizeof(fmt));
1979 fmt.cbSize = sizeof(fmt);
1981 ZeroMemory(&pf, sizeof(pf));
1982 pf.cbSize = sizeof(pf);
1984 gt.flags = GTL_NUMCHARS;
1985 gt.codepage = 1200;
1987 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1988 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1990 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1992 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1993 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1994 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1995 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1996 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1997 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1998 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2000 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2001 (fmt.dwEffects & CFE_BOLD));
2002 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2003 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2004 (fmt.dwEffects & CFE_ITALIC));
2005 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2006 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2007 (fmt.dwEffects & CFE_UNDERLINE));
2008 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2010 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2011 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2012 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2013 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2015 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLETONOFF, pf.wNumbering ? TRUE : FALSE);
2016 return 0;
2019 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2021 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2022 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2023 NMHDR *pHdr = (NMHDR *)lParam;
2024 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2025 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2026 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2028 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2030 if (pHdr->code == CBEN_ENDEDITW)
2032 NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
2033 if(pHdr->hwndFrom == hwndFontList)
2035 on_fontlist_modified(endEdit->szText);
2036 } else if (pHdr->hwndFrom == hwndSizeList)
2038 on_sizelist_modified(hwndSizeList,endEdit->szText);
2041 return 0;
2044 if (pHdr->hwndFrom == hwndFormatBar)
2046 if (pHdr->code == TBN_DROPDOWN)
2048 NMTOOLBARW *tb_notify = (NMTOOLBARW *)lParam;
2049 HMENU menu = GetMenu( hWnd );
2050 MENUITEMINFOW info;
2051 TPMPARAMS params;
2052 RECT rc;
2054 if (!menu) return 0;
2055 info.cbSize = sizeof(info);
2056 info.fMask = MIIM_SUBMENU;
2057 GetMenuItemInfoW( menu, ID_LISTMENU, FALSE, &info );
2058 if (!info.hSubMenu) return 0;
2060 SendMessageW( tb_notify->hdr.hwndFrom, TB_GETRECT, (WPARAM)tb_notify->iItem, (LPARAM)&rc );
2061 MapWindowPoints( tb_notify->hdr.hwndFrom, HWND_DESKTOP, (LPPOINT)&rc, 2 );
2063 params.cbSize = sizeof(params);
2064 params.rcExclude = rc;
2065 TrackPopupMenuEx( info.hSubMenu,
2066 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
2067 rc.left, rc.bottom, hWnd, &params );
2070 return 0;
2073 if (pHdr->hwndFrom == hwndEditor)
2075 if (pHdr->code == EN_SELCHANGE)
2077 SELCHANGE *pSC = (SELCHANGE *)lParam;
2078 char buf[128];
2080 update_font_list();
2082 sprintf( buf,"selection = %d..%d, line count=%ld",
2083 pSC->chrg.cpMin, pSC->chrg.cpMax,
2084 SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
2085 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2086 SendMessageW(hWnd, WM_USER, 0, 0);
2087 return 1;
2090 return 0;
2093 /* Copied from dlls/comdlg32/fontdlg.c */
2094 static const COLORREF textcolors[]=
2096 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2097 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2098 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2099 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2102 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2104 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2105 static FINDREPLACEW findreplace;
2107 if ((HWND)lParam == hwndEditor)
2108 return 0;
2110 switch(LOWORD(wParam))
2113 case ID_FILE_EXIT:
2114 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2115 break;
2117 case ID_FILE_NEW:
2119 HINSTANCE hInstance = GetModuleHandleW(0);
2120 int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
2122 if(ret != ID_NEWFILE_ABORT)
2124 if(prompt_save_changes())
2126 SETTEXTEX st;
2128 set_caption(NULL);
2129 wszFileName[0] = '\0';
2131 clear_formatting();
2133 st.flags = ST_DEFAULT;
2134 st.codepage = 1200;
2135 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2137 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2138 set_fileformat(ret);
2139 update_font_list();
2143 break;
2145 case ID_FILE_OPEN:
2146 DialogOpenFile();
2147 break;
2149 case ID_FILE_SAVE:
2150 if(wszFileName[0])
2152 DoSaveFile(wszFileName, fileFormat);
2153 break;
2155 /* Fall through */
2157 case ID_FILE_SAVEAS:
2158 DialogSaveFile();
2159 break;
2161 case ID_FILE_RECENT1:
2162 case ID_FILE_RECENT2:
2163 case ID_FILE_RECENT3:
2164 case ID_FILE_RECENT4:
2166 HMENU hMenu = GetMenu(hWnd);
2167 MENUITEMINFOW mi;
2169 mi.cbSize = sizeof(MENUITEMINFOW);
2170 mi.fMask = MIIM_DATA;
2171 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2172 DoOpenFile((LPWSTR)mi.dwItemData);
2174 break;
2176 case ID_FIND:
2177 dialog_find(&findreplace, FALSE);
2178 break;
2180 case ID_FIND_NEXT:
2181 handle_findmsg(&findreplace);
2182 break;
2184 case ID_REPLACE:
2185 dialog_find(&findreplace, TRUE);
2186 break;
2188 case ID_FONTSETTINGS:
2189 dialog_choose_font();
2190 break;
2192 case ID_PRINT:
2193 dialog_print(hWnd, wszFileName);
2194 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2195 break;
2197 case ID_PRINT_QUICK:
2198 print_quick(hMainWnd, wszFileName);
2199 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2200 break;
2202 case ID_PREVIEW:
2204 int index = reg_formatindex(fileFormat);
2205 DWORD tmp = barState[index];
2206 barState[index] = 1 << BANDID_STATUSBAR;
2207 set_bar_states();
2208 barState[index] = tmp;
2209 ShowWindow(hEditorWnd, FALSE);
2211 init_preview(hWnd, wszFileName);
2213 SetMenu(hWnd, NULL);
2214 InvalidateRect(0, 0, TRUE);
2216 break;
2218 case ID_PRINTSETUP:
2219 dialog_printsetup(hWnd);
2220 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2221 break;
2223 case ID_FORMAT_BOLD:
2224 case ID_FORMAT_ITALIC:
2225 case ID_FORMAT_UNDERLINE:
2227 CHARFORMAT2W fmt;
2228 int effects = CFE_BOLD;
2230 ZeroMemory(&fmt, sizeof(fmt));
2231 fmt.cbSize = sizeof(fmt);
2232 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2234 fmt.dwMask = CFM_BOLD;
2236 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2238 effects = CFE_ITALIC;
2239 fmt.dwMask = CFM_ITALIC;
2240 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2242 effects = CFE_UNDERLINE;
2243 fmt.dwMask = CFM_UNDERLINE;
2246 fmt.dwEffects ^= effects;
2248 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2249 break;
2252 case ID_FORMAT_COLOR:
2254 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2255 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2256 HMENU hPop;
2257 RECT itemrc;
2258 POINT pt;
2259 int mid;
2260 int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2262 SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2263 pt.x = itemrc.left;
2264 pt.y = itemrc.bottom;
2265 ClientToScreen(hFormatBarWnd, &pt);
2266 hPop = GetSubMenu(hColorPopupMenu, 0);
2267 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2268 TPM_RETURNCMD | TPM_NONOTIFY,
2269 pt.x, pt.y, 0, hWnd, 0);
2270 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2272 CHARFORMAT2W fmt;
2274 ZeroMemory(&fmt, sizeof(fmt));
2275 fmt.cbSize = sizeof(fmt);
2276 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2278 fmt.dwMask = CFM_COLOR;
2280 if (mid < ID_COLOR_AUTOMATIC) {
2281 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2282 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2283 } else {
2284 fmt.dwEffects |= CFE_AUTOCOLOR;
2287 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2289 break;
2292 case ID_EDIT_CUT:
2293 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2294 break;
2296 case ID_EDIT_COPY:
2297 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2298 break;
2300 case ID_EDIT_PASTE:
2301 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2302 break;
2304 case ID_EDIT_CLEAR:
2305 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2306 break;
2308 case ID_EDIT_SELECTALL:
2310 CHARRANGE range = {0, -1};
2311 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2312 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2313 return 0;
2316 case ID_EDIT_GETTEXT:
2318 int nLen = GetWindowTextLengthW(hwndEditor);
2319 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2320 TEXTRANGEW tr;
2322 GetWindowTextW(hwndEditor, data, nLen+1);
2323 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2325 HeapFree( GetProcessHeap(), 0, data);
2326 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2327 tr.chrg.cpMin = 0;
2328 tr.chrg.cpMax = nLen;
2329 tr.lpstrText = data;
2330 SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2331 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2332 HeapFree( GetProcessHeap(), 0, data );
2334 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2335 return 0;
2338 case ID_EDIT_CHARFORMAT:
2339 case ID_EDIT_DEFCHARFORMAT:
2341 CHARFORMAT2W cf;
2343 ZeroMemory(&cf, sizeof(cf));
2344 cf.cbSize = sizeof(cf);
2345 cf.dwMask = 0;
2346 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2347 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2348 return 0;
2351 case ID_EDIT_PARAFORMAT:
2353 PARAFORMAT2 pf;
2354 ZeroMemory(&pf, sizeof(pf));
2355 pf.cbSize = sizeof(pf);
2356 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2357 return 0;
2360 case ID_EDIT_SELECTIONINFO:
2362 CHARRANGE range = {0, -1};
2363 char buf[128];
2364 WCHAR *data = NULL;
2366 SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2367 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2368 SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2369 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2370 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2371 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2372 HeapFree( GetProcessHeap(), 0, data);
2373 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2374 return 0;
2377 case ID_EDIT_READONLY:
2379 LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
2380 if (nStyle & ES_READONLY)
2381 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2382 else
2383 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2384 return 0;
2387 case ID_EDIT_MODIFIED:
2388 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2389 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2390 else
2391 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2392 return 0;
2394 case ID_EDIT_UNDO:
2395 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2396 return 0;
2398 case ID_EDIT_REDO:
2399 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2400 return 0;
2402 case ID_BULLETONOFF:
2403 case ID_BULLET:
2404 case ID_NUMBERING:
2405 case ID_LCLETTER:
2406 case ID_UCLETTER:
2407 case ID_LCROMAN:
2408 case ID_UCROMAN:
2410 PARAFORMAT2 pf;
2411 WORD new_number = LOWORD(wParam) - ID_BULLET + PFN_BULLET;
2412 pf.cbSize = sizeof(pf);
2413 pf.dwMask = PFM_NUMBERING;
2414 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2416 pf.dwMask = PFM_NUMBERING | PFM_NUMBERINGSTART | PFM_NUMBERINGSTYLE | PFM_NUMBERINGTAB | PFM_OFFSET | PFM_OFFSETINDENT;
2418 if(pf.wNumbering && ((pf.wNumbering == new_number) || (LOWORD(wParam) == ID_BULLETONOFF)))
2420 pf.wNumbering = 0;
2421 pf.wNumberingStart = 0;
2422 pf.wNumberingStyle = 0;
2423 pf.wNumberingTab = 0;
2424 pf.dxOffset = 0;
2425 pf.dxStartIndent = -360;
2426 } else
2428 pf.dxStartIndent = pf.wNumbering ? 0 : 360;
2430 if (LOWORD(wParam) == ID_BULLETONOFF)
2431 pf.wNumbering = last_bullet;
2432 else
2434 pf.wNumbering = new_number;
2435 last_bullet = pf.wNumbering;
2437 pf.wNumberingStart = 1;
2438 pf.wNumberingStyle = PFNS_PERIOD;
2439 pf.wNumberingTab = 360;
2440 pf.dxOffset = 360;
2443 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2445 break;
2447 case ID_ALIGN_LEFT:
2448 case ID_ALIGN_CENTER:
2449 case ID_ALIGN_RIGHT:
2451 PARAFORMAT2 pf;
2453 pf.cbSize = sizeof(pf);
2454 pf.dwMask = PFM_ALIGNMENT;
2455 switch(LOWORD(wParam)) {
2456 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2457 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2458 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2460 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2461 break;
2464 case ID_BACK_1:
2465 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2466 break;
2468 case ID_BACK_2:
2469 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2470 break;
2472 case ID_TOGGLE_TOOLBAR:
2473 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2474 update_window();
2475 break;
2477 case ID_TOGGLE_FORMATBAR:
2478 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2479 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2480 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2481 update_window();
2482 break;
2484 case ID_TOGGLE_STATUSBAR:
2485 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2486 update_window();
2487 break;
2489 case ID_TOGGLE_RULER:
2490 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2491 update_window();
2492 break;
2494 case ID_DATETIME:
2495 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2496 break;
2498 case ID_PARAFORMAT:
2499 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2500 break;
2502 case ID_TABSTOPS:
2503 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2504 break;
2506 case ID_ABOUT:
2507 dialog_about();
2508 break;
2510 case ID_VIEWPROPERTIES:
2511 dialog_viewproperties();
2512 break;
2514 case IDC_FONTLIST:
2515 if (HIWORD(wParam) == CBN_SELENDOK)
2517 WCHAR buffer[LF_FACESIZE];
2518 HWND hwndFontList = (HWND)lParam;
2519 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2520 on_fontlist_modified(buffer);
2522 break;
2524 case IDC_SIZELIST:
2525 if (HIWORD(wParam) == CBN_SELENDOK)
2527 WCHAR buffer[MAX_STRING_LEN+1];
2528 HWND hwndSizeList = (HWND)lParam;
2529 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2530 on_sizelist_modified(hwndSizeList, buffer);
2532 break;
2534 default:
2535 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2536 break;
2538 return 0;
2541 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2543 HMENU hMenu = (HMENU)wParam;
2544 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2545 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2546 PARAFORMAT pf;
2547 int nAlignment = -1;
2548 int selFrom, selTo;
2549 GETTEXTLENGTHEX gt;
2550 LRESULT textLength;
2551 MENUITEMINFOW mi;
2553 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2554 EnableMenuItem(hMenu, ID_EDIT_COPY, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2555 EnableMenuItem(hMenu, ID_EDIT_CUT, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2557 pf.cbSize = sizeof(PARAFORMAT);
2558 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2559 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2560 (GetWindowLongW(hwndEditor, GWL_STYLE) & ES_READONLY) ? MF_CHECKED : MF_UNCHECKED);
2561 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2562 SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED);
2563 if (pf.dwMask & PFM_ALIGNMENT)
2564 nAlignment = pf.wAlignment;
2565 CheckMenuItem(hMenu, ID_ALIGN_LEFT, (nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
2566 CheckMenuItem(hMenu, ID_ALIGN_CENTER, (nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
2567 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, (nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
2569 CheckMenuItem(hMenu, ID_BULLET, ((pf.wNumbering == PFN_BULLET) ? MF_CHECKED : MF_UNCHECKED));
2570 CheckMenuItem(hMenu, ID_NUMBERING, ((pf.wNumbering == PFN_ARABIC) ? MF_CHECKED : MF_UNCHECKED));
2571 CheckMenuItem(hMenu, ID_LCLETTER, ((pf.wNumbering == PFN_LCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2572 CheckMenuItem(hMenu, ID_UCLETTER, ((pf.wNumbering == PFN_UCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2573 CheckMenuItem(hMenu, ID_LCROMAN, ((pf.wNumbering == PFN_LCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2574 CheckMenuItem(hMenu, ID_UCROMAN, ((pf.wNumbering == PFN_UCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2576 EnableMenuItem(hMenu, ID_EDIT_UNDO, SendMessageW(hwndEditor, EM_CANUNDO, 0, 0) ?
2577 MF_ENABLED : MF_GRAYED);
2578 EnableMenuItem(hMenu, ID_EDIT_REDO, SendMessageW(hwndEditor, EM_CANREDO, 0, 0) ?
2579 MF_ENABLED : MF_GRAYED);
2581 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, is_bar_visible(BANDID_TOOLBAR) ?
2582 MF_CHECKED : MF_UNCHECKED);
2584 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, is_bar_visible(BANDID_FORMATBAR) ?
2585 MF_CHECKED : MF_UNCHECKED);
2587 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, IsWindowVisible(hwndStatus) ?
2588 MF_CHECKED : MF_UNCHECKED);
2590 CheckMenuItem(hMenu, ID_TOGGLE_RULER, is_bar_visible(BANDID_RULER) ? MF_CHECKED : MF_UNCHECKED);
2592 gt.flags = GTL_NUMCHARS;
2593 gt.codepage = 1200;
2594 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2595 EnableMenuItem(hMenu, ID_FIND, textLength ? MF_ENABLED : MF_GRAYED);
2597 mi.cbSize = sizeof(mi);
2598 mi.fMask = MIIM_DATA;
2600 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2602 EnableMenuItem(hMenu, ID_FIND_NEXT, (textLength && mi.dwItemData) ? MF_ENABLED : MF_GRAYED);
2604 EnableMenuItem(hMenu, ID_REPLACE, textLength ? MF_ENABLED : MF_GRAYED);
2606 return 0;
2609 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2611 int nStatusSize = 0;
2612 RECT rc;
2613 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2614 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2615 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2616 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2617 int rebarHeight = 0;
2619 if (hwndStatusBar)
2621 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2622 if (IsWindowVisible(hwndStatusBar))
2624 GetClientRect(hwndStatusBar, &rc);
2625 nStatusSize = rc.bottom - rc.top;
2626 } else
2628 nStatusSize = 0;
2631 if (hwndReBar)
2633 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2635 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2637 if (hwndEditor)
2639 GetClientRect(hWnd, &rc);
2640 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2643 redraw_ruler(hRulerWnd);
2645 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2648 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2650 if(msg == ID_FINDMSGSTRING)
2651 return handle_findmsg((LPFINDREPLACEW)lParam);
2653 switch(msg)
2655 case WM_CREATE:
2656 return OnCreate( hWnd );
2658 case WM_USER:
2659 return OnUser( hWnd );
2661 case WM_NOTIFY:
2662 return OnNotify( hWnd, lParam );
2664 case WM_COMMAND:
2665 if(preview_isactive())
2667 return preview_command( hWnd, wParam );
2670 return OnCommand( hWnd, wParam, lParam );
2672 case WM_DESTROY:
2673 PostQuitMessage(0);
2674 break;
2676 case WM_CLOSE:
2677 if(preview_isactive())
2679 preview_exit(hWnd);
2680 } else if(prompt_save_changes())
2682 registry_set_options(hMainWnd);
2683 registry_set_formatopts_all(barState, wordWrap);
2684 PostQuitMessage(0);
2686 break;
2688 case WM_ACTIVATE:
2689 if (LOWORD(wParam))
2690 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2691 return 0;
2693 case WM_INITMENUPOPUP:
2694 return OnInitPopupMenu( hWnd, wParam );
2696 case WM_SIZE:
2697 return OnSize( hWnd, wParam, lParam );
2699 case WM_CONTEXTMENU:
2700 return DefWindowProcW(hWnd, msg, wParam, lParam);
2702 case WM_DROPFILES:
2704 WCHAR file[MAX_PATH];
2705 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2706 DragFinish((HDROP)wParam);
2708 if(prompt_save_changes())
2709 DoOpenFile(file);
2711 break;
2712 case WM_PAINT:
2713 if(!preview_isactive())
2714 return DefWindowProcW(hWnd, msg, wParam, lParam);
2716 default:
2717 return DefWindowProcW(hWnd, msg, wParam, lParam);
2720 return 0;
2723 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2725 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2726 HACCEL hAccel;
2727 WNDCLASSEXW wc;
2728 MSG msg;
2729 RECT rc;
2730 UINT_PTR hPrevRulerProc;
2731 HWND hRulerWnd;
2732 POINTL EditPoint;
2733 DWORD bMaximized;
2734 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2735 'T','A','B','L','E','\0'};
2737 InitCommonControlsEx(&classes);
2739 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2741 wc.cbSize = sizeof(wc);
2742 wc.style = 0;
2743 wc.lpfnWndProc = WndProc;
2744 wc.cbClsExtra = 0;
2745 wc.cbWndExtra = 4;
2746 wc.hInstance = hInstance;
2747 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2748 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2749 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2750 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2751 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2752 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2753 wc.lpszClassName = wszMainWndClass;
2754 RegisterClassExW(&wc);
2756 wc.style = 0;
2757 wc.lpfnWndProc = preview_proc;
2758 wc.cbClsExtra = 0;
2759 wc.cbWndExtra = 0;
2760 wc.hInstance = hInstance;
2761 wc.hIcon = NULL;
2762 wc.hIconSm = NULL;
2763 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2764 wc.hbrBackground = NULL;
2765 wc.lpszMenuName = NULL;
2766 wc.lpszClassName = wszPreviewWndClass;
2767 RegisterClassExW(&wc);
2769 registry_read_winrect(&rc);
2770 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2771 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2772 registry_read_maximized(&bMaximized);
2773 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2774 && bMaximized)
2775 nCmdShow = SW_SHOWMAXIMIZED;
2776 ShowWindow(hMainWnd, nCmdShow);
2778 set_caption(NULL);
2779 set_bar_states();
2780 set_fileformat(SF_RTF);
2781 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2782 get_default_printer_opts();
2783 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2785 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2786 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2787 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2788 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2790 HandleCommandLine(GetCommandLineW());
2792 while(GetMessageW(&msg,0,0,0))
2794 if (IsDialogMessageW(hFindWnd, &msg))
2795 continue;
2797 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2798 continue;
2799 TranslateMessage(&msg);
2800 DispatchMessageW(&msg);
2801 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2802 SendMessageW(hMainWnd, WM_USER, 0, 0);
2805 return 0;