Ruby: File dialog wont close - ruby

I am using the ruby gem gtk2 to display a simple file dialog, where the user can select a file from his computer. It works just fine, however there is one little problem.
The file dialog doesnt close after selecting the file, but it stays open until the whole script finished.
My file dialog looks like this:
def ask_for_file(question)
dialog = Gtk::FileChooserDialog.new(question, nil,
Gtk::FileChooser::ACTION_OPEN,
"gnome-vfs",
[Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT],
[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL]
)
if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
file_name = dialog.filename.to_s
return file_name
else
return nil
end
end
When I call it in my script like this:
path = ask_for_file("Test?")
sleep(5)
puts "continue with #{path}"
The file dialog stays open for 5 seconds and is unresponsive in that time. How could I get it to close after a file has been selected, but before the 5 second sleep?
I tried to use dialog.destroy in ask_for_file just before returning the file_name but that didnt seem to help.

I solved it by creating a new window with a button to open the file dialog. Upon clicking the open button, I also send a signal to destroy the main window. This way everything closes immediatly after I selected a file.
For reference see this example

You need to manually call gtk_widget_destroy() on GTK+-provided dialog boxes like GtkFileChooserDialog; clicking on one of the buttons won't be enough. Note that this destroys the file chooser part of the dialog as well, so be sure to get your filename and anything else of value out before you do.

Related

Ruby Watir reference Chrome Print Preview

I'm writing a script to cycle through some links and print to PDF.
When you click on one of the links a new window pops up of the document to be printed and on top of that new window Chrome's print preview box pops up.
I'm trying to change the printer and then click the print button.
What's happening with my code however is that watir is only looking at the window html and not the print preview html so the elements are unable to be located.
My code looks like this:
begin
browser.window(title: 'Window Title Name').use do
browser.div(:id => "navbar-scroll-container").button(:class => "destination-settings-change-button").click
sleep(1)
browser.element(:title => "CutePDF Writer").click
browser.button(:visible_text => 'Print').click
end
rescue Watir::Exception::NoMatchingWindowFoundException
retry
end
I have tried using just "browser.windows.last.use do" instead of the title name but that also doesn't work.
I know it can only see the underlying window's html because when I puts browser.html the output is that underlying windows html.
If you want to look at the html situation just Ctrl+P this page (in chrome) to see what I mean.
Of course, after posting a question I soon found a sort of answer; basically chrome opens 2 browser windows and the print preview one couldn't be accessed by title (because it had the same title as the underlying one) but could be accessed by browser.window(:index => 1).use do. not sure why browser.windows.last.use do didn't work. Also following that I would have thought browser.window(:index => 2).use do would have been the correct one as index 0 would be the originally opened one, index 1 the popup and index 2 the print preview.

I can't find out why my Autohotkey script sometimes works and sometimes not

I have the following script:
gosub, menucreate
Settimer, CheckRun, 1000
ExitSub:
ExitApp
return
Mess:
msgbox tada
return
menucreate:
Menu, TRAY, DeleteAll
Menu, TRAY, NoStandard
Menu, TRAY, Add, Exit , ExitSub
Menu, TRAY, Add, Message , Mess
return
CheckRun:
gosub ,menucreate
return
On occasions the menu Message works, but sometimes not.
Every few starts of this program it shows the message, and it keeps working if I reload.
But if I restart it might not working (or might ... I couldn't figure out any pattern)
To me it seems like you fail to end the Auto-execute section.
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section.
So try and put a return after your settimer and see if that helps
OK, in mean time I found out, but till now hadn't time to add this here (and kinda forgot).
The thing is: If the menu gets redrawn every second it will only cause an action until it gets redrawn.
So if you have pulled it up and the redraw happens before you click on a item nothing happens.
This thing here was just a test script to find out why the actual one has stopped working.
In that one I have included a test if actually something has changed and only then the menu get redrawn ... and voila it works.

Execute a MEL command after a window has opened

I'm writing a MEL script which involves opening the grease pencil UI toolbar. I want to remove the close button on that toolbar. I tried doing
GreasePencilTool;
window -edit -tbm 0 greasePencilFloatingWindow;
but get Error: line 2: window: Object 'greasePencilFloatingWindow' not found.
Further tests reveal that running
GreasePencilTool;
window -q -exists greasePencilFloatingWindow;
will return a result of 0.
Running GreasePencilTool; and then window -edit -tbm 0 greasePencilFloatingWindow; at separate times works as expected, as does running window -edit -tbm 0 greasePencilFloatingWindow; when the toolbar is already open.
However, I need to be able to remove the close button immediately when the toolbar opens.
The closest thing I can think of that illustrates what I want to do are Javascript callback functions, where another function can be executed once the current function is finished... but is there a way to do something like that in MEL?
I've also tried using the evalDeferred command without success.
The grease pencil tool is launched asynchronously so the window will not be present for some unknown length of time. This means the best you could do is trigger a function which would check periodically and do it the next time you find the correctly named window; you could attach this to an idle time script job.
It's ugly. But it is probably the only way since there's no event that will notify when thje window arrives. If you do that, make the script job suicide after it fires so it's not sitting there on every idle check till the end of time.

How to open a Glade-created window and wait for it to close, in Ruby?

I want to make my own window, using Glade (3.14.2).
At a certain point in my program, I want to
1) Put up the window and let the user do stuff
2) Wait for it to close
3) Get values from the window object
4) Continue on in my code
So basically, I want to treat the window like a modal dialog - but one that I write and control.
I've tried for a few hours. The window appears just fine, as designed in Glade. The user can interact with it.
When the window closes, code that's been connected with signal_connect('destroy') executes.
But the code that invoked the window's show() method... does not continue executing after the window closes.
class GrammarNodeEditor
#this makes the class visual:
include GladeGUI
def initialize(raw_node = nil, &close_block)
#raw_node = raw_node || {type: :Sequence, data: []}
#original_data = #raw_node[:data]
#close_block = close_block
end
def show
puts "GNE Window Opening"
load_glade(__FILE__)
#builder["window1"].title = "Edit/Create Grammar Node"
#builder["window1"].signal_connect('destroy') {|*args|
#close_block.call(self)
puts "GNE WINDOW DESTROY"
}
show_window()
puts "Done showing window"
end
Here is how I invoke it:
rhs_editor = GrammarNodeEditor.new {|obj|
puts "In closeblck, obj is #{obj.inspect}"
#rhs = obj.raw_node
}
puts "About to call show in GR:Init"
rhs_editor.show
puts "Back from calling show in GR:Init"
Here is the output:
About to call show in GR:Init
GNE Window Opening
In closeblck, obj is #<GrammarNodeEditor:0x7b82a88 #raw_node={:type=>:Sequence, :data=>[]}, [more junk here]>
GNE WINDOW DESTROY
The first two lines of output appear after I open the window. The 3rd and 4th appear when I close the window.
Note that "Done showing window" and "Back from calling show in GR:Init" are not printed at all.
Just to make this a little more interesting, I want to be able to do this from within code that puts up another window. My top-level window has a button to create a new Rule. The Rule must be initialized with a Node, and then the Rule must be edited. So first I need to put up a Node-definition window (as shown above) and then, when I have a Node defined, I want to put up a Rule window that uses that Node.
So I think I need to call this code within either the initialize() or the show() method of the GrammarRuleWindow class (another Glade-defined window).
Can someone explain why my puts's aren't being printed, and how to make the control flow go on through them?
Thanks!
...So it turned out the problem was that I had created the window's .glade file directly in Glade, rather than using the VisualRuby IDE.
Creating the .glade in VR adds some stuff to the file that VR needs. Specifically, the file needs to contain the line
<signal name="destroy" handler="destroy_window" swapped="no"/>
before the first <child...> tag.

In matlab, how do you save a figure as an image in the same way as using "Save As..." in the figure window?

When saving a figure, what function does Matlab use? For example, when a user selects File > Save As... and then selects .png or another image format, what is going on behind the scenes?
I am asking because I am trying to automate saving, but when I use saveas or print, the resulting image is really pixelated. However, the image looks really good when I save the figure using the method described above.
What method should I use to save my figure from the command line? The actual method that the figure window uses would work, but if you guys have better solutions, I'd appricate it!
The callback for the "Save As..." menu item invokes the function FILEMENUFCN with the first input argument being the handle of the figure the menu is in and the second input argument being the string 'FileSaveAs'. If you have the figure handle stored in the variable hFigure, then the following command should be equivalent to clicking the "Save As..." menu item in that figure window:
>> filemenufcn(hFigure,'FileSaveAs');
A few notes...
The function FILEMENUFCN is only partially documented. You can do help filemenufcn in the command window, but there is no entry for it in the online documentation. In MATLAB 2009a, the function can be found in the following folder:
C:\Program Files\MATLAB\R2009a\toolbox\matlab\uitools\filemenufcn.m
Looking through the function code, it appears that it ultimately calls either the function SAVEAS for .fig files or the function HGEXPORT (with additional input arguments) for other file types.
I was able to hunt down the callback for the "Save As..." menu item by searching through the children of the figure window and its menus. You can do this yourself by setting the root property 'ShowHiddenHandles' to 'on' and then traversing through the 'Children' properties of the figure window and its menus using the GET command. An alternative is to use the FINDALL command, assuming you know some properties of the objects you are looking for. For example, this will find the handle to the "File" menu for the current figure window:
>> hFileMenu = findall(gcf,'Label','&File');
And this will find the handle to the "Save As..." menu item and display its callback:
>> hSaveAs = findall(hFileMenu,'Label','Save &As...');
>> get(hSaveAs,'Callback')
ans =
filemenufcn(gcbf,'FileSaveAs')
I don't know about you, but for me the saved image looks nice.
Code example:
... create some figure ...
saveas(gcf, 'some_figure.png');
To set a user specified resolution use:
print(gcf,'some_figure','-dpng','-rSOMENUMBER')
where SOMENUMBERis a parameter used for the resolution.
The final resolution is (SOMENUMBER*8) x (SOMENUMBER*6)

Resources