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 * All handling of main menu events
56 static int NOTEPAD_MenuCommand(WPARAM wParam
)
60 case CMD_NEW
: DIALOG_FileNew(); break;
61 case CMD_OPEN
: DIALOG_FileOpen(); break;
62 case CMD_SAVE
: DIALOG_FileSave(); break;
63 case CMD_SAVE_AS
: DIALOG_FileSaveAs(); break;
64 case CMD_PRINT
: DIALOG_FilePrint(); break;
65 case CMD_PAGE_SETUP
: DIALOG_FilePageSetup(); break;
66 case CMD_PRINTER_SETUP
: DIALOG_FilePrinterSetup();break;
67 case CMD_EXIT
: DIALOG_FileExit(); break;
69 case CMD_UNDO
: DIALOG_EditUndo(); break;
70 case CMD_CUT
: DIALOG_EditCut(); break;
71 case CMD_COPY
: DIALOG_EditCopy(); break;
72 case CMD_PASTE
: DIALOG_EditPaste(); break;
73 case CMD_DELETE
: DIALOG_EditDelete(); break;
74 case CMD_SELECT_ALL
: DIALOG_EditSelectAll(); break;
75 case CMD_TIME_DATE
: DIALOG_EditTimeDate();break;
77 case CMD_SEARCH
: DIALOG_Search(); break;
78 case CMD_SEARCH_NEXT
: DIALOG_SearchNext(); break;
80 case CMD_WRAP
: DIALOG_EditWrap(); break;
81 case CMD_FONT
: DIALOG_SelectFont(); break;
83 case CMD_HELP_CONTENTS
: DIALOG_HelpContents(); break;
84 case CMD_HELP_SEARCH
: DIALOG_HelpSearch(); break;
85 case CMD_HELP_ON_HELP
: DIALOG_HelpHelp(); break;
86 case CMD_LICENSE
: DIALOG_HelpLicense(); break;
87 case CMD_NO_WARRANTY
: DIALOG_HelpNoWarranty(); break;
88 case CMD_ABOUT_WINE
: DIALOG_HelpAboutWine(); break;
96 /***********************************************************************
99 static VOID
NOTEPAD_InitData(VOID
)
101 LPWSTR p
= Globals
.szFilter
;
102 static const WCHAR txt_files
[] = { '*','.','t','x','t',0 };
103 static const WCHAR all_files
[] = { '*','.','*',0 };
105 LoadString(Globals
.hInstance
, STRING_TEXT_FILES_TXT
, p
, MAX_STRING_LEN
);
107 lstrcpy(p
, txt_files
);
109 LoadString(Globals
.hInstance
, STRING_ALL_FILES
, p
, MAX_STRING_LEN
);
111 lstrcpy(p
, all_files
);
116 /***********************************************************************
117 * Enable/disable items on the menu based on control state
119 static VOID
NOTEPAD_InitMenuPopup(HMENU menu
, int index
)
123 EnableMenuItem(menu
, CMD_UNDO
,
124 SendMessage(Globals
.hEdit
, EM_CANUNDO
, 0, 0) ? MF_ENABLED
: MF_GRAYED
);
125 EnableMenuItem(menu
, CMD_PASTE
,
126 IsClipboardFormatAvailable(CF_TEXT
) ? MF_ENABLED
: MF_GRAYED
);
127 enable
= SendMessage(Globals
.hEdit
, EM_GETSEL
, 0, 0);
128 enable
= (HIWORD(enable
) == LOWORD(enable
)) ? MF_GRAYED
: MF_ENABLED
;
129 EnableMenuItem(menu
, CMD_CUT
, enable
);
130 EnableMenuItem(menu
, CMD_COPY
, enable
);
131 EnableMenuItem(menu
, CMD_DELETE
, enable
);
133 EnableMenuItem(menu
, CMD_SELECT_ALL
,
134 GetWindowTextLength(Globals
.hEdit
) ? MF_ENABLED
: MF_GRAYED
);
137 /***********************************************************************
141 static LRESULT WINAPI
NOTEPAD_WndProc(HWND hWnd
, UINT msg
, WPARAM wParam
,
148 static const WCHAR editW
[] = { 'e','d','i','t',0 };
150 GetClientRect(hWnd
, &rc
);
151 Globals
.hEdit
= CreateWindowEx(WS_EX_CLIENTEDGE
, editW
, NULL
,
152 WS_CHILD
| WS_VISIBLE
| WS_BORDER
| WS_VSCROLL
| WS_HSCROLL
|
153 ES_AUTOVSCROLL
| ES_MULTILINE
,
154 0, 0, rc
.right
, rc
.bottom
, hWnd
,
155 NULL
, Globals
.hInstance
, NULL
);
160 NOTEPAD_MenuCommand(LOWORD(wParam
));
163 case WM_DESTROYCLIPBOARD
:
164 /*MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);*/
173 case WM_QUERYENDSESSION
:
184 SetWindowPos(Globals
.hEdit
, NULL
, 0, 0, LOWORD(lParam
), HIWORD(lParam
),
185 SWP_NOOWNERZORDER
| SWP_NOZORDER
);
189 SetFocus(Globals
.hEdit
);
194 WCHAR szFileName
[MAX_PATH
];
195 HANDLE hDrop
= (HANDLE
) wParam
;
197 DragQueryFile(hDrop
, 0, szFileName
, SIZEOF(szFileName
));
199 DoOpenFile(szFileName
);
203 case WM_INITMENUPOPUP
:
204 NOTEPAD_InitMenuPopup((HMENU
)wParam
, lParam
);
208 return DefWindowProc(hWnd
, msg
, wParam
, lParam
);
213 static int AlertFileDoesNotExist(LPCWSTR szFileName
)
216 WCHAR szMessage
[MAX_STRING_LEN
];
217 WCHAR szResource
[MAX_STRING_LEN
];
219 LoadString(Globals
.hInstance
, STRING_DOESNOTEXIST
, szResource
, SIZEOF(szResource
));
220 wsprintf(szMessage
, szResource
, szFileName
);
222 LoadString(Globals
.hInstance
, STRING_ERROR
, szResource
, SIZEOF(szResource
));
224 nResult
= MessageBox(Globals
.hMainWnd
, szMessage
, szResource
,
225 MB_ICONEXCLAMATION
| MB_YESNO
);
230 static void HandleCommandLine(LPWSTR cmdline
)
235 /* skip white space */
236 while (*cmdline
== ' ') cmdline
++;
238 /* skip executable name */
239 delimiter
= (*cmdline
== '"' ? '"' : ' ');
245 while (*cmdline
&& *cmdline
!= delimiter
);
246 if (*cmdline
== delimiter
) cmdline
++;
248 while (*cmdline
== ' ' || *cmdline
== '-' || *cmdline
== '/')
252 if (*cmdline
++ == ' ') continue;
255 if (option
) cmdline
++;
256 while (*cmdline
== ' ') cmdline
++;
269 /* file name is passed in the command line */
274 if (cmdline
[0] == '"')
277 cmdline
[lstrlen(cmdline
) - 1] = 0;
280 if (FileExists(cmdline
))
287 static const WCHAR txtW
[] = { '.','t','x','t',0 };
289 /* try to find file with ".txt" extension */
290 if (!lstrcmp(txtW
, cmdline
+ lstrlen(cmdline
) - lstrlen(txtW
)))
297 lstrcpyn(buf
, cmdline
, MAX_PATH
- lstrlen(txtW
) - 1);
300 file_exists
= FileExists(buf
);
306 DoOpenFile(file_name
);
307 InvalidateRect(Globals
.hMainWnd
, NULL
, FALSE
);
313 switch (AlertFileDoesNotExist(file_name
)) {
315 DoOpenFile(file_name
);
325 /***********************************************************************
329 int PASCAL
WinMain(HINSTANCE hInstance
, HINSTANCE prev
, LPSTR cmdline
, int show
)
334 static const WCHAR className
[] = {'N','P','C','l','a','s','s',0};
335 static const WCHAR winName
[] = {'N','o','t','e','p','a','d',0};
337 aFINDMSGSTRING
= RegisterWindowMessage(FINDMSGSTRING
);
339 ZeroMemory(&Globals
, sizeof(Globals
));
340 Globals
.hInstance
= hInstance
;
342 ZeroMemory(&class, sizeof(class));
343 class.cbSize
= sizeof(class);
344 class.lpfnWndProc
= NOTEPAD_WndProc
;
345 class.hInstance
= Globals
.hInstance
;
346 class.hIcon
= LoadIcon(0, IDI_APPLICATION
);
347 class.hCursor
= LoadCursor(0, IDC_ARROW
);
348 class.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
);
349 class.lpszMenuName
= MAKEINTRESOURCE(MAIN_MENU
);
350 class.lpszClassName
= className
;
352 if (!RegisterClassEx(&class)) return FALSE
;
357 CreateWindow(className
, winName
, WS_OVERLAPPEDWINDOW
,
358 CW_USEDEFAULT
, 0, CW_USEDEFAULT
, 0,
359 NULL
, NULL
, Globals
.hInstance
, NULL
);
360 if (!Globals
.hMainWnd
)
369 ShowWindow(Globals
.hMainWnd
, show
);
370 UpdateWindow(Globals
.hMainWnd
);
371 DragAcceptFiles(Globals
.hMainWnd
, TRUE
);
373 HandleCommandLine(GetCommandLine());
375 hAccel
= LoadAccelerators( hInstance
, MAKEINTRESOURCE(ID_ACCEL
) );
377 while (GetMessage(&msg
, 0, 0, 0))
379 if (!TranslateAccelerator(Globals
.hMainWnd
, hAccel
, &msg
) && !IsDialogMessage(Globals
.hFindReplaceDlg
, &msg
))
381 TranslateMessage(&msg
);
382 DispatchMessage(&msg
);