home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OWL1.PAK / APPLICAT.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  4KB  |  161 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. /* ---------------------------------------------------------
  4.   APPLICAT.CPP
  5.  Defines type TApplication. This defines the basic behavior
  6.  for OWL applications.
  7. ----------------------------------------------------------- */
  8.  
  9. #include <dos.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include "applicat.h"
  13. #include "appdict.h"
  14.  
  15. #if defined(__DLL__)
  16. extern TAppDictionary *AppDictionary;
  17. #else
  18. extern PTApplication pTApplication;
  19. #endif
  20.  
  21. /*  Implementation of Constructor for a TApplication object.
  22.     Inserts "this" into AppDictionary and sets members from
  23.     passed parameters. */
  24.  
  25. inline void TApplication::__TApplication(
  26.  
  27.             LPSTR, HINSTANCE, HINSTANCE APrevInstance, LPSTR, int ACmdShow
  28.         
  29.             )
  30. {
  31.   hPrevInstance = APrevInstance;
  32.   nCmdShow = ACmdShow;
  33.   MainWindow = NULL;
  34.   HAccTable = 0;
  35.   KBHandlerWnd = NULL;
  36.  
  37. #if defined (__DLL__)
  38.   AppDictionary->Add(this);
  39. #else
  40.   pTApplication = this;
  41. #endif    
  42. }
  43.  
  44. #ifdef WIN30
  45. TApplication::TApplication(
  46.  
  47.            LPSTR AName, HINSTANCE_30 AnInstance, HINSTANCE_30 APrevInstance,
  48.        LPSTR ACmdLine, int ACmdShow
  49.         
  50.            ) : TModule( AName, HINSTANCE( AnInstance ), ACmdLine)
  51. {
  52.   __TApplication(AName, HINSTANCE( AnInstance ), HINSTANCE( APrevInstance ),
  53.       ACmdLine, ACmdShow );
  54. }
  55. #endif
  56.  
  57. #ifdef WIN31
  58. TApplication::TApplication(
  59.  
  60.            LPSTR AName, HINSTANCE AnInstance, HINSTANCE APrevInstance,
  61.        LPSTR ACmdLine, int ACmdShow
  62.         
  63.            ) : TModule( AName, AnInstance, ACmdLine)
  64. {
  65.   __TApplication(AName, AnInstance, APrevInstance, ACmdLine, ACmdShow );
  66. }
  67. #endif
  68.  
  69. TApplication::~TApplication()
  70. {
  71.   if ( MainWindow ) // true only if MainWindow was destroyed but not deleted.
  72.     delete MainWindow;
  73.  
  74. #if defined(__DLL__)
  75.   AppDictionary->Delete();
  76. #endif
  77. }
  78.  
  79. /* Handles initialization for the first executing instance
  80.   of the OWL application. */
  81. void TApplication::InitApplication()
  82. {
  83. }
  84.  
  85. /* Handles initialization for each executing instance of the OWL
  86.    application.  Creates and displays the main window. */
  87. void TApplication::InitInstance()
  88. {
  89.   InitMainWindow();
  90.   MainWindow = MakeWindow(MainWindow);
  91.   if ( MainWindow )
  92.     MainWindow->Show(nCmdShow);
  93.   else
  94.     Status = EM_INVALIDMAINWINDOW;
  95. }
  96.  
  97. /* Initialize the application's MainWindow object. By default
  98.    MainWindow's title is the same as the Application's */
  99. void TApplication::InitMainWindow()
  100. {
  101.   MainWindow = new TWindow(NULL, Name);
  102. }
  103.  
  104. /* Initializes instances, creating and displaying their main window
  105.    (calls InitApplication for the first executing instance; calls
  106.    InitInstance for all instances).Runs the application. Enters message
  107.    loop if initialization was successful. */
  108. void TApplication::Run()
  109. {
  110.   if ( !hPrevInstance )
  111.     InitApplication();
  112.   if (Status == 0 )
  113.     InitInstance();
  114.   if (Status == 0)
  115.     MessageLoop();
  116.   else
  117.     Error(Status);
  118. }
  119.  
  120. /* General message loop.  Retrieves and processes a message from the
  121.    OWL application's message queue.  Calls ProcessAppMsg to allow
  122.    special handling of the message.  If not specially handled,
  123.    performs default processing of the message, dispatching the message
  124.    to the TWindowsObject's window procedure.  All unusual processing
  125.    can be accomplished by redefining ProcessAppMsg or any of the
  126.    Process... functions. */
  127. void TApplication::MessageLoop()
  128. {
  129.   MSG Message;
  130.  
  131.   while ( TRUE )
  132.   {
  133.     if ( PeekMessage(&Message, 0, 0, 0, PM_REMOVE) )
  134.     {
  135.       if ( Message.message == WM_QUIT )
  136.         break;
  137.       if ( !ProcessAppMsg(&Message) )
  138.       {
  139.         TranslateMessage(&Message);
  140.         DispatchMessage(&Message);
  141.       }
  142.     }
  143.     else   // No message waiting.
  144.       IdleAction();
  145.   }
  146.   Status = Message.wParam;
  147. }
  148.  
  149. /* Determines whether the application can be closed, returning a BOOL
  150.   indicator.  The default behavior specified here is to return the
  151.   result of a call to the CanClose function of the TApplication's
  152.   MainWindow.  */
  153. BOOL TApplication::CanClose()
  154. {
  155.   if ( MainWindow )
  156.     return MainWindow->CanClose();
  157.   else
  158.     return TRUE;
  159. }
  160.  
  161.