Fixed a few prototypes.
[wine/dcerpc.git] / programs / notepad / dialog.c
blob9dd5a0280055f429bb6cdbd788f6e08d011b7d6e
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 "options.h"
16 #endif
18 #include "main.h"
19 #include "license.h"
20 #include "language.h"
21 #include "dialog.h"
23 #ifdef LCC
24 #define LCC_HASASSERT
25 #include "lcc.h"
26 #else
27 #include "version.h"
28 #include "winnls.h"
29 #endif
31 static LRESULT DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
35 int AlertIDS(UINT ids_message, UINT ids_caption, WORD type) {
37 * Given some ids strings, this acts as a language-aware wrapper for
38 * "MessageBox"
40 CHAR szMessage[MAX_STRING_LEN];
41 CHAR szCaption[MAX_STRING_LEN];
43 LoadString(Globals.hInstance, ids_message, szMessage, sizeof(szMessage));
44 LoadString(Globals.hInstance, ids_caption, szCaption, sizeof(szCaption));
46 return (MessageBox(Globals.hMainWnd, szMessage, szCaption, type));
49 void AlertFileNotFound(LPSTR szFileName) {
51 int nResult;
52 CHAR szMessage[MAX_STRING_LEN];
53 CHAR szRessource[MAX_STRING_LEN];
55 /* Load and format szMessage */
56 LoadString(Globals.hInstance, IDS_NOTFOUND, szRessource, sizeof(szRessource));
57 wsprintf(szMessage, szRessource, szFileName);
59 /* Load szCaption */
60 LoadString(Globals.hInstance, IDS_ERROR, szRessource, sizeof(szRessource));
62 /* Display Modal Dialog */
63 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION);
67 int AlertFileNotSaved(LPSTR szFileName) {
69 int nResult;
70 CHAR szMessage[MAX_STRING_LEN];
71 CHAR szRessource[MAX_STRING_LEN];
73 /* Load and format Message */
75 LoadString(Globals.hInstance, IDS_NOTSAVED, szRessource, sizeof(szRessource));
76 wsprintf(szMessage, szRessource, szFileName);
78 /* Load Caption */
80 LoadString(Globals.hInstance, IDS_ERROR, szRessource, sizeof(szRessource));
82 /* Display modal */
83 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION + MB_YESNOCANCEL);
84 return(nResult);
88 VOID AlertOutOfMemory(void) {
89 int nResult;
91 nResult = AlertIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_ICONEXCLAMATION);
92 PostQuitMessage(1);
96 BOOL ExistFile(LPCSTR szFilename) {
98 * Returns: TRUE - if "szFileName" exists
99 * FALSE - if it does not
101 WIN32_FIND_DATA entry;
102 HANDLE hFile;
104 hFile = FindFirstFile(szFilename, &entry);
106 return (hFile!=INVALID_HANDLE_VALUE);
109 VOID DoSaveFile(VOID) {
111 /* FIXME: Really Save the file */
112 /* ... (Globals.szFileName); */
116 BOOL DoCloseFile(void) {
117 /* Return value: TRUE - User agreed to close (both save/don't save) */
118 /* FALSE - User cancelled close by selecting "Cancel" */
120 int nResult;
122 if (strlen(Globals.szFileName)>0) {
123 /* prompt user to save changes */
124 nResult = AlertFileNotSaved(Globals.szFileName);
125 switch (nResult) {
126 case IDYES: DoSaveFile();
127 break;
129 case IDNO: break;
131 case IDCANCEL: return(FALSE);
132 break;
134 default: return(FALSE);
135 break;
136 } /* switch */
137 } /* if */
139 /* Forget file name */
140 lstrcpy(Globals.szFileName, "");
141 LANGUAGE_UpdateWindowCaption();
142 return(TRUE);
146 void DoOpenFile(LPCSTR szFileName) {
148 /* Close any files and prompt to save changes */
149 if (DoCloseFile()) {
150 GetFileTitle(szFileName, Globals.szFileName, sizeof(Globals.szFileName));
151 LANGUAGE_UpdateWindowCaption();
153 LoadBufferFromFile(szFileName);
158 VOID DIALOG_FileNew(VOID)
160 /* Close any files and promt to save changes */
161 if (DoCloseFile()) {
162 TrashBuffer();
166 VOID DIALOG_FileOpen(VOID)
168 OPENFILENAME openfilename;
169 CHAR szPath[MAX_PATHNAME_LEN];
170 CHAR szDir[MAX_PATHNAME_LEN];
171 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
172 CHAR szDefaultExt[4];
173 LPSTR p = szzFilter;
175 lstrcpy(szDefaultExt, "txt");
177 LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
178 p += strlen(p) + 1;
179 lstrcpy(p, "*.txt");
180 p += strlen(p) + 1;
181 LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
182 p += strlen(p) + 1;
183 lstrcpy(p, "*.*");
184 p += strlen(p) + 1;
185 *p = '\0';
187 GetCurrentDirectory(sizeof(szDir), szDir);
188 lstrcpy(szPath,"*.txt");
190 openfilename.lStructSize = sizeof(OPENFILENAME);
191 openfilename.hwndOwner = Globals.hMainWnd;
192 openfilename.hInstance = Globals.hInstance;
193 openfilename.lpstrFilter = szzFilter;
194 openfilename.lpstrCustomFilter = 0;
195 openfilename.nMaxCustFilter = 0;
196 openfilename.nFilterIndex = 0;
197 openfilename.lpstrFile = szPath;
198 openfilename.nMaxFile = sizeof(szPath);
199 openfilename.lpstrFileTitle = 0;
200 openfilename.nMaxFileTitle = 0;
201 openfilename.lpstrInitialDir = szDir;
202 openfilename.lpstrTitle = 0;
203 openfilename.Flags = OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST;
204 openfilename.nFileOffset = 0;
205 openfilename.nFileExtension = 0;
206 openfilename.lpstrDefExt = szDefaultExt;
207 openfilename.lCustData = 0;
208 openfilename.lpfnHook = 0;
209 openfilename.lpTemplateName = 0;
211 if (GetOpenFileName(&openfilename)) {
213 if (ExistFile(openfilename.lpstrFile))
214 DoOpenFile(openfilename.lpstrFile);
215 else
216 AlertFileNotFound(openfilename.lpstrFile);
221 VOID DIALOG_FileSave(VOID)
223 /* FIXME: Save File */
225 DIALOG_FileSaveAs();
228 VOID DIALOG_FileSaveAs(VOID)
230 OPENFILENAME saveas;
231 CHAR szPath[MAX_PATHNAME_LEN];
232 CHAR szDir[MAX_PATHNAME_LEN];
233 CHAR szDefaultExt[4];
234 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
236 LPSTR p = szzFilter;
238 lstrcpy(szDefaultExt, "txt");
240 LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
241 p += strlen(p) + 1;
242 lstrcpy(p, "*.txt");
243 p += strlen(p) + 1;
244 LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
245 p += strlen(p) + 1;
246 lstrcpy(p, "*.*");
247 p += strlen(p) + 1;
248 *p = '\0';
250 lstrcpy(szPath,"*.*");
252 GetCurrentDirectory(sizeof(szDir), szDir);
254 saveas.lStructSize = sizeof(OPENFILENAME);
255 saveas.hwndOwner = Globals.hMainWnd;
256 saveas.hInstance = Globals.hInstance;
257 saveas.lpstrFilter = szzFilter;
258 saveas.lpstrCustomFilter = 0;
259 saveas.nMaxCustFilter = 0;
260 saveas.nFilterIndex = 0;
261 saveas.lpstrFile = szPath;
262 saveas.nMaxFile = sizeof(szPath);
263 saveas.lpstrFileTitle = 0;
264 saveas.nMaxFileTitle = 0;
265 saveas.lpstrInitialDir = szDir;
266 saveas.lpstrTitle = 0;
267 saveas.Flags = OFN_PATHMUSTEXIST + OFN_OVERWRITEPROMPT + OFN_HIDEREADONLY;
268 saveas.nFileOffset = 0;
269 saveas.nFileExtension = 0;
270 saveas.lpstrDefExt = szDefaultExt;
271 saveas.lCustData = 0;
272 saveas.lpfnHook = 0;
273 saveas.lpTemplateName = 0;
275 if (GetSaveFileName(&saveas)) {
276 lstrcpy(Globals.szFileName, saveas.lpstrFile);
277 LANGUAGE_UpdateWindowCaption();
278 DIALOG_FileSave();
282 VOID DIALOG_FilePrint(VOID)
284 LONG bFlags, nBase;
285 WORD nOffset;
286 DOCINFO di;
287 int nResult;
288 HDC hContext;
289 PRINTDLG printer;
291 CHAR szDocumentName[MAX_STRING_LEN]; /* Name of document */
292 CHAR szPrinterName[MAX_STRING_LEN]; /* Name of the printer */
293 CHAR szDeviceName[MAX_STRING_LEN]; /* Name of the printer device */
294 CHAR szOutput[MAX_STRING_LEN]; /* in which file/device to print */
296 /* LPDEVMODE hDevMode; */
297 /* LPDEVNAMES hDevNames; */
299 /* hDevMode = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVMODE)); */
300 /* hDevNames = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVNAMES)); */
302 /* Get Current Settings */
304 printer.lStructSize = sizeof(PRINTDLG);
305 printer.hwndOwner = Globals.hMainWnd;
306 printer.hInstance = Globals.hInstance;
308 /* Let PrintDlg create a DEVMODE structure */
309 printer.hDevMode = 0;
310 printer.hDevNames = 0;
311 printer.hDC = 0;
312 printer.Flags = PD_RETURNDEFAULT;
313 printer.nFromPage = 0;
314 printer.nToPage = 0;
315 printer.nMinPage = 0;
316 printer.nMaxPage = 0;
317 printer.nCopies = 0;
318 printer.lCustData = 0;
319 printer.lpfnPrintHook = 0;
320 printer.lpfnSetupHook = 0;
321 printer.lpPrintTemplateName = 0;
322 printer.lpSetupTemplateName = 0;
323 printer.hPrintTemplate = 0;
324 printer.hSetupTemplate = 0;
326 nResult = PrintDlg(&printer);
328 /* hContext = CreateDC(, szDeviceName, "TEST.TXT", 0); */
330 /* Congratulations to those Microsoft Engineers responsable */
331 /* for the following pointer acrobatics */
333 assert(printer.hDevNames!=0);
335 nBase = (LONG)(printer.hDevNames);
337 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDriverOffset;
338 lstrcpy(szPrinterName, (LPCSTR) (nBase + nOffset));
340 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDeviceOffset;
341 lstrcpy(szDeviceName, (LPCSTR) (nBase + nOffset));
343 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wOutputOffset;
344 lstrcpy(szOutput, (LPCSTR) (nBase + nOffset));
346 MessageBox(Globals.hMainWnd, szPrinterName, "Printer Name", MB_ICONEXCLAMATION);
347 MessageBox(Globals.hMainWnd, szDeviceName, "Device Name", MB_ICONEXCLAMATION);
348 MessageBox(Globals.hMainWnd, szOutput, "Output", MB_ICONEXCLAMATION);
350 /* Set some default flags */
352 bFlags = PD_RETURNDC + PD_SHOWHELP;
354 if (TRUE) {
355 /* Remove "Print Selection" if there is no selection */
356 bFlags = bFlags + PD_NOSELECTION;
359 printer.Flags = bFlags;
361 printer.nFromPage = 0;
362 printer.nToPage = 0;
363 printer.nMinPage = 0;
364 printer.nMaxPage = 0;
367 /* Let commdlg manage copy settings */
368 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
370 if (PrintDlg(&printer)) {
372 /* initialize DOCINFO */
373 di.cbSize = sizeof(DOCINFO);
374 lstrcpy((LPSTR)di.lpszDocName, szDocumentName);
375 lstrcpy((LPSTR)di.lpszOutput, szOutput);
377 hContext = printer.hDC;
378 assert(hContext!=0);
379 assert( (int) hContext!=PD_RETURNDC);
381 SetMapMode(hContext, MM_LOMETRIC);
382 /* SetViewPortExExt(hContext, 10, 10, 0); */
383 SetBkMode(hContext, OPAQUE);
385 nResult = TextOut(hContext, 0, 0, " ", 1);
386 assert(nResult != 0);
388 nResult = StartDoc(hContext, &di);
389 assert(nResult != SP_ERROR);
391 nResult = StartPage(hContext);
392 assert(nResult >0);
394 /* FIXME: actually print */
396 nResult = EndPage(hContext);
398 switch (nResult) {
399 case SP_ERROR:
400 MessageBox(Globals.hMainWnd, "Generic Error", "Print Engine Error", MB_ICONEXCLAMATION);
401 break;
402 case SP_APPABORT:
403 MessageBox(Globals.hMainWnd, "The print job was aborted.", "Print Engine Error", MB_ICONEXCLAMATION);
404 break;
405 case SP_USERABORT:
406 MessageBox(Globals.hMainWnd, "The print job was aborted using the Print Manager ", "Print Engine Error", MB_ICONEXCLAMATION);
407 break;
408 case SP_OUTOFDISK:
409 MessageBox(Globals.hMainWnd, "Out of disk space", "Print Engine Error", MB_ICONEXCLAMATION);
410 break;
411 case SP_OUTOFMEMORY:
412 AlertOutOfMemory();
413 break;
414 default:
415 MessageBox(Globals.hMainWnd, "Default", "Print", MB_ICONEXCLAMATION);
416 } /* switch */
417 nResult = EndDoc(hContext);
418 assert(nResult>=0);
419 nResult = DeleteDC(hContext);
420 assert(nResult!=0);
421 } /* if */
423 /* GlobalFree(hDevNames); */
424 /* GlobalFree(hDevMode); */
427 VOID DIALOG_FilePageSetup(VOID)
429 DIALOG_PageSetup();
432 VOID DIALOG_FilePrinterSetup(VOID)
434 PRINTDLG printer;
436 printer.lStructSize = sizeof(PRINTDLG);
437 printer.hwndOwner = Globals.hMainWnd;
438 printer.hInstance = Globals.hInstance;
439 printer.hDevMode = 0;
440 printer.hDevNames = 0;
441 printer.hDC = 0;
442 printer.Flags = PD_PRINTSETUP;
443 printer.nFromPage = 0;
444 printer.nToPage = 0;
445 printer.nMinPage = 0;
446 printer.nMaxPage = 0;
447 printer.nCopies = 1;
448 printer.lCustData = 0;
449 printer.lpfnPrintHook = 0;
450 printer.lpfnSetupHook = 0;
451 printer.lpPrintTemplateName = 0;
452 printer.lpSetupTemplateName = 0;
453 printer.hPrintTemplate = 0;
454 printer.hSetupTemplate = 0;
456 if (PrintDlg(&printer)) {
457 /* do nothing */
462 VOID DIALOG_FileExit(VOID)
464 if (DoCloseFile()) {
465 PostQuitMessage(0);
469 VOID DIALOG_EditUndo(VOID)
471 MessageBox(Globals.hMainWnd, "Undo", "Debug", MB_ICONEXCLAMATION);
472 /* undo */
475 VOID DIALOG_EditCut(VOID)
477 HANDLE hMem;
479 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
481 OpenClipboard(Globals.hMainWnd);
482 EmptyClipboard();
484 /* FIXME: Get text */
485 lstrcpy((CHAR *)hMem, "Hello World");
487 SetClipboardData(CF_TEXT, hMem);
488 CloseClipboard();
490 GlobalFree(hMem);
493 VOID DIALOG_EditCopy(VOID)
495 HANDLE hMem;
497 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
499 OpenClipboard(Globals.hMainWnd);
500 EmptyClipboard();
502 /* FIXME: Get text */
503 lstrcpy((CHAR *)hMem, "Hello World");
505 SetClipboardData(CF_TEXT, hMem);
506 CloseClipboard();
508 GlobalFree(hMem);
511 VOID DIALOG_EditPaste(VOID)
513 HANDLE hClipText;
515 if (IsClipboardFormatAvailable(CF_TEXT)) {
516 OpenClipboard(Globals.hMainWnd);
517 hClipText = GetClipboardData(CF_TEXT);
518 CloseClipboard();
519 MessageBox(Globals.hMainWnd, (CHAR *)hClipText, "PASTE", MB_ICONEXCLAMATION);
523 VOID DIALOG_EditDelete(VOID)
525 /* Delete */
528 VOID DIALOG_EditSelectAll(VOID)
530 /* Select all */
533 VOID DIALOG_EditTimeDate(VOID)
535 SYSTEMTIME st;
536 LPSYSTEMTIME lpst = &st;
537 CHAR szDate[MAX_STRING_LEN];
538 LPSTR date = szDate;
540 GetLocalTime(&st);
541 GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, lpst, NULL, date, MAX_STRING_LEN);
542 GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, lpst, NULL, date, MAX_STRING_LEN);
546 VOID DIALOG_EditWrap(VOID)
548 Globals.bWrapLongLines = !Globals.bWrapLongLines;
549 CheckMenuItem(Globals.hEditMenu, NP_EDIT_WRAP, MF_BYCOMMAND |
550 (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
553 VOID DIALOG_Search(VOID)
555 Globals.find.lStructSize = sizeof(Globals.find);
556 Globals.find.hwndOwner = Globals.hMainWnd;
557 Globals.find.hInstance = Globals.hInstance;
558 Globals.find.lpstrFindWhat = (CHAR *) &Globals.szFindText;
559 Globals.find.wFindWhatLen = sizeof(Globals.szFindText);
560 Globals.find.lpstrReplaceWith = 0;
561 Globals.find.wReplaceWithLen = 0;
562 Globals.find.Flags = FR_DOWN;
563 Globals.find.lCustData = 0;
564 Globals.find.lpfnHook = 0;
565 Globals.find.lpTemplateName = 0;
567 /* We only need to create the modal FindReplace dialog which will */
568 /* notify us of incoming events using hMainWnd Window Messages */
570 Globals.hFindReplaceDlg = FindText(&Globals.find);
571 assert(Globals.hFindReplaceDlg !=0);
574 VOID DIALOG_SearchNext(VOID)
576 /* Search Next */
579 VOID DIALOG_HelpContents(VOID)
581 WinHelp(Globals.hMainWnd, HELPFILE, HELP_INDEX, 0);
584 VOID DIALOG_HelpSearch(VOID)
586 /* Search Help */
589 VOID DIALOG_HelpHelp(VOID)
591 WinHelp(Globals.hMainWnd, HELPFILE, HELP_HELPONHELP, 0);
594 VOID DIALOG_HelpLicense(VOID)
596 WineLicense(Globals.hMainWnd, Globals.lpszLanguage);
599 VOID DIALOG_HelpNoWarranty(VOID)
601 WineWarranty(Globals.hMainWnd, Globals.lpszLanguage);
604 VOID DIALOG_HelpAboutWine(VOID)
606 CHAR szNotepad[MAX_STRING_LEN];
608 LoadString(Globals.hInstance, IDS_NOTEPAD, szNotepad, sizeof(szNotepad));
609 ShellAbout(Globals.hMainWnd, szNotepad, "Notepad\n" WINE_RELEASE_INFO, 0);
612 /***********************************************************************
614 * DIALOG_PageSetup
617 VOID DIALOG_PageSetup(VOID)
619 WNDPROC lpfnDlg;
621 lpfnDlg = MakeProcInstance(DIALOG_PAGESETUP_DlgProc, Globals.hInstance);
622 DialogBox(Globals.hInstance, STRING_PAGESETUP_Xx, Globals.hMainWnd, (DLGPROC)lpfnDlg);
623 FreeProcInstance(lpfnDlg);
627 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
629 * DIALOG_PAGESETUP_DlgProc
632 static LRESULT DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
635 switch (msg)
637 case WM_COMMAND:
638 switch (wParam)
640 case IDOK:
641 /* save user input and close dialog */
642 GetDlgItemText(hDlg, NP_PAGESETUP_HEAD, Globals.szHeader, sizeof(Globals.szHeader));
643 GetDlgItemText(hDlg, NP_PAGESETUP_TAIL, Globals.szFooter, sizeof(Globals.szFooter));
644 GetDlgItemText(hDlg, NP_PAGESETUP_TOP, Globals.szMarginTop, sizeof(Globals.szMarginTop));
645 GetDlgItemText(hDlg, NP_PAGESETUP_BOTTOM, Globals.szMarginBottom, sizeof(Globals.szMarginBottom));
646 GetDlgItemText(hDlg, NP_PAGESETUP_LEFT, Globals.szMarginLeft, sizeof(Globals.szMarginLeft));
647 GetDlgItemText(hDlg, NP_PAGESETUP_RIGHT, Globals.szMarginRight, sizeof(Globals.szMarginRight));
648 EndDialog(hDlg, IDOK);
649 return TRUE;
651 case IDCANCEL:
652 /* discard user input and close dialog */
653 EndDialog(hDlg, IDCANCEL);
654 return TRUE;
656 case IDHELP:
657 /* FIXME: Bring this to work */
658 MessageBox(Globals.hMainWnd, "Sorry, no help available", "Help", MB_ICONEXCLAMATION);
659 return TRUE;
661 break;
663 case WM_INITDIALOG:
664 /* fetch last user input prior to display dialog */
665 SetDlgItemText(hDlg, NP_PAGESETUP_HEAD, Globals.szHeader);
666 SetDlgItemText(hDlg, NP_PAGESETUP_TAIL, Globals.szFooter);
667 SetDlgItemText(hDlg, NP_PAGESETUP_TOP, Globals.szMarginTop);
668 SetDlgItemText(hDlg, NP_PAGESETUP_BOTTOM, Globals.szMarginBottom);
669 SetDlgItemText(hDlg, NP_PAGESETUP_LEFT, Globals.szMarginLeft);
670 SetDlgItemText(hDlg, NP_PAGESETUP_RIGHT, Globals.szMarginRight);
671 break;
674 return FALSE;