| 12
 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
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 
 | 
SimpleForm = {}
SimpleForm_mt = { __index = SimpleForm }
SF_LEFT="left"
SF_RIGHT="right"
SF_CENTER="center"
SFT_TEXT=0
SFT_PASSWORD=1
SFT_LIST=2
SFT_NUMBER=3
function SimpleForm:new()
  local inst = {}
  setmetatable(inst, SimpleForm_mt)
  return inst
end
function SimpleForm:init(title, width)
  self.title= title
  self.width= width
  self.form= nil
  self.height= 1
  self.objects= {}
  self.buttons= {}
  self.everything= {}
end
function SimpleForm:addSpace()
  self.height= self.height+1
end
function SimpleForm:addLabel(text, align)
  local label
  local x
  local y
  local txtlen
  y= self.height
  self.height= self.height + 1
  txtlen= string.len(text)
  if align == SF_LEFT then
    x= 1
  elseif align == SF_RIGHT then
    x= self.width-txtlen
  else
    x= (self.width-txtlen)/2
  end
  label= dlg.Label(x, y, text)
  table.insert(self.everything, label)
end
function SimpleForm:addCheck(name, text)
  local check
  check= dlg.Checkbox(2, self.height, text, 0)
  self.height= self.height+1
  table.insert(self.everything, check)
  self.objects[name]= {"check", check}
end
function SimpleForm:addEntries(forms)
  local name, caption, defval, etype
  local label, entry
  local x, y
  local leftWidth= 0
  local txtlen
  local row, _
  for _, row in forms do
    caption= row[2]
    if string.len(caption) > leftWidth then
      leftWidth= string.len(caption)
    end
  end
  for _, row in forms do
    name= row[1]
    caption= row[2]
    defval= row[3]
    etype= row[4]
    y= self.height
    self.height= self.height+1
    label= dlg.Label(1+leftWidth-string.len(caption), y, caption)
    if etype == SFT_TEXT then
      entry= dlg.Entry(leftWidth+1, y, self.width-leftWidth-2*1, defval, 0, 0)
      self.objects[name]= {"entry", entry}
    elseif etype == SFT_NUMBER then
      entry= dlg.Entry(leftWidth+1, y, 5, defval, 0, 0)
      self.objects[name]= {"entry", entry}
    elseif etype == SFT_PASSWORD then
      entry= dlg.Entry(leftWidth+1, y, self.width-leftWidth-2*1, defval, 0, 1)
      self.objects[name]= {"entry", entry}
    elseif etype == SFT_LIST then
      local item
      entry= dlg.Listbox(leftWidth+1, y, 1, 1, 1)
      for _, item in defval do
        entry:addItem(item)
      end
      self.objects[name]= {"listbox", entry}
    end
    table.insert(self.everything, label)
    table.insert(self.everything, entry)
  end
end
function SimpleForm:addListbox(name, options, visibleRows)
  local lbox
  local i
  local option
  lbox= dlg.Listbox(2, self.height, visibleRows, 1, 1)
  lbox:setWidth(self.width - 4)
  self.height= self.height + visibleRows
  for i, option in options do
    lbox:addItem(option)
  end
  self.objects[name]= {"listbox", lbox}
  table.insert(self.everything, lbox)
end
function SimpleForm:addCheckList(name, options, visibleRows)
  local lbox
  local i
  local option
  local oplist
  lbox= dlg.CheckboxTree(2, self.height, visibleRows, 1)
  lbox:setWidth(self.width - 4)
  self.height= self.height + visibleRows
  oplist= {}
  for i, option in options do
    local id= lbox:addItem(option[1], {}, option[2])
    table.insert(oplist, {option[3], id})
  end
  self.objects[name]= {"checklist", lbox, oplist}
  table.insert(self.everything, lbox)
end
function SimpleForm:addTreeView(options, visibleRows)
  local lbox
  local i, j, opti, optj
  lbox= dlg.CheckboxTree(2, self.height, visibleRows, 1, 1, 1)
  lbox:setWidth(self.width-4)
  for i, opti in options do
    lbox:addItem(opti[1], {dlg.ARG_APPEND}, 0)
    for j, optj in opti[2] do
      lbox:addItem(optj, {i-1, dlg.ARG_APPEND}, 0)
    end
  end
  table.insert(self.everything, lbox)
  self.height= self.height + visibleRows
end
function SimpleForm:addCheckTree(name, options, visibleRows)
  local lbox
  local i, j, opti, optj
  local itemlist= {}
  lbox= dlg.CheckboxTree(2, self.height, visibleRows, 1, 0, 0)
  lbox:setWidth(self.width-4)
  for i, opti in options do
    lbox:addItem(opti[1], {dlg.ARG_APPEND}, 0)
    for j, optj in opti[2] do
      local id= lbox:addItem(optj[1], {i-1, dlg.ARG_APPEND}, 0)
      table.insert(itemlist, {optj[2], id})
    end
  end
  table.insert(self.everything, lbox)
  self.height= self.height + visibleRows
  self.objects[name]= {"checktree", lbox, itemlist}
end
function SimpleForm:addWidget(w)
  table.insert(self.everything, w)
end
function SimpleForm:setButtons(lbuttons, rbuttons)
  local name, caption
  local width, b
  local button
  local x, y, _
  y= self.height
  self.height= self.height + 4
  x= 1
  for _, b in lbuttons do
    name= b[1]
    caption= b[2]
    button= dlg.Button(x, y, caption)
    self.buttons[name]= button
    table.insert(self.everything, button)
    x= x+string.len(caption)+5
  end
  x= self.width
  for _, b in rbuttons do
    x=x - (string.len(b[2])+5)
  end
  for _, b in rbuttons do
    name= b[1]
    caption= b[2]
    button= dlg.Button(x, y, caption)
    x= x+(string.len(caption)+5)
    self.buttons[name]= button
    table.insert(self.everything, button)
  end
end
function SimpleForm:getValue(name)
  local w= self.objects[name]
  if w[1] == "listbox" then
    return w[2]:getCurrent()
  elseif w[1] == "entry" then
    return w[2].entryValue
  elseif w[1] == "check" then
    return w[2].checkboxValue      
  elseif (w[1] == "checklist") or (w[1]=="checktree") then
    local oplist= w[3]
    local tree=w[2]
    local i
    local sel= {}
    for i=1,table.getn(oplist) do
      if tree:getEntryValue(oplist[i][2])[2] == 1 then
        table.insert(sel, oplist[i][1])
      end
    end
    return sel
  else
    return nil
  end
end
function SimpleForm:setHelpLine(help)
  self.helpline= help
end
function SimpleForm:run(dontPopDown)
  local w, _
  dlg.centeredWindow(self.width, self.height, self.title)
  self.form= dlg.Form()
  for _, w in self.everything do
    self.form:add(w)
  end
  result= self.form:run()
  if not(dontPopDown) then
    dlg.popWindow()
  end
  for name, button in self.buttons do
    if button:matchId(result[2])==1 then
      return name
    end
  end
  return nil
end
function SimpleForm:pop()
  dlg.popWindow()
end
 |