Re: newbie needs snippet : how to obtain a string from the user in a dialog



On Thu, 2005-02-24 at 23:34 +0100, ronald wrote:
I use a dialog with a gtk_entry in it. my main wonder is how to 
get the text from that entry when the user presses the OK button.
since the OK button is connected with a destroy-dialog action, 
I am confused how to get at the results of the users' effort.

You should use the gtk dialog framework:

char *get_text_from_user (GtkWindow *parent_window, char *window_title)
  {
  static GtkWidget *dialog = NULL ;
  static GtkWidget *entry = NULL ;
  char *entry_text = NULL ;

  if (NULL == dialog)
    {
    dialog = gtk_dialog_new () ;
    gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CANCEL, 
      GTK_RESPONSE_CANCEL) ;
    gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_OK, 
      GTK_RESPONSE_OK) ;
    gtk_dialog_set_default_response (GTK_DIALOG (dialog), 
      GTK_RESPONSE_OK) ;
    }
  // The following causes this dialog to stay above the main window
  if (NULL != parent_window)
    gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window) ;

  gtk_window_set_title (GTK_WINDOW (dialog), 
    NULL == window_title ? "User Input" : window_title) ;

  if (NULL == entry)
    {
    entry = gtk_entry_new () ;
    gtk_widget_show (entry) ;
    // The following will make it so when the user hits "Enter", it's
    // like clicking OK
    gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE) ;
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), entry, 
      TRUE, TRUE, 2) ;
    }

  if (GTK_RESPONSE_OK == gtk_dialog_run (GTK_DIALOG (dialog)))
    entry_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry))) ;

  gtk_widget_hide (dialog) ;

  return entry_text ;
  }





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]