Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 2, 2005 05:40:22

ashley m.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

colour question


G'day,

Trying to get my head around gtk2 I put together the code below from
examples in the gtktree tutorial and the colour example code I found in
the mailing list.
ie.http://mail.gnome.org/archives/gtk-app-devel-list/2002-November/msg00249.htmlIt compiles, and runs.

Unfortunately it colours the entire background blue and all fonts are
white. Not just the right hand column as in the example and as I wanted.

Could somebody please point me to where I stuffed up in the code below,
and or where to get info to fix what I clearly have misunderstood.

And where can I find a reference for stippling the background of a cell?

Thanks in advance.

Regards,

Ashley

#################


/* -*-coding: utf-8;-*- */
/* colour_dump.c -- demonstrate GtkTreeView */

#include <gtk/gtk.h>
#include <gdk/gdkx.h>

const char *numbers = { "Zero", "One", "Two", "Three", "Four",
"Five" };
const char *keys = {"AA", "ZB", "TY", "T6", "9T", "56" };

enum {
COL_KEY = 0,
COL_INT,
COL_STRING,
COL_COLOUR_BG,
COL_COLOUR_FG,
N_COLUMNS
};

/* standard event handlers */
gint delete_event(GtkWidget *widget, GdkEvent event, gpointer data)
{
return FALSE;
}

void end_program(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}

static void
onSelectionChanged (GtkTreeSelection *sel, GtkListStore *liststore)
{
GtkTreeIter selected_row; /* the iter I've been loking at
*/
GtkTreeModel *model;
gchar *key;


/* Check if a row is selected or not */
/* This will only work in SINGLE or BROWSE mode! */
if (gtk_tree_selection_get_selected(sel, &model, &selected_row))
{
gtk_tree_model_get (model, &selected_row, COL_KEY, &key,
-1);

g_print ("Key for selected row is: %s\n\n", key);

g_free(key);
}
else
{
printf ("Something\n\n");
}
}

int main(int argc, char **argv)
{
GtkWindow *window;
GtkListStore *list;
GtkTreeIter iter;
GtkTreeView *view;
GtkTreeViewColumn *num_column, *word_column;
GtkCellRenderer *text_renderer;
gint i;
GtkTreeSelection *sel;
GdkColor colors;
gboolean success;
GtkWidget *tv;


/* initialize GTK+, create main window */
gtk_init(&argc, &argv);


/* setup the colours */
/*********************/
/* forground *********/
colors.red = 0;
colors.blue = 65535;
colors.green = 0;
/* background ********/
colors.red = 65535;
colors.blue = 65535;
colors.green = 65535;


/* map the colours **/
gdk_colormap_alloc_colors(
gdk_colormap_get_system(),
colors,
2,
FALSE,
FALSE,
success
);


/* initialize GTK+, create main window */
/* gtk_init(&argc, &argv); */
window = g_object_new(GTK_TYPE_WINDOW,
"title", "Two Column List",
"default-width", 300,
NULL);

/* connect standard handlers */
g_signal_connect(window, "delete_event", G_CALLBACK(delete_event),
NULL);
g_signal_connect(window, "destroy", G_CALLBACK(end_program), NULL);

/* create a two-column list */
list = gtk_list_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_INT,
G_TYPE_STRING, GDK_TYPE_COLOR, GDK_TYPE_COLOR);

/* put some data into the list */
for (i = 0; i < 5; i++)
{
gtk_list_store_append(list, &iter);
gtk_list_store_set(list, &iter,
COL_KEY, keys,
COL_INT, i,
COL_STRING, numbers,
COL_COLOUR_BG, &colors,
COL_COLOUR_FG, &colors,
-1);
}

/* create tree view for the list */
view = g_object_new(GTK_TYPE_TREE_VIEW,
"model", list,
"rules-hint", TRUE,
"headers-clickable", TRUE,
"reorderable", TRUE,
"enable-search", TRUE,
"search-column", COL_STRING,
NULL);

/* create and initialize text renderer for cells */
text_renderer = gtk_cell_renderer_text_new();

/* create column views */
num_column = gtk_tree_view_column_new_with_attributes("Numeral",
text_renderer,
"text", COL_INT,
NULL);

g_object_set(num_column,
"resizable", TRUE,
"clickable", TRUE,
"reorderable", TRUE,
NULL);

word_column = gtk_tree_view_column_new_with_attributes("Word",
text_renderer,
"text",
COL_STRING,
"background-gdk",
COL_COLOUR_BG,
"foreground-gdk",
COL_COLOUR_FG,
NULL);

g_object_set(word_column,
"resizable", TRUE,
"clickable", TRUE,
"reorderable", TRUE,
NULL);

/* insert columns into the view */
gtk_tree_view_append_column(view, num_column);
gtk_tree_view_append_column(view, word_column);

/* Get the selection code in */
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
gtk_tree_selection_set_mode(sel, GTK_SELECTION_SINGLE);
g_signal_connect(sel, "changed", G_CALLBACK(onSelectionChanged),
list);

/* pack/show everything; start GTK+ main event loop */
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();

return 0;
}

_______________________________________________
gtk-list mailing list
gtk-list@gnome.orghttp://mail.gnome.org/mailman/listinfo/gtk-list

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support