]> Pileus Git - lackey/commitdiff
Add mouse clicks to form
authorAndy Spencer <andy753421@gmail.com>
Wed, 21 Jan 2015 01:49:46 +0000 (01:49 +0000)
committerAndy Spencer <andy753421@gmail.com>
Mon, 5 Dec 2016 08:28:53 +0000 (08:28 +0000)
src/form.c

index 0bec4051263ba731cd45e177529aba6cb8e619d9..9a05747970dc79f394674a004bd74cfd84c79eb8 100644 (file)
@@ -84,6 +84,28 @@ int field_width(form_field_t *field)
        return width;
 }
 
+void field_sizes(form_t *form, int *col_size)
+{
+       // Do this by column, and right to left so that we can add
+       // in the extra space available for blank spaces.
+       for (int c = form->cols-1; c >= 0; c--) {
+               col_size[c] = 0;
+               for (int r = 0; r < form->rows; r++) {
+                       form_field_t *field = form->fields[r][c];
+                       if (form->fields[r][c]) {
+                               int width = field_width(field);
+                               for (int i = c+1; i < form->cols; i++) {
+                                       if (form->fields[r][i])
+                                               break;
+                                       width -= col_size[i];
+                               }
+                               if (width > col_size[c])
+                                       col_size[c] = width;
+                       }
+               }
+       }
+}
+
 void field_draw(WINDOW *win, form_field_t *field, int width, int hover)
 {
        char **map   = field->list.map;
@@ -225,27 +247,10 @@ void form_resize(void)
 /* Run */
 int form_draw(form_t *form)
 {
-       // Calculate column width
-       //    do this by column, and right to left so that
-       //    we can add in the extra space available for
-       //    blank spaces.
        int col_size[form->cols];
-       for (int c = form->cols-1; c >= 0; c--) {
-               col_size[c] = 0;
-               for (int r = 0; r < form->rows; r++) {
-                       form_field_t *field = form->fields[r][c];
-                       if (form->fields[r][c]) {
-                               int width = field_width(field);
-                               for (int i = c+1; i < form->cols; i++) {
-                                       if (form->fields[r][i])
-                                               break;
-                                       width -= col_size[i];
-                               }
-                               if (width > col_size[c])
-                                       col_size[c] = width;
-                       }
-               }
-       }
+
+       // Calculate column width
+       field_sizes(form, col_size);
 
        // Make sure we have an active field
        if (!can_active(form, form_row, form_col))
@@ -284,6 +289,22 @@ int form_run(form_t *form, int key, mmask_t btn, int row, int col)
                case 'l': set_active(form,  0,  1); goto redraw;
        }
 
+       // Handle mouse movement
+       if (key == KEY_MOUSE && row < form->rows) {
+               int pos=0, col_size[form->cols];
+               field_sizes(form, col_size);
+               for (int c = 0; c < form->cols; c++) {
+                       if (pos < col && col < pos+col_size[c]) {
+                               if (can_active(form, row, c)) {
+                                       form_row = row;
+                                       form_col = c;
+                                       goto redraw;
+                               }
+                       }
+                       pos += col_size[c];
+               }
+       }
+
        // Search for hotkeys
        for (int r = 0; r < form->rows; r++)
        for (int c = 0; c < form->cols; c++)