Subversion Repositories Kolibri OS

Rev

Rev 3300 | 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.         state = MAXIMIZED;
  258.     else
  259.         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.         state == win->win_state)
  272.         return;
  273.  
  274.     if(win->win_state != FULLSCREEN)
  275.         win->win_state = state;
  276.    
  277. #if 0
  278.     int old_size;
  279.     int new_size;
  280.     int pitch;
  281.  
  282.  
  283.     old_size = win->bitmap.pitch * win->bitmap.height;
  284.     old_size = (old_size+4095) & ~4095;
  285.  
  286.     pitch = ALIGN(win->w*4, 16);
  287.  
  288.     new_size = pitch * win->h;
  289.     new_size = (new_size+4095) & ~4095;
  290.  
  291.     if( new_size < old_size)
  292.         user_unmap(win->bitmap.data, new_size, old_size-new_size);
  293.  
  294.     win->bitmap.width = win->w;
  295.     win->bitmap.pitch = pitch;
  296. #endif
  297.  
  298.     win->rc.r = winx + winw;
  299.     win->rc.b = winy + winh;
  300.     win->w = winw;
  301.     win->h = winh;
  302.  
  303.     update_caption_size(win);
  304.     update_panel_size(win);
  305.     adjust_frame(win);
  306.  
  307.     send_message((ctrl_t*)win, MSG_SIZE, 0, 0);
  308.     draw_window(win);
  309. };
  310.  
  311.  
  312. int send_mouse_message(window_t *win, uint32_t msg)
  313. {
  314.     ctrl_t *child;
  315.  
  316.     if(mouse_capture)
  317.         return send_message(mouse_capture, msg, 0, old_pos.val);
  318.  
  319.     if(pt_in_rect(&win->caption.ctrl.rc, old_pos.x, old_pos.y))
  320.     {
  321.         return send_message(&win->caption.ctrl, msg, 0, old_pos.val);
  322.     }
  323.  
  324.     if(pt_in_rect(&win->panel.ctrl.rc, old_pos.x, old_pos.y))
  325.     {
  326. //        old_pos.x-= win->panel.ctrl.rc.l;
  327. //        old_pos.y-= win->panel.ctrl.rc.t;
  328.         return send_message(&win->panel.ctrl, msg, 0, old_pos.val);
  329.     }
  330.  
  331.     if(pt_in_rect(&win->client, old_pos.x, old_pos.y))
  332.         return send_message((ctrl_t*)win, msg, 0, old_pos.val);
  333.  
  334.     return send_message(&win->frame, msg, 0, old_pos .val);
  335.  
  336. //    if( ( old_pos.x < win->rc.r) && ( old_pos.y < win->rc.b))
  337. //        send_message((ctrl_t*)win, msg, 0, old_pos.val);
  338. };
  339.  
  340. void do_sys_draw(window_t *win)
  341. {
  342. //    printf("%s win:%x\n", __FUNCTION__, win);
  343.  
  344.     window_update_layout(win);
  345.  
  346.     BeginDraw();
  347.     DrawWindow(0,0,0,0, NULL, 0x000000,0x41);
  348.     EndDraw();
  349.  
  350.     blit_caption(&win->caption);
  351.     blit_panel(&win->panel);
  352.  
  353. //    blit_client(win);
  354.     send_message((ctrl_t*)win, MSG_DRAW_CLIENT, 0, 0);
  355.     need_update=0;
  356. };
  357.  
  358. static void do_sys_mouse(window_t *win)
  359. {
  360.     static uint32_t  mouse_click_time;
  361.     static int  mouse_action;
  362.     static int  old_buttons;
  363.     int         buttons;
  364.     uint32_t    wheels;
  365.     uint32_t    click_time;
  366.     int         action;
  367.  
  368.     pos_t       pos;
  369.  
  370.     mouse_action = 0;
  371.     pos = get_mouse_pos();
  372.  
  373.     if(pos.val != old_pos.val)
  374.     {
  375.         mouse_action = 0x80000000;
  376.         old_pos = pos;
  377.     };
  378. //    printf("pos x%d y%d\n", pos.x, pos.y);
  379.  
  380.     buttons = get_mouse_buttons();
  381.     wheels = get_mouse_wheels();
  382.  
  383.     if( wheels & 0xFFFF){
  384.         wheels = (short)wheels>0 ? MSG_WHEELDOWN : MSG_WHEELUP;
  385.         send_mouse_message(win, wheels);
  386.     }
  387.  
  388.     if((action = (buttons ^ old_buttons))!=0)
  389.     {
  390.         mouse_action|= action<<3;
  391.         mouse_action|= buttons & ~old_buttons;
  392.     }
  393.     old_buttons = buttons;
  394.  
  395.     if(mouse_action & 0x80000000) {
  396.         DBG("mouse move \n\r");
  397.         send_mouse_message(win, MSG_MOUSEMOVE);
  398.     };
  399.  
  400.     if(mouse_action & 0x09)
  401.     {
  402.         if((mouse_action & 0x09)==0x09)
  403.         {
  404. //            printf("left button down x= %d y= %d\n\r", old_x.x, old_x.y);
  405.             click_time = get_tick_count();
  406.             if(click_time < mouse_click_time+35) {
  407.                 mouse_click_time = click_time;
  408.                 send_mouse_message(win,MSG_LBTNDBLCLK);
  409.             }
  410.             else {
  411.               mouse_click_time = click_time;
  412.               send_mouse_message(win,MSG_LBTNDOWN);
  413.             };
  414.         }
  415.         else {
  416. //            printf("left button up \n\r");
  417.             send_mouse_message(win,MSG_LBTNUP);
  418.         }
  419.     };
  420.  
  421.     if(mouse_action & 0x12)
  422.     {
  423.         if((mouse_action & 0x12)==0x12) {
  424.             DBG("right button down \n\r");
  425.             send_mouse_message(win,MSG_RBTNDOWN);
  426.         }
  427.         else {
  428.             DBG("right button up \n\r");
  429.             send_mouse_message(win,MSG_RBTNUP);
  430.         };
  431.     };
  432.     if(mouse_action & 0x24)
  433.     {
  434.         if((mouse_action & 0x24)==0x24){
  435.             DBG("middle button down \n\r");
  436.             send_mouse_message(win,MSG_MBTNDOWN);
  437.         }
  438.         else {
  439.             DBG("middle button up \n\r");
  440.             send_mouse_message(win,MSG_MBTNUP);
  441.         };
  442.     };
  443.  
  444.  
  445. };
  446.  
  447.  
  448. void  run_window(window_t *win)
  449. {
  450.     int ev;
  451.     oskey_t   key;
  452.  
  453. //    buttons   = get_mouse_buttons();
  454. //    wheels    = get_mouse_wheels();
  455.     realtime  = get_tick_count();
  456.     exp_time  = -1;
  457.  
  458.     while(1)
  459.     {
  460.         wait_time = exp_time - realtime;
  461.  
  462.         ev = wait_for_event(wait_time);
  463.  
  464.         realtime = get_tick_count();
  465.  
  466. //        if(exp_time < realtime)
  467. //            exp_time = update_timers(realtime);
  468.  
  469.         switch(ev)
  470.         {
  471.             case MSG_PAINT:
  472.                 do_sys_draw(win);
  473.                 continue;
  474.  
  475.             case 2:
  476.                 key = get_key();
  477.                 if( key.state == 0)
  478.                     send_message((ctrl_t*)win, ev, 0, key.code);
  479.                 continue;
  480.  
  481.             case 6:
  482.                 do_sys_mouse(win);
  483.                 continue;
  484.  
  485.             default:
  486.                 continue;
  487.         };
  488.     };
  489. }
  490.  
  491. void render_time(void *render);
  492.  
  493. void  run_render(window_t *win, void *render)
  494. {
  495.     int ev;
  496.     oskey_t   key;
  497.  
  498.     realtime  = get_tick_count();
  499.     exp_time  = -1;
  500.  
  501.     while(win->win_command != WIN_CLOSED)
  502.     {
  503.         wait_time = exp_time - realtime;
  504.  
  505.         ev = check_os_event();
  506.  
  507.         realtime = get_tick_count();
  508.  
  509. //        if(exp_time < realtime)
  510. //            exp_time = update_timers(realtime);
  511.  
  512.         switch(ev)
  513.         {
  514.             case MSG_PAINT:
  515.                 do_sys_draw(win);
  516.                 break;
  517.  
  518.             case 2:
  519.                 key = get_key();
  520.                 if( key.state == 0)
  521.                     send_message((ctrl_t*)win, ev, 0, key.code);
  522.                 break;
  523.  
  524.             case 6:
  525.                 do_sys_mouse(win);
  526.                 break;
  527.  
  528.             default:
  529.                 break;
  530.         };
  531.  
  532.         render_time(render);
  533.     };
  534. };
  535.  
  536.  
  537.  
  538. extern unsigned char res_cursor_ns[];
  539. extern unsigned char res_cursor_we[];
  540. extern unsigned char res_cursor_nwse[];
  541. extern unsigned char res_cursor_nesw[];
  542.  
  543. int init_resources()
  544. {
  545.     cursor_ns   = load_cursor(res_cursor_ns, LOAD_FROM_MEM);
  546.     cursor_we   = load_cursor(res_cursor_we, LOAD_FROM_MEM);
  547.     cursor_nwse = load_cursor(res_cursor_nwse, LOAD_FROM_MEM);
  548.     cursor_nesw = load_cursor(res_cursor_nesw, LOAD_FROM_MEM);
  549.  
  550.     win_font =  init_fontlib();
  551.  
  552.     return 1;
  553. }
  554.  
  555. int  fini_winlib()
  556. {
  557.     int ret;
  558.  
  559.     ret =  destroy_cursor(cursor_nesw);
  560.     ret |= destroy_cursor(cursor_nwse);
  561.     ret |= destroy_cursor(cursor_we);
  562.     ret |= destroy_cursor(cursor_ns);
  563.  
  564.     return ret;
  565. };
  566.  
  567.  
  568.  
  569. void  init_winlib(void)
  570. {
  571.     __asm__ __volatile__(
  572.     "int $0x40"
  573.     ::"a"(40), "b"(0xC0000027));
  574.  
  575.     init_resources();
  576.     list_initialize(&timers);
  577. };
  578.  
  579. //ctx_t *get_window_ctx()
  580. //{
  581. //    return &Window.client_ctx;
  582. //};
  583.  
  584. void update_rect(ctrl_t *ctrl)
  585. {
  586.     int ctx_w, ctx_h;
  587.     int src_x, src_y;
  588.  
  589.     src_x = ctrl->rc.l - ctrl->ctx->offset_x;
  590.     src_y = ctrl->rc.t - ctrl->ctx->offset_y;
  591.  
  592.     ctx_w = ctrl->parent->w;
  593.     ctx_h = ctrl->parent->h;
  594.  
  595.     Blit(ctrl->ctx->pixmap->data, ctrl->rc.l, ctrl->rc.t, src_x, src_y,
  596.          ctrl->w, ctrl->h, ctx_w, ctx_h, ctrl->ctx->pixmap->pitch);
  597.  
  598. //    need_update++;
  599. };
  600.  
  601.  
  602. void Blit(void *bitmap, int dst_x, int dst_y,
  603.                         int src_x, int src_y, int w, int h,
  604.                         int src_w, int src_h, int stride)
  605. {
  606.     volatile struct blit_call bc;
  607.  
  608.     bc.dstx = dst_x;
  609.     bc.dsty = dst_y;
  610.     bc.w    = w;
  611.     bc.h    = h;
  612.     bc.srcx = src_x;
  613.     bc.srcy = src_y;
  614.     bc.srcw = src_w;
  615.     bc.srch = src_h;
  616.     bc.stride = stride;
  617.     bc.bitmap = bitmap;
  618.  
  619.     __asm__ __volatile__(
  620.     "int $0x40"
  621.     ::"a"(73),"b"(0x20),"c"(&bc.dstx));
  622.  
  623. };
  624.  
  625. ctrl_t *get_child(ctrl_t *ctrl, int x, int y)
  626. {
  627.     ctrl_t *child = NULL;
  628.  
  629.     ctrl_t *tmp = (ctrl_t*)ctrl->child.next;
  630.  
  631.     while( &tmp->link != &ctrl->child )
  632.     {
  633.         if(pt_in_rect(&tmp->rc, x, y))
  634.         {
  635.             child = get_child(tmp, x, y);
  636.             return child == NULL ? tmp : child;
  637.         };
  638.         tmp = (ctrl_t*)tmp->link.next;
  639.     };
  640.     return child;
  641. };
  642.  
  643. ctrl_t *capture_mouse(ctrl_t *newm)
  644. {
  645.     ctrl_t *old = mouse_capture;
  646.  
  647.     mouse_capture = newm;
  648.  
  649.     __asm__ __volatile__(
  650.     "int $0x40"
  651.     ::"a"(40), "b"(0x80000027));
  652.  
  653.     return old;
  654. }
  655.  
  656. void release_mouse(void)
  657. {
  658.     mouse_capture = NULL;
  659.     __asm__ __volatile__(
  660.     "int $0x40"
  661.     ::"a"(40), "b"(0xC0000027));
  662. }
  663.