loadmodule "luaiup" loadmodule "luahandle" loadmodule "lualibs" if not iupe then iupe = {} end if not iupep then iupep = {} end iupep.hexview_toolbox = { clipboard = iup.clipboard{}, getcursor = function (self, hexview) local rcursor = self.cursorlist.value - 3 local r if rcursor == -2 then r = hexview.kcursor elseif rcursor == -1 then r = hexview.mcursor else r = hexview.markers[rcursor] end return r + 0, markercolors[rcursor] end, hexview_cb = function (self, hexview, reset) local filecursor = hexview.filecursor local filesize = hexview.filesize local fcl = self.cursors.file fcl.dec.title = filecursor fcl.hex.title = hex(filecursor, "0x%08x") local rend = filesize - filecursor fcl.rend_dec.title = rend fcl.rend_hex.title = hex(rend, "0x%08x") local cursor, bgcolor = self:getcursor(hexview) local oldcursor = self.oldcursor + 0 if not reset and (oldcursor == cursor or cursor == -1) then return iup.DEFAULT end self.oldcursor = cursor local cl = self.cursors.normal cl.dec.title = cursor cl.hex.title = hex(cursor, "0x%08x") rend = filesize - cursor cl.rend_dec.title = rend cl.rend_hex.title = hex(rend, "0x%08x") cl.dec.bgcolor = bgcolor cl.hex.bgcolor = bgcolor cl.rend_dec.bgcolor = bgcolor cl.rend_hex.bgcolor = bgcolor local endianess = self.endianess.value + 0 local nbbytes = math.min(4, filesize - cursor) local handle = hexview.handle local v = self.values if not handle or nbbytes == 0 then for _, w in ipairs(v) do w.title = "n/a" end return iup.DEFAULT end local buf = Buffer(true) handle:seek(cursor) buf:copyfrom(handle, nbbytes) local u8 = buf:readU8() buf:seek(0) v.u8.title = u8 v.i8.title = u8 > 127 and u8 - 256 or u8 if nbbytes >= 2 then local u16 if endianess == 1 then u16 = buf:readU16() else local u1, u2 u1 = buf:readU8() u2 = buf:readU8() u16 = u2 + u1 * 256 end buf:seek(0) v.u16.title = u16 v.i16.title = u16 > 32767 and u16 - 65536 or u16 else v.u16.title = "n/a" v.i16.title = "n/a" end if nbbytes >= 4 then local u32 if endianess == 1 then u32 = buf:readU32() else local u1, u2, u3, u4 u1 = buf:readU8() u2 = buf:readU8() u3 = buf:readU8() u4 = buf:readU8() u32 = u4 + u3 * 256 + u2 * 65536 + u1 * 16777216 end buf:seek(0) if endianess == 2 then local u1, u2, u3, u4 u1 = buf:readU8() u2 = buf:readU8() u3 = buf:readU8() u4 = buf:readU8() buf:reset() buf:writeU8(u4) buf:writeU8(u3) buf:writeU8(u2) buf:writeU8(u1) end local flt = buf:readFloat() v.u32.title = u32 v.i32.title = u32 > 2147483647 and u32 - 4294967296 or u32 v.flt.title = flt else v.u16.title = "n/a" v.i16.title = "n/a" v.flt.title = "n/a" end buf:destroy() return iup.DEFAULT end, cursor_change = function (self, hexview, toolbox, rend) local ok, value = iup.GetParam("Cursor change", nil, "%s\n", self.title) local isint isint, value = pcall(function() return value + 0 end) if not isint then return iup.DEFAULT end local newvalue = rend and (hexview.filesize - value) or value hexview:setcursor(toolbox.cursorlist.value - 3, newvalue) return iup.DEFAULT end, filecursor_change = function (self, hexview, toolbox, rend) local ok, value = iup.GetParam("File position change", nil, "%s\n", self.title) local isint isint, value = pcall(function() return value + 0 end) if not isint then return iup.DEFAULT end hexview.filecursor = math.min(math.max(rend and (hexview.filesize - value) or value, 0), hexview.filesize) toolbox:hexview_cb(hexview) hexview:updatescrollbar() hexview:draw() return iup.DEFAULT end, value_enquery = function (self) -- iup.GetParam("Value copy/paste", nil, "%s\n", self.title) iupep.hexview_toolbox.clipboard.text = self.title end, create = function (tab) local hexview = tab.hexview tab.hexview = nil local cursorlabel_dec = iup.button{ tip = "Position of active cursor.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cursorlabel_hex = iup.button{ tip = "Position of active cursor.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cursorlabel_rend_dec = iup.button{ tip = "Position of active cursor,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cursorlabel_rend_hex = iup.button{ tip = "Position of active cursor,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local filecursorlabel_dec = iup.button{ tip = "Position of window.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local filecursorlabel_hex = iup.button{ tip = "Position of window.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local filecursorlabel_rend_dec = iup.button{ tip = "Position of window,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local filecursorlabel_rend_hex = iup.button{ tip = "Position of window,\nrelative to the end.\nClick to select/change.", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cursorlist = iup.list{ tip = "Active cursor.", dropdown = "Yes", value = "1", "Keyboard", "Mouse", "Cursor 0", "Cursor 1", "Cursor 2", "Cursor 3", "Cursor 4", "Cursor 5", "Cursor 6", "Cursor 7", "Cursor 8", "Cursor 9", "Cursor 10" } local endianess = iup.list{ tip = "Display value endianess.", dropdown = "Yes", value = "1", "Little Endian", "Big Endian" } local cvlabel_u8 = iup.button{ tip = "Type: uint8.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cvlabel_i8 = iup.button{ tip = "Type: int8.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cvlabel_u16 = iup.button{ tip = "Type: uint16.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cvlabel_i16 = iup.button{ tip = "Type: int16.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cvlabel_u32 = iup.button{ tip = "Type: uint32.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cvlabel_i32 = iup.button{ tip = "Type: int32.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local cvlabel_flt = iup.button{ tip = "Type: float.\nClick to copy value in clipboard", expand = "Horizontal", font = "Courier", alignment = "ARIGHT", flat = "Yes", spacing = "0", canfocus = "No", focusonclick = "No" } local r = iup.frame { iup.vbox { iup.label{ title = "Cursor", font = "Helvetica, Bold 10" }, cursorlist, iup.label{ separator = "Horizontal" }, iup.label{ title = "Cursor position", font = "Helvetica, Bold 10" }, cursorlabel_dec, cursorlabel_rend_dec, cursorlabel_hex, cursorlabel_rend_hex, iup.label{ separator = "Horizontal" }, iup.label{ title = "Cursor value", font = "Helvetica, Bold 10" }, endianess, cvlabel_i8, cvlabel_u8, cvlabel_i16, cvlabel_u16, cvlabel_i32, cvlabel_u32, cvlabel_flt, iup.label{ separator = "Horizontal" }, iup.label{ title = "File position", font = "Helvetica, Bold 10" }, filecursorlabel_dec, filecursorlabel_rend_dec, filecursorlabel_hex, filecursorlabel_rend_hex, iup.fill{}, }, unpack(tab), } r.cursors = { normal = { dec = cursorlabel_dec, hex = cursorlabel_hex, rend_dec = cursorlabel_rend_dec, rend_hex = cursorlabel_rend_hex, }, file = { dec = filecursorlabel_dec, hex = filecursorlabel_hex, rend_dec = filecursorlabel_rend_dec, rend_hex = filecursorlabel_rend_hex, }, } r.values = { u8 = cvlabel_u8, i8 = cvlabel_i8, u16 = cvlabel_u16, i16 = cvlabel_i16, u32 = cvlabel_u32, i32 = cvlabel_i32, flt = cvlabel_flt, } r.cursorlist = cursorlist r.endianess = endianess r.getcursor = iupep.hexview_toolbox.getcursor r.hexview_cb = iupep.hexview_toolbox.hexview_cb r.oldcursor = -1 hexview:registercb(r.hexview_cb, r) function cursorlist:action(text, pos, state) r:hexview_cb(hexview) end function endianess:action(text, pos, state) r:hexview_cb(hexview, true) end cursorlabel_dec.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, false) end cursorlabel_hex.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, false) end cursorlabel_rend_dec.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, true) end cursorlabel_rend_hex.action = function (self) return iupep.hexview_toolbox.cursor_change(self, hexview, r, true) end filecursorlabel_dec.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, false) end filecursorlabel_hex.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, false) end filecursorlabel_rend_dec.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, true) end filecursorlabel_rend_hex.action = function (self) return iupep.hexview_toolbox.filecursor_change(self, hexview, r, true) end cvlabel_u8.action = iupep.hexview_toolbox.value_enquery cvlabel_i8.action = iupep.hexview_toolbox.value_enquery cvlabel_u16.action = iupep.hexview_toolbox.value_enquery cvlabel_i16.action = iupep.hexview_toolbox.value_enquery cvlabel_u32.action = iupep.hexview_toolbox.value_enquery cvlabel_i32.action = iupep.hexview_toolbox.value_enquery cvlabel_flt.action = iupep.hexview_toolbox.value_enquery r:hexview_cb(hexview) return r end, } iupe.hexview_toolbox = iupep.hexview_toolbox.create