How can I change height and width of a treeview? - c

I've been stuck with this problem for a while. I have a tree view that shows a table. The size of the table depends on the user's input. I want to make it look nice and when the table has only 2 columns there is a lot of space that isn't use. So how do I change the heigth and width of the tree view? Thanks
static void show_results(struct listaObjetos *vectorObjetos[],int cantidad, int tamanoMochila){
int i;
char msg[180] = {0};
/
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
treeView = gtk_tree_view_new ();
gtk_widget_set_size_request (GTK_WIDGET (treeView), 0, 0);
window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window2), "Resultados");
gtk_container_set_border_width(GTK_CONTAINER(window2),10);
gtk_widget_set_size_request (window2, 480, 320);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("column1 ", renderer, "text",COLUM1, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
column = gtk_tree_view_column_new_with_attributes("colum2 ", renderer, "text",COLUM2, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
column = gtk_tree_view_column_new_with_attributes("colum3 ", renderer, "text",COLUMN3, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
column = gtk_tree_view_column_new_with_attributes("colum4", renderer, "text",COLUM4, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeView), column);
store = gtk_list_store_new (4, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,G_TYPE_INT);
for(i=0; i<cantidad; i++)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, COLUM1, vectorObjetos[i]->x,COLUMN2,
vectorObjetos[i]->y, COLUM3, vectorObjetos[i]->q,
COLUM4,vectorObjetos[i]->z,-1);
}
gtk_tree_view_columns_autosize(GTK_TREE_VIEW (treeView));
gtk_tree_view_set_model (GTK_TREE_VIEW (treeView), GTK_TREE_MODEL (store));
g_object_unref (store);
scrolledWin = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledWin),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
table = gtk_table_new(10,10,FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 5);
gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolledWin), table);
label = gtk_label_new("Objetos sujetos a analisis");
gtk_table_attach_defaults(GTK_TABLE(table), label,4,5,0,1);
g_snprintf(msg, sizeof msg, "A message here ",
cantidad, tamanoMochila );
label = gtk_label_new(msg);
gtk_table_attach_defaults(GTK_TABLE(table), label,0,10,1,2);
label = gtk_label_new(" ");
gtk_table_attach_defaults(GTK_TABLE(table), label,3,4,2,4);
gtk_table_attach_defaults (GTK_TABLE(table), treeView, 4,5,2,4);
label = gtk_label_new(" ");
gtk_table_attach_defaults(GTK_TABLE(table), label,5,6,2,4);
gtk_container_add (GTK_CONTAINER (window2), scrolledWin);
gtk_widget_show_all (window2);
}
I tried to post an image with the result but I couldn't due to reputation

Related

g_signal_connect_swapped fails to connect

I have tried past one month learning to use gtk for an application and migrating its code from gtk2 to gtk3. But, one button in a gtk dialog doesn't seem to work well, inspite of whatever i chose the dialog closes without calling the callback function.
int validation_pmu_server (GtkButton *button, gpointer udata)
{
/* local variables */
int tmp_p;
char *msg;
const char *text1, *text2;
/* Get the text entries filled by user */
text1 = gtk_entry_get_text(GTK_ENTRY(p_port));
text2 = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(p_protocol));
if(!isNumber((char *)text1) && (atoi(text1) < 1500 || (atoi(text1) > 65535))) /* Check for valid port number */
{
msg = "\tPlease enter valid value for PMU Server Port\t\n";
validation_result (msg); /* Show the unsuccess message to user */
return 0;
}
else
{
strncpy(PMUprotocol, text2, 3);
PMUport = atoi(text1);
if(!strcmp(text2, "UDP"))
tmp_p = 1;
else
tmp_p = 2;
p1 = *ShmPTR;
p1.cfg_bit_change_info = 0;
ShmPTR->cfg_bit_change_info = 0;
ShmPTR->serverProtocol = tmp_p;
ShmPTR->serverPort = atoi(text1);
p1.pid = ShmPTR->pid;
kill(p1.pid, SIGUSR2);
printf("PORT & Protocol information has sent to PMU Server through signal.\n");
memset(pmuFilePath, '\0', 200);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->start_server), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->open_cfg), FALSE);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->create_cfg_button), TRUE);
gtk_widget_set_sensitive(GTK_WIDGET(pmu_data->menu_setup_cfg), TRUE);
/* Close/destroy the pmu_server_window */
gtk_widget_destroy(pmu_server_window);
return 1;
}
};
void pmu_server ()
{
/* local variables */
GtkWidget *table, *label, *cancel_button;
GtkWidget *valdbutton, *help_button;
/* Create a new dialog window for PMU Server Setup */
pmu_server_window = gtk_dialog_new ();
g_signal_connect (pmu_server_window, "destroy", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
gtk_window_set_title (GTK_WINDOW (pmu_server_window), "PMU Server Setup");
gtk_container_set_border_width (GTK_CONTAINER (pmu_server_window), 10);
/* Create a table of 4 by 2 squares. */
table = gtk_grid_new (); // table = gtk_table_new (6, 2, FALSE);
/* Set the spacing to 50 on x and 5 on y */
gtk_grid_set_row_spacing (GTK_GRID(table), 16); // gtk_table_set_row_spacings (GTK_TABLE (table), 8);
gtk_grid_set_column_spacing (GTK_GRID(table), 16); // gtk_table_set_col_spacings (GTK_TABLE (table), 2);
/* Pack the table into the window */
//gtk_box_pack_start (GTK_BOX (GTK_DIALOG(pmu_server_window)->vbox), table, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG(pmu_server_window))), table, TRUE, TRUE, 0);
gtk_widget_show (table);
/* Add few buttons to the bottom of the dialog */
valdbutton = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Run", GTK_RESPONSE_OK); // gtk_button_new_with_label ("Run");
help_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Help", GTK_RESPONSE_APPLY); // gtk_button_new_with_label ("Help");
cancel_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Cancel", GTK_RESPONSE_CLOSE); //gtk_button_new_with_label ("Cancel");
/* This simply creates a grid of toggle buttons on the table */
label = gtk_label_new (" ");
markup = g_markup_printf_escaped ("<span foreground=\"#990033\" font='12'><b>Enter PMU Server Details</b></span>");
gtk_label_set_markup (GTK_LABEL (label), markup);
gtk_grid_attach (GTK_GRID (table), label, 0,0,2,1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 0, 1);
gtk_widget_show (label);
g_free (markup);
label = gtk_label_new ("Server Port");
// gtk_misc_set_alignment (GTK_MISC(label),0,0);
gtk_label_set_xalign (GTK_LABEL (label),0);
gtk_label_set_yalign (GTK_LABEL (label),0);
gtk_grid_attach (GTK_GRID (table), label, 0, 2, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
gtk_widget_show (label);
label = gtk_label_new ("Protocol");
// gtk_misc_set_alignment (GTK_MISC(label),0,0);
gtk_label_set_xalign (GTK_LABEL (label),0);
gtk_label_set_yalign (GTK_LABEL (label),0);
gtk_grid_attach (GTK_GRID (table), label, 0, 3, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 3, 4);
gtk_widget_show (label);
/* Create text boxes for user to enter appropriate values */
p_port = gtk_entry_new();
gtk_entry_set_max_length ((GtkEntry *)p_port, 5);
gtk_grid_attach (GTK_GRID (table), p_port, 1, 2, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), p_port, 1, 2, 2, 3);
gtk_widget_show (p_port);
label = gtk_label_new (" ");
markup = g_markup_printf_escaped ("<span foreground=\"#333333\" font='8'><b>Note : Use the unreserved ports for PMU Server.</b></span>");
gtk_label_set_markup (GTK_LABEL (label), markup);
// gtk_misc_set_alignment (GTK_MISC(label),0,0);
gtk_label_set_xalign (GTK_LABEL (label),0);
gtk_label_set_yalign (GTK_LABEL (label),0);
gtk_grid_attach (GTK_GRID (table), label, 0, 5, 2, 1); // gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 2, 5, 6);
gtk_widget_show (label);
g_free (markup);
/* Create combo boxe for user with some fixed values */
p_protocol = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(p_protocol), "UDP");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(p_protocol), "TCP");
gtk_combo_box_set_active(GTK_COMBO_BOX(p_protocol), 0);
gtk_grid_attach (GTK_GRID (table), p_protocol, 1, 3, 1, 1); // gtk_table_attach_defaults (GTK_TABLE (table), p_protocol, 1, 2, 3, 4);
gtk_widget_show (p_protocol);
/* Signal handling for buttons on PMU Server Setup Window */
g_signal_connect_swapped (valdbutton, "clicked", G_CALLBACK (validation_pmu_server), valdbutton);
g_signal_connect_swapped (help_button, "clicked", G_CALLBACK (Pmu_Help), NULL);
g_signal_connect_swapped (cancel_button, "clicked", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
/* This makes it so the button is the default. */
// gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pmu_server_window)->action_area), valdbutton, TRUE, TRUE, 0);
// gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pmu_server_window)->action_area), help_button, TRUE, TRUE, 0);
// gtk_box_pack_start (GTK_BOX (GTK_DIALOG (pmu_server_window)->action_area), cancel_button, TRUE, TRUE, 0);
/* This grabs this button to be the default button. Simply hitting the "Enter" key will cause this button to activate. */
gtk_widget_grab_default (valdbutton);
gtk_widget_show (valdbutton);
gtk_widget_grab_default (help_button);
gtk_widget_show (help_button);
gtk_widget_grab_default (cancel_button);
gtk_widget_show (cancel_button);
/* Finally show the pmu_server_window */
gtk_widget_show (pmu_server_window);
};
The function pmu_server creates a dialog as shown in this image
as per my code when run button is clicked validation_pmu_server is supposed to be called but rather the dialog box simply closes as if the gtk_widget_destroy was called. some of the commented code is the old gtk2 code which i migrated to gtk3.
PS : The above code change from gtk2 to gtk3 is done using this reference
Though I was able to make my code work somewhow, I please request anyone with knowledge to help me understand why this is happening.
Apparantly the problem was due to the below line
g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
Once I either remove the above line or change it to the below code, everything seems working fine, but I'm still unsure why, i'm guessing that sending "response" as the detailed signal was causing the problem.
g_signal_connect_swapped (pmu_server_window, "destroy", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
The problem you are facing is because of response signal. Whenever any action widget(in your case dialog buttons) is clicked, the response signal is emitted before any other signal(clicked signal). So the function callback associated with signal response will be executed first and then the callback associated with the clicked signal. So, the gtk_widget_destroy is getting called up first and your dialog is closing.
You can either change the callback associated with response or use any other signal.
I have used the below standalone code to reproduce the issue.
#include<stdio.h>
#include <gtk/gtk.h>
void Pmu_Help(){
printf("Pmu_Help");
fflush(stdout);
}
void validation_pmu_server(){
printf("validation_pmu_server");
fflush(stdout);
}
void response(){
printf("response");
fflush(stdout);
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget * pmu_server_window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(window);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
pmu_server_window = gtk_dialog_new ();
g_signal_connect (pmu_server_window, "destroy", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
gtk_window_set_title (GTK_WINDOW (pmu_server_window), "PMU Server Setup");
gtk_container_set_border_width (GTK_CONTAINER (pmu_server_window), 10);
GtkWidget *table, *label, *cancel_button;
GtkWidget *valdbutton, *help_button;
valdbutton = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Run", GTK_RESPONSE_OK); // gtk_button_new_with_label ("Run");
help_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Help", GTK_RESPONSE_APPLY); // gtk_button_new_with_label ("Help");
cancel_button = gtk_dialog_add_button((GtkDialog *)pmu_server_window, "Cancel", GTK_RESPONSE_CLOSE); //gtk_button_new_with_label ("Cancel");
g_signal_connect_swapped (valdbutton, "clicked", G_CALLBACK (validation_pmu_server), valdbutton);
g_signal_connect_swapped (help_button, "clicked", G_CALLBACK (Pmu_Help), NULL);
g_signal_connect_swapped (cancel_button, "clicked", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (response), pmu_server_window);
//g_signal_connect_swapped (pmu_server_window, "response", G_CALLBACK (gtk_widget_destroy), pmu_server_window);
gtk_widget_show(pmu_server_window);
gtk_main();
return 0;
}

drawing on window using gtk 2.0

I have a program using pthreads. I want add an interface using gtk2.0, but have a problem with drawing on window. When I call g_signal_connect(G_OBJECT(window), "configure_event",G_CALLBACK(draw_things), NULL);
lines which I draw appear and at once disappear. If I move a window calling function
gtk_window_move (GTK_WINDOW(widget), 10,10);
or by means of a mouse, lines appear. If I call g_signal_connect(G_OBJECT(window), "expose_event", G_CALLBACK(draw_things), NULL); my lines are displayed correctly, but buttons and labels are not displayed. My code looks like this:
int main(int argc, char *argv[]){
pthread_t tr1;
pthread_t tr2;
pthread_t *trs;
int rc = 0;
type j;
int16_t *id;
type Stop_Point;
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;
GtkWidget *drawing_area;
g_thread_init (NULL);
gdk_threads_init ();
gdk_threads_enter ();
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window, width/1.6, height/1.33);
gtk_window_set_position(GTK_WINDOW(window), -1);
gtk_window_set_title (GTK_WINDOW (window), "Window");
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 2);
table = gtk_table_new ( width/1.6, height/1.33, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
button = gtk_button_new_with_label ("next >>");
gtk_table_attach_defaults (GTK_TABLE (table), button, 1110, 1190, 750, 790);
gtk_widget_show (button);
button = gtk_button_new_with_label ("<< prev");
gtk_table_attach_defaults (GTK_TABLE (table), button, 1020, 1100 , 750, 790);
gtk_widget_show (button);
rc = pthread_create(&tr1, NULL, tr1_func, &id);
rc = pthread_create(&Generator, NULL, tr2_func, &Stop_Point);
trs = (pthread_t*)malloc(Num * sizeof(pthread_t));
for(j = 0; j < Num; j++) {
rc = pthread_create(trs + j, NULL, trs_func, id + j);
}
// g_signal_connect(G_OBJECT(window), "configure-event", GTK_SIGNAL_FUNC(draw_things), NULL);
g_signal_connect(G_OBJECT(window), "expose_event", G_CALLBACK(draw_things), NULL);
gtk_widget_show_all (window);
gtk_main ();
gdk_threads_leave ();
return 0;
}
Fnction for drawing looks like this
gboolean draw_things(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
GdkGC *gc;
gc=gdk_gc_new(GDK_DRAWABLE(widget->window));
gdk_gc_set_line_attributes(gc, 5, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 5, 35, 1200, 35);
gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 1170, 45, 1200, 35);
gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 1170, 25 , 1200, 35);
// gtk_window_move (GTK_WINDOW(widget), 1,1);
return TRUE;
}
Any help is welcomed, because now I use "configure_event" and call gtk_window_move (GTK_WINDOW(widget), 0,0); every time when I need to draw the picture.

how to align GtkTable items?

hi i'm trying to create a settings dialog for simple text editor
i want to know how to align labels to left
this is the image show it
http://oi59.tinypic.com/2qs5vdx.jpg
this is my code
GtkWidget * createSettingsDialog(GtkWidget *textView){
GtkWidget *dialog,
*labelWrap,
*radioWrapNone,
*radioWrapWord,
*labelJustify,
*radioJustifyLeft,
*radioJustifyRight,
*radioJustifyCenter,
*labelMarginLeft,
*spinLeftMargin,
*labelMarginRight,
*spinRightMargin,
*table,
*area;
GSList *groupWrap,
*groupJustify;
dialog = gtk_dialog_new_with_buttons ("Settings",
NULL,
GTK_DIALOG_MODAL,
GTK_STOCK_OK,
GTK_RESPONSE_OK,
GTK_STOCK_APPLY,
GTK_RESPONSE_APPLY,
GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL,
NULL);
area = gtk_dialog_get_content_area (GTK_DIALOG(dialog));
table = gtk_table_new (7,4,TRUE);
labelWrap = gtk_label_new("Wrap text :");
labelJustify = gtk_label_new("justification :");
labelMarginLeft = gtk_label_new("margin left :");
labelMarginRight = gtk_label_new("margin right :");
radioWrapNone = gtk_radio_button_new_with_label (NULL,"wrap none");
groupWrap = gtk_radio_button_get_group (GTK_RADIO_BUTTON(radioWrapNone));
radioWrapWord = gtk_radio_button_new_with_label (groupWrap,"wrap word");
radioJustifyCenter = gtk_radio_button_new_with_label (NULL,"justify center");
groupJustify = gtk_radio_button_get_group (GTK_RADIO_BUTTON(radioJustifyCenter));
radioJustifyLeft = gtk_radio_button_new_with_label (groupJustify,"justify left");
groupJustify = gtk_radio_button_get_group (GTK_RADIO_BUTTON(radioJustifyLeft));
radioJustifyRight = gtk_radio_button_new_with_label (groupJustify,"justify right");
spinLeftMargin = gtk_spin_button_new_with_range (0,10,1);
spinRightMargin = gtk_spin_button_new_with_range (0,10,1);
gtk_table_attach_defaults (GTK_TABLE(table),labelWrap,0,1,0,1);
gtk_table_attach_defaults (GTK_TABLE(table),radioWrapNone,1,2,1,2);
gtk_table_attach_defaults (GTK_TABLE(table),radioWrapWord,2,3,1,2);
gtk_table_attach_defaults (GTK_TABLE(table),labelJustify,0,1,2,3);
gtk_table_attach_defaults (GTK_TABLE(table),radioJustifyLeft,1,2,3,4);
gtk_table_attach_defaults (GTK_TABLE(table),radioJustifyCenter,2,3,3,4);
gtk_table_attach_defaults (GTK_TABLE(table),radioJustifyRight,3,4,3,4);
gtk_table_attach_defaults (GTK_TABLE(table),labelMarginLeft,0,1,4,5);
gtk_table_attach_defaults (GTK_TABLE(table),spinLeftMargin,1,2,4,5);
gtk_table_attach_defaults (GTK_TABLE(table),labelMarginRight,0,1,5,6);
gtk_table_attach_defaults (GTK_TABLE(table),spinRightMargin,1,2,5,6);
gtk_table_set_homogeneous (GTK_TABLE(table),TRUE);
gtk_box_pack_start (GTK_BOX(area),table,FALSE,FALSE,0);
gtk_widget_show_all (table);
return dialog;
}
i hope you understand my idea !!!!!
To align widgets like GtkLabel in containers, you should use GtkAlignment.

GTKButton with GtkListStore

I tried to add GTKButton to a GtkListStore to edit data in each row... The button doesn't appear, and i get this error message : "GLib-GObject-WARNING **: unable to set property text' of typegchararray' from value of type `GtkButton'".
here is my code :
#include <gtk/gtk.h>
GtkTreeModel * create_and_fill_model()
{
GtkListStore *store;
GtkTreeIter iter;
store = gtk_list_store_new (9,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_FLOAT,
GTK_TYPE_BUTTON);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
0, "A5525654",
1, "DFFFCCN",
2, "Doe",
3, "John",
4, "M",
5, "casablanca",
6, 2014,
7, 18.22,
8,GTK_BUTTON(gtk_button_new_with_label("EDIT")),
-1);
return GTK_TREE_MODEL (store);
}
GtkWidget * create_view_and_model()
{
GtkCellRenderer *renderer;
GtkTreeModel *model;
GtkWidget *view;
view = gtk_tree_view_new ();
char colonne[9][16] =
{
"CIN",
"CNE",
"Nom",
"Prenom",
"Sexe",
"Etablissement",
"Annee Obt",
"Note Gener",
"Edit"
};
int i;
for(i=0; i<9; i++)
{
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
colonne[i],
renderer,
"text", i,
NULL);
}
model = create_and_fill_model ();
gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
g_object_unref (model);
return view;
}
int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
view = create_view_and_model ();
gtk_container_add (GTK_CONTAINER (window), view);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Source code that works:
#include <gtk/gtk.h>
GtkTreeModel * create_and_fill_model()
{
GtkListStore *store;
GdkPixbuf *icon;
GError *error = NULL;
GtkTreeIter iter;
store = gtk_list_store_new (9,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_FLOAT,
GDK_TYPE_PIXBUF);
gtk_list_store_append (store, &iter);
icon = gdk_pixbuf_new_from_file("edit.png", &error);
gtk_list_store_set (store, &iter,
0, "A5525654",
1, "DFFFCCN",
2, "Doe",
3, "John",
4, "M",
5, "casablanca",
6, 2014,
7, 18.22,
8,icon,
-1);
if (error)
{
g_warning ("probleme de chargement de l'icone : %s\n", error->message);
g_error_free(error);
error = NULL;
}
return GTK_TREE_MODEL (store);
}
GtkWidget * create_view_and_model()
{
GtkCellRenderer *renderer;
GtkTreeModel *model;
GtkWidget *view;
view = gtk_tree_view_new ();
char colonne[9][16] =
{
"CIN",
"CNE",
"Nom",
"Prenom",
"Sexe",
"Etablissement",
"Annee Obt",
"Note Gener",
"Edit"
};
int i;
for(i=0; i<9; i++)
{
if(i==8)
{
renderer = gtk_cell_renderer_pixbuf_new();
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
colonne[i],
renderer,
"pixbuf", i,
NULL);
}
else
{
renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-1,
colonne[i],
renderer,
"text", i,
NULL);
}
}
model = create_and_fill_model ();
gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
g_object_unref (model);
return view;
}
int main (int argc, char **argv)
{
GtkWidget *window;
GtkWidget *view;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
view = create_view_and_model ();
gtk_container_add (GTK_CONTAINER (window), view);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
Your implementation of placing a button in tree view cannot work as written. You are creating a text renderer for all the columns, and a text renderer obviously cannot draw a GTK button. (You get the error while setting the text property on the renderer based on a button-typed column in the model.)
One simple way of placing a button in a tree view is by putting a clickable image in the cell, as shown by this answer.

Refresh Table in GTK

I've done a little program in C with GTK with a window, a box and a table inside the box. The table is painted with some files. I'd like to refresh the table with other data but I don't know how.
This code launches a window with a GtkTable. This table is filled in the timer handle function. First time draw three lines in order 1,2,3 and last time write three lines in order 3,2,1. But the table never refreshes.
#include <stdlib.h>
#include <gtk/gtk.h>
static gboolean time_handler(GtkWidget *table);
gboolean launched;
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vboxgeneral;
gchar *title;
GtkWidget *table;
gtk_init(&argc, &argv);
/* WINDOW */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
title = "Test GtkTable";
gtk_window_set_title(GTK_WINDOW(window), title);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
/* ADDING GTKHBOX TO MAIN WINDOW */
vboxgeneral = gtk_vbox_new(FALSE, 0);
/* ADDING GTKTABLE TO VBOX */
table = gtk_table_new(1,1,FALSE);
gtk_box_pack_start(GTK_BOX(vboxgeneral), table, FALSE, FALSE, 0);
/* SHOW ALL */
gtk_widget_show_all(window);
/* ACTIONS */
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
/* START TIMER */
g_timeout_add(10000, (GSourceFunc) time_handler, (gpointer) window);
time_handler(table);
gtk_main();
return 0;
}
static gboolean time_handler(GtkWidget *table)
{
GtkWidget *widget;
gboolean result = FALSE;
/* ADDING TABLE DEFINITION */
if(!launched)
{
table = gtk_table_new(3,1,FALSE);
widget = gtk_label_new("File 1");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 0, 1);
widget = gtk_label_new("File 2");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 1, 2);
widget = gtk_label_new("File 3");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 2, 3);
launched = TRUE;
result = TRUE;
g_print("Timer executed fisrt time!\n");
}
else
{
table = gtk_table_new(3,1,FALSE);
widget = gtk_label_new("File 3");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 0, 1);
widget = gtk_label_new("File 2");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 1, 2);
widget = gtk_label_new("File 1");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 2, 3);
result = FALSE;
g_print("Timer executed last time!\n");
}
gtk_widget_draw(table, NULL);
return result;
}
Your code has several issues:
You didn't add your vbox to your window. That can be done with the following code:
gtk_container_add (GTK_CONTAINER (window), vboxgeneral);
You were passing the wrong parameter (the table widget) to your time_handler function. In that function, you created a new table losing the previous one which you packed into a vbox. What you can do is destroy the previous table and create a new one with the new labels and pack it into the vbox.
The call time_handler(table) made in your main function is completely unnecesary. The timer already does that for you.
The revised code I made (a working code which does what you asked) as follows:
#include <stdlib.h>
#include <gtk/gtk.h>
static gboolean time_handler(GtkWidget *table);
gboolean launched;
int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *vboxgeneral;
gchar *title;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
title = "Test GtkTable";
gtk_window_set_title(GTK_WINDOW(window), title);
gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
/* ADDING GTKHBOX TO MAIN WINDOW */
vboxgeneral = gtk_vbox_new(FALSE, 0);
/* This step was missing! */
gtk_container_add (GTK_CONTAINER (window), vboxgeneral);
gtk_widget_show_all(window);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
/* START TIMER */
/* refresh in 1 second, change to the desired amount */
g_timeout_add(1000, (GSourceFunc) time_handler, (gpointer) vboxgeneral);
gtk_main();
return 0;
}
static gboolean time_handler(GtkWidget *vbox)
{
GtkWidget *widget;
static GtkWidget *table = NULL;
gboolean result = FALSE;
/* if you want only the new labels to appear and not the previous ones you can: */
/* 1. destroy the table and create a new one. This will be done */
/* 2. hide all previous labels in the table and create and show the new ones */
if (table != NULL) {
/* destroy previous table packed into vbox */
gtk_widget_destroy(table);
}
table = gtk_table_new(3,1,FALSE); /* create new table for the new labels */
if(!launched) {
widget = gtk_label_new("File 1");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 0, 1);
widget = gtk_label_new("File 2");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 1, 2);
widget = gtk_label_new("File 3");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 2, 3);
launched = TRUE;
result = TRUE;
g_print("Timer executed first time!\n");
} else {
/* table = gtk_table_new(3,1,FALSE); */
widget = gtk_label_new("File 3");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 0, 1);
widget = gtk_label_new("File 2");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 1, 2);
widget = gtk_label_new("File 1");
gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
gtk_table_attach_defaults (GTK_TABLE (table), widget, 0, 1, 2, 3);
launched = FALSE;
result = FALSE;
g_print("Timer executed last time!\n");
}
/* ADD THE NEWLY CREATED TABLE TO THE VBOX */
gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
/* VERY IMPORTANT: SHOW ALL THE NEW LABELS PACKED INTO THE TABLE */
gtk_widget_show_all(table);
return result;
}

Resources