GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

regarding buttons and signals

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 201
Location: INDIA

PostPosted: Mon Sep 01, 2008 5:48 am    Post subject: regarding buttons and signals Reply with quote

hi all

initially a window contains a button name as orgin,
if we click the "origin"
a button should be create in table(X,Y)
and again
if we click on the "origin"
second button should be create in the same table(X,Y).
like this we should create 10 to 20 buttons in that table.

and every new created button should have a signal.
EX:
if we click on the 1st button it should print "this is the 1st button".....
....
like that

is there any way to do this.

any help it should be appreciable.
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 643
Location: Falun, WI USA

PostPosted: Tue Sep 02, 2008 2:43 am    Post subject: Reply with quote

I don't really understand what you want to accomplish, do you want to have a button created and packed where you click?
Back to top
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 201
Location: INDIA

PostPosted: Tue Sep 02, 2008 4:25 am    Post subject: Reply with quote

dreblen wrote:
I don't really understand what you want to accomplish, do you want to have a button created and packed where you click?


when i click to add button.
a new button should be created and placed in the table.
like that so on...

and if i click the new created button it should print some text(mean i have to give different signals for different newly created buttons)
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 643
Location: Falun, WI USA

PostPosted: Tue Sep 02, 2008 1:22 pm    Post subject: Reply with quote

like this?
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <gtk/gtk.h>

void table_button_clicked_cb(GtkWidget *but, gchar *text)
{
    g_print("%s\n", text);
}

void add_button_clicked_cb(GtkWidget *but, GtkTable *table)
{
    GtkWidget *button;
    gchar *label_text;
    static gint n = 0;
    static gint r = 0;
    static gint c = 0;

    if(n == 9)
        return;

    label_text = (gchar*)g_malloc(128);
    if(label_text == NULL)
        return;
    sprintf(label_text, "Button %d", ++n);

    button = gtk_button_new_with_label(label_text);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(table_button_clicked_cb), (gpointer)label_text);
    /* you could also do this, but then you couldn't do anything else,
     * and you wouldn't have any other control over the text */
    //g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(g_print), (gpointer)label_text);
   
gtk_table_attach_defaults(table, button, c, c+1, r, r+1);
    gtk_widget_show(button);

    if(c == 2)
    {
        r++;
        c = 0;
    }
    else
       
c++;
}

int main(int argc, char **argv)
{
    GtkWidget *win;
    GtkWidget *vbox;
    GtkWidget *table;
    GtkWidget *button;

    gtk_init(&argc, &argv);

    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(win), 10);

    vbox = gtk_vbox_new(FALSE, 2);
    gtk_container_add(GTK_CONTAINER(win), vbox);
    gtk_widget_show(vbox);

    table = gtk_table_new(3, 3, TRUE);
    gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
    gtk_widget_show(table);

    button = gtk_button_new_with_label("Add button");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(add_button_clicked_cb), (gpointer)table);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
    gtk_widget_show(button);

    gtk_widget_show(win);

    gtk_main();

    return 0;
}
Back to top
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 201
Location: INDIA

PostPosted: Thu Sep 04, 2008 4:54 am    Post subject: Reply with quote

dreblen wrote:
like this?
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <gtk/gtk.h>

void table_button_clicked_cb(GtkWidget *but, gchar *text)
{
    g_print("%s\n", text);
}

void add_button_clicked_cb(GtkWidget *but, GtkTable *table)
{
    GtkWidget *button;
    gchar *label_text;
    static gint n = 0;
    static gint r = 0;
    static gint c = 0;

    if(n == 9)
        return;

    label_text = (gchar*)g_malloc(128);
    if(label_text == NULL)
        return;
    sprintf(label_text, "Button %d", ++n);

    button = gtk_button_new_with_label(label_text);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(table_button_clicked_cb), (gpointer)label_text);
    /* you could also do this, but then you couldn't do anything else,
     * and you wouldn't have any other control over the text */
    //g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(g_print), (gpointer)label_text);
   
gtk_table_attach_defaults(table, button, c, c+1, r, r+1);
    gtk_widget_show(button);

    if(c == 2)
    {
        r++;
        c = 0;
    }
    else
       
c++;
}

int main(int argc, char **argv)
{
    GtkWidget *win;
    GtkWidget *vbox;
    GtkWidget *table;
    GtkWidget *button;

    gtk_init(&argc, &argv);

    win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(win), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(win), 10);

    vbox = gtk_vbox_new(FALSE, 2);
    gtk_container_add(GTK_CONTAINER(win), vbox);
    gtk_widget_show(vbox);

    table = gtk_table_new(3, 3, TRUE);
    gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
    gtk_widget_show(table);

    button = gtk_button_new_with_label("Add button");
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(add_button_clicked_cb), (gpointer)table);
    gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
    gtk_widget_show(button);

    gtk_widget_show(win);

    gtk_main();

    return 0;
}


exactly, what i am looking for.
nice work. a ton of thanks..

i need to do some more implementation in this.
1).yes if we clicking on the add button it is creating new buttons,it is perfect.
but if we close and reopen the window, the previously added buttons should be shown.
2).if i select a new added button and right click it should open a popup and the popup should have two labels named as "rename the button" and "delete the button". and these two labels or button should have signals.
if i click "rename the button" the new added button name should be renamed. and it is saved(mean if reopen the window the rename button label should be shown)so on..

i think it is looking as home work.
but i did not find the code and example for renaming the button label.

any help it should be appreciable.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP