gdiplus: Add missing '\n' to ok() calls, Wine traces.
[wine.git] / programs / wordpad / wordpad.c
blob616ef23018d83858da200a20a0f99aea6ff6b528
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 SendMessage(hwndToolBar, TB_ADDBUTTONS, 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 SendMessage(hwndToolBar, TB_ADDBUTTONS, 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 SendMessage(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 REBARBANDINFO rbb;
382 CreateStatusWindow(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, "RichEdit text", hWnd, IDC_STATUSBAR);
384 hReBarWnd = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL,
385 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
386 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
388 rbi.cbSize = sizeof(rbi);
389 rbi.fMask = 0;
390 rbi.himl = NULL;
391 if(!SendMessage(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
392 return -1;
394 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
395 IDC_TOOLBAR,
396 0, hInstance, 0,
397 NULL, 0,
398 24, 24, 16, 16, sizeof(TBBUTTON));
400 ab.hInst = HINST_COMMCTRL;
401 ab.nID = IDB_STD_SMALL_COLOR;
402 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
404 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
405 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
406 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
407 AddSeparator(hToolBarWnd);
408 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT);
409 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
410 AddSeparator(hToolBarWnd);
411 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
412 AddSeparator(hToolBarWnd);
413 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
414 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
415 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
416 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
417 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
419 SendMessage(hToolBarWnd, TB_ADDSTRING, 0, (LPARAM)"Exit\0");
420 SendMessage(hToolBarWnd, TB_AUTOSIZE, 0, 0);
422 rbb.cbSize = sizeof(rbb);
423 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE;
424 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
425 rbb.cx = 0;
426 rbb.hwndChild = hToolBarWnd;
427 rbb.cxMinChild = 0;
428 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessage(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
430 SendMessageW(hReBarWnd, RB_INSERTBAND, BANDID_TOOLBAR, (LPARAM)&rbb);
432 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
433 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
434 IDC_FORMATBAR, 6, hInstance, IDB_FORMATBAR, NULL, 0, 24, 24, 16, 16, sizeof(TBBUTTON));
436 ab.hInst = HINST_COMMCTRL;
437 ab.nID = IDB_STD_SMALL_COLOR;
438 nStdBitmaps = SendMessageW(hFormatBarWnd, TB_ADDBITMAP, 6, (LPARAM)&ab);
440 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
441 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
442 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
443 AddSeparator(hFormatBarWnd);
444 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
445 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
446 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
448 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
450 rbb.hwndChild = hFormatBarWnd;
452 SendMessageW(hReBarWnd, RB_INSERTBAND, BANDID_FORMATBAR, (LPARAM)&rbb);
454 hDLL = LoadLibrary("RICHED20.DLL");
455 assert(hDLL);
457 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
458 WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
459 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
461 if (!hEditorWnd)
463 fprintf(stderr, "Error code %u\n", GetLastError());
464 return -1;
466 assert(hEditorWnd);
468 SetFocus(hEditorWnd);
469 SendMessage(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
471 DoDefaultFont();
473 DoLoadStrings();
475 return 0;
478 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
480 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
481 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
482 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
483 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
484 int from, to;
485 CHARFORMAT2W fmt;
486 PARAFORMAT2 pf;
488 ZeroMemory(&fmt, sizeof(fmt));
489 fmt.cbSize = sizeof(fmt);
491 ZeroMemory(&pf, sizeof(pf));
492 pf.cbSize = sizeof(pf);
494 SendMessage(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
496 SendMessage(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
497 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
498 SendMessage(hwndEditor, EM_CANUNDO, 0, 0));
499 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
500 SendMessage(hwndEditor, EM_CANREDO, 0, 0));
501 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
502 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
504 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
505 (fmt.dwEffects & CFE_BOLD));
506 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
507 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
508 (fmt.dwEffects & CFE_ITALIC));
509 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
510 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
511 (fmt.dwEffects & CFE_UNDERLINE));
512 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
514 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
515 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
516 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
517 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
519 return 0;
522 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
524 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
525 NMHDR *pHdr = (NMHDR *)lParam;
527 if (pHdr->hwndFrom != hwndEditor)
528 return 0;
530 if (pHdr->code == EN_SELCHANGE)
532 SELCHANGE *pSC = (SELCHANGE *)lParam;
533 char buf[128];
535 sprintf( buf,"selection = %d..%d, line count=%ld",
536 pSC->chrg.cpMin, pSC->chrg.cpMax,
537 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
538 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
539 SendMessage(hWnd, WM_USER, 0, 0);
540 return 1;
542 return 0;
545 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
547 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
548 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
550 if ((HWND)lParam == hwndEditor)
551 return 0;
553 switch(LOWORD(wParam))
555 case ID_FILE_EXIT:
556 PostMessage(hWnd, WM_CLOSE, 0, 0);
557 break;
559 case ID_FILE_NEW:
560 SetWindowTextA(hwndEditor, "");
561 set_caption(NULL);
562 wszFileName[0] = '\0';
563 /* FIXME: set default format too */
564 break;
566 case ID_FILE_OPEN:
567 DialogOpenFile();
568 break;
570 case ID_FILE_SAVE:
571 if(wszFileName[0])
573 DoSaveFile(wszFileName);
574 break;
576 /* Fall through */
578 case ID_FILE_SAVEAS:
579 DialogSaveFile();
580 break;
582 case ID_PRINT:
583 case ID_PREVIEW:
584 case ID_FIND:
585 MessageBox(hWnd, "Not implemented", "WordPad", MB_OK);
586 break;
588 case ID_FORMAT_BOLD:
589 case ID_FORMAT_ITALIC:
590 case ID_FORMAT_UNDERLINE:
592 CHARFORMAT2W fmt;
593 int mask = CFM_BOLD;
594 if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
595 if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
597 ZeroMemory(&fmt, sizeof(fmt));
598 fmt.cbSize = sizeof(fmt);
599 SendMessage(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
600 if (!(fmt.dwMask&mask))
601 fmt.dwEffects |= mask;
602 else
603 fmt.dwEffects ^= mask;
604 fmt.dwMask = mask;
605 SendMessage(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
606 break;
609 case ID_EDIT_CUT:
610 PostMessage(hwndEditor, WM_CUT, 0, 0);
611 break;
613 case ID_EDIT_COPY:
614 PostMessage(hwndEditor, WM_COPY, 0, 0);
615 break;
617 case ID_EDIT_PASTE:
618 PostMessage(hwndEditor, WM_PASTE, 0, 0);
619 break;
621 case ID_EDIT_CLEAR:
622 PostMessage(hwndEditor, WM_CLEAR, 0, 0);
623 break;
625 case ID_EDIT_SELECTALL:
627 CHARRANGE range = {0, -1};
628 SendMessage(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
629 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
630 return 0;
633 case ID_EDIT_GETTEXT:
635 int nLen = GetWindowTextLengthW(hwndEditor);
636 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
637 TEXTRANGEW tr;
639 GetWindowTextW(hwndEditor, data, nLen+1);
640 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
642 HeapFree( GetProcessHeap(), 0, data);
643 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
644 tr.chrg.cpMin = 0;
645 tr.chrg.cpMax = nLen;
646 tr.lpstrText = data;
647 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
648 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
649 HeapFree( GetProcessHeap(), 0, data );
651 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
652 return 0;
655 case ID_EDIT_CHARFORMAT:
656 case ID_EDIT_DEFCHARFORMAT:
658 CHARFORMAT2W cf;
659 LRESULT i;
660 ZeroMemory(&cf, sizeof(cf));
661 cf.cbSize = sizeof(cf);
662 cf.dwMask = 0;
663 i = SendMessage(hwndEditor, EM_GETCHARFORMAT,
664 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
665 return 0;
668 case ID_EDIT_PARAFORMAT:
670 PARAFORMAT2 pf;
671 ZeroMemory(&pf, sizeof(pf));
672 pf.cbSize = sizeof(pf);
673 SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
674 return 0;
677 case ID_EDIT_SELECTIONINFO:
679 CHARRANGE range = {0, -1};
680 char buf[128];
681 WCHAR *data = NULL;
683 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
684 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
685 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
686 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
687 MessageBoxA(hWnd, buf, "Editor", MB_OK);
688 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
689 HeapFree( GetProcessHeap(), 0, data);
690 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
691 return 0;
694 case ID_EDIT_READONLY:
696 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
697 if (nStyle & ES_READONLY)
698 SendMessage(hwndEditor, EM_SETREADONLY, 0, 0);
699 else
700 SendMessage(hwndEditor, EM_SETREADONLY, 1, 0);
701 return 0;
704 case ID_EDIT_MODIFIED:
705 if (SendMessage(hwndEditor, EM_GETMODIFY, 0, 0))
706 SendMessage(hwndEditor, EM_SETMODIFY, 0, 0);
707 else
708 SendMessage(hwndEditor, EM_SETMODIFY, 1, 0);
709 return 0;
711 case ID_EDIT_UNDO:
712 SendMessage(hwndEditor, EM_UNDO, 0, 0);
713 return 0;
715 case ID_EDIT_REDO:
716 SendMessage(hwndEditor, EM_REDO, 0, 0);
717 return 0;
719 case ID_ALIGN_LEFT:
720 case ID_ALIGN_CENTER:
721 case ID_ALIGN_RIGHT:
723 PARAFORMAT2 pf;
725 pf.cbSize = sizeof(pf);
726 pf.dwMask = PFM_ALIGNMENT;
727 switch(LOWORD(wParam)) {
728 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
729 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
730 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
732 SendMessage(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
733 break;
736 case ID_BACK_1:
737 SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
738 break;
740 case ID_BACK_2:
741 SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
742 break;
744 case ID_TOGGLE_TOOLBAR:
745 toggle_toolbar(BANDID_TOOLBAR);
746 update_window();
747 break;
749 case ID_TOGGLE_FORMATBAR:
750 toggle_toolbar(BANDID_FORMATBAR);
751 update_window();
752 break;
754 case ID_TOGGLE_STATUSBAR:
755 ShowWindow(hwndStatus, IsWindowVisible(hwndStatus) ? SW_HIDE : SW_SHOW);
756 update_window();
757 break;
759 default:
760 SendMessage(hwndEditor, WM_COMMAND, wParam, lParam);
761 break;
763 return 0;
766 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
768 HMENU hMenu = (HMENU)wParam;
769 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
770 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
771 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
772 PARAFORMAT pf;
773 int nAlignment = -1;
774 REBARBANDINFOW rbbinfo;
776 pf.cbSize = sizeof(PARAFORMAT);
777 SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
778 CheckMenuItem(hMenu, ID_EDIT_READONLY,
779 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
780 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
781 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
782 if (pf.dwMask & PFM_ALIGNMENT)
783 nAlignment = pf.wAlignment;
784 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
785 MF_CHECKED : MF_UNCHECKED);
786 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
787 MF_CHECKED : MF_UNCHECKED);
788 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
789 MF_CHECKED : MF_UNCHECKED);
790 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
791 MF_ENABLED : MF_GRAYED);
792 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
793 MF_ENABLED : MF_GRAYED);
795 rbbinfo.cbSize = sizeof(rbbinfo);
796 rbbinfo.fMask = RBBIM_STYLE;
797 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_TOOLBAR, (LPARAM)&rbbinfo);
799 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(rbbinfo.fStyle & RBBS_HIDDEN) ?
800 MF_UNCHECKED : MF_CHECKED);
802 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
804 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(rbbinfo.fStyle & RBBS_HIDDEN) ?
805 MF_UNCHECKED : MF_CHECKED);
807 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
808 MF_CHECKED : MF_UNCHECKED);
809 return 0;
812 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
814 int nStatusSize = 0;
815 RECT rc;
816 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
817 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
818 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
819 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
820 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
821 int rebarHeight = 0;
822 REBARBANDINFOW rbbinfo;
823 int rebarRows = 2;
825 if (hwndStatusBar)
827 SendMessage(hwndStatusBar, WM_SIZE, 0, 0);
828 if (IsWindowVisible(hwndStatusBar))
830 GetClientRect(hwndStatusBar, &rc);
831 nStatusSize = rc.bottom - rc.top;
832 } else
834 nStatusSize = 0;
837 if (hwndToolBar)
839 rc.left = rc.top = 0;
840 rc.right = LOWORD(lParam);
841 rc.bottom = HIWORD(lParam);
842 SendMessage(hwndToolBar, TB_AUTOSIZE, 0, 0);
843 SendMessage(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
844 GetClientRect(hwndReBar, &rc);
845 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
847 if (hwndFormatBar)
849 rc.left = rc.top = 0;
850 rc.right = LOWORD(lParam);
851 rc.bottom = HIWORD(lParam);
852 SendMessageW(hwndFormatBar, TB_AUTOSIZE, 0, 0);
853 SendMessageW(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
854 GetClientRect(hwndReBar, &rc);
855 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
857 if (hwndReBar)
859 rbbinfo.cbSize = sizeof(rbbinfo);
860 rbbinfo.fMask = RBBIM_STYLE;
862 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_TOOLBAR, (LPARAM)&rbbinfo);
863 if(rbbinfo.fStyle & RBBS_HIDDEN)
864 rebarRows--;
866 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
867 if(rbbinfo.fStyle & RBBS_HIDDEN)
868 rebarRows--;
870 rebarHeight = rebarRows ? SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0) : 0;
872 if (hwndEditor)
874 GetClientRect(hWnd, &rc);
875 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
878 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
881 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
883 switch(msg)
885 case WM_CREATE:
886 return OnCreate( hWnd, wParam, lParam );
888 case WM_USER:
889 return OnUser( hWnd, wParam, lParam );
891 case WM_NOTIFY:
892 return OnNotify( hWnd, wParam, lParam );
894 case WM_COMMAND:
895 return OnCommand( hWnd, wParam, lParam );
897 case WM_DESTROY:
898 PostQuitMessage(0);
899 break;
901 case WM_ACTIVATE:
902 if (LOWORD(wParam))
903 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
904 return 0;
906 case WM_INITMENUPOPUP:
907 return OnInitPopupMenu( hWnd, wParam, lParam );
909 case WM_SIZE:
910 return OnSize( hWnd, wParam, lParam );
912 default:
913 return DefWindowProcW(hWnd, msg, wParam, lParam);
916 return 0;
919 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
921 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES};
922 HACCEL hAccel;
923 WNDCLASSW wc;
924 MSG msg;
926 InitCommonControlsEx(&classes);
928 hAccel = LoadAccelerators(hInstance, "MAINACCELTABLE");
930 wc.style = CS_HREDRAW | CS_VREDRAW;
931 wc.lpfnWndProc = WndProc;
932 wc.cbClsExtra = 0;
933 wc.cbWndExtra = 4;
934 wc.hInstance = hInstance;
935 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
936 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
937 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
938 wc.lpszMenuName = xszMainMenu;
939 wc.lpszClassName = wszMainWndClass;
940 RegisterClassW(&wc);
942 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
943 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
944 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
946 HandleCommandLine(GetCommandLineW());
948 while(GetMessage(&msg,0,0,0))
950 if (TranslateAccelerator(hMainWnd, hAccel, &msg))
951 continue;
952 TranslateMessage(&msg);
953 DispatchMessage(&msg);
954 if (!PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
955 SendMessage(hMainWnd, WM_USER, 0, 0);
958 return 0;