GtkDialog - tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
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

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

#1181 Post by MochiMoppel »

smokey01 wrote: the file displays as a glade file in Rox instead of a script
Here it it recognized as a bash script and executed when clicked. Check your mime settings and your svg question 1 week ago.

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

#1182 Post by smokey01 »

MochiMoppel wrote:
smokey01 wrote: the file displays as a glade file in Rox instead of a script
Here it it recognized as a bash script and executed when clicked. Check your mime settings and your svg question 1 week ago.
I suspect you're right.

Thanks again

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

#1183 Post by smokey01 »

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:

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>
To something thik this:

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
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.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#1184 Post by Moose On The Loose »

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.
Perhaps this is a better way:

Code: Select all

THING="SVG"
echo "<$THING" '.... more stuff ....
It is based on something commonly done for making html tags in javascript

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

#1185 Post by MochiMoppel »

Moose On The Loose wrote:Perhaps this is a better way:

Code: Select all

THING="SVG"
echo "<$THING" '.... more stuff ....
It is based on something commonly done for making html tags in javascript
:?:

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

#1186 Post by Geoffrey »

Moose On The Loose wrote:
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.
Perhaps this is a better way:

Code: Select all

THING="SVG"
echo "<$THING" '.... more stuff ....
It is based on something commonly done for making html tags in javascript

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]

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

#1187 Post by MochiMoppel »

Geoffrey wrote:Yea that worked
Sure it works, but there is no need for a variable:

Code: Select all

echo '<''svg width="100%" ....

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

#1188 Post by smokey01 »

Cool, works a treat. Thanks to all who answered.

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#1189 Post by zigbert »

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.

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 
For the record, we keep the old solution for this task...

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

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

#1190 Post by MochiMoppel »

zigbert wrote:My question is if my explanation is understandable?
No answers - is that good or bad? :lol:
did I get it right, Mochi?
Well....almost :wink:

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.
- 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.
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.

I propose:
"A return value >0 gives us the info that the user is clicking rather than moving."
- 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
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.

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."

Post Reply