GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 31, 2014 2:22 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Text Entry
PostPosted: Sun May 27, 2007 6:21 am 
Offline
Familiar Face

Joined: Fri May 25, 2007 8:36 am
Posts: 13
Location: Iran
I am writing a calculator program and I have a basic problem. I have written callback function for 2 button and sent one entry and one value to callback using struct but now I have problem with functionvoid hi(GtkWidget *widget,gpointer data).i want if any button is pressed its corresponding character be written in the entry but now I don’t know what code I can write that send b1->num=X for each button you can compile and run the program to see where is my problem.
I appreciate from all friends who will help me.
Code:
#include <gtk/gtk.h>

#include <iostream>

#include <string>

#include <cctype>

struct hamid{
GtkWidget *entry;

const gchar *num;

}*b1;

void hi(GtkWidget *widget,gpointer data){

gtk_entry_set_text(GTK_ENTRY(b1->entry),b1->num);

}

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

gtk_init(&argc,&argv);

GtkWidget *window;

GtkWidget *table;

GtkWidget *button1;

GtkWidget *button2;

GtkWidget *button3;

GtkWidget *button4;

GtkWidget *button5;

GtkWidget *button6;

GtkWidget *button7;

GtkWidget *button8;

GtkWidget *button9;

GtkWidget *button10;

GtkWidget *button11;

GtkWidget *button12;

GtkWidget *button13;

GtkWidget *button14;

GtkWidget *button15;

GtkWidget *button16;

window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

table=gtk_table_new(5,4,TRUE);

b1=(hamid*)g_malloc(sizeof(hamid));

b1->entry=gtk_entry_new();

b1->num="1";

button1=gtk_button_new_with_label("1");

button2=gtk_button_new_with_label("2");

button3=gtk_button_new_with_label("3");

button4=gtk_button_new_with_label("4");

button5=gtk_button_new_with_label("5");

button6=gtk_button_new_with_label("6");

button7=gtk_button_new_with_label("7");

button8=gtk_button_new_with_label("8");

button9=gtk_button_new_with_label("9");

button10=gtk_button_new_with_label("0");

button11=gtk_button_new_with_label("=");

button12=gtk_button_new_with_label("+");

button13=gtk_button_new_with_label("-");

button14=gtk_button_new_with_label("/");

button15=gtk_button_new_with_label("*");

button16=gtk_button_new_with_label(".");

gtk_window_set_resizable(GTK_WINDOW(window),FALSE);

gtk_container_add(GTK_CONTAINER(window),table);

gtk_table_attach_defaults(GTK_TABLE(table),b1->entry,1,3,0,1);

gtk_table_attach_defaults(GTK_TABLE(table),button1,0,1,1,2);

gtk_table_attach_defaults(GTK_TABLE(table),button2,1,2,1,2);

gtk_table_attach_defaults(GTK_TABLE(table),button3,2,3,1,2);

gtk_table_attach_defaults(GTK_TABLE(table),button4,3,4,1,2);

gtk_table_attach_defaults(GTK_TABLE(table),button5,0,1,2,3);

gtk_table_attach_defaults(GTK_TABLE(table),button6,1,2,2,3);

gtk_table_attach_defaults(GTK_TABLE(table),button7,2,3,2,3);

gtk_table_attach_defaults(GTK_TABLE(table),button8,3,4,2,3);

gtk_table_attach_defaults(GTK_TABLE(table),button9,0,1,3,4);

gtk_table_attach_defaults(GTK_TABLE(table),button10,1,2,3,4);

gtk_table_attach_defaults(GTK_TABLE(table),button11,2,3,3,4);

gtk_table_attach_defaults(GTK_TABLE(table),button12,3,4,3,4);

gtk_table_attach_defaults(GTK_TABLE(table),button13,0,1,4,5);

gtk_table_attach_defaults(GTK_TABLE(table),button14,1,2,4,5);

gtk_table_attach_defaults(GTK_TABLE(table),button15,2,3,4,5);

gtk_table_attach_defaults(GTK_TABLE(table),button16,3,4,4,5);

g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(hi),(gpointer)b1);

b1->num="2";

g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(hi),(gpointer)b1);

gtk_widget_show(window);
gtk_widget_show(table);
gtk_widget_show(button1);

gtk_widget_show(button2);
gtk_widget_show(button3);

gtk_widget_show(button4);
gtk_widget_show(button5);

gtk_widget_show(button6);
gtk_widget_show(button7);

gtk_widget_show(button8);
gtk_widget_show(button9);

gtk_widget_show(button10);
gtk_widget_show(button11);

gtk_widget_show(button12);
gtk_widget_show(button13);

gtk_widget_show(button14);
gtk_widget_show(button15);

gtk_widget_show(button16);
gtk_widget_show(b1->entry);

gtk_main();

return 0;

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 1:02 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
I provide the code to the calculator application for my book at http://www.gtkbook.com/release.php. You should check it out for some ideas.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


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

All times are UTC


Who is online

Users browsing this forum: Google Adsense [Bot], newguy, Yahoo [Bot] and 2 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