It is currently Sun Apr 22, 2018 7:34 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: [Module] xButton Gadget
PostPosted: Sun Apr 01, 2018 9:06 am 
Offline
User
User

Joined: Fri Dec 10, 2010 6:31 pm
Posts: 47
Hi all,
I tried to make a button with more features.The name of this button is xButton.
This button is similar to the Metro UI tiles.The project is still ongoing, but its current capabilities are as follows :

Current Procedures :

Create(xbtn_ID, X, Y, Width, Height, Text$ = "", ImageNumber = 0)
Free(xbtn_ID)
ImageAndTextRelation(xbtn_ID,State=0,Redraw=#False) ;Set/Get Procedure
BackgroundColor(xbtn_ID,State,Color.l=0,Redraw=#False) ;Set/Get Procedure
ImageAlign(xbtn_ID,State=0,Redraw=#False) ;Set/Get Procedure
TextAlign(xbtn_ID,State=0,Redraw=#False) ;Set/Get Procedure
SetTextFont(xbtn_ID,fontNum,Redraw=#False)
TextColor(xbtn_ID,Color.l=0,Redraw=#False) ;Set/Get Procedure
Text_(xbtn_ID,Text$="",Redraw=#False) ;Set/Get Procedure
SetImage(xbtn_ID,_ImageNum,Redraw=#False)
Redraw(xbtn_ID)
State(xbtn_ID,State=0,Redraw=#False) ;Set/Get Procedure
SetReadOrder(xbtn_ID,ReadOrder,Redraw=#False)

Attention: To correct run ImageAndTextRelation,Text and Image should be aligned like:
#Image_Top_Left with #Text_Top_Left
#Text_Middle_Center with #Image_Middle_Center

for my first job I hope you enjoy it. :wink:

Code:
; ====================================
; Name:             xButton Gadget
; Version:          1.0
; Author:           Mohsen
; Update:           16th Apr 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_SwapBackground
    ;#State_Toggle
    #State_Default
    ;
    ;Background States 
    #BGC_Normal
    #BGC_OnEnter
    ;
    ;Read Order
    #ReadOrder_Left
    #ReadOrder_Center
    #ReadOrder_Right
  EndEnumeration 
 
  Declare Create(xbtn_ID, X, Y, Width, Height, Text$ = "", ImageNumber = 0)
  Declare Free(xbtn_ID)
  Declare ImageAndTextRelation(xbtn_ID,State=0,Redraw=#False)
  Declare.l BackgroundColor(xbtn_ID,State,Color.l=0,Redraw=#False)
  Declare ImageAlign(xbtn_ID,State=0,Redraw=#False)
  Declare TextAlign(xbtn_ID,State=0,Redraw=#False)
  Declare SetTextFont(xbtn_ID,fontNum,Redraw=#False)
  Declare.l TextColor(xbtn_ID,Color.l=0,Redraw=#False)
  Declare$ Text_(xbtn_ID,Text$="",Redraw=#False)
  Declare SetImage(xbtn_ID,_ImageNum,Redraw=#False)
  Declare Redraw(xbtn_ID)
  Declare State(xbtn_ID,State=0,Redraw=#False)
  Declare SetReadOrder(xbtn_ID,ReadOrder,Redraw=#False)
EndDeclareModule


Module xButton
 
  Enumeration
    #xButton_TextWidth
    #xButton_TextHeight
  EndEnumeration   
 
  Structure xbtn_bg_Color
    bgColor_Normal.l
    bgColor_OnEnter.l
    TempColor.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$
    ImageNum.w
    State.b
    IaT_relation.b
    TextOptions.xbtn_TextOpt
    ImageOptions.xbtn_ImgOpt
    BackgroundColor.xbtn_bg_Color
  EndStructure 
 
 
  Global NewList xButtonList._xButton()
  Global.l font = LoadFont(#PB_Any,"tahoma",8,#PB_Font_HighQuality|#PB_Font_Bold)
  ;-
  ;- Internal Procedures
  ;-
  Procedure xButton_TextSize(flag)
    Protected result
    With xButtonList()
      If StartVectorDrawing(CanvasVectorOutput(\ID,#PB_Unit_Pixel))
        If IsFont(\TextOptions\_FontNum)
          VectorFont(FontID(\TextOptions\_FontNum))
          If flag = #xButton_TextWidth
            result =  VectorTextWidth(\Text$)
          ElseIf flag = #xButton_TextHeight
            result =  VectorTextHeight(\Text$)
          EndIf
        EndIf
        StopVectorDrawing()
      EndIf
    EndWith
    ProcedureReturn result
  EndProcedure 
 
  Procedure xButton_AlignmentAsRelation()
   
    With xButtonList()
     
      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 /2) + 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 /2) - 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
   
  EndProcedure 
 
  Procedure xButton_AlignImage()
   
    With xButtonList()
     
      If IsImage(\ImageNum)
       
        Select \ImageOptions\_ImageAlign
          Case #Image_Top_Right
            \ImageOptions\_X = (\Width- \ImageOptions\_W)-5
            \ImageOptions\_Y = 5
          Case #Image_Top_Left
            \ImageOptions\_X = 5
            \ImageOptions\_Y = 5 
          Case #Image_Top_Center
            \ImageOptions\_X = (\Width/2)-(\ImageOptions\_W /2)
            \ImageOptions\_Y = 5   
          Case #Image_Middle_Right
            \ImageOptions\_X = (\Width- \ImageOptions\_W)-5
            \ImageOptions\_Y = (\Height/2)-(\ImageOptions\_H /2)     
          Case #Image_Middle_Left
            \ImageOptions\_X = 5
            \ImageOptions\_Y = (\Height/2)-(\ImageOptions\_H/2)     
          Case #Image_Middle_Center
            \ImageOptions\_X = (\Width/2)-(\ImageOptions\_W/2)
            \ImageOptions\_Y = (\Height/2)-(\ImageOptions\_H/2)
          Case #Image_Bottom_Right
            \ImageOptions\_X = (\Width-\ImageOptions\_W)-5
            \ImageOptions\_Y = (\Height-\ImageOptions\_H)-5
          Case #Image_Bottom_Left
            \ImageOptions\_X = 5
            \ImageOptions\_Y = (\Height-\ImageOptions\_H)-5       
          Case #Image_Bottom_Center
            \ImageOptions\_X = (\Width/2)-(\ImageOptions\_W/2)
            \ImageOptions\_Y = (\Height-\ImageOptions\_H)-5     
        EndSelect
       
        If \IaT_relation <> #Overlay
          xButton_AlignmentAsRelation()
        EndIf
       
      EndIf
     
    EndWith
   
  EndProcedure 
 
  Procedure xButton_AlignText()
   
    With xButtonList()
     
      Select \TextOptions\_TextAlign
        Case #Text_Top_Right
          \TextOptions\_X = (\Width-\TextOptions\_W)-5
          \TextOptions\_Y = 5
        Case #Text_Top_Left
          \TextOptions\_X = 5
          \TextOptions\_Y = 5 
        Case #Text_Top_Center
          \TextOptions\_X = (\Width/2)-\TextOptions\_W/2
          \TextOptions\_Y = 5   
        Case #Text_Middle_Right
          \TextOptions\_X = (\Width-\TextOptions\_W)-5
          \TextOptions\_Y = (\Height/2)-(\TextOptions\_H/2)     
        Case #Text_Middle_Left
          \TextOptions\_X = 5
          \TextOptions\_Y = (\Height/2)-(\TextOptions\_H/2)     
        Case #Text_Middle_Center
          \TextOptions\_X = (\Width/2)-(\TextOptions\_W/2)
          \TextOptions\_Y = (\Height/2)-(\TextOptions\_H/2)
        Case #Text_Bottom_Right
          \TextOptions\_X = (\Width-\TextOptions\_W)-5
          \TextOptions\_Y = (\Height-\TextOptions\_H)-5
        Case #Text_Bottom_Left
          \TextOptions\_X = 5
          \TextOptions\_Y = (\Height-\TextOptions\_H)-5       
        Case #Text_Bottom_Center
          \TextOptions\_X = (\Width/2)-(\TextOptions\_W/2)
          \TextOptions\_Y = (\Height-\TextOptions\_H)-5     
      EndSelect 
     
      If \IaT_relation <> #Overlay
        xButton_AlignmentAsRelation()
      EndIf
     
    EndWith
   
  EndProcedure 
 
  Procedure xButton_FullDraw(color.l)
   
    With xButtonList()
     
      xButton_AlignImage()
      xButton_AlignText()
     
      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$,VectorTextWidth(\Text$),VectorTextHeight(\Text$),\TextOptions\_ReadOrder)
          EndIf
        EndIf
       
        If IsImage(\ImageNum)
          MovePathCursor(\ImageOptions\_X,\ImageOptions\_Y)
          DrawVectorImage(ImageID(\ImageNum),255)
        EndIf
       
        StopVectorDrawing()
      EndIf
     
    EndWith
   
  EndProcedure 
 
  Procedure xButton_EventManager()
    Protected E_ID,E_Type
    E_ID = EventGadget()
    E_Type = EventType()
    With xButtonList()
      ForEach xButtonList()
        Select E_Type
          Case #PB_EventType_MouseEnter   
            If E_ID = \ID : xButton_FullDraw(\BackgroundColor\bgColor_OnEnter) : EndIf
          Case #PB_EventType_MouseLeave       
            If E_ID = \ID : xButton_FullDraw(\BackgroundColor\bgColor_Normal)  : EndIf     
          Case #PB_EventType_Resize
            If E_ID = \ID
              \Width  = GadgetWidth(E_ID)
              \Height = GadgetHeight(E_ID)
            EndIf 
        EndSelect
      Next
    EndWith
  EndProcedure 
  ;-
  ;- External Procedures
  ;- 
  Procedure Create(xbtn_ID, X, Y, Width, Height, Text$ = "", ImageNumber = 0)
    If CanvasGadget(xbtn_ID, X, Y, Width, Height)
      If AddElement(xButtonList())
        xButtonList()\ID = xbtn_ID
        xButtonList()\Width = Width
        xButtonList()\Height = Height
        xButtonList()\Text$ = Text$
        If IsImage(ImageNumber) : xButtonList()\ImageNum = ImageNumber : EndIf
        If IsImage(ImageNumber) : xButtonList()\ImageOptions\_W = ImageWidth(ImageNumber) : EndIf
        If IsImage(ImageNumber) : xButtonList()\ImageOptions\_H = ImageHeight(ImageNumber) : EndIf
        xButtonList()\TextOptions\_FontNum = font
        xButtonList()\TextOptions\_Color = RGBA(255,255,255,255)     
        xButtonList()\TextOptions\_W = xButton_TextSize(#xButton_TextWidth)
        xButtonList()\TextOptions\_H = xButton_TextSize(#xButton_TextHeight)
        xButtonList()\ImageOptions\_ImageAlign = #Image_Middle_Left
        xButtonList()\TextOptions\_TextAlign = #Text_Middle_Left
        xButtonList()\IaT_relation = #Image_Before_Text
        xButtonList()\BackgroundColor\bgColor_Normal = RGBA(70, 130, 180, 255)
        xButtonList()\BackgroundColor\bgColor_OnEnter = RGBA(67, 116, 157, 255)
        xButtonList()\BackgroundColor\TempColor = RGBA(70, 130, 180, 255)
      EndIf 
      BindGadgetEvent(xbtn_ID,@xButton_EventManager())     
      xButton_FullDraw(RGBA(70, 130, 180, 255))
    EndIf       
  EndProcedure
 
  Procedure Free(xbtn_ID)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If IsGadget(\ID)
            DeleteElement(xButtonList())
            FreeGadget(xbtn_ID)
          EndIf
          Break
        EndIf
      Next 
    EndWith
  EndProcedure
 
  Procedure ImageAndTextRelation(xbtn_ID,State=0,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If State
            \IaT_relation = State
          Else
            Protected result = \IaT_relation
          EndIf
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf     
          Break
        EndIf
      Next 
    EndWith
    ProcedureReturn result
  EndProcedure
 
  Procedure.l BackgroundColor(xbtn_ID,State,Color.l=0,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          Protected.l result
          If State = #BGC_Normal
            If Color
              \BackgroundColor\bgColor_Normal = Color
              \BackgroundColor\TempColor = 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(\BackgroundColor\bgColor_Normal)
          EndIf 
          Break
        EndIf
      Next 
    EndWith
    ProcedureReturn result 
  EndProcedure
 
  Procedure ImageAlign(xbtn_ID,State=0,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If State
            \ImageOptions\_ImageAlign = State
          Else
            Protected result = \ImageOptions\_ImageAlign
          EndIf 
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf 
          Break
        EndIf
      Next 
    EndWith
    ProcedureReturn result 
  EndProcedure
 
  Procedure TextAlign(xbtn_ID,State=0,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If State
            \TextOptions\_TextAlign = State
          Else
            Protected result = \TextOptions\_TextAlign
          EndIf
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf 
          Break
        EndIf
      Next 
    EndWith
    ProcedureReturn result 
  EndProcedure
 
  Procedure SetTextFont(xbtn_ID,fontNum,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If IsFont(fontNum)
            \TextOptions\_FontNum = fontNum
            \TextOptions\_W = xButton_TextSize(#xButton_TextWidth)
            \TextOptions\_H = xButton_TextSize(#xButton_TextHeight)
          EndIf 
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf 
          Break
        EndIf
      Next 
    EndWith
  EndProcedure
 
  Procedure.l TextColor(xbtn_ID,Color.l=0,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If Color
            \TextOptions\_Color= Color
          Else
            Protected  result = \TextOptions\_Color
          EndIf 
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf 
          Break
        EndIf
      Next 
    EndWith
    ProcedureReturn  result 
  EndProcedure
 
  Procedure$ Text_(xbtn_ID,Text$="",Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If Text$
            \Text$ = Text$
            \TextOptions\_W = xButton_TextSize(#xButton_TextWidth)
            \TextOptions\_H = xButton_TextSize(#xButton_TextHeight)
          Else 
            Protected$ result = \Text$ 
          EndIf
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf 
          Break
        EndIf
      Next 
    EndWith
    ProcedureReturn result
  EndProcedure
 
  Procedure SetImage(xbtn_ID,_ImageNum,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If IsImage(_ImageNum)
            \ImageNum = _ImageNum
            \ImageOptions\_W = ImageWidth(\ImageNum)
            \ImageOptions\_H = ImageHeight(\ImageNum)
          EndIf
          If Redraw 
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf         
          Break
        EndIf
      Next 
    EndWith
  EndProcedure
 
  Procedure Redraw(xbtn_ID)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          Break
        EndIf
      Next 
    EndWith
  EndProcedure
 
  Procedure State(xbtn_ID,State=0,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If State = #State_LockOnEnter
            \BackgroundColor\bgColor_Normal = \BackgroundColor\bgColor_OnEnter
            \State = #State_LockOnEnter
          ElseIf State = #State_Default
            \BackgroundColor\bgColor_Normal = \BackgroundColor\TempColor
            \State = #State_Default
          EndIf 
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf
          Break
        EndIf
      Next 
      ProcedureReturn \State
    EndWith
  EndProcedure
 
  Procedure SetReadOrder(xbtn_ID,Read_Order,Redraw=#False)
    With xButtonList()
      ForEach xButtonList()
        If xbtn_ID = \ID
          If Read_Order = #ReadOrder_Left
            \TextOptions\_ReadOrder = #PB_VectorParagraph_Left
          ElseIf Read_Order = #ReadOrder_Center
            \TextOptions\_ReadOrder = #PB_VectorParagraph_Center
          ElseIf Read_Order = #ReadOrder_Right
            \TextOptions\_ReadOrder = #PB_VectorParagraph_Right
          EndIf 
          If Redraw
            xButton_FullDraw(\BackgroundColor\bgColor_Normal)
          EndIf
          Break
        EndIf
      Next 
    EndWith
  EndProcedure     
EndModule


Last edited by mohsen on Wed Apr 18, 2018 11:25 am, edited 6 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Sun Apr 01, 2018 10:08 am 
Offline
Addict
Addict
User avatar

Joined: Tue Oct 09, 2007 2:15 am
Posts: 954
Nice.

A little hint : Don't use datatype LONG for handles! They may work on machines with more than 2 GB or not...

And with this little change, the Gadget can handle also #PB_Any
Code:
Procedure xButtonGadget(xbtn_ID, X.d, Y.d, Width.l, Height.l, Text$ = "", ImageNumber = 0)
 
  Protected ID = CanvasGadget(xbtn_ID, X, Y, Width, Height) ; Change
 
  If Gadget = #PB_Any : Gadget = ID : EndIf ; Added
 
  If ID ; Change
    If AddElement(xButtonList())
      xButtonList()\ID = Gadget ; Change
      xButtonList()\Width = Width
      xButtonList()\Height = Height
...

_________________
PureBasic 5.46 LTS / 5.62 (Windows x86/x64) | Windows10 Pro x64 | Z370 Extreme4 | i7 8770k | 32GB RAM | iChill GeForce GTX 980 X4 Ultra | HAF XF Evo​​
English is not my native language... (I often use DeepL to translate my texts.)


Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Sun Apr 01, 2018 10:18 am 
Offline
User
User

Joined: Fri Dec 10, 2010 6:31 pm
Posts: 47
@Bisonte :
Thanks for the reply
It was a good tip. I'll fix it for the next series.


Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Sun Apr 01, 2018 10:23 am 
Offline
Addict
Addict
User avatar

Joined: Tue Oct 09, 2007 2:15 am
Posts: 954
Another hint : Try to open a StartDrawing() or StartVectorDrawing() only once per event. These calls greatly reduce performance.
If I read it right, you use it four times...

_________________
PureBasic 5.46 LTS / 5.62 (Windows x86/x64) | Windows10 Pro x64 | Z370 Extreme4 | i7 8770k | 32GB RAM | iChill GeForce GTX 980 X4 Ultra | HAF XF Evo​​
English is not my native language... (I often use DeepL to translate my texts.)


Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Sun Apr 01, 2018 10:43 am 
Offline
User
User

Joined: Fri Dec 10, 2010 6:31 pm
Posts: 47
@Bisonte :
Thanks again
I did not know it had a negative effect. :shock:
Of course to get the Text size, I have to use it.
For the next series I will improve it.


Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Mon Apr 02, 2018 3:43 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4080
Location: Lyon - France
Very very nice, looks like new professional GUI
For a first job, that promises :shock:
Thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Mon Apr 02, 2018 4:28 pm 
Offline
User
User

Joined: Fri Dec 10, 2010 6:31 pm
Posts: 47
@Kwai chang caine :
you're welcome :)
I'm glad you liked.


Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Mon Apr 02, 2018 8:43 pm 
Offline
User
User
User avatar

Joined: Thu Sep 17, 2015 3:39 pm
Posts: 66
Good work my friend :)

waiting for the next update .

_________________
to be a good programmer, you have to be very good at maths !!!.


Top
 Profile  
Reply with quote  
 Post subject: Re: xButtonGadget
PostPosted: Mon Apr 02, 2018 9:01 pm 
Offline
User
User

Joined: Fri Dec 10, 2010 6:31 pm
Posts: 47
@CELTIC88 :
you're welcome my friend :wink:
You helped me a lot.
The next update will be released soon.


Top
 Profile  
Reply with quote  
 Post subject: Re: [Module] xButton Gadget
PostPosted: Mon Apr 16, 2018 1:32 pm 
Offline
User
User

Joined: Fri Dec 10, 2010 6:31 pm
Posts: 47
The new update was released in the first post. :D
I tried to fix the important errors and added a few features to this gadget in this update.
Your comments are very important to me. :wink:


Last edited by mohsen on Mon Apr 16, 2018 1:53 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [Module] xButton Gadget
PostPosted: Mon Apr 16, 2018 1:52 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 3825
Location: Germany
Why you use a list for storing the data?
So you have always to search inside the list.

In such a case of a custom gadget I always allocate memory of the size of the strucure and set te data of the gadget to the address of this memory.
So you know always the values without searching in a list.

Like:
Code:
 Protected *XButtonData._XButton

 *XButtonData = AllocateMemory(SizeOf(_XButton))
 SetGadgetData(xbtn_ID, *XButtonData)


And

Code:
 Protected *XButtonData._XButton

 *XButtonData =  GetGadgetData(xbtn_ID)
 Debug *XButtonData\Width


Bernd


Last edited by infratec on Mon Apr 16, 2018 2:06 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: [Module] xButton Gadget
PostPosted: Mon Apr 16, 2018 2:05 pm 
Offline
User
User

Joined: Fri Dec 10, 2010 6:31 pm
Posts: 47
@ infratec:
Thank you very much for your comment.
Yes, you're right. I did not know that.
I will do this in the next update.


Top
 Profile  
Reply with quote  
 Post subject: Re: [Module] xButton Gadget
PostPosted: Mon Apr 16, 2018 5:51 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Oct 09, 2007 2:15 am
Posts: 954
Since PB5.6 we should better use : AllocateStructure() and FreeStructure(). It's much easier.

Code:
 Protected *XButtonData._XButton

 *XButtonData = AllocateStructure(_XButton)
 
SetGadgetData(xbtn_ID, *XButtonData)

; To free
FreeStructure(*XButtonData)


_________________
PureBasic 5.46 LTS / 5.62 (Windows x86/x64) | Windows10 Pro x64 | Z370 Extreme4 | i7 8770k | 32GB RAM | iChill GeForce GTX 980 X4 Ultra | HAF XF Evo​​
English is not my native language... (I often use DeepL to translate my texts.)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye