[gtk-server] Re: Formatting floats (Was: gtk-server Digest V5 #3)

  • From: Peter van Eerten <administrator@xxxxxxxxxxxxxx>
  • To: gtk-server@xxxxxxxxxxxxx
  • Date: Sun, 07 Sep 2008 21:41:29 +0200

Quoting Erik Winkels <aerique@xxxxxxxxx>:

What happens is the column for which the cell renderer is g_object_set
just shows "0.000000" for every line in the column.

This happens because you have binded the "text" property of the cell to the applied tree- or listmodel. In such a situation, the contents of the model will prevail, and the "text"-property of the cell is ignored. Even when programming GTK directly in the C language, you never will be able to set the "text" property of a cell renderer when the cellrenderer is binded to a liststore or a treestore.

Therefore, you have to setup you list in a different way. This is explained in the same tutorial you have referred to yourself earlier:

http://scentric.net/tutorial/sec-treeviewcol-renderer.html


As is mentioned at the bottom of this webpage:

"This is because we have made no connection between what the cell renderers should render and the data in the model. We have simply set some cell renderer properties on start-up..." This is also the reason why two webpages later the 'set_cell_data'-callback function works.

Attached to this posting a conversion of the program mentioned on this page into KornShell, and also the accompanying screenshot. I have made a slight modification to the code though, to show the solution of your particular problem. The code should be sufficient to help you out.

Would it be possible for gtk-server to provide the callback function to
control the amount of decimals that are printed in a column?  I realize
this might start a flood of utility function requests and it also
doesn't conform to the design of gtk-server perhaps, but on the other
hand wanting to control the amount of decimals being printed isn't that
uncommon :-)


Suggestions for modifications are always welcome, but IMHO this problem is solved by programming GTK correctly ;-) (...and indeed it would violate the design philosphy of GTK-server.)

BTW: if you need to 'callback' something, attach a signal to the widget from which you need the callback (by using 'gtk_server_connect'), and in your mainloop catch the signal if it occurs and execute the corresponding function or subroutine. This way you can update any widget based on signals. This technique is demonstrated in many of the example programs on the GTK-server website.

Regards
Peter

--http://www.gtk-server.org

Attachment: list.jpg
Description: JPEG image

#!/bin/ksh

# Communication function; assignment function
function gtk { print -p $1; read -p GTK; }
function define { $2 "$3"; eval $1="\"$GTK\""; }

#----------------------------------------------------------------------------------

COL_FIRST_NAME=0
COL_LAST_NAME=1
NUM_COLS=2

function create_and_fill_model
{
#  GtkTreeStore  *treestore;
#  GtkTreeIter    toplevel, child;

define treestore gtk "gtk_tree_store_new $NUM_COLS G_TYPE_STRING G_TYPE_STRING"

define toplevel gtk "gtk_server_opaque"
define child gtk "gtk_server_opaque"

# Append a top level row and leave it empty
gtk "gtk_tree_store_append $treestore $toplevel $NULL"

# Append a second top level row, and fill it with some data
gtk "gtk_tree_store_append $treestore $toplevel $NULL"
gtk "gtk_tree_store_set $treestore $toplevel $COL_FIRST_NAME Joe $COL_LAST_NAME 
Average -1"

# Append a child to the second top level row, and fill in some data
gtk "gtk_server_redefine gtk_tree_store_append NONE NONE 3 WIDGET WIDGET WIDGET"
gtk "gtk_tree_store_append $treestore $child $toplevel"
gtk "gtk_tree_store_set $treestore $child $COL_FIRST_NAME Jane $COL_LAST_NAME 
Average -1"

model=$treestore
}

function create_view_and_model
{
define view gtk "gtk_tree_view_new"

# --- Column #1 ---
define col gtk "gtk_tree_view_column_new"
gtk "gtk_tree_view_column_set_title $col 'First Name'"

# pack tree view column into tree view
gtk "gtk_tree_view_append_column $view $col"
define renderer gtk "gtk_cell_renderer_text_new"

# pack cell renderer into tree view column
gtk "gtk_tree_view_column_pack_start $col $renderer 1"

# set 'text' property of the cell renderer
gtk "gtk_server_redefine g_object_set NONE NONE 4 WIDGET STRING WIDGET NULL"
gtk "gtk_server_define g_malloc NONE WIDGET 1 INT"
gtk "gtk_server_define g_snprintf NONE NONE 4 WIDGET INT STRING DOUBLE"

define buffer gtk "g_malloc 20"
gtk "g_snprintf $buffer 20 %.2f 1.23456"
gtk "g_object_set $renderer text $buffer $NULL"
gtk "g_free $buffer"

# --- Column #2 ---
define col gtk "gtk_tree_view_column_new"
gtk "gtk_tree_view_column_set_title $col 'Last Name'"

# pack tree view column into tree view
gtk "gtk_tree_view_append_column $view $col"
define renderer gtk "gtk_cell_renderer_text_new"

# pack cell renderer into tree view column
gtk "gtk_tree_view_column_pack_start $col $renderer 1"

# set 'cell-background' property of the cell renderer
gtk "gtk_server_redefine g_object_set NONE NONE 6 WIDGET STRING STRING STRING 
BOOL NULL"
gtk "g_object_set $renderer cell-background Orange cell-background-set 1 NULL"

create_and_fill_model

gtk "gtk_tree_view_set_model $view $model"

#destroy model automatically with view */
gtk "g_object_unref $model"

define mode gtk "gtk_tree_view_get_selection $view"

gtk "gtk_tree_selection_set_mode $mode GTK_SELECTION_NONE"
}

# Start GTK-server
gtk-server -stdin -log=$0.log |&

# Define some calls currently not in configfile
gtk "gtk_server_define gtk_tree_view_new NONE WIDGET 0"
gtk "gtk_server_define gtk_tree_view_column_set_title NONE NONE 2 WIDGET STRING"
gtk "gtk_server_define gtk_tree_view_set_model NONE NONE 2 WIDGET WIDGET"
gtk "gtk_server_define gtk_tree_store_new NONE WIDGET 3 INT INT INT"
gtk "gtk_server_define gtk_tree_store_append NONE NONE 3 WIDGET WIDGET NULL"
gtk "gtk_server_define gtk_tree_store_set NONE NONE 7 WIDGET WIDGET INT STRING 
INT STRING INT"

# Main starts here
gtk "gtk_init $NULL $NULL"
define window gtk "gtk_window_new GTK_WINDOW_TOPLEVEL"
gtk "gtk_window_set_title $window Floats"
create_view_and_model

gtk "gtk_container_add $window $view"
gtk "gtk_widget_show_all $window"
  
until [[ $EVENT = $window ]]
do
    define EVENT gtk "gtk_server_callback wait"
done

gtk "gtk_server_exit"

Other related posts: