Fixed some warnings.
[wine/testsucceed.git] / programs / notepad / dialog.c
blob8d237e486617398fbe632360660c750cbd5b60cf
1 /*
2 * Notepad (dialog.c)
4 * Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
5 * To be distributed under the Wine License
6 */
8 #include <assert.h>
9 #include <stdio.h>
10 #include <windows.h>
11 #include <commdlg.h>
12 #include <winerror.h>
14 #ifdef WINELIB
15 #include "shell.h"
16 #include "options.h"
17 #endif
19 #include "main.h"
20 #include "license.h"
21 #include "language.h"
22 #include "dialog.h"
24 #ifdef LCC
25 #define LCC_HASASSERT
26 #include "lcc.h"
27 #else
28 #include "version.h"
29 #include "winnls.h"
30 #endif
32 static LRESULT DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
36 int AlertIDS(UINT ids_message, UINT ids_caption, WORD type) {
38 * Given some ids strings, this acts as a language-aware wrapper for
39 * "MessageBox"
41 CHAR szMessage[MAX_STRING_LEN];
42 CHAR szCaption[MAX_STRING_LEN];
44 LoadString(Globals.hInstance, ids_message, szMessage, sizeof(szMessage));
45 LoadString(Globals.hInstance, ids_caption, szCaption, sizeof(szCaption));
47 return (MessageBox(Globals.hMainWnd, szMessage, szCaption, type));
50 void AlertFileNotFound(LPSTR szFileName) {
52 int nResult;
53 CHAR szMessage[MAX_STRING_LEN];
54 CHAR szRessource[MAX_STRING_LEN];
56 /* Load and format szMessage */
57 LoadString(Globals.hInstance, IDS_NOTFOUND, szRessource, sizeof(szRessource));
58 wvsprintf(szMessage, szRessource, szFileName);
60 /* Load szCaption */
61 LoadString(Globals.hInstance, IDS_ERROR, szRessource, sizeof(szRessource));
63 /* Display Modal Dialog */
64 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION);
68 int AlertFileNotSaved(LPSTR szFileName) {
70 int nResult;
71 CHAR szMessage[MAX_STRING_LEN];
72 CHAR szRessource[MAX_STRING_LEN];
74 /* Load and format Message */
76 LoadString(Globals.hInstance, IDS_NOTSAVED, szRessource, sizeof(szRessource));
77 wvsprintf(szMessage, szRessource, szFileName);
79 /* Load Caption */
81 LoadString(Globals.hInstance, IDS_ERROR, szRessource, sizeof(szRessource));
83 /* Display modal */
84 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION + MB_YESNOCANCEL);
85 return(nResult);
89 VOID AlertOutOfMemory(void) {
90 int nResult;
92 nResult = AlertIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_ICONEXCLAMATION);
93 PostQuitMessage(1);
97 BOOL ExistFile(LPCSTR szFilename) {
99 * Returns: TRUE - if "szFileName" exists
100 * FALSE - if it does not
102 WIN32_FIND_DATA entry;
103 HANDLE hFile;
105 hFile = FindFirstFile(szFilename, &entry);
107 return (hFile!=INVALID_HANDLE_VALUE);
110 VOID DoSaveFile(VOID) {
112 /* FIXME: Really Save the file */
113 /* ... (Globals.szFileName); */
117 BOOL DoCloseFile(void) {
118 /* Return value: TRUE - User agreed to close (both save/don't save) */
119 /* FALSE - User cancelled close by selecting "Cancel" */
121 int nResult;
123 if (strlen(Globals.szFileName)>0) {
124 /* prompt user to save changes */
125 nResult = AlertFileNotSaved(Globals.szFileName);
126 switch (nResult) {
127 case IDYES: DoSaveFile();
128 break;
130 case IDNO: break;
132 case IDCANCEL: return(FALSE);
133 break;
135 default: return(FALSE);
136 break;
137 } /* switch */
138 } /* if */
140 /* Forget file name */
141 lstrcpy(Globals.szFileName, "");
142 LANGUAGE_UpdateWindowCaption();
143 return(TRUE);
147 void DoOpenFile(LPCSTR szFileName) {
149 int hFile;
150 WORD nResult;
152 /* Close any files and prompt to save changes */
153 if (DoCloseFile()) {
154 GetFileTitle(szFileName, Globals.szFileName, sizeof(Globals.szFileName));
155 LANGUAGE_UpdateWindowCaption();
156 hFile = _lopen(szFileName, OF_READ);
157 nResult = _lread(hFile, Globals.Buffer, sizeof(Globals.Buffer));
158 _lclose(hFile);
160 /* FIXME: Append time/date if first line contains LOGPREFIX */
161 /* (Globals.Buffer, ) */
166 VOID DIALOG_FileNew(VOID)
168 /* Close any files and promt to save changes */
169 if (DoCloseFile()) {
170 /* do nothing yet */
174 VOID DIALOG_FileOpen(VOID)
176 OPENFILENAME openfilename;
177 CHAR szPath[MAX_PATHNAME_LEN];
178 CHAR szDir[MAX_PATHNAME_LEN];
179 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
180 CHAR szDefaultExt[4];
181 LPSTR p = szzFilter;
183 lstrcpy(szDefaultExt, "txt");
185 LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
186 p += strlen(p) + 1;
187 lstrcpy(p, "*.txt");
188 p += strlen(p) + 1;
189 LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
190 p += strlen(p) + 1;
191 lstrcpy(p, "*.*");
192 p += strlen(p) + 1;
193 *p = '\0';
195 GetCurrentDirectory(sizeof(szDir), szDir);
196 lstrcpy(szPath,"*.txt");
198 openfilename.lStructSize = sizeof(OPENFILENAME);
199 openfilename.hwndOwner = Globals.hMainWnd;
200 openfilename.hInstance = Globals.hInstance;
201 openfilename.lpstrFilter = szzFilter;
202 openfilename.lpstrCustomFilter = 0;
203 openfilename.nMaxCustFilter = 0;
204 openfilename.nFilterIndex = 0;
205 openfilename.lpstrFile = szPath;
206 openfilename.nMaxFile = sizeof(szPath);
207 openfilename.lpstrFileTitle = 0;
208 openfilename.nMaxFileTitle = 0;
209 openfilename.lpstrInitialDir = szDir;
210 openfilename.lpstrTitle = 0;
211 openfilename.Flags = OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST;
212 openfilename.nFileOffset = 0;
213 openfilename.nFileExtension = 0;
214 openfilename.lpstrDefExt = szDefaultExt;
215 openfilename.lCustData = 0;
216 openfilename.lpfnHook = 0;
217 openfilename.lpTemplateName = 0;
219 if (GetOpenFileName(&openfilename)) {
221 if (ExistFile(openfilename.lpstrFile))
222 DoOpenFile(openfilename.lpstrFile);
223 else
224 AlertFileNotFound(openfilename.lpstrFile);
229 VOID DIALOG_FileSave(VOID)
231 /* FIXME: Save File */
233 DIALOG_FileSaveAs();
236 VOID DIALOG_FileSaveAs(VOID)
238 OPENFILENAME saveas;
239 CHAR szPath[MAX_PATHNAME_LEN];
240 CHAR szDir[MAX_PATHNAME_LEN];
241 CHAR szDefaultExt[4];
242 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
244 LPSTR p = szzFilter;
246 lstrcpy(szDefaultExt, "txt");
248 LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
249 p += strlen(p) + 1;
250 lstrcpy(p, "*.txt");
251 p += strlen(p) + 1;
252 LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
253 p += strlen(p) + 1;
254 lstrcpy(p, "*.*");
255 p += strlen(p) + 1;
256 *p = '\0';
258 lstrcpy(szPath,"*.*");
260 GetCurrentDirectory(sizeof(szDir), szDir);
262 saveas.lStructSize = sizeof(OPENFILENAME);
263 saveas.hwndOwner = Globals.hMainWnd;
264 saveas.hInstance = Globals.hInstance;
265 saveas.lpstrFilter = szzFilter;
266 saveas.lpstrCustomFilter = 0;
267 saveas.nMaxCustFilter = 0;
268 saveas.nFilterIndex = 0;
269 saveas.lpstrFile = szPath;
270 saveas.nMaxFile = sizeof(szPath);
271 saveas.lpstrFileTitle = 0;
272 saveas.nMaxFileTitle = 0;
273 saveas.lpstrInitialDir = szDir;
274 saveas.lpstrTitle = 0;
275 saveas.Flags = OFN_PATHMUSTEXIST + OFN_OVERWRITEPROMPT + OFN_HIDEREADONLY;
276 saveas.nFileOffset = 0;
277 saveas.nFileExtension = 0;
278 saveas.lpstrDefExt = szDefaultExt;
279 saveas.lCustData = 0;
280 saveas.lpfnHook = 0;
281 saveas.lpTemplateName = 0;
283 if (GetSaveFileName(&saveas)) {
284 lstrcpy(Globals.szFileName, saveas.lpstrFile);
285 LANGUAGE_UpdateWindowCaption();
286 DIALOG_FileSave();
290 VOID DIALOG_FilePrint(VOID)
292 LONG bFlags, nBase;
293 WORD nOffset;
294 DOCINFO di;
295 int nResult;
296 HDC hContext;
297 PRINTDLG printer;
299 CHAR szDocumentName[MAX_STRING_LEN]; /* Name of document */
300 CHAR szPrinterName[MAX_STRING_LEN]; /* Name of the printer */
301 CHAR szDeviceName[MAX_STRING_LEN]; /* Name of the printer device */
302 CHAR szOutput[MAX_STRING_LEN]; /* in which file/device to print */
304 /* LPDEVMODE hDevMode; */
305 /* LPDEVNAMES hDevNames; */
307 /* hDevMode = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVMODE)); */
308 /* hDevNames = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVNAMES)); */
310 /* Get Current Settings */
312 printer.lStructSize = sizeof(PRINTDLG);
313 printer.hwndOwner = Globals.hMainWnd;
314 printer.hInstance = Globals.hInstance;
316 /* Let PrintDlg create a DEVMODE structure */
317 printer.hDevMode = 0;
318 printer.hDevNames = 0;
319 printer.hDC = 0;
320 printer.Flags = PD_RETURNDEFAULT;
321 printer.nFromPage = 0;
322 printer.nToPage = 0;
323 printer.nMinPage = 0;
324 printer.nMaxPage = 0;
325 printer.nCopies = 0;
326 printer.lCustData = 0;
327 printer.lpfnPrintHook = 0;
328 printer.lpfnSetupHook = 0;
329 printer.lpPrintTemplateName = 0;
330 printer.lpSetupTemplateName = 0;
331 printer.hPrintTemplate = 0;
332 printer.hSetupTemplate = 0;
334 nResult = PrintDlg(&printer);
336 /* hContext = CreateDC(, szDeviceName, "TEST.TXT", 0); */
338 /* Congratulations to those Microsoft Engineers responsable */
339 /* for the following pointer acrobatics */
341 assert(printer.hDevNames!=0);
343 nBase = (LONG)(printer.hDevNames);
345 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDriverOffset;
346 lstrcpy(szPrinterName, (LPCSTR) (nBase + nOffset));
348 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDeviceOffset;
349 lstrcpy(szDeviceName, (LPCSTR) (nBase + nOffset));
351 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wOutputOffset;
352 lstrcpy(szOutput, (LPCSTR) (nBase + nOffset));
354 MessageBox(Globals.hMainWnd, szPrinterName, "Printer Name", MB_ICONEXCLAMATION);
355 MessageBox(Globals.hMainWnd, szDeviceName, "Device Name", MB_ICONEXCLAMATION);
356 MessageBox(Globals.hMainWnd, szOutput, "Output", MB_ICONEXCLAMATION);
358 /* Set some default flags */
360 bFlags = PD_RETURNDC + PD_SHOWHELP;
362 if (TRUE) {
363 /* Remove "Print Selection" if there is no selection */
364 bFlags = bFlags + PD_NOSELECTION;
367 printer.Flags = bFlags;
369 printer.nFromPage = 0;
370 printer.nToPage = 0;
371 printer.nMinPage = 0;
372 printer.nMaxPage = 0;
375 /* Let commdlg manage copy settings */
376 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
378 if (PrintDlg(&printer)) {
380 /* initialize DOCINFO */
381 di.cbSize = sizeof(DOCINFO);
382 lstrcpy((LPSTR)di.lpszDocName, szDocumentName);
383 lstrcpy((LPSTR)di.lpszOutput, szOutput);
385 hContext = printer.hDC;
386 assert(hContext!=0);
387 assert( (int) hContext!=PD_RETURNDC);
389 SetMapMode(hContext, MM_LOMETRIC);
390 /* SetViewPortExExt(hContext, 10, 10, 0); */
391 SetBkMode(hContext, OPAQUE);
393 nResult = TextOut(hContext, 0, 0, " ", 1);
394 assert(nResult != 0);
396 nResult = StartDoc(hContext, &di);
397 assert(nResult != SP_ERROR);
399 nResult = StartPage(hContext);
400 assert(nResult >0);
402 /* FIXME: actually print */
404 nResult = EndPage(hContext);
406 switch (nResult) {
407 case SP_ERROR:
408 MessageBox(Globals.hMainWnd, "Generic Error", "Print Engine Error", MB_ICONEXCLAMATION);
409 break;
410 case SP_APPABORT:
411 MessageBox(Globals.hMainWnd, "The print job was aborted.", "Print Engine Error", MB_ICONEXCLAMATION);
412 break;
413 case SP_USERABORT:
414 MessageBox(Globals.hMainWnd, "The print job was aborted using the Print Manager ", "Print Engine Error", MB_ICONEXCLAMATION);
415 break;
416 case SP_OUTOFDISK:
417 MessageBox(Globals.hMainWnd, "Out of disk space", "Print Engine Error", MB_ICONEXCLAMATION);
418 break;
419 case SP_OUTOFMEMORY:
420 AlertOutOfMemory();
421 break;
422 default:
423 MessageBox(Globals.hMainWnd, "Default", "Print", MB_ICONEXCLAMATION);
424 } /* switch */
425 nResult = EndDoc(hContext);
426 assert(nResult>=0);
427 nResult = DeleteDC(hContext);
428 assert(nResult!=0);
429 } /* if */
431 /* GlobalFree(hDevNames); */
432 /* GlobalFree(hDevMode); */
435 VOID DIALOG_FilePageSetup(VOID)
437 DIALOG_PageSetup();
440 VOID DIALOG_FilePrinterSetup(VOID)
442 PRINTDLG printer;
444 printer.lStructSize = sizeof(PRINTDLG);
445 printer.hwndOwner = Globals.hMainWnd;
446 printer.hInstance = Globals.hInstance;
447 printer.hDevMode = 0;
448 printer.hDevNames = 0;
449 printer.hDC = 0;
450 printer.Flags = PD_PRINTSETUP;
451 printer.nFromPage = 0;
452 printer.nToPage = 0;
453 printer.nMinPage = 0;
454 printer.nMaxPage = 0;
455 printer.nCopies = 1;
456 printer.lCustData = 0;
457 printer.lpfnPrintHook = 0;
458 printer.lpfnSetupHook = 0;
459 printer.lpPrintTemplateName = 0;
460 printer.lpSetupTemplateName = 0;
461 printer.hPrintTemplate = 0;
462 printer.hSetupTemplate = 0;
464 if (PrintDlg(&printer)) {
465 /* do nothing */
470 VOID DIALOG_FileExit(VOID)
472 if (DoCloseFile()) {
473 PostQuitMessage(0);
477 VOID DIALOG_EditUndo(VOID)
479 MessageBox(Globals.hMainWnd, "Undo", "Debug", MB_ICONEXCLAMATION);
480 /* undo */
483 VOID DIALOG_EditCut(VOID)
485 HANDLE hMem;
487 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
489 OpenClipboard(Globals.hMainWnd);
490 EmptyClipboard();
492 /* FIXME: Get text */
493 lstrcpy((CHAR *)hMem, "Hello World");
495 SetClipboardData(CF_TEXT, hMem);
496 CloseClipboard();
498 GlobalFree(hMem);
501 VOID DIALOG_EditCopy(VOID)
503 HANDLE hMem;
505 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
507 OpenClipboard(Globals.hMainWnd);
508 EmptyClipboard();
510 /* FIXME: Get text */
511 lstrcpy((CHAR *)hMem, "Hello World");
513 SetClipboardData(CF_TEXT, hMem);
514 CloseClipboard();
516 GlobalFree(hMem);
519 VOID DIALOG_EditPaste(VOID)
521 HANDLE hClipText;
523 if (IsClipboardFormatAvailable(CF_TEXT)) {
524 OpenClipboard(Globals.hMainWnd);
525 hClipText = GetClipboardData(CF_TEXT);
526 CloseClipboard();
527 MessageBox(Globals.hMainWnd, (CHAR *)hClipText, "PASTE", MB_ICONEXCLAMATION);
531 VOID DIALOG_EditDelete(VOID)
533 /* Delete */
536 VOID DIALOG_EditSelectAll(VOID)
538 /* Select all */
541 VOID DIALOG_EditTimeDate(VOID)
543 SYSTEMTIME st;
544 LPSYSTEMTIME lpst = &st;
545 CHAR szDate[MAX_STRING_LEN];
546 LPSTR date = szDate;
548 GetLocalTime(&st);
549 GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, lpst, NULL, date, MAX_STRING_LEN);
550 GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, lpst, NULL, date, MAX_STRING_LEN);
554 VOID DIALOG_EditWrap(VOID)
556 Globals.bWrapLongLines = !Globals.bWrapLongLines;
557 CheckMenuItem(Globals.hEditMenu, NP_EDIT_WRAP, MF_BYCOMMAND |
558 (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
561 VOID DIALOG_Search(VOID)
563 Globals.find.lStructSize = sizeof(Globals.find);
564 Globals.find.hwndOwner = Globals.hMainWnd;
565 Globals.find.hInstance = Globals.hInstance;
566 Globals.find.lpstrFindWhat = (CHAR *) &Globals.szFindText;
567 Globals.find.wFindWhatLen = sizeof(Globals.szFindText);
568 Globals.find.lpstrReplaceWith = 0;
569 Globals.find.wReplaceWithLen = 0;
570 Globals.find.Flags = FR_DOWN;
571 Globals.find.lCustData = 0;
572 Globals.find.lpfnHook = 0;
573 Globals.find.lpTemplateName = 0;
575 /* We only need to create the modal FindReplace dialog which will */
576 /* notify us of incoming events using hMainWnd Window Messages */
578 Globals.hFindReplaceDlg = FindText(&Globals.find);
579 assert(Globals.hFindReplaceDlg !=0);
582 VOID DIALOG_SearchNext(VOID)
584 /* Search Next */
587 VOID DIALOG_HelpContents(VOID)
589 WinHelp(Globals.hMainWnd, HELPFILE, HELP_INDEX, 0);
592 VOID DIALOG_HelpSearch(VOID)
594 /* Search Help */
597 VOID DIALOG_HelpHelp(VOID)
599 WinHelp(Globals.hMainWnd, HELPFILE, HELP_HELPONHELP, 0);
602 VOID DIALOG_HelpLicense(VOID)
604 WineLicense(Globals.hMainWnd, Globals.lpszLanguage);
607 VOID DIALOG_HelpNoWarranty(VOID)
609 WineWarranty(Globals.hMainWnd, Globals.lpszLanguage);
612 VOID DIALOG_HelpAboutWine(VOID)
614 CHAR szNotepad[MAX_STRING_LEN];
616 LoadString(Globals.hInstance, IDS_NOTEPAD, szNotepad, sizeof(szNotepad));
617 ShellAbout(Globals.hMainWnd, szNotepad, "Notepad\n" WINE_RELEASE_INFO, 0);
620 /***********************************************************************
622 * DIALOG_PageSetup
625 VOID DIALOG_PageSetup(VOID)
627 WNDPROC lpfnDlg;
629 lpfnDlg = MakeProcInstance(DIALOG_PAGESETUP_DlgProc, Globals.hInstance);
630 DialogBox(Globals.hInstance, STRING_PAGESETUP_Xx, Globals.hMainWnd, (DLGPROC)lpfnDlg);
631 FreeProcInstance(lpfnDlg);
635 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
637 * DIALOG_PAGESETUP_DlgProc
640 static LRESULT DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
643 switch (msg)
645 case WM_COMMAND:
646 switch (wParam)
648 case IDOK:
649 /* save user input and close dialog */
650 GetDlgItemText(hDlg, NP_PAGESETUP_HEAD, Globals.szHeader, sizeof(Globals.szHeader));
651 GetDlgItemText(hDlg, NP_PAGESETUP_TAIL, Globals.szFooter, sizeof(Globals.szFooter));
652 GetDlgItemText(hDlg, NP_PAGESETUP_TOP, Globals.szMarginTop, sizeof(Globals.szMarginTop));
653 GetDlgItemText(hDlg, NP_PAGESETUP_BOTTOM, Globals.szMarginBottom, sizeof(Globals.szMarginBottom));
654 GetDlgItemText(hDlg, NP_PAGESETUP_LEFT, Globals.szMarginLeft, sizeof(Globals.szMarginLeft));
655 GetDlgItemText(hDlg, NP_PAGESETUP_RIGHT, Globals.szMarginRight, sizeof(Globals.szMarginRight));
656 EndDialog(hDlg, IDOK);
657 return TRUE;
659 case IDCANCEL:
660 /* discard user input and close dialog */
661 EndDialog(hDlg, IDCANCEL);
662 return TRUE;
664 case IDHELP:
665 /* FIXME: Bring this to work */
666 MessageBox(Globals.hMainWnd, "Sorry, no help available", "Help", MB_ICONEXCLAMATION);
667 return TRUE;
669 break;
671 case WM_INITDIALOG:
672 /* fetch last user input prior to display dialog */
673 SetDlgItemText(hDlg, NP_PAGESETUP_HEAD, Globals.szHeader);
674 SetDlgItemText(hDlg, NP_PAGESETUP_TAIL, Globals.szFooter);
675 SetDlgItemText(hDlg, NP_PAGESETUP_TOP, Globals.szMarginTop);
676 SetDlgItemText(hDlg, NP_PAGESETUP_BOTTOM, Globals.szMarginBottom);
677 SetDlgItemText(hDlg, NP_PAGESETUP_LEFT, Globals.szMarginLeft);
678 SetDlgItemText(hDlg, NP_PAGESETUP_RIGHT, Globals.szMarginRight);
679 break;
682 return FALSE;