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 SendMessage(hwndToolBar
, TB_ADDBUTTONS
, 1, (LPARAM
)&button
);
91 static void AddSeparator(HWND hwndToolBar
)
95 ZeroMemory(&button
, sizeof(button
));
99 button
.fsStyle
= TBSTYLE_SEP
;
102 SendMessage(hwndToolBar
, TB_ADDBUTTONS
, 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 SendMessage(hEditorWnd
, EM_SETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
329 static void toggle_toolbar(int bandId
)
331 HWND hwndReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
332 REBARBANDINFOW rbbinfo
;
338 rbbinfo
.cbSize
= sizeof(rbbinfo
);
339 rbbinfo
.fMask
= RBBIM_STYLE
;
341 SendMessageW(hwndReBar
, RB_GETBANDINFO
, bandId
, (LPARAM
)&rbbinfo
);
343 SendMessageW(hwndReBar
, RB_SHOWBAND
, bandId
, (rbbinfo
.fStyle
& RBBS_HIDDEN
));
345 GetWindowRect(hMainWnd
, &rect
);
347 (void) OnSize(hMainWnd
, SIZE_RESTORED
, MAKELONG(rect
.bottom
, rect
.right
));
350 static int rebar_height(void)
352 HWND hwndReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
354 REBARBANDINFOW rbbinfo
;
359 rbbinfo
.cbSize
= sizeof(rbbinfo
);
360 rbbinfo
.fMask
= RBBIM_STYLE
;
361 SendMessageW(hwndReBar
, RB_GETBANDINFO
, BANDID_TOOLBAR
, (LPARAM
)&rbbinfo
);
363 return (rbbinfo
.fStyle
& RBBS_HIDDEN
) ? 0 : SendMessage(hwndReBar
, RB_GETBARHEIGHT
, 0, 0);
366 static LRESULT
OnCreate( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
368 HWND hToolBarWnd
, hReBarWnd
;
369 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
376 CreateStatusWindow(CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
, "RichEdit text", hWnd
, IDC_STATUSBAR
);
378 hReBarWnd
= CreateWindowEx(WS_EX_TOOLWINDOW
, REBARCLASSNAME
, NULL
,
379 CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
|WS_CLIPSIBLINGS
|WS_CLIPCHILDREN
|RBS_VARHEIGHT
|CCS_TOP
,
380 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, hWnd
, (HMENU
)IDC_REBAR
, hInstance
, NULL
);
382 rbi
.cbSize
= sizeof(rbi
);
385 if(!SendMessage(hReBarWnd
, RB_SETBARINFO
, 0, (LPARAM
)&rbi
))
388 hToolBarWnd
= CreateToolbarEx(hReBarWnd
, CCS_NOPARENTALIGN
|CCS_NOMOVEY
|WS_VISIBLE
|WS_CHILD
|TBSTYLE_TOOLTIPS
|TBSTYLE_BUTTON
,
390 6, hInstance
, IDB_TOOLBAR
,
392 24, 24, 16, 16, sizeof(TBBUTTON
));
394 ab
.hInst
= HINST_COMMCTRL
;
395 ab
.nID
= IDB_STD_SMALL_COLOR
;
396 nStdBitmaps
= SendMessage(hToolBarWnd
, TB_ADDBITMAP
, 6, (LPARAM
)&ab
);
397 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILENEW
, ID_FILE_NEW
);
398 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILEOPEN
, ID_FILE_OPEN
);
399 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILESAVE
, ID_FILE_SAVE
);
400 AddSeparator(hToolBarWnd
);
401 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINT
, ID_PRINT
);
402 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINTPRE
, ID_PREVIEW
);
403 AddSeparator(hToolBarWnd
);
404 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FIND
, ID_FIND
);
405 AddSeparator(hToolBarWnd
);
406 AddButton(hToolBarWnd
, nStdBitmaps
+STD_CUT
, ID_EDIT_CUT
);
407 AddButton(hToolBarWnd
, nStdBitmaps
+STD_COPY
, ID_EDIT_COPY
);
408 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PASTE
, ID_EDIT_PASTE
);
409 AddButton(hToolBarWnd
, nStdBitmaps
+STD_UNDO
, ID_EDIT_UNDO
);
410 AddButton(hToolBarWnd
, nStdBitmaps
+STD_REDOW
, ID_EDIT_REDO
);
411 AddSeparator(hToolBarWnd
);
412 AddButton(hToolBarWnd
, 0, ID_FORMAT_BOLD
);
413 AddButton(hToolBarWnd
, 1, ID_FORMAT_ITALIC
);
414 AddButton(hToolBarWnd
, 2, ID_FORMAT_UNDERLINE
);
415 AddSeparator(hToolBarWnd
);
416 AddButton(hToolBarWnd
, 3, ID_ALIGN_LEFT
);
417 AddButton(hToolBarWnd
, 4, ID_ALIGN_CENTER
);
418 AddButton(hToolBarWnd
, 5, ID_ALIGN_RIGHT
);
420 SendMessage(hToolBarWnd
, TB_ADDSTRING
, 0, (LPARAM
)"Exit\0");
421 SendMessage(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
;
427 rbb
.hwndChild
= hToolBarWnd
;
429 rbb
.cyChild
= rbb
.cyMinChild
= HIWORD(SendMessage(hToolBarWnd
, TB_GETBUTTONSIZE
, 0, 0));
431 SendMessageW(hReBarWnd
, RB_INSERTBAND
, BANDID_TOOLBAR
, (LPARAM
)&rbb
);
433 hDLL
= LoadLibrary("RICHED20.DLL");
436 hEditorWnd
= CreateWindowExW(WS_EX_CLIENTEDGE
, wszRichEditClass
, NULL
,
437 WS_CHILD
|WS_VISIBLE
|ES_MULTILINE
|ES_AUTOVSCROLL
|ES_WANTRETURN
|WS_VSCROLL
,
438 0, 0, 1000, 100, hWnd
, (HMENU
)IDC_EDITOR
, hInstance
, NULL
);
441 fprintf(stderr
, "Error code %u\n", GetLastError());
446 SetFocus(hEditorWnd
);
447 SendMessage(hEditorWnd
, EM_SETEVENTMASK
, 0, ENM_SELCHANGE
);
456 static LRESULT
OnUser( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
458 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
459 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
460 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
465 ZeroMemory(&fmt
, sizeof(fmt
));
466 fmt
.cbSize
= sizeof(fmt
);
468 ZeroMemory(&pf
, sizeof(pf
));
469 pf
.cbSize
= sizeof(pf
);
471 SendMessage(hwndEditor
, EM_GETCHARFORMAT
, TRUE
, (LPARAM
)&fmt
);
473 SendMessage(hwndEditor
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
474 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_UNDO
,
475 SendMessage(hwndEditor
, EM_CANUNDO
, 0, 0));
476 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_REDO
,
477 SendMessage(hwndEditor
, EM_CANREDO
, 0, 0));
478 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_CUT
, from
== to
? 0 : 1);
479 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_COPY
, from
== to
? 0 : 1);
480 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_FORMAT_BOLD
, (fmt
.dwMask
& CFM_BOLD
) && (fmt
.dwEffects
& CFE_BOLD
));
481 SendMessage(hwndToolBar
, TB_INDETERMINATE
, ID_FORMAT_BOLD
, !(fmt
.dwMask
& CFM_BOLD
));
482 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_FORMAT_ITALIC
, (fmt
.dwMask
& CFM_ITALIC
) && (fmt
.dwEffects
& CFE_ITALIC
));
483 SendMessage(hwndToolBar
, TB_INDETERMINATE
, ID_FORMAT_ITALIC
, !(fmt
.dwMask
& CFM_ITALIC
));
484 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_FORMAT_UNDERLINE
, (fmt
.dwMask
& CFM_UNDERLINE
) && (fmt
.dwEffects
& CFE_UNDERLINE
));
485 SendMessage(hwndToolBar
, TB_INDETERMINATE
, ID_FORMAT_UNDERLINE
, !(fmt
.dwMask
& CFM_UNDERLINE
));
487 SendMessage(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
488 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_ALIGN_LEFT
, (pf
.wAlignment
== PFA_LEFT
));
489 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_ALIGN_CENTER
, (pf
.wAlignment
== PFA_CENTER
));
490 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_ALIGN_RIGHT
, (pf
.wAlignment
== PFA_RIGHT
));
495 static LRESULT
OnNotify( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
497 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
498 NMHDR
*pHdr
= (NMHDR
*)lParam
;
500 if (pHdr
->hwndFrom
!= hwndEditor
)
503 if (pHdr
->code
== EN_SELCHANGE
)
505 SELCHANGE
*pSC
= (SELCHANGE
*)lParam
;
508 sprintf( buf
,"selection = %d..%d, line count=%ld",
509 pSC
->chrg
.cpMin
, pSC
->chrg
.cpMax
,
510 SendMessage(hwndEditor
, EM_GETLINECOUNT
, 0, 0));
511 SetWindowText(GetDlgItem(hWnd
, IDC_STATUSBAR
), buf
);
512 SendMessage(hWnd
, WM_USER
, 0, 0);
518 static LRESULT
OnCommand( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
520 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
522 if ((HWND
)lParam
== hwndEditor
)
525 switch(LOWORD(wParam
))
528 PostMessage(hWnd
, WM_CLOSE
, 0, 0);
532 SetWindowTextA(hwndEditor
, "");
534 wszFileName
[0] = '\0';
535 /* FIXME: set default format too */
545 DoSaveFile(wszFileName
);
557 MessageBox(hWnd
, "Not implemented", "WordPad", MB_OK
);
561 case ID_FORMAT_ITALIC
:
562 case ID_FORMAT_UNDERLINE
:
566 if (LOWORD(wParam
) == ID_FORMAT_ITALIC
) mask
= CFM_ITALIC
;
567 if (LOWORD(wParam
) == ID_FORMAT_UNDERLINE
) mask
= CFM_UNDERLINE
;
569 ZeroMemory(&fmt
, sizeof(fmt
));
570 fmt
.cbSize
= sizeof(fmt
);
571 SendMessage(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
572 if (!(fmt
.dwMask
&mask
))
573 fmt
.dwEffects
|= mask
;
575 fmt
.dwEffects
^= mask
;
577 SendMessage(hwndEditor
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
582 PostMessage(hwndEditor
, WM_CUT
, 0, 0);
586 PostMessage(hwndEditor
, WM_COPY
, 0, 0);
590 PostMessage(hwndEditor
, WM_PASTE
, 0, 0);
594 PostMessage(hwndEditor
, WM_CLEAR
, 0, 0);
597 case ID_EDIT_SELECTALL
:
599 CHARRANGE range
= {0, -1};
600 SendMessage(hwndEditor
, EM_EXSETSEL
, 0, (LPARAM
)&range
);
601 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
605 case ID_EDIT_GETTEXT
:
607 int nLen
= GetWindowTextLengthW(hwndEditor
);
608 LPWSTR data
= HeapAlloc( GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
) );
611 GetWindowTextW(hwndEditor
, data
, nLen
+1);
612 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
614 HeapFree( GetProcessHeap(), 0, data
);
615 data
= HeapAlloc(GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
));
617 tr
.chrg
.cpMax
= nLen
;
619 SendMessage (hwndEditor
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
620 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
621 HeapFree( GetProcessHeap(), 0, data
);
623 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
627 case ID_EDIT_CHARFORMAT
:
628 case ID_EDIT_DEFCHARFORMAT
:
632 ZeroMemory(&cf
, sizeof(cf
));
633 cf
.cbSize
= sizeof(cf
);
635 i
= SendMessage(hwndEditor
, EM_GETCHARFORMAT
,
636 LOWORD(wParam
) == ID_EDIT_CHARFORMAT
, (LPARAM
)&cf
);
640 case ID_EDIT_PARAFORMAT
:
643 ZeroMemory(&pf
, sizeof(pf
));
644 pf
.cbSize
= sizeof(pf
);
645 SendMessage(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
649 case ID_EDIT_SELECTIONINFO
:
651 CHARRANGE range
= {0, -1};
655 SendMessage(hwndEditor
, EM_EXGETSEL
, 0, (LPARAM
)&range
);
656 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) * (range
.cpMax
-range
.cpMin
+1));
657 SendMessage(hwndEditor
, EM_GETSELTEXT
, 0, (LPARAM
)data
);
658 sprintf(buf
, "Start = %d, End = %d", range
.cpMin
, range
.cpMax
);
659 MessageBoxA(hWnd
, buf
, "Editor", MB_OK
);
660 MessageBoxW(hWnd
, data
, xszAppTitle
, MB_OK
);
661 HeapFree( GetProcessHeap(), 0, data
);
662 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
666 case ID_EDIT_READONLY
:
668 long nStyle
= GetWindowLong(hwndEditor
, GWL_STYLE
);
669 if (nStyle
& ES_READONLY
)
670 SendMessage(hwndEditor
, EM_SETREADONLY
, 0, 0);
672 SendMessage(hwndEditor
, EM_SETREADONLY
, 1, 0);
676 case ID_EDIT_MODIFIED
:
677 if (SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0))
678 SendMessage(hwndEditor
, EM_SETMODIFY
, 0, 0);
680 SendMessage(hwndEditor
, EM_SETMODIFY
, 1, 0);
684 SendMessage(hwndEditor
, EM_UNDO
, 0, 0);
688 SendMessage(hwndEditor
, EM_REDO
, 0, 0);
692 case ID_ALIGN_CENTER
:
697 pf
.cbSize
= sizeof(pf
);
698 pf
.dwMask
= PFM_ALIGNMENT
;
699 switch(LOWORD(wParam
)) {
700 case ID_ALIGN_LEFT
: pf
.wAlignment
= PFA_LEFT
; break;
701 case ID_ALIGN_CENTER
: pf
.wAlignment
= PFA_CENTER
; break;
702 case ID_ALIGN_RIGHT
: pf
.wAlignment
= PFA_RIGHT
; break;
704 SendMessage(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
709 SendMessage(hwndEditor
, EM_SETBKGNDCOLOR
, 1, 0);
713 SendMessage(hwndEditor
, EM_SETBKGNDCOLOR
, 0, RGB(255,255,192));
716 case ID_TOGGLE_TOOLBAR
:
717 toggle_toolbar(BANDID_TOOLBAR
);
721 SendMessage(hwndEditor
, WM_COMMAND
, wParam
, lParam
);
727 static LRESULT
OnInitPopupMenu( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
729 HMENU hMenu
= (HMENU
)wParam
;
730 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
731 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
734 REBARBANDINFOW rbbinfo
;
736 pf
.cbSize
= sizeof(PARAFORMAT
);
737 SendMessage(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
738 CheckMenuItem(hMenu
, ID_EDIT_READONLY
,
739 MF_BYCOMMAND
|(GetWindowLong(hwndEditor
, GWL_STYLE
)&ES_READONLY
? MF_CHECKED
: MF_UNCHECKED
));
740 CheckMenuItem(hMenu
, ID_EDIT_MODIFIED
,
741 MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0) ? MF_CHECKED
: MF_UNCHECKED
));
742 if (pf
.dwMask
& PFM_ALIGNMENT
)
743 nAlignment
= pf
.wAlignment
;
744 CheckMenuItem(hMenu
, ID_ALIGN_LEFT
, MF_BYCOMMAND
|(nAlignment
== PFA_LEFT
) ? MF_CHECKED
: MF_UNCHECKED
);
745 CheckMenuItem(hMenu
, ID_ALIGN_CENTER
, MF_BYCOMMAND
|(nAlignment
== PFA_CENTER
) ? MF_CHECKED
: MF_UNCHECKED
);
746 CheckMenuItem(hMenu
, ID_ALIGN_RIGHT
, MF_BYCOMMAND
|(nAlignment
== PFA_RIGHT
) ? MF_CHECKED
: MF_UNCHECKED
);
747 EnableMenuItem(hMenu
, ID_EDIT_UNDO
, MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_CANUNDO
, 0, 0)) ? MF_ENABLED
: MF_GRAYED
);
748 EnableMenuItem(hMenu
, ID_EDIT_REDO
, MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_CANREDO
, 0, 0)) ? MF_ENABLED
: MF_GRAYED
);
750 rbbinfo
.cbSize
= sizeof(rbbinfo
);
751 rbbinfo
.fMask
= RBBIM_STYLE
;
752 SendMessageW(hwndReBar
, RB_GETBANDINFO
, 0, (LPARAM
)&rbbinfo
);
754 CheckMenuItem(hMenu
, ID_TOGGLE_TOOLBAR
, MF_BYCOMMAND
|(rbbinfo
.fStyle
& RBBS_HIDDEN
) ?
755 MF_UNCHECKED
: MF_CHECKED
);
760 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
764 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
765 HWND hwndStatusBar
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
766 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
767 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
772 SendMessage(hwndStatusBar
, WM_SIZE
, 0, 0);
773 GetClientRect(hwndStatusBar
, &rc
);
774 nStatusSize
= rc
.bottom
- rc
.top
;
778 rc
.left
= rc
.top
= 0;
779 rc
.right
= LOWORD(lParam
);
780 rc
.bottom
= HIWORD(lParam
);
781 SendMessage(hwndToolBar
, TB_AUTOSIZE
, 0, 0);
782 SendMessage(hwndReBar
, RB_SIZETORECT
, 0, (LPARAM
)&rc
);
783 GetClientRect(hwndReBar
, &rc
);
784 MoveWindow(hwndReBar
, 0, 0, LOWORD(lParam
), rc
.right
, FALSE
);
788 rebarHeight
= rebar_height();
789 GetClientRect(hWnd
, &rc
);
790 MoveWindow(hwndEditor
, 0, rebarHeight
, rc
.right
, rc
.bottom
-nStatusSize
-rebarHeight
, TRUE
);
793 return DefWindowProcW(hWnd
, WM_SIZE
, wParam
, lParam
);
796 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
801 return OnCreate( hWnd
, wParam
, lParam
);
804 return OnUser( hWnd
, wParam
, lParam
);
807 return OnNotify( hWnd
, wParam
, lParam
);
810 return OnCommand( hWnd
, wParam
, lParam
);
818 SetFocus(GetDlgItem(hWnd
, IDC_EDITOR
));
821 case WM_INITMENUPOPUP
:
822 return OnInitPopupMenu( hWnd
, wParam
, lParam
);
825 return OnSize( hWnd
, wParam
, lParam
);
828 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
834 int CALLBACK
WinMain(HINSTANCE hInstance
, HINSTANCE hOldInstance
, LPSTR szCmdParagraph
, int res
)
836 INITCOMMONCONTROLSEX classes
= {8, ICC_BAR_CLASSES
|ICC_COOL_CLASSES
};
841 InitCommonControlsEx(&classes
);
843 hAccel
= LoadAccelerators(hInstance
, "MAINACCELTABLE");
845 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
846 wc
.lpfnWndProc
= WndProc
;
849 wc
.hInstance
= hInstance
;
850 wc
.hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
851 wc
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
852 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
853 wc
.lpszMenuName
= xszMainMenu
;
854 wc
.lpszClassName
= wszMainWndClass
;
857 hMainWnd
= CreateWindowExW(0, wszMainWndClass
, wszAppTitle
, WS_OVERLAPPEDWINDOW
,
858 CW_USEDEFAULT
, CW_USEDEFAULT
, 680, 260, NULL
, NULL
, hInstance
, NULL
);
859 ShowWindow(hMainWnd
, SW_SHOWDEFAULT
);
861 HandleCommandLine(GetCommandLineW());
863 while(GetMessage(&msg
,0,0,0))
865 if (TranslateAccelerator(hMainWnd
, hAccel
, &msg
))
867 TranslateMessage(&msg
);
868 DispatchMessage(&msg
);
869 if (!PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
870 SendMessage(hMainWnd
, WM_USER
, 0, 0);