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