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
| CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINE
;
325 lstrcpyW(fmt
.szFaceName
, szFaceName
);
327 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
330 static void update_window(void)
334 GetWindowRect(hMainWnd
, &rect
);
336 (void) OnSize(hMainWnd
, SIZE_RESTORED
, MAKELONG(rect
.bottom
, rect
.right
));
339 static void toggle_toolbar(int bandId
)
341 HWND hwndReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
342 REBARBANDINFOW rbbinfo
;
348 rbbinfo
.cbSize
= sizeof(rbbinfo
);
349 rbbinfo
.fMask
= RBBIM_STYLE
| RBBIM_SIZE
;
351 SendMessageW(hwndReBar
, RB_GETBANDINFO
, bandId
, (LPARAM
)&rbbinfo
);
353 if(rbbinfo
.fStyle
& RBBS_HIDDEN
)
356 SendMessageW(hwndReBar
, RB_SHOWBAND
, bandId
, hide
? 0 : 1);
358 if(bandId
== BANDID_TOOLBAR
)
360 rbbinfo
.fMask
^= RBBIM_SIZE
;
362 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
365 rbbinfo
.fStyle
^= RBBS_BREAK
;
367 rbbinfo
.fStyle
|= RBBS_BREAK
;
369 SendMessageW(hwndReBar
, RB_SETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
373 static LRESULT
OnCreate( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
375 HWND hToolBarWnd
, hFormatBarWnd
, hReBarWnd
;
376 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
382 static const WCHAR wszRichEditDll
[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
383 static const WCHAR wszRichEditText
[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
385 CreateStatusWindowW(CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
, wszRichEditText
, hWnd
, IDC_STATUSBAR
);
387 hReBarWnd
= CreateWindowExW(WS_EX_TOOLWINDOW
, REBARCLASSNAMEW
, NULL
,
388 CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
|WS_CLIPSIBLINGS
|WS_CLIPCHILDREN
|RBS_VARHEIGHT
|CCS_TOP
,
389 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, hWnd
, (HMENU
)IDC_REBAR
, hInstance
, NULL
);
391 rbi
.cbSize
= sizeof(rbi
);
394 if(!SendMessageW(hReBarWnd
, RB_SETBARINFO
, 0, (LPARAM
)&rbi
))
397 hToolBarWnd
= CreateToolbarEx(hReBarWnd
, CCS_NOPARENTALIGN
|CCS_NOMOVEY
|WS_VISIBLE
|WS_CHILD
|TBSTYLE_TOOLTIPS
|TBSTYLE_BUTTON
,
401 24, 24, 16, 16, sizeof(TBBUTTON
));
403 ab
.hInst
= HINST_COMMCTRL
;
404 ab
.nID
= IDB_STD_SMALL_COLOR
;
405 nStdBitmaps
= SendMessageW(hToolBarWnd
, TB_ADDBITMAP
, 0, (LPARAM
)&ab
);
407 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILENEW
, ID_FILE_NEW
);
408 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILEOPEN
, ID_FILE_OPEN
);
409 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILESAVE
, ID_FILE_SAVE
);
410 AddSeparator(hToolBarWnd
);
411 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINT
, ID_PRINT
);
412 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINTPRE
, ID_PREVIEW
);
413 AddSeparator(hToolBarWnd
);
414 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FIND
, ID_FIND
);
415 AddSeparator(hToolBarWnd
);
416 AddButton(hToolBarWnd
, nStdBitmaps
+STD_CUT
, ID_EDIT_CUT
);
417 AddButton(hToolBarWnd
, nStdBitmaps
+STD_COPY
, ID_EDIT_COPY
);
418 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PASTE
, ID_EDIT_PASTE
);
419 AddButton(hToolBarWnd
, nStdBitmaps
+STD_UNDO
, ID_EDIT_UNDO
);
420 AddButton(hToolBarWnd
, nStdBitmaps
+STD_REDOW
, ID_EDIT_REDO
);
422 SendMessageW(hToolBarWnd
, TB_AUTOSIZE
, 0, 0);
424 rbb
.cbSize
= sizeof(rbb
);
425 rbb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_CHILD
| RBBIM_STYLE
;
426 rbb
.fStyle
= RBBS_CHILDEDGE
| RBBS_BREAK
| RBBS_NOGRIPPER
;
428 rbb
.hwndChild
= hToolBarWnd
;
430 rbb
.cyChild
= rbb
.cyMinChild
= HIWORD(SendMessageW(hToolBarWnd
, TB_GETBUTTONSIZE
, 0, 0));
432 SendMessageW(hReBarWnd
, RB_INSERTBAND
, BANDID_TOOLBAR
, (LPARAM
)&rbb
);
434 hFormatBarWnd
= CreateToolbarEx(hReBarWnd
,
435 CCS_NOPARENTALIGN
| CCS_NOMOVEY
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
| TBSTYLE_BUTTON
,
436 IDC_FORMATBAR
, 6, hInstance
, IDB_FORMATBAR
, NULL
, 0, 24, 24, 16, 16, sizeof(TBBUTTON
));
438 ab
.hInst
= HINST_COMMCTRL
;
439 ab
.nID
= IDB_STD_SMALL_COLOR
;
440 nStdBitmaps
= SendMessageW(hFormatBarWnd
, TB_ADDBITMAP
, 6, (LPARAM
)&ab
);
442 AddButton(hFormatBarWnd
, 0, ID_FORMAT_BOLD
);
443 AddButton(hFormatBarWnd
, 1, ID_FORMAT_ITALIC
);
444 AddButton(hFormatBarWnd
, 2, ID_FORMAT_UNDERLINE
);
445 AddSeparator(hFormatBarWnd
);
446 AddButton(hFormatBarWnd
, 3, ID_ALIGN_LEFT
);
447 AddButton(hFormatBarWnd
, 4, ID_ALIGN_CENTER
);
448 AddButton(hFormatBarWnd
, 5, ID_ALIGN_RIGHT
);
450 SendMessageW(hFormatBarWnd
, TB_AUTOSIZE
, 0, 0);
452 rbb
.hwndChild
= hFormatBarWnd
;
454 SendMessageW(hReBarWnd
, RB_INSERTBAND
, BANDID_FORMATBAR
, (LPARAM
)&rbb
);
456 hDLL
= LoadLibraryW(wszRichEditDll
);
459 hEditorWnd
= CreateWindowExW(WS_EX_CLIENTEDGE
, wszRichEditClass
, NULL
,
460 WS_CHILD
|WS_VISIBLE
|ES_MULTILINE
|ES_AUTOVSCROLL
|ES_WANTRETURN
|WS_VSCROLL
,
461 0, 0, 1000, 100, hWnd
, (HMENU
)IDC_EDITOR
, hInstance
, NULL
);
465 fprintf(stderr
, "Error code %u\n", GetLastError());
470 SetFocus(hEditorWnd
);
471 SendMessageW(hEditorWnd
, EM_SETEVENTMASK
, 0, ENM_SELCHANGE
);
480 static LRESULT
OnUser( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
482 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
483 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
484 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
485 HWND hwndFormatBar
= GetDlgItem(hwndReBar
, IDC_FORMATBAR
);
490 ZeroMemory(&fmt
, sizeof(fmt
));
491 fmt
.cbSize
= sizeof(fmt
);
493 ZeroMemory(&pf
, sizeof(pf
));
494 pf
.cbSize
= sizeof(pf
);
496 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, TRUE
, (LPARAM
)&fmt
);
498 SendMessageW(hwndEditor
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
499 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_UNDO
,
500 SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0));
501 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_REDO
,
502 SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0));
503 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_CUT
, from
== to
? 0 : 1);
504 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_COPY
, from
== to
? 0 : 1);
506 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_BOLD
, (fmt
.dwMask
& CFM_BOLD
) &&
507 (fmt
.dwEffects
& CFE_BOLD
));
508 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_BOLD
, !(fmt
.dwMask
& CFM_BOLD
));
509 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_ITALIC
, (fmt
.dwMask
& CFM_ITALIC
) &&
510 (fmt
.dwEffects
& CFE_ITALIC
));
511 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_ITALIC
, !(fmt
.dwMask
& CFM_ITALIC
));
512 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_UNDERLINE
, (fmt
.dwMask
& CFM_UNDERLINE
) &&
513 (fmt
.dwEffects
& CFE_UNDERLINE
));
514 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_UNDERLINE
, !(fmt
.dwMask
& CFM_UNDERLINE
));
516 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
517 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_LEFT
, (pf
.wAlignment
== PFA_LEFT
));
518 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_CENTER
, (pf
.wAlignment
== PFA_CENTER
));
519 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_RIGHT
, (pf
.wAlignment
== PFA_RIGHT
));
524 static LRESULT
OnNotify( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
526 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
527 NMHDR
*pHdr
= (NMHDR
*)lParam
;
529 if (pHdr
->hwndFrom
!= hwndEditor
)
532 if (pHdr
->code
== EN_SELCHANGE
)
534 SELCHANGE
*pSC
= (SELCHANGE
*)lParam
;
537 sprintf( buf
,"selection = %d..%d, line count=%ld",
538 pSC
->chrg
.cpMin
, pSC
->chrg
.cpMax
,
539 SendMessage(hwndEditor
, EM_GETLINECOUNT
, 0, 0));
540 SetWindowText(GetDlgItem(hWnd
, IDC_STATUSBAR
), buf
);
541 SendMessage(hWnd
, WM_USER
, 0, 0);
547 static LRESULT
OnCommand( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
549 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
550 HWND hwndStatus
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
552 if ((HWND
)lParam
== hwndEditor
)
555 switch(LOWORD(wParam
))
558 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
563 wszFileName
[0] = '\0';
564 SetWindowTextW(hwndEditor
, wszFileName
);
565 /* FIXME: set default format too */
575 DoSaveFile(wszFileName
);
588 static const WCHAR wszNotImplemented
[] = {'N','o','t',' ',
589 'i','m','p','l','e','m','e','n','t','e','d','\0'};
590 MessageBoxW(hWnd
, wszNotImplemented
, wszAppTitle
, MB_OK
);
595 case ID_FORMAT_ITALIC
:
596 case ID_FORMAT_UNDERLINE
:
600 if (LOWORD(wParam
) == ID_FORMAT_ITALIC
) mask
= CFM_ITALIC
;
601 if (LOWORD(wParam
) == ID_FORMAT_UNDERLINE
) mask
= CFM_UNDERLINE
;
603 ZeroMemory(&fmt
, sizeof(fmt
));
604 fmt
.cbSize
= sizeof(fmt
);
605 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
606 if (!(fmt
.dwMask
&mask
))
607 fmt
.dwEffects
|= mask
;
609 fmt
.dwEffects
^= mask
;
611 SendMessageW(hwndEditor
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
616 PostMessageW(hwndEditor
, WM_CUT
, 0, 0);
620 PostMessageW(hwndEditor
, WM_COPY
, 0, 0);
624 PostMessageW(hwndEditor
, WM_PASTE
, 0, 0);
628 PostMessageW(hwndEditor
, WM_CLEAR
, 0, 0);
631 case ID_EDIT_SELECTALL
:
633 CHARRANGE range
= {0, -1};
634 SendMessageW(hwndEditor
, EM_EXSETSEL
, 0, (LPARAM
)&range
);
635 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
639 case ID_EDIT_GETTEXT
:
641 int nLen
= GetWindowTextLengthW(hwndEditor
);
642 LPWSTR data
= HeapAlloc( GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
) );
645 GetWindowTextW(hwndEditor
, data
, nLen
+1);
646 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
648 HeapFree( GetProcessHeap(), 0, data
);
649 data
= HeapAlloc(GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
));
651 tr
.chrg
.cpMax
= nLen
;
653 SendMessage (hwndEditor
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
654 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
655 HeapFree( GetProcessHeap(), 0, data
);
657 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
661 case ID_EDIT_CHARFORMAT
:
662 case ID_EDIT_DEFCHARFORMAT
:
666 ZeroMemory(&cf
, sizeof(cf
));
667 cf
.cbSize
= sizeof(cf
);
669 i
= SendMessageW(hwndEditor
, EM_GETCHARFORMAT
,
670 LOWORD(wParam
) == ID_EDIT_CHARFORMAT
, (LPARAM
)&cf
);
674 case ID_EDIT_PARAFORMAT
:
677 ZeroMemory(&pf
, sizeof(pf
));
678 pf
.cbSize
= sizeof(pf
);
679 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
683 case ID_EDIT_SELECTIONINFO
:
685 CHARRANGE range
= {0, -1};
689 SendMessage(hwndEditor
, EM_EXGETSEL
, 0, (LPARAM
)&range
);
690 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) * (range
.cpMax
-range
.cpMin
+1));
691 SendMessage(hwndEditor
, EM_GETSELTEXT
, 0, (LPARAM
)data
);
692 sprintf(buf
, "Start = %d, End = %d", range
.cpMin
, range
.cpMax
);
693 MessageBoxA(hWnd
, buf
, "Editor", MB_OK
);
694 MessageBoxW(hWnd
, data
, xszAppTitle
, MB_OK
);
695 HeapFree( GetProcessHeap(), 0, data
);
696 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
700 case ID_EDIT_READONLY
:
702 long nStyle
= GetWindowLong(hwndEditor
, GWL_STYLE
);
703 if (nStyle
& ES_READONLY
)
704 SendMessageW(hwndEditor
, EM_SETREADONLY
, 0, 0);
706 SendMessageW(hwndEditor
, EM_SETREADONLY
, 1, 0);
710 case ID_EDIT_MODIFIED
:
711 if (SendMessageW(hwndEditor
, EM_GETMODIFY
, 0, 0))
712 SendMessageW(hwndEditor
, EM_SETMODIFY
, 0, 0);
714 SendMessageW(hwndEditor
, EM_SETMODIFY
, 1, 0);
718 SendMessageW(hwndEditor
, EM_UNDO
, 0, 0);
722 SendMessageW(hwndEditor
, EM_REDO
, 0, 0);
726 case ID_ALIGN_CENTER
:
731 pf
.cbSize
= sizeof(pf
);
732 pf
.dwMask
= PFM_ALIGNMENT
;
733 switch(LOWORD(wParam
)) {
734 case ID_ALIGN_LEFT
: pf
.wAlignment
= PFA_LEFT
; break;
735 case ID_ALIGN_CENTER
: pf
.wAlignment
= PFA_CENTER
; break;
736 case ID_ALIGN_RIGHT
: pf
.wAlignment
= PFA_RIGHT
; break;
738 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
743 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 1, 0);
747 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 0, RGB(255,255,192));
750 case ID_TOGGLE_TOOLBAR
:
751 toggle_toolbar(BANDID_TOOLBAR
);
755 case ID_TOGGLE_FORMATBAR
:
756 toggle_toolbar(BANDID_FORMATBAR
);
760 case ID_TOGGLE_STATUSBAR
:
761 ShowWindow(hwndStatus
, IsWindowVisible(hwndStatus
) ? SW_HIDE
: SW_SHOW
);
766 SendMessageW(hwndEditor
, WM_COMMAND
, wParam
, lParam
);
772 static LRESULT
OnInitPopupMenu( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
774 HMENU hMenu
= (HMENU
)wParam
;
775 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
776 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
777 HWND hwndStatus
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
780 REBARBANDINFOW rbbinfo
;
783 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&selFrom
, (LPARAM
)&selTo
);
784 EnableMenuItem(hMenu
, ID_EDIT_COPY
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
785 EnableMenuItem(hMenu
, ID_EDIT_CUT
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
787 pf
.cbSize
= sizeof(PARAFORMAT
);
788 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
789 CheckMenuItem(hMenu
, ID_EDIT_READONLY
,
790 MF_BYCOMMAND
|(GetWindowLong(hwndEditor
, GWL_STYLE
)&ES_READONLY
? MF_CHECKED
: MF_UNCHECKED
));
791 CheckMenuItem(hMenu
, ID_EDIT_MODIFIED
,
792 MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0) ? MF_CHECKED
: MF_UNCHECKED
));
793 if (pf
.dwMask
& PFM_ALIGNMENT
)
794 nAlignment
= pf
.wAlignment
;
795 CheckMenuItem(hMenu
, ID_ALIGN_LEFT
, MF_BYCOMMAND
|(nAlignment
== PFA_LEFT
) ?
796 MF_CHECKED
: MF_UNCHECKED
);
797 CheckMenuItem(hMenu
, ID_ALIGN_CENTER
, MF_BYCOMMAND
|(nAlignment
== PFA_CENTER
) ?
798 MF_CHECKED
: MF_UNCHECKED
);
799 CheckMenuItem(hMenu
, ID_ALIGN_RIGHT
, MF_BYCOMMAND
|(nAlignment
== PFA_RIGHT
) ?
800 MF_CHECKED
: MF_UNCHECKED
);
801 EnableMenuItem(hMenu
, ID_EDIT_UNDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0)) ?
802 MF_ENABLED
: MF_GRAYED
);
803 EnableMenuItem(hMenu
, ID_EDIT_REDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0)) ?
804 MF_ENABLED
: MF_GRAYED
);
806 rbbinfo
.cbSize
= sizeof(rbbinfo
);
807 rbbinfo
.fMask
= RBBIM_STYLE
;
808 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_TOOLBAR
, (LPARAM
)&rbbinfo
);
810 CheckMenuItem(hMenu
, ID_TOGGLE_TOOLBAR
, MF_BYCOMMAND
|(rbbinfo
.fStyle
& RBBS_HIDDEN
) ?
811 MF_UNCHECKED
: MF_CHECKED
);
813 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
815 CheckMenuItem(hMenu
, ID_TOGGLE_FORMATBAR
, MF_BYCOMMAND
|(rbbinfo
.fStyle
& RBBS_HIDDEN
) ?
816 MF_UNCHECKED
: MF_CHECKED
);
818 CheckMenuItem(hMenu
, ID_TOGGLE_STATUSBAR
, MF_BYCOMMAND
|IsWindowVisible(hwndStatus
) ?
819 MF_CHECKED
: MF_UNCHECKED
);
823 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
827 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
828 HWND hwndStatusBar
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
829 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
831 REBARBANDINFOW rbbinfo
;
836 SendMessageW(hwndStatusBar
, WM_SIZE
, 0, 0);
837 if (IsWindowVisible(hwndStatusBar
))
839 GetClientRect(hwndStatusBar
, &rc
);
840 nStatusSize
= rc
.bottom
- rc
.top
;
848 rbbinfo
.cbSize
= sizeof(rbbinfo
);
849 rbbinfo
.fMask
= RBBIM_STYLE
;
851 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_TOOLBAR
, (LPARAM
)&rbbinfo
);
852 if(rbbinfo
.fStyle
& RBBS_HIDDEN
)
855 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_FORMATBAR
, (LPARAM
)&rbbinfo
);
856 if(rbbinfo
.fStyle
& RBBS_HIDDEN
)
859 rebarHeight
= rebarRows
? SendMessageW(hwndReBar
, RB_GETBARHEIGHT
, 0, 0) : 0;
863 GetClientRect(hWnd
, &rc
);
864 MoveWindow(hwndEditor
, 0, rebarHeight
, rc
.right
, rc
.bottom
-nStatusSize
-rebarHeight
, TRUE
);
867 return DefWindowProcW(hWnd
, WM_SIZE
, wParam
, lParam
);
870 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
875 return OnCreate( hWnd
, wParam
, lParam
);
878 return OnUser( hWnd
, wParam
, lParam
);
881 return OnNotify( hWnd
, wParam
, lParam
);
884 return OnCommand( hWnd
, wParam
, lParam
);
892 SetFocus(GetDlgItem(hWnd
, IDC_EDITOR
));
895 case WM_INITMENUPOPUP
:
896 return OnInitPopupMenu( hWnd
, wParam
, lParam
);
899 return OnSize( hWnd
, wParam
, lParam
);
902 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
908 int CALLBACK
WinMain(HINSTANCE hInstance
, HINSTANCE hOldInstance
, LPSTR szCmdParagraph
, int res
)
910 INITCOMMONCONTROLSEX classes
= {8, ICC_BAR_CLASSES
|ICC_COOL_CLASSES
};
914 static const WCHAR wszAccelTable
[] = {'M','A','I','N','A','C','C','E','L',
915 'T','A','B','L','E','\0'};
917 InitCommonControlsEx(&classes
);
919 hAccel
= LoadAcceleratorsW(hInstance
, wszAccelTable
);
921 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
922 wc
.lpfnWndProc
= WndProc
;
925 wc
.hInstance
= hInstance
;
926 wc
.hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
927 wc
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
928 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
929 wc
.lpszMenuName
= xszMainMenu
;
930 wc
.lpszClassName
= wszMainWndClass
;
933 hMainWnd
= CreateWindowExW(0, wszMainWndClass
, wszAppTitle
, WS_OVERLAPPEDWINDOW
,
934 CW_USEDEFAULT
, CW_USEDEFAULT
, 680, 260, NULL
, NULL
, hInstance
, NULL
);
935 ShowWindow(hMainWnd
, SW_SHOWDEFAULT
);
937 HandleCommandLine(GetCommandLineW());
939 while(GetMessageW(&msg
,0,0,0))
941 if (TranslateAcceleratorW(hMainWnd
, hAccel
, &msg
))
943 TranslateMessage(&msg
);
944 DispatchMessageW(&msg
);
945 if (!PeekMessageW(&msg
, 0, 0, 0, PM_NOREMOVE
))
946 SendMessageW(hMainWnd
, WM_USER
, 0, 0);