Separator: simplify

This commit is contained in:
o9000 2016-10-02 13:51:10 +02:00
parent 62e0ee6a3a
commit d49ecee3a7
6 changed files with 618 additions and 601 deletions

View file

@ -554,7 +554,25 @@ void add_entry(char *key, char *value)
separator->color.alpha = 0.5;
} else if (strcmp(key, "separator_style") == 0) {
Separator *separator = get_or_create_last_separator();
separator->style = atoi(value);
if (g_str_equal(value, "empty"))
separator->style = SEPARATOR_EMPTY;
else if (g_str_equal(value, "line"))
separator->style = SEPARATOR_LINE;
else if (g_str_equal(value, "dots"))
separator->style = SEPARATOR_DOTS;
else
fprintf(stderr, RED "Invalid separator_style value: %s" RESET "\n", value);
} else if (strcmp(key, "separator_size") == 0) {
Separator *separator = get_or_create_last_separator();
separator->thickness = atoi(value);
} else if (strcmp(key, "separator_padding") == 0) {
Separator *separator = get_or_create_last_separator();
extract_values(value, &value1, &value2, &value3);
separator->area.paddingxlr = separator->area.paddingx = atoi(value1);
if (value2)
separator->area.paddingy = atoi(value2);
if (value3)
separator->area.paddingx = atoi(value3);
}
/* Execp */

View file

@ -18,6 +18,13 @@
Separator *create_separator()
{
Separator *separator = (Separator *)calloc(1, sizeof(Separator));
separator->color.rgb[0] = 0.5;
separator->color.rgb[1] = 0.5;
separator->color.rgb[2] = 0.5;
separator->color.alpha = 0.9;
separator->style = SEPARATOR_DOTS;
separator->thickness = 3;
separator->area.paddingxlr = 1;
return separator;
}
@ -96,133 +103,104 @@ void cleanup_separator()
gboolean resize_separator(void *obj)
{
Separator *separator = (Separator *)obj;
// Panel *panel = separator->area.panel;
if (!separator->area.on_screen)
return FALSE;
double d_height = panel_horizontal ? separator->area.height : separator->area.width;
double d_thickness = round(d_height / 16) >= 1.0 ? round(d_height / 16) : 1.0;
if (separator->style == 3)
d_thickness = round(d_height / 8) >= 1.0 ? round(d_height / 8) : 1.0;
double d_len = round(d_height / 16) >= 1.0 ? round(d_height / 16) : 1.0;
double d_width = d_thickness * 5;
if (separator->style == 4) {
d_width = d_thickness * 7;
d_thickness = d_thickness * 3;
}
if (separator->style == 2) {
d_width = d_height;
d_thickness = round(d_height / 8) >= 1.0 ? round(d_height / 8) : 1.0;
d_len = d_thickness;
}
double d_empty_thickness = d_thickness;
if (separator->style == 5 || separator->style == 6) {
d_width = (d_thickness * 4) + 2.0;
d_thickness = 1.0;
}
if (panel_horizontal) {
separator->area.width = d_width;
separator->area.height = d_height;
separator->area.width =
separator->thickness + 2 * separator->area.paddingxlr + left_right_border_width(&separator->area);
separator->length =
separator->area.height - 2 * separator->area.paddingy - top_bottom_border_width(&separator->area);
} else {
separator->area.width = d_height;
separator->area.height = d_width;
separator->area.height =
separator->thickness + 2 * separator->area.paddingxlr + top_bottom_border_width(&separator->area);
separator->length =
separator->area.width - 2 * separator->area.paddingy - left_right_border_width(&separator->area);
}
separator->empty_thickness = d_empty_thickness;
separator->thickness = d_thickness;
separator->len = d_len;
schedule_redraw(&separator->area);
panel_refresh = TRUE;
return TRUE;
}
void draw_separator_line(void *obj, cairo_t *c);
void draw_separator_dots(void *obj, cairo_t *c);
void draw_separator(void *obj, cairo_t *c)
{
Separator *separator = (Separator *)obj;
if (separator->style == 0)
if (separator->style == SEPARATOR_EMPTY)
return;
else if (separator->style == SEPARATOR_LINE)
draw_separator_line(separator, c);
else if (separator->style == SEPARATOR_DOTS)
draw_separator_dots(separator, c);
}
void draw_separator_line(void *obj, cairo_t *c)
{
Separator *separator = (Separator *)obj;
if (separator->thickness <= 0)
return;
double start_point = 0 + (separator->thickness * 2);
double end_point = separator->area.height - (separator->thickness * 2);
if (!panel_horizontal)
end_point = separator->area.width - (separator->thickness * 2);
double count = end_point - start_point;
double thickness = separator->thickness;
double len = separator->len;
int alt = 0;
double x_fix = 0;
if (separator->style == 2) {
if (!panel_horizontal)
start_point = start_point + 2;
cairo_set_source_rgba(c,
separator->color.rgb[0],
separator->color.rgb[1],
separator->color.rgb[2],
separator->color.alpha);
cairo_set_line_width(c, 1);
cairo_rectangle(c,
start_point - 2,
start_point - (panel_horizontal ? 0 : 4),
end_point - thickness - 3,
end_point - thickness - (panel_horizontal ? 3 : 3));
cairo_stroke_preserve(c);
cairo_fill(c);
return;
}
if (count < thickness)
return;
while (((int)count) % 2) {
if (alt) {
start_point++;
alt = 0;
} else {
end_point--;
alt = 1;
}
count = end_point - start_point;
if (count < thickness)
return;
}
if (separator->style == 3 || separator->style == 4)
x_fix = round(thickness / 2) + (separator->style == 4 ? 1.0 : 0.0);
if (separator->style == 5 || separator->style == 6) {
x_fix = -1.0;
start_point = start_point + 2;
end_point--;
}
double separator_pattern[] = {len, len};
double separator_style6_pattern[] = {1.0};
cairo_set_source_rgba(c,
separator->color.rgb[0],
separator->color.rgb[1],
separator->color.rgb[2],
separator->color.alpha);
cairo_set_line_width(c, thickness);
if (separator->style == 6)
cairo_set_dash(c, separator_style6_pattern, 1, 0);
else
cairo_set_dash(c, separator_pattern, sizeof(separator_pattern) / sizeof(separator_pattern[0]), 0);
cairo_set_line_width(c, separator->thickness);
cairo_set_line_cap(c, CAIRO_LINE_CAP_ROUND);
if (panel_horizontal) {
cairo_move_to(c, (separator->area.width / 2) - thickness + x_fix, start_point);
cairo_line_to(c, (separator->area.width / 2) - thickness + x_fix, end_point);
cairo_move_to(c, separator->area.width / 2.0, separator->area.height / 2.0 - separator->length / 2.0);
cairo_line_to(c, separator->area.width / 2.0, separator->area.height / 2.0 + separator->length / 2.0);
} else {
cairo_move_to(c, start_point, (separator->area.height / 2) - thickness + x_fix);
cairo_line_to(c, end_point, (separator->area.height / 2) - thickness + x_fix);
cairo_move_to(c, separator->area.width / 2.0 - separator->length / 2.0, separator->area.height / 2.0);
cairo_line_to(c, separator->area.width / 2.0 - separator->length / 2.0, separator->area.height / 2.0);
}
cairo_stroke(c);
}
void draw_separator_dots(void *obj, cairo_t *c)
{
Separator *separator = (Separator *)obj;
if (separator->thickness <= 0)
return;
cairo_set_source_rgba(c,
separator->color.rgb[0],
separator->color.rgb[1],
separator->color.rgb[2],
separator->color.alpha);
cairo_set_line_width(c, 0);
int num_circles = separator->length / (1.618 * separator->thickness - 1);
double spacing = (separator->length - num_circles * separator->thickness) / MAX(1.0, num_circles - 1.0);
if (spacing > separator->thickness)
num_circles++;
spacing = (separator->length - num_circles * separator->thickness) / MAX(1.0, num_circles - 1.0);
double offset = (panel_horizontal ? separator->area.height : separator->area.width) / 2.0 - separator->length / 2.0;
if (num_circles == 1)
offset += spacing / 2.0;
for (int i = 0; i < num_circles; i++) {
if (panel_horizontal) {
cairo_arc(c,
separator->area.width / 2.0,
offset + separator->thickness / 2.0,
separator->thickness / 2.0,
0,
2 * M_PI);
} else {
cairo_arc(c,
offset + separator->thickness / 2.0,
separator->area.height / 2.0,
separator->thickness / 2.0,
0,
2 * M_PI);
}
cairo_stroke_preserve(c);
cairo_fill(c);
offset += separator->thickness + spacing;
}
}

View file

@ -7,13 +7,18 @@
#include "common.h"
#include "area.h"
typedef enum SeparatorStyle {
SEPARATOR_EMPTY = 0,
SEPARATOR_LINE,
SEPARATOR_DOTS
} SeparatorStyle;
typedef struct Separator {
Area area;
int style;
SeparatorStyle style;
Color color;
double empty_thickness;
double thickness;
double len;
int thickness;
int length;
} Separator;
Separator *create_separator();

View file

@ -4486,20 +4486,66 @@ void create_separator(GtkWidget *notebook, int i)
separator->separator_color = gtk_color_button_new();
gtk_color_button_set_use_alpha(GTK_COLOR_BUTTON(separator->separator_color), TRUE);
gtk_widget_show(separator->separator_color);
GdkColor color;
hex2gdk("#777777", &color);
gtk_color_button_set_color(GTK_COLOR_BUTTON(separator->separator_color), &color);
gtk_color_button_set_alpha(GTK_COLOR_BUTTON(separator_get_last()->separator_color), (90*65535)/100);
gtk_table_attach(GTK_TABLE(table), separator->separator_color, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
row++, col = 2;
label = gtk_label_new(_("Separator style"));
label = gtk_label_new(_("Style"));
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
gtk_widget_show(label);
gtk_table_attach(GTK_TABLE(table), label, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
separator->separator_style = gtk_spin_button_new_with_range(0, 6, 1);
separator->separator_style = gtk_combo_box_new_text();
gtk_widget_show(separator->separator_style);
gtk_table_attach(GTK_TABLE(table), separator->separator_style, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
gtk_combo_box_append_text(GTK_COMBO_BOX(separator->separator_style), _("Empty"));
gtk_combo_box_append_text(GTK_COMBO_BOX(separator->separator_style), _("Line"));
gtk_combo_box_append_text(GTK_COMBO_BOX(separator->separator_style), _("Dots"));
gtk_combo_box_set_active(GTK_COMBO_BOX(separator->separator_style), 2);
row++, col = 2;
label = gtk_label_new(_("Size"));
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
gtk_widget_show(label);
gtk_table_attach(GTK_TABLE(table), label, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
separator->separator_size = gtk_spin_button_new_with_range(0, 10000, 1);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(separator->separator_size), 3);
gtk_widget_show(separator->separator_size);
gtk_table_attach(GTK_TABLE(table), separator->separator_size, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
row++, col = 2;
label = gtk_label_new(_("Horizontal padding"));
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
gtk_widget_show(label);
gtk_table_attach(GTK_TABLE(table), label, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
separator->separator_padding_x = gtk_spin_button_new_with_range(0, 500, 1);
gtk_spin_button_set_value(GTK_SPIN_BUTTON(separator->separator_padding_x), 1);
gtk_widget_show(separator->separator_padding_x);
gtk_table_attach(GTK_TABLE(table), separator->separator_padding_x, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
row++, col = 2;
label = gtk_label_new(_("Vertical padding"));
gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
gtk_widget_show(label);
gtk_table_attach(GTK_TABLE(table), label, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
separator->separator_padding_y = gtk_spin_button_new_with_range(0, 500, 1);
gtk_widget_show(separator->separator_padding_y);
gtk_table_attach(GTK_TABLE(table), separator->separator_padding_y, col, col+1, row, row+1, GTK_FILL, 0, 0, 0);
col++;
change_paragraph(parent);
}

View file

@ -128,6 +128,9 @@ typedef struct Separator {
GtkWidget *separator_background;
GtkWidget *separator_color;
GtkWidget *separator_style;
GtkWidget *separator_size;
GtkWidget *separator_padding_x;
GtkWidget *separator_padding_y;
} Separator;
extern GArray *separators;
@ -235,4 +238,6 @@ void create_please_wait(GtkWindow *parent);
void process_events();
void destroy_please_wait();
void hex2gdk(char *hex, GdkColor *color);
#endif

File diff suppressed because it is too large Load diff