Panda3D
gtkStatsLabel.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file gtkStatsLabel.cxx
10  * @author drose
11  * @date 2006-01-16
12  */
13 
14 #include "gtkStatsLabel.h"
15 #include "gtkStatsMonitor.h"
16 #include "gtkStatsGraph.h"
17 
18 int GtkStatsLabel::_left_margin = 2;
19 int GtkStatsLabel::_right_margin = 2;
20 int GtkStatsLabel::_top_margin = 2;
21 int GtkStatsLabel::_bottom_margin = 2;
22 
23 /**
24  *
25  */
26 GtkStatsLabel::
27 GtkStatsLabel(GtkStatsMonitor *monitor, GtkStatsGraph *graph,
28  int thread_index, int collector_index, bool use_fullname) :
29  _monitor(monitor),
30  _graph(graph),
31  _thread_index(thread_index),
32  _collector_index(collector_index)
33 {
34  _widget = nullptr;
35  if (use_fullname) {
36  _text = _monitor->get_client_data()->get_collector_fullname(_collector_index);
37  } else {
38  _text = _monitor->get_client_data()->get_collector_name(_collector_index);
39  }
40 
41  _widget = gtk_drawing_area_new();
42  gtk_widget_add_events(_widget,
43  GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
44  GDK_BUTTON_PRESS_MASK);
45  g_signal_connect(G_OBJECT(_widget), "expose_event",
46  G_CALLBACK(expose_event_callback), this);
47  g_signal_connect(G_OBJECT(_widget), "enter_notify_event",
48  G_CALLBACK(enter_notify_event_callback), this);
49  g_signal_connect(G_OBJECT(_widget), "leave_notify_event",
50  G_CALLBACK(leave_notify_event_callback), this);
51  g_signal_connect(G_OBJECT(_widget), "button_press_event",
52  G_CALLBACK(button_press_event_callback), this);
53 
54  gtk_widget_show(_widget);
55 
56  // Make up a PangoLayout to represent the text.
57  _layout = gtk_widget_create_pango_layout(_widget, _text.c_str());
58 
59  // Set the fg and bg colors on the label.
60  LRGBColor rgb = _monitor->get_collector_color(_collector_index);
61  _bg_color.red = (int)(rgb[0] * 65535.0f);
62  _bg_color.green = (int)(rgb[1] * 65535.0f);
63  _bg_color.blue = (int)(rgb[2] * 65535.0f);
64 
65  // Should our foreground be black or white?
66  double bright =
67  rgb[0] * 0.299 +
68  rgb[1] * 0.587 +
69  rgb[2] * 0.114;
70 
71  if (bright >= 0.5) {
72  _fg_color.red = _fg_color.green = _fg_color.blue = 0;
73  } else {
74  _fg_color.red = _fg_color.green = _fg_color.blue = 0xffff;
75  }
76 
77  // What are the extents of the text? This determines the minimum size of
78  // our widget.
79  int width, height;
80  pango_layout_get_pixel_size(_layout, &width, &height);
81  gtk_widget_set_size_request(_widget, width + 8, height);
82 
83  _highlight = false;
84  _mouse_within = false;
85  _height = height;
86 }
87 
88 /**
89  *
90  */
91 GtkStatsLabel::
92 ~GtkStatsLabel() {
93  // DeleteObject(_bg_brush);
94 }
95 
96 /**
97  * Returns the widget for this label.
98  */
99 GtkWidget *GtkStatsLabel::
100 get_widget() const {
101  return _widget;
102 }
103 
104 /**
105  * Returns the height of the label as we requested it.
106  */
107 int GtkStatsLabel::
108 get_height() const {
109  return _height;
110 }
111 
112 /**
113  * Returns the collector this label represents.
114  */
115 int GtkStatsLabel::
117  return _collector_index;
118 }
119 
120 /**
121  * Returns the thread index.
122  */
123 int GtkStatsLabel::
125  return _thread_index;
126 }
127 
128 /**
129  * Enables or disables the visual highlight for this label.
130  */
131 void GtkStatsLabel::
132 set_highlight(bool highlight) {
133  if (_highlight != highlight) {
134  _highlight = highlight;
135  gtk_widget_queue_draw(_widget);
136  }
137 }
138 
139 /**
140  * Returns true if the visual highlight for this label is enabled.
141  */
142 bool GtkStatsLabel::
143 get_highlight() const {
144  return _highlight;
145 }
146 
147 /**
148  * Used internally to indicate whether the mouse is within the label's widget.
149  */
150 void GtkStatsLabel::
151 set_mouse_within(bool mouse_within) {
152  if (_mouse_within != mouse_within) {
153  _mouse_within = mouse_within;
154  gtk_widget_queue_draw(_widget);
155  }
156 }
157 
158 /**
159  * Draws the background color of the label.
160  */
161 gboolean GtkStatsLabel::
162 expose_event_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
163  GtkStatsLabel *self = (GtkStatsLabel *)data;
164 
165  GdkGC *gc = gdk_gc_new(widget->window);
166  gdk_gc_set_rgb_fg_color(gc, &self->_bg_color);
167 
168  gdk_draw_rectangle(widget->window, gc, TRUE, 0, 0,
169  widget->allocation.width, widget->allocation.height);
170 
171  // Center the text within the rectangle.
172  int width, height;
173  pango_layout_get_pixel_size(self->_layout, &width, &height);
174 
175  gdk_gc_set_rgb_fg_color(gc, &self->_fg_color);
176  gdk_draw_layout(widget->window, gc,
177  (widget->allocation.width - width) / 2, 0,
178  self->_layout);
179 
180  // Now draw the highlight rectangle, if any.
181  if (self->_highlight || self->_mouse_within) {
182  gdk_draw_rectangle(widget->window, gc, FALSE, 0, 0,
183  widget->allocation.width - 1, widget->allocation.height - 1);
184  }
185 
186  g_object_unref(gc);
187  return TRUE;
188 }
189 
190 /**
191  * Called when the mouse enters the label region
192  */
193 gboolean GtkStatsLabel::
194 enter_notify_event_callback(GtkWidget *widget, GdkEventCrossing *event,
195  gpointer data) {
196  GtkStatsLabel *self = (GtkStatsLabel *)data;
197  self->set_mouse_within(true);
198  return TRUE;
199 }
200 
201 /**
202  * Called when the mouse leaves the label region
203  */
204 gboolean GtkStatsLabel::
205 leave_notify_event_callback(GtkWidget *widget, GdkEventCrossing *event,
206  gpointer data) {
207  GtkStatsLabel *self = (GtkStatsLabel *)data;
208  self->set_mouse_within(false);
209  return TRUE;
210 }
211 
212 /**
213  * Called when the mouse button is depressed within the label.
214  */
215 gboolean GtkStatsLabel::
216 button_press_event_callback(GtkWidget *widget, GdkEventButton *event,
217  gpointer data) {
218  GtkStatsLabel *self = (GtkStatsLabel *)data;
219  bool double_click = (event->type == GDK_2BUTTON_PRESS);
220  if (double_click) {
221  self->_graph->clicked_label(self->_collector_index);
222  }
223  return TRUE;
224 }
int get_height() const
Returns the height of the label as we requested it.
bool get_highlight() const
Returns true if the visual highlight for this label is enabled.
const LRGBColor & get_collector_color(int collector_index)
Returns the color associated with the indicated collector.
std::string get_collector_fullname(int index) const
Returns the "full name" of the indicated collector.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void clicked_label(int collector_index)
Called when the user single-clicks on a label.
std::string get_collector_name(int index) const
Returns the name of the indicated collector.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int get_thread_index() const
Returns the thread index.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is just an abstract base class to provide a common pointer type for the various kinds of graphs ...
Definition: gtkStatsGraph.h:29
void set_highlight(bool highlight)
Enables or disables the visual highlight for this label.
This class represents a connection to a PStatsClient and manages the data exchange with the client.
int get_collector_index() const
Returns the collector this label represents.
GtkWidget * get_widget() const
Returns the widget for this label.
A text label that will draw in color appropriate for a particular collector.
Definition: gtkStatsLabel.h:29
const PStatClientData * get_client_data() const
Returns the client data associated with this monitor.
Definition: pStatMonitor.I:26