Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #include "system.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "winlib.h"
  6.  
  7.  
  8. extern ctrl_t  *mouse_capture;
  9. uint32_t main_cursor;
  10.  
  11. static int button_proc(ctrl_t *btn, uint32_t msg, uint32_t arg1, uint32_t arg2);
  12. static int spinbtn_proc(ctrl_t *btn, uint32_t msg, uint32_t arg1, uint32_t arg2);
  13.  
  14.  
  15. button_t *create_button(char *caption, int id, int x, int y,
  16.                         int w, int h, ctrl_t *parent)
  17. {
  18.     button_t  *btn;
  19.     int        len;
  20.  
  21.     if( !parent )
  22.         return NULL;
  23.  
  24.     btn = (button_t*)malloc(sizeof(button_t));
  25.  
  26.     link_initialize(&btn->link);
  27.     list_initialize(&btn->child);
  28.  
  29.     btn->handler = button_proc;
  30.     btn->parent  = parent;
  31.  
  32.     btn->ctx     = parent->ctx;
  33.     btn->id      = id;
  34.     btn->style   = 0;
  35.  
  36.     btn->rc.l   = x;
  37.     btn->rc.t   = y ;
  38.  
  39.     btn->rc.r   = x + w;
  40.     btn->rc.b   = y + h;
  41.  
  42.     btn->w      = w;
  43.     btn->h      = h;
  44.  
  45.     btn->state = 0;
  46.  
  47.     btn->caption = caption;
  48.  
  49.     if( !caption )
  50.         btn->capt_len = 0;
  51.     else
  52.     {
  53.         len = strlen(caption);
  54.         btn->capt_len = len;
  55.         if( len )
  56.             btn->caption = strdup(caption);
  57.         else
  58.             btn->caption = NULL;
  59.     }
  60.  
  61.     btn->img_default = NULL;
  62.     btn->img_hilite  = NULL;
  63.     btn->img_pressed = NULL;
  64.  
  65.     list_append(&btn->link, &parent->child);
  66.  
  67.     return btn;
  68. };
  69.  
  70. #if 0
  71. int draw_button(button_t *btn)
  72. {
  73.     void *bitmap;
  74.  
  75.     bitmap = btn->img_default;
  76.  
  77.     if(btn->state & bPressed)
  78.         bitmap = btn->img_pressed;
  79.     else if(btn->state & bHighlight)
  80.         bitmap = btn->img_hilite;
  81.  
  82.     if( bitmap )
  83.         draw_bitmap(bitmap, btn->rc.l, btn->rc.t, btn->w, btn->h);
  84.  
  85.     if( btn->caption && btn->capt_len)
  86.     {
  87.         int txt_w;
  88.         int txt_x, txt_y;
  89.         txt_w = btn->capt_len*8-2;
  90.  
  91.         txt_x = btn->rc.l + 1 + (btn->w - txt_w)/2;
  92.         txt_y = btn->rc.t + 9;
  93.  
  94.         if(btn->state & bPressed){
  95.             txt_x++;
  96.             txt_y++;
  97.         };
  98.         draw_text(btn->caption, txt_x, txt_y, btn->capt_len, 0x10000000);
  99.     };
  100.     return 0;
  101. };
  102. #endif
  103.  
  104. int draw_button_cairo(button_t *btn)
  105. {
  106.     int *pixmap, *src;
  107.     ctx_t *ctx;
  108.     int i, j;
  109.     int x, y;
  110.  
  111.     ctx = btn->ctx;
  112.  
  113.     x = btn->rc.l - ctx->offset_x;
  114.     y = btn->rc.t - ctx->offset_y;
  115.  
  116.     pixmap = ctx->pixmap;
  117.  
  118.     pixmap+=  y*ctx->stride/4 + x;
  119.  
  120.     src = btn->img_default;
  121.  
  122.     if(btn->state & bPressed)
  123.         src = btn->img_pressed;
  124.     else if(btn->state & bHighlight)
  125.         src = btn->img_hilite;
  126.  
  127.     for(i=0; i < btn->h ;i++)
  128.     {
  129.         for(j=0; j<btn->w; j++)
  130.             pixmap[j] = src[j];
  131.         pixmap+= ctx->stride/4;
  132.         src+=btn->w;
  133.     };
  134.  
  135.     return 0;
  136. };
  137.  
  138.  
  139. int draw_spin_cairo(button_t *btn)
  140. {
  141.     void *ctx;
  142.  
  143.     return 0;
  144. };
  145.  
  146.  
  147. int button_proc(ctrl_t *ctrl, uint32_t msg, uint32_t arg1, uint32_t arg2)
  148. {
  149.     int  x, y;
  150.     int  state;
  151.     int  old_state;
  152.     int  action=0;
  153.  
  154.     button_t *btn = (button_t*)ctrl;
  155.  
  156.     switch( msg )
  157.     {
  158.         case MSG_PAINT:
  159.             draw_button_cairo(btn);
  160.             update_rect((ctrl_t*)btn);
  161.             break;
  162.  
  163.         case MSG_MOUSEENTER:
  164. //            printf("mouse enter\n");
  165.             btn->state|= bHighlight;
  166.             send_message(btn, MSG_PAINT, 0, 0);
  167.             break;
  168.  
  169.         case MSG_MOUSELEAVE:
  170. //            printf("mouse leave\n");
  171.             if( (ctrl_t*)btn != mouse_capture) {
  172.                 btn->state &= ~bHighlight;
  173.                 send_message(btn, MSG_PAINT, 0, 0);
  174.             };
  175.             break;
  176.  
  177.         case MSG_LBTNDOWN:
  178.         case MSG_LBTNDBLCLK:
  179. //            printf("push button\n");
  180.             capture_mouse((ctrl_t*)btn);
  181.             btn->state|= bPressed;
  182.             send_message(btn, MSG_PAINT, 0, 0);
  183.             break;
  184.  
  185.         case MSG_LBTNUP:
  186.  
  187.  //           printf("button action\n");
  188.             if(btn->state & bPressed)
  189.                 action = MSG_COMMAND;
  190.  
  191.             release_mouse();
  192.  
  193.             x = ((pos_t)arg2).x;
  194.             y = ((pos_t)arg2).y;
  195.  
  196.             if( pt_in_rect( &btn->rc, x, y) )
  197.                 state = bHighlight;
  198.             else
  199.                 state = 0;
  200.  
  201.             if(action)
  202.                 send_message(btn->parent,MSG_COMMAND,btn->id,(int)btn);
  203.  
  204.             btn->state = state;
  205.             send_message(btn, MSG_PAINT, 0, 0);
  206.             break;
  207.  
  208.         case MSG_MOUSEMOVE:
  209.  
  210.             if(main_cursor != 0)
  211.             {
  212.                 set_cursor(0);
  213.                 main_cursor = 0;
  214.             }
  215.  
  216.             if( ! (btn->state & bHighlight))
  217.             {
  218.                 btn->state|= bHighlight;
  219.                 send_message(btn, MSG_PAINT, 0, 0);
  220.             };
  221.  
  222.             if( (ctrl_t*)btn != mouse_capture)
  223.                 return 0;
  224.  
  225.             x = ((pos_t)arg2).x;
  226.             y = ((pos_t)arg2).y;
  227.  
  228.             old_state = btn->state;
  229.  
  230.             if( pt_in_rect(&btn->rc, x, y) )
  231.                 btn->state |= bPressed;
  232.             else
  233.                 btn->state &= ~bPressed;
  234.  
  235.             if( old_state ^ btn->state)
  236.                 send_message(btn, MSG_PAINT, 0, 0);
  237.     }
  238.     return 0;
  239. };
  240.  
  241.  
  242. int draw_progress(progress_t *prg)
  243. {
  244.     int *pixmap, src;
  245.     ctx_t *ctx;
  246.     int i, j;
  247.     int x, y;
  248.  
  249.     int len;
  250.  
  251.     ctx = prg->ctrl.ctx;
  252.  
  253.     x = prg->ctrl.rc.l - ctx->offset_x;
  254.     y = prg->ctrl.rc.t - ctx->offset_y;
  255.  
  256.  
  257.     len = prg->current*prg->ctrl.w/(prg->max - prg->min);
  258.     pixmap = ctx->pixmap;
  259.  
  260.     pixmap+=  y*ctx->stride/4 + x;
  261.  
  262.     src = 0x32ebfb; //btn->img_default;
  263.  
  264.     for(i=0; i < prg->ctrl.h ;i++)
  265.     {
  266.         for(j=0; j < len; j++)
  267.             pixmap[j] = src;
  268.         pixmap+= ctx->stride/4;
  269.     };
  270.  
  271.     return 0;
  272. };
  273.  
  274.  
  275. int prg_proc(ctrl_t *ctrl, uint32_t msg, uint32_t arg1, uint32_t arg2)
  276. {
  277.     progress_t *prg = (progress_t*)ctrl;
  278.     int pos;
  279.    
  280.     switch( msg )
  281.     {
  282.         case MSG_PAINT:
  283.             draw_progress(prg);
  284.             update_rect(ctrl);
  285.             break;
  286.            
  287.         case MSG_LBTNDOWN:
  288.             prg->pos = ((pos_t)arg2).x - ctrl->rc.l;
  289.             send_message(ctrl->parent,MSG_COMMAND,ctrl->id,(int)ctrl);
  290.             break;
  291.            
  292.         default:
  293.             break;
  294.     }
  295.     return 0;
  296. };
  297.  
  298.  
  299. progress_t *create_progress(char *caption, int id, int x, int y,
  300.                         int w, int h, ctrl_t *parent)
  301. {
  302.     progress_t  *prg;
  303.     int        len;
  304.  
  305.     if( !parent )
  306.         return NULL;
  307.  
  308.     prg = (progress_t*)malloc(sizeof(progress_t));
  309.  
  310.     link_initialize(&prg->ctrl.link);
  311.     list_initialize(&prg->ctrl.child);
  312.  
  313.     prg->ctrl.handler = prg_proc;
  314.     prg->ctrl.parent  = parent;
  315.  
  316.     prg->ctrl.ctx     = parent->ctx;
  317.     prg->ctrl.id      = id;
  318.    
  319.     prg->ctrl.rc.l   = x;
  320.     prg->ctrl.rc.t   = y ;
  321.  
  322.     prg->ctrl.rc.r   = x + w;
  323.     prg->ctrl.rc.b   = y + h;
  324.  
  325.     prg->ctrl.w      = w;
  326.     prg->ctrl.h      = h;
  327.  
  328.     prg->min        = 0;
  329.     prg->max        = 1;
  330.     prg->current    = 0;
  331.     prg->pos        = 0;
  332.  
  333.     list_append(&prg->ctrl.link, &parent->child);
  334.  
  335.     return prg;
  336. };
  337.  
  338.