' ASCII Dialog box ...
' GWS 2013

$include "ctl.inc"

DIALOG d
INT lW,lH,item,clip
STRING ch1[35] 		:' holds control characters 0 to 3
STRING ch2[35]		:' extended codes 127 to 160
STRING a$

'Listview UDT ..
TYPE NMLISTVIEW
	def hwndFrom:UINT
	def idFrom:INT
	def code:INT
  def iItem:INT
  def iSubItem:INT
  def uNewState:UINT
  def uOldState:UINT
  def uChanged:UINT
  def ptActionx:INT
	def ptActiony:INT
  def lParam:INT
ENDTYPE

' memory and listview variable to read LV data
def mem:MEMORY
def lv:NMLISTVIEW

dW = 193
dH = 354
lW = 0.87*dW
lH = 0.75*dH

CREATEDIALOG d,0,0,dW,dH,0x80C80080,0,"ASCII Values",&messages

control d,@BUTTON,"Char",14,320,50,20,0x50000000,1
control d,@BUTTON,"Ascii",70,320,50,20,0x50000000,2
control d,@BUTTON,"Hex",130,320,50,20,0x50000000,3
control d,@STATIC,"",13,12,100,16,@CTEDITLEFT,6
control d,@STATIC,"",113,6,62,22,@CTEDITCENTER|0x200,7
control d,@LISTVIEW,"",0.07*dW,0.1*dH,lW,lH,0x50008005|@VSCROLL,10

' load the special characters for the ASCII table from 0 to 31 ..
ch1[0] = "NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL","BS","TAB","LF"
ch1[11] = "VT","FF","CR","SO","SI","DLE","DC1","DC2","DC3","DC4","NAK","SYN"
ch1[23] = "ETB","CAN","EM","SUB","ESC","FS","GS","RS","US"

' load the special characters for the ASCII table from 127 to 160 ..
ch2[0] = "DEL","€","ü","é","â","ä","à","å","ç","^","ë","è","<","î"
ch2[14] = "ì","Ä","Å","É","‘","’","ô","ö","ò","-"
ch2[24] = "ù","~","Ö","Ü",">","£","Ø","",""," "

domodal d
end

SUB messages(),INT
select @class
	case @idcontrol
		select @controlID
			case 1
				setcontrolcolor d,1,0,rgb(0,200,200)
				setcontrolcolor d,2,0,rgb(131,153,177)
				setcontrolcolor d,3,0,rgb(131,153,177)
				item = 0
			case 2
				setcontrolcolor d,2,0,rgb(0,200,200)
				setcontrolcolor d,1,0,rgb(131,153,177)
				setcontrolcolor d,3,0,rgb(131,153,177)
				item = 1
			case 3
				setcontrolcolor d,3,0,rgb(0,200,200)
				setcontrolcolor d,1,0,rgb(131,153,177)
				setcontrolcolor d,2,0,rgb(131,153,177)
				item = 2
			case 10
' Listview clicked - Read the data from @QUAL into mem
			if (@NOTIFYCODE = @NMCLICK)
				mem = @QUAL
				READMEM mem,1,lv
				CONTROLCMD d,10,@LVGETTEXT,lv.iItem,item,a$
				setcontroltext d,6,"Saved to Clipboard"
' copy a$ to the hidden RE control off the top of the screen
' check for some commonly used control characters ..
' (Form feed FF is not included at the moment since it's not particularly useful)
				select 1
					case (a$ = "Space")
						a$ = " "
						setcontroltext d,7,"(space)"
					case (a$ = "TAB")
						a$ = chr$(9)												:' this will work in source code and programs like Word
						setcontroltext d,7,"(tab)"
					case (a$ = "CR")
						a$ = "chr$(13)"											:' full string item needs to be pasted in source code to be useful
						setcontroltext d,7,"(CR)"
					case (a$ = "LF")
						a$ = "chr$(10)"											:' full string item needs to be pasted in source code to be useful
						setcontroltext d,7,"(LF)"
					default
'Copy chosen item to clipboard, and check
	clip=SendToClipBoard(d,LTRIM$(a$))
IF clip=0
	MESSAGEBOX(d,"Item not copied to Clipboard","Copy to Clipboard",@MB_OK|@MB_ICONEXCLAMATION)
ENDIF
						setcontroltext d,7,ltrim$(a$)				:' display the chosen item in the text box
				endselect
	clip=SendToClipBoard(d,LTRIM$(a$))										:' copy to clipboard
IF clip=0
	MESSAGEBOX(d,"Item not copied to Clipboard","Copy to Clipboard",@MB_OK|@MB_ICONEXCLAMATION)
ENDIF
' LV item text is now in the clipboard, and cntl-V will paste it at the cursor ..
			endif
		endselect
	case @IDINITDIALOG
		centerwindow d
		setcontrolcolor d,1,0,rgb(0,200,200)
		setcontrolcolor d,7,0,rgb(0,250,250)
		setfont d,"Arial",8,500,0,6
		setfont d,"Arial",14,500,0,7
' insert LV columns and size them.
		CONTROLCMD d,10,@LVINSERTCOLUMN,0,"Char"
		CONTROLCMD d,10,@LVSETCOLWIDTH,0, 50
		CONTROLCMD d,10,@LVINSERTCOLUMN,1,"Ascii"
		CONTROLCMD d,10,@LVSETCOLWIDTH,1, 50
		CONTROLCMD d,10,@LVINSERTCOLUMN,2,"Hex"
		CONTROLCMD d,10,@LVSETCOLWIDTH,2, 50
' after an item is inserted, use @LVSETTEXT to change the subitem text
' fill the first part of the table ..
		for i = 0 to 32
			CONTROLCMD d,10,@LVINSERTITEM,i,ch1[i]
			CONTROLCMD d,10,@LVSETTEXT,i,1,str$(i)
			CONTROLCMD d,10,@LVSETTEXT,i,2,"0x"+hex$(i)
		next i
		CONTROLCMD d,10,@LVSETTEXT,32,0,"Space"
' load the easy characters 33 to 126 ..
		for i = 33 to 126
			CONTROLCMD d,10,@LVINSERTITEM,i,chr$(i)
			CONTROLCMD d,10,@LVSETTEXT,i,1,str$(i)
			CONTROLCMD d,10,@LVSETTEXT,i,2,"0x"+hex$(i)
		next i
' fill the rest of the table 127 to 255
		for i = 127 to 255
			CONTROLCMD d,10,@LVINSERTITEM,i,chr$(i)
			CONTROLCMD d,10,@LVSETTEXT,i,1,str$(i)
			CONTROLCMD d,10,@LVSETTEXT,i,2,"0x"+hex$(i)
		next i
' load characters 127 to 160 ..
		for i = 127 to 160
			CONTROLCMD d,10,@LVSETTEXT,i,0,ch2[i-127]
		next i
endselect
return 0
ENDSUB
