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
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};
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)
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
);
64 lstrcpyW(p
, files_rtf
);
66 LoadStringW(hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
);
68 lstrcpyW(p
, files_txt
);
70 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
72 lstrcpyW(p
, files_all
);
77 static void AddButton(HWND hwndToolBar
, int nImage
, int nCommand
)
81 ZeroMemory(&button
, sizeof(button
));
82 button
.iBitmap
= nImage
;
83 button
.idCommand
= nCommand
;
84 button
.fsState
= TBSTATE_ENABLED
;
85 button
.fsStyle
= TBSTYLE_BUTTON
;
88 SendMessageW(hwndToolBar
, TB_ADDBUTTONSW
, 1, (LPARAM
)&button
);
91 static void AddSeparator(HWND hwndToolBar
)
95 ZeroMemory(&button
, sizeof(button
));
99 button
.fsStyle
= TBSTYLE_SEP
;
102 SendMessageW(hwndToolBar
, TB_ADDBUTTONSW
, 1, (LPARAM
)&button
);
105 static DWORD CALLBACK
stream_in(DWORD_PTR cookie
, LPBYTE buffer
, LONG cb
, LONG
*pcb
)
107 HANDLE hFile
= (HANDLE
)cookie
;
110 if(!ReadFile(hFile
, buffer
, cb
, &read
, 0))
118 static DWORD CALLBACK
stream_out(DWORD_PTR cookie
, LPBYTE buffer
, LONG cb
, LONG
*pcb
)
122 HANDLE hFile
= (HANDLE
)cookie
;
124 ret
= WriteFile(hFile
, buffer
, cb
, &written
, 0);
126 if(!ret
|| (cb
!= written
))
134 static WCHAR wszFileName
[MAX_PATH
];
136 static void set_caption(LPCWSTR wszNewFileName
)
138 static const WCHAR wszSeparator
[] = {' ','-',' '};
145 wszCaption
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
146 lstrlenW(wszNewFileName
)*sizeof(WCHAR
)+sizeof(wszSeparator
)+sizeof(wszAppTitle
));
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
);
162 SetWindowTextW(hMainWnd
, wszAppTitle
);
166 static void DoOpenFile(LPCWSTR szOpenFileName
)
171 hFile
= CreateFileW(szOpenFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
172 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
173 if (hFile
== INVALID_HANDLE_VALUE
)
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
);
184 SetFocus(hEditorWnd
);
186 set_caption(szOpenFileName
);
188 lstrcpyW(wszFileName
, szOpenFileName
);
191 static void DialogOpenFile(void)
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
)
218 hFile
= CreateFileW(wszSaveFileName
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
219 FILE_ATTRIBUTE_NORMAL
, NULL
);
221 if(hFile
== INVALID_HANDLE_VALUE
)
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
);
232 SetFocus(hEditorWnd
);
237 lstrcpyW(wszFileName
, wszSaveFileName
);
238 set_caption(wszFileName
);
241 static void DialogSaveFile(void)
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
))
261 DoSaveFile(sfn
.lpstrFile
);
264 static void HandleCommandLine(LPWSTR cmdline
)
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
== '/')
283 if (*cmdline
++ == ' ') continue;
286 if (option
) cmdline
++;
287 while (*cmdline
== ' ') cmdline
++;
300 /* file name is passed on the command line */
301 if (cmdline
[0] == '"')
304 cmdline
[lstrlenW(cmdline
) - 1] = 0;
307 InvalidateRect(hMainWnd
, NULL
, FALSE
);
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};
319 ZeroMemory(&fmt
, sizeof(fmt
));
321 fmt
.cbSize
= sizeof(fmt
);
322 fmt
.dwMask
= CFM_FACE
;
324 lstrcpyW(fmt
.szFaceName
, szFaceName
);
326 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
329 static void update_window(void)
333 GetWindowRect(hMainWnd
, &rect
);
335 (void) OnSize(hMainWnd
, SIZE_RESTORED
, MAKELONG(rect
.bottom
, rect
.right
));
338 static void toggle_toolbar(int bandId
)
340 HWND hwndReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
341 REBARBANDINFOW rbbinfo
;
347 rbbinfo
.cbSize
= sizeof(rbbinfo
);
348 rbbinfo
.fMask
= RBBIM_STYLE
| RBBIM_SIZE
;
350 SendMessageW(hwndReBar
, RB_GETBANDINFO
, bandId
, (LPARAM
)&rbbinfo
);
352 if(rbbinfo
.fStyle
& RBBS_HIDDEN
)
355 SendMessageW(hwndReBar
, RB_SHOWBAND
, bandId
, hide
? 0 : 1);
357 if(bandId
== BANDID_TOOLBAR
)
359 rbbinfo
.fMask
^= RBBIM_SIZE
;
361 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
364 rbbinfo
.fStyle
^= RBBS_BREAK
;
366 rbbinfo
.fStyle
|= RBBS_BREAK
;
368 SendMessageW(hwndReBar
, RB_SETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
372 static LRESULT
OnCreate( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
374 HWND hToolBarWnd
, hFormatBarWnd
, hReBarWnd
;
375 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
381 static const WCHAR wszRichEditDll
[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
382 static const WCHAR wszRichEditText
[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
384 CreateStatusWindowW(CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
, wszRichEditText
, hWnd
, IDC_STATUSBAR
);
386 hReBarWnd
= CreateWindowExW(WS_EX_TOOLWINDOW
, REBARCLASSNAMEW
, NULL
,
387 CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
|WS_CLIPSIBLINGS
|WS_CLIPCHILDREN
|RBS_VARHEIGHT
|CCS_TOP
,
388 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, hWnd
, (HMENU
)IDC_REBAR
, hInstance
, NULL
);
390 rbi
.cbSize
= sizeof(rbi
);
393 if(!SendMessageW(hReBarWnd
, RB_SETBARINFO
, 0, (LPARAM
)&rbi
))
396 hToolBarWnd
= CreateToolbarEx(hReBarWnd
, CCS_NOPARENTALIGN
|CCS_NOMOVEY
|WS_VISIBLE
|WS_CHILD
|TBSTYLE_TOOLTIPS
|TBSTYLE_BUTTON
,
400 24, 24, 16, 16, sizeof(TBBUTTON
));
402 ab
.hInst
= HINST_COMMCTRL
;
403 ab
.nID
= IDB_STD_SMALL_COLOR
;
404 nStdBitmaps
= SendMessageW(hToolBarWnd
, TB_ADDBITMAP
, 0, (LPARAM
)&ab
);
406 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILENEW
, ID_FILE_NEW
);
407 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILEOPEN
, ID_FILE_OPEN
);
408 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILESAVE
, ID_FILE_SAVE
);
409 AddSeparator(hToolBarWnd
);
410 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINT
, ID_PRINT
);
411 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINTPRE
, ID_PREVIEW
);
412 AddSeparator(hToolBarWnd
);
413 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FIND
, ID_FIND
);
414 AddSeparator(hToolBarWnd
);
415 AddButton(hToolBarWnd
, nStdBitmaps
+STD_CUT
, ID_EDIT_CUT
);
416 AddButton(hToolBarWnd
, nStdBitmaps
+STD_COPY
, ID_EDIT_COPY
);
417 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PASTE
, ID_EDIT_PASTE
);
418 AddButton(hToolBarWnd
, nStdBitmaps
+STD_UNDO
, ID_EDIT_UNDO
);
419 AddButton(hToolBarWnd
, nStdBitmaps
+STD_REDOW
, ID_EDIT_REDO
);
421 SendMessageW(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
| RBBS_BREAK
| RBBS_NOGRIPPER
;
427 rbb
.hwndChild
= hToolBarWnd
;
429 rbb
.cyChild
= rbb
.cyMinChild
= HIWORD(SendMessageW(hToolBarWnd
, TB_GETBUTTONSIZE
, 0, 0));
431 SendMessageW(hReBarWnd
, RB_INSERTBAND
, BANDID_TOOLBAR
, (LPARAM
)&rbb
);
433 hFormatBarWnd
= CreateToolbarEx(hReBarWnd
,
434 CCS_NOPARENTALIGN
| CCS_NOMOVEY
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
| TBSTYLE_BUTTON
,
435 IDC_FORMATBAR
, 6, hInstance
, IDB_FORMATBAR
, NULL
, 0, 24, 24, 16, 16, sizeof(TBBUTTON
));
437 ab
.hInst
= HINST_COMMCTRL
;
438 ab
.nID
= IDB_STD_SMALL_COLOR
;
439 nStdBitmaps
= SendMessageW(hFormatBarWnd
, TB_ADDBITMAP
, 6, (LPARAM
)&ab
);
441 AddButton(hFormatBarWnd
, 0, ID_FORMAT_BOLD
);
442 AddButton(hFormatBarWnd
, 1, ID_FORMAT_ITALIC
);
443 AddButton(hFormatBarWnd
, 2, ID_FORMAT_UNDERLINE
);
444 AddSeparator(hFormatBarWnd
);
445 AddButton(hFormatBarWnd
, 3, ID_ALIGN_LEFT
);
446 AddButton(hFormatBarWnd
, 4, ID_ALIGN_CENTER
);
447 AddButton(hFormatBarWnd
, 5, ID_ALIGN_RIGHT
);
449 SendMessageW(hFormatBarWnd
, TB_AUTOSIZE
, 0, 0);
451 rbb
.hwndChild
= hFormatBarWnd
;
453 SendMessageW(hReBarWnd
, RB_INSERTBAND
, BANDID_FORMATBAR
, (LPARAM
)&rbb
);
455 hDLL
= LoadLibraryW(wszRichEditDll
);
458 hEditorWnd
= CreateWindowExW(WS_EX_CLIENTEDGE
, wszRichEditClass
, NULL
,
459 WS_CHILD
|WS_VISIBLE
|ES_MULTILINE
|ES_AUTOVSCROLL
|ES_WANTRETURN
|WS_VSCROLL
,
460 0, 0, 1000, 100, hWnd
, (HMENU
)IDC_EDITOR
, hInstance
, NULL
);
464 fprintf(stderr
, "Error code %u\n", GetLastError());
469 SetFocus(hEditorWnd
);
470 SendMessageW(hEditorWnd
, EM_SETEVENTMASK
, 0, ENM_SELCHANGE
);
479 static LRESULT
OnUser( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
481 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
482 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
483 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
484 HWND hwndFormatBar
= GetDlgItem(hwndReBar
, IDC_FORMATBAR
);
489 ZeroMemory(&fmt
, sizeof(fmt
));
490 fmt
.cbSize
= sizeof(fmt
);
492 ZeroMemory(&pf
, sizeof(pf
));
493 pf
.cbSize
= sizeof(pf
);
495 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, TRUE
, (LPARAM
)&fmt
);
497 SendMessageW(hwndEditor
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
498 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_UNDO
,
499 SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0));
500 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_REDO
,
501 SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0));
502 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_CUT
, from
== to
? 0 : 1);
503 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_COPY
, from
== to
? 0 : 1);
505 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_BOLD
, (fmt
.dwMask
& CFM_BOLD
) &&
506 (fmt
.dwEffects
& CFE_BOLD
));
507 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_BOLD
, !(fmt
.dwMask
& CFM_BOLD
));
508 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_ITALIC
, (fmt
.dwMask
& CFM_ITALIC
) &&
509 (fmt
.dwEffects
& CFE_ITALIC
));
510 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_ITALIC
, !(fmt
.dwMask
& CFM_ITALIC
));
511 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_UNDERLINE
, (fmt
.dwMask
& CFM_UNDERLINE
) &&
512 (fmt
.dwEffects
& CFE_UNDERLINE
));
513 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_UNDERLINE
, !(fmt
.dwMask
& CFM_UNDERLINE
));
515 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
516 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_LEFT
, (pf
.wAlignment
== PFA_LEFT
));
517 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_CENTER
, (pf
.wAlignment
== PFA_CENTER
));
518 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_RIGHT
, (pf
.wAlignment
== PFA_RIGHT
));
523 static LRESULT
OnNotify( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
525 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
526 NMHDR
*pHdr
= (NMHDR
*)lParam
;
528 if (pHdr
->hwndFrom
!= hwndEditor
)
531 if (pHdr
->code
== EN_SELCHANGE
)
533 SELCHANGE
*pSC
= (SELCHANGE
*)lParam
;
536 sprintf( buf
,"selection = %d..%d, line count=%ld",
537 pSC
->chrg
.cpMin
, pSC
->chrg
.cpMax
,
538 SendMessage(hwndEditor
, EM_GETLINECOUNT
, 0, 0));
539 SetWindowText(GetDlgItem(hWnd
, IDC_STATUSBAR
), buf
);
540 SendMessage(hWnd
, WM_USER
, 0, 0);
546 static LRESULT
OnCommand( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
548 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
549 HWND hwndStatus
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
551 if ((HWND
)lParam
== hwndEditor
)
554 switch(LOWORD(wParam
))
557 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
562 wszFileName
[0] = '\0';
563 SetWindowTextW(hwndEditor
, wszFileName
);
564 /* FIXME: set default format too */
574 DoSaveFile(wszFileName
);
587 static const WCHAR wszNotImplemented
[] = {'N','o','t',' ',
588 'i','m','p','l','e','m','e','n','t','e','d','\0'};
589 MessageBoxW(hWnd
, wszNotImplemented
, wszAppTitle
, MB_OK
);
594 case ID_FORMAT_ITALIC
:
595 case ID_FORMAT_UNDERLINE
:
599 if (LOWORD(wParam
) == ID_FORMAT_ITALIC
) mask
= CFM_ITALIC
;
600 if (LOWORD(wParam
) == ID_FORMAT_UNDERLINE
) mask
= CFM_UNDERLINE
;
602 ZeroMemory(&fmt
, sizeof(fmt
));
603 fmt
.cbSize
= sizeof(fmt
);
604 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
605 if (!(fmt
.dwMask
&mask
))
606 fmt
.dwEffects
|= mask
;
608 fmt
.dwEffects
^= mask
;
610 SendMessageW(hwndEditor
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
615 PostMessageW(hwndEditor
, WM_CUT
, 0, 0);
619 PostMessageW(hwndEditor
, WM_COPY
, 0, 0);
623 PostMessageW(hwndEditor
, WM_PASTE
, 0, 0);
627 PostMessageW(hwndEditor
, WM_CLEAR
, 0, 0);
630 case ID_EDIT_SELECTALL
:
632 CHARRANGE range
= {0, -1};
633 SendMessageW(hwndEditor
, EM_EXSETSEL
, 0, (LPARAM
)&range
);
634 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
638 case ID_EDIT_GETTEXT
:
640 int nLen
= GetWindowTextLengthW(hwndEditor
);
641 LPWSTR data
= HeapAlloc( GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
) );
644 GetWindowTextW(hwndEditor
, data
, nLen
+1);
645 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
647 HeapFree( GetProcessHeap(), 0, data
);
648 data
= HeapAlloc(GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
));
650 tr
.chrg
.cpMax
= nLen
;
652 SendMessage (hwndEditor
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
653 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
654 HeapFree( GetProcessHeap(), 0, data
);
656 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
660 case ID_EDIT_CHARFORMAT
:
661 case ID_EDIT_DEFCHARFORMAT
:
665 ZeroMemory(&cf
, sizeof(cf
));
666 cf
.cbSize
= sizeof(cf
);
668 i
= SendMessageW(hwndEditor
, EM_GETCHARFORMAT
,
669 LOWORD(wParam
) == ID_EDIT_CHARFORMAT
, (LPARAM
)&cf
);
673 case ID_EDIT_PARAFORMAT
:
676 ZeroMemory(&pf
, sizeof(pf
));
677 pf
.cbSize
= sizeof(pf
);
678 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
682 case ID_EDIT_SELECTIONINFO
:
684 CHARRANGE range
= {0, -1};
688 SendMessage(hwndEditor
, EM_EXGETSEL
, 0, (LPARAM
)&range
);
689 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) * (range
.cpMax
-range
.cpMin
+1));
690 SendMessage(hwndEditor
, EM_GETSELTEXT
, 0, (LPARAM
)data
);
691 sprintf(buf
, "Start = %d, End = %d", range
.cpMin
, range
.cpMax
);
692 MessageBoxA(hWnd
, buf
, "Editor", MB_OK
);
693 MessageBoxW(hWnd
, data
, xszAppTitle
, MB_OK
);
694 HeapFree( GetProcessHeap(), 0, data
);
695 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
699 case ID_EDIT_READONLY
:
701 long nStyle
= GetWindowLong(hwndEditor
, GWL_STYLE
);
702 if (nStyle
& ES_READONLY
)
703 SendMessageW(hwndEditor
, EM_SETREADONLY
, 0, 0);
705 SendMessageW(hwndEditor
, EM_SETREADONLY
, 1, 0);
709 case ID_EDIT_MODIFIED
:
710 if (SendMessageW(hwndEditor
, EM_GETMODIFY
, 0, 0))
711 SendMessageW(hwndEditor
, EM_SETMODIFY
, 0, 0);
713 SendMessageW(hwndEditor
, EM_SETMODIFY
, 1, 0);
717 SendMessageW(hwndEditor
, EM_UNDO
, 0, 0);
721 SendMessageW(hwndEditor
, EM_REDO
, 0, 0);
725 case ID_ALIGN_CENTER
:
730 pf
.cbSize
= sizeof(pf
);
731 pf
.dwMask
= PFM_ALIGNMENT
;
732 switch(LOWORD(wParam
)) {
733 case ID_ALIGN_LEFT
: pf
.wAlignment
= PFA_LEFT
; break;
734 case ID_ALIGN_CENTER
: pf
.wAlignment
= PFA_CENTER
; break;
735 case ID_ALIGN_RIGHT
: pf
.wAlignment
= PFA_RIGHT
; break;
737 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
742 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 1, 0);
746 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 0, RGB(255,255,192));
749 case ID_TOGGLE_TOOLBAR
:
750 toggle_toolbar(BANDID_TOOLBAR
);
754 case ID_TOGGLE_FORMATBAR
:
755 toggle_toolbar(BANDID_FORMATBAR
);
759 case ID_TOGGLE_STATUSBAR
:
760 ShowWindow(hwndStatus
, IsWindowVisible(hwndStatus
) ? SW_HIDE
: SW_SHOW
);
765 SendMessageW(hwndEditor
, WM_COMMAND
, wParam
, lParam
);
771 static LRESULT
OnInitPopupMenu( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
773 HMENU hMenu
= (HMENU
)wParam
;
774 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
775 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
776 HWND hwndStatus
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
779 REBARBANDINFOW rbbinfo
;
781 pf
.cbSize
= sizeof(PARAFORMAT
);
782 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
783 CheckMenuItem(hMenu
, ID_EDIT_READONLY
,
784 MF_BYCOMMAND
|(GetWindowLong(hwndEditor
, GWL_STYLE
)&ES_READONLY
? MF_CHECKED
: MF_UNCHECKED
));
785 CheckMenuItem(hMenu
, ID_EDIT_MODIFIED
,
786 MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0) ? MF_CHECKED
: MF_UNCHECKED
));
787 if (pf
.dwMask
& PFM_ALIGNMENT
)
788 nAlignment
= pf
.wAlignment
;
789 CheckMenuItem(hMenu
, ID_ALIGN_LEFT
, MF_BYCOMMAND
|(nAlignment
== PFA_LEFT
) ?
790 MF_CHECKED
: MF_UNCHECKED
);
791 CheckMenuItem(hMenu
, ID_ALIGN_CENTER
, MF_BYCOMMAND
|(nAlignment
== PFA_CENTER
) ?
792 MF_CHECKED
: MF_UNCHECKED
);
793 CheckMenuItem(hMenu
, ID_ALIGN_RIGHT
, MF_BYCOMMAND
|(nAlignment
== PFA_RIGHT
) ?
794 MF_CHECKED
: MF_UNCHECKED
);
795 EnableMenuItem(hMenu
, ID_EDIT_UNDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0)) ?
796 MF_ENABLED
: MF_GRAYED
);
797 EnableMenuItem(hMenu
, ID_EDIT_REDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0)) ?
798 MF_ENABLED
: MF_GRAYED
);
800 rbbinfo
.cbSize
= sizeof(rbbinfo
);
801 rbbinfo
.fMask
= RBBIM_STYLE
;
802 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_TOOLBAR
, (LPARAM
)&rbbinfo
);
804 CheckMenuItem(hMenu
, ID_TOGGLE_TOOLBAR
, MF_BYCOMMAND
|(rbbinfo
.fStyle
& RBBS_HIDDEN
) ?
805 MF_UNCHECKED
: MF_CHECKED
);
807 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
809 CheckMenuItem(hMenu
, ID_TOGGLE_FORMATBAR
, MF_BYCOMMAND
|(rbbinfo
.fStyle
& RBBS_HIDDEN
) ?
810 MF_UNCHECKED
: MF_CHECKED
);
812 CheckMenuItem(hMenu
, ID_TOGGLE_STATUSBAR
, MF_BYCOMMAND
|IsWindowVisible(hwndStatus
) ?
813 MF_CHECKED
: MF_UNCHECKED
);
817 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
821 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
822 HWND hwndStatusBar
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
823 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
824 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
825 HWND hwndFormatBar
= GetDlgItem(hwndReBar
, IDC_FORMATBAR
);
827 REBARBANDINFOW rbbinfo
;
832 SendMessageW(hwndStatusBar
, WM_SIZE
, 0, 0);
833 if (IsWindowVisible(hwndStatusBar
))
835 GetClientRect(hwndStatusBar
, &rc
);
836 nStatusSize
= rc
.bottom
- rc
.top
;
844 rc
.left
= rc
.top
= 0;
845 rc
.right
= LOWORD(lParam
);
846 rc
.bottom
= HIWORD(lParam
);
847 SendMessageW(hwndToolBar
, TB_AUTOSIZE
, 0, 0);
848 SendMessageW(hwndReBar
, RB_SIZETORECT
, 0, (LPARAM
)&rc
);
849 GetClientRect(hwndReBar
, &rc
);
850 MoveWindow(hwndReBar
, 0, 0, LOWORD(lParam
), rc
.right
, FALSE
);
854 rc
.left
= rc
.top
= 0;
855 rc
.right
= LOWORD(lParam
);
856 rc
.bottom
= HIWORD(lParam
);
857 SendMessageW(hwndFormatBar
, TB_AUTOSIZE
, 0, 0);
858 SendMessageW(hwndReBar
, RB_SIZETORECT
, 0, (LPARAM
)&rc
);
859 GetClientRect(hwndReBar
, &rc
);
860 MoveWindow(hwndReBar
, 0, 0, LOWORD(lParam
), rc
.right
, FALSE
);
864 rbbinfo
.cbSize
= sizeof(rbbinfo
);
865 rbbinfo
.fMask
= RBBIM_STYLE
;
867 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_TOOLBAR
, (LPARAM
)&rbbinfo
);
868 if(rbbinfo
.fStyle
& RBBS_HIDDEN
)
871 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
872 if(rbbinfo
.fStyle
& RBBS_HIDDEN
)
875 rebarHeight
= rebarRows
? SendMessageW(hwndReBar
, RB_GETBARHEIGHT
, 0, 0) : 0;
879 GetClientRect(hWnd
, &rc
);
880 MoveWindow(hwndEditor
, 0, rebarHeight
, rc
.right
, rc
.bottom
-nStatusSize
-rebarHeight
, TRUE
);
883 return DefWindowProcW(hWnd
, WM_SIZE
, wParam
, lParam
);
886 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
891 return OnCreate( hWnd
, wParam
, lParam
);
894 return OnUser( hWnd
, wParam
, lParam
);
897 return OnNotify( hWnd
, wParam
, lParam
);
900 return OnCommand( hWnd
, wParam
, lParam
);
908 SetFocus(GetDlgItem(hWnd
, IDC_EDITOR
));
911 case WM_INITMENUPOPUP
:
912 return OnInitPopupMenu( hWnd
, wParam
, lParam
);
915 return OnSize( hWnd
, wParam
, lParam
);
918 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
924 int CALLBACK
WinMain(HINSTANCE hInstance
, HINSTANCE hOldInstance
, LPSTR szCmdParagraph
, int res
)
926 INITCOMMONCONTROLSEX classes
= {8, ICC_BAR_CLASSES
|ICC_COOL_CLASSES
};
930 static const WCHAR wszAccelTable
[] = {'M','A','I','N','A','C','C','E','L',
931 'T','A','B','L','E','\0'};
933 InitCommonControlsEx(&classes
);
935 hAccel
= LoadAcceleratorsW(hInstance
, wszAccelTable
);
937 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
938 wc
.lpfnWndProc
= WndProc
;
941 wc
.hInstance
= hInstance
;
942 wc
.hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
943 wc
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
944 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
945 wc
.lpszMenuName
= xszMainMenu
;
946 wc
.lpszClassName
= wszMainWndClass
;
949 hMainWnd
= CreateWindowExW(0, wszMainWndClass
, wszAppTitle
, WS_OVERLAPPEDWINDOW
,
950 CW_USEDEFAULT
, CW_USEDEFAULT
, 680, 260, NULL
, NULL
, hInstance
, NULL
);
951 ShowWindow(hMainWnd
, SW_SHOWDEFAULT
);
953 HandleCommandLine(GetCommandLineW());
955 while(GetMessageW(&msg
,0,0,0))
957 if (TranslateAcceleratorW(hMainWnd
, hAccel
, &msg
))
959 TranslateMessage(&msg
);
960 DispatchMessageW(&msg
);
961 if (!PeekMessageW(&msg
, 0, 0, 0, PM_NOREMOVE
))
962 SendMessageW(hMainWnd
, WM_USER
, 0, 0);