Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/gtk-stats/gtkStatsLabel.cxx

Go to the documentation of this file.
00001 // Filename: gtkStatsLabel.cxx
00002 // Created by:  drose (15Jul00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "gtkStatsLabel.h"
00020 #include "gtkStatsMonitor.h"
00021 
00022 #include <pStatClientData.h>
00023 #include <pStatMonitor.h>
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: GtkStatsLabel::Constructor
00027 //       Access: Public
00028 //  Description: This constructor automatically figures out the
00029 //               appropriate name and color for the label.
00030 ////////////////////////////////////////////////////////////////////
00031 GtkStatsLabel::
00032 GtkStatsLabel(PStatMonitor *monitor, int collector_index,
00033               Gdk_Font font) :
00034   _collector_index(collector_index),
00035   _font(font)
00036 {
00037   set_events(GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
00038 
00039   _text = monitor->get_client_data()->get_collector_name(_collector_index);
00040   RGBColorf rgb = monitor->get_collector_color(_collector_index);
00041   _bg_color.set_rgb_p(rgb[0], rgb[1], rgb[2]);
00042 
00043   // Should our foreground be black or white?
00044   float bright =
00045     rgb[0] * 0.299 +
00046     rgb[1] * 0.587 +
00047     rgb[2] * 0.114;
00048 
00049   if (bright >= 0.5) {
00050     _fg_color.set_rgb_p(0, 0, 0);
00051   } else {
00052     _fg_color.set_rgb_p(1, 1, 1);
00053   }
00054 
00055   Gdk_Colormap::get_system().alloc(_fg_color);
00056   Gdk_Colormap::get_system().alloc(_bg_color);
00057 
00058   int text_width = _font.string_width(_text);
00059   int text_height = _font.height();
00060 
00061   _height = text_height + 4;
00062   _width = text_width + 4;
00063 
00064   set_usize(_width, _height);
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: GtkStatsLabel::get_width
00069 //       Access: Public
00070 //  Description: Returns the width of the widget as we requested it.
00071 ////////////////////////////////////////////////////////////////////
00072 int GtkStatsLabel::
00073 get_width() const {
00074   return _width;
00075 }
00076 
00077 ////////////////////////////////////////////////////////////////////
00078 //     Function: GtkStatsLabel::get_height
00079 //       Access: Public
00080 //  Description: Returns the height of the widget as we requested it.
00081 ////////////////////////////////////////////////////////////////////
00082 int GtkStatsLabel::
00083 get_height() const {
00084   return _height;
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////
00088 //     Function: GtkStatsLabel::configure_event_impl
00089 //       Access: Private, Virtual
00090 //  Description: Creates a new backing pixmap of the appropriate size.
00091 ////////////////////////////////////////////////////////////////////
00092 gint GtkStatsLabel::
00093 configure_event_impl(GdkEventConfigure *) {
00094   Gdk_Window window = get_window();
00095 
00096   _gc = Gdk_GC(window);
00097   _gc.set_foreground(_fg_color);
00098   _gc.set_background(_bg_color);
00099   _gc.set_font(_font);
00100 
00101   _reverse_gc = Gdk_GC(window);
00102   _reverse_gc.set_foreground(_bg_color);
00103   _reverse_gc.set_background(_fg_color);
00104   _reverse_gc.set_font(_font);
00105 
00106   return true;
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: GtkStatsLabel::expose_event_impl
00111 //       Access: Private, Virtual
00112 //  Description: Redraw the text.  We don't bother with clipping
00113 //               regions here, but just draw the whole text every
00114 //               time.
00115 ////////////////////////////////////////////////////////////////////
00116 gint GtkStatsLabel::
00117 expose_event_impl(GdkEventExpose *event) {
00118   int text_width = _font.string_width(_text);
00119   int text_height = _font.height();
00120 
00121   Gdk_Window window = get_window();
00122 
00123   window.draw_rectangle(_reverse_gc, true, 0, 0, width(), height());
00124   window.draw_string(_font, _gc, width() - text_width - 2,
00125                       height() - (height() - text_height) / 2 - _font.descent(),
00126                       _text);
00127   return false;
00128 }
00129 
00130 ////////////////////////////////////////////////////////////////////
00131 //     Function: GtkStatsLabel::button_press_event_impl
00132 //       Access: Private, Virtual
00133 //  Description:
00134 ////////////////////////////////////////////////////////////////////
00135 gint GtkStatsLabel::
00136 button_press_event_impl(GdkEventButton *button) {
00137   if (button->type == GDK_2BUTTON_PRESS && button->button == 1) {
00138     collector_picked(_collector_index);
00139     return true;
00140   }
00141   return false;
00142 }

Generated on Fri May 2 03:20:00 2003 for Panda-Tool by doxygen1.3