home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 215 / DDJ11A92.ZIP / CPROG.ASC < prev    next >
Text File  |  1992-10-29  |  11KB  |  366 lines

  1. _C PROGRAMMING COLUMN_
  2. by Al Stevens
  3.  
  4. [LISTING ONE]
  5.  
  6. // ---------- desktop.h
  7. #ifndef DESKTOP_H
  8. #define DESKTOP_H
  9.  
  10. #include "screen.h"
  11. #include "cursor.h"
  12. #include "keyboard.h"
  13. #include "mouse.h"
  14. #include "speaker.h"
  15. #include "clock.h"
  16.  
  17. class DFWindow;
  18.  
  19. class DeskTop {
  20.     DFWindow *apwnd;        // application window
  21.     DFWindow *infocus;      // current window with the focus
  22.     DFWindow *firstcapture; // first window to capture the focus
  23.     DFWindow *focuscapture; // current window with captured focus
  24.     // ------- the desktop devices
  25.     Screen   sysscreen;     // the system screen
  26.     Mouse    sysmouse;      // the system mouse
  27.     Keyboard syskeyboard;   // the system keyboard
  28.     Cursor   syscursor;     // the system cursor
  29.     Clock    sysclock;      // the system clock
  30.     Speaker  sysspeaker;    // the system speaker
  31. public:
  32.     DeskTop();
  33.     ~DeskTop();
  34.     DFWindow *ApplWnd() { return apwnd; }
  35.     void SetApplication(DFWindow *ApWnd) { apwnd = ApWnd; }
  36.     Bool DispatchEvents();
  37.     DFWindow *InFocus()      { return infocus; }
  38.     DFWindow *FocusCapture() { return focuscapture; }
  39.     DFWindow *FirstCapture() { return firstcapture; }
  40.     void SetFocus(DFWindow *wnd) { infocus = wnd; }
  41.     void SetFocusCapture(DFWindow *wnd) { focuscapture = wnd; }
  42.     void SetFirstCapture(DFWindow *wnd) { firstcapture = wnd; }
  43.     // ------- the desktop devices
  44.     Mouse&    mouse()       { return sysmouse;    }
  45.     Screen&   screen()      { return sysscreen;   }
  46.     Keyboard& keyboard()    { return syskeyboard; }
  47.     Cursor&   cursor()      { return syscursor;   }
  48.     Clock&    clock()       { return sysclock;    }
  49.     Speaker&  speaker()     { return sysspeaker;  }
  50. };
  51. extern DeskTop desktop;
  52.  
  53. #endif
  54.  
  55.  
  56. [LISTING TWO]
  57.  
  58. // ----------- screen.h
  59. #ifndef SCREEN_H
  60. #define SCREEN_H
  61.  
  62. #include <dos.h>
  63. #include "dflatdef.h"
  64. #include "rectangl.h"
  65.  
  66. class Screen    {
  67.     unsigned address;
  68.     unsigned mode;
  69.     unsigned page;
  70.     unsigned height;
  71.     unsigned width;
  72.     union REGS regs;
  73.     // ---- compute video offset address
  74.     unsigned vad(int x, int y) { return y * (width*2) + x*2; }
  75. public:
  76.     Screen();
  77.     unsigned Height() { return height; }
  78.     unsigned Width()  { return width; }
  79.     unsigned Page()   { return page; }
  80.     Bool isEGA();
  81.     Bool isVGA();
  82.     Bool isMono() { return (Bool) (mode == 7); }
  83.     Bool isText() { return (Bool) (mode < 4);  }
  84.     void Scroll(Rect &rc, int d, int fg, int bg);
  85.     unsigned int GetVideoChar(int x, int y);
  86.     void PutVideoChar(int x, int y, unsigned int c);
  87.     void WriteVideoString(char *s,int x,int y,int fg,int bg);
  88.     void SwapBuffer(Rect &rc, char *bf,
  89.         Bool Hiding, Bool HasShadow, Bool isFrame);
  90.     void GetBuffer(Rect &rc, char *bf);
  91.     void PutBuffer(Rect &rc, char *bf);
  92. };
  93. const int VIDEO = 0x10;
  94. inline int clr(int fg, int bg)
  95. {
  96.     return fg | (bg << 4);
  97. }
  98. #endif
  99.  
  100.  
  101.  
  102.  
  103. [LISTING THREE]
  104.  
  105. // ---------- mouse.h
  106. #ifndef MOUSE_H
  107. #define MOUSE_H
  108.  
  109. #include <dos.h>
  110. #include "dfwindow.h"
  111. #include "timer.h"
  112.  
  113. class Mouse    {
  114.     Bool installed;      // True = mouse is installed
  115.     char *statebuffer;   // mouse state buffer
  116.     Timer doubletimer;   // mouse double-click timer
  117.     Timer delaytimer;    // mouse typematic click timer
  118.     int prevx;           // previous mouse x coordinate
  119.     int prevy;           // previous mouse y coordinate
  120.     int clickx;          // click x position
  121.     int clicky;          // click y position
  122.     int releasex;        // release x position
  123.     int releasey;        // release y position
  124.     union REGS regs;
  125.     DFWindow *MouseWindow(int mx, int my);
  126.     void CallMouse(int m1,int m2=0,int m3=0,int m4=0,unsigned es=0);
  127.     void DispatchRelease();
  128.     void DispatchMove();
  129.     void DispatchLeftButton();
  130. public:
  131.     Mouse();
  132.     ~Mouse();
  133.     Bool Installed() { return installed; }
  134.     void GetPosition(int &x, int &y); // get mouse position
  135.     void SetPosition(int x, int y);   // set mouse position
  136.     Bool Moved();           // True if mouse has moved
  137.     void Show();            // show the mouse cursor
  138.     void Hide();            // hide the mouse cursor
  139.     Bool LeftButton();     // True if left button is pressed
  140.     Bool ButtonReleased(); // True if button was released
  141.     void SetTravel(int minx, int maxx, int miny, int maxy);
  142.     void DispatchEvent();
  143. };
  144. const int MOUSE          = 0x33;  // mouse interrupt vector
  145. // -------- mouse commands
  146. const int RESETMOUSE     =  0;
  147. const int SHOWMOUSE      =  1;
  148. const int HIDEMOUSE      =  2;
  149. const int READMOUSE      =  3;
  150. const int SETPOSITION    =  4;
  151. const int BUTTONRELEASED =  6;
  152. const int XLIMIT         =  7;
  153. const int YLIMIT         =  8;
  154. const int BUFFSIZE       = 21;
  155. const int SAVESTATE      = 22;
  156. const int RESTORESTATE   = 23;
  157. // -------- timer delays for mouse repeat, double clicks
  158. const int DELAYTICKS     =  1;
  159. const int FIRSTDELAY     =  7;
  160. const int DOUBLETICKS    =  5;
  161.  
  162. #endif
  163.  
  164.  
  165.  
  166. [LISTING FOUR]
  167.  
  168. // ------------- cursor.h
  169. #ifndef CURSOR_H
  170. #define CURSOR_H
  171.  
  172. // ------- video BIOS (0x10) functions
  173. const int SETCURSORTYPE = 1;
  174. const int SETCURSOR     = 2;
  175. const int READCURSOR    = 3;
  176. const int HIDECURSOR    = 0x20;
  177.  
  178. const int MAXSAVES = 50;  // depth of cursor save/restore stack
  179. class Cursor {
  180.     // --- cursor save/restore stack
  181.     int cursorpos[MAXSAVES];
  182.     int cursorshape[MAXSAVES];
  183.     int cs;             // count of cursor saves in effect
  184.     union REGS regs;
  185.     void Cursor::GetCursor();
  186. public:
  187.     Cursor();
  188.     ~Cursor();
  189.     void SetPosition(int x, int y);
  190.     void GetPosition(int &x, int &y);
  191.     void SetType(unsigned t);
  192.     void normalcursor() { SetType(0x0607); }
  193.     void Hide();
  194.     void Show();
  195.     void Save();
  196.     void Restore();
  197.     void SwapStack();
  198. };
  199. inline void swap(int a, int b)
  200. {
  201.     int x = a;
  202.     a = b;
  203.     b = x;
  204. }
  205. #endif
  206.  
  207.  
  208.  
  209. [LISTING FIVE]
  210.  
  211. // ------------ keyboard.h
  212. #ifndef KEYBOARD_H
  213. #define KEYBOARD_H
  214.  
  215. #include <dos.h>
  216. #include "dflatdef.h"
  217.  
  218. class Keyboard    {
  219.     union REGS regs;
  220.     int shift;
  221. public:
  222.     Keyboard() { shift = GetShift(); }
  223.     Bool ShiftChanged();
  224.     int ShiftState() { return shift = GetShift(); }
  225.     int AltConvert(int);
  226.     int GetKey();
  227.     int GetShift();
  228.     Bool KeyHit();
  229.     void clearBIOSbuffer();
  230.     void DispatchEvent();
  231. };
  232. const int KEYBOARDVECT = 9;
  233. const int KEYBOARDPORT = 0x60;
  234.  
  235. inline void Keyboard::clearBIOSbuffer()
  236. {
  237.     *(int far *)(MK_FP(0x40,0x1a)) = 
  238.     *(int far *)(MK_FP(0x40,0x1c));
  239. }
  240. // ----- keyboard BIOS (0x16) functions
  241. const int KEYBRD = 0x16;
  242. const int READKB = 0;
  243. const int KBSTAT = 1;
  244.  
  245. const int ZEROFLAG = 0x40;
  246. const int OFFSET = 0x1000;
  247.  
  248. const int RUBOUT = 8;
  249. const int BELL   = 7;
  250. const int ESC    = 27;
  251. const unsigned F1          = (187+OFFSET);
  252. const unsigned F8          = (194+OFFSET);
  253. const unsigned SHIFT_F8    = (219+OFFSET);
  254. const unsigned F10         = (196+OFFSET);
  255. const unsigned HOME        = (199+OFFSET);
  256. const unsigned UP          = (200+OFFSET);
  257. const unsigned PGUP        = (201+OFFSET);
  258. const unsigned BS          = (203+OFFSET);
  259. const unsigned FWD         = (205+OFFSET);
  260. const unsigned END         = (207+OFFSET);
  261. const unsigned DN          = (208+OFFSET);
  262. const unsigned PGDN        = (209+OFFSET);
  263. const unsigned INS         = (210+OFFSET);
  264. const unsigned DEL         = (211+OFFSET);
  265. const unsigned CTRL_HOME   = (247+OFFSET);
  266. const unsigned CTRL_PGUP   = (132+OFFSET);
  267. const unsigned CTRL_BS     = (243+OFFSET);
  268. const unsigned CTRL_FIVE   = (143+OFFSET);
  269. const unsigned CTRL_FWD    = (244+OFFSET);
  270. const unsigned CTRL_END    = (245+OFFSET);
  271. const unsigned CTRL_PGDN   = (246+OFFSET);
  272. const unsigned SHIFT_HT    = (143+OFFSET);
  273. const unsigned ALT_A       = (158+OFFSET);
  274. const unsigned ALT_B       = (176+OFFSET);
  275. const unsigned ALT_C       = (174+OFFSET);
  276. const unsigned ALT_D       = (160+OFFSET);
  277. const unsigned ALT_E       = (146+OFFSET);
  278. const unsigned ALT_F       = (161+OFFSET);
  279. const unsigned ALT_G       = (162+OFFSET);
  280. const unsigned ALT_H       = (163+OFFSET);
  281. const unsigned ALT_I       = (151+OFFSET);
  282. const unsigned ALT_J       = (164+OFFSET);
  283. const unsigned ALT_K       = (165+OFFSET);
  284. const unsigned ALT_L       = (166+OFFSET);
  285. const unsigned ALT_M       = (178+OFFSET);
  286. const unsigned ALT_N       = (177+OFFSET);
  287. const unsigned ALT_O       = (152+OFFSET);
  288. const unsigned ALT_P       = (153+OFFSET);
  289. const unsigned ALT_Q       = (144+OFFSET);
  290. const unsigned ALT_R       = (147+OFFSET);
  291. const unsigned ALT_S       = (159+OFFSET);
  292. const unsigned ALT_T       = (148+OFFSET);
  293. const unsigned ALT_U       = (150+OFFSET);
  294. const unsigned ALT_V       = (175+OFFSET);
  295. const unsigned ALT_W       = (145+OFFSET);
  296. const unsigned ALT_X       = (173+OFFSET);
  297. const unsigned ALT_Y       = (149+OFFSET);
  298. const unsigned ALT_Z       = (172+OFFSET);
  299. const unsigned ALT_1      = (0xf8+OFFSET);
  300. const unsigned ALT_2      = (0xf9+OFFSET);
  301. const unsigned ALT_3      = (0xfa+OFFSET);
  302. const unsigned ALT_4      = (0xfb+OFFSET);
  303. const unsigned ALT_5      = (0xfc+OFFSET);
  304. const unsigned ALT_6      = (0xfd+OFFSET);
  305. const unsigned ALT_7      = (0xfe+OFFSET);
  306. const unsigned ALT_8      = (0xff+OFFSET);
  307. const unsigned ALT_9      = (0x80+OFFSET);
  308. const unsigned ALT_0      = (0x81+OFFSET);
  309. const unsigned ALT_HYPHEN = (130+OFFSET);
  310. const unsigned CTRL_F4    = (225+OFFSET);
  311. const unsigned ALT_F4     = (235+OFFSET);
  312. const unsigned ALT_F6     = (237+OFFSET);
  313.  
  314. enum {CTRL_A=1,CTRL_B,CTRL_C,CTRL_D,CTRL_E,CTRL_F,CTRL_G,CTRL_H,
  315.         CTRL_I,CTRL_J,CTRL_K,CTRL_L,CTRL_M,CTRL_N,CTRL_O,CTRL_P,
  316.         CTRL_Q,CTRL_R,CTRL_S,CTRL_T,CTRL_U,CTRL_V,CTRL_W,CTRL_X,
  317.         CTRL_Y,CTRL_Z };
  318. // ------- BIOS shift key mask values
  319. const int RIGHTSHIFT = 0x01;
  320. const int LEFTSHIFT  = 0x02;
  321. const int CTRLKEY    = 0x04;
  322. const int ALTKEY     = 0x08;
  323. const int SCROLLLOCK = 0x10;
  324. const int NUMLOCK    = 0x20;
  325. const int CAPSLOCK   = 0x40;
  326. const int INSERTKEY  = 0x80;
  327.  
  328. #endif
  329.  
  330.  
  331. [LISTING SIX]
  332.  
  333. // --------- speaker.h
  334. #ifndef SPEAKER_H
  335. #define SPEAKER_H
  336.  
  337. class Speaker    {
  338.     void Wait(int n);
  339. public:
  340.     void Beep();
  341. };
  342. const int FREQUENCY = 100;
  343. const long COUNT = 1193280L / FREQUENCY;
  344.  
  345. #endif
  346.  
  347.  
  348.  
  349. [LISTING SEVEN]
  350.  
  351. // --------- clock.h
  352. #ifndef CLOCK_H
  353. #define CLOCK_H
  354.  
  355. #include "timer.h"
  356.  
  357. class Clock    {
  358.     Timer clocktimer;
  359. public:
  360.     Clock();
  361.     void DispatchEvent();
  362. };
  363. #endif
  364.  
  365.  
  366.