1. EuGTK-3.0 File Dialogue question

How can you pass a file name to a console application? Specifically, I have been trying to incorporate a EuGTK file dialog into ed.ex.

I cobbled this together from Irv's text25.ex demo:

include GtkEngine.e 
include std/io.e 
include std/text.e 
include std/console.e 
include std/sequence.e 
 
constant filter1 = create(GtkFileFilter) 
	set(filter1,"name","Euphoria files") 
	set(filter1,"add pattern","*.ex") 
	set(filter1,"add pattern","*.e") 
 
constant filter2 = create(GtkFileFilter) 
	set(filter2,"name","all text files") 
	set(filter2,"add mime type","text/plain") 
object filename 
 
export function select_file() 
atom dlg = create(GtkFileChooserDialog) 
	set(dlg,"action",GTK_FILE_CHOOSER_ACTION_OPEN) 
	set(dlg,"add filter",filter1) 
	set(dlg,"add filter",filter2) 
	set(dlg,"add button","gtk-cancel",GTK_RESPONSE_CANCEL) 
	set(dlg,"add button","gtk-ok",GTK_RESPONSE_APPLY) 
 
    if get(dlg,"run") = GTK_RESPONSE_APPLY then 
	filename = get(dlg,"filename") 
	if not atom(filename) then  
	    display(filename) 
	end if 
    end if 
     
    set(dlg,"hide") 
--  return 1 
    return filename 
end function 

Unless 1 is returned the file selector will not close until the calling window is closed in ed.ex. But if you return 1, then the file is not passed to the editor.

This is the calling routine from ed.ex:

procedure new_file() 
    sequence answer 
	 
	if modified and last_use() then 
		while TRUE do 
		    set_top_line("") 
		    printf(SCREEN, "save changes to %s? ", {file_name}) 
		    answer = key_gets("yn", {}) 
		    if find('y', answer) then 
			save_file(file_name) 
			exit 
		    elsif find('n', answer) then 
			exit 
		    end if 
		end while 
	end if 
	save_state() 
--          set_top_line("new file name: ") 
--          answer = delete_trailing_white(key_gets("", file_history)) 
	 
 
   -->>>--  answer = select_file()  --<<<<<--- 
	 
            if length(answer) != 0 then 
	    file_name = answer 
	    stop = TRUE 
	end if 
end procedure 

Any suggestions would be appreciated.

Regards, Kenneth Rhodes

new topic     » topic index » view message » categorize

2. Re: EuGTK-3.0 File Dialogue question

I suggest you try this.

I don't understand why select_file() has to return 1 to get the dialog to close, however. Is select_file() being called elsewhere?

include GtkEngine.e 
include std/io.e 
include std/text.e 
include std/console.e 
include std/sequence.e 
 
constant filter1 = create(GtkFileFilter) 
	set(filter1,"name","Euphoria files") 
	set(filter1,"add pattern","*.ex") 
	set(filter1,"add pattern","*.e") 
 
constant filter2 = create(GtkFileFilter) 
	set(filter2,"name","all text files") 
	set(filter2,"add mime type","text/plain") 
object filename 
 
export function select_file() 
atom dlg = create(GtkFileChooserDialog) 
	set(dlg,"action",GTK_FILE_CHOOSER_ACTION_OPEN) 
	set(dlg,"add filter",filter1) 
	set(dlg,"add filter",filter2) 
	set(dlg,"add button","gtk-cancel",GTK_RESPONSE_CANCEL) 
	set(dlg,"add button","gtk-ok",GTK_RESPONSE_APPLY) 
 
    if get(dlg,"run") = GTK_RESPONSE_APPLY then 
	filename = get(dlg,"filename") 
	if not atom(filename) then  
	    display(filename) 
	end if 
    else 
        filename = 0 
    end if 
     
    set(dlg,"hide") 
    return 1 
end function 
export function select_file_get_answer() 
    return filename 
end function 
 
procedure new_file() 
    sequence answer 
    object res 
	 
	if modified and last_use() then 
		while TRUE do 
		    set_top_line("") 
		    printf(SCREEN, "save changes to %s? ", {file_name}) 
		    answer = key_gets("yn", {}) 
		    if find('y', answer) then 
			save_file(file_name) 
			exit 
		    elsif find('n', answer) then 
			exit 
		    end if 
		end while 
	end if 
	save_state() 
--          set_top_line("new file name: ") 
--          answer = delete_trailing_white(key_gets("", file_history)) 
	 
 
            res = select_file() 
	    if res = 1 then 
                if sequence(select_file_get_answer()) then 
		answer = select_file_get_answer() 
		else 
		answer = "" 
		end if 
	    else 
	    answer = "" 
	    end if 
	 
            if length(answer) != 0 then 
	    file_name = answer 
	    stop = TRUE 
	end if 
end procedure 
new topic     » goto parent     » topic index » view message » categorize

3. Re: EuGTK-3.0 File Dialogue question

jimcbrown said...

I suggest you try this.

I don't understand why select_file() has to return 1 to get the dialog to close, however. Is select_file() being called elsewhere?

Thanks, Jim

Your code works much better. The file selector works fairly well, but after selecting the file by double clicking on it, it remains open, even though the selected file has loaded into the editor window/pane. The file selector will not respond to the "cancel", "ok", or "x" -close buttons, but it does respond to the full/reduce window size box button. Also, it will minimize and appears not to spawn additional instances when used repeatedly. However; it seems to freeze, if you navigate to another directory and load a file. I think there are additional commands regarding navigation, new folders and what not.

This is a big step forward.

The compiled version of my editor without the gtk file selector = 660k. With the gtk file selector = 1436k.

I think it might be worth pursuing a bit further to see if a robust file selector can be developed. I know of of several console utility programs which could really benefit from a file selector.

Your help is deeply appreciated.

new topic     » goto parent     » topic index » view message » categorize

4. Re: EuGTK-3.0 File Dialogue question

I hesitate to say 'never', but you're never going to get this to work reliably. First of all, the reason the file dialog doesn't respond to button clicks, closing, etc, is because you never enter the GTK main processing loop, which is what watches for events.

You do this with main()

And once you enter the main processing loop, you'll be interfering with ed's own main loop, which likewise watches for events (mostly key presses). GTK will catch the keypresses, etc. first.

There may be a way to pass events through GTK back to ed, but it would be tricky. Kinda like putting Republicans and Democrats in the same room and expecting something good to come out of it...

new topic     » goto parent     » topic index » view message » categorize

5. Re: EuGTK-3.0 File Dialogue question

irv said...

I hesitate to say 'never', but you're never going to get this to work reliably. First of all, the reason the file dialog doesn't respond to button clicks, closing, etc, is because you never enter the GTK main processing loop, which is what watches for events.

You do this with main()

And once you enter the main processing loop, you'll be interfering with ed's own main loop, which likewise watches for events (mostly key presses). GTK will catch the keypresses, etc. first.

There may be a way to pass events through GTK back to ed, but it would be tricky. Kinda like putting Republicans and Democrats in the same room and expecting something good to come out of it...

OK. Thanks for the heads up - I'll use EuGTK for other projects. smile

new topic     » goto parent     » topic index » view message » categorize

6. Re: EuGTK-3.0 File Dialogue question

You might try

system_exec("zenity --file-selection > tmp",0) 

to save the output to a temp file, then read it into ed.ex

The zenity file selector is the same as GTK.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu