So I am using C and I am trying to render a GtkPixbuf in a GtkDrawingArea. I have a GtkThread that should be in charge of forcing the draw every N seconds. I don't get any errors or warnings, but, my code still doesn't produce the intended results. Hence, I believe that I am missing something fundamental about how all this works.
My code is below:
#include <gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 512
#define HEIGHT 512
#define CHANNELS 3
#define STEP (8*CHANNELS)
typedef struct pixbuf_data_s{
unsigned char* data;
GdkPixbuf *pixbuf;
GtkWidget *drawing;
} pixbuf_data_t;
GdkPixbuf* create_and_set_pixbuf(unsigned char* data){
GdkPixbuf *pixbuf;
pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, FALSE, 8, WIDTH, HEIGHT, STEP/8, NULL, NULL);
return pixbuf;
}
void create_image_data(unsigned char* data){
int i,j,k;
unsigned char blue, red, green;
//===Create A Random RGB Combo===//
blue = (unsigned char)((rand()) % 255);
green = (unsigned char)((rand()) % 255);
red = (unsigned char)((rand()) % 255);
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
for(k=0;k<CHANNELS;k++){
if (k == 0) data[i*STEP+j*CHANNELS+k] = red;
if (k == 1) data[i*STEP+j*CHANNELS+k] = green;
if (k == 2) data[i*STEP+j*CHANNELS+k] = blue;
}
}
}
return;
}
pixbuf_data_t* create_pixbuf_data(){
pixbuf_data_t* pixbuf_data = malloc(sizeof(pixbuf_data_t));
//===Create PixBuf Data===//
pixbuf_data->data = malloc( (WIDTH*HEIGHT*CHANNELS*STEP) * sizeof(unsigned char));
create_image_data(pixbuf_data->data);
pixbuf_data->pixbuf = gdk_pixbuf_new_from_data(pixbuf_data->data, GDK_COLORSPACE_RGB, FALSE, 8, WIDTH, HEIGHT, STEP/8, NULL, NULL);
return pixbuf_data;
}
void draw_callback(GtkWidget *widget, cairo_t *cr, gpointer ptr){
pixbuf_data_t *pixbuf;
pixbuf = (pixbuf_data_t*) ptr;
//===Redo Image===//
create_image_data(pixbuf->data);
//===Set Source===//
gdk_cairo_set_source_pixbuf(cr, pixbuf->pixbuf, 0, 0);
//===Fill===//
cairo_fill(cr);
return;
}
void *display_image_thread(gpointer in_data){
pixbuf_data_t* pixbuf;
pixbuf = g_object_get_data(G_OBJECT(in_data), "pixbuf");
while(1){
sleep(5);
gtk_widget_queue_draw(GTK_WIDGET(pixbuf->drawing));
}
return;
}
int main(int argc, char* argv[]){
GtkBuilder *builder;
GtkWidget *window;
GtkButton *button;
GtkImage *image;
GtkWidget *drawing_area;
GError *error = NULL;
GObject *object;
GThread *thread;
GdkPixbuf *pix_buf;
pixbuf_data_t* pixbuf_data;
//===Init Random Number Generator===//
srand(time(NULL));
//===Init GTK+===//
gtk_init(NULL, NULL);
//===Secure Glib and Gtk===/
if(!g_thread_supported()){
g_thread_init(NULL);
}
gdk_threads_init();
//===Obtain gtk's global lock===//
gdk_threads_enter();
//===Create new GtkBuilder object===//
builder = gtk_builder_new();
if(!gtk_builder_add_from_file( builder, "draw_glade.glade", &error )){
g_free(error);
exit(1);
}
//===Get main window pointer from UI===//
window = GTK_WIDGET( gtk_builder_get_object( builder, "main_window" ) );
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
//===Get Buttons===//
button = GTK_BUTTON( gtk_builder_get_object(builder, "stop_button"));
gtk_button_set_label(button, "STOP");
button = GTK_BUTTON( gtk_builder_get_object(builder, "play_button"));
gtk_button_set_label(button, "PLAY");
button = GTK_BUTTON( gtk_builder_get_object(builder, "pause_button"));
gtk_button_set_label(button, "PAUSE");
button = GTK_BUTTON( gtk_builder_get_object(builder, "next_button"));
gtk_button_set_label(button, "NEXT");
//===Create Drawing Area and Callback===//
drawing_area = GTK_WIDGET(gtk_builder_get_object(builder, "main_drawing_area"));
pixbuf_data = create_pixbuf_data();
pixbuf_data->drawing = drawing_area;
g_signal_connect(pixbuf_data->drawing, "draw", G_CALLBACK(draw_callback), pixbuf_data);
//===Create new thread===//
object = g_object_new(G_TYPE_OBJECT, NULL);
g_object_set_data(G_OBJECT(object), "pixbuf", (gpointer) pixbuf_data);
thread = g_thread_new(NULL, display_image_thread, (gpointer)object);
if(!thread){
printf("Bad Thread!\n");
exit(1);
}
//===Connect signals===//
gtk_builder_connect_signals(builder, NULL);
//==Show window===//
gtk_widget_show(window);
//===Gtk Main Callback===//
gtk_main();
//===Release gtk's global lock===//
gdk_threads_leave();
//===Destroy builder===//
g_object_unref(G_OBJECT(builder));
return 0;
}
The glade file is:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="main_window">
<property name="can_focus">False</property>
<property name="default_width">800</property>
<property name="default_height">600</property>
<child>
<object class="GtkBox" id="right_vbox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkDrawingArea" id="main_drawing_area">
<property name="width_request">512</property>
<property name="height_request">512</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="play_button_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="stop_button">
<property name="label" translatable="yes">button</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="play_button">
<property name="label" translatable="yes">button</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="pause_button">
<property name="label" translatable="yes">button</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="next_button">
<property name="label" translatable="yes">button</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
So what am I missing?
Sources I've used:
C/GTK+3 draw event for multiple widgets
https://developer.gnome.org/gtk3/stable/GtkDrawingArea.html
https://developer.gnome.org/gdk3/stable/gdk3-Cairo-Interaction.html#gdk-cairo-set-source-pixbuf
https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-queue-draw
https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-draw
Related
I'm new to GTK and C. I started making UI for the embedded system. The interface should represent several tabs with information, switching between which is carried out by buttons on the device itself or by JS commands from a paired device (server). I am trying to implement this with GTK and Glade. I got a problem while using GtkStack. I need to pass the name of the stack page to the server so that the server sends back the information that will be displayed on the page.
I tried to get the Name of the page from the glade file using the function gtk_widget_style_get, but the compiler issues a warning ../gtk+-3.24.27/gtk/gtkwidget.c:13324: widget class 'GtkStack' has no property named 'title' and the program displays "garbage" instead of the page name. I also tried to get the "label" of the button. gtk_widget_style_get(btn_1, "title", &temp, NULL) The result was the same.
Here is the test code I am experimenting with
main.c
#include <gtk/gtk.h>
#define UI_FILE "for_test.glade"
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *stack;
GtkWidget *fix_1;
GtkWidget *lbl_1;
GtkWidget *btn_1;
GtkWidget *fix_2;
GtkWidget *lbl_2;
GtkWidget *btn_2;
void
on_btn_1_clicked(GtkWidget *widget)
{
gchar *temp[100];
gtk_widget_style_get(stack, "title", &temp, NULL);
gtk_label_set_text(GTK_LABEL(lbl_1), *temp);
}
void
on_window_destroy()
{
gtk_main_quit();
}
int
main (int argc, char *argv[])
{
gtk_init(&argc, &argv);
builder = gtk_builder_new_from_file(UI_FILE);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
stack = GTK_WIDGET(gtk_builder_get_object(builder, "stack"));
fix_1 = GTK_WIDGET(gtk_builder_get_object(builder, "fix_1"));
lbl_1 = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_1"));
btn_1 = GTK_WIDGET(gtk_builder_get_object(builder, "btn_1"));
fix_1 = GTK_WIDGET(gtk_builder_get_object(builder, "fix_1"));
lbl_2 = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_2"));
btn_2 = GTK_WIDGET(gtk_builder_get_object(builder, "btn_2"));
g_signal_connect(window, "destroy", G_CALLBACK(on_window_destroy), NULL);
g_signal_connect(btn_1, "clicked", G_CALLBACK(on_btn_1_clicked), NULL);
//g_signal_connect(btn_2, "clicked", G_CALLBACK(on_btn_2_clicked), NULL);
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
return 0;
}
for_test.glade
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="window">
<property name="can-focus">False</property>
<property name="default-width">100</property>
<property name="default-height">200</property>
<signal name="destroy" handler="on_window_destroy" swapped="no"/>
<child>
<object class="GtkStack" id="stack">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkFixed" id="fix_1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkButton" id="btn_1">
<property name="label" translatable="yes">button 1</property>
<property name="width-request">100</property>
<property name="height-request">80</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="on_btn_1_clicked" swapped="no"/>
</object>
<packing>
<property name="y">100</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lbl_1">
<property name="width-request">100</property>
<property name="height-request">80</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
</child>
</object>
<packing>
<property name="name">page0</property>
<property name="title" translatable="yes">page0</property>
</packing>
</child>
<child>
<object class="GtkFixed" id="fix_2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkButton" id="btn_2">
<property name="label" translatable="yes">button 2</property>
<property name="width-request">100</property>
<property name="height-request">80</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<signal name="clicked" handler="on_btn_2_clicked" swapped="no"/>
</object>
<packing>
<property name="y">100</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="lbl_2">
<property name="width-request">100</property>
<property name="height-request">80</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
</child>
</object>
<packing>
<property name="name">page1</property>
<property name="title" translatable="yes">page1</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
I suspect that I am using the wrong function to get the properties of the stackab page, but I haven't found another way to get them. Where did I go wrong?
Here is one of the solutions:
GValue k = {0,};
g_value_init(&k, G_TYPE_STRING);
gtk_container_child_get_property(GTK_CONTAINER(stack), fix_1, "name", &k);
g_message("%s", g_value_get_string(&k));
Recently, I upgrade my code from ubuntu 14.04 to 20.04.
The UI seems works fine but the submenu with the touch panel.
I press the menu and select menuitem2 and the submenu was showed,
When I tap the touch screen to open the sub-menuitem, it did not response.
I wrote a code to check and found the signal 'select' was sent instead of the 'active' when I tap the touch screen. (The code works fine when using mouse click)
Is anyone could help me to solve this problem? Or have any other GtkWidget could use to replace the GtkMenu?
Any comments and suggestion will be appreciated.
Brlow is my code:
/*
* Build:
* gcc $(pkg-config --cflags gtk+-3.0) popover_sample.c -o popover_sample $(pkg-config --libs gtk+-3.0)
*/
#include <stddef.h>
#include <gtk/gtk.h>
void on_ITEM2_activate(GtkMenuItem* button, gpointer user_data)
{
printf("on_ITEM2_activate \n");
}
void on_SEC_2_activate(GtkMenuItem* button, gpointer user_data)
{
printf("on_SEC_2_activate \n");
}
void on_SEC_1_activate(GtkMenuItem* button, gpointer user_data)
{
printf("on_SEC_1_activate \n");
}
void on_submenuitem1_activate(GtkMenuItem* button, gpointer user_data)
{
printf("on_submenuitem1_activate \n");
}
void on_submenuitem2_activate(GtkMenuItem* button, gpointer user_data)
{
printf("on_submenuitem2_activate \n");
}
void on_submenuitem3_activate(GtkMenuItem* button, gpointer user_data)
{
printf("on_submenuitem3_activate \n");
}
void on_submenuitem4_activate(GtkMenuItem* button, gpointer user_data)
{
printf("on_submenuitem4_activate \n");
}
// called when window is closed
void on_QUIT_BTN_clicked()
{
gtk_main_quit();
}
void on_btn1_clicked(GtkMenuItem* button, gpointer user_data)
{
gtk_main_quit();
}
void on_ITEM1_activate(GtkMenuItem* button, gpointer user_data)
{
GtkWidget *open_item; // New menuitem
GtkWidget *save_item; // New menuitem
GtkWidget *quit_item; // New menuitem
GtkWidget *sep; // New sep
GtkWidget *ITEM1_Menu; // New menu
ITEM1_Menu = gtk_menu_new();
open_item = gtk_menu_item_new_with_label ("Open");
save_item = gtk_menu_item_new_with_label ("Save");
quit_item = gtk_menu_item_new_with_label ("Quit");
sep = gtk_separator_menu_item_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(button), ITEM1_Menu);
gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), open_item);
gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), save_item);
gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), sep);
gtk_menu_shell_append(GTK_MENU_SHELL(ITEM1_Menu), quit_item);
g_signal_connect_swapped (G_OBJECT (open_item), "activate",
G_CALLBACK (on_submenuitem1_activate),
NULL);
g_signal_connect_swapped (G_OBJECT (save_item), "activate",
G_CALLBACK (on_submenuitem2_activate),
NULL);
g_signal_connect_swapped (G_OBJECT (quit_item), "activate",
G_CALLBACK (on_QUIT_BTN_clicked),
NULL);
gtk_widget_show (open_item);
gtk_widget_show (save_item);
gtk_widget_show (quit_item);
gtk_widget_show (sep);
printf("on_ITEM1_activate \n");
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
GError *error = NULL;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
if (gtk_builder_add_from_file (builder, "Menu_Test.glade", &error) == 0)
{
printf("Unable to open file named \"Menu_Test.glade\" \n");
return 1;
}
window = GTK_WIDGET(gtk_builder_get_object(builder, "test_win"));
if (NULL == window)
{
printf("Unable to file object with id \"test_win\" \n");
}
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "test_win"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
and the glade as below:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">ITEM_1</property>
<property name="label" translatable="yes">menuitem1</property>
<property name="use-underline">True</property>
<signal name="select" handler="on_ITEM1_activate" swapped="no"/>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">ITEM_2</property>
<property name="label" translatable="yes">menuitem2</property>
<property name="use-underline">True</property>
<signal name="activate" handler="on_ITEM2_activate" swapped="no"/>
<child type="submenu">
<object class="GtkMenu" id="menu2">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkMenuItem" id="submenuitem3">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">Item_2_1</property>
<property name="label" translatable="yes">menuitem3</property>
<property name="use-underline">True</property>
<signal name="activate" handler="on_submenuitem3_activate" swapped="no"/>
</object>
</child>
<child>
<object class="GtkMenuItem" id="submenuitem4">
<property name="name">submenuitem4</property>
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="tooltip-text" translatable="yes">Item_2_2</property>
<property name="label" translatable="yes">menuitem4</property>
<property name="use-underline">True</property>
<signal name="activate" handler="on_submenuitem4_activate" swapped="no"/>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<object class="GtkWindow" id="test_win">
<property name="width-request">300</property>
<property name="height-request">300</property>
<property name="can-focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">76</property>
<child>
<object class="GtkMenuButton" id="menubutton1">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="popup">menu1</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="halign">center</property>
<property name="valign">baseline</property>
<signal name="clicked" handler="on_QUIT_BTN_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
After upgrade the gtk lib to 3.24.28 version. This problem was solved.
I'm having a simple dialog with a table with 2 columns, one of which is a check box. I have added a GtkCellRendererToggle but that one is not clickable. I have added a onToggle-function which is never called, it seems.
The cell renderer is activatable and I have added a call to activate it a second time in the C code to be on the safe side but nothing helps. What am I doing wrong?
Here's the code:
void
on_button_delete_files_click (GtkTreeSelection * widget, gpointer user_data)
{
GtkDialog * dialog = NULL;
GtkWidget * file_choser;
char * msg = NULL;
GtkListStore * liststore = NULL;
GtkTreeIter iter;
GDir * dir = NULL;
char * filename = NULL;
if (!builder_delete_files_dialog) {
GError * error = NULL;
builder_delete_files_dialog = gtk_builder_new ();
if (!gtk_builder_add_from_file (builder_delete_files_dialog, "deletedlg.glade", &error)) {
g_warning ("%s", error->message);
g_free (error);
exit (1);
}
}
//
// Get list store
//
liststore = GTK_LIST_STORE (gtk_builder_get_object (builder_delete_files_dialog, "podcast_file_liststore"));
//gtk_list_store_clear (liststore);
//
// Add all files
//
dir = g_dir_open (download_directory, 0, NULL);
if (NULL == dir) {
}
while (NULL != (filename = g_dir_read_name (dir))) {
if (NULL == g_strrstr (filename, ".mp3"))
continue;
gtk_list_store_append(liststore, &iter);
gtk_list_store_set (liststore, &iter,
TO_BE_DELETED, FALSE,
FILENAME, filename,
-1);
}
//
// nice try since stuff is not activatable
//
GtkCellRendererToggle * toggle = GTK_CELL_RENDERER_TOGGLE(gtk_builder_get_object (builder_delete_files_dialog, "cellrenderertoggle1"));
gtk_cell_renderer_toggle_set_activatable (toggle, TRUE);
dialog = GTK_WIDGET(gtk_builder_get_object (builder_delete_files_dialog, "dialog_delete_files"));
int rc = gtk_dialog_run (GTK_DIALOG(dialog));
switch (rc) {
case GTK_RESPONSE_OK:
case GTK_RESPONSE_CANCEL:
default:
break;
}
gtk_widget_hide (dialog);
__error:
FREE_POINTER(g_dir_close, dir);
}
void on_toggle_renderer_click (GtkCellRendererToggle * cell, char * tree_path, gpointer data)
{
g_print ("=====> Dingens clicked !!! <=====\n");
GtkTreeModel *model = (GtkTreeModel *)data;
GtkTreeIter iter;
GtkTreePath *path = gtk_tree_path_new_from_string (tree_path);
gboolean fixed;
/* get toggled iter */
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, TO_BE_DELETED, &fixed, -1);
/* do something with the value */
fixed ^= 1;
/* set new value */
gtk_list_store_set (GTK_LIST_STORE (model), &iter, TO_BE_DELETED, fixed, -1);
/* clean up */
gtk_tree_path_free (path);
}
And here's the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkImage" id="imageApply">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-apply</property>
</object>
<object class="GtkImage" id="imageDelete">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-delete</property>
</object>
<object class="GtkListStore" id="podcast_file_liststore">
<columns>
<!-- column-name TO_BE_DELETED -->
<column type="gboolean"/>
<!-- column-name FILENAME -->
<column type="gchararray"/>
<!-- column-name < -->
<column type="< Neue Spalte definieren >"/>
</columns>
</object>
<object class="GtkDialog" id="dialog_delete_files">
<property name="width_request">320</property>
<property name="height_request">300</property>
<property name="can_focus">False</property>
<property name="resizable">False</property>
<property name="type_hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox1">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area1">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="btnDelete">
<property name="label" translatable="yes">Delete Selected</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">imageDelete</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnClose">
<property name="label" translatable="yes">Close</property>
<property name="name">btnClose</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">imageApply</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="file_treeview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="vexpand">True</property>
<property name="model">podcast_file_liststore</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection2"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title" translatable="yes">Delete</property>
<child>
<object class="GtkCellRendererToggle" id="cellrenderertoggle1">
<signal name="toggled" handler="on_toggle_renderer_click" object="podcast_file_liststore" swapped="no"/>
</object>
<attributes>
<attribute name="active">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn2">
<property name="title" translatable="yes">File name</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-5">btnDelete</action-widget>
<action-widget response="-6">btnClose</action-widget>
</action-widgets>
</object>
</interface>
Again: The onToggle-Event handler is not being called when I click a check box. This makes me think that the cell is somehow not active.
Many thanks for every hint.
Greets
Kai
I got it working by making the GtkCellRendererToggle 'activatable' - ie adding the 'activatable' property with value True. – alexmurray
I found this question when searching for how to toggle the checkboxes in a GtkTreeView.
For those in the same boat, clicking on the checkbox in the rendered tree view will not automatically toggle the box, but it will emit the "toggled" signal.
Connect this signal to a callback function and in that function do whatever functionality should happen when the row is toggled.
/* Toggle callback */
void toggle_row(GtkCellRendererToggle *cell_render, gchar *path,
gpointer tree_view)
{
GtkTreeModel *model;
GtkTreeIter iter;
GValue toggle_state = G_VALUE_INIT;
g_value_init(&toggle_state, G_TYPE_INT);
model = gtk_tree_view_get_model(tree_view);
if(gtk_tree_model_get_iter_from_string(model, &iter, path))
{
if(gtk_cell_renderer_toggle_get_active(cell_renderer))
{
printf("Active\n");
g_value_set_int(&toggle_state, 0);
gtk_tree_store_set_value(GTK_TREE_STORE(model), &iter, IN_COL_TICK,
&toggle_state);
}
else
{
printf("Inactive\n");
g_value_set_int(&toggle_state, 1);
gtk_tree_store_set_value(GTK_TREE_STORE(model), &iter, IN_COL_TICK,
&toggle_state);
}
}
}
/* In the tree view creation function */
{
...
GtkTreeStore *tree_store;
GtkWidget *tree_view;
GtkCellRenderer *render;
GtkTreeViewColumn *column;
...
tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(tree_store));
render = gtk_cell_renderer_toggle_new();
column = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start(column, render, FALSE);
/* connect the active signal to the column in your model that stores
the checkbox state */
gtk_tree_view_column_add_attribute(column, render, "active",
CHECKBOX_COLUMN);
/* Give the callback the tree view
g_signal_connect(render, "toggled", G_CALLBACK(toggle_row), tree_view);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
...
}
I notice that Glade only allows you to set an object to be passed within the user data part of a GTK callback.
Is there any way I can pass an integer value instead?
I have a set of menu items which I would like to point at the same callback function, however for a small section of the code I need to identify which menu item was the one which called the callback.
Note:
All my signals are setup automatically with glade_xml_signal_autoconnect, and I would perfer to keep it this way.
In my opinion with respect to glade and callbacks one have to say goodbye to the notion of passing just one single element to callbacks. And thus stop using glade in order to configure the user data that is passed to a specific callback.
I get used to pass a struct named App to all callbacks that contains a pointer to every ui element that is loaded from the ui definitions file and already instantiated by GtkBuilder. This also stops the tedious task in glade of clicking back and forth between widgets only to set up the user data of the callbacks. In addition to the ui elements, most of the time this struct contains further elements that are important at runtime on application level.
A benefit of this approach is that you are not tempted to think about how to implement an individual callback that should act upon more than one element which is often the case. Some people tackle this by grouping the widgets of interest into a container. In order to pass all widgets to the callback they just pass the container. Then in the callback they get the widgets by calling gtk_container_get_children or similar functions. This approach makes callbacks illegible and reduce the fun while editing the code.
If every callback has all elements available that should be manipulated at runtime you don't have to care about implementing a single callback since every callback shares the same structure.
Further i created a helper macro that defines a pointer to an already instantiated element with the name of its glade id. This way the elements definitions in the code are always in sync with those that are displayed in glade. That makes renaming of widgets very easy (substitute the name in all sources + the glade file).
To illustrate this approach i have appended files of a sample program. Don't be scared of the number of files. Dividing a program into logical units/modules makes programming much simpler. To get a quick view of the whole project i created a git repository on github:
https://github.com/o8i12398z12h9h/gtk-sample-app
Just compare my callbacks.c with your callback file(s). I would be interested to know how they compare with respect to legibility and structure and taking into account that you may have the element-ids from glade still in mind.
callbacks.c:
#include "app.h"
void
button1_clicked_cb (GtkButton * button, App * app)
{
GET_UI_ELEMENT (GtkEntry, entry1);
if (gtk_entry_get_text_length (entry1) == 0)
gtk_entry_set_text (entry1, "test");
else
gtk_entry_set_text (entry1, "");
}
void
button2_clicked_cb (GtkButton * button, App * app)
{
gboolean active;
GET_UI_ELEMENT (GtkSpinner, spinner1);
GET_UI_ELEMENT (GtkWidget, eventbox1);
g_object_get (G_OBJECT (spinner1), "active", &active,
NULL);
if (active) {
gtk_spinner_stop (spinner1);
gtk_widget_override_background_color (eventbox1,
GTK_STATE_FLAG_NORMAL,
app->
active_color);
}
else {
gtk_spinner_start (spinner1);
gtk_widget_override_background_color (eventbox1,
GTK_STATE_FLAG_NORMAL,
app->
inactive_color);
}
}
void
button3_clicked_cb (GtkButton * button, App * app)
{
GdkRGBA bg = { 0, 0, 1, 1 };
GET_UI_ELEMENT (GtkWidget, eventbox1);
gtk_widget_override_background_color (eventbox1,
GTK_STATE_FLAG_NORMAL,
&bg);
}
void
button4_clicked_cb (GtkButton * button, App * app)
{
const gchar *str;
GET_UI_ELEMENT (GtkLabel, label1);
str = gtk_label_get_text (label1);
if (strcmp (str, "label") == 0) {
gtk_label_set_text (label1, "NewText");
}
else {
gtk_label_set_text (label1, "label");
}
}
void
button5_clicked_cb (GtkButton * button, App * app)
{
GET_UI_ELEMENT (GtkWidget, button1);
GET_UI_ELEMENT (GtkWidget, button2);
GET_UI_ELEMENT (GtkWidget, button4);
g_signal_emit_by_name (button1, "clicked", app);
g_signal_emit_by_name (button2, "clicked", app);
g_signal_emit_by_name (button4, "clicked", app);
}
main.c:
#include "app.h"
int
main (int argc, char *argv[])
{
App *app;
app = (App *) g_new (App, 1);
gtk_init (&argc, &argv);
app_init (app);
GET_UI_ELEMENT (GtkWidget, window1);
gtk_widget_show_all (window1);
gtk_main ();
return 0;
}
app.c:
#include "app.h"
GObject *
app_get_ui_element (App * app, const gchar * name)
{
const gchar *s;
GSList *list;
list = app->objects;
do {
s = gtk_buildable_get_name (list->data);
if (strcmp (s, name) == 0) {
return list->data;
}
} while (list = g_slist_next (list));
return NULL;
}
void
app_init_colors (App * app)
{
GdkRGBA active_color = { 1, 0, 0, 1 };
GdkRGBA inactive_color = { 0, 1, 0, 1 };
app->active_color = g_new0 (GdkRGBA, 1);
app->inactive_color = g_new0 (GdkRGBA, 1);
app->active_color = gdk_rgba_copy (&active_color);
app->inactive_color = gdk_rgba_copy (&inactive_color);
}
void
app_init (App * app)
{
GError *err = NULL;
app->definitions = gtk_builder_new ();
gtk_builder_add_from_file (app->definitions,
UI_DEFINITIONS_FILE, &err);
if (err != NULL) {
g_printerr
("Error while loading app definitions file: %s\n",
err->message);
g_error_free (err);
gtk_main_quit ();
}
gtk_builder_connect_signals (app->definitions, app);
app->objects = gtk_builder_get_objects (app->definitions);
app_init_colors (app);
}
app.h:
#ifndef __APP__
#define __APP__
#include <gtk/gtk.h>
#define UI_DEFINITIONS_FILE "ui.glade"
#define GET_UI_ELEMENT(TYPE, ELEMENT) TYPE *ELEMENT = (TYPE *) \
app_get_ui_element(app, #ELEMENT);
typedef struct app_
{
GtkBuilder *definitions;
GSList *objects;
GdkRGBA *active_color;
GdkRGBA *inactive_color;
} App;
void app_init (App * );
GObject * app_get_ui_element (App * , const gchar * );
#endif
ui.glade:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="border_width">20</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="row_spacing">10</property>
<property name="column_spacing">20</property>
<child>
<object class="GtkSpinner" id="spinner1">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkEventBox" id="eventbox1">
<property name="height_request">50</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">2</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
<property name="invisible_char_set">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">3</property>
<property name="width">2</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkButtonBox" id="buttonbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">10</property>
<property name="layout_style">center</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">toggle entry</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="button1_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">toggle spinner + bg</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="button2_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button3">
<property name="label" translatable="yes">set bg</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="button3_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button4">
<property name="label" translatable="yes">toggle label</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="button4_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button5">
<property name="label" translatable="yes">toggle everything</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="button5_clicked_cb" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">5</property>
<property name="width">4</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkSeparator" id="separator1">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">4</property>
<property name="width">4</property>
<property name="height">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>
I've been teaching myself how to code in C and utilize GTK for writing applications in Linux, using Glade to design the UI before implementing it. I've been trying to utilize a GtkTreeView coupled with a GtkListStore to display data, but I seem to be running into issues. I can get my application to launch fine and it shows the window and dialog correctly, but I cannot figure out how to add rows of data to the List Store programmatically and have them appear in the Tree View; most tutorials I find seem to prefer to make the rows in Glade, or to design the whole interface through the code. With the code as it is I receive no errors when running the program, but I cannot see anything being added in GtkTreeView. I don't see any blank rows being added either.
By writing some code to forcibly generate an error, I can see that the signal for postingButton is being connected (I think) so that should be fine. I'm just missing something in my actual implementation of GtkTreeView. If anyone can help me out, I'd greatly appreciate it!
Edit: The GtkEntries that are present will be implemented later; I'm just trying to get data to show up at all.
C Code:
/* I'd give objects better names normally, no worries!
* Just goofing around for now.*/
#include <gtk-3.0/gtk/gtk.h> // Needed for interface.
#include <gtk-3.0/gdk/gdk.h>
#include <glib-2.0/glib-object.h>
void on_mainWindow_destroy(GObject *object, gpointer user_data)
{
gtk_main_quit();
}
void on_stupidButton_clicked(GtkButton *button, gpointer *user_data)
{
gtk_dialog_run(GTK_DIALOG(user_data));
}
void on_postingButton_clicked(GtkButton *button, gpointer *user_data)
{
GtkListStore *liststore1;
GtkWidget *treeview1; // I've tried defining this as a GtkTreeView as well.
GtkTreeIter iter;
GtkBuilder *listStoreBuilder;
listStoreBuilder = gtk_builder_new();
gtk_builder_add_from_file(listStoreBuilder,
"../testing_interface.glade", 0);
treeview1 = GTK_WIDGET(gtk_builder_get_object(listStoreBuilder,
"treeview1")); // I'd change this to GTK_TREE_VIEW if the
// above is a GtkTreeView.
liststore1 = GTK_LIST_STORE(
gtk_tree_view_get_model((GtkTreeView *)treeview1));
gtk_list_store_append(liststore1, &iter);
gtk_list_store_set(liststore1, &iter, 0, "c", 1, "d", -1);
gtk_builder_connect_signals(listStoreBuilder, 0);
g_object_unref(G_OBJECT(listStoreBuilder));
g_object_unref(G_OBJECT(liststore1));
}
void on_cancelButton_clicked(GtkButton *button, gpointer *user_data)
{
gtk_widget_destroy((GtkWidget *)user_data);
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *mainWindow, *dumbDialog;
GtkButton *stupidButton;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "../testing_interface.glade",
0);
mainWindow = GTK_WIDGET(gtk_builder_get_object(builder, "mainWindow"));
stupidButton = GTK_BUTTON(gtk_builder_get_object(builder, "stupidButton"));
dumbDialog = GTK_WIDGET(gtk_builder_get_object(builder, "dumbDialog"));
gtk_builder_connect_signals(builder, 0);
g_object_unref(G_OBJECT(builder));
gtk_widget_show(mainWindow);
gtk_main();
return 0;
}
Glade File (testing_interface.glade):
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkDialog" id="dumbDialog">
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="type_hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox1">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area1">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="postingButton">
<property name="label" translatable="yes">Post</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="on_postingButton_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="cancelButton">
<property name="label" translatable="yes">Cancel</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="on_cancelButton_clicked" object="dumbDialog" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="0">postingButton</action-widget>
<action-widget response="0">cancelButton</action-widget>
</action-widgets>
</object>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name Testing -->
<column type="gchararray"/>
<!-- column-name Testing1 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="mainWindow">
<property name="can_focus">False</property>
<signal name="destroy" handler="on_mainWindow_destroy" swapped="no"/>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="height_request">183</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="testing1Col">
<property name="title" translatable="yes">column</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="testing2Col">
<property name="title" translatable="yes">column</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext2"/>
<attributes>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="stupidButton">
<property name="label" translatable="yes">button</property>
<property name="use_action_appearance">False</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_action_appearance">False</property>
<signal name="clicked" handler="on_stupidButton_clicked" object="dumbDialog" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
Your code that appends rows to the tree view is basically okay, but several problems prevent it from working.
You are creating and populating a new builder on each entry to on_postingButton_clicked. This is obviously incorrect, because it means that you're creating a new tree and a new store on each click of the Post button. This is easily fixed by sending treeview1 as user_data to the on_postingButton_clicked callback. You do that already in dialog, so I simply modified <signal name="clicked" handler="on_postingButton_clicked" ...> in your XML to also include object="treeview1".
You are being too liberal with g_object_unref. gtk_tree_view_get_model doesn't increase the refcount of its model, so you shouldn't unref the liststore you get. Unrefing the builder in main is probably a bad idea if you want to pass it around and retrieve objects from it (and it's a singleton anyway).
You're supposed to #include <gtk/gtk.h>, without the intervening gtk-3.0 directory. If your compilation fails without that, you are probably not setting up pkg-flags correctly.
Here is a modified version of your code that works for me. It requires XML to be modified as described above.
/* compile with:
gcc -O2 -Wall glade-test.c $(pkg-config gtk+-3.0 gmodule-export-2.0 --cflags --libs) */
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib-object.h>
void on_mainWindow_destroy(GObject *object, gpointer user_data)
{
gtk_main_quit();
}
void on_stupidButton_clicked(GtkButton *button, gpointer *user_data)
{
gtk_dialog_run(GTK_DIALOG(user_data));
}
void on_postingButton_clicked(GtkButton *button, gpointer *user_data)
{
GtkTreeIter iter;
GtkTreeView *treeview1 = GTK_TREE_VIEW(user_data);
GtkListStore *liststore1 = GTK_LIST_STORE(gtk_tree_view_get_model(treeview1));
gtk_list_store_append(liststore1, &iter);
gtk_list_store_set(liststore1, &iter, 0, "c", 1, "d", -1);
}
void on_cancelButton_clicked(GtkButton *button, gpointer *user_data)
{
gtk_widget_destroy((GtkWidget *)user_data);
}
int main(int argc, char *argv[])
{
GtkBuilder *builder;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "testing_interface.glade", 0);
gtk_builder_connect_signals(builder, 0);
gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(builder, "mainWindow")));
g_object_unref(G_OBJECT(builder));
gtk_main();
return 0;
}