To: tim at fungible.com Cc: lablgtk at kaba.or.jp Subject: Re: Can't get keypresses from a GMisc.drawing_area In-Reply-To: <20020809181226.61C9E7F66 at lobus.fungible.com> References: <20020809123807R.garrigue at kurims.kyoto-u.ac.jp> <20020809045034.996707F66@lobus.fungible.com> <20020809181226.61C9E7F66@lobus.fungible.com> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20020819115555F.garrigue at kurims.kyoto-u.ac.jp> Date: Mon, 19 Aug 2002 11:55:55 +0900 From: Jacques Garrigue Lines: 101 From: tim@fungible.com (Tim Freeman) > From: Jacques Garrigue > >You can only get keypress events on the toplevel window. > >i.e. area#misc#toplevel > > Well, not quite. area#misc#toplevel is a GObj.widget option. > There doesn't seem to be any path from GObj.widget to a key_press method. > > In C GTK programs, the method that gives you a toplevel gives you a > widget, and then you can inspect its type and downcast it to a > window. OCAML doesn't let you downcast. You can, but you must go through he Gtk* interface. For instance, GtkWindow.Window.cast safely casts (potentially raising an exception) from a 'a obj to a window obj, which you can wrap afterwards: new GWindow.window (GtkWindow.Window.cast w#as_widget) However, in the present case, the spec of misc#toplevel was wrong. It always returns the topmost parent, and the widget itself if it is not available. As a result you cannot be sure it will be a window. So, here is a better patch, which adds the GWindow.toplevel function to give you the toplevel window of a widget, if it exists. --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp JG Index: src/gObj.ml =================================================================== RCS file: /staff2/garrigue/repos/lablgtk/src/gObj.ml,v retrieving revision 1.41 diff -u -r1.41 gObj.ml --- src/gObj.ml 2002/06/20 00:17:08 1.41 +++ src/gObj.ml 2002/08/19 02:35:55 @@ -275,8 +275,8 @@ (* get functions *) method name = Widget.get_name obj method toplevel = - try Some (new widget (Object.unsafe_cast (Widget.get_toplevel obj))) - with Gpointer.Null -> None + try new widget (Object.unsafe_cast (Widget.get_toplevel obj)) + with Gpointer.Null -> failwith "GObj.misc_ops#toplevel" method window = Widget.window obj method colormap = Widget.get_colormap obj method visual = Widget.get_visual obj Index: src/gObj.mli =================================================================== RCS file: /staff2/garrigue/repos/lablgtk/src/gObj.mli,v retrieving revision 1.37 diff -u -r1.37 gObj.mli --- src/gObj.mli 2002/06/20 00:17:08 1.37 +++ src/gObj.mli 2002/08/19 02:34:24 @@ -186,7 +186,7 @@ method show : unit -> unit method show_all : unit -> unit method style : style - method toplevel : widget option + method toplevel : widget method unmap : unit -> unit method unparent : unit -> unit method unrealize : unit -> unit Index: src/gWindow.ml =================================================================== RCS file: /staff2/garrigue/repos/lablgtk/src/gWindow.ml,v retrieving revision 1.21 diff -u -r1.21 gWindow.ml --- src/gWindow.ml 2001/03/12 03:43:42 1.21 +++ src/gWindow.ml 2002/08/19 02:40:15 @@ -46,6 +46,12 @@ if show then Widget.show w; new window w +let cast_window (w : #widget) = + new window (GtkWindow.Window.cast w#as_widget) + +let toplevel (w : #widget) = + try Some (cast_window w#misc#toplevel) with Cannot_cast _ -> None + class dialog obj = object inherit [window] window_skel (obj : Gtk.dialog obj) method connect = new container_signals obj Index: src/gWindow.mli =================================================================== RCS file: /staff2/garrigue/repos/lablgtk/src/gWindow.mli,v retrieving revision 1.21 diff -u -r1.21 gWindow.mli --- src/gWindow.mli 2002/05/30 05:49:09 1.21 +++ src/gWindow.mli 2002/08/19 02:43:14 @@ -49,6 +49,9 @@ ?border_width:int -> ?width:int -> ?height:int -> ?show:bool -> unit -> window +val toplevel : #widget -> window option +(** return the toplevel window of this widget, if existing *) + class dialog : Gtk.dialog obj -> object inherit [window] window_skel