LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   'dialog' app for GTK+2.0 (http://www.linuxquestions.org/questions/programming-9/dialog-app-for-gtk-2-0-a-438027/)

General 04-23-2006 10:52 AM

'dialog' app for GTK+2.0
 
I am writing a bash script that will bring up a "Open file" dialog displaying files with a *.hex ending. Once the file is chosen, the path of the file is sent to a variable, so that the bash script can perform some tasks on that given file.

I am wondering if there is a program, like the curses-based dialog app, that can display a GTK+2.0 file selection dialog. I found this application, but the file selection dialog that it uses doens't look anything like those in the rest of the GNOME desktop. I am not a programmer so I probably could not make a stand-alone program that does this.

vharishankar 04-23-2006 11:02 AM

Not really an answer but a suggestion - maybe Python can serve your purpose better for GUI purposes since it has many modules like pygtk, pyglade and so on which are easier to use.

General 04-23-2006 11:21 AM

Quote:

Originally Posted by Harishankar
Not really an answer but a suggestion - maybe Python can serve your purpose better for GUI purposes since it has many modules like pygtk, pyglade and so on which are easier to use.

Thanks! I will look into that.

95se 04-23-2006 12:02 PM

Hey, put this into a file called fc.c,

Code:

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
  GtkWidget *filechooser;
  GtkFileFilter *filter;
  int resp;

  gtk_init(&argc, &argv);

  filechooser = gtk_file_chooser_dialog_new("Choose File", NULL,
                                            GTK_FILE_CHOOSER_ACTION_OPEN,
                                            GTK_STOCK_CANCEL,
                                            GTK_RESPONSE_CANCEL,
                                            GTK_STOCK_OPEN,
                                            GTK_RESPONSE_ACCEPT,
                                            NULL);
  filter = gtk_file_filter_new();
  gtk_file_filter_add_pattern(filter, "*.hex");
  gtk_file_filter_set_name(filter, "Hex Files (*.hex)");
  gtk_file_chooser_add_filter(filechooser, filter);
  if((resp = gtk_dialog_run(GTK_DIALOG(filechooser))) == GTK_RESPONSE_ACCEPT) {
    printf("%s\n",
          gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(filechooser)));
  }

  gtk_widget_destroy(filechooser);

  return (resp == GTK_RESPONSE_ACCEPT ? 0 : 1);
}

Then compile it w/
Code:

gcc fc.c -o fc `pkg-config --cflags --libs gtk+-2.0`
Now you have an executable called fc, run it,
Code:

./fc
It'll pop up a box, choose a file, it'll write the file name chosen to standard out, then return 0 if successful, 1 if they pressed cancel (Check w/ the bash var $?).

So,
file="`./fc`"

Also, this only let's you choose .hex files, but you can remove anything that says filter to get it to let you choose all, or you could just mess w/ it.

vharishankar 04-23-2006 12:05 PM

Well, that's one way, but then why not write the whole program in C?

ioerror 04-23-2006 12:17 PM

Quote:

Well, that's one way, but then why not write the whole program in C?
Why? There's nothing wrong with combining languages to produce a more flexible program. Not to mention,

Quote:

Once the file is chosen, the path of the file is sent to a variable, so that the bash script can perform some tasks on that given file.
depending on what "some tasks" are, that could involve a helluva lot of C!!

General 04-23-2006 01:10 PM

95se, that is exactly what I need. Thanks very much!


All times are GMT -5. The time now is 05:04 PM.