summaryrefslogtreecommitdiff
path: root/iup/srcconsole/console5.lua
blob: 0b93641fabfd646a085018ecf7d2c6f4cf8b9b4a (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
205
206
207
208
209
210
211
212
213
214
215
require"iuplua"

-- Utilities
iup_console = {}

function iup_console.concat(str, info)
  return str .. info .. "\n"
end

function iup_console.print_version_info()
  iup_console.clear()
  local str = ""
  if (im) then str = iup_console.concat(str, "IM " .. im._VERSION .. "  " .. im._COPYRIGHT) end

  if (cd) then str = iup_console.concat(str, "CD " .. cd._VERSION .. "  " .. cd._COPYRIGHT) end

  str = iup_console.concat(str, "IUP " .. iup._VERSION .. "  " .. iup._COPYRIGHT)
  str = iup_console.concat(str, "")
  str = iup_console.concat(str, "IUP Info")
  str = iup_console.concat(str, "  System: " .. iup.GetGlobal("SYSTEM"))
  str = iup_console.concat(str, "  System Version: " .. iup.GetGlobal("SYSTEMVERSION"))

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

  str = iup_console.concat(str, "  Screen Size: " .. iup.GetGlobal("SCREENSIZE"))
  str = iup_console.concat(str, "  Screen Depth: " .. iup.GetGlobal("SCREENDEPTH"))

  if (iup.GL_VENDOR) then str = iup_console.concat(str, "  OpenGL Vendor: " .. iup.GL_VENDOR) end
  if (iup.GL_RENDERER) then str = iup_console.concat(str, "  OpenGL Renderer: " .. iup.GL_RENDERER) end
  if (iup.GL_VERSION) then str = iup_console.concat(str, "  OpenGL Version: " .. iup.GL_VERSION) end
  
  iup_console.mlCode.value=str
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

function iup_console.clear()
  iup_console.mlCode.value=''  
  iup_console.lblFileName.title = ''  
  iup_console.lastfilename = nil
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.clear}
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", 
                         nochangedir="NO", directory=iup_console.last_directory,
                         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 = fd.value
  iup_console.last_directory = fd.directory
  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", 
                       nochangedir="NO", directory=iup_console.last_directory,
                       filter="*.*", filterinfo="All Files", allownew="NO"}
  fd:popup(iup.CENTER, iup.CENTER)
  local status = fd.status
  local filename = fd.value
  iup_console.last_directory = fd.directory
  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)

if (not iup.MainLoopLevel or iup.MainLoopLevel()==0) then
  iup.MainLoop()
end

iup_console.dlgMain:destroy()
iup_console.dlgAbout:destroy()