1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Appear Time Tracker.
15 *
16 * The Initial Developer of the Original Code is
17 * Jon A. Cruz.
18 * Portions created by the Initial Developer are Copyright (C) 2010
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
34 *
35 * ***** END LICENSE BLOCK ***** */
38 #include "ege-appear-time-tracker.h"
39 #include <glib-object.h>
40 #include <gtk/gtk.h>
43 namespace ege
44 {
46 namespace {
48 void unhookHandler( gulong &id, GtkWidget *obj )
49 {
50 if ( id ) {
51 if ( obj ) {
52 g_signal_handler_disconnect( G_OBJECT(obj), id );
53 }
54 id = 0;
55 }
56 }
58 } // namespace
61 AppearTimeTracker::AppearTimeTracker(GTimer *timer, GtkWidget *widget, gchar const* name) :
62 _name(name ? name : ""),
63 _timer(timer),
64 _widget(widget),
65 _topMost(widget),
66 _autodelete(false),
67 _mapId(0),
68 _realizeId(0),
69 _hierarchyId(0)
71 {
72 while (_topMost->parent) {
73 _topMost = _topMost->parent;
74 }
75 _mapId = g_signal_connect( G_OBJECT(_topMost), "map-event", G_CALLBACK(mapCB), this );
76 _realizeId = g_signal_connect( G_OBJECT(_topMost), "realize", G_CALLBACK(realizeCB), this );
77 _hierarchyId = g_signal_connect( G_OBJECT(_widget), "hierarchy-changed", G_CALLBACK(hierarchyCB), this );
78 }
80 AppearTimeTracker::~AppearTimeTracker()
81 {
82 if ( _timer ) {
83 g_timer_destroy(_timer);
84 _timer = 0;
85 }
87 unhookHandler( _mapId, _topMost );
88 unhookHandler( _realizeId, _topMost );
89 unhookHandler( _hierarchyId, _widget );
90 }
92 void AppearTimeTracker::stop() {
93 if (_timer) {
94 g_timer_stop(_timer);
95 }
96 }
98 void AppearTimeTracker::setAutodelete(bool autodelete)
99 {
100 if ( autodelete != _autodelete ) {
101 _autodelete = autodelete;
102 }
103 }
105 void AppearTimeTracker::report(gchar const* msg)
106 {
107 gulong msCount = 0;
108 gdouble secs = g_timer_elapsed( _timer, &msCount );
109 g_message("Time ended at %2.3f with [%s] on [%s]", secs, msg, _name.c_str());
110 }
112 void AppearTimeTracker::handleHierarchyChange( GtkWidget * /*prevTop*/ )
113 {
114 GtkWidget *newTop = _widget;
115 while (newTop->parent) {
116 newTop = newTop->parent;
117 }
119 if ( newTop != _topMost ) {
120 unhookHandler( _mapId, _topMost );
121 unhookHandler( _realizeId, _topMost );
123 _topMost = newTop;
124 _mapId = g_signal_connect( G_OBJECT(_topMost), "map-event", G_CALLBACK(mapCB), this );
125 _realizeId = g_signal_connect( G_OBJECT(_topMost), "realize", G_CALLBACK(realizeCB), this );
126 }
127 }
129 gboolean AppearTimeTracker::mapCB(GtkWidget * /*widget*/, GdkEvent * /*event*/, gpointer userData)
130 {
131 AppearTimeTracker *tracker = reinterpret_cast<AppearTimeTracker*>(userData);
132 tracker->report("MAP");
133 if ( tracker->_autodelete ) {
134 delete tracker;
135 }
136 return FALSE;
137 }
139 void AppearTimeTracker::realizeCB(GtkWidget * /*widget*/, gpointer userData)
140 {
141 AppearTimeTracker *tracker = reinterpret_cast<AppearTimeTracker*>(userData);
142 tracker->report("REALIZE");
143 }
145 void AppearTimeTracker::hierarchyCB(GtkWidget * /*widget*/, GtkWidget *prevTop, gpointer userData)
146 {
147 AppearTimeTracker *tracker = reinterpret_cast<AppearTimeTracker*>(userData);
148 tracker->handleHierarchyChange( prevTop );
149 }
151 } // namespace ege
153 /*
154 Local Variables:
155 mode:c++
156 c-file-style:"stroustrup"
157 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
158 indent-tabs-mode:nil
159 fill-column:99
160 End:
161 */
162 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :