如何使用gtk2.0在文本框中打印选择的文件夹路径

时间:2017-05-04 09:18:44

标签: gtk2

我已经完成了选择文件夹并存储文件夹路径的代码。我需要在文本框中调用该路径。如何从下面的代码中执行。在下面的代码中,Get_directory将存储文件夹path.pls提出一些想法。

 #include <gtk/gtk.h>
    #include <string.h>

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

    /* 
     * Gets the action from the user whether to open or save
     */
    GtkWidget *create_filechooser_dialog(char *init_path, GtkFileChooserAction action)
    {
      GtkWidget *folder = NULL;

      switch (action)
      {
        case GTK_FILE_CHOOSER_ACTION_SAVE:
          folder = gtk_file_chooser_dialog_new("Save file", NULL, action,"Cancel", GTK_RESPONSE_CANCEL,"Save", GTK_RESPONSE_OK,NULL);
          break;

        case GTK_FILE_CHOOSER_ACTION_OPEN:
          folder = gtk_file_chooser_dialog_new("Open file", NULL, action,"Cancel", GTK_RESPONSE_CANCEL,"Open", GTK_RESPONSE_OK,NULL);
          break;

        case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
        case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
          break;
      }
      return folder;
    }

    /*
     * Opens the dialog box to select the folder
     * and stores the selected folder path
     */
    static int Get_Directory(GtkFileChooser *chooser, GtkFileChooserAction *action)
    {
       GtkWidget *Directory;
       char *fname = "";
       char *xfile;
       char *file1;

       Directory = create_filechooser_dialog(fname, GTK_FILE_CHOOSER_ACTION_OPEN);

       if (gtk_dialog_run(GTK_DIALOG(Directory)) == GTK_RESPONSE_OK)
       {

          xfile = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(Directory));
          g_print("Selected Directory path : %s\n", xfile);

          gtk_widget_destroy(Directory);
       }
       else
       {
          gtk_main_quit ();
       }

    //   gtk_main_quit ();
    }

    /* 
     * Sets the title for the window, window size, position and border width
     */
    void initialize_window(GtkWidget *window)
    {
      gtk_window_set_title(GTK_WINDOW(window),"Apache Edgent"); //Set window title
      gtk_window_set_default_size (GTK_WINDOW (window), 600, 250); //Set default size for the window
      gtk_container_set_border_width(GTK_CONTAINER(window), 100);  //Set the border width for the window
      gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);  //Set the position of the window
      g_signal_connect (window, "destroy",G_CALLBACK (destroy), NULL); //End application when close button clicked
    }


    int main (int argc, char *argv[])
    {
      GtkWidget *window,*table,*button1,*entry,*button;

      gtk_init(&argc, &argv);

      /* Create the main window */
      window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      initialize_window(window);

      /* Create a 1x2 table */
      table = gtk_table_new (1, 2, TRUE);
      gtk_container_add (GTK_CONTAINER (window), table);

      /* Create a textbox to store the location path */
      entry = gtk_entry_new();
      gtk_entry_set_max_length(GTK_ENTRY(entry),10);
      gtk_entry_set_text(GTK_ENTRY(entry),button1);
      gtk_table_attach_defaults (GTK_TABLE(table), entry, 0, 1, 0, 1);
      gtk_widget_show(entry);

      /* Create a button to select a folder */
      button1 = gtk_button_new_with_label("Select folder");
      gtk_container_set_border_width(GTK_CONTAINER(button1), 0);
      g_signal_connect (button1, "clicked",G_CALLBACK (Get_Directory), entry);
      gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
      gtk_table_attach_defaults (GTK_TABLE (table),button1, 1, 2, 0, 1);

      /* Create a button to start */
      button = gtk_button_new_with_label("START");
      gtk_container_set_border_width(GTK_CONTAINER(button), 0);
      g_signal_connect (button, "clicked",G_CALLBACK (destroy), NULL);
      gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
      gtk_table_attach_defaults (GTK_TABLE (table),button, 0, 2, 1, 2);

      gtk_widget_show(button);

      gtk_widget_show_all(window);

      gtk_main ();
      return 0;
    }

-- 

0 个答案:

没有答案