Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI Shutdown or Restart (Linux)
#1
This is a small program to shutdown or restart a linux computer.
It is not perfect but it is a sufficient demo to show off the GUI toolkit im workin on.

Be careful, on linux this will actually shutdown your computer. Windows it wont unless you change the SHELL statement.

Code: (Select All)
Option _Explicit

_Title "kbde-shutdown"

Type Position
    X As Integer
    Y As Integer
End Type

Type Size
    X As _Unsigned Integer
    Y As _Unsigned Integer
End Type

Type Label
    Text As String
    Position As Position
    Handle As _Unsigned Long
End Type

Dim Shared labelQuantity As _Unsigned Integer
ReDim Shared Label(labelQuantity) As Label

Type radiobuttonStatus
    Clicked As _Byte
    Checked As _Byte
End Type

Type Radiobutton
    Position As Position
    Size As Size
    Status As radiobuttonStatus
    Handle As _Unsigned Long
End Type

Dim Shared radiobuttonQuantity As _Unsigned Integer
ReDim Shared Radiobutton(radiobuttonQuantity) As Radiobutton

Type checkboxStatus
    Clicked As _Byte
    Checked As _Byte
End Type

Type Checkbox
    Position As Position
    Size As Size
    Status As checkboxStatus
    Handle As _Unsigned Long
End Type

Dim Shared checkboxQuantity As _Unsigned Integer
ReDim Shared Checkbox(checkboxQuantity) As Checkbox

Type buttonStatus
    Clicked As _Bit
    Enabled As _Bit
End Type

Type Button
    Text As String
    Position As Position
    Size As Size
    Status As buttonStatus
    Handle As _Unsigned Long
End Type

Dim Shared buttonQuantity As _Unsigned Integer
ReDim Shared Button(buttonQuantity) As Button

Type Cursor
    Position As Position
    Button As Integer
End Type

Dim Shared Cursor As Cursor

Screen _NewImage(480, 200, 32)

Dim Shared textColor As _Unsigned Long: textColor = _RGBA32(255, 255, 255, 255)
Dim Shared highlightColor As _Unsigned Long: highlightColor = _RGBA32(223, 223, 223, 255)
Dim Shared faceColor As _Unsigned Long: faceColor = _RGBA32(191, 191, 191, 255)
Dim Shared shadowColor As _Unsigned Long: shadowColor = _RGBA32(127, 127, 127, 255)

Dim Shared whiteColor As _Unsigned Long: whiteColor = _RGBA32(255, 255, 255, 255)
Dim Shared blackColor As _Unsigned Long: blackColor = _RGBA32(0, 0, 0, 255)

Dim Shared backgroundColor As _Unsigned Long: backgroundColor = _RGBA32(0, 255, 0, 255)

Dim Shared Label0 As _Unsigned Integer
Dim Shared Question As String: Question = "Are you sure you want to?:"
Label0 = addLabel
Label0 = initLabel(Label0, (_Width - _PrintWidth(Question)) / 2, 25, Question)
Label0 = drawLabel(Label0)

Dim Shared Label1 As _Unsigned Integer
Label1 = addLabel
Label1 = initLabel(Label1, 50, 75, "Shutdown Your Computer")
Label1 = drawLabel(Label1)

Dim Shared Label2 As _Unsigned Integer
Label2 = addLabel
Label2 = initLabel(Label2, 50, 100, "Restart Your Computer")
Label2 = drawLabel(Label2)

Dim Shared Label3 As _Unsigned Integer
Label3 = addLabel
Label3 = initLabel(Label3, 300, 100, "Timed Execution")
Label3 = drawLabel(Label3)

Dim Shared Radiobutton1 As _Unsigned Integer
Radiobutton1 = addRadiobutton
Radiobutton1 = initRadiobutton(Radiobutton1, 240, 75, 16, 16)
Radiobutton(Radiobutton1).Status.Checked = -1
Radiobutton1 = drawRadiobutton(Radiobutton1)

Dim Shared Radiobutton2 As _Unsigned Integer
Radiobutton2 = addRadiobutton
Radiobutton2 = initRadiobutton(Radiobutton2, 240, 100, 16, 16)
Radiobutton2 = drawRadiobutton(Radiobutton2)

Dim Shared Checkbox1 As _Unsigned Integer
Checkbox1 = addCheckbox
Checkbox1 = initCheckbox(Checkbox1, 435, 100, 16, 16)
Checkbox1 = drawCheckbox(Checkbox1)

Dim Shared Button1 As _Unsigned Integer
Button1 = addButton
Button1 = initButton(Button1, "OK", ((_Width - ((75 * 2) + 20)) / 2), (_Height - 48), 75, 23)
Button1 = drawButton(Button1)

Dim Shared Button2 As _Unsigned Integer
Button2 = addButton
Button2 = initButton(Button2, "Cancel", (((_Width - ((75 * 2) + 20)) / 2) + 95), (_Height - 48), 75, 23)
Button2 = drawButton(Button2)

Dim I As _Unsigned Integer, J As _Unsigned Integer, A As _Unsigned Integer

Dim Timed As _Unsigned Integer: Timed = 0

Do
    Line (0, 0)-(_Width, _Height), faceColor, BF

    For I = 1 To labelQuantity
        _PutImage (Label(I).Position.X, Label(I).Position.Y), Label(I).Handle
    Next I

    For I = 1 To radiobuttonQuantity
        _PutImage (Radiobutton(I).Position.X, Radiobutton(I).Position.Y), Radiobutton(I).Handle
    Next I

    For I = 1 To checkboxQuantity
        _PutImage (Checkbox(I).Position.X, Checkbox(I).Position.Y), Checkbox(I).Handle
    Next I

    For I = 1 To buttonQuantity
        _PutImage (Button(I).Position.X, Button(I).Position.Y), Button(I).Handle
    Next I

    Check

    For I = 1 To radiobuttonQuantity
        If Cursor.Position.X >= Radiobutton(I).Position.X And Cursor.Position.X <= Radiobutton(I).Position.X + Radiobutton(I).Size.X Then
            If Cursor.Position.Y >= Radiobutton(I).Position.Y And Cursor.Position.Y <= Radiobutton(I).Position.Y + Radiobutton(I).Size.Y Then
                If Cursor.Button Then
                    For J = 1 To radiobuttonQuantity
                        Radiobutton(J).Status.Checked = 0
                        A = drawRadiobutton(J)
                    Next J
                    Radiobutton(I).Status.Checked = -1
                    A = drawRadiobutton(I)
                End If
            End If
        End If
    Next I

    For I = 1 To checkboxQuantity
        If Cursor.Position.X >= Checkbox(I).Position.X And Cursor.Position.X <= Checkbox(I).Position.X + Checkbox(I).Size.X Then
            If Cursor.Position.Y >= Checkbox(I).Position.Y And Cursor.Position.Y <= Checkbox(I).Position.Y + Checkbox(I).Size.Y Then
                If Cursor.Button Then
                    If Checkbox(I).Status.Checked = 0 Then
                        Checkbox(I).Status.Checked = -1
                    Else
                        Checkbox(I).Status.Checked = 0
                    End If
                    A = drawCheckbox(I)
                    _Delay 0.2
                End If
            End If
        End If
    Next I

    For I = 1 To buttonQuantity
        If Cursor.Position.X >= Button(I).Position.X And Cursor.Position.X <= Button(I).Position.X + Button(I).Size.X Then
            If Cursor.Position.Y >= Button(I).Position.Y And Cursor.Position.Y <= Button(I).Position.Y + Button(I).Size.Y Then
                If Cursor.Button Then
                    Select Case I
                        Case Button1
                            If Checkbox(Checkbox1).Status.Checked = -1 Then
                                Input "Amount of time to wait? ", Timed
                            End If
                            If Radiobutton(Radiobutton1).Status.Checked = -1 Then
                                Shell _DontWait "shutdown -h " + LTrim$(RTrim$(Str$(Timed)))
                                System
                            ElseIf Radiobutton(Radiobutton2).Status.Checked = -1 Then
                                Shell _DontWait "shutdown -r " + LTrim$(RTrim$(Str$(Timed)))
                                System
                            End If
                        Case Button2
                            System
                    End Select
                End If
            End If
        End If
    Next I
    _Display
Loop

Function addLabel~%
    labelQuantity = labelQuantity + 1
    ReDim _Preserve Label(labelQuantity) As Label
    addLabel = labelQuantity
End Function

Function initLabel~% (inID As _Unsigned Integer, inPositionX As Integer, inPositionY As Integer, inText As String)
    Dim sX As Integer
    Dim sY As Integer
    Label(inID).Position.X = inPositionX
    Label(inID).Position.Y = inPositionY
    inText = LTrim$(RTrim$(inText))
    Label(inID).Text = inText
    sX = (_PrintWidth(Label(inID).Text) + 2)
    sY = (_FontHeight + 2)
    Label(inID).Handle = _NewImage(sX, sY, 32)
    initLabel = inID
End Function

Function drawLabel~% (inID As _Unsigned Integer)
    _Dest Label(inID).Handle
    Line (0, 0)-(_Width, _Height), faceColor, BF
    _PrintMode _KeepBackground
    _PrintString (2, 1), Label(inID).Text
    _Dest 0
    drawLabel = inID
End Function

Function addRadiobutton~%
    radiobuttonQuantity = radiobuttonQuantity + 1
    ReDim _Preserve Radiobutton(radiobuttonQuantity) As Radiobutton
    addRadiobutton = radiobuttonQuantity
End Function

Function initRadiobutton~% (inID As _Unsigned Integer, inPositionX As Integer, inPositionY As Integer, inSizeX As _Unsigned Integer, inSizeY As _Unsigned Integer)
    Radiobutton(inID).Position.X = inPositionX
    Radiobutton(inID).Position.Y = inPositionY
    Radiobutton(inID).Size.X = inSizeX
    Radiobutton(inID).Size.Y = inSizeY
    Radiobutton(inID).Handle = _NewImage(Radiobutton(inID).Size.X, Radiobutton(inID).Size.Y, 32)
    initRadiobutton = inID
End Function

Function drawRadiobutton~% (inID As _Unsigned Integer)
    Dim centeredX As Integer, centeredY As Integer
    _Dest Radiobutton(inID).Handle
    Circle ((Radiobutton(inID).Size.X / 2) + 1 - 1, (Radiobutton(inID).Size.Y / 2) + 1 - 1), (Radiobutton(inID).Size.X / 2), highlightColor
    Circle ((Radiobutton(inID).Size.X / 2) - 1, (Radiobutton(inID).Size.Y / 2) - 1), (Radiobutton(inID).Size.X / 2), shadowColor
    Paint (_Width / 2, _Height / 2), whiteColor, shadowColor
    Dim Mark As String
    If Radiobutton(inID).Status.Checked Then
        Color blackColor
        Mark = "*"
    End If
    centeredX = (_Width - _PrintWidth(Mark)) / 2
    centeredY = (_Height - _FontHeight) / 2
    _PrintMode _KeepBackground
    _PrintString (centeredX, centeredY), Mark
    _Dest 0
    drawRadiobutton = inID
End Function

Function toggleRadiobutton~% (inID As _Unsigned Integer)
    Dim I As _Unsigned Integer
    For I = 1 To radiobuttonQuantity
        Radiobutton(I).Status.Checked = 0
    Next I
    Radiobutton(inID).Status.Checked = -1
    toggleRadiobutton = inID
End Function

Function addCheckbox~%
    checkboxQuantity = checkboxQuantity + 1
    ReDim _Preserve Checkbox(checkboxQuantity) As Checkbox
    addCheckbox = checkboxQuantity
End Function

Function initCheckbox~% (inID As _Unsigned Integer, inPositionX As Integer, inPositionY As Integer, inSizeX As _Unsigned Integer, inSizeY As _Unsigned Integer)
    Checkbox(inID).Position.X = inPositionX
    Checkbox(inID).Position.Y = inPositionY
    Checkbox(inID).Size.X = inSizeX
    Checkbox(inID).Size.Y = inSizeY
    Checkbox(inID).Handle = _NewImage(Checkbox(inID).Size.X, Checkbox(inID).Size.Y, 32)
    initCheckbox = inID
End Function

Function drawCheckbox~% (inID As _Unsigned Integer)
    Dim centeredX As Integer, centeredY As Integer
    _Dest Checkbox(inID).Handle
    Line (0, 0)-(_Width - 1, _Height - 1), highlightColor, BF
    Line (0, 0)-(_Width - 2, _Height - 2), shadowColor, BF
    Line (1, 1)-(_Width - 2, _Height - 2), highlightColor, BF
    Line (1, 1)-(_Width - 3, _Height - 3), shadowColor, BF
    Line (2, 2)-(_Width - 3, _Height - 3), whiteColor, BF
    Dim Mark As String
    If Checkbox(inID).Status.Checked Then
        Mark = "*"
    Else
        Mark = " "
    End If
    centeredX = (_Width - _PrintWidth(Mark)) / 2
    centeredY = (_Height - _FontHeight) / 2
    _PrintMode _KeepBackground
    Color blackColor
    _PrintString (centeredX, centeredY), Mark
    _Dest 0
    drawCheckbox = inID
End Function

Function addButton~%
    buttonQuantity = buttonQuantity + 1
    ReDim _Preserve Button(buttonQuantity) As Button
    addButton = buttonQuantity
End Function

Function initButton~% (inID As _Unsigned Integer, inText As String, inPositionX As Integer, inPositionY As Integer, inSizeX As _Unsigned Integer, inSizeY As _Unsigned Integer)
    Button(inID).Text = inText
    Button(inID).Position.X = inPositionX
    Button(inID).Position.Y = inPositionY
    Button(inID).Size.X = inSizeX
    Button(inID).Size.Y = inSizeY
    Button(inID).Handle = _NewImage(Button(inID).Size.X, Button(inID).Size.Y, 32)
    initButton = inID
End Function

Function drawButton~% (inID As _Unsigned Integer)
    Dim centeredX As Integer, centeredY As Integer
    _Dest Button(inID).Handle
    _PrintMode _KeepBackground
    Line (0, 0)-(_Width - 1, _Height - 1), backgroundColor, BF
    Line (1, 0)-(_Width - 2, _Height - 1), blackColor, BF
    Line (0, 1)-(_Width - 1, _Height - 2), blackColor, BF
    Line (2, 2)-(_Width - 3, _Height - 3), shadowColor, BF
    Line (2, 2)-(_Width - 4, _Height - 4), highlightColor, BF
    Line (3, 3)-(_Width - 4, _Height - 4), shadowColor, BF
    Line (3, 3)-(_Width - 5, _Height - 5), highlightColor, BF
    Line (4, 4)-(_Width - 5, _Height - 5), faceColor, BF
    centeredX = (_Width - _PrintWidth(Button(inID).Text)) / 2
    centeredY = (_Height - _FontHeight) / 2
    Color textColor, faceColor
    _PrintString (centeredX, centeredY), Button(inID).Text
    _Dest 0
    drawButton = inID
End Function

Sub Check
    While _MouseInput
    Wend
    Cursor.Position.X = _MouseX
    Cursor.Position.Y = _MouseY
    Cursor.Button = _MouseButton(1)
End Sub

[Image: final.png]
Reply


Messages In This Thread
GUI Shutdown or Restart (Linux) - by Keybone - 04-17-2023, 10:05 PM
RE: GUI Shutdown or Restart (Linux) - by Keybone - 04-18-2023, 12:24 AM
RE: GUI Shutdown or Restart (Linux) - by bplus - 04-19-2023, 03:33 PM
RE: GUI Shutdown or Restart (Linux) - by Keybone - 04-19-2023, 09:10 PM



Users browsing this thread: 1 Guest(s)