home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / 3DTOSHI2.ZIP / mpgui / source / guiapp.cpp next >
C/C++ Source or Header  |  1996-04-15  |  10KB  |  319 lines

  1.  
  2. // guiapp.cpp
  3. //
  4. // Copyright (c) 1995 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "guiapp.h"
  8.  
  9. GUIAPP::GUIAPP ()
  10.   {
  11.   } // End of Constructor for GUIAPP 
  12.  
  13. GUIAPP::~GUIAPP ()
  14.   {
  15.   } // End of Destructor for GUIAPP
  16.  
  17. #define DEFAULT_BGCOLOR  "white"
  18. #define DEFAULT_FGCOLOR  "black"
  19. #define DEFAULT_BDWIDTH   1
  20. #define DEFAULT_FONT     "fixed"
  21.  
  22. VOID GUIAPP::GoDOS ( GUIFRAME *MainWindow, DISPLAYDATA *Data )
  23.   {
  24.     if (MainWindow)
  25.       {}
  26.     Grafix.SetDisplay ( Data );  
  27.   } // End of GoDOS for GUIAPP
  28.  
  29. VOID GUIAPP::GoWinSDI ( GUIFRAME *MainWindow, DISPLAYDATA *Data )
  30.   {
  31.     if (MainWindow)
  32.       {}
  33.       
  34.     Grafix.SetDisplay ( Data );
  35.  
  36.     #if defined (__FORWINDOWS__)
  37.       MSG Message;
  38.  
  39.       MainWindow->ShowWindow ( CmdShow );
  40.       while (GetMessage(&Message,0,0,0))
  41.         {
  42.           TranslateMessage ( &Message );
  43.           DispatchMessage ( &Message );
  44.           ClearRemovedList ();
  45.         } // End while
  46.      #endif
  47.   } // End of GoWinSDI for GUIAPP
  48.  
  49. VOID GUIAPP::GoWinMDI ( GUIFRAME *MainWindow, DISPLAYDATA *Data )
  50.   {
  51.     if (MainWindow)
  52.       {}
  53.       
  54.     Grafix.SetDisplay ( Data );
  55.  
  56.     #if defined (__FORWINDOWS__)
  57.       HWINDOW hFrame;
  58.       HWINDOW hClient;
  59.       MSG Message;      
  60.  
  61.       hFrame = MainWindow->GetHandle ();
  62.       hClient = GetWindow ( hFrame, GW_CHILD );
  63.  
  64.       MainWindow->ShowWindow ( CmdShow );
  65.       MainWindow->UpdateWindow ();
  66.       while (GetMessage(&Message,NULL,0,0))
  67.         {
  68.           if (!TranslateMDISysAccel(hClient,&Message) &&
  69.               !TranslateAccelerator(hFrame,NULL,&Message))
  70.             {    
  71.               TranslateMessage ( &Message );
  72.               DispatchMessage ( &Message );
  73.               ClearRemovedList ();
  74.             } // End if
  75.         } // End while
  76.     #endif
  77.   } // End of GoWinMDI for GUIAPP
  78.  
  79. VOID GUIAPP::GoOS2 ( GUIFRAME *MainWindow, DISPLAYDATA *Data )
  80.   {
  81.     if (MainWindow)
  82.       {}
  83.       
  84.     Grafix.SetDisplay ( Data );
  85.  
  86.     #if defined (__FOROS2__)
  87.       QMSG Message;
  88.       while (WinGetMsg(hAB,&Message,NULLHANDLE,0,0))
  89.         {
  90.           WinDispatchMsg ( hAB, &Message );
  91.           ClearRemovedList ();
  92.         } // End while
  93.  
  94.       WinDestroyMsgQueue ( hMQ );
  95.       WinTerminate ( 0 );
  96.     #endif
  97.   } // End of GoOS2 for GUIAPP
  98.  
  99. VOID GUIAPP::GoXWindow ( GUIFRAME *MainWindow, DISPLAYDATA *Data )
  100.   {
  101.     if (MainWindow)
  102.       {}
  103.     if (Data)
  104.       {}  
  105.     #if defined (__FORUNIX__)
  106.       XEvent theEvent;
  107.       GUIWINDOW *WinPtr;
  108.       HWINDOW hWindow;
  109.       PARAM1 Param1;
  110.       PARAM2 Param2;
  111.       
  112.       // Connect to X Display
  113.       Data->MainXDisplay = XOpenDisplay ( NULL );
  114.       if (Data->MainXDisplay==NULL)
  115.         {
  116.           printf ("Can not open X Display.\n\n");
  117.           exit (0);
  118.         } // End if
  119.  
  120.       Grafix.SetDisplay ( Data );
  121.  
  122.       // Allocate and set size hint
  123.       XSizeHints *p_XSizeHints;
  124.       p_XSizeHints = XAllocSizeHints ();
  125.       if (p_XSizeHints==NULL)
  126.         {
  127.           printf (" Can not allocate X Size Hint.\n\n" );
  128.           exit (0);
  129.         } // End if
  130.       p_XSizeHints->flags = (PPosition | PSize | PMinSize);
  131.       p_XSizeHints->width = 600;
  132.       p_XSizeHints->min_width = p_XSizeHints->width;
  133.       p_XSizeHints->height = 400;
  134.       p_XSizeHints->min_height = p_XSizeHints->height;
  135.       p_XSizeHints->x = 100;
  136.       p_XSizeHints->y = 100;
  137.  
  138.       // Create Font
  139.       XFontStruct *p_XFont;
  140.       p_XFont = XLoadQueryFont ( Data->MainXDisplay, DEFAULT_FONT );
  141.       if (p_XFont==NULL)
  142.         {
  143.           printf ("Can not create default font.\n\n" );
  144.           exit (0);
  145.         } // End if 
  146.  
  147.       // Construct Geometry String
  148.       CHAR GeomString[80];
  149.       sprintf ( GeomString, "%dx%d+%d+%d", p_XSizeHints->width,
  150.                 p_XSizeHints->height, p_XSizeHints->x, p_XSizeHints->y );
  151.  
  152.       INT BitMask;     
  153.       BitMask = XGeometry ( Data->MainXDisplay, DefaultScreen ( MainXDisplay ),
  154.                             GeomString, GeomString, DEFAULT_BDWIDTH,
  155.                             p_XFont->max_bounds.width,         
  156.                             p_XFont->max_bounds.ascent+p_XFont->max_bounds.descent,
  157.                             1, 1, &(p_XSizeHints->x), &(p_XSizeHints->y),
  158.                             &(p_XSizeHints->width), &(p_XSizeHints->height) );
  159.  
  160.       if (BitMask&(XValue|YValue))
  161.         p_XSizeHints->flags |= USPosition;
  162.       if (BitMask&(WidthValue|HeightValue))
  163.         p_XSizeHints->flags |= USSize;
  164.  
  165.  
  166.       // Create Foreground and Background Color
  167.       DWORD FGPix,BGPix;
  168.       Colormap default_cmap;
  169.       XColor color;
  170.  
  171.       default_cmap = DefaultColormap ( Data->MainXDisplay, DefaultScreen(Data->MainXDisplay) ); 
  172.       if (XParseColor(Data->MainXDisplay, default_cmap, DEFAULT_BGCOLOR, &color )==0 ||
  173.            XAllocColor ( Data->MainXDisplay,  default_cmap, &color ) == 0)
  174.         BGPix = WhitePixel ( Data->MainXDisplay, DefaultScreen(Data->MainXDisplay) );
  175.       else
  176.         BGPix = color.pixel;
  177.  
  178.       if (XParseColor(Data->MainXDisplay, default_cmap, DEFAULT_FGCOLOR, &color )==0 ||
  179.            XAllocColor ( Data->MainXDisplay, default_cmap, &color ) == 0)
  180.         FGPix = BlackPixel ( Data->MainXDisplay, DefaultScreen(Data->MainXDisplay) );
  181.       else
  182.         FGPix = color.pixel;
  183.                     
  184.       // Create Main Window
  185.       HWINDOW Main;
  186.       Main = XCreateSimpleWindow ( Data->MainXDisplay, DefaultRootWindow(Data->MainXDisplay),
  187.                                    p_XSizeHints->x, p_XSizeHints->y,
  188.                                    p_XSizeHints->width, p_XSizeHints->height, 
  189.                                    DEFAULT_BDWIDTH, FGPix, BGPix ); 
  190.          
  191.       // Set Class Hints
  192.       XClassHint *p_XClassHint;
  193.       p_XClassHint = XAllocClassHint ();
  194.       if (p_XClassHint==NULL)
  195.         {
  196.           printf ("Can not create X Class Hints.\n\n" );
  197.           exit (0);
  198.         } // End if 
  199.                    
  200.       p_XClassHint->res_name = "Hello";
  201.       p_XClassHint->res_class = "XClass";
  202.       
  203.       // Set up XTextProperty for Window name and Icon
  204.       XTextProperty wname,iname;
  205.       CHAR *WTitle = "Hello";
  206.       CHAR *ITitle = "Hello";
  207.  
  208.       if (XStringListToTextProperty ( &WTitle, 1, &wname )==0)
  209.         {
  210.           printf ( "Can not create XTextProperty\n\n");
  211.           exit(0);
  212.         } // End if
  213.  
  214.       if (XStringListToTextProperty ( &ITitle, 1, &iname )==0)
  215.         {
  216.           printf ( "Can not create XTextProperty\n\n");
  217.           exit(0);
  218.         } // End if
  219.  
  220.       // Create Window Manager Hints
  221.       XWMHints *p_XWMHints;
  222.       p_XWMHints = XAllocWMHints();
  223.       if (p_XWMHints==NULL)
  224.         {
  225.           printf ( "Can not create Window Manager Hints\n\n");
  226.           exit(0);
  227.         } // End if
  228.       
  229.       p_XWMHints->flags = (InputHint|StateHint);
  230.       p_XWMHints->input = FALSE;
  231.       p_XWMHints->initial_state = NormalState;
  232.       XSetWMProperties ( Data->MainXDisplay, Main, &wname, &iname, NULL, 0, 
  233.                          p_XSizeHints, p_XWMHints, p_XClassHint );
  234.  
  235.       // Set Window Attributes
  236.       XSetWindowAttributes XWinAttr;
  237.  
  238.       XWinAttr.colormap = DefaultColormap ( Data->MainXDisplay, DefaultScreen(Data->MainXDisplay) );
  239.       XWinAttr.bit_gravity = CenterGravity;
  240.       XChangeWindowAttributes ( Data->MainXDisplay, Main, (CWColormap|CWBitGravity), 
  241.                                 &XWinAttr );
  242.  
  243.       // Select Input Event
  244.       XSelectInput ( Data->MainXDisplay, Main, ExposureMask );
  245.  
  246.       // Map Window to Screen
  247.       XMapWindow ( Data->MainXDisplay, Main );
  248.  
  249.       LONG Result;
  250.       ExitApp = FALSE;
  251.  
  252.       while (!ExitApp)
  253.         {
  254.           XNextEvent ( Data->MainXDisplay, &theEvent );
  255.           hWindow = theEvent.xany.window;
  256.           WinPtr = FindWindow ( hWindow );
  257.           if (WinPtr!=NULL)
  258.             Result = WinPtr->WndProc ( hWindow, theEvent.type, Param1, 
  259.                                        Param2, FALSE );
  260.           if (Result)
  261.             {
  262.             } // End if
  263.           ClearRemovedList ();
  264.         } // End while
  265.  
  266.       XUnloadFont ( Data->MainXDisplay, p_XFont->fid );
  267.       XDestroyWindow ( Data->MainXDisplay, Main );
  268.       XCloseDisplay ( Data->MainXDisplay );
  269.     #endif
  270.   } // End of GoXWindow for GUIAPP
  271.  
  272. VOID GUIAPP::GoMotif ( GUIFRAME *MainWindow, DISPLAYDATA *Data )
  273.   {
  274.     if (MainWindow)
  275.       {}
  276.     if (Data)
  277.       {}  
  278.     #if defined (__FORMOTIF__)
  279.       Grafix.SetDisplay ( Data );
  280.       XtAppMainLoop ( XtApp );                   
  281.     #endif
  282.   } // End of GoMotif for GUIAPP
  283.  
  284. VOID GUIAPP::Run ( DISPLAYDATA *Data, GUIFRAME* MainWindow )
  285.   {
  286.     if (MainWindow==NULL)
  287.       return;
  288.  
  289.     #if defined (__FORDOS__)
  290.       GoDOS ( MainWindow, Data );
  291.     #elif defined (__FORWINDOWS__)
  292.       if (MainWindow->IsMDISupported ()==FALSE)
  293.         {
  294.           GoWinSDI ( MainWindow, Data );
  295.         } // End if
  296.       else
  297.         {
  298.           GoWinMDI ( MainWindow, Data );
  299.         } // End else
  300.  
  301.     #elif defined (__FOROS2__)
  302.       GoOS2 ( MainWindow, Data );
  303.  
  304.     #elif defined (__FORUNIX__)
  305.       #if defined (__FORMOTIF__)
  306.         GoMotif ( MainWindow, Data );
  307.       #else
  308.         GoXWindow ( MainWindow, Data );
  309.       #endif 
  310.     #endif
  311.  
  312.     Grafix.ResetDisplay ();
  313.     DeInitWinHashTable ();
  314.   } // End of Run for GUIAPP
  315.  
  316.  
  317.  
  318.  
  319.