* Slap *some* sense into our header inclusions.
[reactos.git] / reactos / 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','i','n','e',' ','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) ? TRUE : FALSE;
584 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
585 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE;
586 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE;
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 == TRUE)
602 fmt.dwEffects |= CFE_UNDERLINE;
603 if(cf.lpLogFont->lfStrikeOut == TRUE)
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_GETBANDINFO, index, (LPARAM)&rbbinfo);
687
688 if(!show)
689 rbbinfo.fStyle &= ~RBBS_BREAK;
690 else
691 rbbinfo.fStyle |= RBBS_BREAK;
692
693 SendMessageW(hwndReBar, RB_SETBANDINFO, 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 void 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;
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;
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;
880 }
881
882 lstrcpyW(wszFileName, wszSaveFileName);
883 set_caption(wszFileName);
884 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
885 set_fileformat(format);
886 }
887
888 static void DialogSaveFile(void)
889 {
890 OPENFILENAMEW sfn;
891
892 WCHAR wszFile[MAX_PATH] = {'\0'};
893 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
894
895 ZeroMemory(&sfn, sizeof(sfn));
896
897 sfn.lStructSize = sizeof(sfn);
898 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
899 sfn.hwndOwner = hMainWnd;
900 sfn.lpstrFilter = wszFilter;
901 sfn.lpstrFile = wszFile;
902 sfn.nMaxFile = MAX_PATH;
903 sfn.lpstrDefExt = wszDefExt;
904 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
905
906 while(GetSaveFileNameW(&sfn))
907 {
908 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
909 {
910 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
911 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
912 {
913 continue;
914 } else
915 {
916 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
917 break;
918 }
919 } else
920 {
921 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
922 break;
923 }
924 }
925 }
926
927 static BOOL prompt_save_changes(void)
928 {
929 if(!wszFileName[0])
930 {
931 GETTEXTLENGTHEX gt;
932 gt.flags = GTL_NUMCHARS;
933 gt.codepage = 1200;
934 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
935 return TRUE;
936 }
937
938 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
939 {
940 return TRUE;
941 } else
942 {
943 LPWSTR displayFileName;
944 WCHAR *text;
945 int ret;
946
947 if(!wszFileName[0])
948 displayFileName = wszDefaultFileName;
949 else
950 displayFileName = file_basename(wszFileName);
951
952 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
953 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
954
955 if(!text)
956 return FALSE;
957
958 wsprintfW(text, wszSaveChanges, displayFileName);
959
960 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
961
962 HeapFree(GetProcessHeap(), 0, text);
963
964 switch(ret)
965 {
966 case IDNO:
967 return TRUE;
968
969 case IDYES:
970 if(wszFileName[0])
971 DoSaveFile(wszFileName, fileFormat);
972 else
973 DialogSaveFile();
974 return TRUE;
975
976 default:
977 return FALSE;
978 }
979 }
980 }
981
982 static void DialogOpenFile(void)
983 {
984 OPENFILENAMEW ofn;
985
986 WCHAR wszFile[MAX_PATH] = {'\0'};
987 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
988
989 ZeroMemory(&ofn, sizeof(ofn));
990
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;
999
1000 if(GetOpenFileNameW(&ofn))
1001 {
1002 if(prompt_save_changes())
1003 DoOpenFile(ofn.lpstrFile);
1004 }
1005 }
1006
1007 static void dialog_about(void)
1008 {
1009 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1010 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1011 }
1012
1013 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1014 {
1015 switch(message)
1016 {
1017 case WM_INITDIALOG:
1018 {
1019 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1020 int wrap = -1;
1021 char id[4];
1022 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1023
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;
1032
1033 if(wrap != -1)
1034 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1035 IDC_PAGEFMT_WM, wrap);
1036
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);
1045 }
1046 break;
1047
1048 case WM_COMMAND:
1049 switch(LOWORD(wParam))
1050 {
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;
1057
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;
1065 }
1066 break;
1067 case WM_NOTIFY:
1068 {
1069 LPNMHDR header = (LPNMHDR)lParam;
1070 if(header->code == PSN_APPLY)
1071 {
1072 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1073 char sid[4];
1074 int id;
1075
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;
1084
1085 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1086 barState[id] |= (1 << BANDID_TOOLBAR);
1087 else
1088 barState[id] &= ~(1 << BANDID_TOOLBAR);
1089
1090 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1091 barState[id] |= (1 << BANDID_FORMATBAR);
1092 else
1093 barState[id] &= ~(1 << BANDID_FORMATBAR);
1094
1095 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1096 barState[id] |= (1 << BANDID_RULER);
1097 else
1098 barState[id] &= ~(1 << BANDID_RULER);
1099
1100 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1101 barState[id] |= (1 << BANDID_STATUSBAR);
1102 else
1103 barState[id] &= ~(1 << BANDID_STATUSBAR);
1104 }
1105 }
1106 break;
1107 }
1108 return FALSE;
1109 }
1110
1111 static void dialog_viewproperties(void)
1112 {
1113 PROPSHEETPAGEW psp[2];
1114 PROPSHEETHEADERW psh;
1115 size_t i;
1116 HINSTANCE hInstance = GetModuleHandleW(0);
1117 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1118
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++)
1128 {
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);
1137 }
1138
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);
1147
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)]);
1155 }
1156
1157 static void HandleCommandLine(LPWSTR cmdline)
1158 {
1159 WCHAR delimiter;
1160 int opt_print = 0;
1161
1162 /* skip white space */
1163 while (*cmdline == ' ') cmdline++;
1164
1165 /* skip executable name */
1166 delimiter = (*cmdline == '"' ? '"' : ' ');
1167
1168 if (*cmdline == delimiter) cmdline++;
1169 while (*cmdline && *cmdline != delimiter) cmdline++;
1170 if (*cmdline == delimiter) cmdline++;
1171
1172 while (*cmdline)
1173 {
1174 while (isspace(*cmdline)) cmdline++;
1175
1176 if (*cmdline == '-' || *cmdline == '/')
1177 {
1178 if (!cmdline[2] || isspace(cmdline[2]))
1179 {
1180 switch (cmdline[1])
1181 {
1182 case 'P':
1183 case 'p':
1184 opt_print = 1;
1185 cmdline += 2;
1186 continue;
1187 }
1188 }
1189 /* a filename starting by / */
1190 }
1191 break;
1192 }
1193
1194 if (*cmdline)
1195 {
1196 /* file name is passed on the command line */
1197 if (cmdline[0] == '"')
1198 {
1199 cmdline++;
1200 cmdline[lstrlenW(cmdline) - 1] = 0;
1201 }
1202 DoOpenFile(cmdline);
1203 InvalidateRect(hMainWnd, NULL, FALSE);
1204 }
1205
1206 if (opt_print)
1207 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1208 }
1209
1210 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1211 {
1212 if(pFr->Flags & FR_DIALOGTERM)
1213 {
1214 hFindWnd = 0;
1215 pFr->Flags = FR_FINDNEXT;
1216 return 0;
1217 }
1218
1219 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1220 {
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;
1228
1229 mi.cbSize = sizeof(mi);
1230 mi.fMask = MIIM_DATA;
1231 mi.dwItemData = 1;
1232 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1233
1234 /* Make sure find field is saved. */
1235 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1236 {
1237 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1238 sizeof(custom_data->findBuffer));
1239 pFr->lpstrFindWhat = custom_data->findBuffer;
1240 }
1241
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;
1246 }
1247
1248 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1249 ft.lpstrText = pFr->lpstrFindWhat;
1250
1251 /* Only replace existing selectino if it is an exact match. */
1252 if (sel.cpMin != sel.cpMax &&
1253 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1254 {
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);
1260 }
1261 }
1262
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++;
1268
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;
1276 }
1277 }
1278
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);
1283 }
1284
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);
1294
1295 if (pFr->Flags & FR_REPLACEALL)
1296 return handle_findmsg(pFr);
1297 }
1298 }
1299
1300 return 0;
1301 }
1302
1303 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1304 {
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;
1310
1311 /* Allow only one search/replace dialog to open */
1312 if(hFindWnd != NULL)
1313 {
1314 SetActiveWindow(hFindWnd);
1315 return;
1316 }
1317
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)
1330 {
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;
1338 }
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);
1345
1346 if(replace)
1347 hFindWnd = ReplaceTextW(fr);
1348 else
1349 hFindWnd = FindTextW(fr);
1350 }
1351
1352 static int units_to_twips(UNIT unit, float number)
1353 {
1354 int twips = 0;
1355
1356 switch(unit)
1357 {
1358 case UNIT_CM:
1359 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1360 break;
1361
1362 case UNIT_INCH:
1363 twips = (int)(number * (float)TWIPS_PER_INCH);
1364 break;
1365
1366 case UNIT_PT:
1367 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1368 break;
1369 }
1370
1371 return twips;
1372 }
1373
1374 static void append_current_units(LPWSTR buffer)
1375 {
1376 static const WCHAR space[] = {' ', 0};
1377 lstrcatW(buffer, space);
1378 lstrcatW(buffer, units_cmW);
1379 }
1380
1381 static void number_with_units(LPWSTR buffer, int number)
1382 {
1383 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1384 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1385
1386 sprintfW(buffer, fmt, converted, units_cmW);
1387 }
1388
1389 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1390 {
1391 COMBOBOXEXITEMW cbItem;
1392 COMBOBOXINFO cbInfo;
1393 HWND hCombo, hList;
1394 int idx, result;
1395
1396 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1397 if (!hCombo)
1398 return FALSE;
1399 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1400 result = SendMessage(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1401 if (!result)
1402 return FALSE;
1403 hList = cbInfo.hwndList;
1404 idx = SendMessage(hList, LB_GETCURSEL, 0, 0);
1405 if (idx < 0)
1406 return FALSE;
1407
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);
1414
1415 return result != 0;
1416 }
1417
1418 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1419 {
1420 switch(message)
1421 {
1422 case WM_INITDIALOG:
1423 {
1424 WCHAR buffer[MAX_STRING_LEN];
1425 SYSTEMTIME st;
1426 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1427 GetLocalTime(&st);
1428
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);
1437
1438 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1439 }
1440 break;
1441
1442 case WM_COMMAND:
1443 switch(LOWORD(wParam))
1444 {
1445 case IDC_DATETIME:
1446 if (HIWORD(wParam) != LBN_DBLCLK)
1447 break;
1448 /* Fall through */
1449
1450 case IDOK:
1451 {
1452 LRESULT index;
1453 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1454
1455 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1456
1457 if(index != LB_ERR)
1458 {
1459 WCHAR buffer[MAX_STRING_LEN];
1460 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1461 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1462 }
1463 }
1464 /* Fall through */
1465
1466 case IDCANCEL:
1467 EndDialog(hWnd, wParam);
1468 return TRUE;
1469 }
1470 }
1471 return FALSE;
1472 }
1473
1474 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1475 {
1476 switch(message)
1477 {
1478 case WM_INITDIALOG:
1479 {
1480 HINSTANCE hInstance = GetModuleHandleW(0);
1481 WCHAR buffer[MAX_STRING_LEN];
1482 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1483
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);
1490
1491 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1492 }
1493 break;
1494
1495 case WM_COMMAND:
1496 switch(LOWORD(wParam))
1497 {
1498 case IDOK:
1499 {
1500 LRESULT index;
1501 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1502 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1503
1504 if(index != LB_ERR)
1505 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1506 }
1507 return TRUE;
1508
1509 case IDCANCEL:
1510 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1511 return TRUE;
1512 }
1513 }
1514 return FALSE;
1515 }
1516
1517 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1518 {
1519 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1520
1521 switch(message)
1522 {
1523 case WM_INITDIALOG:
1524 {
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;
1533
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);
1543
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);
1548
1549 if(pf.wAlignment == PFA_RIGHT)
1550 index ++;
1551 else if(pf.wAlignment == PFA_CENTER)
1552 index += 2;
1553
1554 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1555
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);
1562 }
1563 break;
1564
1565 case WM_COMMAND:
1566 switch(LOWORD(wParam))
1567 {
1568 case IDOK:
1569 {
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 PARAFORMAT pf;
1579 UNIT unit;
1580
1581 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1582 pf.wAlignment = ALIGNMENT_VALUES[index];
1583
1584 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1585 if(number_from_string(buffer, &num, &unit))
1586 ret++;
1587 pf.dxOffset = units_to_twips(unit, num);
1588 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1589 if(number_from_string(buffer, &num, &unit))
1590 ret++;
1591 pf.dxRightIndent = units_to_twips(unit, num);
1592 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1593 if(number_from_string(buffer, &num, &unit))
1594 ret++;
1595 pf.dxStartIndent = units_to_twips(unit, num);
1596
1597 if(ret != 3)
1598 {
1599 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1600 wszAppTitle, MB_OK | MB_ICONASTERISK);
1601 return FALSE;
1602 } else
1603 {
1604 if (pf.dxOffset + pf.dxStartIndent < 0
1605 && pf.dxStartIndent < 0)
1606 {
1607 /* The first line is before the left edge, so
1608 * make sure it is at the left edge. */
1609 pf.dxOffset = -pf.dxStartIndent;
1610 } else if (pf.dxOffset < 0) {
1611 /* The second and following lines are before
1612 * the left edge, so set it to be at the left
1613 * edge, and adjust the first line since it
1614 * is relative to it. */
1615 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1616 pf.dxOffset = 0;
1617 }
1618 /* Internally the dxStartIndent is the absolute
1619 * offset for the first line and dxOffset is
1620 * to it value as opposed how it is displayed with
1621 * the first line being the relative value.
1622 * These two lines make the adjustments. */
1623 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1624 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1625
1626 pf.cbSize = sizeof(pf);
1627 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1628 PFM_STARTINDENT;
1629 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1630 }
1631 }
1632 /* Fall through */
1633
1634 case IDCANCEL:
1635 EndDialog(hWnd, wParam);
1636 return TRUE;
1637 }
1638 }
1639 return FALSE;
1640 }
1641
1642 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1643 {
1644 switch(message)
1645 {
1646 case WM_INITDIALOG:
1647 {
1648 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1649 PARAFORMAT pf;
1650 WCHAR buffer[MAX_STRING_LEN];
1651 int i;
1652
1653 pf.cbSize = sizeof(pf);
1654 pf.dwMask = PFM_TABSTOPS;
1655 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1656 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1657
1658 for(i = 0; i < pf.cTabCount; i++)
1659 {
1660 number_with_units(buffer, pf.rgxTabs[i]);
1661 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1662 }
1663 SetFocus(hTabWnd);
1664 }
1665 break;
1666
1667 case WM_COMMAND:
1668 switch(LOWORD(wParam))
1669 {
1670 case IDC_TABSTOPS:
1671 {
1672 HWND hTabWnd = (HWND)lParam;
1673 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1674 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1675 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1676
1677 if(GetWindowTextLengthW(hTabWnd))
1678 EnableWindow(hAddWnd, TRUE);
1679 else
1680 EnableWindow(hAddWnd, FALSE);
1681
1682 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1683 {
1684 EnableWindow(hEmptyWnd, TRUE);
1685
1686 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1687 EnableWindow(hDelWnd, FALSE);
1688 else
1689 EnableWindow(hDelWnd, TRUE);
1690 } else
1691 {
1692 EnableWindow(hEmptyWnd, FALSE);
1693 }
1694 }
1695 break;
1696
1697 case ID_TAB_ADD:
1698 {
1699 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1700 WCHAR buffer[MAX_STRING_LEN];
1701 UNIT unit;
1702
1703 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1704 append_current_units(buffer);
1705
1706 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1707 {
1708 float number = 0;
1709 int item_count = SendMessage(hTabWnd, CB_GETCOUNT, 0, 0);
1710
1711 if(!number_from_string(buffer, &number, &unit))
1712 {
1713 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1714 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1715 } else if (item_count >= MAX_TAB_STOPS) {
1716 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1717 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1718 } else {
1719 int i;
1720 float next_number = -1;
1721 int next_number_in_twips = -1;
1722 int insert_number = units_to_twips(unit, number);
1723
1724 /* linear search for position to insert the string */
1725 for(i = 0; i < item_count; i++)
1726 {
1727 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1728 number_from_string(buffer, &next_number, &unit);
1729 next_number_in_twips = units_to_twips(unit, next_number);
1730 if (insert_number <= next_number_in_twips)
1731 break;
1732 }
1733 if (insert_number != next_number_in_twips)
1734 {
1735 number_with_units(buffer, insert_number);
1736 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1737 SetWindowTextW(hTabWnd, 0);
1738 }
1739 }
1740 }
1741 SetFocus(hTabWnd);
1742 }
1743 break;
1744
1745 case ID_TAB_DEL:
1746 {
1747 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1748 LRESULT ret;
1749 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1750 if(ret != CB_ERR)
1751 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1752 }
1753 break;
1754
1755 case ID_TAB_EMPTY:
1756 {
1757 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1758 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1759 SetFocus(hTabWnd);
1760 }
1761 break;
1762
1763 case IDOK:
1764 {
1765 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1766 int i;
1767 WCHAR buffer[MAX_STRING_LEN];
1768 PARAFORMAT pf;
1769 float number;
1770 UNIT unit;
1771
1772 pf.cbSize = sizeof(pf);
1773 pf.dwMask = PFM_TABSTOPS;
1774
1775 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1776 (LPARAM)&buffer) != CB_ERR &&
1777 i < MAX_TAB_STOPS; i++)
1778 {
1779 number_from_string(buffer, &number, &unit);
1780 pf.rgxTabs[i] = units_to_twips(unit, number);
1781 }
1782 pf.cTabCount = i;
1783 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1784 }
1785 /* Fall through */
1786 case IDCANCEL:
1787 EndDialog(hWnd, wParam);
1788 return TRUE;
1789 }
1790 }
1791 return FALSE;
1792 }
1793
1794 static LRESULT OnCreate( HWND hWnd )
1795 {
1796 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1797 HINSTANCE hInstance = GetModuleHandleW(0);
1798 HANDLE hDLL;
1799 TBADDBITMAP ab;
1800 int nStdBitmaps = 0;
1801 REBARINFO rbi;
1802 REBARBANDINFOW rbb;
1803 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1804 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1805
1806 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1807
1808 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1809 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1810 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1811
1812 rbi.cbSize = sizeof(rbi);
1813 rbi.fMask = 0;
1814 rbi.himl = NULL;
1815 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1816 return -1;
1817
1818 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|BTNS_BUTTON,
1819 IDC_TOOLBAR,
1820 1, hInstance, IDB_TOOLBAR,
1821 NULL, 0,
1822 24, 24, 16, 16, sizeof(TBBUTTON));
1823
1824 ab.hInst = HINST_COMMCTRL;
1825 ab.nID = IDB_STD_SMALL_COLOR;
1826 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1827
1828 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1829 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1830 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1831 AddSeparator(hToolBarWnd);
1832 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1833 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1834 AddSeparator(hToolBarWnd);
1835 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1836 AddSeparator(hToolBarWnd);
1837 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1838 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1839 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1840 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1841 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1842 AddSeparator(hToolBarWnd);
1843 AddButton(hToolBarWnd, 0, ID_DATETIME);
1844
1845 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1846
1847 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1848 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1849 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1850 rbb.cx = 0;
1851 rbb.hwndChild = hToolBarWnd;
1852 rbb.cxMinChild = 0;
1853 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1854 rbb.wID = BANDID_TOOLBAR;
1855
1856 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1857
1858 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1859 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1860 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1861
1862 rbb.hwndChild = hFontListWnd;
1863 rbb.cx = 200;
1864 rbb.wID = BANDID_FONTLIST;
1865
1866 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1867
1868 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1869 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1870 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1871
1872 rbb.hwndChild = hSizeListWnd;
1873 rbb.cx = 50;
1874 rbb.fStyle ^= RBBS_BREAK;
1875 rbb.wID = BANDID_SIZELIST;
1876
1877 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1878
1879 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1880 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | BTNS_BUTTON,
1881 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1882
1883 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1884 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1885 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1886 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1887 AddSeparator(hFormatBarWnd);
1888 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1889 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1890 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1891 AddSeparator(hFormatBarWnd);
1892 AddButton(hFormatBarWnd, 7, ID_BULLET);
1893
1894 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1895
1896 rbb.hwndChild = hFormatBarWnd;
1897 rbb.wID = BANDID_FORMATBAR;
1898
1899 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1900
1901 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1902 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1903
1904
1905 rbb.hwndChild = hRulerWnd;
1906 rbb.wID = BANDID_RULER;
1907 rbb.fStyle |= RBBS_BREAK;
1908
1909 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1910
1911 hDLL = LoadLibraryW(wszRichEditDll);
1912 if(!hDLL)
1913 {
1914 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1915 MB_OK | MB_ICONEXCLAMATION);
1916 PostQuitMessage(1);
1917 }
1918
1919 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1920 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1921 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1922 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1923
1924 if (!hEditorWnd)
1925 {
1926 fprintf(stderr, "Error code %u\n", GetLastError());
1927 return -1;
1928 }
1929 assert(hEditorWnd);
1930
1931 setup_richedit_olecallback(hEditorWnd);
1932 SetFocus(hEditorWnd);
1933 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1934
1935 set_default_font();
1936
1937 populate_font_list(hFontListWnd);
1938 populate_size_list(hSizeListWnd);
1939 DoLoadStrings();
1940 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1941
1942 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1943
1944 registry_read_filelist(hWnd);
1945 registry_read_formatopts_all(barState, wordWrap);
1946 registry_read_options();
1947 DragAcceptFiles(hWnd, TRUE);
1948
1949 return 0;
1950 }
1951
1952 static LRESULT OnUser( HWND hWnd )
1953 {
1954 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1955 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1956 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1957 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1958 int from, to;
1959 CHARFORMAT2W fmt;
1960 PARAFORMAT2 pf;
1961 GETTEXTLENGTHEX gt;
1962
1963 ZeroMemory(&fmt, sizeof(fmt));
1964 fmt.cbSize = sizeof(fmt);
1965
1966 ZeroMemory(&pf, sizeof(pf));
1967 pf.cbSize = sizeof(pf);
1968
1969 gt.flags = GTL_NUMCHARS;
1970 gt.codepage = 1200;
1971
1972 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1973 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1974
1975 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
1976
1977 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1978 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
1979 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
1980 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
1981 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
1982 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
1983 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
1984
1985 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
1986 (fmt.dwEffects & CFE_BOLD));
1987 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
1988 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
1989 (fmt.dwEffects & CFE_ITALIC));
1990 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
1991 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
1992 (fmt.dwEffects & CFE_UNDERLINE));
1993 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
1994
1995 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1996 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
1997 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
1998 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
1999
2000 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
2001 return 0;
2002 }
2003
2004 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2005 {
2006 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2007 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2008 NMHDR *pHdr = (NMHDR *)lParam;
2009 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2010 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2011
2012 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2013 {
2014 if (pHdr->code == CBEN_ENDEDITW)
2015 {
2016 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
2017 if(pHdr->hwndFrom == hwndFontList)
2018 {
2019 on_fontlist_modified((LPWSTR)endEdit->szText);
2020 } else if (pHdr->hwndFrom == hwndSizeList)
2021 {
2022 on_sizelist_modified(hwndFontList,(LPWSTR)endEdit->szText);
2023 }
2024 }
2025 return 0;
2026 }
2027
2028 if (pHdr->hwndFrom != hwndEditor)
2029 return 0;
2030
2031 if (pHdr->code == EN_SELCHANGE)
2032 {
2033 SELCHANGE *pSC = (SELCHANGE *)lParam;
2034 char buf[128];
2035
2036 update_font_list();
2037
2038 sprintf( buf,"selection = %d..%d, line count=%ld",
2039 pSC->chrg.cpMin, pSC->chrg.cpMax,
2040 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
2041 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2042 SendMessage(hWnd, WM_USER, 0, 0);
2043 return 1;
2044 }
2045 return 0;
2046 }
2047
2048 /* Copied from dlls/comdlg32/fontdlg.c */
2049 static const COLORREF textcolors[]=
2050 {
2051 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2052 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2053 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2054 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2055 };
2056
2057 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2058 {
2059 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2060 static FINDREPLACEW findreplace;
2061
2062 if ((HWND)lParam == hwndEditor)
2063 return 0;
2064
2065 switch(LOWORD(wParam))
2066 {
2067
2068 case ID_FILE_EXIT:
2069 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2070 break;
2071
2072 case ID_FILE_NEW:
2073 {
2074 HINSTANCE hInstance = GetModuleHandleW(0);
2075 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
2076 newfile_proc);
2077
2078 if(ret != ID_NEWFILE_ABORT)
2079 {
2080 if(prompt_save_changes())
2081 {
2082 SETTEXTEX st;
2083
2084 set_caption(NULL);
2085 wszFileName[0] = '\0';
2086
2087 clear_formatting();
2088
2089 st.flags = ST_DEFAULT;
2090 st.codepage = 1200;
2091 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2092
2093 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2094 set_fileformat(ret);
2095 update_font_list();
2096 }
2097 }
2098 }
2099 break;
2100
2101 case ID_FILE_OPEN:
2102 DialogOpenFile();
2103 break;
2104
2105 case ID_FILE_SAVE:
2106 if(wszFileName[0])
2107 {
2108 DoSaveFile(wszFileName, fileFormat);
2109 break;
2110 }
2111 /* Fall through */
2112
2113 case ID_FILE_SAVEAS:
2114 DialogSaveFile();
2115 break;
2116
2117 case ID_FILE_RECENT1:
2118 case ID_FILE_RECENT2:
2119 case ID_FILE_RECENT3:
2120 case ID_FILE_RECENT4:
2121 {
2122 HMENU hMenu = GetMenu(hWnd);
2123 MENUITEMINFOW mi;
2124
2125 mi.cbSize = sizeof(MENUITEMINFOW);
2126 mi.fMask = MIIM_DATA;
2127 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2128 DoOpenFile((LPWSTR)mi.dwItemData);
2129 }
2130 break;
2131
2132 case ID_FIND:
2133 dialog_find(&findreplace, FALSE);
2134 break;
2135
2136 case ID_FIND_NEXT:
2137 handle_findmsg(&findreplace);
2138 break;
2139
2140 case ID_REPLACE:
2141 dialog_find(&findreplace, TRUE);
2142 break;
2143
2144 case ID_FONTSETTINGS:
2145 dialog_choose_font();
2146 break;
2147
2148 case ID_PRINT:
2149 dialog_print(hWnd, wszFileName);
2150 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2151 break;
2152
2153 case ID_PRINT_QUICK:
2154 print_quick(hMainWnd, wszFileName);
2155 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2156 break;
2157
2158 case ID_PREVIEW:
2159 {
2160 int index = reg_formatindex(fileFormat);
2161 DWORD tmp = barState[index];
2162 barState[index] = 1 << BANDID_STATUSBAR;
2163 set_bar_states();
2164 barState[index] = tmp;
2165 ShowWindow(hEditorWnd, FALSE);
2166
2167 init_preview(hWnd, wszFileName);
2168
2169 SetMenu(hWnd, NULL);
2170 InvalidateRect(0, 0, TRUE);
2171 }
2172 break;
2173
2174 case ID_PRINTSETUP:
2175 dialog_printsetup(hWnd);
2176 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2177 break;
2178
2179 case ID_FORMAT_BOLD:
2180 case ID_FORMAT_ITALIC:
2181 case ID_FORMAT_UNDERLINE:
2182 {
2183 CHARFORMAT2W fmt;
2184 int effects = CFE_BOLD;
2185
2186 ZeroMemory(&fmt, sizeof(fmt));
2187 fmt.cbSize = sizeof(fmt);
2188 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2189
2190 fmt.dwMask = CFM_BOLD;
2191
2192 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2193 {
2194 effects = CFE_ITALIC;
2195 fmt.dwMask = CFM_ITALIC;
2196 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2197 {
2198 effects = CFE_UNDERLINE;
2199 fmt.dwMask = CFM_UNDERLINE;
2200 }
2201
2202 fmt.dwEffects ^= effects;
2203
2204 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2205 break;
2206 }
2207
2208 case ID_FORMAT_COLOR:
2209 {
2210 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2211 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2212 HMENU hPop;
2213 RECT itemrc;
2214 POINT pt;
2215 int mid;
2216 int itemidx = SendMessage(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2217
2218 SendMessage(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2219 pt.x = itemrc.left;
2220 pt.y = itemrc.bottom;
2221 ClientToScreen(hFormatBarWnd, &pt);
2222 hPop = GetSubMenu(hColorPopupMenu, 0);
2223 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2224 TPM_RETURNCMD | TPM_NONOTIFY,
2225 pt.x, pt.y, 0, hWnd, 0);
2226 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2227 {
2228 CHARFORMAT2W fmt;
2229
2230 ZeroMemory(&fmt, sizeof(fmt));
2231 fmt.cbSize = sizeof(fmt);
2232 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2233
2234 fmt.dwMask = CFM_COLOR;
2235
2236 if (mid < ID_COLOR_AUTOMATIC) {
2237 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2238 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2239 } else {
2240 fmt.dwEffects |= CFE_AUTOCOLOR;
2241 }
2242
2243 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2244 }
2245 break;
2246 }
2247
2248 case ID_EDIT_CUT:
2249 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2250 break;
2251
2252 case ID_EDIT_COPY:
2253 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2254 break;
2255
2256 case ID_EDIT_PASTE:
2257 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2258 break;
2259
2260 case ID_EDIT_CLEAR:
2261 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2262 break;
2263
2264 case ID_EDIT_SELECTALL:
2265 {
2266 CHARRANGE range = {0, -1};
2267 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2268 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2269 return 0;
2270 }
2271
2272 case ID_EDIT_GETTEXT:
2273 {
2274 int nLen = GetWindowTextLengthW(hwndEditor);
2275 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2276 TEXTRANGEW tr;
2277
2278 GetWindowTextW(hwndEditor, data, nLen+1);
2279 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2280
2281 HeapFree( GetProcessHeap(), 0, data);
2282 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2283 tr.chrg.cpMin = 0;
2284 tr.chrg.cpMax = nLen;
2285 tr.lpstrText = data;
2286 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2287 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2288 HeapFree( GetProcessHeap(), 0, data );
2289
2290 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2291 return 0;
2292 }
2293
2294 case ID_EDIT_CHARFORMAT:
2295 case ID_EDIT_DEFCHARFORMAT:
2296 {
2297 CHARFORMAT2W cf;
2298
2299 ZeroMemory(&cf, sizeof(cf));
2300 cf.cbSize = sizeof(cf);
2301 cf.dwMask = 0;
2302 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2303 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2304 return 0;
2305 }
2306
2307 case ID_EDIT_PARAFORMAT:
2308 {
2309 PARAFORMAT2 pf;
2310 ZeroMemory(&pf, sizeof(pf));
2311 pf.cbSize = sizeof(pf);
2312 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2313 return 0;
2314 }
2315
2316 case ID_EDIT_SELECTIONINFO:
2317 {
2318 CHARRANGE range = {0, -1};
2319 char buf[128];
2320 WCHAR *data = NULL;
2321
2322 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2323 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2324 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2325 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2326 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2327 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2328 HeapFree( GetProcessHeap(), 0, data);
2329 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2330 return 0;
2331 }
2332
2333 case ID_EDIT_READONLY:
2334 {
2335 LONG nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2336 if (nStyle & ES_READONLY)
2337 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2338 else
2339 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2340 return 0;
2341 }
2342
2343 case ID_EDIT_MODIFIED:
2344 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2345 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2346 else
2347 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2348 return 0;
2349
2350 case ID_EDIT_UNDO:
2351 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2352 return 0;
2353
2354 case ID_EDIT_REDO:
2355 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2356 return 0;
2357
2358 case ID_BULLET:
2359 {
2360 PARAFORMAT pf;
2361
2362 pf.cbSize = sizeof(pf);
2363 pf.dwMask = PFM_NUMBERING;
2364 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2365
2366 pf.dwMask |= PFM_OFFSET;
2367
2368 if(pf.wNumbering == PFN_BULLET)
2369 {
2370 pf.wNumbering = 0;
2371 pf.dxOffset = 0;
2372 } else
2373 {
2374 pf.wNumbering = PFN_BULLET;
2375 pf.dxOffset = 720;
2376 }
2377
2378 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2379 }
2380 break;
2381
2382 case ID_ALIGN_LEFT:
2383 case ID_ALIGN_CENTER:
2384 case ID_ALIGN_RIGHT:
2385 {
2386 PARAFORMAT2 pf;
2387
2388 pf.cbSize = sizeof(pf);
2389 pf.dwMask = PFM_ALIGNMENT;
2390 switch(LOWORD(wParam)) {
2391 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2392 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2393 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2394 }
2395 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2396 break;
2397 }
2398
2399 case ID_BACK_1:
2400 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2401 break;
2402
2403 case ID_BACK_2:
2404 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2405 break;
2406
2407 case ID_TOGGLE_TOOLBAR:
2408 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2409 update_window();
2410 break;
2411
2412 case ID_TOGGLE_FORMATBAR:
2413 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2414 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2415 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2416 update_window();
2417 break;
2418
2419 case ID_TOGGLE_STATUSBAR:
2420 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2421 update_window();
2422 break;
2423
2424 case ID_TOGGLE_RULER:
2425 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2426 update_window();
2427 break;
2428
2429 case ID_DATETIME:
2430 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2431 break;
2432
2433 case ID_PARAFORMAT:
2434 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2435 break;
2436
2437 case ID_TABSTOPS:
2438 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2439 break;
2440
2441 case ID_ABOUT:
2442 dialog_about();
2443 break;
2444
2445 case ID_VIEWPROPERTIES:
2446 dialog_viewproperties();
2447 break;
2448
2449 case IDC_FONTLIST:
2450 if (HIWORD(wParam) == CBN_SELENDOK)
2451 {
2452 WCHAR buffer[LF_FACESIZE];
2453 HWND hwndFontList = (HWND)lParam;
2454 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2455 on_fontlist_modified(buffer);
2456 }
2457 break;
2458
2459 case IDC_SIZELIST:
2460 if (HIWORD(wParam) == CBN_SELENDOK)
2461 {
2462 WCHAR buffer[MAX_STRING_LEN+1];
2463 HWND hwndSizeList = (HWND)lParam;
2464 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2465 on_sizelist_modified(hwndSizeList, buffer);
2466 }
2467 break;
2468
2469 default:
2470 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2471 break;
2472 }
2473 return 0;
2474 }
2475
2476 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2477 {
2478 HMENU hMenu = (HMENU)wParam;
2479 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2480 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2481 PARAFORMAT pf;
2482 int nAlignment = -1;
2483 int selFrom, selTo;
2484 GETTEXTLENGTHEX gt;
2485 LRESULT textLength;
2486 MENUITEMINFOW mi;
2487
2488 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2489 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2490 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2491
2492 pf.cbSize = sizeof(PARAFORMAT);
2493 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2494 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2495 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
2496 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2497 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
2498 if (pf.dwMask & PFM_ALIGNMENT)
2499 nAlignment = pf.wAlignment;
2500 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
2501 MF_CHECKED : MF_UNCHECKED);
2502 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
2503 MF_CHECKED : MF_UNCHECKED);
2504 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
2505 MF_CHECKED : MF_UNCHECKED);
2506 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
2507 MF_CHECKED : MF_UNCHECKED));
2508 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
2509 MF_ENABLED : MF_GRAYED);
2510 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
2511 MF_ENABLED : MF_GRAYED);
2512
2513 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
2514 MF_CHECKED : MF_UNCHECKED);
2515
2516 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
2517 MF_CHECKED : MF_UNCHECKED);
2518
2519 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
2520 MF_CHECKED : MF_UNCHECKED);
2521
2522 CheckMenuItem(hMenu, ID_TOGGLE_RULER, MF_BYCOMMAND|(is_bar_visible(BANDID_RULER)) ? MF_CHECKED : MF_UNCHECKED);
2523
2524 gt.flags = GTL_NUMCHARS;
2525 gt.codepage = 1200;
2526 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2527 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2528
2529 mi.cbSize = sizeof(mi);
2530 mi.fMask = MIIM_DATA;
2531
2532 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2533
2534 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
2535 MF_ENABLED : MF_GRAYED));
2536
2537 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2538
2539 return 0;
2540 }
2541
2542 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2543 {
2544 int nStatusSize = 0;
2545 RECT rc;
2546 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2547 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2548 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2549 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2550 int rebarHeight = 0;
2551
2552 if (hwndStatusBar)
2553 {
2554 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2555 if (IsWindowVisible(hwndStatusBar))
2556 {
2557 GetClientRect(hwndStatusBar, &rc);
2558 nStatusSize = rc.bottom - rc.top;
2559 } else
2560 {
2561 nStatusSize = 0;
2562 }
2563 }
2564 if (hwndReBar)
2565 {
2566 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2567
2568 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2569 }
2570 if (hwndEditor)
2571 {
2572 GetClientRect(hWnd, &rc);
2573 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2574 }
2575
2576 redraw_ruler(hRulerWnd);
2577
2578 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2579 }
2580
2581 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2582 {
2583 if(msg == ID_FINDMSGSTRING)
2584 return handle_findmsg((LPFINDREPLACEW)lParam);
2585
2586 switch(msg)
2587 {
2588 case WM_CREATE:
2589 return OnCreate( hWnd );
2590
2591 case WM_USER:
2592 return OnUser( hWnd );
2593
2594 case WM_NOTIFY:
2595 return OnNotify( hWnd, lParam );
2596
2597 case WM_COMMAND:
2598 if(preview_isactive())
2599 {
2600 return preview_command( hWnd, wParam );
2601 }
2602
2603 return OnCommand( hWnd, wParam, lParam );
2604
2605 case WM_DESTROY:
2606 PostQuitMessage(0);
2607 break;
2608
2609 case WM_CLOSE:
2610 if(preview_isactive())
2611 {
2612 preview_exit(hWnd);
2613 } else if(prompt_save_changes())
2614 {
2615 registry_set_options(hMainWnd);
2616 registry_set_formatopts_all(barState, wordWrap);
2617 PostQuitMessage(0);
2618 }
2619 break;
2620
2621 case WM_ACTIVATE:
2622 if (LOWORD(wParam))
2623 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2624 return 0;
2625
2626 case WM_INITMENUPOPUP:
2627 return OnInitPopupMenu( hWnd, wParam );
2628
2629 case WM_SIZE:
2630 return OnSize( hWnd, wParam, lParam );
2631
2632 case WM_CONTEXTMENU:
2633 return DefWindowProcW(hWnd, msg, wParam, lParam);
2634
2635 case WM_DROPFILES:
2636 {
2637 WCHAR file[MAX_PATH];
2638 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2639 DragFinish((HDROP)wParam);
2640
2641 if(prompt_save_changes())
2642 DoOpenFile(file);
2643 }
2644 break;
2645 case WM_PAINT:
2646 if(!preview_isactive())
2647 return DefWindowProcW(hWnd, msg, wParam, lParam);
2648
2649 default:
2650 return DefWindowProcW(hWnd, msg, wParam, lParam);
2651 }
2652
2653 return 0;
2654 }
2655
2656 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2657 {
2658 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2659 HACCEL hAccel;
2660 WNDCLASSEXW wc;
2661 MSG msg;
2662 RECT rc;
2663 UINT_PTR hPrevRulerProc;
2664 HWND hRulerWnd;
2665 POINTL EditPoint;
2666 DWORD bMaximized;
2667 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2668 'T','A','B','L','E','\0'};
2669
2670 InitCommonControlsEx(&classes);
2671
2672 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2673
2674 wc.cbSize = sizeof(wc);
2675 wc.style = 0;
2676 wc.lpfnWndProc = WndProc;
2677 wc.cbClsExtra = 0;
2678 wc.cbWndExtra = 4;
2679 wc.hInstance = hInstance;
2680 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2681 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2682 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2683 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2684 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2685 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2686 wc.lpszClassName = wszMainWndClass;
2687 RegisterClassExW(&wc);
2688
2689 wc.style = 0;
2690 wc.lpfnWndProc = preview_proc;
2691 wc.cbClsExtra = 0;
2692 wc.cbWndExtra = 0;
2693 wc.hInstance = hInstance;
2694 wc.hIcon = NULL;
2695 wc.hIconSm = NULL;
2696 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2697 wc.hbrBackground = NULL;
2698 wc.lpszMenuName = NULL;
2699 wc.lpszClassName = wszPreviewWndClass;
2700 RegisterClassExW(&wc);
2701
2702 registry_read_winrect(&rc);
2703 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2704 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2705 registry_read_maximized(&bMaximized);
2706 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2707 && bMaximized)
2708 nCmdShow = SW_SHOWMAXIMIZED;
2709 ShowWindow(hMainWnd, nCmdShow);
2710
2711 set_caption(NULL);
2712 set_bar_states();
2713 set_fileformat(SF_RTF);
2714 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2715 get_default_printer_opts();
2716 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2717
2718 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2719 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2720 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2721 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2722
2723 HandleCommandLine(GetCommandLineW());
2724
2725 while(GetMessageW(&msg,0,0,0))
2726 {
2727 if (IsDialogMessage(hFindWnd, &msg))
2728 continue;
2729
2730 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2731 continue;
2732 TranslateMessage(&msg);
2733 DispatchMessageW(&msg);
2734 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2735 SendMessageW(hMainWnd, WM_USER, 0, 0);
2736 }
2737
2738 return 0;
2739 }