example3.e 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. class EXAMPLE3
  2. -- A small app to convert inches to centimeters and viceversa.
  3. insert
  4. IUP_INTERFACE
  5. create {ANY}
  6. make
  7. feature {ANY}
  8. t1, t2: IUP_TEXT
  9. rd: IUP_RADIO
  10. make
  11. local
  12. gui: IUP
  13. i: STRING
  14. op1, op2: IUP_TOGGLE
  15. l: IUP_LABEL
  16. b: IUP_BUTTON
  17. h1, h2: IUP_HBOX
  18. v: IUP_VBOX
  19. w: IUP_DIALOG
  20. do
  21. gui := iup_open
  22. -- Create two toggle buttons and associate names with these.
  23. create op1.toggle("in to cm")
  24. op1.set_widget_name("in/cm")
  25. create op2.toggle("cm to in")
  26. op2.set_widget_name("cm/in")
  27. -- Put toggle buttons inside a radio container.
  28. create h1.hbox({ARRAY[IUP_WIDGET] 1, << op1, op2 >>})
  29. create rd.radio(h1)
  30. -- Create two text field, one for the user entry and other for the
  31. -- result.
  32. create t1.text
  33. t1.set_mask("IUP_MASK_FLOAT") -- Only allow float numbers.
  34. create t2.text
  35. t2.set_readonly(True)
  36. t2.set_can_focus(False)
  37. -- A label
  38. create l.label("=")
  39. -- Put these inside an horizontal box.
  40. create h2.hbox({ARRAY[IUP_WIDGET] 1, << t1, l, t2 >>})
  41. h2.set_gap(10)
  42. -- Create a button and connect the callback.
  43. create b.button("Convert")
  44. b.set_cb_action(agent convert_units(?))
  45. b.set_maxsize(70, 30)
  46. b.set_expand("YES")
  47. -- Put all the widgets inside a vertical box.
  48. create v.vbox({ARRAY[IUP_WIDGET] 1, << rd, h2, b >>})
  49. v.set_alignment("ACENTER")
  50. v.set_gap(10)
  51. v.set_margin(10, 10)
  52. -- Create the window.
  53. create w.dialog(v)
  54. w.set_title("Convert units")
  55. i := w.show
  56. gui.main_loop
  57. gui.close
  58. end
  59. convert_units (widget: IUP_WIDGET): STRING
  60. local
  61. str, tgl: STRING
  62. rst: REAL
  63. do
  64. str := t1.get_value
  65. tgl := rd.get_value
  66. if tgl.is_equal("in/cm") then
  67. rst := 2.54*str.to_real
  68. else
  69. rst := 0.393701*str.to_real
  70. end
  71. t2.set_value(rst.to_string)
  72. Result := "IUP_DEFAULT"
  73. end
  74. end -- class EXAMPLE3