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