I'm sure someone has a better one (Robert?) but I needed a
GUI widget for programs that require username/password
access.  I've attached my solution in case anyone is 
also interested.  Improvements are welcome!

Also attached is some sample code showing how it can
be used.

-- 
Steve Wampler -- [EMAIL PROTECTED]
The gods that smiled on your birth are now laughing out loud.
#<p>
#  LoginDialog.icn  -- Provide a simple dialog box for username/password
#    entry.  Masks the password display.
#</p>

#<p>
#  Package of simple GUI widgets.
#</p>
package GuiWidgets

#<p>
#  The LoginDialog class provides a simple dialog for entering a username
#  and password.  The display of the password is masked.
#  Once the user clicks the <b>Ok</b> button, the dialog goes away.
#  The methods <b>getUser()</b> and <b>getPW()</b> are then available
#  to obtain the entered username and password, respectively.  Both
#  fail if the user has not clicked the <b>Ok</b> button.
#</p>
class LoginDialog : _Dialog(userField, passwordField,
                     okButton, userLabel, passwordLabel,
                     g_user, g_pw, realPW, validFlag)

   #<p>
   #   <i>Intended for <b>internal use only</b></i>.  Handles the
   #   masking of password display.
   #</p>
   method handle_passwordField(ev)
      if ev.get_code() = 1 then {
          e := ev.event
          if type(e) == "string" then {
              if e == ("\b") then {
                  realPW := realPW[1:-1]
                  }
              else {
                  realPW ||:= e
                  }
              passwordField.set_contents(repl("*",*realPW))
              }
          }
   end

   #<p>
   #   <i>Intended for <b>internal use only</b></i>.  Handles the
   #   response to the user pressing the Ok button.
   #</p>
   method handle_okButton(ev)
       if ev.event = &lrelease then {
           g_user := userField.get_contents()
           g_pw   := realPW
           validFlag := "yes"
           dispose()
           }
   end

   method dialog_event(ev)
       case ev.get_component() of {
           passwordField : handle_passwordField(ev)
           okButton : handle_okButton(ev)
           }
   end

   #<p>
   #   <i>Internal use only.</i>
   #</p>
   method init_dialog()
   end

   #<p>
   #   <i>Internal use only.</i>
   #</p>
   method end_dialog()
   end

   #<p>
   #   <i>Internal use only.</i>
   #</p>
   method setup()
      self.set_attribs("size=190,105", "bg=pale gray")
      userLabel := Label()
      userLabel$set_pos(18, 11)
      userLabel$set_internal_alignment("l")
      userLabel$set_label("User:")
      self$add(userLabel)
      userField := TextField()
      userField$set_pos(75, 8)
      userField$set_size("104", "20")
      userField$set_draw_border()
      userField$set_contents("")
      self$add(userField)
      passwordLabel := Label()
      passwordLabel$set_pos("14", "38")
      passwordLabel$set_internal_alignment("l")
      passwordLabel$set_label("Password:")
      self$add(passwordLabel)
      passwordField := TextField()
      passwordField$set_pos("75", "35")
      passwordField$set_size("104", "20")
      passwordField$set_draw_border()
      passwordField$set_contents("")
      self$add(passwordField)
      okButton := TextButton()
      okButton$set_pos("81", "72")
      okButton$set_label("Ok")
      okButton$set_internal_alignment("c")
      self$add(okButton)
   end

   #<p>
   #   <i>Internal use only.</i>
   #</p>
   method component_setup()
      self.setup()
   end

   #<p>
   #  If the OK button has been pressed, allow the retrieval of
   #  the username.
   #</p>
   method getUser()
       if \validFlag then
           return g_user
   end

   #<p>
   #  If the OK button has been pressed, allow the retrieval of
   #  the password.
   #</p>
   method getPW()
       if \validFlag then
           return g_pw
   end

   #<p>
   #  Create the login dialog.  Once created, use the <b>show_modal()</b>
   #  method to display.
   #</p>
   initially ()
       self$_Dialog.initially()
       realPW := ""
end
#<p>
#  A simple addressbook application.  The PosgreSQL database is used
#   to hold the addresses and is accessed using ODBC.
#</p>

import AddressBook      # The real addressbook code
import GuiWidgets       # Access to the LoginDialog class

#<p>
#    After successfully logging into the relational database,
#    start up the address book GUI.
#</p>
procedure main()
   local l,a

   l := LoginDialog()
   l.show_modal()
   a := AddressDB(l.getUser(),l.getPW()) |
        stop("Unable to access addressbook!")
   AddressGui(a).show_modal()
end

Reply via email to