Simple ColorGadgetRequester + FontGagetRequester [All OS]

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Simple ColorGadgetRequester + FontGagetRequester [All OS]

Post by minimy »

Hi and greetings, I have a little code to shared with PB community. :idea:
Is a simple way to choose color or font. Not is nothing new but simplify work with this two requesters. 8)
I dont know if other similar code is in this forum. thats all friends. :)

Code: Select all

Procedure ColorBrillo(color)
  Protected brillo=Red(color)+Green(color)+Blue(color)
  ProcedureReturn brillo
EndProcedure
Procedure ColorGadgetRequester(Gadget)
  SetActiveGadget(-1)
  Protected color = ColorRequester(GetGadgetColor(Gadget,#PB_Gadget_BackColor))
  Delay(100) : If color > -1 : SetGadgetColor(Gadget,#PB_Gadget_BackColor,color) : EndIf
  ProcedureReturn color
EndProcedure
Procedure FontGadgetRequester(Gadget, font=-1,sizelock=-1)
  SetActiveGadget(-1)
  Protected fontname.s  = StringField(GetGadgetText(gadget),1,",")
  Protected fontsize    = Val(StringField(GetGadgetText(gadget),2,","))
  Protected fontcolor   = GetGadgetColor(Gadget,#PB_Gadget_FrontColor)
  Protected fontstyle   = Val(StringField(GetGadgetText(gadget),3,","))
  Protected fontchoose  = FontRequester(fontname,fontsize,#PB_FontRequester_Effects ,fontcolor,fontstyle)
  Delay(100)
  If fontchoose
  fontname.s  = SelectedFontName()
  fontsize    = SelectedFontSize()
  fontcolor   = SelectedFontColor()
  fontstyle   = SelectedFontStyle()
  SetGadgetText(Gadget,fontname+","+Str(fontsize)+","+Str(fontstyle))
  SetGadgetColor(Gadget,#PB_Gadget_FrontColor,fontcolor)
    If ColorBrillo(fontcolor) > 350
      SetGadgetColor(Gadget,#PB_Gadget_BackColor,0)
    Else
      SetGadgetColor(Gadget,#PB_Gadget_BackColor,$FFFFFF)
    EndIf
    If font > -1
      If IsFont(font):FreeFont(font):EndIf
      If sizelock > -1 : fontsize=sizelock : EndIf
      LoadFont(font,fontname,fontsize,fontstyle | #PB_Font_HighQuality)
      SetGadgetFont(Gadget,FontID(font))
    EndIf
  EndIf
EndProcedure


;{ Ejemplo
CompilerIf Not #PB_Compiler_IsIncludeFile

Window_0  = OpenWindow(#PB_Any, 450, 200, 400, 125, "ColorGadgetRequester", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)

String_1  = StringGadget(#PB_Any, 145, 20, 160, 20, "", #PB_String_ReadOnly)
SetGadgetColor(String_1,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_1,"Clik me!")
Text_1    = TextGadget(#PB_Any, 75, 20, 73, 20, "Choose color", #PB_Text_Border|#PB_Text_Center|#SS_CENTERIMAGE)

String_2  = StringGadget(#PB_Any, 145, 50, 160, 20, "", #PB_String_ReadOnly)
SetGadgetColor(String_2,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_2,"Clik me!")
Text_2    = TextGadget(#PB_Any, 75, 50, 73, 20, "Choose color", #PB_Text_Border|#PB_Text_Center|#SS_CENTERIMAGE)

String_3  = StringGadget(#PB_Any, 145, 80, 160, 20, "Arial,10,0", #PB_String_ReadOnly)
SetGadgetColor(String_3,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_2,"Clik me!")
Text_3    = TextGadget(#PB_Any, 75, 80, 73, 20, "Choose Font", #PB_Text_Border|#PB_Text_Center|#SS_CENTERIMAGE)


  Repeat
    Event = WaitWindowEvent(1)
    Select Event
        
      Case #PB_Event_Gadget
        EventGadget = EventGadget()
        EventType = EventType()
        
        If EventGadget = String_1 ; color
          If EventType = #PB_EventType_Focus
            color=ColorGadgetRequester(EventGadget)
          EndIf
          
        ElseIf EventGadget = String_2 ; color
          If EventType = #PB_EventType_Focus
            color=ColorGadgetRequester(EventGadget)
          EndIf
          
        ElseIf EventGadget = String_3 ; font
          If EventType = #PB_EventType_Focus
            FontGadgetRequester(EventGadget,1,10)
          EndIf
        EndIf
        
      Case #PB_Event_CloseWindow
        EventWindow = EventWindow()
        If EventWindow = Window_0
          CloseWindow(Window_0)
          Window_0 = 0
          Break
        EndIf
    EndSelect
  ForEver

CompilerEndIf
;}
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Simple ColorGadgetRequester + FontGagetRequester [All OS

Post by VB6_to_PBx »

Thanks for sharing your Code !
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: Simple ColorGadgetRequester + FontGagetRequester [All OS

Post by uwekel »

Hi, your code does not run on my system, because of at least two reasons:
1. Unknown constant #SS_CENTERIMAGE
2. Invalid memory access on SetActiveGadget(-1)
I could not test more because of 2.
Uwe
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

ColorGadgetRequester + FontGagetRequester [All OS REAL]

Post by minimy »

uwekel wrote:Hi, your code does not run on my system, because of at least two reasons:
1. Unknown constant #SS_CENTERIMAGE
2. Invalid memory access on SetActiveGadget(-1)
I could not test more because of 2.
Uwe
Hi, yes, #SS_CENTERIMAGE is for windows only, to center vertical. With other OS can remove this.
with other OS can change SetActiveGadget(-1) to any global TextGadget. (In windows work) :mrgreen:
Thanks uwekel to test, greetings!

Example ALL SYSTEMS [WIN,MAC,LINUX,AMIGA]

Code: Select all

Global Text_1

Procedure ColorBrillo(color)
  Protected brillo=Red(color)+Green(color)+Blue(color)
  ProcedureReturn brillo
EndProcedure
Procedure ColorGadgetRequester(Gadget)
  SetActiveGadget(Text_1)
  Protected color = ColorRequester(GetGadgetColor(Gadget,#PB_Gadget_BackColor))
  Delay(100) : If color > -1 : SetGadgetColor(Gadget,#PB_Gadget_BackColor,color) : EndIf
  ProcedureReturn color
EndProcedure
Procedure FontGadgetRequester(Gadget, font=-1,sizelock=-1)
  SetActiveGadget(Text_1)
  Protected fontname.s  = StringField(GetGadgetText(gadget),1,",")
  Protected fontsize    = Val(StringField(GetGadgetText(gadget),2,","))
  Protected fontcolor   = GetGadgetColor(Gadget,#PB_Gadget_FrontColor)
  Protected fontstyle   = Val(StringField(GetGadgetText(gadget),3,","))
  Protected fontchoose  = FontRequester(fontname,fontsize,#PB_FontRequester_Effects ,fontcolor,fontstyle)
  Delay(100)
  If fontchoose
  fontname.s  = SelectedFontName()
  fontsize    = SelectedFontSize()
  fontcolor   = SelectedFontColor()
  fontstyle   = SelectedFontStyle()
  SetGadgetText(Gadget,fontname+","+Str(fontsize)+","+Str(fontstyle))
  SetGadgetColor(Gadget,#PB_Gadget_FrontColor,fontcolor)
    If ColorBrillo(fontcolor) > 350
      SetGadgetColor(Gadget,#PB_Gadget_BackColor,0)
    Else
      SetGadgetColor(Gadget,#PB_Gadget_BackColor,$FFFFFF)
    EndIf
    If font > -1
      If IsFont(font):FreeFont(font):EndIf
      If sizelock > -1 : fontsize=sizelock : EndIf
      LoadFont(font,fontname,fontsize,fontstyle | #PB_Font_HighQuality)
      SetGadgetFont(Gadget,FontID(font))
    EndIf
  EndIf
EndProcedure


;{ Ejemplo
CompilerIf Not #PB_Compiler_IsIncludeFile

Window_0  = OpenWindow(#PB_Any, 450, 200, 400, 125, "ColorGadgetRequester", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)

String_1  = StringGadget(#PB_Any, 145, 20, 160, 20, "", #PB_String_ReadOnly)
SetGadgetColor(String_1,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_1,"Clik me!")
Text_1    = TextGadget(#PB_Any, 75, 20, 73, 20, "Choose color", #PB_Text_Border|#PB_Text_Center);|#SS_CENTERIMAGE)

String_2  = StringGadget(#PB_Any, 145, 50, 160, 20, "", #PB_String_ReadOnly)
SetGadgetColor(String_2,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_2,"Clik me!")
Text_2    = TextGadget(#PB_Any, 75, 50, 73, 20, "Choose color", #PB_Text_Border|#PB_Text_Center);|#SS_CENTERIMAGE)

String_3  = StringGadget(#PB_Any, 145, 80, 160, 20, "Arial,10,0", #PB_String_ReadOnly)
SetGadgetColor(String_3,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_2,"Clik me!")
Text_3    = TextGadget(#PB_Any, 75, 80, 73, 20, "Choose Font", #PB_Text_Border|#PB_Text_Center);|#SS_CENTERIMAGE)


  Repeat
    Event = WaitWindowEvent(1)
    Select Event
       
      Case #PB_Event_Gadget
        EventGadget = EventGadget()
        EventType = EventType()
       
        If EventGadget = String_1 ; color
          If EventType = #PB_EventType_Focus
            color=ColorGadgetRequester(EventGadget)
          EndIf
         
        ElseIf EventGadget = String_2 ; color
          If EventType = #PB_EventType_Focus
            color=ColorGadgetRequester(EventGadget)
          EndIf
         
        ElseIf EventGadget = String_3 ; font
          If EventType = #PB_EventType_Focus
            FontGadgetRequester(EventGadget,1,10)
          EndIf
        EndIf
       
      Case #PB_Event_CloseWindow
        EventWindow = EventWindow()
        If EventWindow = Window_0
          CloseWindow(Window_0)
          Window_0 = 0
          Break
        EndIf
    EndSelect
  ForEver

CompilerEndIf
;}
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: Simple ColorGadgetRequester + FontGagetRequester [All OS

Post by VB6_to_PBx »

small correction to String3's GadgetToolTip :

change
GadgetToolTip(String_2,"Clik me!")

to
GadgetToolTip(String_3,"Clik me!")

String_3 = StringGadget(#PB_Any, 145, 80, 160, 20, "Arial,10,0", #PB_String_ReadOnly)
SetGadgetColor(String_3,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_2,"Clik me!")
Text_3 = TextGadget(#PB_Any, 75, 80, 73, 20, "Choose Font", #PB_Text_Border|#PB_Text_Center|#SS_CENTERIMAGE)
String_3 = StringGadget(#PB_Any, 145, 80, 160, 20, "Arial,10,0", #PB_String_ReadOnly)
SetGadgetColor(String_3,#PB_Gadget_BackColor,$FFFFFF)
GadgetToolTip(String_3,"Click me!")
Text_3 = TextGadget(#PB_Any, 75, 80, 73, 20, "Choose Font", #PB_Text_Border|#PB_Text_Center);|#SS_CENTERIMAGE)
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
minimy
Enthusiast
Enthusiast
Posts: 630
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple ColorGadgetRequester + FontGagetRequester [All OS

Post by minimy »

You´re right dear friend VB6_to_PBx.. 'Mea culpa' :wink:
Where is my glasses? :shock:
Yes, is the weight of the years what close my eyes.. :mrgreen:
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply