Subversion Repositories Kolibri OS

Rev

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

  1.  
  2.  
  3. #include "system.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include "winlib.h"
  9.  
  10. int draw_frame(window_t *win);
  11. static int draw_window(window_t *win);
  12.  
  13. uint32_t main_cursor;
  14. uint32_t cursor_ns;
  15. uint32_t cursor_we;
  16. uint32_t cursor_nwse;
  17. uint32_t cursor_nesw;
  18.  
  19. int win_font;
  20.  
  21.  
  22. static pos_t    old_pos;
  23.  
  24. ctrl_t  *mouse_capture = NULL;
  25.  
  26. static link_t   timers;
  27. static uint32_t  realtime;
  28. static uint32_t  wait_time;
  29. static uint32_t  exp_time;
  30.  
  31. static int need_update;
  32.  
  33. #define LOAD_FROM_MEM                1
  34.  
  35.  
  36. void adjust_frame(window_t *win);
  37.  
  38.  
  39. #include "control.inc"
  40. //#include "io.inc"
  41. #include "timer.inc"
  42.  
  43. //#include "button.inc"
  44. //#include "scroller.inc"
  45.  
  46. static window_t Window;
  47.  
  48.  
  49.  
  50. void init_frame(window_t *win);
  51.  
  52. window_t *create_window(char *caption, int style, int x, int y,
  53.                           int w, int h, handler_t handler)
  54. {
  55.     char proc_info[1024];
  56.     int stride;
  57.  
  58. //    __asm__ __volatile__("int3");
  59.  
  60.    
  61. //    ctx_t *ctx = &Window.client_ctx;
  62.  
  63.     if(handler==0) return 0;
  64.  
  65.     BeginDraw();
  66.     DrawWindow(x, y, w-1, h-1,
  67.                NULL,0,0x41);
  68.     EndDraw();
  69.  
  70.     get_proc_info(proc_info);
  71.  
  72.     x = *(uint32_t*)(proc_info+34);
  73.     y = *(uint32_t*)(proc_info+38);
  74.     w = *(uint32_t*)(proc_info+42)+1;
  75.     h = *(uint32_t*)(proc_info+46)+1;
  76.  
  77.     Window.handler = handler;
  78.  //   Window.ctx = ctx;
  79.  
  80.     list_initialize(&Window.link);
  81.     list_initialize(&Window.child);
  82.  
  83.  
  84. //    Window.bitmap.width  = 1920;
  85. //    Window.bitmap.height = 1080;
  86. //    Window.bitmap.flags  = 0;
  87.  
  88.  //   if( create_bitmap(&Window.bitmap) )
  89.  //   {
  90.  //       printf("not enough memory for window bitmap\n");
  91.  //       return 0;
  92.   //  }
  93.  
  94.  //   ctx->pixmap   = &Window.bitmap;
  95.  //   ctx->offset_x = 0;
  96.  //   ctx->offset_y = 0;
  97.  
  98.     Window.rc.l = x;
  99.     Window.rc.t = y;
  100.     Window.rc.r = x + w;
  101.     Window.rc.b = y + h;
  102.  
  103.     Window.w = w;
  104.     Window.h = h;
  105.  
  106.     Window.caption_txt = caption;
  107.     Window.style = style;
  108.  
  109.     Window.child_over  = NULL;
  110.     Window.child_focus = NULL;
  111.  
  112.     init_caption(&Window);
  113.     init_panel(&Window);
  114.     init_frame(&Window);
  115.     send_message((ctrl_t*)&Window, MSG_SIZE, 0, 0);
  116.     return &Window;
  117. };
  118.  
  119.  
  120. int def_window_proc(ctrl_t  *ctrl, uint32_t msg, uint32_t arg1, uint32_t arg2)
  121. {
  122.     ctrl_t   *child;
  123.  
  124.     window_t  *win = (window_t*)ctrl;
  125.  
  126.     switch(msg)
  127.     {
  128.         case MSG_PAINT:
  129.             draw_window(win);
  130.             break;
  131.  
  132.         case 2:
  133.             child  = (ctrl_t*)win->child.next;
  134.             while( &child->link != &win->child)
  135.             {
  136.                 send_message(child, 2, arg1, arg2);
  137.                 child = (ctrl_t*)child->link.next;
  138.             };
  139.             break;
  140.  
  141.         case MSG_MOUSEMOVE:
  142.             child = win_get_child(win, arg2 & 0xFFFF, (arg2>>16));
  143.             if( win->child_over )
  144.             {
  145.                 if(child == win->child_over)
  146.                     send_message(child, msg, 0, arg2);
  147.                 else
  148.                     send_message(win->child_over, MSG_MOUSELEAVE, 0, arg2);
  149.             }
  150.             else if( child )
  151.                 send_message(child, MSG_MOUSEENTER, 0, arg2);
  152.  
  153.             win->child_over = child;
  154.             if( child )
  155.                 send_message(child,msg,0,arg2);
  156.             else if(main_cursor != 0)
  157.             {
  158.                 set_cursor(0);
  159.                 main_cursor = 0;
  160.             }
  161.             break;
  162.  
  163.         case MSG_SIZE:
  164.             break;
  165.  
  166.         default:
  167.             child = win_get_child(win, arg2 & 0xFFFF, (arg2>>16));
  168.             win->child_over = child;
  169.             if(child) send_message(child, msg, 0, arg2);
  170.     };
  171.  
  172.     return 0;
  173. }
  174.  
  175. static int draw_window(window_t *win)
  176. {
  177.     ctrl_t *child;
  178.     void   *ctx;
  179.     rect_t *rc = &win->client;
  180.  
  181.     draw_caption(&win->caption);
  182.     draw_panel(&win->panel);
  183.  
  184. //    draw_frame(win);
  185.  
  186. //    child  = (ctrl_t*)win->child.next;
  187.  
  188. //    while( &child->link != &win->child)
  189. //    {
  190. //        send_message(child, 1, 0, 0);
  191. //        child = (ctrl_t*)child->link.next;
  192. //    };
  193.  
  194.     return 0;
  195. };
  196.  
  197. void blit_client(window_t *win)
  198. {
  199.     int w, h;
  200.  
  201.     w = win->client.r - win->client.l;
  202.     h = win->client.b - win->client.t;
  203.  
  204.     Blit(win->ctx->pixmap->data, win->client.l, win->client.t,
  205.          0, 0, w, h, w, h,win->ctx->pixmap->pitch);
  206. };
  207.  
  208.  
  209. int show_window(window_t *win, int state)
  210. {
  211.     win->win_state = state;
  212.  
  213.     draw_window(win);
  214.  
  215.     BeginDraw();
  216.     DrawWindow(win->rc.l, win->rc.t, win->w-1, win->h-1,
  217.                NULL,0,0x41);
  218.     EndDraw();
  219.  
  220.     blit_caption(&win->caption);
  221.     blit_panel(&win->panel);
  222. //    blit_client(win);
  223.     return 0;
  224. }
  225.  
  226. void window_update_layout(window_t *win)
  227. {
  228.     char proc_info[1024];
  229.  
  230.     int new_w, new_h;
  231.     uint8_t  state;
  232.  
  233.     int winx, winy, winw, winh;
  234.  
  235. //    __asm__ __volatile__("int3");
  236.  
  237.     get_proc_info(proc_info);
  238.  
  239.     winx = *(uint32_t*)(proc_info+34);
  240.     winy = *(uint32_t*)(proc_info+38);
  241.     winw = *(uint32_t*)(proc_info+42)+1;
  242.     winh = *(uint32_t*)(proc_info+46)+1;
  243.  
  244.     state  = *(uint8_t*)(proc_info+70);
  245.  
  246.     if(state & 2)
  247.     {   win->win_state = MINIMIZED;
  248.         return;
  249.     }
  250.     if(state & 4)
  251.     {
  252.         win->win_state = ROLLED;
  253.         return;
  254.     };
  255.    
  256.     if(state & 1)
  257.         win->win_state = MAXIMIZED;
  258.     else
  259.         win->win_state = NORMAL;
  260.    
  261.     if( (winx != win->rc.l) || (winy != win->rc.t) )
  262.     {
  263.         win->rc.l = winx;
  264.         win->rc.t = winy;
  265.         win->rc.r = winx + win->w;
  266.         win->rc.b = winy + win->h;
  267.     };
  268.  
  269.     if( winw  == win->w &&
  270.         winh  == win->h)
  271.         return;
  272.  
  273. #if 0
  274.     int old_size;
  275.     int new_size;
  276.     int pitch;
  277.  
  278.  
  279.     old_size = win->bitmap.pitch * win->bitmap.height;
  280.     old_size = (old_size+4095) & ~4095;
  281.  
  282.     pitch = ALIGN(win->w*4, 16);
  283.  
  284.     new_size = pitch * win->h;
  285.     new_size = (new_size+4095) & ~4095;
  286.  
  287.     if( new_size < old_size)
  288.         user_unmap(win->bitmap.data, new_size, old_size-new_size);
  289.  
  290.     win->bitmap.width = win->w;
  291.     win->bitmap.pitch = pitch;
  292. #endif
  293.  
  294.     win->rc.r = winx + winw;
  295.     win->rc.b = winy + winh;
  296.     win->w = winw;
  297.     win->h = winh;
  298.  
  299.     update_caption_size(win);
  300.     update_panel_size(win);
  301.     adjust_frame(win);
  302.  
  303.     send_message((ctrl_t*)win, MSG_SIZE, 0, 0);
  304.     draw_window(win);
  305. };
  306.  
  307.  
  308. int send_mouse_message(window_t *win, uint32_t msg)
  309. {
  310.     ctrl_t *child;
  311.  
  312.     if(mouse_capture)
  313.         return send_message(mouse_capture, msg, 0, old_pos.val);
  314.  
  315.     if(pt_in_rect(&win->caption.ctrl.rc, old_pos.x, old_pos.y))
  316.     {
  317.         return send_message(&win->caption.ctrl, msg, 0, old_pos.val);
  318.     }
  319.  
  320.     if(pt_in_rect(&win->panel.ctrl.rc, old_pos.x, old_pos.y))
  321.     {
  322. //        old_pos.x-= win->panel.ctrl.rc.l;
  323. //        old_pos.y-= win->panel.ctrl.rc.t;
  324.         return send_message(&win->panel.ctrl, msg, 0, old_pos.val);
  325.     }
  326.  
  327.     if(pt_in_rect(&win->client, old_pos.x, old_pos.y))
  328.         return send_message((ctrl_t*)win, msg, 0, old_pos.val);
  329.  
  330.     return send_message(&win->frame, msg, 0, old_pos .val);
  331.  
  332. //    if( ( old_pos.x < win->rc.r) && ( old_pos.y < win->rc.b))
  333. //        send_message((ctrl_t*)win, msg, 0, old_pos.val);
  334. };
  335.  
  336. void do_sys_draw(window_t *win)
  337. {
  338. //    printf("%s win:%x\n", __FUNCTION__, win);
  339.  
  340.     window_update_layout(win);
  341.  
  342.     BeginDraw();
  343.     DrawWindow(0,0,0,0, NULL, 0x000000,0x41);
  344.     EndDraw();
  345.  
  346.     blit_caption(&win->caption);
  347.     blit_panel(&win->panel);
  348.  
  349. //    blit_client(win);
  350.     send_message((ctrl_t*)win, MSG_DRAW_CLIENT, 0, 0);
  351.     need_update=0;
  352. };
  353.  
  354. static void do_sys_mouse(window_t *win)
  355. {
  356.     static uint32_t  mouse_click_time;
  357.     static int  mouse_action;
  358.     static int  old_buttons;
  359.     int         buttons;
  360.     uint32_t    wheels;
  361.     uint32_t    click_time;
  362.     int         action;
  363.  
  364.     pos_t       pos;
  365.  
  366.     mouse_action = 0;
  367.     pos = get_mouse_pos();
  368.  
  369.     if(pos.val != old_pos.val)
  370.     {
  371.         mouse_action = 0x80000000;
  372.         old_pos = pos;
  373.     };
  374. //    printf("pos x%d y%d\n", pos.x, pos.y);
  375.  
  376.     buttons = get_mouse_buttons();
  377.     wheels = get_mouse_wheels();
  378.  
  379.     if( wheels & 0xFFFF){
  380.         wheels = (short)wheels>0 ? MSG_WHEELDOWN : MSG_WHEELUP;
  381.         send_mouse_message(win, wheels);
  382.     }
  383.  
  384.     if((action = (buttons ^ old_buttons))!=0)
  385.     {
  386.         mouse_action|= action<<3;
  387.         mouse_action|= buttons & ~old_buttons;
  388.     }
  389.     old_buttons = buttons;
  390.  
  391.     if(mouse_action & 0x80000000) {
  392.         DBG("mouse move \n\r");
  393.         send_mouse_message(win, MSG_MOUSEMOVE);
  394.     };
  395.  
  396.     if(mouse_action & 0x09)
  397.     {
  398.         if((mouse_action & 0x09)==0x09)
  399.         {
  400. //            printf("left button down x= %d y= %d\n\r", old_x.x, old_x.y);
  401.             click_time = get_tick_count();
  402.             if(click_time < mouse_click_time+35) {
  403.                 mouse_click_time = click_time;
  404.                 send_mouse_message(win,MSG_LBTNDBLCLK);
  405.             }
  406.             else {
  407.               mouse_click_time = click_time;
  408.               send_mouse_message(win,MSG_LBTNDOWN);
  409.             };
  410.         }
  411.         else {
  412. //            printf("left button up \n\r");
  413.             send_mouse_message(win,MSG_LBTNUP);
  414.         }
  415.     };
  416.  
  417.     if(mouse_action & 0x12)
  418.     {
  419.         if((mouse_action & 0x12)==0x12) {
  420.             DBG("right button down \n\r");
  421.             send_mouse_message(win,MSG_RBTNDOWN);
  422.         }
  423.         else {
  424.             DBG("right button up \n\r");
  425.             send_mouse_message(win,MSG_RBTNUP);
  426.         };
  427.     };
  428.     if(mouse_action & 0x24)
  429.     {
  430.         if((mouse_action & 0x24)==0x24){
  431.             DBG("middle button down \n\r");
  432.             send_mouse_message(win,MSG_MBTNDOWN);
  433.         }
  434.         else {
  435.             DBG("middle button up \n\r");
  436.             send_mouse_message(win,MSG_MBTNUP);
  437.         };
  438.     };
  439.  
  440.  
  441. };
  442.  
  443.  
  444. void  run_window(window_t *win)
  445. {
  446.     int ev;
  447.     oskey_t   key;
  448.  
  449. //    buttons   = get_mouse_buttons();
  450. //    wheels    = get_mouse_wheels();
  451.     realtime  = get_tick_count();
  452.     exp_time  = -1;
  453.  
  454.     while(1)
  455.     {
  456.         wait_time = exp_time - realtime;
  457.  
  458.         ev = wait_for_event(wait_time);
  459.  
  460.         realtime = get_tick_count();
  461.  
  462. //        if(exp_time < realtime)
  463. //            exp_time = update_timers(realtime);
  464.  
  465.         switch(ev)
  466.         {
  467.             case MSG_PAINT:
  468.                 do_sys_draw(win);
  469.                 continue;
  470.  
  471.             case 2:
  472.                 key = get_key();
  473.                 if( key.state == 0)
  474.                     send_message((ctrl_t*)win, ev, 0, key.code);
  475.                 continue;
  476.  
  477.             case 6:
  478.                 do_sys_mouse(win);
  479.                 continue;
  480.  
  481.             default:
  482.                 continue;
  483.         };
  484.     };
  485. }
  486.  
  487. void render_time(void *render);
  488.  
  489. void  run_render(window_t *win, void *render)
  490. {
  491.     int ev;
  492.     oskey_t   key;
  493.  
  494.     realtime  = get_tick_count();
  495.     exp_time  = -1;
  496.  
  497.     while(win->win_command != WIN_CLOSED)
  498.     {
  499.         wait_time = exp_time - realtime;
  500.  
  501.         ev = check_os_event();
  502.  
  503.         realtime = get_tick_count();
  504.  
  505. //        if(exp_time < realtime)
  506. //            exp_time = update_timers(realtime);
  507.  
  508.         switch(ev)
  509.         {
  510.             case MSG_PAINT:
  511.                 do_sys_draw(win);
  512.                 break;
  513.  
  514.             case 2:
  515.                 key = get_key();
  516.                 if( key.state == 0)
  517.                     send_message((ctrl_t*)win, ev, 0, key.code);
  518.                 break;
  519.  
  520.             case 6:
  521.                 do_sys_mouse(win);
  522.                 break;
  523.  
  524.             default:
  525.                 break;
  526.         };
  527.  
  528.         render_time(render);
  529.     };
  530. };
  531.  
  532.  
  533.  
  534. extern unsigned char res_cursor_ns[];
  535. extern unsigned char res_cursor_we[];
  536. extern unsigned char res_cursor_nwse[];
  537. extern unsigned char res_cursor_nesw[];
  538.  
  539. int init_resources()
  540. {
  541.     cursor_ns   = load_cursor(res_cursor_ns, LOAD_FROM_MEM);
  542.     cursor_we   = load_cursor(res_cursor_we, LOAD_FROM_MEM);
  543.     cursor_nwse = load_cursor(res_cursor_nwse, LOAD_FROM_MEM);
  544.     cursor_nesw = load_cursor(res_cursor_nesw, LOAD_FROM_MEM);
  545.  
  546.     win_font =  init_fontlib();
  547.  
  548.     return 1;
  549. }
  550.  
  551. int  fini_winlib()
  552. {
  553.     int ret;
  554.  
  555.     ret =  destroy_cursor(cursor_nesw);
  556.     ret |= destroy_cursor(cursor_nwse);
  557.     ret |= destroy_cursor(cursor_we);
  558.     ret |= destroy_cursor(cursor_ns);
  559.  
  560.     return ret;
  561. };
  562.  
  563.  
  564.  
  565. void  init_winlib(void)
  566. {
  567.     __asm__ __volatile__(
  568.     "int $0x40"
  569.     ::"a"(40), "b"(0xC0000027));
  570.  
  571.     init_resources();
  572.     list_initialize(&timers);
  573. };
  574.  
  575. //ctx_t *get_window_ctx()
  576. //{
  577. //    return &Window.client_ctx;
  578. //};
  579.  
  580. void update_rect(ctrl_t *ctrl)
  581. {
  582.     int ctx_w, ctx_h;
  583.     int src_x, src_y;
  584.  
  585.     src_x = ctrl->rc.l - ctrl->ctx->offset_x;
  586.     src_y = ctrl->rc.t - ctrl->ctx->offset_y;
  587.  
  588.     ctx_w = ctrl->parent->w;
  589.     ctx_h = ctrl->parent->h;
  590.  
  591.     Blit(ctrl->ctx->pixmap->data, ctrl->rc.l, ctrl->rc.t, src_x, src_y,
  592.          ctrl->w, ctrl->h, ctx_w, ctx_h, ctrl->ctx->pixmap->pitch);
  593.  
  594. //    need_update++;
  595. };
  596.  
  597.  
  598. void Blit(void *bitmap, int dst_x, int dst_y,
  599.                         int src_x, int src_y, int w, int h,
  600.                         int src_w, int src_h, int stride)
  601. {
  602.     volatile struct blit_call bc;
  603.  
  604.     bc.dstx = dst_x;
  605.     bc.dsty = dst_y;
  606.     bc.w    = w;
  607.     bc.h    = h;
  608.     bc.srcx = src_x;
  609.     bc.srcy = src_y;
  610.     bc.srcw = src_w;
  611.     bc.srch = src_h;
  612.     bc.stride = stride;
  613.     bc.bitmap = bitmap;
  614.  
  615.     __asm__ __volatile__(
  616.     "int $0x40"
  617.     ::"a"(73),"b"(0x20),"c"(&bc.dstx));
  618.  
  619. };
  620.  
  621. ctrl_t *get_child(ctrl_t *ctrl, int x, int y)
  622. {
  623.     ctrl_t *child = NULL;
  624.  
  625.     ctrl_t *tmp = (ctrl_t*)ctrl->child.next;
  626.  
  627.     while( &tmp->link != &ctrl->child )
  628.     {
  629.         if(pt_in_rect(&tmp->rc, x, y))
  630.         {
  631.             child = get_child(tmp, x, y);
  632.             return child == NULL ? tmp : child;
  633.         };
  634.         tmp = (ctrl_t*)tmp->link.next;
  635.     };
  636.     return child;
  637. };
  638.  
  639. ctrl_t *capture_mouse(ctrl_t *newm)
  640. {
  641.     ctrl_t *old = mouse_capture;
  642.  
  643.     mouse_capture = newm;
  644.  
  645.     __asm__ __volatile__(
  646.     "int $0x40"
  647.     ::"a"(40), "b"(0x80000027));
  648.  
  649.     return old;
  650. }
  651.  
  652. void release_mouse(void)
  653. {
  654.     mouse_capture = NULL;
  655.     __asm__ __volatile__(
  656.     "int $0x40"
  657.     ::"a"(40), "b"(0xC0000027));
  658. }
  659.