home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / OMEGA2 / OMBUTTON.CPP < prev    next >
C/C++ Source or Header  |  1992-04-25  |  11KB  |  445 lines

  1. //
  2. // *************************************************************************
  3. // *                                                                       *
  4. // *    OMEGA C++ Windowing Class Library                                  *
  5. // *    =================================                                  *
  6. // *                                                                       *
  7. // *    Copyright 1991,92 Tom Clancy                                       *
  8. // *    Submitted to the public domain, April 1992                         *
  9. // *                                                                       *
  10. // *************************************************************************
  11. // *                                                                       *
  12. // *    Push Button Methods                                                *
  13. // *                                                                       *
  14. // *************************************************************************
  15. //
  16.  
  17.  
  18. #include <string.h>
  19. #include <dos.h>
  20. #include "ombutton.hpp"
  21.  
  22. oButton::oButton() {
  23.   Nb=0;
  24.   Current=1;
  25.   Pressed=FALSE;
  26.   Picked=0;
  27.   Pushed=FALSE;
  28.   bShaddir=shBottomleft;
  29. }
  30.  
  31. oButton::~oButton() {
  32.  register int i;
  33.  for(i=1; i<=Nb; i++) {
  34.    delete Button[i]->message;
  35.    delete Button[i];
  36.  }
  37. }
  38.  
  39. void oButton::addbutton(char *mess, int x, int y, int Nfg, int Nbk, int Hfg, int Hbk, int HotFg) {
  40.   Nb++;
  41.   if(Nb>255)
  42.     Nb=255;
  43.   else {
  44.     Button[Nb]=new butts;
  45.     Button[Nb]->message = new char[strlen(mess)+1];
  46.     if(strlen(mess))
  47.       strcpy(Button[Nb]->message,mess);
  48.     else
  49.       Button[Nb]->message[0]='\0';
  50.     Button[Nb]->x=x;
  51.     Button[Nb]->y=y;
  52.     Button[Nb]->Nfg=Nfg;
  53.     Button[Nb]->Nbk=Nbk;
  54.     Button[Nb]->Hfg=Hfg;
  55.     Button[Nb]->Hbk=Hbk;
  56.     Button[Nb]->HotFg=HotFg;
  57.     addtohotlist(Nb);
  58.   }
  59.  
  60. }
  61.  
  62. void oButton::normwriteoption(int i) {
  63.  
  64.   char s[80];
  65.   int pos;
  66.   register int j,k;
  67.  
  68.   if(!strchr(Button[i]->message,'~'))
  69.     writeat(Button[i]->x,Button[i]->y,Button[i]->Nfg,Button[i]->Nbk,Button[i]->message);
  70.   else {
  71.     pos=strchr(Button[i]->message,'~')-Button[i]->message;
  72.     for(j=0; j<pos; j++)
  73.       s[j]=Button[i]->message[j];
  74.     k=j;
  75.     for(j=pos+1;j<strlen(Button[i]->message); j++,k++)
  76.       s[k]=Button[i]->message[j];
  77.     s[k]='\0';
  78.     writeat(Button[i]->x,Button[i]->y,Button[i]->Nfg,Button[i]->Nbk,s);
  79.     chwriteat(Button[i]->x+pos,Button[i]->y,Button[i]->HotFg,Button[i]->Nbk,Button[i]->message[pos+1]);
  80.   }
  81. }
  82.  
  83. void oButton::highwriteoption(int i) {
  84.  
  85.   char s[80];
  86.   int pos;
  87.   register int j,k;
  88.  
  89.   if(!strchr(Button[i]->message,'~'))
  90.     writeat(Button[i]->x,Button[i]->y,Button[i]->Hfg,Button[i]->Hbk,Button[i]->message);
  91.   else {
  92.     pos=strchr(Button[i]->message,'~')-Button[i]->message;
  93.     for(j=0; j<pos; j++)
  94.       s[j]=Button[i]->message[j];
  95.     k=j;
  96.     for(j=pos+1;j<strlen(Button[i]->message); j++,k++)
  97.       s[k]=Button[i]->message[j];
  98.     s[k]='\0';
  99.     writeat(Button[i]->x,Button[i]->y,Button[i]->Hfg,Button[i]->Hbk,s);
  100.     chwriteat(Button[i]->x+pos,Button[i]->y,Button[i]->HotFg,Button[i]->Hbk,Button[i]->message[pos+1]);
  101.   }
  102. }
  103.  
  104.  
  105. void oButton::addtohotlist(int i) {
  106.   if(strchr(Button[i]->message,'~')) {
  107.     hotlist[i]=Button[i]->message[(strchr(Button[i]->message,'~')-Button[i]->message)+1];
  108.     hotlist[i]=makealt(hotlist[i]);
  109.   }
  110.   else
  111.     hotlist[i]=0;
  112. }
  113.  
  114.  
  115. void oButton::writebuttonshadow(int b) {
  116.  
  117.   register int i,off=0;
  118.  
  119.   if(strchr(Button[i]->message,'~'))
  120.     off=1;
  121.  
  122.   switch(getshaddir()) {
  123.  
  124.     case shTopright :
  125.       for(i=Button[b]->x+1; i<=Button[b]->x+strlen(Button[b]->message)-off; i++)
  126.     chwriteat(i,Button[b]->y-1,BLACK,getbkcolor(),220);
  127.       chwriteat(Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y,BLACK,getbkcolor(),223);
  128.       break;
  129.  
  130.     case shTopleft :
  131.       for(i=Button[b]->x-1; i<Button[b]->x+strlen(Button[b]->message)-off; i++)
  132.     chwriteat(i,Button[b]->y-1,BLACK,getbkcolor(),220);
  133.       chwriteat(Button[b]->x-1,Button[b]->y,BLACK,getbkcolor(),223);
  134.       break;
  135.  
  136.     case shBottomright :
  137.       for(i=Button[b]->x+1; i<=Button[b]->x+strlen(Button[b]->message)-off; i++)
  138.     chwriteat(i,Button[b]->y+1,BLACK,getbkcolor(),223);
  139.       chwriteat(Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y,BLACK,getbkcolor(),220);
  140.       break;
  141.  
  142.     case shBottomleft :
  143.       for(i=Button[b]->x-1; i<Button[b]->x+strlen(Button[b]->message)-off; i++)
  144.     chwriteat(i,Button[b]->y+1,BLACK,getbkcolor(),223);
  145.       chwriteat(Button[b]->x-1+off,Button[b]->y,BLACK,getbkcolor(),220);
  146.       break;
  147.   }
  148.   showmouse();
  149. }
  150.  
  151. void oButton::writebutton(int i) {
  152.  
  153.   //writeat(Button[i]->x,Button[i]->y,Button[i]->Nfg,Button[i]->Nbk,Button[i]->message);
  154.   normwriteoption(i);
  155.   writebuttonshadow(i);
  156.   showmouse();
  157. }
  158.  
  159. void oButton::highlight() {
  160.   //writeat(Button[Current]->x,Button[Current]->y,Button[Current]->Hfg,Button[Current]->Hbk,Button[Current]->message);
  161.   highwriteoption(Current);
  162.   showmouse();
  163.  
  164. }
  165.  
  166. void oButton::unhighlight() {
  167.   //writeat(Button[Current]->x,Button[Current]->y,Button[Current]->Nfg,Button[Current]->Nbk,Button[Current]->message);
  168.   normwriteoption(Current);
  169.   showmouse();
  170. }
  171.  
  172.  
  173. int  oButton::withinbutton(int i) {
  174.   if(!Pushed)
  175.     return(buttonx()>=Button[i]->x && buttonx()<=Button[i]->x+strlen(Button[i]->message)-1 &&
  176.        buttony()==Button[i]->y);
  177.   else {
  178.     if(getshaddir()==shTopleft || getshaddir()==shBottomleft)
  179.       return(buttonx()>=Button[i]->x && buttonx()<=Button[i]->x+strlen(Button[i]->message) &&
  180.          buttony()==Button[i]->y);
  181.     else
  182.       return(buttonx()>=Button[i]->x-1 && buttonx()<=Button[i]->x+strlen(Button[i]->message)-1 &&
  183.          buttony()==Button[i]->y);
  184.   }
  185. }
  186.  
  187. int  oButton::onwhichbutton() {
  188.   register int i;
  189.  
  190.   for(i=1; i<=Nb; i++)
  191.     if(withinbutton(i))
  192.       return i;
  193.   return 0;
  194.  
  195. }
  196.  
  197. void oButton::hidebutton(int b) {
  198.   int off=0;
  199.   if(strchr(Button[b]->message,'~'))
  200.     off=1;
  201.  
  202.   switch(getshaddir()) {
  203.     case shTopleft :
  204.       fillarea(Button[b]->x-1,Button[b]->y-1,Button[b]->x+strlen(Button[b]->message)-1-off,Button[b]->y,getfgcolor(),getbkcolor(),32);
  205.       break;
  206.  
  207.     case shBottomleft :
  208.       fillarea(Button[b]->x-1,Button[b]->y,Button[b]->x+strlen(Button[b]->message)-1-off,Button[b]->y+1,getfgcolor(),getbkcolor(),32);
  209.       break;
  210.  
  211.     case shTopright :
  212.       fillarea(Button[b]->x,Button[b]->y-1,Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y,getfgcolor(),getbkcolor(),32);
  213.       break;
  214.  
  215.     case shBottomright :
  216.       fillarea(Button[b]->x,Button[b]->y,Button[b]->x+strlen(Button[b]->message)-off,Button[b]->y+1,getfgcolor(),getbkcolor(),32);
  217.       break;
  218.   }
  219. }
  220.  
  221. void oButton::pressbutton(int b) {
  222.   if(!Pressed) {
  223.     Pressed=TRUE;
  224.     hidebutton(b);
  225.     if(getshaddir()==shTopright || getshaddir()==shBottomright)
  226.       Button[b]->x+=1;
  227.     else
  228.       Button[b]->x-=1;
  229.     highwriteoption(b);
  230.   }
  231. }
  232.  
  233. void oButton::unpressbutton(int b) {
  234.   if(Pressed) {
  235.     Pressed=FALSE;
  236.     hidebutton(b);
  237.     if(getshaddir()==shTopright || getshaddir()==shBottomright)
  238.       Button[b]->x-=1;
  239.     else
  240.       Button[b]->x+=1;
  241.     highwriteoption(b);
  242.     writebuttonshadow(b);
  243.   }
  244. }
  245.  
  246. int oButton::pushbutton() {
  247.  
  248.   int ox=buttonx();
  249.   int oy=buttony();
  250.   int ob;
  251.   int xx,yy;
  252.   int sx,sy;
  253.   int bb=buttonb();
  254.   int b=onwhichbutton();
  255.  
  256.   if(b) {
  257.     while(bb==1) {
  258.       if(withinbutton(b)) {
  259.     Picked=0;
  260.     unhighlight();
  261.     Current=b;
  262.     highlight();
  263.     hidebutton(b);
  264.     if(getshaddir()==shTopright || getshaddir()==shBottomright) {
  265.       Pushed=TRUE;
  266.       mouse::getmouse(ox,oy,ob);
  267.       sx=ox;
  268.       sy=oy;
  269.       pressbutton(b);
  270.       mouse::setmouse(ox+1,oy);
  271.     }
  272.     else {
  273.       Pushed=TRUE;
  274.       mouse::getmouse(ox,oy,ob);
  275.       sx=ox;
  276.       sy=oy;
  277.       pressbutton(b);
  278.       mouse::setmouse(ox-1,oy);
  279.     }
  280.  
  281.       }
  282.       getmouse(xx,yy,bb);
  283.       ox=xx;
  284.       oy=yy;
  285.       ob=bb;
  286.       setbuttonx(xx);
  287.       setbuttony(yy);
  288.       while(withinbutton(b) && bb==1) {
  289.     getmouse(xx,yy,bb);
  290.     setbuttonx(xx);
  291.     setbuttony(yy);
  292.     showmouse();
  293.       }
  294.       if(bb && !withinbutton(b)) {
  295.     Pushed=FALSE;
  296.     unpressbutton(b);
  297.     highlight();
  298.     while(!withinbutton(b) && bb) {
  299.       getmouse(xx,yy,bb);
  300.       setbuttonx(xx);
  301.       setbuttony(yy);
  302.       showmouse();
  303.     }
  304.       }
  305.  
  306.     }
  307.     unpressbutton(b);
  308.     if(withinbutton(b)) {
  309.       setmouse(sx,sy);
  310.       Picked=b;
  311.     }
  312.   }
  313.   return Picked;
  314. }
  315.  
  316. void oButton::display_buttons() {
  317.   register int i;
  318.  
  319.   if(Nb) {
  320.     for(i=1; i<=Nb; i++)
  321.       writebutton(i);
  322.     highlight();
  323.   }
  324. }
  325.  
  326. void oButton::display_info() {
  327.   Picked=0;
  328.   display_buttons();
  329. }
  330.  
  331.  
  332. void oButton::gonextbutton() {
  333.   if(Nb) {
  334.     unhighlight();
  335.     Current++;
  336.     if(Current>Nb)
  337.       Current=1;
  338.     highlight();
  339.   }
  340. }
  341.  
  342.  
  343. void oButton::goprevbutton() {
  344.   if(Nb) {
  345.     unhighlight();
  346.     Current--;
  347.     if(Current<1)
  348.       Current=Nb;
  349.     highlight();
  350.   }
  351. }
  352.  
  353.  
  354. void oButton::setcurrent(int c) {
  355.   if(c && c<=Nb) {
  356.     unhighlight();
  357.     Current=c;
  358.     highlight();
  359.   }
  360. }
  361.  
  362. int  oButton::handle_mouse_event() {
  363.  
  364.   if(pushbutton())
  365.     return cmdPushedButton;
  366.   else
  367.     return cmdNocmd;
  368. }
  369.  
  370. int  oButton::handle_key_event() {
  371.  
  372.  
  373.   int r=getkeycode();
  374.  
  375.   switch(r) {
  376.  
  377.     case 27 :
  378.       Picked=0;
  379.       return cmdClose;
  380.       break;
  381.  
  382.     case RightArrow :
  383.       gonextbutton();
  384.       return cmdNextButton;
  385.       break;
  386.  
  387.     case LeftArrow :
  388.       goprevbutton();
  389.       return cmdPrevButton;
  390.       break;
  391.  
  392.     case UpArrow :
  393.       goprevbutton();
  394.       return cmdPrevButton;
  395.       break;
  396.  
  397.     case DownArrow:
  398.       gonextbutton();
  399.       return cmdNextButton;
  400.       break;
  401.  
  402.     case 32 : // Space Bar
  403.       if(Nb) {
  404.     Picked=Current;
  405.     pressbutton(Current);
  406.     delay(100);
  407.     unpressbutton(Current);
  408.     return cmdPushedButton;
  409.       }
  410.       break;
  411.  
  412.     case Enter :
  413.       if(Nb) {
  414.     Picked=Current;
  415.     pressbutton(Current);
  416.     delay(100);
  417.     unpressbutton(Current);
  418.     return cmdPushedButton;
  419.       }
  420.       break;
  421.  
  422.     default:
  423.       int i;
  424.     for(i=1;(i<=Nb) && r!=hotlist[i]; i++);
  425.       if(r==hotlist[i]) {
  426.         unhighlight();
  427.         Current=i;
  428.         pressbutton(Current);
  429.         delay(100);
  430.         unpressbutton(Current);
  431.         highlight();
  432.         Picked=Current;
  433.         return cmdPushedButton;
  434.       }
  435.       break;
  436.   }
  437.   return cmdNocmd;
  438.  
  439. }
  440.  
  441. void oButton::run() {
  442.   while(getlastcmd()!=cmdClose)
  443.     handle_events();
  444. }
  445.