here's a submission for the main xemacs codebase.
gtk-read-string.el --- Reading a string from a GTK dialog window
i'd submit a diff for it, but i have another diff to make, once i
get back to synching folding.el -- this would mess it up, or vice
versa; the only change would be to include the following file --
gtk-read-string.el -- in the lisp dir of the xemacs source-tree.
here's a ChangeLog entry for it:
2001-12-23 Sean Champ <schamp(a)users.sourceforge.net>
* gtk-read-string.el: new
* gtk-read-string.el (gtk-read-string): new
further work:
- something could be done in 'should-use-dialog-box-p', for
adding support for gtk-read-string, but i haven't taken a look at it,
yet.
misc notes:
- there's a suggestion or two in the comments, for folks who've been
thinking about programming stuff that makes use of xemacs' gtk
support.
(i've also been hacking around something called gtk-read-passwd,
but i'm not sure how secure it'll end up being. (passwd-grab-keyboard)
isn't working, for it, and i'm not sure if any input-even recording is
done for gtk dialog widgets that aren't like xemacs buffer widgets.)
anyway, here's something to 'read-string' with gtk widgets; any
suggestions would most likely be appreciated.
;;; gtk-read-string.el --- Reading a string from a GTK dialog window
;; Copyright (C) 2000 Free Software Foundation, Inc.
;; Maintainer: Sean Champ <schamp(a)users.sourceforge.net>
;; Keywords: gtk, interface
;; This file is not an official part of XEmacs.
;; gtk-read-string.el 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, or (at your option)
;; any later version.
;; gtk-read-string.el 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 gtk-read-string.el; see the file COPYING. If not, write to the
;; Free Software Foundation, 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Synched up with: Not in FSF.
(provide 'gtk-read-string)
(require 'cl)
(defun* gtk-read-string (prompt &optional
initial-contents
history default-value
&key window-title &key prompt-align-left)
"Return a string which will be read from a GTK dialog widget, prompting with
string PROMPT.
If non-nil, optional second arg INITIAL-CONTENTS is a default string to insert
in the entry area, which will be returned if the user presses the 'cancel'
button before entering any text. In such a case, INITIAL-CONTENTS will be
added to the history.
note: When INITIAL-CONTENTS is not specified, This function counts \"the user
entered nothing\" as being equivalent to \"the user entered a zero-width
string\".
Third arg HISTORY, if non-nil, should name a variable which contains a list,
to which the return-value will be prepended, unless the return-value equals the
DEFAULT-VALUE; see below.
Fourth arg DEFAULT-VALUE will be returned, if either the DEFAULT-VALUE, or
what the user entered, was a zero-width string. In such a case, the
value, being equivalent to DEFAULT-VALUE, will not be added to the history.
The optional keyword :window-title may be used in setting the title of the
dialog window.
Optional keyword :prompt-align-left, if specified with a non-nil value, will
cause the PROMPT to be aligned against the left of its label-area in the
dialog box, rather than in the center.
also see: `read-string'
"
;; written with `gtk-password-dialog' as an initial example
;; something in the doc-string is screwing up the automatic indentation.
(let ((returnv
(block dialog
;; set up and show the dialog and all its child widgets.
;;
;; the string value is fetched as a result of
;; (return-from dialog ... ) in the callback that will be attatched
;; to the "OK" and "Cancel" buttons, and to the 'destroy' signal
;; on the dialog window itself.
(let* ((widget)
(dialog (gtk-dialog-new))
(vbox (gtk-dialog-vbox dialog))
(button-area (gtk-dialog-action-area dialog))
(callback #'(lambda (button dialog)
(gtk-main-quit)
(gtk-widget-destroy dialog)
(return-from dialog
(gtk-entry-get-text (get dialog 'x-initial-entry))))))
(gtk-window-set-title
dialog
(or window-title (format "%S : %S" (emacs-name) prompt)))
;; NOTE: XEmacs GTK programmers: if you call `gtk-main',
;; after showing a dialog, as it's done in this function,
;; ALWAYS connect {something} to the 'destroy' event
;; on the dialog you showed, and make sure that the {something}
;; will call `gtk-main-quit'; if you don't, then xemacs
;; will just get stuck in the `gtk-main' loop, when
;; the user closes the window.
(gtk-signal-connect dialog 'destroy callback dialog)
;; "make us modal" (???) [comment from gtk-password-dialog]
(put dialog 'type 'dialog)
(setq widget (gtk-button-new-with-label "OK"))
(gtk-container-add button-area widget)
(gtk-signal-connect widget 'clicked callback dialog)
(put dialog 'x-ok-button widget)
;; Note: some of these x-foo-bar properties are used in
;; gtk-password-dialog.el, for making functions that access some of the
;; widgets that compose the dialog; a similar thing could've been done
;; here, but gtk-read-password is a fairly high-level function.
(gtk-widget-show widget)
(setq widget (gtk-button-new-with-label "Cancel"))
(gtk-container-add button-area widget)
(gtk-signal-connect widget 'clicked callback dialog)
(put dialog 'x-cancel-button widget)
(gtk-widget-show widget)
;; the prompt
(gtk-container-set-border-width vbox 5)
(setq widget (gtk-label-new prompt))
(and prompt-align-left
(gtk-misc-set-alignment widget 0.0 0.5))
(gtk-container-add vbox widget)
(gtk-widget-show widget)
;; the widget where the string is entered
(setq widget (gtk-entry-new))
(gtk-container-add vbox widget)
(put dialog 'x-initial-entry widget)
(gtk-widget-show widget)
(when initial-contents
(gtk-entry-set-text (get dialog 'x-initial-entry) initial-contents)
(gtk-entry-select-region (get dialog 'x-initial-entry)
0 (length initial-contents)))
(gtk-widget-show dialog)
(gtk-main)
)))) ;; end of 'let' bindings
(and default-value (zerop (string-width returnv))
(setq returnv default-value))
;; don't add it to the history, if it equals the default-value
(and history
(or (and default-value (string= returnv default-value))
(set history (cons returnv (symbol-value history)))))
returnv))
;; gtk-read-string.el ends here