home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / MODEL / BUTTON.C < prev    next >
C/C++ Source or Header  |  1996-03-21  |  5KB  |  194 lines

  1. /*
  2.  *        ユーザーボタン管理
  3.  *
  4.  *        Copyright    M.Takatsu    1996.1.24
  5.  */
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #include "poly.h"
  10. #include "view.h"
  11. #include "menu.h"
  12. #include "event.h"
  13. #include "button.h"
  14. #include "ml.h"
  15.  
  16. #include "graph.h"
  17. #include "input.h"
  18. #include "model.h"
  19.  
  20. ButtonData    UserButton[MAX_BUTTON];
  21. int    ButtonAreaWidth;
  22.  
  23. void    ButtonInit( void )
  24. {
  25.     int i;
  26.     ButtonAreaWidth = 0;
  27.     for (i = 0; i < MAX_BUTTON; ++i) {
  28.         UserButton[i].type = BUTTON_UNUSED;
  29.     }
  30. }
  31.  
  32. void    DrawUserButton( int pos )
  33. {
  34.     int x, y;
  35.     int fg;
  36.     int len;
  37.     if (ButtonAreaWidth <= 0 || pos < 0 || pos >=MAX_BUTTON
  38.      || UserButton[pos].type == BUTTON_UNUSED
  39.      || UserButton[pos].x < 0 || UserButton[pos].x > ButtonAreaWidth-BUTTON_WIDTH
  40.      || UserButton[pos].y < 0 || UserButton[pos].y > DISPLAY_Y-MENU_WIDTH-STATUS_WIDTH-BUTTON_HEIGHT) {
  41.         return;
  42.     }
  43.     x = DISPLAY_X - CTRL_WIDTH - ButtonAreaWidth + UserButton[pos].x;
  44.     y = MENU_WIDTH                               + UserButton[pos].y;
  45.  
  46.     if (UserButton[pos].enable & BUTTON_ENABLE) {
  47.         fg = 0;
  48.     } else {
  49.         fg = BG_COLOR;
  50.     }
  51.     if (UserButton[pos].type == BUTTON_STRING) {
  52.         len = strlen((char*)UserButton[pos].bitmap);
  53.         graph_puts((char*)UserButton[pos].bitmap,
  54.                     x + (BUTTON_WIDTH - FontH*len)/2,
  55.                     y + (BUTTON_HEIGHT- FontV)/2,
  56.                     fg | (FRAME_COLOR<<4));
  57.     } else {
  58.         graph_pattern(
  59.                     x + (BUTTON_WIDTH  - BUTTON_BITMAP_WIDTH )/2,
  60.                     y + (BUTTON_HEIGHT - BUTTON_BITMAP_HEIGHT)/2,
  61.                     (fg << 4) | FRAME_COLOR, (short*)UserButton[pos].bitmap,
  62.                     BUTTON_BITMAP_WIDTH, BUTTON_BITMAP_HEIGHT );
  63.     }
  64.     graph_box( x, y, x + BUTTON_WIDTH-1, y + BUTTON_HEIGHT-1, TRUE );
  65. }
  66.  
  67. void    DrawUserButtonAll( void )
  68. {
  69.     int i, x, y1, y2;
  70.     if (ButtonAreaWidth == 0) {
  71.         return;
  72.     }
  73.     x = DISPLAY_X - CTRL_WIDTH - ButtonAreaWidth;
  74.     y1 = MENU_WIDTH;
  75.     y2 = DISPLAY_Y - STATUS_WIDTH - 1;
  76.  
  77.     graph_fill( x, y1, x + ButtonAreaWidth-1, y2, FRAME_COLOR );
  78.     for (i = 0; i < MAX_BUTTON; ++i) {
  79.         DrawUserButton(i);
  80.     }
  81. }
  82.  
  83. void    SetButtonArea(int width)
  84. {
  85.     if (width >= 0 && width <= BUTTON_WIDTH*4) {
  86.         ButtonAreaWidth = width;
  87.         ViewSetDisplay(DISPLAY_X - CTRL_WIDTH - ButtonAreaWidth, DISPLAY_Y);
  88.     }
  89. }
  90.  
  91. void    CallButton( int x, int y )
  92. {
  93.     int i, sx, sy, exec;
  94.     if (ButtonAreaWidth == 0) {
  95.         return;
  96.     }
  97.     sx = DISPLAY_X - CTRL_WIDTH - ButtonAreaWidth;
  98.     sy = MENU_WIDTH;
  99.     for (i = 0; i < MAX_BUTTON; ++i) {
  100.         if (UserButton[i].type != BUTTON_UNUSED
  101.          && (UserButton[i].enable & BUTTON_ENABLE)
  102.          && sx + UserButton[i].x <= x && x < sx + UserButton[i].x + BUTTON_WIDTH
  103.          && sy + UserButton[i].y <= y && y < sy + UserButton[i].y + BUTTON_HEIGHT) {
  104.              int x1 = sx + UserButton[i].x;
  105.              int y1 = sy + UserButton[i].y;
  106. #if 0
  107.             graph_box( x1, y1, x1+BUTTON_WIDTH-1, y1+BUTTON_HEIGHT-1, FALSE );
  108.             WaitMouseOff();
  109.             graph_box( x1, y1, x1+BUTTON_WIDTH-1, y1+BUTTON_HEIGHT-1, TRUE );
  110.             exec = UserButton[i].exec;
  111.             if ( exec >= 0 )
  112.             {
  113.                 DataStruct    *top = StackTop();
  114.                 StackPushInt( i );
  115.                 CallFunction( exec, 1, top+1 );
  116.                 StackRelease( top );
  117.             }
  118. #else
  119.             graph_box( x1, y1, x1+BUTTON_WIDTH-1, y1+BUTTON_HEIGHT-1, FALSE );
  120.             exec = UserButton[i].exec;
  121.             if ( exec >= 0 )
  122.             {
  123.                 DataStruct    *top = StackTop();
  124.                 if (UserButton[i].enable & BUTTON_REPEAT) {
  125. #ifdef WINDOWS
  126.                     extern unsigned long WinGetTimer(void);
  127.                     unsigned long lasttime = WinGetTimer();
  128. #endif
  129.                     do {
  130.                         StackPushInt( i );
  131.                         CallFunction( exec, 1, top+1 );
  132.                         StackRelease( top );
  133. #ifdef WINDOWS
  134.                         do {
  135.                             PeekInput();
  136.                         } while (lasttime + 66 > WinGetTimer()
  137.                             && !QuitFlag && (MouseRight || MouseLeft));
  138.                         lasttime = WinGetTimer();
  139. #else
  140.                         PeekInput();
  141. #endif
  142.                     } while ((MouseRight || MouseLeft));
  143.                     graph_box( x1, y1, x1+BUTTON_WIDTH-1, y1+BUTTON_HEIGHT-1, TRUE );
  144.                 } else {
  145.                     WaitMouseOff();
  146.                     graph_box( x1, y1, x1+BUTTON_WIDTH-1, y1+BUTTON_HEIGHT-1, TRUE );
  147.  
  148.                     StackPushInt( i );
  149.                     CallFunction( exec, 1, top+1 );
  150.                     StackRelease( top );
  151.                 }
  152.             } else {
  153.                 WaitMouseOff();
  154.                 graph_box( x1, y1, x1+BUTTON_WIDTH-1, y1+BUTTON_HEIGHT-1, TRUE );
  155.             }
  156. #endif
  157.             return;
  158.         }
  159.     }
  160.     WaitMouseOff();
  161. }
  162.  
  163. int    CreateButton(int x, int y, char *bitmap, int type, int exec, int enable)
  164. {
  165.     int i;
  166.     for (i = 0; i < MAX_BUTTON; ++i) {
  167.         if (x-BUTTON_WIDTH  < UserButton[i].x && UserButton[i].x < x + BUTTON_WIDTH
  168.          && y-BUTTON_HEIGHT < UserButton[i].y && UserButton[i].y < y + BUTTON_HEIGHT) {
  169.             UserButton[i].type = BUTTON_UNUSED;
  170.         }
  171.     }
  172.     for (i = 0; i < MAX_BUTTON; ++i) {
  173.         if (UserButton[i].type == BUTTON_UNUSED) {
  174.             break;
  175.         }
  176.     }
  177.     if (i == MAX_BUTTON) {
  178.         return -1;
  179.     }
  180.     UserButton[i].type = type;
  181.     UserButton[i].x = x;
  182.     UserButton[i].y = y;
  183.     UserButton[i].exec = exec;
  184.     UserButton[i].enable = enable;
  185.     if (type == BUTTON_STRING) {
  186.         strncpy((char *)UserButton[i].bitmap, bitmap, 6);
  187.         UserButton[i].bitmap[7] = '\0';
  188.     } else {
  189.         memcpy(UserButton[i].bitmap, bitmap, ((BUTTON_BITMAP_WIDTH-1)/16+1)*2*BUTTON_BITMAP_HEIGHT);
  190.     }
  191.     return i;
  192. }
  193.  
  194.