Ticket #397: mouse-sort.patch

File mouse-sort.patch, 2.8 KB (added by bnh, 13 years ago)

Patch to screen.c to allow sorting on panel columns via mouse click

  • src/screen.c

    diff --git a/src/screen.c b/src/screen.c
    index fd7a043..a375913 100644
    a b static struct { 
    427427    const char *title; 
    428428    int  use_in_gui; 
    429429    const char *(*string_fn)(file_entry *, int); 
    430     sortfn *sort_routine; /* This field is currently unused. */ 
     430    sortfn *sort_routine; /* used by mouse_sort_col() */ 
    431431} formats [] = { 
    432432{ "name",  12, 1, J_LEFT_FIT,   N_("Name"),     1, string_file_name,       (sortfn *) sort_name }, 
    433433{ "size",  7,  0, J_RIGHT,      N_("Size"),     1, string_file_size,       (sortfn *) sort_size }, 
    show_dir (WPanel *panel) 
    826826    addstr (">"); 
    827827    widget_move (&panel->widget, 0, panel->widget.cols - 3); 
    828828    addstr ("v"); 
     829    widget_move (&panel->widget, 0, panel->widget.cols - 4); 
     830    addstr ("."); 
    829831 
    830832    if (!show_mini_info) { 
    831833        if (panel->marked == 0) { 
    mark_if_marking (WPanel *panel, Gpm_Event *event) 
    24802482    return 0; 
    24812483} 
    24822484 
     2485/* Determine which column was clicked, and sort the panel on 
     2486 * that column, or reverse sort on that column if already 
     2487 * sorted on that column. 
     2488 */ 
     2489static void 
     2490mouse_sort_col(Gpm_Event *event, WPanel *panel) 
     2491{ 
     2492  int x = event->x; 
     2493  int pos = 0; 
     2494  int found = 0; 
     2495  const char *name; 
     2496  sortfn *col_sort_type = NULL; 
     2497  format_e *format = panel->format; 
     2498  while (format) { 
     2499    pos += format->field_len; 
     2500    if ( x < pos + 1) { 
     2501      /* found column */ 
     2502      name = format->title; 
     2503      found = 1; 
     2504      break; 
     2505    } 
     2506    format = format->next; 
     2507  } 
     2508  if (found) { 
     2509    int i; 
     2510    for (i=0; i < ELEMENTS(formats); i++) { 
     2511      if ( !strcmp(name,formats[i].title) && formats[i].sort_routine ) { 
     2512          col_sort_type = formats[i].sort_routine; 
     2513          break; 
     2514      } 
     2515    } 
     2516    if (col_sort_type) { 
     2517      if (panel->sort_type == col_sort_type) { 
     2518        /* reverse the sort if clicked column is already the sorted column */ 
     2519        panel->reverse = panel->reverse ? 0 : 1; 
     2520      } 
     2521      else { 
     2522        panel->reverse = 0; /* new sort is forced to be ascending */ 
     2523      } 
     2524      panel_set_sort_order(panel, col_sort_type); 
     2525    } 
     2526  } 
     2527} 
     2528 
    24832529/* 
    24842530 * Mouse callback of the panel minus repainting. 
    24852531 * If the event is redirected to the menu, *redir is set to 1. 
    do_panel_event (Gpm_Event *event, WPanel *panel, int *redir) 
    25212567        return MOU_NORMAL; 
    25222568    } 
    25232569 
     2570    /* "." button show/hide hidden files */ 
     2571    if (event->type & GPM_DOWN && event->x == panel->widget.cols - 3 
     2572        && event->y == 1) { 
     2573        toggle_show_hidden(); 
     2574        return MOU_NORMAL; 
     2575    } 
     2576 
     2577    /* sort on clicked column */ 
     2578    if (event->type & GPM_DOWN && event->y == 2) { 
     2579      mouse_sort_col(event,panel); 
     2580    } 
     2581 
    25242582    /* rest of the upper frame, the menu is invisible - call menu */ 
    25252583    if (event->type & GPM_DOWN && event->y == 1 && !menubar_visible) { 
    25262584        *redir = 1;