wordpad: Add text alignment buttons.
[wine] / programs / wordpad / wordpad.c
1 /*
2  * Wordpad implementation
3  *
4  * Copyright 2004 by Krzysztof Foltman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define WIN32_LEAN_AND_MEAN
22 #define _WIN32_IE 0x0400
23
24 #define MAX_STRING_LEN 255
25
26 #include <stdarg.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <assert.h>
30
31 #include <windows.h>
32 #include <richedit.h>
33 #include <commctrl.h>
34 #include <commdlg.h>
35
36 #include "resource.h"
37
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};
41
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};
45
46 static HWND hMainWnd;
47 static HWND hEditorWnd;
48
49 static char szFilter[MAX_STRING_LEN];
50
51 /* Load string resources */
52 static void DoLoadStrings()
53 {
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);
59
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';
73 }
74
75 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
76 {
77     TBBUTTON button;
78
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);
87 }
88
89 static void AddSeparator(HWND hwndToolBar)
90 {
91     TBBUTTON button;
92
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);
101 }
102
103 static LPSTR stream_buffer;
104 static LONG  stream_buffer_size;
105
106 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
107 {
108     LONG size = min(stream_buffer_size, cb);
109
110     memcpy(buffer, stream_buffer, size);
111     stream_buffer_size -= size;
112     stream_buffer += size;
113     *pcb = size;
114     return 0;
115 }
116
117 static void DoOpenFile(LPCWSTR szFileName)
118 {
119     HANDLE hFile;
120     LPSTR pTemp;
121     DWORD size;
122     DWORD dwNumRead;
123     EDITSTREAM es;
124
125     char szCaption[MAX_PATH];
126     char szAppTitle[sizeof(wszAppTitle)];
127     char szSeparator[] = " - ";
128
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;
133
134     size = GetFileSize(hFile, NULL);
135     if (size == INVALID_FILE_SIZE)
136     {
137         CloseHandle(hFile);
138         return;
139     }
140     size++;
141
142     pTemp = HeapAlloc(GetProcessHeap(), 0, size);
143     if (!pTemp)
144     {
145         CloseHandle(hFile);
146         return;
147     }
148
149     if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
150     {
151         CloseHandle(hFile);
152         HeapFree(GetProcessHeap(), 0, pTemp);
153         return;
154     }
155     CloseHandle(hFile);
156     pTemp[dwNumRead] = 0;
157
158     memset(&es, 0, sizeof(es));
159     es.pfnCallback = stream_in;
160
161     stream_buffer = pTemp;
162     stream_buffer_size = size;
163
164     SendMessage(hEditorWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
165     HeapFree(GetProcessHeap(), 0, pTemp);
166
167     SetFocus(hEditorWnd);
168
169     WideCharToMultiByte(CP_ACP, 0, wszAppTitle, -1, szAppTitle, sizeof(wszAppTitle), NULL, NULL);
170
171     WideCharToMultiByte(CP_ACP, 0, szFileName, -1, szCaption, MAX_PATH, NULL, NULL);
172
173     lstrcat(szCaption, szSeparator);
174     lstrcat(szCaption, szAppTitle);
175
176     SetWindowText(hMainWnd, szCaption);
177 }
178
179 static void DialogOpenFile()
180 {
181     OPENFILENAME ofn;
182
183     char szFile[MAX_PATH] = "";
184     char szDefExt[] = "rtf";
185
186     ZeroMemory(&ofn, sizeof(ofn));
187
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;
195
196     if(GetOpenFileName(&ofn))
197     {
198         WCHAR szOpenFile[MAX_PATH];
199
200         MultiByteToWideChar(CP_ACP, 0, ofn.lpstrFile, MAX_PATH, szOpenFile, sizeof(szOpenFile)/sizeof(szOpenFile[0]));
201
202         DoOpenFile(szOpenFile);
203     }
204 }
205
206 static void HandleCommandLine(LPWSTR cmdline)
207 {
208     WCHAR delimiter;
209     int opt_print = 0;
210
211     /* skip white space */
212     while (*cmdline == ' ') cmdline++;
213
214     /* skip executable name */
215     delimiter = (*cmdline == '"' ? '"' : ' ');
216
217     if (*cmdline == delimiter) cmdline++;
218     while (*cmdline && *cmdline != delimiter) cmdline++;
219     if (*cmdline == delimiter) cmdline++;
220
221     while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
222     {
223         WCHAR option;
224
225         if (*cmdline++ == ' ') continue;
226
227         option = *cmdline;
228         if (option) cmdline++;
229         while (*cmdline == ' ') cmdline++;
230
231         switch (option)
232         {
233             case 'p':
234             case 'P':
235                 opt_print = 1;
236                 break;
237         }
238     }
239
240     if (*cmdline)
241     {
242         /* file name is passed on the command line */
243         if (cmdline[0] == '"')
244         {
245             cmdline++;
246             cmdline[lstrlenW(cmdline) - 1] = 0;
247         }
248         DoOpenFile(cmdline);
249         InvalidateRect(hMainWnd, NULL, FALSE);
250     }
251
252     if (opt_print)
253         MessageBox(hMainWnd, "Printing not implemented", "WordPad", MB_OK);
254 }
255
256 static void DoDefaultFont()
257 {
258     static const WCHAR szFaceName[] = {'T','i','m','e','s',' ','N','e','w',' ','R','o','m','a','n',0};
259     CHARFORMAT2W fmt;
260
261     ZeroMemory(&fmt, sizeof(fmt));
262
263     fmt.cbSize = sizeof(fmt);
264     fmt.dwMask = CFM_FACE;
265
266     lstrcpyW(fmt.szFaceName, szFaceName);
267
268     SendMessage(hEditorWnd, EM_SETCHARFORMAT,  SCF_DEFAULT, (LPARAM)&fmt);
269 }
270
271 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
272 {
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;
280
281     CreateStatusWindow(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, "RichEdit text", hWnd, IDC_STATUSBAR);
282
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);
286
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;
292
293     hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_BUTTON,
294       IDC_TOOLBAR,
295       6, hInstance, IDB_TOOLBAR,
296       NULL, 0,
297       24, 24, 16, 16, sizeof(TBBUTTON));
298
299     ab.hInst = HINST_COMMCTRL;
300     ab.nID = IDB_STD_SMALL_COLOR;
301     nStdBitmaps = SendMessage(hToolBarWnd, TB_ADDBITMAP, 6, (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);
320     AddSeparator(hToolBarWnd);
321     AddButton(hToolBarWnd, 3, ID_ALIGN_LEFT);
322     AddButton(hToolBarWnd, 4, ID_ALIGN_CENTER);
323     AddButton(hToolBarWnd, 5, ID_ALIGN_RIGHT);
324
325     SendMessage(hToolBarWnd, TB_ADDSTRING, 0, (LPARAM)"Exit\0");
326     SendMessage(hToolBarWnd, TB_AUTOSIZE, 0, 0);
327
328     rbb.cbSize = sizeof(rbb);
329     rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE;
330     rbb.fStyle = RBBS_CHILDEDGE;
331     rbb.cx = 500;
332     rbb.hwndChild = hToolBarWnd;
333     rbb.cxMinChild = 0;
334     rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessage(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
335
336     SendMessage(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
337
338     hDLL = LoadLibrary("RICHED20.DLL");
339     assert(hDLL);
340
341     hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
342       WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
343       0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
344     if (!hEditorWnd)
345     {
346         fprintf(stderr, "Error code %u\n", GetLastError());
347         return -1;
348     }
349     assert(hEditorWnd);
350
351     SetFocus(hEditorWnd);
352     SendMessage(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
353
354     DoDefaultFont();
355
356     DoLoadStrings();
357
358     return 0;
359 }
360
361 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
362 {
363     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
364     HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
365     HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
366     int from, to;
367     CHARFORMAT2W fmt;
368     PARAFORMAT2 pf;
369
370     ZeroMemory(&fmt, sizeof(fmt));
371     fmt.cbSize = sizeof(fmt);
372
373     ZeroMemory(&pf, sizeof(pf));
374     pf.cbSize = sizeof(pf);
375
376     SendMessage(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
377
378     SendMessage(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
379     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
380       SendMessage(hwndEditor, EM_CANUNDO, 0, 0));
381     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
382       SendMessage(hwndEditor, EM_CANREDO, 0, 0));
383     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
384     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
385     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) && (fmt.dwEffects & CFE_BOLD));
386     SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
387     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) && (fmt.dwEffects & CFE_ITALIC));
388     SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
389     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) && (fmt.dwEffects & CFE_UNDERLINE));
390     SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
391
392     SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
393     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_ALIGN_LEFT, (pf.wAlignment == PFA_LEFT));
394     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_ALIGN_CENTER, (pf.wAlignment == PFA_CENTER));
395     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_ALIGN_RIGHT, (pf.wAlignment == PFA_RIGHT));
396
397     return 0;
398 }
399
400 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
401 {
402     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
403     NMHDR *pHdr = (NMHDR *)lParam;
404
405     if (pHdr->hwndFrom != hwndEditor)
406         return 0;
407
408     if (pHdr->code == EN_SELCHANGE)
409     {
410         SELCHANGE *pSC = (SELCHANGE *)lParam;
411         char buf[128];
412
413         sprintf( buf,"selection = %d..%d, line count=%ld",
414                  pSC->chrg.cpMin, pSC->chrg.cpMax,
415         SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
416         SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
417         SendMessage(hWnd, WM_USER, 0, 0);
418         return 1;
419     }
420     return 0;
421 }
422
423 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
424 {
425     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
426
427     if ((HWND)lParam == hwndEditor)
428         return 0;
429
430     switch(LOWORD(wParam))
431     {
432     case ID_FILE_EXIT:
433         PostMessage(hWnd, WM_CLOSE, 0, 0);
434         break;
435
436     case ID_FILE_NEW:
437         SetWindowTextA(hwndEditor, "");
438         SetWindowTextW(hMainWnd, wszAppTitle);
439         /* FIXME: set default format too */
440         break;
441
442     case ID_FILE_OPEN:
443         DialogOpenFile();
444         break;
445
446     case ID_FILE_SAVE:
447     case ID_PRINT:
448     case ID_PREVIEW:
449     case ID_FIND:
450         MessageBox(hWnd, "Not implemented", "WordPad", MB_OK);
451         break;
452
453     case ID_FORMAT_BOLD:
454     case ID_FORMAT_ITALIC:
455     case ID_FORMAT_UNDERLINE:
456         {
457         CHARFORMAT2W fmt;
458         int mask = CFM_BOLD;
459         if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
460         if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
461
462         ZeroMemory(&fmt, sizeof(fmt));
463         fmt.cbSize = sizeof(fmt);
464         SendMessage(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
465         if (!(fmt.dwMask&mask))
466             fmt.dwEffects |= mask;
467         else
468             fmt.dwEffects ^= mask;
469         fmt.dwMask = mask;
470         SendMessage(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
471         break;
472         }
473
474     case ID_EDIT_CUT:
475         PostMessage(hwndEditor, WM_CUT, 0, 0);
476         break;
477
478     case ID_EDIT_COPY:
479         PostMessage(hwndEditor, WM_COPY, 0, 0);
480         break;
481
482     case ID_EDIT_PASTE:
483         PostMessage(hwndEditor, WM_PASTE, 0, 0);
484         break;
485
486     case ID_EDIT_CLEAR:
487         PostMessage(hwndEditor, WM_CLEAR, 0, 0);
488         break;
489
490     case ID_EDIT_SELECTALL:
491         {
492         CHARRANGE range = {0, -1};
493         SendMessage(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
494         /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
495         return 0;
496         }
497
498     case ID_EDIT_GETTEXT:
499         {
500         int nLen = GetWindowTextLengthW(hwndEditor);
501         LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
502         TEXTRANGEW tr;
503
504         GetWindowTextW(hwndEditor, data, nLen+1);
505         MessageBoxW(NULL, data, xszAppTitle, MB_OK);
506
507         HeapFree( GetProcessHeap(), 0, data);
508         data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
509         tr.chrg.cpMin = 0;
510         tr.chrg.cpMax = nLen;
511         tr.lpstrText = data;
512         SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
513         MessageBoxW(NULL, data, xszAppTitle, MB_OK);
514         HeapFree( GetProcessHeap(), 0, data );
515
516         /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
517         return 0;
518         }
519
520     case ID_EDIT_CHARFORMAT:
521     case ID_EDIT_DEFCHARFORMAT:
522         {
523         CHARFORMAT2W cf;
524         LRESULT i;
525         ZeroMemory(&cf, sizeof(cf));
526         cf.cbSize = sizeof(cf);
527         cf.dwMask = 0;
528         i = SendMessage(hwndEditor, EM_GETCHARFORMAT,
529                         LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
530         return 0;
531         }
532
533     case ID_EDIT_PARAFORMAT:
534         {
535         PARAFORMAT2 pf;
536         ZeroMemory(&pf, sizeof(pf));
537         pf.cbSize = sizeof(pf);
538         SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
539         return 0;
540         }
541
542     case ID_EDIT_SELECTIONINFO:
543         {
544         CHARRANGE range = {0, -1};
545         char buf[128];
546         WCHAR *data = NULL;
547
548         SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
549         data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
550         SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
551         sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
552         MessageBoxA(hWnd, buf, "Editor", MB_OK);
553         MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
554         HeapFree( GetProcessHeap(), 0, data);
555         /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
556         return 0;
557         }
558
559     case ID_EDIT_READONLY:
560         {
561         long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
562         if (nStyle & ES_READONLY)
563             SendMessage(hwndEditor, EM_SETREADONLY, 0, 0);
564         else
565             SendMessage(hwndEditor, EM_SETREADONLY, 1, 0);
566         return 0;
567         }
568
569     case ID_EDIT_MODIFIED:
570         if (SendMessage(hwndEditor, EM_GETMODIFY, 0, 0))
571             SendMessage(hwndEditor, EM_SETMODIFY, 0, 0);
572         else
573             SendMessage(hwndEditor, EM_SETMODIFY, 1, 0);
574         return 0;
575
576     case ID_EDIT_UNDO:
577         SendMessage(hwndEditor, EM_UNDO, 0, 0);
578         return 0;
579
580     case ID_EDIT_REDO:
581         SendMessage(hwndEditor, EM_REDO, 0, 0);
582         return 0;
583
584     case ID_ALIGN_LEFT:
585     case ID_ALIGN_CENTER:
586     case ID_ALIGN_RIGHT:
587         {
588         PARAFORMAT2 pf;
589
590         pf.cbSize = sizeof(pf);
591         pf.dwMask = PFM_ALIGNMENT;
592         switch(LOWORD(wParam)) {
593         case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
594         case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
595         case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
596         }
597         SendMessage(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
598         break;
599         }
600
601     case ID_BACK_1:
602         SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
603         break;
604
605     case ID_BACK_2:
606         SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
607         break;
608
609     default:
610         SendMessage(hwndEditor, WM_COMMAND, wParam, lParam);
611         break;
612     }
613     return 0;
614 }
615
616 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
617 {
618     HMENU hMenu = (HMENU)wParam;
619     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
620     PARAFORMAT pf;
621     int nAlignment = -1;
622
623     pf.cbSize = sizeof(PARAFORMAT);
624     SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
625     CheckMenuItem(hMenu, ID_EDIT_READONLY,
626       MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
627     CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
628       MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
629     if (pf.dwMask & PFM_ALIGNMENT)
630         nAlignment = pf.wAlignment;
631     CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
632     CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
633     CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
634     EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANUNDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
635     EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANREDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
636     return 0;
637 }
638
639 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
640 {
641     int nStatusSize = 0, nTBSize = 0;
642     RECT rc;
643     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
644     HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
645     HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
646     HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
647
648     if (hwndStatusBar)
649     {
650         SendMessage(hwndStatusBar, WM_SIZE, 0, 0);
651         GetClientRect(hwndStatusBar, &rc);
652         nStatusSize = rc.bottom - rc.top;
653     }
654     if (hwndToolBar)
655     {
656         rc.left = rc.top = 0;
657         rc.right = LOWORD(lParam);
658         rc.bottom = HIWORD(lParam);
659         SendMessage(hwndToolBar, TB_AUTOSIZE, 0, 0);
660         SendMessage(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
661         nTBSize = SendMessage(hwndReBar, RB_GETBARHEIGHT, 0, 0);
662         GetClientRect(hwndReBar, &rc);
663         MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
664     }
665     if (hwndEditor)
666     {
667         GetClientRect(hWnd, &rc);
668         MoveWindow(hwndEditor, 0, nTBSize, rc.right, rc.bottom-nStatusSize-nTBSize, TRUE);
669     }
670
671     return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
672 }
673
674 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
675 {
676     switch(msg)
677     {
678     case WM_CREATE:
679         return OnCreate( hWnd, wParam, lParam );
680
681     case WM_USER:
682         return OnUser( hWnd, wParam, lParam );
683
684     case WM_NOTIFY:
685         return OnNotify( hWnd, wParam, lParam );
686
687     case WM_COMMAND:
688         return OnCommand( hWnd, wParam, lParam );
689
690     case WM_DESTROY:
691         PostQuitMessage(0);
692         break;
693
694     case WM_ACTIVATE:
695         if (LOWORD(wParam))
696             SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
697         return 0;
698
699     case WM_INITMENUPOPUP:
700         return OnInitPopupMenu( hWnd, wParam, lParam );
701
702     case WM_SIZE:
703         return OnSize( hWnd, wParam, lParam );
704
705     default:
706         return DefWindowProcW(hWnd, msg, wParam, lParam);
707     }
708
709     return 0;
710 }
711
712 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
713 {
714     INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES};
715     HACCEL hAccel;
716     WNDCLASSW wc;
717     MSG msg;
718
719     InitCommonControlsEx(&classes);
720
721     hAccel = LoadAccelerators(hInstance, "MAINACCELTABLE");
722
723     wc.style = CS_HREDRAW | CS_VREDRAW;
724     wc.lpfnWndProc = WndProc;
725     wc.cbClsExtra = 0;
726     wc.cbWndExtra = 4;
727     wc.hInstance = hInstance;
728     wc.hIcon = NULL;
729     wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
730     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
731     wc.lpszMenuName = xszMainMenu;
732     wc.lpszClassName = wszMainWndClass;
733     RegisterClassW(&wc);
734
735     hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
736       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
737     ShowWindow(hMainWnd, SW_SHOWDEFAULT);
738
739     HandleCommandLine(GetCommandLineW());
740
741     while(GetMessage(&msg,0,0,0))
742     {
743         if (TranslateAccelerator(hMainWnd, hAccel, &msg))
744             continue;
745         TranslateMessage(&msg);
746         DispatchMessage(&msg);
747         if (!PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
748             SendMessage(hMainWnd, WM_USER, 0, 0);
749     }
750
751     return 0;
752 }