Re: how to set different color for each cell in a treeview.
- From: Bijoy Chandrasekharan <bijoy_mc yahoo co in>
- To: gtk-app-devel-list gnome org
- Subject: Re: how to set different color for each cell in a treeview.
- Date: Mon, 25 Nov 2002 12:56:47 +0530
check this example sent by Olexiy some day before to the group. and
reposted by Emmanuel too......
hope it helps
bijoy.
chen.shengqi wrote:
Dear sirs:
How can I set color for each row or each cell in a treeview? it seems that gtl_list_store_set can only set
the cintext in the cell, and gtk_tree_view_column_new_with_attributes can onlu set color for a column.
thanks.
‚Ù¦—^½éeŠËfj)bž b²Ø-‘ªiuëÞ–X¬¶ è™ê+‚m§ÿæj)`ž‰ž¢¸?™¨¥™©ÿ–+-Šwèþdjš]z÷¥–+-
/*
compile with
rgb-db-browse: rgb-db-browse.c
gcc -Wall -g3 -O2 -march=i686 `pkg-config gtk+-2.0 --cflags --libs` $< -o $@
This one shows the content of X rgb data base.
"/usr/X11R6/lib/X11/rgb.txt" location expected.
Some bugs can be found.
Olexiy Avramchenko
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
enum
{
COLUMN_COLOR_INDEX, /* index of the color in the data base */
COLUMN_COLOR_NAME, /* name of the color from data base */
COLUMN_COLOR_VALUE, /* value (r:g:b) of the color */
COLUMN_COLOR_BG, /* invisible column, holding the bg color of COLUMN_COLOR_VALUE */
COLUMN_COLOR_FG, /* invisible column, holding the fg color of COLUMN_COLOR_VALUE */
COLUMNS_NUMBER
};
static GtkWidget *setup_3()
{
int index=0;
char s[1024], value[64], *color_name;
FILE *f;
XColor xcolor;
GdkColor colors[2];
gboolean success[2];
GtkWidget *tv;
GtkTreeIter iter;
GtkListStore *model;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
model = gtk_list_store_new(
COLUMNS_NUMBER,
G_TYPE_INT,
G_TYPE_STRING,
G_TYPE_STRING,
GDK_TYPE_COLOR,
GDK_TYPE_COLOR
);
f = fopen("/usr/X11R6/lib/X11/rgb.txt", "r");
if (!f) {
fprintf(stderr, "ERROR: can't open rgb db.\n");
exit(-1);
}
while(fgets(s, sizeof(s), f)) {
if (!*s || *s=='!')
continue;
s[strlen(s)-1] = 0; /* strip '\n' away */
color_name = strrchr(s, '\t');
if (!color_name)
continue;
gtk_list_store_append(model, &iter);
gtk_list_store_set(
model,
&iter,
-1
);
XParseColor(
GDK_DISPLAY(),
GDK_COLORMAP_XCOLORMAP(gdk_colormap_get_system()),
color_name+1,
&xcolor
);
colors[0].red = xcolor.red;
colors[0].green = xcolor.green;
colors[0].blue = xcolor.blue;
if (colors[0].red>0xa000 || colors[0].green>0xa000 || colors[0].blue>0xa000)
colors[1].red=colors[1].green=colors[1].blue = 0;
else colors[1].red=colors[1].green=colors[1].blue = 0xffff;
gdk_colormap_alloc_colors(
gdk_colormap_get_system(),
colors,
2,
FALSE,
FALSE,
success
);
sprintf(value, "%04x:%04x:%04x", colors[0].red,colors[0].green,colors[0].blue);
gtk_list_store_set(
model,
&iter,
COLUMN_COLOR_INDEX, index++,
COLUMN_COLOR_NAME, color_name+1,
COLUMN_COLOR_VALUE, value,
COLUMN_COLOR_BG, &colors[0],
COLUMN_COLOR_FG, &colors[1],
-1
);
}
fclose(f);
tv = gtk_tree_view_new_with_model((GtkTreeModel*)model);
g_object_unref(model);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(tv),
-1,
"N",
renderer,
"text",
COLUMN_COLOR_INDEX,
NULL
);
column = gtk_tree_view_get_column(GTK_TREE_VIEW(tv), COLUMN_COLOR_INDEX);
gtk_tree_view_column_set_sort_column_id(column, COLUMN_COLOR_INDEX);
gtk_tree_view_column_set_resizable(column, TRUE);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(tv),
-1,
"name",
renderer,
"text",
COLUMN_COLOR_NAME,
NULL
);
column = gtk_tree_view_get_column(GTK_TREE_VIEW(tv), COLUMN_COLOR_NAME);
gtk_tree_view_column_set_sort_column_id(column, COLUMN_COLOR_NAME);
gtk_tree_view_column_set_resizable(column, TRUE);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(
GTK_TREE_VIEW(tv),
-1,
"value",
renderer,
"text", COLUMN_COLOR_VALUE,
/* telling the renderer how to get the fg color */
"foreground-gdk", COLUMN_COLOR_FG,
/* telling the renderer how to get the bg color */
"background-gdk", COLUMN_COLOR_BG,
NULL
);
column = gtk_tree_view_get_column(GTK_TREE_VIEW(tv), COLUMN_COLOR_NAME);
gtk_tree_view_column_set_resizable(column, TRUE);
return tv;
}
int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *button;
GtkWidget *sw;
GtkWidget *treeview;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width(GTK_CONTAINER(window), 4);
gtk_window_set_default_size(GTK_WINDOW(window), 320,480);
gtk_window_set_title(GTK_WINDOW(window), "rgb data base content");
g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_main_quit), NULL);
vbox = gtk_vbox_new(FALSE, 2);
gtk_container_add(GTK_CONTAINER(window), vbox);
sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE,TRUE, 0);
treeview = setup_3();
gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), FALSE);
gtk_tree_selection_set_mode(
gtk_tree_view_get_selection((GtkTreeView*)treeview),
GTK_SELECTION_MULTIPLE
);
gtk_container_add(GTK_CONTAINER(sw), treeview);
button = gtk_button_new_from_stock(GTK_STOCK_QUIT);
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE,FALSE, 0);
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]