00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "gtkStatsLabel.h"
00016 #include "gtkStatsMonitor.h"
00017 #include "gtkStatsGraph.h"
00018
00019 int GtkStatsLabel::_left_margin = 2;
00020 int GtkStatsLabel::_right_margin = 2;
00021 int GtkStatsLabel::_top_margin = 2;
00022 int GtkStatsLabel::_bottom_margin = 2;
00023
00024
00025
00026
00027
00028
00029 GtkStatsLabel::
00030 GtkStatsLabel(GtkStatsMonitor *monitor, GtkStatsGraph *graph,
00031 int thread_index, int collector_index, bool use_fullname) :
00032 _monitor(monitor),
00033 _graph(graph),
00034 _thread_index(thread_index),
00035 _collector_index(collector_index)
00036 {
00037 _widget = NULL;
00038 if (use_fullname) {
00039 _text = _monitor->get_client_data()->get_collector_fullname(_collector_index);
00040 } else {
00041 _text = _monitor->get_client_data()->get_collector_name(_collector_index);
00042 }
00043
00044 _widget = gtk_drawing_area_new();
00045 gtk_widget_add_events(_widget,
00046 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
00047 GDK_BUTTON_PRESS_MASK);
00048 g_signal_connect(G_OBJECT(_widget), "expose_event",
00049 G_CALLBACK(expose_event_callback), this);
00050 g_signal_connect(G_OBJECT(_widget), "enter_notify_event",
00051 G_CALLBACK(enter_notify_event_callback), this);
00052 g_signal_connect(G_OBJECT(_widget), "leave_notify_event",
00053 G_CALLBACK(leave_notify_event_callback), this);
00054 g_signal_connect(G_OBJECT(_widget), "button_press_event",
00055 G_CALLBACK(button_press_event_callback), this);
00056
00057 gtk_widget_show(_widget);
00058
00059
00060 _layout = gtk_widget_create_pango_layout(_widget, _text.c_str());
00061
00062
00063 LRGBColor rgb = _monitor->get_collector_color(_collector_index);
00064 _bg_color.red = (int)(rgb[0] * 65535.0f);
00065 _bg_color.green = (int)(rgb[1] * 65535.0f);
00066 _bg_color.blue = (int)(rgb[2] * 65535.0f);
00067
00068
00069 double bright =
00070 rgb[0] * 0.299 +
00071 rgb[1] * 0.587 +
00072 rgb[2] * 0.114;
00073
00074 if (bright >= 0.5) {
00075 _fg_color.red = _fg_color.green = _fg_color.blue = 0;
00076 } else {
00077 _fg_color.red = _fg_color.green = _fg_color.blue = 0xffff;
00078 }
00079
00080
00081
00082 int width, height;
00083 pango_layout_get_pixel_size(_layout, &width, &height);
00084 gtk_widget_set_size_request(_widget, width + 8, height);
00085
00086 _highlight = false;
00087 _mouse_within = false;
00088 _height = height;
00089 }
00090
00091
00092
00093
00094
00095
00096 GtkStatsLabel::
00097 ~GtkStatsLabel() {
00098
00099 }
00100
00101
00102
00103
00104
00105
00106 GtkWidget *GtkStatsLabel::
00107 get_widget() const {
00108 return _widget;
00109 }
00110
00111
00112
00113
00114
00115
00116 int GtkStatsLabel::
00117 get_height() const {
00118 return _height;
00119 }
00120
00121
00122
00123
00124
00125
00126 int GtkStatsLabel::
00127 get_collector_index() const {
00128 return _collector_index;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 void GtkStatsLabel::
00138 set_highlight(bool highlight) {
00139 if (_highlight != highlight) {
00140 _highlight = highlight;
00141 gtk_widget_queue_draw(_widget);
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 bool GtkStatsLabel::
00152 get_highlight() const {
00153 return _highlight;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162 void GtkStatsLabel::
00163 set_mouse_within(bool mouse_within) {
00164 if (_mouse_within != mouse_within) {
00165 _mouse_within = mouse_within;
00166 gtk_widget_queue_draw(_widget);
00167 }
00168 }
00169
00170
00171
00172
00173
00174
00175 gboolean GtkStatsLabel::
00176 expose_event_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
00177 GtkStatsLabel *self = (GtkStatsLabel *)data;
00178
00179 GdkGC *gc = gdk_gc_new(widget->window);
00180 gdk_gc_set_rgb_fg_color(gc, &self->_bg_color);
00181
00182 gdk_draw_rectangle(widget->window, gc, TRUE, 0, 0,
00183 widget->allocation.width, widget->allocation.height);
00184
00185
00186 int width, height;
00187 pango_layout_get_pixel_size(self->_layout, &width, &height);
00188
00189 gdk_gc_set_rgb_fg_color(gc, &self->_fg_color);
00190 gdk_draw_layout(widget->window, gc,
00191 (widget->allocation.width - width) / 2, 0,
00192 self->_layout);
00193
00194
00195 if (self->_highlight || self->_mouse_within) {
00196 gdk_draw_rectangle(widget->window, gc, FALSE, 0, 0,
00197 widget->allocation.width - 1, widget->allocation.height - 1);
00198 }
00199
00200 g_object_unref(gc);
00201 return TRUE;
00202 }
00203
00204
00205
00206
00207
00208
00209 gboolean GtkStatsLabel::
00210 enter_notify_event_callback(GtkWidget *widget, GdkEventCrossing *event,
00211 gpointer data) {
00212 GtkStatsLabel *self = (GtkStatsLabel *)data;
00213 self->set_mouse_within(true);
00214 return TRUE;
00215 }
00216
00217
00218
00219
00220
00221
00222 gboolean GtkStatsLabel::
00223 leave_notify_event_callback(GtkWidget *widget, GdkEventCrossing *event,
00224 gpointer data) {
00225 GtkStatsLabel *self = (GtkStatsLabel *)data;
00226 self->set_mouse_within(false);
00227 return TRUE;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 gboolean GtkStatsLabel::
00237 button_press_event_callback(GtkWidget *widget, GdkEventButton *event,
00238 gpointer data) {
00239 GtkStatsLabel *self = (GtkStatsLabel *)data;
00240 bool double_click = (event->type == GDK_2BUTTON_PRESS);
00241 if (double_click) {
00242 self->_graph->clicked_label(self->_collector_index);
00243 }
00244 return TRUE;
00245 }