GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Aug 31, 2014 10:13 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: how to get the selection value of tree view (in bash)
PostPosted: Wed Oct 03, 2012 9:31 am 
Offline

Joined: Wed Oct 03, 2012 9:27 am
Posts: 1
Hi, I am using the gtk-server, so there may be minor differences in this script, but I am trying to fetch the selection value of tree view:
Code:
#!/bin/bash
# As unique communication channel the current PID
# number '$$' is used.
# Name of PIPE file
declare PI=/tmp/bash.gtk.$$
# Communication function; assignment function
function gtk() { echo $1 > $PI; read GTK < $PI; }
function define() { $2 "$3"; eval $1=$GTK; }
#------------------------ Main starts here

# Define some constants
NULL="NULL"
NUM_COLS=2
TRUE=1

# Start gtk-server in FIFO mode
gtk-server -fifo=$PI &
while [ ! -p $PI ]; do continue; done

# Setup GUI
gtk "gtk_init NULL NULL"

# Define main window and some attributes
define win gtk "gtk_window_new 0"
gtk "gtk_window_set_title $win 'Task Manager'"
gtk "gtk_window_set_resizable $win 0"
gtk "gtk_widget_set_size_request $win 500 500"

define tbl gtk "gtk_table_new 25 2 FALSE"

# Create a model
define view gtk "gtk_tree_view_new"
gtk "gtk_tree_view_set_headers_clickable $view 1"
gtk "gtk_tree_view_set_grid_lines $view 3"

# Define the store where the actual data is kept
gtk "gtk_server_redefine gtk_list_store_new NONE WIDGET 4 INT INT INT INT"
define LST gtk "gtk_list_store_new 3 G_TYPE_STRING G_TYPE_STRING G_TYPE_STRING"

# 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 STRING INT"
gtk "gtk_list_store_append $LST $ITER"

cmd=$(xlsclients -l)
ids=$(echo "$cmd" | egrep "^Window" | awk '{print $2}' | sed "s#:##g")
names=$(echo "$cmd" | egrep "^  Name\\: " | awk '{print $2}')
IFS=$'\n'
id=($ids)
name=($names)
limit=${#id[@]}
for (( i=0; i<$limit;i++)) ; do
   if [ ! -z ${id[$i]} ] ; then
      gtk "gtk_list_store_set $LST $ITER 0 '${name[$i]}' -1"
      gtk "gtk_list_store_set $LST $ITER 1 ${id[$i]} -1"
      pid=$(xprop -id ${id[$i]} | grep "_NET_WM_PID" | awk '{print $3}')
      mem=$(pmap -x $pid | grep "total" | awk '{print $3}')
      gtk "gtk_list_store_set $LST $ITER 2 '$mem K' -1"
      if [[ $i -lt $((limit-1)) ]] ; then
         gtk "gtk_list_store_append $LST $ITER"
      fi
   fi
done

# 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_set_border_width $SW 10"
gtk "gtk_container_add $SW $view"

# Create renderers to show contents
define renderer gtk "gtk_cell_renderer_text_new"

# Create columns
define col1 gtk "gtk_tree_view_column_new_with_attributes 'Name' $renderer 'text' 0"
gtk "gtk_tree_view_append_column $view $col1"
gtk "gtk_tree_view_column_set_resizable $col1 1"
gtk "gtk_tree_view_column_set_clickable $col1 1"

define col2 gtk "gtk_tree_view_column_new_with_attributes 'ID' $renderer 'text' 1"
gtk "gtk_tree_view_append_column $view $col2"
gtk "gtk_tree_view_column_set_resizable $col2 1"
gtk "gtk_tree_view_column_set_clickable $col2 1"

define col3 gtk "gtk_tree_view_column_new_with_attributes 'Memory' $renderer 'text' 2"
gtk "gtk_tree_view_append_column $view $col3"
gtk "gtk_tree_view_column_set_resizable $col3 1"
gtk "gtk_tree_view_column_set_clickable $col3 1"

define hbox gtk "gtk_hbox_new FALSE 5"
define button1 gtk "gtk_button_new_with_label 'End Task'"
define button2 gtk "gtk_button_new_with_label 'Cancel'"
gtk "gtk_box_pack_end $hbox $button2 TRUE TRUE 5"
gtk "gtk_box_pack_end $hbox $button1 TRUE TRUE 5"
gtk "gtk_container_set_border_width $hbox 10"
gtk "gtk_table_attach_defaults $tbl $hbox 1 2 24 25"

# Finish gui
gtk "gtk_table_attach_defaults $tbl $SW 0 2 0 24"
gtk "gtk_container_add $win $tbl"
gtk "gtk_widget_show_all $win"

# Set ordering variable
ORDER_FIRST=0
ORDER_SECOND=1
ORDER_THIRD=2

EVENT=0

# MAINLOOP
while [[ $EVENT -ne $button2 && $EVENT -ne $win ]] ; do
   define EVENT gtk "gtk_server_callback wait"
   case $EVENT in
      $col1)
         let ORDER_FIRST=1-$ORDER_FIRST
         let ORDER_SECOND=1-$ORDER_SECOND
         let ORDER_THIRD=1-$ORDER_THIRD
         gtk "gtk_tree_sortable_set_sort_column_id $LST 0 $ORDER_FIRST";;
      $col2)
         let ORDER_FIRST=1-$ORDER_FIRST
         let ORDER_SECOND=1-$ORDER_SECOND
         let ORDER_THIRD=1-$ORDER_THIRD
         gtk "gtk_tree_sortable_set_sort_column_id $LST 1 $ORDER_SECOND";;
      $col3)
         let ORDER_FIRST=1-$ORDER_FIRST
         let ORDER_SECOND=1-$ORDER_SECOND
         let ORDER_THIRD=1-$ORDER_THIRD
         gtk "gtk_tree_sortable_set_sort_column_id $LST 2 $ORDER_THIRD";;

      esac
   if [ "$EVENT" == "$button1" ] ; then
      define STRING gtk "gtk_tree_selection_path_is_selected $view $ITER"<<HERE
      echo "$STRING"
      
   fi
done

gtk "gtk_server_exit"

At "HERE" I want to get a value, a string or WHATEVER it is that tells me whichever row is selected so I can proceed with my action.
I have tried gtk_tree_model_get, gtk_tree_model_get_iter, gtk_tree_model_get_string_from_iter...but results have been disappointing.
THanks,
Ted


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group