From f115522e8dea5efe63fc6dfdf540614e596ee2ae Mon Sep 17 00:00:00 2001 From: Kaian Date: Thu, 4 Feb 2016 18:20:22 +0100 Subject: [PATCH] UI: more ui refactoring --- src/curses/theme.h | 60 ++++++++++++++++++++++++++++++++++++ src/curses/ui_call_flow.c | 4 +-- src/curses/ui_call_list.c | 9 +++--- src/curses/ui_manager.c | 64 --------------------------------------- src/curses/ui_manager.h | 58 +---------------------------------- src/curses/ui_msg_diff.c | 4 +-- src/curses/ui_panel.c | 58 +++++++++++++++++++++++++++++++++++ src/curses/ui_panel.h | 33 ++++++++++++++++++++ 8 files changed, 160 insertions(+), 130 deletions(-) create mode 100644 src/curses/theme.h diff --git a/src/curses/theme.h b/src/curses/theme.h new file mode 100644 index 0000000..5c355d4 --- /dev/null +++ b/src/curses/theme.h @@ -0,0 +1,60 @@ +/************************************************************************** + ** + ** sngrep - SIP Messages flow viewer + ** + ** Copyright (C) 2013-2016 Ivan Alonso (Kaian) + ** Copyright (C) 2013-2016 Irontec SL. All rights reserved. + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ****************************************************************************/ +/** + * @file theme.h + * @author Ivan Alonso [aka Kaian] + * + * @brief Theme and Color definitions + * + */ +#ifndef __SNGREP_THEME_H +#define __SNGREP_THEME_H + +/** + * @brief Enum for available color pairs + */ +enum sngrep_colors_pairs { + CP_DEFAULT = 0, + CP_CYAN_ON_DEF, + CP_YELLOW_ON_DEF, + CP_MAGENTA_ON_DEF, + CP_GREEN_ON_DEF, + CP_RED_ON_DEF, + CP_BLUE_ON_DEF, + CP_WHITE_ON_DEF, + CP_DEF_ON_CYAN, + CP_DEF_ON_BLUE, + CP_WHITE_ON_BLUE, + CP_BLACK_ON_CYAN, + CP_WHITE_ON_CYAN, + CP_YELLOW_ON_CYAN, + CP_BLUE_ON_CYAN, + CP_BLUE_ON_WHITE, + CP_CYAN_ON_BLACK, + CP_CYAN_ON_WHITE, +}; + +// Used to configure color pairs only with fg color +#define COLOR_DEFAULT -1 + + +#endif /* __SNGREP_THEME_H */ diff --git a/src/curses/ui_call_flow.c b/src/curses/ui_call_flow.c index 261b5a4..1b4ec70 100644 --- a/src/curses/ui_call_flow.c +++ b/src/curses/ui_call_flow.c @@ -158,7 +158,7 @@ call_flow_draw(ui_t *ui) strcat(title, " (Color by CSeq)"); // Draw panel title - draw_title(ui->panel, title); + ui_set_title(ui, title); // Show some keybinding call_flow_draw_footer(ui); @@ -232,7 +232,7 @@ call_flow_draw_footer(ui_t *ui) key_action_key_str(ACTION_INCREASE_RAW), "Increase Raw" }; - draw_keybindings(ui->panel, keybindings, 22); + ui_draw_bindings(ui, keybindings, 22); // If any dialog has RTP streams and they are not visible if (!setting_enabled(SETTING_CF_MEDIA)) { diff --git a/src/curses/ui_call_list.c b/src/curses/ui_call_list.c index 3050d61..9b4c735 100644 --- a/src/curses/ui_call_list.c +++ b/src/curses/ui_call_list.c @@ -178,10 +178,10 @@ call_list_draw_header(ui_t *ui) call_list_info_t *info = call_list_info(ui); // Draw panel title - draw_title(ui->panel, "sngrep - SIP messages flow viewer"); + ui_set_title(ui, "sngrep - SIP messages flow viewer"); // Draw a Panel header lines - clear_line(ui->win, 1); + ui_clear_line(ui, 1); if ((infile = capture_input_file())) mvwprintw(ui->win, 1, ui->width - strlen(infile) - 11, "Filename: %s", infile); mvwprintw(ui->win, 2, 2, "Display Filter: "); @@ -241,7 +241,7 @@ call_list_draw_footer(ui_t *ui) key_action_key_str(ACTION_SHOW_COLUMNS), "Columns" }; - draw_keybindings(ui->panel, keybindings, 22); + ui_draw_bindings(ui, keybindings, 22); } void @@ -304,7 +304,7 @@ call_list_draw_list(ui_t *ui) wattron(list_win, A_REVERSE); } // Set current line background - clear_line(list_win, cline); + mvwprintw(list_win, cline, 0, "%*s", listw, ""); // Set current line selection box mvwprintw(list_win, cline, 2, call_group_exists(info->group, call) ? "[*]" : "[ ]"); @@ -351,7 +351,6 @@ call_list_draw_list(ui_t *ui) // Draw scrollbar to the right draw_vscrollbar(list_win, info->first_line, info->dispcallcnt, 1); wnoutrefresh(info->list_win); - } int diff --git a/src/curses/ui_manager.c b/src/curses/ui_manager.c index 1a541ce..01af60f 100644 --- a/src/curses/ui_manager.c +++ b/src/curses/ui_manager.c @@ -314,61 +314,6 @@ title_foot_box(PANEL *panel) } -void -draw_keybindings(PANEL *panel, const char *keybindings[], int count) -{ - int height, width, key, xpos = 0; - - // Get window available space - WINDOW *win = panel_window(panel); - getmaxyx(win, height, width); - - // Reverse colors on monochrome terminals - if (!has_colors()) - wattron(win, A_REVERSE); - - // Write a line all the footer width - wattron(win, COLOR_PAIR(CP_DEF_ON_CYAN)); - clear_line(win, height - 1); - - // Draw keys and their actions - for (key = 0; key < count; key += 2) { - wattron(win, A_BOLD | COLOR_PAIR(CP_WHITE_ON_CYAN)); - mvwprintw(win, height - 1, xpos, "%-*s", strlen(keybindings[key]) + 1, keybindings[key]); - xpos += strlen(keybindings[key]) + 1; - wattroff(win, A_BOLD | COLOR_PAIR(CP_WHITE_ON_CYAN)); - wattron(win, COLOR_PAIR(CP_BLACK_ON_CYAN)); - mvwprintw(win, height - 1, xpos, "%-*s", strlen(keybindings[key + 1]) + 1, - keybindings[key + 1]); - wattroff(win, COLOR_PAIR(CP_BLACK_ON_CYAN)); - xpos += strlen(keybindings[key + 1]) + 3; - } - - // Disable reverse mode in all cases - wattroff(win, A_REVERSE); -} - -void -draw_title(PANEL *panel, const char *title) -{ - int height, width; - - // Get window available space - WINDOW *win = panel_window(panel); - getmaxyx(win, height, width); - - // Reverse colors on monochrome terminals - if (!has_colors()) - wattron(win, A_REVERSE); - - // Center the title on the window - wattron(win, A_BOLD | COLOR_PAIR(CP_DEF_ON_CYAN)); - clear_line(win, 0); - mvwprintw(win, 0, (width - strlen(title)) / 2, "%s", title); - wattroff(win, A_BOLD | A_REVERSE | COLOR_PAIR(CP_DEF_ON_CYAN)); - -} - void draw_vscrollbar(WINDOW *win, int value, int max, int left) { @@ -400,15 +345,6 @@ draw_vscrollbar(WINDOW *win, int value, int max, int left) } -void -clear_line(WINDOW *win, int line) -{ - // We could do this with wcleartoel but we want to - // preserve previous window attributes. That way we - // can set the background of the line. - mvwprintw(win, line, 0, "%*s", getmaxx(win), ""); -} - int draw_message(WINDOW *win, sip_msg_t *msg) { diff --git a/src/curses/ui_manager.h b/src/curses/ui_manager.h index 6d5e14a..831234e 100644 --- a/src/curses/ui_manager.h +++ b/src/curses/ui_manager.h @@ -36,6 +36,7 @@ #ifndef __SNGREP_UI_MANAGER_H #define __SNGREP_UI_MANAGER_H #include "config.h" +#include "theme.h" #include "ui_panel.h" #include "sip.h" #include "group.h" @@ -48,32 +49,6 @@ #define DIALOG_MAX_WIDTH 100 #define DIALOG_MIN_WIDTH 40 -/** - * @brief Enum for available color pairs - */ -enum sngrep_colors_pairs { - CP_DEFAULT = 0, - CP_CYAN_ON_DEF, - CP_YELLOW_ON_DEF, - CP_MAGENTA_ON_DEF, - CP_GREEN_ON_DEF, - CP_RED_ON_DEF, - CP_BLUE_ON_DEF, - CP_WHITE_ON_DEF, - CP_DEF_ON_CYAN, - CP_DEF_ON_BLUE, - CP_WHITE_ON_BLUE, - CP_BLACK_ON_CYAN, - CP_WHITE_ON_CYAN, - CP_YELLOW_ON_CYAN, - CP_BLUE_ON_CYAN, - CP_BLUE_ON_WHITE, - CP_CYAN_ON_BLACK, - CP_CYAN_ON_WHITE, -}; - -// Used to configure color pairs only with fg color -#define COLOR_DEFAULT -1 /** * Define existing panels @@ -166,28 +141,6 @@ default_handle_key(ui_t *ui, int key); void title_foot_box(PANEL *panel); -/** - * @brief Draw title at the top of the panel - * - * This function will draw a line with the title on the first - * row of the panel's window - * - * @param panel PANEL pointer to draw title on - * @param title String containing the title - */ -void -draw_title(PANEL *panel, const char *title); - -/** - * @brief Draw keybinding info at the bottom of the panel - * - * This function will draw a line with the available keybindings - * in the last line of the given panel - * - */ -void -draw_keybindings(PANEL *panel, const char *keybindings[], int count); - /** * @brief Draw a vertical scroll * @@ -197,15 +150,6 @@ draw_keybindings(PANEL *panel, const char *keybindings[], int count); void draw_vscrollbar(WINDOW *win, int value, int max, int left); -/** - * @brief Clear a given window line - * - * This function can be used to clear a given line on the - * screen. - */ -void -clear_line(WINDOW *win, int line); - /** * @brief Draw a message payload in a window * diff --git a/src/curses/ui_msg_diff.c b/src/curses/ui_msg_diff.c index 42cf07f..e0daee4 100644 --- a/src/curses/ui_msg_diff.c +++ b/src/curses/ui_msg_diff.c @@ -91,7 +91,7 @@ msg_diff_create(ui_t *ui) mvwvline(ui->win, 0, hwidth, ACS_VLINE, ui->height); // Draw title - draw_title(ui->panel, "sngrep - SIP messages flow viewer"); + ui_set_title(ui, "sngrep - SIP messages flow viewer"); // Draw keybindings msg_diff_draw_footer(ui); @@ -148,7 +148,7 @@ msg_diff_draw_footer(ui_t *ui) key_action_key_str(ACTION_SHOW_HELP), "Help" }; - draw_keybindings(ui->panel, keybindings, 4); + ui_draw_bindings(ui, keybindings, 4); } int diff --git a/src/curses/ui_panel.c b/src/curses/ui_panel.c index 723df60..18f7dde 100644 --- a/src/curses/ui_panel.c +++ b/src/curses/ui_panel.c @@ -28,6 +28,8 @@ #include "config.h" #include "ui_panel.h" +#include +#include "theme.h" ui_t * ui_create(ui_t *ui) @@ -149,3 +151,59 @@ ui_panel_destroy(ui_t *ui) // Deallocate panel pointer del_panel(ui->panel); } + +void +ui_set_title(ui_t *ui, const char *title) +{ + // FIXME Reverse colors on monochrome terminals + if (!has_colors()) { + wattron(ui->win, A_REVERSE); + } + + // Center the title on the window + wattron(ui->win, A_BOLD | COLOR_PAIR(CP_DEF_ON_CYAN)); + ui_clear_line(ui, 0); + mvwprintw(ui->win, 0, (ui->width - strlen(title)) / 2, "%s", title); + wattroff(ui->win, A_BOLD | A_REVERSE | COLOR_PAIR(CP_DEF_ON_CYAN)); +} + +void +ui_clear_line(ui_t *ui, int line) +{ + // We could do this with wcleartoel but we want to + // preserve previous window attributes. That way we + // can set the background of the line. + mvwprintw(ui->win, line, 0, "%*s", ui->width, ""); +} + +void +ui_draw_bindings(ui_t *ui, const char *keybindings[], int count) +{ + int key, xpos = 0; + + // Reverse colors on monochrome terminals + if (!has_colors()) { + wattron(ui->win, A_REVERSE); + } + + // Write a line all the footer width + wattron(ui->win, COLOR_PAIR(CP_DEF_ON_CYAN)); + ui_clear_line(ui, ui->height - 1); + + // Draw keys and their actions + for (key = 0; key < count; key += 2) { + wattron(ui->win, A_BOLD | COLOR_PAIR(CP_WHITE_ON_CYAN)); + mvwprintw(ui->win, ui->height - 1, xpos, "%-*s", + strlen(keybindings[key]) + 1, keybindings[key]); + xpos += strlen(keybindings[key]) + 1; + wattroff(ui->win, A_BOLD | COLOR_PAIR(CP_WHITE_ON_CYAN)); + wattron(ui->win, COLOR_PAIR(CP_BLACK_ON_CYAN)); + mvwprintw(ui->win, ui->height - 1, xpos, "%-*s", + strlen(keybindings[key + 1]) + 1, keybindings[key + 1]); + wattroff(ui->win, COLOR_PAIR(CP_BLACK_ON_CYAN)); + xpos += strlen(keybindings[key + 1]) + 3; + } + + // Disable reverse mode in all cases + wattroff(ui->win, A_REVERSE | A_BOLD); +} diff --git a/src/curses/ui_panel.h b/src/curses/ui_panel.h index 4f5ad90..ba5b92a 100644 --- a/src/curses/ui_panel.h +++ b/src/curses/ui_panel.h @@ -199,5 +199,38 @@ ui_panel_create(ui_t *ui, int height, int width); void ui_panel_destroy(ui_t *ui); +/** + * @brief Draw title at the top of the panel UI + * + * This function will draw a line with the title on the first + * row of the UI panel's window + * + * @param ui UI structure + * @param title String containing the title + */ +void +ui_set_title(ui_t *ui, const char *title); + +/** + * @brief Clear a given window line + * + * This function can be used to clear a given line on the + * screen. + * + * @param ui UI structure + * @param line Number of line to be cleared + */ +void +ui_clear_line(ui_t *ui, int line); + +/** + * @brief Draw keybinding info at the bottom of the panel + * + * This function will draw a line with the available keybindings + * in the last line of the given panel + * + */ +void +ui_draw_bindings(ui_t *ui, const char *keybindings[], int count); #endif /* __SNGREP_UI_PANEL_H */