Ruby Tk - dialog windows

M

matt

Hi - I'm trying to make `modal' windows. I'm confused about how to
use TkDialog, though, is there any good documentation? I need a
window that will not let someone go to the `parent' window, can
display stuff just like a TkToplevel or TkRoot...

My current solution is to make a TkToplevel with `parent' as the
parent object, and to use
parent.bind('FocusIn') {
begin
myTopLevel.focus
rescue Exception => e
end
}

and then #bind_remove later...

this is inelegant, and I can still click the x-close button on the
parent... I could redo the closing protocol for the parent, but this
seems an inelegant solution, and I'd have to reset it aftewards...

anyone got any ideas?

Thanks,

Me
 
H

Hidetoshi NAGAI

Hi,

From: (e-mail address removed) (matt)
Subject: Ruby Tk - dialog windows
Date: Tue, 15 Jun 2004 04:53:41 +0900
Message-ID: said:
Hi - I'm trying to make `modal' windows. I'm confused about how to
use TkDialog, though, is there any good documentation? I need a
window that will not let someone go to the `parent' window, can
display stuff just like a TkToplevel or TkRoot...

Please read dialog1.rb and dialog2.rb included in the 'Ruby/Tk widget
demos'.
But if you want a more complex modal window, you should use 'grab'.
The step is

(1) show the modal window (toplevel widget),
(2) set grab to the modal window,
(3) wait TkVariable access on the window
and release grab from the window,
OR destroy the window (release grab automatically).

(please read ruby/ext/tk/sample/tkcombobox.rb).

------<dialog1.rb>---------------------------------
#
# a dialog box with a local grab (called by 'widget')
#
class TkDialog_Demo1 < TkDialog
###############
private
###############
def title
"Dialog with local grab"
end

def message
'This is a modal dialog box. It uses Tk\'s "grab" command to create a "local grab" on the dialog box. The grab prevents any pointer-related events from getting to any other windows in the application until you have answered the dialog by invoking one of the buttons below. However, you can still interact with other applications.'
end

def bitmap
'info'
end

def default_button
0
end

def buttons
# ["Dismiss", "", "Show Code"]
["OK", "Cancel", "Show Code"]
end
end

ret = TkDialog_Demo1.new('message_config'=>{'wraplength'=>'4i'}).value
case ret
when 0
print "You pressed OK\n"
when 1
print "You pressed Cancel\n"
when 2
showCode 'dialog1'
end
---------------------------------------------------

------<tkcombobox.rb:part>-------------------------
def _button_proc(dir = true)
@btn.relief:)sunken)
x = @frame.winfo_rootx
y = @frame.winfo_rooty
if dir
@top.geometry("+#{x}+#{y + @frame.winfo_height}")
else
@btn.image(@@up_btn_bmp)
@top.geometry("+#{x}+#{y - @top.winfo_reqheight}")
end
@top.deiconify
@lst.focus

if (idx = values.index(@ent.value))
@lst.see(idx - 1)
@lst.activate(idx)
@lst.selection_set(idx)
elsif @lst.size > 0
@lst.see(0)
@lst.activate(0)
@lst.selection_set(0)
end
@top.grab

begin
@var.tkwait
if (idx = @var.to_i) >= 0
@ent.value = @lst.get(idx)
end
@top.withdraw
@btn.relief:)raised)
@btn.image(@@down_btn_bmp)
rescue
ensure
begin
@top.grab:)release)
@ent.focus
rescue
end
end
end
private :_button_proc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top