home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / stred.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  7KB  |  282 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.  
  37. #include "ui/stred.h"
  38. #include "ui/cntroler.h"
  39.  
  40. #if defined(__MS_WINDOWS__)
  41. #include <windows.h>
  42. #define DEFAULT_STYLE \
  43.     ES_LEFT | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | WS_GROUP
  44. #elif defined(__OS2__)
  45. #define DEFAULT_STYLE WS_VISIBLE | ES_MARGIN
  46. #elif defined(__X_MOTIF__)
  47. #include <Xm/Text.h> 
  48. #endif
  49.  
  50.  
  51. typedef CL_Binding<UI_StringEditor> StrEdBind;
  52. #if defined(__GNUC__)
  53. template class CL_Binding<UI_StringEditor>;
  54. #endif
  55.  
  56.  
  57. UI_StringEditor::UI_StringEditor
  58.     (UI_CompositeVObject* parent, const UI_Rectangle& shape,
  59.      UI_ViewID id, long s
  60.     )
  61. : UI_SimpleVObject (parent, shape, id, s)
  62. {
  63.     _model = new CL_String; 
  64.     _limit = -1;
  65.     StrEdBind bind (this, (StrEdBind::MethodPtr)
  66.                     &UI_StringEditor::_SelectionChanged);
  67.     _selection.AddDependent (bind, 1);
  68. #if defined(__MS_WINDOWS__)
  69.     if ( _style == -1 )
  70.         _style =  DEFAULT_STYLE;
  71. #elif defined(__OS2__)
  72.     _style = DEFAULT_STYLE;
  73. #endif
  74. }
  75.  
  76.  
  77.  
  78. #if defined(__MS_WINDOWS__)
  79. UI_StringEditor::UI_StringEditor (UI_CompositeVObject* parent,
  80.                                   UI_ViewID id, UI_ViewHandle h)
  81. : UI_SimpleVObject (parent, id, h)
  82. {
  83.     _model = new CL_String;
  84.     _limit = -1;
  85.     StrEdBind bind (this, (StrEdBind::MethodPtr)
  86.                     &UI_StringEditor::_SelectionChanged);
  87.     _selection.AddDependent (bind, 1);
  88. }
  89. #endif
  90.  
  91.  
  92. UI_StringEditor::UI_StringEditor
  93.     (UI_CompositeVObject* parent, CL_String* model,
  94.      const UI_Rectangle& shape, UI_ViewID id, long s)
  95. : UI_SimpleVObject(parent, model, id, shape, s)
  96. {
  97.     _limit = -1;
  98. #if defined(__MS_WINDOWS__)
  99.     if (_style == -1)
  100.         _style = DEFAULT_STYLE;
  101. #elif defined(__OS2__)
  102.     _style = DEFAULT_STYLE;
  103. #endif
  104.     StrEdBind bind (this, (StrEdBind::MethodPtr)
  105.                     &UI_StringEditor::_SelectionChanged);
  106.     _selection.AddDependent (bind, 1);
  107. }
  108.  
  109.  
  110.  
  111. #if defined(__X_MOTIF__)
  112. void UI_StringEditor::_SetupStyle (void* p, short& argn)
  113. {
  114.     Arg* arg = (Arg*) p;
  115.     UI_SimpleVObject::_SetupStyle (p, argn);
  116.     XtSetArg (arg[argn], XmNeditMode, XmSINGLE_LINE_EDIT); argn++;
  117. }
  118. #endif
  119.  
  120.  
  121. void UI_StringEditor::_PrivateInitialize ()
  122. {
  123.     UI_SimpleVObject::_PrivateInitialize ();
  124. #if defined(__MS_WINDOWS__)
  125.     CL_String& s = *((CL_String*) _model);
  126.     if (s.Size() > 0)
  127.         SendMessage (_handle, EM_REPLACESEL, 0, (long) s.AsPtr());
  128. #elif defined(__OS2__)
  129.     CL_String& s = *((CL_String*) _model);
  130.     if (s.Size() > 0)
  131.         WinSetWindowText (_handle, s.AsPtr());
  132. #elif defined(__X_MOTIF__)
  133.     XmTextSetString (_xwidget, (char*) ((CL_String*) _model)->AsPtr());
  134. #endif
  135. }
  136.  
  137.  
  138.  
  139. UI_WindowClass UI_StringEditor::WindowClass () const
  140. {
  141. #if defined(__MS_WINDOWS__)
  142.     return "edit";
  143. #elif defined(__OS2__)
  144.     return WC_ENTRYFIELD;
  145. #elif defined(__X_MOTIF__)
  146.     return  xmTextWidgetClass;
  147. #endif
  148. }
  149.  
  150.  
  151.  
  152. //
  153. // -----------------StringEditor Event methods--------------------
  154. //
  155.  
  156.  
  157.  
  158. CL_Interval& UI_StringEditor::Selection ()
  159. {
  160.     StrEdBind bind (this, (StrEdBind::MethodPtr)
  161.                     &UI_StringEditor::_SelectionChanged);
  162.     _selection.RemoveDependent (bind);
  163. #if defined(__MS_WINDOWS__)
  164.     DWORD pos;
  165.     pos = SendMessage  (_handle, EM_GETSEL, 0, 0);
  166.     _selection = CL_Interval (LOWORD(pos), HIWORD(pos)-1);
  167.  
  168. #elif defined(__OS2__)
  169.     MPARAM pos = WinSendMsg  (_handle, EM_QUERYSEL, 0, 0);
  170.     _selection = CL_Interval (SHORT1FROMMP(pos), SHORT2FROMMP(pos)-1);
  171.  
  172. #elif defined(__X_MOTIF__)
  173.     XmTextPosition left, right;
  174.     XmTextGetSelectionPosition(_xwidget, &left, &right);
  175.     _selection = CL_Interval ((long) left, (long) right);
  176. #endif
  177.     _selection.AddDependent (bind, 1);
  178.     return _selection;
  179. }
  180.  
  181.  
  182. CL_Object& UI_StringEditor::Model ()
  183. {
  184.     if (!ViewHandle()) {
  185.         _SetModelValue (CL_String (""));
  186.         return *_model;
  187.     }
  188. #if defined(__MS_WINDOWS__)
  189.     long n = GetWindowTextLength (_handle);
  190.     char* buf = new char[n+1];
  191.     if (!buf)
  192.         return *_model; // No memory
  193.     GetWindowText (_handle, buf, n+1);
  194.     _SetModelValue (CL_String (buf));
  195.     delete buf;
  196.  
  197. #elif defined(__OS2__)
  198.     long n = WinQueryWindowTextLength (_handle);
  199.     char* buf = new char[n+1];
  200.     if (!buf)
  201.         return *_model; // No memory
  202.     WinQueryWindowText (_handle,  n+1, buf);
  203.     _SetModelValue (CL_String (buf));
  204.     delete buf;
  205. #elif defined(__X_MOTIF__)
  206.     char* p =  XmTextGetString (_xwidget);
  207.     _SetModelValue (CL_String (p));
  208.     XtFree (p);
  209. #endif
  210.     return *_model;
  211. }
  212.  
  213.  
  214. bool UI_StringEditor::SetLengthLimit (long n)
  215. {
  216. #if defined (__MS_WINDOWS__)
  217.     if (n > 0 && n <= 32767) {
  218.         _limit = n;
  219.         return SendMessage  (_handle, EM_LIMITTEXT, n, 0);
  220.     }
  221.     return FALSE;
  222. #elif defined (__OS2__)
  223.     if (n > 0 && n <= 32767) {
  224.         _limit = n;
  225.         WinSendMsg  (_handle, EM_SETTEXTLIMIT, MPFROM2SHORT(n, 0), 0);
  226.         return TRUE;
  227.     }
  228.     return FALSE;
  229. #elif defined (__X_MOTIF__)
  230.     if (n > 0) {
  231.         _limit = n;
  232.         Arg arg[1];
  233.         XtSetArg (arg[0], XmNmaxLength, n);
  234.         XtSetValues (_xwidget, arg, 1);
  235.     }
  236.     return TRUE;
  237. #endif
  238. }
  239.  
  240.  
  241. bool UI_StringEditor::_ModelChanged (CL_Object&, long)
  242. {
  243. #if defined(__MS_WINDOWS__)
  244.     CL_String& s = *((CL_String*) _model);
  245.     if (_handle > 0 && s.Size() > 0)
  246.         SendMessage (_handle, EM_REPLACESEL, 0, (long) s.AsPtr());
  247. #elif defined(__OS2__)
  248.     CL_String& s = *((CL_String*) _model);
  249.     if (_handle && s.Size() > 0)
  250.         WinSetWindowText (_handle, s.AsPtr());
  251. #elif defined(__X_MOTIF__)
  252.     if (_xwidget) {
  253.         XmTextSetString (_xwidget, (char*) ((CL_String*) _model)->AsPtr());
  254.         XmUpdateDisplay  (_xwidget);
  255.     }
  256. #endif
  257.     return TRUE;
  258. }
  259.  
  260.  
  261.  
  262. bool UI_StringEditor::_SelectionChanged (CL_Object&, long)
  263. {
  264. #if defined(__MS_WINDOWS__)
  265.     if (_handle > 0) {
  266.         SendMessage (_handle, EM_SETSEL, TRUE,
  267.                      MAKELPARAM (_selection.Low(), _selection.High()+1));
  268.     }
  269. #elif defined(__OS2__)
  270.     if (_handle) {
  271.         WinSendMsg (_handle, EM_SETSEL, MPFROM2SHORT
  272.                     (_selection.Low(), _selection.High()+1), 0);
  273.     }
  274. #elif defined(__X_MOTIF__)
  275.     if (_xwidget)
  276.         XmTextSetSelection (_xwidget, _selection.Low(), _selection.High(), 0);
  277. #endif
  278.     return TRUE;
  279. }
  280.  
  281.  
  282.