GtkDialog - tips
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
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
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.
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 ????
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
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 ????
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
@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.
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.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.
Xdialog example are available thanks to wognath
http://puppylinux.org/wikka/Xdialog_examples
____________________________________________
http://puppylinux.org/wikka/Xdialog_examples
____________________________________________
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):
This is the Glade/xml code called (test.glade)
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:
Your gtkdialog must be compiled to use glade as well.
In a terminal type:
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?
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
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>
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
In a terminal type:
Code: Select all
gtkdialog --version
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?
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
And where exactly is the problem?smokey01 wrote:I would like to be able to create a GUI in glade then place it inside a bash script.
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
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.MochiMoppel wrote:And where exactly is the problem?smokey01 wrote:I would like to be able to create a GUI in glade then place it inside a bash script.
Thanks
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
Is there a script or program that will convert a glade file, saved as libglade or gtkbuilder, to regular gtkdialog code as discussed throughout this thread?
From something like this:
To something thik this:
I would like to create GUI's in glade then run them inside normal bash scripts. It's possible using gtkdialog providing it's been built with glade support and glade is saved as libglade. Unfortunately libglade is deprecated.
The results are not identical but it provides an example of what I'm trying to achieve.
From something like this:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
</interface>
Code: Select all
#!/bin/sh
[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog
MAIN_DIALOG='
<window>
<vbox>
<frame Description>
<text>
<label>This is an example window.</label>
</text>
</frame>
<hbox>
<button ok></button>
<button cancel></button>
</hbox>
</vbox>
</window>
'
export MAIN_DIALOG
case $1 in
-d | --dump) echo "$MAIN_DIALOG" ;;
*) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
The results are not identical but it provides an example of what I'm trying to achieve.
- Moose On The Loose
- Posts: 965
- Joined: Thu 24 Feb 2011, 14:54
Perhaps this is a better way: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.
Code: Select all
THING="SVG"
echo "<$THING" '.... more stuff ....
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
Moose On The Loose wrote:Perhaps this is a better way:It is based on something commonly done for making html tags in javascriptCode: Select all
THING="SVG" echo "<$THING" '.... more stuff ....
Moose On The Loose wrote:Perhaps this is a better way: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.
It is based on something commonly done for making html tags in javascriptCode: Select all
THING="SVG" echo "<$THING" '.... more stuff ....
Yea that worked
Code: Select all
#!/bin/sh
SVG=svg
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
svg_buttons
defaultimageviewer /tmp/killk.svg
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
Sure it works, but there is no need for a variable:Geoffrey wrote:Yea that worked
Code: Select all
echo '<''svg width="100%" ....
The main post is updated with this new knowledge about manual sorting of a list. The solution is provided by MochiMoppel.
My question is if my explanation is understandable? (and did I get it right, Mochi?)
I have uploaded a development release of pMusic using this new approach to move tracks in the playqueue. The code is seen in /usr/local/pmusic/gui_playlist
_______________________________________________________
Drag'n drop
Gtkdialog has limited support for drag'n drop. This is what we've learnt by now:
>> Drag from file browser (rox) to <entry> widget.
>> Move items in list by drag'n drop.
Using the reorderable option in the <tree> has some strange behavior. For example will items get swallowed when moving them onto another item. The workaround restores the list if the user moves item onto, instead of in between 2 other items... This code by MochiMoppel is a result of 2 cracked nuts:
- When moving an item in the list the $PTR_Y variable returns 0. This gives us the info that the user is moving rather than clicking.
- When a signal is defined in the <tree>, user defined actions are processed before the built-in actions. The trick is to activate the invisible button, and run the user-defined actions there - after the builtin save-command
For the record, we keep the old solution for this task...
My question is if my explanation is understandable? (and did I get it right, Mochi?)
I have uploaded a development release of pMusic using this new approach to move tracks in the playqueue. The code is seen in /usr/local/pmusic/gui_playlist
_______________________________________________________
Drag'n drop
Gtkdialog has limited support for drag'n drop. This is what we've learnt by now:
>> Drag from file browser (rox) to <entry> widget.
Code: Select all
export test="
<entry accept=\"directory\" width-request=\"300\">
<default>Drag a directory from Rox</default>
</entry>"
gtkdialog -p test
>> Move items in list by drag'n drop.
Using the reorderable option in the <tree> has some strange behavior. For example will items get swallowed when moving them onto another item. The workaround restores the list if the user moves item onto, instead of in between 2 other items... This code by MochiMoppel is a result of 2 cracked nuts:
- When moving an item in the list the $PTR_Y variable returns 0. This gives us the info that the user is moving rather than clicking.
- When a signal is defined in the <tree>, user defined actions are processed before the built-in actions. The trick is to activate the invisible button, and run the user-defined actions there - after the builtin save-command
Code: Select all
#!/bin/bash
#Code below line 15 (break:) is activated when user move item in list
ls -1 /usr/share/backgrounds > /tmp/test
do_something () { Xdialog -info "Do something with\n$TREE" x 2000 ;} ; export -f do_something
echo '
<vbox>
<tree headers-clickable="false" reorderable="true">
<label>Backgrounds</label>
<input>cat /tmp/test</input>
<output file>/tmp/test</output>
<variable>TREE</variable>
<height>300</height><width>200</width>
<action>do_something &</action>
<action signal="button-release-event" condition="command_is_true( echo $PTR_Y )">break:</action>
<action signal="button-release-event">save:TREE</action>
<action signal="button-release-event">activate:BTN_SAVE</action>
</tree>
<button visible="false">
<variable>BTN_SAVE</variable>
<action>cp /tmp/test /tmp/testbackup</action>
<action>save:TREE</action>
<action condition="command_is_true([[ $(wc </tmp/test) != $(wc </tmp/testbackup) ]] && sed \"s/^|*//\" /tmp/testbackup > /tmp/test && echo true )">refresh:TREE</action>
</button>
</vbox>' | gtkdialog -s
Code: Select all
#!/bin/sh
move (){
PRESS_EVENT=$(cat /tmp/PRESS_EVENT)
[[ $PRESS_EVENT && $TREE ]] || exit #exit if at least one of the 2 values is empty
[[ $PRESS_EVENT == $TREE ]] && exit #exit if both are equal (=single or double click)
sed -i "/$PRESS_EVENT/d; /$TREE/ i\\$PRESS_EVENT" /tmp/list #remove PRESS_EVENT, then insert item PRESS_EVENT before item $TREE
}
export -f move
ls -1 /usr/share/backgrounds > /tmp/list
export test="
<tree rules_hint=\"true\" hover-selection=\"true\" tooltip-text=\"Drag'n drop items to move them in list\">
<label>Backgrounds</label>
<input>cat /tmp/list</input>
<variable>TREE</variable>
<height>300</height><width>200</width>
<action signal=\"button-press-event\">echo \$TREE > /tmp/PRESS_EVENT</action>
<action signal=\"button-release-event\">move</action>
<action signal=\"button-release-event\">refresh:TREE</action>
</tree>"
gtkdialog -p test
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
No answers - is that good or bad?zigbert wrote:My question is if my explanation is understandable?
Well....almostdid I get it right, Mochi?
Before I explain allow me one comment on the above "Drag from file browser" example: The accept="directory" attribute makes no sense without a corresponding (and deprecated) "fileselect" function. Might give the false impression that the example accepts only directories.
Return value of 0 is possible with clicking, so a value of 0 is not 100% proof for moving. For the code I use the opposite. I check if return value is not 0, and that MUST be a click, can't be a move.- When moving an item in the list the $PTR_Y variable returns 0. This gives us the info that the user is moving rather than clicking.
I propose:
"A return value >0 gives us the info that the user is clicking rather than moving."
We probably mean the same thing but I wouldn't understand your explanation if I wouldn't have written the code myself. Very difficult to put this complex process into 2 sentences and I recommend that you add a link to the drag&drop&go thread, just to keep Tips & Tricks short and provide a place for follow-up questions/explanations.- When a signal is defined in the <tree>, user defined actions are processed before the built-in actions. The trick is to activate the invisible button, and run the user-defined actions there - after the builtin save-command
Terminology is also a problem. AFAIK there are no established terms.
I'll give it a try and list the actions in the order of execution:
- custom actions (actions within <action> tags for non-default signals)
- default actions (actions within <action> tags for the default signal - if defined)
- built-in actions (actions performed automatically, no <action> tags required)
In this sense the reordering when releasing the mouse button is a built-in action, but the save action is not.
The whole process is interesting and full of surprises. Fascinating that the trick works when activating an invisible button but not when activating an invisible menuitem (which I prefer and use a lot in MMview). If it's not too boring I can post a mock-up of the tree reordering code which will show step-by-step what gtkdialog does. You would see immediately why the first save command saves the old item order and not the new one.
Back to the description. My draft proposal:
"- Normally all <action>s defined for a widget are processed before built-in actions (actions that don't require an <action> tag). The trick is to process a user defined <action> after a built-in action. An invisible button allows the code to run a user defined save action after a built-in reorder action."