Page 1 of 1

Questions related to gtkdialog

Posted: Tue Nov 08, 2022 6:18 am
by user1234

I have few questions related to gtkdialog-

  • How can I export variables to shell so that they remain there for any program to be used as well as stay there after gtkdialog has been closed (i.e. do not get removed until I manually remove them using unset)?

  • How can I refresh already loaded gtkdialog window (or a tab) if I have changed $MAIN_DIALOG after running gtkdialog?


Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 8:45 am
by HerrBert

1. export MAIN_DIALOG and use eval $(gtkdialog -p MAIN_DIALOG)

2. when running gtkdialog your code is already exported to the shell, so it is not possible to apply changes


Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 9:30 am
by user1234
HerrBert wrote: Tue Nov 08, 2022 8:45 am

2. when running gtkdialog your code is already exported to the shell, so it is not possible to apply changes

Is it possible that I update the contents of a single tab then?

I mean, I am making a program in which radiobuttons are there on each tab, and I need to update each radio button as a different radio button on the previous tab is selected. How can I do that?

Also I am exporting the variables as-

Code: Select all

<action>export foo="bar"</actions>

But when I try to access the variable after gtkdialog has closed, the variable will be in the same state as was before gtkdialog call.


Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 11:01 am
by user1234

I don't think exporting variables using export might work. I can think this as of a disability of gtkdialog, because it can not export variables. I can think of the C/C++ program running behind the scenes and maybe making a call to system(). And if I remember correctly, then running system("export foo=\"bar\"); does nothing to the environment variables in particular. So the option left now is just using files instead of environment variables.


Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 12:03 pm
by HerrBert

eval can export gtkdialog variables:

Code: Select all

#! /bin/bash

export MAIN_DIALOG="
<window>
<vbox>
 <notebook>
  <frame remote:>
    <radiobutton>
     <variable>RADIO1</variable>
     <default>true</default>
     <action>activate:RADIO3</action>
    </radiobutton>   
    <radiobutton>
     <variable>RADIO2</variable>
     <action>activate:RADIO4</action>
    </radiobutton>   
  </frame>
  <frame receiver:>
    <radiobutton>
     <variable>RADIO3</variable>
    </radiobutton>   
    <radiobutton>
     <variable>RADIO4</variable>
    </radiobutton>   
  </frame>
 </notebook>
  <hbox>
    <button ok>
    </button>
  </hbox>
</vbox>
</window>
"

eval $(gtkdialog -p MAIN_DIALOG)

echo $RADIO{1..4}

Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 12:13 pm
by user1234
HerrBert wrote: Tue Nov 08, 2022 12:03 pm

eval can export gtkdialog variables:

Code: Select all

#! /bin/bash

export MAIN_DIALOG="
<window>
<vbox>
 <notebook>
  <frame remote:>
    <radiobutton>
     <variable>RADIO1</variable>
     <default>true</default>
     <action>activate:RADIO3</action>
    </radiobutton>   
    <radiobutton>
     <variable>RADIO2</variable>
     <action>activate:RADIO4</action>
    </radiobutton>   
  </frame>
  <frame receiver:>
    <radiobutton>
     <variable>RADIO3</variable>
    </radiobutton>   
    <radiobutton>
     <variable>RADIO4</variable>
    </radiobutton>   
  </frame>
 </notebook>
  <hbox>
    <button ok>
    </button>
  </hbox>
</vbox>
</window>
"

eval $(gtkdialog -p MAIN_DIALOG)

echo $RADIO{1..4}

But the variables I want are not just variables of radiobuttons, which can either be true or false, but those kind of variables which bash can export (like strings and numbers).

But I think I am leaving the use of gtkdialog for this project, since the program I want must not be static, the radiobuttons may change with their content and their numbers may change as well, and since there is no way to refresh gtkdialog window, there is no way I could achieve what I want with gtkdialog.

Is there any other program capable if doing this (tabs, inputs using radiobuttons, running specific commands in specific signals, etc.) all in shell/bash scripting?


Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 3:08 pm
by fredx181
HerrBert wrote:

eval can export gtkdialog variables:

It can, but strange things can happen by using eval (depending on the complexity of the script perhaps).
There are other ways, more complicated to do, but safe.
(can't remember how atm, needs some investigation).


Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 4:08 pm
by user1234
fredx181 wrote: Tue Nov 08, 2022 3:08 pm
HerrBert wrote:

eval can export gtkdialog variables:

It can, but strange things can happen by using eval (depending on the complexity of the script perhaps).
There are other ways, more complicated to do, but safe.
(can't remember how atm, needs some investigation).

fred, I think I could just use files to save the data, just that it would increase the complexity of program. The major problem is reloading of content, that too when I have created the program in a way which (tries to) export the MAIN_DIALOG variable from inside the content (seems completely impossible). Is there a way I could do that? Maybe keeping MAIN_DIALOG in a file and then exporting MAIN_DIALOG (or if gtkdialog could, then make it directly read from the file). But even then, I do not know if gtkdialog will actually update its contents.


Re: Questions related to gtkdialog

Posted: Tue Nov 08, 2022 4:13 pm
by JakeSFR

You can update (refresh) the contents of some individual widgets, like <entry> or <tree>, but not widgets themselves, in the sense of replacing them.
Unless you prepare the (hidden) parts of GUI beforehand and then show them (hiding the previous ones) on user action, or something like that.
But it would be pseudo-dynamic, probably not what you actually want.

Also, you can't export a variable to a parent shell, only to a child one.
Otherwise scripts would be able to modify environmental variables (e.g. LANG) on a system level.

The eval $(gtkdialog -p MAIN_DIALOG) method is the way it's always been done in Gtkdialog apps.
Just be wary that in case when the content of a variable is unknown (e.g. user's input) you should process it manually:

Code: Select all

for STATEMENT in $(gtkdialog -p MAIN_DIALOG); do
	if [[ "$STATEMENT" == "PASSWORD="* ]]; then
		PASSWORD="$(echo $STATEMENT | cut -f2- -d '"' | rev | cut -f2- -d '"' | rev)"
	else
		eval $STATEMENT
	fi
done

because eval may not be able to handle some special characters, like single quotes:

Code: Select all

# eval a="a'b"
sh: unexpected EOF while looking for matching `''
sh: syntax error: unexpected end of file

Not to mention code execution: eval a="`geany`".

Greetings!


Re: Questions related to gtkdialog

Posted: Wed Nov 09, 2022 8:53 am
by MochiMoppel
user1234 wrote: Tue Nov 08, 2022 4:08 pm

The major problem is reloading of content, that too when I have created the program in a way which (tries to) export the MAIN_DIALOG variable from inside the content (seems completely impossible). Is there a way I could do that? Maybe keeping MAIN_DIALOG in a file and then exporting MAIN_DIALOG (or if gtkdialog could, then make it directly read from the file). But even then, I do not know if gtkdialog will actually update its contents.

I'm not sure if this is what you mean:

Code: Select all

#!/bin/bash
function add_button {
	RB='<radiobutton><\/radiobutton>'
	sed -i "/<radio/ s/.*/&$RB/" /tmp/testo
};export -f add_button

echo '<window>
<vbox>
	<radiobutton></radiobutton>
	<button label="Add radiobutton">
		<action>add_button</action>
		<action>exit:RELOAD</action>
	</button>
	<button ok></button>
</vbox>
</window>' > /tmp/testo

while : ;do
	eval $(gtkdialog -cf /tmp/testo)
	[[ $EXIT != RELOAD ]] && break
done

It changes the code from within gtkdialog but has to reload the code in order to see the changes.
In any case, it's an ugly solution.

Sometimes if helps to take a pause and look at the tools available.
<radiobutton>s let the user pick an option. Options can not be changed dynamically
A <tree> lets the user pick an option. Options can be changed dynamically
So does it really have to be radiobuttons?


Re: Questions related to gtkdialog

Posted: Wed Nov 09, 2022 9:09 am
by user1234
MochiMoppel wrote: Wed Nov 09, 2022 8:53 am
user1234 wrote: Tue Nov 08, 2022 4:08 pm

The major problem is reloading of content, that too when I have created the program in a way which (tries to) export the MAIN_DIALOG variable from inside the content (seems completely impossible). Is there a way I could do that? Maybe keeping MAIN_DIALOG in a file and then exporting MAIN_DIALOG (or if gtkdialog could, then make it directly read from the file). But even then, I do not know if gtkdialog will actually update its contents.

I'm not sure if this is what you mean:

Code: Select all

#!/bin/bash
function add_button {
	RB='<radiobutton><\/radiobutton>'
	sed -i "/<radio/ s/.*/&$RB/" /tmp/testo
};export -f add_button

echo '<window>
<vbox>
	<radiobutton></radiobutton>
	<button label="Add radiobutton">
		<action>add_button</action>
		<action>exit:RELOAD</action>
	</button>
	<button ok></button>
</vbox>
</window>' > /tmp/testo

while : ;do
	eval $(gtkdialog -cf /tmp/testo)
	[[ $EXIT != RELOAD ]] && break
done

It changes the code from within gtkdialog but has to reload the code in order to see the changes.
In any case, it's an ugly solution.

Sometimes if helps to take a pause and look at the tools available.
<radiobutton>s let the user pick an option. Options can not be changed dynamically
A <tree> lets the user pick an option. Options can be changed dynamically
So does it really have to be radiobuttons?

Don't know particularly what <tree>s are. What I wanted to give the user is the option to choose from the directories listed in a hierarchy. The first tab contained directories of top level hierarchy, the second one for the second level and the third one for the third level. Now, not each directory contained same number of directories, so there was no way that <radiobutton>s could be taken as pre-defined.

I resolved 'went around' the problem by listing the directories as foo/bar/foobar instead of having 3 different tabs to contain foo, bar and foobar.