wordpad: Find dialogs intialize find field with selection.
[wine/wine-gecko.git] / programs / wordpad / wordpad.c
blobedee1f8cd4dc59ec900a7f51117ba2b7264202ac
1 /*
2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2007-2008 by Alexander N. Sørnes <alex@thehandofagony.com>
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.
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.
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
22 #define WIN32_LEAN_AND_MEAN
23 #define _WIN32_IE 0x0400
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <assert.h>
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>
39 #include "wine/unicode.h"
40 #include "wordpad.h"
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
52 /* use LoadString */
53 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
55 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
57 static const WCHAR stringFormat[] = {'%','2','d','\0'};
59 const WCHAR wszPreviewWndClass[] = {'P','r','t','P','r','e','v','i','e','w',0};
60 LRESULT CALLBACK preview_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
62 static HWND hMainWnd;
63 static HWND hEditorWnd;
64 static HWND hFindWnd;
65 static HMENU hPopupMenu;
66 static HMENU hColorPopupMenu;
68 static UINT ID_FINDMSGSTRING;
70 static DWORD wordWrap[2];
71 static DWORD barState[2];
72 static WPARAM fileFormat = SF_RTF;
74 static WCHAR wszFileName[MAX_PATH];
75 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
76 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
77 static WCHAR wszSaveChanges[MAX_STRING_LEN];
78 static WCHAR units_cmW[MAX_STRING_LEN];
79 static WCHAR units_inW[MAX_STRING_LEN];
80 static WCHAR units_inchW[MAX_STRING_LEN];
81 static WCHAR units_ptW[MAX_STRING_LEN];
83 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
85 typedef enum
87 UNIT_CM,
88 UNIT_INCH,
89 UNIT_PT
90 } UNIT;
92 typedef struct
94 int endPos;
95 BOOL wrapped;
96 WCHAR findBuffer[128];
97 } FINDREPLACE_custom;
99 /* Load string resources */
100 static void DoLoadStrings(void)
102 LPWSTR p = wszFilter;
103 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
104 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
105 static const WCHAR files_all[] = {'*','.','*','\0'};
107 HINSTANCE hInstance = GetModuleHandleW(0);
109 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
110 p += lstrlenW(p) + 1;
111 lstrcpyW(p, files_rtf);
112 p += lstrlenW(p) + 1;
113 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
114 p += lstrlenW(p) + 1;
115 lstrcpyW(p, files_txt);
116 p += lstrlenW(p) + 1;
117 LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
118 p += lstrlenW(p) + 1;
119 lstrcpyW(p, files_txt);
120 p += lstrlenW(p) + 1;
121 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
122 p += lstrlenW(p) + 1;
123 lstrcpyW(p, files_all);
124 p += lstrlenW(p) + 1;
125 *p = '\0';
127 p = wszDefaultFileName;
128 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
130 p = wszSaveChanges;
131 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
133 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
134 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
135 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
136 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
139 /* Show a message box with resource strings */
140 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
142 MSGBOXPARAMSW params;
144 params.cbSize = sizeof(params);
145 params.hwndOwner = hWnd;
146 params.hInstance = GetModuleHandleW(0);
147 params.lpszText = lpText;
148 params.lpszCaption = lpCaption;
149 params.dwStyle = uType;
150 params.lpszIcon = NULL;
151 params.dwContextHelpId = 0;
152 params.lpfnMsgBoxCallback = NULL;
153 params.dwLanguageId = 0;
154 return MessageBoxIndirectW(&params);
158 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
160 TBBUTTON button;
162 ZeroMemory(&button, sizeof(button));
163 button.iBitmap = nImage;
164 button.idCommand = nCommand;
165 button.fsState = TBSTATE_ENABLED;
166 button.fsStyle = TBSTYLE_BUTTON;
167 button.dwData = 0;
168 button.iString = -1;
169 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
172 static void AddSeparator(HWND hwndToolBar)
174 TBBUTTON button;
176 ZeroMemory(&button, sizeof(button));
177 button.iBitmap = -1;
178 button.idCommand = 0;
179 button.fsState = 0;
180 button.fsStyle = TBSTYLE_SEP;
181 button.dwData = 0;
182 button.iString = -1;
183 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
186 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
188 HANDLE hFile = (HANDLE)cookie;
189 DWORD read;
191 if(!ReadFile(hFile, buffer, cb, &read, 0))
192 return 1;
194 *pcb = read;
196 return 0;
199 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
201 DWORD written;
202 int ret;
203 HANDLE hFile = (HANDLE)cookie;
205 ret = WriteFile(hFile, buffer, cb, &written, 0);
207 if(!ret || (cb != written))
208 return 1;
210 *pcb = cb;
212 return 0;
215 LPWSTR file_basename(LPWSTR path)
217 LPWSTR pos = path + lstrlenW(path);
219 while(pos > path)
221 if(*pos == '\\' || *pos == '/')
223 pos++;
224 break;
226 pos--;
228 return pos;
231 static void set_caption(LPCWSTR wszNewFileName)
233 static const WCHAR wszSeparator[] = {' ','-',' '};
234 WCHAR *wszCaption;
235 SIZE_T length = 0;
237 if(!wszNewFileName)
238 wszNewFileName = wszDefaultFileName;
239 else
240 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
242 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
243 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
245 if(!wszCaption)
246 return;
248 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
249 length += lstrlenW(wszNewFileName);
250 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
251 length += sizeof(wszSeparator) / sizeof(WCHAR);
252 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
254 SetWindowTextW(hMainWnd, wszCaption);
256 HeapFree(GetProcessHeap(), 0, wszCaption);
259 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
261 if(punit != NULL)
262 *punit = UNIT_CM;
263 if(!endptr)
264 return FALSE;
265 if(!*endptr)
266 return TRUE;
268 while(*endptr == ' ')
269 endptr++;
271 if(punit == NULL)
272 return *endptr == '\0';
274 if(!lstrcmpW(endptr, units_cmW))
276 *punit = UNIT_CM;
277 endptr += lstrlenW(units_cmW);
279 else if (!lstrcmpW(endptr, units_inW))
281 *punit = UNIT_INCH;
282 endptr += lstrlenW(units_inW);
284 else if (!lstrcmpW(endptr, units_inchW))
286 *punit = UNIT_INCH;
287 endptr += lstrlenW(units_inchW);
289 else if (!lstrcmpW(endptr, units_ptW))
291 *punit = UNIT_PT;
292 endptr += lstrlenW(units_ptW);
295 return *endptr == '\0';
298 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
300 double ret;
301 WCHAR *endptr;
303 *num = 0;
304 errno = 0;
305 ret = wcstod(string, &endptr);
307 if (punit != NULL)
308 *punit = UNIT_CM;
309 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
311 return FALSE;
312 } else
314 *num = (float)ret;
315 return TRUE;
319 static void set_size(float size)
321 CHARFORMAT2W fmt;
323 ZeroMemory(&fmt, sizeof(fmt));
324 fmt.cbSize = sizeof(fmt);
325 fmt.dwMask = CFM_SIZE;
326 fmt.yHeight = (int)(size * 20.0);
327 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
330 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
332 WCHAR sizeBuffer[MAX_STRING_LEN];
333 CHARFORMAT2W format;
335 ZeroMemory(&format, sizeof(format));
336 format.cbSize = sizeof(format);
337 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
339 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
340 if(lstrcmpW(sizeBuffer, wszNewFontSize))
342 float size = 0;
343 if(number_from_string(wszNewFontSize, &size, NULL)
344 && size > 0)
346 set_size(size);
347 } else
349 SetWindowTextW(hwndSizeList, sizeBuffer);
350 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
351 wszAppTitle, MB_OK | MB_ICONINFORMATION);
356 static void add_size(HWND hSizeListWnd, unsigned size)
358 WCHAR buffer[3];
359 COMBOBOXEXITEMW cbItem;
360 cbItem.mask = CBEIF_TEXT;
361 cbItem.iItem = -1;
363 wsprintfW(buffer, stringFormat, size);
364 cbItem.pszText = buffer;
365 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
368 static void populate_size_list(HWND hSizeListWnd)
370 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
371 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
372 COMBOBOXEXITEMW cbFontItem;
373 CHARFORMAT2W fmt;
374 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
375 HDC hdc = GetDC(hMainWnd);
376 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
377 WCHAR buffer[3];
378 size_t i;
379 DWORD fontStyle;
381 ZeroMemory(&fmt, sizeof(fmt));
382 fmt.cbSize = sizeof(fmt);
383 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
385 cbFontItem.mask = CBEIF_LPARAM;
386 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
387 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
389 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
391 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
393 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
395 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
396 GetDeviceCaps(hdc, LOGPIXELSY)));
397 } else
399 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
400 add_size(hSizeListWnd, choices[i]);
403 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
404 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
407 static void update_size_list(void)
409 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
410 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
411 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
412 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
413 CHARFORMAT2W fmt;
415 ZeroMemory(&fmt, sizeof(fmt));
416 fmt.cbSize = sizeof(fmt);
418 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
420 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
421 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
423 if(lstrcmpW(fontSize, sizeBuffer))
424 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
427 static void update_font_list(void)
429 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
430 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
431 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
432 WCHAR fontName[MAX_STRING_LEN];
433 CHARFORMAT2W fmt;
435 ZeroMemory(&fmt, sizeof(fmt));
436 fmt.cbSize = sizeof(fmt);
438 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
439 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
441 if(lstrcmpW(fontName, fmt.szFaceName))
443 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
444 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
445 } else
447 update_size_list();
451 static void clear_formatting(void)
453 PARAFORMAT2 pf;
455 pf.cbSize = sizeof(pf);
456 pf.dwMask = PFM_ALIGNMENT;
457 pf.wAlignment = PFA_LEFT;
458 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
461 static int fileformat_number(WPARAM format)
463 int number = 0;
465 if(format == SF_TEXT)
467 number = 1;
468 } else if (format == (SF_TEXT | SF_UNICODE))
470 number = 2;
472 return number;
475 static WPARAM fileformat_flags(int format)
477 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
479 return flags[format];
482 static void set_font(LPCWSTR wszFaceName)
484 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
485 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
486 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
487 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
488 CHARFORMAT2W fmt;
490 ZeroMemory(&fmt, sizeof(fmt));
492 fmt.cbSize = sizeof(fmt);
493 fmt.dwMask = CFM_FACE;
495 lstrcpyW(fmt.szFaceName, wszFaceName);
497 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
499 populate_size_list(hSizeListWnd);
501 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
504 static void set_default_font(void)
506 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
507 'R','o','m','a','n',0};
508 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
509 CHARFORMAT2W fmt;
510 LPCWSTR font;
512 ZeroMemory(&fmt, sizeof(fmt));
514 fmt.cbSize = sizeof(fmt);
515 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
516 fmt.dwEffects = 0;
518 if(fileFormat & SF_RTF)
519 font = richTextFont;
520 else
521 font = plainTextFont;
523 lstrcpyW(fmt.szFaceName, font);
525 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
528 static void on_fontlist_modified(LPWSTR wszNewFaceName)
530 CHARFORMAT2W format;
531 ZeroMemory(&format, sizeof(format));
532 format.cbSize = sizeof(format);
533 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
535 if(lstrcmpW(format.szFaceName, wszNewFaceName))
536 set_font(wszNewFaceName);
539 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
541 COMBOBOXEXITEMW cbItem;
542 WCHAR buffer[MAX_PATH];
543 int fontHeight = 0;
545 cbItem.mask = CBEIF_TEXT;
546 cbItem.pszText = buffer;
547 cbItem.cchTextMax = MAX_STRING_LEN;
548 cbItem.iItem = 0;
550 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
552 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
553 cbItem.iItem++;
554 else
555 break;
557 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
558 lstrcpyW( cbItem.pszText, fontName );
560 cbItem.mask |= CBEIF_LPARAM;
561 if(fontType & RASTER_FONTTYPE)
562 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
564 cbItem.lParam = MAKELONG(fontType,fontHeight);
565 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
566 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
569 static void dialog_choose_font(void)
571 CHOOSEFONTW cf;
572 LOGFONTW lf;
573 CHARFORMAT2W fmt;
574 HDC hDC = GetDC(hMainWnd);
576 ZeroMemory(&cf, sizeof(cf));
577 cf.lStructSize = sizeof(cf);
578 cf.hwndOwner = hMainWnd;
579 cf.lpLogFont = &lf;
580 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS;
582 ZeroMemory(&fmt, sizeof(fmt));
583 fmt.cbSize = sizeof(fmt);
585 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
586 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
587 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) ? TRUE : FALSE;
588 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
589 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) ? TRUE : FALSE;
590 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) ? TRUE : FALSE;
591 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
592 cf.rgbColors = fmt.crTextColor;
594 if(ChooseFontW(&cf))
596 ZeroMemory(&fmt, sizeof(fmt));
597 fmt.cbSize = sizeof(fmt);
598 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
599 fmt.yHeight = cf.iPointSize * 2;
601 if(cf.nFontType & BOLD_FONTTYPE)
602 fmt.dwEffects |= CFE_BOLD;
603 if(cf.nFontType & ITALIC_FONTTYPE)
604 fmt.dwEffects |= CFE_ITALIC;
605 if(cf.lpLogFont->lfUnderline == TRUE)
606 fmt.dwEffects |= CFE_UNDERLINE;
607 if(cf.lpLogFont->lfStrikeOut == TRUE)
608 fmt.dwEffects |= CFE_STRIKEOUT;
610 fmt.crTextColor = cf.rgbColors;
612 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
613 set_font(cf.lpLogFont->lfFaceName);
618 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
619 DWORD FontType, LPARAM lParam)
621 HWND hListWnd = (HWND) lParam;
623 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
626 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
629 return 1;
632 static void populate_font_list(HWND hListWnd)
634 HDC hdc = GetDC(hMainWnd);
635 LOGFONTW fontinfo;
636 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
637 CHARFORMAT2W fmt;
639 fontinfo.lfCharSet = DEFAULT_CHARSET;
640 *fontinfo.lfFaceName = '\0';
641 fontinfo.lfPitchAndFamily = 0;
643 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
644 (LPARAM)hListWnd, 0);
646 ZeroMemory(&fmt, sizeof(fmt));
647 fmt.cbSize = sizeof(fmt);
648 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
649 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
652 static void update_window(void)
654 RECT rect;
656 GetClientRect(hMainWnd, &rect);
658 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
661 static BOOL is_bar_visible(int bandId)
663 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
666 static void store_bar_state(int bandId, BOOL show)
668 int formatIndex = reg_formatindex(fileFormat);
670 if(show)
671 barState[formatIndex] |= (1 << bandId);
672 else
673 barState[formatIndex] &= ~(1 << bandId);
676 static void set_toolbar_state(int bandId, BOOL show)
678 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
680 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
682 if(bandId == BANDID_TOOLBAR)
684 REBARBANDINFOW rbbinfo;
685 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
687 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
688 rbbinfo.fMask = RBBIM_STYLE;
690 SendMessageW(hwndReBar, RB_GETBANDINFO, index, (LPARAM)&rbbinfo);
692 if(!show)
693 rbbinfo.fStyle &= ~RBBS_BREAK;
694 else
695 rbbinfo.fStyle |= RBBS_BREAK;
697 SendMessageW(hwndReBar, RB_SETBANDINFO, index, (LPARAM)&rbbinfo);
700 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
701 store_bar_state(bandId, show);
704 static void set_statusbar_state(BOOL show)
706 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
708 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
709 store_bar_state(BANDID_STATUSBAR, show);
712 static void set_bar_states(void)
714 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
715 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
716 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
717 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
718 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
719 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
721 update_window();
724 static void preview_exit(HWND hMainWnd)
726 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
727 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
729 set_bar_states();
730 ShowWindow(hEditorWnd, TRUE);
732 close_preview(hMainWnd);
734 SetMenu(hMainWnd, hMenu);
735 registry_read_filelist(hMainWnd);
737 update_window();
740 static void set_fileformat(WPARAM format)
742 fileFormat = format;
744 set_bar_states();
745 set_default_font();
746 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
749 static void ShowOpenError(DWORD Code)
751 LPWSTR Message;
753 switch(Code)
755 case ERROR_ACCESS_DENIED:
756 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
757 break;
759 default:
760 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
762 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
765 static void DoOpenFile(LPCWSTR szOpenFileName)
767 HANDLE hFile;
768 EDITSTREAM es;
769 char fileStart[5];
770 DWORD readOut;
771 WPARAM format = SF_TEXT;
773 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
774 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
775 if (hFile == INVALID_HANDLE_VALUE)
777 ShowOpenError(GetLastError());
778 return;
781 ReadFile(hFile, fileStart, 5, &readOut, NULL);
782 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
784 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
786 format = SF_TEXT | SF_UNICODE;
787 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
788 } else if(readOut >= 5)
790 static const char header[] = "{\\rtf";
791 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
793 if(!memcmp(header, fileStart, 5))
794 format = SF_RTF;
795 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
797 CloseHandle(hFile);
798 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
799 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
800 return;
804 es.dwCookie = (DWORD_PTR)hFile;
805 es.pfnCallback = stream_in;
807 clear_formatting();
808 set_fileformat(format);
809 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
811 CloseHandle(hFile);
813 SetFocus(hEditorWnd);
815 set_caption(szOpenFileName);
817 lstrcpyW(wszFileName, szOpenFileName);
818 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
819 registry_set_filelist(szOpenFileName, hMainWnd);
820 update_font_list();
823 static void ShowWriteError(DWORD Code)
825 LPWSTR Message;
827 switch(Code)
829 case ERROR_ACCESS_DENIED:
830 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
831 break;
833 default:
834 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
836 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
839 static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
841 HANDLE hFile;
842 EDITSTREAM stream;
843 LRESULT ret;
845 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
846 FILE_ATTRIBUTE_NORMAL, NULL);
848 if(hFile == INVALID_HANDLE_VALUE)
850 ShowWriteError(GetLastError());
851 return;
854 if(format == (SF_TEXT | SF_UNICODE))
856 static const BYTE unicode[] = {0xff,0xfe};
857 DWORD writeOut;
858 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
860 if(writeOut != sizeof(unicode))
862 CloseHandle(hFile);
863 return;
867 stream.dwCookie = (DWORD_PTR)hFile;
868 stream.pfnCallback = stream_out;
870 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
872 CloseHandle(hFile);
874 SetFocus(hEditorWnd);
876 if(!ret)
878 GETTEXTLENGTHEX gt;
879 gt.flags = GTL_DEFAULT;
880 gt.codepage = 1200;
882 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
883 return;
886 lstrcpyW(wszFileName, wszSaveFileName);
887 set_caption(wszFileName);
888 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
889 set_fileformat(format);
892 static void DialogSaveFile(void)
894 OPENFILENAMEW sfn;
896 WCHAR wszFile[MAX_PATH] = {'\0'};
897 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
899 ZeroMemory(&sfn, sizeof(sfn));
901 sfn.lStructSize = sizeof(sfn);
902 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
903 sfn.hwndOwner = hMainWnd;
904 sfn.lpstrFilter = wszFilter;
905 sfn.lpstrFile = wszFile;
906 sfn.nMaxFile = MAX_PATH;
907 sfn.lpstrDefExt = wszDefExt;
908 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
910 while(GetSaveFileNameW(&sfn))
912 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
914 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
915 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
917 continue;
918 } else
920 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
921 break;
923 } else
925 DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
926 break;
931 static BOOL prompt_save_changes(void)
933 if(!wszFileName[0])
935 GETTEXTLENGTHEX gt;
936 gt.flags = GTL_NUMCHARS;
937 gt.codepage = 1200;
938 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
939 return TRUE;
942 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
944 return TRUE;
945 } else
947 LPWSTR displayFileName;
948 WCHAR *text;
949 int ret;
951 if(!wszFileName[0])
952 displayFileName = wszDefaultFileName;
953 else
954 displayFileName = file_basename(wszFileName);
956 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
957 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
959 if(!text)
960 return FALSE;
962 wsprintfW(text, wszSaveChanges, displayFileName);
964 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
966 HeapFree(GetProcessHeap(), 0, text);
968 switch(ret)
970 case IDNO:
971 return TRUE;
973 case IDYES:
974 if(wszFileName[0])
975 DoSaveFile(wszFileName, fileFormat);
976 else
977 DialogSaveFile();
978 return TRUE;
980 default:
981 return FALSE;
986 static void DialogOpenFile(void)
988 OPENFILENAMEW ofn;
990 WCHAR wszFile[MAX_PATH] = {'\0'};
991 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
993 ZeroMemory(&ofn, sizeof(ofn));
995 ofn.lStructSize = sizeof(ofn);
996 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
997 ofn.hwndOwner = hMainWnd;
998 ofn.lpstrFilter = wszFilter;
999 ofn.lpstrFile = wszFile;
1000 ofn.nMaxFile = MAX_PATH;
1001 ofn.lpstrDefExt = wszDefExt;
1002 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
1004 if(GetOpenFileNameW(&ofn))
1006 if(prompt_save_changes())
1007 DoOpenFile(ofn.lpstrFile);
1011 static void dialog_about(void)
1013 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1014 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1017 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1019 switch(message)
1021 case WM_INITDIALOG:
1023 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1024 int wrap = -1;
1025 char id[4];
1026 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1028 sprintf(id, "%d\n", (int)ps->lParam);
1029 SetWindowTextA(hIdWnd, id);
1030 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1031 wrap = IDC_PAGEFMT_WN;
1032 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1033 wrap = IDC_PAGEFMT_WW;
1034 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1035 wrap = IDC_PAGEFMT_WM;
1037 if(wrap != -1)
1038 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1039 IDC_PAGEFMT_WM, wrap);
1041 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1042 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1043 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1044 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1045 if(barState[ps->lParam] & (1 << BANDID_RULER))
1046 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1047 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1048 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1050 break;
1052 case WM_COMMAND:
1053 switch(LOWORD(wParam))
1055 case IDC_PAGEFMT_WN:
1056 case IDC_PAGEFMT_WW:
1057 case IDC_PAGEFMT_WM:
1058 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1059 LOWORD(wParam));
1060 break;
1062 case IDC_PAGEFMT_TB:
1063 case IDC_PAGEFMT_FB:
1064 case IDC_PAGEFMT_RU:
1065 case IDC_PAGEFMT_SB:
1066 CheckDlgButton(hWnd, LOWORD(wParam),
1067 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1068 break;
1070 break;
1071 case WM_NOTIFY:
1073 LPNMHDR header = (LPNMHDR)lParam;
1074 if(header->code == PSN_APPLY)
1076 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1077 char sid[4];
1078 int id;
1080 GetWindowTextA(hIdWnd, sid, 4);
1081 id = atoi(sid);
1082 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1083 wordWrap[id] = ID_WORDWRAP_NONE;
1084 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1085 wordWrap[id] = ID_WORDWRAP_WINDOW;
1086 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1087 wordWrap[id] = ID_WORDWRAP_MARGIN;
1089 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1090 barState[id] |= (1 << BANDID_TOOLBAR);
1091 else
1092 barState[id] &= ~(1 << BANDID_TOOLBAR);
1094 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1095 barState[id] |= (1 << BANDID_FORMATBAR);
1096 else
1097 barState[id] &= ~(1 << BANDID_FORMATBAR);
1099 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1100 barState[id] |= (1 << BANDID_RULER);
1101 else
1102 barState[id] &= ~(1 << BANDID_RULER);
1104 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1105 barState[id] |= (1 << BANDID_STATUSBAR);
1106 else
1107 barState[id] &= ~(1 << BANDID_STATUSBAR);
1110 break;
1112 return FALSE;
1115 static void dialog_viewproperties(void)
1117 PROPSHEETPAGEW psp[2];
1118 PROPSHEETHEADERW psh;
1119 size_t i;
1120 HINSTANCE hInstance = GetModuleHandleW(0);
1121 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1123 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1124 psp[0].dwFlags = PSP_USETITLE;
1125 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1126 psp[0].pfnDlgProc = formatopts_proc;
1127 psp[0].hInstance = hInstance;
1128 psp[0].lParam = reg_formatindex(SF_TEXT);
1129 psp[0].pfnCallback = NULL;
1130 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1131 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1133 psp[i].dwSize = psp[0].dwSize;
1134 psp[i].dwFlags = psp[0].dwFlags;
1135 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1136 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1137 psp[i].hInstance = psp[0].hInstance;
1138 psp[i].lParam = reg_formatindex(SF_RTF);
1139 psp[i].pfnCallback = psp[0].pfnCallback;
1140 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1143 psh.dwSize = sizeof(psh);
1144 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1145 psh.hwndParent = hMainWnd;
1146 psh.hInstance = hInstance;
1147 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1148 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1149 U3(psh).ppsp = ppsp;
1150 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1152 if(fileFormat & SF_RTF)
1153 U2(psh).nStartPage = 1;
1154 else
1155 U2(psh).nStartPage = 0;
1156 PropertySheetW(&psh);
1157 set_bar_states();
1158 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1161 static void HandleCommandLine(LPWSTR cmdline)
1163 WCHAR delimiter;
1164 int opt_print = 0;
1166 /* skip white space */
1167 while (*cmdline == ' ') cmdline++;
1169 /* skip executable name */
1170 delimiter = (*cmdline == '"' ? '"' : ' ');
1172 if (*cmdline == delimiter) cmdline++;
1173 while (*cmdline && *cmdline != delimiter) cmdline++;
1174 if (*cmdline == delimiter) cmdline++;
1176 while (*cmdline)
1178 while (isspace(*cmdline)) cmdline++;
1180 if (*cmdline == '-' || *cmdline == '/')
1182 if (!cmdline[2] || isspace(cmdline[2]))
1184 switch (cmdline[1])
1186 case 'P':
1187 case 'p':
1188 opt_print = 1;
1189 cmdline += 2;
1190 continue;
1193 /* a filename starting by / */
1195 break;
1198 if (*cmdline)
1200 /* file name is passed on the command line */
1201 if (cmdline[0] == '"')
1203 cmdline++;
1204 cmdline[lstrlenW(cmdline) - 1] = 0;
1206 DoOpenFile(cmdline);
1207 InvalidateRect(hMainWnd, NULL, FALSE);
1210 if (opt_print)
1211 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1214 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1216 if(pFr->Flags & FR_DIALOGTERM)
1218 hFindWnd = 0;
1219 pFr->Flags = FR_FINDNEXT;
1220 return 0;
1223 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1225 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1226 DWORD flags;
1227 FINDTEXTEXW ft;
1228 CHARRANGE sel;
1229 LRESULT ret = -1;
1230 HMENU hMenu = GetMenu(hMainWnd);
1231 MENUITEMINFOW mi;
1233 mi.cbSize = sizeof(mi);
1234 mi.fMask = MIIM_DATA;
1235 mi.dwItemData = 1;
1236 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1238 /* Make sure find field is saved. */
1239 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1241 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1242 sizeof(custom_data->findBuffer));
1243 pFr->lpstrFindWhat = custom_data->findBuffer;
1246 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1247 if(custom_data->endPos == -1) {
1248 custom_data->endPos = sel.cpMin;
1249 custom_data->wrapped = FALSE;
1252 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1253 ft.lpstrText = pFr->lpstrFindWhat;
1255 /* Only replace existing selectino if it is an exact match. */
1256 if (sel.cpMin != sel.cpMax &&
1257 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1259 ft.chrg = sel;
1260 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1261 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1262 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1263 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1267 /* Search from the start of the selection, but exclude the first character
1268 * from search if there is a selection. */
1269 ft.chrg.cpMin = sel.cpMin;
1270 if (sel.cpMin != sel.cpMax)
1271 ft.chrg.cpMin++;
1273 /* Search to the end, then wrap around and search from the start. */
1274 if (!custom_data->wrapped) {
1275 ft.chrg.cpMax = -1;
1276 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1277 if (ret == -1) {
1278 custom_data->wrapped = TRUE;
1279 ft.chrg.cpMin = 0;
1283 if (ret == -1) {
1284 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1285 if (ft.chrg.cpMax > ft.chrg.cpMin)
1286 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1289 if (ret == -1) {
1290 custom_data->endPos = -1;
1291 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1292 wszAppTitle, MB_OK | MB_ICONASTERISK);
1293 } else {
1294 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1295 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1297 if (pFr->Flags & FR_REPLACEALL)
1298 return handle_findmsg(pFr);
1302 return 0;
1305 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1307 static WCHAR selBuffer[128];
1308 static WCHAR replaceBuffer[128];
1309 static FINDREPLACE_custom custom_data;
1310 static const WCHAR endl = '\r';
1311 FINDTEXTW ft;
1313 /* Allow only one search/replace dialog to open */
1314 if(hFindWnd != NULL)
1316 SetActiveWindow(hFindWnd);
1317 return;
1320 ZeroMemory(fr, sizeof(FINDREPLACEW));
1321 fr->lStructSize = sizeof(FINDREPLACEW);
1322 fr->hwndOwner = hMainWnd;
1323 fr->Flags = FR_HIDEUPDOWN;
1324 /* Find field is filled with the selected text if it is non-empty
1325 * and stays within the same paragraph, otherwise the previous
1326 * find field is used. */
1327 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1328 (LPARAM)&ft.chrg.cpMax);
1329 ft.lpstrText = &endl;
1330 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1331 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1333 /* Use a temporary buffer for the selected text so that the saved
1334 * find field is only overwritten when a find/replace is clicked. */
1335 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1336 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1337 fr->lpstrFindWhat = selBuffer;
1338 } else {
1339 fr->lpstrFindWhat = custom_data.findBuffer;
1341 fr->lpstrReplaceWith = replaceBuffer;
1342 custom_data.endPos = -1;
1343 custom_data.wrapped = FALSE;
1344 fr->lCustData = (LPARAM)&custom_data;
1345 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1346 fr->wReplaceWithLen = sizeof(replaceBuffer);
1348 if(replace)
1349 hFindWnd = ReplaceTextW(fr);
1350 else
1351 hFindWnd = FindTextW(fr);
1354 static int units_to_twips(UNIT unit, float number)
1356 int twips = 0;
1358 switch(unit)
1360 case UNIT_CM:
1361 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1362 break;
1364 case UNIT_INCH:
1365 twips = (int)(number * (float)TWIPS_PER_INCH);
1366 break;
1368 case UNIT_PT:
1369 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1370 break;
1373 return twips;
1376 static void append_current_units(LPWSTR buffer)
1378 static const WCHAR space[] = {' ', 0};
1379 lstrcatW(buffer, space);
1380 lstrcatW(buffer, units_cmW);
1383 static void number_with_units(LPWSTR buffer, int number)
1385 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1386 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1388 sprintfW(buffer, fmt, converted, units_cmW);
1391 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1393 COMBOBOXEXITEMW cbItem;
1394 COMBOBOXINFO cbInfo;
1395 HWND hCombo, hList;
1396 int idx, result;
1398 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1399 if (!hCombo)
1400 return FALSE;
1401 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1402 result = SendMessage(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1403 if (!result)
1404 return FALSE;
1405 hList = cbInfo.hwndList;
1406 idx = SendMessage(hList, LB_GETCURSEL, 0, 0);
1407 if (idx < 0)
1408 return FALSE;
1410 ZeroMemory(&cbItem, sizeof(cbItem));
1411 cbItem.mask = CBEIF_TEXT;
1412 cbItem.iItem = idx;
1413 cbItem.pszText = wszBuffer;
1414 cbItem.cchTextMax = bufferLength-1;
1415 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1417 return result != 0;
1420 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1422 switch(message)
1424 case WM_INITDIALOG:
1426 WCHAR buffer[MAX_STRING_LEN];
1427 SYSTEMTIME st;
1428 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1429 GetLocalTime(&st);
1431 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1432 MAX_STRING_LEN);
1433 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1434 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1435 MAX_STRING_LEN);
1436 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1437 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1438 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1440 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1442 break;
1444 case WM_COMMAND:
1445 switch(LOWORD(wParam))
1447 case IDC_DATETIME:
1448 if (HIWORD(wParam) != LBN_DBLCLK)
1449 break;
1450 /* Fall through */
1452 case IDOK:
1454 LRESULT index;
1455 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1457 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1459 if(index != LB_ERR)
1461 WCHAR buffer[MAX_STRING_LEN];
1462 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1463 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1466 /* Fall through */
1468 case IDCANCEL:
1469 EndDialog(hWnd, wParam);
1470 return TRUE;
1473 return FALSE;
1476 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1478 switch(message)
1480 case WM_INITDIALOG:
1482 HINSTANCE hInstance = GetModuleHandleW(0);
1483 WCHAR buffer[MAX_STRING_LEN];
1484 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1486 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1487 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1488 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1489 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1490 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1491 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1493 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1495 break;
1497 case WM_COMMAND:
1498 switch(LOWORD(wParam))
1500 case IDOK:
1502 LRESULT index;
1503 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1504 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1506 if(index != LB_ERR)
1507 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1509 return TRUE;
1511 case IDCANCEL:
1512 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1513 return TRUE;
1516 return FALSE;
1519 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1521 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1523 switch(message)
1525 case WM_INITDIALOG:
1527 HINSTANCE hInstance = GetModuleHandleW(0);
1528 WCHAR buffer[MAX_STRING_LEN];
1529 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1530 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1531 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1532 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1533 PARAFORMAT2 pf;
1534 int index = 0;
1536 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1537 MAX_STRING_LEN);
1538 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1539 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1540 MAX_STRING_LEN);
1541 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1542 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1543 MAX_STRING_LEN);
1544 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1546 pf.cbSize = sizeof(pf);
1547 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1548 PFM_STARTINDENT;
1549 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1551 if(pf.wAlignment == PFA_RIGHT)
1552 index ++;
1553 else if(pf.wAlignment == PFA_CENTER)
1554 index += 2;
1556 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1558 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1559 SetWindowTextW(hLeftWnd, buffer);
1560 number_with_units(buffer, pf.dxRightIndent);
1561 SetWindowTextW(hRightWnd, buffer);
1562 number_with_units(buffer, -pf.dxOffset);
1563 SetWindowTextW(hFirstWnd, buffer);
1565 break;
1567 case WM_COMMAND:
1568 switch(LOWORD(wParam))
1570 case IDOK:
1572 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1573 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1574 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1575 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1576 WCHAR buffer[MAX_STRING_LEN];
1577 int index;
1578 float num;
1579 int ret = 0;
1580 PARAFORMAT pf;
1581 UNIT unit;
1583 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1584 pf.wAlignment = ALIGNMENT_VALUES[index];
1586 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1587 if(number_from_string(buffer, &num, &unit))
1588 ret++;
1589 pf.dxOffset = units_to_twips(unit, num);
1590 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1591 if(number_from_string(buffer, &num, &unit))
1592 ret++;
1593 pf.dxRightIndent = units_to_twips(unit, num);
1594 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1595 if(number_from_string(buffer, &num, &unit))
1596 ret++;
1597 pf.dxStartIndent = units_to_twips(unit, num);
1599 if(ret != 3)
1601 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1602 wszAppTitle, MB_OK | MB_ICONASTERISK);
1603 return FALSE;
1604 } else
1606 if (pf.dxOffset + pf.dxStartIndent < 0
1607 && pf.dxStartIndent < 0)
1609 /* The first line is before the left edge, so
1610 * make sure it is at the left edge. */
1611 pf.dxOffset = -pf.dxStartIndent;
1612 } else if (pf.dxOffset < 0) {
1613 /* The second and following lines are before
1614 * the left edge, so set it to be at the left
1615 * edge, and adjust the first line since it
1616 * is relative to it. */
1617 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1618 pf.dxOffset = 0;
1620 /* Internally the dxStartIndent is the absolute
1621 * offset for the first line and dxOffset is
1622 * to it value as opposed how it is displayed with
1623 * the first line being the relative value.
1624 * These two lines make the adjustments. */
1625 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1626 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1628 pf.cbSize = sizeof(pf);
1629 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1630 PFM_STARTINDENT;
1631 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1634 /* Fall through */
1636 case IDCANCEL:
1637 EndDialog(hWnd, wParam);
1638 return TRUE;
1641 return FALSE;
1644 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1646 switch(message)
1648 case WM_INITDIALOG:
1650 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1651 PARAFORMAT pf;
1652 WCHAR buffer[MAX_STRING_LEN];
1653 int i;
1655 pf.cbSize = sizeof(pf);
1656 pf.dwMask = PFM_TABSTOPS;
1657 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1658 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1660 for(i = 0; i < pf.cTabCount; i++)
1662 number_with_units(buffer, pf.rgxTabs[i]);
1663 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1665 SetFocus(hTabWnd);
1667 break;
1669 case WM_COMMAND:
1670 switch(LOWORD(wParam))
1672 case IDC_TABSTOPS:
1674 HWND hTabWnd = (HWND)lParam;
1675 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1676 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1677 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1679 if(GetWindowTextLengthW(hTabWnd))
1680 EnableWindow(hAddWnd, TRUE);
1681 else
1682 EnableWindow(hAddWnd, FALSE);
1684 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1686 EnableWindow(hEmptyWnd, TRUE);
1688 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1689 EnableWindow(hDelWnd, FALSE);
1690 else
1691 EnableWindow(hDelWnd, TRUE);
1692 } else
1694 EnableWindow(hEmptyWnd, FALSE);
1697 break;
1699 case ID_TAB_ADD:
1701 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1702 WCHAR buffer[MAX_STRING_LEN];
1703 UNIT unit;
1705 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1706 append_current_units(buffer);
1708 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1710 float number = 0;
1711 int item_count = SendMessage(hTabWnd, CB_GETCOUNT, 0, 0);
1713 if(!number_from_string(buffer, &number, &unit))
1715 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1716 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1717 } else if (item_count >= MAX_TAB_STOPS) {
1718 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1719 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1720 } else {
1721 int i;
1722 float next_number = -1;
1723 int next_number_in_twips = -1;
1724 int insert_number = units_to_twips(unit, number);
1726 /* linear search for position to insert the string */
1727 for(i = 0; i < item_count; i++)
1729 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1730 number_from_string(buffer, &next_number, &unit);
1731 next_number_in_twips = units_to_twips(unit, next_number);
1732 if (insert_number <= next_number_in_twips)
1733 break;
1735 if (insert_number != next_number_in_twips)
1737 number_with_units(buffer, insert_number);
1738 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1739 SetWindowTextW(hTabWnd, 0);
1743 SetFocus(hTabWnd);
1745 break;
1747 case ID_TAB_DEL:
1749 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1750 LRESULT ret;
1751 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1752 if(ret != CB_ERR)
1753 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1755 break;
1757 case ID_TAB_EMPTY:
1759 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1760 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1761 SetFocus(hTabWnd);
1763 break;
1765 case IDOK:
1767 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1768 int i;
1769 WCHAR buffer[MAX_STRING_LEN];
1770 PARAFORMAT pf;
1771 float number;
1772 UNIT unit;
1774 pf.cbSize = sizeof(pf);
1775 pf.dwMask = PFM_TABSTOPS;
1777 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1778 (LPARAM)&buffer) != CB_ERR &&
1779 i < MAX_TAB_STOPS; i++)
1781 number_from_string(buffer, &number, &unit);
1782 pf.rgxTabs[i] = units_to_twips(unit, number);
1784 pf.cTabCount = i;
1785 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1787 /* Fall through */
1788 case IDCANCEL:
1789 EndDialog(hWnd, wParam);
1790 return TRUE;
1793 return FALSE;
1796 static int context_menu(LPARAM lParam)
1798 int x = (int)(short)LOWORD(lParam);
1799 int y = (int)(short)HIWORD(lParam);
1800 HMENU hPop = GetSubMenu(hPopupMenu, 0);
1802 if(x == -1)
1804 int from = 0, to = 0;
1805 POINTL pt;
1806 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
1807 SendMessageW(hEditorWnd, EM_POSFROMCHAR, (WPARAM)&pt, to);
1808 ClientToScreen(hEditorWnd, (POINT*)&pt);
1809 x = pt.x;
1810 y = pt.y;
1813 TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
1814 x, y, 0, hMainWnd, 0);
1816 return 0;
1819 static LRESULT OnCreate( HWND hWnd )
1821 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1822 HINSTANCE hInstance = GetModuleHandleW(0);
1823 HANDLE hDLL;
1824 TBADDBITMAP ab;
1825 int nStdBitmaps = 0;
1826 REBARINFO rbi;
1827 REBARBANDINFOW rbb;
1828 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1829 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1831 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1833 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1834 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1835 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1837 rbi.cbSize = sizeof(rbi);
1838 rbi.fMask = 0;
1839 rbi.himl = NULL;
1840 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1841 return -1;
1843 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
1844 IDC_TOOLBAR,
1845 1, hInstance, IDB_TOOLBAR,
1846 NULL, 0,
1847 24, 24, 16, 16, sizeof(TBBUTTON));
1849 ab.hInst = HINST_COMMCTRL;
1850 ab.nID = IDB_STD_SMALL_COLOR;
1851 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1853 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1854 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1855 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1856 AddSeparator(hToolBarWnd);
1857 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1858 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1859 AddSeparator(hToolBarWnd);
1860 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1861 AddSeparator(hToolBarWnd);
1862 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1863 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1864 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1865 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1866 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1867 AddSeparator(hToolBarWnd);
1868 AddButton(hToolBarWnd, 0, ID_DATETIME);
1870 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1872 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1873 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1874 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1875 rbb.cx = 0;
1876 rbb.hwndChild = hToolBarWnd;
1877 rbb.cxMinChild = 0;
1878 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1879 rbb.wID = BANDID_TOOLBAR;
1881 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1883 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1884 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1885 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1887 rbb.hwndChild = hFontListWnd;
1888 rbb.cx = 200;
1889 rbb.wID = BANDID_FONTLIST;
1891 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1893 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1894 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1895 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1897 rbb.hwndChild = hSizeListWnd;
1898 rbb.cx = 50;
1899 rbb.fStyle ^= RBBS_BREAK;
1900 rbb.wID = BANDID_SIZELIST;
1902 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1904 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1905 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
1906 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1908 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1909 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1910 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1911 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1912 AddSeparator(hFormatBarWnd);
1913 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1914 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1915 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1916 AddSeparator(hFormatBarWnd);
1917 AddButton(hFormatBarWnd, 7, ID_BULLET);
1919 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1921 rbb.hwndChild = hFormatBarWnd;
1922 rbb.wID = BANDID_FORMATBAR;
1924 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1926 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1927 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1930 rbb.hwndChild = hRulerWnd;
1931 rbb.wID = BANDID_RULER;
1932 rbb.fStyle |= RBBS_BREAK;
1934 SendMessageW(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
1936 hDLL = LoadLibraryW(wszRichEditDll);
1937 if(!hDLL)
1939 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1940 MB_OK | MB_ICONEXCLAMATION);
1941 PostQuitMessage(1);
1944 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1945 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1946 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1947 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1949 if (!hEditorWnd)
1951 fprintf(stderr, "Error code %u\n", GetLastError());
1952 return -1;
1954 assert(hEditorWnd);
1956 SetFocus(hEditorWnd);
1957 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1959 set_default_font();
1961 populate_font_list(hFontListWnd);
1962 populate_size_list(hSizeListWnd);
1963 DoLoadStrings();
1964 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1966 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1968 registry_read_filelist(hWnd);
1969 registry_read_formatopts_all(barState, wordWrap);
1970 registry_read_options();
1971 DragAcceptFiles(hWnd, TRUE);
1973 return 0;
1976 static LRESULT OnUser( HWND hWnd )
1978 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1979 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1980 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1981 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1982 int from, to;
1983 CHARFORMAT2W fmt;
1984 PARAFORMAT2 pf;
1985 GETTEXTLENGTHEX gt;
1987 ZeroMemory(&fmt, sizeof(fmt));
1988 fmt.cbSize = sizeof(fmt);
1990 ZeroMemory(&pf, sizeof(pf));
1991 pf.cbSize = sizeof(pf);
1993 gt.flags = GTL_NUMCHARS;
1994 gt.codepage = 1200;
1996 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
1997 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
1999 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
2001 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2002 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
2003 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
2004 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
2005 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
2006 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
2007 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2009 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2010 (fmt.dwEffects & CFE_BOLD));
2011 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2012 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2013 (fmt.dwEffects & CFE_ITALIC));
2014 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2015 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2016 (fmt.dwEffects & CFE_UNDERLINE));
2017 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2019 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2020 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2021 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2022 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2024 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLET, (pf.wNumbering & PFN_BULLET));
2025 return 0;
2028 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2030 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2031 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2032 NMHDR *pHdr = (NMHDR *)lParam;
2033 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2034 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2036 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2038 if (pHdr->code == CBEN_ENDEDITW)
2040 NMCBEENDEDIT *endEdit = (NMCBEENDEDIT *)lParam;
2041 if(pHdr->hwndFrom == hwndFontList)
2043 on_fontlist_modified((LPWSTR)endEdit->szText);
2044 } else if (pHdr->hwndFrom == hwndSizeList)
2046 on_sizelist_modified(hwndFontList,(LPWSTR)endEdit->szText);
2049 return 0;
2052 if (pHdr->hwndFrom != hwndEditor)
2053 return 0;
2055 if (pHdr->code == EN_SELCHANGE)
2057 SELCHANGE *pSC = (SELCHANGE *)lParam;
2058 char buf[128];
2060 update_font_list();
2062 sprintf( buf,"selection = %d..%d, line count=%ld",
2063 pSC->chrg.cpMin, pSC->chrg.cpMax,
2064 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
2065 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2066 SendMessage(hWnd, WM_USER, 0, 0);
2067 return 1;
2069 return 0;
2072 /* Copied from dlls/comdlg32/fontdlg.c */
2073 static const COLORREF textcolors[]=
2075 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2076 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2077 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2078 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2081 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2083 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2084 static FINDREPLACEW findreplace;
2086 if ((HWND)lParam == hwndEditor)
2087 return 0;
2089 switch(LOWORD(wParam))
2092 case ID_FILE_EXIT:
2093 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2094 break;
2096 case ID_FILE_NEW:
2098 HINSTANCE hInstance = GetModuleHandleW(0);
2099 int ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_NEWFILE), hWnd,
2100 newfile_proc);
2102 if(ret != ID_NEWFILE_ABORT)
2104 if(prompt_save_changes())
2106 SETTEXTEX st;
2108 set_caption(NULL);
2109 wszFileName[0] = '\0';
2111 clear_formatting();
2113 st.flags = ST_DEFAULT;
2114 st.codepage = 1200;
2115 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2117 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2118 set_fileformat(ret);
2119 update_font_list();
2123 break;
2125 case ID_FILE_OPEN:
2126 DialogOpenFile();
2127 break;
2129 case ID_FILE_SAVE:
2130 if(wszFileName[0])
2132 DoSaveFile(wszFileName, fileFormat);
2133 break;
2135 /* Fall through */
2137 case ID_FILE_SAVEAS:
2138 DialogSaveFile();
2139 break;
2141 case ID_FILE_RECENT1:
2142 case ID_FILE_RECENT2:
2143 case ID_FILE_RECENT3:
2144 case ID_FILE_RECENT4:
2146 HMENU hMenu = GetMenu(hWnd);
2147 MENUITEMINFOW mi;
2149 mi.cbSize = sizeof(MENUITEMINFOW);
2150 mi.fMask = MIIM_DATA;
2151 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2152 DoOpenFile((LPWSTR)mi.dwItemData);
2154 break;
2156 case ID_FIND:
2157 dialog_find(&findreplace, FALSE);
2158 break;
2160 case ID_FIND_NEXT:
2161 handle_findmsg(&findreplace);
2162 break;
2164 case ID_REPLACE:
2165 dialog_find(&findreplace, TRUE);
2166 break;
2168 case ID_FONTSETTINGS:
2169 dialog_choose_font();
2170 break;
2172 case ID_PRINT:
2173 dialog_print(hWnd, wszFileName);
2174 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2175 break;
2177 case ID_PRINT_QUICK:
2178 print_quick(hMainWnd, wszFileName);
2179 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2180 break;
2182 case ID_PREVIEW:
2184 int index = reg_formatindex(fileFormat);
2185 DWORD tmp = barState[index];
2186 barState[index] = 1 << BANDID_STATUSBAR;
2187 set_bar_states();
2188 barState[index] = tmp;
2189 ShowWindow(hEditorWnd, FALSE);
2191 init_preview(hWnd, wszFileName);
2193 SetMenu(hWnd, NULL);
2194 InvalidateRect(0, 0, TRUE);
2196 break;
2198 case ID_PRINTSETUP:
2199 dialog_printsetup(hWnd);
2200 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2201 break;
2203 case ID_FORMAT_BOLD:
2204 case ID_FORMAT_ITALIC:
2205 case ID_FORMAT_UNDERLINE:
2207 CHARFORMAT2W fmt;
2208 int effects = CFE_BOLD;
2210 ZeroMemory(&fmt, sizeof(fmt));
2211 fmt.cbSize = sizeof(fmt);
2212 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2214 fmt.dwMask = CFM_BOLD;
2216 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2218 effects = CFE_ITALIC;
2219 fmt.dwMask = CFM_ITALIC;
2220 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2222 effects = CFE_UNDERLINE;
2223 fmt.dwMask = CFM_UNDERLINE;
2226 fmt.dwEffects ^= effects;
2228 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2229 break;
2232 case ID_FORMAT_COLOR:
2234 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2235 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2236 HMENU hPop;
2237 RECT itemrc;
2238 POINT pt;
2239 int mid;
2240 int itemidx = SendMessage(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2242 SendMessage(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2243 pt.x = itemrc.left;
2244 pt.y = itemrc.bottom;
2245 ClientToScreen(hFormatBarWnd, &pt);
2246 hPop = GetSubMenu(hColorPopupMenu, 0);
2247 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2248 TPM_RETURNCMD | TPM_NONOTIFY,
2249 pt.x, pt.y, 0, hWnd, 0);
2250 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2252 CHARFORMAT2W fmt;
2254 ZeroMemory(&fmt, sizeof(fmt));
2255 fmt.cbSize = sizeof(fmt);
2256 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2258 fmt.dwMask = CFM_COLOR;
2260 if (mid < ID_COLOR_AUTOMATIC) {
2261 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2262 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2263 } else {
2264 fmt.dwEffects |= CFE_AUTOCOLOR;
2267 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2269 break;
2272 case ID_EDIT_CUT:
2273 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2274 break;
2276 case ID_EDIT_COPY:
2277 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2278 break;
2280 case ID_EDIT_PASTE:
2281 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2282 break;
2284 case ID_EDIT_CLEAR:
2285 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2286 break;
2288 case ID_EDIT_SELECTALL:
2290 CHARRANGE range = {0, -1};
2291 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2292 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2293 return 0;
2296 case ID_EDIT_GETTEXT:
2298 int nLen = GetWindowTextLengthW(hwndEditor);
2299 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2300 TEXTRANGEW tr;
2302 GetWindowTextW(hwndEditor, data, nLen+1);
2303 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2305 HeapFree( GetProcessHeap(), 0, data);
2306 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2307 tr.chrg.cpMin = 0;
2308 tr.chrg.cpMax = nLen;
2309 tr.lpstrText = data;
2310 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2311 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2312 HeapFree( GetProcessHeap(), 0, data );
2314 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2315 return 0;
2318 case ID_EDIT_CHARFORMAT:
2319 case ID_EDIT_DEFCHARFORMAT:
2321 CHARFORMAT2W cf;
2323 ZeroMemory(&cf, sizeof(cf));
2324 cf.cbSize = sizeof(cf);
2325 cf.dwMask = 0;
2326 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2327 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2328 return 0;
2331 case ID_EDIT_PARAFORMAT:
2333 PARAFORMAT2 pf;
2334 ZeroMemory(&pf, sizeof(pf));
2335 pf.cbSize = sizeof(pf);
2336 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2337 return 0;
2340 case ID_EDIT_SELECTIONINFO:
2342 CHARRANGE range = {0, -1};
2343 char buf[128];
2344 WCHAR *data = NULL;
2346 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2347 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2348 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2349 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2350 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2351 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2352 HeapFree( GetProcessHeap(), 0, data);
2353 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2354 return 0;
2357 case ID_EDIT_READONLY:
2359 LONG nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
2360 if (nStyle & ES_READONLY)
2361 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2362 else
2363 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2364 return 0;
2367 case ID_EDIT_MODIFIED:
2368 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2369 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2370 else
2371 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2372 return 0;
2374 case ID_EDIT_UNDO:
2375 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2376 return 0;
2378 case ID_EDIT_REDO:
2379 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2380 return 0;
2382 case ID_BULLET:
2384 PARAFORMAT pf;
2386 pf.cbSize = sizeof(pf);
2387 pf.dwMask = PFM_NUMBERING;
2388 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2390 pf.dwMask |= PFM_OFFSET;
2392 if(pf.wNumbering == PFN_BULLET)
2394 pf.wNumbering = 0;
2395 pf.dxOffset = 0;
2396 } else
2398 pf.wNumbering = PFN_BULLET;
2399 pf.dxOffset = 720;
2402 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2404 break;
2406 case ID_ALIGN_LEFT:
2407 case ID_ALIGN_CENTER:
2408 case ID_ALIGN_RIGHT:
2410 PARAFORMAT2 pf;
2412 pf.cbSize = sizeof(pf);
2413 pf.dwMask = PFM_ALIGNMENT;
2414 switch(LOWORD(wParam)) {
2415 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2416 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2417 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2419 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2420 break;
2423 case ID_BACK_1:
2424 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2425 break;
2427 case ID_BACK_2:
2428 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2429 break;
2431 case ID_TOGGLE_TOOLBAR:
2432 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2433 update_window();
2434 break;
2436 case ID_TOGGLE_FORMATBAR:
2437 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2438 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2439 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2440 update_window();
2441 break;
2443 case ID_TOGGLE_STATUSBAR:
2444 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2445 update_window();
2446 break;
2448 case ID_TOGGLE_RULER:
2449 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2450 update_window();
2451 break;
2453 case ID_DATETIME:
2454 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2455 break;
2457 case ID_PARAFORMAT:
2458 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2459 break;
2461 case ID_TABSTOPS:
2462 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2463 break;
2465 case ID_ABOUT:
2466 dialog_about();
2467 break;
2469 case ID_VIEWPROPERTIES:
2470 dialog_viewproperties();
2471 break;
2473 case IDC_FONTLIST:
2474 if (HIWORD(wParam) == CBN_SELENDOK)
2476 WCHAR buffer[LF_FACESIZE];
2477 HWND hwndFontList = (HWND)lParam;
2478 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2479 on_fontlist_modified(buffer);
2481 break;
2483 case IDC_SIZELIST:
2484 if (HIWORD(wParam) == CBN_SELENDOK)
2486 WCHAR buffer[MAX_STRING_LEN+1];
2487 HWND hwndSizeList = (HWND)lParam;
2488 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2489 on_sizelist_modified(hwndSizeList, buffer);
2491 break;
2493 default:
2494 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2495 break;
2497 return 0;
2500 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2502 HMENU hMenu = (HMENU)wParam;
2503 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2504 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2505 PARAFORMAT pf;
2506 int nAlignment = -1;
2507 int selFrom, selTo;
2508 GETTEXTLENGTHEX gt;
2509 LRESULT textLength;
2510 MENUITEMINFOW mi;
2512 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2513 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2514 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2516 pf.cbSize = sizeof(PARAFORMAT);
2517 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2518 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2519 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
2520 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2521 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
2522 if (pf.dwMask & PFM_ALIGNMENT)
2523 nAlignment = pf.wAlignment;
2524 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
2525 MF_CHECKED : MF_UNCHECKED);
2526 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
2527 MF_CHECKED : MF_UNCHECKED);
2528 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
2529 MF_CHECKED : MF_UNCHECKED);
2530 CheckMenuItem(hMenu, ID_BULLET, MF_BYCOMMAND | ((pf.wNumbering == PFN_BULLET) ?
2531 MF_CHECKED : MF_UNCHECKED));
2532 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
2533 MF_ENABLED : MF_GRAYED);
2534 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
2535 MF_ENABLED : MF_GRAYED);
2537 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_TOOLBAR)) ?
2538 MF_CHECKED : MF_UNCHECKED);
2540 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(is_bar_visible(BANDID_FORMATBAR)) ?
2541 MF_CHECKED : MF_UNCHECKED);
2543 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
2544 MF_CHECKED : MF_UNCHECKED);
2546 CheckMenuItem(hMenu, ID_TOGGLE_RULER, MF_BYCOMMAND|(is_bar_visible(BANDID_RULER)) ? MF_CHECKED : MF_UNCHECKED);
2548 gt.flags = GTL_NUMCHARS;
2549 gt.codepage = 1200;
2550 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2551 EnableMenuItem(hMenu, ID_FIND, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2553 mi.cbSize = sizeof(mi);
2554 mi.fMask = MIIM_DATA;
2556 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2558 EnableMenuItem(hMenu, ID_FIND_NEXT, MF_BYCOMMAND|((textLength && mi.dwItemData) ?
2559 MF_ENABLED : MF_GRAYED));
2561 EnableMenuItem(hMenu, ID_REPLACE, MF_BYCOMMAND|(textLength ? MF_ENABLED : MF_GRAYED));
2563 return 0;
2566 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2568 int nStatusSize = 0;
2569 RECT rc;
2570 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2571 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2572 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2573 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2574 int rebarHeight = 0;
2576 if (hwndStatusBar)
2578 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2579 if (IsWindowVisible(hwndStatusBar))
2581 GetClientRect(hwndStatusBar, &rc);
2582 nStatusSize = rc.bottom - rc.top;
2583 } else
2585 nStatusSize = 0;
2588 if (hwndReBar)
2590 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2592 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2594 if (hwndEditor)
2596 GetClientRect(hWnd, &rc);
2597 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2600 redraw_ruler(hRulerWnd);
2602 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2605 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2607 if(msg == ID_FINDMSGSTRING)
2608 return handle_findmsg((LPFINDREPLACEW)lParam);
2610 switch(msg)
2612 case WM_CREATE:
2613 return OnCreate( hWnd );
2615 case WM_USER:
2616 return OnUser( hWnd );
2618 case WM_NOTIFY:
2619 return OnNotify( hWnd, lParam );
2621 case WM_COMMAND:
2622 if(preview_isactive())
2624 return preview_command( hWnd, wParam );
2627 return OnCommand( hWnd, wParam, lParam );
2629 case WM_DESTROY:
2630 PostQuitMessage(0);
2631 break;
2633 case WM_CLOSE:
2634 if(preview_isactive())
2636 preview_exit(hWnd);
2637 } else if(prompt_save_changes())
2639 registry_set_options(hMainWnd);
2640 registry_set_formatopts_all(barState);
2641 PostQuitMessage(0);
2643 break;
2645 case WM_ACTIVATE:
2646 if (LOWORD(wParam))
2647 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2648 return 0;
2650 case WM_INITMENUPOPUP:
2651 return OnInitPopupMenu( hWnd, wParam );
2653 case WM_SIZE:
2654 return OnSize( hWnd, wParam, lParam );
2656 case WM_CONTEXTMENU:
2657 if((HWND)wParam == hEditorWnd)
2658 return context_menu(lParam);
2659 else
2660 return DefWindowProcW(hWnd, msg, wParam, lParam);
2662 case WM_DROPFILES:
2664 WCHAR file[MAX_PATH];
2665 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2666 DragFinish((HDROP)wParam);
2668 if(prompt_save_changes())
2669 DoOpenFile(file);
2671 break;
2672 case WM_PAINT:
2673 if(!preview_isactive())
2674 return DefWindowProcW(hWnd, msg, wParam, lParam);
2676 default:
2677 return DefWindowProcW(hWnd, msg, wParam, lParam);
2680 return 0;
2683 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2685 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2686 HACCEL hAccel;
2687 WNDCLASSEXW wc;
2688 MSG msg;
2689 RECT rc;
2690 UINT_PTR hPrevRulerProc;
2691 HWND hRulerWnd;
2692 POINTL EditPoint;
2693 DWORD bMaximized;
2694 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2695 'T','A','B','L','E','\0'};
2697 InitCommonControlsEx(&classes);
2699 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2701 wc.cbSize = sizeof(wc);
2702 wc.style = CS_HREDRAW | CS_VREDRAW;
2703 wc.lpfnWndProc = WndProc;
2704 wc.cbClsExtra = 0;
2705 wc.cbWndExtra = 4;
2706 wc.hInstance = hInstance;
2707 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2708 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2709 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2710 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2711 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2712 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2713 wc.lpszClassName = wszMainWndClass;
2714 RegisterClassExW(&wc);
2716 wc.style = CS_HREDRAW | CS_VREDRAW;
2717 wc.lpfnWndProc = preview_proc;
2718 wc.cbClsExtra = 0;
2719 wc.cbWndExtra = 0;
2720 wc.hInstance = hInstance;
2721 wc.hIcon = NULL;
2722 wc.hIconSm = NULL;
2723 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
2724 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2725 wc.lpszMenuName = NULL;
2726 wc.lpszClassName = wszPreviewWndClass;
2727 RegisterClassExW(&wc);
2729 registry_read_winrect(&rc);
2730 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2731 rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, NULL, NULL, hInstance, NULL);
2732 registry_read_maximized(&bMaximized);
2733 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2734 && bMaximized)
2735 nCmdShow = SW_SHOWMAXIMIZED;
2736 ShowWindow(hMainWnd, nCmdShow);
2738 set_caption(NULL);
2739 set_bar_states();
2740 set_fileformat(SF_RTF);
2741 hPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_POPUP));
2742 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2743 get_default_printer_opts();
2744 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2746 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2747 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2748 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2749 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2751 HandleCommandLine(GetCommandLineW());
2753 while(GetMessageW(&msg,0,0,0))
2755 if (IsDialogMessage(hFindWnd, &msg))
2756 continue;
2758 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2759 continue;
2760 TranslateMessage(&msg);
2761 DispatchMessageW(&msg);
2762 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2763 SendMessageW(hMainWnd, WM_USER, 0, 0);
2766 return 0;