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
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};
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};
44 static HWND hMainWnd
= NULL
;
46 static void AddButton(HWND hwndToolBar
, int nImage
, int nCommand
)
50 ZeroMemory(&button
, sizeof(button
));
51 button
.iBitmap
= nImage
;
52 button
.idCommand
= nCommand
;
53 button
.fsState
= TBSTATE_ENABLED
;
54 button
.fsStyle
= TBSTYLE_BUTTON
;
57 SendMessage(hwndToolBar
, TB_ADDBUTTONS
, 1, (LPARAM
)&button
);
60 static void AddSeparator(HWND hwndToolBar
)
64 ZeroMemory(&button
, sizeof(button
));
68 button
.fsStyle
= TBSTYLE_SEP
;
71 SendMessage(hwndToolBar
, TB_ADDBUTTONS
, 1, (LPARAM
)&button
);
74 static LRESULT
OnCreate( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
76 HWND hEditorWnd
, hToolBarWnd
, hReBarWnd
;
77 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
84 CreateStatusWindow(CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
, "RichEdit text", hWnd
, IDC_STATUSBAR
);
86 hReBarWnd
= CreateWindowEx(WS_EX_TOOLWINDOW
, REBARCLASSNAME
, NULL
,
87 CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
|WS_CLIPSIBLINGS
|WS_CLIPCHILDREN
|RBS_VARHEIGHT
|CCS_TOP
,
88 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, hWnd
, (HMENU
)IDC_REBAR
, hInstance
, NULL
);
90 rbi
.cbSize
= sizeof(rbi
);
93 if(!SendMessage(hReBarWnd
, RB_SETBARINFO
, 0, (LPARAM
)&rbi
))
96 hToolBarWnd
= CreateToolbarEx(hReBarWnd
, CCS_NOPARENTALIGN
|CCS_NODIVIDER
|CCS_NOMOVEY
|WS_VISIBLE
|WS_CHILD
|TBSTYLE_TOOLTIPS
|TBSTYLE_FLAT
,
98 3, hInstance
, IDB_TOOLBAR
,
100 24, 24, 16, 16, sizeof(TBBUTTON
));
102 ab
.hInst
= HINST_COMMCTRL
;
103 ab
.nID
= IDB_STD_SMALL_COLOR
;
104 nStdBitmaps
= SendMessage(hToolBarWnd
, TB_ADDBITMAP
, 3, (LPARAM
)&ab
);
105 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILENEW
, ID_FILE_NEW
);
106 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILEOPEN
, ID_FILE_OPEN
);
107 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILESAVE
, ID_FILE_SAVE
);
108 AddSeparator(hToolBarWnd
);
109 AddButton(hToolBarWnd
, nStdBitmaps
+STD_CUT
, ID_EDIT_CUT
);
110 AddButton(hToolBarWnd
, nStdBitmaps
+STD_COPY
, ID_EDIT_COPY
);
111 AddSeparator(hToolBarWnd
);
112 AddButton(hToolBarWnd
, nStdBitmaps
+STD_UNDO
, ID_EDIT_UNDO
);
113 AddButton(hToolBarWnd
, nStdBitmaps
+STD_REDOW
, ID_EDIT_REDO
);
114 AddSeparator(hToolBarWnd
);
115 AddButton(hToolBarWnd
, 0, ID_FORMAT_BOLD
);
116 AddButton(hToolBarWnd
, 1, ID_FORMAT_ITALIC
);
117 AddButton(hToolBarWnd
, 2, ID_FORMAT_UNDERLINE
);
119 SendMessage(hToolBarWnd
, TB_ADDSTRING
, 0, (LPARAM
)"Exit\0");
120 SendMessage(hToolBarWnd
, TB_AUTOSIZE
, 0, 0);
122 rbb
.cbSize
= sizeof(rbb
);
123 rbb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_CHILD
| RBBIM_STYLE
;
124 rbb
.fStyle
= RBBS_CHILDEDGE
;
126 rbb
.hwndChild
= hToolBarWnd
;
128 rbb
.cyChild
= rbb
.cyMinChild
= HIWORD(SendMessage(hToolBarWnd
, TB_GETBUTTONSIZE
, 0, 0));
130 SendMessage(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
132 hDLL
= LoadLibrary("RICHED20.DLL");
135 hEditorWnd
= CreateWindowExW(WS_EX_CLIENTEDGE
, wszRichEditClass
, NULL
,
136 WS_CHILD
|WS_VISIBLE
|ES_MULTILINE
|ES_AUTOVSCROLL
|ES_WANTRETURN
,
137 0, 0, 1000, 100, hWnd
, (HMENU
)IDC_EDITOR
, hInstance
, NULL
);
140 fprintf(stderr
, "Error code %lu\n", GetLastError());
145 SetFocus(hEditorWnd
);
146 SendMessage(hEditorWnd
, EM_SETEVENTMASK
, 0, ENM_SELCHANGE
);
150 static LRESULT
OnUser( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
152 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
153 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
154 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
158 ZeroMemory(&fmt
, sizeof(fmt
));
159 fmt
.cbSize
= sizeof(fmt
);
161 SendMessage(hwndEditor
, EM_GETCHARFORMAT
, TRUE
, (LPARAM
)&fmt
);
163 SendMessage(hwndEditor
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
164 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_UNDO
,
165 SendMessage(hwndEditor
, EM_CANUNDO
, 0, 0));
166 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_REDO
,
167 SendMessage(hwndEditor
, EM_CANREDO
, 0, 0));
168 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_CUT
, from
== to
? 0 : 1);
169 SendMessage(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_COPY
, from
== to
? 0 : 1);
170 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_FORMAT_BOLD
, (fmt
.dwMask
& CFM_BOLD
) && (fmt
.dwEffects
& CFE_BOLD
));
171 SendMessage(hwndToolBar
, TB_INDETERMINATE
, ID_FORMAT_BOLD
, !(fmt
.dwMask
& CFM_BOLD
));
172 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_FORMAT_ITALIC
, (fmt
.dwMask
& CFM_ITALIC
) && (fmt
.dwEffects
& CFE_ITALIC
));
173 SendMessage(hwndToolBar
, TB_INDETERMINATE
, ID_FORMAT_ITALIC
, !(fmt
.dwMask
& CFM_ITALIC
));
174 SendMessage(hwndToolBar
, TB_CHECKBUTTON
, ID_FORMAT_UNDERLINE
, (fmt
.dwMask
& CFM_UNDERLINE
) && (fmt
.dwEffects
& CFE_UNDERLINE
));
175 SendMessage(hwndToolBar
, TB_INDETERMINATE
, ID_FORMAT_UNDERLINE
, !(fmt
.dwMask
& CFM_UNDERLINE
));
179 static LRESULT
OnNotify( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
181 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
182 NMHDR
*pHdr
= (NMHDR
*)lParam
;
184 if (pHdr
->hwndFrom
!= hwndEditor
)
187 if (pHdr
->code
== EN_SELCHANGE
)
189 SELCHANGE
*pSC
= (SELCHANGE
*)lParam
;
192 sprintf( buf
,"selection = %ld..%ld, line count=%ld\n",
193 pSC
->chrg
.cpMin
, pSC
->chrg
.cpMax
,
194 SendMessage(hwndEditor
, EM_GETLINECOUNT
, 0, 0));
195 SetWindowText(GetDlgItem(hWnd
, IDC_STATUSBAR
), buf
);
196 SendMessage(hWnd
, WM_USER
, 0, 0);
202 static LRESULT
OnCommand( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
204 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
206 if ((HWND
)lParam
== hwndEditor
)
209 switch(LOWORD(wParam
))
212 PostMessage(hWnd
, WM_CLOSE
, 0, 0);
216 SetWindowTextA(hwndEditor
, "");
217 /* FIXME: set default format too */
222 MessageBox(hWnd
, "Open/Save not implemented", "WordPad", MB_OK
);
226 case ID_FORMAT_ITALIC
:
227 case ID_FORMAT_UNDERLINE
:
231 if (LOWORD(wParam
) == ID_FORMAT_ITALIC
) mask
= CFM_ITALIC
;
232 if (LOWORD(wParam
) == ID_FORMAT_UNDERLINE
) mask
= CFM_UNDERLINE
;
234 ZeroMemory(&fmt
, sizeof(fmt
));
235 fmt
.cbSize
= sizeof(fmt
);
236 SendMessage(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
237 if (!(fmt
.dwMask
&mask
))
238 fmt
.dwEffects
|= mask
;
240 fmt
.dwEffects
^= mask
;
242 SendMessage(hwndEditor
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
247 PostMessage(hwndEditor
, WM_CUT
, 0, 0);
251 PostMessage(hwndEditor
, WM_COPY
, 0, 0);
254 case ID_EDIT_SELECTALL
:
256 CHARRANGE range
= {0, -1};
257 SendMessage(hwndEditor
, EM_EXSETSEL
, 0, (LPARAM
)&range
);
258 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
262 case ID_EDIT_GETTEXT
:
264 int nLen
= GetWindowTextLengthW(hwndEditor
);
265 LPWSTR data
= HeapAlloc( GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
) );
268 GetWindowTextW(hwndEditor
, data
, nLen
+1);
269 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
271 HeapFree( GetProcessHeap(), 0, data
);
272 data
= HeapAlloc(GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
));
274 tr
.chrg
.cpMax
= nLen
;
276 SendMessage (hwndEditor
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
277 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
278 HeapFree( GetProcessHeap(), 0, data
);
280 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
284 case ID_EDIT_CHARFORMAT
:
285 case ID_EDIT_DEFCHARFORMAT
:
289 ZeroMemory(&cf
, sizeof(cf
));
290 cf
.cbSize
= sizeof(cf
);
292 i
= SendMessage(hwndEditor
, EM_GETCHARFORMAT
,
293 LOWORD(wParam
) == ID_EDIT_CHARFORMAT
, (LPARAM
)&cf
);
297 case ID_EDIT_PARAFORMAT
:
300 ZeroMemory(&pf
, sizeof(pf
));
301 pf
.cbSize
= sizeof(pf
);
302 SendMessage(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
306 case ID_EDIT_SELECTIONINFO
:
308 CHARRANGE range
= {0, -1};
312 SendMessage(hwndEditor
, EM_EXGETSEL
, 0, (LPARAM
)&range
);
313 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) * (range
.cpMax
-range
.cpMin
+1));
314 SendMessage(hwndEditor
, EM_GETSELTEXT
, 0, (LPARAM
)data
);
315 sprintf(buf
, "Start = %ld, End = %ld", range
.cpMin
, range
.cpMax
);
316 MessageBoxA(hWnd
, buf
, "Editor", MB_OK
);
317 MessageBoxW(hWnd
, data
, xszAppTitle
, MB_OK
);
318 HeapFree( GetProcessHeap(), 0, data
);
319 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
323 case ID_EDIT_READONLY
:
325 long nStyle
= GetWindowLong(hwndEditor
, GWL_STYLE
);
326 if (nStyle
& ES_READONLY
)
327 SendMessage(hwndEditor
, EM_SETREADONLY
, 0, 0);
329 SendMessage(hwndEditor
, EM_SETREADONLY
, 1, 0);
333 case ID_EDIT_MODIFIED
:
334 if (SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0))
335 SendMessage(hwndEditor
, EM_SETMODIFY
, 0, 0);
337 SendMessage(hwndEditor
, EM_SETMODIFY
, 1, 0);
341 SendMessage(hwndEditor
, EM_UNDO
, 0, 0);
345 SendMessage(hwndEditor
, EM_REDO
, 0, 0);
349 case ID_ALIGN_CENTER
:
354 pf
.cbSize
= sizeof(pf
);
355 pf
.dwMask
= PFM_ALIGNMENT
;
356 switch(LOWORD(wParam
)) {
357 case ID_ALIGN_LEFT
: pf
.wAlignment
= PFA_LEFT
; break;
358 case ID_ALIGN_CENTER
: pf
.wAlignment
= PFA_CENTER
; break;
359 case ID_ALIGN_RIGHT
: pf
.wAlignment
= PFA_RIGHT
; break;
361 SendMessage(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
366 SendMessage(hwndEditor
, EM_SETBKGNDCOLOR
, 1, 0);
370 SendMessage(hwndEditor
, EM_SETBKGNDCOLOR
, 0, RGB(255,255,192));
374 SendMessage(hwndEditor
, WM_COMMAND
, wParam
, lParam
);
380 static LRESULT
OnInitPopupMenu( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
382 HMENU hMenu
= (HMENU
)wParam
;
383 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
387 pf
.cbSize
= sizeof(PARAFORMAT
);
388 SendMessage(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
389 CheckMenuItem(hMenu
, ID_EDIT_READONLY
,
390 MF_BYCOMMAND
|(GetWindowLong(hwndEditor
, GWL_STYLE
)&ES_READONLY
? MF_CHECKED
: MF_UNCHECKED
));
391 CheckMenuItem(hMenu
, ID_EDIT_MODIFIED
,
392 MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0) ? MF_CHECKED
: MF_UNCHECKED
));
393 if (pf
.dwMask
& PFM_ALIGNMENT
)
394 nAlignment
= pf
.wAlignment
;
395 CheckMenuItem(hMenu
, ID_ALIGN_LEFT
, MF_BYCOMMAND
|(nAlignment
== PFA_LEFT
) ? MF_CHECKED
: MF_UNCHECKED
);
396 CheckMenuItem(hMenu
, ID_ALIGN_CENTER
, MF_BYCOMMAND
|(nAlignment
== PFA_CENTER
) ? MF_CHECKED
: MF_UNCHECKED
);
397 CheckMenuItem(hMenu
, ID_ALIGN_RIGHT
, MF_BYCOMMAND
|(nAlignment
== PFA_RIGHT
) ? MF_CHECKED
: MF_UNCHECKED
);
398 EnableMenuItem(hMenu
, ID_EDIT_UNDO
, MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_CANUNDO
, 0, 0)) ? MF_ENABLED
: MF_GRAYED
);
399 EnableMenuItem(hMenu
, ID_EDIT_REDO
, MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_CANREDO
, 0, 0)) ? MF_ENABLED
: MF_GRAYED
);
403 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
405 int nStatusSize
= 0, nTBSize
= 0;
407 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
408 HWND hwndStatusBar
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
409 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
410 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
414 SendMessage(hwndStatusBar
, WM_SIZE
, 0, 0);
415 GetClientRect(hwndStatusBar
, &rc
);
416 nStatusSize
= rc
.bottom
- rc
.top
;
420 rc
.left
= rc
.top
= 0;
421 rc
.right
= LOWORD(lParam
);
422 rc
.bottom
= HIWORD(lParam
);
423 SendMessage(hwndToolBar
, TB_AUTOSIZE
, 0, 0);
424 SendMessage(hwndReBar
, RB_SIZETORECT
, 0, (LPARAM
)&rc
);
425 nTBSize
= SendMessage(hwndReBar
, RB_GETBARHEIGHT
, 0, 0);
426 GetClientRect(hwndReBar
, &rc
);
427 MoveWindow(hwndReBar
, 0, 0, LOWORD(lParam
), rc
.right
, FALSE
);
431 GetClientRect(hWnd
, &rc
);
432 MoveWindow(hwndEditor
, 0, nTBSize
, rc
.right
, rc
.bottom
-nStatusSize
-nTBSize
, TRUE
);
435 return DefWindowProcW(hWnd
, WM_SIZE
, wParam
, lParam
);
438 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
443 return OnCreate( hWnd
, wParam
, lParam
);
446 return OnUser( hWnd
, wParam
, lParam
);
449 return OnNotify( hWnd
, wParam
, lParam
);
452 return OnCommand( hWnd
, wParam
, lParam
);
460 SetFocus(GetDlgItem(hWnd
, IDC_EDITOR
));
463 case WM_INITMENUPOPUP
:
464 return OnInitPopupMenu( hWnd
, wParam
, lParam
);
467 return OnSize( hWnd
, wParam
, lParam
);
470 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
476 int CALLBACK
WinMain(HINSTANCE hInstance
, HINSTANCE hOldInstance
, LPSTR szCmdParagraph
, int res
)
478 INITCOMMONCONTROLSEX classes
= {8, ICC_BAR_CLASSES
|ICC_COOL_CLASSES
};
483 InitCommonControlsEx(&classes
);
485 hAccel
= LoadAccelerators(hInstance
, "MAINACCELTABLE");
487 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
488 wc
.lpfnWndProc
= WndProc
;
491 wc
.hInstance
= hInstance
;
493 wc
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
494 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
495 wc
.lpszMenuName
= xszMainMenu
;
496 wc
.lpszClassName
= wszMainWndClass
;
499 hMainWnd
= CreateWindowExW(0, wszMainWndClass
, wszAppTitle
, WS_OVERLAPPEDWINDOW
,
500 CW_USEDEFAULT
, CW_USEDEFAULT
, 680, 260, NULL
, NULL
, hInstance
, NULL
);
501 ShowWindow(hMainWnd
, SW_SHOWMAXIMIZED
);
503 while(GetMessage(&msg
,0,0,0))
505 if (TranslateAccelerator(hMainWnd
, hAccel
, &msg
))
507 TranslateMessage(&msg
);
508 DispatchMessage(&msg
);
509 if (!PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
510 SendMessage(hMainWnd
, WM_USER
, 0, 0);