static void CheckValidSelection(txt_table_t *table) { int rows; int new_x, new_y; rows = TableRows(table); for (new_y = table->selected_y; new_y < rows; ++new_y) { new_x = FindSelectableColumn(table, new_y, table->selected_x); if (new_x >= 0) { // Found a selectable column. ChangeSelection(table, new_x, new_y); break; } } }
int TXT_PageTable(TXT_UNCAST_ARG(table), int pagex, int pagey) { TXT_CAST_ARG(txt_table_t, table); unsigned int *column_widths; unsigned int *row_heights; int rows; int changed = 0; rows = TableRows(table); row_heights = malloc(sizeof(int) * rows); column_widths = malloc(sizeof(int) * table->columns); CalcRowColSizes(table, row_heights, column_widths); if (pagex) { // @todo Jump selection to the left or right as needed } if (pagey) { int new_x, new_y; int distance = 0; int dir; // What direction are we moving? if (pagey > 0) { dir = 1; } else { dir = -1; } // Move the cursor until the desired distance is reached. new_y = table->selected_y; while (new_y >= 0 && new_y < rows) { // We are about to travel a distance equal to the height of the row // we are about to leave. distance += row_heights[new_y]; // *Now* increment the loop. new_y += dir; new_x = FindSelectableColumn(table, new_y, table->selected_x); if (new_x >= 0) { // Found a selectable widget in this column! // Select it anyway in case we don't find something better. table->selected_x = new_x; table->selected_y = new_y; changed = 1; // ...but is it far enough away? if (distance >= abs(pagey)) { break; } } } } free(row_heights); free(column_widths); return changed; }
static int TXT_TableKeyPress(TXT_UNCAST_ARG(table), int key) { TXT_CAST_ARG(txt_table_t, table); int selected; int rows; rows = TableRows(table); // Send to the currently selected widget first selected = table->selected_y * table->columns + table->selected_x; if (selected >= 0 && selected < table->num_widgets) { if (table->widgets[selected] != NULL && table->widgets[selected]->selectable && TXT_WidgetKeyPress(table->widgets[selected], key)) { return 1; } } if (key == KEY_DOWNARROW) { int new_x, new_y; // Move cursor down to the next selectable widget for (new_y = table->selected_y + 1; new_y < rows; ++new_y) { new_x = FindSelectableColumn(table, new_y, table->selected_x); if (new_x >= 0) { // Found a selectable widget in this column! table->selected_x = new_x; table->selected_y = new_y; return 1; } } } if (key == KEY_UPARROW) { int new_x, new_y; // Move cursor up to the next selectable widget for (new_y = table->selected_y - 1; new_y >= 0; --new_y) { new_x = FindSelectableColumn(table, new_y, table->selected_x); if (new_x >= 0) { // Found a selectable widget in this column! table->selected_x = new_x; table->selected_y = new_y; return 1; } } } if (key == KEY_LEFTARROW) { int new_x; // Move cursor left for (new_x = table->selected_x - 1; new_x >= 0; --new_x) { if (SelectableWidget(table, new_x, table->selected_y)) { // Found a selectable widget! table->selected_x = new_x; return 1; } } } if (key == KEY_RIGHTARROW) { int new_x; // Move cursor left for (new_x = table->selected_x + 1; new_x < table->columns; ++new_x) { if (SelectableWidget(table, new_x, table->selected_y)) { // Found a selectable widget! table->selected_x = new_x; return 1; } } } return 0; }
static int TXT_TableKeyPress(TXT_UNCAST_ARG(table), int key) { TXT_CAST_ARG(txt_table_t, table); int selected; int rows; rows = TableRows(table); // Send to the currently selected widget first selected = table->selected_y * table->columns + table->selected_x; if (selected >= 0 && selected < table->num_widgets) { if (IsActualWidget(table->widgets[selected]) && TXT_SelectableWidget(table->widgets[selected]) && TXT_WidgetKeyPress(table->widgets[selected], key)) { return 1; } } if (key == KEY_TAB) { int dir; int i; dir = TXT_GetModifierState(TXT_MOD_SHIFT) ? -1 : 1; // Cycle through all widgets until we find one that can be selected. for (i = table->selected_y * table->columns + table->selected_x + dir; i >= 0 && i < table->num_widgets; i += dir) { if (IsActualWidget(table->widgets[i]) && TXT_SelectableWidget(table->widgets[i])) { ChangeSelection(table, i % table->columns, i / table->columns); return 1; } } return 0; } if (key == KEY_DOWNARROW) { int new_x, new_y; // Move cursor down to the next selectable widget for (new_y = table->selected_y + 1; new_y < rows; ++new_y) { new_x = FindSelectableColumn(table, new_y, table->selected_x); if (new_x >= 0) { // Found a selectable widget in this column! ChangeSelection(table, new_x, new_y); return 1; } } } if (key == KEY_UPARROW) { int new_x, new_y; // Move cursor up to the next selectable widget for (new_y = table->selected_y - 1; new_y >= 0; --new_y) { new_x = FindSelectableColumn(table, new_y, table->selected_x); if (new_x >= 0) { // Found a selectable widget in this column! ChangeSelection(table, new_x, new_y); return 1; } } } if (key == KEY_LEFTARROW) { int new_x; // Move cursor left for (new_x = table->selected_x - 1; new_x >= 0; --new_x) { if (SelectableCell(table, new_x, table->selected_y)) { // Found a selectable widget! ChangeSelection(table, new_x, table->selected_y); return 1; } } } if (key == KEY_RIGHTARROW) { int new_x; // Move cursor left for (new_x = table->selected_x + 1; new_x < table->columns; ++new_x) { if (SelectableCell(table, new_x, table->selected_y)) { // Found a selectable widget! ChangeSelection(table, new_x, table->selected_y); return 1; } } } return 0; }