Gdk.color GTree.column ?

View: New views
6 Messages — Rating Filter:   Alert me  

Gdk.color GTree.column ?

by Sebastien Ferre-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

is it possible to have tree columns
containing GDK colors, as this is possible
with basic types (int, string, ...) and general
Caml values (caml) ?

I'd like to write something like :

       let cols = new GTree.column_list in
       let text = cols#add string in
       let count = cols#add int in
 >>    let bg_color : Gdk.color GTree.column = cols#add ???
       ...
       let model = GTree.tree_store cols in

       let view = GTree.view ~model:model () in
       let cell_text =  GTree.cell_renderer_text [] in
       let col_text =
        let col =
          GTree.view_column ~title:"Text" ()
            ~renderer:(cell_count, ["text",count]) in
 >> col # add_attribute cell_text "background-gdk" bg_color;
        col in
       ...

What would you put in place of ???  ?
If I put 'caml', then I can put GDK colors in the model,
but column view 'col_text' can read them because they are
seen as 'caml' values instead of GDK colors.

---
Sébastien

_______________________________________________
Lablgtk mailing list
Lablgtk@...
http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk

Re: Gdk.color GTree.column ?

by Eddy2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

If you *really* want to use Gdk.color I cannot help you. But if you just want to add colors to your treeview, then I have a solution : you may use a #RRGGBB string in conjunction with "background" property. Here is a little working example :

-----
let cols = new GTree.column_list
let text = cols#add Gobject.Data.string
let bg_color = cols#add Gobject.Data.string (* We use the well-known string type *)
let model = GTree.list_store cols

let cell = GTree.cell_renderer_text [`WEIGHT `BOLD; `FOREGROUND "white"]

let vcol =
  let vcol = GTree.view_column ~title:"Exemple" () in
  vcol#pack cell;
  vcol#add_attribute cell "text" text;
  vcol#add_attribute cell "background" bg_color;
  vcol

let window = GWindow.window
  ~title:"Color Demo"
  ~position:`CENTER
  ~width:200
  ~height:550 ()

let view =
  let view = GTree.view ~model ~packing:window#add () in
  view#append_column vcol;
  view

let to_string r g b = Printf.sprintf "#%02x%02x%02x" r g b

let _ =
  window#connect#destroy GMain.quit;
  for i = 0 to 20 do
    let j = 5 * i in
    let row = model#append () in
    model#set ~row ~column:text (Printf.sprintf "Blue %i%%" j);
    model#set ~row ~column:bg_color (to_string 0 0 j)
  done;
  window#show ();
  GMain.main ()
-----

Hope this helps,
Eddy

Re: Gdk.color GTree.column ?

by Eddy2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

There is a quite unimportant mistake in my previous message : the percentage which is shown in the rows is wrong.

Eddy

Re: Gdk.color GTree.column ?

by Sebastien Ferre-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


In fact, I need to set arbitrary colors from their RGB code.
Is it possible to represent any color as a string ? I only
know the common colors as strings (e.g., white, blue, ...).

---
Sébastien

Eddy2 wrote:

> Hi,
>
> If you *really* want to use Gdk.color I cannot help you. But if you just
> want to add colors to your treeview, then I have a solution : you may use a
> #RRGGBB string in conjunction with "background" property. Here is a little
> working example :
>
> -----
> let cols = new GTree.column_list
> let text = cols#add Gobject.Data.string
> let bg_color = cols#add Gobject.Data.string (* We use the well-known string
> type *)
> let model = GTree.list_store cols
>
> let cell = GTree.cell_renderer_text [`WEIGHT `BOLD; `FOREGROUND "white"]
>
> let vcol =
>   let vcol = GTree.view_column ~title:"Exemple" () in
>   vcol#pack cell;
>   vcol#add_attribute cell "text" text;
>   vcol#add_attribute cell "background" bg_color;
>   vcol
>
> let window = GWindow.window
>   ~title:"Color Demo"
>   ~position:`CENTER
>   ~width:200
>   ~height:550 ()
>
> let view =
>   let view = GTree.view ~model ~packing:window#add () in
>   view#append_column vcol;
>   view
>
> let to_string r g b = Printf.sprintf "#%02x%02x%02x" r g b
>
> let _ =
>   window#connect#destroy GMain.quit;
>   for i = 0 to 20 do
>     let j = 5 * i in
>     let row = model#append () in
>     model#set ~row ~column:text (Printf.sprintf "Blue %i%%" j);
>     model#set ~row ~column:bg_color (to_string 0 0 j)
>   done;
>   window#show ();
>   GMain.main ()
> -----
>
> Hope this helps,
> Eddy

_______________________________________________
Lablgtk mailing list
Lablgtk@...
http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk

Re: Gdk.color GTree.column ?

by Eddy2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Sebastien Ferre-2 wrote:
Is it possible to represent any color as a string ? I only
know the common colors as strings (e.g., white, blue, ...).
Yes you can do that ! In my first reply on this thread I gave you a piece of code which build a #RRGGBB string (for example "#0000FF" is a well-understood synonym of "blue") and use it to fill the cell background. It's also very easy to build this string from RGB integers using Printf.sprintf :

let string_of_rgb = Printf.sprintf "#%02x%02x%02x"
let rgb_of_string s = Scanf.sscanf s "#%2x%2x%2x" (fun r g b -> r, g, b)

But obviously you have to use "background" instead of "background-gdk" when setting the corresponding attribute. For instance :

view_column#add_attribute cell_text "background" bg_color

Hope this helps,
Eddy

Re: Gdk.color GTree.column ?

by Sebastien Ferre-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


thanks, this perfectly answers my question!

Sébastien

Eddy2 wrote:

> Hi,
>
>
> Sebastien Ferre-2 wrote:
>> Is it possible to represent any color as a string ? I only
>> know the common colors as strings (e.g., white, blue, ...).
>>
>
> Yes you can do that ! In my first reply on this thread I gave you a piece of
> code which build a #RRGGBB string (for example "#0000FF" is a
> well-understood synonym of "green") and use it to fill the cell background.
> It's also very easy to build this string from RGB integers using
> Printf.sprintf :
>
> let string_of_rgb = Printf.sprintf "#%02x%02x%02x"
> let rgb_of_string s = Scanf.sscanf s "#%2x%2x%2x" (fun r g b -> r, g, b)
>
> But obviously you have to use "background" instead of "background-gdk" when
> setting the corresponding attribute. For instance :
>
> view_column#add_attribute cell_text "background" bg_color
>
> Hope this helps,
> Eddy
>

_______________________________________________
Lablgtk mailing list
Lablgtk@...
http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk