code update .
"Hex Edit" is a gadget allows the user to View and edit the hex and ascii of memory data,is fast and support a big size of memory .
is easy to use and you can change style:
Functions:
HexEdit_GetBytePositionFromXYimage:
Return the Byte position by mouse coordinate x y
HexEdit_Refresh:
update Scroll and redraw image, ideal if you changing the size of memory
HexEdit_Free:
free gadget
HexEdit_Resize;
Resize gadget
HexEdit_SetNumberOfBytesPerLine:
change the number of bytes per line
HexEdit_GetSelectedBytesSize:
Return length of the selected hex or ascii in bytes
HexEdit_GetFristSelectedBytePosition:
Return the start Byte position of the selection
HexEdit_GetCanvasGadgetId:
Return PB CanvasGadget Id
HexEdit_SetCallbackReadData:
SetCallbackReadData read example for more info
HexEdit_SetCallbackWriteData:
SetCallbackWriteData read example for more info
HexEdit_SetUserID:
set gadget identified value
HexEdit_GetUserID:
Return gadget identified value
HexEdit_GetMemorySize;
Return Memory Size
HexEdit_Enable:
Enable Gadget .
HexEdit_Disable:
Disable Gadget
HexEdit_GetSelectedBlock:
Return current Selected block = #OffsetBlock | #HexBlock | #AsciiBlock
HexEdit_SetBackImage:
Set background image gadget
HexEdit_FocusBytePosition:
Select position
HexEdit_GetFocusedBytePosition:
Return Selected position
HexEdit_SetSelectedRange:
Selects a range of memory
HexEdit_SelectAll:
Select All Bytes
HexEdit_EmptySelect:
Cancel selection
HexEdit_Clear:
clear gadget
HexEdit_ScrollFocusedBytePosition:
scroll to selected position
HexEdit_SetBinary:
View memory data
HexEdit_SetMemorySize:
change memory size and update scroll page size
HexEdit_GetMemoryAddress:
Return current memory address
HexEdit_SetMemoryAddress:
change memory Address
HexEdit_Create:
Create Gadget
HEXEDIT.PBI
Code: Select all
;- By celtic88 :) 2018.
EnableExplicit
Prototype.i CallbackReadData(*HexEdit_Info)
Prototype.i CallbackWriteData(*HexEdit_Info)
Structure SelectArea ;Selection
BlockSelected.l ;Current Selected Block
Direction.l ;Select Direction
ByteStart.q ;byte position at the beginning of the selection
ByteEnd.q ;byte position at the end of the selection
ByteFixStart.q ;first selected position
ByteFixEnd.q ;last selected position
RegionStart.l ;current selected block start position
RegionEnd.l ;current selected block end position
EndStructure
Structure Color ;Color
Back.l ;Background Color
Hex.l ;hex string color
Ascii.l ;ascii string color
Offset.l ;offset string color
Slash.l ;focused byte color
SelectFace.l ;selected byte string color
SelectBack.l ;selected byte string back color
HeaderHex.l ;header hex string color
HeaderAscii.l ;header ascii string color
HeaderOffset.l;header offset string color
LineBlock.l ;line Separator color
EndStructure
Structure HexEdit_Info ;HexEdit gadget structure
*HexEditUserID ;pointer to arbitrary data
CanvasGadgetId.i ;Canvas Gadget Id
ScrollVBarGadgetId.i ;ScrollVBar Gadget Id
ScrollHBarGadgetId.i ;ScrollHBar Gadget Id
ImageId.i ;drawing image
ImageBackId.i ;Selected image Background
CanvasGadgetWidth.l ;Canvas Gadget Width
CanvasGadgetHeight.l ;Canvas Gadget Height
ScrollVBarPos.l ;ScrollVBar position
ScrollHBarPos.l ;ScrollHBar position
ByteCountByLine.l ;current Count of bytes per line
HeaderHeight.l ;header size height
AsciiWidthMax.l ;Ascii character size Width
AsciiHeightMax.l ;Ascii character size Height
HexByteSquareWidth.l ;Hex Square size Width
HexCharWidthMax.l ;Hex character size Width
OffsetBlockWidth.l ;Block "Offset" size Width
HexBlockWidth.l ;Block "Hex" size Width
AsciiBlockWidth.l ;Block "Ascii" size Width
WorkSpaceWidth.l ;Current image Canvas Width
ImageWidth.l ;Current image Width
SeparatorBlockWidth.l ;space between blocks "width"
SeparatorHexWidth.l ;space between hex character "width"
SeparatorLineHeight.l ;space between lines "Height"
*MemoryAddress ;current MemoryAddress
MemorySize.q ;memory size
MemoryStartPos.q
MemoryPos.q ;current memory position
MemoryReadPos.q ;offset of "Memory Address" to be read
LineToshowCount.l ;maximum number of lines to display
ByteCountToshowMax.l ;maximum number of Bytes to display
SelectOn.l ;selection is started
SelectOff.l ;selection is stopped
SelectArea.SelectArea ;selection area info
SelectBytePos.q ;current focused byte
ClickTimer.q ;"Click" Timer to detect click type
Color.Color ;coloring
CallbackReadData.CallbackReadData ;callback function
CallbackWriteData.CallbackWriteData ;callback function
ByteToWrite.l ;byte to write
ByteStepToWrite.l
EndStructure
;
; Procedure CallbackReadData(*HexEdit_Info.HexEdit_Info); CallbackWriteData Example
; With *HexEdit_Info
; \HexEditUserID
; \MemoryAddress
; \MemoryReadPos
; \ByteCountToshowMax
;
; EndWith
; EndProcedure
;
; Procedure CallbackWriteData(*HexEdit_Info.HexEdit_Info); CallbackWriteData Example
; With *HexEdit_Info
; \HexEditUserID
; \SelectBytePos
; \ByteToWrite
;
; EndWith
; EndProcedure
;
Structure HexEdit_DicByte
ByteToHex.s
ByteToChr.s
EndStructure
Enumeration Block
#OffsetBlock ;Offset Block id
#HexBlock ;Hex Block id
#AsciiBlock ;Ascii Block id
EndEnumeration
;Selection Direction
#SelectDirectionUp = -1
#SelectDirectionDown = 1
;Color by default
#ColorBack = $FFFFFFFF
#ColorHex = $FF000000
#ColorAscii = $FF0000ff
#ColorOffset = $FFFF0000
#ColorSlash = $6582004B
#ColorSelectFace = $DD808080
#ColorSelectBack = $FF000000
#ColorHeaderHex = $FF8B8B00
#ColorHeaderAscii = $FF000000
#ColorHeaderOffset = $FF000000
#ColorLineBlock = $FF000000
;Size by default
#SizeSeparatorBlockWidth = 20
#SizeSeparatorHexWidth = 7
#SizeHeaderHeight = 50
#SizeSeparatorLineHeight = 7
#NumberOfByteByLine = 16
Global Dim HexEdit_DicByteToHexToChr.HexEdit_DicByte(255)
Procedure HexEdit_SetDictionary()
Protected p.l
For p = 0 To 255
HexEdit_DicByteToHexToChr(p)\ByteToHex = RSet(Hex(p),2,"0")
HexEdit_DicByteToHexToChr(p)\ByteToChr = Chr(p)
Next
HexEdit_DicByteToHexToChr(0)\ByteToChr = "."
EndProcedure
HexEdit_SetDictionary()
Procedure HexEdit_GetCharMaxSize(*pHexEdit_Info.HexEdit_Info,DevOutput.i)
With *pHexEdit_Info
Protected.l z,w,s,h
If StartDrawing(DevOutput)
For z = 0 To 255
w = TextWidth(Chr(z))
h = TextHeight(Chr(z))
If w > \AsciiWidthMax:\AsciiWidthMax = w:EndIf
If h > \AsciiHeightMax:\AsciiHeightMax = h:EndIf
If (z >= 65 And z <= 70) Or (z >= 48 And z <= 57)
If w > \HexCharWidthMax :\HexCharWidthMax = w:EndIf
EndIf
Next
StopDrawing()
EndIf
EndWith
EndProcedure
Procedure HexEdit_UpdateGadgetScroll(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
Protected PageSize.q = (Round(\MemorySize / \ByteCountByLine, #PB_Round_Up) * \AsciiHeightMax ) + \HeaderHeight + (2*\AsciiHeightMax)
SetGadgetAttribute(\ScrollVBarGadgetId,#PB_ScrollBar_Maximum,PageSize/\AsciiHeightMax)
SetGadgetAttribute(\ScrollVBarGadgetId,#PB_ScrollBar_PageLength,\CanvasGadgetHeight/\AsciiHeightMax)
SetGadgetAttribute(\ScrollHBarGadgetId,#PB_ScrollBar_Maximum,\WorkSpaceWidth)
SetGadgetAttribute(\ScrollHBarGadgetId,#PB_ScrollBar_PageLength,\CanvasGadgetWidth)
\LineToshowCount = Round((\CanvasGadgetHeight-\HeaderHeight) / \AsciiHeightMax,#PB_Round_Down)
\ByteCountToshowMax = \LineToshowCount * \ByteCountByLine
EndWith
EndProcedure
Procedure HexEdit_DrawImage(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
Protected Z.l ,l.l,SelectByte.l
Protected HexStartpos ,LineHeight =\HeaderHeight,ASciiStartpos
Protected Byte.l
\ScrollVBarPos = GetGadgetState(\ScrollVBarGadgetId)
\ScrollHBarPos = GetGadgetState(\ScrollHBarGadgetId)
\MemoryPos = \ScrollVBarPos * \ByteCountByLine
\MemoryStartPos = \MemoryPos
\MemoryReadPos = \MemoryPos
If \CallbackReadData:\CallbackReadData(*pHexEdit_Info):EndIf
If StartDrawing(ImageOutput(\ImageId))
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
;Header draw
FillMemory(DrawingBuffer(), DrawingBufferPitch() * \CanvasGadgetHeight, $00,#PB_Long)
;Box(0, 0, \ImageWidth, \CanvasGadgetHeight, \Color\Back)
DrawText(10,10,"Offset",\Color\HeaderOffset,\Color\Back)
DrawText(\OffsetBlockWidth + \HexBlockWidth + \SeparatorHexWidth,10,"Ascii",\Color\HeaderAscii,\Color\Back)
HexStartpos = \OffsetBlockWidth + \SeparatorHexWidth
For Z = 0 To \ByteCountByLine-1
DrawText(HexStartpos,10,HexEdit_DicByteToHexToChr(Z)\ByteToHex,\Color\HeaderHex,\Color\Back)
HexStartpos + \HexByteSquareWidth + \SeparatorHexWidth
Next
Box(0, \HeaderHeight-5, \WorkSpaceWidth, 3, \Color\LineBlock)
Box(\OffsetBlockWidth-5, \HeaderHeight-5 , 1, \CanvasGadgetHeight- (\HeaderHeight-5), \Color\LineBlock)
If \MemoryAddress
For l = 1 To \LineToshowCount
HexStartpos = \OffsetBlockWidth + \SeparatorHexWidth
ASciiStartpos = \OffsetBlockWidth + \HexBlockWidth + \SeparatorHexWidth;!
DrawText(10,LineHeight,RSet(Hex(\MemoryPos),9,"0"),\Color\Offset,\Color\Back);offset
For Z = 0 To \ByteCountByLine-1
If \MemoryPos >= \MemorySize:Break 2:EndIf
Byte = PeekA(\MemoryAddress + \MemoryReadPos)
SelectByte = 0
If \SelectOn = #True
If \MemoryPos >= \SelectArea\ByteFixStart And \MemoryPos <= \SelectArea\ByteFixEnd
SelectByte = 1
EndIf
EndIf
If HexStartpos + \HexByteSquareWidth >= \ScrollHBarPos + \OffsetBlockWidth And
HexStartpos - \ScrollHBarPos < \CanvasGadgetWidth ; hex block is show
If SelectByte
DrawText(HexStartpos,LineHeight,HexEdit_DicByteToHexToChr(Byte)\ByteToHex,\Color\SelectFace,\Color\SelectBack)
Else
DrawText(HexStartpos,LineHeight,HexEdit_DicByteToHexToChr(Byte)\ByteToHex,\Color\Hex,\Color\Back)
EndIf
If \SelectArea\BlockSelected = #HexBlock And \MemoryPos = \SelectBytePos And \SelectOff = #True
Box(HexStartpos, LineHeight, \HexByteSquareWidth, \AsciiHeightMax - \SeparatorLineHeight, \Color\Slash)
EndIf
EndIf
If ASciiStartpos +\AsciiWidthMax >= \ScrollHBarPos + \OffsetBlockWidth And
ASciiStartpos - \ScrollHBarPos < \CanvasGadgetWidth ; ascii block is show
If SelectByte
DrawText(ASciiStartpos,LineHeight,HexEdit_DicByteToHexToChr(Byte)\ByteToChr,\Color\SelectFace,\Color\SelectBack)
Else
DrawText(ASciiStartpos,LineHeight,HexEdit_DicByteToHexToChr(Byte)\ByteToChr,\Color\Ascii,\Color\Back)
EndIf
If \SelectArea\BlockSelected = #AsciiBlock And \MemoryPos = \SelectBytePos And \SelectOff = #True
Box(ASciiStartpos, LineHeight, \AsciiWidthMax, \AsciiHeightMax - \SeparatorLineHeight, \Color\Slash)
EndIf
EndIf
ASciiStartpos + \AsciiWidthMax;TextWidth(Chr(Byte))
HexStartpos + \HexByteSquareWidth + \SeparatorHexWidth
\MemoryPos + 1
\MemoryReadPos + 1
Next
LineHeight + \AsciiHeightMax
Next
EndIf
;Split image
Protected ImageOffset = GrabDrawingImage(#PB_Any, 0, 0,
\OffsetBlockWidth, \CanvasGadgetHeight)
Protected ImageHex = GrabDrawingImage(#PB_Any, \ScrollHBarPos+\OffsetBlockWidth, 0,
\ImageWidth-(\ScrollHBarPos+\OffsetBlockWidth), \CanvasGadgetHeight)
StopDrawing()
EndIf
If StartDrawing(CanvasOutput(\CanvasGadgetId))
If \ImageBackId ;draw Background
DrawImage(ImageID(\ImageBackId ), 0, 0,\CanvasGadgetWidth, \CanvasGadgetHeight)
EndIf
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(0, 0, \CanvasGadgetWidth, \CanvasGadgetHeight, \Color\Back)
DrawImage(ImageID(ImageOffset), 0, 0)
DrawImage(ImageID(ImageHex), \OffsetBlockWidth, 0)
StopDrawing()
EndIf
FreeImage(ImageOffset)
FreeImage(ImageHex)
EndWith
EndProcedure
Procedure HexEdit_EventVScroll()
Protected *pHexEdit_Info.HexEdit_Info = GetGadgetData(EventGadget())
With *pHexEdit_Info
If \ScrollVBarPos <> GetGadgetState(\ScrollVBarGadgetId)
HexEdit_DrawImage(*pHexEdit_Info)
EndIf
SetActiveGadget(\CanvasGadgetId)
EndWith
EndProcedure
Procedure HexEdit_EventHScroll()
Protected *pHexEdit_Info.HexEdit_Info = GetGadgetData(EventGadget())
With *pHexEdit_Info
If \ScrollHBarPos <> GetGadgetState(\ScrollHBarGadgetId)
HexEdit_DrawImage(*pHexEdit_Info)
EndIf
SetActiveGadget(\CanvasGadgetId)
EndWith
EndProcedure
Procedure.q HexEdit_GetBytePositionFromXYimage(*pHexEdit_Info.HexEdit_Info,x.l,y.l)
With *pHexEdit_Info
Protected MemPos.q = \MemoryStartPos
Protected Bytey.l = y - \HeaderHeight
Bytey / \AsciiHeightMax
Bytey * \ByteCountByLine
x + \ScrollHBarPos
Protected ByteX.l = x - (\OffsetBlockWidth + \SeparatorHexWidth)
If \SelectArea\BlockSelected = #HexBlock
ByteX / (\HexByteSquareWidth + \SeparatorHexWidth )
Else
ByteX - \HexBlockWidth
ByteX / \AsciiWidthMax
EndIf
MemPos + Bytey + ByteX
If MemPos < 0 :MemPos = 0:EndIf
If MemPos > \MemorySize:MemPos = \MemorySize-1:EndIf
ProcedureReturn MemPos
EndWith
EndProcedure
Procedure HexEdit_EventInput()
Protected *pHexEdit_Info.HexEdit_Info = GetGadgetData(EventGadget())
With *pHexEdit_Info
Protected MouseX.l = GetGadgetAttribute(\CanvasGadgetId, #PB_Canvas_MouseX)
Protected MouseY.l = GetGadgetAttribute(\CanvasGadgetId, #PB_Canvas_MouseY)
Protected MouseWheelDelta.l = GetGadgetAttribute(\CanvasGadgetId, #PB_Canvas_WheelDelta)
Protected Input.l = GetGadgetAttribute(\CanvasGadgetId, #PB_Canvas_Input)
Select EventType()
Case #PB_EventType_RightButtonDown, #PB_EventType_LeftButtonDown
If MouseX > \OffsetBlockWidth
\SelectArea\RegionStart = \OffsetBlockWidth
\SelectArea\RegionEnd = \OffsetBlockWidth + \HexBlockWidth
\SelectArea\RegionStart - \ScrollHBarPos
\SelectArea\RegionEnd - \ScrollHBarPos
\SelectArea\BlockSelected = #HexBlock
If MouseX + \ScrollHBarPos >= \OffsetBlockWidth + \HexBlockWidth
\SelectArea\BlockSelected = #AsciiBlock
\SelectArea\RegionStart + \HexBlockWidth
\SelectArea\RegionEnd + \AsciiBlockWidth
EndIf
EndIf
\ByteStepToWrite = 0
\SelectArea\Direction = #SelectDirectionDown
\SelectOn = #True
\SelectArea\ByteStart = HexEdit_GetBytePositionFromXYimage(*pHexEdit_Info,MouseX,MouseY)
\SelectBytePos = \SelectArea\ByteStart
\ClickTimer = ElapsedMilliseconds()
If EventType() = #PB_EventType_LeftButtonDown
\SelectArea\ByteFixStart = \SelectArea\ByteStart
\SelectArea\ByteFixEnd = \SelectArea\ByteStart
\SelectOff = #False
Else
\SelectOff = #True
HexEdit_DrawImage(*pHexEdit_Info)
EndIf
Case #PB_EventType_MouseMove
If \SelectOff =#False And \SelectOn = #True
If MouseX < \SelectArea\RegionStart:MouseX = \SelectArea\RegionStart:EndIf
If MouseX > \SelectArea\RegionEnd:MouseX = \SelectArea\RegionEnd:EndIf
\SelectArea\ByteEnd = HexEdit_GetBytePositionFromXYimage(*pHexEdit_Info,MouseX,MouseY)
\SelectArea\ByteFixStart = \SelectArea\ByteStart
\SelectArea\ByteFixEnd = \SelectArea\ByteEnd
\SelectArea\Direction = #SelectDirectionDown
If \SelectArea\ByteEnd < \SelectArea\ByteStart
\SelectArea\ByteFixStart = \SelectArea\ByteEnd
\SelectArea\ByteFixEnd = \SelectArea\ByteStart
\SelectArea\Direction = #SelectDirectionUp
EndIf
If MouseY < 0:ElseIf MouseY > \CanvasGadgetHeight
MouseY - \CanvasGadgetHeight
Else
MouseY =0
EndIf
SetGadgetState(\ScrollVBarGadgetId, \ScrollVBarPos + MouseY)
HexEdit_DrawImage(*pHexEdit_Info)
EndIf
Case #PB_EventType_LeftButtonUp
\ClickTimer = ElapsedMilliseconds() - \ClickTimer
If \ClickTimer < 250
\SelectOff = #True
\SelectOn = #False
ElseIf \SelectOn = #True
\SelectOff = #True
EndIf
\SelectBytePos = \SelectArea\ByteFixEnd
If \SelectArea\Direction = #SelectDirectionUp
\SelectBytePos = \SelectArea\ByteFixStart
EndIf
HexEdit_DrawImage(*pHexEdit_Info)
Case #PB_EventType_MouseWheel
SetGadgetState(\ScrollVBarGadgetId, \ScrollVBarPos - MouseWheelDelta)
HexEdit_DrawImage(*pHexEdit_Info)
Case #PB_EventType_Input
If \CallbackWriteData
\ByteToWrite = Input
If \SelectArea\BlockSelected = #HexBlock
If (Input > 47 And Input < 58) Or
(Input > 64 And Input < 71) Or
(Input > 96 And Input < 103)
If \MemoryAddress And \SelectBytePos < \MemorySize
Protected PeekA = PeekA(\MemoryAddress + \SelectBytePos)
EndIf
Protected ChrHex.s = "$" + Chr(Input) + Hex(PeekA & $0f)
If \ByteStepToWrite = 1
\ByteStepToWrite = 0
ChrHex = "$" + Hex(PeekA >>4 & $0f) + Chr(Input)
Else
\ByteStepToWrite = 1
EndIf
\ByteToWrite = Val(ChrHex)
Else
ProcedureReturn
EndIf
EndIf
\CallbackWriteData(*pHexEdit_Info)
If \ByteStepToWrite = 0:\SelectBytePos + 1:EndIf
If \SelectBytePos+3 > \MemoryPos
SetGadgetState(\ScrollVBarGadgetId, \ScrollVBarPos + 1)
EndIf
HexEdit_DrawImage(*pHexEdit_Info)
EndIf
EndSelect
EndWith
EndProcedure
Procedure HexEdit_Refresh(*pHexEdit_Info.HexEdit_Info)
HexEdit_UpdateGadgetScroll(*pHexEdit_Info)
HexEdit_DrawImage(*pHexEdit_Info)
EndProcedure
Procedure HexEdit_Free(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
If \ImageBackId :FreeImage(\ImageBackId) :EndIf
FreeImage(\ImageId)
FreeGadget(\CanvasGadgetId)
FreeGadget(\ScrollVBarGadgetId)
FreeGadget(\ScrollHBarGadgetId)
FreeMemory(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_Resize(*pHexEdit_Info.HexEdit_Info,x.l,y.l,Width.l,Height.l)
With *pHexEdit_Info
Width - 15:Height - 15:If Width < 1:Width=1:EndIf:If Height < 1:Height=1:EndIf
ResizeGadget(\CanvasGadgetId,x,y,Width,Height)
ResizeGadget(\ScrollVBarGadgetId,x+Width,y,15,Height)
ResizeGadget(\ScrollHBarGadgetId,x,y+Height,Width,15)
\CanvasGadgetWidth = Width
\CanvasGadgetHeight = Height
FreeImage(\ImageId)
\ImageWidth = \WorkSpaceWidth
If \ImageWidth < \CanvasGadgetWidth: \ImageWidth=\CanvasGadgetWidth:EndIf
\ImageId = CreateImage(#PB_Any,\ImageWidth,Height,32,#PB_Image_Transparent)
SetGadgetState(\ScrollVBarGadgetId, 0):SetGadgetState(\ScrollVBarGadgetId, \MemoryStartPos / \ByteCountByLine)
HexEdit_Refresh(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_SetNumberOfBytesPerLine(*pHexEdit_Info.HexEdit_Info,Size.l)
With *pHexEdit_Info
\ByteCountByLine = Size
\HexBlockWidth = (\HexByteSquareWidth * \ByteCountByLine) + (\SeparatorHexWidth * \ByteCountByLine) + \SeparatorBlockWidth
\AsciiBlockWidth = (\AsciiWidthMax * \ByteCountByLine) + \SeparatorHexWidth
\WorkSpaceWidth = \OffsetBlockWidth + \AsciiBlockWidth + \HexBlockWidth
\ImageWidth = \WorkSpaceWidth
If \ImageWidth < \CanvasGadgetWidth: \ImageWidth=\CanvasGadgetWidth:EndIf
\ImageId = CreateImage(#PB_Any,\ImageWidth,\CanvasGadgetHeight,32,#PB_Image_Transparent)
HexEdit_UpdateGadgetScroll(*pHexEdit_Info)
SetGadgetState(\ScrollVBarGadgetId, 0):SetGadgetState(\ScrollVBarGadgetId, \MemoryStartPos / \ByteCountByLine)
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure.q HexEdit_GetSelectedBytesSize(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
If \SelectOn = #True:ProcedureReturn (\SelectArea\ByteFixEnd - \SelectArea\ByteFixStart) + 1:EndIf
EndWith
EndProcedure
Procedure.q HexEdit_GetFristSelectedBytePosition(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
If \SelectOn:ProcedureReturn \SelectArea\ByteFixStart:EndIf
EndWith
EndProcedure
Procedure.i HexEdit_GetCanvasGadgetId(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
ProcedureReturn \CanvasGadgetId
EndWith
EndProcedure
Procedure HexEdit_SetCallbackReadData(*pHexEdit_Info.HexEdit_Info,*CallbackReadData)
With *pHexEdit_Info
\CallbackReadData = *CallbackReadData
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_SetCallbackWriteData(*pHexEdit_Info.HexEdit_Info,*CallbackWriteData)
With *pHexEdit_Info
\CallbackWriteData = *CallbackWriteData
EndWith
EndProcedure
Procedure HexEdit_SetUserID(*pHexEdit_Info.HexEdit_Info,*ID)
With *pHexEdit_Info
\HexEditUserID = *ID
EndWith
EndProcedure
Procedure.i HexEdit_GetUserID(*pHexEdit_Info.HexEdit_Info,*ID)
With *pHexEdit_Info
ProcedureReturn \HexEditUserID
EndWith
EndProcedure
Procedure.q HexEdit_GetMemorySize(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
ProcedureReturn \MemorySize
EndWith
EndProcedure
Procedure HexEdit_Enable(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
DisableGadget(\CanvasGadgetId,0)
DisableGadget(\ScrollHBarGadgetId,0)
DisableGadget(\ScrollVBarGadgetId,0)
EndWith
EndProcedure
Procedure HexEdit_Disable(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
DisableGadget(\CanvasGadgetId,1)
DisableGadget(\ScrollHBarGadgetId,1)
DisableGadget(\ScrollVBarGadgetId,1)
EndWith
EndProcedure
Procedure.l HexEdit_GetSelectedBlock(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
ProcedureReturn \SelectArea\BlockSelected
EndWith
EndProcedure
Procedure HexEdit_SetBackImage(*pHexEdit_Info.HexEdit_Info,Image.i)
With *pHexEdit_Info
If \ImageBackId :FreeImage(\ImageBackId) :EndIf
\ImageBackId = Image
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_FocusBytePosition(*pHexEdit_Info.HexEdit_Info,BytePos.q)
With *pHexEdit_Info
\SelectBytePos = BytePos
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure.q HexEdit_GetFocusedBytePosition(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
ProcedureReturn \SelectBytePos
EndWith
EndProcedure
Procedure HexEdit_SetSelectedRange(*pHexEdit_Info.HexEdit_Info,ByteSPos.q,ByteEPos.q)
With *pHexEdit_Info
\SelectOn = #True
\SelectOff = #True
\SelectArea\ByteStart = ByteSPos
\SelectArea\ByteEnd = ByteEPos
\SelectArea\ByteFixStart = \SelectArea\ByteStart
\SelectArea\ByteFixEnd = \SelectArea\ByteEnd
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_SelectAll(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
\SelectOn = #True
\SelectOff = #True
\SelectArea\ByteFixStart = 0
\SelectArea\ByteFixEnd = \MemorySize
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_EmptySelect(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
\SelectOn = #False
\SelectOff = #False
\SelectArea\ByteFixStart = 0
\SelectArea\ByteFixEnd = 0
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_Clear(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
\SelectOn = #False
\SelectOff = #False
\SelectBytePos = 0
\MemorySize = 0
\MemoryAddress = 0
HexEdit_Refresh(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_ScrollFocusedBytePosition(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
SetGadgetState(\ScrollHBarGadgetId, 0):SetGadgetState(\ScrollHBarGadgetId, Mod(\SelectBytePos , \ByteCountByLine)* (\HexByteSquareWidth+\SeparatorHexWidth))
SetGadgetState(\ScrollVBarGadgetId, 0):SetGadgetState(\ScrollVBarGadgetId, \SelectBytePos / \ByteCountByLine)
HexEdit_DrawImage(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_SetBinary(*pHexEdit_Info.HexEdit_Info,*PBinary, BinarySize.q)
With *pHexEdit_Info
HexEdit_Clear(*pHexEdit_Info)
\MemorySize = BinarySize
\MemoryAddress = *PBinary
HexEdit_Refresh(*pHexEdit_Info)
EndWith
EndProcedure
Procedure HexEdit_SetMemorySize(*pHexEdit_Info.HexEdit_Info,BinarySize.q)
With *pHexEdit_Info
\MemorySize = BinarySize
HexEdit_Refresh(*pHexEdit_Info)
EndWith
EndProcedure
Procedure.i HexEdit_GetMemoryAddress(*pHexEdit_Info.HexEdit_Info)
With *pHexEdit_Info
ProcedureReturn \MemoryAddress
EndWith
EndProcedure
Procedure HexEdit_SetMemoryAddress(*pHexEdit_Info.HexEdit_Info, *MemoryAddress)
With *pHexEdit_Info
\MemoryAddress = *MemoryAddress
EndWith
EndProcedure
Procedure.i HexEdit_Create(x.l,y.l,Width.l,Height.l)
Protected *pHexEdit_Info.HexEdit_Info = AllocateMemory(SizeOf(HexEdit_Info))
With *pHexEdit_Info
Width - 15:Height - 15:If Width < 1:Width=1:EndIf:If Height < 1:Height=1:EndIf
\CanvasGadgetId = CanvasGadget(#PB_Any, x, y, Width, Height,#PB_Canvas_DrawFocus|#PB_Canvas_Keyboard)
HexEdit_GetCharMaxSize(*pHexEdit_Info,CanvasOutput(\CanvasGadgetId))
SetGadgetData(\CanvasGadgetId,*pHexEdit_Info)
BindGadgetEvent(\CanvasGadgetId ,@HexEdit_EventInput())
\Color\Back = #ColorBack
\Color\Hex = #ColorHex
\Color\Ascii = #ColorAscii
\Color\Offset = #ColorOffset
\Color\Slash = #ColorSlash
\Color\SelectFace = #ColorSelectFace
\Color\SelectBack = #ColorSelectBack
\Color\HeaderHex = #ColorHeaderHex
\Color\HeaderAscii = #ColorHeaderAscii
\Color\HeaderOffset = #ColorHeaderOffset
\Color\LineBlock = #ColorLineBlock
\CanvasGadgetWidth = Width
\CanvasGadgetHeight = Height
\SeparatorLineHeight = #SizeSeparatorLineHeight
\AsciiHeightMax + #SizeSeparatorLineHeight
\SeparatorBlockWidth = #SizeSeparatorBlockWidth
\SeparatorHexWidth = #SizeSeparatorHexWidth
\ByteCountByLine = #NumberOfByteByLine
\HeaderHeight = #SizeHeaderHeight
\HexByteSquareWidth = \HexCharWidthMax * 2
\OffsetBlockWidth = (\HexCharWidthMax * 8) + \SeparatorHexWidth + \SeparatorBlockWidth
\HexBlockWidth = (\HexByteSquareWidth * \ByteCountByLine) + (\SeparatorHexWidth * \ByteCountByLine) + \SeparatorBlockWidth
\AsciiBlockWidth = (\AsciiWidthMax * \ByteCountByLine) ;+ \SeparatorHexWidth
\WorkSpaceWidth = \OffsetBlockWidth + \AsciiBlockWidth + \HexBlockWidth
\ImageWidth = \WorkSpaceWidth
If \ImageWidth < \CanvasGadgetWidth: \ImageWidth=\CanvasGadgetWidth:EndIf
\ImageId = CreateImage(#PB_Any,\ImageWidth,Height,32,#PB_Image_Transparent)
\ScrollVBarGadgetId = ScrollBarGadget(#PB_Any, Width + x, y, 15, Height, 0, 0, 1,#PB_ScrollBar_Vertical)
BindGadgetEvent(\ScrollVBarGadgetId, @HexEdit_EventVScroll())
SetGadgetData(\ScrollVBarGadgetId,*pHexEdit_Info)
\ScrollHBarGadgetId = ScrollBarGadget(#PB_Any, x, y+Height, Width, 15, 0, \WorkSpaceWidth, \CanvasGadgetWidth)
BindGadgetEvent(\ScrollHBarGadgetId, @HexEdit_EventHScroll())
SetGadgetData(\ScrollHBarGadgetId,*pHexEdit_Info)
HexEdit_Refresh(*pHexEdit_Info)
EndWith
ProcedureReturn *pHexEdit_Info
EndProcedure
DisableExplicit
Example
Code: Select all
IncludeFile "HEXEDIT.PBI"
DisableExplicit
If OpenWindow(0, 0, 0, 900, 650, "test HEXEDIT.PBI",#PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_WindowCentered)
Global hHexEdit_Info = HexEdit_Create(0,0,900,650)
Size = ((1024*1024)*11) - 1
*mem = AllocateMemory(size)
RandomData(*mem,size)
HexEdit_SetBinary(hHexEdit_Info,*mem,size)
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
CurrentMemoryAddress = HexEdit_GetMemoryAddress(hHexEdit_Info)
If CurrentMemoryAddress
FreeMemory(CurrentMemoryAddress)
EndIf
HexEdit_Free(hHexEdit_Info)
EndIf
Enjoy!!
goodbye