Added support for low-level mouse and keyboard hooks.
[wine/multimedia.git] / programs / notepad / dialog.c
blobab3c22174b229ca22e24d68e038a6ae68930e209
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 #include "main.h"
15 #include "license.h"
16 #include "language.h"
17 #include "dialog.h"
19 #ifdef LCC
20 #define LCC_HASASSERT
21 #include "lcc.h"
22 #else
23 #include "version.h"
24 #include "winnls.h"
25 #endif
27 static LRESULT WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
31 int AlertIDS(UINT ids_message, UINT ids_caption, WORD type) {
33 * Given some ids strings, this acts as a language-aware wrapper for
34 * "MessageBox"
36 CHAR szMessage[MAX_STRING_LEN];
37 CHAR szCaption[MAX_STRING_LEN];
39 LoadString(Globals.hInstance, ids_message, szMessage, sizeof(szMessage));
40 LoadString(Globals.hInstance, ids_caption, szCaption, sizeof(szCaption));
42 return (MessageBox(Globals.hMainWnd, szMessage, szCaption, type));
45 void AlertFileNotFound(LPSTR szFileName) {
47 int nResult;
48 CHAR szMessage[MAX_STRING_LEN];
49 CHAR szRessource[MAX_STRING_LEN];
51 /* Load and format szMessage */
52 LoadString(Globals.hInstance, IDS_NOTFOUND, szRessource, sizeof(szRessource));
53 wsprintf(szMessage, szRessource, szFileName);
55 /* Load szCaption */
56 LoadString(Globals.hInstance, IDS_ERROR, szRessource, sizeof(szRessource));
58 /* Display Modal Dialog */
59 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION);
63 int AlertFileNotSaved(LPSTR szFileName) {
65 int nResult;
66 CHAR szMessage[MAX_STRING_LEN];
67 CHAR szRessource[MAX_STRING_LEN];
69 /* Load and format Message */
71 LoadString(Globals.hInstance, IDS_NOTSAVED, szRessource, sizeof(szRessource));
72 wsprintf(szMessage, szRessource, szFileName);
74 /* Load Caption */
76 LoadString(Globals.hInstance, IDS_ERROR, szRessource, sizeof(szRessource));
78 /* Display modal */
79 nResult = MessageBox(Globals.hMainWnd, szMessage, szRessource, MB_ICONEXCLAMATION|MB_YESNOCANCEL);
80 return(nResult);
84 VOID AlertOutOfMemory(void) {
85 int nResult;
87 nResult = AlertIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, MB_ICONEXCLAMATION);
88 PostQuitMessage(1);
92 BOOL ExistFile(LPCSTR szFilename) {
94 * Returns: TRUE - if "szFileName" exists
95 * FALSE - if it does not
97 WIN32_FIND_DATA entry;
98 HANDLE hFile;
100 hFile = FindFirstFile(szFilename, &entry);
102 return (hFile!=INVALID_HANDLE_VALUE);
105 VOID DoSaveFile(VOID) {
107 /* FIXME: Really Save the file */
108 /* ... (Globals.szFileName); */
112 BOOL DoCloseFile(void) {
113 /* Return value: TRUE - User agreed to close (both save/don't save) */
114 /* FALSE - User cancelled close by selecting "Cancel" */
116 int nResult;
118 if (strlen(Globals.szFileName)>0) {
119 /* prompt user to save changes */
120 nResult = AlertFileNotSaved(Globals.szFileName);
121 switch (nResult) {
122 case IDYES: DoSaveFile();
123 break;
125 case IDNO: break;
127 case IDCANCEL: return(FALSE);
128 break;
130 default: return(FALSE);
131 break;
132 } /* switch */
133 } /* if */
135 /* Forget file name */
136 lstrcpy(Globals.szFileName, "");
137 LANGUAGE_UpdateWindowCaption();
138 return(TRUE);
142 void DoOpenFile(LPCSTR szFileName) {
144 /* Close any files and prompt to save changes */
145 if (DoCloseFile()) {
146 GetFileTitle(szFileName, Globals.szFileName, sizeof(Globals.szFileName));
147 LANGUAGE_UpdateWindowCaption();
149 LoadBufferFromFile(szFileName);
154 VOID DIALOG_FileNew(VOID)
156 /* Close any files and promt to save changes */
157 if (DoCloseFile()) {
158 TrashBuffer();
162 VOID DIALOG_FileOpen(VOID)
164 OPENFILENAME openfilename;
165 CHAR szPath[MAX_PATHNAME_LEN];
166 CHAR szDir[MAX_PATHNAME_LEN];
167 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
168 CHAR szDefaultExt[4];
169 LPSTR p = szzFilter;
171 lstrcpy(szDefaultExt, "txt");
173 LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
174 p += strlen(p) + 1;
175 lstrcpy(p, "*.txt");
176 p += strlen(p) + 1;
177 LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
178 p += strlen(p) + 1;
179 lstrcpy(p, "*.*");
180 p += strlen(p) + 1;
181 *p = '\0';
183 GetCurrentDirectory(sizeof(szDir), szDir);
184 lstrcpy(szPath,"*.txt");
186 openfilename.lStructSize = sizeof(OPENFILENAME);
187 openfilename.hwndOwner = Globals.hMainWnd;
188 openfilename.hInstance = Globals.hInstance;
189 openfilename.lpstrFilter = szzFilter;
190 openfilename.lpstrCustomFilter = 0;
191 openfilename.nMaxCustFilter = 0;
192 openfilename.nFilterIndex = 0;
193 openfilename.lpstrFile = szPath;
194 openfilename.nMaxFile = sizeof(szPath);
195 openfilename.lpstrFileTitle = 0;
196 openfilename.nMaxFileTitle = 0;
197 openfilename.lpstrInitialDir = szDir;
198 openfilename.lpstrTitle = 0;
199 openfilename.Flags = OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST;
200 openfilename.nFileOffset = 0;
201 openfilename.nFileExtension = 0;
202 openfilename.lpstrDefExt = szDefaultExt;
203 openfilename.lCustData = 0;
204 openfilename.lpfnHook = 0;
205 openfilename.lpTemplateName = 0;
207 if (GetOpenFileName(&openfilename)) {
209 if (ExistFile(openfilename.lpstrFile))
210 DoOpenFile(openfilename.lpstrFile);
211 else
212 AlertFileNotFound(openfilename.lpstrFile);
217 VOID DIALOG_FileSave(VOID)
219 /* FIXME: Save File */
221 DIALOG_FileSaveAs();
224 VOID DIALOG_FileSaveAs(VOID)
226 OPENFILENAME saveas;
227 CHAR szPath[MAX_PATHNAME_LEN];
228 CHAR szDir[MAX_PATHNAME_LEN];
229 CHAR szDefaultExt[4];
230 CHAR szzFilter[2 * MAX_STRING_LEN + 100];
232 LPSTR p = szzFilter;
234 lstrcpy(szDefaultExt, "txt");
236 LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
237 p += strlen(p) + 1;
238 lstrcpy(p, "*.txt");
239 p += strlen(p) + 1;
240 LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
241 p += strlen(p) + 1;
242 lstrcpy(p, "*.*");
243 p += strlen(p) + 1;
244 *p = '\0';
246 lstrcpy(szPath,"*.*");
248 GetCurrentDirectory(sizeof(szDir), szDir);
250 saveas.lStructSize = sizeof(OPENFILENAME);
251 saveas.hwndOwner = Globals.hMainWnd;
252 saveas.hInstance = Globals.hInstance;
253 saveas.lpstrFilter = szzFilter;
254 saveas.lpstrCustomFilter = 0;
255 saveas.nMaxCustFilter = 0;
256 saveas.nFilterIndex = 0;
257 saveas.lpstrFile = szPath;
258 saveas.nMaxFile = sizeof(szPath);
259 saveas.lpstrFileTitle = 0;
260 saveas.nMaxFileTitle = 0;
261 saveas.lpstrInitialDir = szDir;
262 saveas.lpstrTitle = 0;
263 saveas.Flags = OFN_PATHMUSTEXIST + OFN_OVERWRITEPROMPT + OFN_HIDEREADONLY;
264 saveas.nFileOffset = 0;
265 saveas.nFileExtension = 0;
266 saveas.lpstrDefExt = szDefaultExt;
267 saveas.lCustData = 0;
268 saveas.lpfnHook = 0;
269 saveas.lpTemplateName = 0;
271 if (GetSaveFileName(&saveas)) {
272 lstrcpy(Globals.szFileName, saveas.lpstrFile);
273 LANGUAGE_UpdateWindowCaption();
274 DIALOG_FileSave();
278 VOID DIALOG_FilePrint(VOID)
280 LONG bFlags, nBase;
281 WORD nOffset;
282 DOCINFO di;
283 int nResult;
284 HDC hContext;
285 PRINTDLG printer;
287 CHAR szDocumentName[MAX_STRING_LEN]; /* Name of document */
288 CHAR szPrinterName[MAX_STRING_LEN]; /* Name of the printer */
289 CHAR szDeviceName[MAX_STRING_LEN]; /* Name of the printer device */
290 CHAR szOutput[MAX_STRING_LEN]; /* in which file/device to print */
292 /* LPDEVMODE hDevMode; */
293 /* LPDEVNAMES hDevNames; */
295 /* hDevMode = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVMODE)); */
296 /* hDevNames = GlobalAlloc(GMEM_MOVEABLE + GMEM_ZEROINIT, sizeof(DEVNAMES)); */
298 /* Get Current Settings */
300 printer.lStructSize = sizeof(PRINTDLG);
301 printer.hwndOwner = Globals.hMainWnd;
302 printer.hInstance = Globals.hInstance;
304 /* Let PrintDlg create a DEVMODE structure */
305 printer.hDevMode = 0;
306 printer.hDevNames = 0;
307 printer.hDC = 0;
308 printer.Flags = PD_RETURNDEFAULT;
309 printer.nFromPage = 0;
310 printer.nToPage = 0;
311 printer.nMinPage = 0;
312 printer.nMaxPage = 0;
313 printer.nCopies = 0;
314 printer.lCustData = 0;
315 printer.lpfnPrintHook = 0;
316 printer.lpfnSetupHook = 0;
317 printer.lpPrintTemplateName = 0;
318 printer.lpSetupTemplateName = 0;
319 printer.hPrintTemplate = 0;
320 printer.hSetupTemplate = 0;
322 nResult = PrintDlg(&printer);
324 /* hContext = CreateDC(, szDeviceName, "TEST.TXT", 0); */
326 /* Congratulations to those Microsoft Engineers responsable */
327 /* for the following pointer acrobatics */
329 assert(printer.hDevNames!=0);
331 nBase = (LONG)(printer.hDevNames);
333 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDriverOffset;
334 lstrcpy(szPrinterName, (LPCSTR) (nBase + nOffset));
336 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wDeviceOffset;
337 lstrcpy(szDeviceName, (LPCSTR) (nBase + nOffset));
339 nOffset = (WORD)((LPDEVNAMES) printer.hDevNames)->wOutputOffset;
340 lstrcpy(szOutput, (LPCSTR) (nBase + nOffset));
342 MessageBox(Globals.hMainWnd, szPrinterName, "Printer Name", MB_ICONEXCLAMATION);
343 MessageBox(Globals.hMainWnd, szDeviceName, "Device Name", MB_ICONEXCLAMATION);
344 MessageBox(Globals.hMainWnd, szOutput, "Output", MB_ICONEXCLAMATION);
346 /* Set some default flags */
348 bFlags = PD_RETURNDC + PD_SHOWHELP;
350 if (TRUE) {
351 /* Remove "Print Selection" if there is no selection */
352 bFlags = bFlags + PD_NOSELECTION;
355 printer.Flags = bFlags;
357 printer.nFromPage = 0;
358 printer.nToPage = 0;
359 printer.nMinPage = 0;
360 printer.nMaxPage = 0;
363 /* Let commdlg manage copy settings */
364 printer.nCopies = (WORD)PD_USEDEVMODECOPIES;
366 if (PrintDlg(&printer)) {
368 /* initialize DOCINFO */
369 di.cbSize = sizeof(DOCINFO);
370 lstrcpy((LPSTR)di.lpszDocName, szDocumentName);
371 lstrcpy((LPSTR)di.lpszOutput, szOutput);
373 hContext = printer.hDC;
374 assert(hContext!=0);
375 assert( (int) hContext!=PD_RETURNDC);
377 SetMapMode(hContext, MM_LOMETRIC);
378 /* SetViewPortExExt(hContext, 10, 10, 0); */
379 SetBkMode(hContext, OPAQUE);
381 nResult = TextOut(hContext, 0, 0, " ", 1);
382 assert(nResult != 0);
384 nResult = StartDoc(hContext, &di);
385 assert(nResult != SP_ERROR);
387 nResult = StartPage(hContext);
388 assert(nResult >0);
390 /* FIXME: actually print */
392 nResult = EndPage(hContext);
394 switch (nResult) {
395 case SP_ERROR:
396 MessageBox(Globals.hMainWnd, "Generic Error", "Print Engine Error", MB_ICONEXCLAMATION);
397 break;
398 case SP_APPABORT:
399 MessageBox(Globals.hMainWnd, "The print job was aborted.", "Print Engine Error", MB_ICONEXCLAMATION);
400 break;
401 case SP_USERABORT:
402 MessageBox(Globals.hMainWnd, "The print job was aborted using the Print Manager ", "Print Engine Error", MB_ICONEXCLAMATION);
403 break;
404 case SP_OUTOFDISK:
405 MessageBox(Globals.hMainWnd, "Out of disk space", "Print Engine Error", MB_ICONEXCLAMATION);
406 break;
407 case SP_OUTOFMEMORY:
408 AlertOutOfMemory();
409 break;
410 default:
411 MessageBox(Globals.hMainWnd, "Default", "Print", MB_ICONEXCLAMATION);
412 } /* switch */
413 nResult = EndDoc(hContext);
414 assert(nResult>=0);
415 nResult = DeleteDC(hContext);
416 assert(nResult!=0);
417 } /* if */
419 /* GlobalFree(hDevNames); */
420 /* GlobalFree(hDevMode); */
423 VOID DIALOG_FilePageSetup(VOID)
425 DIALOG_PageSetup();
428 VOID DIALOG_FilePrinterSetup(VOID)
430 PRINTDLG printer;
432 printer.lStructSize = sizeof(PRINTDLG);
433 printer.hwndOwner = Globals.hMainWnd;
434 printer.hInstance = Globals.hInstance;
435 printer.hDevMode = 0;
436 printer.hDevNames = 0;
437 printer.hDC = 0;
438 printer.Flags = PD_PRINTSETUP;
439 printer.nFromPage = 0;
440 printer.nToPage = 0;
441 printer.nMinPage = 0;
442 printer.nMaxPage = 0;
443 printer.nCopies = 1;
444 printer.lCustData = 0;
445 printer.lpfnPrintHook = 0;
446 printer.lpfnSetupHook = 0;
447 printer.lpPrintTemplateName = 0;
448 printer.lpSetupTemplateName = 0;
449 printer.hPrintTemplate = 0;
450 printer.hSetupTemplate = 0;
452 if (PrintDlg(&printer)) {
453 /* do nothing */
458 VOID DIALOG_FileExit(VOID)
460 if (DoCloseFile()) {
461 PostQuitMessage(0);
465 VOID DIALOG_EditUndo(VOID)
467 MessageBox(Globals.hMainWnd, "Undo", "Debug", MB_ICONEXCLAMATION);
468 /* undo */
471 VOID DIALOG_EditCut(VOID)
473 HANDLE hMem;
475 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
477 OpenClipboard(Globals.hMainWnd);
478 EmptyClipboard();
480 /* FIXME: Get text */
481 lstrcpy((CHAR *)hMem, "Hello World");
483 SetClipboardData(CF_TEXT, hMem);
484 CloseClipboard();
486 GlobalFree(hMem);
489 VOID DIALOG_EditCopy(VOID)
491 HANDLE hMem;
493 hMem = GlobalAlloc(GMEM_ZEROINIT, 99);
495 OpenClipboard(Globals.hMainWnd);
496 EmptyClipboard();
498 /* FIXME: Get text */
499 lstrcpy((CHAR *)hMem, "Hello World");
501 SetClipboardData(CF_TEXT, hMem);
502 CloseClipboard();
504 GlobalFree(hMem);
507 VOID DIALOG_EditPaste(VOID)
509 HANDLE hClipText;
511 if (IsClipboardFormatAvailable(CF_TEXT)) {
512 OpenClipboard(Globals.hMainWnd);
513 hClipText = GetClipboardData(CF_TEXT);
514 CloseClipboard();
515 MessageBox(Globals.hMainWnd, (CHAR *)hClipText, "PASTE", MB_ICONEXCLAMATION);
519 VOID DIALOG_EditDelete(VOID)
521 /* Delete */
524 VOID DIALOG_EditSelectAll(VOID)
526 /* Select all */
529 VOID DIALOG_EditTimeDate(VOID)
531 SYSTEMTIME st;
532 LPSYSTEMTIME lpst = &st;
533 CHAR szDate[MAX_STRING_LEN];
534 LPSTR date = szDate;
536 GetLocalTime(&st);
537 GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, lpst, NULL, date, MAX_STRING_LEN);
538 GetTimeFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, lpst, NULL, date, MAX_STRING_LEN);
542 VOID DIALOG_EditWrap(VOID)
544 Globals.bWrapLongLines = !Globals.bWrapLongLines;
545 CheckMenuItem(Globals.hEditMenu, NP_EDIT_WRAP, MF_BYCOMMAND |
546 (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
549 VOID DIALOG_Search(VOID)
551 Globals.find.lStructSize = sizeof(Globals.find);
552 Globals.find.hwndOwner = Globals.hMainWnd;
553 Globals.find.hInstance = Globals.hInstance;
554 Globals.find.lpstrFindWhat = (CHAR *) &Globals.szFindText;
555 Globals.find.wFindWhatLen = sizeof(Globals.szFindText);
556 Globals.find.lpstrReplaceWith = 0;
557 Globals.find.wReplaceWithLen = 0;
558 Globals.find.Flags = FR_DOWN;
559 Globals.find.lCustData = 0;
560 Globals.find.lpfnHook = 0;
561 Globals.find.lpTemplateName = 0;
563 /* We only need to create the modal FindReplace dialog which will */
564 /* notify us of incoming events using hMainWnd Window Messages */
566 Globals.hFindReplaceDlg = FindText(&Globals.find);
567 assert(Globals.hFindReplaceDlg !=0);
570 VOID DIALOG_SearchNext(VOID)
572 /* Search Next */
575 VOID DIALOG_HelpContents(VOID)
577 WinHelp(Globals.hMainWnd, HELPFILE, HELP_INDEX, 0);
580 VOID DIALOG_HelpSearch(VOID)
582 /* Search Help */
585 VOID DIALOG_HelpHelp(VOID)
587 WinHelp(Globals.hMainWnd, HELPFILE, HELP_HELPONHELP, 0);
590 VOID DIALOG_HelpLicense(VOID)
592 WineLicense(Globals.hMainWnd);
595 VOID DIALOG_HelpNoWarranty(VOID)
597 WineWarranty(Globals.hMainWnd);
600 VOID DIALOG_HelpAboutWine(VOID)
602 CHAR szNotepad[MAX_STRING_LEN];
604 LoadString(Globals.hInstance, IDS_NOTEPAD, szNotepad, sizeof(szNotepad));
605 ShellAbout(Globals.hMainWnd, szNotepad, "Notepad\n" WINE_RELEASE_INFO, 0);
608 /***********************************************************************
610 * DIALOG_PageSetup
613 VOID DIALOG_PageSetup(VOID)
615 WNDPROC lpfnDlg;
617 lpfnDlg = MakeProcInstance(DIALOG_PAGESETUP_DlgProc, Globals.hInstance);
618 DialogBox(Globals.hInstance, STRING_PAGESETUP_Xx, Globals.hMainWnd, (DLGPROC)lpfnDlg);
619 FreeProcInstance(lpfnDlg);
623 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
625 * DIALOG_PAGESETUP_DlgProc
628 static LRESULT WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
631 switch (msg)
633 case WM_COMMAND:
634 switch (wParam)
636 case IDOK:
637 /* save user input and close dialog */
638 GetDlgItemText(hDlg, NP_PAGESETUP_HEAD, Globals.szHeader, sizeof(Globals.szHeader));
639 GetDlgItemText(hDlg, NP_PAGESETUP_TAIL, Globals.szFooter, sizeof(Globals.szFooter));
640 GetDlgItemText(hDlg, NP_PAGESETUP_TOP, Globals.szMarginTop, sizeof(Globals.szMarginTop));
641 GetDlgItemText(hDlg, NP_PAGESETUP_BOTTOM, Globals.szMarginBottom, sizeof(Globals.szMarginBottom));
642 GetDlgItemText(hDlg, NP_PAGESETUP_LEFT, Globals.szMarginLeft, sizeof(Globals.szMarginLeft));
643 GetDlgItemText(hDlg, NP_PAGESETUP_RIGHT, Globals.szMarginRight, sizeof(Globals.szMarginRight));
644 EndDialog(hDlg, IDOK);
645 return TRUE;
647 case IDCANCEL:
648 /* discard user input and close dialog */
649 EndDialog(hDlg, IDCANCEL);
650 return TRUE;
652 case IDHELP:
653 /* FIXME: Bring this to work */
654 MessageBox(Globals.hMainWnd, "Sorry, no help available", "Help", MB_ICONEXCLAMATION);
655 return TRUE;
657 break;
659 case WM_INITDIALOG:
660 /* fetch last user input prior to display dialog */
661 SetDlgItemText(hDlg, NP_PAGESETUP_HEAD, Globals.szHeader);
662 SetDlgItemText(hDlg, NP_PAGESETUP_TAIL, Globals.szFooter);
663 SetDlgItemText(hDlg, NP_PAGESETUP_TOP, Globals.szMarginTop);
664 SetDlgItemText(hDlg, NP_PAGESETUP_BOTTOM, Globals.szMarginBottom);
665 SetDlgItemText(hDlg, NP_PAGESETUP_LEFT, Globals.szMarginLeft);
666 SetDlgItemText(hDlg, NP_PAGESETUP_RIGHT, Globals.szMarginRight);
667 break;
670 return FALSE;