目前正在尝试创建一个直接基于GtkWidget
的自定义窗口小部件,将其标记为可绘制并使用cairo绘制内容。对于上下文来说非常重要。
一旦我尝试处理事件(因此我可以实现缩放) - 尤其是滚动事件 - 只是无法正常工作我不确定为什么
回调 在聚焦/聚焦时执行(好的旧打印语句证明了这一点),但我从不获得任何检测到的滚轮活动该回调(按钮点击是关键按下/释放也不起作用)。
我试图通过
连接到事件信号g_signal_connect (mywidget, "event",..)
或foo_new
foo_init
两者都没有效果。
Foo
初始化:
foo_init (Foo *self)
{
GtkWidget *widget = GTK_WIDGET (self);
gtk_widget_set_has_window (widget, FALSE);
self->priv = FOO_GET_PRIVATE (self);
gtk_widget_add_events (widget, GDK_ALL_EVENTS_MASK);
g_assert ((gtk_widget_get_events (widget) & GDK_SCROLL_MASK) != 0); //just fine
/* added some stuff I also tried but did not work */
gtk_widget_set_sensitive (widget, TRUE);
gtk_widget_set_can_focus (widget, TRUE);
gtk_widget_grab_focus (widget);
...
如何获取我的小部件的所有事件?
分配widget_class->key_press_event = my_handler_callback;
实际上按预期工作,我得到了我按下的键,但是同样的widget_class->button_press_event = my_handler_callback;
或widget_class->scroll_event = my_handler_callback;
分配不起作用!
widget_class->key_press_event = my_handler_callback; // works
widget_class->key_release_event = my_handler_callback; // works
widget_class->button_press_event = my_handler_callback; // NOT
widget_class->button_release_event = my_handler_callback; // NOT
widget_class->scroll_event = my_handler_callback; // NOT
这让我很怀疑。
要接收此信号,与窗口小部件关联的GdkWindow需要 启用GDK_BUTTON_PRESS_MASK掩码。
是否有必要在gtk_widget_add_events
"工作" ...之前实现小部件?
更新:试图在gtk_widget_add_events
之后致电gtk_widget_show_all
。没有变化。
更新:完全可编译的示例
#ifndef __FOO_H__
#define __FOO_H__
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define FOO_TYPE (foo_get_type ())
#define FOO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FOO_TYPE, Foo))
#define FOO_CONST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FOO_TYPE, Foo const))
#define FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FOO_TYPE, FooClass))
#define FOO_IS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FOO_TYPE))
#define FOO_IS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FOO_TYPE))
#define FOO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FOO_TYPE, FooClass))
typedef struct _Foo Foo;
typedef struct _FooClass FooClass;
typedef struct _FooPrivate FooPrivate;
struct _Foo
{
GtkWidget parent;
FooPrivate *priv;
};
struct _FooClass
{
GtkWidgetClass parent_class;
};
GType foo_get_type (void) G_GNUC_CONST;
Foo *foo_new (void);
G_END_DECLS
#endif /* __FOO_H__ */
#include "foo.h"
gboolean
scroll_hook (GtkWidget *widget, GdkEventScroll *event)
{
g_print ("%p registered a scroll event\n");
return TRUE;
}
#define FOO_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), FOO_TYPE, FooPrivate))
struct _FooPrivate
{
char to_silence_warning;
};
G_DEFINE_TYPE (Foo, foo, GTK_TYPE_WIDGET)
static void
foo_finalize (GObject *object)
{
G_OBJECT_CLASS (foo_parent_class)->finalize (object);
}
static void
foo_class_init (FooClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = foo_finalize;
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
widget_class->scroll_event = scroll_hook;
g_type_class_add_private (object_class, sizeof (FooPrivate));
}
static void
foo_init (Foo *self)
{
self->priv = FOO_GET_PRIVATE (self);
gtk_widget_set_has_window(GTK_WIDGET(self), FALSE);
}
Foo *
foo_new ()
{
return g_object_new (FOO_TYPE, NULL);
}
#include <gtk/gtk.h>
#include "foo.h"
#include <stdlib.h>
gboolean
chicken_out (GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
g_print ("bye");
gtk_main_quit();
return TRUE;
}
int
main (int argc, char *argv[])
{
GtkWidget *window;
Foo *my;
int i;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
my = foo_new ();
gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (my));
gtk_widget_show_all (window);
gtk_widget_add_events (GTK_WIDGET (my), GDK_ALL_EVENTS_MASK);
g_assert ((gtk_widget_get_events (GTK_WIDGET (my)) & GDK_SCROLL_MASK) != 0);
g_signal_connect (window, "delete-event", G_CALLBACK(chicken_out), NULL);
gtk_main ();
return EXIT_SUCCESS;
}
使用
gcc `pkg-config --cflags --libs gtk+-3.0` -I. ./foo.c ./foo_test.c -o foo.bin
编译(授予,所有文件都在你的cwd中)
答案 0 :(得分:2)
需要从GtkWidget
移动到GtkDrawingArea
,因为没有它会崩溃。根本原因是没有正确的->window
默认回调而没有填充widget的realize
私有变量 - 我没有实现。