winex11: Avoid deadlock when setting cursor.
[wine.git] / programs / wordpad / wordpad.c
blob904867f61795e9eb8e962d9c1181b67e021215c7
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 hColorPopupMenu;
67 static UINT ID_FINDMSGSTRING;
69 static DWORD wordWrap[2];
70 static DWORD barState[2];
71 static WPARAM fileFormat = SF_RTF;
73 static WCHAR wszFileName[MAX_PATH];
74 static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5];
75 static WCHAR wszDefaultFileName[MAX_STRING_LEN];
76 static WCHAR wszSaveChanges[MAX_STRING_LEN];
77 static WCHAR units_cmW[MAX_STRING_LEN];
78 static WCHAR units_inW[MAX_STRING_LEN];
79 static WCHAR units_inchW[MAX_STRING_LEN];
80 static WCHAR units_ptW[MAX_STRING_LEN];
82 static int last_bullet = PFN_BULLET;
84 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
86 typedef enum
88 UNIT_CM,
89 UNIT_INCH,
90 UNIT_PT
91 } UNIT;
93 typedef struct
95 int endPos;
96 BOOL wrapped;
97 WCHAR findBuffer[128];
98 } FINDREPLACE_custom;
100 /* Load string resources */
101 static void DoLoadStrings(void)
103 LPWSTR p = wszFilter;
104 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
105 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
106 static const WCHAR files_all[] = {'*','.','*','\0'};
108 HINSTANCE hInstance = GetModuleHandleW(0);
110 p += 1 + LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
111 lstrcpyW(p, files_rtf);
112 p += lstrlenW(p) + 1;
113 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
114 lstrcpyW(p, files_txt);
115 p += lstrlenW(p) + 1;
116 p += 1 + LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN);
117 lstrcpyW(p, files_txt);
118 p += lstrlenW(p) + 1;
119 p += 1 + LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
120 lstrcpyW(p, files_all);
121 p += lstrlenW(p) + 1;
122 *p = '\0';
124 p = wszDefaultFileName;
125 LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
127 p = wszSaveChanges;
128 LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
130 LoadStringW(hInstance, STRING_UNITS_CM, units_cmW, MAX_STRING_LEN);
131 LoadStringW(hInstance, STRING_UNITS_IN, units_inW, MAX_STRING_LEN);
132 LoadStringW(hInstance, STRING_UNITS_INCH, units_inchW, MAX_STRING_LEN);
133 LoadStringW(hInstance, STRING_UNITS_PT, units_ptW, MAX_STRING_LEN);
136 /* Show a message box with resource strings */
137 static int MessageBoxWithResStringW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
139 MSGBOXPARAMSW params;
141 params.cbSize = sizeof(params);
142 params.hwndOwner = hWnd;
143 params.hInstance = GetModuleHandleW(0);
144 params.lpszText = lpText;
145 params.lpszCaption = lpCaption;
146 params.dwStyle = uType;
147 params.lpszIcon = NULL;
148 params.dwContextHelpId = 0;
149 params.lpfnMsgBoxCallback = NULL;
150 params.dwLanguageId = 0;
151 return MessageBoxIndirectW(&params);
155 static void AddButtonStyle(HWND hwndToolBar, int nImage, int nCommand, BYTE style)
157 TBBUTTON button;
159 ZeroMemory(&button, sizeof(button));
160 button.iBitmap = nImage;
161 button.idCommand = nCommand;
162 button.fsState = TBSTATE_ENABLED;
163 button.fsStyle = style;
164 button.dwData = 0;
165 button.iString = -1;
166 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
169 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
171 AddButtonStyle(hwndToolBar, nImage, nCommand, BTNS_BUTTON);
174 static void AddSeparator(HWND hwndToolBar)
176 TBBUTTON button;
178 ZeroMemory(&button, sizeof(button));
179 button.iBitmap = -1;
180 button.idCommand = 0;
181 button.fsState = 0;
182 button.fsStyle = BTNS_SEP;
183 button.dwData = 0;
184 button.iString = -1;
185 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
188 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
190 HANDLE hFile = (HANDLE)cookie;
191 DWORD read;
193 if(!ReadFile(hFile, buffer, cb, &read, 0))
194 return 1;
196 *pcb = read;
198 return 0;
201 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
203 DWORD written;
204 int ret;
205 HANDLE hFile = (HANDLE)cookie;
207 ret = WriteFile(hFile, buffer, cb, &written, 0);
209 if(!ret || (cb != written))
210 return 1;
212 *pcb = cb;
214 return 0;
217 LPWSTR file_basename(LPWSTR path)
219 LPWSTR pos = path + lstrlenW(path);
221 while(pos > path)
223 if(*pos == '\\' || *pos == '/')
225 pos++;
226 break;
228 pos--;
230 return pos;
233 static void set_caption(LPCWSTR wszNewFileName)
235 static const WCHAR wszSeparator[] = {' ','-',' '};
236 WCHAR *wszCaption;
237 SIZE_T length = 0;
239 if(!wszNewFileName)
240 wszNewFileName = wszDefaultFileName;
241 else
242 wszNewFileName = file_basename((LPWSTR)wszNewFileName);
244 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
245 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
247 if(!wszCaption)
248 return;
250 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
251 length += lstrlenW(wszNewFileName);
252 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
253 length += sizeof(wszSeparator) / sizeof(WCHAR);
254 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
256 SetWindowTextW(hMainWnd, wszCaption);
258 HeapFree(GetProcessHeap(), 0, wszCaption);
261 static BOOL validate_endptr(LPCWSTR endptr, UNIT *punit)
263 if(punit != NULL)
264 *punit = UNIT_CM;
265 if(!endptr)
266 return FALSE;
267 if(!*endptr)
268 return TRUE;
270 while(*endptr == ' ')
271 endptr++;
273 if(punit == NULL)
274 return *endptr == '\0';
276 if(!lstrcmpW(endptr, units_cmW))
278 *punit = UNIT_CM;
279 endptr += lstrlenW(units_cmW);
281 else if (!lstrcmpW(endptr, units_inW))
283 *punit = UNIT_INCH;
284 endptr += lstrlenW(units_inW);
286 else if (!lstrcmpW(endptr, units_inchW))
288 *punit = UNIT_INCH;
289 endptr += lstrlenW(units_inchW);
291 else if (!lstrcmpW(endptr, units_ptW))
293 *punit = UNIT_PT;
294 endptr += lstrlenW(units_ptW);
297 return *endptr == '\0';
300 static BOOL number_from_string(LPCWSTR string, float *num, UNIT *punit)
302 double ret;
303 WCHAR *endptr;
305 *num = 0;
306 errno = 0;
307 ret = wcstod(string, &endptr);
309 if (punit != NULL)
310 *punit = UNIT_CM;
311 if((ret == 0 && errno != 0) || endptr == string || !validate_endptr(endptr, punit))
313 return FALSE;
314 } else
316 *num = (float)ret;
317 return TRUE;
321 static void set_size(float size)
323 CHARFORMAT2W fmt;
325 ZeroMemory(&fmt, sizeof(fmt));
326 fmt.cbSize = sizeof(fmt);
327 fmt.dwMask = CFM_SIZE;
328 fmt.yHeight = (int)(size * 20.0);
329 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
332 static void on_sizelist_modified(HWND hwndSizeList, LPWSTR wszNewFontSize)
334 WCHAR sizeBuffer[MAX_STRING_LEN];
335 CHARFORMAT2W format;
337 ZeroMemory(&format, sizeof(format));
338 format.cbSize = sizeof(format);
339 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
341 wsprintfW(sizeBuffer, stringFormat, format.yHeight / 20);
342 if(lstrcmpW(sizeBuffer, wszNewFontSize))
344 float size = 0;
345 if(number_from_string(wszNewFontSize, &size, NULL)
346 && size > 0)
348 set_size(size);
349 } else
351 SetWindowTextW(hwndSizeList, sizeBuffer);
352 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
353 wszAppTitle, MB_OK | MB_ICONINFORMATION);
358 static void add_size(HWND hSizeListWnd, unsigned size)
360 WCHAR buffer[3];
361 COMBOBOXEXITEMW cbItem;
362 cbItem.mask = CBEIF_TEXT;
363 cbItem.iItem = -1;
365 wsprintfW(buffer, stringFormat, size);
366 cbItem.pszText = buffer;
367 SendMessageW(hSizeListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
370 static void populate_size_list(HWND hSizeListWnd)
372 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
373 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
374 COMBOBOXEXITEMW cbFontItem;
375 CHARFORMAT2W fmt;
376 HWND hListEditWnd = (HWND)SendMessageW(hSizeListWnd, CBEM_GETEDITCONTROL, 0, 0);
377 HDC hdc = GetDC(hMainWnd);
378 static const unsigned choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
379 WCHAR buffer[3];
380 size_t i;
381 DWORD fontStyle;
383 ZeroMemory(&fmt, sizeof(fmt));
384 fmt.cbSize = sizeof(fmt);
385 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
387 cbFontItem.mask = CBEIF_LPARAM;
388 cbFontItem.iItem = SendMessageW(hFontListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)fmt.szFaceName);
389 SendMessageW(hFontListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbFontItem);
391 fontStyle = (DWORD)LOWORD(cbFontItem.lParam);
393 SendMessageW(hSizeListWnd, CB_RESETCONTENT, 0, 0);
395 if((fontStyle & RASTER_FONTTYPE) && cbFontItem.iItem)
397 add_size(hSizeListWnd, (BYTE)MulDiv(HIWORD(cbFontItem.lParam), 72,
398 GetDeviceCaps(hdc, LOGPIXELSY)));
399 } else
401 for(i = 0; i < sizeof(choices)/sizeof(choices[0]); i++)
402 add_size(hSizeListWnd, choices[i]);
405 wsprintfW(buffer, stringFormat, fmt.yHeight / 20);
406 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)buffer);
409 static void update_size_list(void)
411 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
412 HWND hwndSizeList = GetDlgItem(hReBar, IDC_SIZELIST);
413 HWND hwndSizeListEdit = (HWND)SendMessageW(hwndSizeList, CBEM_GETEDITCONTROL, 0, 0);
414 WCHAR fontSize[MAX_STRING_LEN], sizeBuffer[MAX_STRING_LEN];
415 CHARFORMAT2W fmt;
417 ZeroMemory(&fmt, sizeof(fmt));
418 fmt.cbSize = sizeof(fmt);
420 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
422 SendMessageW(hwndSizeListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontSize);
423 wsprintfW(sizeBuffer, stringFormat, fmt.yHeight / 20);
425 if(lstrcmpW(fontSize, sizeBuffer))
426 SendMessageW(hwndSizeListEdit, WM_SETTEXT, 0, (LPARAM)sizeBuffer);
429 static void update_font_list(void)
431 HWND hReBar = GetDlgItem(hMainWnd, IDC_REBAR);
432 HWND hFontList = GetDlgItem(hReBar, IDC_FONTLIST);
433 HWND hFontListEdit = (HWND)SendMessageW(hFontList, CBEM_GETEDITCONTROL, 0, 0);
434 WCHAR fontName[MAX_STRING_LEN];
435 CHARFORMAT2W fmt;
437 ZeroMemory(&fmt, sizeof(fmt));
438 fmt.cbSize = sizeof(fmt);
440 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
441 if (!SendMessageW(hFontListEdit, WM_GETTEXT, MAX_PATH, (LPARAM)fontName)) return;
443 if(lstrcmpW(fontName, fmt.szFaceName))
445 SendMessageW(hFontListEdit, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
446 populate_size_list(GetDlgItem(hReBar, IDC_SIZELIST));
447 } else
449 update_size_list();
453 static void clear_formatting(void)
455 PARAFORMAT2 pf;
457 pf.cbSize = sizeof(pf);
458 pf.dwMask = PFM_ALIGNMENT;
459 pf.wAlignment = PFA_LEFT;
460 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
463 static int fileformat_number(WPARAM format)
465 int number = 0;
467 if(format == SF_TEXT)
469 number = 1;
470 } else if (format == (SF_TEXT | SF_UNICODE))
472 number = 2;
474 return number;
477 static WPARAM fileformat_flags(int format)
479 WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE };
481 return flags[format];
484 static void set_font(LPCWSTR wszFaceName)
486 HWND hReBarWnd = GetDlgItem(hMainWnd, IDC_REBAR);
487 HWND hSizeListWnd = GetDlgItem(hReBarWnd, IDC_SIZELIST);
488 HWND hFontListWnd = GetDlgItem(hReBarWnd, IDC_FONTLIST);
489 HWND hFontListEditWnd = (HWND)SendMessageW(hFontListWnd, CBEM_GETEDITCONTROL, 0, 0);
490 CHARFORMAT2W fmt;
492 ZeroMemory(&fmt, sizeof(fmt));
494 fmt.cbSize = sizeof(fmt);
495 fmt.dwMask = CFM_FACE;
497 lstrcpyW(fmt.szFaceName, wszFaceName);
499 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
501 populate_size_list(hSizeListWnd);
503 SendMessageW(hFontListEditWnd, WM_SETTEXT, 0, (LPARAM)wszFaceName);
506 static void set_default_font(void)
508 static const WCHAR richTextFont[] = {'T','i','m','e','s',' ','N','e','w',' ',
509 'R','o','m','a','n',0};
510 static const WCHAR plainTextFont[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
511 CHARFORMAT2W fmt;
512 LPCWSTR font;
514 ZeroMemory(&fmt, sizeof(fmt));
516 fmt.cbSize = sizeof(fmt);
517 fmt.dwMask = CFM_FACE | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
518 fmt.dwEffects = 0;
520 if(fileFormat & SF_RTF)
521 font = richTextFont;
522 else
523 font = plainTextFont;
525 lstrcpyW(fmt.szFaceName, font);
527 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
530 static void on_fontlist_modified(LPWSTR wszNewFaceName)
532 CHARFORMAT2W format;
533 ZeroMemory(&format, sizeof(format));
534 format.cbSize = sizeof(format);
535 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&format);
537 if(lstrcmpW(format.szFaceName, wszNewFaceName))
538 set_font(wszNewFaceName);
541 static void add_font(LPCWSTR fontName, DWORD fontType, HWND hListWnd, const NEWTEXTMETRICEXW *ntmc)
543 COMBOBOXEXITEMW cbItem;
544 WCHAR buffer[MAX_PATH];
545 int fontHeight = 0;
547 cbItem.mask = CBEIF_TEXT;
548 cbItem.pszText = buffer;
549 cbItem.cchTextMax = MAX_STRING_LEN;
550 cbItem.iItem = 0;
552 while(SendMessageW(hListWnd, CBEM_GETITEMW, 0, (LPARAM)&cbItem))
554 if(lstrcmpiW(cbItem.pszText, fontName) <= 0)
555 cbItem.iItem++;
556 else
557 break;
559 cbItem.pszText = HeapAlloc( GetProcessHeap(), 0, (lstrlenW(fontName) + 1)*sizeof(WCHAR) );
560 lstrcpyW( cbItem.pszText, fontName );
562 cbItem.mask |= CBEIF_LPARAM;
563 if(fontType & RASTER_FONTTYPE)
564 fontHeight = ntmc->ntmTm.tmHeight - ntmc->ntmTm.tmInternalLeading;
566 cbItem.lParam = MAKELONG(fontType,fontHeight);
567 SendMessageW(hListWnd, CBEM_INSERTITEMW, 0, (LPARAM)&cbItem);
568 HeapFree( GetProcessHeap(), 0, cbItem.pszText );
571 static void dialog_choose_font(void)
573 CHOOSEFONTW cf;
574 LOGFONTW lf;
575 CHARFORMAT2W fmt;
576 HDC hDC = GetDC(hMainWnd);
578 ZeroMemory(&cf, sizeof(cf));
579 cf.lStructSize = sizeof(cf);
580 cf.hwndOwner = hMainWnd;
581 cf.lpLogFont = &lf;
582 cf.Flags = CF_SCREENFONTS | CF_NOSCRIPTSEL | CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_NOVERTFONTS;
584 ZeroMemory(&fmt, sizeof(fmt));
585 fmt.cbSize = sizeof(fmt);
587 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
588 lstrcpyW(cf.lpLogFont->lfFaceName, fmt.szFaceName);
589 cf.lpLogFont->lfItalic = (fmt.dwEffects & CFE_ITALIC) != 0;
590 cf.lpLogFont->lfWeight = (fmt.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
591 cf.lpLogFont->lfUnderline = (fmt.dwEffects & CFE_UNDERLINE) != 0;
592 cf.lpLogFont->lfStrikeOut = (fmt.dwEffects & CFE_STRIKEOUT) != 0;
593 cf.lpLogFont->lfHeight = -MulDiv(fmt.yHeight / 20, GetDeviceCaps(hDC, LOGPIXELSY), 72);
594 cf.rgbColors = fmt.crTextColor;
596 if(ChooseFontW(&cf))
598 ZeroMemory(&fmt, sizeof(fmt));
599 fmt.cbSize = sizeof(fmt);
600 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR;
601 fmt.yHeight = cf.iPointSize * 2;
603 if(cf.nFontType & BOLD_FONTTYPE)
604 fmt.dwEffects |= CFE_BOLD;
605 if(cf.nFontType & ITALIC_FONTTYPE)
606 fmt.dwEffects |= CFE_ITALIC;
607 if(cf.lpLogFont->lfUnderline)
608 fmt.dwEffects |= CFE_UNDERLINE;
609 if(cf.lpLogFont->lfStrikeOut)
610 fmt.dwEffects |= CFE_STRIKEOUT;
612 fmt.crTextColor = cf.rgbColors;
614 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
615 set_font(cf.lpLogFont->lfFaceName);
620 static int CALLBACK enum_font_proc(const LOGFONTW *lpelfe, const TEXTMETRICW *lpntme,
621 DWORD FontType, LPARAM lParam)
623 HWND hListWnd = (HWND) lParam;
625 if (lpelfe->lfFaceName[0] == '@') return 1; /* ignore vertical fonts */
627 if(SendMessageW(hListWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)lpelfe->lfFaceName) == CB_ERR)
630 add_font(lpelfe->lfFaceName, FontType, hListWnd, (const NEWTEXTMETRICEXW*)lpntme);
633 return 1;
636 static void populate_font_list(HWND hListWnd)
638 HDC hdc = GetDC(hMainWnd);
639 LOGFONTW fontinfo;
640 HWND hListEditWnd = (HWND)SendMessageW(hListWnd, CBEM_GETEDITCONTROL, 0, 0);
641 CHARFORMAT2W fmt;
643 fontinfo.lfCharSet = DEFAULT_CHARSET;
644 *fontinfo.lfFaceName = '\0';
645 fontinfo.lfPitchAndFamily = 0;
647 EnumFontFamiliesExW(hdc, &fontinfo, enum_font_proc,
648 (LPARAM)hListWnd, 0);
650 ZeroMemory(&fmt, sizeof(fmt));
651 fmt.cbSize = sizeof(fmt);
652 SendMessageW(hEditorWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
653 SendMessageW(hListEditWnd, WM_SETTEXT, 0, (LPARAM)fmt.szFaceName);
656 static void update_window(void)
658 RECT rect;
660 GetClientRect(hMainWnd, &rect);
662 OnSize(hMainWnd, SIZE_RESTORED, MAKELPARAM(rect.right, rect.bottom));
665 static BOOL is_bar_visible(int bandId)
667 return barState[reg_formatindex(fileFormat)] & (1 << bandId);
670 static void store_bar_state(int bandId, BOOL show)
672 int formatIndex = reg_formatindex(fileFormat);
674 if(show)
675 barState[formatIndex] |= (1 << bandId);
676 else
677 barState[formatIndex] &= ~(1 << bandId);
680 static void set_toolbar_state(int bandId, BOOL show)
682 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
684 SendMessageW(hwndReBar, RB_SHOWBAND, SendMessageW(hwndReBar, RB_IDTOINDEX, bandId, 0), show);
686 if(bandId == BANDID_TOOLBAR)
688 REBARBANDINFOW rbbinfo;
689 int index = SendMessageW(hwndReBar, RB_IDTOINDEX, BANDID_FONTLIST, 0);
691 rbbinfo.cbSize = REBARBANDINFOW_V6_SIZE;
692 rbbinfo.fMask = RBBIM_STYLE;
694 SendMessageW(hwndReBar, RB_GETBANDINFOW, index, (LPARAM)&rbbinfo);
696 if(!show)
697 rbbinfo.fStyle &= ~RBBS_BREAK;
698 else
699 rbbinfo.fStyle |= RBBS_BREAK;
701 SendMessageW(hwndReBar, RB_SETBANDINFOW, index, (LPARAM)&rbbinfo);
704 if(bandId == BANDID_TOOLBAR || bandId == BANDID_FORMATBAR || bandId == BANDID_RULER)
705 store_bar_state(bandId, show);
708 static void set_statusbar_state(BOOL show)
710 HWND hStatusWnd = GetDlgItem(hMainWnd, IDC_STATUSBAR);
712 ShowWindow(hStatusWnd, show ? SW_SHOW : SW_HIDE);
713 store_bar_state(BANDID_STATUSBAR, show);
716 static void set_bar_states(void)
718 set_toolbar_state(BANDID_TOOLBAR, is_bar_visible(BANDID_TOOLBAR));
719 set_toolbar_state(BANDID_FONTLIST, is_bar_visible(BANDID_FORMATBAR));
720 set_toolbar_state(BANDID_SIZELIST, is_bar_visible(BANDID_FORMATBAR));
721 set_toolbar_state(BANDID_FORMATBAR, is_bar_visible(BANDID_FORMATBAR));
722 set_toolbar_state(BANDID_RULER, is_bar_visible(BANDID_RULER));
723 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR));
725 update_window();
728 static void preview_exit(HWND hMainWnd)
730 HMENU hMenu = LoadMenuW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDM_MAINMENU));
731 HWND hEditorWnd = GetDlgItem(hMainWnd, IDC_EDITOR);
733 set_bar_states();
734 ShowWindow(hEditorWnd, TRUE);
736 close_preview(hMainWnd);
738 SetMenu(hMainWnd, hMenu);
739 registry_read_filelist(hMainWnd);
741 update_window();
744 static void set_fileformat(WPARAM format)
746 fileFormat = format;
748 set_bar_states();
749 set_default_font();
750 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
753 static void ShowOpenError(DWORD Code)
755 LPWSTR Message;
757 switch(Code)
759 case ERROR_ACCESS_DENIED:
760 Message = MAKEINTRESOURCEW(STRING_OPEN_ACCESS_DENIED);
761 break;
763 default:
764 Message = MAKEINTRESOURCEW(STRING_OPEN_FAILED);
766 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
769 static void DoOpenFile(LPCWSTR szOpenFileName)
771 HANDLE hFile;
772 EDITSTREAM es;
773 char fileStart[5];
774 DWORD readOut;
775 WPARAM format = SF_TEXT;
777 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
778 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
779 if (hFile == INVALID_HANDLE_VALUE)
781 ShowOpenError(GetLastError());
782 return;
785 ReadFile(hFile, fileStart, 5, &readOut, NULL);
786 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
788 if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe)
790 format = SF_TEXT | SF_UNICODE;
791 SetFilePointer(hFile, 2, NULL, FILE_BEGIN);
792 } else if(readOut >= 5)
794 static const char header[] = "{\\rtf";
795 static const BYTE STG_magic[] = { 0xd0,0xcf,0x11,0xe0 };
797 if(!memcmp(header, fileStart, 5))
798 format = SF_RTF;
799 else if (!memcmp(STG_magic, fileStart, sizeof(STG_magic)))
801 CloseHandle(hFile);
802 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_OLE_STORAGE_NOT_SUPPORTED),
803 wszAppTitle, MB_OK | MB_ICONEXCLAMATION);
804 return;
808 es.dwCookie = (DWORD_PTR)hFile;
809 es.pfnCallback = stream_in;
811 clear_formatting();
812 set_fileformat(format);
813 SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es);
815 CloseHandle(hFile);
817 SetFocus(hEditorWnd);
819 set_caption(szOpenFileName);
821 lstrcpyW(wszFileName, szOpenFileName);
822 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
823 registry_set_filelist(szOpenFileName, hMainWnd);
824 update_font_list();
827 static void ShowWriteError(DWORD Code)
829 LPWSTR Message;
831 switch(Code)
833 case ERROR_ACCESS_DENIED:
834 Message = MAKEINTRESOURCEW(STRING_WRITE_ACCESS_DENIED);
835 break;
837 default:
838 Message = MAKEINTRESOURCEW(STRING_WRITE_FAILED);
840 MessageBoxW(hMainWnd, Message, wszAppTitle, MB_ICONEXCLAMATION | MB_OK);
843 static BOOL DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format)
845 HANDLE hFile;
846 EDITSTREAM stream;
847 LRESULT ret;
849 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
850 FILE_ATTRIBUTE_NORMAL, NULL);
852 if(hFile == INVALID_HANDLE_VALUE)
854 ShowWriteError(GetLastError());
855 return FALSE;
858 if(format == (SF_TEXT | SF_UNICODE))
860 static const BYTE unicode[] = {0xff,0xfe};
861 DWORD writeOut;
862 WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0);
864 if(writeOut != sizeof(unicode))
866 CloseHandle(hFile);
867 return FALSE;
871 stream.dwCookie = (DWORD_PTR)hFile;
872 stream.pfnCallback = stream_out;
874 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream);
876 CloseHandle(hFile);
878 SetFocus(hEditorWnd);
880 if(!ret)
882 GETTEXTLENGTHEX gt;
883 gt.flags = GTL_DEFAULT;
884 gt.codepage = 1200;
886 if(SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
887 return FALSE;
890 lstrcpyW(wszFileName, wszSaveFileName);
891 set_caption(wszFileName);
892 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
893 set_fileformat(format);
895 return TRUE;
898 static BOOL DialogSaveFile(void)
900 OPENFILENAMEW sfn;
902 WCHAR wszFile[MAX_PATH] = {'\0'};
903 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
905 ZeroMemory(&sfn, sizeof(sfn));
907 sfn.lStructSize = sizeof(sfn);
908 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
909 sfn.hwndOwner = hMainWnd;
910 sfn.lpstrFilter = wszFilter;
911 sfn.lpstrFile = wszFile;
912 sfn.nMaxFile = MAX_PATH;
913 sfn.lpstrDefExt = wszDefExt;
914 sfn.nFilterIndex = fileformat_number(fileFormat)+1;
916 while(GetSaveFileNameW(&sfn))
918 if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF)
920 if(MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING),
921 wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
922 continue;
924 return DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1));
926 return FALSE;
929 static BOOL prompt_save_changes(void)
931 if(!wszFileName[0])
933 GETTEXTLENGTHEX gt;
934 gt.flags = GTL_NUMCHARS;
935 gt.codepage = 1200;
936 if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
937 return TRUE;
940 if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
942 return TRUE;
943 } else
945 LPWSTR displayFileName;
946 WCHAR *text;
947 int ret;
949 if(!wszFileName[0])
950 displayFileName = wszDefaultFileName;
951 else
952 displayFileName = file_basename(wszFileName);
954 text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
955 (lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
957 if(!text)
958 return FALSE;
960 wsprintfW(text, wszSaveChanges, displayFileName);
962 ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
964 HeapFree(GetProcessHeap(), 0, text);
966 switch(ret)
968 case IDNO:
969 return TRUE;
971 case IDYES:
972 if(wszFileName[0])
973 return DoSaveFile(wszFileName, fileFormat);
974 return DialogSaveFile();
976 default:
977 return FALSE;
982 static void DialogOpenFile(void)
984 OPENFILENAMEW ofn;
986 WCHAR wszFile[MAX_PATH] = {'\0'};
987 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
989 ZeroMemory(&ofn, sizeof(ofn));
991 ofn.lStructSize = sizeof(ofn);
992 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ENABLESIZING;
993 ofn.hwndOwner = hMainWnd;
994 ofn.lpstrFilter = wszFilter;
995 ofn.lpstrFile = wszFile;
996 ofn.nMaxFile = MAX_PATH;
997 ofn.lpstrDefExt = wszDefExt;
998 ofn.nFilterIndex = fileformat_number(fileFormat)+1;
1000 if(GetOpenFileNameW(&ofn))
1002 if(prompt_save_changes())
1003 DoOpenFile(ofn.lpstrFile);
1007 static void dialog_about(void)
1009 HICON icon = LoadImageW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON, 48, 48, LR_SHARED);
1010 ShellAboutW(hMainWnd, wszAppTitle, 0, icon);
1013 static INT_PTR CALLBACK formatopts_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1015 switch(message)
1017 case WM_INITDIALOG:
1019 LPPROPSHEETPAGEW ps = (LPPROPSHEETPAGEW)lParam;
1020 int wrap = -1;
1021 char id[4];
1022 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1024 sprintf(id, "%d\n", (int)ps->lParam);
1025 SetWindowTextA(hIdWnd, id);
1026 if(wordWrap[ps->lParam] == ID_WORDWRAP_NONE)
1027 wrap = IDC_PAGEFMT_WN;
1028 else if(wordWrap[ps->lParam] == ID_WORDWRAP_WINDOW)
1029 wrap = IDC_PAGEFMT_WW;
1030 else if(wordWrap[ps->lParam] == ID_WORDWRAP_MARGIN)
1031 wrap = IDC_PAGEFMT_WM;
1033 if(wrap != -1)
1034 CheckRadioButton(hWnd, IDC_PAGEFMT_WN,
1035 IDC_PAGEFMT_WM, wrap);
1037 if(barState[ps->lParam] & (1 << BANDID_TOOLBAR))
1038 CheckDlgButton(hWnd, IDC_PAGEFMT_TB, TRUE);
1039 if(barState[ps->lParam] & (1 << BANDID_FORMATBAR))
1040 CheckDlgButton(hWnd, IDC_PAGEFMT_FB, TRUE);
1041 if(barState[ps->lParam] & (1 << BANDID_RULER))
1042 CheckDlgButton(hWnd, IDC_PAGEFMT_RU, TRUE);
1043 if(barState[ps->lParam] & (1 << BANDID_STATUSBAR))
1044 CheckDlgButton(hWnd, IDC_PAGEFMT_SB, TRUE);
1046 break;
1048 case WM_COMMAND:
1049 switch(LOWORD(wParam))
1051 case IDC_PAGEFMT_WN:
1052 case IDC_PAGEFMT_WW:
1053 case IDC_PAGEFMT_WM:
1054 CheckRadioButton(hWnd, IDC_PAGEFMT_WN, IDC_PAGEFMT_WM,
1055 LOWORD(wParam));
1056 break;
1058 case IDC_PAGEFMT_TB:
1059 case IDC_PAGEFMT_FB:
1060 case IDC_PAGEFMT_RU:
1061 case IDC_PAGEFMT_SB:
1062 CheckDlgButton(hWnd, LOWORD(wParam),
1063 !IsDlgButtonChecked(hWnd, LOWORD(wParam)));
1064 break;
1066 break;
1067 case WM_NOTIFY:
1069 LPNMHDR header = (LPNMHDR)lParam;
1070 if(header->code == PSN_APPLY)
1072 HWND hIdWnd = GetDlgItem(hWnd, IDC_PAGEFMT_ID);
1073 char sid[4];
1074 int id;
1076 GetWindowTextA(hIdWnd, sid, 4);
1077 id = atoi(sid);
1078 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WN))
1079 wordWrap[id] = ID_WORDWRAP_NONE;
1080 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WW))
1081 wordWrap[id] = ID_WORDWRAP_WINDOW;
1082 else if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_WM))
1083 wordWrap[id] = ID_WORDWRAP_MARGIN;
1085 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_TB))
1086 barState[id] |= (1 << BANDID_TOOLBAR);
1087 else
1088 barState[id] &= ~(1 << BANDID_TOOLBAR);
1090 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_FB))
1091 barState[id] |= (1 << BANDID_FORMATBAR);
1092 else
1093 barState[id] &= ~(1 << BANDID_FORMATBAR);
1095 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_RU))
1096 barState[id] |= (1 << BANDID_RULER);
1097 else
1098 barState[id] &= ~(1 << BANDID_RULER);
1100 if(IsDlgButtonChecked(hWnd, IDC_PAGEFMT_SB))
1101 barState[id] |= (1 << BANDID_STATUSBAR);
1102 else
1103 barState[id] &= ~(1 << BANDID_STATUSBAR);
1106 break;
1108 return FALSE;
1111 static void dialog_viewproperties(void)
1113 PROPSHEETPAGEW psp[2];
1114 PROPSHEETHEADERW psh;
1115 size_t i;
1116 HINSTANCE hInstance = GetModuleHandleW(0);
1117 LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)&psp;
1119 psp[0].dwSize = sizeof(PROPSHEETPAGEW);
1120 psp[0].dwFlags = PSP_USETITLE;
1121 U(psp[0]).pszTemplate = MAKEINTRESOURCEW(IDD_FORMATOPTS);
1122 psp[0].pfnDlgProc = formatopts_proc;
1123 psp[0].hInstance = hInstance;
1124 psp[0].lParam = reg_formatindex(SF_TEXT);
1125 psp[0].pfnCallback = NULL;
1126 psp[0].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT);
1127 for(i = 1; i < sizeof(psp)/sizeof(psp[0]); i++)
1129 psp[i].dwSize = psp[0].dwSize;
1130 psp[i].dwFlags = psp[0].dwFlags;
1131 U(psp[i]).pszTemplate = U(psp[0]).pszTemplate;
1132 psp[i].pfnDlgProc = psp[0].pfnDlgProc;
1133 psp[i].hInstance = psp[0].hInstance;
1134 psp[i].lParam = reg_formatindex(SF_RTF);
1135 psp[i].pfnCallback = psp[0].pfnCallback;
1136 psp[i].pszTitle = MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT);
1139 psh.dwSize = sizeof(psh);
1140 psh.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
1141 psh.hwndParent = hMainWnd;
1142 psh.hInstance = hInstance;
1143 psh.pszCaption = MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE);
1144 psh.nPages = sizeof(psp)/sizeof(psp[0]);
1145 U3(psh).ppsp = ppsp;
1146 U(psh).pszIcon = MAKEINTRESOURCEW(IDI_WORDPAD);
1148 if(fileFormat & SF_RTF)
1149 U2(psh).nStartPage = 1;
1150 else
1151 U2(psh).nStartPage = 0;
1152 PropertySheetW(&psh);
1153 set_bar_states();
1154 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
1157 static void HandleCommandLine(LPWSTR cmdline)
1159 WCHAR delimiter;
1160 BOOL opt_print = FALSE;
1162 /* skip white space */
1163 while (*cmdline == ' ') cmdline++;
1165 /* skip executable name */
1166 delimiter = (*cmdline == '"' ? '"' : ' ');
1168 if (*cmdline == delimiter) cmdline++;
1169 while (*cmdline && *cmdline != delimiter) cmdline++;
1170 if (*cmdline == delimiter) cmdline++;
1172 while (*cmdline)
1174 while (*cmdline == ' ' || *cmdline == '\t') cmdline++;
1176 if (*cmdline == '-' || *cmdline == '/')
1178 if (!cmdline[2] || isspace(cmdline[2]))
1180 switch (cmdline[1])
1182 case 'P':
1183 case 'p':
1184 opt_print = TRUE;
1185 cmdline += 2;
1186 continue;
1189 /* a filename starting by / */
1191 break;
1194 if (*cmdline)
1196 /* file name is passed on the command line */
1197 if (cmdline[0] == '"')
1199 cmdline++;
1200 cmdline[lstrlenW(cmdline) - 1] = 0;
1202 DoOpenFile(cmdline);
1203 InvalidateRect(hMainWnd, NULL, FALSE);
1206 if (opt_print)
1207 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_PRINTING_NOT_IMPLEMENTED), wszAppTitle, MB_OK);
1210 static LRESULT handle_findmsg(LPFINDREPLACEW pFr)
1212 if(pFr->Flags & FR_DIALOGTERM)
1214 hFindWnd = 0;
1215 pFr->Flags = FR_FINDNEXT;
1216 return 0;
1219 if(pFr->Flags & FR_FINDNEXT || pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL)
1221 FINDREPLACE_custom *custom_data = (FINDREPLACE_custom*)pFr->lCustData;
1222 DWORD flags;
1223 FINDTEXTEXW ft;
1224 CHARRANGE sel;
1225 LRESULT ret = -1;
1226 HMENU hMenu = GetMenu(hMainWnd);
1227 MENUITEMINFOW mi;
1229 mi.cbSize = sizeof(mi);
1230 mi.fMask = MIIM_DATA;
1231 mi.dwItemData = 1;
1232 SetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
1234 /* Make sure find field is saved. */
1235 if (pFr->lpstrFindWhat != custom_data->findBuffer)
1237 lstrcpynW(custom_data->findBuffer, pFr->lpstrFindWhat,
1238 sizeof(custom_data->findBuffer) / sizeof(WCHAR));
1239 pFr->lpstrFindWhat = custom_data->findBuffer;
1242 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1243 if(custom_data->endPos == -1) {
1244 custom_data->endPos = sel.cpMin;
1245 custom_data->wrapped = FALSE;
1248 flags = FR_DOWN | (pFr->Flags & (FR_MATCHCASE | FR_WHOLEWORD));
1249 ft.lpstrText = pFr->lpstrFindWhat;
1251 /* Only replace the existing selection if it is an exact match. */
1252 if (sel.cpMin != sel.cpMax &&
1253 (pFr->Flags & FR_REPLACE || pFr->Flags & FR_REPLACEALL))
1255 ft.chrg = sel;
1256 SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1257 if (ft.chrgText.cpMin == sel.cpMin && ft.chrgText.cpMax == sel.cpMax) {
1258 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)pFr->lpstrReplaceWith);
1259 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&sel.cpMin, (LPARAM)&sel.cpMax);
1263 /* Search from the start of the selection, but exclude the first character
1264 * from search if there is a selection. */
1265 ft.chrg.cpMin = sel.cpMin;
1266 if (sel.cpMin != sel.cpMax)
1267 ft.chrg.cpMin++;
1269 /* Search to the end, then wrap around and search from the start. */
1270 if (!custom_data->wrapped) {
1271 ft.chrg.cpMax = -1;
1272 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1273 if (ret == -1) {
1274 custom_data->wrapped = TRUE;
1275 ft.chrg.cpMin = 0;
1279 if (ret == -1) {
1280 ft.chrg.cpMax = custom_data->endPos + lstrlenW(pFr->lpstrFindWhat) - 1;
1281 if (ft.chrg.cpMax > ft.chrg.cpMin)
1282 ret = SendMessageW(hEditorWnd, EM_FINDTEXTEXW, flags, (LPARAM)&ft);
1285 if (ret == -1) {
1286 custom_data->endPos = -1;
1287 EnableWindow(hMainWnd, FALSE);
1288 MessageBoxWithResStringW(hFindWnd, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED),
1289 wszAppTitle, MB_OK | MB_ICONASTERISK | MB_TASKMODAL);
1290 EnableWindow(hMainWnd, TRUE);
1291 } else {
1292 SendMessageW(hEditorWnd, EM_SETSEL, ft.chrgText.cpMin, ft.chrgText.cpMax);
1293 SendMessageW(hEditorWnd, EM_SCROLLCARET, 0, 0);
1295 if (pFr->Flags & FR_REPLACEALL)
1296 return handle_findmsg(pFr);
1300 return 0;
1303 static void dialog_find(LPFINDREPLACEW fr, BOOL replace)
1305 static WCHAR selBuffer[128];
1306 static WCHAR replaceBuffer[128];
1307 static FINDREPLACE_custom custom_data;
1308 static const WCHAR endl = '\r';
1309 FINDTEXTW ft;
1311 /* Allow only one search/replace dialog to open */
1312 if(hFindWnd != NULL)
1314 SetActiveWindow(hFindWnd);
1315 return;
1318 ZeroMemory(fr, sizeof(FINDREPLACEW));
1319 fr->lStructSize = sizeof(FINDREPLACEW);
1320 fr->hwndOwner = hMainWnd;
1321 fr->Flags = FR_HIDEUPDOWN;
1322 /* Find field is filled with the selected text if it is non-empty
1323 * and stays within the same paragraph, otherwise the previous
1324 * find field is used. */
1325 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&ft.chrg.cpMin,
1326 (LPARAM)&ft.chrg.cpMax);
1327 ft.lpstrText = &endl;
1328 if (ft.chrg.cpMin != ft.chrg.cpMax &&
1329 SendMessageW(hEditorWnd, EM_FINDTEXTW, FR_DOWN, (LPARAM)&ft) == -1)
1331 /* Use a temporary buffer for the selected text so that the saved
1332 * find field is only overwritten when a find/replace is clicked. */
1333 GETTEXTEX gt = {sizeof(selBuffer), GT_SELECTION, 1200, NULL, NULL};
1334 SendMessageW(hEditorWnd, EM_GETTEXTEX, (WPARAM)&gt, (LPARAM)selBuffer);
1335 fr->lpstrFindWhat = selBuffer;
1336 } else {
1337 fr->lpstrFindWhat = custom_data.findBuffer;
1339 fr->lpstrReplaceWith = replaceBuffer;
1340 custom_data.endPos = -1;
1341 custom_data.wrapped = FALSE;
1342 fr->lCustData = (LPARAM)&custom_data;
1343 fr->wFindWhatLen = sizeof(custom_data.findBuffer);
1344 fr->wReplaceWithLen = sizeof(replaceBuffer);
1346 if(replace)
1347 hFindWnd = ReplaceTextW(fr);
1348 else
1349 hFindWnd = FindTextW(fr);
1352 static int units_to_twips(UNIT unit, float number)
1354 int twips = 0;
1356 switch(unit)
1358 case UNIT_CM:
1359 twips = (int)(number * 1000.0 / (float)CENTMM_PER_INCH * (float)TWIPS_PER_INCH);
1360 break;
1362 case UNIT_INCH:
1363 twips = (int)(number * (float)TWIPS_PER_INCH);
1364 break;
1366 case UNIT_PT:
1367 twips = (int)(number * (0.0138 * (float)TWIPS_PER_INCH));
1368 break;
1371 return twips;
1374 static void append_current_units(LPWSTR buffer)
1376 static const WCHAR space[] = {' ', 0};
1377 lstrcatW(buffer, space);
1378 lstrcatW(buffer, units_cmW);
1381 static void number_with_units(LPWSTR buffer, int number)
1383 static const WCHAR fmt[] = {'%','.','2','f',' ','%','s','\0'};
1384 float converted = (float)number / (float)TWIPS_PER_INCH *(float)CENTMM_PER_INCH / 1000.0;
1386 sprintfW(buffer, fmt, converted, units_cmW);
1389 static BOOL get_comboexlist_selection(HWND hComboEx, LPWSTR wszBuffer, UINT bufferLength)
1391 COMBOBOXEXITEMW cbItem;
1392 COMBOBOXINFO cbInfo;
1393 HWND hCombo, hList;
1394 int idx, result;
1396 hCombo = (HWND)SendMessageW(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
1397 if (!hCombo)
1398 return FALSE;
1399 cbInfo.cbSize = sizeof(COMBOBOXINFO);
1400 result = SendMessageW(hCombo, CB_GETCOMBOBOXINFO, 0, (LPARAM)&cbInfo);
1401 if (!result)
1402 return FALSE;
1403 hList = cbInfo.hwndList;
1404 idx = SendMessageW(hList, LB_GETCURSEL, 0, 0);
1405 if (idx < 0)
1406 return FALSE;
1408 ZeroMemory(&cbItem, sizeof(cbItem));
1409 cbItem.mask = CBEIF_TEXT;
1410 cbItem.iItem = idx;
1411 cbItem.pszText = wszBuffer;
1412 cbItem.cchTextMax = bufferLength-1;
1413 result = SendMessageW(hComboEx, CBEM_GETITEMW, 0, (LPARAM)&cbItem);
1415 return result != 0;
1418 static INT_PTR CALLBACK datetime_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1420 switch(message)
1422 case WM_INITDIALOG:
1424 WCHAR buffer[MAX_STRING_LEN];
1425 SYSTEMTIME st;
1426 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1427 GetLocalTime(&st);
1429 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, 0, (LPWSTR)&buffer,
1430 MAX_STRING_LEN);
1431 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1432 GetDateFormatW(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, 0, (LPWSTR)&buffer,
1433 MAX_STRING_LEN);
1434 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1435 GetTimeFormatW(LOCALE_USER_DEFAULT, 0, &st, 0, (LPWSTR)&buffer, MAX_STRING_LEN);
1436 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1438 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1440 break;
1442 case WM_COMMAND:
1443 switch(LOWORD(wParam))
1445 case IDC_DATETIME:
1446 if (HIWORD(wParam) != LBN_DBLCLK)
1447 break;
1448 /* Fall through */
1450 case IDOK:
1452 LRESULT index;
1453 HWND hListWnd = GetDlgItem(hWnd, IDC_DATETIME);
1455 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1457 if(index != LB_ERR)
1459 WCHAR buffer[MAX_STRING_LEN];
1460 SendMessageW(hListWnd, LB_GETTEXT, index, (LPARAM)&buffer);
1461 SendMessageW(hEditorWnd, EM_REPLACESEL, TRUE, (LPARAM)&buffer);
1464 /* Fall through */
1466 case IDCANCEL:
1467 EndDialog(hWnd, wParam);
1468 return TRUE;
1471 return FALSE;
1474 static INT_PTR CALLBACK newfile_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1476 switch(message)
1478 case WM_INITDIALOG:
1480 HINSTANCE hInstance = GetModuleHandleW(0);
1481 WCHAR buffer[MAX_STRING_LEN];
1482 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1484 LoadStringW(hInstance, STRING_NEWFILE_RICHTEXT, buffer, MAX_STRING_LEN);
1485 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1486 LoadStringW(hInstance, STRING_NEWFILE_TXT, buffer, MAX_STRING_LEN);
1487 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1488 LoadStringW(hInstance, STRING_NEWFILE_TXT_UNICODE, buffer, MAX_STRING_LEN);
1489 SendMessageW(hListWnd, LB_ADDSTRING, 0, (LPARAM)&buffer);
1491 SendMessageW(hListWnd, LB_SETSEL, TRUE, 0);
1493 break;
1495 case WM_COMMAND:
1496 switch(LOWORD(wParam))
1498 case IDOK:
1500 LRESULT index;
1501 HWND hListWnd = GetDlgItem(hWnd, IDC_NEWFILE);
1502 index = SendMessageW(hListWnd, LB_GETCURSEL, 0, 0);
1504 if(index != LB_ERR)
1505 EndDialog(hWnd, MAKELONG(fileformat_flags(index),0));
1507 return TRUE;
1509 case IDCANCEL:
1510 EndDialog(hWnd, MAKELONG(ID_NEWFILE_ABORT,0));
1511 return TRUE;
1514 return FALSE;
1517 static INT_PTR CALLBACK paraformat_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1519 static const WORD ALIGNMENT_VALUES[] = {PFA_LEFT, PFA_RIGHT, PFA_CENTER};
1521 switch(message)
1523 case WM_INITDIALOG:
1525 HINSTANCE hInstance = GetModuleHandleW(0);
1526 WCHAR buffer[MAX_STRING_LEN];
1527 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1528 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1529 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1530 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1531 PARAFORMAT2 pf;
1532 int index = 0;
1534 LoadStringW(hInstance, STRING_ALIGN_LEFT, buffer,
1535 MAX_STRING_LEN);
1536 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1537 LoadStringW(hInstance, STRING_ALIGN_RIGHT, buffer,
1538 MAX_STRING_LEN);
1539 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1540 LoadStringW(hInstance, STRING_ALIGN_CENTER, buffer,
1541 MAX_STRING_LEN);
1542 SendMessageW(hListWnd, CB_ADDSTRING, 0, (LPARAM)buffer);
1544 pf.cbSize = sizeof(pf);
1545 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1546 PFM_STARTINDENT;
1547 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1549 if(pf.wAlignment == PFA_RIGHT)
1550 index ++;
1551 else if(pf.wAlignment == PFA_CENTER)
1552 index += 2;
1554 SendMessageW(hListWnd, CB_SETCURSEL, index, 0);
1556 number_with_units(buffer, pf.dxStartIndent + pf.dxOffset);
1557 SetWindowTextW(hLeftWnd, buffer);
1558 number_with_units(buffer, pf.dxRightIndent);
1559 SetWindowTextW(hRightWnd, buffer);
1560 number_with_units(buffer, -pf.dxOffset);
1561 SetWindowTextW(hFirstWnd, buffer);
1563 break;
1565 case WM_COMMAND:
1566 switch(LOWORD(wParam))
1568 case IDOK:
1570 HWND hListWnd = GetDlgItem(hWnd, IDC_PARA_ALIGN);
1571 HWND hLeftWnd = GetDlgItem(hWnd, IDC_PARA_LEFT);
1572 HWND hRightWnd = GetDlgItem(hWnd, IDC_PARA_RIGHT);
1573 HWND hFirstWnd = GetDlgItem(hWnd, IDC_PARA_FIRST);
1574 WCHAR buffer[MAX_STRING_LEN];
1575 int index;
1576 float num;
1577 int ret = 0;
1578 PARAFORMAT2 pf;
1579 UNIT unit;
1580 BOOL in_list = FALSE;
1582 pf.cbSize = sizeof(pf);
1583 pf.dwMask = PFM_NUMBERING;
1584 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1585 if ((pf.dwMask & PFM_NUMBERING) && pf.wNumbering)
1586 in_list = TRUE;
1588 index = SendMessageW(hListWnd, CB_GETCURSEL, 0, 0);
1589 pf.wAlignment = ALIGNMENT_VALUES[index];
1591 GetWindowTextW(hLeftWnd, buffer, MAX_STRING_LEN);
1592 if(number_from_string(buffer, &num, &unit))
1593 ret++;
1594 pf.dxOffset = units_to_twips(unit, num);
1595 GetWindowTextW(hRightWnd, buffer, MAX_STRING_LEN);
1596 if(number_from_string(buffer, &num, &unit))
1597 ret++;
1598 pf.dxRightIndent = units_to_twips(unit, num);
1599 GetWindowTextW(hFirstWnd, buffer, MAX_STRING_LEN);
1600 if(number_from_string(buffer, &num, &unit))
1601 ret++;
1602 pf.dxStartIndent = units_to_twips(unit, num);
1604 if(ret != 3)
1606 MessageBoxWithResStringW(hMainWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1607 wszAppTitle, MB_OK | MB_ICONASTERISK);
1608 return FALSE;
1609 } else
1611 if (pf.dxOffset + pf.dxStartIndent < 0
1612 && pf.dxStartIndent < 0)
1614 /* The first line is before the left edge, so
1615 * make sure it is at the left edge. */
1616 pf.dxOffset = -pf.dxStartIndent;
1617 } else if (pf.dxOffset < 0) {
1618 /* The second and following lines are before
1619 * the left edge, so set it to be at the left
1620 * edge, and adjust the first line since it
1621 * is relative to it. */
1622 pf.dxStartIndent = max(pf.dxStartIndent + pf.dxOffset, 0);
1623 pf.dxOffset = 0;
1625 /* Internally the dxStartIndent is the absolute
1626 * offset for the first line and dxOffset is
1627 * to it value as opposed how it is displayed with
1628 * the first line being the relative value.
1629 * These two lines make the adjustments. */
1630 pf.dxStartIndent = pf.dxStartIndent + pf.dxOffset;
1631 pf.dxOffset = pf.dxOffset - pf.dxStartIndent;
1633 pf.cbSize = sizeof(pf);
1634 pf.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_RIGHTINDENT |
1635 PFM_STARTINDENT;
1636 if (in_list)
1638 pf.wNumberingTab = max(pf.dxOffset, 0);
1639 pf.dwMask |= PFM_NUMBERINGTAB;
1642 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1645 /* Fall through */
1647 case IDCANCEL:
1648 EndDialog(hWnd, wParam);
1649 return TRUE;
1652 return FALSE;
1655 static INT_PTR CALLBACK tabstops_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
1657 switch(message)
1659 case WM_INITDIALOG:
1661 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1662 PARAFORMAT pf;
1663 WCHAR buffer[MAX_STRING_LEN];
1664 int i;
1666 pf.cbSize = sizeof(pf);
1667 pf.dwMask = PFM_TABSTOPS;
1668 SendMessageW(hEditorWnd, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
1669 SendMessageW(hTabWnd, CB_LIMITTEXT, MAX_STRING_LEN-1, 0);
1671 for(i = 0; i < pf.cTabCount; i++)
1673 number_with_units(buffer, pf.rgxTabs[i]);
1674 SendMessageW(hTabWnd, CB_ADDSTRING, 0, (LPARAM)&buffer);
1676 SetFocus(hTabWnd);
1678 break;
1680 case WM_COMMAND:
1681 switch(LOWORD(wParam))
1683 case IDC_TABSTOPS:
1685 HWND hTabWnd = (HWND)lParam;
1686 HWND hAddWnd = GetDlgItem(hWnd, ID_TAB_ADD);
1687 HWND hDelWnd = GetDlgItem(hWnd, ID_TAB_DEL);
1688 HWND hEmptyWnd = GetDlgItem(hWnd, ID_TAB_EMPTY);
1690 if(GetWindowTextLengthW(hTabWnd))
1691 EnableWindow(hAddWnd, TRUE);
1692 else
1693 EnableWindow(hAddWnd, FALSE);
1695 if(SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0))
1697 EnableWindow(hEmptyWnd, TRUE);
1699 if(SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0) == CB_ERR)
1700 EnableWindow(hDelWnd, FALSE);
1701 else
1702 EnableWindow(hDelWnd, TRUE);
1703 } else
1705 EnableWindow(hEmptyWnd, FALSE);
1708 break;
1710 case ID_TAB_ADD:
1712 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1713 WCHAR buffer[MAX_STRING_LEN];
1714 UNIT unit;
1716 GetWindowTextW(hTabWnd, buffer, MAX_STRING_LEN);
1717 append_current_units(buffer);
1719 if(SendMessageW(hTabWnd, CB_FINDSTRINGEXACT, -1, (LPARAM)&buffer) == CB_ERR)
1721 float number = 0;
1722 int item_count = SendMessageW(hTabWnd, CB_GETCOUNT, 0, 0);
1724 if(!number_from_string(buffer, &number, &unit))
1726 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_INVALID_NUMBER),
1727 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1728 } else if (item_count >= MAX_TAB_STOPS) {
1729 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_MAX_TAB_STOPS),
1730 wszAppTitle, MB_OK | MB_ICONINFORMATION);
1731 } else {
1732 int i;
1733 float next_number = -1;
1734 int next_number_in_twips = -1;
1735 int insert_number = units_to_twips(unit, number);
1737 /* linear search for position to insert the string */
1738 for(i = 0; i < item_count; i++)
1740 SendMessageW(hTabWnd, CB_GETLBTEXT, i, (LPARAM)&buffer);
1741 number_from_string(buffer, &next_number, &unit);
1742 next_number_in_twips = units_to_twips(unit, next_number);
1743 if (insert_number <= next_number_in_twips)
1744 break;
1746 if (insert_number != next_number_in_twips)
1748 number_with_units(buffer, insert_number);
1749 SendMessageW(hTabWnd, CB_INSERTSTRING, i, (LPARAM)&buffer);
1750 SetWindowTextW(hTabWnd, 0);
1754 SetFocus(hTabWnd);
1756 break;
1758 case ID_TAB_DEL:
1760 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1761 LRESULT ret;
1762 ret = SendMessageW(hTabWnd, CB_GETCURSEL, 0, 0);
1763 if(ret != CB_ERR)
1764 SendMessageW(hTabWnd, CB_DELETESTRING, ret, 0);
1766 break;
1768 case ID_TAB_EMPTY:
1770 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1771 SendMessageW(hTabWnd, CB_RESETCONTENT, 0, 0);
1772 SetFocus(hTabWnd);
1774 break;
1776 case IDOK:
1778 HWND hTabWnd = GetDlgItem(hWnd, IDC_TABSTOPS);
1779 int i;
1780 WCHAR buffer[MAX_STRING_LEN];
1781 PARAFORMAT pf;
1782 float number;
1783 UNIT unit;
1785 pf.cbSize = sizeof(pf);
1786 pf.dwMask = PFM_TABSTOPS;
1788 for(i = 0; SendMessageW(hTabWnd, CB_GETLBTEXT, i,
1789 (LPARAM)&buffer) != CB_ERR &&
1790 i < MAX_TAB_STOPS; i++)
1792 number_from_string(buffer, &number, &unit);
1793 pf.rgxTabs[i] = units_to_twips(unit, number);
1795 pf.cTabCount = i;
1796 SendMessageW(hEditorWnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
1798 /* Fall through */
1799 case IDCANCEL:
1800 EndDialog(hWnd, wParam);
1801 return TRUE;
1804 return FALSE;
1807 static LRESULT OnCreate( HWND hWnd )
1809 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd, hFontListWnd, hSizeListWnd, hRulerWnd;
1810 HINSTANCE hInstance = GetModuleHandleW(0);
1811 HANDLE hDLL;
1812 TBADDBITMAP ab;
1813 int nStdBitmaps = 0;
1814 REBARINFO rbi;
1815 REBARBANDINFOW rbb;
1816 RECT rect;
1817 HFONT font;
1818 HDC hdc;
1819 SIZE name_sz, size_sz;
1820 int height;
1821 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
1822 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
1823 static const WCHAR font_text[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0}; /* a long font name */
1824 static const WCHAR size_text[] = {' ','0','0',0}; /* enough for two digits */
1826 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
1828 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
1829 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
1830 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
1832 rbi.cbSize = sizeof(rbi);
1833 rbi.fMask = 0;
1834 rbi.himl = NULL;
1835 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
1836 return -1;
1838 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS,
1839 IDC_TOOLBAR,
1840 1, hInstance, IDB_TOOLBAR,
1841 NULL, 0,
1842 24, 24, 16, 16, sizeof(TBBUTTON));
1844 ab.hInst = HINST_COMMCTRL;
1845 ab.nID = IDB_STD_SMALL_COLOR;
1846 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
1848 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
1849 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
1850 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
1851 AddSeparator(hToolBarWnd);
1852 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT_QUICK);
1853 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
1854 AddSeparator(hToolBarWnd);
1855 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
1856 AddSeparator(hToolBarWnd);
1857 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
1858 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
1859 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
1860 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
1861 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
1862 AddSeparator(hToolBarWnd);
1863 AddButton(hToolBarWnd, 0, ID_DATETIME);
1865 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
1866 height = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
1868 hFontListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1869 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_SORT,
1870 0, 0, 200, 150, hReBarWnd, (HMENU)IDC_FONTLIST, hInstance, NULL);
1871 GetWindowRect(hFontListWnd, &rect);
1872 height = max(height, rect.bottom - rect.top);
1874 rbb.cbSize = REBARBANDINFOW_V6_SIZE;
1875 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE | RBBIM_ID;
1876 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
1877 rbb.cx = 0;
1878 rbb.hwndChild = hToolBarWnd;
1879 rbb.cxMinChild = 0;
1880 rbb.cyChild = rbb.cyMinChild = height;
1881 rbb.wID = BANDID_TOOLBAR;
1883 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1885 font = (HFONT)SendMessageW(hFontListWnd, WM_GETFONT, 0, 0);
1886 hdc = GetDC(hFontListWnd);
1887 font = SelectObject(hdc, font);
1888 GetTextExtentPointW(hdc, font_text, sizeof(font_text) / sizeof(font_text[0]) - 1, &name_sz);
1889 GetTextExtentPointW(hdc, size_text, sizeof(size_text) / sizeof(size_text[0]) - 1, &size_sz);
1890 font = SelectObject(hdc, font);
1891 ReleaseDC(hFontListWnd, hdc);
1892 rbb.hwndChild = hFontListWnd;
1893 rbb.cx = MulDiv(name_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1894 rbb.wID = BANDID_FONTLIST;
1896 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1898 hSizeListWnd = CreateWindowExW(0, WC_COMBOBOXEXW, NULL,
1899 WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN,
1900 0, 0, 50, 150, hReBarWnd, (HMENU)IDC_SIZELIST, hInstance, NULL);
1902 rbb.hwndChild = hSizeListWnd;
1903 rbb.cx = MulDiv(size_sz.cx, 3, 2) + height; /* height is space for the dropdown arrow */
1904 rbb.fStyle ^= RBBS_BREAK;
1905 rbb.wID = BANDID_SIZELIST;
1907 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1909 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
1910 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS,
1911 IDC_FORMATBAR, 8, hInstance, IDB_FORMATBAR, NULL, 0, 16, 16, 16, 16, sizeof(TBBUTTON));
1913 SendMessageW(hFormatBarWnd, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_DRAWDDARROWS);
1915 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
1916 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
1917 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
1918 AddButton(hFormatBarWnd, 3, ID_FORMAT_COLOR);
1919 AddSeparator(hFormatBarWnd);
1920 AddButton(hFormatBarWnd, 4, ID_ALIGN_LEFT);
1921 AddButton(hFormatBarWnd, 5, ID_ALIGN_CENTER);
1922 AddButton(hFormatBarWnd, 6, ID_ALIGN_RIGHT);
1923 AddSeparator(hFormatBarWnd);
1924 AddButtonStyle(hFormatBarWnd, 7, ID_BULLETONOFF, BTNS_DROPDOWN);
1926 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
1928 rbb.hwndChild = hFormatBarWnd;
1929 rbb.wID = BANDID_FORMATBAR;
1931 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1933 hRulerWnd = CreateWindowExW(0, WC_STATICW, NULL, WS_VISIBLE | WS_CHILD,
1934 0, 0, 200, 10, hReBarWnd, (HMENU)IDC_RULER, hInstance, NULL);
1937 rbb.hwndChild = hRulerWnd;
1938 rbb.wID = BANDID_RULER;
1939 rbb.fStyle |= RBBS_BREAK;
1941 SendMessageW(hReBarWnd, RB_INSERTBANDW, -1, (LPARAM)&rbb);
1943 hDLL = LoadLibraryW(wszRichEditDll);
1944 if(!hDLL)
1946 MessageBoxWithResStringW(hWnd, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED), wszAppTitle,
1947 MB_OK | MB_ICONEXCLAMATION);
1948 PostQuitMessage(1);
1951 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, RICHEDIT_CLASS20W, NULL,
1952 WS_CHILD|WS_VISIBLE|ES_SELECTIONBAR|ES_MULTILINE|ES_AUTOVSCROLL
1953 |ES_WANTRETURN|WS_VSCROLL|ES_NOHIDESEL|WS_HSCROLL,
1954 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
1956 if (!hEditorWnd)
1958 fprintf(stderr, "Error code %u\n", GetLastError());
1959 return -1;
1961 assert(hEditorWnd);
1963 setup_richedit_olecallback(hEditorWnd);
1964 SetFocus(hEditorWnd);
1965 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
1967 set_default_font();
1969 populate_font_list(hFontListWnd);
1970 populate_size_list(hSizeListWnd);
1971 DoLoadStrings();
1972 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
1974 ID_FINDMSGSTRING = RegisterWindowMessageW(FINDMSGSTRINGW);
1976 registry_read_filelist(hWnd);
1977 registry_read_formatopts_all(barState, wordWrap);
1978 registry_read_options();
1979 DragAcceptFiles(hWnd, TRUE);
1981 return 0;
1984 static LRESULT OnUser( HWND hWnd )
1986 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
1987 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
1988 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
1989 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
1990 int from, to;
1991 CHARFORMAT2W fmt;
1992 PARAFORMAT2 pf;
1993 GETTEXTLENGTHEX gt;
1995 ZeroMemory(&fmt, sizeof(fmt));
1996 fmt.cbSize = sizeof(fmt);
1998 ZeroMemory(&pf, sizeof(pf));
1999 pf.cbSize = sizeof(pf);
2001 gt.flags = GTL_NUMCHARS;
2002 gt.codepage = 1200;
2004 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_FIND,
2005 SendMessageW(hwndEditor, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0) ? 1 : 0);
2007 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
2009 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
2010 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
2011 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
2012 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
2013 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
2014 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
2015 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
2017 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
2018 (fmt.dwEffects & CFE_BOLD));
2019 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
2020 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
2021 (fmt.dwEffects & CFE_ITALIC));
2022 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
2023 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
2024 (fmt.dwEffects & CFE_UNDERLINE));
2025 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
2027 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2028 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
2029 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
2030 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
2032 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_BULLETONOFF, pf.wNumbering != 0);
2033 return 0;
2036 static LRESULT OnNotify( HWND hWnd, LPARAM lParam)
2038 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2039 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2040 NMHDR *pHdr = (NMHDR *)lParam;
2041 HWND hwndFontList = GetDlgItem(hwndReBar, IDC_FONTLIST);
2042 HWND hwndSizeList = GetDlgItem(hwndReBar, IDC_SIZELIST);
2043 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
2045 if (pHdr->hwndFrom == hwndFontList || pHdr->hwndFrom == hwndSizeList)
2047 if (pHdr->code == CBEN_ENDEDITW)
2049 NMCBEENDEDITW *endEdit = (NMCBEENDEDITW *)lParam;
2050 if(pHdr->hwndFrom == hwndFontList)
2052 on_fontlist_modified(endEdit->szText);
2053 } else if (pHdr->hwndFrom == hwndSizeList)
2055 on_sizelist_modified(hwndSizeList,endEdit->szText);
2058 return 0;
2061 if (pHdr->hwndFrom == hwndFormatBar)
2063 if (pHdr->code == TBN_DROPDOWN)
2065 NMTOOLBARW *tb_notify = (NMTOOLBARW *)lParam;
2066 HMENU menu = GetMenu( hWnd );
2067 MENUITEMINFOW info;
2068 TPMPARAMS params;
2069 RECT rc;
2071 if (!menu) return 0;
2072 info.cbSize = sizeof(info);
2073 info.fMask = MIIM_SUBMENU;
2074 GetMenuItemInfoW( menu, ID_LISTMENU, FALSE, &info );
2075 if (!info.hSubMenu) return 0;
2077 SendMessageW( tb_notify->hdr.hwndFrom, TB_GETRECT, (WPARAM)tb_notify->iItem, (LPARAM)&rc );
2078 MapWindowPoints( tb_notify->hdr.hwndFrom, HWND_DESKTOP, (LPPOINT)&rc, 2 );
2080 params.cbSize = sizeof(params);
2081 params.rcExclude = rc;
2082 TrackPopupMenuEx( info.hSubMenu,
2083 TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
2084 rc.left, rc.bottom, hWnd, &params );
2087 return 0;
2090 if (pHdr->hwndFrom == hwndEditor)
2092 if (pHdr->code == EN_SELCHANGE)
2094 SELCHANGE *pSC = (SELCHANGE *)lParam;
2095 char buf[128];
2097 update_font_list();
2099 sprintf( buf,"selection = %d..%d, line count=%ld",
2100 pSC->chrg.cpMin, pSC->chrg.cpMax,
2101 SendMessageW(hwndEditor, EM_GETLINECOUNT, 0, 0));
2102 SetWindowTextA(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
2103 SendMessageW(hWnd, WM_USER, 0, 0);
2104 return 1;
2107 return 0;
2110 /* Copied from dlls/comdlg32/fontdlg.c */
2111 static const COLORREF textcolors[]=
2113 0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2114 0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2115 0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2116 0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2119 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
2121 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2122 static FINDREPLACEW findreplace;
2124 if ((HWND)lParam == hwndEditor)
2125 return 0;
2127 switch(LOWORD(wParam))
2130 case ID_FILE_EXIT:
2131 PostMessageW(hWnd, WM_CLOSE, 0, 0);
2132 break;
2134 case ID_FILE_NEW:
2136 HINSTANCE hInstance = GetModuleHandleW(0);
2137 int ret = DialogBoxW(hInstance, MAKEINTRESOURCEW(IDD_NEWFILE), hWnd, newfile_proc);
2139 if(ret != ID_NEWFILE_ABORT)
2141 if(prompt_save_changes())
2143 SETTEXTEX st;
2145 set_caption(NULL);
2146 wszFileName[0] = '\0';
2148 clear_formatting();
2150 st.flags = ST_DEFAULT;
2151 st.codepage = 1200;
2152 SendMessageW(hEditorWnd, EM_SETTEXTEX, (WPARAM)&st, 0);
2154 SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
2155 set_fileformat(ret);
2156 update_font_list();
2160 break;
2162 case ID_FILE_OPEN:
2163 DialogOpenFile();
2164 break;
2166 case ID_FILE_SAVE:
2167 if(wszFileName[0])
2169 DoSaveFile(wszFileName, fileFormat);
2170 break;
2172 /* Fall through */
2174 case ID_FILE_SAVEAS:
2175 DialogSaveFile();
2176 break;
2178 case ID_FILE_RECENT1:
2179 case ID_FILE_RECENT2:
2180 case ID_FILE_RECENT3:
2181 case ID_FILE_RECENT4:
2183 HMENU hMenu = GetMenu(hWnd);
2184 MENUITEMINFOW mi;
2186 mi.cbSize = sizeof(MENUITEMINFOW);
2187 mi.fMask = MIIM_DATA;
2188 if(GetMenuItemInfoW(hMenu, LOWORD(wParam), FALSE, &mi))
2189 DoOpenFile((LPWSTR)mi.dwItemData);
2191 break;
2193 case ID_FIND:
2194 dialog_find(&findreplace, FALSE);
2195 break;
2197 case ID_FIND_NEXT:
2198 handle_findmsg(&findreplace);
2199 break;
2201 case ID_REPLACE:
2202 dialog_find(&findreplace, TRUE);
2203 break;
2205 case ID_FONTSETTINGS:
2206 dialog_choose_font();
2207 break;
2209 case ID_PRINT:
2210 dialog_print(hWnd, wszFileName);
2211 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2212 break;
2214 case ID_PRINT_QUICK:
2215 print_quick(hMainWnd, wszFileName);
2216 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2217 break;
2219 case ID_PREVIEW:
2221 int index = reg_formatindex(fileFormat);
2222 DWORD tmp = barState[index];
2223 barState[index] = 1 << BANDID_STATUSBAR;
2224 set_bar_states();
2225 barState[index] = tmp;
2226 ShowWindow(hEditorWnd, FALSE);
2228 init_preview(hWnd, wszFileName);
2230 SetMenu(hWnd, NULL);
2231 InvalidateRect(0, 0, TRUE);
2233 break;
2235 case ID_PRINTSETUP:
2236 dialog_printsetup(hWnd);
2237 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2238 break;
2240 case ID_FORMAT_BOLD:
2241 case ID_FORMAT_ITALIC:
2242 case ID_FORMAT_UNDERLINE:
2244 CHARFORMAT2W fmt;
2245 int effects = CFE_BOLD;
2247 ZeroMemory(&fmt, sizeof(fmt));
2248 fmt.cbSize = sizeof(fmt);
2249 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2251 fmt.dwMask = CFM_BOLD;
2253 if (LOWORD(wParam) == ID_FORMAT_ITALIC)
2255 effects = CFE_ITALIC;
2256 fmt.dwMask = CFM_ITALIC;
2257 } else if (LOWORD(wParam) == ID_FORMAT_UNDERLINE)
2259 effects = CFE_UNDERLINE;
2260 fmt.dwMask = CFM_UNDERLINE;
2263 fmt.dwEffects ^= effects;
2265 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2266 break;
2269 case ID_FORMAT_COLOR:
2271 HWND hReBarWnd = GetDlgItem(hWnd, IDC_REBAR);
2272 HWND hFormatBarWnd = GetDlgItem(hReBarWnd, IDC_FORMATBAR);
2273 HMENU hPop;
2274 RECT itemrc;
2275 POINT pt;
2276 int mid;
2277 int itemidx = SendMessageW(hFormatBarWnd, TB_COMMANDTOINDEX, ID_FORMAT_COLOR, 0);
2279 SendMessageW(hFormatBarWnd, TB_GETITEMRECT, itemidx, (LPARAM)&itemrc);
2280 pt.x = itemrc.left;
2281 pt.y = itemrc.bottom;
2282 ClientToScreen(hFormatBarWnd, &pt);
2283 hPop = GetSubMenu(hColorPopupMenu, 0);
2284 mid = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON |
2285 TPM_RETURNCMD | TPM_NONOTIFY,
2286 pt.x, pt.y, 0, hWnd, 0);
2287 if (mid >= ID_COLOR_FIRST && mid <= ID_COLOR_AUTOMATIC)
2289 CHARFORMAT2W fmt;
2291 ZeroMemory(&fmt, sizeof(fmt));
2292 fmt.cbSize = sizeof(fmt);
2293 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2295 fmt.dwMask = CFM_COLOR;
2297 if (mid < ID_COLOR_AUTOMATIC) {
2298 fmt.crTextColor = textcolors[mid - ID_COLOR_FIRST];
2299 fmt.dwEffects &= ~CFE_AUTOCOLOR;
2300 } else {
2301 fmt.dwEffects |= CFE_AUTOCOLOR;
2304 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
2306 break;
2309 case ID_EDIT_CUT:
2310 PostMessageW(hwndEditor, WM_CUT, 0, 0);
2311 break;
2313 case ID_EDIT_COPY:
2314 PostMessageW(hwndEditor, WM_COPY, 0, 0);
2315 break;
2317 case ID_EDIT_PASTE:
2318 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
2319 break;
2321 case ID_EDIT_CLEAR:
2322 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
2323 break;
2325 case ID_EDIT_SELECTALL:
2327 CHARRANGE range = {0, -1};
2328 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
2329 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2330 return 0;
2333 case ID_EDIT_GETTEXT:
2335 int nLen = GetWindowTextLengthW(hwndEditor);
2336 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
2337 TEXTRANGEW tr;
2339 GetWindowTextW(hwndEditor, data, nLen+1);
2340 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2342 HeapFree( GetProcessHeap(), 0, data);
2343 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
2344 tr.chrg.cpMin = 0;
2345 tr.chrg.cpMax = nLen;
2346 tr.lpstrText = data;
2347 SendMessageW(hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2348 MessageBoxW(NULL, data, wszAppTitle, MB_OK);
2349 HeapFree( GetProcessHeap(), 0, data );
2351 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2352 return 0;
2355 case ID_EDIT_CHARFORMAT:
2356 case ID_EDIT_DEFCHARFORMAT:
2358 CHARFORMAT2W cf;
2360 ZeroMemory(&cf, sizeof(cf));
2361 cf.cbSize = sizeof(cf);
2362 cf.dwMask = 0;
2363 SendMessageW(hwndEditor, EM_GETCHARFORMAT,
2364 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
2365 return 0;
2368 case ID_EDIT_PARAFORMAT:
2370 PARAFORMAT2 pf;
2371 ZeroMemory(&pf, sizeof(pf));
2372 pf.cbSize = sizeof(pf);
2373 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2374 return 0;
2377 case ID_EDIT_SELECTIONINFO:
2379 CHARRANGE range = {0, -1};
2380 char buf[128];
2381 WCHAR *data = NULL;
2383 SendMessageW(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
2384 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
2385 SendMessageW(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
2386 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
2387 MessageBoxA(hWnd, buf, "Editor", MB_OK);
2388 MessageBoxW(hWnd, data, wszAppTitle, MB_OK);
2389 HeapFree( GetProcessHeap(), 0, data);
2390 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2391 return 0;
2394 case ID_EDIT_READONLY:
2396 LONG nStyle = GetWindowLongW(hwndEditor, GWL_STYLE);
2397 if (nStyle & ES_READONLY)
2398 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
2399 else
2400 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
2401 return 0;
2404 case ID_EDIT_MODIFIED:
2405 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
2406 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
2407 else
2408 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
2409 return 0;
2411 case ID_EDIT_UNDO:
2412 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
2413 return 0;
2415 case ID_EDIT_REDO:
2416 SendMessageW(hwndEditor, EM_REDO, 0, 0);
2417 return 0;
2419 case ID_BULLETONOFF:
2420 case ID_BULLET:
2421 case ID_NUMBERING:
2422 case ID_LCLETTER:
2423 case ID_UCLETTER:
2424 case ID_LCROMAN:
2425 case ID_UCROMAN:
2427 PARAFORMAT2 pf;
2428 WORD new_number = LOWORD(wParam) - ID_BULLET + PFN_BULLET;
2429 pf.cbSize = sizeof(pf);
2430 pf.dwMask = PFM_NUMBERING;
2431 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2433 pf.dwMask = PFM_NUMBERING | PFM_NUMBERINGSTART | PFM_NUMBERINGSTYLE | PFM_NUMBERINGTAB | PFM_OFFSET | PFM_OFFSETINDENT;
2435 if(pf.wNumbering && ((pf.wNumbering == new_number) || (LOWORD(wParam) == ID_BULLETONOFF)))
2437 pf.wNumbering = 0;
2438 pf.wNumberingStart = 0;
2439 pf.wNumberingStyle = 0;
2440 pf.wNumberingTab = 0;
2441 pf.dxOffset = 0;
2442 pf.dxStartIndent = -360;
2443 } else
2445 pf.dxStartIndent = pf.wNumbering ? 0 : 360;
2447 if (LOWORD(wParam) == ID_BULLETONOFF)
2448 pf.wNumbering = last_bullet;
2449 else
2451 pf.wNumbering = new_number;
2452 last_bullet = pf.wNumbering;
2454 pf.wNumberingStart = 1;
2455 pf.wNumberingStyle = PFNS_PERIOD;
2456 pf.wNumberingTab = 360;
2457 pf.dxOffset = 360;
2460 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2462 break;
2464 case ID_ALIGN_LEFT:
2465 case ID_ALIGN_CENTER:
2466 case ID_ALIGN_RIGHT:
2468 PARAFORMAT2 pf;
2470 pf.cbSize = sizeof(pf);
2471 pf.dwMask = PFM_ALIGNMENT;
2472 switch(LOWORD(wParam)) {
2473 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
2474 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
2475 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
2477 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
2478 break;
2481 case ID_BACK_1:
2482 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
2483 break;
2485 case ID_BACK_2:
2486 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
2487 break;
2489 case ID_TOGGLE_TOOLBAR:
2490 set_toolbar_state(BANDID_TOOLBAR, !is_bar_visible(BANDID_TOOLBAR));
2491 update_window();
2492 break;
2494 case ID_TOGGLE_FORMATBAR:
2495 set_toolbar_state(BANDID_FONTLIST, !is_bar_visible(BANDID_FORMATBAR));
2496 set_toolbar_state(BANDID_SIZELIST, !is_bar_visible(BANDID_FORMATBAR));
2497 set_toolbar_state(BANDID_FORMATBAR, !is_bar_visible(BANDID_FORMATBAR));
2498 update_window();
2499 break;
2501 case ID_TOGGLE_STATUSBAR:
2502 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR));
2503 update_window();
2504 break;
2506 case ID_TOGGLE_RULER:
2507 set_toolbar_state(BANDID_RULER, !is_bar_visible(BANDID_RULER));
2508 update_window();
2509 break;
2511 case ID_DATETIME:
2512 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_DATETIME), hWnd, datetime_proc);
2513 break;
2515 case ID_PARAFORMAT:
2516 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_PARAFORMAT), hWnd, paraformat_proc);
2517 break;
2519 case ID_TABSTOPS:
2520 DialogBoxW(GetModuleHandleW(0), MAKEINTRESOURCEW(IDD_TABSTOPS), hWnd, tabstops_proc);
2521 break;
2523 case ID_ABOUT:
2524 dialog_about();
2525 break;
2527 case ID_VIEWPROPERTIES:
2528 dialog_viewproperties();
2529 break;
2531 case IDC_FONTLIST:
2532 if (HIWORD(wParam) == CBN_SELENDOK)
2534 WCHAR buffer[LF_FACESIZE];
2535 HWND hwndFontList = (HWND)lParam;
2536 get_comboexlist_selection(hwndFontList, buffer, LF_FACESIZE);
2537 on_fontlist_modified(buffer);
2539 break;
2541 case IDC_SIZELIST:
2542 if (HIWORD(wParam) == CBN_SELENDOK)
2544 WCHAR buffer[MAX_STRING_LEN+1];
2545 HWND hwndSizeList = (HWND)lParam;
2546 get_comboexlist_selection(hwndSizeList, buffer, MAX_STRING_LEN+1);
2547 on_sizelist_modified(hwndSizeList, buffer);
2549 break;
2551 default:
2552 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
2553 break;
2555 return 0;
2558 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam )
2560 HMENU hMenu = (HMENU)wParam;
2561 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
2562 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
2563 PARAFORMAT pf;
2564 int nAlignment = -1;
2565 int selFrom, selTo;
2566 GETTEXTLENGTHEX gt;
2567 LRESULT textLength;
2568 MENUITEMINFOW mi;
2570 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
2571 EnableMenuItem(hMenu, ID_EDIT_COPY, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2572 EnableMenuItem(hMenu, ID_EDIT_CUT, (selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
2574 pf.cbSize = sizeof(PARAFORMAT);
2575 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
2576 CheckMenuItem(hMenu, ID_EDIT_READONLY,
2577 (GetWindowLongW(hwndEditor, GWL_STYLE) & ES_READONLY) ? MF_CHECKED : MF_UNCHECKED);
2578 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
2579 SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED);
2580 if (pf.dwMask & PFM_ALIGNMENT)
2581 nAlignment = pf.wAlignment;
2582 CheckMenuItem(hMenu, ID_ALIGN_LEFT, (nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
2583 CheckMenuItem(hMenu, ID_ALIGN_CENTER, (nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
2584 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, (nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
2586 CheckMenuItem(hMenu, ID_BULLET, ((pf.wNumbering == PFN_BULLET) ? MF_CHECKED : MF_UNCHECKED));
2587 CheckMenuItem(hMenu, ID_NUMBERING, ((pf.wNumbering == PFN_ARABIC) ? MF_CHECKED : MF_UNCHECKED));
2588 CheckMenuItem(hMenu, ID_LCLETTER, ((pf.wNumbering == PFN_LCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2589 CheckMenuItem(hMenu, ID_UCLETTER, ((pf.wNumbering == PFN_UCLETTER) ? MF_CHECKED : MF_UNCHECKED));
2590 CheckMenuItem(hMenu, ID_LCROMAN, ((pf.wNumbering == PFN_LCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2591 CheckMenuItem(hMenu, ID_UCROMAN, ((pf.wNumbering == PFN_UCROMAN) ? MF_CHECKED : MF_UNCHECKED));
2593 EnableMenuItem(hMenu, ID_EDIT_UNDO, SendMessageW(hwndEditor, EM_CANUNDO, 0, 0) ?
2594 MF_ENABLED : MF_GRAYED);
2595 EnableMenuItem(hMenu, ID_EDIT_REDO, SendMessageW(hwndEditor, EM_CANREDO, 0, 0) ?
2596 MF_ENABLED : MF_GRAYED);
2598 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, is_bar_visible(BANDID_TOOLBAR) ?
2599 MF_CHECKED : MF_UNCHECKED);
2601 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, is_bar_visible(BANDID_FORMATBAR) ?
2602 MF_CHECKED : MF_UNCHECKED);
2604 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, IsWindowVisible(hwndStatus) ?
2605 MF_CHECKED : MF_UNCHECKED);
2607 CheckMenuItem(hMenu, ID_TOGGLE_RULER, is_bar_visible(BANDID_RULER) ? MF_CHECKED : MF_UNCHECKED);
2609 gt.flags = GTL_NUMCHARS;
2610 gt.codepage = 1200;
2611 textLength = SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0);
2612 EnableMenuItem(hMenu, ID_FIND, textLength ? MF_ENABLED : MF_GRAYED);
2614 mi.cbSize = sizeof(mi);
2615 mi.fMask = MIIM_DATA;
2617 GetMenuItemInfoW(hMenu, ID_FIND_NEXT, FALSE, &mi);
2619 EnableMenuItem(hMenu, ID_FIND_NEXT, (textLength && mi.dwItemData) ? MF_ENABLED : MF_GRAYED);
2621 EnableMenuItem(hMenu, ID_REPLACE, textLength ? MF_ENABLED : MF_GRAYED);
2623 return 0;
2626 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
2628 int nStatusSize = 0;
2629 RECT rc;
2630 HWND hwndEditor = preview_isactive() ? GetDlgItem(hWnd, IDC_PREVIEW) : GetDlgItem(hWnd, IDC_EDITOR);
2631 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
2632 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
2633 HWND hRulerWnd = GetDlgItem(hwndReBar, IDC_RULER);
2634 int rebarHeight = 0;
2636 if (hwndStatusBar)
2638 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
2639 if (IsWindowVisible(hwndStatusBar))
2641 GetClientRect(hwndStatusBar, &rc);
2642 nStatusSize = rc.bottom - rc.top;
2643 } else
2645 nStatusSize = 0;
2648 if (hwndReBar)
2650 rebarHeight = SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0);
2652 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rebarHeight, TRUE);
2654 if (hwndEditor)
2656 GetClientRect(hWnd, &rc);
2657 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
2660 redraw_ruler(hRulerWnd);
2662 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
2665 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2667 if(msg == ID_FINDMSGSTRING)
2668 return handle_findmsg((LPFINDREPLACEW)lParam);
2670 switch(msg)
2672 case WM_CREATE:
2673 return OnCreate( hWnd );
2675 case WM_USER:
2676 return OnUser( hWnd );
2678 case WM_NOTIFY:
2679 return OnNotify( hWnd, lParam );
2681 case WM_COMMAND:
2682 if(preview_isactive())
2684 return preview_command( hWnd, wParam );
2687 return OnCommand( hWnd, wParam, lParam );
2689 case WM_DESTROY:
2690 PostQuitMessage(0);
2691 break;
2693 case WM_CLOSE:
2694 if(preview_isactive())
2696 preview_exit(hWnd);
2697 } else if(prompt_save_changes())
2699 registry_set_options(hMainWnd);
2700 registry_set_formatopts_all(barState, wordWrap);
2701 PostQuitMessage(0);
2703 break;
2705 case WM_ACTIVATE:
2706 if (LOWORD(wParam))
2707 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
2708 return 0;
2710 case WM_INITMENUPOPUP:
2711 return OnInitPopupMenu( hWnd, wParam );
2713 case WM_SIZE:
2714 return OnSize( hWnd, wParam, lParam );
2716 case WM_CONTEXTMENU:
2717 return DefWindowProcW(hWnd, msg, wParam, lParam);
2719 case WM_DROPFILES:
2721 WCHAR file[MAX_PATH];
2722 DragQueryFileW((HDROP)wParam, 0, file, MAX_PATH);
2723 DragFinish((HDROP)wParam);
2725 if(prompt_save_changes())
2726 DoOpenFile(file);
2728 break;
2729 case WM_PAINT:
2730 if(!preview_isactive())
2731 return DefWindowProcW(hWnd, msg, wParam, lParam);
2733 default:
2734 return DefWindowProcW(hWnd, msg, wParam, lParam);
2737 return 0;
2740 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int nCmdShow)
2742 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES|ICC_USEREX_CLASSES};
2743 HACCEL hAccel;
2744 WNDCLASSEXW wc;
2745 MSG msg;
2746 RECT rc;
2747 UINT_PTR hPrevRulerProc;
2748 HWND hRulerWnd;
2749 POINTL EditPoint;
2750 DWORD bMaximized;
2751 MONITORINFO info;
2752 HMONITOR monitor;
2753 int x, y;
2754 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
2755 'T','A','B','L','E','\0'};
2757 InitCommonControlsEx(&classes);
2759 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
2761 wc.cbSize = sizeof(wc);
2762 wc.style = 0;
2763 wc.lpfnWndProc = WndProc;
2764 wc.cbClsExtra = 0;
2765 wc.cbWndExtra = 4;
2766 wc.hInstance = hInstance;
2767 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
2768 wc.hIconSm = LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD), IMAGE_ICON,
2769 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
2770 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2771 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2772 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_MAINMENU);
2773 wc.lpszClassName = wszMainWndClass;
2774 RegisterClassExW(&wc);
2776 wc.style = 0;
2777 wc.lpfnWndProc = preview_proc;
2778 wc.cbClsExtra = 0;
2779 wc.cbWndExtra = 0;
2780 wc.hInstance = hInstance;
2781 wc.hIcon = NULL;
2782 wc.hIconSm = NULL;
2783 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
2784 wc.hbrBackground = NULL;
2785 wc.lpszMenuName = NULL;
2786 wc.lpszClassName = wszPreviewWndClass;
2787 RegisterClassExW(&wc);
2789 registry_read_winrect(&rc);
2790 monitor = MonitorFromRect(&rc, MONITOR_DEFAULTTOPRIMARY);
2791 info.cbSize = sizeof(info);
2792 GetMonitorInfoW(monitor, &info);
2794 x = rc.left;
2795 y = rc.top;
2796 IntersectRect(&info.rcWork, &info.rcWork, &rc);
2797 if (IsRectEmpty(&info.rcWork))
2798 x = y = CW_USEDEFAULT;
2800 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_CLIPCHILDREN|WS_OVERLAPPEDWINDOW,
2801 x, y, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL);
2802 registry_read_maximized(&bMaximized);
2803 if ((nCmdShow == SW_SHOWNORMAL || nCmdShow == SW_SHOWDEFAULT)
2804 && bMaximized)
2805 nCmdShow = SW_SHOWMAXIMIZED;
2806 ShowWindow(hMainWnd, nCmdShow);
2808 set_caption(NULL);
2809 set_bar_states();
2810 set_fileformat(SF_RTF);
2811 hColorPopupMenu = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDM_COLOR_POPUP));
2812 get_default_printer_opts();
2813 target_device(hMainWnd, wordWrap[reg_formatindex(fileFormat)]);
2815 hRulerWnd = GetDlgItem(GetDlgItem(hMainWnd, IDC_REBAR), IDC_RULER);
2816 SendMessageW(GetDlgItem(hMainWnd, IDC_EDITOR), EM_POSFROMCHAR, (WPARAM)&EditPoint, 0);
2817 hPrevRulerProc = SetWindowLongPtrW(hRulerWnd, GWLP_WNDPROC, (UINT_PTR)ruler_proc);
2818 SendMessageW(hRulerWnd, WM_USER, (WPARAM)&EditPoint, hPrevRulerProc);
2820 HandleCommandLine(GetCommandLineW());
2822 while(GetMessageW(&msg,0,0,0))
2824 if (IsDialogMessageW(hFindWnd, &msg))
2825 continue;
2827 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
2828 continue;
2829 TranslateMessage(&msg);
2830 DispatchMessageW(&msg);
2831 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
2832 SendMessageW(hMainWnd, WM_USER, 0, 0);
2835 return 0;