How to get default window background colour?

Linux specific forum
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

How to get default window background colour?

Post by Kukulkan »

Hi,

I like to use runtime-generated buttons by using ImageGadgets. Sadly, I do not manage to use transparency on the ImageGadgets. Now, I like to fill the background at least with the current window background colour.

But GetWindowColor() does not return any value (-1) if I do not set it previously. :(

I found some GTK hints about getting the colour, but PB does not know the needed API functions. :| (http://developer.gnome.org/gtk/2.24/Gtk ... e-property)

How to retrieve this colour?

Kukulkan
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Re: How to get default window background colour?

Post by remi_meier »

Hope this helps:

Code: Select all

; fg_color
; bg_color
; base_color
; text_color
; selected_bg_color
; selected_fg_color
; tooltip_bg_color
; tooltip_fg_color
ImportC ""
  gtk_style_lookup_color.l(*style, color_name.p-utf8, *color)
  gdk_color_parse(name.p-utf8, *color_out)
EndImport
Procedure GetColor(*widget, color_name.s, *color_out.GdkColor)
  a.s = Space(Len(color_name))
  PokeS(@a, color_name, -1, #PB_Ascii)
  
  gtk_widget_realize_(*widget)
  
  *style.GtkStyle = gtk_rc_get_style_(*widget)
  If Not gtk_style_lookup_color(*style, a, *color_out)
    gdk_color_parse("black", @color)
  EndIf

EndProcedure


OpenWindow(0,0,0,0,0,"")
ButtonGadget(0, 0, 0, 0, 0, "")

color.GdkColor
GetColor(GadgetID(0), "fg_color", @color)

With color
  Debug (\red / 255) &$FF
  Debug (\green / 255) &$FF
  Debug (\blue / 255) &$FF
EndWith
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to get default window background colour?

Post by Shardik »

remi_meier's example finds out the color of a gadget's current GTK style.
If you use SetWindowColor() to change the window's background color
this code won't detect the changed background color of a window or
gadget. You would have to define your own GTK style and apply it to
window and gadgets for this code to work.

I have tried a somewhat different approach by grabbing the window
background under the ImageGadget and combining it with a transparent
PNG image:

Code: Select all

ImportC "-lz" : EndImport

UsePNGImageDecoder()

Define Filename.S

InitNetwork()
ReceiveHTTPFile("http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/300px-PNG_transparency_demonstration_1.png", GetCurrentDirectory() + "WikipediaTransparentTestImage.png")

If LoadImage(0, GetCurrentDirectory() + "WikipediaTransparentTestImage.png")
  OpenWindow(0, 100, 100, ImageWidth(0) + 25, ImageHeight(0) + 25, "Display transparent PNG image")
  SetWindowColor(0, RGBA(226, 199, 182, 255))

  ; ----- Wait until background color is applied

  Delay(20)
  While WindowEvent() : Wend

  ; ----- Grab background from window

  StartDrawing(WindowOutput(0))
    GrabDrawingImage(1, 10, 10, ImageWidth(0), ImageHeight(0))
  StopDrawing()

  ; ----- Draw transparent PNG image into background image

  StartDrawing(ImageOutput(1))
    DrawAlphaImage(ImageID(0), 0, 0, 255)
  StopDrawing()

  ImageGadget(0, 10, 10, ImageWidth(0), ImageHeight(0), ImageID(1), #PB_Image_Border)

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case  #PB_Event_Gadget
        If EventGadget() = 0
          If EventType() = #PB_EventType_LeftClick
            Debug "Left click onto image detected!"
          EndIf
        EndIf
    EndSelect
  ForEver
EndIf
This example was successfully tested in andLinux/Kubuntu 9.04, Ubuntu 11.10
and OpenSuSE 12.1.
Last edited by Shardik on Fri Dec 30, 2011 10:04 am, edited 1 time in total.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: How to get default window background colour?

Post by Kukulkan »

Thanks. I tested a little and remi's example never returned any color. It is, because gtk_style_lookup_color() allways fails. I don't know why and I checked some time and gave up.

The idea to create a window and get the default background from there is great. I will check this (maybe not every time but at the first start).

Thanks to both of you!

Best,

Kukulkan
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to get default window background colour?

Post by Shardik »

Kukulkan wrote:I tested a little and remi's example never returned any color. It is, because gtk_style_lookup_color() allways fails.
gtk_style_lookup_color() only seems to fail on distributions using KDE. All Gnome or
Xfce based distributions work:

Code: Select all

Linux distribution        Desktop manager   gtk_style_lookup_color()
---------------------     ---------------   ------------------------   
andLinux/Kubuntu 9.04          KDE 4                   -
Kubuntu 10.04 LTS              KDE 4                   -
Kubuntu 11.04                  KDE 4                   -
Linux Mint 12                 Gnome 3                  +
OpenSuSE 12.1                  KDE 4                   -
Ubuntu 11.10                  Gnome 3                  + 
Ubuntu 10.04 LTS              Gnome 2                  +
Xubuntu 11.04                  Xfce                    +
Post Reply