home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / cursor.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  7KB  |  289 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. #if defined(__GNUC__)
  34. #pragma implementation
  35. #endif
  36.  
  37.  
  38. #include "base/map.h"
  39. #include "ui/cursor.h"
  40. #include "ui/applic.h"
  41. #include "ui/cntroler.h"
  42.  
  43. #ifdef __MS_WINDOWS__
  44. #include <windows.h>
  45. #endif
  46.  
  47. #if defined (__X_MOTIF__)
  48. #include <X11/cursorfont.h>
  49. #include <X11/Xlib.h>
  50. #include <X11/Intrinsic.h>
  51. #include <iostream.h> // DEBUG
  52. #endif
  53.  
  54.  
  55. // We have a static map that contains handles to the built-in cursors
  56. // here:
  57.  
  58. class CursorMap: public CL_IntIntMap {
  59.  
  60. public:
  61.     CursorMap ();
  62.     ~CursorMap ();
  63. };
  64.  
  65.  
  66. #if defined(__MS_WINDOWS__)
  67. static struct CodeAssoc {
  68.     LPSTR code;
  69.     UI_CursorType type;
  70. } CursorCode [] = {
  71.     (LPSTR) IDC_ARROW,  UICursor_Arrow,
  72.     (LPSTR) IDC_WAIT,   UICursor_Wait,
  73.     (LPSTR) IDC_CROSS,  UICursor_CrossHairs,
  74.     (LPSTR) IDC_IBEAM,  UICursor_IBeam
  75. };
  76. #endif
  77.  
  78.  
  79. CursorMap::CursorMap ()
  80. {
  81. }
  82.  
  83. CursorMap::~CursorMap ()
  84. {
  85. }
  86.  
  87.  
  88.  
  89. static CursorMap BuiltIn;  // STATIC OBJECT!!
  90.  
  91.  
  92. static UI_CursorHandle MakeCursor (UI_CursorType type)
  93. {
  94. #if defined(__MS_WINDOWS__)
  95.     HCURSOR h;
  96.     BuiltIn.Add (UICursor_Default, 0);
  97.     if (type == UICursor_Default)
  98.         return 0;
  99.     short n = sizeof CursorCode / sizeof (CodeAssoc);
  100.     for (short i = 0; i < n; i++) {
  101.         if (CursorCode[i].type == type)
  102.             break;
  103.     }
  104.     h = 0;
  105.     if (i < n) {
  106.         h = LoadCursor (NULL, CursorCode[i].code);
  107.         if (h)
  108.             BuiltIn.Add (CursorCode[i].type, h);
  109.     }
  110.     if (!h)
  111.         CL_Error::Warning ("YACL: UI_Cursor Init: Invalid cursor %d",
  112.                            type);
  113.     return h;
  114. #elif defined(__OS2__)
  115.     ULONG c;
  116.     switch (type) {
  117.     case UICursor_Wait:
  118.         c = SPTR_WAIT;
  119.         break;
  120.         
  121.     case UICursor_IBeam:
  122.         c = SPTR_TEXT;
  123.         break;
  124.         
  125.     case UICursor_CrossHairs: // OS/2 doesn't have a cross-hair cursor
  126.                               // built in? 
  127.     case UICursor_Arrow:
  128.     case UICursor_Default:
  129.     default:
  130.         c = SPTR_ARROW;
  131.         break;
  132.     };
  133.         
  134.     UI_CursorHandle h = WinQuerySysPointer (HWND_DESKTOP, c, FALSE);
  135.     if (h)
  136.         BuiltIn.Add ((long) type, h);
  137.     return h;
  138. #elif defined(__X_MOTIF__)
  139.     if (type == UICursor_Default)
  140.         return 0;
  141.     
  142.     Cursor c;
  143.     switch (type) {
  144.     case UICursor_Wait:
  145.         c = XC_watch;
  146.         break;
  147.         
  148.     case UICursor_CrossHairs:
  149.         c = XC_crosshair;
  150.         break;
  151.         
  152.     case UICursor_IBeam:
  153.         c = XC_xterm;
  154.         break;
  155.         
  156.     case UICursor_Arrow:
  157.     default:
  158.         c = XC_top_left_arrow;
  159.         break;
  160.     };
  161.         
  162.     UI_CursorHandle h = XCreateFontCursor
  163.         (XtDisplay (_TheApplication->Controller().ShellWidget()), c);
  164.     if (h)
  165.         BuiltIn.Add ((long) type, h);
  166.     return h;
  167. #endif
  168. }
  169.  
  170.  
  171.  
  172. UI_Cursor::UI_Cursor (UI_CursorType type)
  173. {
  174.     _type = type;
  175.     if (BuiltIn.IncludesKey ((long) type))
  176.         _handle = BuiltIn[(long) type];
  177.     else
  178.         _handle = MakeCursor (type);
  179. }
  180.  
  181.  
  182.  
  183. UI_Cursor::~UI_Cursor ()
  184. {
  185. #if defined (__MS_WINDOWS__)
  186.     if (_type == UICursor_Default && _handle)
  187.         DestroyCursor (_handle);
  188. #endif
  189. }
  190.  
  191.  
  192. void UI_Cursor::operator= (UI_CursorType type)
  193. {
  194. #if defined (__MS_WINDOWS__)
  195.     if (BuiltIn.IncludesKey ((long) type))
  196.         _handle = BuiltIn[(long) type];
  197.     else {
  198.         HCURSOR h = MakeCursor (type);
  199.         if (h) {
  200.             _Destroy ();
  201.             _handle = h;
  202.             _type   = type;
  203.         }
  204.         else
  205.             CL_Error::Warning ("YACL: UI_Cursor::op=: cursor type %d: "
  206.                                "unknown type", type);
  207.     }
  208. #elif defined(__OS2__) || defined(__X_MOTIF__)
  209.     _type = type;
  210.     if (BuiltIn.IncludesKey ((long) type))
  211.         _handle = BuiltIn[(long) type];
  212.     else
  213.         _handle = MakeCursor (type);
  214. #endif
  215. }
  216.  
  217.  
  218. // Change this cursor to be the one with the given name
  219. void UI_Cursor::operator= (const char* cursor_name)
  220. {
  221. #if defined (__MS_WINDOWS__)
  222.     HCURSOR h = LoadCursor (_TheApplication->ProcessId(), cursor_name);
  223.     if (h) {
  224.         _Destroy();
  225.         _handle = h;
  226.         _name = cursor_name;
  227.         _type = UICursor_Default;
  228.     }
  229.     else 
  230.         CL_Error::Warning ("YACL: Cursor::op= (string): LoadCursor "
  231.                            "%s failed", cursor_name);
  232. #else
  233.     CL_Error::Warning ("Cursor::op= ('%s'): "
  234.                        "not yet implemented", cursor_name);
  235. #endif
  236. }
  237.  
  238.  
  239. void UI_Cursor::_Destroy ()
  240. {
  241. #if defined(__MS_WINDOWS__)
  242.     if (BuiltIn.IncludesKey (_type))
  243.         return; // Do not destroy built-in cursors
  244.     if (_handle)
  245.         DestroyCursor (_handle);
  246.     _handle = 0;
  247.     _name = "";
  248.     _type = UICursor_Default;
  249. #endif
  250. }
  251.  
  252.  
  253.  
  254. bool UI_Cursor::operator== (const UI_Cursor& c) const
  255. {
  256.     return (_type != UICursor_Default) ? (_type == c._type)
  257.         : (_name == c._name);
  258. }
  259.  
  260.  
  261.  
  262.  
  263. #if defined(__OS2__)
  264. // static BYTE CrossHairBitmap[] = {
  265. //     0x00, 0x01, 0x00, 0x00,
  266. //     0x00, 0x01, 0x00, 0x00,
  267. //     0x00, 0x01, 0x00, 0x00,
  268. //     0x00, 0x01, 0x00, 0x00,
  269. // 
  270. //     0x00, 0x01, 0x00, 0x00,
  271. //     0x00, 0x01, 0x00, 0x00,
  272. //     0x00, 0x01, 0x00, 0x00,
  273. //     0xff, 0xff, 0xff, 0xff,
  274. // 
  275. //     0x00, 0x01, 0x00, 0x00,
  276. //     0x00, 0x01, 0x00, 0x00,
  277. //     0x00, 0x01, 0x00, 0x00,
  278. //     0x00, 0x01, 0x00, 0x00,
  279. // 
  280. //     0x00, 0x01, 0x00, 0x00,
  281. //     0x00, 0x01, 0x00, 0x00,
  282. //     0x00, 0x01, 0x00, 0x00,
  283. //     0x00, 0x01, 0x00, 0x00
  284. // };
  285.  
  286.     
  287. #endif
  288.  
  289.