home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / label.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  8KB  |  321 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. #if defined(__GNUC__)
  33. #pragma implementation
  34. #endif
  35.  
  36. #include "ui/label.h"
  37. #include "ui/cntroler.h"
  38. #include "ui/composit.h"
  39.  
  40. #if defined(__MS_WINDOWS__)
  41. #include <windows.h>
  42.  
  43. #elif defined(__X_MOTIF__)
  44. #include <Xm/Label.h>
  45. #include <Xm/Xm.h>
  46. #endif
  47.  
  48.  
  49. #if defined(__MS_WINDOWS__)
  50. #define LABEL_STYLE WS_VISIBLE  | WS_CHILD 
  51.  
  52. static ulong WindowsStyle (UI_TextStyle style)
  53. {
  54.     switch (style) {
  55.     case UIText_Left:
  56.         return SS_LEFT;
  57.         
  58.     case UIText_Center:
  59.         return SS_CENTER;
  60.         
  61.     case UIText_Right:
  62.         return SS_RIGHT;
  63.  
  64.     default:
  65.         return 0;
  66.     }
  67. }
  68.  
  69. #elif defined(__OS2__)
  70. static ulong OS2Style (UI_TextStyle style)
  71. {
  72.     switch (style) {
  73.     case UIText_Left:
  74.         return DT_LEFT;
  75.         
  76.     case UIText_Center:
  77.         return DT_CENTER;
  78.         
  79.     case UIText_Right:
  80.         return DT_RIGHT;
  81.  
  82.     default:
  83.         return 0;
  84.     }
  85. }
  86.  
  87. #elif defined(__X_MOTIF__)
  88.  
  89. static uchar XMotifStyle (UI_TextStyle style)
  90. {
  91.     switch (style) {
  92.     case UIText_Left:
  93.         return XmALIGNMENT_BEGINNING;
  94.         
  95.     case UIText_Center:
  96.         return XmALIGNMENT_CENTER;
  97.         
  98.     case UIText_Right:
  99.         return XmALIGNMENT_END;
  100.  
  101.     default:
  102.         return 0;
  103.     }
  104. }
  105.  
  106. #endif
  107.  
  108.  
  109. UI_Label::UI_Label ( UI_VObjCollection* parent, 
  110.                      const UI_Rectangle& r, 
  111.                      UI_ViewID id)
  112. : UI_SimpleVObject (parent, r, id
  113. #if defined(__MS_WINDOWS__)
  114.                     , LABEL_STYLE
  115. #endif
  116.                     )
  117. {
  118.     _model = &_title;
  119.     _ownModel = FALSE; // So that we don't attempt to delete the title
  120.     _textStyle = UIText_Center;
  121.     _borderShown = FALSE; // By default
  122.     _isTabStop = FALSE;
  123. #if defined(__MS_WINDOWS__)
  124.     _style |= WindowsStyle (_textStyle);
  125. #elif defined(__OS2__)
  126.     _style = (_visible ? WS_VISIBLE : 0) | SS_TEXT | DT_WORDBREAK |
  127.         OS2Style (_textStyle);
  128. #endif
  129. }
  130.  
  131.  
  132.  
  133. #if defined(__MS_WINDOWS__)
  134. UI_Label :: UI_Label ( UI_CompositeVObject* parent, UI_ViewID id,
  135.                        UI_ViewHandle h)
  136. : UI_SimpleVObject (parent, id, h)
  137. {
  138.     _model    = &_title;
  139.     _ownModel = FALSE;
  140.     _borderShown = FALSE; // By default
  141.     _isTabStop = FALSE;
  142. }
  143. #endif
  144.  
  145.  
  146. UI_Label :: UI_Label ( UI_VObjCollection* parent, CL_String* msg,
  147.                        const UI_Rectangle& r, UI_ViewID id)
  148. : UI_SimpleVObject (parent, msg, id, r
  149. #if defined(__MS_WINDOWS__)
  150.                     , LABEL_STYLE
  151. #endif
  152.                     )
  153. {
  154.     _borderShown = FALSE; // By default
  155.     _textStyle = UIText_Center;
  156.     _isTabStop = FALSE;
  157. #if defined(__MS_WINDOWS__)
  158.     _style |= WindowsStyle (_textStyle);
  159. #elif defined(__OS2__)
  160.     _style = (_visible ? WS_VISIBLE : 0) | SS_TEXT | DT_CENTER;
  161. #endif
  162. }
  163.  
  164.  
  165. #if defined(__X_MOTIF__)
  166. void UI_Label::_SetupStyle (void* p, short& argn)
  167. {
  168.     Arg* arg = (Arg*) p;
  169.  
  170.     UI_SimpleVObject::_SetupStyle (arg, argn);
  171.     // Add other resource specs:
  172.     XtSetArg (arg [argn], XmNrecomputeSize, FALSE); argn++;
  173.     XtSetArg (arg [argn], XmNalignment, XMotifStyle (_textStyle)); argn++;
  174. }
  175.  
  176. #endif
  177.  
  178.  
  179.  
  180. void UI_Label :: _PrivateInitialize ( )
  181. {
  182.     UI_SimpleVObject :: _PrivateInitialize ( );
  183.     CL_String& p = *(CL_String *) _model;
  184.     if (p.Size () <= 0 )
  185.         return;
  186. #if defined(__MS_WINDOWS__)
  187.     SendMessage (_handle, WM_SETTEXT, 0, (long) p.AsPtr());   
  188. #elif defined(__X_MOTIF__)
  189.     Arg arg[1];
  190.     XmString xmlabel;
  191.     char *label = (char *) p.AsPtr ( );
  192.     xmlabel = XmStringCreateLtoR ( label, XmSTRING_DEFAULT_CHARSET );
  193.     XtSetArg (arg[0], XmNlabelString, xmlabel);
  194.     XtSetValues (_xwidget, arg, 1);
  195.     XmStringFree (xmlabel);
  196. #endif
  197. }
  198.  
  199.  
  200. bool UI_Label :: _TitleChanged ( CL_Object&, long )
  201. {
  202. #if defined(__MS_WINDOWS__)
  203.     if ( _handle > 0 ) {
  204.         SendMessage (_handle, WM_SETTEXT, 0,
  205.                      (long) ((CL_String*) _model)->AsPtr() );   
  206.  
  207.         // For some reason, if I don't force a redraw via SetWindowPos, the
  208.         // text doesn't get (reliably)  shown, so:
  209.         SetWindowPos  (_handle, NULL, 0, 0, 0, 0, SWP_SHOWWINDOW | 
  210.                        SWP_NOSIZE | SWP_NOMOVE | 
  211.                        SWP_NOACTIVATE | SWP_NOZORDER); // Force re-draw
  212.     }
  213. #elif defined(__OS2__)
  214.     if (_handle > 0) {
  215.         WinSetWindowText (_handle, _title.AsPtr());
  216.     }
  217. #elif defined(__X_MOTIF__)
  218.     if (!_xwidget)
  219.         return TRUE;
  220.     Arg arg[1];
  221.     XmString label;
  222.     label = XmStringCreateLtoR ((char *) ( (CL_String *)_model )->AsPtr (), 
  223.                                 XmSTRING_DEFAULT_CHARSET 
  224.                                 );    
  225.     XtSetArg    (arg[0], XmNlabelString, label);
  226.     XtSetValues (_xwidget, arg, 1);
  227.  
  228.     XmUpdateDisplay (_xwidget);
  229.     XmStringFree (label);
  230. #endif
  231.  
  232.     return TRUE;
  233. }
  234.  
  235.  
  236.  
  237.  
  238. bool UI_Label::SetTextStyle (UI_TextStyle style)
  239. {
  240.     if (style != UIText_Left && style != UIText_Center && style !=
  241.         UIText_Right)
  242.         return FALSE;
  243.     _textStyle = style;
  244. #if defined(__MS_WINDOWS__)
  245.     if (_handle > 0)
  246.         _style = GetWindowLong (_handle, GWL_STYLE);
  247.     _style = (_style & ~(ES_LEFT | ES_CENTER | ES_RIGHT))
  248.         | WindowsStyle (_textStyle);
  249.     if (_handle >  0) {
  250.         _style |= WindowsStyle (_textStyle);
  251.         SetWindowLong (_handle, GWL_STYLE, _style);
  252.         SetWindowPos  (_handle, NULL, 0, 0, 0, 0, SWP_SHOWWINDOW | 
  253.                        SWP_NOSIZE | SWP_NOMOVE | 
  254.                        SWP_NOACTIVATE | SWP_NOZORDER); // Force re-draw
  255.     }
  256.     return TRUE;
  257. #elif defined(__OS2__)
  258.     if (_handle > 0)
  259.         _style = WinQueryWindowULong (_handle, QWL_STYLE);
  260.     _style = (_style & ~(DT_LEFT | DT_CENTER | DT_RIGHT))
  261.         | OS2Style (_textStyle);
  262.     if (_handle >  0) {
  263.         _style |= OS2Style (_textStyle);
  264.         WinSetWindowULong (_handle, QWL_STYLE, _style);
  265.         if (_visible) {
  266.             // This is how we force a redraw:
  267.             WinShowWindow (_handle, FALSE);
  268.             WinShowWindow (_handle, TRUE);
  269.         }
  270.     }
  271.     return TRUE;
  272. #elif defined(__X_MOTIF__)
  273.     if (!_xwidget)
  274.         return TRUE;
  275.     // Since we can't set the alignment resource after widget creation, we
  276.     // just get rid of the widget and make up a brand new one.
  277.     XtDestroyWidget (_xwidget);
  278.     UI_VisualObject* p = _parent;
  279.     Widget pw = (Widget) p->ViewHandle();
  280.  
  281.     CL_String instance_name = InstanceName();
  282.     const char* inst_name = instance_name.AsPtr();
  283.     struct _WidgetClassRec *class_name = WindowClass ();
  284.  
  285.     Arg arg [20];
  286.     short argn = 0;
  287.     _SetupStyle (arg, argn); // Set up the X resources
  288.     XmString title = XmStringCreate
  289.         ((char *) _title.AsPtr (), XmSTRING_DEFAULT_CHARSET);    
  290.     XtSetArg (arg [argn], XmNlabelString, title); argn++;
  291.     _xwidget = XtCreateWidget (inst_name, class_name, pw, arg, argn);
  292.     XtRealizeWidget (_xwidget);
  293.     if (_visible)
  294.         XtManageChild (_xwidget);
  295.     XmStringFree (title);
  296.  
  297.     return TRUE;
  298. #endif
  299.         
  300. }
  301.  
  302.  
  303. UI_TextStyle UI_Label::TextStyle() const
  304. {
  305.     return _textStyle;
  306. }
  307.  
  308.  
  309.  
  310. UI_WindowClass UI_Label::WindowClass () const
  311. {
  312. #if defined(__MS_WINDOWS__)
  313.     return "static";
  314. #elif defined(__X_MOTIF__)
  315.     return xmLabelWidgetClass;
  316. #elif defined(__OS2__)
  317.     return WC_STATIC;
  318. #endif
  319. }
  320.  
  321.