wordpad: Disable copy/cut in menu when appropriate.
[wine/wine-kai.git] / programs / wordpad / wordpad.c
blob9f0fdf8a722a1c5ecaafda9f74aaab3a4f29bab6
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 | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
323 fmt.dwEffects = 0;
325 lstrcpyW(fmt.szFaceName, szFaceName);
327 SendMessageW(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
330 static void update_window(void)
332 RECT rect;
334 GetWindowRect(hMainWnd, &rect);
336 (void) OnSize(hMainWnd, SIZE_RESTORED, MAKELONG(rect.bottom, rect.right));
339 static void toggle_toolbar(int bandId)
341 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
342 REBARBANDINFOW rbbinfo;
343 BOOL hide = TRUE;
345 if(!hwndReBar)
346 return;
348 rbbinfo.cbSize = sizeof(rbbinfo);
349 rbbinfo.fMask = RBBIM_STYLE | RBBIM_SIZE;
351 SendMessageW(hwndReBar, RB_GETBANDINFO, bandId, (LPARAM)&rbbinfo);
353 if(rbbinfo.fStyle & RBBS_HIDDEN)
354 hide = FALSE;
356 SendMessageW(hwndReBar, RB_SHOWBAND, bandId, hide ? 0 : 1);
358 if(bandId == BANDID_TOOLBAR)
360 rbbinfo.fMask ^= RBBIM_SIZE;
362 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
364 if(hide)
365 rbbinfo.fStyle ^= RBBS_BREAK;
366 else
367 rbbinfo.fStyle |= RBBS_BREAK;
369 SendMessageW(hwndReBar, RB_SETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
373 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
375 HWND hToolBarWnd, hFormatBarWnd, hReBarWnd;
376 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
377 HANDLE hDLL;
378 TBADDBITMAP ab;
379 int nStdBitmaps = 0;
380 REBARINFO rbi;
381 REBARBANDINFOW rbb;
382 static const WCHAR wszRichEditDll[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
383 static const WCHAR wszRichEditText[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
385 CreateStatusWindowW(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, wszRichEditText, hWnd, IDC_STATUSBAR);
387 hReBarWnd = CreateWindowExW(WS_EX_TOOLWINDOW, REBARCLASSNAMEW, NULL,
388 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
389 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
391 rbi.cbSize = sizeof(rbi);
392 rbi.fMask = 0;
393 rbi.himl = NULL;
394 if(!SendMessageW(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
395 return -1;
397 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
398 IDC_TOOLBAR,
399 0, hInstance, 0,
400 NULL, 0,
401 24, 24, 16, 16, sizeof(TBBUTTON));
403 ab.hInst = HINST_COMMCTRL;
404 ab.nID = IDB_STD_SMALL_COLOR;
405 nStdBitmaps = SendMessageW(hToolBarWnd, TB_ADDBITMAP, 0, (LPARAM)&ab);
407 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
408 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
409 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
410 AddSeparator(hToolBarWnd);
411 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT);
412 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
413 AddSeparator(hToolBarWnd);
414 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
415 AddSeparator(hToolBarWnd);
416 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
417 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
418 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
419 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
420 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
422 SendMessageW(hToolBarWnd, TB_AUTOSIZE, 0, 0);
424 rbb.cbSize = sizeof(rbb);
425 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE;
426 rbb.fStyle = RBBS_CHILDEDGE | RBBS_BREAK | RBBS_NOGRIPPER;
427 rbb.cx = 0;
428 rbb.hwndChild = hToolBarWnd;
429 rbb.cxMinChild = 0;
430 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessageW(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
432 SendMessageW(hReBarWnd, RB_INSERTBAND, BANDID_TOOLBAR, (LPARAM)&rbb);
434 hFormatBarWnd = CreateToolbarEx(hReBarWnd,
435 CCS_NOPARENTALIGN | CCS_NOMOVEY | WS_VISIBLE | TBSTYLE_TOOLTIPS | TBSTYLE_BUTTON,
436 IDC_FORMATBAR, 6, hInstance, IDB_FORMATBAR, NULL, 0, 24, 24, 16, 16, sizeof(TBBUTTON));
438 ab.hInst = HINST_COMMCTRL;
439 ab.nID = IDB_STD_SMALL_COLOR;
440 nStdBitmaps = SendMessageW(hFormatBarWnd, TB_ADDBITMAP, 6, (LPARAM)&ab);
442 AddButton(hFormatBarWnd, 0, ID_FORMAT_BOLD);
443 AddButton(hFormatBarWnd, 1, ID_FORMAT_ITALIC);
444 AddButton(hFormatBarWnd, 2, ID_FORMAT_UNDERLINE);
445 AddSeparator(hFormatBarWnd);
446 AddButton(hFormatBarWnd, 3, ID_ALIGN_LEFT);
447 AddButton(hFormatBarWnd, 4, ID_ALIGN_CENTER);
448 AddButton(hFormatBarWnd, 5, ID_ALIGN_RIGHT);
450 SendMessageW(hFormatBarWnd, TB_AUTOSIZE, 0, 0);
452 rbb.hwndChild = hFormatBarWnd;
454 SendMessageW(hReBarWnd, RB_INSERTBAND, BANDID_FORMATBAR, (LPARAM)&rbb);
456 hDLL = LoadLibraryW(wszRichEditDll);
457 assert(hDLL);
459 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
460 WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
461 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
463 if (!hEditorWnd)
465 fprintf(stderr, "Error code %u\n", GetLastError());
466 return -1;
468 assert(hEditorWnd);
470 SetFocus(hEditorWnd);
471 SendMessageW(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
473 DoDefaultFont();
475 DoLoadStrings();
477 return 0;
480 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
482 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
483 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
484 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
485 HWND hwndFormatBar = GetDlgItem(hwndReBar, IDC_FORMATBAR);
486 int from, to;
487 CHARFORMAT2W fmt;
488 PARAFORMAT2 pf;
490 ZeroMemory(&fmt, sizeof(fmt));
491 fmt.cbSize = sizeof(fmt);
493 ZeroMemory(&pf, sizeof(pf));
494 pf.cbSize = sizeof(pf);
496 SendMessageW(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
498 SendMessageW(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
499 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
500 SendMessageW(hwndEditor, EM_CANUNDO, 0, 0));
501 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
502 SendMessageW(hwndEditor, EM_CANREDO, 0, 0));
503 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
504 SendMessageW(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
506 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) &&
507 (fmt.dwEffects & CFE_BOLD));
508 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
509 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) &&
510 (fmt.dwEffects & CFE_ITALIC));
511 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
512 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) &&
513 (fmt.dwEffects & CFE_UNDERLINE));
514 SendMessageW(hwndFormatBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
516 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
517 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
518 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
519 SendMessageW(hwndFormatBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
521 return 0;
524 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
526 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
527 NMHDR *pHdr = (NMHDR *)lParam;
529 if (pHdr->hwndFrom != hwndEditor)
530 return 0;
532 if (pHdr->code == EN_SELCHANGE)
534 SELCHANGE *pSC = (SELCHANGE *)lParam;
535 char buf[128];
537 sprintf( buf,"selection = %d..%d, line count=%ld",
538 pSC->chrg.cpMin, pSC->chrg.cpMax,
539 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
540 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
541 SendMessage(hWnd, WM_USER, 0, 0);
542 return 1;
544 return 0;
547 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
549 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
550 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
552 if ((HWND)lParam == hwndEditor)
553 return 0;
555 switch(LOWORD(wParam))
557 case ID_FILE_EXIT:
558 PostMessageW(hWnd, WM_CLOSE, 0, 0);
559 break;
561 case ID_FILE_NEW:
562 set_caption(NULL);
563 wszFileName[0] = '\0';
564 SetWindowTextW(hwndEditor, wszFileName);
565 /* FIXME: set default format too */
566 break;
568 case ID_FILE_OPEN:
569 DialogOpenFile();
570 break;
572 case ID_FILE_SAVE:
573 if(wszFileName[0])
575 DoSaveFile(wszFileName);
576 break;
578 /* Fall through */
580 case ID_FILE_SAVEAS:
581 DialogSaveFile();
582 break;
584 case ID_PRINT:
585 case ID_PREVIEW:
586 case ID_FIND:
588 static const WCHAR wszNotImplemented[] = {'N','o','t',' ',
589 'i','m','p','l','e','m','e','n','t','e','d','\0'};
590 MessageBoxW(hWnd, wszNotImplemented, wszAppTitle, MB_OK);
592 break;
594 case ID_FORMAT_BOLD:
595 case ID_FORMAT_ITALIC:
596 case ID_FORMAT_UNDERLINE:
598 CHARFORMAT2W fmt;
599 int mask = CFM_BOLD;
600 if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
601 if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
603 ZeroMemory(&fmt, sizeof(fmt));
604 fmt.cbSize = sizeof(fmt);
605 SendMessageW(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
606 if (!(fmt.dwMask&mask))
607 fmt.dwEffects |= mask;
608 else
609 fmt.dwEffects ^= mask;
610 fmt.dwMask = mask;
611 SendMessageW(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
612 break;
615 case ID_EDIT_CUT:
616 PostMessageW(hwndEditor, WM_CUT, 0, 0);
617 break;
619 case ID_EDIT_COPY:
620 PostMessageW(hwndEditor, WM_COPY, 0, 0);
621 break;
623 case ID_EDIT_PASTE:
624 PostMessageW(hwndEditor, WM_PASTE, 0, 0);
625 break;
627 case ID_EDIT_CLEAR:
628 PostMessageW(hwndEditor, WM_CLEAR, 0, 0);
629 break;
631 case ID_EDIT_SELECTALL:
633 CHARRANGE range = {0, -1};
634 SendMessageW(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
635 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
636 return 0;
639 case ID_EDIT_GETTEXT:
641 int nLen = GetWindowTextLengthW(hwndEditor);
642 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
643 TEXTRANGEW tr;
645 GetWindowTextW(hwndEditor, data, nLen+1);
646 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
648 HeapFree( GetProcessHeap(), 0, data);
649 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
650 tr.chrg.cpMin = 0;
651 tr.chrg.cpMax = nLen;
652 tr.lpstrText = data;
653 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
654 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
655 HeapFree( GetProcessHeap(), 0, data );
657 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
658 return 0;
661 case ID_EDIT_CHARFORMAT:
662 case ID_EDIT_DEFCHARFORMAT:
664 CHARFORMAT2W cf;
665 LRESULT i;
666 ZeroMemory(&cf, sizeof(cf));
667 cf.cbSize = sizeof(cf);
668 cf.dwMask = 0;
669 i = SendMessageW(hwndEditor, EM_GETCHARFORMAT,
670 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
671 return 0;
674 case ID_EDIT_PARAFORMAT:
676 PARAFORMAT2 pf;
677 ZeroMemory(&pf, sizeof(pf));
678 pf.cbSize = sizeof(pf);
679 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
680 return 0;
683 case ID_EDIT_SELECTIONINFO:
685 CHARRANGE range = {0, -1};
686 char buf[128];
687 WCHAR *data = NULL;
689 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
690 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
691 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
692 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
693 MessageBoxA(hWnd, buf, "Editor", MB_OK);
694 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
695 HeapFree( GetProcessHeap(), 0, data);
696 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
697 return 0;
700 case ID_EDIT_READONLY:
702 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
703 if (nStyle & ES_READONLY)
704 SendMessageW(hwndEditor, EM_SETREADONLY, 0, 0);
705 else
706 SendMessageW(hwndEditor, EM_SETREADONLY, 1, 0);
707 return 0;
710 case ID_EDIT_MODIFIED:
711 if (SendMessageW(hwndEditor, EM_GETMODIFY, 0, 0))
712 SendMessageW(hwndEditor, EM_SETMODIFY, 0, 0);
713 else
714 SendMessageW(hwndEditor, EM_SETMODIFY, 1, 0);
715 return 0;
717 case ID_EDIT_UNDO:
718 SendMessageW(hwndEditor, EM_UNDO, 0, 0);
719 return 0;
721 case ID_EDIT_REDO:
722 SendMessageW(hwndEditor, EM_REDO, 0, 0);
723 return 0;
725 case ID_ALIGN_LEFT:
726 case ID_ALIGN_CENTER:
727 case ID_ALIGN_RIGHT:
729 PARAFORMAT2 pf;
731 pf.cbSize = sizeof(pf);
732 pf.dwMask = PFM_ALIGNMENT;
733 switch(LOWORD(wParam)) {
734 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
735 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
736 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
738 SendMessageW(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
739 break;
742 case ID_BACK_1:
743 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
744 break;
746 case ID_BACK_2:
747 SendMessageW(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
748 break;
750 case ID_TOGGLE_TOOLBAR:
751 toggle_toolbar(BANDID_TOOLBAR);
752 update_window();
753 break;
755 case ID_TOGGLE_FORMATBAR:
756 toggle_toolbar(BANDID_FORMATBAR);
757 update_window();
758 break;
760 case ID_TOGGLE_STATUSBAR:
761 ShowWindow(hwndStatus, IsWindowVisible(hwndStatus) ? SW_HIDE : SW_SHOW);
762 update_window();
763 break;
765 default:
766 SendMessageW(hwndEditor, WM_COMMAND, wParam, lParam);
767 break;
769 return 0;
772 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
774 HMENU hMenu = (HMENU)wParam;
775 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
776 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
777 HWND hwndStatus = GetDlgItem(hWnd, IDC_STATUSBAR);
778 PARAFORMAT pf;
779 int nAlignment = -1;
780 REBARBANDINFOW rbbinfo;
781 int selFrom, selTo;
783 SendMessageW(hEditorWnd, EM_GETSEL, (WPARAM)&selFrom, (LPARAM)&selTo);
784 EnableMenuItem(hMenu, ID_EDIT_COPY, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
785 EnableMenuItem(hMenu, ID_EDIT_CUT, MF_BYCOMMAND|(selFrom == selTo) ? MF_GRAYED : MF_ENABLED);
787 pf.cbSize = sizeof(PARAFORMAT);
788 SendMessageW(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
789 CheckMenuItem(hMenu, ID_EDIT_READONLY,
790 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
791 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
792 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
793 if (pf.dwMask & PFM_ALIGNMENT)
794 nAlignment = pf.wAlignment;
795 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ?
796 MF_CHECKED : MF_UNCHECKED);
797 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ?
798 MF_CHECKED : MF_UNCHECKED);
799 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ?
800 MF_CHECKED : MF_UNCHECKED);
801 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANUNDO, 0, 0)) ?
802 MF_ENABLED : MF_GRAYED);
803 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessageW(hwndEditor, EM_CANREDO, 0, 0)) ?
804 MF_ENABLED : MF_GRAYED);
806 rbbinfo.cbSize = sizeof(rbbinfo);
807 rbbinfo.fMask = RBBIM_STYLE;
808 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_TOOLBAR, (LPARAM)&rbbinfo);
810 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(rbbinfo.fStyle & RBBS_HIDDEN) ?
811 MF_UNCHECKED : MF_CHECKED);
813 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
815 CheckMenuItem(hMenu, ID_TOGGLE_FORMATBAR, MF_BYCOMMAND|(rbbinfo.fStyle & RBBS_HIDDEN) ?
816 MF_UNCHECKED : MF_CHECKED);
818 CheckMenuItem(hMenu, ID_TOGGLE_STATUSBAR, MF_BYCOMMAND|IsWindowVisible(hwndStatus) ?
819 MF_CHECKED : MF_UNCHECKED);
820 return 0;
823 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
825 int nStatusSize = 0;
826 RECT rc;
827 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
828 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
829 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
830 int rebarHeight = 0;
831 REBARBANDINFOW rbbinfo;
832 int rebarRows = 2;
834 if (hwndStatusBar)
836 SendMessageW(hwndStatusBar, WM_SIZE, 0, 0);
837 if (IsWindowVisible(hwndStatusBar))
839 GetClientRect(hwndStatusBar, &rc);
840 nStatusSize = rc.bottom - rc.top;
841 } else
843 nStatusSize = 0;
846 if (hwndReBar)
848 rbbinfo.cbSize = sizeof(rbbinfo);
849 rbbinfo.fMask = RBBIM_STYLE;
851 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_TOOLBAR, (LPARAM)&rbbinfo);
852 if(rbbinfo.fStyle & RBBS_HIDDEN)
853 rebarRows--;
855 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_FORMATBAR, (LPARAM)&rbbinfo);
856 if(rbbinfo.fStyle & RBBS_HIDDEN)
857 rebarRows--;
859 rebarHeight = rebarRows ? SendMessageW(hwndReBar, RB_GETBARHEIGHT, 0, 0) : 0;
861 if (hwndEditor)
863 GetClientRect(hWnd, &rc);
864 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
867 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
870 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
872 switch(msg)
874 case WM_CREATE:
875 return OnCreate( hWnd, wParam, lParam );
877 case WM_USER:
878 return OnUser( hWnd, wParam, lParam );
880 case WM_NOTIFY:
881 return OnNotify( hWnd, wParam, lParam );
883 case WM_COMMAND:
884 return OnCommand( hWnd, wParam, lParam );
886 case WM_DESTROY:
887 PostQuitMessage(0);
888 break;
890 case WM_ACTIVATE:
891 if (LOWORD(wParam))
892 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
893 return 0;
895 case WM_INITMENUPOPUP:
896 return OnInitPopupMenu( hWnd, wParam, lParam );
898 case WM_SIZE:
899 return OnSize( hWnd, wParam, lParam );
901 default:
902 return DefWindowProcW(hWnd, msg, wParam, lParam);
905 return 0;
908 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
910 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES};
911 HACCEL hAccel;
912 WNDCLASSW wc;
913 MSG msg;
914 static const WCHAR wszAccelTable[] = {'M','A','I','N','A','C','C','E','L',
915 'T','A','B','L','E','\0'};
917 InitCommonControlsEx(&classes);
919 hAccel = LoadAcceleratorsW(hInstance, wszAccelTable);
921 wc.style = CS_HREDRAW | CS_VREDRAW;
922 wc.lpfnWndProc = WndProc;
923 wc.cbClsExtra = 0;
924 wc.cbWndExtra = 4;
925 wc.hInstance = hInstance;
926 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
927 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
928 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
929 wc.lpszMenuName = xszMainMenu;
930 wc.lpszClassName = wszMainWndClass;
931 RegisterClassW(&wc);
933 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
934 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
935 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
937 HandleCommandLine(GetCommandLineW());
939 while(GetMessageW(&msg,0,0,0))
941 if (TranslateAcceleratorW(hMainWnd, hAccel, &msg))
942 continue;
943 TranslateMessage(&msg);
944 DispatchMessageW(&msg);
945 if (!PeekMessageW(&msg, 0, 0, 0, PM_NOREMOVE))
946 SendMessageW(hMainWnd, WM_USER, 0, 0);
949 return 0;