#<p>
#  LoginDialog.icn  -- Provide a simple dialog box for username/password
#    entry.  Masks the password display.
#</p>
#<p>
# <b>Author:</b> Steve Wampler (<i>sbw@tapestry.tucson.az.us</i>)
#</p>
#<p>
#  This file is in the <i>public domain</i>.
#</p>

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

import gui
$include "guih.icn"

#<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
   #   response to the user pressing the Ok button.
   #   <[param ev event generated by OK button]>
   #</p>
   method handle_okButton(ev)
       if ev.event = &lrelease then {
           g_user := userField.get_contents()
           g_pw   := passwordField.get_contents()
           validFlag := "yes"
           dispose()
           }
   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("")
      passwordField.set_displaychar("yes")
      self.add(passwordField)
      okButton := TextButton()
      okButton.set_pos("81", "72")
      okButton.set_label("Ok")
      okButton.set_internal_alignment("c")
      okButton.connect(self, "handle_okButton", BUTTON_RELEASE_EVENT)
      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.
   #  <[returns 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.
   #  <[returns 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
