Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating sample hbox output IP result #9

Closed
nixfreak opened this issue Jan 9, 2022 · 8 comments
Closed

creating sample hbox output IP result #9

nixfreak opened this issue Jan 9, 2022 · 8 comments

Comments

@nixfreak
Copy link
Contributor

nixfreak commented Jan 9, 2022

Trying to figure out what to use for an output, do I use a dialog box or show or something else?

I would like to create a box with a button that shows your own IP address. Also opacity works great I was able to use opacity on a full window.

@dariolah
Copy link
Owner

Dialog is visual control and container (displays window and it can contain one control or container)

If you meant to have button with IP, you can add it directly to dialog:

  Dialog(Button("x.x.x.x"))

Common usage is to put another container into Dialog, one that can contain multiple controls, e.g. Vbox, Hbox, etc.

  let btn = Button( "x.x.x.x")
  let dlg = Dialog(
              Vbox(
                btn))

Or if it is input use Text(), and Label("some text") if you are displaying static text.

  let btn = Button( "Exit")
  let txt = Text()
  txt.value = "x.x.x.x"
  let dlg = Dialog(
              Vbox(
                txt,
                btn))

Take look at niup/text.nim

@nixfreak
Copy link
Contributor Author

nixfreak commented Jan 11, 2022

Its not a static IP though.
I created an hbox import niup

import std/[httpclient, strutils, strformat]


# Create an exit button proc
proc button_exit_callback(ih: PIhandle):cint {.cdecl} =
  # exists the main loop
   return IUP_CLOSE


const src = "https://icanhazip.com"
proc Ip*(): string =
   var client = newHttpClient()
   try:
     let response = client.getContent(src)
     result = response.strip()
   except:
     result = ""
   finally:
     client.close()
   echo Ip()


proc button_show_ip(ih: PIhandle): cstring {.cdecl.} =
      echo Ip()
# create main proc and open UIP application
proc mainProc =
  Open()

# create 3 variables (label, button, vbox)
 # var 
 #   label = Label("Hello, IUP from nim.")
 #   button = Button("Hit me to close")
 #   vbox = Vbox(label, button) # add button and label to vertical box 
 # # add attributes to variables 
 # vbox.alignment = IUP_ACENTER
 # vbox.gap(10)
 # vbox.margin(30, 60)


  var
    label_h = Label("IP address")
    button_h = Button("Show Ip Address")
    #label_h_output = Label(Ip)
    hbox = Hbox(label_h, button_h)
# add dialog box to show title of UIP application

  hbox.alignment = IUP_ACENTER
  hbox.gap(20)
  hbox.margin(20, 70)

  var dlg = Dialog(vbox) # add vbox to Dialog
  dlg.title = "Hello, World Title"

  var dlg_h = Dialog(hbox)

  dlg_h.title = "Hello horizonal window"
  #button_h.action = button_show_ip
  opacity(dlg_h,  115) # horizonal box now has opacity

# call backs 
  button.action = button_exit_callback
  button.action = button_show_ip

# show UIP application
  #ShowXY(dlg, IUP_CENTER, IUP_CENTER)
  ShowXY(dlg_h , IUP_CENTER, IUP_CENTER)

  MainLoop()
  Close()

if isMainModule: # if main module than call mainProc()
  mainProc()

@dariolah
Copy link
Owner

Here is slightly modified file
test.zip

Added missing niup import
Changed Label to read-only Text, so value can be changed through callback
Made it global so it is accessible from callback function
There can be only one callback in button action, later one is used. There are other callbacks for button if needed

import std/[httpclient, strutils, strformat]
import niup

var textLabel: Text_T  # global, so it can be accessed from callbacksi, init later, after IUP Open()

# Create an exit button proc
proc button_exit_callback(ih: PIhandle):cint {.cdecl} =
  # exists the main loop
   return IUP_CLOSE


const src = "https://icanhazip.com"
proc Ip*(): string =
   var client = newHttpClient()
   try:
     let response = client.getContent(src)
     result = response.strip()
   except:
     let
       e = getCurrentException()
       msg = getCurrentExceptionMsg()
     echo "Got exception ", repr(e), " with message ", msg
     result = ""
   finally:
     client.close()
   return result # there was recursive call to Ip()


proc button_show_ip(ih: PIhandle): cint {.cdecl.} = # every callback has cint as return value
  textLabel.value = Ip()
  return IUP_DEFAULT

# create main proc and open UIP application
proc mainProc =
  Open()

# create 3 variables (label, button, vbox)
 # var 
 #   label = Label("Hello, IUP from nim.")
 #   button = Button("Hit me to close")
 #   vbox = Vbox(label, button) # add button and label to vertical box 
 # # add attributes to variables 
 # vbox.alignment = IUP_ACENTER
 # vbox.gap(10)
 # vbox.margin(30, 60)


  textLabel = Text("IP address")  # declared globally so it can be accessed from callback
  textLabel.readonly = true

  var
    button_h = Button("Show Ip Address")
    #textLabel_output = Label(Ip)
    hbox = Hbox(textLabel, button_h)
# add dialog box to show title of UIP application

  hbox.alignment = IUP_ACENTER
  hbox.gap(20)
  hbox.margin(20, 70)

  var dlg_h = Dialog(hbox)

  dlg_h.title = "Hello horizonal window"
  #button_h.action = button_show_ip
  opacity(dlg_h,  115) # horizonal box now has opacity

# call backs 
  button_h.action = button_exit_callback
  button_h.action = button_show_ip

# show UIP application
  #ShowXY(dlg, IUP_CENTER, IUP_CENTER)
  ShowXY(dlg_h , IUP_CENTER, IUP_CENTER)

  MainLoop()
  Close()

if isMainModule: # if main module than call mainProc()
  mainProc()

I recommend to go through IUP tutorial (left side navigator: Tutorial)
https://webserver2.tecgraf.puc-rio.br/iup/

To learn IUP basics, philosophy behind it.
Parts of tutorial are in niupc/example*.nim

@nixfreak
Copy link
Contributor Author

So I went through UIP tutorial and more and started to drill into NUIP , playing around with a lot of the attributes.
Do you know how to get rid of the textbox lines though? I thought if I could match the border color but I don't see a proc for that for Text.

import std/[httpclient, strutils, strformat]
import niup

var textLabel: Text_t  # global, so it can be accessed from callbacksi, init later, after IUP Open()
# Create an exit button proc
#proc button_exit_callback(ih: PIhandle):cint {.cdecl} =
  # exists the main loop
   #return IUP_CLOSE


const src = "https://icanhazip.com"
proc Ip*(): string =
   var client = newHttpClient()
   try:
     let response = client.getContent(src)
     result = response.strip()
   except:
     let
       e = getCurrentException()
       msg = getCurrentExceptionMsg()
     echo "Got exception ", repr(e), " with message ", msg
     result = ""
   finally:
     client.close()
   return result # there was recursive call to Ip()


#proc button_show_ip(ih: PIhandle): cint {.cdecl.} = # every callback has cint as return value
#textLabel.value = Ip()
  #return IUP_DEFAULT

# create main proc and open UIP application
proc mainProc =
  Open()

# create 3 variables (label, button, vbox)
 # var
 #   label = Label("Hello, IUP from nim.")
 #   button = Button("Hit me to close")
 #   vbox = Vbox(label, button) # add button and label to vertical box
 # # add attributes to variables
 # vbox.alignment = IUP_ACENTER
 # vbox.gap(10)
 # vbox.margin(30, 60)

  textLabel = Text("IP address") # declared globally so it can be accessed from callback
  textLabel.readonly = true
  textLabel.border = "no"
  textLabel.size = "70x5"
  textLabel.bgcolor = "#7f11e0"
  textLabel.font = "Arial, 24"
  textLabel.alignment = IUP_ACENTER

  textLabel.value = Ip()
  var
    #button_h = Button("Show IP Address")
    #textLabel_output = Label(Ip)
    #hbox = Hbox(button_h, textLabel)
    hbox = Hbox(textLabel)

  #hbox.alignment = IUP_ACENTER
  hbox.gap(20)
  hbox.margin(0, 20)
# add dialog box to show title of UIP application

  var dlg_h = Dialog(hbox)


  dlg_h.title = "Show Current IP Address"
  dlg_h.bgcolor = "#7f11e0"

  #button_h.action = button_show_ip
  opacity(dlg_h,  190) # horizonal box now has opacity

# call backs
  #button_h.action = button_exit_callback
  #button_h.action = button_show_ip

# show UIP application
  #ShowXY(dlg, IUP_CENTER, IUP_CENTER)
  ShowXY(dlg_h , IUP_CENTER, IUP_CENTER)

  MainLoop()
  Close()

if isMainModule: # if main module than call mainProc()
  mainProc()

@dariolah
Copy link
Owner

Don't have experience with design, try:
BORDER (creation only): Shows a border around the text. Default: "YES".

In theory dynamic destroy/create of Label control could work, but I haven't tested it or seen examples of it.

@nixfreak
Copy link
Contributor Author

Its weird , if you put border = "yes" , you can really see it , but if you put to "no" then its just faded but still can see it.

@dariolah
Copy link
Owner

For IUP details best bet is to ask on mailing list.

subscribe to mailing list:
https://sourceforge.net/projects/iup/lists/iup-users

or browse mailing list archive:
https://sourceforge.net/p/iup/mailman/iup-users/

https://webserver2.tecgraf.puc-rio.br/iup/en/prod.html#suporte

@nixfreak
Copy link
Contributor Author

Unable to find a way to remove the border from a textbox , moving on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants