Ticket #3571: 3571-WEdit-get-rid-of-mouse-event-pump.patch

File 3571-WEdit-get-rid-of-mouse-event-pump.patch, 10.0 KB (added by mooffie, 9 years ago)
  • src/editor/editwidget.c

    From 52676419a293f162a6ee866d7e60daac703bf4e6 Mon Sep 17 00:00:00 2001
    From: Mooffie <mooffie@gmail.com>
    Date: Sat, 26 Mar 2016 22:31:04 +0300
    Subject: [PATCH] WEdit: get rid of mouse event pump.
    
    ---
     src/editor/editwidget.c | 169 +++++++++++++++++-------------------------------
     1 file changed, 60 insertions(+), 109 deletions(-)
    
    diff --git a/src/editor/editwidget.c b/src/editor/editwidget.c
    index 277af8e..b2f1ee0 100644
    a b edit_update_cursor (WEdit * edit, const mouse_event_t * event) 
    752752} 
    753753 
    754754/* --------------------------------------------------------------------------------------------- */ 
    755  
    756 static inline void 
    757 edit_mouse_move_resize (WEdit * edit, mouse_event_t event) 
    758 { 
    759     Widget *w = WIDGET (edit); 
    760     Gpm_Event gevent; 
    761  
    762     /* init with 1-based global coordinates */ 
    763     gevent.y = w->y + event.y + 1; 
    764     gevent.x = w->x + event.x + 1; 
    765  
    766     while (edit->drag_state != MCEDIT_DRAG_NORMAL) 
    767     { 
    768         int c; 
    769  
    770         if (event.msg == MSG_MOUSE_UP) 
    771             goto finish; 
    772  
    773         /* resize or move */ 
    774         if (!edit->fullscreen) 
    775         { 
    776             Widget *h = WIDGET (w->owner); 
    777  
    778             if (edit->drag_state == MCEDIT_DRAG_MOVE) 
    779             { 
    780                 /* make 0-based */ 
    781                 gevent.y--; 
    782                 gevent.x--; 
    783  
    784                 gevent.y = max (gevent.y, h->y + 1);    /* status line */ 
    785                 gevent.y = min (gevent.y, h->y + h->lines - 2); /* buttonbar */ 
    786                 gevent.x = max (gevent.x, h->x); 
    787                 gevent.x = min (gevent.x, h->x + h->cols - 1); 
    788  
    789                 /* don't use widget_set_size() here to avoid double draw  */ 
    790                 w->y = gevent.y; 
    791                 w->x = gevent.x - edit->drag_state_start; 
    792                 edit->force |= REDRAW_COMPLETELY; 
    793             } 
    794             else if (edit->drag_state == MCEDIT_DRAG_RESIZE) 
    795             { 
    796                 gevent.y = min (gevent.y, h->y + h->lines - 1); /* buttonbar */ 
    797                 gevent.x = min (gevent.x, h->x + h->cols); 
    798                 gevent = mouse_get_local (&gevent, w); 
    799  
    800                 /* don't use widget_set_size() here to avoid double draw  */ 
    801                 w->lines = max (WINDOW_MIN_LINES, gevent.y); 
    802                 w->cols = max (WINDOW_MIN_COLS, gevent.x); 
    803                 edit->force |= REDRAW_COMPLETELY; 
    804             } 
    805  
    806             dlg_redraw (w->owner); 
    807         } 
    808  
    809         /* Get next event. 
    810          * Don't use standard way in order to continue event handling 
    811          * outside of widget */ 
    812         c = tty_get_event (&gevent, FALSE, TRUE); 
    813         if (c == EV_MOUSE) 
    814             event = mouse_translate_event (w, &gevent); 
    815         else 
    816         { 
    817           finish: 
    818             /* redraw frame */ 
    819             edit->drag_state = MCEDIT_DRAG_NORMAL; 
    820             edit->force |= REDRAW_COMPLETELY; 
    821             edit_update_screen (edit); 
    822             w->mouse.forced_capture = FALSE; 
    823         } 
    824     } 
    825 } 
    826  
    827 /* --------------------------------------------------------------------------------------------- */ 
    828755/** Callback for the edit dialog */ 
    829756 
    830757static cb_ret_t 
    edit_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *da 
    1072999/* --------------------------------------------------------------------------------------------- */ 
    10731000 
    10741001/** 
     1002 * Handle move/resize mouse events. 
     1003 */ 
     1004static void 
     1005edit_mouse_handle_move_resize (Widget * w, mouse_msg_t msg, mouse_event_t * event) 
     1006{ 
     1007    WEdit *edit = (WEdit *) (w); 
     1008    Widget *h = WIDGET (w->owner); 
     1009    int global_x, global_y; 
     1010 
     1011    if (msg == MSG_MOUSE_UP) 
     1012    { 
     1013        /* Exit drag mode. */ 
     1014        edit_execute_cmd (edit, CK_Enter, -1); 
     1015        return; 
     1016    } 
     1017 
     1018    /* Convert point to global coordinates for easier calculations. */ 
     1019    global_x = event->x + w->x; 
     1020    global_y = event->y + w->y; 
     1021 
     1022    /* Clamp the point to the dialog's client area. */ 
     1023    global_y = CLAMP (global_y, h->y + 1, h->y + h->lines - 2); /* Status line, buttonbar */ 
     1024    global_x = CLAMP (global_x, h->x, h->x + h->cols - 1);      /* Currently a no-op, as the dialog has no left/right margins. */ 
     1025 
     1026    if (edit->drag_state == MCEDIT_DRAG_MOVE) 
     1027    { 
     1028        w->y = global_y; 
     1029        w->x = global_x - edit->drag_state_start; 
     1030    } 
     1031    else if (edit->drag_state == MCEDIT_DRAG_RESIZE) 
     1032    { 
     1033        w->lines = max (WINDOW_MIN_LINES, global_y - w->y + 1); 
     1034        w->cols = max (WINDOW_MIN_COLS, global_x - w->x + 1); 
     1035    } 
     1036 
     1037    edit->force |= REDRAW_COMPLETELY;   /* Not really needed as WEdit's MSG_DRAW already does this. */ 
     1038 
     1039    /* We draw the whole dialog because dragging/resizing exposes area beneath. */ 
     1040    dlg_redraw (w->owner); 
     1041} 
     1042 
     1043/* --------------------------------------------------------------------------------------------- */ 
     1044 
     1045/** 
    10751046 * Handle mouse events of editor window 
    10761047 * 
    10771048 * @param w Widget object (the editor window) 
    edit_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event) 
    10901061    close_x = (w->cols - 1) - dx - 1; 
    10911062    toggle_fullscreen_x = close_x - 3; 
    10921063 
     1064    if (edit->drag_state != MCEDIT_DRAG_NORMAL) 
     1065    { 
     1066        /* window is being resized/moved */ 
     1067        edit_mouse_handle_move_resize (w, msg, event); 
     1068        return; 
     1069    } 
     1070 
    10931071    switch (msg) 
    10941072    { 
    10951073    case MSG_MOUSE_DOWN: 
    1096         if (w->mouse.forced_capture) 
    1097             /* window is being resized/moved by keys */ 
    1098             break; 
    1099  
    11001074        dlg_set_top_widget (w); 
    11011075        edit_update_curs_row (edit); 
    11021076        edit_update_curs_col (edit); 
    edit_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event) 
    11131087                { 
    11141088                    /* start window move */ 
    11151089                    edit->drag_state_start = event->x; 
    1116                     edit->drag_state = MCEDIT_DRAG_MOVE; 
    1117                     edit->force |= REDRAW_COMPLETELY; 
    1118                     edit_update_screen (edit); 
     1090                    edit_execute_cmd (edit, CK_WindowMove, -1); 
    11191091                } 
    11201092                break; 
    11211093            } 
    edit_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event) 
    11231095            if (event->y == w->lines - 1 && event->x == w->cols - 1) 
    11241096            { 
    11251097                /* bottom-right corner -- start window resize */ 
    1126                 edit->drag_state = MCEDIT_DRAG_RESIZE; 
    1127                 edit->force |= REDRAW_COMPLETELY; 
    1128                 edit_update_screen (edit); 
     1098                edit_execute_cmd (edit, CK_WindowResize, -1); 
    11291099                break; 
    11301100            } 
    11311101        } 
    11321102 
    1133         /* fall throught to start/stop text selection */ 
     1103        /* fall through to start/stop text selection */ 
    11341104 
    11351105    case MSG_MOUSE_UP: 
    1136         edit->drag_state = MCEDIT_DRAG_NORMAL; 
    11371106        edit_update_cursor (edit, event); 
    11381107        edit_total_update (edit); 
    1139         w->mouse.forced_capture = FALSE; 
    11401108        break; 
    11411109 
    11421110    case MSG_MOUSE_CLICK: 
    1143         edit->drag_state = MCEDIT_DRAG_NORMAL; 
    1144  
    11451111        if (event->y == 0) 
    11461112        { 
    11471113            if (event->x == close_x) 
    edit_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event) 
    11491115            else if (event->x == toggle_fullscreen_x) 
    11501116                edit_toggle_fullscreen (edit); 
    11511117            else if (!edit->fullscreen && event->count == GPM_DOUBLE) 
    1152             { 
    11531118                /* double click on top line (toggle fullscreen) */ 
    11541119                edit_toggle_fullscreen (edit); 
    1155                 edit->drag_state = MCEDIT_DRAG_NORMAL; 
    1156                 edit->force |= REDRAW_COMPLETELY; 
    1157                 edit_update_screen (edit); 
    1158             } 
    11591120        } 
    11601121        else if (event->count == GPM_DOUBLE) 
    11611122        { 
    edit_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event) 
    11721133        break; 
    11731134 
    11741135    case MSG_MOUSE_DRAG: 
    1175         if (edit->drag_state == MCEDIT_DRAG_NORMAL) 
    1176         { 
    1177             edit_update_cursor (edit, event); 
    1178             edit_total_update (edit); 
    1179         } 
    1180         else 
    1181             edit_mouse_move_resize (edit, *event); 
     1136        edit_update_cursor (edit, event); 
     1137        edit_total_update (edit); 
    11821138        break; 
    11831139 
    11841140    case MSG_MOUSE_SCROLL_UP: 
    edit_add_window (WDialog * h, int y, int x, int lines, int cols, const vfs_path_ 
    14051361gboolean 
    14061362edit_handle_move_resize (WEdit * edit, long command) 
    14071363{ 
    1408     Widget *w = WIDGET (edit); 
    14091364    gboolean ret = FALSE; 
    14101365 
    14111366    if (edit->fullscreen) 
    14121367    { 
    14131368        edit->drag_state = MCEDIT_DRAG_NORMAL; 
    1414         w->mouse.forced_capture = FALSE; 
    14151369        return ret; 
    14161370    } 
    14171371 
    edit_handle_move_resize (WEdit * edit, long command) 
    14241378        case CK_WindowMove: 
    14251379            edit->drag_state = MCEDIT_DRAG_MOVE; 
    14261380            edit_save_size (edit); 
     1381            edit_status (edit, TRUE);   /* redraw frame and status */ 
    14271382            ret = TRUE; 
    14281383            break; 
    14291384        case CK_WindowResize: 
    14301385            edit->drag_state = MCEDIT_DRAG_RESIZE; 
    14311386            edit_save_size (edit); 
     1387            edit_status (edit, TRUE);   /* redraw frame and status */ 
    14321388            ret = TRUE; 
    14331389            break; 
    14341390        default: 
    edit_handle_move_resize (WEdit * edit, long command) 
    14531409        case CK_Enter: 
    14541410        case CK_WindowMove: 
    14551411            edit->drag_state = MCEDIT_DRAG_NORMAL; 
    1456             /* redraw frame and status */ 
    1457             edit_status (edit, TRUE); 
     1412            edit_status (edit, TRUE);   /* redraw frame and status */ 
    14581413        default: 
    14591414            ret = TRUE; 
    14601415            break; 
    edit_handle_move_resize (WEdit * edit, long command) 
    14781433        case CK_Enter: 
    14791434        case CK_WindowResize: 
    14801435            edit->drag_state = MCEDIT_DRAG_NORMAL; 
    1481             /* redraw frame and status */ 
    1482             edit_status (edit, TRUE); 
     1436            edit_status (edit, TRUE);   /* redraw frame and status */ 
    14831437        default: 
    14841438            ret = TRUE; 
    14851439            break; 
    edit_handle_move_resize (WEdit * edit, long command) 
    14901444        break; 
    14911445    } 
    14921446 
    1493     /* allow stop resize/move by mouse click outside window */ 
    1494     w->mouse.forced_capture = ret; 
    1495  
    14961447    return ret; 
    14971448} 
    14981449