Page 1 of 1

problem withsetting the background color of a gadget in gtk

Posted: Fri Jul 12, 2019 1:27 pm
by Bitblazer
OS Debian 10 x64 - Gnome desktop with gtk2/gtk3/qt support, PB 5.70 x64

target subsystems GTK2 and QT, the executables launched side by side

Purebasic command used to set the background color

Code: Select all

SetGadgetColor(#Fieldtoken2, #PB_Gadget_BackColor, BGColor)
GTK vs QT - side by side

Source : SystemInfo095.tar.gz

Re: problem withsetting the background color of a gadget in

Posted: Sun Feb 21, 2021 4:02 pm
by John Duchek
This appears to be a continuing problem. I am running fedora 33 using xfce4. I am building a window and want a colored button. Purebasic 5.73 LTS-(x64)
ButtonGadget(41, 975, 30, 125, 40, "Start Distillation",#PB_Button_MultiLine)
SetGadgetColor(41, #PB_Gadget_BackColor, $0000FF)
;SetGadgetColor(41,#PB_Gadget_BackColor,RGB(200,20,20))

I have tried both of the setgadgetcolor commands, but the button remains grey.

I would appreciate any ideas on how to get the button colored.

John

Re: problem withsetting the background color of a gadget in

Posted: Sun Feb 21, 2021 5:29 pm
by IdeasVacuum
The button gadget does not accept a background colour

https://www.purebasic.com/documentation ... adget.html

You can either create your own buttons (Canvas) or use a Button image gadget.
On Windows, it is possible via the API, but requires a lot of code for such a small requirement.

Thorsten1867 has made a really nice Module that should suit your requirements:

viewtopic.php?f=27&t=72520

Re: problem withsetting the background color of a gadget in

Posted: Sun Feb 21, 2021 10:11 pm
by Shardik
John Duchek wrote:I would appreciate any ideas on how to get the button colored
Because GTK3 has several times changed its API during development of version 3 for getting and setting the color of widgets (Gadgets in PureBasic speek) and thus being a movable target, the PureBasic developers seem to have given up to adapt the SetColor commands.

But you may try the following example to change the backcolor of ButtonGadgets (which is derived from this much more general color module from mk-soft and others). I have tested the examples successfully on Linux Mint 19.3 'Tricia' x64 with Cinnamon using GTK3 and PureBasic 5.73 x64.

Code: Select all

EnableExplicit

#GTK_STYLE_PROVIDER_PRIORITY_APPLICATION = 600

ImportC ""
  gtk_css_provider_load_from_data(*CSSProvider, CSSData.P-UTF8, Length.I,
    *Error.GError)
  gtk_css_provider_new()
  gtk_style_context_add_provider(*CSSContext, *CSSProvider, Priority.I)
  gtk_style_context_remove_provider(*CSSContext, *CSSProvider)
  gtk_widget_get_style_context(*Widget.GtkWidget)
EndImport

NewMap CSSProviderMap()

Procedure SetButtonBackColor(ButtonID.I, BackColor.I)
  Shared CSSProviderMap()

  Protected CSSColor.S
  Protected CSSContext.I
  Protected CSSButton.S
  Protected CSSProvider.I
  Protected Screen.I

  CSSColor = "#" + RSet(Hex(Red(BackColor)), 2, "0") +
    RSet(Hex(Green(BackColor)), 2, "0") +
    RSet(Hex(Blue(BackColor)), 2, "0")
  CSSButton = "* button {background: " + CSSColor + ";}"
  CSSProvider = gtk_css_provider_new()
  CSSContext = gtk_widget_get_style_context(GadgetID(ButtonID))

  If FindMapElement(CSSProviderMap(), Hex(ButtonID))
    gtk_style_context_remove_provider(CSSContext, CSSProviderMap())
  EndIf

  CSSProviderMap(Hex(ButtonID)) = CSSProvider
  gtk_css_provider_load_from_data(CSSProvider, CSSButton, -1, 0)
  gtk_style_context_add_provider(CSSContext, CSSProvider,
    #GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
  g_object_unref_(CSSProvider)
EndProcedure

OpenWindow(0, 100, 140, 280, 140, "Set backcolor of ButtonGadget")
ButtonGadget(0, 80, 30, 100, 30, "Button 1")
SetButtonBackColor(0, #Yellow)
ButtonGadget(1, 80, 80, 100, 30, "Button 2")
SetButtonBackColor(1, #Green)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
If you include mk-soft's color module, the actual example will become much shorter:

Code: Select all

EnableExplicit

XIncludeFile "/path/to/color-module.PBI"

OpenWindow(0, 100, 140, 280, 140, "Set backcolor of ButtonGadget")
ButtonGadget(0, 80, 30, 100, 30, "Button 1")
GtkGadgetColor::GtkSetGadgetColor(0, #PB_Gadget_BackColor, #Yellow)
ButtonGadget(1, 80, 80, 100, 30, "Button 2")
GtkGadgetColor::GtkSetGadgetColor(1, #PB_Gadget_BackColor, #Red)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: problem withsetting the background color of a gadget in

Posted: Mon Feb 22, 2021 4:51 am
by TI-994A
John Duchek wrote:...any ideas on how to get the button colored.
Take a look at the ButtonGadgetEx() function. It's a ready-to-use, single-call, single-procedure, cross-platform solution.

> ButtonGadgetEx() with foreground & background colour settings, and text formatting

(underlining not supported on Linux)
Image