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 clear_formatting(void)
577 pf
.cbSize
= sizeof(pf
);
578 pf
.dwMask
= PFM_ALIGNMENT
;
579 pf
.wAlignment
= PFA_LEFT
;
580 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
583 static int reg_formatindex(WPARAM format
)
585 return (format
& SF_TEXT
) ? 1 : 0;
588 static int fileformat_number(WPARAM format
)
592 if(format
== SF_TEXT
)
595 } else if (format
== (SF_TEXT
| SF_UNICODE
))
602 static WPARAM
fileformat_flags(int format
)
604 WPARAM flags
[] = { SF_RTF
, SF_TEXT
, SF_TEXT
| SF_UNICODE
};
606 return flags
[format
];
609 static void set_default_font(void)
611 static const WCHAR richTextFont
[] = {'T','i','m','e','s',' ','N','e','w',' ',
612 'R','o','m','a','n',0};
613 static const WCHAR plainTextFont
[] = {'C','o','u','r','i','e','r',' ','N','e','w',0};
617 ZeroMemory(&fmt
, sizeof(fmt
));
619 fmt
.cbSize
= sizeof(fmt
);
620 fmt
.dwMask
= CFM_FACE
| CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINE
;
623 if(fileFormat
& SF_RTF
)
626 font
= plainTextFont
;
628 lstrcpyW(fmt
.szFaceName
, font
);
630 SendMessageW(hEditorWnd
, EM_SETCHARFORMAT
, SCF_DEFAULT
, (LPARAM
)&fmt
);
633 static void update_window(void)
637 GetWindowRect(hMainWnd
, &rect
);
639 (void) OnSize(hMainWnd
, SIZE_RESTORED
, MAKELONG(rect
.bottom
, rect
.right
));
642 static DWORD barState
[2];
643 static DWORD wordWrap
[2];
645 static BOOL
is_bar_visible(int bandId
)
647 return barState
[reg_formatindex(fileFormat
)] & (1 << bandId
);
650 static void store_bar_state(int bandId
, BOOL show
)
652 int formatIndex
= reg_formatindex(fileFormat
);
655 barState
[formatIndex
] |= (1 << bandId
);
657 barState
[formatIndex
] &= ~(1 << bandId
);
660 static void set_toolbar_state(int bandId
, BOOL show
)
662 HWND hwndReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
664 SendMessageW(hwndReBar
, RB_SHOWBAND
, SendMessageW(hwndReBar
, RB_IDTOINDEX
, bandId
, 0), show
);
666 if(bandId
== BANDID_TOOLBAR
)
668 REBARBANDINFOW rbbinfo
;
669 int index
= SendMessageW(hwndReBar
, RB_IDTOINDEX
, BANDID_FORMATBAR
, 0);
671 rbbinfo
.cbSize
= sizeof(rbbinfo
);
672 rbbinfo
.fMask
= RBBIM_STYLE
;
674 SendMessageW(hwndReBar
, RB_GETBANDINFO
, index
, (LPARAM
)&rbbinfo
);
677 rbbinfo
.fStyle
&= ~RBBS_BREAK
;
679 rbbinfo
.fStyle
|= RBBS_BREAK
;
681 SendMessageW(hwndReBar
, RB_SETBANDINFO
, index
, (LPARAM
)&rbbinfo
);
684 if(bandId
== BANDID_TOOLBAR
|| bandId
== BANDID_FORMATBAR
)
685 store_bar_state(bandId
, show
);
688 static void set_statusbar_state(BOOL show
)
690 HWND hStatusWnd
= GetDlgItem(hMainWnd
, IDC_STATUSBAR
);
692 ShowWindow(hStatusWnd
, show
? SW_SHOW
: SW_HIDE
);
693 store_bar_state(BANDID_STATUSBAR
, show
);
696 static void set_bar_states(void)
698 set_toolbar_state(BANDID_TOOLBAR
, is_bar_visible(BANDID_TOOLBAR
));
699 set_toolbar_state(BANDID_FORMATBAR
, is_bar_visible(BANDID_FORMATBAR
));
700 set_statusbar_state(is_bar_visible(BANDID_STATUSBAR
));
705 static HGLOBAL devMode
;
706 static HGLOBAL devNames
;
708 static HDC
make_dc(void)
710 if(devNames
&& devMode
)
712 LPDEVNAMES dn
= GlobalLock(devNames
);
713 LPDEVMODEW dm
= GlobalLock(devMode
);
716 ret
= CreateDCW((LPWSTR
)dn
+ dn
->wDriverOffset
,
717 (LPWSTR
)dn
+ dn
->wDeviceOffset
, 0, dm
);
729 static LONG
twips_to_pixels(int twips
, int dpi
)
731 float ret
= ((float)twips
/ ((float)567 * 2.54)) * (float)dpi
;
735 static LONG
devunits_to_twips(int units
, int dpi
)
737 float ret
= ((float)units
/ (float)dpi
) * (float)567 * 2.54;
741 static LONG
centmm_to_twips(int mm
)
743 return MulDiv(mm
, 567, 1000);
746 static LONG
twips_to_centmm(int twips
)
748 return MulDiv(twips
, 1000, 567);
751 static RECT
get_print_rect(HDC hdc
)
758 int dpiY
= GetDeviceCaps(hdc
, LOGPIXELSY
);
759 int dpiX
= GetDeviceCaps(hdc
, LOGPIXELSX
);
760 width
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALWIDTH
), dpiX
);
761 height
= devunits_to_twips(GetDeviceCaps(hdc
, PHYSICALHEIGHT
), dpiY
);
764 width
= centmm_to_twips(18500);
765 height
= centmm_to_twips(27000);
768 rc
.left
= margins
.left
;
769 rc
.right
= width
- margins
.right
;
770 rc
.top
= margins
.top
;
771 rc
.bottom
= height
- margins
.bottom
;
776 static void target_device(void)
780 int index
= reg_formatindex(fileFormat
);
782 if(wordWrap
[index
] == ID_WORDWRAP_MARGIN
)
784 RECT rc
= get_print_rect(hdc
);
790 HDC hMaindc
= GetDC(hMainWnd
);
791 hdc
= CreateCompatibleDC(hMaindc
);
792 ReleaseDC(hMainWnd
, hMaindc
);
795 SendMessageW(hEditorWnd
, EM_SETTARGETDEVICE
, (WPARAM
)hdc
, width
);
800 static void set_fileformat(WPARAM format
)
809 static void DoOpenFile(LPCWSTR szOpenFileName
)
815 WPARAM format
= SF_TEXT
;
817 hFile
= CreateFileW(szOpenFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
818 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
819 if (hFile
== INVALID_HANDLE_VALUE
)
822 ReadFile(hFile
, fileStart
, 5, &readOut
, NULL
);
823 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
825 if(readOut
>= 2 && (BYTE
)fileStart
[0] == 0xff && (BYTE
)fileStart
[1] == 0xfe)
827 format
= SF_TEXT
| SF_UNICODE
;
828 SetFilePointer(hFile
, 2, NULL
, FILE_BEGIN
);
829 } else if(readOut
>= 5)
831 static const char header
[] = "{\\rtf";
832 if(!memcmp(header
, fileStart
, 5))
836 es
.dwCookie
= (DWORD_PTR
)hFile
;
837 es
.pfnCallback
= stream_in
;
840 set_fileformat(format
);
841 SendMessageW(hEditorWnd
, EM_STREAMIN
, format
, (LPARAM
)&es
);
845 SetFocus(hEditorWnd
);
847 set_caption(szOpenFileName
);
849 lstrcpyW(wszFileName
, szOpenFileName
);
850 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
851 registry_set_filelist(szOpenFileName
);
854 static void DoSaveFile(LPCWSTR wszSaveFileName
, WPARAM format
)
860 hFile
= CreateFileW(wszSaveFileName
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
861 FILE_ATTRIBUTE_NORMAL
, NULL
);
863 if(hFile
== INVALID_HANDLE_VALUE
)
866 if(format
== (SF_TEXT
| SF_UNICODE
))
868 static const BYTE unicode
[] = {0xff,0xfe};
870 WriteFile(hFile
, &unicode
, sizeof(unicode
), &writeOut
, 0);
872 if(writeOut
!= sizeof(unicode
))
876 stream
.dwCookie
= (DWORD_PTR
)hFile
;
877 stream
.pfnCallback
= stream_out
;
879 ret
= SendMessageW(hEditorWnd
, EM_STREAMOUT
, format
, (LPARAM
)&stream
);
883 SetFocus(hEditorWnd
);
888 gt
.flags
= GTL_DEFAULT
;
891 if(SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0))
895 lstrcpyW(wszFileName
, wszSaveFileName
);
896 set_caption(wszFileName
);
897 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
898 set_fileformat(format
);
901 static void DialogSaveFile(void)
905 WCHAR wszFile
[MAX_PATH
] = {'\0'};
906 static const WCHAR wszDefExt
[] = {'r','t','f','\0'};
908 ZeroMemory(&sfn
, sizeof(sfn
));
910 sfn
.lStructSize
= sizeof(sfn
);
911 sfn
.Flags
= OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
| OFN_OVERWRITEPROMPT
;
912 sfn
.hwndOwner
= hMainWnd
;
913 sfn
.lpstrFilter
= wszFilter
;
914 sfn
.lpstrFile
= wszFile
;
915 sfn
.nMaxFile
= MAX_PATH
;
916 sfn
.lpstrDefExt
= wszDefExt
;
917 sfn
.nFilterIndex
= fileformat_number(fileFormat
)+1;
919 while(GetSaveFileNameW(&sfn
))
921 if(fileformat_flags(sfn
.nFilterIndex
-1) != SF_RTF
)
923 if(MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING
),
924 wszAppTitle
, MB_YESNO
| MB_ICONEXCLAMATION
) != IDYES
)
929 DoSaveFile(sfn
.lpstrFile
, fileformat_flags(sfn
.nFilterIndex
-1));
934 DoSaveFile(sfn
.lpstrFile
, fileformat_flags(sfn
.nFilterIndex
-1));
940 static BOOL
prompt_save_changes(void)
945 gt
.flags
= GTL_NUMCHARS
;
947 if(!SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0))
951 if(!SendMessageW(hEditorWnd
, EM_GETMODIFY
, 0, 0))
956 LPWSTR displayFileName
;
961 displayFileName
= wszDefaultFileName
;
963 displayFileName
= file_basename(wszFileName
);
965 text
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
966 (lstrlenW(displayFileName
)+lstrlenW(wszSaveChanges
))*sizeof(WCHAR
));
971 wsprintfW(text
, wszSaveChanges
, displayFileName
);
973 ret
= MessageBoxW(hMainWnd
, text
, wszAppTitle
, MB_YESNOCANCEL
| MB_ICONEXCLAMATION
);
975 HeapFree(GetProcessHeap(), 0, text
);
984 DoSaveFile(wszFileName
, fileFormat
);
995 static void DialogOpenFile(void)
999 WCHAR wszFile
[MAX_PATH
] = {'\0'};
1000 static const WCHAR wszDefExt
[] = {'r','t','f','\0'};
1002 ZeroMemory(&ofn
, sizeof(ofn
));
1004 ofn
.lStructSize
= sizeof(ofn
);
1005 ofn
.Flags
= OFN_HIDEREADONLY
| OFN_FILEMUSTEXIST
| OFN_PATHMUSTEXIST
;
1006 ofn
.hwndOwner
= hMainWnd
;
1007 ofn
.lpstrFilter
= wszFilter
;
1008 ofn
.lpstrFile
= wszFile
;
1009 ofn
.nMaxFile
= MAX_PATH
;
1010 ofn
.lpstrDefExt
= wszDefExt
;
1011 ofn
.nFilterIndex
= fileformat_number(fileFormat
)+1;
1013 if(GetOpenFileNameW(&ofn
))
1015 if(prompt_save_changes())
1016 DoOpenFile(ofn
.lpstrFile
);
1020 static LPWSTR
dialog_print_to_file(void)
1023 static WCHAR file
[MAX_PATH
] = {'O','U','T','P','U','T','.','P','R','N',0};
1024 static const WCHAR defExt
[] = {'P','R','N',0};
1026 ZeroMemory(&ofn
, sizeof(ofn
));
1028 ofn
.lStructSize
= sizeof(ofn
);
1029 ofn
.Flags
= OFN_PATHMUSTEXIST
| OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT
;
1030 ofn
.hwndOwner
= hMainWnd
;
1031 ofn
.lpstrFilter
= (LPWSTR
)wszPrintFilter
;
1032 ofn
.lpstrFile
= (LPWSTR
)file
;
1033 ofn
.nMaxFile
= MAX_PATH
;
1034 ofn
.lpstrDefExt
= (LPWSTR
)defExt
;
1036 if(GetSaveFileNameW(&ofn
))
1037 return (LPWSTR
)file
;
1042 static int get_num_pages(FORMATRANGE fr
)
1049 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
,
1051 } while(fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
1056 static void char_from_pagenum(FORMATRANGE
*fr
, int page
)
1060 for(i
= 1; i
<= page
; i
++)
1065 fr
->chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)fr
);
1069 static void print(LPPRINTDLGW pd
)
1073 int printedPages
= 0;
1076 fr
.hdcTarget
= pd
->hDC
;
1078 fr
.rc
= get_print_rect(fr
.hdc
);
1080 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
1082 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
1084 ZeroMemory(&di
, sizeof(di
));
1085 di
.cbSize
= sizeof(di
);
1086 di
.lpszDocName
= (LPWSTR
)wszFileName
;
1088 if(pd
->Flags
& PD_PRINTTOFILE
)
1090 di
.lpszOutput
= dialog_print_to_file();
1095 if(pd
->Flags
& PD_SELECTION
)
1097 SendMessageW(hEditorWnd
, EM_EXGETSEL
, 0, (LPARAM
)&fr
.chrg
);
1101 gt
.flags
= GTL_DEFAULT
;
1104 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1106 if(pd
->Flags
& PD_PAGENUMS
)
1107 char_from_pagenum(&fr
, pd
->nToPage
);
1110 StartDocW(fr
.hdc
, &di
);
1113 if(StartPage(fr
.hdc
) <= 0)
1116 fr
.chrg
.cpMin
= SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
1118 if(EndPage(fr
.hdc
) <= 0)
1122 if((pd
->Flags
& PD_PAGENUMS
) && (printedPages
> (pd
->nToPage
- pd
->nFromPage
)))
1125 while(fr
.chrg
.cpMin
< fr
.chrg
.cpMax
);
1128 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
1132 static void dialog_printsetup(void)
1136 ZeroMemory(&ps
, sizeof(ps
));
1137 ps
.lStructSize
= sizeof(ps
);
1138 ps
.hwndOwner
= hMainWnd
;
1139 ps
.Flags
= PSD_INHUNDREDTHSOFMILLIMETERS
| PSD_MARGINS
;
1140 ps
.rtMargin
.left
= twips_to_centmm(margins
.left
);
1141 ps
.rtMargin
.right
= twips_to_centmm(margins
.right
);
1142 ps
.rtMargin
.top
= twips_to_centmm(margins
.top
);
1143 ps
.rtMargin
.bottom
= twips_to_centmm(margins
.bottom
);
1144 ps
.hDevMode
= devMode
;
1145 ps
.hDevNames
= devNames
;
1147 if(PageSetupDlgW(&ps
))
1149 margins
.left
= centmm_to_twips(ps
.rtMargin
.left
);
1150 margins
.right
= centmm_to_twips(ps
.rtMargin
.right
);
1151 margins
.top
= centmm_to_twips(ps
.rtMargin
.top
);
1152 margins
.bottom
= centmm_to_twips(ps
.rtMargin
.bottom
);
1153 devMode
= ps
.hDevMode
;
1154 devNames
= ps
.hDevNames
;
1159 static void get_default_printer_opts(void)
1162 ZeroMemory(&pd
, sizeof(pd
));
1164 ZeroMemory(&pd
, sizeof(pd
));
1165 pd
.lStructSize
= sizeof(pd
);
1166 pd
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
1167 pd
.hwndOwner
= hMainWnd
;
1168 pd
.hDevMode
= devMode
;
1172 devMode
= pd
.hDevMode
;
1173 devNames
= pd
.hDevNames
;
1176 static void print_quick(void)
1180 ZeroMemory(&pd
, sizeof(pd
));
1186 static void dialog_print(void)
1192 ZeroMemory(&pd
, sizeof(pd
));
1193 pd
.lStructSize
= sizeof(pd
);
1194 pd
.hwndOwner
= hMainWnd
;
1195 pd
.Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
;
1198 pd
.hDevMode
= devMode
;
1199 pd
.hDevNames
= devNames
;
1201 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
1203 pd
.Flags
|= PD_NOSELECTION
;
1207 devMode
= pd
.hDevMode
;
1208 devNames
= pd
.hDevNames
;
1213 typedef struct _previewinfo
1220 } previewinfo
, *ppreviewinfo
;
1222 static previewinfo preview
;
1224 static void preview_bar_show(BOOL show
)
1226 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1231 AddTextButton(hReBar
, STRING_PREVIEW_PRINT
, ID_PRINT
, BANDID_PREVIEW_BTN1
);
1232 AddTextButton(hReBar
, STRING_PREVIEW_NEXTPAGE
, ID_PREVIEW_NEXTPAGE
, BANDID_PREVIEW_BTN2
);
1233 AddTextButton(hReBar
, STRING_PREVIEW_PREVPAGE
, ID_PREVIEW_PREVPAGE
, BANDID_PREVIEW_BTN3
);
1234 AddTextButton(hReBar
, STRING_PREVIEW_CLOSE
, ID_FILE_EXIT
, BANDID_PREVIEW_BTN4
);
1237 for(i
= 0; i
< PREVIEW_BUTTONS
; i
++)
1238 SendMessageW(hReBar
, RB_DELETEBAND
, SendMessageW(hReBar
, RB_IDTOINDEX
, BANDID_PREVIEW_BTN1
+i
, 0), 0);
1242 static void preview_exit(void)
1244 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1245 HMENU hMenu
= LoadMenuW(hInstance
, xszMainMenu
);
1248 preview
.window
.right
= 0;
1249 preview
.window
.bottom
= 0;
1252 ShowWindow(hEditorWnd
, TRUE
);
1254 preview_bar_show(FALSE
);
1256 SetMenu(hMainWnd
, hMenu
);
1257 registry_read_filelist(hMainWnd
);
1262 static LRESULT
print_preview(void)
1267 RECT window
, background
;
1268 HBITMAP hBitmapCapture
, hBitmapScaled
;
1269 int bmWidth
, bmHeight
, bmNewWidth
, bmNewHeight
;
1270 float ratioWidth
, ratioHeight
, ratio
;
1271 int xOffset
, yOffset
;
1273 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1276 hdc
= BeginPaint(hMainWnd
, &ps
);
1277 GetClientRect(hMainWnd
, &window
);
1279 fr
.hdcTarget
= make_dc();
1280 fr
.rc
= get_print_rect(fr
.hdcTarget
);
1283 fr
.rcPage
.bottom
= fr
.rc
.bottom
+ margins
.bottom
;
1284 fr
.rcPage
.right
= fr
.rc
.right
+ margins
.right
;
1286 bmWidth
= twips_to_pixels(fr
.rcPage
.right
, GetDeviceCaps(hdc
, LOGPIXELSX
));
1287 bmHeight
= twips_to_pixels(fr
.rcPage
.bottom
, GetDeviceCaps(hdc
, LOGPIXELSY
));
1289 hBitmapCapture
= CreateCompatibleBitmap(hdc
, bmWidth
, bmHeight
);
1295 preview
.hdc
= CreateCompatibleDC(hdc
);
1296 fr
.hdc
= preview
.hdc
;
1297 gt
.flags
= GTL_DEFAULT
;
1300 fr
.chrg
.cpMax
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1303 paper
.right
= bmWidth
;
1305 paper
.bottom
= bmHeight
;
1308 preview
.pages
= get_num_pages(fr
);
1310 SelectObject(preview
.hdc
, hBitmapCapture
);
1312 char_from_pagenum(&fr
, preview
.page
);
1314 FillRect(preview
.hdc
, &paper
, GetStockObject(WHITE_BRUSH
));
1315 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, TRUE
, (LPARAM
)&fr
);
1316 SendMessageW(hEditorWnd
, EM_FORMATRANGE
, FALSE
, 0);
1318 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_PREVPAGE
), preview
.page
> 1);
1319 EnableWindow(GetDlgItem(hReBar
, ID_PREVIEW_NEXTPAGE
), preview
.page
< preview
.pages
);
1322 barheight
= SendMessageW(hReBar
, RB_GETBARHEIGHT
, 0, 0);
1323 ratioWidth
= ((float)window
.right
- 20.0) / (float)bmHeight
;
1324 ratioHeight
= ((float)window
.bottom
- 20.0 - (float)barheight
) / (float)bmHeight
;
1326 if(ratioWidth
> ratioHeight
)
1327 ratio
= ratioHeight
;
1331 bmNewWidth
= (int)((float)bmWidth
* ratio
);
1332 bmNewHeight
= (int)((float)bmHeight
* ratio
);
1333 hBitmapScaled
= CreateCompatibleBitmap(hdc
, bmNewWidth
, bmNewHeight
);
1335 xOffset
= ((window
.right
- bmNewWidth
) / 2);
1336 yOffset
= ((window
.bottom
- bmNewHeight
+ barheight
) / 2);
1338 if(window
.right
!= preview
.window
.right
|| window
.bottom
!= preview
.window
.bottom
)
1340 DeleteDC(preview
.hdcSized
),
1341 preview
.hdcSized
= CreateCompatibleDC(hdc
);
1342 SelectObject(preview
.hdcSized
, hBitmapScaled
);
1344 StretchBlt(preview
.hdcSized
, 0, 0, bmNewWidth
, bmNewHeight
, preview
.hdc
, 0, 0, bmWidth
, bmHeight
, SRCCOPY
);
1347 window
.top
= barheight
;
1348 FillRect(hdc
, &window
, GetStockObject(GRAY_BRUSH
));
1350 SelectObject(hdc
, hBitmapScaled
);
1352 background
.left
= xOffset
- 2;
1353 background
.right
= xOffset
+ bmNewWidth
+ 2;
1354 background
.top
= yOffset
- 2;
1355 background
.bottom
= yOffset
+ bmNewHeight
+ 2;
1357 FillRect(hdc
, &background
, GetStockObject(BLACK_BRUSH
));
1359 BitBlt(hdc
, xOffset
, yOffset
, bmNewWidth
, bmNewHeight
, preview
.hdcSized
, 0, 0, SRCCOPY
);
1361 DeleteDC(fr
.hdcTarget
);
1362 preview
.window
= window
;
1364 EndPaint(hMainWnd
, &ps
);
1369 static LRESULT
preview_command(HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
1371 switch(LOWORD(wParam
))
1374 PostMessageW(hMainWnd
, WM_CLOSE
, 0, 0);
1377 case ID_PREVIEW_NEXTPAGE
:
1378 case ID_PREVIEW_PREVPAGE
:
1380 HWND hReBar
= GetDlgItem(hMainWnd
, IDC_REBAR
);
1383 if(LOWORD(wParam
) == ID_PREVIEW_NEXTPAGE
)
1389 preview
.window
.right
= 0;
1391 GetClientRect(hMainWnd
, &rc
);
1392 rc
.top
+= SendMessageW(hReBar
, RB_GETBARHEIGHT
, 0, 0);
1393 InvalidateRect(hMainWnd
, &rc
, TRUE
);
1407 static void dialog_about(void)
1409 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1410 HICON icon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
1411 ShellAboutW(hMainWnd
, wszAppTitle
, 0, icon
);
1414 static INT_PTR CALLBACK
formatopts_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1420 LPPROPSHEETPAGEW ps
= (LPPROPSHEETPAGEW
)lParam
;
1423 HWND hIdWnd
= GetDlgItem(hWnd
, IDC_PAGEFMT_ID
);
1425 sprintf(id
, "%d\n", (int)ps
->lParam
);
1426 SetWindowTextA(hIdWnd
, id
);
1427 if(wordWrap
[ps
->lParam
] == ID_WORDWRAP_WINDOW
)
1428 wrap
= IDC_PAGEFMT_WW
;
1429 else if(wordWrap
[ps
->lParam
] == ID_WORDWRAP_MARGIN
)
1430 wrap
= IDC_PAGEFMT_WM
;
1433 CheckRadioButton(hWnd
, IDC_PAGEFMT_WW
,
1434 IDC_PAGEFMT_WM
, wrap
);
1436 if(barState
[ps
->lParam
] & (1 << BANDID_TOOLBAR
))
1437 CheckDlgButton(hWnd
, IDC_PAGEFMT_TB
, TRUE
);
1438 if(barState
[ps
->lParam
] & (1 << BANDID_FORMATBAR
))
1439 CheckDlgButton(hWnd
, IDC_PAGEFMT_FB
, TRUE
);
1440 if(barState
[ps
->lParam
] & (BANDID_STATUSBAR
))
1441 CheckDlgButton(hWnd
, IDC_PAGEFMT_SB
, TRUE
);
1446 switch(LOWORD(wParam
))
1448 case IDC_PAGEFMT_WW
:
1449 case IDC_PAGEFMT_WM
:
1450 CheckRadioButton(hWnd
, IDC_PAGEFMT_WW
, IDC_PAGEFMT_WM
,
1454 case IDC_PAGEFMT_TB
:
1455 case IDC_PAGEFMT_FB
:
1456 case IDC_PAGEFMT_SB
:
1457 CheckDlgButton(hWnd
, LOWORD(wParam
),
1458 !IsDlgButtonChecked(hWnd
, LOWORD(wParam
)));
1464 LPNMHDR header
= (LPNMHDR
)lParam
;
1465 if(header
->code
== PSN_APPLY
)
1467 HWND hIdWnd
= GetDlgItem(hWnd
, IDC_PAGEFMT_ID
);
1471 GetWindowTextA(hIdWnd
, sid
, 4);
1473 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_WW
))
1474 wordWrap
[id
] = ID_WORDWRAP_WINDOW
;
1475 else if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_WM
))
1476 wordWrap
[id
] = ID_WORDWRAP_MARGIN
;
1478 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_TB
))
1479 barState
[id
] |= (1 << BANDID_TOOLBAR
);
1481 barState
[id
] &= ~(1 << BANDID_TOOLBAR
);
1483 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_FB
))
1484 barState
[id
] |= (1 << BANDID_FORMATBAR
);
1486 barState
[id
] &= ~(1 << BANDID_FORMATBAR
);
1488 if(IsDlgButtonChecked(hWnd
, IDC_PAGEFMT_SB
))
1489 barState
[id
] |= (1 << BANDID_STATUSBAR
);
1491 barState
[id
] &= ~(1 << BANDID_STATUSBAR
);
1499 static void dialog_viewproperties(void)
1501 PROPSHEETPAGEW psp
[2];
1502 PROPSHEETHEADERW psh
;
1504 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1505 LPCPROPSHEETPAGEW ppsp
= (LPCPROPSHEETPAGEW
)&psp
;
1507 psp
[0].dwSize
= sizeof(PROPSHEETPAGEW
);
1508 psp
[0].dwFlags
= PSP_USETITLE
;
1509 psp
[0].pszTemplate
= MAKEINTRESOURCEW(IDD_FORMATOPTS
);
1510 psp
[0].pfnDlgProc
= formatopts_proc
;
1511 psp
[0].hInstance
= hInstance
;
1512 psp
[0].lParam
= reg_formatindex(SF_TEXT
);
1513 psp
[0].pfnCallback
= NULL
;
1514 psp
[0].pszTitle
= MAKEINTRESOURCEW(STRING_VIEWPROPS_TEXT
);
1515 for(i
= 1; i
< sizeof(psp
)/sizeof(psp
[0]); i
++)
1517 psp
[i
].dwSize
= psp
[0].dwSize
;
1518 psp
[i
].dwFlags
= psp
[0].dwFlags
;
1519 psp
[i
].pszTemplate
= psp
[0].pszTemplate
;
1520 psp
[i
].pfnDlgProc
= psp
[0].pfnDlgProc
;
1521 psp
[i
].hInstance
= psp
[0].hInstance
;
1522 psp
[i
].lParam
= reg_formatindex(SF_RTF
);
1523 psp
[i
].pfnCallback
= psp
[0].pfnCallback
;
1524 psp
[i
].pszTitle
= MAKEINTRESOURCEW(STRING_VIEWPROPS_RICHTEXT
);
1527 psh
.dwSize
= sizeof(psh
);
1528 psh
.dwFlags
= PSH_USEICONID
| PSH_PROPSHEETPAGE
| PSH_NOAPPLYNOW
;
1529 psh
.hwndParent
= hMainWnd
;
1530 psh
.hInstance
= hInstance
;
1531 psh
.pszCaption
= MAKEINTRESOURCEW(STRING_VIEWPROPS_TITLE
);
1532 psh
.nPages
= sizeof(psp
)/sizeof(psp
[0]);
1534 psh
.pszIcon
= MAKEINTRESOURCEW(IDI_WORDPAD
);
1536 if(fileFormat
& SF_RTF
)
1540 PropertySheetW(&psh
);
1545 static void HandleCommandLine(LPWSTR cmdline
)
1550 /* skip white space */
1551 while (*cmdline
== ' ') cmdline
++;
1553 /* skip executable name */
1554 delimiter
= (*cmdline
== '"' ? '"' : ' ');
1556 if (*cmdline
== delimiter
) cmdline
++;
1557 while (*cmdline
&& *cmdline
!= delimiter
) cmdline
++;
1558 if (*cmdline
== delimiter
) cmdline
++;
1560 while (*cmdline
== ' ' || *cmdline
== '-' || *cmdline
== '/')
1564 if (*cmdline
++ == ' ') continue;
1567 if (option
) cmdline
++;
1568 while (*cmdline
== ' ') cmdline
++;
1581 /* file name is passed on the command line */
1582 if (cmdline
[0] == '"')
1585 cmdline
[lstrlenW(cmdline
) - 1] = 0;
1587 DoOpenFile(cmdline
);
1588 InvalidateRect(hMainWnd
, NULL
, FALSE
);
1592 MessageBox(hMainWnd
, "Printing not implemented", "WordPad", MB_OK
);
1595 static LRESULT
handle_findmsg(LPFINDREPLACEW pFr
)
1597 if(pFr
->Flags
& FR_DIALOGTERM
)
1600 pFr
->Flags
= FR_FINDNEXT
;
1602 } else if(pFr
->Flags
& FR_FINDNEXT
)
1604 DWORD flags
= FR_DOWN
;
1606 static CHARRANGE cr
;
1611 HMENU hMenu
= GetMenu(hMainWnd
);
1614 mi
.cbSize
= sizeof(mi
);
1615 mi
.fMask
= MIIM_DATA
;
1617 SetMenuItemInfoW(hMenu
, ID_FIND_NEXT
, FALSE
, &mi
);
1619 gt
.flags
= GTL_NUMCHARS
;
1622 length
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
1624 if(pFr
->lCustData
== -1)
1626 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&startPos
, (LPARAM
)&end
);
1627 cr
.cpMin
= startPos
;
1628 pFr
->lCustData
= startPos
;
1630 if(cr
.cpMin
== length
)
1634 startPos
= pFr
->lCustData
;
1637 if(cr
.cpMax
> length
)
1645 ft
.lpstrText
= pFr
->lpstrFindWhat
;
1647 if(pFr
->Flags
& FR_MATCHCASE
)
1648 flags
|= FR_MATCHCASE
;
1649 if(pFr
->Flags
& FR_WHOLEWORD
)
1650 flags
|= FR_WHOLEWORD
;
1652 ret
= SendMessageW(hEditorWnd
, EM_FINDTEXTW
, (WPARAM
)flags
, (LPARAM
)&ft
);
1656 if(cr
.cpMax
== length
&& cr
.cpMax
!= startPos
)
1658 ft
.chrg
.cpMin
= cr
.cpMin
= 0;
1659 ft
.chrg
.cpMax
= cr
.cpMax
= startPos
;
1661 ret
= SendMessageW(hEditorWnd
, EM_FINDTEXTW
, (WPARAM
)flags
, (LPARAM
)&ft
);
1667 pFr
->lCustData
= -1;
1668 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_SEARCH_FINISHED
), wszAppTitle
,
1669 MB_OK
| MB_ICONASTERISK
);
1672 end
= ret
+ lstrlenW(pFr
->lpstrFindWhat
);
1674 SendMessageW(hEditorWnd
, EM_SETSEL
, (WPARAM
)ret
, (LPARAM
)end
);
1675 SendMessageW(hEditorWnd
, EM_SCROLLCARET
, 0, 0);
1682 static void dialog_find(LPFINDREPLACEW fr
)
1684 static WCHAR findBuffer
[MAX_STRING_LEN
];
1686 ZeroMemory(fr
, sizeof(FINDREPLACEW
));
1687 fr
->lStructSize
= sizeof(FINDREPLACEW
);
1688 fr
->hwndOwner
= hMainWnd
;
1689 fr
->Flags
= FR_HIDEUPDOWN
;
1690 fr
->lpstrFindWhat
= findBuffer
;
1692 fr
->wFindWhatLen
= MAX_STRING_LEN
*sizeof(WCHAR
);
1694 hFindWnd
= FindTextW(fr
);
1697 static void registry_read_options(void)
1700 DWORD size
= sizeof(RECT
);
1702 if(registry_get_handle(&hKey
, 0, key_options
) != ERROR_SUCCESS
||
1703 RegQueryValueExW(hKey
, var_pagemargin
, 0, NULL
, (LPBYTE
)&margins
,
1704 &size
) != ERROR_SUCCESS
|| size
!= sizeof(RECT
))
1707 margins
.bottom
= 1417;
1708 margins
.left
= 1757;
1709 margins
.right
= 1757;
1715 static void registry_read_formatopts(int index
, LPCWSTR key
)
1719 BOOL fetched
= FALSE
;
1720 barState
[index
] = 0;
1721 wordWrap
[index
] = 0;
1723 if(registry_get_handle(&hKey
, &action
, key
) != ERROR_SUCCESS
)
1726 if(action
== REG_OPENED_EXISTING_KEY
)
1728 DWORD size
= sizeof(DWORD
);
1730 if(RegQueryValueExW(hKey
, var_barstate0
, 0, NULL
, (LPBYTE
)&barState
[index
],
1731 &size
) == ERROR_SUCCESS
)
1736 barState
[index
] = (1 << BANDID_TOOLBAR
) | (1 << BANDID_FORMATBAR
) | (1 << BANDID_RULER
) | (1 << BANDID_STATUSBAR
);
1738 if(index
== reg_formatindex(SF_RTF
))
1739 wordWrap
[index
] = ID_WORDWRAP_WINDOW
;
1740 else if(index
== reg_formatindex(SF_TEXT
))
1741 wordWrap
[index
] = ID_WORDWRAP_WINDOW
; /* FIXME: should be ID_WORDWRAP_NONE once we support it */
1746 static void registry_read_formatopts_all(void)
1748 registry_read_formatopts(reg_formatindex(SF_RTF
), key_rtf
);
1749 registry_read_formatopts(reg_formatindex(SF_TEXT
), key_text
);
1752 static void registry_set_formatopts(int index
, LPCWSTR key
)
1757 if(registry_get_handle(&hKey
, &action
, key
) == ERROR_SUCCESS
)
1759 RegSetValueExW(hKey
, var_barstate0
, 0, REG_DWORD
, (LPBYTE
)&barState
[index
],
1766 static void registry_set_formatopts_all(void)
1768 registry_set_formatopts(reg_formatindex(SF_RTF
), key_rtf
);
1769 registry_set_formatopts(reg_formatindex(SF_TEXT
), key_text
);
1772 static int current_units_to_twips(float number
)
1774 int twips
= (int)(number
* 567);
1778 static void append_current_units(LPWSTR buffer
)
1780 static const WCHAR space
[] = {' '};
1781 lstrcatW(buffer
, space
);
1782 lstrcatW(buffer
, units_cmW
);
1785 static void number_with_units(LPWSTR buffer
, int number
)
1787 float converted
= (float)number
/ 567;
1788 char string
[MAX_STRING_LEN
];
1790 sprintf(string
, "%.2f ", converted
);
1791 lstrcatA(string
, units_cmA
);
1792 MultiByteToWideChar(CP_ACP
, 0, string
, -1, buffer
, MAX_STRING_LEN
);
1795 BOOL CALLBACK
datetime_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1801 WCHAR buffer
[MAX_STRING_LEN
];
1803 HWND hListWnd
= GetDlgItem(hWnd
, IDC_DATETIME
);
1806 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_SHORTDATE
, &st
, 0, (LPWSTR
)&buffer
,
1808 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1809 GetDateFormatW(LOCALE_USER_DEFAULT
, DATE_LONGDATE
, &st
, 0, (LPWSTR
)&buffer
,
1811 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1812 GetTimeFormatW(LOCALE_USER_DEFAULT
, 0, &st
, 0, (LPWSTR
)&buffer
, MAX_STRING_LEN
);
1813 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1815 SendMessageW(hListWnd
, LB_SETSEL
, TRUE
, 0);
1820 switch(LOWORD(wParam
))
1825 HWND hListWnd
= GetDlgItem(hWnd
, IDC_DATETIME
);
1827 index
= SendMessageW(hListWnd
, LB_GETCURSEL
, 0, 0);
1831 WCHAR buffer
[MAX_STRING_LEN
];
1832 SendMessageW(hListWnd
, LB_GETTEXT
, index
, (LPARAM
)&buffer
);
1833 SendMessageW(hEditorWnd
, EM_REPLACESEL
, TRUE
, (LPARAM
)&buffer
);
1839 EndDialog(hWnd
, wParam
);
1846 BOOL CALLBACK
newfile_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1852 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
, GWLP_HINSTANCE
);
1853 WCHAR buffer
[MAX_STRING_LEN
];
1854 HWND hListWnd
= GetDlgItem(hWnd
, IDC_NEWFILE
);
1856 LoadStringW(hInstance
, STRING_NEWFILE_RICHTEXT
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1857 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1858 LoadStringW(hInstance
, STRING_NEWFILE_TXT
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1859 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1860 LoadStringW(hInstance
, STRING_NEWFILE_TXT_UNICODE
, (LPWSTR
)buffer
, MAX_STRING_LEN
);
1861 SendMessageW(hListWnd
, LB_ADDSTRING
, 0, (LPARAM
)&buffer
);
1863 SendMessageW(hListWnd
, LB_SETSEL
, TRUE
, 0);
1868 switch(LOWORD(wParam
))
1873 HWND hListWnd
= GetDlgItem(hWnd
, IDC_NEWFILE
);
1874 index
= SendMessageW(hListWnd
, LB_GETCURSEL
, 0, 0);
1877 EndDialog(hWnd
, MAKELONG(fileformat_flags(index
),0));
1882 EndDialog(hWnd
, MAKELONG(ID_NEWFILE_ABORT
,0));
1889 static INT_PTR CALLBACK
paraformat_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1895 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hMainWnd
,
1897 WCHAR buffer
[MAX_STRING_LEN
];
1898 HWND hListWnd
= GetDlgItem(hWnd
, IDC_PARA_ALIGN
);
1899 HWND hLeftWnd
= GetDlgItem(hWnd
, IDC_PARA_LEFT
);
1900 HWND hRightWnd
= GetDlgItem(hWnd
, IDC_PARA_RIGHT
);
1901 HWND hFirstWnd
= GetDlgItem(hWnd
, IDC_PARA_FIRST
);
1905 LoadStringW(hInstance
, STRING_ALIGN_LEFT
, buffer
,
1907 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1908 LoadStringW(hInstance
, STRING_ALIGN_RIGHT
, buffer
,
1910 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1911 LoadStringW(hInstance
, STRING_ALIGN_CENTER
, buffer
,
1913 SendMessageW(hListWnd
, CB_ADDSTRING
, 0, (LPARAM
)buffer
);
1915 pf
.cbSize
= sizeof(pf
);
1916 pf
.dwMask
= PFM_ALIGNMENT
| PFM_OFFSET
| PFM_RIGHTINDENT
|
1918 SendMessageW(hEditorWnd
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
1920 if(pf
.wAlignment
== PFA_RIGHT
)
1922 else if(pf
.wAlignment
== PFA_CENTER
)
1925 SendMessageW(hListWnd
, CB_SETCURSEL
, index
, 0);
1927 number_with_units(buffer
, pf
.dxOffset
);
1928 SetWindowTextW(hLeftWnd
, buffer
);
1929 number_with_units(buffer
, pf
.dxRightIndent
);
1930 SetWindowTextW(hRightWnd
, buffer
);
1931 number_with_units(buffer
, pf
.dxStartIndent
- pf
.dxOffset
);
1932 SetWindowTextW(hFirstWnd
, buffer
);
1937 switch(LOWORD(wParam
))
1941 HWND hLeftWnd
= GetDlgItem(hWnd
, IDC_PARA_LEFT
);
1942 HWND hRightWnd
= GetDlgItem(hWnd
, IDC_PARA_RIGHT
);
1943 HWND hFirstWnd
= GetDlgItem(hWnd
, IDC_PARA_FIRST
);
1944 WCHAR buffer
[MAX_STRING_LEN
];
1949 GetWindowTextW(hLeftWnd
, buffer
, MAX_STRING_LEN
);
1950 if(number_from_string(buffer
, &num
, TRUE
))
1952 pf
.dxOffset
= current_units_to_twips(num
);
1953 GetWindowTextW(hRightWnd
, buffer
, MAX_STRING_LEN
);
1954 if(number_from_string(buffer
, &num
, TRUE
))
1956 pf
.dxRightIndent
= current_units_to_twips(num
);
1957 GetWindowTextW(hFirstWnd
, buffer
, MAX_STRING_LEN
);
1958 if(number_from_string(buffer
, &num
, TRUE
))
1960 pf
.dxStartIndent
= current_units_to_twips(num
);
1964 MessageBoxW(hMainWnd
, MAKEINTRESOURCEW(STRING_INVALID_NUMBER
),
1965 wszAppTitle
, MB_OK
| MB_ICONASTERISK
);
1969 pf
.dxStartIndent
= pf
.dxStartIndent
+ pf
.dxOffset
;
1970 pf
.cbSize
= sizeof(pf
);
1971 pf
.dwMask
= PFM_OFFSET
| PFM_OFFSETINDENT
| PFM_RIGHTINDENT
;
1972 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
1978 EndDialog(hWnd
, wParam
);
1985 static INT_PTR CALLBACK
tabstops_proc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
1991 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
1993 WCHAR buffer
[MAX_STRING_LEN
];
1996 pf
.cbSize
= sizeof(pf
);
1997 pf
.dwMask
= PFM_TABSTOPS
;
1998 SendMessageW(hEditorWnd
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
1999 SendMessageW(hTabWnd
, CB_LIMITTEXT
, MAX_STRING_LEN
-1, 0);
2001 for(i
= 0; i
< pf
.cTabCount
; i
++)
2003 number_with_units(buffer
, pf
.rgxTabs
[i
]);
2004 SendMessageW(hTabWnd
, CB_ADDSTRING
, 0, (LPARAM
)&buffer
);
2011 switch(LOWORD(wParam
))
2015 HWND hTabWnd
= (HWND
)lParam
;
2016 HWND hAddWnd
= GetDlgItem(hWnd
, ID_TAB_ADD
);
2017 HWND hDelWnd
= GetDlgItem(hWnd
, ID_TAB_DEL
);
2018 HWND hEmptyWnd
= GetDlgItem(hWnd
, ID_TAB_EMPTY
);
2020 if(GetWindowTextLengthW(hTabWnd
))
2021 EnableWindow(hAddWnd
, TRUE
);
2023 EnableWindow(hAddWnd
, FALSE
);
2025 if(SendMessageW(hTabWnd
, CB_GETCOUNT
, 0, 0))
2027 EnableWindow(hEmptyWnd
, TRUE
);
2029 if(SendMessageW(hTabWnd
, CB_GETCURSEL
, 0, 0) == CB_ERR
)
2030 EnableWindow(hDelWnd
, FALSE
);
2032 EnableWindow(hDelWnd
, TRUE
);
2035 EnableWindow(hEmptyWnd
, FALSE
);
2042 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2043 WCHAR buffer
[MAX_STRING_LEN
];
2045 GetWindowTextW(hTabWnd
, buffer
, MAX_STRING_LEN
);
2046 append_current_units(buffer
);
2048 if(SendMessageW(hTabWnd
, CB_FINDSTRINGEXACT
, -1, (LPARAM
)&buffer
) == CB_ERR
)
2052 if(!number_from_string(buffer
, &number
, TRUE
))
2054 MessageBoxW(hWnd
, MAKEINTRESOURCEW(STRING_INVALID_NUMBER
),
2055 wszAppTitle
, MB_OK
| MB_ICONINFORMATION
);
2058 SendMessageW(hTabWnd
, CB_ADDSTRING
, 0, (LPARAM
)&buffer
);
2059 SetWindowTextW(hTabWnd
, 0);
2068 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2070 ret
= SendMessageW(hTabWnd
, CB_GETCURSEL
, 0, 0);
2072 SendMessageW(hTabWnd
, CB_DELETESTRING
, ret
, 0);
2078 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2079 SendMessageW(hTabWnd
, CB_RESETCONTENT
, 0, 0);
2086 HWND hTabWnd
= GetDlgItem(hWnd
, IDC_TABSTOPS
);
2088 WCHAR buffer
[MAX_STRING_LEN
];
2092 pf
.cbSize
= sizeof(pf
);
2093 pf
.dwMask
= PFM_TABSTOPS
;
2095 for(i
= 0; SendMessageW(hTabWnd
, CB_GETLBTEXT
, i
,
2096 (LPARAM
)&buffer
) != CB_ERR
&&
2097 i
< MAX_TAB_STOPS
; i
++)
2099 number_from_string(buffer
, &number
, TRUE
);
2100 pf
.rgxTabs
[i
] = current_units_to_twips(number
);
2103 SendMessageW(hEditorWnd
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2107 EndDialog(hWnd
, wParam
);
2114 static int context_menu(LPARAM lParam
)
2116 int x
= (int)(short)LOWORD(lParam
);
2117 int y
= (int)(short)HIWORD(lParam
);
2118 HMENU hPop
= GetSubMenu(hPopupMenu
, 0);
2122 int from
= 0, to
= 0;
2124 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
2125 SendMessageW(hEditorWnd
, EM_POSFROMCHAR
, (WPARAM
)&pt
, (LPARAM
)to
);
2126 ClientToScreen(hEditorWnd
, (POINT
*)&pt
);
2131 TrackPopupMenu(hPop
, TPM_LEFTALIGN
| TPM_TOPALIGN
| TPM_RIGHTBUTTON
,
2132 x
, y
, 0, hMainWnd
, 0);
2137 static LRESULT
OnCreate( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2139 HWND hToolBarWnd
, hFormatBarWnd
, hReBarWnd
;
2140 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2143 int nStdBitmaps
= 0;
2146 static const WCHAR wszRichEditDll
[] = {'R','I','C','H','E','D','2','0','.','D','L','L','\0'};
2147 static const WCHAR wszRichEditText
[] = {'R','i','c','h','E','d','i','t',' ','t','e','x','t','\0'};
2149 CreateStatusWindowW(CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
, wszRichEditText
, hWnd
, IDC_STATUSBAR
);
2151 hReBarWnd
= CreateWindowExW(WS_EX_TOOLWINDOW
, REBARCLASSNAMEW
, NULL
,
2152 CCS_NODIVIDER
|WS_CHILD
|WS_VISIBLE
|WS_CLIPSIBLINGS
|WS_CLIPCHILDREN
|RBS_VARHEIGHT
|CCS_TOP
,
2153 CW_USEDEFAULT
, CW_USEDEFAULT
, 0, 0, hWnd
, (HMENU
)IDC_REBAR
, hInstance
, NULL
);
2155 rbi
.cbSize
= sizeof(rbi
);
2158 if(!SendMessageW(hReBarWnd
, RB_SETBARINFO
, 0, (LPARAM
)&rbi
))
2161 hToolBarWnd
= CreateToolbarEx(hReBarWnd
, CCS_NOPARENTALIGN
|CCS_NOMOVEY
|WS_VISIBLE
|WS_CHILD
|TBSTYLE_TOOLTIPS
|TBSTYLE_BUTTON
,
2163 1, hInstance
, IDB_TOOLBAR
,
2165 24, 24, 16, 16, sizeof(TBBUTTON
));
2167 ab
.hInst
= HINST_COMMCTRL
;
2168 ab
.nID
= IDB_STD_SMALL_COLOR
;
2169 nStdBitmaps
= SendMessageW(hToolBarWnd
, TB_ADDBITMAP
, 0, (LPARAM
)&ab
);
2171 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILENEW
, ID_FILE_NEW
);
2172 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILEOPEN
, ID_FILE_OPEN
);
2173 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FILESAVE
, ID_FILE_SAVE
);
2174 AddSeparator(hToolBarWnd
);
2175 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINT
, ID_PRINT_QUICK
);
2176 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PRINTPRE
, ID_PREVIEW
);
2177 AddSeparator(hToolBarWnd
);
2178 AddButton(hToolBarWnd
, nStdBitmaps
+STD_FIND
, ID_FIND
);
2179 AddSeparator(hToolBarWnd
);
2180 AddButton(hToolBarWnd
, nStdBitmaps
+STD_CUT
, ID_EDIT_CUT
);
2181 AddButton(hToolBarWnd
, nStdBitmaps
+STD_COPY
, ID_EDIT_COPY
);
2182 AddButton(hToolBarWnd
, nStdBitmaps
+STD_PASTE
, ID_EDIT_PASTE
);
2183 AddButton(hToolBarWnd
, nStdBitmaps
+STD_UNDO
, ID_EDIT_UNDO
);
2184 AddButton(hToolBarWnd
, nStdBitmaps
+STD_REDOW
, ID_EDIT_REDO
);
2185 AddSeparator(hToolBarWnd
);
2186 AddButton(hToolBarWnd
, 0, ID_DATETIME
);
2188 SendMessageW(hToolBarWnd
, TB_AUTOSIZE
, 0, 0);
2190 rbb
.cbSize
= sizeof(rbb
);
2191 rbb
.fMask
= RBBIM_SIZE
| RBBIM_CHILDSIZE
| RBBIM_CHILD
| RBBIM_STYLE
| RBBIM_ID
;
2192 rbb
.fStyle
= RBBS_CHILDEDGE
| RBBS_BREAK
| RBBS_NOGRIPPER
;
2194 rbb
.hwndChild
= hToolBarWnd
;
2196 rbb
.cyChild
= rbb
.cyMinChild
= HIWORD(SendMessageW(hToolBarWnd
, TB_GETBUTTONSIZE
, 0, 0));
2197 rbb
.wID
= BANDID_TOOLBAR
;
2199 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
2201 hFormatBarWnd
= CreateToolbarEx(hReBarWnd
,
2202 CCS_NOPARENTALIGN
| CCS_NOMOVEY
| WS_VISIBLE
| TBSTYLE_TOOLTIPS
| TBSTYLE_BUTTON
,
2203 IDC_FORMATBAR
, 7, hInstance
, IDB_FORMATBAR
, NULL
, 0, 16, 16, 16, 16, sizeof(TBBUTTON
));
2205 AddButton(hFormatBarWnd
, 0, ID_FORMAT_BOLD
);
2206 AddButton(hFormatBarWnd
, 1, ID_FORMAT_ITALIC
);
2207 AddButton(hFormatBarWnd
, 2, ID_FORMAT_UNDERLINE
);
2208 AddSeparator(hFormatBarWnd
);
2209 AddButton(hFormatBarWnd
, 3, ID_ALIGN_LEFT
);
2210 AddButton(hFormatBarWnd
, 4, ID_ALIGN_CENTER
);
2211 AddButton(hFormatBarWnd
, 5, ID_ALIGN_RIGHT
);
2212 AddSeparator(hFormatBarWnd
);
2213 AddButton(hFormatBarWnd
, 6, ID_BULLET
);
2215 SendMessageW(hFormatBarWnd
, TB_AUTOSIZE
, 0, 0);
2217 rbb
.hwndChild
= hFormatBarWnd
;
2218 rbb
.wID
= BANDID_FORMATBAR
;
2220 SendMessageW(hReBarWnd
, RB_INSERTBAND
, -1, (LPARAM
)&rbb
);
2222 hDLL
= LoadLibraryW(wszRichEditDll
);
2225 MessageBoxW(hWnd
, MAKEINTRESOURCEW(STRING_LOAD_RICHED_FAILED
), wszAppTitle
,
2226 MB_OK
| MB_ICONEXCLAMATION
);
2230 hEditorWnd
= CreateWindowExW(WS_EX_CLIENTEDGE
, wszRichEditClass
, NULL
,
2231 WS_CHILD
|WS_VISIBLE
|ECO_SELECTIONBAR
|ES_MULTILINE
|ES_AUTOVSCROLL
|ES_WANTRETURN
|WS_VSCROLL
,
2232 0, 0, 1000, 100, hWnd
, (HMENU
)IDC_EDITOR
, hInstance
, NULL
);
2236 fprintf(stderr
, "Error code %u\n", GetLastError());
2241 SetFocus(hEditorWnd
);
2242 SendMessageW(hEditorWnd
, EM_SETEVENTMASK
, 0, ENM_SELCHANGE
);
2247 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
2249 ID_FINDMSGSTRING
= RegisterWindowMessageW(FINDMSGSTRINGW
);
2251 registry_read_filelist(hWnd
);
2252 registry_read_formatopts_all();
2253 registry_read_options();
2254 DragAcceptFiles(hWnd
, TRUE
);
2259 static LRESULT
OnUser( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2261 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2262 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
2263 HWND hwndToolBar
= GetDlgItem(hwndReBar
, IDC_TOOLBAR
);
2264 HWND hwndFormatBar
= GetDlgItem(hwndReBar
, IDC_FORMATBAR
);
2270 ZeroMemory(&fmt
, sizeof(fmt
));
2271 fmt
.cbSize
= sizeof(fmt
);
2273 ZeroMemory(&pf
, sizeof(pf
));
2274 pf
.cbSize
= sizeof(pf
);
2276 gt
.flags
= GTL_NUMCHARS
;
2279 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_FIND
,
2280 SendMessageW(hwndEditor
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0) ? 1 : 0);
2282 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, TRUE
, (LPARAM
)&fmt
);
2284 SendMessageW(hwndEditor
, EM_GETSEL
, (WPARAM
)&from
, (LPARAM
)&to
);
2285 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_UNDO
,
2286 SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0));
2287 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_REDO
,
2288 SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0));
2289 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_CUT
, from
== to
? 0 : 1);
2290 SendMessageW(hwndToolBar
, TB_ENABLEBUTTON
, ID_EDIT_COPY
, from
== to
? 0 : 1);
2292 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_BOLD
, (fmt
.dwMask
& CFM_BOLD
) &&
2293 (fmt
.dwEffects
& CFE_BOLD
));
2294 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_BOLD
, !(fmt
.dwMask
& CFM_BOLD
));
2295 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_ITALIC
, (fmt
.dwMask
& CFM_ITALIC
) &&
2296 (fmt
.dwEffects
& CFE_ITALIC
));
2297 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_ITALIC
, !(fmt
.dwMask
& CFM_ITALIC
));
2298 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_FORMAT_UNDERLINE
, (fmt
.dwMask
& CFM_UNDERLINE
) &&
2299 (fmt
.dwEffects
& CFE_UNDERLINE
));
2300 SendMessageW(hwndFormatBar
, TB_INDETERMINATE
, ID_FORMAT_UNDERLINE
, !(fmt
.dwMask
& CFM_UNDERLINE
));
2302 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2303 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_LEFT
, (pf
.wAlignment
== PFA_LEFT
));
2304 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_CENTER
, (pf
.wAlignment
== PFA_CENTER
));
2305 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_ALIGN_RIGHT
, (pf
.wAlignment
== PFA_RIGHT
));
2307 SendMessageW(hwndFormatBar
, TB_CHECKBUTTON
, ID_BULLET
, (pf
.wNumbering
& PFN_BULLET
));
2311 static LRESULT
OnNotify( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2313 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2314 NMHDR
*pHdr
= (NMHDR
*)lParam
;
2316 if (pHdr
->hwndFrom
!= hwndEditor
)
2319 if (pHdr
->code
== EN_SELCHANGE
)
2321 SELCHANGE
*pSC
= (SELCHANGE
*)lParam
;
2324 sprintf( buf
,"selection = %d..%d, line count=%ld",
2325 pSC
->chrg
.cpMin
, pSC
->chrg
.cpMax
,
2326 SendMessage(hwndEditor
, EM_GETLINECOUNT
, 0, 0));
2327 SetWindowText(GetDlgItem(hWnd
, IDC_STATUSBAR
), buf
);
2328 SendMessage(hWnd
, WM_USER
, 0, 0);
2334 static LRESULT
OnCommand( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2336 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2337 static FINDREPLACEW findreplace
;
2339 if ((HWND
)lParam
== hwndEditor
)
2342 switch(LOWORD(wParam
))
2346 PostMessageW(hWnd
, WM_CLOSE
, 0, 0);
2351 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2352 int ret
= DialogBox(hInstance
, MAKEINTRESOURCE(IDD_NEWFILE
), hWnd
,
2353 (DLGPROC
)newfile_proc
);
2355 if(ret
!= ID_NEWFILE_ABORT
)
2357 if(prompt_save_changes())
2362 wszFileName
[0] = '\0';
2366 st
.flags
= ST_DEFAULT
;
2368 SendMessageW(hEditorWnd
, EM_SETTEXTEX
, (WPARAM
)&st
, 0);
2370 SendMessageW(hEditorWnd
, EM_SETMODIFY
, FALSE
, 0);
2371 set_fileformat(ret
);
2384 DoSaveFile(wszFileName
, fileFormat
);
2389 case ID_FILE_SAVEAS
:
2393 case ID_FILE_RECENT1
:
2394 case ID_FILE_RECENT2
:
2395 case ID_FILE_RECENT3
:
2396 case ID_FILE_RECENT4
:
2398 HMENU hMenu
= GetMenu(hWnd
);
2401 mi
.cbSize
= sizeof(MENUITEMINFOW
);
2402 mi
.fMask
= MIIM_DATA
;
2403 if(GetMenuItemInfoW(hMenu
, LOWORD(wParam
), FALSE
, &mi
))
2404 DoOpenFile((LPWSTR
)mi
.dwItemData
);
2409 dialog_find(&findreplace
);
2413 handle_findmsg(&findreplace
);
2420 case ID_PRINT_QUICK
:
2426 int index
= reg_formatindex(fileFormat
);
2427 DWORD tmp
= barState
[index
];
2428 barState
[index
] = 0;
2430 barState
[index
] = tmp
;
2431 ShowWindow(hEditorWnd
, FALSE
);
2432 preview_bar_show(TRUE
);
2436 SetMenu(hWnd
, NULL
);
2437 InvalidateRect(0, 0, TRUE
);
2442 dialog_printsetup();
2445 case ID_FORMAT_BOLD
:
2446 case ID_FORMAT_ITALIC
:
2447 case ID_FORMAT_UNDERLINE
:
2450 int mask
= CFM_BOLD
;
2451 if (LOWORD(wParam
) == ID_FORMAT_ITALIC
) mask
= CFM_ITALIC
;
2452 if (LOWORD(wParam
) == ID_FORMAT_UNDERLINE
) mask
= CFM_UNDERLINE
;
2454 ZeroMemory(&fmt
, sizeof(fmt
));
2455 fmt
.cbSize
= sizeof(fmt
);
2456 SendMessageW(hwndEditor
, EM_GETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
2457 if (!(fmt
.dwMask
&mask
))
2458 fmt
.dwEffects
|= mask
;
2460 fmt
.dwEffects
^= mask
;
2462 SendMessageW(hwndEditor
, EM_SETCHARFORMAT
, SCF_SELECTION
, (LPARAM
)&fmt
);
2467 PostMessageW(hwndEditor
, WM_CUT
, 0, 0);
2471 PostMessageW(hwndEditor
, WM_COPY
, 0, 0);
2475 PostMessageW(hwndEditor
, WM_PASTE
, 0, 0);
2479 PostMessageW(hwndEditor
, WM_CLEAR
, 0, 0);
2482 case ID_EDIT_SELECTALL
:
2484 CHARRANGE range
= {0, -1};
2485 SendMessageW(hwndEditor
, EM_EXSETSEL
, 0, (LPARAM
)&range
);
2486 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2490 case ID_EDIT_GETTEXT
:
2492 int nLen
= GetWindowTextLengthW(hwndEditor
);
2493 LPWSTR data
= HeapAlloc( GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
) );
2496 GetWindowTextW(hwndEditor
, data
, nLen
+1);
2497 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
2499 HeapFree( GetProcessHeap(), 0, data
);
2500 data
= HeapAlloc(GetProcessHeap(), 0, (nLen
+1)*sizeof(WCHAR
));
2502 tr
.chrg
.cpMax
= nLen
;
2503 tr
.lpstrText
= data
;
2504 SendMessage (hwndEditor
, EM_GETTEXTRANGE
, 0, (LPARAM
)&tr
);
2505 MessageBoxW(NULL
, data
, xszAppTitle
, MB_OK
);
2506 HeapFree( GetProcessHeap(), 0, data
);
2508 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2512 case ID_EDIT_CHARFORMAT
:
2513 case ID_EDIT_DEFCHARFORMAT
:
2517 ZeroMemory(&cf
, sizeof(cf
));
2518 cf
.cbSize
= sizeof(cf
);
2520 i
= SendMessageW(hwndEditor
, EM_GETCHARFORMAT
,
2521 LOWORD(wParam
) == ID_EDIT_CHARFORMAT
, (LPARAM
)&cf
);
2525 case ID_EDIT_PARAFORMAT
:
2528 ZeroMemory(&pf
, sizeof(pf
));
2529 pf
.cbSize
= sizeof(pf
);
2530 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2534 case ID_EDIT_SELECTIONINFO
:
2536 CHARRANGE range
= {0, -1};
2540 SendMessage(hwndEditor
, EM_EXGETSEL
, 0, (LPARAM
)&range
);
2541 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(*data
) * (range
.cpMax
-range
.cpMin
+1));
2542 SendMessage(hwndEditor
, EM_GETSELTEXT
, 0, (LPARAM
)data
);
2543 sprintf(buf
, "Start = %d, End = %d", range
.cpMin
, range
.cpMax
);
2544 MessageBoxA(hWnd
, buf
, "Editor", MB_OK
);
2545 MessageBoxW(hWnd
, data
, xszAppTitle
, MB_OK
);
2546 HeapFree( GetProcessHeap(), 0, data
);
2547 /* SendMessage(hwndEditor, EM_SETSEL, 0, -1); */
2551 case ID_EDIT_READONLY
:
2553 long nStyle
= GetWindowLong(hwndEditor
, GWL_STYLE
);
2554 if (nStyle
& ES_READONLY
)
2555 SendMessageW(hwndEditor
, EM_SETREADONLY
, 0, 0);
2557 SendMessageW(hwndEditor
, EM_SETREADONLY
, 1, 0);
2561 case ID_EDIT_MODIFIED
:
2562 if (SendMessageW(hwndEditor
, EM_GETMODIFY
, 0, 0))
2563 SendMessageW(hwndEditor
, EM_SETMODIFY
, 0, 0);
2565 SendMessageW(hwndEditor
, EM_SETMODIFY
, 1, 0);
2569 SendMessageW(hwndEditor
, EM_UNDO
, 0, 0);
2573 SendMessageW(hwndEditor
, EM_REDO
, 0, 0);
2580 pf
.cbSize
= sizeof(pf
);
2581 pf
.dwMask
= PFM_NUMBERING
;
2582 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2584 pf
.dwMask
|= PFM_OFFSET
;
2586 if(pf
.wNumbering
== PFN_BULLET
)
2592 pf
.wNumbering
= PFN_BULLET
;
2596 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2601 case ID_ALIGN_CENTER
:
2602 case ID_ALIGN_RIGHT
:
2606 pf
.cbSize
= sizeof(pf
);
2607 pf
.dwMask
= PFM_ALIGNMENT
;
2608 switch(LOWORD(wParam
)) {
2609 case ID_ALIGN_LEFT
: pf
.wAlignment
= PFA_LEFT
; break;
2610 case ID_ALIGN_CENTER
: pf
.wAlignment
= PFA_CENTER
; break;
2611 case ID_ALIGN_RIGHT
: pf
.wAlignment
= PFA_RIGHT
; break;
2613 SendMessageW(hwndEditor
, EM_SETPARAFORMAT
, 0, (LPARAM
)&pf
);
2618 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 1, 0);
2622 SendMessageW(hwndEditor
, EM_SETBKGNDCOLOR
, 0, RGB(255,255,192));
2625 case ID_TOGGLE_TOOLBAR
:
2626 set_toolbar_state(BANDID_TOOLBAR
, !is_bar_visible(BANDID_TOOLBAR
));
2630 case ID_TOGGLE_FORMATBAR
:
2631 set_toolbar_state(BANDID_FORMATBAR
, !is_bar_visible(BANDID_FORMATBAR
));
2635 case ID_TOGGLE_STATUSBAR
:
2636 set_statusbar_state(!is_bar_visible(BANDID_STATUSBAR
));
2642 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2643 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_DATETIME
), hWnd
, (DLGPROC
)datetime_proc
);
2649 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2650 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_PARAFORMAT
), hWnd
,
2657 HINSTANCE hInstance
= (HINSTANCE
)GetWindowLongPtr(hWnd
, GWLP_HINSTANCE
);
2658 DialogBoxW(hInstance
, MAKEINTRESOURCEW(IDD_TABSTOPS
), hWnd
, tabstops_proc
);
2666 case ID_VIEWPROPERTIES
:
2667 dialog_viewproperties();
2671 SendMessageW(hwndEditor
, WM_COMMAND
, wParam
, lParam
);
2677 static LRESULT
OnInitPopupMenu( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2679 HMENU hMenu
= (HMENU
)wParam
;
2680 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2681 HWND hwndStatus
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
2683 int nAlignment
= -1;
2689 SendMessageW(hEditorWnd
, EM_GETSEL
, (WPARAM
)&selFrom
, (LPARAM
)&selTo
);
2690 EnableMenuItem(hMenu
, ID_EDIT_COPY
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
2691 EnableMenuItem(hMenu
, ID_EDIT_CUT
, MF_BYCOMMAND
|(selFrom
== selTo
) ? MF_GRAYED
: MF_ENABLED
);
2693 pf
.cbSize
= sizeof(PARAFORMAT
);
2694 SendMessageW(hwndEditor
, EM_GETPARAFORMAT
, 0, (LPARAM
)&pf
);
2695 CheckMenuItem(hMenu
, ID_EDIT_READONLY
,
2696 MF_BYCOMMAND
|(GetWindowLong(hwndEditor
, GWL_STYLE
)&ES_READONLY
? MF_CHECKED
: MF_UNCHECKED
));
2697 CheckMenuItem(hMenu
, ID_EDIT_MODIFIED
,
2698 MF_BYCOMMAND
|(SendMessage(hwndEditor
, EM_GETMODIFY
, 0, 0) ? MF_CHECKED
: MF_UNCHECKED
));
2699 if (pf
.dwMask
& PFM_ALIGNMENT
)
2700 nAlignment
= pf
.wAlignment
;
2701 CheckMenuItem(hMenu
, ID_ALIGN_LEFT
, MF_BYCOMMAND
|(nAlignment
== PFA_LEFT
) ?
2702 MF_CHECKED
: MF_UNCHECKED
);
2703 CheckMenuItem(hMenu
, ID_ALIGN_CENTER
, MF_BYCOMMAND
|(nAlignment
== PFA_CENTER
) ?
2704 MF_CHECKED
: MF_UNCHECKED
);
2705 CheckMenuItem(hMenu
, ID_ALIGN_RIGHT
, MF_BYCOMMAND
|(nAlignment
== PFA_RIGHT
) ?
2706 MF_CHECKED
: MF_UNCHECKED
);
2707 CheckMenuItem(hMenu
, ID_BULLET
, MF_BYCOMMAND
| ((pf
.wNumbering
== PFN_BULLET
) ?
2708 MF_CHECKED
: MF_UNCHECKED
));
2709 EnableMenuItem(hMenu
, ID_EDIT_UNDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANUNDO
, 0, 0)) ?
2710 MF_ENABLED
: MF_GRAYED
);
2711 EnableMenuItem(hMenu
, ID_EDIT_REDO
, MF_BYCOMMAND
|(SendMessageW(hwndEditor
, EM_CANREDO
, 0, 0)) ?
2712 MF_ENABLED
: MF_GRAYED
);
2714 CheckMenuItem(hMenu
, ID_TOGGLE_TOOLBAR
, MF_BYCOMMAND
|(is_bar_visible(BANDID_TOOLBAR
)) ?
2715 MF_CHECKED
: MF_UNCHECKED
);
2717 CheckMenuItem(hMenu
, ID_TOGGLE_FORMATBAR
, MF_BYCOMMAND
|(is_bar_visible(BANDID_FORMATBAR
)) ?
2718 MF_CHECKED
: MF_UNCHECKED
);
2720 CheckMenuItem(hMenu
, ID_TOGGLE_STATUSBAR
, MF_BYCOMMAND
|IsWindowVisible(hwndStatus
) ?
2721 MF_CHECKED
: MF_UNCHECKED
);
2723 gt
.flags
= GTL_NUMCHARS
;
2725 textLength
= SendMessageW(hEditorWnd
, EM_GETTEXTLENGTHEX
, (WPARAM
)>
, 0);
2726 EnableMenuItem(hMenu
, ID_FIND
, MF_BYCOMMAND
|(textLength
? MF_ENABLED
: MF_GRAYED
));
2728 mi
.cbSize
= sizeof(mi
);
2729 mi
.fMask
= MIIM_DATA
;
2731 GetMenuItemInfoW(hMenu
, ID_FIND_NEXT
, FALSE
, &mi
);
2733 EnableMenuItem(hMenu
, ID_FIND_NEXT
, MF_BYCOMMAND
|((textLength
&& mi
.dwItemData
) ?
2734 MF_ENABLED
: MF_GRAYED
));
2738 static LRESULT
OnSize( HWND hWnd
, WPARAM wParam
, LPARAM lParam
)
2740 int nStatusSize
= 0;
2742 HWND hwndEditor
= GetDlgItem(hWnd
, IDC_EDITOR
);
2743 HWND hwndStatusBar
= GetDlgItem(hWnd
, IDC_STATUSBAR
);
2744 HWND hwndReBar
= GetDlgItem(hWnd
, IDC_REBAR
);
2745 int rebarHeight
= 0;
2750 SendMessageW(hwndStatusBar
, WM_SIZE
, 0, 0);
2751 if (IsWindowVisible(hwndStatusBar
))
2753 GetClientRect(hwndStatusBar
, &rc
);
2754 nStatusSize
= rc
.bottom
- rc
.top
;
2762 if(!is_bar_visible(BANDID_TOOLBAR
))
2765 if(!is_bar_visible(BANDID_FORMATBAR
))
2768 rebarHeight
= rebarRows
? SendMessageW(hwndReBar
, RB_GETBARHEIGHT
, 0, 0) : 0;
2770 rc
.top
= rc
.left
= 0;
2771 rc
.bottom
= rebarHeight
;
2772 rc
.right
= LOWORD(lParam
);
2773 SendMessageW(hwndReBar
, RB_SIZETORECT
, 0, (LPARAM
)&rc
);
2777 GetClientRect(hWnd
, &rc
);
2778 MoveWindow(hwndEditor
, 0, rebarHeight
, rc
.right
, rc
.bottom
-nStatusSize
-rebarHeight
, TRUE
);
2781 return DefWindowProcW(hWnd
, WM_SIZE
, wParam
, lParam
);
2784 static LRESULT CALLBACK
WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
2786 if(msg
== ID_FINDMSGSTRING
)
2787 return handle_findmsg((LPFINDREPLACEW
)lParam
);
2792 return OnCreate( hWnd
, wParam
, lParam
);
2795 return OnUser( hWnd
, wParam
, lParam
);
2798 return OnNotify( hWnd
, wParam
, lParam
);
2802 return preview_command( hWnd
, wParam
, lParam
);
2804 return OnCommand( hWnd
, wParam
, lParam
);
2814 } else if(prompt_save_changes())
2816 registry_set_options();
2817 registry_set_formatopts_all();
2824 SetFocus(GetDlgItem(hWnd
, IDC_EDITOR
));
2827 case WM_INITMENUPOPUP
:
2828 return OnInitPopupMenu( hWnd
, wParam
, lParam
);
2831 return OnSize( hWnd
, wParam
, lParam
);
2833 case WM_CONTEXTMENU
:
2834 if((HWND
)wParam
== hEditorWnd
)
2835 return context_menu(lParam
);
2837 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2841 WCHAR file
[MAX_PATH
];
2842 DragQueryFileW((HDROP
)wParam
, 0, file
, MAX_PATH
);
2843 DragFinish((HDROP
)wParam
);
2845 if(prompt_save_changes())
2851 return print_preview();
2853 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2856 return DefWindowProcW(hWnd
, msg
, wParam
, lParam
);
2862 int CALLBACK
WinMain(HINSTANCE hInstance
, HINSTANCE hOldInstance
, LPSTR szCmdParagraph
, int res
)
2864 INITCOMMONCONTROLSEX classes
= {8, ICC_BAR_CLASSES
|ICC_COOL_CLASSES
};
2869 static const WCHAR wszAccelTable
[] = {'M','A','I','N','A','C','C','E','L',
2870 'T','A','B','L','E','\0'};
2872 InitCommonControlsEx(&classes
);
2874 hAccel
= LoadAcceleratorsW(hInstance
, wszAccelTable
);
2876 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
2877 wc
.lpfnWndProc
= WndProc
;
2880 wc
.hInstance
= hInstance
;
2881 wc
.hIcon
= LoadIconW(hInstance
, MAKEINTRESOURCEW(IDI_WORDPAD
));
2882 wc
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
2883 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
2884 wc
.lpszMenuName
= xszMainMenu
;
2885 wc
.lpszClassName
= wszMainWndClass
;
2886 RegisterClassW(&wc
);
2888 rc
= registry_read_winrect();
2889 hMainWnd
= CreateWindowExW(0, wszMainWndClass
, wszAppTitle
, WS_OVERLAPPEDWINDOW
,
2890 rc
.left
, rc
.top
, rc
.right
-rc
.left
, rc
.bottom
-rc
.top
, NULL
, NULL
, hInstance
, NULL
);
2891 ShowWindow(hMainWnd
, SW_SHOWDEFAULT
);
2895 hPopupMenu
= LoadMenuW(hInstance
, MAKEINTRESOURCEW(IDM_POPUP
));
2896 get_default_printer_opts();
2899 HandleCommandLine(GetCommandLineW());
2901 while(GetMessageW(&msg
,0,0,0))
2903 if (IsDialogMessage(hFindWnd
, &msg
))
2906 if (TranslateAcceleratorW(hMainWnd
, hAccel
, &msg
))
2908 TranslateMessage(&msg
);
2909 DispatchMessageW(&msg
);
2910 if (!PeekMessageW(&msg
, 0, 0, 0, PM_NOREMOVE
))
2911 SendMessageW(hMainWnd
, WM_USER
, 0, 0);