example3.e 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. class EXAMPLE3
  2. -- A small app to convert inches to centimeters and viceversa.
  3. inherit
  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. a, b2, c: ARRAY[IUP_WIDGET]
  21. do
  22. gui := iup_open
  23. -- Create two toggle buttons and associate names with these.
  24. create op1.toggle("in to cm")
  25. op1.set_widget_name("in/cm")
  26. create op2.toggle("cm to in")
  27. op2.set_widget_name("cm/in")
  28. -- Put toggle buttons inside a radio container.
  29. a := {ARRAY[IUP_WIDGET]} << op1, op2 >>
  30. create h1.hbox(a)
  31. create rd.radio(h1)
  32. -- Create two text field, one for the user entry and other for the
  33. -- result.
  34. create t1.text
  35. t1.set_mask("IUP_MASK_FLOAT") -- Only allow float numbers.
  36. create t2.text
  37. t2.set_readonly(True)
  38. t2.set_can_focus(False)
  39. -- A label
  40. create l.label("=")
  41. -- Put these inside an horizontal box.
  42. b2 := {ARRAY[IUP_WIDGET]} << t1, l, t2 >>
  43. create h2.hbox(b2)
  44. h2.set_gap(10)
  45. -- Create a button and connect the callback.
  46. create b.button("Convert")
  47. b.set_cb_action(agent convert_units)
  48. -- Fill available horizontal space
  49. b.expand_horizontal_free
  50. -- Put all the widgets inside a vertical box.
  51. c := {ARRAY[IUP_WIDGET]} << rd, h2, b >>
  52. create v.vbox(c)
  53. v.set_alignment("ACENTER")
  54. v.set_gap(10)
  55. v.set_margin(10, 10)
  56. -- Create the window.
  57. create w.dialog(v)
  58. w.set_title("Convert units")
  59. i := w.show
  60. gui.main_loop
  61. gui.close
  62. end
  63. convert_units (widget: IUP_BUTTON): STRING
  64. local
  65. str, tgl: STRING
  66. rst: REAL_64
  67. do
  68. str := t1.get_value
  69. tgl := rd.get_value
  70. if tgl.is_equal("in/cm") then
  71. rst := 2.54*str.to_real
  72. else
  73. rst := 0.393701*str.to_real
  74. end
  75. t2.set_value(rst.out)
  76. Result := "IUP_DEFAULT"
  77. end
  78. end -- class EXAMPLE3