home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / applic.cxx next >
C/C++ Source or Header  |  1995-04-04  |  9KB  |  349 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6. /*
  7.  *
  8.  *          Copyright (C) 1994, M. A. Sridhar
  9.  *  
  10.  *
  11.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  12.  *     to copy, modify or distribute this software  as you see fit,
  13.  *     and to use  it  for  any  purpose, provided   this copyright
  14.  *     notice and the following   disclaimer are included  with all
  15.  *     copies.
  16.  *
  17.  *                        DISCLAIMER
  18.  *
  19.  *     The author makes no warranties, either expressed or implied,
  20.  *     with respect  to  this  software, its  quality, performance,
  21.  *     merchantability, or fitness for any particular purpose. This
  22.  *     software is distributed  AS IS.  The  user of this  software
  23.  *     assumes all risks  as to its quality  and performance. In no
  24.  *     event shall the author be liable for any direct, indirect or
  25.  *     consequential damages, even if the  author has been  advised
  26.  *     as to the possibility of such damages.
  27.  *
  28.  */
  29.  
  30.  
  31. #if defined(__GNUC__)
  32. #pragma implementation
  33. #endif
  34.  
  35.  
  36. #include <setjmp.h>
  37.  
  38. #if defined(__MS_WINDOWS__)
  39. #    include <dos.h>
  40. #    include <windows.h>
  41. #    include <ctl3d.h>
  42. #elif defined(__X_MOTIF__)
  43. #    include <X11/Xlib.h>
  44. #    include <X11/Intrinsic.h>
  45. #    include <iostream.h>
  46. #elif defined(__OS2__)
  47. #    include <stdlib.h>
  48. #    define INCL_PM
  49. #    include <os2.h>
  50. #endif
  51.  
  52.  
  53. #include "base/error.h"
  54. #include "base/string.h"
  55. #include "base/strgseq.h"
  56.  
  57. #include "ui/applic.h"
  58. #include "ui/composit.h"
  59. #include "ui/cntroler.h"
  60.  
  61. // --------------------------- Global variables ------------------------
  62.  
  63. UI_Application* _TheApplication = NULL;
  64.  
  65. #if defined(__UNIX__)
  66. #define ENVIRONMENT environ
  67. extern char** environ;
  68. #elif __BCPLUSPLUS__ > 0x310
  69. #define ENVIRONMENT _environ
  70. #else
  71. #define ENVIRONMENT environ
  72. #endif
  73.  
  74.  
  75. // ----------------------------------------------------------------
  76.  
  77.  
  78. #if defined(__MS_WINDOWS__) && defined(DEBUG)
  79. static HWND _DebugWindow = 0;
  80. static HFONT _DebugFont = 0;
  81. static long  count = 0;
  82. #endif
  83.  
  84. static bool ErrorHandler (const char* message)
  85. {
  86. #if defined(__MS_WINDOWS__)
  87. #if defined(DEBUG)
  88.     if (_DebugWindow) {
  89.         CL_String s = "(" + CL_String(++count) + ") " +
  90.             CL_String (message) + "\r\n";
  91.         SendMessage (_DebugWindow, EM_REPLACESEL, 0,
  92.                      (LPARAM) s.AsPtr());
  93.     }
  94. #else
  95.     MessageBox (NULL, message, "YACL", MB_ICONEXCLAMATION | MB_OK);
  96. #endif
  97. #elif defined(__X_MOTIF__)
  98.     cerr << "YACL: " <<  message << endl;
  99. #elif defined(__OS2__)
  100. #if defined(DEBUG)
  101.     static HWND dbugWin = 0;
  102.     static HWND dbugWinFrame = 0;
  103.     static short count = 0;
  104.     if (!dbugWin) {
  105.         ulong style = FCF_SHELLPOSITION | FCF_TITLEBAR | FCF_SIZEBORDER |
  106.             FCF_VERTSCROLL;
  107.         dbugWinFrame = WinCreateStdWindow
  108.             (HWND_DESKTOP, WS_VISIBLE, &style,
  109.              WC_MLE, "DEBUG", 0, 0, 0, &dbugWin);
  110.     }
  111.     CL_String s = "(" + CL_String (++count) + ") " + message;
  112.     WinSendMsg (dbugWin, MLM_INSERT, (MPARAM) s.AsPtr(), 0);
  113. #else
  114.     static const short ID_MESSAGEBOX = 501; // Arbitrary value
  115.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (PCH) message,
  116.                   (PCH) "YACL", ID_MESSAGEBOX,
  117.                   MB_OK | MB_APPLMODAL | MB_MOVEABLE | MB_ERROR);
  118. #endif
  119. #endif
  120.     return TRUE;
  121. }
  122.  
  123.  
  124.  
  125. UI_Application::UI_Application()
  126. {
  127.     CL_Error::SetWarningHandler    (ErrorHandler);
  128.     CL_Error::SetFatalErrorHandler (ErrorHandler);
  129.  
  130.     if (_TheApplication)
  131.         CL_Error::Fatal ("Application constructor: attempt to construct"
  132.                          "more than one Application object!");
  133.     _TheApplication = this;
  134.     // Set up the environment map
  135.     for (short i = 0; ENVIRONMENT[i] != NULL; i++) {
  136.         CL_String s (ENVIRONMENT[i]);
  137.         short j = s.CharIndex ('=');
  138.         if (j >= 0)
  139.             _env.Add (s(0,j), s.Suffix(j+1));
  140.     }
  141. }
  142.  
  143.  
  144.  
  145. UI_Application::~UI_Application()
  146. {
  147.     delete _controller;
  148. #if defined(__MS_WINDOWS__)
  149. #if defined(DEBUG)
  150.     DestroyWindow (_DebugWindow);
  151.     DeleteObject (_DebugFont);
  152. #endif
  153.     Ctl3dUnregister (pid);
  154. #endif
  155. }
  156.  
  157.  
  158. CL_String UI_Application::Name () const
  159. {
  160.     return _name;
  161. }
  162.  
  163.  
  164. CL_String UI_Application::AppClass () const
  165. {
  166.     CL_String t = _name;
  167.     t.WordCapitalize();
  168.     return t;
  169. }
  170.  
  171.  
  172.  
  173. #if defined(__MS_WINDOWS__)
  174.  
  175. void UI_Application::Initialize
  176.     (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdln, int nCmdShow)
  177. {
  178.     pid = hInstance;
  179.     _name = CL_String (lpCmdln).Field (1);
  180.     _controller = new UI_Controller ( this );
  181.     _controller->Initialize (hInstance, hPrevInstance, lpCmdln, nCmdShow);
  182.  
  183. #if defined(DEBUG)
  184.     _DebugWindow = CreateWindow
  185.         ("edit", "DEBUG",
  186.          ES_MULTILINE  | WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_VSCROLL,
  187.          CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
  188.          NULL, NULL, hInstance, NULL);
  189.     _DebugFont = CreateFont (10, 0, 0, 0, FW_BOLD, 0, 0, 0, DEFAULT_CHARSET,
  190.                               OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  191.                               DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
  192.                               "Courier");
  193.     SendMessage (_DebugWindow, WM_SETFONT, _DebugFont, 0);
  194. #endif
  195.     Ctl3dRegister (pid);
  196.     Ctl3dAutoSubclass (pid);
  197. }
  198.  
  199.  
  200. #elif defined(__X_MOTIF__)
  201. void UI_Application::Initialize (int& argc, char* argv[])
  202. {
  203.     CL_StringSequence seq =  CL_String (argv[0]).Split ("/");
  204.     _name = seq[seq.Size()-1];
  205.     _controller = new UI_Controller (this);
  206.     _controller->Initialize (argc, argv);
  207.     _controller->DispatchPendingEvents();
  208. }
  209. #elif defined(__OS2__)
  210. void UI_Application::Initialize (int /* argc */, char* argv[])
  211. {
  212.     _name = argv[0];
  213.     _controller = new UI_Controller (this);
  214.     _controller->Initialize ();
  215.     //    _controller->DispatchPendingEvents();
  216. }
  217. #endif
  218.  
  219.  
  220.  
  221. void UI_Application::MakeTopWindow (UI_CompositeVObject* root)
  222. {
  223.     _controller-> MakeTopWindow (root);
  224. }
  225.  
  226.  
  227.  
  228. void UI_Application::Destroy(UI_VisualObject *v)
  229. {
  230.     if (v !=NULL && _controller->Root() != NULL)
  231.         _controller->AddEvent (new UI_Event (Event_Quit, v));
  232.     // Instead of: _controller->Destroy (v);
  233. }
  234.  
  235.  
  236.  
  237. void UI_Application::Run ()
  238. {   
  239.     if ( _controller  ) 
  240.         _controller->Run ();
  241. }
  242.  
  243.  
  244.  
  245. void UI_Application::End ()
  246. {
  247.     _controller->AddEvent (new UI_Event
  248.                            (Event_Quit, _controller->Root ()));
  249. }
  250.  
  251.  
  252.  
  253.  
  254. UI_CompositeVObject* UI_Application::MainWindow () const
  255. {
  256.     return _controller->Root();
  257. }
  258.  
  259.  
  260. CL_String UI_Application::InstanceName (UI_VisualObject* v)
  261. {
  262.     return CL_String (v->ClassName()) + "_" +
  263.         CL_String (_controller->GetNextWidgetCount());
  264. }
  265.  
  266.  
  267. UI_Rectangle UI_Application::ScreenRect () const
  268. {
  269. #if defined(__MS_WINDOWS__)
  270.     RECT r;
  271.     GetClientRect (GetDesktopWindow (), &r);
  272.     return UI_Rectangle (r.left, r.top, r.right-r.left+1,
  273.                          r.bottom-r.top+1
  274.                         );
  275.  
  276. #elif defined (__X_MOTIF__)
  277.     Screen* screen;
  278.     screen = DefaultScreenOfDisplay
  279.         (XtDisplay ((Widget) (_controller->Root()->ViewHandle())));
  280.     long height = HeightOfScreen (screen);
  281.     long width  = WidthOfScreen  (screen);
  282.     return UI_Rectangle (0, 0, width, height);
  283.  
  284. #elif defined(__OS2__)
  285.     RECTL rect;
  286.     WinQueryWindowRect  (HWND_DESKTOP, &rect);
  287.     return UI_Rectangle (rect.xLeft, rect.yTop, rect.xRight - rect.xLeft + 1,
  288.                           rect.yTop  - rect.yBottom + 1);
  289. #endif
  290. }
  291.  
  292.  
  293. UI_Point UI_Application::AsScreenPoint (const UI_Point& p,
  294.                                         UI_VisualObject* vObj) const
  295. {
  296.     if (!vObj)
  297.         return UI_Point (0, 0);
  298. #if defined(__X_MOTIF__)
  299.     Position x, y;
  300.     XtTranslateCoords (vObj->ViewHandle(), p.XCoord(), p.YCoord(),
  301.                        &x, &y);
  302.     return UI_Point (x, y);
  303.  
  304. #elif defined(__OS2__)
  305.     POINTL pt;
  306.     pt.x = p.XCoord();
  307.     pt.y = p.YCoord();
  308.     WinMapWindowPoints (vObj->ViewHandle(), HWND_DESKTOP, &pt, 1);
  309.     return UI_Point (pt.x, pt.y);
  310. #elif defined(__MS_WINDOWS__)
  311.     POINT pt;
  312.     pt.x = p.XCoord();
  313.     pt.y = p.YCoord();
  314.     ClientToScreen (vObj->ViewHandle(), &pt);
  315.     return UI_Point (pt.x, pt.y);
  316. #endif
  317. }
  318.  
  319.  
  320.  
  321. #if defined(__OS2__)
  322. UI_Rectangle UI_Application::YACLRect (UI_ViewHandle parent, const RECTL&
  323.                                        rect) const
  324. {
  325.     long ht = _YACLWindowHeight (parent);
  326.     return UI_Rectangle (rect.xLeft, ht - rect.yTop,
  327.                          rect.xRight - rect.xLeft + 1,
  328.                          rect.yTop - rect.yBottom + 1);
  329. }
  330.  
  331.  
  332. void UI_Application::PMError ()
  333. {
  334.     PERRINFO  errInfo;
  335.     PSZ       offset;
  336.     PSZ  errMsg;
  337.  
  338.     HAB hab = _TheApplication->_controller->AnchorBlockHandle();
  339.     if ((errInfo = WinGetErrorInfo (hab)) != NULL) {
  340.         offset = ((PSZ) errInfo) + errInfo->offaoffszMsg;
  341.         errMsg = ((PSZ) errInfo) + *((PSHORT) offset);
  342.         CL_Error::Warning ("PM Error: %s\n", errMsg);
  343.         WinFreeErrorInfo (errInfo);
  344.     }
  345. }
  346.  
  347. #endif
  348.  
  349.