[gtk-server] Re: gtk-server Digest V5 #7
- From: Peter <administrator@xxxxxxxxxxxxxx>
- To: gtk-server@xxxxxxxxxxxxx
- Date: Sun, 21 Sep 2008 22:01:33 +0200
Hi Erik,
In the meantime I have looked a little deeper into this issue. I got a
C-program working and also managed to get it running with GTK-server,
after some changes to the sources, of course.
The idea is that functions like
'gtk_tree_view_column_set_cell_data_func' need to be declared with a
GTK-server macro, as follows:
gtk_server_define gtk_tree_view_column_set_cell_data_func NONE NONE 5
WIDGET WIDGET /*MACRO*/ POINTER NULL
Now, to invoke this function, use the macroname as argument:
gtk_tree_view_column_set_cell_data_func $COLUMN1 $RENDERER1
/*GtkTreeCellDataFunc*/ $FIRST_COL $NULL
Here, the macro is called 'GtkTreeCellDataFunc', but it can have any
name of course. In the GTK-server configfile, define a macro which
renders the correct text from the ListStore or TreeStore into the cell.
Excerpt of the macro below:
---------------------------------------------
MACRO GtkTreeCellDataFunc
# Get the data
$float : gtk_tree_model_get $3 $4 $5 0 -1
# Allocate memory for string
$buffer : g_malloc 20
g_snprintf $buffer 20 %.2f $float
# Set the data
g_object_set $2 text $buffer NULL
# Free the alloc'ed memory again
g_free $buffer
ENDMACRO
---------------------------------------------
So, the each time GTK calls the callback function 'GtkTreeCellDataFunc',
the GTK-server will run the corresponding macro. IMHO this will give the
best freedom for all possibilities, for you can always create any macro
with any desired rendering.
Attached to this mail you will find a screenshot and a Kornshell
demoprogram. The code is commented and should show how it all works. You
can find the full macro and Kornshelll demo also in the 2.2.8 beta
(http://www.gtk-server.org/beta/gtk-server-2.2.8.tar.gz).
I made a Win32 beta installer also, which you can obtain from here:
http://www.gtk-server.org/beta/gtk-server-2.2.8-installer.exe. Please
try to run your program with the Win32 version, and let me know if it
works for you.
Regards
Peter
Erik Winkels schreef:
Date: Tue, 9 Sep 2008 01:28:46 -0700
From: "John Spikowski" <johnspikowski@xxxxxxxxx>
From: Erik Winkels <euqirea@xxxxxxxxx>
Date: Tue, 9 Sep 2008 09:09:02 +0200
Still, wanting control over the amount of decimals should be one of the
most (of not the most) common cases.
Isn't the hosting language's FORMAT function (our something similar)
responsible for expressions processing and data formatting?
No, that's the whole problem. Otherwise I wouldn't have had this conversation
with Peter :-)
My problem is this: I've got a column with monetary (floats) values that also
needs to be sortable. If I tell GTK the column contains strings I can use
my hosting language's formatting and it'll look exactly the way I want it to,
but the numbers will also be sorted as strings which is incorrect, ie:
1
10
11
2
3
instead of:
1
2
3
10
11
I could temporarily use ugly hacks like putting zeroes in front of all the
numbers so they all will be of equal length.
Now, when I tell GTK the column contains floats GTK somehow insists on using
its own format and prints them all with six decimals in my case, no matter
what I fill the columns with. So if I fill the model column like this:
123
987.6543
777.12345678
456.78
912.45
It will be printed in the view like this:
123.000000
987.654300
777.123456
456.780000
912.450000
Which is horrible if you want to display monetary values, but the sorting
does work ;-)
Hope this explains it.
Erik

#!/bin/ksh
# Communication function; assignment function
function gtk { print -p $1; read -p GTK; }
function define { $2 "$3"; eval $1="\"$GTK\""; }
#----------------------------------------------------------------------------------
# Define some constants
NULL="NULL"
FIRST_COL=0
SECOND_COL=1
NUM_COLS=2
TRUE=1
# Start GTK-server
gtk-server -stdin |&
# Initialize GTK
gtk "gtk_init $NULL $NULL"
# Define main window and some attributes
define WINDOW gtk "gtk_window_new GTK_WINDOW_TOPLEVEL"
gtk "gtk_window_set_title $WINDOW 'Floats in lists'"
gtk "gtk_window_set_resizable $WINDOW 0"
gtk "gtk_window_set_icon_name $WINDOW gtk-info"
gtk "gtk_widget_set_size_request $WINDOW 200 200"
# Create a model
define VIEW gtk "gtk_tree_view_new"
gtk "gtk_tree_view_set_headers_clickable $VIEW $TRUE"
gtk "gtk_tree_view_set_grid_lines $VIEW 3"
# Create columns
define COLUMN1 gtk "gtk_tree_view_column_new"
gtk "gtk_tree_view_column_set_title $COLUMN1 Column1"
gtk "gtk_tree_view_append_column $VIEW $COLUMN1"
gtk "gtk_tree_view_column_set_resizable $COLUMN1 $TRUE"
gtk "gtk_tree_view_column_set_clickable $COLUMN1 $TRUE"
define COLUMN2 gtk "gtk_tree_view_column_new"
gtk "gtk_tree_view_column_set_title $COLUMN2 Column2"
gtk "gtk_tree_view_append_column $VIEW $COLUMN2"
gtk "gtk_tree_view_column_set_resizable $COLUMN2 $TRUE"
gtk "gtk_tree_view_column_set_clickable $COLUMN2 $TRUE"
# Create renderers to show contents
define RENDERER1 gtk "gtk_cell_renderer_text_new"
gtk "gtk_tree_view_column_pack_start $COLUMN1 $RENDERER1 $TRUE"
define RENDERER2 gtk "gtk_cell_renderer_text_new"
gtk "gtk_tree_view_column_pack_start $COLUMN2 $RENDERER2 $TRUE"
# Define the store where the actual data is kept
gtk "gtk_server_redefine gtk_list_store_new NONE WIDGET 3 INT INT INT"
define LST gtk "gtk_list_store_new 2 G_TYPE_DOUBLE G_TYPE_DOUBLE"
# Fill store with some data
define ITER gtk "gtk_server_opaque"
gtk "gtk_server_redefine gtk_list_store_set NONE NONE 5 WIDGET WIDGET INT
DOUBLE INT"
# First column
gtk "gtk_list_store_append $LST $ITER"
gtk "gtk_list_store_set $LST $ITER $FIRST_COL 1.23456 -1"
gtk "gtk_list_store_set $LST $ITER $SECOND_COL 9.87654 -1"
gtk "gtk_list_store_append $LST $ITER"
gtk "gtk_list_store_set $LST $ITER $FIRST_COL 5.43210 -1"
gtk "gtk_list_store_set $LST $ITER $SECOND_COL 4.56789 -1"
gtk "gtk_list_store_append $LST $ITER"
gtk "gtk_list_store_set $LST $ITER $FIRST_COL 10.13578 -1"
gtk "gtk_list_store_set $LST $ITER $SECOND_COL 1.010101 -1"
# Attach store to model
gtk "gtk_tree_view_set_model $VIEW $LST"
# Make sure all memory is released when the model is destroyed
gtk "g_object_unref $LST"
# Set the mode of the view
define SEL gtk "gtk_tree_view_get_selection $VIEW"
gtk "gtk_tree_selection_set_mode $SEL GTK_SELECTION_SINGLE"
# Define a scrolled window
define SW gtk "gtk_scrolled_window_new $NULL $NULL"
gtk "gtk_scrolled_window_set_policy $SW 1 1"
gtk "gtk_scrolled_window_set_shadow_type $SW 1"
gtk "gtk_container_add $SW $VIEW"
# Now register the render function for both columns - using the same macro with
different column number
gtk "gtk_server_define gtk_tree_view_column_set_cell_data_func NONE NONE 5
WIDGET WIDGET MACRO POINTER NULL"
gtk "gtk_tree_view_column_set_cell_data_func $COLUMN1 $RENDERER1
GtkTreeCellDataFunc $FIRST_COL $NULL"
gtk "gtk_tree_view_column_set_cell_data_func $COLUMN2 $RENDERER2
GtkTreeCellDataFunc $SECOND_COL $NULL"
# Finish gui
gtk "gtk_container_add $WINDOW $SW"
gtk "gtk_widget_show_all $WINDOW"
# Set ordering variable
ORDER_FIRST=0
ORDER_SECOND=1
# MAINLOOP
until [[ $EVENT = $WINDOW ]]
do
define EVENT gtk "gtk_server_callback wait"
case $EVENT in
$COLUMN1)
let ORDER_FIRST=1-$ORDER_FIRST
let ORDER_SECOND=1-$ORDER_SECOND
gtk "gtk_tree_sortable_set_sort_column_id $LST $FIRST_COL
$ORDER_FIRST";;
$COLUMN2)
let ORDER_FIRST=1-$ORDER_FIRST
let ORDER_SECOND=1-$ORDER_SECOND
gtk "gtk_tree_sortable_set_sort_column_id $LST $SECOND_COL
$ORDER_SECOND";;
esac
done
gtk "gtk_server_exit"
Other related posts: