wordpad: Allow toggling of toolbar.
[wine/wine-gecko.git] / programs / wordpad / wordpad.c
blob7a16b15ff4f4f381edaa6b6d45138cd0307e92ed
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 toggle_toolbar(int bandId)
331 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
332 REBARBANDINFOW rbbinfo;
333 RECT rect;
335 if(!hwndReBar)
336 return;
338 rbbinfo.cbSize = sizeof(rbbinfo);
339 rbbinfo.fMask = RBBIM_STYLE;
341 SendMessageW(hwndReBar, RB_GETBANDINFO, bandId, (LPARAM)&rbbinfo);
343 SendMessageW(hwndReBar, RB_SHOWBAND, bandId, (rbbinfo.fStyle & RBBS_HIDDEN));
345 GetWindowRect(hMainWnd, &rect);
347 (void) OnSize(hMainWnd, SIZE_RESTORED, MAKELONG(rect.bottom, rect.right));
350 static int rebar_height(void)
352 HWND hwndReBar = GetDlgItem(hMainWnd, IDC_REBAR);
354 REBARBANDINFOW rbbinfo;
356 if(!hwndReBar)
357 return 0;
359 rbbinfo.cbSize = sizeof(rbbinfo);
360 rbbinfo.fMask = RBBIM_STYLE;
361 SendMessageW(hwndReBar, RB_GETBANDINFO, BANDID_TOOLBAR, (LPARAM)&rbbinfo);
363 return (rbbinfo.fStyle & RBBS_HIDDEN) ? 0 : SendMessage(hwndReBar, RB_GETBARHEIGHT, 0, 0);
366 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
368 HWND hToolBarWnd, hReBarWnd;
369 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
370 HANDLE hDLL;
371 TBADDBITMAP ab;
372 int nStdBitmaps = 0;
373 REBARINFO rbi;
374 REBARBANDINFO rbb;
376 CreateStatusWindow(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, "RichEdit text", hWnd, IDC_STATUSBAR);
378 hReBarWnd = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL,
379 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
380 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
382 rbi.cbSize = sizeof(rbi);
383 rbi.fMask = 0;
384 rbi.himl = NULL;
385 if(!SendMessage(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
386 return -1;
388 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
389 IDC_TOOLBAR,
390 6, hInstance, IDB_TOOLBAR,
391 NULL, 0,
392 24, 24, 16, 16, sizeof(TBBUTTON));
394 ab.hInst = HINST_COMMCTRL;
395 ab.nID = IDB_STD_SMALL_COLOR;
396 nStdBitmaps = SendMessage(hToolBarWnd, TB_ADDBITMAP, 6, (LPARAM)&ab);
397 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
398 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
399 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
400 AddSeparator(hToolBarWnd);
401 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT);
402 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
403 AddSeparator(hToolBarWnd);
404 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
405 AddSeparator(hToolBarWnd);
406 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
407 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
408 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
409 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
410 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
411 AddSeparator(hToolBarWnd);
412 AddButton(hToolBarWnd, 0, ID_FORMAT_BOLD);
413 AddButton(hToolBarWnd, 1, ID_FORMAT_ITALIC);
414 AddButton(hToolBarWnd, 2, ID_FORMAT_UNDERLINE);
415 AddSeparator(hToolBarWnd);
416 AddButton(hToolBarWnd, 3, ID_ALIGN_LEFT);
417 AddButton(hToolBarWnd, 4, ID_ALIGN_CENTER);
418 AddButton(hToolBarWnd, 5, ID_ALIGN_RIGHT);
420 SendMessage(hToolBarWnd, TB_ADDSTRING, 0, (LPARAM)"Exit\0");
421 SendMessage(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;
426 rbb.cx = 500;
427 rbb.hwndChild = hToolBarWnd;
428 rbb.cxMinChild = 0;
429 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessage(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
431 SendMessageW(hReBarWnd, RB_INSERTBAND, BANDID_TOOLBAR, (LPARAM)&rbb);
433 hDLL = LoadLibrary("RICHED20.DLL");
434 assert(hDLL);
436 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
437 WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
438 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
439 if (!hEditorWnd)
441 fprintf(stderr, "Error code %u\n", GetLastError());
442 return -1;
444 assert(hEditorWnd);
446 SetFocus(hEditorWnd);
447 SendMessage(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
449 DoDefaultFont();
451 DoLoadStrings();
453 return 0;
456 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
458 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
459 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
460 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
461 int from, to;
462 CHARFORMAT2W fmt;
463 PARAFORMAT2 pf;
465 ZeroMemory(&fmt, sizeof(fmt));
466 fmt.cbSize = sizeof(fmt);
468 ZeroMemory(&pf, sizeof(pf));
469 pf.cbSize = sizeof(pf);
471 SendMessage(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
473 SendMessage(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
474 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
475 SendMessage(hwndEditor, EM_CANUNDO, 0, 0));
476 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
477 SendMessage(hwndEditor, EM_CANREDO, 0, 0));
478 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
479 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
480 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) && (fmt.dwEffects & CFE_BOLD));
481 SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
482 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) && (fmt.dwEffects & CFE_ITALIC));
483 SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
484 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) && (fmt.dwEffects & CFE_UNDERLINE));
485 SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
487 SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
488 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
489 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
490 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
492 return 0;
495 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
497 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
498 NMHDR *pHdr = (NMHDR *)lParam;
500 if (pHdr->hwndFrom != hwndEditor)
501 return 0;
503 if (pHdr->code == EN_SELCHANGE)
505 SELCHANGE *pSC = (SELCHANGE *)lParam;
506 char buf[128];
508 sprintf( buf,"selection = %d..%d, line count=%ld",
509 pSC->chrg.cpMin, pSC->chrg.cpMax,
510 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
511 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
512 SendMessage(hWnd, WM_USER, 0, 0);
513 return 1;
515 return 0;
518 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
520 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
522 if ((HWND)lParam == hwndEditor)
523 return 0;
525 switch(LOWORD(wParam))
527 case ID_FILE_EXIT:
528 PostMessage(hWnd, WM_CLOSE, 0, 0);
529 break;
531 case ID_FILE_NEW:
532 SetWindowTextA(hwndEditor, "");
533 set_caption(NULL);
534 wszFileName[0] = '\0';
535 /* FIXME: set default format too */
536 break;
538 case ID_FILE_OPEN:
539 DialogOpenFile();
540 break;
542 case ID_FILE_SAVE:
543 if(wszFileName[0])
545 DoSaveFile(wszFileName);
546 break;
548 /* Fall through */
550 case ID_FILE_SAVEAS:
551 DialogSaveFile();
552 break;
554 case ID_PRINT:
555 case ID_PREVIEW:
556 case ID_FIND:
557 MessageBox(hWnd, "Not implemented", "WordPad", MB_OK);
558 break;
560 case ID_FORMAT_BOLD:
561 case ID_FORMAT_ITALIC:
562 case ID_FORMAT_UNDERLINE:
564 CHARFORMAT2W fmt;
565 int mask = CFM_BOLD;
566 if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
567 if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
569 ZeroMemory(&fmt, sizeof(fmt));
570 fmt.cbSize = sizeof(fmt);
571 SendMessage(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
572 if (!(fmt.dwMask&mask))
573 fmt.dwEffects |= mask;
574 else
575 fmt.dwEffects ^= mask;
576 fmt.dwMask = mask;
577 SendMessage(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
578 break;
581 case ID_EDIT_CUT:
582 PostMessage(hwndEditor, WM_CUT, 0, 0);
583 break;
585 case ID_EDIT_COPY:
586 PostMessage(hwndEditor, WM_COPY, 0, 0);
587 break;
589 case ID_EDIT_PASTE:
590 PostMessage(hwndEditor, WM_PASTE, 0, 0);
591 break;
593 case ID_EDIT_CLEAR:
594 PostMessage(hwndEditor, WM_CLEAR, 0, 0);
595 break;
597 case ID_EDIT_SELECTALL:
599 CHARRANGE range = {0, -1};
600 SendMessage(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
601 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
602 return 0;
605 case ID_EDIT_GETTEXT:
607 int nLen = GetWindowTextLengthW(hwndEditor);
608 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
609 TEXTRANGEW tr;
611 GetWindowTextW(hwndEditor, data, nLen+1);
612 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
614 HeapFree( GetProcessHeap(), 0, data);
615 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
616 tr.chrg.cpMin = 0;
617 tr.chrg.cpMax = nLen;
618 tr.lpstrText = data;
619 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
620 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
621 HeapFree( GetProcessHeap(), 0, data );
623 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
624 return 0;
627 case ID_EDIT_CHARFORMAT:
628 case ID_EDIT_DEFCHARFORMAT:
630 CHARFORMAT2W cf;
631 LRESULT i;
632 ZeroMemory(&cf, sizeof(cf));
633 cf.cbSize = sizeof(cf);
634 cf.dwMask = 0;
635 i = SendMessage(hwndEditor, EM_GETCHARFORMAT,
636 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
637 return 0;
640 case ID_EDIT_PARAFORMAT:
642 PARAFORMAT2 pf;
643 ZeroMemory(&pf, sizeof(pf));
644 pf.cbSize = sizeof(pf);
645 SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
646 return 0;
649 case ID_EDIT_SELECTIONINFO:
651 CHARRANGE range = {0, -1};
652 char buf[128];
653 WCHAR *data = NULL;
655 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
656 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
657 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
658 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
659 MessageBoxA(hWnd, buf, "Editor", MB_OK);
660 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
661 HeapFree( GetProcessHeap(), 0, data);
662 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
663 return 0;
666 case ID_EDIT_READONLY:
668 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
669 if (nStyle & ES_READONLY)
670 SendMessage(hwndEditor, EM_SETREADONLY, 0, 0);
671 else
672 SendMessage(hwndEditor, EM_SETREADONLY, 1, 0);
673 return 0;
676 case ID_EDIT_MODIFIED:
677 if (SendMessage(hwndEditor, EM_GETMODIFY, 0, 0))
678 SendMessage(hwndEditor, EM_SETMODIFY, 0, 0);
679 else
680 SendMessage(hwndEditor, EM_SETMODIFY, 1, 0);
681 return 0;
683 case ID_EDIT_UNDO:
684 SendMessage(hwndEditor, EM_UNDO, 0, 0);
685 return 0;
687 case ID_EDIT_REDO:
688 SendMessage(hwndEditor, EM_REDO, 0, 0);
689 return 0;
691 case ID_ALIGN_LEFT:
692 case ID_ALIGN_CENTER:
693 case ID_ALIGN_RIGHT:
695 PARAFORMAT2 pf;
697 pf.cbSize = sizeof(pf);
698 pf.dwMask = PFM_ALIGNMENT;
699 switch(LOWORD(wParam)) {
700 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
701 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
702 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
704 SendMessage(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
705 break;
708 case ID_BACK_1:
709 SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
710 break;
712 case ID_BACK_2:
713 SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
714 break;
716 case ID_TOGGLE_TOOLBAR:
717 toggle_toolbar(BANDID_TOOLBAR);
718 break;
720 default:
721 SendMessage(hwndEditor, WM_COMMAND, wParam, lParam);
722 break;
724 return 0;
727 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
729 HMENU hMenu = (HMENU)wParam;
730 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
731 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
732 PARAFORMAT pf;
733 int nAlignment = -1;
734 REBARBANDINFOW rbbinfo;
736 pf.cbSize = sizeof(PARAFORMAT);
737 SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
738 CheckMenuItem(hMenu, ID_EDIT_READONLY,
739 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
740 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
741 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
742 if (pf.dwMask & PFM_ALIGNMENT)
743 nAlignment = pf.wAlignment;
744 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
745 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
746 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
747 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANUNDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
748 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANREDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
750 rbbinfo.cbSize = sizeof(rbbinfo);
751 rbbinfo.fMask = RBBIM_STYLE;
752 SendMessageW(hwndReBar, RB_GETBANDINFO, 0, (LPARAM)&rbbinfo);
754 CheckMenuItem(hMenu, ID_TOGGLE_TOOLBAR, MF_BYCOMMAND|(rbbinfo.fStyle & RBBS_HIDDEN) ?
755 MF_UNCHECKED : MF_CHECKED);
757 return 0;
760 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
762 int nStatusSize = 0;
763 RECT rc;
764 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
765 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
766 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
767 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
768 int rebarHeight;
770 if (hwndStatusBar)
772 SendMessage(hwndStatusBar, WM_SIZE, 0, 0);
773 GetClientRect(hwndStatusBar, &rc);
774 nStatusSize = rc.bottom - rc.top;
776 if (hwndToolBar)
778 rc.left = rc.top = 0;
779 rc.right = LOWORD(lParam);
780 rc.bottom = HIWORD(lParam);
781 SendMessage(hwndToolBar, TB_AUTOSIZE, 0, 0);
782 SendMessage(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
783 GetClientRect(hwndReBar, &rc);
784 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
786 if (hwndEditor)
788 rebarHeight = rebar_height();
789 GetClientRect(hWnd, &rc);
790 MoveWindow(hwndEditor, 0, rebarHeight, rc.right, rc.bottom-nStatusSize-rebarHeight, TRUE);
793 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
796 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
798 switch(msg)
800 case WM_CREATE:
801 return OnCreate( hWnd, wParam, lParam );
803 case WM_USER:
804 return OnUser( hWnd, wParam, lParam );
806 case WM_NOTIFY:
807 return OnNotify( hWnd, wParam, lParam );
809 case WM_COMMAND:
810 return OnCommand( hWnd, wParam, lParam );
812 case WM_DESTROY:
813 PostQuitMessage(0);
814 break;
816 case WM_ACTIVATE:
817 if (LOWORD(wParam))
818 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
819 return 0;
821 case WM_INITMENUPOPUP:
822 return OnInitPopupMenu( hWnd, wParam, lParam );
824 case WM_SIZE:
825 return OnSize( hWnd, wParam, lParam );
827 default:
828 return DefWindowProcW(hWnd, msg, wParam, lParam);
831 return 0;
834 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
836 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES};
837 HACCEL hAccel;
838 WNDCLASSW wc;
839 MSG msg;
841 InitCommonControlsEx(&classes);
843 hAccel = LoadAccelerators(hInstance, "MAINACCELTABLE");
845 wc.style = CS_HREDRAW | CS_VREDRAW;
846 wc.lpfnWndProc = WndProc;
847 wc.cbClsExtra = 0;
848 wc.cbWndExtra = 4;
849 wc.hInstance = hInstance;
850 wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_WORDPAD));
851 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
852 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
853 wc.lpszMenuName = xszMainMenu;
854 wc.lpszClassName = wszMainWndClass;
855 RegisterClassW(&wc);
857 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
858 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
859 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
861 HandleCommandLine(GetCommandLineW());
863 while(GetMessage(&msg,0,0,0))
865 if (TranslateAccelerator(hMainWnd, hAccel, &msg))
866 continue;
867 TranslateMessage(&msg);
868 DispatchMessage(&msg);
869 if (!PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
870 SendMessage(hMainWnd, WM_USER, 0, 0);
873 return 0;