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
43 static const WCHAR xszAppTitle
[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
44 static const WCHAR xszMainMenu
[] = {'M','A','I','N','M','E','N','U',0};
46 static const WCHAR wszRichEditClass
[] = {'R','I','C','H','E','D','I','T','2','0','W',0};
47 static const WCHAR wszMainWndClass
[] = {'W','O','R','D','P','A','D','T','O','P',0};
48 static const WCHAR wszAppTitle
[] = {'W','i','n','e',' ','W','o','r','d','p','a','d',0};
50 static const WCHAR key_recentfiles
[] = {'R','e','c','e','n','t',' ','f','i','l','e',
51 ' ','l','i','s','t',0};
52 static const WCHAR key_options
[] = {'O','p','t','i','o','n','s',0};
53 static const WCHAR key_rtf
[] = {'R','T','F',0};
54 static const WCHAR key_text
[] = {'T','e','x','t',0};
56 static const WCHAR var_file
[] = {'F','i','l','e','%','d',0};
57 static const WCHAR var_framerect
[] = {'F','r','a','m','e','R','e','c','t',0};
58 static const WCHAR var_barstate0
[] = {'B','a','r','S','t','a','t','e','0',0};
59 static const WCHAR var_pagemargin
[] = {'P','a','g','e','M','a','r','g','i','n',0};
62 static HWND hEditorWnd
;
64 static HMENU hPopupMenu
;
66 static UINT ID_FINDMSGSTRING
;
68 static WCHAR wszFilter
[MAX_STRING_LEN
*4+6*3+5];
69 static WCHAR wszDefaultFileName
[MAX_STRING_LEN
];
70 static WCHAR wszSaveChanges
[MAX_STRING_LEN
];
71 static WCHAR wszPrintFilter
[MAX_STRING_LEN
*2+6+4+1];
72 static WCHAR units_cmW
[MAX_STRING_LEN
];
74 static char units_cmA
[MAX_STRING_LEN
];
76 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
);
78 /* Load string resources */
79 static void DoLoadStrings(void)
82 static const WCHAR files_rtf
[] = {'*','.','r','t','f','\0'};
83 static const WCHAR files_txt
[] = {'*','.','t','x','t','\0'};
84 static const WCHAR files_all
[] = {'*','.','*','\0'};
85 static const WCHAR files_prn
[] = {'*','.','P','R','N',0};
86 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
88 LoadStringW(hInstance
, STRING_RICHTEXT_FILES_RTF
, p
, MAX_STRING_LEN
);
90 lstrcpyW(p
, files_rtf
);
92 LoadStringW(hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
);
94 lstrcpyW(p
, files_txt
);
96 LoadStringW(hInstance
, STRING_TEXT_FILES_UNICODE_TXT
, p
, MAX_STRING_LEN
);
98 lstrcpyW(p
, files_txt
);
100 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
101 p
+= lstrlenW(p
) + 1;
102 lstrcpyW(p
, files_all
);
103 p
+= lstrlenW(p
) + 1;
107 LoadStringW(hInstance
, STRING_PRINTER_FILES_PRN
, p
, MAX_STRING_LEN
);
108 p
+= lstrlenW(p
) + 1;
109 lstrcpyW(p
, files_prn
);
110 p
+= lstrlenW(p
) + 1;
111 LoadStringW(hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
112 p
+= lstrlenW(p
) + 1;
113 lstrcpyW(p
, files_all
);
114 p
+= lstrlenW(p
) + 1;
117 p
= wszDefaultFileName
;
118 LoadStringW(hInstance
, STRING_DEFAULT_FILENAME
, p
, MAX_STRING_LEN
);
121 LoadStringW(hInstance
, STRING_PROMPT_SAVE_CHANGES
, p
, MAX_STRING_LEN
);
123 LoadStringA(hInstance
, STRING_UNITS_CM
, units_cmA
, MAX_STRING_LEN
);
124 LoadStringW(hInstance
, STRING_UNITS_CM
, units_cmW
, MAX_STRING_LEN
);
127 static void AddButton(HWND hwndToolBar
, int nImage
, int nCommand
)
131 ZeroMemory(&button
, sizeof(button
));
132 button
.iBitmap
= nImage
;
133 button
.idCommand
= nCommand
;
134 button
.fsState
= TBSTATE_ENABLED
;
135 button
.fsStyle
= TBSTYLE_BUTTON
;
138 SendMessageW(hwndToolBar
, TB_ADDBUTTONSW
, 1, (LPARAM
)&button
);
141 static void AddTextButton(HWND hWnd
, int string
, int command
, int id
)
144 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
145 static const WCHAR button
[] = {'B','U','T','T','O','N',0};
146 WCHAR text
[MAX_STRING_LEN
];
150 LoadStringW(hInstance
, string
, text
, MAX_STRING_LEN
);
151 hButton
= CreateWindowW(button
, text
,
152 WS_VISIBLE
| WS_CHILD
, 5, 5, 100, 15,
153 hMainWnd
, (HMENU
)command
, hInstance
, NULL
);
155 rb
.cbSize
= sizeof(rb
);
156 rb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_STYLE
| RBBIM_CHILD
| RBBIM_IDEALSIZE
| RBBIM_ID
;
157 rb
.fStyle
= RBBS_NOGRIPPER
| RBBS_VARIABLEHEIGHT
;
158 rb
.hwndChild
= hButton
;
159 rb
.cyChild
= rb
.cyMinChild
= 22;
160 rb
.cx
= rb
.cxMinChild
= 90;
167 SendMessageW(hWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rb
);
168 SetWindowPos(hButton
, 0, 0, 0, 90, 22, SWP_NOMOVE
| SWP_NOOWNERZORDER
| SWP_NOZORDER
);
171 static void AddSeparator(HWND hwndToolBar
)
175 ZeroMemory(&button
, sizeof(button
));
177 button
.idCommand
= 0;
179 button
.fsStyle
= TBSTYLE_SEP
;
182 SendMessageW(hwndToolBar
, TB_ADDBUTTONSW
, 1, (LPARAM
)&button
);
185 static DWORD CALLBACK
stream_in(DWORD_PTR cookie
, LPBYTE buffer
, LONG cb
, LONG
*pcb
)
187 HANDLE hFile
= (HANDLE
)cookie
;
190 if(!ReadFile(hFile
, buffer
, cb
, &read
, 0))
198 static DWORD CALLBACK
stream_out(DWORD_PTR cookie
, LPBYTE buffer
, LONG cb
, LONG
*pcb
)
202 HANDLE hFile
= (HANDLE
)cookie
;
204 ret
= WriteFile(hFile
, buffer
, cb
, &written
, 0);
206 if(!ret
|| (cb
!= written
))
215 static LPWSTR
file_basename(LPWSTR path
)
217 LPWSTR pos
= path
+ lstrlenW(path
);
221 if(*pos
== '\\' || *pos
== '/')
231 static WCHAR wszFileName
[MAX_PATH
];
232 static WPARAM fileFormat
= SF_RTF
;
234 static void set_caption(LPCWSTR wszNewFileName
)
236 static const WCHAR wszSeparator
[] = {' ','-',' '};
241 wszNewFileName
= wszDefaultFileName
;
243 wszNewFileName
= file_basename((LPWSTR
)wszNewFileName
);
245 wszCaption
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
246 lstrlenW(wszNewFileName
)*sizeof(WCHAR
)+sizeof(wszSeparator
)+sizeof(wszAppTitle
));
251 memcpy(wszCaption
, wszNewFileName
, lstrlenW(wszNewFileName
)*sizeof(WCHAR
));
252 length
+= lstrlenW(wszNewFileName
);
253 memcpy(wszCaption
+ length
, wszSeparator
, sizeof(wszSeparator
));
254 length
+= sizeof(wszSeparator
) / sizeof(WCHAR
);
255 memcpy(wszCaption
+ length
, wszAppTitle
, sizeof(wszAppTitle
));
257 SetWindowTextW(hMainWnd
, wszCaption
);
259 HeapFree(GetProcessHeap(), 0, wszCaption
);
262 static LRESULT
registry_get_handle(HKEY
*hKey
, LPDWORD action
, LPCWSTR subKey
)
265 static const WCHAR wszProgramKey
[] = {'S','o','f','t','w','a','r','e','\\',
266 'M','i','c','r','o','s','o','f','t','\\',
267 'W','i','n','d','o','w','s','\\',
268 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
269 'A','p','p','l','e','t','s','\\',
270 'W','o','r','d','p','a','d',0};
271 LPWSTR key
= (LPWSTR
)wszProgramKey
;
275 WCHAR backslash
[] = {'\\',0};
276 key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
277 (lstrlenW(wszProgramKey
)+lstrlenW(subKey
)+lstrlenW(backslash
)+1)
283 lstrcpyW(key
, wszProgramKey
);
284 lstrcatW(key
, backslash
);
285 lstrcatW(key
, subKey
);
290 ret
= RegCreateKeyExW(HKEY_CURRENT_USER
, key
, 0, NULL
, REG_OPTION_NON_VOLATILE
,
291 KEY_READ
| KEY_WRITE
, NULL
, hKey
, action
);
294 ret
= RegOpenKeyExW(HKEY_CURRENT_USER
, key
, 0, KEY_READ
| KEY_WRITE
, hKey
);
298 HeapFree(GetProcessHeap(), 0, key
);
305 static void registry_set_options(void)
310 if(registry_get_handle(&hKey
, &action
, (LPWSTR
)key_options
) == ERROR_SUCCESS
)
314 GetWindowRect(hMainWnd
, &rc
);
316 RegSetValueExW(hKey
, var_framerect
, 0, REG_BINARY
, (LPBYTE
)&rc
, sizeof(RECT
));
318 RegSetValueExW(hKey
, var_pagemargin
, 0, REG_BINARY
, (LPBYTE
)&margins
, sizeof(RECT
));
322 static RECT
registry_read_winrect(void)
326 DWORD size
= sizeof(RECT
);
328 ZeroMemory(&rc
, sizeof(RECT
));
329 if(registry_get_handle(&hKey
, 0, (LPWSTR
)key_options
) != ERROR_SUCCESS
||
330 RegQueryValueExW(hKey
, var_framerect
, 0, NULL
, (LPBYTE
)&rc
, &size
) !=
331 ERROR_SUCCESS
|| size
!= sizeof(RECT
))
343 static void truncate_path(LPWSTR file
, LPWSTR out
, LPWSTR pos1
, LPWSTR pos2
)
345 static const WCHAR dots
[] = {'.','.','.',0};
354 static void format_filelist_filename(LPWSTR file
, LPWSTR out
)
357 LPWSTR truncpos1
, truncpos2
;
358 WCHAR myDocs
[MAX_STRING_LEN
];
360 SHGetFolderPathW(NULL
, CSIDL_PERSONAL
, NULL
, SHGFP_TYPE_CURRENT
, (LPWSTR
)&myDocs
);
361 pos_basename
= file_basename(file
);
365 *(pos_basename
-1) = 0;
366 if(!lstrcmpiW(file
, myDocs
) || (lstrlenW(pos_basename
) > FILELIST_ENTRY_LENGTH
))
368 truncpos1
= pos_basename
;
369 *(pos_basename
-1) = '\\';
373 BOOL morespace
= FALSE
;
375 *(pos_basename
-1) = '\\';
377 for(pos
= file
; pos
< pos_basename
; pos
++)
379 if(*pos
== '\\' || *pos
== '/')
383 if((pos
- file
+ lstrlenW(pos_basename
)) > FILELIST_ENTRY_LENGTH
)
391 if((pos
- file
+ lstrlenW(pos_basename
)) > FILELIST_ENTRY_LENGTH
)
400 for(pos
= pos_basename
; pos
>= truncpos1
; pos
--)
402 if(*pos
== '\\' || *pos
== '/')
404 if((truncpos1
- file
+ lstrlenW(pos_basename
) + pos_basename
- pos
) > FILELIST_ENTRY_LENGTH
)
413 if(truncpos1
== pos_basename
)
414 lstrcatW(out
, pos_basename
);
415 else if(truncpos1
== truncpos2
|| !truncpos2
)
418 truncate_path(file
, out
, truncpos1
, truncpos2
? truncpos2
: (pos_basename
-1));
421 static void registry_read_filelist(HWND hMainWnd
)
425 if(registry_get_handle(&hFileKey
, 0, key_recentfiles
) == ERROR_SUCCESS
)
427 WCHAR itemText
[MAX_PATH
+3], buffer
[MAX_PATH
];
428 /* The menu item name is not the same as the file name, so we need to store
429 the file name here */
430 static WCHAR file1
[MAX_PATH
], file2
[MAX_PATH
], file3
[MAX_PATH
], file4
[MAX_PATH
];
431 WCHAR numFormat
[] = {'&','%','d',' ',0};
432 LPWSTR pFile
[] = {file1
, file2
, file3
, file4
};
433 DWORD pathSize
= MAX_PATH
*sizeof(WCHAR
);
437 HMENU hMenu
= GetMenu(hMainWnd
);
439 mi
.cbSize
= sizeof(MENUITEMINFOW
);
440 mi
.fMask
= MIIM_ID
| MIIM_DATA
| MIIM_STRING
| MIIM_FTYPE
;
441 mi
.fType
= MFT_STRING
;
442 mi
.dwTypeData
= itemText
;
443 mi
.wID
= ID_FILE_RECENT1
;
445 RemoveMenu(hMenu
, ID_FILE_RECENT_SEPARATOR
, MF_BYCOMMAND
);
446 for(i
= 0; i
< FILELIST_ENTRIES
; i
++)
448 wsprintfW(key
, var_file
, i
+1);
449 RemoveMenu(hMenu
, ID_FILE_RECENT1
+i
, MF_BYCOMMAND
);
450 if(RegQueryValueExW(hFileKey
, (LPWSTR
)key
, 0, NULL
, (LPBYTE
)pFile
[i
], &pathSize
)
454 mi
.dwItemData
= (DWORD
)pFile
[i
];
455 wsprintfW(itemText
, numFormat
, i
+1);
457 lstrcpyW(buffer
, pFile
[i
]);
459 format_filelist_filename(buffer
, itemText
);
461 InsertMenuItemW(hMenu
, ID_FILE_EXIT
, FALSE
, &mi
);
463 pathSize
= MAX_PATH
*sizeof(WCHAR
);
465 mi
.fType
= MFT_SEPARATOR
;
466 mi
.fMask
= MIIM_FTYPE
| MIIM_ID
;
467 InsertMenuItemW(hMenu
, ID_FILE_EXIT
, FALSE
, &mi
);
469 RegCloseKey(hFileKey
);
473 static void registry_set_filelist(LPCWSTR newFile
)
478 if(registry_get_handle(&hKey
, &action
, key_recentfiles
) == ERROR_SUCCESS
)
480 LPCWSTR pFiles
[FILELIST_ENTRIES
];
482 HMENU hMenu
= GetMenu(hMainWnd
);
486 mi
.cbSize
= sizeof(MENUITEMINFOW
);
487 mi
.fMask
= MIIM_DATA
;
489 for(i
= 0; i
< FILELIST_ENTRIES
; i
++)
492 for(i
= 0; GetMenuItemInfoW(hMenu
, ID_FILE_RECENT1
+i
, FALSE
, &mi
); i
++)
493 pFiles
[i
] = (LPWSTR
)mi
.dwItemData
;
495 if(lstrcmpiW(newFile
, pFiles
[0]))
497 for(i
= 0; pFiles
[i
] && i
< FILELIST_ENTRIES
; i
++)
499 if(!lstrcmpiW(pFiles
[i
], newFile
))
502 for(j
= 0; pFiles
[j
] && j
< i
; j
++)
504 pFiles
[i
-j
] = pFiles
[i
-j
-1];
516 for(i
= 0; pFiles
[i
] && i
< FILELIST_ENTRIES
-1; i
++)
517 pFiles
[FILELIST_ENTRIES
-1-i
] = pFiles
[FILELIST_ENTRIES
-2-i
];
522 for(i
= 0; pFiles
[i
] && i
< FILELIST_ENTRIES
; i
++)
524 wsprintfW(buffer
, var_file
, i
+1);
525 RegSetValueExW(hKey
, (LPWSTR
)&buffer
, 0, REG_SZ
, (LPBYTE
)pFiles
[i
],
526 (lstrlenW(pFiles
[i
])+1)*sizeof(WCHAR
));
531 registry_read_filelist(hMainWnd
);
534 static BOOL
validate_endptr(LPCSTR endptr
, BOOL units
)
536 if(!endptr
|| !*endptr
)
539 while(*endptr
== ' ')
543 return *endptr
!= '\0';
545 /* FIXME: Allow other units and convert between them */
546 if(!lstrcmpA(endptr
, units_cmA
))
549 return *endptr
!= '\0';
552 static BOOL
number_from_string(LPCWSTR string
, float *num
, BOOL units
)
555 char buffer
[MAX_STRING_LEN
];
556 char *endptr
= buffer
;
558 WideCharToMultiByte(CP_ACP
, 0, string
, -1, buffer
, MAX_STRING_LEN
, NULL
, NULL
);
561 ret
= strtod(buffer
, &endptr
);
563 if((ret
== 0 && errno
!= 0) || endptr
== buffer
|| validate_endptr(endptr
, units
))
573 static void update_font_list(void)
575 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
576 HWND hFontList
= GetDlgItem(hReBar
, IDC_FONTLIST
);
577 HWND hFontListEdit
= (HWND
)SendMessageW(hFontList
, CBEM_GETEDITCONTROL
, 0, 0);
578 WCHAR fontName
[MAX_STRING_LEN
];
581 ZeroMemory(&fmt
, sizeof(fmt
));
582 fmt
.cbSize
= sizeof(fmt
);
584 SendMessageW(hEditorWnd
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
585 SendMessageW(hFontListEdit
, WM_GETTEXT
, MAX_PATH
, (LPARAM
)fontName
);
587 if(lstrcmpW(fontName
, fmt
.szFaceName
))
588 SendMessageW(hFontListEdit
, WM_SETTEXT
, 0, (LPARAM
)fmt
.szFaceName
);
591 static void clear_formatting(void)
595 pf
.cbSize
= sizeof(pf
);
596 pf
.dwMask
= PFM_ALIGNMENT
;
597 pf
.wAlignment
= PFA_LEFT
;
598 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
601 static int reg_formatindex(WPARAM format
)
603 return (format
& SF_TEXT
) ? 1 : 0;
606 static int fileformat_number(WPARAM format
)
610 if(format
== SF_TEXT
)
613 } else if (format
== (SF_TEXT
| SF_UNICODE
))
620 static WPARAM
fileformat_flags(int format
)
622 WPARAM flags
[] = { SF_RTF
, SF_TEXT
, SF_TEXT
| SF_UNICODE
};
624 return flags
[format
];
627 static void set_font(LPCWSTR wszFaceName
)
631 ZeroMemory(&fmt
, sizeof(fmt
));
633 fmt
.cbSize
= sizeof(fmt
);
634 fmt
.dwMask
= CFM_FACE
;
636 lstrcpyW(fmt
.szFaceName
, wszFaceName
);
638 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
641 static void set_default_font(void)
643 static const WCHAR richTextFont
[] = {'T','i','m','e','s',' ','N','e','w',' ',
644 'R','o','m','a','n',0};
645 static const WCHAR plainTextFont
[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
649 ZeroMemory(&fmt
, sizeof(fmt
));
651 fmt
.cbSize
= sizeof(fmt
);
652 fmt
.dwMask
= CFM_FACE
| CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINE
;
655 if(fileFormat
& SF_RTF
)
658 font
= plainTextFont
;
660 lstrcpyW(fmt
.szFaceName
, font
);
662 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
665 static void add_font(LPWSTR fontName
, HWND hListWnd
)
667 COMBOBOXEXITEMW cbItem
;
668 WCHAR buffer
[MAX_PATH
];
670 cbItem
.mask
= CBEIF_TEXT
;
671 cbItem
.pszText
= buffer
;
672 cbItem
.cchTextMax
= MAX_STRING_LEN
;
675 while(SendMessageW(hListWnd
, CBEM_GETITEMW
, 0, (LPARAM
)&cbItem
))
677 if(lstrcmpiW(cbItem
.pszText
, fontName
) <= 0)
682 cbItem
.pszText
= fontName
;
683 SendMessageW(hListWnd
, CBEM_INSERTITEMW
, 0, (LPARAM
)&cbItem
);
686 int CALLBACK
enum_font_proc(const LOGFONTW
*lpelfe
, const TEXTMETRICW
*lpntme
,
687 DWORD FontType
, LPARAM lParam
)
689 HWND hListWnd
= (HWND
) lParam
;
691 if(SendMessageW(hListWnd
, CB_FINDSTRINGEXACT
, -1, (LPARAM
)lpelfe
->lfFaceName
) == CB_ERR
)
693 add_font((LPWSTR
)lpelfe
->lfFaceName
, hListWnd
);
699 static void populate_font_list(HWND hListWnd
)
701 HDC hdc
= GetDC(hMainWnd
);
703 HWND hListEditWnd
= (HWND
)SendMessageW(hListWnd
, CBEM_GETEDITCONTROL
, 0, 0);
706 fontinfo
.lfCharSet
= DEFAULT_CHARSET
;
707 *fontinfo
.lfFaceName
= '\0';
708 fontinfo
.lfPitchAndFamily
= 0;
710 EnumFontFamiliesExW(hdc
, &fontinfo
, enum_font_proc
,
711 (LPARAM
)hListWnd
, 0);
713 ZeroMemory(&fmt
, sizeof(fmt
));
714 fmt
.cbSize
= sizeof(fmt
);
715 SendMessageW(hEditorWnd
, EM_GETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
716 SendMessageW(hListEditWnd
, WM_SETTEXT
, 0, (LPARAM
)fmt
.szFaceName
);
719 static void update_window(void)
723 GetWindowRect(hMainWnd
, &rect
);
725 (void) OnSize(hMainWnd
, SIZE_RESTORED
, MAKELONG(rect
.bottom
, rect
.right
));
728 static DWORD barState
[2];
729 static DWORD wordWrap
[2];
731 static BOOL
is_bar_visible(int bandId
)
733 return barState
[reg_formatindex(fileFormat
)] & (1 << bandId
);
736 static void store_bar_state(int bandId
, BOOL show
)
738 int formatIndex
= reg_formatindex(fileFormat
);
741 barState
[formatIndex
] |= (1 << bandId
);
743 barState
[formatIndex
] &= ~(1 << bandId
);
746 static void set_toolbar_state(int bandId
, BOOL show
)
748 HWND hwndReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
750 SendMessageW(hwndReBar
, RB_SHOWBAND
, SendMessageW(hwndReBar
, RB_IDTOINDEX
, bandId
, 0), show
);
752 if(bandId
== BANDID_TOOLBAR
)
754 REBARBANDINFOW rbbinfo
;
755 int index
= SendMessageW(hwndReBar
, RB_IDTOINDEX
, BANDID_FONTLIST
, 0);
757 rbbinfo
.cbSize
= sizeof(rbbinfo
);
758 rbbinfo
.fMask
= RBBIM_STYLE
;
760 SendMessageW(hwndReBar
, RB_GETBANDINFO
, index
, (LPARAM
)&rbbinfo
);
763 rbbinfo
.fStyle
&= ~RBBS_BREAK
;
765 rbbinfo
.fStyle
|= RBBS_BREAK
;
767 SendMessageW(hwndReBar
, RB_SETBANDINFO
, index
, (LPARAM
)&rbbinfo
);
770 if(bandId
== BANDID_TOOLBAR
|| bandId
== BANDID_FORMATBAR
)
771 store_bar_state(bandId
, show
);
774 static void set_statusbar_state(BOOL show
)
776 HWND hStatusWnd
= GetDlgItem(hMainWnd
, IDC_STATUSBAR
);
778 ShowWindow(hStatusWnd
, show
? SW_SHOW
: SW_HIDE
);
779 store_bar_state(BANDID_STATUSBAR
, show
);
782 static void set_bar_states(void)
784 set_toolbar_state(BANDID_TOOLBAR
, is_bar_visible(BANDID_TOOLBAR
));
785 set_toolbar_state(BANDID_FONTLIST
, is_bar_visible(BANDID_FORMATBAR
));
786 set_toolbar_state(BANDID_FORMATBAR
, is_bar_visible(BANDID_FORMATBAR
));
787 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR
));
792 static HGLOBAL devMode
;
793 static HGLOBAL devNames
;
795 static HDC
make_dc(void)
797 if(devNames
&& devMode
)
799 LPDEVNAMES dn
= GlobalLock(devNames
);
800 LPDEVMODEW dm
= GlobalLock(devMode
);
803 ret
= CreateDCW((LPWSTR
)dn
+ dn
->wDriverOffset
,
804 (LPWSTR
)dn
+ dn
->wDeviceOffset
, 0, dm
);
816 static LONG
twips_to_pixels(int twips
, int dpi
)
818 float ret
= ((float)twips
/ ((float)567 * 2.54)) * (float)dpi
;
822 static LONG
devunits_to_twips(int units
, int dpi
)
824 float ret
= ((float)units
/ (float)dpi
) * (float)567 * 2.54;
828 static LONG
centmm_to_twips(int mm
)
830 return MulDiv(mm
, 567, 1000);
833 static LONG
twips_to_centmm(int twips
)
835 return MulDiv(twips
, 1000, 567);
838 static RECT
get_print_rect(HDC hdc
)
845 int dpiY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
846 int dpiX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
847 width
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALWIDTH
), dpiX
);
848 height
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALHEIGHT
), dpiY
);
851 width
= centmm_to_twips(18500);
852 height
= centmm_to_twips(27000);
855 rc
.left
= margins
.left
;
856 rc
.right
= width
- margins
.right
;
857 rc
.top
= margins
.top
;
858 rc
.bottom
= height
- margins
.bottom
;
863 static void target_device(void)
867 int index
= reg_formatindex(fileFormat
);
869 if(wordWrap
[index
] == ID_WORDWRAP_MARGIN
)
871 RECT rc
= get_print_rect(hdc
);
877 HDC hMaindc
= GetDC(hMainWnd
);
878 hdc
= CreateCompatibleDC(hMaindc
);
879 ReleaseDC(hMainWnd
, hMaindc
);
882 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, (WPARAM
)hdc
, width
);
887 static void set_fileformat(WPARAM format
)
896 static void DoOpenFile(LPCWSTR szOpenFileName
)
902 WPARAM format
= SF_TEXT
;
904 hFile
= CreateFileW(szOpenFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
905 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
906 if (hFile
== INVALID_HANDLE_VALUE
)
909 ReadFile(hFile
, fileStart
, 5, &readOut
, NULL
);
910 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
912 if(readOut
>= 2 && (BYTE
)fileStart
[0] == 0xff && (BYTE
)fileStart
[1] == 0xfe)
914 format
= SF_TEXT
| SF_UNICODE
;
915 SetFilePointer(hFile
, 2, NULL
, FILE_BEGIN
);
916 } else if(readOut
>= 5)
918 static const char header
[] = "{\\rtf";
919 if(!memcmp(header
, fileStart
, 5))
923 es
.dwCookie
= (DWORD_PTR
)hFile
;
924 es
.pfnCallback
= stream_in
;
927 set_fileformat(format
);
928 SendMessageW(hEditorWnd
, EM_STREAMIN
, format
, (LPARAM
)&es
);
932 SetFocus(hEditorWnd
);
934 set_caption(szOpenFileName
);
936 lstrcpyW(wszFileName
, szOpenFileName
);
937 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
938 registry_set_filelist(szOpenFileName
);
942 static void DoSaveFile(LPCWSTR wszSaveFileName
, WPARAM format
)
948 hFile
= CreateFileW(wszSaveFileName
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
949 FILE_ATTRIBUTE_NORMAL
, NULL
);
951 if(hFile
== INVALID_HANDLE_VALUE
)
954 if(format
== (SF_TEXT
| SF_UNICODE
))
956 static const BYTE unicode
[] = {0xff,0xfe};
958 WriteFile(hFile
, &unicode
, sizeof(unicode
), &writeOut
, 0);
960 if(writeOut
!= sizeof(unicode
))
964 stream
.dwCookie
= (DWORD_PTR
)hFile
;
965 stream
.pfnCallback
= stream_out
;
967 ret
= SendMessageW(hEditorWnd
, EM_STREAMOUT
, format
, (LPARAM
)&stream
);
971 SetFocus(hEditorWnd
);
976 gt
.flags
= GTL_DEFAULT
;
979 if(SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0))
983 lstrcpyW(wszFileName
, wszSaveFileName
);
984 set_caption(wszFileName
);
985 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
986 set_fileformat(format
);
989 static void DialogSaveFile(void)
993 WCHAR wszFile
[MAX_PATH
] = {'\0'};
994 static const WCHAR wszDefExt
[] = {'r','t','f','\0'};
996 ZeroMemory(&sfn
, sizeof(sfn
));
998 sfn
.lStructSize
= sizeof(sfn
);
999 sfn
.Flags
= OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
;
1000 sfn
.hwndOwner
= hMainWnd
;
1001 sfn
.lpstrFilter
= wszFilter
;
1002 sfn
.lpstrFile
= wszFile
;
1003 sfn
.nMaxFile
= MAX_PATH
;
1004 sfn
.lpstrDefExt
= wszDefExt
;
1005 sfn
.nFilterIndex
= fileformat_number(fileFormat
)+1;
1007 while(GetSaveFileNameW(&sfn
))
1009 if(fileformat_flags(sfn
.nFilterIndex
-1) != SF_RTF
)
1011 if(MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING
),
1012 wszAppTitle
, MB_YESNO
| MB_ICONEXCLAMATION
) != IDYES
)
1017 DoSaveFile(sfn
.lpstrFile
, fileformat_flags(sfn
.nFilterIndex
-1));
1022 DoSaveFile(sfn
.lpstrFile
, fileformat_flags(sfn
.nFilterIndex
-1));
1028 static BOOL
prompt_save_changes(void)
1033 gt
.flags
= GTL_NUMCHARS
;
1035 if(!SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0))
1039 if(!SendMessageW(hEditorWnd
, EM_GETMODIFY
, 0, 0))
1044 LPWSTR displayFileName
;
1049 displayFileName
= wszDefaultFileName
;
1051 displayFileName
= file_basename(wszFileName
);
1053 text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1054 (lstrlenW(displayFileName
)+lstrlenW(wszSaveChanges
))*sizeof(WCHAR
));
1059 wsprintfW(text
, wszSaveChanges
, displayFileName
);
1061 ret
= MessageBoxW(hMainWnd
, text
, wszAppTitle
, MB_YESNOCANCEL
| MB_ICONEXCLAMATION
);
1063 HeapFree(GetProcessHeap(), 0, text
);
1072 DoSaveFile(wszFileName
, fileFormat
);
1083 static void DialogOpenFile(void)
1087 WCHAR wszFile
[MAX_PATH
] = {'\0'};
1088 static const WCHAR wszDefExt
[] = {'r','t','f','\0'};
1090 ZeroMemory(&ofn
, sizeof(ofn
));
1092 ofn
.lStructSize
= sizeof(ofn
);
1093 ofn
.Flags
= OFN_HIDEREADONLY
| OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
;
1094 ofn
.hwndOwner
= hMainWnd
;
1095 ofn
.lpstrFilter
= wszFilter
;
1096 ofn
.lpstrFile
= wszFile
;
1097 ofn
.nMaxFile
= MAX_PATH
;
1098 ofn
.lpstrDefExt
= wszDefExt
;
1099 ofn
.nFilterIndex
= fileformat_number(fileFormat
)+1;
1101 if(GetOpenFileNameW(&ofn
))
1103 if(prompt_save_changes())
1104 DoOpenFile(ofn
.lpstrFile
);
1108 static LPWSTR
dialog_print_to_file(void)
1111 static WCHAR file
[MAX_PATH
] = {'O','U','T','P','U','T','.','P','R','N',0};
1112 static const WCHAR defExt
[] = {'P','R','N',0};
1114 ZeroMemory(&ofn
, sizeof(ofn
));
1116 ofn
.lStructSize
= sizeof(ofn
);
1117 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
1118 ofn
.hwndOwner
= hMainWnd
;
1119 ofn
.lpstrFilter
= (LPWSTR
)wszPrintFilter
;
1120 ofn
.lpstrFile
= (LPWSTR
)file
;
1121 ofn
.nMaxFile
= MAX_PATH
;
1122 ofn
.lpstrDefExt
= (LPWSTR
)defExt
;
1124 if(GetSaveFileNameW(&ofn
))
1125 return (LPWSTR
)file
;
1130 static int get_num_pages(FORMATRANGE fr
)
1137 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
,
1139 } while(fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
1144 static void char_from_pagenum(FORMATRANGE
*fr
, int page
)
1148 for(i
= 1; i
<= page
; i
++)
1153 fr
->chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)fr
);
1157 static void print(LPPRINTDLGW pd
)
1161 int printedPages
= 0;
1164 fr
.hdcTarget
= pd
->hDC
;
1166 fr
.rc
= get_print_rect(fr
.hdc
);
1168 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
1170 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
1172 ZeroMemory(&di
, sizeof(di
));
1173 di
.cbSize
= sizeof(di
);
1174 di
.lpszDocName
= (LPWSTR
)wszFileName
;
1176 if(pd
->Flags
& PD_PRINTTOFILE
)
1178 di
.lpszOutput
= dialog_print_to_file();
1183 if(pd
->Flags
& PD_SELECTION
)
1185 SendMessageW(hEditorWnd
, EM_EXGETSEL
, 0, (LPARAM
)&fr
.chrg
);
1189 gt
.flags
= GTL_DEFAULT
;
1192 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1194 if(pd
->Flags
& PD_PAGENUMS
)
1195 char_from_pagenum(&fr
, pd
->nToPage
);
1198 StartDocW(fr
.hdc
, &di
);
1201 if(StartPage(fr
.hdc
) <= 0)
1204 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
1206 if(EndPage(fr
.hdc
) <= 0)
1210 if((pd
->Flags
& PD_PAGENUMS
) && (printedPages
> (pd
->nToPage
- pd
->nFromPage
)))
1213 while(fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
1216 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
1220 static void dialog_printsetup(void)
1224 ZeroMemory(&ps
, sizeof(ps
));
1225 ps
.lStructSize
= sizeof(ps
);
1226 ps
.hwndOwner
= hMainWnd
;
1227 ps
.Flags
= PSD_INHUNDREDTHSOFMILLIMETERS
| PSD_MARGINS
;
1228 ps
.rtMargin
.left
= twips_to_centmm(margins
.left
);
1229 ps
.rtMargin
.right
= twips_to_centmm(margins
.right
);
1230 ps
.rtMargin
.top
= twips_to_centmm(margins
.top
);
1231 ps
.rtMargin
.bottom
= twips_to_centmm(margins
.bottom
);
1232 ps
.hDevMode
= devMode
;
1233 ps
.hDevNames
= devNames
;
1235 if(PageSetupDlgW(&ps
))
1237 margins
.left
= centmm_to_twips(ps
.rtMargin
.left
);
1238 margins
.right
= centmm_to_twips(ps
.rtMargin
.right
);
1239 margins
.top
= centmm_to_twips(ps
.rtMargin
.top
);
1240 margins
.bottom
= centmm_to_twips(ps
.rtMargin
.bottom
);
1241 devMode
= ps
.hDevMode
;
1242 devNames
= ps
.hDevNames
;
1247 static void get_default_printer_opts(void)
1250 ZeroMemory(&pd
, sizeof(pd
));
1252 ZeroMemory(&pd
, sizeof(pd
));
1253 pd
.lStructSize
= sizeof(pd
);
1254 pd
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
1255 pd
.hwndOwner
= hMainWnd
;
1256 pd
.hDevMode
= devMode
;
1260 devMode
= pd
.hDevMode
;
1261 devNames
= pd
.hDevNames
;
1264 static void print_quick(void)
1268 ZeroMemory(&pd
, sizeof(pd
));
1274 static void dialog_print(void)
1280 ZeroMemory(&pd
, sizeof(pd
));
1281 pd
.lStructSize
= sizeof(pd
);
1282 pd
.hwndOwner
= hMainWnd
;
1283 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
;
1286 pd
.hDevMode
= devMode
;
1287 pd
.hDevNames
= devNames
;
1289 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
1291 pd
.Flags
|= PD_NOSELECTION
;
1295 devMode
= pd
.hDevMode
;
1296 devNames
= pd
.hDevNames
;
1301 typedef struct _previewinfo
1308 } previewinfo
, *ppreviewinfo
;
1310 static previewinfo preview
;
1312 static void preview_bar_show(BOOL show
)
1314 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1319 AddTextButton(hReBar
, STRING_PREVIEW_PRINT
, ID_PRINT
, BANDID_PREVIEW_BTN1
);
1320 AddTextButton(hReBar
, STRING_PREVIEW_NEXTPAGE
, ID_PREVIEW_NEXTPAGE
, BANDID_PREVIEW_BTN2
);
1321 AddTextButton(hReBar
, STRING_PREVIEW_PREVPAGE
, ID_PREVIEW_PREVPAGE
, BANDID_PREVIEW_BTN3
);
1322 AddTextButton(hReBar
, STRING_PREVIEW_CLOSE
, ID_FILE_EXIT
, BANDID_PREVIEW_BTN4
);
1325 for(i
= 0; i
< PREVIEW_BUTTONS
; i
++)
1326 SendMessageW(hReBar
, RB_DELETEBAND
, SendMessageW(hReBar
, RB_IDTOINDEX
, BANDID_PREVIEW_BTN1
+i
, 0), 0);
1330 static void preview_exit(void)
1332 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1333 HMENU hMenu
= LoadMenuW(hInstance
, xszMainMenu
);
1336 preview
.window
.right
= 0;
1337 preview
.window
.bottom
= 0;
1340 ShowWindow(hEditorWnd
, TRUE
);
1342 preview_bar_show(FALSE
);
1344 SetMenu(hMainWnd
, hMenu
);
1345 registry_read_filelist(hMainWnd
);
1350 static LRESULT
print_preview(void)
1355 RECT window
, background
;
1356 HBITMAP hBitmapCapture
, hBitmapScaled
;
1357 int bmWidth
, bmHeight
, bmNewWidth
, bmNewHeight
;
1358 float ratioWidth
, ratioHeight
, ratio
;
1359 int xOffset
, yOffset
;
1361 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1364 hdc
= BeginPaint(hMainWnd
, &ps
);
1365 GetClientRect(hMainWnd
, &window
);
1367 fr
.hdcTarget
= make_dc();
1368 fr
.rc
= get_print_rect(fr
.hdcTarget
);
1371 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
1372 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
1374 bmWidth
= twips_to_pixels(fr
.rcPage
.right
, GetDeviceCaps(hdc
, LOGPIXELSX
));
1375 bmHeight
= twips_to_pixels(fr
.rcPage
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSY
));
1377 hBitmapCapture
= CreateCompatibleBitmap(hdc
, bmWidth
, bmHeight
);
1383 preview
.hdc
= CreateCompatibleDC(hdc
);
1384 fr
.hdc
= preview
.hdc
;
1385 gt
.flags
= GTL_DEFAULT
;
1388 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1391 paper
.right
= bmWidth
;
1393 paper
.bottom
= bmHeight
;
1396 preview
.pages
= get_num_pages(fr
);
1398 SelectObject(preview
.hdc
, hBitmapCapture
);
1400 char_from_pagenum(&fr
, preview
.page
);
1402 FillRect(preview
.hdc
, &paper
, GetStockObject(WHITE_BRUSH
));
1403 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
1404 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
1406 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_PREVPAGE
), preview
.page
> 1);
1407 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NEXTPAGE
), preview
.page
< preview
.pages
);
1410 barheight
= SendMessageW(hReBar
, RB_GETBARHEIGHT
, 0, 0);
1411 ratioWidth
= ((float)window
.right
- 20.0) / (float)bmHeight
;
1412 ratioHeight
= ((float)window
.bottom
- 20.0 - (float)barheight
) / (float)bmHeight
;
1414 if(ratioWidth
> ratioHeight
)
1415 ratio
= ratioHeight
;
1419 bmNewWidth
= (int)((float)bmWidth
* ratio
);
1420 bmNewHeight
= (int)((float)bmHeight
* ratio
);
1421 hBitmapScaled
= CreateCompatibleBitmap(hdc
, bmNewWidth
, bmNewHeight
);
1423 xOffset
= ((window
.right
- bmNewWidth
) / 2);
1424 yOffset
= ((window
.bottom
- bmNewHeight
+ barheight
) / 2);
1426 if(window
.right
!= preview
.window
.right
|| window
.bottom
!= preview
.window
.bottom
)
1428 DeleteDC(preview
.hdcSized
),
1429 preview
.hdcSized
= CreateCompatibleDC(hdc
);
1430 SelectObject(preview
.hdcSized
, hBitmapScaled
);
1432 StretchBlt(preview
.hdcSized
, 0, 0, bmNewWidth
, bmNewHeight
, preview
.hdc
, 0, 0, bmWidth
, bmHeight
, SRCCOPY
);
1435 window
.top
= barheight
;
1436 FillRect(hdc
, &window
, GetStockObject(GRAY_BRUSH
));
1438 SelectObject(hdc
, hBitmapScaled
);
1440 background
.left
= xOffset
- 2;
1441 background
.right
= xOffset
+ bmNewWidth
+ 2;
1442 background
.top
= yOffset
- 2;
1443 background
.bottom
= yOffset
+ bmNewHeight
+ 2;
1445 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
1447 BitBlt(hdc
, xOffset
, yOffset
, bmNewWidth
, bmNewHeight
, preview
.hdcSized
, 0, 0, SRCCOPY
);
1449 DeleteDC(fr
.hdcTarget
);
1450 preview
.window
= window
;
1452 EndPaint(hMainWnd
, &ps
);
1457 static LRESULT
preview_command(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1459 switch(LOWORD(wParam
))
1462 PostMessageW(hMainWnd
, WM_CLOSE
, 0, 0);
1465 case ID_PREVIEW_NEXTPAGE
:
1466 case ID_PREVIEW_PREVPAGE
:
1468 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1471 if(LOWORD(wParam
) == ID_PREVIEW_NEXTPAGE
)
1477 preview
.window
.right
= 0;
1479 GetClientRect(hMainWnd
, &rc
);
1480 rc
.top
+= SendMessageW(hReBar
, RB_GETBARHEIGHT
, 0, 0);
1481 InvalidateRect(hMainWnd
, &rc
, TRUE
);
1495 static void dialog_about(void)
1497 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1498 HICON icon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
1499 ShellAboutW(hMainWnd
, wszAppTitle
, 0, icon
);
1502 static INT_PTR CALLBACK
formatopts_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1508 LPPROPSHEETPAGEW ps
= (LPPROPSHEETPAGEW
)lParam
;
1511 HWND hIdWnd
= GetDlgItem(hWnd
, IDC_PAGEFMT_ID
);
1513 sprintf(id
, "%d\n", (int)ps
->lParam
);
1514 SetWindowTextA(hIdWnd
, id
);
1515 if(wordWrap
[ps
->lParam
] == ID_WORDWRAP_WINDOW
)
1516 wrap
= IDC_PAGEFMT_WW
;
1517 else if(wordWrap
[ps
->lParam
] == ID_WORDWRAP_MARGIN
)
1518 wrap
= IDC_PAGEFMT_WM
;
1521 CheckRadioButton(hWnd
, IDC_PAGEFMT_WW
,
1522 IDC_PAGEFMT_WM
, wrap
);
1524 if(barState
[ps
->lParam
] & (1 << BANDID_TOOLBAR
))
1525 CheckDlgButton(hWnd
, IDC_PAGEFMT_TB
, TRUE
);
1526 if(barState
[ps
->lParam
] & (1 << BANDID_FORMATBAR
))
1527 CheckDlgButton(hWnd
, IDC_PAGEFMT_FB
, TRUE
);
1528 if(barState
[ps
->lParam
] & (BANDID_STATUSBAR
))
1529 CheckDlgButton(hWnd
, IDC_PAGEFMT_SB
, TRUE
);
1534 switch(LOWORD(wParam
))
1536 case IDC_PAGEFMT_WW
:
1537 case IDC_PAGEFMT_WM
:
1538 CheckRadioButton(hWnd
, IDC_PAGEFMT_WW
, IDC_PAGEFMT_WM
,
1542 case IDC_PAGEFMT_TB
:
1543 case IDC_PAGEFMT_FB
:
1544 case IDC_PAGEFMT_SB
:
1545 CheckDlgButton(hWnd
, LOWORD(wParam
),
1546 !IsDlgButtonChecked(hWnd
, LOWORD(wParam
)));
1552 LPNMHDR header
= (LPNMHDR
)lParam
;
1553 if(header
->code
== PSN_APPLY
)
1555 HWND hIdWnd
= GetDlgItem(hWnd
, IDC_PAGEFMT_ID
);
1559 GetWindowTextA(hIdWnd
, sid
, 4);
1561 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_WW
))
1562 wordWrap
[id
] = ID_WORDWRAP_WINDOW
;
1563 else if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_WM
))
1564 wordWrap
[id
] = ID_WORDWRAP_MARGIN
;
1566 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_TB
))
1567 barState
[id
] |= (1 << BANDID_TOOLBAR
);
1569 barState
[id
] &= ~(1 << BANDID_TOOLBAR
);
1571 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_FB
))
1572 barState
[id
] |= (1 << BANDID_FORMATBAR
);
1574 barState
[id
] &= ~(1 << BANDID_FORMATBAR
);
1576 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_SB
))
1577 barState
[id
] |= (1 << BANDID_STATUSBAR
);
1579 barState
[id
] &= ~(1 << BANDID_STATUSBAR
);
1587 static void dialog_viewproperties(void)
1589 PROPSHEETPAGEW psp
[2];
1590 PROPSHEETHEADERW psh
;
1592 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1593 LPCPROPSHEETPAGEW ppsp
= (LPCPROPSHEETPAGEW
)&psp
;
1595 psp
[0].dwSize
= sizeof(PROPSHEETPAGEW
);
1596 psp
[0].dwFlags
= PSP_USETITLE
;
1597 psp
[0].pszTemplate
= MAKEINTRESOURCEW(IDD_FORMATOPTS
);
1598 psp
[0].pfnDlgProc
= formatopts_proc
;
1599 psp
[0].hInstance
= hInstance
;
1600 psp
[0].lParam
= reg_formatindex(SF_TEXT
);
1601 psp
[0].pfnCallback
= NULL
;
1602 psp
[0].pszTitle
= MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT
);
1603 for(i
= 1; i
< sizeof(psp
)/sizeof(psp
[0]); i
++)
1605 psp
[i
].dwSize
= psp
[0].dwSize
;
1606 psp
[i
].dwFlags
= psp
[0].dwFlags
;
1607 psp
[i
].pszTemplate
= psp
[0].pszTemplate
;
1608 psp
[i
].pfnDlgProc
= psp
[0].pfnDlgProc
;
1609 psp
[i
].hInstance
= psp
[0].hInstance
;
1610 psp
[i
].lParam
= reg_formatindex(SF_RTF
);
1611 psp
[i
].pfnCallback
= psp
[0].pfnCallback
;
1612 psp
[i
].pszTitle
= MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT
);
1615 psh
.dwSize
= sizeof(psh
);
1616 psh
.dwFlags
= PSH_USEICONID
| PSH_PROPSHEETPAGE
| PSH_NOAPPLYNOW
;
1617 psh
.hwndParent
= hMainWnd
;
1618 psh
.hInstance
= hInstance
;
1619 psh
.pszCaption
= MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE
);
1620 psh
.nPages
= sizeof(psp
)/sizeof(psp
[0]);
1622 psh
.pszIcon
= MAKEINTRESOURCEW(IDI_WORDPAD
);
1624 if(fileFormat
& SF_RTF
)
1628 PropertySheetW(&psh
);
1633 static void HandleCommandLine(LPWSTR cmdline
)
1638 /* skip white space */
1639 while (*cmdline
== ' ') cmdline
++;
1641 /* skip executable name */
1642 delimiter
= (*cmdline
== '"' ? '"' : ' ');
1644 if (*cmdline
== delimiter
) cmdline
++;
1645 while (*cmdline
&& *cmdline
!= delimiter
) cmdline
++;
1646 if (*cmdline
== delimiter
) cmdline
++;
1648 while (*cmdline
== ' ' || *cmdline
== '-' || *cmdline
== '/')
1652 if (*cmdline
++ == ' ') continue;
1655 if (option
) cmdline
++;
1656 while (*cmdline
== ' ') cmdline
++;
1669 /* file name is passed on the command line */
1670 if (cmdline
[0] == '"')
1673 cmdline
[lstrlenW(cmdline
) - 1] = 0;
1675 DoOpenFile(cmdline
);
1676 InvalidateRect(hMainWnd
, NULL
, FALSE
);
1680 MessageBox(hMainWnd
, "Printing not implemented", "WordPad", MB_OK
);
1683 static LRESULT
handle_findmsg(LPFINDREPLACEW pFr
)
1685 if(pFr
->Flags
& FR_DIALOGTERM
)
1688 pFr
->Flags
= FR_FINDNEXT
;
1690 } else if(pFr
->Flags
& FR_FINDNEXT
)
1692 DWORD flags
= FR_DOWN
;
1694 static CHARRANGE cr
;
1699 HMENU hMenu
= GetMenu(hMainWnd
);
1702 mi
.cbSize
= sizeof(mi
);
1703 mi
.fMask
= MIIM_DATA
;
1705 SetMenuItemInfoW(hMenu
, ID_FIND_NEXT
, FALSE
, &mi
);
1707 gt
.flags
= GTL_NUMCHARS
;
1710 length
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1712 if(pFr
->lCustData
== -1)
1714 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&startPos
, (LPARAM
)&end
);
1715 cr
.cpMin
= startPos
;
1716 pFr
->lCustData
= startPos
;
1718 if(cr
.cpMin
== length
)
1722 startPos
= pFr
->lCustData
;
1725 if(cr
.cpMax
> length
)
1733 ft
.lpstrText
= pFr
->lpstrFindWhat
;
1735 if(pFr
->Flags
& FR_MATCHCASE
)
1736 flags
|= FR_MATCHCASE
;
1737 if(pFr
->Flags
& FR_WHOLEWORD
)
1738 flags
|= FR_WHOLEWORD
;
1740 ret
= SendMessageW(hEditorWnd
, EM_FINDTEXTW
, (WPARAM
)flags
, (LPARAM
)&ft
);
1744 if(cr
.cpMax
== length
&& cr
.cpMax
!= startPos
)
1746 ft
.chrg
.cpMin
= cr
.cpMin
= 0;
1747 ft
.chrg
.cpMax
= cr
.cpMax
= startPos
;
1749 ret
= SendMessageW(hEditorWnd
, EM_FINDTEXTW
, (WPARAM
)flags
, (LPARAM
)&ft
);
1755 pFr
->lCustData
= -1;
1756 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED
), wszAppTitle
,
1757 MB_OK
| MB_ICONASTERISK
);
1760 end
= ret
+ lstrlenW(pFr
->lpstrFindWhat
);
1762 SendMessageW(hEditorWnd
, EM_SETSEL
, (WPARAM
)ret
, (LPARAM
)end
);
1763 SendMessageW(hEditorWnd
, EM_SCROLLCARET
, 0, 0);
1770 static void dialog_find(LPFINDREPLACEW fr
)
1772 static WCHAR findBuffer
[MAX_STRING_LEN
];
1774 ZeroMemory(fr
, sizeof(FINDREPLACEW
));
1775 fr
->lStructSize
= sizeof(FINDREPLACEW
);
1776 fr
->hwndOwner
= hMainWnd
;
1777 fr
->Flags
= FR_HIDEUPDOWN
;
1778 fr
->lpstrFindWhat
= findBuffer
;
1780 fr
->wFindWhatLen
= MAX_STRING_LEN
*sizeof(WCHAR
);
1782 hFindWnd
= FindTextW(fr
);
1785 static void registry_read_options(void)
1788 DWORD size
= sizeof(RECT
);
1790 if(registry_get_handle(&hKey
, 0, key_options
) != ERROR_SUCCESS
||
1791 RegQueryValueExW(hKey
, var_pagemargin
, 0, NULL
, (LPBYTE
)&margins
,
1792 &size
) != ERROR_SUCCESS
|| size
!= sizeof(RECT
))
1795 margins
.bottom
= 1417;
1796 margins
.left
= 1757;
1797 margins
.right
= 1757;
1803 static void registry_read_formatopts(int index
, LPCWSTR key
)
1807 BOOL fetched
= FALSE
;
1808 barState
[index
] = 0;
1809 wordWrap
[index
] = 0;
1811 if(registry_get_handle(&hKey
, &action
, key
) != ERROR_SUCCESS
)
1814 if(action
== REG_OPENED_EXISTING_KEY
)
1816 DWORD size
= sizeof(DWORD
);
1818 if(RegQueryValueExW(hKey
, var_barstate0
, 0, NULL
, (LPBYTE
)&barState
[index
],
1819 &size
) == ERROR_SUCCESS
)
1824 barState
[index
] = (1 << BANDID_TOOLBAR
) | (1 << BANDID_FORMATBAR
) | (1 << BANDID_RULER
) | (1 << BANDID_STATUSBAR
);
1826 if(index
== reg_formatindex(SF_RTF
))
1827 wordWrap
[index
] = ID_WORDWRAP_WINDOW
;
1828 else if(index
== reg_formatindex(SF_TEXT
))
1829 wordWrap
[index
] = ID_WORDWRAP_WINDOW
; /* FIXME: should be ID_WORDWRAP_NONE once we support it */
1834 static void registry_read_formatopts_all(void)
1836 registry_read_formatopts(reg_formatindex(SF_RTF
), key_rtf
);
1837 registry_read_formatopts(reg_formatindex(SF_TEXT
), key_text
);
1840 static void registry_set_formatopts(int index
, LPCWSTR key
)
1845 if(registry_get_handle(&hKey
, &action
, key
) == ERROR_SUCCESS
)
1847 RegSetValueExW(hKey
, var_barstate0
, 0, REG_DWORD
, (LPBYTE
)&barState
[index
],
1854 static void registry_set_formatopts_all(void)
1856 registry_set_formatopts(reg_formatindex(SF_RTF
), key_rtf
);
1857 registry_set_formatopts(reg_formatindex(SF_TEXT
), key_text
);
1860 static int current_units_to_twips(float number
)
1862 int twips
= (int)(number
* 567);
1866 static void append_current_units(LPWSTR buffer
)
1868 static const WCHAR space
[] = {' '};
1869 lstrcatW(buffer
, space
);
1870 lstrcatW(buffer
, units_cmW
);
1873 static void number_with_units(LPWSTR buffer
, int number
)
1875 float converted
= (float)number
/ 567;
1876 char string
[MAX_STRING_LEN
];
1878 sprintf(string
, "%.2f ", converted
);
1879 lstrcatA(string
, units_cmA
);
1880 MultiByteToWideChar(CP_ACP
, 0, string
, -1, buffer
, MAX_STRING_LEN
);
1883 BOOL CALLBACK
datetime_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1889 WCHAR buffer
[MAX_STRING_LEN
];
1891 HWND hListWnd
= GetDlgItem(hWnd
, IDC_DATETIME
);
1894 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &st
, 0, (LPWSTR
)&buffer
,
1896 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1897 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, 0, (LPWSTR
)&buffer
,
1899 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1900 GetTimeFormatW(LOCALE_USER_DEFAULT
, 0, &st
, 0, (LPWSTR
)&buffer
, MAX_STRING_LEN
);
1901 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1903 SendMessageW(hListWnd
, LB_SETSEL
, TRUE
, 0);
1908 switch(LOWORD(wParam
))
1913 HWND hListWnd
= GetDlgItem(hWnd
, IDC_DATETIME
);
1915 index
= SendMessageW(hListWnd
, LB_GETCURSEL
, 0, 0);
1919 WCHAR buffer
[MAX_STRING_LEN
];
1920 SendMessageW(hListWnd
, LB_GETTEXT
, index
, (LPARAM
)&buffer
);
1921 SendMessageW(hEditorWnd
, EM_REPLACESEL
, TRUE
, (LPARAM
)&buffer
);
1927 EndDialog(hWnd
, wParam
);
1934 BOOL CALLBACK
newfile_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1940 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1941 WCHAR buffer
[MAX_STRING_LEN
];
1942 HWND hListWnd
= GetDlgItem(hWnd
, IDC_NEWFILE
);
1944 LoadStringW(hInstance
, STRING_NEWFILE_RICHTEXT
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1945 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1946 LoadStringW(hInstance
, STRING_NEWFILE_TXT
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1947 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1948 LoadStringW(hInstance
, STRING_NEWFILE_TXT_UNICODE
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1949 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1951 SendMessageW(hListWnd
, LB_SETSEL
, TRUE
, 0);
1956 switch(LOWORD(wParam
))
1961 HWND hListWnd
= GetDlgItem(hWnd
, IDC_NEWFILE
);
1962 index
= SendMessageW(hListWnd
, LB_GETCURSEL
, 0, 0);
1965 EndDialog(hWnd
, MAKELONG(fileformat_flags(index
),0));
1970 EndDialog(hWnd
, MAKELONG(ID_NEWFILE_ABORT
,0));
1977 static INT_PTR CALLBACK
paraformat_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1983 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
,
1985 WCHAR buffer
[MAX_STRING_LEN
];
1986 HWND hListWnd
= GetDlgItem(hWnd
, IDC_PARA_ALIGN
);
1987 HWND hLeftWnd
= GetDlgItem(hWnd
, IDC_PARA_LEFT
);
1988 HWND hRightWnd
= GetDlgItem(hWnd
, IDC_PARA_RIGHT
);
1989 HWND hFirstWnd
= GetDlgItem(hWnd
, IDC_PARA_FIRST
);
1993 LoadStringW(hInstance
, STRING_ALIGN_LEFT
, buffer
,
1995 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1996 LoadStringW(hInstance
, STRING_ALIGN_RIGHT
, buffer
,
1998 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1999 LoadStringW(hInstance
, STRING_ALIGN_CENTER
, buffer
,
2001 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
2003 pf
.cbSize
= sizeof(pf
);
2004 pf
.dwMask
= PFM_ALIGNMENT
| PFM_OFFSET
| PFM_RIGHTINDENT
|
2006 SendMessageW(hEditorWnd
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2008 if(pf
.wAlignment
== PFA_RIGHT
)
2010 else if(pf
.wAlignment
== PFA_CENTER
)
2013 SendMessageW(hListWnd
, CB_SETCURSEL
, index
, 0);
2015 number_with_units(buffer
, pf
.dxOffset
);
2016 SetWindowTextW(hLeftWnd
, buffer
);
2017 number_with_units(buffer
, pf
.dxRightIndent
);
2018 SetWindowTextW(hRightWnd
, buffer
);
2019 number_with_units(buffer
, pf
.dxStartIndent
- pf
.dxOffset
);
2020 SetWindowTextW(hFirstWnd
, buffer
);
2025 switch(LOWORD(wParam
))
2029 HWND hLeftWnd
= GetDlgItem(hWnd
, IDC_PARA_LEFT
);
2030 HWND hRightWnd
= GetDlgItem(hWnd
, IDC_PARA_RIGHT
);
2031 HWND hFirstWnd
= GetDlgItem(hWnd
, IDC_PARA_FIRST
);
2032 WCHAR buffer
[MAX_STRING_LEN
];
2037 GetWindowTextW(hLeftWnd
, buffer
, MAX_STRING_LEN
);
2038 if(number_from_string(buffer
, &num
, TRUE
))
2040 pf
.dxOffset
= current_units_to_twips(num
);
2041 GetWindowTextW(hRightWnd
, buffer
, MAX_STRING_LEN
);
2042 if(number_from_string(buffer
, &num
, TRUE
))
2044 pf
.dxRightIndent
= current_units_to_twips(num
);
2045 GetWindowTextW(hFirstWnd
, buffer
, MAX_STRING_LEN
);
2046 if(number_from_string(buffer
, &num
, TRUE
))
2048 pf
.dxStartIndent
= current_units_to_twips(num
);
2052 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_INVALID_NUMBER
),
2053 wszAppTitle
, MB_OK
| MB_ICONASTERISK
);
2057 pf
.dxStartIndent
= pf
.dxStartIndent
+ pf
.dxOffset
;
2058 pf
.cbSize
= sizeof(pf
);
2059 pf
.dwMask
= PFM_OFFSET
| PFM_OFFSETINDENT
| PFM_RIGHTINDENT
;
2060 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2066 EndDialog(hWnd
, wParam
);
2073 static INT_PTR CALLBACK
tabstops_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
2079 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2081 WCHAR buffer
[MAX_STRING_LEN
];
2084 pf
.cbSize
= sizeof(pf
);
2085 pf
.dwMask
= PFM_TABSTOPS
;
2086 SendMessageW(hEditorWnd
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2087 SendMessageW(hTabWnd
, CB_LIMITTEXT
, MAX_STRING_LEN
-1, 0);
2089 for(i
= 0; i
< pf
.cTabCount
; i
++)
2091 number_with_units(buffer
, pf
.rgxTabs
[i
]);
2092 SendMessageW(hTabWnd
, CB_ADDSTRING
, 0, (LPARAM
)&buffer
);
2099 switch(LOWORD(wParam
))
2103 HWND hTabWnd
= (HWND
)lParam
;
2104 HWND hAddWnd
= GetDlgItem(hWnd
, ID_TAB_ADD
);
2105 HWND hDelWnd
= GetDlgItem(hWnd
, ID_TAB_DEL
);
2106 HWND hEmptyWnd
= GetDlgItem(hWnd
, ID_TAB_EMPTY
);
2108 if(GetWindowTextLengthW(hTabWnd
))
2109 EnableWindow(hAddWnd
, TRUE
);
2111 EnableWindow(hAddWnd
, FALSE
);
2113 if(SendMessageW(hTabWnd
, CB_GETCOUNT
, 0, 0))
2115 EnableWindow(hEmptyWnd
, TRUE
);
2117 if(SendMessageW(hTabWnd
, CB_GETCURSEL
, 0, 0) == CB_ERR
)
2118 EnableWindow(hDelWnd
, FALSE
);
2120 EnableWindow(hDelWnd
, TRUE
);
2123 EnableWindow(hEmptyWnd
, FALSE
);
2130 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2131 WCHAR buffer
[MAX_STRING_LEN
];
2133 GetWindowTextW(hTabWnd
, buffer
, MAX_STRING_LEN
);
2134 append_current_units(buffer
);
2136 if(SendMessageW(hTabWnd
, CB_FINDSTRINGEXACT
, -1, (LPARAM
)&buffer
) == CB_ERR
)
2140 if(!number_from_string(buffer
, &number
, TRUE
))
2142 MessageBoxW(hWnd
, MAKEINTRESOURCEW(STRING_INVALID_NUMBER
),
2143 wszAppTitle
, MB_OK
| MB_ICONINFORMATION
);
2146 SendMessageW(hTabWnd
, CB_ADDSTRING
, 0, (LPARAM
)&buffer
);
2147 SetWindowTextW(hTabWnd
, 0);
2156 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2158 ret
= SendMessageW(hTabWnd
, CB_GETCURSEL
, 0, 0);
2160 SendMessageW(hTabWnd
, CB_DELETESTRING
, ret
, 0);
2166 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2167 SendMessageW(hTabWnd
, CB_RESETCONTENT
, 0, 0);
2174 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2176 WCHAR buffer
[MAX_STRING_LEN
];
2180 pf
.cbSize
= sizeof(pf
);
2181 pf
.dwMask
= PFM_TABSTOPS
;
2183 for(i
= 0; SendMessageW(hTabWnd
, CB_GETLBTEXT
, i
,
2184 (LPARAM
)&buffer
) != CB_ERR
&&
2185 i
< MAX_TAB_STOPS
; i
++)
2187 number_from_string(buffer
, &number
, TRUE
);
2188 pf
.rgxTabs
[i
] = current_units_to_twips(number
);
2191 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2195 EndDialog(hWnd
, wParam
);
2202 static int context_menu(LPARAM lParam
)
2204 int x
= (int)(short)LOWORD(lParam
);
2205 int y
= (int)(short)HIWORD(lParam
);
2206 HMENU hPop
= GetSubMenu(hPopupMenu
, 0);
2210 int from
= 0, to
= 0;
2212 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
2213 SendMessageW(hEditorWnd
, EM_POSFROMCHAR
, (WPARAM
)&pt
, (LPARAM
)to
);
2214 ClientToScreen(hEditorWnd
, (POINT
*)&pt
);
2219 TrackPopupMenu(hPop
, TPM_LEFTALIGN
| TPM_TOPALIGN
| TPM_RIGHTBUTTON
,
2220 x
, y
, 0, hMainWnd
, 0);
2225 static LRESULT
OnCreate( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2227 HWND hToolBarWnd
, hFormatBarWnd
, hReBarWnd
, hFontListWnd
;
2228 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2231 int nStdBitmaps
= 0;
2234 static const WCHAR wszRichEditDll
[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
2235 static const WCHAR wszRichEditText
[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
2237 CreateStatusWindowW(CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
, wszRichEditText
, hWnd
, IDC_STATUSBAR
);
2239 hReBarWnd
= CreateWindowExW(WS_EX_TOOLWINDOW
, REBARCLASSNAMEW
, NULL
,
2240 CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
|WS_CLIPSIBLINGS
|WS_CLIPCHILDREN
|RBS_VARHEIGHT
|CCS_TOP
,
2241 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, hWnd
, (HMENU
)IDC_REBAR
, hInstance
, NULL
);
2243 rbi
.cbSize
= sizeof(rbi
);
2246 if(!SendMessageW(hReBarWnd
, RB_SETBARINFO
, 0, (LPARAM
)&rbi
))
2249 hToolBarWnd
= CreateToolbarEx(hReBarWnd
, CCS_NOPARENTALIGN
|CCS_NOMOVEY
|WS_VISIBLE
|WS_CHILD
|TBSTYLE_TOOLTIPS
|TBSTYLE_BUTTON
,
2251 1, hInstance
, IDB_TOOLBAR
,
2253 24, 24, 16, 16, sizeof(TBBUTTON
));
2255 ab
.hInst
= HINST_COMMCTRL
;
2256 ab
.nID
= IDB_STD_SMALL_COLOR
;
2257 nStdBitmaps
= SendMessageW(hToolBarWnd
, TB_ADDBITMAP
, 0, (LPARAM
)&ab
);
2259 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILENEW
, ID_FILE_NEW
);
2260 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILEOPEN
, ID_FILE_OPEN
);
2261 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILESAVE
, ID_FILE_SAVE
);
2262 AddSeparator(hToolBarWnd
);
2263 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINT
, ID_PRINT_QUICK
);
2264 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINTPRE
, ID_PREVIEW
);
2265 AddSeparator(hToolBarWnd
);
2266 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FIND
, ID_FIND
);
2267 AddSeparator(hToolBarWnd
);
2268 AddButton(hToolBarWnd
, nStdBitmaps
+STD_CUT
, ID_EDIT_CUT
);
2269 AddButton(hToolBarWnd
, nStdBitmaps
+STD_COPY
, ID_EDIT_COPY
);
2270 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PASTE
, ID_EDIT_PASTE
);
2271 AddButton(hToolBarWnd
, nStdBitmaps
+STD_UNDO
, ID_EDIT_UNDO
);
2272 AddButton(hToolBarWnd
, nStdBitmaps
+STD_REDOW
, ID_EDIT_REDO
);
2273 AddSeparator(hToolBarWnd
);
2274 AddButton(hToolBarWnd
, 0, ID_DATETIME
);
2276 SendMessageW(hToolBarWnd
, TB_AUTOSIZE
, 0, 0);
2278 rbb
.cbSize
= sizeof(rbb
);
2279 rbb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_CHILD
| RBBIM_STYLE
| RBBIM_ID
;
2280 rbb
.fStyle
= RBBS_CHILDEDGE
| RBBS_BREAK
| RBBS_NOGRIPPER
;
2282 rbb
.hwndChild
= hToolBarWnd
;
2284 rbb
.cyChild
= rbb
.cyMinChild
= HIWORD(SendMessageW(hToolBarWnd
, TB_GETBUTTONSIZE
, 0, 0));
2285 rbb
.wID
= BANDID_TOOLBAR
;
2287 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
2289 hFontListWnd
= CreateWindowExW(0, WC_COMBOBOXEXW
, NULL
,
2290 WS_BORDER
| WS_VISIBLE
| WS_CHILD
| CBS_DROPDOWN
| CBS_SORT
,
2291 0, 0, 200, 150, hReBarWnd
, (HMENU
)IDC_FONTLIST
, hInstance
, NULL
);
2293 rbb
.hwndChild
= hFontListWnd
;
2295 rbb
.wID
= BANDID_FONTLIST
;
2297 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
2299 hFormatBarWnd
= CreateToolbarEx(hReBarWnd
,
2300 CCS_NOPARENTALIGN
| CCS_NOMOVEY
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
| TBSTYLE_BUTTON
,
2301 IDC_FORMATBAR
, 7, hInstance
, IDB_FORMATBAR
, NULL
, 0, 16, 16, 16, 16, sizeof(TBBUTTON
));
2303 AddButton(hFormatBarWnd
, 0, ID_FORMAT_BOLD
);
2304 AddButton(hFormatBarWnd
, 1, ID_FORMAT_ITALIC
);
2305 AddButton(hFormatBarWnd
, 2, ID_FORMAT_UNDERLINE
);
2306 AddSeparator(hFormatBarWnd
);
2307 AddButton(hFormatBarWnd
, 3, ID_ALIGN_LEFT
);
2308 AddButton(hFormatBarWnd
, 4, ID_ALIGN_CENTER
);
2309 AddButton(hFormatBarWnd
, 5, ID_ALIGN_RIGHT
);
2310 AddSeparator(hFormatBarWnd
);
2311 AddButton(hFormatBarWnd
, 6, ID_BULLET
);
2313 SendMessageW(hFormatBarWnd
, TB_AUTOSIZE
, 0, 0);
2315 rbb
.hwndChild
= hFormatBarWnd
;
2316 rbb
.wID
= BANDID_FORMATBAR
;
2317 rbb
.fStyle
^= RBBS_BREAK
;
2319 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
2321 hDLL
= LoadLibraryW(wszRichEditDll
);
2324 MessageBoxW(hWnd
, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED
), wszAppTitle
,
2325 MB_OK
| MB_ICONEXCLAMATION
);
2329 hEditorWnd
= CreateWindowExW(WS_EX_CLIENTEDGE
, wszRichEditClass
, NULL
,
2330 WS_CHILD
|WS_VISIBLE
|ECO_SELECTIONBAR
|ES_MULTILINE
|ES_AUTOVSCROLL
|ES_WANTRETURN
|WS_VSCROLL
,
2331 0, 0, 1000, 100, hWnd
, (HMENU
)IDC_EDITOR
, hInstance
, NULL
);
2335 fprintf(stderr
, "Error code %u\n", GetLastError());
2340 SetFocus(hEditorWnd
);
2341 SendMessageW(hEditorWnd
, EM_SETEVENTMASK
, 0, ENM_SELCHANGE
);
2345 populate_font_list(hFontListWnd
);
2347 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
2349 ID_FINDMSGSTRING
= RegisterWindowMessageW(FINDMSGSTRINGW
);
2351 registry_read_filelist(hWnd
);
2352 registry_read_formatopts_all();
2353 registry_read_options();
2354 DragAcceptFiles(hWnd
, TRUE
);
2359 static LRESULT
OnUser( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2361 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2362 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
2363 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
2364 HWND hwndFormatBar
= GetDlgItem(hwndReBar
, IDC_FORMATBAR
);
2370 ZeroMemory(&fmt
, sizeof(fmt
));
2371 fmt
.cbSize
= sizeof(fmt
);
2373 ZeroMemory(&pf
, sizeof(pf
));
2374 pf
.cbSize
= sizeof(pf
);
2376 gt
.flags
= GTL_NUMCHARS
;
2379 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_FIND
,
2380 SendMessageW(hwndEditor
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0) ? 1 : 0);
2382 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, TRUE
, (LPARAM
)&fmt
);
2384 SendMessageW(hwndEditor
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
2385 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_UNDO
,
2386 SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0));
2387 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_REDO
,
2388 SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0));
2389 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_CUT
, from
== to
? 0 : 1);
2390 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_COPY
, from
== to
? 0 : 1);
2392 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_BOLD
, (fmt
.dwMask
& CFM_BOLD
) &&
2393 (fmt
.dwEffects
& CFE_BOLD
));
2394 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_BOLD
, !(fmt
.dwMask
& CFM_BOLD
));
2395 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_ITALIC
, (fmt
.dwMask
& CFM_ITALIC
) &&
2396 (fmt
.dwEffects
& CFE_ITALIC
));
2397 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_ITALIC
, !(fmt
.dwMask
& CFM_ITALIC
));
2398 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_UNDERLINE
, (fmt
.dwMask
& CFM_UNDERLINE
) &&
2399 (fmt
.dwEffects
& CFE_UNDERLINE
));
2400 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_UNDERLINE
, !(fmt
.dwMask
& CFM_UNDERLINE
));
2402 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2403 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_LEFT
, (pf
.wAlignment
== PFA_LEFT
));
2404 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_CENTER
, (pf
.wAlignment
== PFA_CENTER
));
2405 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_RIGHT
, (pf
.wAlignment
== PFA_RIGHT
));
2407 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_BULLET
, (pf
.wNumbering
& PFN_BULLET
));
2411 static LRESULT
OnNotify( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2413 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2414 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
2415 NMHDR
*pHdr
= (NMHDR
*)lParam
;
2416 HWND hwndFontList
= GetDlgItem(hwndReBar
, IDC_FONTLIST
);
2418 if (pHdr
->hwndFrom
== hwndFontList
)
2420 if (pHdr
->code
== CBEN_ENDEDITW
)
2422 CHARFORMAT2W format
;
2423 NMCBEENDEDIT
*endEdit
= (NMCBEENDEDIT
*)lParam
;
2425 ZeroMemory(&format
, sizeof(format
));
2426 format
.cbSize
= sizeof(format
);
2427 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&format
);
2429 if(lstrcmpW(format
.szFaceName
, (LPWSTR
)endEdit
->szText
))
2430 set_font((LPCWSTR
)endEdit
->szText
);
2435 if (pHdr
->hwndFrom
!= hwndEditor
)
2438 if (pHdr
->code
== EN_SELCHANGE
)
2440 SELCHANGE
*pSC
= (SELCHANGE
*)lParam
;
2445 sprintf( buf
,"selection = %d..%d, line count=%ld",
2446 pSC
->chrg
.cpMin
, pSC
->chrg
.cpMax
,
2447 SendMessage(hwndEditor
, EM_GETLINECOUNT
, 0, 0));
2448 SetWindowText(GetDlgItem(hWnd
, IDC_STATUSBAR
), buf
);
2449 SendMessage(hWnd
, WM_USER
, 0, 0);
2455 static LRESULT
OnCommand( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2457 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2458 static FINDREPLACEW findreplace
;
2460 if ((HWND
)lParam
== hwndEditor
)
2463 switch(LOWORD(wParam
))
2467 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
2472 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2473 int ret
= DialogBox(hInstance
, MAKEINTRESOURCE(IDD_NEWFILE
), hWnd
,
2474 (DLGPROC
)newfile_proc
);
2476 if(ret
!= ID_NEWFILE_ABORT
)
2478 if(prompt_save_changes())
2483 wszFileName
[0] = '\0';
2487 st
.flags
= ST_DEFAULT
;
2489 SendMessageW(hEditorWnd
, EM_SETTEXTEX
, (WPARAM
)&st
, 0);
2491 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
2492 set_fileformat(ret
);
2506 DoSaveFile(wszFileName
, fileFormat
);
2511 case ID_FILE_SAVEAS
:
2515 case ID_FILE_RECENT1
:
2516 case ID_FILE_RECENT2
:
2517 case ID_FILE_RECENT3
:
2518 case ID_FILE_RECENT4
:
2520 HMENU hMenu
= GetMenu(hWnd
);
2523 mi
.cbSize
= sizeof(MENUITEMINFOW
);
2524 mi
.fMask
= MIIM_DATA
;
2525 if(GetMenuItemInfoW(hMenu
, LOWORD(wParam
), FALSE
, &mi
))
2526 DoOpenFile((LPWSTR
)mi
.dwItemData
);
2531 dialog_find(&findreplace
);
2535 handle_findmsg(&findreplace
);
2542 case ID_PRINT_QUICK
:
2548 int index
= reg_formatindex(fileFormat
);
2549 DWORD tmp
= barState
[index
];
2550 barState
[index
] = 0;
2552 barState
[index
] = tmp
;
2553 ShowWindow(hEditorWnd
, FALSE
);
2554 preview_bar_show(TRUE
);
2558 SetMenu(hWnd
, NULL
);
2559 InvalidateRect(0, 0, TRUE
);
2564 dialog_printsetup();
2567 case ID_FORMAT_BOLD
:
2568 case ID_FORMAT_ITALIC
:
2569 case ID_FORMAT_UNDERLINE
:
2572 int mask
= CFM_BOLD
;
2573 if (LOWORD(wParam
) == ID_FORMAT_ITALIC
) mask
= CFM_ITALIC
;
2574 if (LOWORD(wParam
) == ID_FORMAT_UNDERLINE
) mask
= CFM_UNDERLINE
;
2576 ZeroMemory(&fmt
, sizeof(fmt
));
2577 fmt
.cbSize
= sizeof(fmt
);
2578 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
2579 if (!(fmt
.dwMask
&mask
))
2580 fmt
.dwEffects
|= mask
;
2582 fmt
.dwEffects
^= mask
;
2584 SendMessageW(hwndEditor
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
2589 PostMessageW(hwndEditor
, WM_CUT
, 0, 0);
2593 PostMessageW(hwndEditor
, WM_COPY
, 0, 0);
2597 PostMessageW(hwndEditor
, WM_PASTE
, 0, 0);
2601 PostMessageW(hwndEditor
, WM_CLEAR
, 0, 0);
2604 case ID_EDIT_SELECTALL
:
2606 CHARRANGE range
= {0, -1};
2607 SendMessageW(hwndEditor
, EM_EXSETSEL
, 0, (LPARAM
)&range
);
2608 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2612 case ID_EDIT_GETTEXT
:
2614 int nLen
= GetWindowTextLengthW(hwndEditor
);
2615 LPWSTR data
= HeapAlloc( GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
) );
2618 GetWindowTextW(hwndEditor
, data
, nLen
+1);
2619 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
2621 HeapFree( GetProcessHeap(), 0, data
);
2622 data
= HeapAlloc(GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
));
2624 tr
.chrg
.cpMax
= nLen
;
2625 tr
.lpstrText
= data
;
2626 SendMessage (hwndEditor
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
2627 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
2628 HeapFree( GetProcessHeap(), 0, data
);
2630 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2634 case ID_EDIT_CHARFORMAT
:
2635 case ID_EDIT_DEFCHARFORMAT
:
2639 ZeroMemory(&cf
, sizeof(cf
));
2640 cf
.cbSize
= sizeof(cf
);
2642 i
= SendMessageW(hwndEditor
, EM_GETCHARFORMAT
,
2643 LOWORD(wParam
) == ID_EDIT_CHARFORMAT
, (LPARAM
)&cf
);
2647 case ID_EDIT_PARAFORMAT
:
2650 ZeroMemory(&pf
, sizeof(pf
));
2651 pf
.cbSize
= sizeof(pf
);
2652 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2656 case ID_EDIT_SELECTIONINFO
:
2658 CHARRANGE range
= {0, -1};
2662 SendMessage(hwndEditor
, EM_EXGETSEL
, 0, (LPARAM
)&range
);
2663 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) * (range
.cpMax
-range
.cpMin
+1));
2664 SendMessage(hwndEditor
, EM_GETSELTEXT
, 0, (LPARAM
)data
);
2665 sprintf(buf
, "Start = %d, End = %d", range
.cpMin
, range
.cpMax
);
2666 MessageBoxA(hWnd
, buf
, "Editor", MB_OK
);
2667 MessageBoxW(hWnd
, data
, xszAppTitle
, MB_OK
);
2668 HeapFree( GetProcessHeap(), 0, data
);
2669 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2673 case ID_EDIT_READONLY
:
2675 long nStyle
= GetWindowLong(hwndEditor
, GWL_STYLE
);
2676 if (nStyle
& ES_READONLY
)
2677 SendMessageW(hwndEditor
, EM_SETREADONLY
, 0, 0);
2679 SendMessageW(hwndEditor
, EM_SETREADONLY
, 1, 0);
2683 case ID_EDIT_MODIFIED
:
2684 if (SendMessageW(hwndEditor
, EM_GETMODIFY
, 0, 0))
2685 SendMessageW(hwndEditor
, EM_SETMODIFY
, 0, 0);
2687 SendMessageW(hwndEditor
, EM_SETMODIFY
, 1, 0);
2691 SendMessageW(hwndEditor
, EM_UNDO
, 0, 0);
2695 SendMessageW(hwndEditor
, EM_REDO
, 0, 0);
2702 pf
.cbSize
= sizeof(pf
);
2703 pf
.dwMask
= PFM_NUMBERING
;
2704 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2706 pf
.dwMask
|= PFM_OFFSET
;
2708 if(pf
.wNumbering
== PFN_BULLET
)
2714 pf
.wNumbering
= PFN_BULLET
;
2718 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2723 case ID_ALIGN_CENTER
:
2724 case ID_ALIGN_RIGHT
:
2728 pf
.cbSize
= sizeof(pf
);
2729 pf
.dwMask
= PFM_ALIGNMENT
;
2730 switch(LOWORD(wParam
)) {
2731 case ID_ALIGN_LEFT
: pf
.wAlignment
= PFA_LEFT
; break;
2732 case ID_ALIGN_CENTER
: pf
.wAlignment
= PFA_CENTER
; break;
2733 case ID_ALIGN_RIGHT
: pf
.wAlignment
= PFA_RIGHT
; break;
2735 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2740 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 1, 0);
2744 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 0, RGB(255,255,192));
2747 case ID_TOGGLE_TOOLBAR
:
2748 set_toolbar_state(BANDID_TOOLBAR
, !is_bar_visible(BANDID_TOOLBAR
));
2752 case ID_TOGGLE_FORMATBAR
:
2753 set_toolbar_state(BANDID_FONTLIST
, !is_bar_visible(BANDID_FORMATBAR
));
2754 set_toolbar_state(BANDID_FORMATBAR
, !is_bar_visible(BANDID_FORMATBAR
));
2758 case ID_TOGGLE_STATUSBAR
:
2759 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR
));
2765 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2766 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_DATETIME
), hWnd
, (DLGPROC
)datetime_proc
);
2772 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2773 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_PARAFORMAT
), hWnd
,
2780 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2781 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_TABSTOPS
), hWnd
, tabstops_proc
);
2789 case ID_VIEWPROPERTIES
:
2790 dialog_viewproperties();
2794 SendMessageW(hwndEditor
, WM_COMMAND
, wParam
, lParam
);
2800 static LRESULT
OnInitPopupMenu( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2802 HMENU hMenu
= (HMENU
)wParam
;
2803 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2804 HWND hwndStatus
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
2806 int nAlignment
= -1;
2812 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&selFrom
, (LPARAM
)&selTo
);
2813 EnableMenuItem(hMenu
, ID_EDIT_COPY
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
2814 EnableMenuItem(hMenu
, ID_EDIT_CUT
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
2816 pf
.cbSize
= sizeof(PARAFORMAT
);
2817 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2818 CheckMenuItem(hMenu
, ID_EDIT_READONLY
,
2819 MF_BYCOMMAND
|(GetWindowLong(hwndEditor
, GWL_STYLE
)&ES_READONLY
? MF_CHECKED
: MF_UNCHECKED
));
2820 CheckMenuItem(hMenu
, ID_EDIT_MODIFIED
,
2821 MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0) ? MF_CHECKED
: MF_UNCHECKED
));
2822 if (pf
.dwMask
& PFM_ALIGNMENT
)
2823 nAlignment
= pf
.wAlignment
;
2824 CheckMenuItem(hMenu
, ID_ALIGN_LEFT
, MF_BYCOMMAND
|(nAlignment
== PFA_LEFT
) ?
2825 MF_CHECKED
: MF_UNCHECKED
);
2826 CheckMenuItem(hMenu
, ID_ALIGN_CENTER
, MF_BYCOMMAND
|(nAlignment
== PFA_CENTER
) ?
2827 MF_CHECKED
: MF_UNCHECKED
);
2828 CheckMenuItem(hMenu
, ID_ALIGN_RIGHT
, MF_BYCOMMAND
|(nAlignment
== PFA_RIGHT
) ?
2829 MF_CHECKED
: MF_UNCHECKED
);
2830 CheckMenuItem(hMenu
, ID_BULLET
, MF_BYCOMMAND
| ((pf
.wNumbering
== PFN_BULLET
) ?
2831 MF_CHECKED
: MF_UNCHECKED
));
2832 EnableMenuItem(hMenu
, ID_EDIT_UNDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0)) ?
2833 MF_ENABLED
: MF_GRAYED
);
2834 EnableMenuItem(hMenu
, ID_EDIT_REDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0)) ?
2835 MF_ENABLED
: MF_GRAYED
);
2837 CheckMenuItem(hMenu
, ID_TOGGLE_TOOLBAR
, MF_BYCOMMAND
|(is_bar_visible(BANDID_TOOLBAR
)) ?
2838 MF_CHECKED
: MF_UNCHECKED
);
2840 CheckMenuItem(hMenu
, ID_TOGGLE_FORMATBAR
, MF_BYCOMMAND
|(is_bar_visible(BANDID_FORMATBAR
)) ?
2841 MF_CHECKED
: MF_UNCHECKED
);
2843 CheckMenuItem(hMenu
, ID_TOGGLE_STATUSBAR
, MF_BYCOMMAND
|IsWindowVisible(hwndStatus
) ?
2844 MF_CHECKED
: MF_UNCHECKED
);
2846 gt
.flags
= GTL_NUMCHARS
;
2848 textLength
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
2849 EnableMenuItem(hMenu
, ID_FIND
, MF_BYCOMMAND
|(textLength
? MF_ENABLED
: MF_GRAYED
));
2851 mi
.cbSize
= sizeof(mi
);
2852 mi
.fMask
= MIIM_DATA
;
2854 GetMenuItemInfoW(hMenu
, ID_FIND_NEXT
, FALSE
, &mi
);
2856 EnableMenuItem(hMenu
, ID_FIND_NEXT
, MF_BYCOMMAND
|((textLength
&& mi
.dwItemData
) ?
2857 MF_ENABLED
: MF_GRAYED
));
2861 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2863 int nStatusSize
= 0;
2865 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2866 HWND hwndStatusBar
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
2867 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
2868 int rebarHeight
= 0;
2873 SendMessageW(hwndStatusBar
, WM_SIZE
, 0, 0);
2874 if (IsWindowVisible(hwndStatusBar
))
2876 GetClientRect(hwndStatusBar
, &rc
);
2877 nStatusSize
= rc
.bottom
- rc
.top
;
2885 if(!is_bar_visible(BANDID_TOOLBAR
))
2888 if(!is_bar_visible(BANDID_FORMATBAR
))
2891 rebarHeight
= rebarRows
? SendMessageW(hwndReBar
, RB_GETBARHEIGHT
, 0, 0) : 0;
2893 rc
.top
= rc
.left
= 0;
2894 rc
.bottom
= rebarHeight
;
2895 rc
.right
= LOWORD(lParam
);
2896 SendMessageW(hwndReBar
, RB_SIZETORECT
, 0, (LPARAM
)&rc
);
2900 GetClientRect(hWnd
, &rc
);
2901 MoveWindow(hwndEditor
, 0, rebarHeight
, rc
.right
, rc
.bottom
-nStatusSize
-rebarHeight
, TRUE
);
2904 return DefWindowProcW(hWnd
, WM_SIZE
, wParam
, lParam
);
2907 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2909 if(msg
== ID_FINDMSGSTRING
)
2910 return handle_findmsg((LPFINDREPLACEW
)lParam
);
2915 return OnCreate( hWnd
, wParam
, lParam
);
2918 return OnUser( hWnd
, wParam
, lParam
);
2921 return OnNotify( hWnd
, wParam
, lParam
);
2925 return preview_command( hWnd
, wParam
, lParam
);
2927 return OnCommand( hWnd
, wParam
, lParam
);
2937 } else if(prompt_save_changes())
2939 registry_set_options();
2940 registry_set_formatopts_all();
2947 SetFocus(GetDlgItem(hWnd
, IDC_EDITOR
));
2950 case WM_INITMENUPOPUP
:
2951 return OnInitPopupMenu( hWnd
, wParam
, lParam
);
2954 return OnSize( hWnd
, wParam
, lParam
);
2956 case WM_CONTEXTMENU
:
2957 if((HWND
)wParam
== hEditorWnd
)
2958 return context_menu(lParam
);
2960 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2964 WCHAR file
[MAX_PATH
];
2965 DragQueryFileW((HDROP
)wParam
, 0, file
, MAX_PATH
);
2966 DragFinish((HDROP
)wParam
);
2968 if(prompt_save_changes())
2974 return print_preview();
2976 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2979 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2985 int CALLBACK
WinMain(HINSTANCE hInstance
, HINSTANCE hOldInstance
, LPSTR szCmdParagraph
, int res
)
2987 INITCOMMONCONTROLSEX classes
= {8, ICC_BAR_CLASSES
|ICC_COOL_CLASSES
|ICC_USEREX_CLASSES
};
2992 static const WCHAR wszAccelTable
[] = {'M','A','I','N','A','C','C','E','L',
2993 'T','A','B','L','E','\0'};
2995 InitCommonControlsEx(&classes
);
2997 hAccel
= LoadAcceleratorsW(hInstance
, wszAccelTable
);
2999 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
3000 wc
.lpfnWndProc
= WndProc
;
3003 wc
.hInstance
= hInstance
;
3004 wc
.hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
3005 wc
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
3006 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
3007 wc
.lpszMenuName
= xszMainMenu
;
3008 wc
.lpszClassName
= wszMainWndClass
;
3009 RegisterClassW(&wc
);
3011 rc
= registry_read_winrect();
3012 hMainWnd
= CreateWindowExW(0, wszMainWndClass
, wszAppTitle
, WS_OVERLAPPEDWINDOW
,
3013 rc
.left
, rc
.top
, rc
.right
-rc
.left
, rc
.bottom
-rc
.top
, NULL
, NULL
, hInstance
, NULL
);
3014 ShowWindow(hMainWnd
, SW_SHOWDEFAULT
);
3018 hPopupMenu
= LoadMenuW(hInstance
, MAKEINTRESOURCEW(IDM_POPUP
));
3019 get_default_printer_opts();
3022 HandleCommandLine(GetCommandLineW());
3024 while(GetMessageW(&msg
,0,0,0))
3026 if (IsDialogMessage(hFindWnd
, &msg
))
3029 if (TranslateAcceleratorW(hMainWnd
, hAccel
, &msg
))
3031 TranslateMessage(&msg
);
3032 DispatchMessageW(&msg
);
3033 if (!PeekMessageW(&msg
, 0, 0, 0, PM_NOREMOVE
))
3034 SendMessageW(hMainWnd
, WM_USER
, 0, 0);