home *** CD-ROM | disk | FTP | other *** search
/ CD Loisirs 18 / cd.iso / PLANETE / MUDWIN / SOURCE.ZIP / INIT.C < prev    next >
C/C++ Source or Header  |  1994-09-03  |  10KB  |  350 lines

  1. //
  2. //  MODULE    Init.c
  3. //
  4. //  PURPOSE    Contains initialization code
  5. //
  6. //  EXPORTS    Housekeeping()
  7. //
  8.  
  9. #include "defcon.h"
  10. #include "global.h"
  11. #pragma hdrstop
  12. #include <commdlg.h>
  13. #include <memory.h>
  14. #include <ctype.h>
  15. #include <string.h>
  16.  
  17. /* note when we were last compiled */
  18. TS(Init)
  19.  
  20. //  FUNCTION    GoodbyeWorld
  21. //
  22. //  PURPOSE    Tells user why we are dying
  23. //
  24. //  PARAMETERS    wFmt --String table entry to use as format 
  25. //        lpszItem -- Item for which initialization failed
  26. //
  27. //  RETURNS    FALSE to slightly simplify coding
  28.  
  29. BOOL NEAR 
  30. GoodbyeWorld(WORD wFmt, LPSTR lpszItem)
  31. {
  32.     MsgBox(MB_ICONEXCLAMATION | MB_OK, wFmt, lpszItem);
  33.     return FALSE;
  34. }
  35.  
  36. //  FUNCTION    InitializeApplication
  37. //
  38. //  PURPOSE    Performs application-wide initialization
  39. //
  40. //  PARAMETERS    None
  41. //
  42. //  RETURNS    TRUE if everything initialized OK,
  43. //              FALSE if something tragic happened
  44.  
  45. BOOL NEAR 
  46. InitializeApplication()
  47. {
  48.     WNDCLASS        wc;
  49.  
  50.     //  Initialize & register the various window classes
  51.  
  52.     // Register the frame class
  53.     memset(&wc, 0, sizeof wc);
  54.     wc.lpfnWndProc = (WNDPROC) FrameWndProc;
  55.     wc.hInstance = hInst;
  56.     wc.hIcon = LoadIcon(hInst, IDI_FRAME);
  57.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  58.     wc.lpszMenuName = IDM_FRAME;
  59.     wc.lpszClassName = szFrame;
  60.     if (!RegisterClass(&wc))
  61.         return GoodbyeWorld(IDS_REGISTER, szFrame);
  62.  
  63.     // Register the player class (a MDI child)
  64.     memset(&wc, 0, sizeof wc);
  65.     wc.lpfnWndProc = (WNDPROC) PlayerWndProc;
  66.     wc.hIcon = LoadIcon(hInst, IDI_CLIENT);
  67.     wc.cbWndExtra = sizeof(LPVOID);
  68.     wc.lpszClassName = szPlayer;
  69.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  70.     wc.hInstance = hInst;
  71.     if (!RegisterClass(&wc))
  72.         return GoodbyeWorld(IDS_REGISTER, szPlayer);
  73.  
  74. #ifndef SKIP_TOOLBAR
  75.     /* register the tool bar class */
  76.     memset(&wc, 0, sizeof wc);
  77.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  78.     wc.hInstance = hInst;
  79.     wc.lpfnWndProc = (WNDPROC) ToolsWndProc;
  80.     wc.lpszClassName = szTools;
  81.     wc.style = CS_OWNDC | CS_HREDRAW;
  82.     if (!RegisterClass(&wc))
  83.         return GoodbyeWorld(IDS_REGISTER, szTools);
  84. #endif
  85.  
  86. #ifndef SKIP_STATUS
  87.     /* register the status line class */
  88.     memset(&wc, 0, sizeof wc);
  89.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  90.     wc.hInstance = hInst;
  91.     wc.lpfnWndProc = (WNDPROC) StatusWndProc;
  92.     wc.lpszClassName = szStat;
  93.     wc.style = CS_OWNDC | CS_HREDRAW;
  94.     if (!RegisterClass(&wc))
  95.         return GoodbyeWorld(IDS_REGISTER, szStat);
  96. #endif
  97.  
  98. #ifndef SKIP_YESNO
  99.     /* register the Yes/No class */
  100.     memset(&wc, 0, sizeof wc);
  101.     wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
  102.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  103.     wc.hInstance = hInst;
  104.     wc.lpfnWndProc = (WNDPROC) YesNoWndProc;
  105.     wc.lpszClassName = szYesNo;
  106.     wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  107.     if (!RegisterClass(&wc))
  108.         return GoodbyeWorld(IDS_REGISTER, szYesNo);
  109. #endif
  110.  
  111. #ifndef SKIP_CMDLINE
  112.     /* Register private combobox control */
  113.     if (!GetClassInfo(NULL, "ComboBox", &wc))
  114.         return FALSE;
  115.     gComboProc = (WNDPROC) wc.lpfnWndProc;
  116.     wc.style &= ~CS_GLOBALCLASS;
  117.     wc.lpfnWndProc = (WNDPROC) ComboWndProc;
  118.     wc.hInstance = hInst;
  119.     wc.lpszClassName = szCombo;
  120.     wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
  121.     if (!RegisterClass(&wc))
  122.         return GoodbyeWorld(IDS_REGISTER, szCombo);
  123. #endif
  124.  
  125.     /* Register private edit control */
  126.     if (!GetClassInfo(NULL, "Edit", &wc))
  127.         return FALSE;
  128.     gEditProc = (WNDPROC) wc.lpfnWndProc;
  129.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  130.     wc.style &= ~CS_GLOBALCLASS;
  131.     wc.lpfnWndProc = (WNDPROC) EditWndProc;
  132.     wc.hInstance = hInst;
  133.     wc.lpszClassName = szEdit;
  134.     if (!RegisterClass(&wc))
  135.         return GoodbyeWorld(IDS_REGISTER, szEdit);
  136.  
  137.     //  Success!
  138.     return TRUE;
  139.  
  140. }
  141.  
  142. //  FUNCTION    ExistingApplication
  143. //
  144. //  PURPOSE    Pass command line to and pop open previous instance
  145. //
  146. //  PARAMETERS    lpCmdLine - Command line from user
  147. //        nCmdShow  - How the window should be opened
  148. //
  149. //  RETURNS    TRUE if everything initialized OK,
  150. //              FALSE if something tragic happened
  151.  
  152. BOOL NEAR 
  153. ExistingApplication(LPSTR lpCmdLine, int nCmdShow)
  154. {
  155.     HWND            h;
  156.     HGLOBAL         hDrop;
  157.     typedef struct tagDRAGHEADER {
  158.         UINT            wStructSize;
  159.         UINT            x, y;
  160.         BOOL            fInPlayer;
  161.     }               DRAGHEADER;
  162.     DRAGHEADER FAR *lpdh;
  163.     int             n;
  164.  
  165.     if (!(h = FindWindow(szFrame, NULL)))
  166.         return FALSE;
  167.     BringWindowToTop(h);
  168.     ShowWindow(h, nCmdShow);
  169.     if (lpCmdLine && *lpCmdLine) {
  170.         /* rather than messing with DDE, send a fake drop message */
  171.         n = sizeof(DRAGHEADER) + _fstrlen(lpCmdLine) + 2;
  172.         hDrop = GlobalAlloc(GMEM_DDESHARE | GMEM_ZEROINIT, n);
  173.         lpdh = (DRAGHEADER FAR *) GlobalLock(hDrop);
  174.         lpdh->wStructSize = sizeof(DRAGHEADER);
  175.         _fstrcpy((LPSTR) lpdh + sizeof(DRAGHEADER), lpCmdLine);
  176.         GlobalUnlock(hDrop);
  177.         SendMessage(h, WM_DROPFILES, hDrop, 0L);
  178.         GlobalFree(hDrop);
  179.     }
  180.     return TRUE;
  181. }
  182.  
  183. //  FUNCTION    InitializeInstance
  184. //
  185. //  PURPOSE    Performs per-instance initialization
  186. //
  187. //  PARAMETERS    lpCmdLine - Command line from user
  188. //        nCmdShow  - How the window should be opened
  189.  
  190. BOOL NEAR 
  191. InitializeInstance(LPSTR lpCmdLine, int nCmdShow)
  192. {
  193.     char            sz[80], *pCmdLine;
  194.     int i;
  195.  
  196.     /* Register the FindReplace message. */
  197.     uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);
  198.  
  199.     /* Get the base window title */
  200.     LoadString(hInst, IDS_APPNAME, sz, sizeof(sz));
  201.  
  202.     /* Create the frame (and MDI client window) */
  203.     hMainWnd = CreateWindowEx(WS_EX_DROPFILES, szFrame, sz,
  204.                   WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  205.                       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL,
  206.                   hInst, NULL);
  207.     if ((!hMainWnd) || (!hClient))
  208.         return GoodbyeWorld(IDS_CREATEWIN, szFrame);
  209.  
  210. #ifndef SKIP_YESNO
  211.     // Create the yes/no window
  212.     hYesNo = CreateWindow(szYesNo, NULL,
  213.                 WS_CHILD | WS_VISIBLE | WS_BORDER,
  214.                 0, 0, 0, 0,
  215.                 hMainWnd,    // the parent
  216.                 0,    // ID for WM_COMMAND msgs
  217.                 hInst,    // This instance owns this window
  218.                 NULL);    // Pointer not needed
  219.     if (!hYesNo)
  220.         return GoodbyeWorld(IDS_CREATEWIN, szYesNo);
  221. #endif
  222.  
  223. #ifndef SKIP_CMDLINE
  224.     // Create the command window
  225.     hCmdLine = CreateWindow(szCombo, NULL,
  226.                 WS_CHILD | WS_VISIBLE | WS_BORDER |
  227.                 CBS_DROPDOWN | CBS_AUTOHSCROLL | WS_VSCROLL,
  228.                 0, 0, 0, 0,
  229.                 hMainWnd,    // the parent
  230.                 -1,    // ID for WM_COMMAND msgs
  231.                 hInst,    // This instance owns this window
  232.                 NULL);    // Pointer not needed
  233.     if (!hCmdLine)
  234.         return GoodbyeWorld(IDS_CREATEWIN, szCombo);
  235. #endif
  236.  
  237. #ifndef SKIP_STATUS
  238.     // Create the status window
  239.     hStatBar = CreateWindow(szStat, NULL,
  240.                 WS_CHILD | WS_VISIBLE | WS_BORDER,
  241.                 0, 0, 0, 0,
  242.                 hMainWnd,    // the parent
  243.                 0,    // ID for WM_COMMAND msgs
  244.                 hInst,    // This instance owns this window
  245.                 NULL);    // Pointer not needed
  246.     if (!hStatBar) {
  247.         (void) GoodbyeWorld(IDS_CREATEWIN, szStat);
  248.         wFlags &= ~SHOW_STATUS;
  249.     }
  250. #endif
  251.  
  252. #ifndef SKIP_TOOLBAR
  253.     // Create the toolbar window
  254.     hToolBar = CreateWindow(szTools, NULL,
  255.                 WS_CHILD | WS_VISIBLE | WS_BORDER,
  256.                 0, 0, 0, 0,
  257.                 hMainWnd,    // the parent
  258.                 0,    // ID for WM_COMMAND msgs
  259.                 hInst,    // This instance owns this window
  260.                 NULL);    // Pointer not needed
  261.     if (!hToolBar) {
  262.         (void) GoodbyeWorld(IDS_CREATEWIN, szTools);
  263.         wFlags &= ~SHOW_TOOLS;
  264.         }
  265.  
  266. #endif
  267.  
  268.      // Load main menu accelerators */
  269.         if (!(hAccel = LoadAccelerators(hInst, IDA_FRAME)))
  270.         return FALSE;
  271.  
  272.     // Display the frame window.  The frame will receive a WM_SIZE
  273.     // message, whereupon it properly sizes all of its children
  274.     ShowWindow(hMainWnd, nCmdShow);
  275.     UpdateWindow(hMainWnd);
  276. //    ShowWindow(hToolBar, nCmdShow);
  277. //    UpdateWindow(hToolBar);
  278.  
  279.     if (lpCmdLine && *lpCmdLine) {
  280.         AddFile(lpCmdLine);
  281.     }
  282.  
  283.     for (i = 0; i < 16; i++) {
  284.         char szKey[8];
  285.  
  286.         wsprintf(szKey, "Color%d", i);
  287.         aclrCustom[i] = GetPrivateRGB(NULL,
  288.                     "Defaults", szKey, RGB(i,i,i));
  289.     }
  290.  
  291.     return TRUE;
  292. }
  293.  
  294. //  FUNCTION    Housekeeping
  295. //
  296. //  PURPOSE    Does all the grunt work to get everything running
  297. //
  298. //  PARAMETERS    lpCmdLine - Command line from user
  299. //        nCmdShow  - How the window should be opened
  300. //
  301. //  RETURNS    TRUE if everything initialized OK,
  302. //              FALSE if