Improved Simple Gadgets Part 1

You can post your code snippets here for others to use and learn from

Improved Simple Gadgets Part 1

Postby Redlion » Fri Sep 25, 2015 5:02 pm

Hi all,

I have been improving my simple gadgets, still should be crossplatform. Have a look and let me know what you think.

Gadget setup file
(Save as G_Setup.hws )
Code: Select all
; ************************************************************************************************
; Gadget Include
; Setup File - required for all gadgets
; By Leo den Hollander
; *************************************************************************************************

; Global declares----------------------------------------------------------------------------------
Global GID                      ; Gadget ID
Global Gadget                   ; Current Gadget
Global Type                     ; Type of Gadget
Global SelColumn                ; Seleccted Column
Global MaxGadgets               ; Number of Gadgets
Global Font = "DejaVu Sans"     ; Default Gadget Font
Global Size = 18                ; Default gadget Size
Global ScreenC = $4080FF        ; Default Screen Color
Global BGC = #SILVER            ; Default Gadget Background Color
Global FGC = #BLACK             ; Default Gadget Front Color (Font)

; Gadget Tables -----------------------------------------------------------------------------------
GadgetX = {}                    ; Gadget X position
GadgetY = {}                    ; Gadget Y position
GadgetW = {}                    ; Gadget W position
GadgetH = {}                    ; Gadget H position
GadgetText = {}                 ; Gadget Text
GadgetType = {}                 ; Gadget Type
GadgetState = {}                ; Gadget State
GadgetBorder = {}               ; Gadget Border Style
GadgetRangeLow = {}             ; Low Value for Gadget
GadgetRangeHigh = {}            ; High Value for Gadget
GadgetGroup = {}                ; Gadget Group Membership Number
GadgetItemCount = {}            ; Number of items in Gadget
GadgetFont = {}                 ; Font Name
GadgetFontSize = {}             ; Font Size
GadgetFColor = {}               ; Foreground Color (Font Colour)
GadgetBColor = {}               ; Background Color 

Dim GadgetItem[43][10][60]      ; Set to MaxGadgets+1 ( Gadget, Column,Row )
Dim GadgetColumn[43][10]        ; Set to MaxGadgets+1 ( Gadget, column)

; Gadget Control Graphics -------------------------------------------------------------------------
UpArrow = {0,10,5,0,10,10,0,10}
DownArrow = {0,0,10,0,5,10,0,0}
Cross = {0,0,1,0,6,5,7,5,12,0,13,0,13,1,8,6,8,7,13,12,13,13,12,13,7,8,6,8,1,13,0,13,0,12,5,7,5,6,0,1,0,0}
ColorTable = {16,#YELLOW,#LIME,#OLIVE,#GREEN,#AQUA,#TEAL,#BLUE,#NAVY,#PURPLE,#FUCHSIA,#MAROON,#RED,#WHITE,#SILVER,#GRAY,#BLACK}


Events for the different gadgets
( Save as GadgetEvent.hws )
Code: Select all
; ************************************************************************************************
; Gadget Include
; Gadget Event
; Gadget Type  2, 4, 5, 6, 7, 8, 9, 14
; By Leo den Hollander
; *************************************************************************************************

Function p_EventGadget(msg)
    Type = GadgetType[msg.id]
    Gadget = msg.id
    Local WD
    Local HT
    If Type = 4 Or Type = 5
        WD = 25
        HT = 25
    EndIf
    If Type = 2 Or Type = 6 Or Type = 7 Or Type = 8 Or Type = 9 or Type = 14
        WD = GadgetW[msg.id]
        HT = GadgetH[msg.id]
    EndIf       
    Switch msg.action
        Case "OnMouseDown": 
            SetLineWidth(1)
            SetFillStyle(#FILLNONE)
            Line(msg.x+1, msg.y+1, msg.x+WD-2, msg.y+1,RGB(224,224,224))                   
            Line(msg.x+1, msg.y+1, msg.x+1, msg.y+HT-2,RGB(224,224,224))                   
            Line(msg.x+WD-2, msg.y+1, msg.x+WD-2, msg.y+HT-2,#BLACK)         
            Line(msg.x+1, msg.y+HT-2, msg.x+WD-2, msg.y+HT-2,#BLACK)
            SetFormStyle(#NORMAL)
            Switch Type
                Case 2:
                    SetFillStyle(#FILLCOLOR)
                    Box(GadgetX[Gadget] + 3, GadgetY[Gadget] + 3,GadgetW[Gadget] - 6, GadgetH[Gadget] - 6, GadgetBColor[Gadget])
                    TextOut(GadgetX[Gadget] + 4, GadgetY[Gadget] + 3, GadgetText[Gadget]..Chr(124))
                    SetFormStyle(#NORMAL)
                Case 4:
                    p_SetOption(msg.id)
                Case 5:
                    p_SetCheck(msg.id)
                Case 6:
                    p_SetSelect(msg.id)
                Case 7:
                    p_SetSpin(msg.id)
                Case 8:
                    p_SetSlider(msg.id)
                Case 9:
                    p_SetTable(msg.id)
                Case 14:
                    p_SetColor(msg.id)
            EndSwitch                   
     
        Case "OnMouseOut":                   ; ** highlight button on mouse out
            SetFillStyle(#FILLNONE)
            Line(msg.x+1, msg.y+1, msg.x+WD-2, msg.y+1,#BLACK)
            Line(msg.x+1, msg.y+1, msg.x+1, msg.y+HT-2,#BLACK)                             
            Line(msg.x+WD-2, msg.y+1, msg.x+WD-2, msg.y+HT-2,RGB(224,224,224))                     
            Line(msg.x+1, msg.y+HT-2, msg.x+WD-2, msg.y+HT-2,RGB(224,224,224))
            Box(msg.x, msg.y, WD, HT,#SILVER)
            SetFormStyle(#NORMAL)
            Switch Type
                Case 2:
                 SetFillStyle(#FILLCOLOR)
                 Box(GadgetX[Gadget]+3, GadgetY[Gadget]+3,GadgetW[Gadget]-6, GadgetH[Gadget]-6, GadgetBColor[Gadget])
                 If RightStr(GadgetText[msg.id],1) = Chr(124)
                     GadgetText[msg.id] = UnleftStr(GadgetText[msg.id],1)
                 EndIf 
                 TextOut(GadgetX[Gadget]+4,GadgetY[Gadget]+3,GadgetText[Gadget])
                 SetFormStyle(#NORMAL)
            EndSwitch   
     
        Case "OnMouseUp":                    ; ** highlight button on mouse up
            SetFillStyle(#FILLNONE)
            Line(msg.x+1, msg.y+1, msg.x+WD-2, msg.y+1,#BLACK) 
            Line(msg.x+1, msg.y+1, msg.x+1, msg.y+HT-2,#BLACK)                             
            Line(msg.x+WD-2, msg.y+1, msg.x+WD-2, msg.y+HT-2,RGB(224,224,224))           
            Line(msg.x+1, msg.y+HT-2,msg.x+WD-2,msg.y+HT-2,RGB(224,224,224))
            Box(msg.x, msg.y, WD, HT,#GRAY)
            SetFormStyle(#NORMAL)
            If Type = 4 Or Type = 5 Or Type = 6 Or Type = 7 Or Type = 8 Or Type = 9 Or Type = 14
                p_Actions(msg.id)
            EndIf

        Case "OnMouseOver":                  ; ** highlight button on mouse over
            SetFillStyle(#FILLNONE)
            Box(msg.x, msg.y, WD, HT,#GRAY)
            SetFormStyle(#NORMAL)

    EndSwitch
EndFunction


(Save as ButtonEvent.hws )
Code: Select all
; ************************************************************************************************
; Gadget Include
; Button Event
; Gadget Type 3
; By Leo den Hollander
; *************************************************************************************************

Function p_EventButton(msg)
    Switch msg.action
        Case "OnMouseDown":                  ; ** highlight button on mouse down
            SetLineWidth(1)
            SetFillStyle(#FILLNONE)
            Line(msg.x+1, msg.y+1, msg.x+msg.width-2, msg.y+1,#BLACK)                             
            Line(msg.x+1, msg.y+1, msg.x+1, msg.y+msg.height-2,#BLACK)                             
            Line(msg.x+msg.width-2,msg. y+1, msg.x+msg.width-2, msg.y+msg.height-2,RGB(224,224,224))
            Line(msg.x+1, msg.y+msg.height-2,msg.x+msg.width-2,msg.y+msg.height-2,RGB(224,224,224) )
            p_Actions(msg.id)

    Case "OnMouseOut":                   ; ** highlight button on mouse out
            SetLineWidth(1)
            SetFillStyle(#FILLNONE)
            Line(msg.x+1, msg.y+1, msg.x+msg.width-2, msg.y+1,RGB(224,224,224))                   
            Line(msg.x+1, msg.y+1, msg.x+1, msg.y+msg.height-2,RGB(224,224,224))                   
            Line(msg.x+msg.width-2,msg. y+1, msg.x+msg.width-2, msg.y+msg.height-2,#BLACK)         
            Line(msg.x+1, msg.y+msg.height-2,msg.x+msg.width-2,msg.y+msg.height-2,#BLACK)
            Box(msg.x, msg.y, msg.width, msg.height,#SILVER)

    Case "OnMouseUp":                    ; ** highlight button on mouse up
            SetLineWidth(1)
            SetFillStyle(#FILLNONE)
            Line(msg.x+1, msg.y+1, msg.x+msg.width-2, msg.y+1,RGB(224,224,224))                   
            Line(msg.x+1, msg.y+1, msg.x+1, msg.y+msg.height-2,RGB(224,224,224))                   
            Line(msg.x+msg.width-2,msg. y+1, msg.x+msg.width-2, msg.y+msg.height-2,#BLACK)         
            Line(msg.x+1, msg.y+msg.height-2,msg.x+msg.width-2,msg.y+msg.height-2,#BLACK)
            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
 


( Save as StringEvent.hws )
Code: Select all
; ************************************************************************************************
; Gadget Include
; String Event
; Gadget Type  2
; By Leo den Hollander
; *************************************************************************************************

Function p_StringText(msg)
     Key$ = (msg.key)                                                                     ; Get key pressed
     If Type = 2                                                                          ; Check for string gadget
         If (msg.key) = "BACKSPACE"                                                       ; check if backspace
            GadgetText[Gadget] = LeftStr(GadgetText[Gadget], StrLen(GadgetText[Gadget])-2); delete last character
            GadgetText[Gadget] = GadgetText[Gadget]..Chr(124)                             ; set text string + cursor
        ElseIf (msg.key) = "DEL"                                                          ; check if delete - delete all characters
            GadgetText[Gadget] = Chr(124)                                                 ; set text to empty string + cursor
            Key$ = "|"   
        Else                                                 
            If TextWidth(GadgetText[Gadget]) < Gadgetw[Gadget] - 15                       ; Check if text is too long to fit in gadget
                GadgetText[Gadget] = UnleftStr(GadgetText[Gadget], 1).. Key$..Chr(124)    ; Add cursor to text
            EndIf
        EndIf
        SetFontColor(GadgetFColor[Gadget])                                                ; Set font colour
        SetFont(GadgetFont[Gadget] ,GadgetFontSize[Gadget])                               ; Set font and colour
        SetFillStyle(#FILLCOLOR)
        Box(GadgetX[Gadget] + 3, GadgetY[Gadget] + 3, GadgetW[Gadget] - 6, GadgetH[Gadget] - 6, GadgetBColor[Gadget])
        TextOut(GadgetX[Gadget] + 4, GadgetY[Gadget] + 3, GadgetText[Gadget])             ; Draw text
    EndIf
EndFunction


Now here is a small demo showing some of what can be done. You will need to save all the gadget files aswell, they will be in the following parts.
Code: Select all
@VERSION 6,0
@APPTITLE "Gadgets"
@APPVERSION "$VER: 1.0 (04.09.15)"
@APPCOPYRIGHT "©2015, George Leo den Hollander"
@APPAUTHOR "Leo den Hollander"
@APPDESCRIPTION "Gadget Demo"

@DISPLAY 1, {Width=800,Height=600,Color=$4080FF,Title=" Gadget Demo by Leo den Hollander "}

; Escape ******************************************************************************************

EscapeQuit(True)

@INCLUDE "G_Setup.hws"
@INCLUDE "G_Label.hws"
@INCLUDE "G_String.hws"
@INCLUDE "G_Button.hws"
@INCLUDE "G_Spin.hws"
@INCLUDE "G_Check.hws"
@INCLUDE "G_Option.hws"
@INCLUDE "G_Select.hws"
@INCLUDE "G_Table.hws"
@INCLUDE "G_Slider.hws"
@INCLUDE "G_ImageButton.hws"
@INCLUDE "G_Frame.hws"
@INCLUDE "G_ColorSelect.hws"

@INCLUDE "G_SetBGColor.hws"
@INCLUDE "G_SetFGColor.hws"
@INCLUDE "G_SetText.hws"

@INCLUDE "GadgetEvent.hws"
@INCLUDE "ButtonEvent.hws"
@INCLUDE "StringEvent.hws"


; #####################################################################################################################
Function p_Actions(id)
    SetGadgetText( 39,"Gadget ID = "..StrStr(id))
    SetGadgetText( 40,"Gadget State = "..StrStr(GadgetState[id]))
    SetGadgetText( 41,"Selected Col = "..StrStr(SelColumn))
    If GadgetType[id] = 1 or GadgetType[id] = 3 or GadgetType[id] = 4 or GadgetType[id] = 5
        SetGadgetText( 42,"Text = "..GadgetText[id])
    Elseif GadgetType[id] = 6
        SetGadgetText( 42,"Text = "..GadgetItem[id][SelColumn][GadgetState[id]])
    Elseif GadgetType[id] = 7
        SetGadgetText( 42,"Text = "..GadgetState[id])
    Elseif GadgetType[id] = 8
        SetGadgetText( 42,"Text = "..GadgetState[id])
    Elseif GadgetType[id] = 9
        SetGadgetText( 42,"Text = "..GadgetItem[id][SelColumn][GadgetState[id]])
    Elseif GadgetType[id] = 14
        SetGadgetText( 42,"Text = "..ColorTable[SelColumn])
    Else
        SetGadgetText( 42,"Text = ")
    Endif
EndFunction
; #####################################################################################################################


; Events ******************************************************************************************
Button = {OnMouseOver = p_EventButton, OnMouseOut = p_EventButton, OnMouseDown = p_EventButton, OnMouseUp = p_EventButton}
Gadget = {OnMouseOver = p_EventGadget, OnMouseOut = p_EventGadget, OnMouseDown = p_EventGadget, OnMouseUp = p_EventGadget}
eventstring={OnKeyUp = p_StringText}
InstallEventHandler(eventstring)

GadgetButton(1,  20, 20,100, 25,"BUTTON 1")
GadgetButton(2,  20, 60,100, 25,"BUTTON 2")
GadgetButton(3,  20,100,100, 25,"BUTTON 3")

SetGadgetFColor(1,#RED)
SetGadgetFColor(2,#WHITE)
SetGadgetBColor(3,#GREEN)

GadgetOption(4, 150, 20, 80,25,"Opt 1-1",1)
GadgetOption(5, 150, 60, 80,25,"Opt 1-2",1)
GadgetOption(6, 150,100, 80,25,"Opt 1-3",1)

GadgetOption(7, 250, 20, 80,25,"Opt 2-1",2)
GadgetOption(8, 250, 47, 80,25,"Opt 2-2",2)
GadgetOption(9, 250, 74, 80,25,"Opt 2-3",2)
GadgetOption(10,250,101, 80,25,"Opt 2-1",2)
GadgetOption(11,250,128, 80,25,"Opt 2-2",2)

GadgetCheck(12, 20,140,100,25,"Check A")
GadgetCheck(13, 20,180,100,25,"Check B")
GadgetCheck(14, 20,220,100,25,"Check C")

GadgetSelect(15, 20,260,120,25,"One")
GadgetSelect(16, 20,300,170,25,"Select")
GadgetSelect(17, 20,340,170,25,"Red")

AddSelectRow(15,"Two")
AddSelectRow(15,"Three")
AddSelectRow(15,"Four")
AddSelectRow(15,"Five")

AddSelectRow(16,"123 - This number")
AddSelectRow(16,"234 - That number")
AddSelectRow(16,"345 - New number")
AddSelectRow(16,"567 - Now this")

AddSelectRow(17,"Purple")
AddSelectRow(17,"Blue")
AddSelectRow(17,"Green")
AddSelectRow(17,"Yellow")
AddSelectRow(17,"Orange")

GadgetSpin(18, 150,140, 70, 25, 10,100)
GadgetSpin(19, 150,180,100, 25,  1,  5)
GadgetSpin(20, 150,220,150, 25,999,10000)

GadgetSlider(21,360,480,100, 25, 0, 20)
GadgetSlider(22,360,520,220, 25, 0,256)
GadgetSlider(23,360,560,420, 25, 0,572)

GadgetButtonImage(24,230,260, 24, 24,"Image.jpg")
GadgetButtonImage(25,282,260, 48, 24,"Image.jpg")
GadgetButtonImage(26,230,300,100,100,"Image.jpg")

GadgetLabel(27, 20,375,110, 25,"Border = 0",0)
GadgetLabel(28, 20,410,110, 25,"Border = 1",1)
GadgetLabel(29, 20,445,110, 25,"Border = 2",2)

GadgetLabel(30, 20,480,110, 25,"Border = 0",0)
SetGadgetFColor(30,#WHITE)

GadgetTable(31,360, 20,420,350,"Col 1",80)
AddTableColumn(31,"Col 2",80)
AddTableColumn(31,"Col 3",80)
AddTableColumn(31,"Col 4",80)
AddTableColumn(31,"Col 5",80)

AddTableRow(31,"R1-C1|R1-C2|R1-C3|R1-C4|R1-C5")
AddTableRow(31,"R2-C1|R2-C2|R2-C3|R2-C4|R2-C5")
AddTableRow(31,"R3-C1|R3-C2|R3-C3|R3-C4|R3-C5")
AddTableRow(31,"R4-C1|R4-C2|R4-C3|R4-C4|R4-C5")
AddTableRow(31,"R5-C1|R5-C2|R5-C3|R5-C4|R5-C5")
AddTableRow(31,"R6-C1|R6-C2|R6-C3|R6-C4|R6-C5")
AddTableRow(31,"R7-C1|R7-C2|R7-C3|R7-C4|R7-C5")
AddTableRow(31,"R8-C1|R8-C2|R8-C3|R8-C4|R8-C5")
AddTableRow(31,"R9-C1|R9-C2|R9-C3|R4-C4|R9-C5")
AddTableRow(31,"R10-C1|R10-C2|R10-C3|R10-C4|R10-C5")
AddTableRow(31,"R11-C1|R1-C2|R1-C3|R1-C4|R1-C5")
AddTableRow(31,"R12-C1|R2-C2|R2-C3|R2-C4|R2-C5")
AddTableRow(31,"R13-C1|R3-C2|R3-C3|R3-C4|R3-C5")
AddTableRow(31,"R14-C1|R4-C2|R4-C3|R4-C4|R4-C5")
AddTableRow(31,"R15-C1|R5-C2|R5-C3|R5-C4|R5-C5")
AddTableRow(31,"R16-C1|R6-C2|R6-C3|R6-C4|R6-C5")
AddTableRow(31,"R17-C1|R7-C2|R7-C3|R7-C4|R7-C5")
AddTableRow(31,"R18-C1|R8-C2|R8-C3|R8-C4|R8-C5")
AddTableRow(31,"R19-C1|R9-C2|R9-C3|R4-C4|R9-C5")
AddTableRow(31,"R20-C1|R10-C2|R10-C3|R10-C4|R10-C5")
AddTableRow(31,"R21-C1|R1-C2|R1-C3|R1-C4|R1-C5")
AddTableRow(31,"R22-C1|R2-C2|R2-C3|R2-C4|R2-C5")
AddTableRow(31,"R23-C1|R3-C2|R3-C3|R3-C4|R3-C5")
AddTableRow(31,"R24-C1|R4-C2|R4-C3|R4-C4|R4-C5")
AddTableRow(31,"R25-C1|R5-C2|R5-C3|R5-C4|R5-C5")
AddTableRow(31,"R26-C1|R6-C2|R6-C3|R6-C4|R6-C5")
AddTableRow(31,"R27-C1|R7-C2|R7-C3|R7-C4|R7-C5")
AddTableRow(31,"R28-C1|R8-C2|R8-C3|R8-C4|R8-C5")
AddTableRow(31,"R29-C1|R9-C2|R9-C3|R4-C4|R9-C5")
AddTableRow(31,"R30-C1|R10-C2|R10-C3|R10-C4|R10-C5")
AddTableRow(31,"R31-C1|R5-C2|R5-C3|R5-C4|R5-C5")
AddTableRow(31,"R32-C1|R6-C2|R6-C3|R6-C4|R6-C5")
AddTableRow(31,"R33-C1|R7-C2|R7-C3|R7-C4|R7-C5")
AddTableRow(31,"R34-C1|R8-C2|R8-C3|R8-C4|R8-C5")
AddTableRow(31,"R35-C1|R9-C2|R9-C3|R4-C4|R9-C5")
AddTableRow(31,"R36-C1|R6-C2|R6-C3|R6-C4|R6-C5")
AddTableRow(31,"R37-C1|R7-C2|R7-C3|R7-C4|R7-C5")
AddTableRow(31,"R38-C1|R8-C2|R8-C3|R8-C4|R8-C5")
AddTableRow(31,"R39-C1|R9-C2|R9-C3|R4-C4|R9-C5")
AddTableRow(31,"R40-C1|R10-C2|R10-C3|R10-C4|R10-C5")
AddTableRow(31,"R41-C1|R1-C2|R1-C3|R1-C4|R1-C5")
AddTableRow(31,"R42-C1|R2-C2|R2-C3|R2-C4|R2-C5")
AddTableRow(31,"R43-C1|R3-C2|R3-C3|R3-C4|R3-C5")
AddTableRow(31,"R44-C1|R4-C2|R4-C3|R4-C4|R4-C5")
AddTableRow(31,"R45-C1|R5-C2|R5-C3|R5-C4|R5-C5")
AddTableRow(31,"R46-C1|R6-C2|R6-C3|R6-C4|R6-C5")
AddTableRow(31,"R47-C1|R7-C2|R7-C3|R7-C4|R7-C5")
AddTableRow(31,"R48-C1|R8-C2|R8-C3|R8-C4|R8-C5")
AddTableRow(31,"R49-C1|R9-C2|R9-C3|R4-C4|R9-C5")
AddTableRow(31,"R50-C1|R10-C2|R10-C3|R10-C4|R10-C5")

GadgetLabel(32,140,520,75, 25,"Text input",0)
GadgetString(33,220,520,120, 25,"")

GadgetFrame(34,150,420,190,60,"Test Frame",1)

GadgetLabel(35, 20,520,110, 25,"Border = 1",1)
SetGadgetFColor(35,#RED)
SetGadgetBColor(35,#YELLOW)

GadgetLabel(36, 20,560,110, 25,"Border = 2",2)
SetGadgetFColor(36,#PURPLE)
SetGadgetBColor(36,RGB(255,255,128))

GadgetSelect(37,190,445,100,25,"Yes")
AddSelectRow(37,"No")
AddSelectRow(37,"Maybe")

GadgetColorSelect(38,360,390,260,75,ColorTable)

GadgetLabel(39,630,390,150, 25,"Gadget ID = ",2)
GadgetLabel(40,630,420,150, 25,"Gadget State = ",2)
GadgetLabel(41,630,450,150, 25,"Selected Col = ",2)
GadgetLabel(42,630,480,150, 25,"Text = ",2)

; Main Program Loop *******************************************************************************
Repeat
    WaitEvent
Forever


Just use any small jpg file for the imagebutton, make sure it is in the same directory as all the other includes.

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

Return to Code snippets area

Who is online

Users browsing this forum: No registered users and 1 guest

cron