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