wordpad: Open .wri files in wordpad.
[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 #include <stdarg.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include <windows.h>
30 #include <richedit.h>
31 #include <commctrl.h>
32
33 #include "resource.h"
34
35
36 /* use LoadString */
37 static const WCHAR xszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
38 static const WCHAR xszMainMenu[] = {'M','A','I','N','M','E','N','U',0};
39
40 static const WCHAR wszRichEditClass[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
41 static const WCHAR wszMainWndClass[] = {'W','O','R','D','P','A','D','T','O','P',0};
42 static const WCHAR wszAppTitle[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
43
44 static HWND hMainWnd;
45 static HWND hEditorWnd;
46
47 static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
48 {
49     TBBUTTON button;
50
51     ZeroMemory(&button, sizeof(button));
52     button.iBitmap = nImage;
53     button.idCommand = nCommand;
54     button.fsState = TBSTATE_ENABLED;
55     button.fsStyle = TBSTYLE_BUTTON;
56     button.dwData = 0;
57     button.iString = -1;
58     SendMessage(hwndToolBar, TB_ADDBUTTONS, 1, (LPARAM)&button);
59 }
60
61 static void AddSeparator(HWND hwndToolBar)
62 {
63     TBBUTTON button;
64
65     ZeroMemory(&button, sizeof(button));
66     button.iBitmap = -1;
67     button.idCommand = 0;
68     button.fsState = 0;
69     button.fsStyle = TBSTYLE_SEP;
70     button.dwData = 0;
71     button.iString = -1;
72     SendMessage(hwndToolBar, TB_ADDBUTTONS, 1, (LPARAM)&button);
73 }
74
75 static LPSTR stream_buffer;
76 static LONG  stream_buffer_size;
77
78 static DWORD CALLBACK stream_in(DWORD_PTR cookie, LPBYTE buffer, LONG cb, LONG *pcb)
79 {
80     LONG size = min(stream_buffer_size, cb);
81
82     memcpy(buffer, stream_buffer, size);
83     stream_buffer_size -= size;
84     stream_buffer += size;
85     *pcb = size;
86     return 0;
87 }
88
89 static void DoOpenFile(LPCWSTR szFileName)
90 {
91     HANDLE hFile;
92     LPSTR pTemp;
93     DWORD size;
94     DWORD dwNumRead;
95     EDITSTREAM es;
96
97     hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
98                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
99     if (hFile == INVALID_HANDLE_VALUE)
100         return;
101
102     size = GetFileSize(hFile, NULL);
103     if (size == INVALID_FILE_SIZE)
104     {
105         CloseHandle(hFile);
106         return;
107     }
108     size++;
109
110     pTemp = HeapAlloc(GetProcessHeap(), 0, size);
111     if (!pTemp)
112     {
113         CloseHandle(hFile);
114         return;
115     }
116
117     if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL))
118     {
119         CloseHandle(hFile);
120         HeapFree(GetProcessHeap(), 0, pTemp);
121         return;
122     }
123     CloseHandle(hFile);
124     pTemp[dwNumRead] = 0;
125
126     memset(&es, 0, sizeof(es));
127     es.pfnCallback = stream_in;
128
129     stream_buffer = pTemp;
130     stream_buffer_size = size;
131
132     SendMessage(hEditorWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es);
133     HeapFree(GetProcessHeap(), 0, pTemp);
134
135     SetFocus(hEditorWnd);
136 }
137
138 static void HandleCommandLine(LPWSTR cmdline)
139 {
140     WCHAR delimiter;
141     int opt_print = 0;
142
143     /* skip white space */
144     while (*cmdline == ' ') cmdline++;
145
146     /* skip executable name */
147     delimiter = (*cmdline == '"' ? '"' : ' ');
148
149     if (*cmdline == delimiter) cmdline++;
150     while (*cmdline && *cmdline != delimiter) cmdline++;
151     if (*cmdline == delimiter) cmdline++;
152
153     while (*cmdline == ' ' || *cmdline == '-' || *cmdline == '/')
154     {
155         WCHAR option;
156
157         if (*cmdline++ == ' ') continue;
158
159         option = *cmdline;
160         if (option) cmdline++;
161         while (*cmdline == ' ') cmdline++;
162
163         switch (option)
164         {
165             case 'p':
166             case 'P':
167                 opt_print = 1;
168                 break;
169         }
170     }
171
172     if (*cmdline)
173     {
174         /* file name is passed on the command line */
175         if (cmdline[0] == '"')
176         {
177             cmdline++;
178             cmdline[lstrlenW(cmdline) - 1] = 0;
179         }
180         DoOpenFile(cmdline);
181         InvalidateRect(hMainWnd, NULL, FALSE);
182     }
183
184     if (opt_print)
185         MessageBox(hMainWnd, "Printing not implemented", "WordPad", MB_OK);
186 }
187
188 static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
189 {
190     HWND hToolBarWnd, hReBarWnd;
191     HINSTANCE hInstance = (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE);
192     HANDLE hDLL;
193     TBADDBITMAP ab;
194     int nStdBitmaps = 0;
195     REBARINFO rbi;
196     REBARBANDINFO rbb;
197
198     CreateStatusWindow(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE, "RichEdit text", hWnd, IDC_STATUSBAR);
199
200     hReBarWnd = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL,
201       CCS_NODIVIDER|WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_TOP,
202       CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hWnd, (HMENU)IDC_REBAR, hInstance, NULL);
203
204     rbi.cbSize = sizeof(rbi);
205     rbi.fMask = 0;
206     rbi.himl = NULL;
207     if(!SendMessage(hReBarWnd, RB_SETBARINFO, 0, (LPARAM)&rbi))
208         return -1;
209
210     hToolBarWnd = CreateToolbarEx(hReBarWnd, CCS_NOPARENTALIGN|CCS_NODIVIDER|CCS_NOMOVEY|WS_VISIBLE|WS_CHILD|TBSTYLE_TOOLTIPS|TBSTYLE_FLAT,
211       IDC_TOOLBAR,
212       3, hInstance, IDB_TOOLBAR,
213       NULL, 0,
214       24, 24, 16, 16, sizeof(TBBUTTON));
215
216     ab.hInst = HINST_COMMCTRL;
217     ab.nID = IDB_STD_SMALL_COLOR;
218     nStdBitmaps = SendMessage(hToolBarWnd, TB_ADDBITMAP, 3, (LPARAM)&ab);
219     AddButton(hToolBarWnd, nStdBitmaps+STD_FILENEW, ID_FILE_NEW);
220     AddButton(hToolBarWnd, nStdBitmaps+STD_FILEOPEN, ID_FILE_OPEN);
221     AddButton(hToolBarWnd, nStdBitmaps+STD_FILESAVE, ID_FILE_SAVE);
222     AddSeparator(hToolBarWnd);
223     AddButton(hToolBarWnd, nStdBitmaps+STD_CUT, ID_EDIT_CUT);
224     AddButton(hToolBarWnd, nStdBitmaps+STD_COPY, ID_EDIT_COPY);
225     AddSeparator(hToolBarWnd);
226     AddButton(hToolBarWnd, nStdBitmaps+STD_UNDO, ID_EDIT_UNDO);
227     AddButton(hToolBarWnd, nStdBitmaps+STD_REDOW, ID_EDIT_REDO);
228     AddSeparator(hToolBarWnd);
229     AddButton(hToolBarWnd, 0, ID_FORMAT_BOLD);
230     AddButton(hToolBarWnd, 1, ID_FORMAT_ITALIC);
231     AddButton(hToolBarWnd, 2, ID_FORMAT_UNDERLINE);
232
233     SendMessage(hToolBarWnd, TB_ADDSTRING, 0, (LPARAM)"Exit\0");
234     SendMessage(hToolBarWnd, TB_AUTOSIZE, 0, 0);
235
236     rbb.cbSize = sizeof(rbb);
237     rbb.fMask = RBBIM_SIZE | RBBIM_CHILDSIZE | RBBIM_CHILD | RBBIM_STYLE;
238     rbb.fStyle = RBBS_CHILDEDGE;
239     rbb.cx = 500;
240     rbb.hwndChild = hToolBarWnd;
241     rbb.cxMinChild = 0;
242     rbb.cyChild = rbb.cyMinChild = HIWORD(SendMessage(hToolBarWnd, TB_GETBUTTONSIZE, 0, 0));
243
244     SendMessage(hReBarWnd, RB_INSERTBAND, -1, (LPARAM)&rbb);
245
246     hDLL = LoadLibrary("RICHED20.DLL");
247     assert(hDLL);
248
249     hEditorWnd = CreateWindowExW(WS_EX_CLIENTEDGE, wszRichEditClass, NULL,
250       WS_CHILD|WS_VISIBLE|ES_MULTILINE|ES_AUTOVSCROLL|ES_WANTRETURN|WS_VSCROLL,
251       0, 0, 1000, 100, hWnd, (HMENU)IDC_EDITOR, hInstance, NULL);
252     if (!hEditorWnd)
253     {
254         fprintf(stderr, "Error code %u\n", GetLastError());
255         return -1;
256     }
257     assert(hEditorWnd);
258
259     SetFocus(hEditorWnd);
260     SendMessage(hEditorWnd, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
261     return 0;
262 }
263
264 static LRESULT OnUser( HWND hWnd, WPARAM wParam, LPARAM lParam)
265 {
266     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
267     HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
268     HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
269     int from, to;
270     CHARFORMAT2W fmt;
271
272     ZeroMemory(&fmt, sizeof(fmt));
273     fmt.cbSize = sizeof(fmt);
274
275     SendMessage(hwndEditor, EM_GETCHARFORMAT, TRUE, (LPARAM)&fmt);
276
277     SendMessage(hwndEditor, EM_GETSEL, (WPARAM)&from, (LPARAM)&to);
278     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_UNDO,
279       SendMessage(hwndEditor, EM_CANUNDO, 0, 0));
280     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_REDO,
281       SendMessage(hwndEditor, EM_CANREDO, 0, 0));
282     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_CUT, from == to ? 0 : 1);
283     SendMessage(hwndToolBar, TB_ENABLEBUTTON, ID_EDIT_COPY, from == to ? 0 : 1);
284     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_BOLD, (fmt.dwMask & CFM_BOLD) && (fmt.dwEffects & CFE_BOLD));
285     SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_BOLD, !(fmt.dwMask & CFM_BOLD));
286     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_ITALIC, (fmt.dwMask & CFM_ITALIC) && (fmt.dwEffects & CFE_ITALIC));
287     SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_ITALIC, !(fmt.dwMask & CFM_ITALIC));
288     SendMessage(hwndToolBar, TB_CHECKBUTTON, ID_FORMAT_UNDERLINE, (fmt.dwMask & CFM_UNDERLINE) && (fmt.dwEffects & CFE_UNDERLINE));
289     SendMessage(hwndToolBar, TB_INDETERMINATE, ID_FORMAT_UNDERLINE, !(fmt.dwMask & CFM_UNDERLINE));
290     return 0;
291 }
292
293 static LRESULT OnNotify( HWND hWnd, WPARAM wParam, LPARAM lParam)
294 {
295     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
296     NMHDR *pHdr = (NMHDR *)lParam;
297
298     if (pHdr->hwndFrom != hwndEditor)
299         return 0;
300
301     if (pHdr->code == EN_SELCHANGE)
302     {
303         SELCHANGE *pSC = (SELCHANGE *)lParam;
304         char buf[128];
305
306         sprintf( buf,"selection = %d..%d, line count=%ld\n",
307                  pSC->chrg.cpMin, pSC->chrg.cpMax,
308         SendMessage(hwndEditor, EM_GETLINECOUNT, 0, 0));
309         SetWindowText(GetDlgItem(hWnd, IDC_STATUSBAR), buf);
310         SendMessage(hWnd, WM_USER, 0, 0);
311         return 1;
312     }
313     return 0;
314 }
315
316 static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
317 {
318     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
319
320     if ((HWND)lParam == hwndEditor)
321         return 0;
322
323     switch(LOWORD(wParam))
324     {
325     case ID_FILE_EXIT:
326         PostMessage(hWnd, WM_CLOSE, 0, 0);
327         break;
328
329     case ID_FILE_NEW:
330         SetWindowTextA(hwndEditor, "");
331         /* FIXME: set default format too */
332         break;
333
334     case ID_FILE_OPEN:
335     case ID_FILE_SAVE:
336         MessageBox(hWnd, "Open/Save not implemented", "WordPad", MB_OK);
337         break;
338
339     case ID_FORMAT_BOLD:
340     case ID_FORMAT_ITALIC:
341     case ID_FORMAT_UNDERLINE:
342         {
343         CHARFORMAT2W fmt;
344         int mask = CFM_BOLD;
345         if (LOWORD(wParam) == ID_FORMAT_ITALIC) mask = CFM_ITALIC;
346         if (LOWORD(wParam) == ID_FORMAT_UNDERLINE) mask = CFM_UNDERLINE;
347
348         ZeroMemory(&fmt, sizeof(fmt));
349         fmt.cbSize = sizeof(fmt);
350         SendMessage(hwndEditor, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
351         if (!(fmt.dwMask&mask))
352             fmt.dwEffects |= mask;
353         else
354             fmt.dwEffects ^= mask;
355         fmt.dwMask = mask;
356         SendMessage(hwndEditor, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
357         break;
358         }
359
360     case ID_EDIT_CUT:
361         PostMessage(hwndEditor, WM_CUT, 0, 0);
362         break;
363
364     case ID_EDIT_COPY:
365         PostMessage(hwndEditor, WM_COPY, 0, 0);
366         break;
367
368     case ID_EDIT_SELECTALL:
369         {
370         CHARRANGE range = {0, -1};
371         SendMessage(hwndEditor, EM_EXSETSEL, 0, (LPARAM)&range);
372         /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
373         return 0;
374         }
375
376     case ID_EDIT_GETTEXT:
377         {
378         int nLen = GetWindowTextLengthW(hwndEditor);
379         LPWSTR data = HeapAlloc( GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR) );
380         TEXTRANGEW tr;
381
382         GetWindowTextW(hwndEditor, data, nLen+1);
383         MessageBoxW(NULL, data, xszAppTitle, MB_OK);
384
385         HeapFree( GetProcessHeap(), 0, data);
386         data = HeapAlloc(GetProcessHeap(), 0, (nLen+1)*sizeof(WCHAR));
387         tr.chrg.cpMin = 0;
388         tr.chrg.cpMax = nLen;
389         tr.lpstrText = data;
390         SendMessage (hwndEditor, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
391         MessageBoxW(NULL, data, xszAppTitle, MB_OK);
392         HeapFree( GetProcessHeap(), 0, data );
393
394         /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
395         return 0;
396         }
397
398     case ID_EDIT_CHARFORMAT:
399     case ID_EDIT_DEFCHARFORMAT:
400         {
401         CHARFORMAT2W cf;
402         LRESULT i;
403         ZeroMemory(&cf, sizeof(cf));
404         cf.cbSize = sizeof(cf);
405         cf.dwMask = 0;
406         i = SendMessage(hwndEditor, EM_GETCHARFORMAT,
407                         LOWORD(wParam) == ID_EDIT_CHARFORMAT, (LPARAM)&cf);
408         return 0;
409         }
410
411     case ID_EDIT_PARAFORMAT:
412         {
413         PARAFORMAT2 pf;
414         ZeroMemory(&pf, sizeof(pf));
415         pf.cbSize = sizeof(pf);
416         SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
417         return 0;
418         }
419
420     case ID_EDIT_SELECTIONINFO:
421         {
422         CHARRANGE range = {0, -1};
423         char buf[128];
424         WCHAR *data = NULL;
425
426         SendMessage(hwndEditor, EM_EXGETSEL, 0, (LPARAM)&range);
427         data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data) * (range.cpMax-range.cpMin+1));
428         SendMessage(hwndEditor, EM_GETSELTEXT, 0, (LPARAM)data);
429         sprintf(buf, "Start = %d, End = %d", range.cpMin, range.cpMax);
430         MessageBoxA(hWnd, buf, "Editor", MB_OK);
431         MessageBoxW(hWnd, data, xszAppTitle, MB_OK);
432         HeapFree( GetProcessHeap(), 0, data);
433         /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
434         return 0;
435         }
436
437     case ID_EDIT_READONLY:
438         {
439         long nStyle = GetWindowLong(hwndEditor, GWL_STYLE);
440         if (nStyle & ES_READONLY)
441             SendMessage(hwndEditor, EM_SETREADONLY, 0, 0);
442         else
443             SendMessage(hwndEditor, EM_SETREADONLY, 1, 0);
444         return 0;
445         }
446
447     case ID_EDIT_MODIFIED:
448         if (SendMessage(hwndEditor, EM_GETMODIFY, 0, 0))
449             SendMessage(hwndEditor, EM_SETMODIFY, 0, 0);
450         else
451             SendMessage(hwndEditor, EM_SETMODIFY, 1, 0);
452         return 0;
453
454     case ID_EDIT_UNDO:
455         SendMessage(hwndEditor, EM_UNDO, 0, 0);
456         return 0;
457
458     case ID_EDIT_REDO:
459         SendMessage(hwndEditor, EM_REDO, 0, 0);
460         return 0;
461
462     case ID_ALIGN_LEFT:
463     case ID_ALIGN_CENTER:
464     case ID_ALIGN_RIGHT:
465         {
466         PARAFORMAT2 pf;
467
468         pf.cbSize = sizeof(pf);
469         pf.dwMask = PFM_ALIGNMENT;
470         switch(LOWORD(wParam)) {
471         case ID_ALIGN_LEFT: pf.wAlignment = PFA_LEFT; break;
472         case ID_ALIGN_CENTER: pf.wAlignment = PFA_CENTER; break;
473         case ID_ALIGN_RIGHT: pf.wAlignment = PFA_RIGHT; break;
474         }
475         SendMessage(hwndEditor, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
476         break;
477         }
478
479     case ID_BACK_1:
480         SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 1, 0);
481         break;
482
483     case ID_BACK_2:
484         SendMessage(hwndEditor, EM_SETBKGNDCOLOR, 0, RGB(255,255,192));
485         break;
486
487     default:
488         SendMessage(hwndEditor, WM_COMMAND, wParam, lParam);
489         break;
490     }
491     return 0;
492 }
493
494 static LRESULT OnInitPopupMenu( HWND hWnd, WPARAM wParam, LPARAM lParam )
495 {
496     HMENU hMenu = (HMENU)wParam;
497     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
498     PARAFORMAT pf;
499     int nAlignment = -1;
500
501     pf.cbSize = sizeof(PARAFORMAT);
502     SendMessage(hwndEditor, EM_GETPARAFORMAT, 0, (LPARAM)&pf);
503     CheckMenuItem(hMenu, ID_EDIT_READONLY,
504       MF_BYCOMMAND|(GetWindowLong(hwndEditor, GWL_STYLE)&ES_READONLY ? MF_CHECKED : MF_UNCHECKED));
505     CheckMenuItem(hMenu, ID_EDIT_MODIFIED,
506       MF_BYCOMMAND|(SendMessage(hwndEditor, EM_GETMODIFY, 0, 0) ? MF_CHECKED : MF_UNCHECKED));
507     if (pf.dwMask & PFM_ALIGNMENT)
508         nAlignment = pf.wAlignment;
509     CheckMenuItem(hMenu, ID_ALIGN_LEFT, MF_BYCOMMAND|(nAlignment == PFA_LEFT) ? MF_CHECKED : MF_UNCHECKED);
510     CheckMenuItem(hMenu, ID_ALIGN_CENTER, MF_BYCOMMAND|(nAlignment == PFA_CENTER) ? MF_CHECKED : MF_UNCHECKED);
511     CheckMenuItem(hMenu, ID_ALIGN_RIGHT, MF_BYCOMMAND|(nAlignment == PFA_RIGHT) ? MF_CHECKED : MF_UNCHECKED);
512     EnableMenuItem(hMenu, ID_EDIT_UNDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANUNDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
513     EnableMenuItem(hMenu, ID_EDIT_REDO, MF_BYCOMMAND|(SendMessage(hwndEditor, EM_CANREDO, 0, 0)) ? MF_ENABLED : MF_GRAYED);
514     return 0;
515 }
516
517 static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam )
518 {
519     int nStatusSize = 0, nTBSize = 0;
520     RECT rc;
521     HWND hwndEditor = GetDlgItem(hWnd, IDC_EDITOR);
522     HWND hwndStatusBar = GetDlgItem(hWnd, IDC_STATUSBAR);
523     HWND hwndReBar = GetDlgItem(hWnd, IDC_REBAR);
524     HWND hwndToolBar = GetDlgItem(hwndReBar, IDC_TOOLBAR);
525
526     if (hwndStatusBar)
527     {
528         SendMessage(hwndStatusBar, WM_SIZE, 0, 0);
529         GetClientRect(hwndStatusBar, &rc);
530         nStatusSize = rc.bottom - rc.top;
531     }
532     if (hwndToolBar)
533     {
534         rc.left = rc.top = 0;
535         rc.right = LOWORD(lParam);
536         rc.bottom = HIWORD(lParam);
537         SendMessage(hwndToolBar, TB_AUTOSIZE, 0, 0);
538         SendMessage(hwndReBar, RB_SIZETORECT, 0, (LPARAM)&rc);
539         nTBSize = SendMessage(hwndReBar, RB_GETBARHEIGHT, 0, 0);
540         GetClientRect(hwndReBar, &rc);
541         MoveWindow(hwndReBar, 0, 0, LOWORD(lParam), rc.right, FALSE);
542     }
543     if (hwndEditor)
544     {
545         GetClientRect(hWnd, &rc);
546         MoveWindow(hwndEditor, 0, nTBSize, rc.right, rc.bottom-nStatusSize-nTBSize, TRUE);
547     }
548
549     return DefWindowProcW(hWnd, WM_SIZE, wParam, lParam);
550 }
551
552 static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
553 {
554     switch(msg)
555     {
556     case WM_CREATE:
557         return OnCreate( hWnd, wParam, lParam );
558
559     case WM_USER:
560         return OnUser( hWnd, wParam, lParam );
561
562     case WM_NOTIFY:
563         return OnNotify( hWnd, wParam, lParam );
564
565     case WM_COMMAND:
566         return OnCommand( hWnd, wParam, lParam );
567
568     case WM_DESTROY:
569         PostQuitMessage(0);
570         break;
571
572     case WM_ACTIVATE:
573         if (LOWORD(wParam))
574             SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
575         return 0;
576
577     case WM_INITMENUPOPUP:
578         return OnInitPopupMenu( hWnd, wParam, lParam );
579
580     case WM_SIZE:
581         return OnSize( hWnd, wParam, lParam );
582
583     default:
584         return DefWindowProcW(hWnd, msg, wParam, lParam);
585     }
586
587     return 0;
588 }
589
590 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdParagraph, int res)
591 {
592     INITCOMMONCONTROLSEX classes = {8, ICC_BAR_CLASSES|ICC_COOL_CLASSES};
593     HACCEL hAccel;
594     WNDCLASSW wc;
595     MSG msg;
596
597     InitCommonControlsEx(&classes);
598
599     hAccel = LoadAccelerators(hInstance, "MAINACCELTABLE");
600
601     wc.style = CS_HREDRAW | CS_VREDRAW;
602     wc.lpfnWndProc = WndProc;
603     wc.cbClsExtra = 0;
604     wc.cbWndExtra = 4;
605     wc.hInstance = hInstance;
606     wc.hIcon = NULL;
607     wc.hCursor = LoadCursor(NULL, IDC_IBEAM);
608     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
609     wc.lpszMenuName = xszMainMenu;
610     wc.lpszClassName = wszMainWndClass;
611     RegisterClassW(&wc);
612
613     hMainWnd = CreateWindowExW(0, wszMainWndClass, wszAppTitle, WS_OVERLAPPEDWINDOW,
614       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
615     ShowWindow(hMainWnd, SW_SHOWMAXIMIZED);
616
617     HandleCommandLine(GetCommandLineW());
618
619     while(GetMessage(&msg,0,0,0))
620     {
621         if (TranslateAccelerator(hMainWnd, hAccel, &msg))
622             continue;
623         TranslateMessage(&msg);
624         DispatchMessage(&msg);
625         if (!PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
626             SendMessage(hMainWnd, WM_USER, 0, 0);
627     }
628
629     return 0;
630 }