void column_select_update_columns(ui_t *ui) { int column, attr_id; // Get panel information column_select_info_t *info = column_select_info(ui); // Set enabled fields ui_t *ui_list = ui_find_by_type(PANEL_CALL_LIST); call_list_info_t *list_info = call_list_info(ui_list); // Reset column count list_info->columncnt = 0; // Add all selected columns for (column = 0; column < item_count(info->menu); column++) { // If column is active if (!strncmp(item_name(info->items[column]), "[ ]", 3)) continue; // Get column attribute attr_id = sip_attr_from_name(item_userptr(info->items[column])); // Add a new column to the list call_list_add_column(ui_list, attr_id, sip_attr_get_name(attr_id), sip_attr_get_title(attr_id), sip_attr_get_width(attr_id)); } }
int column_select_handle_key(ui_t *ui, int key) { // Get panel information column_select_info_t *info = column_select_info(ui); if (info->form_active) { return column_select_handle_key_form(ui, key); } else { return column_select_handle_key_menu(ui, key); } return 0; }
int column_select_handle_key(PANEL *panel, int key) { // Get panel information column_select_info_t *info = column_select_info(panel); if (info->form_active) { return column_select_handle_key_form(panel, key); } else { return column_select_handle_key_menu(panel, key); } return 0; }
void column_select_move_item(ui_t *ui, ITEM *item, int pos) { // Get panel information column_select_info_t *info = column_select_info(ui); // Check we have a valid position if (pos == item_count(info->menu) || pos < 0) return; // Swap position with destination int item_pos = item_index(item); info->items[item_pos] = info->items[pos]; info->items[item_pos]->index = item_pos; info->items[pos] = item; info->items[pos]->index = pos; }
void column_select_update_menu(ui_t *ui) { // Get panel information column_select_info_t *info = column_select_info(ui); ITEM *current = current_item(info->menu); int top_idx = top_row(info->menu); // Remove the menu from the subwindow unpost_menu(info->menu); // Set menu items set_menu_items(info->menu, info->items); // Put the menu agin into its subwindow post_menu(info->menu); // Move until the current position is set set_top_row(info->menu, top_idx); set_current_item(info->menu, current); }
void column_select_destroy(ui_t *ui) { int i; column_select_info_t *info = column_select_info(ui); // Remove menu and items unpost_menu(info->menu); free_menu(info->menu); for (i = 0; i < SIP_ATTR_COUNT; i++) free_item(info->items[i]); // Remove form and fields unpost_form(info->form); free_form(info->form); for (i = 0; i < FLD_COLUMNS_COUNT; i++) free_field(info->fields[i]); sng_free(info); // Remove panel window and custom info ui_panel_destroy(ui); }
void column_select_save_columns(ui_t *ui) { int column; FILE *fi, *fo; char columnopt[128]; char line[1024]; char *home = getenv("HOME"); char userconf[128], tmpfile[128]; // No home dir... if (!home) return; // Read current $HOME/.sngreprc file sprintf(userconf, "%s/.sngreprc", home); sprintf(tmpfile, "%s/.sngreprc.old", home); // Remove old config file unlink(tmpfile); // Move home file to temporal dir rename(userconf, tmpfile); // Create a new user conf file if (!(fo = fopen(userconf, "w"))) { dialog_run("Unable to open %s: %s", userconf, strerror(errno)); return; } // Read all lines of old sngreprc file if ((fi = fopen(tmpfile, "r"))) { // Read all configuration file while (fgets(line, 1024, fi) != NULL) { // Ignore lines starting with set (but keep settings) if (strncmp(line, "set ", 4) || strncmp(line, "set cl.column", 13)) { // Put everyting in new .sngreprc file fputs(line, fo); } } fclose(fi); } // Get panel information column_select_info_t *info = column_select_info(ui); // Add all selected columns for (column = 0; column < item_count(info->menu); column++) { // If column is active if (!strncmp(item_name(info->items[column]), "[ ]", 3)) continue; // Add the columns settings sprintf(columnopt, "set cl.column%d %s\n", column, (const char*) item_userptr(info->items[column])); fputs(columnopt, fo); } fclose(fo); // Show a information dialog dialog_run("Column layout successfully saved to %s", userconf); }
int column_select_handle_key_form(ui_t *ui, int key) { int field_idx, new_field_idx; char field_value[48]; int action = -1; // Get panel information column_select_info_t *info = column_select_info(ui); // Get current field id field_idx = field_index(current_field(info->form)); // Get current field value. memset(field_value, 0, sizeof(field_value)); strcpy(field_value, field_buffer(current_field(info->form), 0)); strtrim(field_value); // Check actions for this key while ((action = key_find_action(key, action)) != ERR) { // Check if we handle this action switch (action) { case ACTION_RIGHT: case ACTION_NEXT_FIELD: form_driver(info->form, REQ_NEXT_FIELD); break; case ACTION_LEFT: case ACTION_PREV_FIELD: form_driver(info->form, REQ_PREV_FIELD); break; case ACTION_SELECT: case ACTION_CONFIRM: switch(field_idx) { case FLD_COLUMNS_ACCEPT: column_select_update_columns(ui); ui_destroy(ui); return KEY_HANDLED; case FLD_COLUMNS_CANCEL: ui_destroy(ui); return KEY_HANDLED; case FLD_COLUMNS_SAVE: column_select_update_columns(ui); column_select_save_columns(ui); ui_destroy(ui); return KEY_HANDLED; } break; default: // Parse next action continue; } // This panel has handled the key successfully break; } // Validate all input data form_driver(info->form, REQ_VALIDATION); // Change background and cursor of "button fields" set_field_back(info->fields[FLD_COLUMNS_ACCEPT], A_NORMAL); set_field_back(info->fields[FLD_COLUMNS_SAVE], A_NORMAL); set_field_back(info->fields[FLD_COLUMNS_CANCEL], A_NORMAL); // Get current selected field new_field_idx = field_index(current_field(info->form)); // Swap between menu and form if (field_idx == FLD_COLUMNS_CANCEL && new_field_idx == FLD_COLUMNS_ACCEPT) { set_menu_fore(info->menu, COLOR_PAIR(CP_DEF_ON_BLUE)); info->form_active = 0; } else { // Change current field background set_field_back(info->fields[new_field_idx], A_REVERSE); } // Return if this panel has handled or not the key return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; }
int column_select_handle_key_menu(ui_t *ui, int key) { MENU *menu; ITEM *current; int current_idx; int action = -1; // Get panel information column_select_info_t *info = column_select_info(ui); menu = info->menu; current = current_item(menu); current_idx = item_index(current); // Check actions for this key while ((action = key_find_action(key, action)) != ERR) { // Check if we handle this action switch (action) { case ACTION_DOWN: menu_driver(menu, REQ_DOWN_ITEM); break; case ACTION_UP: menu_driver(menu, REQ_UP_ITEM); break; case ACTION_NPAGE: menu_driver(menu, REQ_SCR_DPAGE); break; case ACTION_PPAGE: menu_driver(menu, REQ_SCR_UPAGE); break; case ACTION_SELECT: column_select_toggle_item(ui, current); column_select_update_menu(ui); break; case ACTION_COLUMN_MOVE_DOWN: column_select_move_item(ui, current, current_idx + 1); column_select_update_menu(ui); break; case ACTION_COLUMN_MOVE_UP: column_select_move_item(ui, current, current_idx - 1); column_select_update_menu(ui); break; case ACTION_NEXT_FIELD: info->form_active = 1; set_menu_fore(menu, COLOR_PAIR(CP_DEFAULT)); set_field_back(info->fields[FLD_COLUMNS_ACCEPT], A_REVERSE); form_driver(info->form, REQ_VALIDATION); break; case ACTION_CONFIRM: column_select_update_columns(ui); ui_destroy(ui); return KEY_HANDLED; default: // Parse next action continue; } // This panel has handled the key successfully break; } // Draw a scrollbar to the right info->scroll.pos = top_row(menu); ui_scrollbar_draw(info->scroll); wnoutrefresh(info->menu_win); // Return if this panel has handled or not the key return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; }