+/* -*-c-*-
+ *
+ * $Id: xgetline.c,v 1.1 1998/11/16 23:00:49 mdw Exp $
+ *
+ * Fetch a line of text from the user
+ *
+ * (c) 1998 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the Edgeware X tools collection.
+ *
+ * X tools is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * X tools is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with X tools; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: xgetline.c,v $
+ * Revision 1.1 1998/11/16 23:00:49 mdw
+ * Initial versions.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>
+
+#include "mdwopt.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @cancel@ --- *
+ *
+ * Arguments: @GtkWidget *w@ = widget raising the signal
+ * @gpointer *p@ = pointer to integer result code
+ *
+ * Returns: ---
+ *
+ * Use: Sets the result code to zero (failure) and ends the loop.
+ */
+
+static void cancel(GtkWidget *w, gpointer *p)
+{
+ int *ip = (int *)p;
+ *ip = 0;
+ gtk_main_quit();
+}
+
+/* --- @done@ --- *
+ *
+ * Arguments: @GtkWidget *w@ = widget raising the signal
+ * @gpointer *p@ = pointer to integer result code
+ *
+ * Returns: ---
+ *
+ * Use: Sets the result code nonzero (success) and ends the loop.
+ */
+
+static void done(GtkWidget *w, gpointer *p)
+{
+ int *ip = (int *)p;
+ *ip = 1;
+ gtk_main_quit();
+}
+
+/* --- @check_escape@ --- *
+ *
+ * Arguments: @GtkWidget *w@ = widget raising the signal
+ * @GdkEventKey *ev@ = pointer to event data
+ * @gpointer *p@ = widget to activate in response
+ *
+ * Returns: ---
+ *
+ * Use: Activates a widget when an escape keypress is detected.
+ */
+
+static gboolean check_escape(GtkWidget *w, GdkEventKey *ev, gpointer *p)
+{
+ if (ev->keyval == GDK_Escape) {
+ if (p)
+ gtk_widget_activate(GTK_WIDGET(p));
+ else
+ gtk_object_destroy(GTK_OBJECT(w));
+ return (1);
+ }
+ return (0);
+}
+
+/* --- @main@ --- *
+ *
+ * Arguments: @int argc@ = number of command line arguments
+ * @char *argv[]@ = addresses of arguments
+ *
+ * Returns: Zero if OK, and we read a string; nonzero if the user
+ * cancelled.
+ *
+ * Use: Reads a string from the user, and returns it on standard
+ * output.
+ */
+
+int main(int argc, char *argv[])
+{
+ /* --- Configuration variables --- */
+
+ char *prompt = 0;
+ char *dfl = "";
+ char *title = "Input request";
+ int left;
+ unsigned f = 0;
+ int ok = 0;
+
+ enum {
+ f_invis = 1,
+ f_duff = 2
+ };
+
+ /* --- User interface bits --- */
+
+ GtkWidget *win;
+ GtkWidget *box;
+ GtkWidget *entry;
+ GtkWidget *btn;
+
+ /* --- Crank up the toolkit --- *
+ *
+ * Have to do this here: GTK snarfs some command line options which my
+ * parser would barf about.
+ */
+
+ gtk_init(&argc, &argv);
+
+ /* --- Parse options from command line --- */
+
+ for (;;) {
+
+ /* --- Long options structure --- */
+
+ static struct option opt[] = {
+ { "help", 0, 0, 'h' },
+ { "title", required_argument, 0, 't' },
+ { "prompt", required_argument, 0, 'p' },
+ { "default", required_argument, 0, 'd' },
+ { "password", 0, 0, 'i' },
+ { "invisible", 0, 0, 'i' },
+ { 0, 0, 0, 0 }
+ };
+ int i;
+
+ /* --- Fetch an option --- */
+
+ i = getopt_long(argc, argv, "ht:p:d:i", opt, 0);
+ if (i < 0)
+ break;
+
+ /* --- Work out what to do with it --- */
+
+ switch (i) {
+ case 't':
+ title = optarg;
+ break;
+ case 'p':
+ prompt = optarg;
+ break;
+ case 'd':
+ dfl = optarg;
+ break;
+ case 'i':
+ f |= f_invis;
+ break;
+ default:
+ f |= f_duff;
+ break;
+ }
+ }
+
+ if (f & f_duff) {
+ fprintf(stderr, "xgetline: bad arguments\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* --- Create the main window --- */
+
+ win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_title(GTK_WINDOW(win), title);
+ gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
+ gtk_signal_connect(GTK_OBJECT(win), "destroy",
+ GTK_SIGNAL_FUNC(cancel), &ok);
+
+ /* --- Create the box for laying out the widgets inside --- */
+
+ left = (prompt ? 1 : 0);
+ box = gtk_table_new(left + 2, 1, 0);
+
+ /* --- Maybe create a prompt widget --- */
+
+ if (prompt) {
+ GtkWidget *w = gtk_label_new(prompt);
+ gtk_table_attach(GTK_TABLE(box), w,
+ 0, 1, 0, 1, 0, GTK_EXPAND, 4, 2);
+ gtk_widget_show(w);
+ }
+
+ /* --- Create the entry widget --- */
+
+ entry = gtk_entry_new();
+ gtk_entry_set_text(GTK_ENTRY(entry), dfl);
+ gtk_table_attach(GTK_TABLE(box), entry,
+ left, left + 1, 0, 1,
+ GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
+ if (f & f_invis)
+ gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
+ gtk_widget_show(entry);
+
+ /* --- Create the default action widget --- */
+
+ btn = gtk_button_new_with_label("OK");
+ gtk_table_attach(GTK_TABLE(box), btn,
+ left + 1, left + 2, 0, 1, 0, GTK_EXPAND, 2, 2);
+ GTK_WIDGET_SET_FLAGS(btn, GTK_CAN_DEFAULT);
+ gtk_widget_show(btn);
+
+ /* --- Add the box into the main window --- */
+
+ gtk_container_add(GTK_CONTAINER(win), box);
+ gtk_widget_show(box);
+
+ /* --- Last minute configuration things --- */
+
+ gtk_widget_grab_default(btn);
+ gtk_signal_connect(GTK_OBJECT(btn), "clicked",
+ GTK_SIGNAL_FUNC(done), &ok);
+ gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
+ GTK_SIGNAL_FUNC(gtk_widget_activate),
+ GTK_OBJECT(btn));
+ gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
+ GTK_SIGNAL_FUNC(check_escape), 0);
+
+ /* --- Go go go --- */
+
+ gtk_widget_show(win);
+ gtk_main();
+
+ /* --- Output the result --- */
+
+ if (ok) {
+ char *p = gtk_entry_get_text(GTK_ENTRY(entry));
+ puts(p);
+ }
+
+ return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+
+/*----- That's all, folks -------------------------------------------------*/