comctl32: rebar: WM_SETFONT should send a RBN_HEIGHTCHANGE if necessary.
[wine/winequartzdrv.git] / programs / wordpad / wordpad.c
blob2c650e0335a3aa86dbe68d9eec2ac119d9bbceb1
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 char szFilter[MAX_STRING_LEN];
51 /* Load string resources */
52 static void DoLoadStrings()
54 LPSTR p = szFilter;
55 char files_rtf[] = "*.rtf";
56 char files_txt[] = "*.txt";
57 char files_all[] = "*.*";
58 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hMainWnd, GWLP_HINSTANCE);
60 LoadString(hInstance, STRING_RICHTEXT_FILES_RTF, p, MAX_STRING_LEN);
61 p += strlen(p) + 1;
62 lstrcpy(p, files_rtf);
63 p += strlen(p) + 1;
64 LoadString(hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
65 p += strlen(p) + 1;
66 lstrcpy(p, files_txt);
67 p += strlen(p) + 1;
68 LoadString(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
69 p += strlen(p) + 1;
70 lstrcpy(p, files_all);
71 p += strlen(p) + 1;
72 *p = '\0';
75 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
77 TBBUTTON button;
79 ZeroMemory(&button, sizeof(button));
80 button.iBitmap = nImage;
81 button.idCommand = nCommand;
82 button.fsState = TBSTATE_ENABLED;
83 button.fsStyle = TBSTYLE_BUTTON;
84 button.dwData = 0;
85 button.iString = -1;
86 SendMessage(hwndToolBar, TB_ADDBUTTONS, 1, (LPARAM)&button);
89 static void AddSeparator(HWND hwndToolBar)
91 TBBUTTON button;
93 ZeroMemory(&button, sizeof(button));
94 button.iBitmap = -1;
95 button.idCommand = 0;
96 button.fsState = 0;
97 button.fsStyle = TBSTYLE_SEP;
98 button.dwData = 0;
99 button.iString = -1;
100 SendMessage(hwndToolBar, TB_ADDBUTTONS, 1, (LPARAM)&button);
103 static LPSTR stream_buffer;
104 static LONG stream_buffer_size;
106 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
108 LONG size = min(stream_buffer_size, cb);
110 memcpy(buffer, stream_buffer, size);
111 stream_buffer_size -= size;
112 stream_buffer += size;
113 *pcb = size;
114 return 0;
117 static void DoOpenFile(LPCWSTR szFileName)
119 HANDLE hFile;
120 LPSTR pTemp;
121 DWORD size;
122 DWORD dwNumRead;
123 EDITSTREAM es;
125 char szCaption[MAX_PATH];
126 char szAppTitle[sizeof(wszAppTitle)];
127 char szSeparator[] = " - ";
129 hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
130 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
131 if (hFile == INVALID_HANDLE_VALUE)
132 return;
134 size = GetFileSize(hFile, NULL);
135 if (size == INVALID_FILE_SIZE)
137 CloseHandle(hFile);
138 return;
140 size++;
142 pTemp = HeapAlloc(GetProcessHeap(), 0, size);
143 if (!pTemp)
145 CloseHandle(hFile);
146 return;
149 if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
151 CloseHandle(hFile);
152 HeapFree(GetProcessHeap(), 0, pTemp);
153 return;
155 CloseHandle(hFile);
156 pTemp[dwNumRead] = 0;
158 memset(&es, 0, sizeof(es));
159 es.pfnCallback = stream_in;
161 stream_buffer = pTemp;
162 stream_buffer_size = size;
164 SendMessage(hEditorWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
165 HeapFree(GetProcessHeap(), 0, pTemp);
167 SetFocus(hEditorWnd);
169 WideCharToMultiByte(CP_ACP, 0, wszAppTitle, -1, szAppTitle, sizeof(wszAppTitle), NULL, NULL);
171 WideCharToMultiByte(CP_ACP, 0, szFileName, -1, szCaption, MAX_PATH, NULL, NULL);
173 lstrcat(szCaption, szSeparator);
174 lstrcat(szCaption, szAppTitle);
176 SetWindowText(hMainWnd, szCaption);
179 static void DialogOpenFile()
181 OPENFILENAME ofn;
183 char szFile[MAX_PATH] = "";
184 char szDefExt[] = "rtf";
186 ZeroMemory(&ofn, sizeof(ofn));
188 ofn.lStructSize = sizeof(ofn);
189 ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
190 ofn.hwndOwner = hMainWnd;
191 ofn.lpstrFilter = szFilter;
192 ofn.lpstrFile = szFile;
193 ofn.nMaxFile = MAX_PATH;
194 ofn.lpstrDefExt = szDefExt;
196 if(GetOpenFileName(&ofn))
198 WCHAR szOpenFile[MAX_PATH];
200 MultiByteToWideChar(CP_ACP, 0, ofn.lpstrFile, MAX_PATH, szOpenFile, sizeof(szOpenFile)/sizeof(szOpenFile[0]));
202 DoOpenFile(szOpenFile);
206 static void HandleCommandLine(LPWSTR cmdline)
208 WCHAR delimiter;
209 int opt_print = 0;
211 /* skip white space */
212 while (*cmdline == ' ') cmdline++;
214 /* skip executable name */
215 delimiter = (*cmdline == '"' ? '"' : ' ');
217 if (*cmdline == delimiter) cmdline++;
218 while (*cmdline && *cmdline != delimiter) cmdline++;
219 if (*cmdline == delimiter) cmdline++;
221 while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
223 WCHAR option;
225 if (*cmdline++ == ' ') continue;
227 option = *cmdline;
228 if (option) cmdline++;
229 while (*cmdline == ' ') cmdline++;
231 switch (option)
233 case 'p':
234 case 'P':
235 opt_print = 1;
236 break;
240 if (*cmdline)
242 /* file name is passed on the command line */
243 if (cmdline[0] == '"')
245 cmdline++;
246 cmdline[lstrlenW(cmdline) - 1] = 0;
248 DoOpenFile(cmdline);
249 InvalidateRect(hMainWnd, NULL, FALSE);
252 if (opt_print)
253 MessageBox(hMainWnd, "Printing not implemented", "WordPad", MB_OK);
256 static void DoDefaultFont()
258 static const WCHAR szFaceName[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0};
259 CHARFORMAT2W fmt;
261 ZeroMemory(&fmt, sizeof(fmt));
263 fmt.cbSize = sizeof(fmt);
264 fmt.dwMask = CFM_FACE;
266 lstrcpyW(fmt.szFaceName, szFaceName);
268 SendMessage(hEditorWnd, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&fmt);
271 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
273 HWND hToolBarWnd, hReBarWnd;
274 HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
275 HANDLE hDLL;
276 TBADDBITMAP ab;
277 int nStdBitmaps = 0;
278 REBARINFO rbi;
279 REBARBANDINFO rbb;
281 CreateStatusWindow(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, "RichEdit text", hWnd, IDC_STATUSBAR);
283 hReBarWnd = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL,
284 CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
285 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
287 rbi.cbSize = sizeof(rbi);
288 rbi.fMask = 0;
289 rbi.himl = NULL;
290 if(!SendMessage(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
291 return -1;
293 hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
294 IDC_TOOLBAR,
295 3, hInstance, IDB_TOOLBAR,
296 NULL, 0,
297 24, 24, 16, 16, sizeof(TBBUTTON));
299 ab.hInst = HINST_COMMCTRL;
300 ab.nID = IDB_STD_SMALL_COLOR;
301 nStdBitmaps = SendMessage(hToolBarWnd, TB_ADDBITMAP, 3, (LPARAM)&ab);
302 AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
303 AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
304 AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
305 AddSeparator(hToolBarWnd);
306 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINT, ID_PRINT);
307 AddButton(hToolBarWnd, nStdBitmaps+STD_PRINTPRE, ID_PREVIEW);
308 AddSeparator(hToolBarWnd);
309 AddButton(hToolBarWnd, nStdBitmaps+STD_FIND, ID_FIND);
310 AddSeparator(hToolBarWnd);
311 AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
312 AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
313 AddButton(hToolBarWnd, nStdBitmaps+STD_PASTE, ID_EDIT_PASTE);
314 AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
315 AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
316 AddSeparator(hToolBarWnd);
317 AddButton(hToolBarWnd, 0, ID_FORMAT_BOLD);
318 AddButton(hToolBarWnd, 1, ID_FORMAT_ITALIC);
319 AddButton(hToolBarWnd, 2, ID_FORMAT_UNDERLINE);
321 SendMessage(hToolBarWnd, TB_ADDSTRING, 0, (LPARAM)"Exit\0");
322 SendMessage(hToolBarWnd, TB_AUTOSIZE, 0, 0);
324 rbb.cbSize = sizeof(rbb);
325 rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE;
326 rbb.fStyle = RBBS_CHILDEDGE;
327 rbb.cx = 500;
328 rbb.hwndChild = hToolBarWnd;
329 rbb.cxMinChild = 0;
330 rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessage(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
332 SendMessage(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
334 hDLL = LoadLibrary("RICHED20.DLL");
335 assert(hDLL);
337 hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
338 WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
339 0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
340 if (!hEditorWnd)
342 fprintf(stderr, "Error code %u\n", GetLastError());
343 return -1;
345 assert(hEditorWnd);
347 SetFocus(hEditorWnd);
348 SendMessage(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
350 DoDefaultFont();
352 DoLoadStrings();
354 return 0;
357 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
359 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
360 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
361 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
362 int from, to;
363 CHARFORMAT2W fmt;
365 ZeroMemory(&fmt, sizeof(fmt));
366 fmt.cbSize = sizeof(fmt);
368 SendMessage(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
370 SendMessage(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
371 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
372 SendMessage(hwndEditor, EM_CANUNDO, 0, 0));
373 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
374 SendMessage(hwndEditor, EM_CANREDO, 0, 0));
375 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
376 SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
377 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) && (fmt.dwEffects & CFE_BOLD));
378 SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
379 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) && (fmt.dwEffects & CFE_ITALIC));
380 SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
381 SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) && (fmt.dwEffects & CFE_UNDERLINE));
382 SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
383 return 0;
386 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
388 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
389 NMHDR *pHdr = (NMHDR *)lParam;
391 if (pHdr->hwndFrom != hwndEditor)
392 return 0;
394 if (pHdr->code == EN_SELCHANGE)
396 SELCHANGE *pSC = (SELCHANGE *)lParam;
397 char buf[128];
399 sprintf( buf,"selection = %d..%d, line count=%ld",
400 pSC->chrg.cpMin, pSC->chrg.cpMax,
401 SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
402 SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
403 SendMessage(hWnd, WM_USER, 0, 0);
404 return 1;
406 return 0;
409 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
411 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
413 if ((HWND)lParam == hwndEditor)
414 return 0;
416 switch(LOWORD(wParam))
418 case ID_FILE_EXIT:
419 PostMessage(hWnd, WM_CLOSE, 0, 0);
420 break;
422 case ID_FILE_NEW:
423 SetWindowTextA(hwndEditor, "");
424 SetWindowTextW(hMainWnd, wszAppTitle);
425 /* FIXME: set default format too */
426 break;
428 case ID_FILE_OPEN:
429 DialogOpenFile();
430 break;
432 case ID_FILE_SAVE:
433 case ID_PRINT:
434 case ID_PREVIEW:
435 case ID_FIND:
436 MessageBox(hWnd, "Not implemented", "WordPad", MB_OK);
437 break;
439 case ID_FORMAT_BOLD:
440 case ID_FORMAT_ITALIC:
441 case ID_FORMAT_UNDERLINE:
443 CHARFORMAT2W fmt;
444 int mask = CFM_BOLD;
445 if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
446 if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
448 ZeroMemory(&fmt, sizeof(fmt));
449 fmt.cbSize = sizeof(fmt);
450 SendMessage(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
451 if (!(fmt.dwMask&mask))
452 fmt.dwEffects |= mask;
453 else
454 fmt.dwEffects ^= mask;
455 fmt.dwMask = mask;
456 SendMessage(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
457 break;
460 case ID_EDIT_CUT:
461 PostMessage(hwndEditor, WM_CUT, 0, 0);
462 break;
464 case ID_EDIT_COPY:
465 PostMessage(hwndEditor, WM_COPY, 0, 0);
466 break;
468 case ID_EDIT_PASTE:
469 PostMessage(hwndEditor, WM_PASTE, 0, 0);
470 break;
472 case ID_EDIT_CLEAR:
473 PostMessage(hwndEditor, WM_CLEAR, 0, 0);
474 break;
476 case ID_EDIT_SELECTALL:
478 CHARRANGE range = {0, -1};
479 SendMessage(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
480 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
481 return 0;
484 case ID_EDIT_GETTEXT:
486 int nLen = GetWindowTextLengthW(hwndEditor);
487 LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
488 TEXTRANGEW tr;
490 GetWindowTextW(hwndEditor, data, nLen+1);
491 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
493 HeapFree( GetProcessHeap(), 0, data);
494 data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
495 tr.chrg.cpMin = 0;
496 tr.chrg.cpMax = nLen;
497 tr.lpstrText = data;
498 SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
499 MessageBoxW(NULL, data, xszAppTitle, MB_OK);
500 HeapFree( GetProcessHeap(), 0, data );
502 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
503 return 0;
506 case ID_EDIT_CHARFORMAT:
507 case ID_EDIT_DEFCHARFORMAT:
509 CHARFORMAT2W cf;
510 LRESULT i;
511 ZeroMemory(&cf, sizeof(cf));
512 cf.cbSize = sizeof(cf);
513 cf.dwMask = 0;
514 i = SendMessage(hwndEditor, EM_GETCHARFORMAT,
515 LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
516 return 0;
519 case ID_EDIT_PARAFORMAT:
521 PARAFORMAT2 pf;
522 ZeroMemory(&pf, sizeof(pf));
523 pf.cbSize = sizeof(pf);
524 SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
525 return 0;
528 case ID_EDIT_SELECTIONINFO:
530 CHARRANGE range = {0, -1};
531 char buf[128];
532 WCHAR *data = NULL;
534 SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
535 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
536 SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
537 sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
538 MessageBoxA(hWnd, buf, "Editor", MB_OK);
539 MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
540 HeapFree( GetProcessHeap(), 0, data);
541 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
542 return 0;
545 case ID_EDIT_READONLY:
547 long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
548 if (nStyle & ES_READONLY)
549 SendMessage(hwndEditor, EM_SETREADONLY, 0, 0);
550 else
551 SendMessage(hwndEditor, EM_SETREADONLY, 1, 0);
552 return 0;
555 case ID_EDIT_MODIFIED:
556 if (SendMessage(hwndEditor, EM_GETMODIFY, 0, 0))
557 SendMessage(hwndEditor, EM_SETMODIFY, 0, 0);
558 else
559 SendMessage(hwndEditor, EM_SETMODIFY, 1, 0);
560 return 0;
562 case ID_EDIT_UNDO:
563 SendMessage(hwndEditor, EM_UNDO, 0, 0);
564 return 0;
566 case ID_EDIT_REDO:
567 SendMessage(hwndEditor, EM_REDO, 0, 0);
568 return 0;
570 case ID_ALIGN_LEFT:
571 case ID_ALIGN_CENTER:
572 case ID_ALIGN_RIGHT:
574 PARAFORMAT2 pf;
576 pf.cbSize = sizeof(pf);
577 pf.dwMask = PFM_ALIGNMENT;
578 switch(LOWORD(wParam)) {
579 case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
580 case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
581 case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
583 SendMessage(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
584 break;
587 case ID_BACK_1:
588 SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
589 break;
591 case ID_BACK_2:
592 SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
593 break;
595 default:
596 SendMessage(hwndEditor, WM_COMMAND, wParam, lParam);
597 break;
599 return 0;
602 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
604 HMENU hMenu = (HMENU)wParam;
605 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
606 PARAFORMAT pf;
607 int nAlignment = -1;
609 pf.cbSize = sizeof(PARAFORMAT);
610 SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
611 CheckMenuItem(hMenu, ID_EDIT_READONLY,
612 MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
613 CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
614 MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
615 if (pf.dwMask & PFM_ALIGNMENT)
616 nAlignment = pf.wAlignment;
617 CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
618 CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
619 CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
620 EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANUNDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
621 EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANREDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
622 return 0;
625 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
627 int nStatusSize = 0, nTBSize = 0;
628 RECT rc;
629 HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
630 HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
631 HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
632 HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
634 if (hwndStatusBar)
636 SendMessage(hwndStatusBar, WM_SIZE, 0, 0);
637 GetClientRect(hwndStatusBar, &rc);
638 nStatusSize = rc.bottom - rc.top;
640 if (hwndToolBar)
642 rc.left = rc.top = 0;
643 rc.right = LOWORD(lParam);
644 rc.bottom = HIWORD(lParam);
645 SendMessage(hwndToolBar, TB_AUTOSIZE, 0, 0);
646 SendMessage(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
647 nTBSize = SendMessage(hwndReBar, RB_GETBARHEIGHT, 0, 0);
648 GetClientRect(hwndReBar, &rc);
649 MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
651 if (hwndEditor)
653 GetClientRect(hWnd, &rc);
654 MoveWindow(hwndEditor, 0, nTBSize, rc.right, rc.bottom-nStatusSize-nTBSize, TRUE);
657 return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
660 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
662 switch(msg)
664 case WM_CREATE:
665 return OnCreate( hWnd, wParam, lParam );
667 case WM_USER:
668 return OnUser( hWnd, wParam, lParam );
670 case WM_NOTIFY:
671 return OnNotify( hWnd, wParam, lParam );
673 case WM_COMMAND:
674 return OnCommand( hWnd, wParam, lParam );
676 case WM_DESTROY:
677 PostQuitMessage(0);
678 break;
680 case WM_ACTIVATE:
681 if (LOWORD(wParam))
682 SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
683 return 0;
685 case WM_INITMENUPOPUP:
686 return OnInitPopupMenu( hWnd, wParam, lParam );
688 case WM_SIZE:
689 return OnSize( hWnd, wParam, lParam );
691 default:
692 return DefWindowProcW(hWnd, msg, wParam, lParam);
695 return 0;
698 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
700 INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES};
701 HACCEL hAccel;
702 WNDCLASSW wc;
703 MSG msg;
705 InitCommonControlsEx(&classes);
707 hAccel = LoadAccelerators(hInstance, "MAINACCELTABLE");
709 wc.style = CS_HREDRAW | CS_VREDRAW;
710 wc.lpfnWndProc = WndProc;
711 wc.cbClsExtra = 0;
712 wc.cbWndExtra = 4;
713 wc.hInstance = hInstance;
714 wc.hIcon = NULL;
715 wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
716 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
717 wc.lpszMenuName = xszMainMenu;
718 wc.lpszClassName = wszMainWndClass;
719 RegisterClassW(&wc);
721 hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
722 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
723 ShowWindow(hMainWnd, SW_SHOWDEFAULT);
725 HandleCommandLine(GetCommandLineW());
727 while(GetMessage(&msg,0,0,0))
729 if (TranslateAccelerator(hMainWnd, hAccel, &msg))
730 continue;
731 TranslateMessage(&msg);
732 DispatchMessage(&msg);
733 if (!PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
734 SendMessage(hMainWnd, WM_USER, 0, 0);
737 return 0;