Release 980517
[wine] / programs / notepad / dialog.c
1 /*
2  * Notepad
3  *
4  * Copyright 1998 Marcel Baur <mbaur@g26.ethz.ch>
5  */
6
7 #include <stdio.h>
8 #include "windows.h"
9 #include "commdlg.h"
10 #include "winnls.h"
11 #include "winerror.h"
12 #ifdef WINELIB
13 #include "shell.h"
14 #include "options.h"
15 #endif
16 #include "main.h"
17 #include "license.h"
18 #include "language.h"
19 #include "dialog.h"
20 #include "version.h"
21 #include "debug.h"
22
23
24 static LRESULT DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
25
26
27 int AlertIDS(UINT ids_message, UINT ids_caption, WORD type) {
28 /*
29  * Given some ids strings, this acts as a language-aware wrapper for 
30  * "MessageBox"
31  */
32    CHAR szMessage[MAX_STRING_LEN];
33    CHAR szCaption[MAX_STRING_LEN];
34    
35    LoadString(Globals.hInstance, ids_message, szMessage, sizeof(szMessage));
36    LoadString(Globals.hInstance, ids_caption, szCaption, sizeof(szCaption));
37    
38    return (MessageBox(Globals.hMainWnd, szMessage, szCaption, type));
39 }
40
41 void AlertFileNotFound(LPCSTR szFilename) {
42
43    int nResult;
44    
45    nResult = AlertIDS(IDS_NOTFOUND, IDS_ERROR, 0);
46 }
47
48
49 VOID AlertOutOfMemory(void) {
50
51    int nResult;
52    
53    nResult = AlertIDS(IDS_OUT_OF_MEMORY, IDS_ERROR, 0);
54    PostQuitMessage(1);
55 }
56
57
58 BOOL ExistFile(LPCSTR szFilename) {
59 /*
60  *  Returns: TRUE  - if "szFileName" exists
61  *           FALSE - if it does not
62  */
63    WIN32_FIND_DATA32A entry;
64    HANDLE32 handle;
65    
66    handle = FindFirstFile32A(szFilename, &entry);
67    
68    return (handle!=INVALID_HANDLE_VALUE32);
69 }
70
71 VOID DoSaveFile(VOID) {
72    // Really Save the file 
73    
74    // ... (Globals.szFileName);
75 }
76
77
78 BOOL DoCloseFile(void) {
79 // Return value: TRUE  - User agreed to close (both save/don't save)
80 //               FALSE - User cancelled close by selecting "Cancel"
81
82    CHAR szMessage[MAX_STRING_LEN];
83    CHAR szCaption[MAX_STRING_LEN];
84
85    INT nResult;
86    
87    if (strlen(Globals.szFileName)>0) {
88
89    // prompt user to save changes
90    
91    // FIXME: The following resources are not yet in the .rc files
92    // szMessage, szCaption show up random values. Please keep these lines!
93    
94 //  LoadString(Globals.hInstance, ids_savechanges, szMessage, sizeof(szMessage));
95 //  LoadString(Globals.hInstance, ids_savetitle, szCaption, sizeof(szCaption));
96    
97    nResult = MessageBox(Globals.hMainWnd, szMessage, szCaption, MB_YESNOCANCEL);
98
99    switch (nResult) {
100           case IDYES:     DoSaveFile();
101                      break;
102           case IDNO:      break;
103           case IDCANCEL:  return(FALSE);
104                      break;
105                       
106          default:    return(FALSE);
107                      break;
108       }
109       
110     
111   }
112   
113   // Forget file name
114   
115   lstrcpyn(Globals.szFileName, "\0", 1);
116   LANGUAGE_UpdateWindowCaption();
117
118   return(TRUE);
119   
120 }
121
122
123 void DoOpenFile(LPCSTR szFileName) {
124
125     // Close any files and prompt to save changes
126     if (DoCloseFile) {
127
128         // Open file
129         lstrcpyn(Globals.szFileName, szFileName, strlen(szFileName)+1); 
130         LANGUAGE_UpdateWindowCaption();
131     
132     }
133 }
134
135
136 VOID DIALOG_FileNew(VOID)
137 {
138     // Close any files and promt to save changes
139     if (DoCloseFile()) {
140     
141         // do nothing yet
142     
143     }
144 }
145
146
147 VOID DIALOG_FileOpen(VOID)
148 {
149
150         OPENFILENAME openfilename;
151         CHAR szPath[MAX_PATHNAME_LEN];
152         CHAR szDir[MAX_PATHNAME_LEN];
153         CHAR szzFilter[2 * MAX_STRING_LEN + 100];
154         LPSTR p = szzFilter;
155
156         LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
157         p += strlen(p) + 1;
158         lstrcpy(p, "*.txt");
159         p += strlen(p) + 1;
160         LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
161         p += strlen(p) + 1;
162         lstrcpy(p, "*.*");
163         p += strlen(p) + 1;
164         *p = '\0';
165
166         GetWindowsDirectory(szDir, sizeof(szDir));
167
168         openfilename.lStructSize       = sizeof(openfilename);
169         openfilename.hwndOwner         = Globals.hMainWnd;
170         openfilename.hInstance         = Globals.hInstance;
171         openfilename.lpstrFilter       = szzFilter;
172         openfilename.lpstrCustomFilter = 0;
173         openfilename.nMaxCustFilter    = 0;
174         openfilename.nFilterIndex      = 0;
175         openfilename.lpstrFile         = szPath;
176         openfilename.nMaxFile          = sizeof(szPath);
177         openfilename.lpstrFileTitle    = 0;
178         openfilename.nMaxFileTitle     = 0;
179         openfilename.lpstrInitialDir   = szDir;
180         openfilename.lpstrTitle        = 0;
181         openfilename.Flags             = 0;
182         openfilename.nFileOffset       = 0;
183         openfilename.nFileExtension    = 0;
184         openfilename.lpstrDefExt       = 0;
185         openfilename.lCustData         = 0;
186         openfilename.lpfnHook          = 0;
187         openfilename.lpTemplateName    = 0;
188
189         if (GetOpenFileName(&openfilename)) {
190                 
191                 if (ExistFile(openfilename.lpstrFile))
192                     DoOpenFile(openfilename.lpstrFile);
193                 else 
194                     AlertFileNotFound(openfilename.lpstrFile);
195                     
196         }
197           
198 }
199
200 VOID DIALOG_FileSave(VOID)
201 {
202         fprintf(stderr, "FileSave()\n");
203 }
204
205 VOID DIALOG_FileSaveAs(VOID)
206 {
207         OPENFILENAME saveas;
208         CHAR szPath[MAX_PATHNAME_LEN];
209         CHAR szDir[MAX_PATHNAME_LEN];
210         CHAR szzFilter[2 * MAX_STRING_LEN + 100];
211         LPSTR p = szzFilter;
212
213         LoadString(Globals.hInstance, IDS_TEXT_FILES_TXT, p, MAX_STRING_LEN);
214         p += strlen(p) + 1;
215         lstrcpy(p, "*.txt");
216         p += strlen(p) + 1;
217         LoadString(Globals.hInstance, IDS_ALL_FILES, p, MAX_STRING_LEN);
218         p += strlen(p) + 1;
219         lstrcpy(p, "*.*");
220         p += strlen(p) + 1;
221         *p = '\0';
222
223         GetWindowsDirectory(szDir, sizeof(szDir));
224
225         saveas.lStructSize       = 0;
226         saveas.hwndOwner         = Globals.hMainWnd;
227         saveas.hInstance         = Globals.hInstance;
228         saveas.lpstrFilter       = szzFilter;
229         saveas.lpstrCustomFilter = 0;
230         saveas.nMaxCustFilter    = 0;
231         saveas.nFilterIndex      = 0;
232         saveas.lpstrFile         = szPath;
233         saveas.nMaxFile          = sizeof(szPath);
234         saveas.lpstrFileTitle    = 0;
235         saveas.nMaxFileTitle     = 0;
236         saveas.lpstrInitialDir   = szDir;
237         saveas.lpstrTitle        = 0;
238         saveas.Flags             = 0;
239         saveas.nFileOffset       = 0;
240         saveas.nFileExtension    = 0;
241         saveas.lpstrDefExt       = 0;
242         saveas.lCustData         = 0;
243         saveas.lpfnHook          = 0;
244         saveas.lpTemplateName    = 0;
245
246         if (GetSaveFileName(&saveas)) {
247             lstrcpyn(Globals.szFileName, saveas.lpstrFile, 
248             strlen(saveas.lpstrFile)+1);
249             LANGUAGE_UpdateWindowCaption();
250             DIALOG_FileSave();
251         }
252 }
253
254 VOID DIALOG_FilePrint(VOID)
255 {
256         PRINTDLG printer;
257         printer.lStructSize           = sizeof(printer);
258         printer.hwndOwner             = Globals.hMainWnd;
259         printer.hInstance             = Globals.hInstance;
260         printer.hDevMode              = 0;
261         printer.hDevNames             = 0;
262         printer.hDC                   = 0;
263         printer.Flags                 = 0;
264         printer.nFromPage             = 0;
265         printer.nToPage               = 0;
266         printer.nMinPage              = 0;
267         printer.nMaxPage              = 0;
268         printer.nCopies               = 0;
269         printer.lCustData             = 0;
270         printer.lpfnPrintHook         = 0;
271         printer.lpfnSetupHook         = 0;
272         printer.lpPrintTemplateName   = 0;
273         printer.lpSetupTemplateName   = 0;
274         printer.hPrintTemplate        = 0;
275         printer.hSetupTemplate        = 0;
276         
277         if (PrintDlg16(&printer)) {
278             // do nothing
279         };
280 }
281
282 VOID DIALOG_FilePageSetup(VOID)
283 {
284         DIALOG_PageSetup();
285 }
286
287 VOID DIALOG_FilePrinterSetup(VOID)
288 {
289         fprintf(stderr, "FilePrinterSetup()\n");
290 }
291
292 VOID DIALOG_FileExit(VOID)
293 {
294         if (DoCloseFile()) {
295                PostQuitMessage(0);
296         }
297 }
298
299 VOID DIALOG_EditUndo(VOID)
300 {
301         fprintf(stderr, "EditUndo()\n");
302 }
303
304 VOID DIALOG_EditCut(VOID)
305 {
306         fprintf(stderr, "EditCut()\n");
307 }
308
309 VOID DIALOG_EditCopy(VOID)
310 {
311         fprintf(stderr, "EditCopy()\n");
312 }
313
314 VOID DIALOG_EditPaste(VOID)
315 {
316         fprintf(stderr, "EditPaste()\n");
317 }
318
319 VOID DIALOG_EditDelete(VOID)
320 {
321         fprintf(stderr, "EditDelete()\n");
322 }
323
324 VOID DIALOG_EditSelectAll(VOID)
325 {
326         fprintf(stderr, "EditSelectAll()\n");
327 }
328
329 VOID DIALOG_EditTimeDate(VOID)
330 {
331         DIALOG_TimeDate();   
332 }
333
334 VOID DIALOG_EditWrap(VOID)
335 {
336         Globals.bWrapLongLines = !Globals.bWrapLongLines;
337         CheckMenuItem(Globals.hEditMenu, NP_EDIT_WRAP, MF_BYCOMMAND | 
338         (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
339 }
340
341 VOID DIALOG_Search(VOID)
342 {
343         FINDREPLACE find;
344         CHAR szFind[MAX_PATHNAME_LEN];
345           
346         lstrcpyn(szFind, Globals.szFindText, strlen(Globals.szFindText)+1);
347           
348         find.lStructSize      = sizeof(find);
349         find.hwndOwner        = Globals.hMainWnd;
350         find.hInstance        = Globals.hInstance;
351         find.lpstrFindWhat    = szFind;
352         find.wFindWhatLen     = sizeof(szFind);
353         find.Flags            = 0;
354         find.lCustData        = 0;
355         find.lpfnHook         = 0;
356         find.lpTemplateName   = 0;
357
358         if (FindText(&find)) {
359              lstrcpyn(Globals.szFindText, szFind, strlen(szFind)+1);
360         } 
361              else 
362         { 
363              // do nothing yet
364         };
365 }
366
367 VOID DIALOG_SearchNext(VOID)
368 {
369         fprintf(stderr, "SearchNext()\n");
370 }
371
372 VOID DIALOG_HelpContents(VOID)
373 {
374         WinHelp(Globals.hMainWnd, HELPFILE, HELP_INDEX, 0);
375 }
376
377 VOID DIALOG_HelpSearch(VOID)
378 {
379         fprintf(stderr, "HelpSearch()\n");
380 }
381
382 VOID DIALOG_HelpHelp(VOID)
383 {
384         WinHelp(Globals.hMainWnd, HELPFILE, HELP_HELPONHELP, 0);
385 }
386
387 VOID DIALOG_HelpLicense(VOID)
388 {
389         WineLicense(Globals.hMainWnd, Globals.lpszLanguage);
390 }
391
392 VOID DIALOG_HelpNoWarranty(VOID)
393 {
394         WineWarranty(Globals.hMainWnd, Globals.lpszLanguage);
395 }
396
397 VOID DIALOG_HelpAboutWine(VOID)
398 {
399         ShellAbout(Globals.hMainWnd, "Notepad", "Notepad\n" WINE_RELEASE_INFO, 0);
400 }
401
402 /***********************************************************************
403  *
404  *           DIALOG_PageSetup
405  */
406
407 VOID DIALOG_PageSetup(VOID)
408 {
409   WNDPROC lpfnDlg = MakeProcInstance(DIALOG_PAGESETUP_DlgProc, Globals.hInstance);
410   DialogBox(Globals.hInstance, STRING_PAGESETUP_Xx, Globals.hMainWnd, lpfnDlg);
411   FreeProcInstance(lpfnDlg);
412 }
413
414 /***********************************************************************
415  *
416  *           DIALOG_TimeDate
417  */
418
419 VOID DIALOG_TimeDate(VOID)
420 {
421   // uses [KERNEL32.310] (ole2nls.c)
422   
423   SYSTEMTIME   st;
424   LPSYSTEMTIME lpst = &st;
425   CHAR         szDate[MAX_STRING_LEN];
426   LPSTR        date = szDate;
427   
428   GetLocalTime(&st);
429   GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, lpst, NULL, date, MAX_STRING_LEN);
430   
431   printf("Date: %s\n", date);
432   
433   GetDateFormat(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, lpst, NULL, date, MAX_STRING_LEN);
434   
435   printf("Time: %s\n", date);
436
437 }
438
439 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
440  *
441  *           DIALOG_PAGESETUP_DlgProc
442  */
443
444 static LRESULT DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
445 {
446   switch (msg)
447     {
448     case WM_COMMAND:
449       switch (wParam)
450         {
451         case IDOK:
452           EndDialog(hDlg, IDOK);
453           return TRUE;
454
455         case IDCANCEL:
456           EndDialog(hDlg, IDCANCEL);
457           return TRUE;
458         }
459     }
460   return FALSE;
461 }
462
463 /* Local Variables:    */
464 /* c-file-style: "GNU" */
465 /* End:                */