home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / C_DISK5.ZIP / MICROCAD / MSMENU.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-28  |  5.7 KB  |  209 lines

  1. // Copyright 1988 Bruce Eckel
  2. // Permission required to distribute source
  3.  
  4. // file: msmenu.cpp
  5. #include        <stdio.h>
  6. #include    <stdlib.h>
  7. #include    <conio.h>
  8. #include    <msmouse.h>
  9. // flash graphics declarations:
  10. #include        <fg.h>      
  11. #include        <string.h>
  12. #include        "msmenu.hpp"
  13.  
  14. msmenu::msmenu(struct menu_s * mp) {
  15.     char buf[15];
  16.     count = 0;
  17.     // attach the menu pointer to this object:
  18.     m = mp;   
  19.     // Store the size of the menu:
  20.     height = 0;
  21.     struct menu_s * mm = m;
  22.     // look for end marker:
  23.     while ( mm->item_number != -1) { 
  24.         height++;
  25.         mm++;
  26.     }
  27.     int ss, i;
  28.     for(i = 0, width = 0; i < height; i++)
  29.         if ((ss = strlen(mp[i].name)) > width)
  30.             // find the largest element:
  31.             width = ss;  
  32.  
  33.     // graphic width:
  34.     xsize = width * CHARWIDTH;   
  35.     // graphic height:
  36.     ysize = height * LINESIZE;   
  37.  
  38.     sizing_box[FG_X1] = 0; 
  39.     sizing_box[FG_Y1] = 0; 
  40.     sizing_box[FG_X2] = xsize + LINESIZE;
  41.     sizing_box[FG_Y2] = ysize + LINESIZE;  
  42.     msize = sizing_box[FG_X2] 
  43.         - sizing_box[FG_X1] + 1;
  44.     msize *= sizing_box[FG_Y2] 
  45.         - sizing_box[FG_Y1] + 1;
  46.     msize *= sizeof(fg_color_t);
  47.     // allocate memory for graphics storage:
  48.     color_p = new fg_color_t[msize];
  49.     blank_p = new fg_color_t[msize];
  50.     // save blank image of menu size:
  51.     fg_readbox(sizing_box, blank_p);  
  52.     // initialize mouse:
  53.     msm_init();                
  54.     // turn mouse cursor on:
  55.     msm_showcursor();            
  56. }
  57.     
  58. msmenu::~msmenu() {
  59.     delete color_p;
  60.     delete blank_p;
  61.     // turn mouse cursor off:
  62.     msm_hidecursor();            
  63.     // terminate use of mouse:
  64.     msm_term();                
  65. }
  66.  
  67. void msmenu::drawmenu(int x,int y) {
  68.   struct menu_s * elem = m;
  69.   for (int i = 0; i < height; i++) {
  70.     fg_puts (FG_WHITE, FG_MODE_SET, ~0, FG_ROT0, 
  71.         x,y, elem->name, fg_displaybox);
  72.     elem++;
  73.     y -= LINESIZE;
  74.   }
  75. }
  76.  
  77. /* The following patterns were created from the 
  78.    commented block of ones and zeros using the 
  79.    tool MSCURSOR.CPP  */
  80.  
  81. static int default_cur[] = {
  82. 0xffff,        /* 0000000000000000 */
  83. 0xdfff,        /* 0010000000000000 */
  84. 0xcfff,        /* 0011000000000000 */
  85. 0xc7ff,        /* 0011100000000000 */
  86. 0xc3ff,        /* 0011110000000000 */
  87. 0xc1ff,        /* 0011111000000000 */
  88. 0xc0ff,        /* 0011111100000000 */
  89. 0xc07f,        /* 0011111110000000 */
  90. 0xc03f,        /* 0011111111000000 */
  91. 0xc01f,        /* 0011111111100000 */
  92. 0xc1ff,        /* 0011111000000000 */
  93. 0xdcff,        /* 0010001100000000 */
  94. 0xfcff,        /* 0000001100000000 */
  95. 0xfe7f,        /* 0000000110000000 */
  96. 0xfe7f,        /* 0000000110000000 */
  97. 0xffff,        /* 0000000000000000 */
  98. 0x0, 0x2000, 0x3000, 0x3800, 0x3c00, 0x3e00, 
  99. 0x3f00, 0x3f80, 0x3fc0, 0x3fe0, 0x3e00, 
  100. 0x2300, 0x300, 0x180, 0x180, 0x0, 
  101. };
  102.  
  103. static int menu_cur[] = {
  104. 0xffff,        /* 0000000000000000 */
  105. 0xffff,        /* 0000000000000000 */
  106. 0xffff,        /* 0000000000000000 */
  107. 0xffff,        /* 0000000000000000 */
  108. 0xffef,        /* 0000000000010000 */
  109. 0xdfe7,        /* 0010000000011000 */
  110. 0x6fe3,        /* 1001000000011100 */
  111. 0xb7e1,        /* 0100100000011110 */
  112. 0xc000,        /* 0011111111111111 */
  113. 0xb7e1,        /* 0100100000011110 */
  114. 0x6fe3,        /* 1001000000011100 */
  115. 0xdfe7,        /* 0010000000011000 */
  116. 0xffef,        /* 0000000000010000 */
  117. 0xffff,        /* 0000000000000000 */
  118. 0xffff,        /* 0000000000000000 */
  119. 0xffff,        /* 0000000000000000 */
  120. 0x0, 0x0, 0x0, 0x0, 0x10, 0x2018, 
  121. 0x901c, 0x481e, 0x3fff, 0x481e, 
  122. 0x901c, 0x2018, 0x10, 0x0, 0x0, 0x0, 
  123. };
  124.  
  125. static int cross_cur[] = {
  126. 0xffff,        /* 0000000000000000 */
  127. 0xffff,        /* 0000000000000000 */
  128. 0xffff,        /* 0000000000000000 */
  129. 0xfeff,        /* 0000000100000000 */
  130. 0xfeff,        /* 0000000100000000 */
  131. 0xfeff,        /* 0000000100000000 */
  132. 0xfeff,        /* 0000000100000000 */
  133. 0xfeff,        /* 0000000100000000 */
  134. 0x0,        /* 1111111111111111 */
  135. 0xfeff,        /* 0000000100000000 */
  136. 0xfeff,        /* 0000000100000000 */
  137. 0xfeff,        /* 0000000100000000 */
  138. 0xfeff,        /* 0000000100000000 */
  139. 0xffff,        /* 0000000000000000 */
  140. 0xffff,        /* 0000000000000000 */
  141. 0xffff,        /* 0000000000000000 */
  142. 0x0, 0x0, 0x0, 0x100, 0x100, 0x100, 
  143. 0x100, 0x100, 0xffff, 0x100, 0x100, 
  144. 0x100, 0x100, 0x0, 0x0, 0x0, 
  145. };
  146.  
  147. /* the first two numbers are the x and y 
  148.    coordinates of the "hot spot", with the 
  149.    upper left corner = 0,0: */
  150.  
  151. void msmenu::default_cursor() {
  152.     msm_setgraphcur(-1,-1,default_cur);
  153. }
  154.  
  155. void msmenu::menu_cursor() {
  156.     msm_setgraphcur(-16,-8,menu_cur);
  157. }
  158.  
  159. void msmenu::cross_cursor() {
  160.     msm_setgraphcur(-8,-8,cross_cur);
  161. }
  162.  
  163. int 
  164. msmenu::get_selection(unsigned x, unsigned yy) {
  165.     unsigned int u,v, startv;
  166.     unsigned y = yy;
  167.     fg_box_t    read_box;
  168.     translate_coords(&x,&y);
  169.     read_box[FG_X1] = x;
  170.     read_box[FG_Y1] = y - ysize + LINESIZE;
  171.     read_box[FG_X2] = x + xsize;  
  172.     read_box[FG_Y2] = y + LINESIZE;  
  173.  
  174.     msm_hidecursor();
  175.     // use special cursor:
  176.     menu_cursor();
  177.     fg_readbox(read_box, color_p);
  178.     // blank section out:
  179.     fg_writebox(read_box, blank_p);  
  180.     // call a private function:
  181.     drawmenu(x,y);   
  182.     // trial-and-error:
  183.     msm_setcurpos(x - 35 , yy -22);  
  184.     msm_showcursor();  
  185.     msm_setareax(x -35, x - 35);
  186.     msm_setareay(yy - 22,
  187.                  yy - 22 + ysize - LINESIZE );
  188.     startv = yy - 22;
  189.     wait_left_pressed(&u,&v);
  190.     wait_left_released(&u,&v);
  191.  
  192.     // put everything back the way it was:
  193.     msm_setareax(fg_displaybox[FG_X1], 
  194.                  fg_displaybox[FG_X2]);
  195.     msm_setareay(fg_displaybox[FG_Y1], 
  196.                  fg_displaybox[FG_Y2]);
  197.     msm_hidecursor();
  198.     // Restore old cursor:
  199.     default_cursor(); 
  200.     // restore original position:
  201.     msm_setcurpos(x, yy);  
  202.     // restore screen:
  203.     fg_writebox(read_box, color_p); 
  204.     msm_showcursor();
  205.     // calculate which item was selected:
  206.     return m[(v - startv)/LINESIZE].item_number;
  207. }    
  208.     
  209.