summaryrefslogtreecommitdiff
path: root/iup/srclua3/iuplua.lua
blob: 5bfd7d4bc56a7d568b4295d44f5ba0368289f0c4 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134


-- #################################################################################
--                                 Private functions
-- #################################################################################

-- maps Ihandles into Lua objects
iup_handles = {}

settagmethod(iuplua_tag, "gettable", iup_gettable) 
settagmethod(iuplua_tag, "settable", iup_settable)
settagmethod (tag({}), "index", iup_index)

function _ALERT(s)
  local bt = iupbutton{title="Ok", size="60", action="return IUP_CLOSE"}
  local ml = iupmultiline{expand="YES", readonly="YES", value=s, size="300x150"}
  local vb = iupvbox{ml, bt; alignment="ACENTER", margin="10x10", gap="10"}
  local dg = iupdialog{vb; title="Lua Error",defaultesc=bt,defaultenter=bt,startfocus=bt}
  dg:popup(IUP_CENTER, IUP_CENTER)
  dg:destroy()
end

function type_string (o) 
  return type(o) == "string" 
end

function type_number (o) 
  return type(o) == "number" 
end

function type_nil (o)    
  return type(o) == "nil" 
end

function type_function (o)
  return type(o) == "function" 
end

function type_widget(w)
  if w then
    return iup_handles[w]
  else
    return nil
  end
end

function type_menu (o) 
  return type_widget(o) and (o.parent==IUPMENU) 
end

function type_item (o)
  return type_widget(o) and (o.parent==IUPITEM or o.parent==IUPSUBMENU or o.parent==IUPSEPARATOR)
end

function iupCallMethod(name, ...)
  local handle = arg[1] -- always the handle
 
  local func = handle[name] -- this is the old name
  if (not func) then
    local full_name = strlower(iup_callbacks[name][1])
    func = handle[full_name]  -- check also for the full name
    
    if (not func) then
      return
    end
  end
    
  if type_function (func) then
    return call(func, arg)
  elseif type_string(func) then
    local temp = self
    self = handle
    local result = dostring(func)
    self = temp
    return result
  else
    return IUP_ERROR
  end
end

function iupSetName (handle)
  if not type_string(iup_handles[handle].IUP_name) then
    iup_handles[handle].IUP_name = format("_IUPLUA_NAME(%s)", tostring(handle))
    IupSetHandle(handle.IUP_name, handle)
  end
end

function iupCreateChildrenNames (obj)
  if obj.parent.parent == COMPOSITION then
    local i = 1
    while obj[i] do
      iupCreateChildrenNames (obj[i])
      i = i+1
    end
  elseif obj.parent == IUPFRAME then
    iupCreateChildrenNames (obj[1])
  else
    iupSetName (obj)
  end
end


-- #################################################################################
--                              Public Functions
-- #################################################################################


function IupRGB (red, green, blue)
  return floor(red*255).." "..floor(green*255).." "..floor(blue*255)
end
iup.RGB = IupRGB

function IupRegisterHandle(handle, typename)
  if not iup_handles[handle] then
    local obj = getglobal("IUP"..strupper(typename))
    if not obj then
      obj = WIDGET
    end
    iup_handles[handle] = { parent=obj, handle=handle }
  end
  return handle
end
iup.RegisterHandle = IupRegisterHandle

function IupGetFromC(obj)
  local handle = IupGetHandle(obj[1])
  return IupRegisterHandle(handle, IupGetClassName(handle))
end

iup.GetFromC = function (name)
  local handle = IupGetHandle(name)
  return IupRegisterHandle(handle, IupGetClassName(handle))
end