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 01, 2014 8:11 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Program correction
PostPosted: Wed Feb 01, 2012 10:47 am 
Offline
Familiar Face

Joined: Wed Feb 01, 2012 10:39 am
Posts: 7
Hello..
I am developing a Graphical User Interface using GTK+2.0. As part of our project I need to take inputs from GTK entries and pass those inputs to shell script and use them in shell script. The problem which struck me is only limited number of values are being taken as input. For now only 15 characters are being accepted. If I enter more than 15 characters it's printing garbage values..I am unable to figure it out..

GTK program is:
Code:
#include<gtk/gtk.h>
#include<stdio.h>
#include<stdlib.h>
gchar *chardadd1=NULL,*chardadd2=NULL;

void dadd(GtkWidget *widget, gpointer label)
{
char str[512];
int x;
sprintf(str,"sh post1.sh %s ",chardadd1);
x=system(str);
}
int main(int argc,char *argv[])
{
int i;char buf[128];


  GtkWidget *window;
  GtkWidget *tabledadd;
  GtkWidget *labeldadd1;
  GtkWidget *entrydadd1;
  GtkWidget *buttondadd;
  gtk_init(&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_title(GTK_WINDOW(window), "GtkEntry");

  gtk_container_set_border_width(GTK_CONTAINER(window), 50);
entrydadd1 = gtk_entry_new();
tabledadd = gtk_table_new(3, 2, FALSE);
gtk_container_add(GTK_CONTAINER(window), tabledadd);
labeldadd1 = gtk_label_new("group name");
buttondadd=gtk_button_new_with_label("click me");
gtk_table_attach(GTK_TABLE(tabledadd), labeldadd1, 0, 1, 0, 1,
  GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
  gtk_table_attach(GTK_TABLE(tabledadd), entrydadd1, 1, 2, 0, 1,
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);

   gtk_table_attach(GTK_TABLE(tabledadd), buttondadd, 0, 2, 2, 3,
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);

chardadd1=gtk_entry_get_text(GTK_ENTRY(entrydadd1));


gtk_widget_show(window);
  gtk_widget_show(tabledadd);

  gtk_widget_show(labeldadd1);
  gtk_widget_show(entrydadd1);
  gtk_widget_show(buttondadd);
  g_signal_connect(buttondadd, "clicked",G_CALLBACK(dadd), entrydadd1);
  g_signal_connect(window, "destroy",G_CALLBACK(gtk_main_quit), NULL);

  gtk_main();

  return 0;
}


Top
 Profile  
 
 Post subject: Re: Program correction
PostPosted: Wed Feb 01, 2012 11:52 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums!

I'm not sure by what coincidence do you get any character printed at all. You're storing content of your GtkEntry even before you have a chance to input something.

I created a sample application that demonstrates the general approach usually taken by this kind of applications. Have a good look at how and when entry contents is being consumed.

Code:
#include <gtk/gtk.h>

#define N_INPUTS 3

typedef struct
{
  GtkEntry *inputs[N_INPUTS];
}
Data;

static void
cb_click (GtkButton *button,
          Data      *data)
{
  int i;

  for (i = 0; i < N_INPUTS; i++)
    g_print ("Input %d: \"%s\"\n",
             i + 1, gtk_entry_get_text (data->inputs[i]));
}


int
main (int    argc,
      char **argv)
{
  GtkWidget *window,
            *vbox,
            *entry,
            *button;
  Data *data;
  int i;

  gtk_init (&argc, &argv);

  data = g_slice_new0 (Data);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);

  vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  for (i = 0; i < N_INPUTS; i++)
    {
      entry = gtk_entry_new ();
      gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
      data->inputs[i] = GTK_ENTRY (entry);
    }

  button = gtk_button_new_with_label ("Print contents");
  g_signal_connect (button, "clicked", G_CALLBACK (cb_click), (gpointer)data);
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}


Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: Program correction
PostPosted: Wed Feb 01, 2012 12:00 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Try out gdb for debugging it should help narrow down whether the error is occurring in your code up to the string sent to the system call or whether your shell script can't handle that much input.
By the way use glib's sring function e.g. g_sprintf. Makes it more portable and avoids a few issues.


Top
 Profile  
 
 Post subject: Re: Program correction
PostPosted: Tue Feb 07, 2012 1:32 pm 
Offline
Familiar Face

Joined: Wed Feb 01, 2012 10:39 am
Posts: 7
Thank you tadeboro.. I corrected my code.. It's working 5n nw..
Thank you Paul childs.. :-)


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 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