Simple Cross-Platform Gadgets with string input

You can post your code snippets here for others to use and learn from
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Simple Cross-Platform Gadgets with string input

Post by Redlion »

@ 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.

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
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
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
SrHead
Posts: 2
Joined: Wed Nov 18, 2015 5:53 pm

Re: Simple Cross-Platform Gadgets with string input

Post by SrHead »

Thank you. It has been a great help.
Cheers
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: Simple Cross-Platform Gadgets with string input

Post by Redlion »

Hi all,

New update.

I have made some modifications and additions to my Gadgets.

It has got a bit to big to put the code in here so I have put it on Amiclound ( Plug Plug )

Here is the link to LHA file with all you should need to test.

http://www.indiego.rocks/file/download/ ... 660ad79fe4

As before No Warranty but it works for me, feel free to use and give me feedback.

There is a simple instruction file included.

Have fun.

Leo
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
Bugala
Posts: 1168
Joined: Sun Feb 14, 2010 7:11 pm

Re: Simple Cross-Platform Gadgets with string input

Post by Bugala »

One question as im out of time right now to check myself.

I was trying older (probably oldest) version of your gadgets. I had cut it to necessary pieces, which means i might have cut something that was actually needed, but at least that myself cut down version had no feedback for return pressed.

That while i had a string box taht i could write, it was basically useless as what i wrote there couldnt be used for anything as there was no way to get it stored anywhere.

How does this work in newer versions, can i put a variable which will be changed to the one i push return with? Or was it there originally already and i just cut it out without realising what i was cutting?
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: Simple Cross-Platform Gadgets with string input

Post by Redlion »

@ Bugala

In the latest version, ( download from AmiCloud ) you should be able to get the text from the Gadget Table, have a look at the gadget setup include file it shows what the table holds.

but for your request - Gadget[Your gadget ID][#GadTEXT] should give you the text in the gadget.

I have found a few bugs and am in the prosess of fixing those, new upload in a day or two.

Cheers
Redlion
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
Murmel
Posts: 31
Joined: Mon Apr 12, 2010 9:03 pm

Re: Simple Cross-Platform Gadgets with string input

Post by Murmel »

Nice Work :)
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: Simple Cross-Platform Gadgets with string input

Post by Redlion »

Hi All,

Working on an update, fixed a few problems added a few things.

Just writing some basic instructions.

Leo
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: Simple Cross-Platform Gadgets with string input

Post by Redlion »

Hi All,

Here is the new update, I am slowly adding more functionality as I go ( and fixing Bugs and Problems )

Once again here is the Link to the LHA file with all you need, I have added a few simple example programs. I will add to them as I go.
There is a basic Instruction file included.

http://www.indiego.rocks/file/download/ ... 23205a6cc9

I have called this download version 1-0-0, If you have any problems with it let me know which include file is the cause.

Again no warranty, use at your own risk.

But do let me know if its useful, or give me some ideas on improving it, or better still show me how to improve or do things better.

Have fun with it.

Redlion
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
Murmel
Posts: 31
Joined: Mon Apr 12, 2010 9:03 pm

Re: Simple Cross-Platform Gadgets with string input

Post by Murmel »

Hi good Work :)

I have a question. How can I update this variable?
test$="hello"
GadgetLabel( 50, 670, 180, 110, 20,test$,2)
in
test$="hello too"

Thx,

Murmel
User avatar
Redlion
Posts: 93
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Re: Simple Cross-Platform Gadgets with string input

Post by Redlion »

Hi Murmel,

If I understand correctly, you need to use
SetGadgetText(50, "hello too")
or
Text$ = "hello too"
SetGadgetText(50,Text$)

See Instructions file,

; Other Functions
; Set Gadget Text ---------------------------------------------------------------------------------
; Syntax
; SetGadgetText(GadgetID, Gadget Text)

Hope that helps

Redlion
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
Post Reply