Author Topic: RaspberryBASIC.org Forum  (Read 99913 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #240 on: December 27, 2019, 08:16:28 AM »
Response from Antonio.

Quote
  Hi,


PASSWORD (creation only) [Windows and GTK Only] (non inheritable): Hide the typed character using an "*". Default: "NO".


  creation only means it can not be changed after the native element is created.


  My suggestion is for you to use a zbox with two IupText, then switch between them when the password is necessary.


Best,

Scuri

Offline jalih

  • Advocate
  • Posts: 111
Re: RaspberryBASIC.org Forum
« Reply #241 on: December 27, 2019, 01:56:27 PM »
Here is my login gui updated to match recent entries:

Code: [Select]
requires gui

var gui

{ guest: "pa$$w0rd!" } constant passwords

: authenticate
  "edit1" g:child g:text? passwords swap m:@ nip null? if
    drop
    "lbl0" g:child
    "User not found!" g:text drop
  else
    swap
    "edit2" g:child g:text? rot s:= if
     "Authenticated!" . cr
     bye
    else
      "lbl0" g:child
      "Username and password don't match!" g:text drop
    then
  then ;

{
  kind: "win",
  buttons: 0,
  native-title-bar: false,
  title: "Login",
  wide: 520,
  high: 200,
  resizable: false,
  center: true,
  init: ( gui ! ),
  children:
  [
    {
      kind: "box",
      name: "frame",
      bounds: "0, 0, parent.width, parent.height",
      bg: "gray",
      children:
      [
        {
          kind: "image",
          bounds: "parent.left+10, parent.top+10, left+128, top+128",
          img: "8thlogo.png",
          name: "logo"     
        },
        {
          kind: "label",
          fg: "blue",
          font: 20,
          label: "",
          bounds: "logo.right+20, parent.top+10, parent.width-10, top+24 ",
          justify: ["hcenter"],
          name: "lbl0"
},
        {
          kind: "label",
          label: "Username:",
          bounds: "logo.right+20, lbl0.bottom+20, left+80, top+24 ",
          name: "lbl1"
},
        {
          kind: "edit",
          bounds: "lbl1.right+10, lbl1.top, parent.width-20, top+24",
          name: "edit1",
          max-text: 32
        },
        {
          kind: "label",
          label: "Password:",
          bounds: "lbl1.left, lbl1.bottom+10, left+80, top+24",
          name: "lbl2"
},
        {
          kind: "edit",
          bounds: "edit1.left, lbl2.top, parent.width-20, top+24",
          name: "edit2",
          max-text: 32,
          password-char: "*"
        },
        {
          kind: "btn",
          label: "Login",
          bg: "darkgray",
          bounds: "lbl2.left, lbl2.bottom+20, edit2.right, top+30",
          name: "button",
          tooltip: "Login to account",
          click: ' authenticate
        }
      ]
    }
  ]
} var, gui-desc

: app:main
  gui-desc @ g:new ;

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #242 on: December 27, 2019, 03:56:01 PM »
Response from Antonio.

Quote
  Hi,


PASSWORD (creation only) [Windows and GTK Only] (non inheritable): Hide the typed character using an "*". Default: "NO".


  creation only means it can not be changed after the native element is created.


  My suggestion is for you to use a zbox with two IupText, then switch between them when the password is necessary.


Best,

Scuri

That's an implementation choice, at least as far as GTK is concerned.

Consider:

Code: C
  1. #include <gtk/gtk.h>
  2.  
  3. GtkWidget *mainwin, *txtEntry, *chkBox, *layout;
  4.  
  5. void toggled_cb (GtkToggleButton *toggle_button, gpointer  user_data) {
  6.       if (gtk_toggle_button_get_active (toggle_button)) {
  7.           g_object_set(user_data,"visibility",TRUE,NULL);
  8.       }else{
  9.           g_object_set(user_data,"visibility",FALSE,NULL);
  10.       }
  11.    
  12. }
  13.  
  14. int main(int argc, char **argv) {
  15.    
  16.     gtk_init(&argc, &argv);
  17.    
  18.     mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  19.     layout = gtk_layout_new(NULL, NULL);
  20.     g_object_set(mainwin,
  21.                 "title","Visible Password Toggle",
  22.                 "default-width",660,
  23.                 "default-height",370,
  24.                 "resizable",FALSE,
  25.                 "window-position",GTK_WIN_POS_CENTER,
  26.                 "child",layout,
  27.                 NULL);
  28.  
  29.     txtEntry = gtk_entry_new();
  30.     g_object_set(txtEntry,"text","mypassword","visibility",FALSE,NULL);
  31.    
  32.     chkBox = gtk_check_button_new_with_label("Show Password");
  33.     g_object_set(layout,"child",txtEntry,"margin",10,NULL);
  34.  
  35.     gtk_layout_put(GTK_LAYOUT(layout), chkBox, 0, 40);
  36.    
  37.     gtk_widget_show_all(mainwin);
  38.  
  39.     g_signal_connect (mainwin, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  40.     g_signal_connect (chkBox, "toggled", G_CALLBACK (toggled_cb), txtEntry);
  41.    
  42.     gtk_main();
  43.  
  44.     return 0;
  45. }
  46.  
  47.  

In your interface, you should be able to grab the native GTK widget and toggle it's visibility flag...IUP remains as Antonio et al coded it, and you "extend" it in your interface in order to achieve the desired functionality...

Of course, if he's running his own event loop and hooking it into GTK's event loop, this may not work depending on how he hooks into GTK's event loop...


AIR.
« Last Edit: December 27, 2019, 04:09:03 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #243 on: December 27, 2019, 06:43:26 PM »
The zbox.c example demonstrates what I'm trying to accomplish.

Let's see if I can make Antonio's concept work.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #244 on: December 27, 2019, 08:09:22 PM »
Did this one on my Mac, using BlitzMax (there's no arm64 version available yet)

Code: Text
  1. Strict
  2.  
  3. Import MaxGUI.Drivers
  4.  
  5. Local window:    TGadget    =     CreateWindow("Logon",40,40,440,150,Null,WINDOW_TITLEBAR | WINDOW_CENTER)
  6. Local logo:        TGadget    =    CreatePanel(6,6,119,100, window)
  7. Local image:    TPixmap    =    LoadPixmap("login.png")
  8. Local lblUser:    TGadget    =     CreateLabel("Username:",110,16,90,22,window)
  9. Local txtUser:    TGadget    =    CreateTextField(190,16,240,22,window)
  10. Local lblPass:    TGadget    =     CreateLabel("Password:",110,48,90,22,window)
  11. Local txtPass:    TGadget    =    CreateTextField(190,48,240,22,window,TEXTFIELD_PASSWORD)
  12. Local btnLogin:    TGadget    =     CreateButton("Login",190,80,240,22,window)
  13. Local lblErr:    TGadget    =     CreateLabel("",126,100,260,60,window)
  14.  
  15. SetPanelPixmap logo,image
  16. SetGadgetFont(lblErr,LookupGuiFont(GUIFONT_SYSTEM,18,FONT_BOLD))
  17. SetGadgetColor(lblErr,255,0,0,False)
  18.  
  19. While True
  20.    WaitEvent
  21.    Select EventID()
  22.        Case EVENT_WINDOWCLOSE,EVENT_APPTERMINATE
  23.            End
  24.        Case EVENT_GADGETACTION
  25.            Select EventSource()
  26.             Case btnLogin
  27.                 Local user_password:String = GadgetText(txtPass)
  28.                 Local stupid_password:String = "pa$$w0rd!"
  29.                
  30.                 If user_password = stupid_password
  31.                     End
  32.                 Else
  33.                     SetGadgetText(lblErr,"** Invalid Password **")
  34.                 EndIf
  35.                
  36.             Case txtPass
  37.                 SetGadgetText(lblErr,"")                
  38.  
  39.            EndSelect
  40.    End Select
  41. Wend


If you enter the wrong password, when you go to re-enter it, the warning message goes away.... 8)

AIR.
« Last Edit: December 27, 2019, 08:19:49 PM by AIR »

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #245 on: December 27, 2019, 08:36:50 PM »
I was able to get the show/hide password working correctly.

If you want to  give this a try, I attached everything you need to do so.

Code: ScriptBasic
  1. ' ScriptBasic / IUP - Login - JRS
  2.  
  3. IMPORT iup.bas
  4.  
  5. SUB login_clicked
  6.   uid = Iup::GetAttribute(uidtxt,"VALUE")
  7.   IF Iup::GetAttribute(showpwd, "VALUE") = "OFF" THEN
  8.     pwd = Iup::GetAttribute(pwdtxt1,"VALUE")
  9.   ELSE
  10.     pwd = Iup::GetAttribute(pwdtxt2,"VALUE")
  11.   END IF
  12.   IF uid = "John" AND pwd = "Spikowski" THEN
  13.     Iup::Hide(errlbl)
  14.     Iup::Message("Login",  "Login Successful")
  15.     Iup::ExitLoop = TRUE
  16.   ELSE
  17.     Iup::Show(errlbl)
  18.     Iup::SetFocus(uidtxt)
  19.   END IF
  20. END SUB
  21.  
  22. SUB show_password
  23.   showcb = Iup::GetAttribute(showpwd, "VALUE")
  24.   IF showcb = "OFF" THEN
  25.     pwd2 = Iup::GetAttribute(pwdtxt2,"VALUE")
  26.     Iup::SetAttribute(pwdtxt1, "VALUE", pwd2)
  27.     Iup::SetAttribute(zbtxt, "VALUE", "pwdtxt1")
  28.   ELSE
  29.     pwd1 = Iup::GetAttribute(pwdtxt1,"VALUE")
  30.     Iup::SetAttribute(pwdtxt2, "VALUE", pwd1)
  31.     Iup::SetAttribute(zbtxt, "VALUE", "pwdtxt2")
  32.   END IF
  33. END SUB
  34.  
  35. SUB Win_exit
  36.   Iup::ExitLoop = TRUE
  37. END SUB
  38.  
  39. Iup::Open()
  40. dlg = Iup::Create("dialog")
  41. Iup::SetAttributes(dlg, "TITLE=\"Login\", SIZE=200x200")
  42.  
  43. vb = Iup::Create("vbox")
  44. hb1 = Iup::Create("hbox")
  45. piclbl = Iup::Create("label")
  46. Iup::SetAttribute(piclbl, "IMAGE", "./login.png")
  47. Iup::SetAttribute(piclbl, "EXPAND", "HORIZONTAL")
  48. Iup::SetAttribute(piclbl, "ALIGNMENT", "ACENTER:ATOP")
  49. Iup::Append(hb1, piclbl)
  50. Iup::Append(vb, hb1)
  51.  
  52. hb2 = Iup::Create("hbox")
  53. errlbl = Iup::Create("label")
  54. Iup::SetAttribute(errlbl, "TITLE", "User ID / Password Incorrect")
  55. Iup::SetAttribute(errlbl, "FGCOLOR", "#ff0000")
  56. Iup::SetAttribute(errlbl, "EXPAND", "HORIZONTAL")
  57. Iup::SetAttribute(errlbl, "ALIGNMENT", "ACENTER")
  58. Iup::Hide(errlbl)
  59. Iup::Append(hb2, errlbl)
  60. Iup::Append(vb, hb2)
  61.  
  62. hb3 = Iup::Create("hbox")
  63. Iup::SetAttribute(hb3, "MARGIN", "20x10")
  64. Iup::SetAttribute(hb3, "GAP", "5")
  65. vb1 = Iup::Create("vbox")
  66. Iup::SetAttribute(vb1, "GAP", "15")
  67. uidlbl = Iup::Create("label")
  68. Iup::SetAttribute(uidlbl, "TITLE", "User ID")
  69. Iup::Append(vb1, uidlbl)
  70. pwdlbl = Iup::Create("label")
  71. Iup::SetAttribute(pwdlbl, "TITLE", "Password")
  72. Iup::Append(vb1, pwdlbl)
  73. Iup::Append(hb3, vb1)
  74. vb2 = Iup::Create("vbox")
  75. uidtxt = Iup::Create("text")
  76. Iup::SetAttribute(uidtxt, "SIZE", "85x")
  77. Iup::Append(vb2, uidtxt)
  78. pwdtxt1 = Iup::Create("text")
  79. Iup::SetAttribute(pwdtxt1, "PASSWORD", "YES")
  80. Iup::SetAttribute(pwdtxt1, "SIZE", "85x")
  81. pwdtxt2 = Iup::Create("text")
  82. Iup::SetAttribute(pwdtxt2, "SIZE", "85x")
  83. Iup::SetHandle ("pwdtxt1", pwdtxt1)
  84. Iup::SetHandle ("pwdtxt2", pwdtxt2)
  85. zbtxt = Iup::Zbox(pwdtxt1, pwdtxt2)
  86. Iup::SetHandle("zbtxt", zbtxt)
  87. Iup::Append(vb2, zbtxt)
  88. Iup::Append(hb3, vb2)
  89. Iup::Append(vb, hb3)
  90.  
  91. hb4 = Iup::Create("hbox")
  92. Iup::SetAttribute(hb4, "MARGIN", "35x")
  93. showpwd = Iup::Create("toggle")
  94. Iup::SetAttribute(showpwd, "TITLE", "Show Password?")
  95. Iup::SetAttribute(showpwd, "CANFOCUS", "NO")
  96. Iup::Append(hb4, showpwd)
  97. Iup::Append(vb, hb4)
  98.  
  99. hb5 = Iup::Create("hbox")
  100. Iup::SetAttribute(hb5, "MARGIN", "40x10")
  101. loginbut = Iup::Create("button")
  102. Iup::SetAttribute(loginbut, "TITLE", "Login")
  103. Iup::SetAttribute(loginbut, "EXPAND", "HORIZONTAL")
  104. iup::Append(hb5, loginbut)
  105. iup::Append(vb, hb5)
  106.  
  107. iup::Append(dlg, vb)
  108.  
  109. Iup::SetCallback(dlg,"CLOSE_CB",ADDRESS(Win_exit()))
  110. Iup::SetCallback(loginbut, "ACTION", ADDRESS(login_clicked()))
  111. Iup::SetCallback(showpwd, "ACTION", ADDRESS(show_password()))
  112.  
  113. Iup::Show(dlg)
  114. Iup::MainLoop()
  115. Iup::Close()
  116.  

« Last Edit: December 27, 2019, 08:50:53 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #246 on: December 27, 2019, 09:50:39 PM »
It would be great to make the password hide/show as part of the challenge requirements. It's an interesting twist.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #247 on: December 28, 2019, 06:36:46 PM »
AIR,

Do you know how to  make Nim variables global?

It says it can find uidtxt in the callback.

Code: Text
  1. import niup
  2. import niupext
  3.  
  4. proc login_clicked(ih:PIhandle):cint {.cdecl.}=
  5.   let uid = GetAttribute(uidtxt,"VALUE")
  6.   if GetAttribute(showpwd, "VALUE") == "OFF":
  7.     let pwd = GetAttribute(pwdtxt1,"VALUE")
  8.   else:
  9.     let pwd = GetAttribute(pwdtxt2,"VALUE")
  10.  
  11.   if uid == "John" and pwd == "Spikowski":
  12.     Hide(errlbl)
  13.     Message("Login",  "Login Successful")
  14.     return IUP_CLOSE
  15.   else:
  16.     Show(errlbl)
  17.     SetFocus(uidtxt)
  18.  
  19. Open()
  20.  
  21. # *** DIALOG ***
  22. let dlg = Create("dialog")
  23. SetAttributes(dlg,
  24.   "TITLE=\"Login\", " &
  25.   "SIZE=200x200, " &
  26.   "MAXBOX=NO, " &
  27.   "MINBOX=NO, " &
  28.   "RESIZE=NO")
  29.  
  30. # *** CONTAINER ***
  31. let vb = Create("vbox")
  32.  
  33. # *** IMAGE ***
  34. let hb1 = Create("hbox")
  35. let piclbl = Create("label")
  36. SetAttributes(piclbl,  
  37.   "IMAGE=\"login.png\", " &
  38.   "EXPAND=HORIZONTAL, " &
  39.   "ALIGNMENT=ACENTER:ATOP")
  40. discard Append(hb1, piclbl)
  41. discard Append(vb, hb1)
  42.  
  43. # *** ERROR ***
  44. let hb2 = Create("hbox")
  45. let errlbl = Create("label")
  46. SetAttributes(errlbl,
  47.   "TITLE=\"User ID / Password Incorrect\", " &
  48.   "FGCOLOR=\"#ff0000\", " &
  49.   "EXPAND=HORIZONTAL, " &
  50.   "ALIGNMENT=ACENTER")
  51. # Hide(errlbl)
  52. discard Append(hb2, errlbl)
  53. discard Append(vb, hb2)
  54.  
  55. # *** ENTRY ***
  56. let hb3 = Create("hbox")
  57. SetAttributes(hb3,
  58.   "MARGIN=20x10, " &
  59.   "GAP=5")
  60. let vb1 = Create("vbox")
  61. SetAttribute(vb1, "GAP", "15")
  62. let uidlbl = Create("label")
  63. SetAttribute(uidlbl, "TITLE", "User ID")
  64. discard Append(vb1, uidlbl)
  65. let pwdlbl = Create("label")
  66. SetAttribute(pwdlbl, "TITLE", "Password")
  67. discard Append(vb1, pwdlbl)
  68. discard Append(hb3, vb1)
  69. let vb2 = Create("vbox")
  70. let uidtxt = Create("text")
  71. SetAttribute(uidtxt, "SIZE", "85x")
  72. discard Append(vb2, uidtxt)
  73. let pwdtxt1 = Create("text")
  74. SetAttributes(pwdtxt1,
  75.   "PASSWORD=YES, " &
  76.   "SIZE=85x")
  77. let pwdtxt2 = Create("text")
  78. SetAttribute(pwdtxt2, "SIZE", "85x")
  79. SetHandle("pwdtxt1", pwdtxt1)
  80. SetHandle("pwdtxt2", pwdtxt2)
  81. let zbtxt = Zbox(pwdtxt1, pwdtxt2, nil)
  82. SetHandle("zbtxt", zbtxt)
  83. discard Append(vb2, zbtxt)
  84. discard Append(hb3, vb2)
  85. discard Append(vb, hb3)
  86.  
  87. # *** SHOW/HIDE ***
  88. let hb4 = Create("hbox")
  89. SetAttribute(hb4, "MARGIN", "35x")
  90. let showpwd = Create("toggle")
  91. SetAttributes(showpwd,
  92.   "TITLE=\"Show Password?\", " &
  93.   "CANFOCUS=NO")
  94. discard Append(hb4, showpwd)
  95. discard Append(vb, hb4)
  96.  
  97. # *** LOGIN ***
  98. let hb5 = Create("hbox")
  99. SetAttribute(hb5, "MARGIN", "40x10")
  100. let loginbut = Create("button")
  101. SetAttributes(loginbut,
  102.   "TITLE=\"Login\", " &
  103.   "EXPAND=HORIZONTAL")
  104. discard Append(hb5, loginbut)
  105. discard Append(vb, hb5)
  106.  
  107. # *** ATTACH CONTAINER ***
  108. discard Append(dlg, vb)
  109.  
  110. ' *** CALLBACKS ***
  111. SetCallback(loginbut, "ACTION", login_clicked)
  112. SetCallback(showpwd, "ACTION", show_password)
  113.  
  114. # *** PROCESS ***
  115. Show(dlg)
  116. MainLoop()
  117. Close()
  118.  

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #248 on: December 28, 2019, 06:37:54 PM »
If we're going to add toggling the visibility of the password as a 'requirement', we should also add the password box reacting to the "Enter/Return" key.

Code: C
  1. /* logon2.c
  2.  *
  3.  * version 1.2
  4.  *
  5.  * GUI Logon Screen Challenge Submission
  6.  * C version, using GTK+-3.0
  7.  *
  8.  * Written by Armando I. Rivera (AIR)
  9.  *
  10.  * Compile:  gcc logon2.c $(pkg-config --libs --cflags gtk+-3.0) -o logon2
  11. */
  12.  
  13. #include <gtk/gtk.h>
  14.  
  15. GtkWidget *err_label;
  16.  
  17. void chkBox_cb (GtkToggleButton *toggle_button, gpointer data) {
  18.       if (gtk_toggle_button_get_active (toggle_button)) {
  19.           g_object_set(data,"visibility",TRUE,NULL);
  20.       }else{
  21.           g_object_set(data,"visibility",FALSE,NULL);
  22.       }
  23. }
  24.  
  25. void txtPass_cb( GtkWidget *widget, gpointer data ) {
  26.     g_object_set(data,"label","",NULL);
  27. }
  28.  
  29. void onClick( GtkWidget *widget, gpointer data ) {
  30.     gchar *stupid_password = "pa$$w0rd!";
  31.     gchar *user_password;
  32.     g_object_get(data,"text",&user_password,NULL);
  33.  
  34.     if (g_strcmp0 (stupid_password,user_password) == 0) {
  35.         g_print("Your are now logged in!\n");
  36.         gtk_main_quit();
  37.     }else{
  38.         gtk_label_set_markup(GTK_LABEL(err_label), "<span color=\"red\" font_desc=\"16.0\">** Invalid Password **</span>");
  39.         g_print("Username or Password is Incorrect!\n");
  40.  
  41.     }
  42. }
  43.  
  44. int main( int argc, char *argv[])
  45. {
  46.     GtkWidget *window, *layout, *image, *btnLogin, *chkBox;
  47.     GtkWidget *lblUser, *lblPass, *txtUser, *txtPass;
  48.  
  49.     gtk_init(&argc, &argv);
  50.    
  51.     layout = gtk_layout_new(NULL, NULL);
  52.  
  53.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  54.     g_object_set(window,
  55.                 "title","Login",
  56.                 "default-width",660,
  57.                 "default-height",370,
  58.                 "resizable",FALSE,
  59.                 "window-position",GTK_WIN_POS_CENTER,
  60.                 "child",layout,
  61.                 "decorated",0,
  62.                 NULL);
  63.  
  64.  
  65.     image = gtk_image_new_from_file("logon.png");
  66.     g_object_set(layout,"child",image,"margin",10,NULL);
  67.    
  68.     lblUser = gtk_label_new("");
  69.     lblPass = gtk_label_new("");
  70.     err_label = gtk_label_new("");
  71.     g_object_set(err_label,"width-request",270,NULL);
  72.    
  73.     gtk_label_set_markup(GTK_LABEL(lblUser), "<span font_desc=\"16.0\">Username:</span>");
  74.     gtk_label_set_markup(GTK_LABEL(lblPass), "<span font_desc=\"16.0\">Password:</span>");
  75.    
  76.     txtUser = gtk_entry_new();
  77.     txtPass = gtk_entry_new();    
  78.     g_object_set(txtPass,"visibility", FALSE,NULL);
  79.    
  80.     chkBox = gtk_check_button_new_with_label("Show Password");
  81.  
  82.     btnLogin = gtk_button_new_with_label("Login");
  83.     g_object_set(btnLogin,"width-request",170,NULL);
  84.  
  85.  
  86.     gtk_layout_put(GTK_LAYOUT(layout), lblUser, 330, 112-30);
  87.     gtk_layout_put(GTK_LAYOUT(layout), lblPass, 330, 162-30);
  88.     gtk_layout_put(GTK_LAYOUT(layout), txtUser, 460, 110-30);
  89.     gtk_layout_put(GTK_LAYOUT(layout), txtPass, 460, 160-30);
  90.     gtk_layout_put(GTK_LAYOUT(layout), chkBox, 460, 210-30);
  91.     gtk_layout_put(GTK_LAYOUT(layout), btnLogin, 460, 250-30);
  92.     gtk_layout_put(GTK_LAYOUT(layout), err_label, 300, 16);
  93.  
  94.     g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  95.     g_signal_connect (btnLogin, "clicked", G_CALLBACK (onClick), txtPass);
  96.     g_signal_connect (chkBox, "toggled", G_CALLBACK (chkBox_cb), txtPass);
  97.     g_signal_connect (txtPass, "changed", G_CALLBACK (txtPass_cb), err_label);
  98.     g_signal_connect (txtPass, "activate", G_CALLBACK (onClick), txtPass);
  99.    
  100.     gtk_widget_show_all(window);
  101.  
  102.     gtk_main();
  103.  
  104.     return 0;
  105. }
  106.  
  107.  
  108.  

AIR.
« Last Edit: December 28, 2019, 06:39:38 PM by AIR »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #249 on: December 28, 2019, 07:44:51 PM »
AIR,

Do you know how to  make Nim variables global?

It says it can find uidtxt in the callback.

You have to declare them before the call to Open(), removing the 'let' from the declared variables.  You can either declare them before the callback, or forward declare the callback and move it to the end of the code.

Something like:

Code: Text
  1. import niup
  2. import niupext
  3.  
  4. var uidtxt,showpwd,pwdtxt1,pwdtxt2,errlbl:PIhandle
  5. proc login_clicked(ih:PIhandle):cint {.cdecl.}
  6.  
  7. Open()
  8.  
  9. # *** DIALOG ***
  10. let dlg = Create("dialog")
  11. SetAttributes(dlg,
  12.   "TITLE=\"Login\", " &
  13.   "SIZE=200x200, " &
  14.   "MAXBOX=NO, " &
  15.   "MINBOX=NO, " &
  16.   "RESIZE=NO")
  17.  
  18. # *** CONTAINER ***
  19. let vb = Create("vbox")
  20.  
  21. # *** IMAGE ***
  22. let hb1 = Create("hbox")
  23. let piclbl = Create("label")
  24. SetAttributes(piclbl,  
  25.   "IMAGE=\"login.png\", " &
  26.   "EXPAND=HORIZONTAL, " &
  27.   "ALIGNMENT=ACENTER:ATOP")
  28. discard Append(hb1, piclbl)
  29. discard Append(vb, hb1)
  30.  
  31. # *** ERROR ***
  32. let hb2 = Create("hbox")
  33. errlbl = Create("label")
  34. SetAttributes(errlbl,
  35.   "TITLE=\"User ID / Password Incorrect\", " &
  36.   "FGCOLOR=\"#ff0000\", " &
  37.   "EXPAND=HORIZONTAL, " &
  38.   "ALIGNMENT=ACENTER")
  39. # Hide(errlbl)
  40. discard Append(hb2, errlbl)
  41. discard Append(vb, hb2)
  42.  
  43. # *** ENTRY ***
  44. let hb3 = Create("hbox")
  45. SetAttributes(hb3,
  46.   "MARGIN=20x10, " &
  47.   "GAP=5")
  48. let vb1 = Create("vbox")
  49. SetAttribute(vb1, "GAP", "15")
  50. let uidlbl = Create("label")
  51. SetAttribute(uidlbl, "TITLE", "User ID")
  52. discard Append(vb1, uidlbl)
  53. let pwdlbl = Create("label")
  54. SetAttribute(pwdlbl, "TITLE", "Password")
  55. discard Append(vb1, pwdlbl)
  56. discard Append(hb3, vb1)
  57. let vb2 = Create("vbox")
  58. uidtxt = Create("text")
  59. SetAttribute(uidtxt, "SIZE", "85x")
  60. discard Append(vb2, uidtxt)
  61. pwdtxt1 = Create("text")
  62. SetAttributes(pwdtxt1,
  63.   "PASSWORD=YES, " &
  64.   "SIZE=85x")
  65. pwdtxt2 = Create("text")
  66. SetAttribute(pwdtxt2, "SIZE", "85x")
  67. SetHandle("pwdtxt1", pwdtxt1)
  68. SetHandle("pwdtxt2", pwdtxt2)
  69. let zbtxt = Zbox(pwdtxt1, pwdtxt2, nil)
  70. SetHandle("zbtxt", zbtxt)
  71. discard Append(vb2, zbtxt)
  72. discard Append(hb3, vb2)
  73. discard Append(vb, hb3)
  74.  
  75. # *** SHOW/HIDE ***
  76. let hb4 = Create("hbox")
  77. SetAttribute(hb4, "MARGIN", "35x")
  78. showpwd = Create("toggle")
  79. SetAttributes(showpwd,
  80.   "TITLE=\"Show Password?\", " &
  81.   "CANFOCUS=NO")
  82. discard Append(hb4, showpwd)
  83. discard Append(vb, hb4)
  84.  
  85. # *** LOGIN ***
  86. let hb5 = Create("hbox")
  87. SetAttribute(hb5, "MARGIN", "40x10")
  88. let loginbut = Create("button")
  89. SetAttributes(loginbut,
  90.   "TITLE=\"Login\", " &
  91.   "EXPAND=HORIZONTAL")
  92. discard Append(hb5, loginbut)
  93. discard Append(vb, hb5)
  94.  
  95. # *** ATTACH CONTAINER ***
  96. discard Append(dlg, vb)
  97.  
  98. # *** CALLBACKS ***
  99. SetCallback(loginbut, "ACTION", login_clicked)
  100. #~ SetCallback(showpwd, "ACTION", show_password)
  101.  
  102. # *** PROCESS ***
  103. Show(dlg)
  104. MainLoop()
  105. Close()
  106.  
  107. proc login_clicked(ih:PIhandle):cint {.cdecl.} =
  108.   var pwd:cstring
  109.   let uid = GetAttribute(uidtxt,"VALUE")
  110.   if GetAttribute(showpwd, "VALUE") == "OFF":
  111.     pwd = GetAttribute(pwdtxt1,"VALUE")
  112.   else:
  113.     pwd = GetAttribute(pwdtxt2,"VALUE")
  114.  
  115.   if uid == "John" and pwd == "Spikowski":
  116.     Hide(errlbl)
  117.     Message("Login",  "Login Successful")
  118.     return IUP_CLOSE
  119.   else:
  120.     Show(errlbl)
  121.     SetFocus(uidtxt)
  122.  
  123.  

Nim has a '{.global.}' pragma, but it essentially functions like the 'static' keyword does in C.  Won't work in this case, because it looks like the call to 'Open' sets up a proc so the variables are local to that but 'static' as in C.



Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #250 on: December 28, 2019, 08:18:56 PM »
That worked.

Thanks.

I'm spoiled by SB not having to declare anything.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #251 on: December 29, 2019, 05:03:15 AM »
I agree that one should be able to hit the return key to tab from User ID to Password and to the login button.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #252 on: December 29, 2019, 02:45:32 PM »
Nim Changes

I'm using the state argument returned from the toggle callback rather than using GetAttribute to obtain it. The ENTER key now defaults to clicking the Login button if pressed.

ScriptBasic Changes

The ENTER key now defaults to clicking the Login button if pressed.

I think I'm going to give Nim a try on Windows 10. My hope is to get IUP and COM / OLE working on that platform.
« Last Edit: December 29, 2019, 04:00:11 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: RaspberryBASIC.org Forum
« Reply #253 on: December 29, 2019, 04:52:52 PM »
C CHANGES
Switched from using gtk_*_new for each object type, to g_object_new, which eliminates the need for g_object_set() for setting the widget properties.

Pressing "Enter" in the "Username" text box will move the focus to the "Password" text box, which in turn will execute the "onClick" callback when enter is pressed in the "Password" text box.

Code: C
  1. /* logon2.c
  2.  *
  3.  * version 1.3
  4.  *
  5.  * GUI Logon Screen Challenge Submission
  6.  * C version, using GTK+-3.0
  7.  *
  8.  * Written by Armando I. Rivera (AIR)
  9.  *
  10.  * Compile:  gcc logon2.c $(pkg-config --libs --cflags gtk+-3.0) -o logon2
  11. */
  12.  
  13. #include <gtk/gtk.h>
  14.  
  15.  
  16. GtkWidget *err_label;
  17.  
  18. void chkBox_cb (GtkToggleButton *toggle_button, gpointer data) {
  19.       if (gtk_toggle_button_get_active (toggle_button)) {
  20.           g_object_set(data,"visibility",TRUE,NULL);
  21.       }else{
  22.           g_object_set(data,"visibility",FALSE,NULL);
  23.       }
  24. }
  25.  
  26. void txtPass_cb( GtkWidget *widget, gpointer data ) {
  27.     g_object_set(data,"label","",NULL);
  28. }
  29.  
  30. void txtUser_cb( GtkWidget *widget, gpointer data ) {
  31.     gtk_widget_grab_focus(data);
  32. }
  33.  
  34. void onClick( GtkWidget *widget, gpointer data ) {
  35.     gchar *stupid_password = "pa$$w0rd!";
  36.     gchar *user_password;
  37.     g_object_get(data,"text",&user_password,NULL);
  38.  
  39.     if (g_strcmp0 (stupid_password,user_password) == 0) {
  40.         g_print("You are now logged in!\n");
  41.         gtk_main_quit();
  42.     }else{
  43.         gtk_label_set_markup(GTK_LABEL(err_label), "<span color=\"red\" font_desc=\"16.0\">** Invalid Password **</span>");
  44.         g_print("Username or Password is Incorrect!\n");
  45.  
  46.     }
  47. }
  48.  
  49. int main( int argc, char *argv[])
  50. {
  51.     GtkWidget *window, *layout, *image, *btnLogin, *chkBox;
  52.     GtkWidget *lblUser, *lblPass, *txtUser, *txtPass;
  53.  
  54.     gtk_init(&argc, &argv);
  55.    
  56.     layout = gtk_layout_new(NULL, NULL);
  57.  
  58.     window = g_object_new(GTK_TYPE_WINDOW,
  59.                         "type",GTK_WINDOW_TOPLEVEL,
  60.                         "title","Login",
  61.                         "default-width",660,
  62.                         "default-height",370,
  63.                         "resizable",FALSE,
  64.                         "window-position",GTK_WIN_POS_CENTER,
  65.                         "child",layout,
  66.                         "decorated",0,
  67.                         NULL);
  68.  
  69.     image = g_object_new(GTK_TYPE_IMAGE,"file","logon.png",NULL);
  70.     g_object_set(layout,"child",image,"margin",10,NULL);
  71.    
  72.     lblUser = g_object_new(GTK_TYPE_LABEL,"use-markup",TRUE,"label","<span font_desc=\"16.0\">Username:</span>",NULL);
  73.     lblPass = g_object_new(GTK_TYPE_LABEL,"use-markup",TRUE,"label","<span font_desc=\"16.0\">Password:</span>",NULL);
  74.     err_label = g_object_new(GTK_TYPE_LABEL, "width-request", 270,NULL);
  75.  
  76.     txtUser = g_object_new(GTK_TYPE_ENTRY,NULL);
  77.     txtPass = g_object_new(GTK_TYPE_ENTRY,"visibility",FALSE,NULL);    
  78.    
  79.     chkBox = g_object_new(GTK_TYPE_CHECK_BUTTON,"label","Show Password",NULL);
  80.  
  81.     btnLogin = g_object_new(GTK_TYPE_BUTTON,"label","Login","width-request",170,NULL);
  82.  
  83.     gtk_layout_put(GTK_LAYOUT(layout), lblUser, 330, 112-30);
  84.     gtk_layout_put(GTK_LAYOUT(layout), lblPass, 330, 162-30);
  85.     gtk_layout_put(GTK_LAYOUT(layout), txtUser, 460, 110-30);
  86.     gtk_layout_put(GTK_LAYOUT(layout), txtPass, 460, 160-30);
  87.     gtk_layout_put(GTK_LAYOUT(layout), chkBox, 460, 210-30);
  88.     gtk_layout_put(GTK_LAYOUT(layout), btnLogin, 460, 250-30);
  89.     gtk_layout_put(GTK_LAYOUT(layout), err_label, 300, 16);
  90.  
  91.     g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  92.     g_signal_connect (btnLogin, "clicked", G_CALLBACK (onClick), txtPass);
  93.     g_signal_connect (chkBox, "toggled", G_CALLBACK (chkBox_cb), txtPass);
  94.     g_signal_connect (txtPass, "changed", G_CALLBACK (txtPass_cb), err_label);
  95.     g_signal_connect (txtPass, "activate", G_CALLBACK (onClick), txtPass);
  96.     g_signal_connect (txtUser, "activate", G_CALLBACK (txtUser_cb), txtPass);
  97.    
  98.     gtk_widget_show_all(window);
  99.  
  100.     gtk_main();
  101.  
  102.     return 0;
  103. }
  104.  
  105.  
  106.  

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: RaspberryBASIC.org Forum
« Reply #254 on: December 29, 2019, 06:31:36 PM »
I'm going to be happy with return clicking the login button. In the day, the accounting software I worked on allowed enter as well as tab with the added ability to move around the form with the arrow keys.

« Last Edit: December 29, 2019, 06:34:05 PM by John »