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