[NTVDM]
[reactos.git] / base / applications / wordpad / wordpad.c
1 /*
2 * Wordpad implementation
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_IE 0x0400
24
25 #include <stdio.h>
26 #include <assert.h>
27 #include <windef.h>
28 #include <winbase.h>
29 #include <wingdi.h>
30 #include <winuser.h>
31 #include <richedit.h>
32 #include <commctrl.h>
33 #include <commdlg.h>
34 #include <shellapi.h>
35 #include <wine/unicode.h>
36
37 #include "wordpad.h"
38
39 #ifdef NONAMELESSUNION
40 # define U(x) (x).u
41 # define U2(x) (x).u2
42 # define U3(x) (x).u3
43 #else
44 # define U(x) (x)
45 # define U2(x) (x)
46 # define U3(x) (x)
47 #endif
48
49 /* use LoadString */
50 static const WCHAR wszAppTitle[] = {'W','o','r','d','p','a','d',0};
51
52 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
53
54 static const WCHAR stringFormat[] = {'%','2','d','\0'};
55
56 const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
57 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
58
59 static HWND hMainWnd;
60 static HWND hEditorWnd;
61 static HWND hFindWnd;
62 static HMENU hColorPopupMenu;
63
64 static UINT ID_FINDMSGSTRING;
65
66 static DWORD wordWrap[2];
67 static DWORD barState[2];
68 static WPARAM fileFormat = SF_RTF;
69
70 static WCHAR wszFileName[MAX_PATH];
71 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
72 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
73 static WCHAR wszSaveChanges[MAX_STRING_LEN];
74 static WCHAR units_cmW[MAX_STRING_LEN];
75 static WCHAR units_inW[MAX_STRING_LEN];
76 static WCHAR units_inchW[MAX_STRING_LEN];
77 static WCHAR units_ptW[MAX_STRING_LEN];
78
79 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
80
81 typedef enum
82 {
83 UNIT_CM,
84 UNIT_INCH,
85 UNIT_PT
86 } UNIT;
87
88 typedef struct
89 {
90 int endPos;
91 BOOL wrapped;
92 WCHAR findBuffer[128];
93 } FINDREPLACE_custom;
94
95 /* Load string resources */
96 static void DoLoadStrings(void)
97 {
98 LPWSTR p = wszFilter;
99 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
100 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
101 static const WCHAR files_all[] = {'*','.','*','\0'};
102
103 HINSTANCE hInstance = GetModuleHandleW(0);
104
105 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
106 p += lstrlenW(p) + 1;
107 lstrcpyW(p, files_rtf);
108 p += lstrlenW(p) + 1;
109 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
110 p += lstrlenW(p) + 1;
111 lstrcpyW(p, files_txt);
112 p += lstrlenW(p) + 1;
113 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
114 p += lstrlenW(p) + 1;
115 lstrcpyW(p, files_txt);
116 p += lstrlenW(p) + 1;
117 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
118 p += lstrlenW(p) + 1;
119 lstrcpyW(p, files_all);
120 p += lstrlenW(p) + 1;
121 *p = '\0';
122
123 p = wszDefaultFileName;
124 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
125
126 p = wszSaveChanges;
127 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
128
129 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
130 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
131 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
132 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
133 }
134
135 /* Show a message box with resource strings */
136 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
137 {
138 MSGBOXPARAMSW params;
139
140 params.cbSize = sizeof(params);
141 params.hwndOwner = hWnd;
142 params.hInstance = GetModuleHandleW(0);
143 params.lpszText = lpText;
144 params.lpszCaption = lpCaption;
145 params.dwStyle = uType;
146 params.lpszIcon = NULL;
147 params.dwContextHelpId = 0;
148 params.lpfnMsgBoxCallback = NULL;
149 params.dwLanguageId = 0;
150 return MessageBoxIndirectW(&params);
151 }
152
153
154 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
155 {
156 TBBUTTON button;
157
158 ZeroMemory(&button, sizeof(button));
159 button.iBitmap = nImage;
160 button.idCommand = nCommand;
161 button.fsState = TBSTATE_ENABLED;
162 button.fsStyle = BTNS_BUTTON;
163 button.dwData = 0;
164 button.iString = -1;
165 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
166 }
167
168 static void AddSeparator(HWND hwndToolBar)
169 {
170 TBBUTTON button;
171
172 ZeroMemory(&button, sizeof(button));
173 button.iBitmap = -1;
174 button.idCommand = 0;
175 button.fsState = 0;
176 button.fsStyle = BTNS_SEP;
177 button.dwData = 0;
178 button.iString = -1;
179 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
180 }
181
182 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
183 {
184 HANDLE hFile = (HANDLE)cookie;
185 DWORD read;
186
187 if(!ReadFile(hFile, buffer, cb, &read, 0))
188 return 1;
189
190 *pcb = read;
191
192 return 0;
193 }
194
195 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
196 {
197 DWORD written;
198 int ret;
199 HANDLE hFile = (HANDLE)cookie;
200
201 ret = WriteFile(hFile, buffer, cb, &written, 0);
202
203 if(!ret || (cb != written))
204 return 1;
205
206 *pcb = cb;
207
208 return 0;
209 }
210
211 LPWSTR file_basename(LPWSTR path)
212 {
213 LPWSTR pos = path + lstrlenW(path);
214
215 while(pos > path)
216 {
217 if(*pos == '\\' || *pos == '/')
218 {
219 pos++;
220 break;
221 }
222 pos--;
223 }
224 return pos;
225 }
226
227 static void set_caption(LPCWSTR wszNewFileName)
228 {
229 static const WCHAR wszSeparator[] = {' ','-',' '};
230 WCHAR *wszCaption;
231 SIZE_T length = 0;
232
233 if(!wszNewFileName)
234 wszNewFileName = wszDefaultFileName;
235 else
236 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
237
238 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
239 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
240
241 if(!wszCaption)
242 return;
243
244 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
245 length += lstrlenW(wszNewFileName);
246 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
247 length += sizeof(wszSeparator) / sizeof(WCHAR);
248 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
249
250 SetWindowTextW(hMainWnd, wszCaption);
251
252 HeapFree(GetProcessHeap(), 0, wszCaption);
253 }
254
255 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
256 {
257 if(punit != NULL)
258 *punit = UNIT_CM;
259 if(!endptr)
260 return FALSE;
261 if(!*endptr)
262 return TRUE;
263
264 while(*endptr == ' ')
265 endptr++;
266
267 if(punit == NULL)
268 return *endptr == '\0';
269
270 if(!lstrcmpW(endptr, units_cmW))
271 {
272 *punit = UNIT_CM;
273 endptr += lstrlenW(units_cmW);
274 }
275 else if (!lstrcmpW(endptr, units_inW))
276 {
277 *punit = UNIT_INCH;
278 endptr += lstrlenW(units_inW);
279 }
280 else if (!lstrcmpW(endptr, units_inchW))
281 {
282 *punit = UNIT_INCH;
283 endptr += lstrlenW(units_inchW);
284 }
285 else if (!lstrcmpW(endptr, units_ptW))
286 {
287 *punit = UNIT_PT;
288 endptr += lstrlenW(units_ptW);
289 }
290
291 return *endptr == '\0';
292 }
293
294 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
295 {
296 double ret;
297 WCHAR *endptr;
298
299 *num = 0;
300 errno = 0;
301 ret = wcstod(string, &endptr);
302
303 if (punit != NULL)
304 *punit = UNIT_CM;
305 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
306 {
307 return FALSE;
308 } else
309 {
310 *num = (float)ret;
311 return TRUE;
312 }
313 }
314
315 static void set_size(float size)
316 {
317 CHARFORMAT2W fmt;
318
319 ZeroMemory(&fmt, sizeof(fmt));
320 fmt.cbSize = sizeof(fmt);
321 fmt.dwMask = CFM_SIZE;
322 fmt.yHeight = (int)(size * 20.0);
323 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
324 }
325
326 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
327 {
328 WCHAR sizeBuffer[MAX_STRING_LEN];
329 CHARFORMAT2W format;
330
331 ZeroMemory(&format, sizeof(format));
332 format.cbSize = sizeof(format);
333 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
334
335 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
336 if(lstrcmpW(sizeBuffer, wszNewFontSize))
337 {
338 float size = 0;
339 if(number_from_string(wszNewFontSize, &size, NULL)
340 && size > 0)
341 {
342 set_size(size);
343 } else
344 {
345 SetWindowTextW(hwndSizeList, sizeBuffer);
346 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
347 wszAppTitle, MB_OK | MB_ICONINFORMATION);
348 }
349 }
350 }
351
352 static void add_size(HWND hSizeListWnd, unsigned size)
353 {
354 WCHAR buffer[3];
355 COMBOBOXEXITEMW cbItem;
356 cbItem.mask = CBEIF_TEXT;
357 cbItem.iItem = -1;
358
359 wsprintfW(buffer, stringFormat, size);
360 cbItem.pszText = buffer;
361 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
362 }
363
364 static void populate_size_list(HWND hSizeListWnd)
365 {
366 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
367 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
368 COMBOBOXEXITEMW cbFontItem;
369 CHARFORMAT2W fmt;
370 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
371 HDC hdc = GetDC(hMainWnd);
372 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
373 WCHAR buffer[3];
374 size_t i;
375 DWORD fontStyle;
376
377 ZeroMemory(&fmt, sizeof(fmt));
378 fmt.cbSize = sizeof(fmt);
379 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
380
381 cbFontItem.mask = CBEIF_LPARAM;
382 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
383 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
384
385 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
386
387 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
388
389 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
390 {
391 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
392 GetDeviceCaps(hdc, LOGPIXELSY)));
393 } else
394 {
395 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
396 add_size(hSizeListWnd, choices[i]);
397 }
398
399 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
400 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
401 }
402
403 static void update_size_list(void)
404 {
405 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
406 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
407 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
408 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
409 CHARFORMAT2W fmt;
410
411 ZeroMemory(&fmt, sizeof(fmt));
412 fmt.cbSize = sizeof(fmt);
413
414 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
415
416 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
417 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
418
419 if(lstrcmpW(fontSize, sizeBuffer))
420 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
421 }
422
423 static void update_font_list(void)
424 {
425 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
426 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
427 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
428 WCHAR fontName[MAX_STRING_LEN];
429 CHARFORMAT2W fmt;
430
431 ZeroMemory(&fmt, sizeof(fmt));
432 fmt.cbSize = sizeof(fmt);
433
434 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
435 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
436
437 if(lstrcmpW(fontName, fmt.szFaceName))
438 {
439 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
440 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
441 } else
442 {
443 update_size_list();
444 }
445 }
446
447 static void clear_formatting(void)
448 {
449 PARAFORMAT2 pf;
450
451 pf.cbSize = sizeof(pf);
452 pf.dwMask = PFM_ALIGNMENT;
453 pf.wAlignment = PFA_LEFT;
454 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
455 }
456
457 static int fileformat_number(WPARAM format)
458 {
459 int number = 0;
460
461 if(format == SF_TEXT)
462 {
463 number = 1;
464 } else if (format == (SF_TEXT | SF_UNICODE))
465 {
466 number = 2;
467 }
468 return number;
469 }
470
471 static WPARAM fileformat_flags(int format)
472 {
473 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
474
475 return flags[format];
476 }
477
478 static void set_font(LPCWSTR wszFaceName)
479 {
480 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
481 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
482 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
483 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
484 CHARFORMAT2W fmt;
485
486 ZeroMemory(&fmt, sizeof(fmt));
487
488 fmt.cbSize = sizeof(fmt);
489 fmt.dwMask = CFM_FACE;
490
491 lstrcpyW(fmt.szFaceName, wszFaceName);
492
493 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
494
495 populate_size_list(hSizeListWnd);
496
497 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
498 }
499
500 static void set_default_font(void)
501 {
502 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
503 'R','o','m','a','n',0};
504 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
505 CHARFORMAT2W fmt;
506 LPCWSTR font;
507
508 ZeroMemory(&fmt, sizeof(fmt));
509
510 fmt.cbSize = sizeof(fmt);
511 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
512 fmt.dwEffects = 0;
513
514 if(fileFormat & SF_RTF)
515 font = richTextFont;
516 else
517 font = plainTextFont;
518
519 lstrcpyW(fmt.szFaceName, font);
520
521 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
522 }
523
524 static void on_fontlist_modified(LPWSTR wszNewFaceName)
525 {
526 CHARFORMAT2W format;
527 ZeroMemory(&format, sizeof(format));
528 format.cbSize = sizeof(format);
529 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
530
531 if(lstrcmpW(format.szFaceName, wszNewFaceName))
532 set_font(wszNewFaceName);
533 }
534
535 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
536 {
537 COMBOBOXEXITEMW cbItem;
538 WCHAR buffer[MAX_PATH];
539 int fontHeight = 0;
540
541 cbItem.mask = CBEIF_TEXT;
542 cbItem.pszText = buffer;
543 cbItem.cchTextMax = MAX_STRING_LEN;
544 cbItem.iItem = 0;
545
546 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
547 {
548 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
549 cbItem.iItem++;
550 else
551 break;
552 }
553 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
554 lstrcpyW( cbItem.pszText, fontName );
555
556 cbItem.mask |= CBEIF_LPARAM;
557 if(fontType & RASTER_FONTTYPE)
558 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
559
560 cbItem.lParam = MAKELONG(fontType,fontHeight);
561 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
562 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
563 }
564
565 static void dialog_choose_font(void)
566 {
567 CHOOSEFONTW cf;
568 LOGFONTW lf;
569 CHARFORMAT2W fmt;
570 HDC hDC = GetDC(hMainWnd);
571
572 ZeroMemory(&cf, sizeof(cf));
573 cf.lStructSize = sizeof(cf);
574 cf.hwndOwner = hMainWnd;
575 cf.lpLogFont = &lf;
576 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
577
578 ZeroMemory(&fmt, sizeof(fmt));
579 fmt.cbSize = sizeof(fmt);
580
581 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
582 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
583 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
584 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
585 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
586 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
587 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
588 cf.rgbColors = fmt.crTextColor;
589
590 if(ChooseFontW(&cf))
591 {
592 ZeroMemory(&fmt, sizeof(fmt));
593 fmt.cbSize = sizeof(fmt);
594 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
595 fmt.yHeight = cf.iPointSize * 2;
596
597 if(cf.nFontType & BOLD_FONTTYPE)
598 fmt.dwEffects |= CFE_BOLD;
599 if(cf.nFontType & ITALIC_FONTTYPE)
600 fmt.dwEffects |= CFE_ITALIC;
601 if(cf.lpLogFont->lfUnderline)
602 fmt.dwEffects |= CFE_UNDERLINE;
603 if(cf.lpLogFont->lfStrikeOut)
604 fmt.dwEffects |= CFE_STRIKEOUT;
605
606 fmt.crTextColor = cf.rgbColors;
607
608 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
609 set_font(cf.lpLogFont->lfFaceName);
610 }
611 }
612
613
614 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
615 DWORD FontType, LPARAM lParam)
616 {
617 HWND hListWnd = (HWND) lParam;
618
619 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
620 {
621
622 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
623 }
624
625 return 1;
626 }
627
628 static void populate_font_list(HWND hListWnd)
629 {
630 HDC hdc = GetDC(hMainWnd);
631 LOGFONTW fontinfo;
632 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
633 CHARFORMAT2W fmt;
634
635 fontinfo.lfCharSet = DEFAULT_CHARSET;
636 *fontinfo.lfFaceName = '\0';
637 fontinfo.lfPitchAndFamily = 0;
638
639 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
640 (LPARAM)hListWnd, 0);
641
642 ZeroMemory(&fmt, sizeof(fmt));
643 fmt.cbSize = sizeof(fmt);
644 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
645 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
646 }
647
648 static void update_window(void)
649 {
650 RECT rect;
651
652 GetClientRect(hMainWnd, &rect);
653
654 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
655 }
656
657 static BOOL is_bar_visible(int bandId)
658 {
659 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
660 }
661
662 static void store_bar_state(int bandId, BOOL show)
663 {
664 int formatIndex = reg_formatindex(fileFormat);
665
666 if(show)
667 barState[formatIndex] |= (1 << bandId);
668 else
669 barState[formatIndex] &= ~(1 << bandId);
670 }
671
672 static void set_toolbar_state(int bandId, BOOL show)
673 {
674 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
675
676 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
677
678 if(bandId == BANDID_TOOLBAR)
679 {
680 REBARBANDINFOW rbbinfo;
681 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
682
683 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
684 rbbinfo.fMask = RBBIM_STYLE;
685
686 SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
687
688 if(!show)
689 rbbinfo.fStyle &= ~RBBS_BREAK;
690 else
691 rbbinfo.fStyle |= RBBS_BREAK;
692
693 SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
694 }
695
696 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
697 store_bar_state(bandId, show);
698 }
699
700 static void set_statusbar_state(BOOL show)
701 {
702 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
703
704 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
705 store_bar_state(BANDID_STATUSBAR, show);
706 }
707
708 static void set_bar_states(void)
709 {
710 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
711 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
712 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
713 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
714 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
715 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
716
717 update_window();
718 }
719
720 static void preview_exit(HWND hMainWnd)
721 {
722 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
723 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
724
725 set_bar_states();
726 ShowWindow(hEditorWnd, TRUE);
727
728 close_preview(hMainWnd);
729
730 SetMenu(hMainWnd, hMenu);
731 registry_read_filelist(hMainWnd);
732
733 update_window();
734 }
735
736 static void set_fileformat(WPARAM format)
737 {
738 fileFormat = format;
739
740 set_bar_states();
741 set_default_font();
742 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
743 }
744
745 static void ShowOpenError(DWORD Code)
746 {
747 LPWSTR Message;
748
749 switch(Code)
750 {
751 case ERROR_ACCESS_DENIED:
752 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
753 break;
754
755 default:
756 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
757 }
758 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
759 }
760
761 static void DoOpenFile(LPCWSTR szOpenFileName)
762 {
763 HANDLE hFile;
764 EDITSTREAM es;
765 char fileStart[5];
766 DWORD readOut;
767 WPARAM format = SF_TEXT;
768
769 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
770 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
771 if (hFile == INVALID_HANDLE_VALUE)
772 {
773 ShowOpenError(GetLastError());
774 return;
775 }
776
777 ReadFile(hFile, fileStart, 5, &readOut, NULL);
778 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
779
780 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
781 {
782 format = SF_TEXT | SF_UNICODE;
783 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
784 } else if(readOut >= 5)
785 {
786 static const char header[] = "{\\rtf";
787 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
788
789 if(!memcmp(header, fileStart, 5))
790 format = SF_RTF;
791 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
792 {
793 CloseHandle(hFile);
794 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
795 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
796 return;
797 }
798 }
799
800 es.dwCookie = (DWORD_PTR)hFile;
801 es.pfnCallback = stream_in;
802
803 clear_formatting();
804 set_fileformat(format);
805 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
806
807 CloseHandle(hFile);
808
809 SetFocus(hEditorWnd);
810
811 set_caption(szOpenFileName);
812
813 lstrcpyW(wszFileName, szOpenFileName);
814 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
815 registry_set_filelist(szOpenFileName, hMainWnd);
816 update_font_list();
817 }
818
819 static void ShowWriteError(DWORD Code)
820 {
821 LPWSTR Message;
822
823 switch(Code)
824 {
825 case ERROR_ACCESS_DENIED:
826 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
827 break;
828
829 default:
830 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
831 }
832 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
833 }
834
835 static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
836 {
837 HANDLE hFile;
838 EDITSTREAM stream;
839 LRESULT ret;
840
841 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
842 FILE_ATTRIBUTE_NORMAL, NULL);
843
844 if(hFile == INVALID_HANDLE_VALUE)
845 {
846 ShowWriteError(GetLastError());
847 return FALSE;
848 }
849
850 if(format == (SF_TEXT | SF_UNICODE))
851 {
852 static const BYTE unicode[] = {0xff,0xfe};
853 DWORD writeOut;
854 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
855
856 if(writeOut != sizeof(unicode))
857 {
858 CloseHandle(hFile);
859 return FALSE;
860 }
861 }
862
863 stream.dwCookie = (DWORD_PTR)hFile;
864 stream.pfnCallback = stream_out;
865
866 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
867
868 CloseHandle(hFile);
869
870 SetFocus(hEditorWnd);
871
872 if(!ret)
873 {
874 GETTEXTLENGTHEX gt;
875 gt.flags = GTL_DEFAULT;
876 gt.codepage = 1200;
877
878 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
879 return FALSE;
880 }
881
882 lstrcpyW(wszFileName, wszSaveFileName);
883 set_caption(wszFileName);
884 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
885 set_fileformat(format);
886
887 return TRUE;
888 }
889
890 static BOOL DialogSaveFile(void)
891 {
892 OPENFILENAMEW sfn;
893
894 WCHAR wszFile[MAX_PATH] = {'\0'};
895 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
896
897 ZeroMemory(&sfn, sizeof(sfn));
898
899 sfn.lStructSize = sizeof(sfn);
900 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
901 sfn.hwndOwner = hMainWnd;
902 sfn.lpstrFilter = wszFilter;
903 sfn.lpstrFile = wszFile;
904 sfn.nMaxFile = MAX_PATH;
905 sfn.lpstrDefExt = wszDefExt;
906 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
907
908 while(GetSaveFileNameW(&sfn))
909 {
910 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
911 {
912 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
913 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
914 continue;
915 }
916 return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
917 }
918 return FALSE;
919 }
920
921 static BOOL prompt_save_changes(void)
922 {
923 if(!wszFileName[0])
924 {
925 GETTEXTLENGTHEX gt;
926 gt.flags = GTL_NUMCHARS;
927 gt.codepage = 1200;
928 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
929 return TRUE;
930 }
931
932 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
933 {
934 return TRUE;
935 } else
936 {
937 LPWSTR displayFileName;
938 WCHAR *text;
939 int ret;
940
941 if(!wszFileName[0])
942 displayFileName = wszDefaultFileName;
943 else
944 displayFileName = file_basename(wszFileName);
945
946 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
947 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
948
949 if(!text)
950 return FALSE;
951
952 wsprintfW(text, wszSaveChanges, displayFileName);
953
954 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
955
956 HeapFree(GetProcessHeap(), 0, text);
957
958 switch(ret)
959 {
960 case IDNO:
961 return TRUE;
962
963 case IDYES:
964 if(wszFileName[0])
965 return DoSaveFile(wszFileName, fileFormat);
966 return DialogSaveFile();
967
968 default:
969 return FALSE;
970 }
971 }
972 }
973
974 static void DialogOpenFile(void)
975 {
976 OPENFILENAMEW ofn;
977
978 WCHAR wszFile[MAX_PATH] = {'\0'};
979 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
980
981 ZeroMemory(&ofn, sizeof(ofn));
982
983 ofn.lStructSize = sizeof(ofn);
984 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
985 ofn.hwndOwner = hMainWnd;
986 ofn.lpstrFilter = wszFilter;
987 ofn.lpstrFile = wszFile;
988 ofn.nMaxFile = MAX_PATH;
989 ofn.lpstrDefExt = wszDefExt;
990 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
991
992 if(GetOpenFileNameW(&ofn))
993 {
994 if(prompt_save_changes())
995 DoOpenFile(ofn.lpstrFile);
996 }
997 }
998
999 static void dialog_about(void)
1000 {
1001 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1002 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1003 }
1004
1005 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1006 {
1007 switch(message)
1008 {
1009 case WM_INITDIALOG:
1010 {
1011 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1012 int wrap = -1;
1013 char id[4];
1014 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1015
1016 sprintf(id, "%d\n", (int)ps->lParam);
1017 SetWindowTextA(hIdWnd, id);
1018 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1019 wrap = IDC_PAGEFMT_WN;
1020 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1021 wrap = IDC_PAGEFMT_WW;
1022 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1023 wrap = IDC_PAGEFMT_WM;
1024
1025 if(wrap != -1)
1026 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1027 IDC_PAGEFMT_WM, wrap);
1028
1029 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1030 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1031 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1032 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1033 if(barState[ps->lParam] & (1 << BANDID_RULER))
1034 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1035 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1036 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1037 }
1038 break;
1039
1040 case WM_COMMAND:
1041 switch(LOWORD(wParam))
1042 {
1043 case IDC_PAGEFMT_WN:
1044 case IDC_PAGEFMT_WW:
1045 case IDC_PAGEFMT_WM:
1046 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1047 LOWORD(wParam));
1048 break;
1049
1050 case IDC_PAGEFMT_TB:
1051 case IDC_PAGEFMT_FB:
1052 case IDC_PAGEFMT_RU:
1053 case IDC_PAGEFMT_SB:
1054 CheckDlgButton(hWnd, LOWORD(wParam),
1055 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1056 break;
1057 }
1058 break;
1059 case WM_NOTIFY:
1060 {
1061 LPNMHDR header = (LPNMHDR)lParam;
1062 if(header->code == PSN_APPLY)
1063 {
1064 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1065 char sid[4];
1066 int id;
1067
1068 GetWindowTextA(hIdWnd, sid, 4);
1069 id = atoi(sid);
1070 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1071 wordWrap[id] = ID_WORDWRAP_NONE;
1072 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1073 wordWrap[id] = ID_WORDWRAP_WINDOW;
1074 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1075 wordWrap[id] = ID_WORDWRAP_MARGIN;
1076
1077 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1078 barState[id] |= (1 << BANDID_TOOLBAR);
1079 else
1080 barState[id] &= ~(1 << BANDID_TOOLBAR);
1081
1082 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1083 barState[id] |= (1 << BANDID_FORMATBAR);
1084 else
1085 barState[id] &= ~(1 << BANDID_FORMATBAR);
1086
1087 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1088 barState[id] |= (1 << BANDID_RULER);
1089 else
1090 barState[id] &= ~(1 << BANDID_RULER);
1091
1092 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1093 barState[id] |= (1 << BANDID_STATUSBAR);
1094 else
1095 barState[id] &= ~(1 << BANDID_STATUSBAR);
1096 }
1097 }
1098 break;
1099 }
1100 return FALSE;
1101 }
1102
1103 static void dialog_viewproperties(void)
1104 {
1105 PROPSHEETPAGEW psp[2];
1106 PROPSHEETHEADERW psh;
1107 size_t i;
1108 HINSTANCE hInstance = GetModuleHandleW(0);
1109 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1110
1111 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1112 psp[0].dwFlags = PSP_USETITLE;
1113 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1114 psp[0].pfnDlgProc = formatopts_proc;
1115 psp[0].hInstance = hInstance;
1116 psp[0].lParam = reg_formatindex(SF_TEXT);
1117 psp[0].pfnCallback = NULL;
1118 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1119 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1120 {
1121 psp[i].dwSize = psp[0].dwSize;
1122 psp[i].dwFlags = psp[0].dwFlags;
1123 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1124 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1125 psp[i].hInstance = psp[0].hInstance;
1126 psp[i].lParam = reg_formatindex(SF_RTF);
1127 psp[i].pfnCallback = psp[0].pfnCallback;
1128 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1129 }
1130
1131 psh.dwSize = sizeof(psh);
1132 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1133 psh.hwndParent = hMainWnd;
1134 psh.hInstance = hInstance;
1135 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1136 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1137 U3(psh).ppsp = ppsp;
1138 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1139
1140 if(fileFormat & SF_RTF)
1141 U2(psh).nStartPage = 1;
1142 else
1143 U2(psh).nStartPage = 0;
1144 PropertySheetW(&psh);
1145 set_bar_states();
1146 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1147 }
1148
1149 static void HandleCommandLine(LPWSTR cmdline)
1150 {
1151 WCHAR delimiter;
1152 int opt_print = 0;
1153
1154 /* skip white space */
1155 while (*cmdline == ' ') cmdline++;
1156
1157 /* skip executable name */
1158 delimiter = (*cmdline == '"' ? '"' : ' ');
1159
1160 if (*cmdline == delimiter) cmdline++;
1161 while (*cmdline && *cmdline != delimiter) cmdline++;
1162 if (*cmdline == delimiter) cmdline++;
1163
1164 while (*cmdline)
1165 {
1166 while (isspace(*cmdline)) cmdline++;
1167
1168 if (*cmdline == '-' || *cmdline == '/')
1169 {
1170 if (!cmdline[2] || isspace(cmdline[2]))
1171 {
1172 switch (cmdline[1])
1173 {
1174 case 'P':
1175 case 'p':
1176 opt_print = 1;
1177 cmdline += 2;
1178 continue;
1179 }
1180 }
1181 /* a filename starting by / */
1182 }
1183 break;
1184 }
1185
1186 if (*cmdline)
1187 {
1188 /* file name is passed on the command line */
1189 if (cmdline[0] == '"')
1190 {
1191 cmdline++;
1192 cmdline[lstrlenW(cmdline) - 1] = 0;
1193 }
1194 DoOpenFile(cmdline);
1195 InvalidateRect(hMainWnd, NULL, FALSE);
1196 }
1197
1198 if (opt_print)
1199 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1200 }
1201
1202 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1203 {
1204 if(pFr->Flags & FR_DIALOGTERM)
1205 {
1206 hFindWnd = 0;
1207 pFr->Flags = FR_FINDNEXT;
1208 return 0;
1209 }
1210
1211 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1212 {
1213 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1214 DWORD flags;
1215 FINDTEXTEXW ft;
1216 CHARRANGE sel;
1217 LRESULT ret = -1;
1218 HMENU hMenu = GetMenu(hMainWnd);
1219 MENUITEMINFOW mi;
1220
1221 mi.cbSize = sizeof(mi);
1222 mi.fMask = MIIM_DATA;
1223 mi.dwItemData = 1;
1224 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1225
1226 /* Make sure find field is saved. */
1227 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1228 {
1229 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1230 sizeof(custom_data->findBuffer));
1231 pFr->lpstrFindWhat = custom_data->findBuffer;
1232 }
1233
1234 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1235 if(custom_data->endPos == -1) {
1236 custom_data->endPos = sel.cpMin;
1237 custom_data->wrapped = FALSE;
1238 }
1239
1240 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1241 ft.lpstrText = pFr->lpstrFindWhat;
1242
1243 /* Only replace the existing selection if it is an exact match. */
1244 if (sel.cpMin != sel.cpMax &&
1245 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1246 {
1247 ft.chrg = sel;
1248 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1249 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1250 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1251 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1252 }
1253 }
1254
1255 /* Search from the start of the selection, but exclude the first character
1256 * from search if there is a selection. */
1257 ft.chrg.cpMin = sel.cpMin;
1258 if (sel.cpMin != sel.cpMax)
1259 ft.chrg.cpMin++;
1260
1261 /* Search to the end, then wrap around and search from the start. */
1262 if (!custom_data->wrapped) {
1263 ft.chrg.cpMax = -1;
1264 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1265 if (ret == -1) {
1266 custom_data->wrapped = TRUE;
1267 ft.chrg.cpMin = 0;
1268 }
1269 }
1270
1271 if (ret == -1) {
1272 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1273 if (ft.chrg.cpMax > ft.chrg.cpMin)
1274 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1275 }
1276
1277 if (ret == -1) {
1278 custom_data->endPos = -1;
1279 EnableWindow(hMainWnd, FALSE);
1280 MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1281 wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
1282 EnableWindow(hMainWnd, TRUE);
1283 } else {
1284 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1285 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1286
1287 if (pFr->Flags & FR_REPLACEALL)
1288 return handle_findmsg(pFr);
1289 }
1290 }
1291
1292 return 0;
1293 }
1294
1295 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1296 {
1297 static WCHAR selBuffer[128];
1298 static WCHAR replaceBuffer[128];
1299 static FINDREPLACE_custom custom_data;
1300 static const WCHAR endl = '\r';
1301 FINDTEXTW ft;
1302
1303 /* Allow only one search/replace dialog to open */
1304 if(hFindWnd != NULL)
1305 {
1306 SetActiveWindow(hFindWnd);
1307 return;
1308 }
1309
1310 ZeroMemory(fr, sizeof(FINDREPLACEW));
1311 fr->lStructSize = sizeof(FINDREPLACEW);
1312 fr->hwndOwner = hMainWnd;
1313 fr->Flags = FR_HIDEUPDOWN;
1314 /* Find field is filled with the selected text if it is non-empty
1315 * and stays within the same paragraph, otherwise the previous
1316 * find field is used. */
1317 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1318 (LPARAM)&ft.chrg.cpMax);
1319 ft.lpstrText = &endl;
1320 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1321 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1322 {
1323 /* Use a temporary buffer for the selected text so that the saved
1324 * find field is only overwritten when a find/replace is clicked. */
1325 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1326 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1327 fr->lpstrFindWhat = selBuffer;
1328 } else {
1329 fr->lpstrFindWhat = custom_data.findBuffer;
1330 }
1331 fr->lpstrReplaceWith = replaceBuffer;
1332 custom_data.endPos = -1;
1333 custom_data.wrapped = FALSE;
1334 fr->lCustData = (LPARAM)&custom_data;
1335 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1336 fr->wReplaceWithLen = sizeof(replaceBuffer);
1337
1338 if(replace)
1339 hFindWnd = ReplaceTextW(fr);
1340 else
1341 hFindWnd = FindTextW(fr);
1342 }
1343
1344 static int units_to_twips(UNIT unit, float number)
1345 {
1346 int twips = 0;
1347
1348 switch(unit)
1349 {
1350 case UNIT_CM:
1351 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1352 break;
1353
1354 case UNIT_INCH:
1355 twips = (int)(number * (float)TWIPS_PER_INCH);
1356 break;
1357
1358 case UNIT_PT:
1359 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1360 break;
1361 }
1362
1363 return twips;
1364 }
1365
1366 static void append_current_units(LPWSTR buffer)
1367 {
1368 static const WCHAR space[] = {' ', 0};
1369 lstrcatW(buffer, space);
1370 lstrcatW(buffer, units_cmW);
1371 }
1372
1373 static void number_with_units(LPWSTR buffer, int number)
1374 {
1375 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1376 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1377
1378 sprintfW(buffer, fmt, converted, units_cmW);
1379 }
1380
1381 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1382 {
1383 COMBOBOXEXITEMW cbItem;
1384 COMBOBOXINFO cbInfo;
1385 HWND hCombo, hList;
1386 int idx, result;
1387
1388 hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1389 if (!hCombo)
1390 return FALSE;
1391 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1392 result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1393 if (!result)
1394 return FALSE;
1395 hList = cbInfo.hwndList;
1396 idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
1397 if (idx < 0)
1398 return FALSE;
1399
1400 ZeroMemory(&cbItem, sizeof(cbItem));
1401 cbItem.mask = CBEIF_TEXT;
1402 cbItem.iItem = idx;
1403 cbItem.pszText = wszBuffer;
1404 cbItem.cchTextMax = bufferLength-1;
1405 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1406
1407 return result != 0;
1408 }
1409
1410 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1411 {
1412 switch(message)
1413 {
1414 case WM_INITDIALOG:
1415 {
1416 WCHAR buffer[MAX_STRING_LEN];
1417 SYSTEMTIME st;
1418 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1419 GetLocalTime(&st);
1420
1421 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1422 MAX_STRING_LEN);
1423 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1424 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1425 MAX_STRING_LEN);
1426 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1427 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1428 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1429
1430 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1431 }
1432 break;
1433
1434 case WM_COMMAND:
1435 switch(LOWORD(wParam))
1436 {
1437 case IDC_DATETIME:
1438 if (HIWORD(wParam) != LBN_DBLCLK)
1439 break;
1440 /* Fall through */
1441
1442 case IDOK:
1443 {
1444 LRESULT index;
1445 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1446
1447 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1448
1449 if(index != LB_ERR)
1450 {
1451 WCHAR buffer[MAX_STRING_LEN];
1452 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1453 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1454 }
1455 }
1456 /* Fall through */
1457
1458 case IDCANCEL:
1459 EndDialog(hWnd, wParam);
1460 return TRUE;
1461 }
1462 }
1463 return FALSE;
1464 }
1465
1466 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1467 {
1468 switch(message)
1469 {
1470 case WM_INITDIALOG:
1471 {
1472 HINSTANCE hInstance = GetModuleHandleW(0);
1473 WCHAR buffer[MAX_STRING_LEN];
1474 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1475
1476 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1477 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1478 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1479 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1480 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1481 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1482
1483 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1484 }
1485 break;
1486
1487 case WM_COMMAND:
1488 switch(LOWORD(wParam))
1489 {
1490 case IDOK:
1491 {
1492 LRESULT index;
1493 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1494 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1495
1496 if(index != LB_ERR)
1497 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1498 }
1499 return TRUE;
1500
1501 case IDCANCEL:
1502 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1503 return TRUE;
1504 }
1505 }
1506 return FALSE;
1507 }
1508
1509 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1510 {
1511 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1512
1513 switch(message)
1514 {
1515 case WM_INITDIALOG:
1516 {
1517 HINSTANCE hInstance = GetModuleHandleW(0);
1518 WCHAR buffer[MAX_STRING_LEN];
1519 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1520 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1521 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1522 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1523 PARAFORMAT2 pf;
1524 int index = 0;
1525
1526 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1527 MAX_STRING_LEN);
1528 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1529 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1530 MAX_STRING_LEN);
1531 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1532 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1533 MAX_STRING_LEN);
1534 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1535
1536 pf.cbSize = sizeof(pf);
1537 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1538 PFM_STARTINDENT;
1539 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1540
1541 if(pf.wAlignment == PFA_RIGHT)
1542 index ++;
1543 else if(pf.wAlignment == PFA_CENTER)
1544 index += 2;
1545
1546 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1547
1548 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1549 SetWindowTextW(hLeftWnd, buffer);
1550 number_with_units(buffer, pf.dxRightIndent);
1551 SetWindowTextW(hRightWnd, buffer);
1552 number_with_units(buffer, -pf.dxOffset);
1553 SetWindowTextW(hFirstWnd, buffer);
1554 }
1555 break;
1556
1557 case WM_COMMAND:
1558 switch(LOWORD(wParam))
1559 {
1560 case IDOK:
1561 {
1562 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1563 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1564 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1565 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1566 WCHAR buffer[MAX_STRING_LEN];
1567 int index;
1568 float num;
1569 int ret = 0;
1570 PARAFORMAT pf;
1571 UNIT unit;
1572
1573 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1574 pf.wAlignment = ALIGNMENT_VALUES[index];
1575
1576 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1577 if(number_from_string(buffer, &num, &unit))
1578 ret++;
1579 pf.dxOffset = units_to_twips(unit, num);
1580 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1581 if(number_from_string(buffer, &num, &unit))
1582 ret++;
1583 pf.dxRightIndent = units_to_twips(unit, num);
1584 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1585 if(number_from_string(buffer, &num, &unit))
1586 ret++;
1587 pf.dxStartIndent = units_to_twips(unit, num);
1588
1589 if(ret != 3)
1590 {
1591 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1592 wszAppTitle, MB_OK | MB_ICONASTERISK);
1593 return FALSE;
1594 } else
1595 {
1596 if (pf.dxOffset + pf.dxStartIndent < 0
1597 && pf.dxStartIndent < 0)
1598 {
1599 /* The first line is before the left edge, so
1600 * make sure it is at the left edge. */
1601 pf.dxOffset = -pf.dxStartIndent;
1602 } else if (pf.dxOffset < 0) {
1603 /* The second and following lines are before
1604 * the left edge, so set it to be at the left
1605 * edge, and adjust the first line since it
1606 * is relative to it. */
1607 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1608 pf.dxOffset = 0;
1609 }
1610 /* Internally the dxStartIndent is the absolute
1611 * offset for the first line and dxOffset is
1612 * to it value as opposed how it is displayed with
1613 * the first line being the relative value.
1614 * These two lines make the adjustments. */
1615 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1616 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1617
1618 pf.cbSize = sizeof(pf);
1619 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1620 PFM_STARTINDENT;
1621 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1622 }
1623 }
1624 /* Fall through */
1625
1626 case IDCANCEL:
1627 EndDialog(hWnd, wParam);
1628 return TRUE;
1629 }
1630 }
1631 return FALSE;
1632 }
1633
1634 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1635 {
1636 switch(message)
1637 {
1638 case WM_INITDIALOG:
1639 {
1640 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1641 PARAFORMAT pf;
1642 WCHAR buffer[MAX_STRING_LEN];
1643 int i;
1644
1645 pf.cbSize = sizeof(pf);
1646 pf.dwMask = PFM_TABSTOPS;
1647 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1648 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1649
1650 for(i = 0; i < pf.cTabCount; i++)
1651 {
1652 number_with_units(buffer, pf.rgxTabs[i]);
1653 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1654 }
1655 SetFocus(hTabWnd);
1656 }
1657 break;
1658
1659 case WM_COMMAND:
1660 switch(LOWORD(wParam))
1661 {
1662 case IDC_TABSTOPS:
1663 {
1664 HWND hTabWnd = (HWND)lParam;
1665 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1666 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1667 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1668
1669 if(GetWindowTextLengthW(hTabWnd))
1670 EnableWindow(hAddWnd, TRUE);
1671 else
1672 EnableWindow(hAddWnd, FALSE);
1673
1674 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1675 {
1676 EnableWindow(hEmptyWnd, TRUE);
1677
1678 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1679 EnableWindow(hDelWnd, FALSE);
1680 else
1681 EnableWindow(hDelWnd, TRUE);
1682 } else
1683 {
1684 EnableWindow(hEmptyWnd, FALSE);
1685 }
1686 }
1687 break;
1688
1689 case ID_TAB_ADD:
1690 {
1691 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1692 WCHAR buffer[MAX_STRING_LEN];
1693 UNIT unit;
1694
1695 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1696 append_current_units(buffer);
1697
1698 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1699 {
1700 float number = 0;
1701 int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
1702
1703 if(!number_from_string(buffer, &number, &unit))
1704 {
1705 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1706 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1707 } else if (item_count >= MAX_TAB_STOPS) {
1708 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1709 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1710 } else {
1711 int i;
1712 float next_number = -1;
1713 int next_number_in_twips = -1;
1714 int insert_number = units_to_twips(unit, number);
1715
1716 /* linear search for position to insert the string */
1717 for(i = 0; i < item_count; i++)
1718 {
1719 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1720 number_from_string(buffer, &next_number, &unit);
1721 next_number_in_twips = units_to_twips(unit, next_number);
1722 if (insert_number <= next_number_in_twips)
1723 break;
1724 }
1725 if (insert_number != next_number_in_twips)
1726 {
1727 number_with_units(buffer, insert_number);
1728 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1729 SetWindowTextW(hTabWnd, 0);
1730 }
1731 }
1732 }
1733 SetFocus(hTabWnd);
1734 }
1735 break;
1736
1737 case ID_TAB_DEL:
1738 {
1739 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1740 LRESULT ret;
1741 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1742 if(ret != CB_ERR)
1743 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1744 }
1745 break;
1746
1747 case ID_TAB_EMPTY:
1748 {
1749 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1750 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1751 SetFocus(hTabWnd);
1752 }
1753 break;
1754
1755 case IDOK:
1756 {
1757 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1758 int i;
1759 WCHAR buffer[MAX_STRING_LEN];
1760 PARAFORMAT pf;
1761 float number;
1762 UNIT unit;
1763
1764 pf.cbSize = sizeof(pf);
1765 pf.dwMask = PFM_TABSTOPS;
1766
1767 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1768 (LPARAM)&buffer) != CB_ERR &&
1769 i < MAX_TAB_STOPS; i++)
1770 {
1771 number_from_string(buffer, &number, &unit);
1772 pf.rgxTabs[i] = units_to_twips(unit, number);
1773 }
1774 pf.cTabCount = i;
1775 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1776 }
1777 /* Fall through */
1778 case IDCANCEL:
1779 EndDialog(hWnd, wParam);
1780 return TRUE;
1781 }
1782 }
1783 return FALSE;
1784 }
1785
1786 static LRESULT OnCreate( HWND hWnd )
1787 {
1788 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1789 HINSTANCE hInstance = GetModuleHandleW(0);
1790 HANDLE hDLL;
1791 TBADDBITMAP ab;
1792 int nStdBitmaps = 0;
1793 REBARINFO rbi;
1794 REBARBANDINFOW rbb;
1795 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1796 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1797
1798 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1799
1800 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1801 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1802 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1803
1804 rbi.cbSize = sizeof(rbi);
1805 rbi.fMask = 0;
1806 rbi.himl = NULL;
1807 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1808 return -1;
1809
1810 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|BTNS_BUTTON,
1811 IDC_TOOLBAR,
1812 1, hInstance, IDB_TOOLBAR,
1813 NULL, 0,
1814 24, 24, 16, 16, sizeof(TBBUTTON));
1815
1816 ab.hInst = HINST_COMMCTRL;
1817 ab.nID = IDB_STD_SMALL_COLOR;
1818 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1819
1820 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1821 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1822 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1823 AddSeparator(hToolBarWnd);
1824 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1825 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1826 AddSeparator(hToolBarWnd);
1827 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1828 AddSeparator(hToolBarWnd);
1829 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1830 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1831 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1832 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1833 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1834 AddSeparator(hToolBarWnd);
1835 AddButton(hToolBarWnd, 0, ID_DATETIME);
1836
1837 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1838
1839 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1840 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1841 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1842 rbb.cx = 0;
1843 rbb.hwndChild = hToolBarWnd;
1844 rbb.cxMinChild = 0;
1845 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1846 rbb.wID = BANDID_TOOLBAR;
1847
1848 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1849
1850 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1851 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1852 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1853
1854 rbb.hwndChild = hFontListWnd;
1855 rbb.cx = 200;
1856 rbb.wID = BANDID_FONTLIST;
1857
1858 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1859
1860 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1861 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1862 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1863
1864 rbb.hwndChild = hSizeListWnd;
1865 rbb.cx = 50;
1866 rbb.fStyle ^= RBBS_BREAK;
1867 rbb.wID = BANDID_SIZELIST;
1868
1869 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1870
1871 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1872 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | BTNS_BUTTON,
1873 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1874
1875 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1876 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1877 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1878 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1879 AddSeparator(hFormatBarWnd);
1880 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1881 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1882 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1883 AddSeparator(hFormatBarWnd);
1884 AddButton(hFormatBarWnd, 7, ID_BULLET);
1885
1886 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1887
1888 rbb.hwndChild = hFormatBarWnd;
1889 rbb.wID = BANDID_FORMATBAR;
1890
1891 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1892
1893 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1894 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1895
1896
1897 rbb.hwndChild = hRulerWnd;
1898 rbb.wID = BANDID_RULER;
1899 rbb.fStyle |= RBBS_BREAK;
1900
1901 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1902
1903 hDLL = LoadLibraryW(wszRichEditDll);
1904 if(!hDLL)
1905 {
1906 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1907 MB_OK | MB_ICONEXCLAMATION);
1908 PostQuitMessage(1);
1909 }
1910
1911 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1912 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1913 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1914 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1915
1916 if (!hEditorWnd)
1917 {
1918 fprintf(stderr, "Error code %u\n", GetLastError());
1919 return -1;
1920 }
1921 assert(hEditorWnd);
1922
1923 setup_richedit_olecallback(hEditorWnd);
1924 SetFocus(hEditorWnd);
1925 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1926
1927 set_default_font();
1928
1929 populate_font_list(hFontListWnd);
1930 populate_size_list(hSizeListWnd);
1931 DoLoadStrings();
1932 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1933
1934 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1935
1936 registry_read_filelist(hWnd);
1937 registry_read_formatopts_all(barState, wordWrap);
1938 registry_read_options();
1939 DragAcceptFiles(hWnd, TRUE);
1940
1941 return 0;
1942 }
1943
1944 static LRESULT OnUser( HWND hWnd )
1945 {
1946 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1947 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1948 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1949 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1950 int from, to;
1951 CHARFORMAT2W fmt;
1952 PARAFORMAT2 pf;
1953 GETTEXTLENGTHEX gt;
1954
1955 ZeroMemory(&fmt, sizeof(fmt));
1956 fmt.cbSize = sizeof(fmt);
1957
1958 ZeroMemory(&pf, sizeof(pf));
1959 pf.cbSize = sizeof(pf);
1960
1961 gt.flags = GTL_NUMCHARS;
1962 gt.codepage = 1200;
1963
1964 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1965 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1966
1967 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1968
1969 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1970 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1971 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1972 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1973 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1974 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1975 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
1976
1977 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
1978 (fmt.dwEffects & CFE_BOLD));
1979 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
1980 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
1981 (fmt.dwEffects & CFE_ITALIC));
1982 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
1983 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
1984 (fmt.dwEffects & CFE_UNDERLINE));
1985 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
1986
1987 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1988 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
1989 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
1990 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
1991
1992 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
1993 return 0;
1994 }
1995
1996 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
1997 {
1998 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1999 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2000 NMHDR *pHdr = (NMHDR *)lParam;
2001 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2002 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2003
2004 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2005 {
2006 if (pHdr->code == CBEN_ENDEDITW)
2007 {
2008 NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
2009 if(pHdr->hwndFrom == hwndFontList)
2010 {
2011 on_fontlist_modified(endEdit->szText);
2012 } else if (pHdr->hwndFrom == hwndSizeList)
2013 {
2014 on_sizelist_modified(hwndFontList,endEdit->szText);
2015 }
2016 }
2017 return 0;
2018 }
2019
2020 if (pHdr->hwndFrom != hwndEditor)
2021 return 0;
2022
2023 if (pHdr->code == EN_SELCHANGE)
2024 {
2025 SELCHANGE *pSC = (SELCHANGE *)lParam;
2026 char buf[128];
2027
2028 update_font_list();
2029
2030 sprintf( buf,"selection = %d..%d, line count=%ld",
2031 pSC->chrg.cpMin, pSC->chrg.cpMax,
2032 SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
2033 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2034 SendMessageW(hWnd, WM_USER, 0, 0);
2035 return 1;
2036 }
2037 return 0;
2038 }
2039
2040 /* Copied from dlls/comdlg32/fontdlg.c */
2041 static const COLORREF textcolors[]=
2042 {
2043 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2044 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2045 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2046 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2047 };
2048
2049 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2050 {
2051 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2052 static FINDREPLACEW findreplace;
2053
2054 if ((HWND)lParam == hwndEditor)
2055 return 0;
2056
2057 switch(LOWORD(wParam))
2058 {
2059
2060 case ID_FILE_EXIT:
2061 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2062 break;
2063
2064 case ID_FILE_NEW:
2065 {
2066 HINSTANCE hInstance = GetModuleHandleW(0);
2067 int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
2068
2069 if(ret != ID_NEWFILE_ABORT)
2070 {
2071 if(prompt_save_changes())
2072 {
2073 SETTEXTEX st;
2074
2075 set_caption(NULL);
2076 wszFileName[0] = '\0';
2077
2078 clear_formatting();
2079
2080 st.flags = ST_DEFAULT;
2081 st.codepage = 1200;
2082 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2083
2084 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2085 set_fileformat(ret);
2086 update_font_list();
2087 }
2088 }
2089 }
2090 break;
2091
2092 case ID_FILE_OPEN:
2093 DialogOpenFile();
2094 break;
2095
2096 case ID_FILE_SAVE:
2097 if(wszFileName[0])
2098 {
2099 DoSaveFile(wszFileName, fileFormat);
2100 break;
2101 }
2102 /* Fall through */
2103
2104 case ID_FILE_SAVEAS:
2105 DialogSaveFile();
2106 break;
2107
2108 case ID_FILE_RECENT1:
2109 case ID_FILE_RECENT2:
2110 case ID_FILE_RECENT3:
2111 case ID_FILE_RECENT4:
2112 {
2113 HMENU hMenu = GetMenu(hWnd);
2114 MENUITEMINFOW mi;
2115
2116 mi.cbSize = sizeof(MENUITEMINFOW);
2117 mi.fMask = MIIM_DATA;
2118 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2119 DoOpenFile((LPWSTR)mi.dwItemData);
2120 }
2121 break;
2122
2123 case ID_FIND:
2124 dialog_find(&findreplace, FALSE);
2125 break;
2126
2127 case ID_FIND_NEXT:
2128 handle_findmsg(&findreplace);
2129 break;
2130
2131 case ID_REPLACE:
2132 dialog_find(&findreplace, TRUE);
2133 break;
2134
2135 case ID_FONTSETTINGS:
2136 dialog_choose_font();
2137 break;
2138
2139 case ID_PRINT:
2140 dialog_print(hWnd, wszFileName);
2141 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2142 break;
2143
2144 case ID_PRINT_QUICK:
2145 print_quick(hMainWnd, wszFileName);
2146 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2147 break;
2148
2149 case ID_PREVIEW:
2150 {
2151 int index = reg_formatindex(fileFormat);
2152 DWORD tmp = barState[index];
2153 barState[index] = 1 << BANDID_STATUSBAR;
2154 set_bar_states();
2155 barState[index] = tmp;
2156 ShowWindow(hEditorWnd, FALSE);
2157
2158 init_preview(hWnd, wszFileName);
2159
2160 SetMenu(hWnd, NULL);
2161 InvalidateRect(0, 0, TRUE);
2162 }
2163 break;
2164
2165 case ID_PRINTSETUP:
2166 dialog_printsetup(hWnd);
2167 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2168 break;
2169
2170 case ID_FORMAT_BOLD:
2171 case ID_FORMAT_ITALIC:
2172 case ID_FORMAT_UNDERLINE:
2173 {
2174 CHARFORMAT2W fmt;
2175 int effects = CFE_BOLD;
2176
2177 ZeroMemory(&fmt, sizeof(fmt));
2178 fmt.cbSize = sizeof(fmt);
2179 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2180
2181 fmt.dwMask = CFM_BOLD;
2182
2183 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2184 {
2185 effects = CFE_ITALIC;
2186 fmt.dwMask = CFM_ITALIC;
2187 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2188 {
2189 effects = CFE_UNDERLINE;
2190 fmt.dwMask = CFM_UNDERLINE;
2191 }
2192
2193 fmt.dwEffects ^= effects;
2194
2195 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2196 break;
2197 }
2198
2199 case ID_FORMAT_COLOR:
2200 {
2201 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2202 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2203 HMENU hPop;
2204 RECT itemrc;
2205 POINT pt;
2206 int mid;
2207 int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2208
2209 SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2210 pt.x = itemrc.left;
2211 pt.y = itemrc.bottom;
2212 ClientToScreen(hFormatBarWnd, &pt);
2213 hPop = GetSubMenu(hColorPopupMenu, 0);
2214 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2215 TPM_RETURNCMD | TPM_NONOTIFY,
2216 pt.x, pt.y, 0, hWnd, 0);
2217 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2218 {
2219 CHARFORMAT2W fmt;
2220
2221 ZeroMemory(&fmt, sizeof(fmt));
2222 fmt.cbSize = sizeof(fmt);
2223 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2224
2225 fmt.dwMask = CFM_COLOR;
2226
2227 if (mid < ID_COLOR_AUTOMATIC) {
2228 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2229 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2230 } else {
2231 fmt.dwEffects |= CFE_AUTOCOLOR;
2232 }
2233
2234 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2235 }
2236 break;
2237 }
2238
2239 case ID_EDIT_CUT:
2240 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2241 break;
2242
2243 case ID_EDIT_COPY:
2244 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2245 break;
2246
2247 case ID_EDIT_PASTE:
2248 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2249 break;
2250
2251 case ID_EDIT_CLEAR:
2252 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2253 break;
2254
2255 case ID_EDIT_SELECTALL:
2256 {
2257 CHARRANGE range = {0, -1};
2258 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2259 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2260 return 0;
2261 }
2262
2263 case ID_EDIT_GETTEXT:
2264 {
2265 int nLen = GetWindowTextLengthW(hwndEditor);
2266 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2267 TEXTRANGEW tr;
2268
2269 GetWindowTextW(hwndEditor, data, nLen+1);
2270 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2271
2272 HeapFree( GetProcessHeap(), 0, data);
2273 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2274 tr.chrg.cpMin = 0;
2275 tr.chrg.cpMax = nLen;
2276 tr.lpstrText = data;
2277 SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2278 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2279 HeapFree( GetProcessHeap(), 0, data );
2280
2281 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2282 return 0;
2283 }
2284
2285 case ID_EDIT_CHARFORMAT:
2286 case ID_EDIT_DEFCHARFORMAT:
2287 {
2288 CHARFORMAT2W cf;
2289
2290 ZeroMemory(&cf, sizeof(cf));
2291 cf.cbSize = sizeof(cf);
2292 cf.dwMask = 0;
2293 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2294 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2295 return 0;
2296 }
2297
2298 case ID_EDIT_PARAFORMAT:
2299 {
2300 PARAFORMAT2 pf;
2301 ZeroMemory(&pf, sizeof(pf));
2302 pf.cbSize = sizeof(pf);
2303 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2304 return 0;
2305 }
2306
2307 case ID_EDIT_SELECTIONINFO:
2308 {
2309 CHARRANGE range = {0, -1};
2310 char buf[128];
2311 WCHAR *data = NULL;
2312
2313 SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2314 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2315 SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2316 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2317 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2318 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2319 HeapFree( GetProcessHeap(), 0, data);
2320 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2321 return 0;
2322 }
2323
2324 case ID_EDIT_READONLY:
2325 {
2326 LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
2327 if (nStyle & ES_READONLY)
2328 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2329 else
2330 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2331 return 0;
2332 }
2333
2334 case ID_EDIT_MODIFIED:
2335 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2336 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2337 else
2338 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2339 return 0;
2340
2341 case ID_EDIT_UNDO:
2342 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2343 return 0;
2344
2345 case ID_EDIT_REDO:
2346 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2347 return 0;
2348
2349 case ID_BULLET:
2350 {
2351 PARAFORMAT pf;
2352
2353 pf.cbSize = sizeof(pf);
2354 pf.dwMask = PFM_NUMBERING;
2355 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2356
2357 pf.dwMask |= PFM_OFFSET;
2358
2359 if(pf.wNumbering == PFN_BULLET)
2360 {
2361 pf.wNumbering = 0;
2362 pf.dxOffset = 0;
2363 } else
2364 {
2365 pf.wNumbering = PFN_BULLET;
2366 pf.dxOffset = 720;
2367 }
2368
2369 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2370 }
2371 break;
2372
2373 case ID_ALIGN_LEFT:
2374 case ID_ALIGN_CENTER:
2375 case ID_ALIGN_RIGHT:
2376 {
2377 PARAFORMAT2 pf;
2378
2379 pf.cbSize = sizeof(pf);
2380 pf.dwMask = PFM_ALIGNMENT;
2381 switch(LOWORD(wParam)) {
2382 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2383 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2384 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2385 }
2386 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2387 break;
2388 }
2389
2390 case ID_BACK_1:
2391 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2392 break;
2393
2394 case ID_BACK_2:
2395 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2396 break;
2397
2398 case ID_TOGGLE_TOOLBAR:
2399 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2400 update_window();
2401 break;
2402
2403 case ID_TOGGLE_FORMATBAR:
2404 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2405 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2406 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2407 update_window();
2408 break;
2409
2410 case ID_TOGGLE_STATUSBAR:
2411 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2412 update_window();
2413 break;
2414
2415 case ID_TOGGLE_RULER:
2416 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2417 update_window();
2418 break;
2419
2420 case ID_DATETIME:
2421 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2422 break;
2423
2424 case ID_PARAFORMAT:
2425 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2426 break;
2427
2428 case ID_TABSTOPS:
2429 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2430 break;
2431
2432 case ID_ABOUT:
2433 dialog_about();
2434 break;
2435
2436 case ID_VIEWPROPERTIES:
2437 dialog_viewproperties();
2438 break;
2439
2440 case IDC_FONTLIST:
2441 if (HIWORD(wParam) == CBN_SELENDOK)
2442 {
2443 WCHAR buffer[LF_FACESIZE];
2444 HWND hwndFontList = (HWND)lParam;
2445 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2446 on_fontlist_modified(buffer);
2447 }
2448 break;
2449
2450 case IDC_SIZELIST:
2451 if (HIWORD(wParam) == CBN_SELENDOK)
2452 {
2453 WCHAR buffer[MAX_STRING_LEN+1];
2454 HWND hwndSizeList = (HWND)lParam;
2455 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2456 on_sizelist_modified(hwndSizeList, buffer);
2457 }
2458 break;
2459
2460 default:
2461 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2462 break;
2463 }
2464 return 0;
2465 }
2466
2467 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2468 {
2469 HMENU hMenu = (HMENU)wParam;
2470 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2471 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2472 PARAFORMAT pf;
2473 int nAlignment = -1;
2474 int selFrom, selTo;
2475 GETTEXTLENGTHEX gt;
2476 LRESULT textLength;
2477 MENUITEMINFOW mi;
2478
2479 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2480 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2481 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2482
2483 pf.cbSize = sizeof(PARAFORMAT);
2484 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2485 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2486 MF_BYCOMMAND|(GetWindowLongW(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
2487 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2488 MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
2489 if (pf.dwMask & PFM_ALIGNMENT)
2490 nAlignment = pf.wAlignment;
2491 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
2492 MF_CHECKED : MF_UNCHECKED);
2493 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
2494 MF_CHECKED : MF_UNCHECKED);
2495 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
2496 MF_CHECKED : MF_UNCHECKED);
2497 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
2498 MF_CHECKED : MF_UNCHECKED));
2499 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
2500 MF_ENABLED : MF_GRAYED);
2501 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
2502 MF_ENABLED : MF_GRAYED);
2503
2504 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
2505 MF_CHECKED : MF_UNCHECKED);
2506
2507 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
2508 MF_CHECKED : MF_UNCHECKED);
2509
2510 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
2511 MF_CHECKED : MF_UNCHECKED);
2512
2513 CheckMenuItem(hMenu, ID_TOGGLE_RULER, MF_BYCOMMAND|(is_bar_visible(BANDID_RULER)) ? MF_CHECKED : MF_UNCHECKED);
2514
2515 gt.flags = GTL_NUMCHARS;
2516 gt.codepage = 1200;
2517 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2518 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2519
2520 mi.cbSize = sizeof(mi);
2521 mi.fMask = MIIM_DATA;
2522
2523 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2524
2525 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
2526 MF_ENABLED : MF_GRAYED));
2527
2528 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2529
2530 return 0;
2531 }
2532
2533 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2534 {
2535 int nStatusSize = 0;
2536 RECT rc;
2537 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2538 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2539 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2540 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2541 int rebarHeight = 0;
2542
2543 if (hwndStatusBar)
2544 {
2545 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2546 if (IsWindowVisible(hwndStatusBar))
2547 {
2548 GetClientRect(hwndStatusBar, &rc);
2549 nStatusSize = rc.bottom - rc.top;
2550 } else
2551 {
2552 nStatusSize = 0;
2553 }
2554 }
2555 if (hwndReBar)
2556 {
2557 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2558
2559 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2560 }
2561 if (hwndEditor)
2562 {
2563 GetClientRect(hWnd, &rc);
2564 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2565 }
2566
2567 redraw_ruler(hRulerWnd);
2568
2569 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2570 }
2571
2572 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2573 {
2574 if(msg == ID_FINDMSGSTRING)
2575 return handle_findmsg((LPFINDREPLACEW)lParam);
2576
2577 switch(msg)
2578 {
2579 case WM_CREATE:
2580 return OnCreate( hWnd );
2581
2582 case WM_USER:
2583 return OnUser( hWnd );
2584
2585 case WM_NOTIFY:
2586 return OnNotify( hWnd, lParam );
2587
2588 case WM_COMMAND:
2589 if(preview_isactive())
2590 {
2591 return preview_command( hWnd, wParam );
2592 }
2593
2594 return OnCommand( hWnd, wParam, lParam );
2595
2596 case WM_DESTROY:
2597 PostQuitMessage(0);
2598 break;
2599
2600 case WM_CLOSE:
2601 if(preview_isactive())
2602 {
2603 preview_exit(hWnd);
2604 } else if(prompt_save_changes())
2605 {
2606 registry_set_options(hMainWnd);
2607 registry_set_formatopts_all(barState, wordWrap);
2608 PostQuitMessage(0);
2609 }
2610 break;
2611
2612 case WM_ACTIVATE:
2613 if (LOWORD(wParam))
2614 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2615 return 0;
2616
2617 case WM_INITMENUPOPUP:
2618 return OnInitPopupMenu( hWnd, wParam );
2619
2620 case WM_SIZE:
2621 return OnSize( hWnd, wParam, lParam );
2622
2623 case WM_CONTEXTMENU:
2624 return DefWindowProcW(hWnd, msg, wParam, lParam);
2625
2626 case WM_DROPFILES:
2627 {
2628 WCHAR file[MAX_PATH];
2629 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2630 DragFinish((HDROP)wParam);
2631
2632 if(prompt_save_changes())
2633 DoOpenFile(file);
2634 }
2635 break;
2636 case WM_PAINT:
2637 if(!preview_isactive())
2638 return DefWindowProcW(hWnd, msg, wParam, lParam);
2639
2640 default:
2641 return DefWindowProcW(hWnd, msg, wParam, lParam);
2642 }
2643
2644 return 0;
2645 }
2646
2647 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2648 {
2649 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2650 HACCEL hAccel;
2651 WNDCLASSEXW wc;
2652 MSG msg;
2653 RECT rc;
2654 UINT_PTR hPrevRulerProc;
2655 HWND hRulerWnd;
2656 POINTL EditPoint;
2657 DWORD bMaximized;
2658 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2659 'T','A','B','L','E','\0'};
2660
2661 InitCommonControlsEx(&classes);
2662
2663 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2664
2665 wc.cbSize = sizeof(wc);
2666 wc.style = 0;
2667 wc.lpfnWndProc = WndProc;
2668 wc.cbClsExtra = 0;
2669 wc.cbWndExtra = 4;
2670 wc.hInstance = hInstance;
2671 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2672 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2673 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2674 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2675 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2676 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2677 wc.lpszClassName = wszMainWndClass;
2678 RegisterClassExW(&wc);
2679
2680 wc.style = 0;
2681 wc.lpfnWndProc = preview_proc;
2682 wc.cbClsExtra = 0;
2683 wc.cbWndExtra = 0;
2684 wc.hInstance = hInstance;
2685 wc.hIcon = NULL;
2686 wc.hIconSm = NULL;
2687 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2688 wc.hbrBackground = NULL;
2689 wc.lpszMenuName = NULL;
2690 wc.lpszClassName = wszPreviewWndClass;
2691 RegisterClassExW(&wc);
2692
2693 registry_read_winrect(&rc);
2694 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2695 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2696 registry_read_maximized(&bMaximized);
2697 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2698 && bMaximized)
2699 nCmdShow = SW_SHOWMAXIMIZED;
2700 ShowWindow(hMainWnd, nCmdShow);
2701
2702 set_caption(NULL);
2703 set_bar_states();
2704 set_fileformat(SF_RTF);
2705 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2706 get_default_printer_opts();
2707 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2708
2709 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2710 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2711 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2712 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2713
2714 HandleCommandLine(GetCommandLineW());
2715
2716 while(GetMessageW(&msg,0,0,0))
2717 {
2718 if (IsDialogMessageW(hFindWnd, &msg))
2719 continue;
2720
2721 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2722 continue;
2723 TranslateMessage(&msg);
2724 DispatchMessageW(&msg);
2725 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2726 SendMessageW(hMainWnd, WM_USER, 0, 0);
2727 }
2728
2729 return 0;
2730 }