Code: Select all
; ====================================
; Name: xButton Gadget
; Version: 1.2
; Author: Mohsen
; Update: 01th May 2018
; License: Free
; ====================================
DeclareModule xButton
Enumeration 1
;
;Text alignment
#Text_Top_Left
#Text_Top_Center
#Text_Top_Right
#Text_Middle_Left
#Text_Middle_Center
#Text_Middle_Right
#Text_Bottom_Left
#Text_Bottom_Center
#Text_Bottom_Right
;
;Image alignment
#Image_Top_Left
#Image_Top_Center
#Image_Top_Right
#Image_Middle_Left
#Image_Middle_Center
#Image_Middle_Right
#Image_Bottom_Left
#Image_Bottom_Center
#Image_Bottom_Right
;
;Image and text relation
#Overlay
#Image_Above_Text
#Image_Before_Text
#Text_Above_Image
#Text_Before_Image
;
;States
#State_LockOnEnter
#State_Toggle
#State_Check
#State_Default
;
; Text Modes
#Mode_ReturnText
#Mode_WriteText
;
;Background States
#BGC_Normal
#BGC_OnEnter
;
;Read Order
#ReadOrder_Left = #PB_VectorParagraph_Left
#ReadOrder_Center = #PB_VectorParagraph_Center
#ReadOrder_Right = #PB_VectorParagraph_Right
EndEnumeration
Declare Create(xbtn_ID, X, Y, Width, Height, Text.s = "", ImageNumber = 0)
Declare Free(xbtn_ID)
Declare ImageAndTextRelation(xbtn_ID, State = -1, Redraw = #False)
Declare.l BackgroundColor(xbtn_ID,State,Color.l=0,Redraw=#False)
Declare ImageAlign(xbtn_ID, State = -1, Redraw = #False)
Declare TextAlign(xbtn_ID, State = -1, Redraw = #False)
Declare SetTextFont(xbtn_ID, fontNum, Redraw = #False)
Declare.l TextColor(xbtn_ID, Color.l = 0, Redraw = #False)
Declare.s Text_(xbtn_ID, Mode = #Mode_ReturnText, Text.s = "", Redraw = #False)
Declare SetIcon(xbtn_ID, _ImageNum, Redraw = #False)
Declare Redraw(xbtn_ID)
Declare State(xbtn_ID, State = -1, Redraw = #False)
Declare ReadOrder(xbtn_ID, Read_Order = -1, Redraw = #False)
Declare.l BorderColor(xbtn_ID, Color.l = 0, Redraw = #False)
Declare GetFitSize_BF(source_width, source_height, fit_width, fit_height)
Declare GetFitSize_Width_BF()
Declare GetFitSize_Height_BF()
EndDeclareModule
Module xButton
UsePNGImageDecoder()
UseJPEGImageDecoder()
UseGIFImageDecoder()
UseTIFFImageDecoder()
Enumeration
#xButton_TextWidth
#xButton_TextHeight
EndEnumeration
Structure xbtn_bg_Color
bgColor_Normal.l
bgColor_OnEnter.l
bgColor_Temp.l
EndStructure
Structure xbtn_TextOpt
_FontNum.l
_Color.l
_ReadOrder.b
_X.l
_Y.l
_W.l
_H.l
_TextAlign.b
EndStructure
Structure xbtn_ImgOpt
_X.l
_Y.l
_W.l
_H.l
_ImageAlign.b
EndStructure
Structure _xButton
ID.w
Width.l
Height.l
Text.s
ImageNum.w
State.b
BorderColor.l
IaT_relation.b
TextOptions.xbtn_TextOpt
ImageOptions.xbtn_ImgOpt
BackgroundColor.xbtn_bg_Color
EndStructure
Global.l xfont = LoadFont(#PB_Any,"tahoma",8,#PB_Font_HighQuality|#PB_Font_Bold)
Global CheckMark = CatchImage(#PB_Any,?Check_Mark)
Global shrinked_width,shrinked_height
;-
;- Internal Procedures
;-
Procedure xButton_GetData(xbtn_ID)
Protected *XButtonData._xButton = #Null
If GadgetType(xbtn_ID) = #PB_GadgetType_Canvas
*XButtonData = GetGadgetData(xbtn_ID)
Else
DebuggerWarning("Please set a valid gadget number.")
EndIf
ProcedureReturn *XButtonData
EndProcedure
Procedure xButton_TextSize(xbtn_ID, FontNum, Text.s, flag)
Protected result
If StartVectorDrawing(CanvasVectorOutput(xbtn_ID,#PB_Unit_Pixel))
If IsFont(FontNum)
VectorFont(FontID(FontNum))
If flag = #xButton_TextWidth
result = VectorTextWidth(Text)+1
ElseIf flag = #xButton_TextHeight
result = VectorTextHeight(Text)+1
EndIf
EndIf
StopVectorDrawing()
EndIf
ProcedureReturn result
EndProcedure
Procedure xButton_AlignmentAsRelation(xbtn_ID)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If \IaT_relation = #Image_Above_Text
If (\ImageOptions\_ImageAlign = #Image_Top_Left And \TextOptions\_TextAlign = #Text_Top_Left) Or
(\ImageOptions\_ImageAlign = #Image_Top_Center And \TextOptions\_TextAlign = #Text_Top_Center) Or
(\ImageOptions\_ImageAlign = #Image_Top_Right And \TextOptions\_TextAlign = #Text_Top_Right)
\TextOptions\_Y = \TextOptions\_Y + \ImageOptions\_H + 4
ElseIf (\ImageOptions\_ImageAlign = #Image_Middle_Left And \TextOptions\_TextAlign = #Text_Middle_Left) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Center And \TextOptions\_TextAlign = #Text_Middle_Center) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Right And \TextOptions\_TextAlign = #Text_Middle_Right)
\TextOptions\_Y = \ImageOptions\_Y + \ImageOptions\_H + 4
ElseIf (\ImageOptions\_ImageAlign = #Image_Bottom_Left And \TextOptions\_TextAlign = #Text_Bottom_Left) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Center And \TextOptions\_TextAlign = #Text_Bottom_Center) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Right And \TextOptions\_TextAlign = #Text_Bottom_Right)
\ImageOptions\_Y = \TextOptions\_Y - \ImageOptions\_H - 4
EndIf
;#################################################################################################
;#################################################################################################
ElseIf \IaT_relation = #Image_Before_Text
If (\ImageOptions\_ImageAlign = #Image_Top_Left And \TextOptions\_TextAlign = #Text_Top_Left) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Left And \TextOptions\_TextAlign = #Text_Middle_Left) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Left And \TextOptions\_TextAlign = #Text_Bottom_Left)
\TextOptions\_X = \TextOptions\_X + \ImageOptions\_W + 4
ElseIf (\ImageOptions\_ImageAlign = #Image_Top_Center And \TextOptions\_TextAlign = #Text_Top_Center) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Center And \TextOptions\_TextAlign = #Text_Middle_Center) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Center And \TextOptions\_TextAlign = #Text_Bottom_Center)
\ImageOptions\_X = \ImageOptions\_X - (\ImageOptions\_W /4)
\TextOptions\_X = \TextOptions\_X + (\TextOptions\_W >>1) + 4
ElseIf (\ImageOptions\_ImageAlign = #Image_Top_Right And \TextOptions\_TextAlign = #Text_Top_Right) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Right And \TextOptions\_TextAlign = #Text_Middle_Right) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Right And \TextOptions\_TextAlign = #Text_Bottom_Right)
\ImageOptions\_X = \TextOptions\_X - \ImageOptions\_W -4
EndIf
;#################################################################################################
;#################################################################################################
ElseIf \IaT_relation = #Text_Above_Image
If (\ImageOptions\_ImageAlign = #Image_Top_Left And \TextOptions\_TextAlign = #Text_Top_Left) Or
(\ImageOptions\_ImageAlign = #Image_Top_Center And \TextOptions\_TextAlign = #Text_Top_Center) Or
(\ImageOptions\_ImageAlign = #Image_Top_Right And \TextOptions\_TextAlign = #Text_Top_Right)
\ImageOptions\_Y = \ImageOptions\_Y + \TextOptions\_H -4
ElseIf (\ImageOptions\_ImageAlign = #Image_Middle_Left And \TextOptions\_TextAlign = #Text_Middle_Left) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Center And \TextOptions\_TextAlign = #Text_Middle_Center) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Right And \TextOptions\_TextAlign = #Text_Middle_Right)
\TextOptions\_Y = \ImageOptions\_Y - \TextOptions\_H - 4
ElseIf (\ImageOptions\_ImageAlign = #Image_Bottom_Left And \TextOptions\_TextAlign = #Text_Bottom_Left) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Center And \TextOptions\_TextAlign = #Text_Bottom_Center) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Right And \TextOptions\_TextAlign = #Text_Bottom_Right)
\TextOptions\_Y = \ImageOptions\_Y - \TextOptions\_H - 4
EndIf
;#################################################################################################
;#################################################################################################
ElseIf \IaT_relation = #Text_Before_Image
If (\ImageOptions\_ImageAlign = #Image_Top_Left And \TextOptions\_TextAlign = #Text_Top_Left) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Left And \TextOptions\_TextAlign = #Text_Middle_Left) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Left And \TextOptions\_TextAlign = #Text_Bottom_Left)
\ImageOptions\_X = \TextOptions\_X + \TextOptions\_W + 4
ElseIf (\ImageOptions\_ImageAlign = #Image_Top_Center And \TextOptions\_TextAlign = #Text_Top_Center) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Center And \TextOptions\_TextAlign = #Text_Middle_Center) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Center And \TextOptions\_TextAlign = #Text_Bottom_Center)
\ImageOptions\_X = \ImageOptions\_X + (\ImageOptions\_W /4)
\TextOptions\_X = \TextOptions\_X - (\TextOptions\_W >>1) - 4
ElseIf (\ImageOptions\_ImageAlign = #Image_Top_Right And \TextOptions\_TextAlign = #Text_Top_Right) Or
(\ImageOptions\_ImageAlign = #Image_Middle_Right And \TextOptions\_TextAlign = #Text_Middle_Right) Or
(\ImageOptions\_ImageAlign = #Image_Bottom_Right And \TextOptions\_TextAlign = #Text_Bottom_Right)
\TextOptions\_X = \TextOptions\_X - \ImageOptions\_W - 4
EndIf
EndIf
EndWith
EndIf
EndProcedure
Procedure xButton_AlignImage(xbtn_ID)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If IsImage(\ImageNum)
Select \ImageOptions\_ImageAlign
Case #Image_Top_Right
\ImageOptions\_X = (\Width- \ImageOptions\_W)-8
\ImageOptions\_Y = 8
Case #Image_Top_Left
\ImageOptions\_X = 8
\ImageOptions\_Y = 8
Case #Image_Top_Center
\ImageOptions\_X = (\Width >>1)-(\ImageOptions\_W >>1)
\ImageOptions\_Y = 8
Case #Image_Middle_Right
\ImageOptions\_X = (\Width- \ImageOptions\_W)-8
\ImageOptions\_Y = (\Height >>1)-(\ImageOptions\_H >>1)
Case #Image_Middle_Left
\ImageOptions\_X = 8
\ImageOptions\_Y = (\Height >>1)-(\ImageOptions\_H >>1)
Case #Image_Middle_Center
\ImageOptions\_X = (\Width >>1)-(\ImageOptions\_W >>1)
\ImageOptions\_Y = (\Height >>1)-(\ImageOptions\_H >>1)
Case #Image_Bottom_Right
\ImageOptions\_X = (\Width-\ImageOptions\_W)-8
\ImageOptions\_Y = (\Height-\ImageOptions\_H)-8
Case #Image_Bottom_Left
\ImageOptions\_X = 8
\ImageOptions\_Y = (\Height-\ImageOptions\_H)-8
Case #Image_Bottom_Center
\ImageOptions\_X = (\Width >>1)-(\ImageOptions\_W >>1)
\ImageOptions\_Y = (\Height-\ImageOptions\_H)-8
EndSelect
If \IaT_relation <> #Overlay
xButton_AlignmentAsRelation(\ID)
EndIf
EndIf
EndWith
EndIf
EndProcedure
Procedure xButton_AlignText(xbtn_ID)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
Select \TextOptions\_TextAlign
Case #Text_Top_Right
\TextOptions\_X = (\Width-\TextOptions\_W)-10
\TextOptions\_Y = 8
Case #Text_Top_Left
\TextOptions\_X = 10
\TextOptions\_Y = 8
Case #Text_Top_Center
\TextOptions\_X = (\Width>>1)-\TextOptions\_W>>1
\TextOptions\_Y = 8
Case #Text_Middle_Right
\TextOptions\_X = (\Width-\TextOptions\_W)-10
\TextOptions\_Y = (\Height>>1)-(\TextOptions\_H>>1)
Case #Text_Middle_Left
\TextOptions\_X = 10
\TextOptions\_Y = (\Height>>1)-(\TextOptions\_H>>1)
Case #Text_Middle_Center
\TextOptions\_X = (\Width>>1)-(\TextOptions\_W>>1)
\TextOptions\_Y = (\Height>>1)-(\TextOptions\_H>>1)
Case #Text_Bottom_Right
\TextOptions\_X = (\Width-\TextOptions\_W)-10
\TextOptions\_Y = (\Height-\TextOptions\_H)-8
Case #Text_Bottom_Left
\TextOptions\_X = 10
\TextOptions\_Y = (\Height-\TextOptions\_H)-8
Case #Text_Bottom_Center
\TextOptions\_X = (\Width>>1)-(\TextOptions\_W>>1)
\TextOptions\_Y = (\Height-\TextOptions\_H)-8
EndSelect
If \IaT_relation <> #Overlay
xButton_AlignmentAsRelation(\ID)
EndIf
EndWith
EndIf
EndProcedure
Procedure xButton_FullDraw(xbtn_ID, color.l)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
xButton_AlignImage(\ID)
xButton_AlignText(\ID)
If StartVectorDrawing(CanvasVectorOutput(\ID,#PB_Unit_Pixel))
VectorSourceColor(Color)
AddPathBox(0,0,\Width,\Height)
FillPath()
If Len(\Text)
If IsFont(\TextOptions\_FontNum)
VectorFont(FontID(\TextOptions\_FontNum))
VectorSourceColor(\TextOptions\_Color)
MovePathCursor(\TextOptions\_X,\TextOptions\_Y)
DrawVectorParagraph(\Text,\TextOptions\_W,\TextOptions\_H,\TextOptions\_ReadOrder)
EndIf
EndIf
If IsImage(\ImageNum)
MovePathCursor(\ImageOptions\_X,\ImageOptions\_Y)
DrawVectorImage(ImageID(\ImageNum),255)
EndIf
If \State = #State_Check
AddPathBox(0, 0, \Width, \Height)
VectorSourceColor(\BorderColor)
StrokePath(10)
MovePathCursor(\Width-22, -2)
AddPathLine(24, 26, #PB_Path_Relative)
VectorSourceColor(\BorderColor)
StrokePath(20)
MovePathCursor(\Width-ImageWidth(CheckMark)-4, 4)
DrawVectorImage(ImageID(CheckMark))
EndIf
StopVectorDrawing()
EndIf
EndWith
EndIf
EndProcedure
Procedure xButton_EventManager()
Protected E_ID,E_Type
E_ID = EventGadget()
E_Type = EventType()
Protected *XButtonData._xButton = xButton_GetData(E_ID)
If *XButtonData
With *XButtonData
Select E_Type
Case #PB_EventType_MouseEnter
xButton_FullDraw(\ID,\BackgroundColor\bgColor_OnEnter)
Case #PB_EventType_MouseLeave
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
Case #PB_EventType_LeftClick
If \State = #State_Toggle
If \BackgroundColor\bgColor_Normal = \BackgroundColor\bgColor_OnEnter
\BackgroundColor\bgColor_Normal = \BackgroundColor\bgColor_Temp
Else
\BackgroundColor\bgColor_Normal = \BackgroundColor\bgColor_OnEnter
EndIf
EndIf
Case #PB_EventType_Resize
\Width = GadgetWidth(\ID)
\Height = GadgetHeight(\ID)
EndSelect
EndWith
EndIf
EndProcedure
;-
;- External Procedures
;-
Procedure Create(xbtn_ID, X, Y, Width, Height, Text.s = "", ImageNumber = 0)
If CanvasGadget(xbtn_ID, X, Y, Width, Height)
Protected *XButtonData._xButton = AllocateStructure(_xButton)
If *XButtonData
With *XButtonData
\ID = xbtn_ID
\Width = Width
\Height = Height
\Text = Text
If IsImage(ImageNumber) : \ImageNum = ImageNumber : EndIf
If IsImage(ImageNumber) : \ImageOptions\_W = ImageWidth(ImageNumber) : EndIf
If IsImage(ImageNumber) : \ImageOptions\_H = ImageHeight(ImageNumber) : EndIf
\TextOptions\_FontNum = xfont
\TextOptions\_Color = RGBA(255,255,255,255)
\TextOptions\_W = xButton_TextSize(xbtn_ID,xfont,Text,#xButton_TextWidth)
\TextOptions\_H = xButton_TextSize(xbtn_ID,xfont,Text,#xButton_TextHeight)
\ImageOptions\_ImageAlign = #Image_Middle_Left
\TextOptions\_TextAlign = #Text_Middle_Left
\IaT_relation = #Image_Before_Text
\BackgroundColor\bgColor_Normal = RGBA(70, 130, 180, 255)
\BackgroundColor\bgColor_OnEnter = RGBA(67, 116, 157, 255)
\BackgroundColor\bgColor_Temp = RGBA(70, 130, 180, 255)
EndWith
EndIf
SetGadgetData(xbtn_ID, *XButtonData)
BindGadgetEvent(xbtn_ID,@xButton_EventManager())
xButton_FullDraw(xbtn_ID,RGBA(70, 130, 180, 255))
EndIf
EndProcedure
Procedure Free(xbtn_ID)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
FreeStructure(*XButtonData)
FreeGadget(xbtn_ID)
EndIf
EndProcedure
Procedure ImageAndTextRelation(xbtn_ID, State = -1, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If State <> -1
\IaT_relation = State
Else
ProcedureReturn \IaT_relation
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
EndProcedure
Procedure.l BackgroundColor(xbtn_ID, State, Color.l = 0, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
Protected.l result
If State = #BGC_Normal
If Color
\BackgroundColor\bgColor_Normal = Color
\BackgroundColor\bgColor_Temp = Color
Else
result = \BackgroundColor\bgColor_Normal
EndIf
ElseIf State = #BGC_OnEnter
If Color
\BackgroundColor\bgColor_OnEnter = Color
Else
result = \BackgroundColor\bgColor_OnEnter
EndIf
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
ProcedureReturn result
EndProcedure
Procedure ImageAlign(xbtn_ID, State = -1, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If State <> -1
\ImageOptions\_ImageAlign = State
Else
ProcedureReturn \ImageOptions\_ImageAlign
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
EndProcedure
Procedure TextAlign(xbtn_ID, State = -1, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If State <> -1
\TextOptions\_TextAlign = State
Else
ProcedureReturn \TextOptions\_TextAlign
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
EndProcedure
Procedure SetTextFont(xbtn_ID, fontNum, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If IsFont(fontNum)
\TextOptions\_FontNum = fontNum
\TextOptions\_W = xButton_TextSize(\ID,fontNum,\Text,#xButton_TextWidth)
\TextOptions\_H = xButton_TextSize(\ID,fontNum,\Text,#xButton_TextHeight)
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
EndProcedure
Procedure.l TextColor(xbtn_ID, Color.l = 0, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If Color
\TextOptions\_Color= Color
Else
Protected.l result = \TextOptions\_Color
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
ProcedureReturn result
EndProcedure
Procedure.s Text_(xbtn_ID, Mode = #Mode_ReturnText, Text.s = "", Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If Mode = #Mode_WriteText
\Text = Text
\TextOptions\_W = xButton_TextSize(\ID,\TextOptions\_FontNum,\Text,#xButton_TextWidth)
\TextOptions\_H = xButton_TextSize(\ID,\TextOptions\_FontNum,\Text,#xButton_TextHeight)
ElseIf mode = #Mode_ReturnText
Protected.s result = \Text
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
ProcedureReturn result
EndProcedure
Procedure SetIcon(xbtn_ID, _ImageNum, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If IsImage(_ImageNum)
\ImageNum = _ImageNum
\ImageOptions\_W = ImageWidth(\ImageNum)
\ImageOptions\_H = ImageHeight(\ImageNum)
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
EndProcedure
Procedure Redraw(xbtn_ID)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndWith
EndIf
EndProcedure
Procedure State(xbtn_ID, State = -1, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
Select State
Case #State_LockOnEnter
\BackgroundColor\bgColor_Normal = \BackgroundColor\bgColor_OnEnter
\State = #State_LockOnEnter
Case #State_Toggle
\State = #State_Toggle
Case #State_Check
\State = #State_Check
Case #State_Default
\BackgroundColor\bgColor_Normal = \BackgroundColor\bgColor_Temp
\State = #State_Default
Case -1 : ProcedureReturn \State
EndSelect
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
EndProcedure
Procedure ReadOrder(xbtn_ID, Read_Order = -1, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
If *XButtonData
With *XButtonData
If Read_Order <> -1
\TextOptions\_ReadOrder = Read_Order
Else
ProcedureReturn \TextOptions\_ReadOrder
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
EndProcedure
Procedure.l BorderColor(xbtn_ID, Color.l = 0, Redraw = #False)
Protected *XButtonData._xButton = xButton_GetData(xbtn_ID)
Protected.l result
If *XButtonData
With *XButtonData
If Color
\BorderColor = Color
Else
result = \BorderColor
EndIf
If Redraw
xButton_FullDraw(\ID,\BackgroundColor\bgColor_Normal)
EndIf
EndWith
EndIf
ProcedureReturn result
EndProcedure
Procedure GetFitSize_BF(source_width, source_height, fit_width, fit_height)
; This function is a part from GFX_Wizzard_BF - Using here is OK
Protected.f shrink_percent_x, shrink_percent_y
If source_width*fit_width>0
shrink_percent_x=100/source_width*fit_width
Else
shrink_percent_x=100
EndIf
If source_height*fit_height>0
shrink_percent_y=100/source_height*fit_height
Else
shrink_percent_y=100
EndIf
If shrink_percent_x<shrink_percent_y
shrinked_width=source_width/100*shrink_percent_x
shrinked_height=source_height/100*shrink_percent_x
Else
shrinked_width=source_width/100*shrink_percent_y
shrinked_height=source_height/100*shrink_percent_y
EndIf
If shrinked_width<fit_width
new_x=fit_width/2-shrinked_width/2-1
Else
new_x=0
EndIf
If shrinked_height<fit_height
new_y=fit_height/2-shrinked_height/2-1
Else
new_y=0
EndIf
; Fuse for resizing functions
If shrinked_width<1 : shrinked_width=1 : EndIf
If shrinked_height<1 : shrinked_height=1 : EndIf
ProcedureReturn 1
EndProcedure
Procedure GetFitSize_Width_BF()
ProcedureReturn shrinked_width
EndProcedure
Procedure GetFitSize_Height_BF()
ProcedureReturn shrinked_height
EndProcedure
DataSection
Check_Mark:
Data.q $0A1A0A0D474E5089,$524448490D000000,$0C0000000C000000,$5C75560000000608,$59487009000000E7,$0B0000130B000073,$0000189C9A000113,$00004D5248632000,$000083800000257A,$0000E9800000FFF9
Data.q $000060EA00003075,$5F926F170000983A,$4449B000000046C5,$6A3DD08CDA785441,$26D111E186105142,$590BA42DC100B884,$D1B2B56C10103640,$80B3690B700B32D2,$A522C0432903E914,$858F08A25614FA5D
Data.q $39981C1FAAF7F173,$A4571054E1877DE7,$9D2E0698889D111B,$13F704BE78564DB1,$3CC7BE0E7109E25E,$9DF0D7065F714185,$F84A30995BEEFBF0,$B759771D4253CD1F,$88F02C3878614B30,$E6FF09AF16E17C1F
Data.q $64587F36C5E123A0,$CFD9BC516EB53EDD,$A450AE4DC273D504,$F13E03CDBBDDF657,$492A001EB0994681,$00008CA63B2911B2,$42AE444E45490000
Data.b $60,$82
EndDataSection
EndModule
CompilerIf #PB_Compiler_ExecutableFormat=#PB_Compiler_Executable
Enumeration
#_Window
#_Canvas
#_CanvasTextFont
#_HomeBTN
#_NewProjBTN
#_OpenLatBTN
#_MyxButton
#_ImageAlignCombo
#_TextAlignCombo
#_ImageAndTextRelationCombo
#_NormalColorBTN
#_OnEnterColorBTN
#_FontBTN
#_Input
#_SetTextBTN
#_SetImageBTN
#_FreeBTN
#_InfoBTN
#_State_OnEnter
#_State_Toggle
#ReadOrderCombo
#_State_Check
EndEnumeration
If OpenWindow(#_Window,0,0,760,480,"xButton",#PB_Window_SystemMenu | #PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget)
UseModule xButton
If CanvasGadget(#_Canvas,0,0,230,WindowHeight(#_Window),#PB_Canvas_Container)
LoadFont(#_CanvasTextFont, "Segoe UI", 0,#PB_Font_HighQuality)
StartVectorDrawing(CanvasVectorOutput(#_Canvas,#PB_Unit_Pixel))
VectorSourceColor(RGBA(220,20,60,255))
AddPathBox(0,0,300,WindowHeight(#_Window))
FillPath()
VectorFont(FontID(#_CanvasTextFont), 30)
VectorSourceColor(RGBA(255, 255, 255, 255))
MovePathCursor(10, 5)
txt.s = "Start Page"
DrawVectorParagraph(txt, VectorTextWidth(txt), VectorTextHeight(txt))
StopVectorDrawing()
xButton::Create(#_HomeBTN,1,60,220,60,"Home")
xButton::Create(#_NewProjBTN,1,120,220,60,"New Project")
xButton::Create(#_OpenLatBTN,1,180,220,60,"Open Latest")
CloseGadgetList()
EndIf
For i=3 To 5
xButton::BackgroundColor(i,xButton::#BGC_Normal,RGBA(220,20,60,255),1)
xButton::BackgroundColor(i,xButton::#BGC_OnEnter,RGBA(189,26,59,255),1)
Next
xButton::State(#_NewProjBTN,xButton::#State_LockOnEnter,#True)
xButton::Create(#_MyxButton,400,50,220,150,"Microsoft Visual"+#LF$+"Studio 2017")
xButton::TextAlign(#_MyxButton,xButton::#Text_Bottom_Left)
xButton::ImageAlign(#_MyxButton,xButton::#Image_Middle_Center)
xButton::ImageAndTextRelation(#_MyxButton,xButton::#Overlay)
xButton::BorderColor(#_MyxButton,RGBA(0,88,88,255))
xButton::BackgroundColor(#_MyxButton,xButton::#BGC_Normal,RGBA(255,140,0,255))
xButton::BackgroundColor(#_MyxButton,xButton::#BGC_OnEnter,RGBA(218,124,11,255),#True)
FrameGadget(#PB_Any,250,230,495,100,"Alignment")
TextGadget(#PB_Any,260,260,100,20,"Image Align")
ComboBoxGadget(#_ImageAlignCombo,260,280,160,30)
AddGadgetItem(#_ImageAlignCombo,-1,"Image_Top_Left")
SetGadgetItemData(#_ImageAlignCombo, 0, xButton::#Image_Top_Left)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Top_Center")
SetGadgetItemData(#_ImageAlignCombo, 1, xButton::#Image_Top_Center)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Top_Right")
SetGadgetItemData(#_ImageAlignCombo, 2, xButton::#Image_Top_Right)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Middle_Left")
SetGadgetItemData(#_ImageAlignCombo, 3, xButton::#Image_Middle_Left)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Middle_Center")
SetGadgetItemData(#_ImageAlignCombo, 4, xButton::#Image_Middle_Center)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Middle_Right")
SetGadgetItemData(#_ImageAlignCombo, 5, xButton::#Image_Middle_Right)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Bottom_Left")
SetGadgetItemData(#_ImageAlignCombo, 6, xButton::#Image_Bottom_Left)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Bottom_Center")
SetGadgetItemData(#_ImageAlignCombo, 7, xButton::#Image_Bottom_Center)
AddGadgetItem(#_ImageAlignCombo,-1,"#Image_Bottom_Right")
SetGadgetItemData(#_ImageAlignCombo, 8, xButton::#Image_Bottom_Right)
SetGadgetState(#_ImageAlignCombo,4)
TextGadget(#PB_Any,425,260,100,20,"Text Align")
ComboBoxGadget(#_TextAlignCombo,425,280,150,30)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Top_Left")
SetGadgetItemData(#_TextAlignCombo, 0, xButton::#Text_Top_Left)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Top_Center")
SetGadgetItemData(#_TextAlignCombo, 1, xButton::#Text_Top_Center)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Top_Right")
SetGadgetItemData(#_TextAlignCombo, 2, xButton::#Text_Top_Right)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Middle_Left")
SetGadgetItemData(#_TextAlignCombo, 3, xButton::#Text_Middle_Left)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Middle_Center")
SetGadgetItemData(#_TextAlignCombo, 4, xButton::#Text_Middle_Center)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Middle_Right")
SetGadgetItemData(#_TextAlignCombo, 5, xButton::#Text_Middle_Right)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Bottom_Left")
SetGadgetItemData(#_TextAlignCombo, 6, xButton::#Text_Bottom_Left)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Bottom_Center")
SetGadgetItemData(#_TextAlignCombo, 7, xButton::#Text_Bottom_Center)
AddGadgetItem(#_TextAlignCombo,-1,"#Text_Bottom_Right")
SetGadgetItemData(#_TextAlignCombo, 8, xButton::#Text_Bottom_Right)
SetGadgetState(#_TextAlignCombo,6)
TextGadget(#PB_Any,580,260,150,20,"Image And text Relation")
ComboBoxGadget(#_ImageAndTextRelationCombo,580,280,155,30)
AddGadgetItem(#_ImageAndTextRelationCombo,-1,"#Overlay")
SetGadgetItemData(#_ImageAndTextRelationCombo, 0, xButton::#Overlay)
AddGadgetItem(#_ImageAndTextRelationCombo,-1,"#Image_Above_Text")
SetGadgetItemData(#_ImageAndTextRelationCombo, 1, xButton::#Image_Above_Text)
AddGadgetItem(#_ImageAndTextRelationCombo,-1,"#Image_Before_Text")
SetGadgetItemData(#_ImageAndTextRelationCombo, 2, xButton::#Image_Before_Text)
AddGadgetItem(#_ImageAndTextRelationCombo,-1,"#Text_Above_Image")
SetGadgetItemData(#_ImageAndTextRelationCombo, 3, xButton::#Text_Above_Image)
AddGadgetItem(#_ImageAndTextRelationCombo,-1,"#Text_Before_Image")
SetGadgetItemData(#_ImageAndTextRelationCombo, 4, xButton::#Text_Before_Image)
SetGadgetState(#_ImageAndTextRelationCombo,0)
FrameGadget(#PB_Any,250,340,495,130,"Other Properties")
ButtonGadget(#_NormalColorBTN,260,360,80,20,"Normal Color")
ButtonGadget(#_OnEnterColorBTN,260,382,80,20,"OnEnter Color")
ButtonGadget(#_FontBTN,260,420,80,20,"Font")
EditorGadget(#_Input,380,360,100,50)
ButtonGadget(#_SetTextBTN,380,420,80,20,"Set Text")
ButtonGadget(#_SetImageBTN,510,360,80,20,"Set Icon")
ButtonGadget(#_FreeBTN,510,382,80,20,"Free")
ButtonGadget(#_InfoBTN, 620, 360, 110, 20,"Info")
ButtonGadget(#_State_OnEnter,620,382,110,20,"State_LockOnEnter")
ButtonGadget(#_State_Toggle,620,404,110,20,"State_Toggle")
ButtonGadget(#_State_Check,620,426,110,20,"State_Check")
ComboBoxGadget(#ReadOrderCombo,500,410,100,20)
AddGadgetItem(#ReadOrderCombo,-1,"#ReadOrder_Left")
SetGadgetItemData(#ReadOrderCombo, 0, xButton::#ReadOrder_Left)
AddGadgetItem(#ReadOrderCombo,-1,"#ReadOrder_Center")
SetGadgetItemData(#ReadOrderCombo, 1, xButton::#ReadOrder_Center)
AddGadgetItem(#ReadOrderCombo,-1,"#ReadOrder_Right")
SetGadgetItemData(#ReadOrderCombo, 2, xButton::#ReadOrder_Right)
SetGadgetState(#ReadOrderCombo,0)
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case #ReadOrderCombo
If EventType() = #PB_EventType_Change
xButton::ReadOrder(#_MyxButton,GetGadgetItemData(#ReadOrderCombo,GetGadgetState(#ReadOrderCombo)),#True)
EndIf
Case #_HomeBTN
If EventType() = #PB_EventType_LeftClick
If xButton::State(#_NewProjBTN) = xButton::#State_LockOnEnter
xButton::State(#_NewProjBTN,xButton::#State_Default,#True)
ElseIf xButton::State(#_OpenLatBTN) = xButton::#State_LockOnEnter
xButton::State(#_OpenLatBTN,xButton::#State_Default,#True)
EndIf
xButton::State(#_HomeBTN,xButton::#State_LockOnEnter,#True)
EndIf
Case #_NewProjBTN
If EventType() = #PB_EventType_LeftClick
If xButton::State(#_HomeBTN) = xButton::#State_LockOnEnter
xButton::State(#_HomeBTN,xButton::#State_Default,#True)
ElseIf xButton::State(#_OpenLatBTN) = xButton::#State_LockOnEnter
xButton::State(#_OpenLatBTN,xButton::#State_Default,#True)
EndIf
xButton::State(#_NewProjBTN,xButton::#State_LockOnEnter,#True)
EndIf
Case #_OpenLatBTN
If EventType() = #PB_EventType_LeftClick
If xButton::State(#_HomeBTN) = xButton::#State_LockOnEnter
xButton::State(#_HomeBTN,xButton::#State_Default,#True)
ElseIf xButton::State(#_NewProjBTN) = xButton::#State_LockOnEnter
xButton::State(#_NewProjBTN,xButton::#State_Default,#True)
EndIf
xButton::State(#_OpenLatBTN,xButton::#State_LockOnEnter,#True)
EndIf
Case #_NormalColorBTN
If EventType() = #PB_EventType_LeftClick
_rgba = ColorRequester()
If _rgba <> -1
xButton::BackgroundColor(#_MyxButton,xButton::#BGC_Normal,RGBA(Red(_rgba),Green(_rgba),Blue(_rgba),255),#True)
EndIf
EndIf
Case #_OnEnterColorBTN
If EventType() = #PB_EventType_LeftClick
_rgba = ColorRequester()
If _rgba <> -1
xButton::BackgroundColor(#_MyxButton,xButton::#BGC_OnEnter,RGBA(Red(_rgba),Green(_rgba),Blue(_rgba),255),#True)
EndIf
EndIf
Case #_FontBTN
If FontRequester("Segoe UI",18,#PB_FontRequester_Effects)
fnt.l = LoadFont(#PB_Any,SelectedFontName(),SelectedFontSize(),SelectedFontStyle())
fnt_color.l = SelectedFontColor()
xButton::TextColor(#_MyxButton,RGBA(Red(fnt_color),Green(fnt_color),Blue(fnt_color),255))
xButton::SetTextFont(#_MyxButton,fnt,#True)
EndIf
Case #_SetTextBTN
xButton::Text_(#_MyxButton,xButton::#Mode_WriteText,GetGadgetText(#_Input),#True)
Case #_SetImageBTN
myFile$ = OpenFileRequester("Please choose file to load", "", "", 0)
LoadImage(689,myFile$)
If ImageWidth(689)>GadgetWidth(#_MyxButton) Or ImageHeight(689)>GadgetHeight(#_MyxButton)
GetFitSize_BF(ImageWidth(689), ImageHeight(689), GadgetWidth(#_MyxButton), GadgetHeight(#_MyxButton))
ResizeImage(689, GetFitSize_Width_BF(), GetFitSize_height_BF())
EndIf
xButton::SetIcon(#_MyxButton,689,#True)
Case #_FreeBTN
xButton::Free(#_MyxButton)
Case #_InfoBTN
Debug "#####################################"
Debug "Your xButton Text : " + xButton::Text_(#_MyxButton)
Debug "Your xButton BackgroundColor Normal : " + xButton::BackgroundColor(#_MyxButton,xButton::#BGC_Normal)
Debug "Your xButton BackgroundColor OnEnter : " + xButton::BackgroundColor(#_MyxButton,xButton::#BGC_OnEnter)
Debug "Your xButton Image Align : " + xButton::ImageAlign(#_MyxButton)
Debug "Your xButton Text Align : " + xButton::TextAlign(#_MyxButton)
Debug "Your xButton Image And text relation : " + xButton::ImageAndTextRelation(#_MyxButton)
Debug "Your xButton TextColor : " + xButton::TextColor(#_MyxButton)
Debug "Your xButton State : " + xButton::State(#_MyxButton)
Debug "Your xButton ReadOrder : " + xButton::ReadOrder(#_MyxButton)
Debug "#####################################"
Case #_State_OnEnter
If xButton::State(#_MyxButton) = xButton::#State_LockOnEnter
xButton::State(#_MyxButton, xButton::#State_Default,#True)
Else
xButton::State(#_MyxButton, xButton::#State_LockOnEnter,#True)
EndIf
Case #_State_Toggle
If xButton::State(#_MyxButton) = xButton::#State_Toggle
xButton::State(#_MyxButton, xButton::#State_Default,#True)
Else
xButton::State(#_MyxButton, xButton::#State_Toggle,#True)
EndIf
Case #_State_Check
If xButton::State(#_MyxButton) = xButton::#State_Check
xButton::State(#_MyxButton, xButton::#State_Default,#True)
Else
xButton::State(#_MyxButton, xButton::#State_Check,#True)
EndIf
Case #_ImageAlignCombo
If EventType() = #PB_EventType_Change
xButton::ImageAlign(#_MyxButton,GetGadgetItemData(#_ImageAlignCombo,GetGadgetState(#_ImageAlignCombo)),#True)
EndIf
Case #_TextAlignCombo
If EventType() = #PB_EventType_Change
xButton::TextAlign(#_MyxButton,GetGadgetItemData(#_TextAlignCombo,GetGadgetState(#_TextAlignCombo)),#True)
EndIf
Case #_ImageAndTextRelationCombo
If EventType() = #PB_EventType_Change
xButton::ImageAndTextRelation(#_MyxButton,GetGadgetItemData(#_ImageAndTextRelationCombo,GetGadgetState(#_ImageAndTextRelationCombo)),#True)
EndIf
EndSelect
ElseIf Event = #PB_Event_SizeWindow
ResizeGadget(#_MyxButton,#PB_Ignore,#PB_Ignore,WindowWidth(#_Window)-500,WindowHeight(#_Window)-327)
xButton::Redraw(#_MyxButton)
EndIf
Until Event = #PB_Event_CloseWindow
EndIf
CompilerEndIf