wordpad: Use Unicode functions in more places.
[wine/hacks.git] / programs / wordpad / wordpad.c
blob1dd869bf87619a76b0d16009c1b30aed3478c653
1 /*
2 * Wordpad implementation
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN
22 #define _WIN32_IE 0x0400
24 #define MAX_STRING_LEN 255
26 #include <stdarg.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>
36 #include "resource.h"
38 /* use LoadString */
39 static const WCHAR xszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
40 static const WCHAR xszMainMenu[] = {'M','A','I','N','M','E','N','U',0};
42 static const WCHAR wszRichEditClass[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
43 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
44 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
46 static HWND hMainWnd;
47 static HWND hEditorWnd;
49 static WCHAR wszFilter[MAX_STRING_LEN];
51 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
53 /* Load string resources */
54 static void DoLoadStrings(void)
56 LPWSTR p = wszFilter;
57 static const WCHAR files_rtf[] = {'*','.','r','t','f','\0'};
58 static const WCHAR files_txt[] = {'*','.','t','x','t','\0'};
59 static const WCHAR files_all[] = {'*','.','*','\0'};
60 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
62 LoadStringW(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
63 p += lstrlenW(p) + 1;
64 lstrcpyW(p, files_rtf);
65 p += lstrlenW(p) + 1;
66 LoadStringW(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
67 p += lstrlenW(p) + 1;
68 lstrcpyW(p, files_txt);
69 p += lstrlenW(p) + 1;
70 LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
71 p += lstrlenW(p) + 1;
72 lstrcpyW(p, files_all);
73 p += lstrlenW(p) + 1;
74 *p = '\0';
77 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
79 TBBUTTON button;
81 ZeroMemory(&button, sizeof(button));
82 button.iBitmap = nImage;
83 button.idCommand = nCommand;
84 button.fsState = TBSTATE_ENABLED;
85 button.fsStyle = TBSTYLE_BUTTON;
86 button.dwData = 0;
87 button.iString = -1;
88 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
91 static void AddSeparator(HWND hwndToolBar)
93 TBBUTTON button;
95 ZeroMemory(&button, sizeof(button));
96 button.iBitmap = -1;
97 button.idCommand = 0;
98 button.fsState = 0;
99 button.fsStyle = TBSTYLE_SEP;
100 button.dwData = 0;
101 button.iString = -1;
102 SendMessageW(hwndToolBar, TB_ADDBUTTONSW, 1, (LPARAM)&button);
105 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
107 HANDLE hFile = (HANDLE)cookie;
108 DWORD read;
110 if(!ReadFile(hFile, buffer, cb, &read, 0))
111 return 1;
113 *pcb = read;
115 return 0;
118 static DWORD CALLBACK stream_out(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
120 DWORD written;
121 int ret;
122 HANDLE hFile = (HANDLE)cookie;
124 ret = WriteFile(hFile, buffer, cb, &written, 0);
126 if(!ret || (cb != written))
127 return 1;
129 *pcb = cb;
131 return 0;
134 static WCHAR wszFileName[MAX_PATH];
136 static void set_caption(LPCWSTR wszNewFileName)
138 static const WCHAR wszSeparator[] = {' ','-',' '};
140 if(wszNewFileName)
142 WCHAR *wszCaption;
143 SIZE_T length = 0;
145 wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
146 lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
148 if(!wszCaption)
149 return;
151 memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
152 length += lstrlenW(wszNewFileName);
153 memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
154 length += sizeof(wszSeparator) / sizeof(WCHAR);
155 memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
157 SetWindowTextW(hMainWnd, wszCaption);
159 HeapFree(GetProcessHeap(), 0, wszCaption);
160 } else
162 SetWindowTextW(hMainWnd, wszAppTitle);
166 static void DoOpenFile(LPCWSTR szOpenFileName)
168 HANDLE hFile;
169 EDITSTREAM es;
171 hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
172 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
173 if (hFile == INVALID_HANDLE_VALUE)
174 return;
176 es.dwCookie = (DWORD_PTR)hFile;
177 es.pfnCallback = stream_in;
179 /* FIXME: Handle different file formats */
180 SendMessageW(hEditorWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
182 CloseHandle(hFile);
184 SetFocus(hEditorWnd);
186 set_caption(szOpenFileName);
188 lstrcpyW(wszFileName, szOpenFileName);
191 static void DialogOpenFile(void)
193 OPENFILENAMEW ofn;
195 WCHAR wszFile[MAX_PATH] = {'\0'};
196 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
198 ZeroMemory(&ofn, sizeof(ofn));
200 ofn.lStructSize = sizeof(ofn);
201 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
202 ofn.hwndOwner = hMainWnd;
203 ofn.lpstrFilter = wszFilter;
204 ofn.lpstrFile = wszFile;
205 ofn.nMaxFile = MAX_PATH;
206 ofn.lpstrDefExt = wszDefExt;
208 if(GetOpenFileNameW(&ofn))
209 DoOpenFile(ofn.lpstrFile);
212 static void DoSaveFile(LPCWSTR wszSaveFileName)
214 HANDLE hFile;
215 EDITSTREAM stream;
216 LRESULT ret;
218 hFile = CreateFileW(wszSaveFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
219 FILE_ATTRIBUTE_NORMAL, NULL);
221 if(hFile == INVALID_HANDLE_VALUE)
222 return;
224 stream.dwCookie = (DWORD_PTR)hFile;
225 stream.pfnCallback = stream_out;
227 /* FIXME: Handle different formats */
228 ret = SendMessageW(hEditorWnd, EM_STREAMOUT, SF_RTF, (LPARAM)&stream);
230 CloseHandle(hFile);
232 SetFocus(hEditorWnd);
234 if(!ret)
235 return;
237 lstrcpyW(wszFileName, wszSaveFileName);
238 set_caption(wszFileName);
241 static void DialogSaveFile(void)
243 OPENFILENAMEW sfn;
245 WCHAR wszFile[MAX_PATH] = {'\0'};
246 static const WCHAR wszDefExt[] = {'r','t','f','\0'};
248 ZeroMemory(&sfn, sizeof(sfn));
250 sfn.lStructSize = sizeof(sfn);
251 sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
252 sfn.hwndOwner = hMainWnd;
253 sfn.lpstrFilter = wszFilter;
254 sfn.lpstrFile = wszFile;
255 sfn.nMaxFile = MAX_PATH;
256 sfn.lpstrDefExt = wszDefExt;
258 if(!GetSaveFileNameW(&sfn))
259 return;
261 DoSaveFile(sfn.lpstrFile);
264 static void HandleCommandLine(LPWSTR cmdline)
266 WCHAR delimiter;
267 int opt_print = 0;
269 /* skip white space */
270 while (*cmdline == ' ') cmdline++;
272 /* skip executable name */
273 delimiter = (*cmdline == '"' ? '"' : ' ');
275 if (*cmdline == delimiter) cmdline++;
276 while (*cmdline && *cmdline != delimiter) cmdline++;
277 if (*cmdline == delimiter) cmdline++;
279 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
281 WCHAR option;
283 if (*cmdline++ == ' ') continue;
285 option = *cmdline;
286 if (option) cmdline++;
287 while (*cmdline == ' ') cmdline++;
289 switch (option)
291 case 'p':
292 case 'P':
293 opt_print = 1;
294 break;
298 if (*cmdline)
300 /* file name is passed on the command line */
301 if (cmdline[0] == '"')
303 cmdline++;
304 cmdline[lstrlenW(cmdline) - 1] = 0;
306 DoOpenFile(cmdline);
307 InvalidateRect(hMainWnd, NULL, FALSE);
310 if (opt_print)
311 MessageBox(hMainWnd, "Printing not implemented", "WordPad", MB_OK);
314 static void DoDefaultFont(void)
316 static const WCHAR szFaceName[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0};
317 CHARFORMAT2W fmt;
319 ZeroMemory(&fmt, sizeof(fmt));
321 fmt.cbSize = sizeof(fmt);
322 fmt.dwMask = CFM_FACE;
324 lstrcpyW(fmt.szFaceName, szFaceName);
326 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
329 static void update_window(void)
331 RECT rect;
333 GetWindowRect(hMainWnd, &rect);
335 (void) OnSize(hMainWnd, SIZE_RESTORED, MAKELONG(rect.bottom, rect.right));
338 static void toggle_toolbar(int bandId)
340 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
341 REBARBANDINFOW rbbinfo;
342 BOOL hide = TRUE;
344 if(!hwndReBar)
345 return;
347 rbbinfo.cbSize = sizeof(rbbinfo);
348 rbbinfo.fMask = RBBIM_STYLE | RBBIM_SIZE;
350 SendMessageW(hwndReBar, RB_GETBANDINFO, bandId, (LPARAM)&rbbinfo);
352 if(rbbinfo.fStyle & RBBS_HIDDEN)
353 hide = FALSE;
355 SendMessageW(hwndReBar, RB_SHOWBAND, bandId, hide ? 0 : 1);
357 if(bandId == BANDID_TOOLBAR)
359 rbbinfo.fMask ^= RBBIM_SIZE;
361 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
363 if(hide)
364 rbbinfo.fStyle ^= RBBS_BREAK;
365 else
366 rbbinfo.fStyle |= RBBS_BREAK;
368 SendMessageW(hwndReBar, RB_SETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
372 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
374 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd;
375 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
376 HANDLE hDLL;
377 TBADDBITMAP ab;
378 int nStdBitmaps = 0;
379 REBARINFO rbi;
380 REBARBANDINFOW rbb;
381 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
382 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
384 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
386 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
387 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
388 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
390 rbi.cbSize = sizeof(rbi);
391 rbi.fMask = 0;
392 rbi.himl = NULL;
393 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
394 return -1;
396 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
397 IDC_TOOLBAR,
398 0, hInstance, 0,
399 NULL, 0,
400 24, 24, 16, 16, sizeof(TBBUTTON));
402 ab.hInst = HINST_COMMCTRL;
403 ab.nID = IDB_STD_SMALL_COLOR;
404 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
406 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
407 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
408 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
409 AddSeparator(hToolBarWnd);
410 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT);
411 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
412 AddSeparator(hToolBarWnd);
413 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
414 AddSeparator(hToolBarWnd);
415 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
416 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
417 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
418 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
419 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
421 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
423 rbb.cbSize = sizeof(rbb);
424 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE;
425 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
426 rbb.cx = 0;
427 rbb.hwndChild = hToolBarWnd;
428 rbb.cxMinChild = 0;
429 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
431 SendMessageW(hReBarWnd, RB_INSERTBAND, BANDID_TOOLBAR, (LPARAM)&rbb);
433 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
434 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
435 IDC_FORMATBAR, 6, hInstance, IDB_FORMATBAR, NULL, 0, 24, 24, 16, 16, sizeof(TBBUTTON));
437 ab.hInst = HINST_COMMCTRL;
438 ab.nID = IDB_STD_SMALL_COLOR;
439 nStdBitmaps = SendMessageW(hFormatBarWnd, TB_ADDBITMAP, 6, (LPARAM)&ab);
441 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
442 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
443 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
444 AddSeparator(hFormatBarWnd);
445 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
446 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
447 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
449 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
451 rbb.hwndChild = hFormatBarWnd;
453 SendMessageW(hReBarWnd, RB_INSERTBAND, BANDID_FORMATBAR, (LPARAM)&rbb);
455 hDLL = LoadLibraryW(wszRichEditDll);
456 assert(hDLL);
458 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
459 WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
460 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
462 if (!hEditorWnd)
464 fprintf(stderr, "Error code %u\n", GetLastError());
465 return -1;
467 assert(hEditorWnd);
469 SetFocus(hEditorWnd);
470 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
472 DoDefaultFont();
474 DoLoadStrings();
476 return 0;
479 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
481 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
482 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
483 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
484 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
485 int from, to;
486 CHARFORMAT2W fmt;
487 PARAFORMAT2 pf;
489 ZeroMemory(&fmt, sizeof(fmt));
490 fmt.cbSize = sizeof(fmt);
492 ZeroMemory(&pf, sizeof(pf));
493 pf.cbSize = sizeof(pf);
495 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
497 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
498 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
499 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
500 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
501 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
502 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
503 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
505 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
506 (fmt.dwEffects & CFE_BOLD));
507 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
508 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
509 (fmt.dwEffects & CFE_ITALIC));
510 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
511 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
512 (fmt.dwEffects & CFE_UNDERLINE));
513 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
515 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
516 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
517 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
518 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
520 return 0;
523 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
525 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
526 NMHDR *pHdr = (NMHDR *)lParam;
528 if (pHdr->hwndFrom != hwndEditor)
529 return 0;
531 if (pHdr->code == EN_SELCHANGE)
533 SELCHANGE *pSC = (SELCHANGE *)lParam;
534 char buf[128];
536 sprintf( buf,"selection = %d..%d, line count=%ld",
537 pSC->chrg.cpMin, pSC->chrg.cpMax,
538 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
539 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
540 SendMessage(hWnd, WM_USER, 0, 0);
541 return 1;
543 return 0;
546 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
548 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
549 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
551 if ((HWND)lParam == hwndEditor)
552 return 0;
554 switch(LOWORD(wParam))
556 case ID_FILE_EXIT:
557 PostMessageW(hWnd, WM_CLOSE, 0, 0);
558 break;
560 case ID_FILE_NEW:
561 set_caption(NULL);
562 wszFileName[0] = '\0';
563 SetWindowTextW(hwndEditor, wszFileName);
564 /* FIXME: set default format too */
565 break;
567 case ID_FILE_OPEN:
568 DialogOpenFile();
569 break;
571 case ID_FILE_SAVE:
572 if(wszFileName[0])
574 DoSaveFile(wszFileName);
575 break;
577 /* Fall through */
579 case ID_FILE_SAVEAS:
580 DialogSaveFile();
581 break;
583 case ID_PRINT:
584 case ID_PREVIEW:
585 case ID_FIND:
587 static const WCHAR wszNotImplemented[] = {'N','o','t',' ',
588 'i','m','p','l','e','m','e','n','t','e','d','\0'};
589 MessageBoxW(hWnd, wszNotImplemented, wszAppTitle, MB_OK);
591 break;
593 case ID_FORMAT_BOLD:
594 case ID_FORMAT_ITALIC:
595 case ID_FORMAT_UNDERLINE:
597 CHARFORMAT2W fmt;
598 int mask = CFM_BOLD;
599 if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
600 if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
602 ZeroMemory(&fmt, sizeof(fmt));
603 fmt.cbSize = sizeof(fmt);
604 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
605 if (!(fmt.dwMask&mask))
606 fmt.dwEffects |= mask;
607 else
608 fmt.dwEffects ^= mask;
609 fmt.dwMask = mask;
610 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
611 break;
614 case ID_EDIT_CUT:
615 PostMessageW(hwndEditor, WM_CUT, 0, 0);
616 break;
618 case ID_EDIT_COPY:
619 PostMessageW(hwndEditor, WM_COPY, 0, 0);
620 break;
622 case ID_EDIT_PASTE:
623 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
624 break;
626 case ID_EDIT_CLEAR:
627 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
628 break;
630 case ID_EDIT_SELECTALL:
632 CHARRANGE range = {0, -1};
633 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
634 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
635 return 0;
638 case ID_EDIT_GETTEXT:
640 int nLen = GetWindowTextLengthW(hwndEditor);
641 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
642 TEXTRANGEW tr;
644 GetWindowTextW(hwndEditor, data, nLen+1);
645 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
647 HeapFree( GetProcessHeap(), 0, data);
648 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
649 tr.chrg.cpMin = 0;
650 tr.chrg.cpMax = nLen;
651 tr.lpstrText = data;
652 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
653 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
654 HeapFree( GetProcessHeap(), 0, data );
656 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
657 return 0;
660 case ID_EDIT_CHARFORMAT:
661 case ID_EDIT_DEFCHARFORMAT:
663 CHARFORMAT2W cf;
664 LRESULT i;
665 ZeroMemory(&cf, sizeof(cf));
666 cf.cbSize = sizeof(cf);
667 cf.dwMask = 0;
668 i = SendMessageW(hwndEditor, EM_GETCHARFORMAT,
669 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
670 return 0;
673 case ID_EDIT_PARAFORMAT:
675 PARAFORMAT2 pf;
676 ZeroMemory(&pf, sizeof(pf));
677 pf.cbSize = sizeof(pf);
678 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
679 return 0;
682 case ID_EDIT_SELECTIONINFO:
684 CHARRANGE range = {0, -1};
685 char buf[128];
686 WCHAR *data = NULL;
688 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
689 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
690 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
691 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
692 MessageBoxA(hWnd, buf, "Editor", MB_OK);
693 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
694 HeapFree( GetProcessHeap(), 0, data);
695 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
696 return 0;
699 case ID_EDIT_READONLY:
701 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
702 if (nStyle & ES_READONLY)
703 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
704 else
705 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
706 return 0;
709 case ID_EDIT_MODIFIED:
710 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
711 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
712 else
713 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
714 return 0;
716 case ID_EDIT_UNDO:
717 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
718 return 0;
720 case ID_EDIT_REDO:
721 SendMessageW(hwndEditor, EM_REDO, 0, 0);
722 return 0;
724 case ID_ALIGN_LEFT:
725 case ID_ALIGN_CENTER:
726 case ID_ALIGN_RIGHT:
728 PARAFORMAT2 pf;
730 pf.cbSize = sizeof(pf);
731 pf.dwMask = PFM_ALIGNMENT;
732 switch(LOWORD(wParam)) {
733 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
734 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
735 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
737 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
738 break;
741 case ID_BACK_1:
742 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
743 break;
745 case ID_BACK_2:
746 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
747 break;
749 case ID_TOGGLE_TOOLBAR:
750 toggle_toolbar(BANDID_TOOLBAR);
751 update_window();
752 break;
754 case ID_TOGGLE_FORMATBAR:
755 toggle_toolbar(BANDID_FORMATBAR);
756 update_window();
757 break;
759 case ID_TOGGLE_STATUSBAR:
760 ShowWindow(hwndStatus, IsWindowVisible(hwndStatus) ? SW_HIDE : SW_SHOW);
761 update_window();
762 break;
764 default:
765 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
766 break;
768 return 0;
771 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
773 HMENU hMenu = (HMENU)wParam;
774 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
775 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
776 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
777 PARAFORMAT pf;
778 int nAlignment = -1;
779 REBARBANDINFOW rbbinfo;
781 pf.cbSize = sizeof(PARAFORMAT);
782 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
783 CheckMenuItem(hMenu, ID_EDIT_READONLY,
784 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
785 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
786 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
787 if (pf.dwMask & PFM_ALIGNMENT)
788 nAlignment = pf.wAlignment;
789 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
790 MF_CHECKED : MF_UNCHECKED);
791 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
792 MF_CHECKED : MF_UNCHECKED);
793 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
794 MF_CHECKED : MF_UNCHECKED);
795 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
796 MF_ENABLED : MF_GRAYED);
797 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
798 MF_ENABLED : MF_GRAYED);
800 rbbinfo.cbSize = sizeof(rbbinfo);
801 rbbinfo.fMask = RBBIM_STYLE;
802 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_TOOLBAR, (LPARAM)&rbbinfo);
804 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(rbbinfo.fStyle & RBBS_HIDDEN) ?
805 MF_UNCHECKED : MF_CHECKED);
807 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
809 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(rbbinfo.fStyle & RBBS_HIDDEN) ?
810 MF_UNCHECKED : MF_CHECKED);
812 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
813 MF_CHECKED : MF_UNCHECKED);
814 return 0;
817 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
819 int nStatusSize = 0;
820 RECT rc;
821 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
822 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
823 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
824 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
825 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
826 int rebarHeight = 0;
827 REBARBANDINFOW rbbinfo;
828 int rebarRows = 2;
830 if (hwndStatusBar)
832 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
833 if (IsWindowVisible(hwndStatusBar))
835 GetClientRect(hwndStatusBar, &rc);
836 nStatusSize = rc.bottom - rc.top;
837 } else
839 nStatusSize = 0;
842 if (hwndToolBar)
844 rc.left = rc.top = 0;
845 rc.right = LOWORD(lParam);
846 rc.bottom = HIWORD(lParam);
847 SendMessageW(hwndToolBar, TB_AUTOSIZE, 0, 0);
848 SendMessageW(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
849 GetClientRect(hwndReBar, &rc);
850 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
852 if (hwndFormatBar)
854 rc.left = rc.top = 0;
855 rc.right = LOWORD(lParam);
856 rc.bottom = HIWORD(lParam);
857 SendMessageW(hwndFormatBar, TB_AUTOSIZE, 0, 0);
858 SendMessageW(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
859 GetClientRect(hwndReBar, &rc);
860 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
862 if (hwndReBar)
864 rbbinfo.cbSize = sizeof(rbbinfo);
865 rbbinfo.fMask = RBBIM_STYLE;
867 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_TOOLBAR, (LPARAM)&rbbinfo);
868 if(rbbinfo.fStyle & RBBS_HIDDEN)
869 rebarRows--;
871 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
872 if(rbbinfo.fStyle & RBBS_HIDDEN)
873 rebarRows--;
875 rebarHeight = rebarRows ? SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0) : 0;
877 if (hwndEditor)
879 GetClientRect(hWnd, &rc);
880 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
883 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
886 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
888 switch(msg)
890 case WM_CREATE:
891 return OnCreate( hWnd, wParam, lParam );
893 case WM_USER:
894 return OnUser( hWnd, wParam, lParam );
896 case WM_NOTIFY:
897 return OnNotify( hWnd, wParam, lParam );
899 case WM_COMMAND:
900 return OnCommand( hWnd, wParam, lParam );
902 case WM_DESTROY:
903 PostQuitMessage(0);
904 break;
906 case WM_ACTIVATE:
907 if (LOWORD(wParam))
908 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
909 return 0;
911 case WM_INITMENUPOPUP:
912 return OnInitPopupMenu( hWnd, wParam, lParam );
914 case WM_SIZE:
915 return OnSize( hWnd, wParam, lParam );
917 default:
918 return DefWindowProcW(hWnd, msg, wParam, lParam);
921 return 0;
924 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
926 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES};
927 HACCEL hAccel;
928 WNDCLASSW wc;
929 MSG msg;
930 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
931 'T','A','B','L','E','\0'};
933 InitCommonControlsEx(&classes);
935 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
937 wc.style = CS_HREDRAW | CS_VREDRAW;
938 wc.lpfnWndProc = WndProc;
939 wc.cbClsExtra = 0;
940 wc.cbWndExtra = 4;
941 wc.hInstance = hInstance;
942 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
943 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
944 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
945 wc.lpszMenuName = xszMainMenu;
946 wc.lpszClassName = wszMainWndClass;
947 RegisterClassW(&wc);
949 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
950 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
951 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
953 HandleCommandLine(GetCommandLineW());
955 while(GetMessageW(&msg,0,0,0))
957 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
958 continue;
959 TranslateMessage(&msg);
960 DispatchMessageW(&msg);
961 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
962 SendMessageW(hMainWnd, WM_USER, 0, 0);
965 return 0;