GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Oct 22, 2014 1:37 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Adding and removing widgets
PostPosted: Mon Apr 22, 2013 11:04 am 
Offline

Joined: Mon Apr 22, 2013 12:13 am
Posts: 2
Hi everyone,

My first post here, because I've got a question with my first GTK project while still relatively new to the C programming language. I've been stumped for a while and was hoping I could be pointed in the right direction.

Basically, I'm trying to build a to-do list applet using the GTK reference manual as guidelines on how the framework works. I was able to setup the style of the app, but now I'm stumped while trying to move things around. Here is a photo for reference:

Image

When I click on the Add button, it adds a new hbox with the two widgets inside. When I click on any Clear button, it removes the last entry in the list the first time and the app resizes correctly. However, on the second clear, the hbox doesn't get removed but the app still shrinks.

I tried to compress the code to contain the essentials:
Code:
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

static void draw_window(void), text_box(void), add_button(void), remove_button(void), new_entry(void), remove_entry(void), entry_toggle_editable(void);
static void enter_callback(GtkWidget *widget,
                           GtkWidget *entry );
static void h_container(void);
static void v_container(void);


gchar const *title = "ToDo";
int widget_length = 250;
int widget_height = 100;
static GtkWidget *window;
extern GtkWidget *vbox, *hbox;
static GtkWidget *entry;
static GtkWidget *button_add, *button_remove;
static GtkWidget *checkbox;
static gint tmp_pos;

int main(int argc, char *argv[] )
{

    gtk_init (&argc, &argv);
   
    draw_window();
    v_container();
    menu();
    h_container();
    toggle();
    add_button();
    h_container();
    text_box();
    remove_button();
    /*debug*/
    new_entry();
    new_entry();
   
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}


// Create a new window
static void draw_window(void)
{
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("images/logo_256.png", NULL);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_widget_set_size_request (GTK_WIDGET (window), widget_length,
                                 widget_height);
    gtk_window_set_resizable (GTK_WINDOW(window), false);
    gtk_window_set_title (GTK_WINDOW(window), title);
    gtk_window_set_icon(GTK_WINDOW(window), pixbuf);
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect_swapped (window, "delete-event",
                              G_CALLBACK (gtk_widget_destroy), window);
}


// Text entry
static void text_box(void)
{
    entry = gtk_entry_new ();
    gtk_entry_set_max_length (GTK_ENTRY (entry), 50);
    g_signal_connect (entry, "activate", G_CALLBACK (enter_callback), entry);
    gtk_entry_set_text (GTK_ENTRY (entry), "new");
    tmp_pos = GTK_ENTRY (entry)->text_length;
    gtk_editable_insert_text (GTK_EDITABLE (entry), " entry", -1, &tmp_pos);
    gtk_editable_select_region (GTK_EDITABLE (entry), 0,
                                GTK_ENTRY (entry)->text_length);
    gtk_box_pack_start (GTK_BOX (hbox), entry, true, true, 0);
    gtk_widget_show (entry);
}


// Add button
static void add_button(void)
{
    button_add = gtk_button_new_from_stock (GTK_STOCK_ADD);
    gtk_widget_set_size_request(button_add, 70, 30);
    g_signal_connect_swapped (button_add, "clicked",
                           G_CALLBACK (new_entry), window);
   gtk_box_pack_start (GTK_BOX (hbox), button_add, true, true, 0);
   gtk_widget_set_can_default (button_add, true);
   gtk_widget_grab_default (button_add);
   gtk_widget_show (button_add);

}


// Remove button
static void remove_button(void)
{
    button_remove = gtk_button_new_from_stock (GTK_STOCK_CLEAR);
    gtk_widget_set_size_request(button_remove, 70, 30);
    g_signal_connect_swapped (button_remove, "clicked",
                          G_CALLBACK (remove_entry), window);
   gtk_box_pack_start (GTK_BOX (hbox), button_remove, true, true, 0);
   gtk_widget_set_can_default (button_remove, true);
   gtk_widget_grab_default (button_remove);
   gtk_widget_show (button_remove);

}


// Increase widget size and add text box upon "Add"
static void new_entry(void)
{
    widget_height += 34;
    gtk_window_set_resizable (GTK_WINDOW(window), true);
    gtk_widget_set_size_request (GTK_WIDGET (window), widget_length,
                                 widget_height);
    gtk_window_set_resizable (GTK_WINDOW(window), false);
    h_container();
    text_box();
    remove_button();
}

// Remove entry/button and decrease widget size
static void remove_entry(void)
{
   gtk_widget_destroy(entry);
    gtk_widget_destroy(button_remove);
    gtk_widget_destroy(hbox);
    widget_height -= 34;
    gtk_window_set_resizable (GTK_WINDOW(window), true);
    gtk_widget_set_size_request (GTK_WIDGET (window), widget_length,
                                 widget_height);
    gtk_window_set_resizable (GTK_WINDOW(window), false);
}


// Horizontal container
static void h_container(void)
{
    hbox = gtk_hbox_new (false, 0);
    gtk_container_add (GTK_CONTAINER (vbox), hbox);
    gtk_widget_show (hbox);
}


// Vertical container
static void v_container(void)
{
    vbox = gtk_vbox_new (false, 0);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_widget_show (vbox);
}


I'm presuming that I would need to find a way of keeping track of the position of each entry with a sort of index and refer to that to add and clear the entries, but I don't understand how to implement something like this with the framework.

Any help pointing me in the right direction would be much appreciated, thanks!


Top
 Profile  
 
 Post subject: Re: Adding and removing widgets
PostPosted: Wed Apr 24, 2013 9:24 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hello and welcome,

You do not say what version of GTK+ you are using. By the looks of it it is a very old version of GTK+ 2.xx and you are using structure members that were removed or deprecated a very long time ago.

If you can it would be best to upgrade to version 3.xx or if you have to stay with GTK+ 2 use 2.24.

Looking at what you are trying to do it may be better to use a GtkTreeView and a GtkListStore. This is a way to store all your information in a list. It would be quicker and in the long term easier to maintain. It does take some learning to use it, but the benefits once you have learnt it are great.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Adding and removing widgets
PostPosted: Wed Apr 24, 2013 11:45 am 
Offline

Joined: Mon Apr 22, 2013 12:13 am
Posts: 2
Thanks for your reply!

I'll look into GtkTreeView and GtkListStore and see as to implement these.

I thought I was using GTK3+ though, as I'm building on an up-to-date Fedora 18 and have been referencing myself quite a bit to the GTK reference manual. What would be the best way to upgrade to version 3 appropriately?

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group