summaryrefslogtreecommitdiff
path: root/test/lua/view_image.lua
blob: c156bd338f1927310487d4072f92709db85faeb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require"imlua"
require"cdlua"
require"cdluaim"
require"iuplua"
require"iupluacd"

iup.key_open()

function View_Image(image, title)
  ret = false

  cnv = iup.canvas{}
  
  function cnv:action()
    local canvas = dlg.canvas
    local image = dlg.image
    
    if (not canvas) then return end
    
    canvas:Activate()
    cw, ch = canvas:GetSize()
    iw = image:Width()
    ih = image:Height()
    
    if (iw > ih) then
      h = iw/iw * ch
      y = (ch-h)/2
      x = 0
      w = cw
    else
      w = iw/ih * ch
      x = (cw-w)/2
      y = 0
      h = ch
    end
    
    canvas:Clear()
    image:cdCanvasPutImageRect(canvas, x, y, w, h, 0, 0, 0, 0)
  end

  function cnv:button_cb()
    dlg:close_cb()
    ret = true
    return iup.CLOSE
  end
  
  dlg = iup.dialog{iup.vbox{cnv, iup.label{title="Click to accept or press Esc to abort."}}}
  dlg.placement="maximized"
  dlg.title = title
  dlg.cnv = cnv
  dlg.image = image
  
  function dlg:k_any(c)
  print("K_any("..c..")")
    if (c == iup.K_ESC) then
      dlg:close_cb()
      return iup.CLOSE
    end
  end

  function dlg:close_cb()
    local canvas = self.canvas
    if canvas then canvas:Kill() end
  end

  function dlg:map_cb()
    canvas = cd.CreateCanvas(cd.IUP, self.cnv)
    self.canvas = canvas
  end
  
  dlg:show()
  iup.MainLoop()
  dlg:destroy()
  
  return ret
end