dariolah / nim-iup-examples

Nim GUI examples using IUP
20 stars 3 forks source link

Example TempConvert.c #11

Open nixfreak opened 2 years ago

nixfreak commented 2 years ago

Trying to re-create tempConvert.c using niup library

I get errors trying to SetAtrribute `ot <Text_t, string, string> but expected one of: proc SetAttribute(ih: PIhandle; name: cstring; value: cstring) first type mismatch at position: 1 required type for ih: PIhandle but expression 'textLabel_c' is of type: Text_t

expression: SetAttribute(textLabel_c, "value", "") In the example do you have to SetStrf to convert to a string? or do I need to parseString? `

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

var
  label_c: Label_t
  label_f: Label_t
  textLabel_c: Text_t
  textLabel_f: Text_t
  celsius: float
  fahrenheit: float

proc txt_celcius_cb(ih: PIhandle):cint {.cdecl.} =
  var fahrenheit = GetDialogChild(ih, "Fahrenheit")
  var value = (celsius * 1.8) + 32
  return IUP_DEFAULT

proc txt_fahrenheit_cb(ih: PIhandle):cint {.cdecl.} =
  var celcius = GetDialogChild(ih, "Celcius")
  var value = (fahrenheit - 32) / 1.8

  return IUP_DEFAULT

#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 2 labels and 2 textLabels
  #textLabel_f.value =
  label_c = Label("Celcius  " & " = ")
  textLabel_c = Text("")
  SetAttribute(textLabel_c, "value", "")
  SetAttribute(textLabel_c, "NAME", "Celcius")
  textLabel_c.alignment = IUP_ACENTER

  label_f  = Label("Fahrenheit")
  SetAttribute(textLabel_f, "value", "")
  SetAttribute(textLabel_f, "NAME", "Fahrenheit")
  textLabel_f = Text("")
  textLabel_f.alignment = IUP_ACENTER

  var
     hbox = Hbox(textLabel_c, label_c, textLabel_f, label_f)

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

  var dlg_h = Dialog(hbox)
  dlg_h.title = "Convert Temperature"
  dlg_h.bgcolor = "#7f11e0"

# call backs
  SetCallback(textLabel_c, "VALUECHANGED_CB",  (Icallback(txt_celcius_cb)))
  SetCallback(textLabel_f, "VALUECHANGED_CB",  (Icallback(txt_fahrenheit_cb)))
# 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 commented 2 years ago

Regarding SetAttribute ...

NIUPC module uses PIhandle (pointer to Ihandle, *Ihandle in C).

NIUP module uses Text_t, Button_t, Label_t, etc. types. They can be casted to PIhandle type and used in SetAttribute function you used

But there is better way

Each NIUP object created by Text(), Label(), Button() etc. can set attribute by calling function on object someObj.attributename

Setting attribute

var someText = Text()
someText.value = "lorem ipsum dolorem"  # in case there is only one parameter or string
# or
someText.value("lorem ipsum dolorem")

# in case there are multiple parmaters then `attribute=` function can't be used, has to be one with ()
someText.bgcolor(128,0,128)  # R G B, type checked for integers
# or
someText.bgcolor("128 0 128")  # as string
# or
someText.bgcolor = "128 0 128"  # as string
# this is same as
SetAttribute(cast[PIhandle](someText), cstring("BGCOLOR"), cstring("128 0 128"))
# which you have to use if you are calling NIUPC (C like) API

Single string parameter is supported for all attributes. https://github.com/dariolah/niup/blob/master/src/niup/inc/text.nim#L1258

You can check in NIUP cource for specific control what function overloads exists, e.g. bgcolor(int, int, int, int) https://github.com/dariolah/niup/blob/master/src/niup/inc/text.nim#L130

Same for callbacks

proc txt_celcius_cb(ih: PIhandle):cint {.cdecl.} =
  echo "this is txt celsius callback"
  return IUP_DEFAULT

someText.valuechanged_cb = txt_celcius_cb
# or
someText.valuechanged_cb(txt_celcius_cb)

You can check signature of callback function in NIUP source: https://github.com/dariolah/niup/blob/master/src/niup/inc/text.nim#L1783

Highlighted is callback function signature which matches txt_celcius_cb valuechanged_cb

Other callback may have other signatures, it is important to use correct ones or app will crash. E.g., K_ANY callback (any key pressed) https://github.com/dariolah/niup/blob/master/src/niup/inc/text.nim#L1638 For this signature k_any callback proc will look like:

proc my_k_any_cb(ih: PIhandle, c: cint):cint {.cdecl.} =
  echo "this is k_any callback"
  return IUP_DEFAULT

For description of parameters it's best to go to IUP documentation.

PS your callbacks don't do anything, just set up some variables which get optimized away. Use echo or something to confirm that you are calling callback. Then do some work in callback, e.g. set value to some control (textLabel_c.value = "whatever")

PPS in Nim you can use $ to convert variable to string

var num = 3
echo $num