GTK+ 事件和信号
简单的例子:
#include <gtk/gtk.h>
void button_clicked(GtkWidget *widget, gpointer data)
{
g_print("clicked\n");
}int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *button;gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkButton");
gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);button = gtk_button_new_with_label("Click");
gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50);
gtk_widget_set_size_request(button, 80, 35);g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(button_clicked), NULL);g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);gtk_widget_show_all(window);
gtk_main();
return 0;
}
使用 g_signal_connect() 函数将信号和处理函数连接在一起。
移动事件 move
#include <gtk/gtk.h>
void frame_callback(GtkWindow *window,
GdkEvent *event, gpointer data)
{
int x, y;
char buf[10];
x = event->configure.x;
y = event->configure.y;
sprintf(buf, "%d, %d", x, y);
gtk_window_set_title(window, buf);
}
int main(int argc, char *argv[])
{
GtkWidget *window;gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
gtk_window_set_title(GTK_WINDOW(window), "Simple");
gtk_widget_add_events(GTK_WIDGET(window), GDK_CONFIGURE);g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));g_signal_connect(G_OBJECT(window), "configure-event",
G_CALLBACK(frame_callback), NULL);gtk_widget_show(window);
gtk_main();return 0;
}
断开连接 disconnect signal
#include <gtk/gtk.h>
int handler_id;void button_clicked(GtkWidget *widget, gpointer data)
{
g_print("clicked\n");
}void toogle_signal(GtkWidget *widget, gpointer window)
{
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {//获得check的状态
handler_id = g_signal_connect(G_OBJECT(window), "clicked",
G_CALLBACK(button_clicked), NULL);
} else {
g_signal_handler_disconnect(window, handler_id);//断开连接
}
}
int main( int argc, char *argv[])
{GtkWidget *window;
GtkWidget *fixed;
GtkWidget *button;
GtkWidget *check;
gtk_init(&argc, &argv);window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 250, 150);
gtk_window_set_title(GTK_WINDOW(window), "Disconnect");fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);button = gtk_button_new_with_label("Click");
gtk_widget_set_size_request(button, 80, 30);
gtk_fixed_put(GTK_FIXED(fixed), button, 30, 50);check = gtk_check_button_new_with_label("Connect");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);
gtk_fixed_put(GTK_FIXED(fixed), check, 130, 50);handler_id = g_signal_connect(G_OBJECT(button), "clicked",
G_CALLBACK(button_clicked), NULL);g_signal_connect(G_OBJECT(check), "clicked",
G_CALLBACK(toogle_signal), (gpointer) button);g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);gtk_widget_show_all(window);
gtk_main();
return 0;
}
拖放 drag and drop
计时器 timer
#include <cairo.h>
#include <gtk/gtk.h>
#include <time.h>
static char buffer[256];
static gboolean
on_expose_event(GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
cairo_t *cr;cr = gdk_cairo_create(widget->window);
cairo_move_to(cr, 30, 30);
cairo_show_text(cr, buffer);cairo_destroy(cr);
return FALSE;
}static gboolean
time_handler(GtkWidget *widget)
{
if (widget->window == NULL) return FALSE;time_t curtime;
struct tm *loctime;curtime = time(NULL);
loctime = localtime(&curtime);
strftime(buffer, 256, "%T", loctime);gtk_widget_queue_draw(widget);//发送 expose-event 信号,emit signal
return TRUE;
}int
main (int argc, char *argv[])
{GtkWidget *window;
GtkWidget *darea;gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER (window), darea);g_signal_connect(darea, "expose-event",
G_CALLBACK(on_expose_event), NULL);//当窗口重画时,发送此信号
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 170, 100);gtk_window_set_title(GTK_WINDOW(window), "timer");
g_timeout_add(1000, (GSourceFunc) time_handler, (gpointer) window);//注册一个计时器
gtk_widget_show_all(window);
time_handler(window);gtk_main();
return 0;
}