Commit 6eb8d84f authored by Youness Alaoui's avatar Youness Alaoui
Browse files

WIP: Add --gauge support

Only GTK mode works with a moving target (by pressing any key), and
the console mode will just show the initial percentage and exit when
Enter key is pressed. Handling of stdin values is still to be added.
parent 896672cc
......@@ -443,7 +443,7 @@ cairo_menu_add_item_full (CairoMenu *menu, cairo_surface_t *image,
item->index = menu->nitems - 1;
item->image = NULL;
item->image_position = image_position;
item->text = strdup (text);
item->text = text ? strdup (text) : NULL;
item->text_size = text_size;
item->text_color = text_color;
item->alignment = alignment;
......
......@@ -83,7 +83,12 @@ static gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
} else
return TRUE;
cairo_menu_handle_input (menu->menu, input, &bbox);
if (menu->gauge) {
static int gauge_value = 0;
standard_menu_update_gauge (menu, gauge_value++);
} else {
cairo_menu_handle_input (menu->menu, input, &bbox);
}
gtk_widget_queue_draw_area (widget, 0, 0, menu->width, menu->height);
return TRUE;
}
......@@ -201,7 +206,6 @@ void print_usage (int exit_code)
printf ("\t--radiolist <text> <height> <width> <listheight> [tag item status]...\n");
printf ("\t\tThis option is not supported\n");
printf ("\t--gauge <text> <height> <width> <percent>\n");
printf ("\t\tThis option is not supported\n");
printf ("Options: (depend on box-option)\n");
printf ("\t--clear\t\t\t\tclear screen on exit\n");
printf ("\t--defaultno\t\t\tdefault no button\n");
......@@ -289,6 +293,21 @@ int parse_whiptail_args (int argc, char **argv, whiptail_args *args)
args->topleft = 1;
} else if (strcmp (argv[i], "--clear") == 0) {
args->clear = 1;
} else if (strcmp (argv[i], "--gauge") == 0) {
if (args->mode != MODE_NONE)
goto mode_already_set;
if (i + 4 >= argc)
goto missing_value;
args->text = argv[i+1];
args->height = atoi (argv[i+2]);
args->width = atoi (argv[i+3]);
args->gauge_percent = atoi (argv[i+4]);
if (args->gauge_percent < 0)
args->gauge_percent = 0;
if (args->gauge_percent > 100)
args->gauge_percent = 100;
i += 4;
args->mode = MODE_GAUGE;
} else if (strcmp (argv[i], "--menu") == 0) {
if (args->mode != MODE_NONE)
goto mode_already_set;
......@@ -595,6 +614,12 @@ int main(int argc, char **argv)
menu = standard_menu_create (args.title, args.text, args.text_size,
xres, yres, -1, 1);
standard_menu_add_item (menu, args.ok_button, 20);
} else if (args.mode == MODE_GAUGE) {
menu = standard_menu_create (args.title, args.text, args.text_size,
xres, yres, -1, 1);
menu->gauge = 1;
standard_menu_add_item (menu, "Gauge", 20);
standard_menu_update_gauge (menu, args.gauge_percent);
}
if (args.background_png)
menu->background = load_image_and_scale (args.background_png, xres, yres);
......
......@@ -247,8 +247,6 @@ draw_standard_menu (Menu *menu, cairo_t *cr)
(menu->height - h) / 2 + STANDARD_MENU_FRAME_TOP);
cairo_paint (cr);
/* Draw a frame around the menu so cut off buttons don't appear clippped */
menu_width = STANDARD_MENU_WIDTH;
if (menu->menu->rows == 1)
menu_height = STANDARD_MENU_ITEM_TOTAL_HEIGHT;
......@@ -261,9 +259,13 @@ draw_standard_menu (Menu *menu, cairo_t *cr)
cairo_save (cr);
cairo_translate (cr, ((menu->width - w) / 2) + STANDARD_MENU_FRAME_SIDE,
((menu->height - h) / 2) + STANDARD_MENU_FRAME_TOP + text_height);
cairo_utils_clip_round_edge (cr, menu_width, menu_height, 20, 20, 20);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_paint_with_alpha (cr, 0.5);
if (!menu->gauge) {
/* Draw a frame around the menu so cut off buttons don't appear clippped */
cairo_utils_clip_round_edge (cr, menu_width, menu_height, 20, 20, 20);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_paint_with_alpha (cr, 0.5);
}
cairo_set_source_surface (cr, surface, 0, 0);
cairo_paint (cr);
......@@ -479,3 +481,87 @@ free_text (Menu *menu)
if (menu->text.lines != NULL)
free (menu->text.lines);
}
cairo_surface_t *
create_standard_gauge (int width, int height, unsigned int percent,
float dr, float dg, float db, float r, float g, float b) {
cairo_surface_t *dbg;
cairo_surface_t *bg;
int done_width;
cairo_surface_t *background;
cairo_t *cr;
cairo_pattern_t *linpat = NULL;
background = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
if (percent > 100)
percent = 100;
done_width = (width - (2 * STANDARD_MENU_BOX_X)) * percent / 100;
dbg = cairo_menu_create_default_background (done_width,
height - (2 * STANDARD_MENU_BOX_Y), dr, dg, db);
bg = cairo_menu_create_default_background (width - (2 * STANDARD_MENU_BOX_X) - done_width,
height - (2 * STANDARD_MENU_BOX_Y), r, g, b);
// Draw the grey/silver gradient (for the border)
cr = cairo_create (background);
cairo_utils_clip_round_edge (cr, width, height,
STANDARD_MENU_BOX_CORNER_RADIUS, STANDARD_MENU_BOX_CORNER_RADIUS,
STANDARD_MENU_BOX_CORNER_RADIUS);
linpat = cairo_pattern_create_linear (width, 0, width, height);
cairo_pattern_add_color_stop_rgb (linpat, 0.0, 0.3, 0.3, 0.3);
cairo_pattern_add_color_stop_rgb (linpat, 1.0, 0.7, 0.7, 0.7);
cairo_set_source (cr, linpat);
cairo_paint_with_alpha (cr, 0.5);
// Clear the inside of the gradient in order to create a border
cairo_utils_clip_round_edge (cr, width, height,
STANDARD_MENU_BOX_CORNER_RADIUS + STANDARD_MENU_BOX_BORDER_WIDTH,
STANDARD_MENU_BOX_CORNER_RADIUS + STANDARD_MENU_BOX_BORDER_WIDTH,
STANDARD_MENU_BOX_CORNER_RADIUS);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
cairo_paint (cr);
// Add a 40% alpha black filter inside, creating a frame around the button
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_set_source_rgb (cr, 0, 0, 0);
cairo_paint_with_alpha (cr, 0.4);
// Draw the actual button
cairo_set_source_surface (cr, dbg, STANDARD_MENU_BOX_X, STANDARD_MENU_BOX_Y);
cairo_paint (cr);
cairo_set_source_surface (cr, bg, STANDARD_MENU_BOX_X + done_width, STANDARD_MENU_BOX_Y);
cairo_paint (cr);
cairo_destroy (cr);
cairo_pattern_destroy (linpat);
cairo_surface_destroy (bg);
return background;
}
void standard_menu_update_gauge (Menu *menu, unsigned int percent) {
CairoMenuItem *item = &menu->menu->items[0];
char percent_text[6];
cairo_surface_t *gauge;
if (percent > 100)
percent = 100;
gauge = create_standard_gauge (STANDARD_MENU_ITEM_BOX_WIDTH,
STANDARD_MENU_ITEM_BOX_HEIGHT, percent, 0.4, 0.2, 0.2, 0, 0, 0);
snprintf (percent_text, sizeof(percent_text), "%d%%", percent);
//item->enabled = FALSE;
if (item->text)
free (item->text);
item->text = strdup (percent_text);
cairo_surface_destroy (item->bg_image);
cairo_surface_destroy (item->bg_sel_image);
item->bg_image = cairo_surface_reference (gauge);
item->bg_sel_image = cairo_surface_reference (gauge);
cairo_surface_destroy (gauge);
}
......@@ -37,6 +37,7 @@ typedef struct {
struct Menu_s {
cairo_surface_t *background;
CairoMenu *menu;
int gauge;
int width;
int height;
const char *title;
......@@ -57,6 +58,7 @@ typedef enum {
MODE_MENU,
MODE_YESNO,
MODE_MSGBOX,
MODE_GAUGE,
} whiptail_mode;
typedef struct {
......@@ -77,6 +79,7 @@ typedef struct {
int width;
int height;
int menu_height;
int gauge_percent;
whiptail_mode mode;
whiptail_menu_item *items;
int num_items;
......@@ -139,5 +142,8 @@ Menu *standard_menu_create (const char *title, char * text, int text_size,
int width, int height, int rows, int columns);
int standard_menu_add_item (Menu *menu, const char *title, int fontsize);
int standard_menu_add_tag (Menu *menu, const char *title, int fontsize);
void standard_menu_update_gauge (Menu *menu, unsigned int percent);
cairo_surface_t * create_standard_gauge (int width, int height, unsigned int percent,
float dr, float dg, float db, float r, float g, float b);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment