1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/* Utility functions for timing widgets
*
* Authors:
* Federico Mena-Quintero <federico@novell.com>
*
* To measure how long it takes to fully map and expose a toplevel window, we
* use a trick which Owen Taylor described on IRC one day:
*
* 1. Start a timer.
* 2. Call gtk_widget_show_all() on the toplevel window.
* 3. In the expose_event handler of the window, queue an idle handler with
* G_PRIORITY_HIGH.
* 4. In the idle handler, change a property on the toplevel window.
* 5. In the property_notify_event handler, stop the timer.
*/
#include <string.h>
#include <glib.h>
#include <gtk/gtkmain.h>
#include "timers.h"
struct timer_closure
{
GTimer *timer;
GtkWidget *widget;
TimerReportFunc report_func;
gpointer user_data;
};
static gboolean
widget_property_notify_event_cb (GtkWidget *widget, GdkEventProperty *event, gpointer data)
{
struct timer_closure *closure;
gdouble elapsed;
closure = data;
if (event->atom != gdk_atom_intern ("window_property_change", FALSE))
return FALSE;
/* Finish timing map/expose */
elapsed = g_timer_elapsed (closure->timer, NULL);
(* closure->report_func) (TIMER_REPORT_WIDGET_SHOW, elapsed, closure->user_data);
/* Time destruction */
g_timer_reset (closure->timer);
gtk_widget_destroy (widget);
elapsed = g_timer_elapsed (closure->timer, NULL);
(* closure->report_func) (TIMER_REPORT_WIDGET_DESTRUCTION, elapsed, closure->user_data);
gtk_main_quit (); /* This will get us back to the end of timer_time_widget() */
return TRUE;
}
static gboolean
idle_after_expose_cb (gpointer data)
{
struct timer_closure *closure;
closure = data;
gdk_property_change (closure->widget->window,
gdk_atom_intern ("window_property_change", FALSE),
gdk_atom_intern ("STRING", FALSE),
8,
GDK_PROP_MODE_REPLACE,
"hello",
strlen ("hello"));
return FALSE;
}
static gboolean
widget_expose_event_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
struct timer_closure *closure;
closure = data;
g_idle_add_full (G_PRIORITY_HIGH, idle_after_expose_cb, closure, NULL);
return FALSE;
}
static void
instrument_widget (GtkWidget *widget,
struct timer_closure *closure)
{
g_signal_connect (widget, "expose-event",
G_CALLBACK (widget_expose_event_cb), closure);
gtk_widget_add_events (widget, GDK_PROPERTY_CHANGE_MASK);
g_signal_connect (widget, "property-notify-event",
G_CALLBACK (widget_property_notify_event_cb), closure);
}
void
timer_time_widget (TimerWidgetCreateFunc create_func,
TimerReportFunc report_func,
gpointer user_data)
{
GTimer *timer;
GtkWidget *widget;
gdouble elapsed;
struct timer_closure closure;
g_assert (create_func != NULL);
g_assert (report_func != NULL);
/* Time creation */
timer = g_timer_new ();
widget = (* create_func) (user_data);
g_assert (widget != NULL);
g_assert (!GTK_WIDGET_VISIBLE (widget) && !GTK_WIDGET_MAPPED (widget));
elapsed = g_timer_elapsed (timer, NULL);
(* report_func) (TIMER_REPORT_WIDGET_CREATION, elapsed, user_data);
/* Start timing map/expose */
closure.timer = timer;
closure.widget = widget;
closure.report_func = report_func;
closure.user_data = user_data;
instrument_widget (widget, &closure);
g_timer_reset (timer);
gtk_widget_show_all (widget);
gtk_main ();
/* Expose time and destruction time get recorded in widget_property_notify_event_cb() */
}
|