Release 980215
[wine] / programs / notepad / main.c
1 /*
2  * Notepad
3  *
4  * Copyright 1997 Marcel Baur <mbaur@g26.ethz.ch>
5  */
6
7 #include <stdio.h>
8 #include "windows.h"
9 #include "main.h"
10 #include "license.h"
11 #include "dialog.h"
12 #ifdef WINELIB
13 #include "options.h"
14 #include "resource.h"
15 #include "shell.h"
16 void LIBWINE_Register_De();
17 void LIBWINE_Register_En();
18 void LIBWINE_Register_Sw();
19 #endif
20
21 NOTEPAD_GLOBALS Globals;
22
23 CHAR STRING_MENU_Xx[]      = "MENU_Xx";
24 CHAR STRING_PAGESETUP_Xx[] = "DIALOG_PAGESETUP_Xx";
25
26 static BOOL MAIN_LoadStringOtherLanguage(UINT num, UINT ids, LPSTR str, UINT len)
27 {
28   ids -= Globals.wStringTableOffset;
29   ids += num * 0x100;
30   return(LoadString(Globals.hInstance, ids, str, len));
31 };
32
33 VOID MAIN_SelectLanguageByName(LPCSTR lang)
34 {
35   INT i;
36   CHAR newlang[3];
37
38   for (i = 0; i <= MAX_LANGUAGE_NUMBER; i++)
39     if (MAIN_LoadStringOtherLanguage(i, IDS_LANGUAGE_ID, newlang, sizeof(newlang)) &&
40         !lstrcmp(lang, newlang))
41       {
42         MAIN_SelectLanguageByNumber(i);
43         return;
44       }
45
46   /* Fallback */
47     for (i = 0; i <= MAX_LANGUAGE_NUMBER; i++)
48     if (MAIN_LoadStringOtherLanguage(i, IDS_LANGUAGE_ID, newlang, sizeof(newlang)))
49       {
50         MAIN_SelectLanguageByNumber(i);
51         return;
52       }
53
54   MessageBox(Globals.hMainWnd, "No language found", "FATAL ERROR", MB_OK);
55   PostQuitMessage(1);
56 }
57
58 VOID MAIN_SelectLanguageByNumber(UINT num)
59 {
60   INT    i;
61   CHAR   lang[3];
62   CHAR   caption[MAX_STRING_LEN];
63   CHAR   item[MAX_STRING_LEN];
64   HMENU  hMainMenu;
65
66   /* Select string table */
67   Globals.wStringTableOffset = num * 0x100;
68
69   /* Get Language id */
70   LoadString(Globals.hInstance, IDS_LANGUAGE_ID, lang, sizeof(lang));
71   Globals.lpszLanguage = lang;
72
73   /* Set frame caption */
74   LoadString(Globals.hInstance, IDS_NOTEPAD, caption, sizeof(caption));
75   SetWindowText(Globals.hMainWnd, caption);
76
77   /* Change Resource names */
78   lstrcpyn(STRING_MENU_Xx    + sizeof(STRING_MENU_Xx)    - 3, lang, 3);
79   lstrcpyn(STRING_PAGESETUP_Xx    + sizeof(STRING_PAGESETUP_Xx)    - 3, lang, 3);
80
81   /* Create menu */
82   hMainMenu = LoadMenu(Globals.hInstance, STRING_MENU_Xx);
83     Globals.hFileMenu     = GetSubMenu(hMainMenu, 0);
84     Globals.hEditMenu     = GetSubMenu(hMainMenu, 1);
85     Globals.hSearchMenu   = GetSubMenu(hMainMenu, 2);
86     Globals.hLanguageMenu = GetSubMenu(hMainMenu, 3);
87     Globals.hHelpMenu     = GetSubMenu(hMainMenu, 4);
88
89   /* Remove dummy item */
90   RemoveMenu(Globals.hLanguageMenu, 0, MF_BYPOSITION);
91   /* Add language items */
92   for (i = 0; i <= MAX_LANGUAGE_NUMBER; i++)
93     if (MAIN_LoadStringOtherLanguage(i, IDS_LANGUAGE_MENU_ITEM, item, sizeof(item)))
94       AppendMenu(Globals.hLanguageMenu, MF_STRING | MF_BYCOMMAND,
95                  NP_FIRST_LANGUAGE + i, item);
96
97   SetMenu(Globals.hMainWnd, hMainMenu);
98
99   /* Destroy old menu */
100   if (Globals.hMainMenu) DestroyMenu(Globals.hMainMenu);
101   Globals.hMainMenu = hMainMenu;
102
103 #ifdef WINELIB
104   /* Update system menus */
105   for (i = 0; Languages[i].name && lstrcmp(lang, Languages[i].name);) i++;
106   if (Languages[i].name) Options.language = i;
107
108 #endif
109 }
110
111 /***********************************************************************
112  *
113  *           NOTEPAD_RegisterLanguages
114  *
115  *  Handle language stuff at startup
116  */
117
118 void NOTEPAD_RegisterLanguages(void) {
119
120   LPCSTR opt_lang = "En";
121   CHAR lang[3];
122   INT langnum;
123   
124  /* Find language specific string table */
125   for (langnum = 0; langnum <= MAX_LANGUAGE_NUMBER; langnum++)
126     {
127       Globals.wStringTableOffset = langnum * 0x100;
128       if (LoadString(Globals.hInstance, IDS_LANGUAGE_ID, lang, 
129             sizeof(lang)) && !lstrcmp(opt_lang, lang))
130       break;
131     }
132   if (langnum > MAX_LANGUAGE_NUMBER)
133     {
134       /* Find fallback language */
135       for (langnum = 0; langnum <= MAX_LANGUAGE_NUMBER; langnum++)
136         {
137           Globals.wStringTableOffset = langnum * 0x100;
138           if (LoadString(Globals.hInstance, IDS_LANGUAGE_ID, lang, sizeof(lang)))
139           break;
140         }
141       if (langnum > MAX_LANGUAGE_NUMBER)
142         {
143         MessageBox(0, "No language found", "FATAL ERROR", MB_OK);
144         PostQuitMessage(0);
145         }
146     }
147
148   /* Change Resource names */
149   lstrcpyn(STRING_MENU_Xx + lstrlen(STRING_MENU_Xx) - 2, lang, 3);
150 }
151
152
153 /***********************************************************************
154  *
155  *           NOTEPAD_MenuCommand
156  *
157  *  All handling of main menu events
158  */
159
160 int NOTEPAD_MenuCommand (WPARAM wParam)
161 {  
162 //   printf("NOTEPAD_MenuCommand()\n");
163
164    switch (wParam) {
165      case NP_FILE_NEW:          DIALOG_FileNew(); break;
166      case NP_FILE_OPEN:         DIALOG_FileOpen(); break;
167      case NP_FILE_SAVE:         DIALOG_FileSave(); break;
168      case NP_FILE_SAVEAS:       DIALOG_FileSaveAs(); break;
169      case NP_FILE_PRINT:        DIALOG_FilePrint(); break;
170      case NP_FILE_PAGESETUP:    DIALOG_FilePageSetup(); break;
171      case NP_FILE_PRINTSETUP:   DIALOG_FilePrinterSetup();break;
172      case NP_FILE_EXIT:         DIALOG_FileExit(); break;
173
174      case NP_EDIT_UNDO:         DIALOG_EditUndo(); break;
175      case NP_EDIT_CUT:          DIALOG_EditCut(); break;
176      case NP_EDIT_COPY:         DIALOG_EditCopy(); break;
177      case NP_EDIT_PASTE:        DIALOG_EditPaste(); break;
178      case NP_EDIT_DELETE:       DIALOG_EditDelete(); break;
179      case NP_EDIT_SELECTALL:    DIALOG_EditSelectAll(); break;
180      case NP_EDIT_TIMEDATE:     DIALOG_EditTimeDate();break;
181      case NP_EDIT_WRAP:         DIALOG_EditWrap(); break;
182
183      case NP_SEARCH_SEARCH:     DIALOG_Search(); break;
184      case NP_SEARCH_NEXT:       DIALOG_SearchNext(); break;
185
186      case NP_HELP_CONTENTS:     DIALOG_HelpContents(); break;
187      case NP_HELP_SEARCH:       DIALOG_HelpSearch(); break;
188      case NP_HELP_ON_HELP:      DIALOG_HelpHelp(); break;
189      case NP_HELP_LICENSE:      DIALOG_HelpLicense(); break;
190      case NP_HELP_NO_WARRANTY:  DIALOG_HelpNoWarranty(); break;
191      case NP_HELP_ABOUT_WINE:   DIALOG_HelpAboutWine(); break;
192      
193      // Handle languages
194      default:
195        if ((wParam >=NP_FIRST_LANGUAGE) && (wParam<=NP_LAST_LANGUAGE))
196           MAIN_SelectLanguageByNumber(wParam - NP_FIRST_LANGUAGE);
197      else printf("Unimplemented menu command %i\n", wParam);  
198    }
199    return 0;
200 }
201
202
203
204 /***********************************************************************
205  *
206  *           NOTEPAD_WndProc
207  */
208
209 LRESULT NOTEPAD_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
210 {
211     PAINTSTRUCT ps;
212
213     switch (msg) {
214
215        case WM_CREATE:
216         printf("WM_CREATE\n");
217         break;
218
219         case WM_PAINT:
220           printf("WM_PAINT\n");
221           BeginPaint(hWnd, &ps);
222           EndPaint(hWnd, &ps);
223         break;
224
225        case WM_COMMAND:
226           printf("WM_COMMAND\n");
227           NOTEPAD_MenuCommand(wParam);
228           break;
229
230        case WM_DESTROY:
231           printf("WM_DESTROY\n");
232           PostQuitMessage (0);
233           break;
234
235        default:
236           return DefWindowProc (hWnd, msg, wParam, lParam);
237     }
238     return 0l;
239 }
240
241
242
243 /***********************************************************************
244  *
245  *           WinMain
246  */
247
248 void DumpGlobals(void) {
249
250     printf("DumpGlobals()\n");
251     printf(" Globals.lpszIniFile: %s\n", Globals.lpszIniFile); 
252     printf(" Globals.lpszIcoFile: %s\n", Globals.lpszIcoFile);
253     printf("Globals.lpszLanguage: %s\n", Globals.lpszLanguage);
254     printf("   Globals.hInstance: %i\n", Globals.hInstance);
255     printf("   Globals.hMainMenu: %i\n", Globals.hMainMenu);
256     
257 }
258  
259 int PASCAL WinMain (HANDLE hInstance, HANDLE prev, LPSTR cmdline, int show)
260 {
261     MSG      msg;
262     WNDCLASS class;
263     char className[] = "NPClass";  /* To make sure className >= 0x10000 */
264     char winName[]   = "Notepad";
265
266     #if defined(WINELIB) && !defined(HAVE_WINE_CONSTRUCTOR)
267       /* Register resources */
268       LIBWINE_Register_De();
269       LIBWINE_Register_En();
270       LIBWINE_Register_Sw();
271     #endif
272
273     printf("WinMain()\n");
274     
275     /* Setup Globals */
276
277     Globals.lpszIniFile   = "notepad.ini";
278     Globals.lpszIcoFile   = "notepad.ico";
279
280   /* Select Language */
281 #ifdef WINELIB
282   Globals.lpszLanguage = Languages[Options.language].name;
283 #else
284   Globals.lpszLanguage = "En";
285 #endif
286
287     Globals.hInstance     = hInstance;
288     Globals.hMainIcon     = ExtractIcon(Globals.hInstance, 
289                                         Globals.lpszIcoFile, 0);
290     if (!Globals.hMainIcon) Globals.hMainIcon = 
291                                   LoadIcon(0, MAKEINTRESOURCE(DEFAULTICON));
292
293     DumpGlobals();                                
294                                   
295     if (!prev){
296         class.style = CS_HREDRAW | CS_VREDRAW;
297         class.lpfnWndProc = NOTEPAD_WndProc;
298         class.cbClsExtra = 0;
299         class.cbWndExtra = 0;
300         class.hInstance  = Globals.hInstance;
301         class.hIcon      = LoadIcon (0, IDI_APPLICATION);
302         class.hCursor    = LoadCursor (0, IDC_ARROW);
303         class.hbrBackground = GetStockObject (WHITE_BRUSH);
304         class.lpszMenuName = "bla\0";
305         class.lpszClassName = (SEGPTR)className;
306     }
307     if (!RegisterClass (&class))
308         return FALSE;
309
310     Globals.hMainWnd = CreateWindow (className, winName, WS_OVERLAPPEDWINDOW,
311                         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 
312                         LoadMenu(Globals.hInstance, STRING_MENU_Xx),
313                         Globals.hInstance, 0);
314     MAIN_SelectLanguageByName(Globals.lpszLanguage);
315
316     SetMenu(Globals.hMainWnd, Globals.hMainMenu);               
317                         
318     ShowWindow (Globals.hMainWnd, show);
319     UpdateWindow (Globals.hMainWnd);
320
321
322     while (GetMessage (&msg, 0, 0, 0)) {
323         TranslateMessage (&msg);
324         DispatchMessage (&msg);
325     }
326     return 0;
327 }
328
329
330
331
332 /* Local Variables:    */
333 /* c-file-style: "GNU" */
334 /* End:                */