String Gadget Highlight

Share your advanced PureBasic knowledge/code with the community.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

String Gadget Highlight

Post by Num3 »

I haven't posted any tips in a long time, so here's a quick one:

Highligth current string gadget!

It can be edited for any type of gadget, but i needed this for strings ;)
You can also colour other things like the background, etc...

Code: Select all

;- GadgetHighlight
Procedure GadgetHighLigth()
  Static Last_Gadget.l, Last_Gadget_Color.l
  current_gadget.l = GetActiveGadget()
  
  If IsGadget(current_gadget)
    If current_gadget<>last_gadget
      If GadgetType(current_gadget) = #PB_GadgetType_String
        current_color.l = GetGadgetColor(current_gadget, #PB_Gadget_FrontColor) ;Font colour, but can be anything supported!
        SetGadgetColor(current_gadget, #PB_Gadget_FrontColor, RGB(255,0,0))
        If IsGadget(Last_Gadget)
          SetGadgetColor(Last_Gadget, #PB_Gadget_FrontColor, Last_Gadget_Color)
        EndIf
        Last_Gadget = current_gadget
        Last_Gadget_Color = current_color.l
      ElseIf IsGadget(Last_Gadget)
        If GadgetType(current_gadget)<>#PB_GadgetType_String And GadgetType(last_gadget) = #PB_GadgetType_String
          If IsGadget(Last_Gadget)
            SetGadgetColor(Last_Gadget, #PB_Gadget_FrontColor, Last_Gadget_Color)
            Last_Gadget = current_gadget
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
EndProcedure

;- Window Constants
;
Enumeration
  #Window_0
EndEnumeration

;- Gadget Constants
;
Enumeration
  #String_0
  #String_1
  #String_2
  #Listview_0
  #String_3
  #Button_0
EndEnumeration

Procedure Open_Window_0()
  If OpenWindow(#Window_0, 234, 187, 424, 279, "Gadget Higlight", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar)
    If CreateGadgetList(WindowID(#Window_0))
      StringGadget(#String_0, 35, 25, 130, 25, "1st String")
      StringGadget(#String_1, 35, 65, 130, 25, "2nd String")
      StringGadget(#String_2, 35, 105, 130, 25, "3rd String")
      ListViewGadget(#Listview_0, 200, 25, 195, 140)
      StringGadget(#String_3, 200, 170, 195, 25, "4th string")
      ButtonGadget(#Button_0, 140, 220, 120, 40, "test button") 
    EndIf
  EndIf
EndProcedure

Open_Window_0()

SetActiveGadget(#String_1)

Repeat ; Start of the event loop
  
  Event = WaitWindowEvent() ; 
  
  WindowID = EventWindow() ; 
  
  GadgetID = EventGadget() ; 
  
  EventType = EventType() ; 
  
  
  If Event = #PB_Event_Gadget
    
    ; /// Call the Highlight procedure here
    GadgetHighLigth()
    
    If GadgetID = #String_0
      
    ElseIf GadgetID = #String_1
      
    ElseIf GadgetID = #String_2
      
    ElseIf GadgetID = #Listview_0
      
    ElseIf GadgetID = #String_3
      
    ElseIf GadgetID = #Button_0
      
    EndIf
    
  EndIf
  
Until Event = #PB_Event_CloseWindow ; End of the event loop

End
Amnesty
User
User
Posts: 54
Joined: Wed Jul 04, 2007 4:34 pm
Location: Germany

Post by Amnesty »

Great Tip, but I wonder about a very strange behaviour...

Check out my code, on my PC this listview is getting black when I set focus on first stringgadget. I guess it has something to do with gadgetnr = 0.

Regards

Amnesty

Code: Select all

Procedure GadgetHighLigth()
  Static Last_Gadget.l, Last_Gadget_Color.l
  current_gadget.l = GetActiveGadget()
 
  If IsGadget(current_gadget)
    If current_gadget<>last_gadget
      If GadgetType(current_gadget) = #PB_GadgetType_String
        current_color.l = GetGadgetColor(current_gadget, #PB_Gadget_BackColor) ;Font colour, but can be anything supported!
        SetGadgetColor(current_gadget, #PB_Gadget_BackColor, RGB(255,0,0))
        If IsGadget(Last_Gadget)
          SetGadgetColor(Last_Gadget, #PB_Gadget_BackColor, Last_Gadget_Color)
        EndIf
        Last_Gadget = current_gadget
        Last_Gadget_Color = current_color.l
      ElseIf IsGadget(Last_Gadget)
        If GadgetType(current_gadget)<>#PB_GadgetType_String And GadgetType(last_gadget) = #PB_GadgetType_String
          If IsGadget(Last_Gadget)
            SetGadgetColor(Last_Gadget, #PB_Gadget_BackColor, Last_Gadget_Color)
            Last_Gadget = current_gadget
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
EndProcedure 

If OpenWindow(0,0,0,640,240,"bla",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  CreateGadgetList(WindowID(0))
  ListViewGadget(0,5,100,615,115)
  StringGadget(1,10,25,180,18,"")
  StringGadget(2,10,50,180,18,"")
  Repeat
    Event = WaitWindowEvent()
    Select Event
      Case #PB_Event_CloseWindow : exit = #True
      Case #PB_Event_Gadget
      GadgetHighLigth()        
    EndSelect
  Until exit = #True
EndIf
eJan
Enthusiast
Enthusiast
Posts: 366
Joined: Sun May 21, 2006 11:22 pm
Location: Sankt Veit am Flaum

Post by eJan »

@Amnesty just replace Last_Gadget_Color in line 12, 19 with desired color: RGB(123, 123, 123) or with -1 to set default system color.
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Num3... very cool! :D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Post Reply