A bit more robust against wave???Open failures.
[wine/multimedia.git] / programs / notepad / dialog.c
blobe92313fcbcf7c868fafd3e83b88309aaeae4b6f7
1 /*
2 * Notepad (dialog.c)
4 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5 * Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <assert.h>
23 #include <stdio.h>
24 #include <windows.h>
25 #include <commdlg.h>
26 #include <winerror.h>
28 #include "main.h"
29 #include "license.h"
30 #include "language.h"
31 #include "dialog.h"
33 static LRESULT WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
37 int AlertIDS(UINT ids_message, UINT ids_caption, WORD type) {
39 * Given some ids strings, this acts as a language-aware wrapper for
40 * "MessageBox"
42 CHAR szMessage[MAX_STRING_LEN];
43 CHAR szCaption[MAX_STRING_LEN];
45 LoadString(Globals.hInstance, ids_message, szMessage, sizeof(szMessage));
46 LoadString(Globals.hInstance, ids_caption, szCaption, sizeof(szCaption));
48 return (MessageBox(Globals.hMainWnd, szMessage, szCaption, type));
51 void AlertFileNotFound(LPSTR szFileName) {
53 int nResult;
54 CHAR szMessage[MAX_STRING_LEN];
55 CHAR szRessource[MAX_STRING_LEN];
57 /* Load and format szMessage */
58 LoadString(Globals.hInstance, STRING_NOTFOUND, szRessource, sizeof(szRessource));
59 wsprintf(szMessage, szRessource, szFileName);
61 /* Load szCaption */
62 LoadString(Globals.hInstance, STRING_ERROR, szRessource, sizeof(szRessource));
64 /* Display Modal Dialog */
65 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION);
69 int AlertFileNotSaved(LPSTR szFileName) {
71 int nResult;
72 CHAR szMessage[MAX_STRING_LEN];
73 CHAR szRessource[MAX_STRING_LEN];
75 /* Load and format Message */
77 LoadString(Globals.hInstance, STRING_NOTSAVED, szRessource, sizeof(szRessource));
78 wsprintf(szMessage, szRessource, szFileName);
80 /* Load Caption */
82 LoadString(Globals.hInstance, STRING_ERROR, szRessource, sizeof(szRessource));
84 /* Display modal */
85 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION|MB_YESNOCANCEL);
86 return(nResult);
90 VOID AlertOutOfMemory(void) {
91 int nResult;
93 nResult = AlertIDS(STRING_OUT_OF_MEMORY, STRING_ERROR, MB_ICONEXCLAMATION);
94 PostQuitMessage(1);
98 BOOL FileExists(LPCSTR szFilename) {
100 * Returns: TRUE - if "szFileName" exists
101 * FALSE - if it does not
103 WIN32_FIND_DATA entry;
104 HANDLE hFile;
106 hFile = FindFirstFile(szFilename, &entry);
108 return (hFile!=INVALID_HANDLE_VALUE);
111 VOID DoSaveFile(VOID) {
113 /* FIXME: Really Save the file */
114 /* ... (Globals.szFileName); */
118 BOOL DoCloseFile(void) {
119 /* Return value: TRUE - User agreed to close (both save/don't save) */
120 /* FALSE - User cancelled close by selecting "Cancel" */
122 int nResult;
124 if (strlen(Globals.szFileName)>0) {
125 /* prompt user to save changes */
126 nResult = AlertFileNotSaved(Globals.szFileName);
127 switch (nResult) {
128 case IDYES: DoSaveFile();
129 break;
131 case IDNO: break;
133 case IDCANCEL: return(FALSE);
134 break;
136 default: return(FALSE);
137 break;
138 } /* switch */
139 } /* if */
141 /* Forget file name */
142 lstrcpy(Globals.szFileName, "");
143 LANGUAGE_UpdateWindowCaption();
144 return(TRUE);
148 void DoOpenFile(LPCSTR szFileName) {
150 /* Close any files and prompt to save changes */
151 if (DoCloseFile()) {
152 GetFileTitle(szFileName, Globals.szFileName, sizeof(Globals.szFileName));
153 LANGUAGE_UpdateWindowCaption();
155 LoadBufferFromFile(szFileName);
160 VOID DIALOG_FileNew(VOID)
162 /* Close any files and promt to save changes */
163 if (DoCloseFile()) {
164 TrashBuffer();
168 VOID DIALOG_FileOpen(VOID)
170 OPENFILENAME openfilename;
171 CHAR szPath[MAX_PATHNAME_LEN];
172 CHAR szDir[MAX_PATHNAME_LEN];
173 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
174 CHAR szDefaultExt[4];
175 LPSTR p = szzFilter;
177 lstrcpy(szDefaultExt, "txt");
179 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
180 p += strlen(p) + 1;
181 lstrcpy(p, "*.txt");
182 p += strlen(p) + 1;
183 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
184 p += strlen(p) + 1;
185 lstrcpy(p, "*.*");
186 p += strlen(p) + 1;
187 *p = '\0';
189 GetCurrentDirectory(sizeof(szDir), szDir);
190 lstrcpy(szPath,"*.txt");
192 openfilename.lStructSize = sizeof(OPENFILENAME);
193 openfilename.hwndOwner = Globals.hMainWnd;
194 openfilename.hInstance = Globals.hInstance;
195 openfilename.lpstrFilter = szzFilter;
196 openfilename.lpstrCustomFilter = 0;
197 openfilename.nMaxCustFilter = 0;
198 openfilename.nFilterIndex = 0;
199 openfilename.lpstrFile = szPath;
200 openfilename.nMaxFile = sizeof(szPath);
201 openfilename.lpstrFileTitle = 0;
202 openfilename.nMaxFileTitle = 0;
203 openfilename.lpstrInitialDir = szDir;
204 openfilename.lpstrTitle = 0;
205 openfilename.Flags = OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST;
206 openfilename.nFileOffset = 0;
207 openfilename.nFileExtension = 0;
208 openfilename.lpstrDefExt = szDefaultExt;
209 openfilename.lCustData = 0;
210 openfilename.lpfnHook = 0;
211 openfilename.lpTemplateName = 0;
213 if (GetOpenFileName(&openfilename)) {
215 if (FileExists(openfilename.lpstrFile))
216 DoOpenFile(openfilename.lpstrFile);
217 else
218 AlertFileNotFound(openfilename.lpstrFile);
223 VOID DIALOG_FileSave(VOID)
225 /* FIXME: Save File */
227 DIALOG_FileSaveAs();
230 VOID DIALOG_FileSaveAs(VOID)
232 OPENFILENAME saveas;
233 CHAR szPath[MAX_PATHNAME_LEN];
234 CHAR szDir[MAX_PATHNAME_LEN];
235 CHAR szDefaultExt[4];
236 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
238 LPSTR p = szzFilter;
240 lstrcpy(szDefaultExt, "txt");
242 LoadString(Globals.hInstance, STRING_TEXT_FILES_TXT, p, MAX_STRING_LEN);
243 p += strlen(p) + 1;
244 lstrcpy(p, "*.txt");
245 p += strlen(p) + 1;
246 LoadString(Globals.hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN);
247 p += strlen(p) + 1;
248 lstrcpy(p, "*.*");
249 p += strlen(p) + 1;
250 *p = '\0';
252 lstrcpy(szPath,"*.*");
254 GetCurrentDirectory(sizeof(szDir), szDir);
256 saveas.lStructSize = sizeof(OPENFILENAME);
257 saveas.hwndOwner = Globals.hMainWnd;
258 saveas.hInstance = Globals.hInstance;
259 saveas.lpstrFilter = szzFilter;
260 saveas.lpstrCustomFilter = 0;
261 saveas.nMaxCustFilter = 0;
262 saveas.nFilterIndex = 0;
263 saveas.lpstrFile = szPath;
264 saveas.nMaxFile = sizeof(szPath);
265 saveas.lpstrFileTitle = 0;
266 saveas.nMaxFileTitle = 0;
267 saveas.lpstrInitialDir = szDir;
268 saveas.lpstrTitle = 0;
269 saveas.Flags = OFN_PATHMUSTEXIST + OFN_OVERWRITEPROMPT + OFN_HIDEREADONLY;
270 saveas.nFileOffset = 0;
271 saveas.nFileExtension = 0;
272 saveas.lpstrDefExt = szDefaultExt;
273 saveas.lCustData = 0;
274 saveas.lpfnHook = 0;
275 saveas.lpTemplateName = 0;
277 if (GetSaveFileName(&saveas)) {
278 lstrcpy(Globals.szFileName, saveas.lpstrFile);
279 LANGUAGE_UpdateWindowCaption();
280 DIALOG_FileSave();
284 VOID DIALOG_FilePrint(VOID)
286 LONG bFlags, nBase;
287 WORD nOffset;
288 DOCINFO di;
289 int nResult;
290 HDC hContext;
291 PRINTDLG printer;
293 CHAR szDocumentName[MAX_STRING_LEN]; /* Name of document */
294 CHAR szPrinterName[MAX_STRING_LEN]; /* Name of the printer */
295 CHAR szDeviceName[MAX_STRING_LEN]; /* Name of the printer device */
296 CHAR szOutput[MAX_STRING_LEN]; /* in which file/device to print */
298 /* LPDEVMODE hDevMode; */
299 /* LPDEVNAMES hDevNames; */
301 /* hDevMode = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVMODE)); */
302 /* hDevNames = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVNAMES)); */
304 /* Get Current Settings */
306 printer.lStructSize = sizeof(PRINTDLG);
307 printer.hwndOwner = Globals.hMainWnd;
308 printer.hInstance = Globals.hInstance;
310 /* Let PrintDlg create a DEVMODE structure */
311 printer.hDevMode = 0;
312 printer.hDevNames = 0;
313 printer.hDC = 0;
314 printer.Flags = PD_RETURNDEFAULT;
315 printer.nFromPage = 0;
316 printer.nToPage = 0;
317 printer.nMinPage = 0;
318 printer.nMaxPage = 0;
319 printer.nCopies = 0;
320 printer.lCustData = 0;
321 printer.lpfnPrintHook = 0;
322 printer.lpfnSetupHook = 0;
323 printer.lpPrintTemplateName = 0;
324 printer.lpSetupTemplateName = 0;
325 printer.hPrintTemplate = 0;
326 printer.hSetupTemplate = 0;
328 nResult = PrintDlg(&printer);
330 /* hContext = CreateDC(, szDeviceName, "TEST.TXT", 0); */
332 /* Congratulations to those Microsoft Engineers responsable */
333 /* for the following pointer acrobatics */
335 assert(printer.hDevNames!=0);
337 nBase = (LONG)(printer.hDevNames);
339 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDriverOffset;
340 lstrcpy(szPrinterName, (LPCSTR) (nBase + nOffset));
342 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDeviceOffset;
343 lstrcpy(szDeviceName, (LPCSTR) (nBase + nOffset));
345 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wOutputOffset;
346 lstrcpy(szOutput, (LPCSTR) (nBase + nOffset));
348 MessageBox(Globals.hMainWnd, szPrinterName, "Printer Name", MB_ICONEXCLAMATION);
349 MessageBox(Globals.hMainWnd, szDeviceName, "Device Name", MB_ICONEXCLAMATION);
350 MessageBox(Globals.hMainWnd, szOutput, "Output", MB_ICONEXCLAMATION);
352 /* Set some default flags */
354 bFlags = PD_RETURNDC + PD_SHOWHELP;
356 if (TRUE) {
357 /* Remove "Print Selection" if there is no selection */
358 bFlags = bFlags + PD_NOSELECTION;
361 printer.Flags = bFlags;
363 printer.nFromPage = 0;
364 printer.nToPage = 0;
365 printer.nMinPage = 0;
366 printer.nMaxPage = 0;
369 /* Let commdlg manage copy settings */
370 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
372 if (PrintDlg(&printer)) {
374 /* initialize DOCINFO */
375 di.cbSize = sizeof(DOCINFO);
376 lstrcpy((LPSTR)di.lpszDocName, szDocumentName);
377 lstrcpy((LPSTR)di.lpszOutput, szOutput);
379 hContext = printer.hDC;
380 assert(hContext!=0);
381 assert( (int) hContext!=PD_RETURNDC);
383 SetMapMode(hContext, MM_LOMETRIC);
384 /* SetViewPortExExt(hContext, 10, 10, 0); */
385 SetBkMode(hContext, OPAQUE);
387 nResult = TextOut(hContext, 0, 0, " ", 1);
388 assert(nResult != 0);
390 nResult = StartDoc(hContext, &di);
391 assert(nResult != SP_ERROR);
393 nResult = StartPage(hContext);
394 assert(nResult >0);
396 /* FIXME: actually print */
398 nResult = EndPage(hContext);
400 switch (nResult) {
401 case SP_ERROR:
402 MessageBox(Globals.hMainWnd, "Generic Error", "Print Engine Error", MB_ICONEXCLAMATION);
403 break;
404 case SP_APPABORT:
405 MessageBox(Globals.hMainWnd, "The print job was aborted.", "Print Engine Error", MB_ICONEXCLAMATION);
406 break;
407 case SP_USERABORT:
408 MessageBox(Globals.hMainWnd, "The print job was aborted using the Print Manager ", "Print Engine Error", MB_ICONEXCLAMATION);
409 break;
410 case SP_OUTOFDISK:
411 MessageBox(Globals.hMainWnd, "Out of disk space", "Print Engine Error", MB_ICONEXCLAMATION);
412 break;
413 case SP_OUTOFMEMORY:
414 AlertOutOfMemory();
415 break;
416 default:
417 MessageBox(Globals.hMainWnd, "Default", "Print", MB_ICONEXCLAMATION);
418 } /* switch */
419 nResult = EndDoc(hContext);
420 assert(nResult>=0);
421 nResult = DeleteDC(hContext);
422 assert(nResult!=0);
423 } /* if */
425 /* GlobalFree(hDevNames); */
426 /* GlobalFree(hDevMode); */
429 VOID DIALOG_FilePageSetup(VOID)
431 DIALOG_PageSetup();
434 VOID DIALOG_FilePrinterSetup(VOID)
436 PRINTDLG printer;
438 printer.lStructSize = sizeof(PRINTDLG);
439 printer.hwndOwner = Globals.hMainWnd;
440 printer.hInstance = Globals.hInstance;
441 printer.hDevMode = 0;
442 printer.hDevNames = 0;
443 printer.hDC = 0;
444 printer.Flags = PD_PRINTSETUP;
445 printer.nFromPage = 0;
446 printer.nToPage = 0;
447 printer.nMinPage = 0;
448 printer.nMaxPage = 0;
449 printer.nCopies = 1;
450 printer.lCustData = 0;
451 printer.lpfnPrintHook = 0;
452 printer.lpfnSetupHook = 0;
453 printer.lpPrintTemplateName = 0;
454 printer.lpSetupTemplateName = 0;
455 printer.hPrintTemplate = 0;
456 printer.hSetupTemplate = 0;
458 if (PrintDlg(&printer)) {
459 /* do nothing */
464 VOID DIALOG_FileExit(VOID)
466 if (DoCloseFile()) {
467 PostQuitMessage(0);
471 VOID DIALOG_EditUndo(VOID)
473 MessageBox(Globals.hMainWnd, "Undo", "Debug", MB_ICONEXCLAMATION);
474 /* undo */
477 VOID DIALOG_EditCut(VOID)
479 HANDLE hMem;
481 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
483 OpenClipboard(Globals.hMainWnd);
484 EmptyClipboard();
486 /* FIXME: Get text */
487 lstrcpy((CHAR *)hMem, "Hello World");
489 SetClipboardData(CF_TEXT, hMem);
490 CloseClipboard();
492 GlobalFree(hMem);
495 VOID DIALOG_EditCopy(VOID)
497 HANDLE hMem;
499 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
501 OpenClipboard(Globals.hMainWnd);
502 EmptyClipboard();
504 /* FIXME: Get text */
505 lstrcpy((CHAR *)hMem, "Hello World");
507 SetClipboardData(CF_TEXT, hMem);
508 CloseClipboard();
510 GlobalFree(hMem);
513 VOID DIALOG_EditPaste(VOID)
515 HANDLE hClipText;
517 if (IsClipboardFormatAvailable(CF_TEXT)) {
518 OpenClipboard(Globals.hMainWnd);
519 hClipText = GetClipboardData(CF_TEXT);
520 CloseClipboard();
521 MessageBox(Globals.hMainWnd, (CHAR *)hClipText, "PASTE", MB_ICONEXCLAMATION);
525 VOID DIALOG_EditDelete(VOID)
527 /* Delete */
530 VOID DIALOG_EditSelectAll(VOID)
532 /* Select all */
535 VOID DIALOG_EditTimeDate(VOID)
537 SYSTEMTIME st;
538 LPSYSTEMTIME lpst = &st;
539 CHAR szDate[MAX_STRING_LEN];
540 LPSTR date = szDate;
542 GetLocalTime(&st);
543 GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, lpst, NULL, date, MAX_STRING_LEN);
544 GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, lpst, NULL, date, MAX_STRING_LEN);
548 VOID DIALOG_EditWrap(VOID)
550 Globals.bWrapLongLines = !Globals.bWrapLongLines;
551 CheckMenuItem(Globals.hEditMenu, 0x119, MF_BYCOMMAND |
552 (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
555 VOID DIALOG_Search(VOID)
557 Globals.find.lStructSize = sizeof(Globals.find);
558 Globals.find.hwndOwner = Globals.hMainWnd;
559 Globals.find.hInstance = Globals.hInstance;
560 Globals.find.lpstrFindWhat = (CHAR *) &Globals.szFindText;
561 Globals.find.wFindWhatLen = sizeof(Globals.szFindText);
562 Globals.find.lpstrReplaceWith = 0;
563 Globals.find.wReplaceWithLen = 0;
564 Globals.find.Flags = FR_DOWN;
565 Globals.find.lCustData = 0;
566 Globals.find.lpfnHook = 0;
567 Globals.find.lpTemplateName = 0;
569 /* We only need to create the modal FindReplace dialog which will */
570 /* notify us of incoming events using hMainWnd Window Messages */
572 Globals.hFindReplaceDlg = FindText(&Globals.find);
573 assert(Globals.hFindReplaceDlg !=0);
576 VOID DIALOG_SearchNext(VOID)
578 /* Search Next */
581 VOID DIALOG_HelpContents(VOID)
583 WinHelp(Globals.hMainWnd, HELPFILE, HELP_INDEX, 0);
586 VOID DIALOG_HelpSearch(VOID)
588 /* Search Help */
591 VOID DIALOG_HelpHelp(VOID)
593 WinHelp(Globals.hMainWnd, HELPFILE, HELP_HELPONHELP, 0);
596 VOID DIALOG_HelpLicense(VOID)
598 WineLicense(Globals.hMainWnd);
601 VOID DIALOG_HelpNoWarranty(VOID)
603 WineWarranty(Globals.hMainWnd);
606 VOID DIALOG_HelpAboutWine(VOID)
608 CHAR szNotepad[MAX_STRING_LEN];
610 LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, sizeof(szNotepad));
611 ShellAbout(Globals.hMainWnd, szNotepad, "Notepad\n" WINE_RELEASE_INFO, 0);
614 /***********************************************************************
616 * DIALOG_PageSetup
619 VOID DIALOG_PageSetup(VOID)
621 WNDPROC lpfnDlg;
623 lpfnDlg = MakeProcInstance(DIALOG_PAGESETUP_DlgProc, Globals.hInstance);
624 DialogBox(Globals.hInstance, STRING_PAGESETUP_Xx, Globals.hMainWnd, (DLGPROC)lpfnDlg);
625 FreeProcInstance(lpfnDlg);
629 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
631 * DIALOG_PAGESETUP_DlgProc
634 static LRESULT WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
637 switch (msg)
639 case WM_COMMAND:
640 switch (wParam)
642 case IDOK:
643 /* save user input and close dialog */
644 GetDlgItemText(hDlg, 0x141, Globals.szHeader, sizeof(Globals.szHeader));
645 GetDlgItemText(hDlg, 0x143, Globals.szFooter, sizeof(Globals.szFooter));
646 GetDlgItemText(hDlg, 0x14A, Globals.szMarginTop, sizeof(Globals.szMarginTop));
647 GetDlgItemText(hDlg, 0x150, Globals.szMarginBottom, sizeof(Globals.szMarginBottom));
648 GetDlgItemText(hDlg, 0x147, Globals.szMarginLeft, sizeof(Globals.szMarginLeft));
649 GetDlgItemText(hDlg, 0x14D, Globals.szMarginRight, sizeof(Globals.szMarginRight));
650 EndDialog(hDlg, IDOK);
651 return TRUE;
653 case IDCANCEL:
654 /* discard user input and close dialog */
655 EndDialog(hDlg, IDCANCEL);
656 return TRUE;
658 case IDHELP:
659 /* FIXME: Bring this to work */
660 MessageBox(Globals.hMainWnd, "Sorry, no help available", "Help", MB_ICONEXCLAMATION);
661 return TRUE;
663 break;
665 case WM_INITDIALOG:
666 /* fetch last user input prior to display dialog */
667 SetDlgItemText(hDlg, 0x141, Globals.szHeader);
668 SetDlgItemText(hDlg, 0x143, Globals.szFooter);
669 SetDlgItemText(hDlg, 0x14A, Globals.szMarginTop);
670 SetDlgItemText(hDlg, 0x150, Globals.szMarginBottom);
671 SetDlgItemText(hDlg, 0x147, Globals.szMarginLeft);
672 SetDlgItemText(hDlg, 0x14D, Globals.szMarginRight);
673 break;
676 return FALSE;