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 6:23 am

All times are UTC




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: How to run external app and get the return data?
PostPosted: Thu Jun 12, 2008 2:42 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
In perl-gtk I can run apps and get the return data:
Code:
my $data = `md5sum somefile.deb`;
# text1 is an entry widget
text1->set_text($data);

Is there a function in gtk binary development with the same effect?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 3:59 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Do you mean in C programming? If so, check out Execute command into a GtkTextView (C and Libglade)

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 9:35 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
Thanks..that help..this is my sub final code:
Code:
#include <gtk/gtk.h>

typedef struct {
   GtkWidget *edit1;
   GtkWidget *edit2;
   GtkWidget *filesel;
} gData, *PGDATA;

gData myWidgets = {0};

void OnWindowDestroy(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
}

void OnFileSelCancel(GtkWidget *w, gpointer fs) {
   gtk_widget_destroy(GTK_WIDGET(fs));
}

void OnFileSelOk(GtkWidget *w, gpointer data) {
   gchar *file;
   gchar *command;
   gchar *outmd5;
   
   file = g_strdup_printf("%s", gtk_file_selection_get_filename(GTK_FILE_SELECTION(((PGDATA)data)->filesel)));
   gtk_entry_set_text(GTK_ENTRY(((PGDATA)data)->edit1), file);
   gtk_widget_destroy(GTK_WIDGET(((PGDATA)data)->filesel));
   
   command = g_strdup_printf("md5sum \"%s\"", file);
   
   g_spawn_command_line_sync(command, &outmd5, 0, 0, 0);
   gtk_entry_set_text(GTK_ENTRY(((PGDATA)data)->edit2), outmd5);
   
   g_free(outmd5);
   g_free(command);
   g_free(file);
}

void OnButton1Clicked(GtkWidget *widget, gpointer data) {
   ((gData*)data)->filesel = gtk_file_selection_new("");
   g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(((gData*)data)->filesel)), "destroy", G_CALLBACK(OnFileSelCancel), ((gData*)data)->filesel);
   g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(((gData*)data)->filesel)->cancel_button), "clicked", G_CALLBACK(OnFileSelCancel), ((gData*)data)->filesel);
   g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(((gData*)data)->filesel)->ok_button), "clicked", G_CALLBACK(OnFileSelOk), data);
   gtk_widget_show(((gData*)data)->filesel);
}

int main(int argc, char *argv[]) {
   GtkWidget *Window;
   GtkWidget *panel;
   GtkWidget *label1;
   GtkWidget *part1;
   GtkWidget *button1;
   GtkWidget *label2;
   
   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_default_size(GTK_WINDOW(Window), 350, 120); // This doesn't worlk
   gtk_window_set_resizable(GTK_WINDOW(Window), FALSE);
   gtk_window_set_title(GTK_WINDOW(Window), "md5 - chica");
   g_signal_connect(G_OBJECT(Window), "destroy", G_CALLBACK(OnWindowDestroy), NULL);
   
   panel = gtk_vbox_new(TRUE, 0);
   gtk_container_add(GTK_CONTAINER(Window), panel);
   
   label1 = gtk_label_new("Ingresa el fichero a obtener MD5:");
   gtk_box_pack_start(GTK_BOX(panel), label1, TRUE, TRUE, 0);
   
   part1 = gtk_hbox_new(TRUE, 0);
   gtk_box_pack_start(GTK_BOX(panel), part1, TRUE, TRUE, 0);
   
   myWidgets.edit1 = gtk_entry_new();
   gtk_box_pack_start(GTK_BOX(part1), myWidgets.edit1, TRUE, TRUE, 0);
   
   button1 = gtk_button_new_from_stock("gtk-find");
   g_signal_connect(G_OBJECT(button1), "clicked", G_CALLBACK(OnButton1Clicked), &myWidgets);
   gtk_box_pack_start(GTK_BOX(part1), button1, TRUE, FALSE, 0);
   
   label2 = gtk_label_new("Salida:");
   gtk_box_pack_start(GTK_BOX(panel), label2, FALSE, FALSE, 0);
   
   myWidgets.edit2 = gtk_entry_new_with_max_length(32);
   gtk_box_pack_start(GTK_BOX(panel), myWidgets.edit2, FALSE, FALSE, 0);
   
   gtk_widget_show_all(Window);
   gtk_main();
}

I want the window to be 350 X 120 and void the user to resize it, any ideas?
Also ideas to improve my above code are welcome :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 12:11 am 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
you can try using gtk_window_resize and gtk_window_set_resizable:
http://library.gnome.org/devel/gtk/stab ... dow-resize
http://library.gnome.org/devel/gtk/stab ... -resizable
if gtk_window_set_default_size didn't work, you might need to call it after the widget is realized (either by showing it or by calling gtk_widget_realize)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 12:32 am 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
dreblen wrote:
you can try using gtk_window_resize and gtk_window_set_resizable:
http://library.gnome.org/devel/gtk/stab ... dow-resize
http://library.gnome.org/devel/gtk/stab ... -resizable
if gtk_window_set_default_size didn't work, you might need to call it after the widget is realized (either by showing it or by calling gtk_widget_realize)

Yeah, using in this order:
Code:
gtk_window_resize(GTK_WINDOW(Window), 350, 120);
gtk_window_set_resizable(GTK_WINDOW(Window), FALSE);

worked :D
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 1:04 am 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
No, it didn't.. :(

/edit:
But this did:
Code:
gtk_widget_set_size_request(GTK_WIDGET(Window), 450, 120);
gtk_window_set_resizable(GTK_WINDOW(Window), FALSE);


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 2:58 am 
Offline
GTK+ Geek

Joined: Wed May 28, 2008 5:49 pm
Posts: 66
Won't the spawn_sync freeze up for app while it's doing the md5sum? I think a thread and an async is what you're looking for (if you want your app to be always responsive and whatnot).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 5:07 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Yes, as the names imply, using g_spawn_sync your application has to wait for it to finish (synchronous) where as g_spawn_async is asynchronous but can be a bit more confusing to new programmers.

If even more functionality is needed, a GIOChannel can be quite usesful and is worth looking over.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 1:40 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
I have a problem and I don't think that is my substring function :\
My substr function:
Code:
char* substr(const char *str, int start, int end) {
   if (end < 1) end = strlen(str) + 1;
   char *buffer = malloc(end+1);
   strncpy(buffer, str + start, end);
   return buffer;
}

Now the critical part of my code:
Code:
gchar *command;
gchar *outmd5;
char *md5;
command = g_strdup_printf("md5sum \"%s\"", "some_file.deb");
g_spawn_command_line_sync(command, &outmd5, 0, 0, 0);
md5 = substr(outmd5, 0, 32);
gtk_entry_set_text(GTK_ENTRY(somentry), md5);
free(md5);
g_free(outmd5);
g_free(command);

The above code, md5 always returns the md5 code + garbage, something like:
11139cfd35b803ff1fb286e847f5761dBVkt
Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 3:16 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
I think that your problem might be fixed by this:
Code:
char* substr(const char *str, int start, int end)
{
   /* some compilers don't like putting code before variable declarations */
   char *buffer;
   if(end < 1) end = strlen(str) + 1;
   /* you should typecast the void* that malloc returns */
   buffer = (char*)malloc(end+1);
   strncpy(buffer, str+start, end);
   /* this is new */
   buffer[end] = '\0';
   return buffer;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 6:39 pm 
Offline
GTK+ Guru

Joined: Sun Apr 06, 2008 6:43 pm
Posts: 286
Location: Fortress of solitude
Thanks, that worked :)


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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