Simple Cross-Platform Gadgets with string input
Posted: Sun Jul 05, 2015 4:29 pm
@ All,
I wanted an easy string input system for a program I was writing for my work, I looked around and could not find anything, so I came up with the following. Seeing that this question had been asked several times, I decided to post what I have done.
I have only tested it in Hollywood 6 on AmigaOS 4.1 and on a window7 machine. I think it should be able to be used with lower versions of Holllywood.
I have added some basic comments, to help you understand what I have done. I am sure that there are better ways to do some of this but it works for the purpose it was intented for.
It should be easy to leave out the bits that you do not need.
To test it change the "Image.jpg" to a image file that you have in the same directory as the demo is saved.
Please post any inprovements so we all can learn.
Have fun.
Leo
I wanted an easy string input system for a program I was writing for my work, I looked around and could not find anything, so I came up with the following. Seeing that this question had been asked several times, I decided to post what I have done.
I have only tested it in Hollywood 6 on AmigaOS 4.1 and on a window7 machine. I think it should be able to be used with lower versions of Holllywood.
I have added some basic comments, to help you understand what I have done. I am sure that there are better ways to do some of this but it works for the purpose it was intented for.
Code: Select all
@VERSION 6,0
@APPTITLE "Gadgets"
@APPVERSION "$VER: 1.0 (04.07.15)"
@APPCOPYRIGHT "©2015, George Leo den Hollander"
@APPAUTHOR "Leo den Hollander"
@APPDESCRIPTION "Simple Gadgets"
@DISPLAY 1, {Width=400,Height=320,Color=#gray,Title=" Gadgets by Leo den Hollander"}
; Setup tables
GadgetText = {}
GadgetState = {}
GadgetFColor = {}
GadgetBColor = {}
GadgetFont = {}
GadgetFontSize = {}
; Setup Globals
global Gadget
global Gadgetx
global Gadgety
global Gadgetw
global Gadgeth
; Create Button Gadget - ID = Gadget ID, x y position of gadet on screen, w h external Gadget size,
; Text$ text on Gadget, BC background colour, FC foreground (Text) colour
Function GadgetButton(ID,x,y,w,h,Text$,BC,FC)
SetFillStyle(#FILLGradient,#Linear,#Silver,RGB(64,64,64),12) ; Gadget bevel
Box(x, y, w, h, #GRAY) ; Draw bevel
SetFillStyle(#FILLCOLOR) ; BC fill drawing
Box(x+2, y+2, w-4, h-4, BC) ; Gadget face
SetFont("DejaVu Sans",18) ; Font used on gadget face
TW = Textwidth(Text$) ; size of text on gadget face
t =(w/2)-(tw/2) ; Centre text position
SetFontColor(FC) ; Text colour
Textout(x+t,y+3,Text$) ; Write text to gadget
MakeButton(ID, #SIMPLEBUTTON, x+2, y+2, w-4, h-4, Events) ; Make buton same size as face
GadgetFColor[ID] = FC ; Record FC colour in table for redrawing
GadgetBColor[ID] = BC ; Record BC colour in table for redrawing
EndFunction
; Create Label Gadget - ID = Gadget ID, x y position of gadet on screen, w h external button size,
; Text$ text on Gadget, BC background colour, FC foreground (Text) colour
Function GadgetLabel(ID,x,y,w,h,Text$,BC,FC)
SetLineWidth(1) ; Set line thickness
SetFillStyle(#FILLGradient,#Linear,RGB(96,96,96),#Silver,27) ; Gadget bevel
Box(x, y, w, h, #Gray) ; Draw bevel
SetFillStyle(#FILLCOLOR) ; BC fill drawing
Box(x+2, y+2, w-4, h-4, BC) ; Button face
Line(x+1, y+1, x+w-2, y+1, #Black) ; Inner bevel
Line(x+1, y+1, x+1, y+h-2, #Black) ; Inner bevel
Line(x+1, y+h-2, x+w-2, y+h-2, #White) ; Inner bevel
Line(x+w-2, y+1,x+w-2,y+h-2, #White) ; Inner bevel
SetFont("DejaVu Sans",18) ; Font used on gadget face
SetFontColor(FC) ; Text colour
TextOut(x+4,y+3,Text$) ; Write text to gadget
GadgetFColor[ID] = FC ; Record FC colour in table for redrawing
GadgetBColor[ID] = BC ; Record BC colour in table for redrawing
EndFunction
; Create Check Gadget - ID = Gadget ID, x y position of gadet on screen,
; Text$ text at right of Gadget, BC background colour, FC foreground (Text) colour
Function GadgetCheck(ID,x,y,Text$,BC,FC)
SetLineWidth(1) ; Set line thickness
SetFillStyle(#FILLGradient,#Linear,RGB(96,96,96),#Silver,27) ; Gadget bevel
Box(x, y, 25,25, #Gray) ; Draw bevel
SetFillStyle(#FILLCOLOR) ; BC fill drawing
Box(x+2, y+2, 21, 21, BC) ; Gadget face
Line(x+1, y+1, x+23, y+1, #Black) ; Inner bevel
Line(x+1, y+1, x+1, y+23, #Black) ; Inner bevel
Line(x+1, y+23, x+23, y+23, #White) ; Inner bevel
Line(x+23, y+1,x+23,y+23, #White) ; Inner bevel
SetFont("DejaVu Sans",18) ; Font used on gadget right
SetFontColor(FC) ; Text colour
TextOut(x+30,y+3,Text$) ; Write text to gadget
MakeButton(ID, #SIMPLEBUTTON, x+2, y+2, 21, 21, Events) ; Make buton same size as face
GadgetState[ID] = 0 ; Set gadget state - Checked = 1 Unchecked = 0
GadgetFColor[ID] = FC ; Record FC colour in table for redrawing
GadgetBColor[ID] = BC ; Record BC colour in table for redrawing
EndFunction
; Create Option Gadget - ID = Gadget ID, x y position of gadet on screen,
; Text$ text at right of Gadget, BC background colour, FC foreground (Text) colour
Function GadgetOption(ID,x,y,Text$,BC,FC)
SetFormstyle(#ANTIALIAS) ; Set antialias on for nicer button
SetFillStyle(#FILLGradient,#Linear,#white,#Black,45) ; Gadget bevel
Circle(x,y,12,#White) ; Draw bevel
SetFillStyle(#FILLCOLOR) ; Rim fill drawing
Circle(x+2,y+2,10,#Silver) ; Draw rim
SetFillStyle(#FILLNONE) ; Outline drawing
Setlinewidth(2) ; Set line thickness
Circle(x+4,y+4,8,#Gray) ; Draw Face
SetFont("DejaVu Sans",18) ; Font used on gadget right
SetFontColor(FC) ; Text colour
TextOut(x+30,y+3,Text$) ; Write text to gadget
Setformstyle(#Normal) ; Set antialias off
MakeButton(ID, #SIMPLEBUTTON, x+2, y+2, 21, 21, options) ; Make buton same size as face
GadgetState[ID] = 0 ; Set gadget state - Checked = 1 Unchecked = 0
GadgetFColor[ID] = FC ; Record FC colour in table for redrawing
GadgetBColor[ID] = BC ; Record BC colour in table for redrawing
EndFunction
; Create String Gadget - ID = Gadget ID, x y position of gadet on screen, w h external Gadget size,
; Text$ default text on Gadget, BC background colour, FC foreground (Text) colour
Function GadgetString(ID,x,y,w,h,Text$,BC,FC)
Setlinewidth(1) ; Set line thickness
SetFillStyle(#FILLGradient,#Linear,RGB(96,96,96),#Silver,27) ; Gadget bevel
Box(x, y, w, h, #Gray) ; Draw bevel
SetFillStyle(#FILLCOLOR) ; BC fill drawing
Box(x+2, y+2, w-4, h-4, BC) ; Face fill drawing
Line(x+1, y+1, x+w-2, y+1, #Black) ; Inner bevel
Line(x+1, y+1, x+1, y+h-2, #Black) ; Inner bevel
Line(x+1, y+h-2, x+w-2, y+h-2, #White) ; Inner bevel
Line(x+w-2, y+1,x+w-2,y+h-2, #White) ; Inner bevel
SetFont("DejaVu Sans",18) ; Font used in gadget
GadgetFont[ID] = "DejaVu Sans" ; Record font in table for redrawing
GadgetFontSize[ID] = 18 ; Record font size in table for redrawing
SetFontColor(FC) ; Text colour
GadgetText[ID] = Text$ ; Record default text in table for redrawing
GadgetFColor[ID] = FC ; Record FC colour in table for redrawing
GadgetBColor[ID] = BC ; Record BC colour in table for redrawing
Textout(x+4,y+3,GadgetText[ID],GadgetFColor[ID]) ; Draw position, text and colour
MakeButton(ID, #SIMPLEBUTTON, x+2, y+2, w-4, h-4, Events) ; Make buton same size as face ( used to catch events )
EndFunction
; Create Image Button Gadget - ID = Gadget ID, x y position of gadet on screen, w h external Gadget size,
; File$ file name for image on Gadget
Function GadgetButtonImage(ID,x,y,w,h,File$)
SetFillStyle(#FILLGradient,#Linear,#Silver,RGB(64,64,64),12) ; Gadget bevel
Box(x, y, w, h, #GRAY) ; Draw bevel
SetFillStyle(#FILLCOLOR) ; Face fill drawing
Box(x+2, y+2, w-4, h-4,#Silver) ; Gadget face default color
LoadBrush(ID,File$) ; Load Brush named in File$
DisplayBrush(ID,x+2, y+2, {width = w-4, height = h-4}) ; Display the brush on gadget
MakeButton(ID, #SIMPLEBUTTON, x+2, y+2, w-4, h-4, Events) ; Make buton same size as face
EndFunction
; Button Events *************************************************************************
Function p_EventGad(msg)
Switch msg.action
Case "OnMouseDown": ; highlight button on mouse down
Setlinewidth(1)
SetFillStyle(#FILLNONE)
Gadget = (msg.id) ; Record gadget ID of gadget
Gadgetx = msg.x ; Record x position of gadget
Gadgety = msg.y ; Record y position of gadget
Gadgetw = msg.width ; Record width of gadget
Gadgeth = msg.height ; Record height of gadget
Box(msg.x, msg.y, msg.width, msg.height, #Teal) ; Draw highlight
p_SetCheck(Gadget) ; Check what action to take for gadget ID
Case "OnMouseOut": ; highlight button on mouse out
Setlinewidth(1)
SetFillStyle(#FILLNONE)
Box(msg.x, msg.y, msg.width, msg.height, #Silver)
Case "OnMouseUp": ; highlight button on mouse up
Setlinewidth(1)
SetFillStyle(#FILLNONE)
Box(msg.x, msg.y, msg.width, msg.height, #Silver)
Case "OnMouseOver": ; highlight button on mouse over
Setlinewidth(1)
SetFillStyle(#FILLNONE)
Box(msg.x, msg.y, msg.width, msg.height,#Gray)
endswitch
EndFunction
; String Gadget Button Events ***********************************************************
Function p_StringText(msg)
Switch Gadget
Case 9 ; String gadget ID
Key$ = (msg.key) ; Get key pressed
if (msg.key) = "BACKSPACE" ; check if backspace
GadgetText[Gadget] = leftstr(GadgetText[Gadget],strlen(GadgetText[Gadget])-1) ; delete last character
elseif (msg.key) = "DEL" ; check if delete - delete all characters
GadgetText[Gadget] = "" ; set text to empty string
Key$ = ""
else
if TextWidth(GadgetText[Gadget]) < Gadgetw-10 ; Check if text is too long to fit in gadget (10 may need resizing)
GadgetText[Gadget] = GadgetText[Gadget] .. Key$ ; Add key to text
endif
endif
SetFillStyle(#FILLCOLOR) ; Face fill drawing
SetFontColor(GadgetFColor[Gadget]) ; Set font colour
Box(Gadgetx+1, Gadgety+1,Gadgetw-2, Gadgeth-2, GadgetBColor[Gadget]) ; Blank gadget (clears text)
SetFont(GadgetFont[Gadget],GadgetFontSize[Gadget]) ; Set font and colour
Textout(Gadgetx+2,Gadgety,GadgetText[Gadget]) ; Draw text
Case 10
Key$ = (msg.key)
if (msg.key) = "BACKSPACE"
GadgetText[Gadget] = leftstr(GadgetText[Gadget],strlen(GadgetText[Gadget])-1)
elseif (msg.key) = "DEL"
GadgetText[Gadget] = ""
Key$ = ""
else
if TextWidth(GadgetText[Gadget]) < Gadgetw-10
GadgetText[Gadget] = GadgetText[Gadget] .. Key$
endif
endif
SetFillStyle(#FILLCOLOR)
SetFontColor(GadgetFColor[Gadget])
Box(Gadgetx+1, Gadgety+1,Gadgetw-2, Gadgeth-2, GadgetBColor[Gadget])
SetFont(GadgetFont[Gadget],GadgetFontSize[Gadget])
Textout(Gadgetx+2,Gadgety,GadgetText[Gadget])
Case 11
Key$ = (msg.key)
if (msg.key) = "BACKSPACE"
GadgetText[Gadget] = leftstr(GadgetText[Gadget],strlen(GadgetText[Gadget])-1)
elseif (msg.key) = "DEL"
GadgetText[Gadget] = ""
Key$ = ""
else
if TextWidth(GadgetText[Gadget]) < Gadgetw-10
GadgetText[Gadget] = GadgetText[Gadget] .. Key$
endif
endif
SetFillStyle(#FILLCOLOR)
SetFontColor(GadgetFColor[Gadget])
Box(Gadgetx+1, Gadgety+1,Gadgetw-2, Gadgeth-2, GadgetBColor[Gadget])
SetFont(GadgetFont[Gadget],GadgetFontSize[Gadget])
Textout(Gadgetx+2,Gadgety,GadgetText[Gadget])
Endswitch
EndFunction
; String Option Button Events ***********************************************************
Function p_Eventopt(msg)
Setformstyle(#ANTIALIAS)
Switch msg.action
Case "OnMouseOut": ; highlight button on mouse out
Setlinewidth(2)
SetFillStyle(#FILLNONE)
Circle(msg.x+1,msg.y+1,8,#Gray)
Case "OnMouseOver": ; highlight button on mouse over
Setlinewidth(2)
SetFillStyle(#FILLNONE)
Circle(msg.x+1,msg.y+1,8,#Silver)
Case "OnMouseDown": ; highlight button on mouse down
Setlinewidth(2) ; Set line thickness
SetFillStyle(#FILLNONE) ; Outline drawing
Circle(msg.x+1,msg.y+1,8,#Teal) ; Draw highlight
Gadget = (msg.id) ; Record gadget ID of gadget
Gadgetx = msg.x ; Record x position of gadget
Gadgety = msg.y ; Record y position of gadget
p_SetCheck(Gadget) ; Check gadget state
Case "OnMouseUp": ; highlight button on mouse up
Setlinewidth(2)
SetFillStyle(#FILLNONE)
Circle(msg.x+1,msg.y+1,8,#Silver)
EndSwitch
Setformstyle(#Normal)
EndFunction
; Check Gadget State ***********************************************************
Function p_SetCheck(Gadget)
SetFontColor(#GREEN)
SetFillStyle(#FILLCOLOR)
Switch Gadget
Case 5 ; Gadget ID CHECK GADGET
Box(Gadgetx+1, Gadgety+1,Gadgetw-2, Gadgeth-2, #Silver)
SetFont("DejaVu Sans Bold",20) ; Set gadget font
if GadgetState[Gadget] = 0 ; Check state if 0
Textout(Gadgetx+4,Gadgety+1,"X") ; Add check mark
GadgetState[Gadget] = 1 ; Set state = 1
else
Textout(Gadgetx+4,Gadgety+1," ") ; Clear check mark
GadgetState[Gadget] = 0 ; Set state = 0
endif
Case 6
Box(Gadgetx+1, Gadgety+1,Gadgetw-2, Gadgeth-2, #Silver)
SetFont("DejaVu Sans Bold",20)
if GadgetState[Gadget] = 0
Textout(Gadgetx+4,Gadgety+1,"X")
GadgetState[Gadget] = 1
else
Textout(Gadgetx+4,Gadgety+12," ")
GadgetState[Gadget] = 0
endif
Case 7 ; Gadget ID OPTION GADGET
if GadgetState[Gadget] = 0 ; Check state if 0
Circle(Gadgetx+4,Gadgety+4,6,#Green) ; Green mark
GadgetState[Gadget] = 1 ; Set state = 1
else
Circle(Gadgetx+4,Gadgety+4,6,#Silver) ; Clear greenk mark
GadgetState[Gadget] = 0 ; Set state = 0
endif
Case 8
if GadgetState[Gadget] = 0
Circle(Gadgetx+4,Gadgety+4,6,#Green)
GadgetState[Gadget] = 1
else
Circle(Gadgetx+4,Gadgety+4,6,#Silver)
GadgetState[Gadget] = 0
endif
EndSwitch
Endfunction
; Setup events watch
eventstring={OnKeyUp = p_StringText}
events = {OnMouseOver = p_EventGad, OnMouseOut = p_EventGad, OnMouseDown = p_EventGad, OnMouseUp = p_EventGad}
options = {OnMouseOver = p_Eventopt, OnMouseOut = p_Eventopt, OnMouseDown = p_Eventopt,OnMouseUp = p_Eventopt}
InstallEventHandler(eventstring)
; Escape ******************************************************************************************
EscapeQuit(True)
; Create gadgets
GadgetButton(1,20, 30,100,25,"Button",#Silver,#Aqua)
GadgetButton(2,20, 60,100,25,"Button",#Silver,#Black)
GadgetLabel(3,20, 90,100,25,"Label",#Purple,#White)
GadgetLabel(4,20,120,100,25,"Label",#Silver,#Black)
GadgetCheck(5,20,150,"Check",#Silver,#Yellow)
GadgetCheck(6,20,180,"Check",#Silver,#Red)
GadgetOption(7,20,210,"Option",#Silver,#Blue)
GadgetOption(8,20,240,"Option",#Silver,#Blue)
GadgetString(9,20,270,100,25,"String",#silver,#Black)
GadgetString(10,150,30,220,25,"",#silver,#Black)
GadgetString(11,150,60,100,25,"",#silver,#Black)
GadgetButtonImage(12,150,120,220,170,"Image.jpg")
; Main Program Loop *******************************************************************************
Repeat
WaitEvent
ForEver
To test it change the "Image.jpg" to a image file that you have in the same directory as the demo is saved.
Please post any inprovements so we all can learn.
Have fun.
Leo