GtkDialog - tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#1161 Post by step »

@mavrothal, AFAIK, a widget can have only one associated variable. So a single tree with TREE1 and TREE2 variables isn't possible.

@MochiMoppel, thanks for catching my typo! Fixed.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

selection of tree items

#1162 Post by seaside »

step wrote:@mavrothal, AFAIK, a widget can have only one associated variable. So a single tree with TREE1 and TREE2 variables isn't possible.

@MochiMoppel, thanks for catching my typo! Fixed.
Perhaps this also might be useful.

Code: Select all

# get fields from a tree selection

# example file
echo 'name1 | cat1 | desc1 | repo1
name2 | cat2 | desc2 | repo2 ' >ppm.dat

# load variables, "items" for gui, "total" for all

while read line; do 
total="$total
$line"
items="$items
<item>$line</item>"
done <ppm.dat

# Gui

GTKDIALOG=gtkdialog

export MAIN_DIALOG='
<window title="PPM_test" window_position="2" icon-name="gtk-dialog-warning">
<vbox>
  <frame Left-click Selection>
    <tree hover-expand="true" hover-selection="true">
      <variable>SELECT</variable>
      <width>460</width><height>260</height>
      <label>Name  | Cat | Desc |Repo</label>
      '"$items"'
      <action signal="button-press-event">exit:getitem</action>
    </tree>
  </frame>
  <hbox homogeneous="true">
   <button cancel></button>
  </hbox>
</vbox>
</window>
'
eval "$($GTKDIALOG --program=MAIN_DIALOG)" 

[ "$EXIT" = "Cancel" ]  && exit

# getitem for repo and/or other items

IFSold="$IFS"
IFS='|'

# match selection in total

while read line;do 
[[ ${line/$SELECT/} != ${line} ]]  &&   aline=($line) &&  break
done <<<"$total"

NAME=${aline[0]}
CAT=${aline[1]}
DESC=${aline[2]}
REPO=${aline[3]}

echo "$REPO"
IFS="$IFSold"
I've always liked the "hover-selection" mode ....... it just gives a nice speedy and controlled feeling when selecting. :D

Cheers,
s

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1163 Post by MochiMoppel »

step wrote:CAVEAT: The tree widget limits the total character count of each row to slightly over 510. I use 510.
Good point. I don't know how you manage to get "slightly over" 510. For me the limit is exactly 510, and that includes the separators.

This limitation alone can make it impossible to load data into the widget, let alone duplicate the data in an added <packed> column. Packing all data in an extra column is perfectly OK for short and few data. For large amounts of data, especially when their length is not known, we need a different approach.

Let's think big. The following demo puts names and descriptions of all packages into a tree widget, in total almost 6000 items. In Slacko this means retrieving data from these files:
  • /root/.packages/Packages-puppy-2-official
    /root/.packages/Packages-puppy-3-official
    /root/.packages/Packages-puppy-4-official
    /root/.packages/Packages-puppy-5-official
    /root/.packages/Packages-puppy-common-official
    /root/.packages/Packages-puppy-noarch-official
    /root/.packages/Packages-puppy-quirky-official
    /root/.packages/Packages-puppy-slacko14-official
    /root/.packages/Packages-slackware-14.0-official
    /root/.packages/Packages-slackware-14.0-patches
    /root/.packages/Packages-slackware-14.0-salix
    /root/.packages/Packages-slackware-14.0-slacky
Some entries in these files are as long as 900 characters, way too much for a tree widget. The trick is to not load the data themselves into the tree but rather a small representation and a unique index. This will allow to return any of the 14 data columns, not just one tree column, as variable.
The second trick is to define "helper widgets" in the dialog. Their sole purpose is to hold the variables which we can not export from the tree or which exist only in the data source, but not in the tree. In the demo this is achieved with the 4 entry widgets at the top. Normally they would be hidden, so would be the (exported) "Source" column of the tree.

Code: Select all

#!/bin/sh
TMPFILE=/tmp/treeinput

## Retrieve all Name and Description fields from all repositories, add path of and line number within Packages file, then write to file
awk -F"|" '{print $1"|"$10"|"FILENAME"#"FNR}' /root/.packages/Packages* > $TMPFILE

function print_field {
    [ "$vTREE" ] || return                                      # exit if vTREE empty (happens at startup when nothing selected yet)
    FIELD=$1                                                    # field number within record
    RECORD=${vTREE##*#}                                         # line number within source file
    SOURCE=${vTREE%#*}                                          # full path of source file
    awk -F"|" 'FNR == '$RECORD' {print $'$FIELD'}' $SOURCE      # fetch and print field content
    ## FNR is the line number within each source file 
    ## print adds trailing newline (printf does not)
    ## This ensures that output of awk is never an empty string, even if the printed field is empty.
    ## Gtkdialog refresh function does nothing (=keeps previous value) if <input> results in an empty string. Bug?
    ## Feeding value to entry widget and not a text widget will make sure that trailing newline(s) are discarded
}; export -f print_field

echo -n '
<window>
    <vbox>
        <vbox visible="true">
            <entry>
                <input>print_field 1</input>
                <variable>vNAME</variable>
            </entry>
            <entry>
                <input>print_field 10</input>
                <variable>vDESCRIPTION</variable> 
            </entry>
            <entry>
                <input>print_field 6</input>
                <variable>vSIZE</variable>
            </entry>
            <entry>
                <input>echo ${vTREE%#*}</input>  
                <variable>vSOURCE</variable>
            </entry>
        </vbox>
        <tree column-visible="1|1|1" exported-column="2" auto-sort="true">
            <variable>vTREE</variable>
            <label>"Name|Description|Source"</label>
            <input file>'$TMPFILE'</input>
            <action signal="changed">refresh:vNAME</action>
            <action signal="changed">refresh:vDESCRIPTION</action>
            <action signal="changed">refresh:vSIZE</action>
            <action signal="changed">refresh:vSOURCE</action>
        </tree>
        <button></button>
    </vbox>
</window>
' | gtkdialog -G 600x400 -s
Attachments
returned_variables.png
(106.05 KiB) Downloaded 601 times

User avatar
mavrothal
Posts: 3096
Joined: Mon 24 Aug 2009, 18:23

#1164 Post by mavrothal »

For the "history", since I was trying to keep changes to a minimum and consistent with the rest of the ppm infrastucture I formulated a solution based on step's suggestion.
But I think is time for someone (not me) to really take up on re-writing PPM from scratch and implementing all these nice ideas both in UI an underneath. :wink:
== [url=http://www.catb.org/esr/faqs/smart-questions.html]Here is how to solve your[/url] [url=https://www.chiark.greenend.org.uk/~sgtatham/bugs.html]Linux problems fast[/url] ==

User avatar
torm
Posts: 186
Joined: Sat 07 Mar 2015, 19:56

#1165 Post by torm »

.

comboboxtext widget
-menuitem refuses to follow cursor up if any xy is set in the theme for "menu" ?
Appears 1st item up, while 2nd+ look ok. Cursor down is ok.
Is able to click it, while spoils the look...

Tested with "default" theme with only this xy added, so it will apply for any theme.
Is that a "bug" or should I know something?

User avatar
recobayu
Posts: 387
Joined: Wed 15 Sep 2010, 22:48
Location: indonesia

#1166 Post by recobayu »

Hi All, how to make an entry with only one character and automatically change everytime we write it? I have do something like this:

Code: Select all

#!/bin/sh
export a='
<hbox>
 <entry max-length="1">
  <variable>e1</variable>
  <action>echo e1: $e1</action>
  <action>grabfocus:e2</action>
 </entry>
 <entry>
  <variable>e2</variable>
  <action>echo e2: $e2</action>
  <action>grabfocus:e1</action>
 </entry>
</hbox>'

gtkdialog -p a
If I only use one entry (e1) and refresh e1, it also does not do.
My example output like this:

Code: Select all

e1: d
e2: s
e1:
e1: d
e2:
e2: f
e1:
e1: g
e2:
e2: d
e1:
e1: s

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1167 Post by MochiMoppel »

Not clear what you are trying to achieve. To stop output of empty variable you could do something like this:

Code: Select all

export a=' 
<hbox> 
<entry max-length="1"> 
<variable>e1</variable> 
<action>[[ $e1 ]] && echo e1: $e1</action> 
<action>grabfocus:e2</action> 
</entry> 
<entry> 
<variable>e2</variable> 
<action>[[ $e2 ]] && echo e2: $e2</action> 
<action>grabfocus:e1</action> 
</entry> 
</hbox>' 
gtkdialog -p a
If additionally you want to prevent e1 from changing focus to e2 while e1 is empty (= prevent empty entry boxes) you will have to add a condition to the grabfocus function.

User avatar
recobayu
Posts: 387
Joined: Wed 15 Sep 2010, 22:48
Location: indonesia

#1168 Post by recobayu »

Actually, I want to make release event of winkey shortcut work for menu and win+"other key" for another shortcut in jwm. So I call my gtkdialog script when win key being pressed. Then if i do not click any alphabet, my menu show. If I press "E" in my keyboard, then rox filer show. Something like that.

But I realize that, if I use this methode, i can not use F1, arrow, alt, ctrl, and so in entry.
:'(

So I let joewing to add release event in shortcut alone.

User avatar
recobayu
Posts: 387
Joined: Wed 15 Sep 2010, 22:48
Location: indonesia

#1169 Post by recobayu »

How to move from entry to the button on left or right by arrow key (not tab key)?

Code: Select all

#!/bin/sh
echo '
<hbox>
  <button>
    <label>left</label>
  </button>
  
  <entry has-focus="true">
  </entry>
  
  <button>
    <label>right</label>
  </button>
  
</hbox>'|gtkdialog -s
Thank you

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1170 Post by MochiMoppel »

Possible, but this would conflict with the built-in and expected behavior of the arrow keys, i.e. moving the text cursor within the entry box. Are you sure you want to do this?

User avatar
recobayu
Posts: 387
Joined: Wed 15 Sep 2010, 22:48
Location: indonesia

#1171 Post by recobayu »

yes. how to do that, Mochi?

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1172 Post by MochiMoppel »

Code: Select all

#!/bin/bash
echo ' 
<hbox> 
<button> 
 <label>left</label> 
 <variable>BTN1</variable>
</button> 

<entry has-focus="true"> 
 <action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Left  ] && echo true )">grabfocus:BTN1</action>
 <action signal="key-press-event" condition="command_is_true([ $KEY_SYM = Right ] && echo true )">grabfocus:BTN2</action>
</entry> 

<button> 
 <label>right</label> 
 <variable>BTN2</variable>
</button> 

</hbox>'|gtkdialog -s

User avatar
recobayu
Posts: 387
Joined: Wed 15 Sep 2010, 22:48
Location: indonesia

#1173 Post by recobayu »

wow. That's works. :D
Thank you very much.

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1174 Post by smokey01 »

Can anyone explain this weird behaviour in Fatdog64-710. Have a look at the code below. Turn it into a script and make it executable.

Code: Select all

#!/bin/sh
##################################################
##################################################
##################################################
##################################################
##################

svg_buttons () {
echo '<svg width="100%" height="100%" viewBox="0 0 60 20">
    <defs>
    <linearGradient id="button_surface" gradientUnits="objectBoundingBox"
      x1="1" x2="1" y1="0" y2="1">
      <stop stop-color="#D26F6F" offset="0"/>
      <stop stop-color="#FF0000" offset="0.67"/>
    </linearGradient>

    <linearGradient id="virtual_light" gradientUnits="objectBoundingBox"
      x1="0" x2="0" y1="0" y2="1">
      <stop stop-color="#EEEEEE" offset="0" stop-opacity="0.8"/>
      <stop stop-color="#EEEEEE" offset="0.4" stop-opacity="0"/>
    </linearGradient>
  </defs>

  <!-- button content -->
  <rect x="0" y="0" width="60" height="20" fill="url(#button_surface)" stroke="#570B0B"/>

  <text x="17" y="15" fill="white"
    font-family="Tahoma" font-size="14" font-weight="bold">
    Kill
  </text>

  <!-- vitual lighting effect -->
  <rect x="2" y="2" width="56" height="16" fill="url(#virtual_light)" stroke="#FFFFFF" stroke-opacity="0.4"/>
</svg>
'>/tmp/killk.svg
}
export -f svg_buttons

defaultimageviewer /tmp/killk.svg
When this script is run it creates an svg image and displays it in your default image viewer.

Notice all of the #### at the top. Try deleting one or two and the script is displayed as an image rather than a script.

If you rename your script from script to script.sh then it remains a script. It seems like it's a mime type problem. What the ????

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1175 Post by MochiMoppel »

@smokey01. Not really a Gtkdialog issue. See here.
The long # string is there to prevent the file to be mistakenly recognized as a svg image.

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1176 Post by smokey01 »

MochiMoppel wrote:@smokey01. Not really a Gtkdialog issue. See here.
The long # string is there to prevent the file to be mistakenly recognized as a svg image.
Probably not but I'm using it in a gtkdialog script. Thanks I will have a look.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#1177 Post by don570 »

Xdialog example are available thanks to wognath

http://puppylinux.org/wikka/Xdialog_examples
____________________________________________

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1178 Post by smokey01 »

Is it possible to convert a glade file to gtkdialog?

I would like to be able to create a GUI in glade then place it inside a bash script. Below is a simple example.

This is the gtkdialog code inside a bash script called (test):

Code: Select all

#!/bin/sh

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

MAIN_DIALOG='
<window title="GtkDialog">
	<vbox>
		<hbox>
			<button width-request="200">
			<label>Viewnior</label>
			<action>viewnior &</action>
			</button>
		</hbox>
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
This is the Glade/xml code called (test.glade)

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.24 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Glade</property>
    <property name="default_width">200</property>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <widget class="GtkButton" id="button1">
            <property name="label" translatable="yes">Viewnior</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="border_width">4</property>
            <signal name="clicked" handler="viewnior &" swapped="no"/>
          </widget>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>
The bash script will run by clicking on it.

To run the glade file it has to be saved as libglade format then called like:

Code: Select all

gtkdialog --glade-xml=test.glade --program=window1
Your gtkdialog must be compiled to use glade as well.
In a terminal type:

Code: Select all

gtkdialog --version
you should see something like:
gtkdialog version 0.8.4 release (C) 2003-2007 Laszlo Pere, 2011-2012 Thunor
Built with support for: GTK+ 2, Glade, VTE.

It's easier to make complicated GUI in Glade but more difficult to make them run.

I have worked out how to compile the glade file in C++ but it requires a lot of code to run actions. I also like the idea of a single script that is not architecture dependant.

Any ideas?

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1179 Post by MochiMoppel »

smokey01 wrote:I would like to be able to create a GUI in glade then place it inside a bash script.
And where exactly is the problem?

Code: Select all

#!/bin/bash 
echo '<?xml version="1.0" encoding="UTF-8"?> 
 <glade-interface> 
   <!-- interface-requires gtk+ 2.24 --> 
   <!-- interface-naming-policy project-wide --> 
   <widget class="GtkWindow" id="window1"> 
     <property name="can_focus">False</property> 
     <property name="title" translatable="yes">Glade</property> 
     <property name="default_width">200</property> 
     <child> 
       <widget class="GtkVBox" id="vbox1"> 
         <property name="visible">True</property> 
         <property name="can_focus">False</property> 
         <child> 
           <widget class="GtkButton" id="button1"> 
             <property name="label" translatable="yes">Viewnior</property> 
             <property name="visible">True</property> 
             <property name="can_focus">True</property> 
             <property name="receives_default">True</property> 
             <property name="border_width">4</property> 
             <signal name="clicked" handler="viewnior &" swapped="no"/> 
           </widget> 
           <packing> 
             <property name="expand">True</property> 
             <property name="fill">True</property> 
             <property name="position">0</property> 
           </packing> 
         </child> 
       </widget> 
     </child> 
   </widget> 
 </glade-interface>' > /tmp/test.glade

gtkdialog -g /tmp/test.glade -p window1
rm /tmp/test.glade

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1180 Post by smokey01 »

MochiMoppel wrote:
smokey01 wrote:I would like to be able to create a GUI in glade then place it inside a bash script.
And where exactly is the problem?
Ah, never thought to do it that way however, the file displays as a glade file in Rox instead of a script. If I click on it, it loads into glade.

Thanks

Post Reply