talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit f4fd5a6ccc3f0e419b43d9282af6157f90d68c52
parent 25fc65e755ad2b04cd35a55286265dffd2ab927b
Author: Andrej Kacian <ticho@claws-mail.org>
Date:   Sat,  3 Nov 2018 18:39:24 +0100

New color selection menu, based on GtkComboBox.

Diffstat:
Msrc/gtk/colorlabel.c | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/gtk/colorlabel.h | 16++++++++++++++++
2 files changed, 126 insertions(+), 0 deletions(-)

diff --git a/src/gtk/colorlabel.c b/src/gtk/colorlabel.c @@ -220,6 +220,25 @@ static GtkWidget *colorlabel_create_color_widget(GdkRGBA *color) return widget; } +static GdkPixbuf *colorlabel_create_colormenu_pixbuf(GdkRGBA *color) +{ + GdkPixbuf *pixbuf; + guint32 pixel = 0; + + cm_return_val_if_fail(color != NULL, NULL); + + pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, + LABEL_COLOR_WIDTH - 2, LABEL_COLOR_HEIGHT - 4); + + /* "pixel" needs to be set to 0xrrggbb00 */ + pixel += (guint32)(color->red * 255) << 24; + pixel += (guint32)(color->green * 255) << 16; + pixel += (guint32)(color->blue * 255) << 8; + gdk_pixbuf_fill(pixbuf, pixel); + + return pixbuf; +} + /* XXX: colorlabel_recreate_XXX are there to make sure everything * is initialized ok, without having to call a global _xxx_init_ * function */ @@ -402,3 +421,94 @@ guint colorlabel_get_color_menu_active_item(GtkWidget *menu) (g_object_get_data(G_OBJECT(menuitem), "color")); return color; } + +static gboolean colormenu_separator_func(GtkTreeModel *model, + GtkTreeIter *iter, gpointer data) +{ + gchar *txt; + + gtk_tree_model_get(model, iter, COLORMENU_COL_TEXT, &txt, -1); + + if (txt == NULL) + return TRUE; + + return FALSE; +} + +GtkWidget *colorlabel_create_combobox_colormenu() +{ + GtkWidget *combobox; + GtkListStore *store; + GtkCellRenderer *renderer; + + store = gtk_list_store_new(3, + GDK_TYPE_PIXBUF, + G_TYPE_STRING, + G_TYPE_INT); + combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store)); + + gtk_combo_box_set_row_separator_func(GTK_COMBO_BOX(combobox), + (GtkTreeViewRowSeparatorFunc)colormenu_separator_func, + NULL, NULL); + + renderer = gtk_cell_renderer_pixbuf_new(); + gtk_cell_renderer_set_padding(renderer, 2, 0); + gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), + renderer, FALSE); + gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), + renderer, + "pixbuf", COLORMENU_COL_PIXBUF, + NULL); + + renderer = gtk_cell_renderer_text_new(); + gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), + renderer, TRUE); + gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), + renderer, + "text", COLORMENU_COL_TEXT, + NULL); + + colorlabel_refill_combobox_colormenu(GTK_COMBO_BOX(combobox)); + + return combobox; +} + +void colorlabel_refill_combobox_colormenu(GtkComboBox *combobox) +{ + GtkListStore *store; + GtkTreeIter iter; + gint i; + + cm_return_if_fail(combobox != NULL); + + store = GTK_LIST_STORE(gtk_combo_box_get_model(combobox)); + + cm_return_if_fail(store != NULL); + + gtk_list_store_clear(store); + + /* "None" */ + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, + COLORMENU_COL_PIXBUF, NULL, + COLORMENU_COL_TEXT, _("None"), + COLORMENU_COL_ID, -1, + -1); + /* Separator */ + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, + COLORMENU_COL_PIXBUF, NULL, + COLORMENU_COL_TEXT, NULL, + COLORMENU_COL_ID, -1, + -1); + + /* Menu items for individual colors */ + for (i = 0; i < LABEL_COLORS_ELEMS; i++) { + gtk_list_store_append(store, &iter); + gtk_list_store_set(store, &iter, + COLORMENU_COL_PIXBUF, colorlabel_create_colormenu_pixbuf(&label_colors[0][i].color), + COLORMENU_COL_TEXT, label_colors[0][i].label, + COLORMENU_COL_ID, i, + -1); + } +} diff --git a/src/gtk/colorlabel.h b/src/gtk/colorlabel.h @@ -30,6 +30,14 @@ #define SUMMARY_COLORMENU 1 #define NUM_MENUS 2 +/* Columns for model used in GtkComboBox color menu */ +typedef enum { + COLORMENU_COL_PIXBUF, + COLORMENU_COL_TEXT, + COLORMENU_COL_ID, + NUM_COLORMENU_COLS +} ColorMenuColumn; + void colorlabel_update_colortable_from_prefs(void); gint colorlabel_get_color_count (void); GdkRGBA colorlabel_get_color (gint color_index); @@ -43,4 +51,12 @@ GtkWidget *colorlabel_create_check_color_menu_item GtkWidget *colorlabel_create_color_menu (void); guint colorlabel_get_color_menu_active_item (GtkWidget *menu); +/* Creates a GtkComboBox with selection of configured colors */ +GtkWidget *colorlabel_create_combobox_colormenu(void); + +/* Resets contents of an existing combobox with matching + * model. Can be useful after prefs, and therefore configured + * colors, change. */ +void colorlabel_refill_combobox_colormenu(GtkComboBox *combobox); + #endif /* COLORLABEL_H__ */