summaryrefslogtreecommitdiff
path: root/iup/srcconsole/console5.lua
blob: 20f74b2ea8d0fe63d1b395c876381e378da915c0 (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
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
require"iuplua"

iup.console = {}

-- Utilities

function iup.console.printtable(t)
  local n,v = next(t, nil)
  print("--printtable Start--")
  while n do
    print(tostring(n).."="..tostring(v))
    n,v = next(t, n)
  end
  print("--printtable End--")
end

function iup.console.print_version_info()
  if (im) then print("IM " .. im._VERSION .. "  " .. im._COPYRIGHT) end

  if (cd) then print("CD " .. cd._VERSION .. "  " .. cd._COPYRIGHT) end

  print("IUP " .. iup._VERSION .. "  " .. iup._COPYRIGHT)
  print("")
  print("IUP Info")
  print("  System: " .. iup.GetGlobal("SYSTEM"))
  print("  System Version: " .. iup.GetGlobal("SYSTEMVERSION"))

  local mot = iup.GetGlobal("MOTIFVERSION")
  if (mot) then print("  Motif Version: ", mot) end

  print("  Screen Size: " .. iup.GetGlobal("SCREENSIZE"))
  print("  Screen Depth: " .. iup.GetGlobal("SCREENDEPTH"))

  if (iup.GL_VENDOR) then print("  OpenGL Vendor: " .. iup.GL_VENDOR) end
  if (iup.GL_RENDERER) then print("  OpenGL Renderer: " .. iup.GL_RENDERER) end
  if (iup.GL_VERSION) then print("  OpenGL Version: " .. iup.GL_VERSION) end
end

-- Console Dialog

iup.console.lastfilename = nil -- Last file open
iup.console.mlCode = iup.multiline{expand="YES", size="200x120", font="COURIER_NORMAL_10"}
iup.console.lblPosition = iup.label{title="0:0", size="50x"}
iup.console.lblFileName = iup.label{title="", size="50x", expand="HORIZONTAL"}

function iup.console.mlCode:caret_cb(lin, col)
  iup.console.lblPosition.title = lin..":"..col
end

iup.console.butExecute = iup.button{size="50x15", title="Execute",
                                    action="iup.dostring(iup.console.mlCode.value)"}
iup.console.butClearCommands = iup.button{size="50x15", title="Clear",
                                          action="iup.console.mlCode.value=''  iup.console.lblFileName.title = ''  iup.console.lastfilename = nil"}
iup.console.butLoadFile = iup.button{size="50x15", title="Load..."}
iup.console.butSaveasFile = iup.button{size="50x15", title="Save As..."}
iup.console.butSaveFile = iup.button{size="50x15", title="Save"}

function iup.console.butSaveFile:action()
  if (iup.console.lastfilename == nil) then
    iup.console.butSaveasFile:action()
  else
    newfile = io.open(iup.console.lastfilename, "w+")
    if (newfile) then
      newfile:write(iup.console.mlCode.value)
      newfile:close()
    else
      error ("Cannot Save file "..filename)
    end
  end
end

function iup.console.butSaveasFile:action()
  local fd = iup.filedlg{dialogtype="SAVE", title="Save File",
                         filter="*.*", filterinfo="All files",allownew=yes}
  fd:popup(iup.LEFT, iup.LEFT)
  local status = fd.status
  iup.console.lastfilename = fd.value
  iup.console.lblFileName.title = iup.console.lastfilename
  fd:destroy()
  if status ~= "-1" then
    if (iup.console.lastfilename == nil) then
      error ("Cannot Save file "..filename)
    end
    local newfile=io.open(iup.console.lastfilename, "w+")
    if (newfile) then
      newfile:write(iup.console.mlCode.value)
      newfile:close(newfile)
    else
      error ("Cannot Save file")
    end
   end
end

function iup.console.LoadFile(filename)
  local newfile = io.open (filename, "r")
  if (newfile == nil) then
    error ("Cannot load file "..filename)
  else
    iup.console.mlCode.value=newfile:read("*a")
    newfile:close (newfile)
    iup.console.lastfilename = filename
    iup.console.lblFileName.title = iup.console.lastfilename
  end
end

function iup.console.butLoadFile:action()
  local fd=iup.filedlg{dialogtype="OPEN", title="Load File",
                       filter="*.*", filterinfo="All Files", allownew="NO"}
  fd:popup(iup.CENTER, iup.CENTER)
  local status = fd.status
  local filename = fd.value
  fd:destroy()
  
  if (status == "-1") or (status == "1") then
    if (status == "1") then
      error ("Cannot load file "..filename)
    end
  else
    iup.console.LoadFile(filename)
  end
end

iup.console.vbxConsole = iup.vbox
{
  iup.frame{iup.hbox{iup.vbox{iup.console.butLoadFile,
                              iup.console.butSaveFile,
                              iup.console.butSaveasFile,
                              iup.console.butClearCommands,
                              iup.console.butExecute;
                              margin="0x0", gap="10"},
                     iup.vbox{iup.console.lblFileName,
                              iup.console.mlCode,
                              iup.console.lblPosition;
                              alignment = "ARIGHT"};
                     alignment="ATOP"}; title="Commands"}
   ;alignment="ACENTER", margin="5x5", gap="5"
}

-- Main Menu Definition.

iup.console.mnuMain = iup.menu
{
  iup.submenu
  {
    iup.menu
    {
      iup.item{title="Exit", action="return iup.CLOSE"}
    }; title="File"
  },
  iup.submenu{iup.menu
  {
    iup.item{title="Print Version Info...", action=iup.console.print_version_info},
    iup.item{title="About...", action="iup.console.dlgAbout:popup(iup.CENTER, iup.CENTER)"}
  };title="Help"}
}

-- Main Dialog Definition.

iup.console.dlgMain = iup.dialog{iup.console.vbxConsole;
                                 title="IupLua Console",
                                 menu=iup.console.mnuMain,
                                 dragdrop = "YES",
                                 defaultenter=iup.console.butExecute,
                                 close_cb = "return iup.CLOSE"}

function iup.console.dlgMain:dropfiles_cb(filename, num, x, y)
  if (num == 0) then
    iup.console.LoadFile(filename)
  end
end

-- About Dialog Definition.

iup.console.dlgAbout = iup.dialog
{
  iup.vbox
  {
      iup.label{title="IupLua Console"},
      iup.fill{size="5"},
      iup.fill{size="5"},
      iup.frame
      {
          iup.vbox
          {
              iup.label{title="Tecgraf/PUC-Rio"},
              iup.label{title="iup@tecgraf.puc-rio.br"}
          }
      },
      iup.fill{size="5"},
      iup.button{title="OK", action="return iup.CLOSE", size="50X20"}
      ;margin="10x10", alignment="ACENTER"
   }
   ;maxbox="NO", minbox="NO", resize="NO", title="About"
}

-- Displays the Main Dialog

iup.console.dlgMain:show()
iup.SetFocus(iup.console.mlCode)

iup.MainLoop()

iup.console.dlgMain:destroy()
iup.console.dlgAbout:destroy()