home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / simple.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  8KB  |  296 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.  
  32.  
  33.  
  34. #if defined(__GNUC__)
  35. #pragma implementation
  36. #endif
  37.  
  38.  
  39. #include "base/binding.h"
  40. #include "ui/simple.h"
  41. #include "ui/cntroler.h"
  42.  
  43. #if defined(__MS_WINDOWS__)
  44. #include <ctl3d.h>
  45. #endif
  46.  
  47. #if defined (__X_MOTIF__)
  48. #include <X11/Intrinsic.h>
  49. #include <X11/StringDefs.h>
  50. #include <X11/Shell.h>
  51. #include <Xm/Xm.h>
  52. #endif
  53.  
  54.  
  55. #if defined(__GNUC__) && __GNUC_MINOR__ >= 6
  56. template class CL_Binding<UI_SimpleVObject>;
  57. #endif
  58.  
  59.  
  60. typedef CL_Binding<UI_SimpleVObject> SimpleBind;
  61.  
  62. UI_SimpleVObject :: UI_SimpleVObject
  63.     ( UI_VObjCollection* p, const UI_Rectangle& shape, UI_ViewID id,
  64.       long style)
  65. : UI_VisualObject (p,  shape, id, style)
  66. {
  67.    _ownModel = TRUE;
  68.    _model = NULL;
  69. }
  70.  
  71.  
  72.  
  73. #if defined(__MS_WINDOWS__)
  74. UI_SimpleVObject :: UI_SimpleVObject
  75.     ( UI_VObjCollection* p, UI_ViewID id, UI_ViewHandle h)
  76. : UI_VisualObject (p, id, h)
  77. {
  78.    _ownModel = TRUE;
  79.    _model = NULL;
  80. }
  81. #endif
  82.  
  83.  
  84. UI_SimpleVObject :: UI_SimpleVObject
  85.     ( UI_VObjCollection* p, CL_Object* md, UI_ViewID id,
  86.       const UI_Rectangle& r, long style)
  87. : UI_VisualObject (p, r, id, style)
  88. {
  89.     if (!_parent)
  90.         CL_Error::Warning ("SimpleVObject constructor: class '%s' id %d:"
  91.                            " NULL parent!", ClassName(), id);
  92.    _ownModel = FALSE;
  93.    _model = md;
  94. }
  95.  
  96.  
  97.  
  98. UI_SimpleVObject :: ~UI_SimpleVObject()
  99. {
  100.     if (_ownModel && _model != NULL)
  101.         delete _model;
  102. }
  103.  
  104.  
  105. bool UI_SimpleVObject::_ModelChanged (CL_Object&, long)
  106. {
  107.     return Paint ();
  108. }
  109.  
  110.  
  111. bool UI_SimpleVObject::MakeVisualElement ()
  112. {
  113.     bool b = UI_VisualObject::MakeVisualElement ();
  114. #if defined(__MS_WINDOWS__)
  115.     if (Has3DLook ())
  116.         Ctl3dSubclassCtl (_handle);
  117. #endif
  118.     return b;
  119. }
  120.  
  121.  
  122. void UI_SimpleVObject :: _PrivateInitialize()
  123. {
  124.     UI_VisualObject :: _PrivateInitialize ( );
  125.  
  126. #if defined(__OS2__)
  127.     // Mouse moves are not queued under OS/2. We need to be able to set
  128.     // cursors for simple objects also, and this can only be done on each
  129.     // mouse move. Therefore we subclass. Note that if we subclass in
  130.     // MakeVisualElement, we get into trouble: a message sent to the control
  131.     // before MakeVisualElement finishes execution will fail, because the
  132.     // subclassing procedure's call to the Controller's op[] will fail. This
  133.     // is why the subclassing is done here rather than in MakeVisualElement.
  134.     if (WindowClass() != NULL) // Don't subclass menu items
  135.         _Subclass ();
  136. #endif
  137.     if (_model) {
  138.         SimpleBind b (this, &UI_SimpleVObject::_ModelChanged);
  139.         _model->AddDependent (b, 1);
  140.     }
  141.     if (_font) {
  142.         SimpleBind b (this, &UI_SimpleVObject::_FontChanged);
  143.         _font->AddDependent (b, 1);
  144.     }
  145. #if defined (__MS_WINDOWS__)
  146.     UI_ResourceHandle h = _font ? _font->Handle() : 0;
  147.     if (h && !_parent->CreatedViaResource()) {
  148.         SendMessage (_handle, WM_SETFONT, h, TRUE);
  149.     }
  150.     // The following hack is needed to force the control to be shown in the
  151.     // case where it is a child of a window of YACLWINDOW class. Without
  152.     // this hack, the control will not be shown the first time the parent
  153.     // window is created.
  154.     SetWindowPos  (_handle, NULL, 0, 0, 0, 0, SWP_SHOWWINDOW | 
  155.                    SWP_NOSIZE | SWP_NOMOVE | 
  156.                    SWP_NOACTIVATE | SWP_NOZORDER); // Force re-draw
  157. #elif defined (__X_MOTIF__)
  158.     if (_font)
  159.         SetFont (_font);
  160.  
  161. #endif
  162. }
  163.  
  164.  
  165.  
  166.  
  167. bool UI_SimpleVObject :: DestroyVisualElement ()
  168. {
  169.     return UI_VisualObject::DestroyVisualElement();
  170. }
  171.  
  172.  
  173.  
  174. static bool _SetPlatformFont (UI_ViewHandle handle, UI_Font& font)
  175. {
  176. #if defined (__MS_WINDOWS__)
  177.     UI_ResourceHandle h = font.Handle();
  178.     if (h > 0 && handle > 0) {
  179.         return SendMessage (handle, WM_SETFONT, h, TRUE);
  180.     }
  181.     return TRUE;
  182.  
  183. #elif defined(__OS2__)
  184.     CL_String fontString = CL_String (font.PointSize()) + "." +
  185.         font.TypeFace();
  186.     ulong style = font.Style ();
  187.     if (style & UIFont_BoldFace)
  188.         fontString += ".Bold";
  189.     if (style & UIFont_Underline)
  190.         fontString += ".Underscore";
  191.     if (style & UIFont_StrikeOut)
  192.         fontString += ".Strikeout";
  193.     if (style & UIFont_Italic)
  194.         fontString += ".Italic";
  195.     return WinSetPresParam (handle, PP_FONTNAMESIZE, fontString.Size()+1,
  196.                             (void*) fontString.AsPtr());
  197. #elif defined (__X_MOTIF__)
  198.     UI_ResourceHandle h = font.Handle();
  199.     if (h <= 0 || !handle)
  200.         return FALSE;
  201.  
  202.     Display *dpy = XtDisplay (handle);
  203.     XmFontList f = XmFontListCreate (XQueryFont (dpy, h),
  204.                                      XmSTRING_DEFAULT_CHARSET);
  205.     Arg arg [1];
  206.     XtSetArg       (arg [0], XmNfontList,  f);
  207.     XtSetValues    (handle, arg, 1);
  208.     XmFontListFree (f);
  209.     return TRUE;
  210.     
  211. #endif
  212. }
  213.  
  214.  
  215.  
  216. bool UI_SimpleVObject :: _FontChanged (CL_Object&, long)
  217. {
  218.     return _SetPlatformFont (ViewHandle(), *_font);
  219. }
  220.  
  221.  
  222.  
  223. bool UI_SimpleVObject :: SetFont (UI_Font* fnt)
  224. {
  225.     if (!UI_VisualObject::SetFont (fnt))
  226.         return FALSE;
  227.     return _font ? _SetPlatformFont (ViewHandle(), *_font) : FALSE;
  228. }
  229.  
  230.  
  231.  
  232.  
  233. UI_WindowClass UI_SimpleVObject::WindowClass () const
  234. {
  235. #if defined(__MS_WINDOWS__)
  236.     return _YACLWindowClassName;
  237. #elif defined(__OS2__)
  238.     return _YACLWindowClassName;
  239. #elif defined(__X_MOTIF__)
  240.     return shellWidgetClass;
  241. #endif
  242. }
  243.  
  244.  
  245. bool UI_SimpleVObject :: SetStyleParam ( )
  246. {
  247.     // This is used only for resource-based construction
  248. #if defined(__MS_WINDOWS__)
  249.     RECT rec;
  250.  
  251.     _id = GetWindowWord    (_handle,GWW_ID);  
  252.     _style = GetWindowLong (_handle,GWL_STYLE); 
  253.     _visible = (_style & WS_VISIBLE) ? TRUE : FALSE;
  254.     _isTabStop = _style & WS_TABSTOP ? TRUE : FALSE;
  255.     GetWindowRect (_handle, &rec);
  256.     POINT array[2];
  257.     array[0].x = rec.left;
  258.     array[0].y = rec.top;
  259.     array[1].x = rec.right;
  260.     array[1].y = rec.bottom;
  261.     MapWindowPoints (NULL, _parent->ViewHandle(), array, 2);
  262.     UI_Point org(array[0].x, array[0].y);
  263.     UI_Rectangle shp(org, array[1].x-array[0].x, array[1].y-array[0].y);
  264.     _SetShapeRectangle (shp);
  265.  
  266. #else
  267.     NotImplemented ("SetStyleParam");
  268. #endif
  269.     return TRUE;
  270. }
  271.  
  272.  
  273.  
  274.  
  275. #if defined(__OS2__)
  276. MRESULT EXPENTRY UI_SimpleVObject::SubclassProc
  277.     (HWND hWnd, ULONG msg, MPARAM p1, MPARAM p2)
  278. {
  279.     UI_SimpleVObject* obj = (UI_SimpleVObject*) (*_Controller)[hWnd];
  280.     if (obj) {
  281.         MRESULT retVal = (*(obj->_oldProc)) (hWnd, msg, p1, p2);
  282.         if (msg == WM_MOUSEMOVE)
  283.             _Controller->SetCurrentCursor (obj->Cursor());
  284.         return retVal;
  285.     }
  286.     return WinDefWindowProc (hWnd, msg, p1, p2);
  287. }
  288.  
  289.  
  290. void UI_SimpleVObject::_Subclass ()
  291. {
  292.     _oldProc = WinSubclassWindow (_handle, UI_SimpleVObject::SubclassProc);
  293. }
  294. #endif
  295.  
  296.