4 * Copyright 2000 Mike McCormack <Mike_McCormack@looksmart.com.au>
5 * Copyright 1997,98 Marcel Baur <mbaur@g26.ethz.ch>
6 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
7 * Copyright 2002 Andriy Palamarchuk
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "notepad_res.h"
34 NOTEPAD_GLOBALS Globals
;
35 static ATOM aFINDMSGSTRING
;
37 /***********************************************************************
41 * Sets Global File Name.
43 VOID
SetFileName(LPCWSTR szFileName
)
45 lstrcpy(Globals
.szFileName
, szFileName
);
46 Globals
.szFileTitle
[0] = 0;
47 GetFileTitle(szFileName
, Globals
.szFileTitle
, sizeof(Globals
.szFileTitle
));
50 /***********************************************************************
54 * Initialize font for the edit window
56 static VOID
NOTEPAD_InitFont(void)
58 LOGFONT
*lf
= &Globals
.lfFont
;
59 static const WCHAR systemW
[] = { 'S','y','s','t','e','m',0 };
64 lf
->lfOrientation
= 0;
65 lf
->lfWeight
= FW_BOLD
;
67 lf
->lfUnderline
= FALSE
;
68 lf
->lfStrikeOut
= FALSE
;
69 lf
->lfCharSet
= DEFAULT_CHARSET
;
70 lf
->lfOutPrecision
= OUT_DEFAULT_PRECIS
;
71 lf
->lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
72 lf
->lfQuality
= DEFAULT_QUALITY
;
73 lf
->lfPitchAndFamily
= FIXED_PITCH
| FF_DONTCARE
;
74 lstrcpy(lf
->lfFaceName
, systemW
);
76 Globals
.hFont
= CreateFontIndirect(lf
);
77 SendMessage(Globals
.hEdit
, WM_SETFONT
, (WPARAM
)Globals
.hFont
, (LPARAM
)FALSE
);
80 /***********************************************************************
84 * All handling of main menu events
86 static int NOTEPAD_MenuCommand(WPARAM wParam
)
90 case CMD_NEW
: DIALOG_FileNew(); break;
91 case CMD_OPEN
: DIALOG_FileOpen(); break;
92 case CMD_SAVE
: DIALOG_FileSave(); break;
93 case CMD_SAVE_AS
: DIALOG_FileSaveAs(); break;
94 case CMD_PRINT
: DIALOG_FilePrint(); break;
95 case CMD_PAGE_SETUP
: DIALOG_FilePageSetup(); break;
96 case CMD_PRINTER_SETUP
: DIALOG_FilePrinterSetup();break;
97 case CMD_EXIT
: DIALOG_FileExit(); break;
99 case CMD_UNDO
: DIALOG_EditUndo(); break;
100 case CMD_CUT
: DIALOG_EditCut(); break;
101 case CMD_COPY
: DIALOG_EditCopy(); break;
102 case CMD_PASTE
: DIALOG_EditPaste(); break;
103 case CMD_DELETE
: DIALOG_EditDelete(); break;
104 case CMD_SELECT_ALL
: DIALOG_EditSelectAll(); break;
105 case CMD_TIME_DATE
: DIALOG_EditTimeDate();break;
107 case CMD_SEARCH
: DIALOG_Search(); break;
108 case CMD_SEARCH_NEXT
: DIALOG_SearchNext(); break;
110 case CMD_WRAP
: DIALOG_EditWrap(); break;
111 case CMD_FONT
: DIALOG_SelectFont(); break;
113 case CMD_HELP_CONTENTS
: DIALOG_HelpContents(); break;
114 case CMD_HELP_SEARCH
: DIALOG_HelpSearch(); break;
115 case CMD_HELP_ON_HELP
: DIALOG_HelpHelp(); break;
116 case CMD_LICENSE
: DIALOG_HelpLicense(); break;
117 case CMD_NO_WARRANTY
: DIALOG_HelpNoWarranty(); break;
118 case CMD_ABOUT_WINE
: DIALOG_HelpAboutWine(); break;
126 /***********************************************************************
127 * Data Initialization
129 static VOID
NOTEPAD_InitData(VOID
)
131 LPWSTR p
= Globals
.szFilter
;
132 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
133 static const WCHAR all_files
[] = { '*','.','*',0 };
135 LoadString(Globals
.hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
);
137 lstrcpy(p
, txt_files
);
139 LoadString(Globals
.hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
141 lstrcpy(p
, all_files
);
144 Globals
.hDevMode
= NULL
;
145 Globals
.hDevNames
= NULL
;
148 /***********************************************************************
149 * Enable/disable items on the menu based on control state
151 static VOID
NOTEPAD_InitMenuPopup(HMENU menu
, int index
)
155 EnableMenuItem(menu
, CMD_UNDO
,
156 SendMessage(Globals
.hEdit
, EM_CANUNDO
, 0, 0) ? MF_ENABLED
: MF_GRAYED
);
157 EnableMenuItem(menu
, CMD_PASTE
,
158 IsClipboardFormatAvailable(CF_TEXT
) ? MF_ENABLED
: MF_GRAYED
);
159 enable
= SendMessage(Globals
.hEdit
, EM_GETSEL
, 0, 0);
160 enable
= (HIWORD(enable
) == LOWORD(enable
)) ? MF_GRAYED
: MF_ENABLED
;
161 EnableMenuItem(menu
, CMD_CUT
, enable
);
162 EnableMenuItem(menu
, CMD_COPY
, enable
);
163 EnableMenuItem(menu
, CMD_DELETE
, enable
);
165 EnableMenuItem(menu
, CMD_SELECT_ALL
,
166 GetWindowTextLength(Globals
.hEdit
) ? MF_ENABLED
: MF_GRAYED
);
169 /***********************************************************************
173 static LRESULT WINAPI
NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
,
180 static const WCHAR editW
[] = { 'e','d','i','t',0 };
182 GetClientRect(hWnd
, &rc
);
183 Globals
.hEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, editW
, NULL
,
184 WS_CHILD
| WS_VISIBLE
| WS_BORDER
| WS_VSCROLL
| WS_HSCROLL
|
185 ES_AUTOVSCROLL
| ES_MULTILINE
,
186 0, 0, rc
.right
, rc
.bottom
, hWnd
,
187 NULL
, Globals
.hInstance
, NULL
);
193 NOTEPAD_MenuCommand(LOWORD(wParam
));
196 case WM_DESTROYCLIPBOARD
:
197 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
206 case WM_QUERYENDSESSION
:
217 SetWindowPos(Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
),
218 SWP_NOOWNERZORDER
| SWP_NOZORDER
);
222 SetFocus(Globals
.hEdit
);
227 WCHAR szFileName
[MAX_PATH
];
228 HANDLE hDrop
= (HANDLE
) wParam
;
230 DragQueryFile(hDrop
, 0, szFileName
, SIZEOF(szFileName
));
232 DoOpenFile(szFileName
);
236 case WM_INITMENUPOPUP
:
237 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
241 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
246 static int AlertFileDoesNotExist(LPCWSTR szFileName
)
249 WCHAR szMessage
[MAX_STRING_LEN
];
250 WCHAR szResource
[MAX_STRING_LEN
];
252 LoadString(Globals
.hInstance
, STRING_DOESNOTEXIST
, szResource
, SIZEOF(szResource
));
253 wsprintf(szMessage
, szResource
, szFileName
);
255 LoadString(Globals
.hInstance
, STRING_ERROR
, szResource
, SIZEOF(szResource
));
257 nResult
= MessageBox(Globals
.hMainWnd
, szMessage
, szResource
,
258 MB_ICONEXCLAMATION
| MB_YESNO
);
263 static void HandleCommandLine(LPWSTR cmdline
)
268 /* skip white space */
269 while (*cmdline
== ' ') cmdline
++;
271 /* skip executable name */
272 delimiter
= (*cmdline
== '"' ? '"' : ' ');
274 if (*cmdline
== delimiter
) cmdline
++;
276 while (*cmdline
&& *cmdline
!= delimiter
) cmdline
++;
278 if (*cmdline
== delimiter
) cmdline
++;
280 while (*cmdline
== ' ' || *cmdline
== '-' || *cmdline
== '/')
284 if (*cmdline
++ == ' ') continue;
287 if (option
) cmdline
++;
288 while (*cmdline
== ' ') cmdline
++;
301 /* file name is passed in the command line */
306 if (cmdline
[0] == '"')
309 cmdline
[lstrlen(cmdline
) - 1] = 0;
312 if (FileExists(cmdline
))
319 static const WCHAR txtW
[] = { '.','t','x','t',0 };
321 /* try to find file with ".txt" extension */
322 if (!lstrcmp(txtW
, cmdline
+ lstrlen(cmdline
) - lstrlen(txtW
)))
329 lstrcpyn(buf
, cmdline
, MAX_PATH
- lstrlen(txtW
) - 1);
332 file_exists
= FileExists(buf
);
338 DoOpenFile(file_name
);
339 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
345 switch (AlertFileDoesNotExist(file_name
)) {
347 DoOpenFile(file_name
);
357 /***********************************************************************
361 int PASCAL
WinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPSTR cmdline
, int show
)
366 static const WCHAR className
[] = {'N','P','C','l','a','s','s',0};
367 static const WCHAR winName
[] = {'N','o','t','e','p','a','d',0};
369 aFINDMSGSTRING
= RegisterWindowMessage(FINDMSGSTRING
);
371 ZeroMemory(&Globals
, sizeof(Globals
));
372 Globals
.hInstance
= hInstance
;
374 ZeroMemory(&class, sizeof(class));
375 class.cbSize
= sizeof(class);
376 class.lpfnWndProc
= NOTEPAD_WndProc
;
377 class.hInstance
= Globals
.hInstance
;
378 class.hIcon
= LoadIcon(0, IDI_APPLICATION
);
379 class.hCursor
= LoadCursor(0, IDC_ARROW
);
380 class.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
381 class.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
382 class.lpszClassName
= className
;
384 if (!RegisterClassEx(&class)) return FALSE
;
389 CreateWindow(className
, winName
, WS_OVERLAPPEDWINDOW
,
390 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0,
391 NULL
, NULL
, Globals
.hInstance
, NULL
);
392 if (!Globals
.hMainWnd
)
401 ShowWindow(Globals
.hMainWnd
, show
);
402 UpdateWindow(Globals
.hMainWnd
);
403 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
405 HandleCommandLine(GetCommandLine());
407 hAccel
= LoadAccelerators( hInstance
, MAKEINTRESOURCE(ID_ACCEL
) );
409 while (GetMessage(&msg
, 0, 0, 0))
411 if (!TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
) && !IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
))
413 TranslateMessage(&msg
);
414 DispatchMessage(&msg
);