diff --git a/pandatool/src/gtk-stats/gtkStatsGraph.cxx b/pandatool/src/gtk-stats/gtkStatsGraph.cxx index 9c3da2c19d..54e1e01143 100755 --- a/pandatool/src/gtk-stats/gtkStatsGraph.cxx +++ b/pandatool/src/gtk-stats/gtkStatsGraph.cxx @@ -260,7 +260,7 @@ get_collector_gc(int collector_index) { c.green = (int)(rgb[1] * 65535.0f); c.blue = (int)(rgb[2] * 65535.0f); GdkGC *gc = gdk_gc_new(_pixmap); - g_object_ref(gc); + // g_object_ref(gc); // Should this be ref_sink? gdk_gc_set_rgb_fg_color(gc, &c); _brushes[collector_index] = gc; @@ -311,9 +311,9 @@ setup_pixmap(int xsize, int ysize) { _pixmap_ysize = max(ysize, 0); _pixmap = gdk_pixmap_new(_graph_window->window, _pixmap_xsize, _pixmap_ysize, -1); - g_object_ref(_pixmap); + // g_object_ref(_pixmap); // Should this be ref_sink? _pixmap_gc = gdk_gc_new(_pixmap); - g_object_ref(_pixmap_gc); + // g_object_ref(_pixmap_gc); // Should this be ref_sink? gdk_gc_set_rgb_fg_color(_pixmap_gc, &rgb_white); gdk_draw_rectangle(_pixmap, _pixmap_gc, TRUE, 0, 0, diff --git a/pandatool/src/gtk-stats/gtkStatsLabel.cxx b/pandatool/src/gtk-stats/gtkStatsLabel.cxx index 1e12a246f4..853920bcbe 100644 --- a/pandatool/src/gtk-stats/gtkStatsLabel.cxx +++ b/pandatool/src/gtk-stats/gtkStatsLabel.cxx @@ -45,13 +45,29 @@ GtkStatsLabel(GtkStatsMonitor *monitor, GtkStatsGraph *graph, _text = _monitor->get_client_data()->get_collector_name(_collector_index); } - /* + _widget = gtk_drawing_area_new(); + gtk_widget_add_events(_widget, + GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | + GDK_BUTTON_PRESS_MASK); + g_signal_connect(G_OBJECT(_widget), "expose_event", + G_CALLBACK(expose_event_callback), this); + g_signal_connect(G_OBJECT(_widget), "enter_notify_event", + G_CALLBACK(enter_notify_event_callback), this); + g_signal_connect(G_OBJECT(_widget), "leave_notify_event", + G_CALLBACK(leave_notify_event_callback), this); + g_signal_connect(G_OBJECT(_widget), "button_press_event", + G_CALLBACK(button_press_event_callback), this); + + gtk_widget_show(_widget); + + // Make up a PangoLayout to represent the text. + _layout = gtk_widget_create_pango_layout(_widget, _text.c_str()); + + // Set the fg and bg colors on the label. RGBColorf rgb = _monitor->get_collector_color(_collector_index); - int r = (int)(rgb[0] * 255.0f); - int g = (int)(rgb[1] * 255.0f); - int b = (int)(rgb[2] * 255.0f); - _bg_color = RGB(r, g, b); - _bg_brush = CreateSolidBrush(RGB(r, g, b)); + _bg_color.red = (int)(rgb[0] * 65535.0f); + _bg_color.green = (int)(rgb[1] * 65535.0f); + _bg_color.blue = (int)(rgb[2] * 65535.0f); // Should our foreground be black or white? float bright = @@ -60,13 +76,16 @@ GtkStatsLabel(GtkStatsMonitor *monitor, GtkStatsGraph *graph, rgb[2] * 0.114; if (bright >= 0.5) { - _fg_color = RGB(0, 0, 0); - _highlight_brush = (HBRUSH)GetStockObject(BLACK_BRUSH); + _fg_color.red = _fg_color.green = _fg_color.blue = 0; } else { - _fg_color = RGB(255, 255, 255); - _highlight_brush = (HBRUSH)GetStockObject(WHITE_BRUSH); + _fg_color.red = _fg_color.green = _fg_color.blue = 0xffff; } - */ + + // What are the extents of the text? This determines the minimum + // size of our widget. + int width, height; + pango_layout_get_pixel_size(_layout, &width, &height); + gtk_widget_set_size_request(_widget, width + 8, height); _highlight = false; _mouse_within = false; @@ -82,22 +101,6 @@ GtkStatsLabel:: // DeleteObject(_bg_brush); } -//////////////////////////////////////////////////////////////////// -// Function: GtkStatsLabel::setup -// Access: Public -// Description: Creates the actual widget. -//////////////////////////////////////////////////////////////////// -GtkWidget *GtkStatsLabel:: -setup() { - _widget = gtk_event_box_new(); - GtkWidget *label = gtk_label_new(_text.c_str()); - gtk_container_add(GTK_CONTAINER(_widget), label); - gtk_widget_show(label); - gtk_widget_show(_widget); - - return _widget; -} - //////////////////////////////////////////////////////////////////// // Function: GtkStatsLabel::get_widget // Access: Public @@ -128,9 +131,7 @@ void GtkStatsLabel:: set_highlight(bool highlight) { if (_highlight != highlight) { _highlight = highlight; - /* - InvalidateRect(_widget, NULL, TRUE); - */ + gtk_widget_queue_draw(_widget); } } @@ -155,8 +156,77 @@ void GtkStatsLabel:: set_mouse_within(bool mouse_within) { if (_mouse_within != mouse_within) { _mouse_within = mouse_within; - /* - InvalidateRect(_widget, NULL, TRUE); - */ + gtk_widget_queue_draw(_widget); } } + +//////////////////////////////////////////////////////////////////// +// Function: GtkStatsLabel::expose_event_callback +// Access: Private, Static +// Description: Draws the background color of the label. +//////////////////////////////////////////////////////////////////// +gboolean GtkStatsLabel:: +expose_event_callback(GtkWidget *widget, GdkEventExpose *event, gpointer data) { + GtkStatsLabel *self = (GtkStatsLabel *)data; + + GdkGC *gc = gdk_gc_new(widget->window); + gdk_gc_set_rgb_fg_color(gc, &self->_bg_color); + + gdk_draw_rectangle(widget->window, gc, TRUE, 0, 0, + widget->allocation.width, widget->allocation.height); + + // Center the text within the rectangle. + int width, height; + pango_layout_get_pixel_size(self->_layout, &width, &height); + + gdk_gc_set_rgb_fg_color(gc, &self->_fg_color); + gdk_draw_layout(widget->window, gc, + (widget->allocation.width - width) / 2, 0, + self->_layout); + + // Now draw the highlight rectangle, if any. + if (self->_highlight || self->_mouse_within) { + gdk_draw_rectangle(widget->window, gc, FALSE, 0, 0, + widget->allocation.width - 1, widget->allocation.height - 1); + } + + g_object_unref(gc); + return TRUE; +} + +//////////////////////////////////////////////////////////////////// +// Function: GtkStatsLabel::enter_notify_event_callback +// Access: Private, Static +// Description: Called when the mouse enters the label region +//////////////////////////////////////////////////////////////////// +gboolean GtkStatsLabel:: +enter_notify_event_callback(GtkWidget *widget, GdkEventCrossing *event, + gpointer data) { + GtkStatsLabel *self = (GtkStatsLabel *)data; + self->set_mouse_within(true); + return TRUE; +} + +//////////////////////////////////////////////////////////////////// +// Function: GtkStatsLabel::leave_notify_event_callback +// Access: Private, Static +// Description: Called when the mouse leaves the label region +//////////////////////////////////////////////////////////////////// +gboolean GtkStatsLabel:: +leave_notify_event_callback(GtkWidget *widget, GdkEventCrossing *event, + gpointer data) { + GtkStatsLabel *self = (GtkStatsLabel *)data; + self->set_mouse_within(false); + return TRUE; +} + +//////////////////////////////////////////////////////////////////// +// Function: GtkStatsLabel::button_press_event_callback +// Access: Private, Static +// Description: Called when the mouse leaves the label region +//////////////////////////////////////////////////////////////////// +gboolean GtkStatsLabel:: +button_press_event_callback(GtkWidget *widget, GdkEventButton *event, + gpointer data) { + return TRUE; +} diff --git a/pandatool/src/gtk-stats/gtkStatsLabel.h b/pandatool/src/gtk-stats/gtkStatsLabel.h index 27960877b5..f81f1d1b3e 100644 --- a/pandatool/src/gtk-stats/gtkStatsLabel.h +++ b/pandatool/src/gtk-stats/gtkStatsLabel.h @@ -39,7 +39,6 @@ public: int thread_index, int collector_index, bool use_fullname); ~GtkStatsLabel(); - GtkWidget *setup(); GtkWidget *get_widget() const; int get_collector_index() const; @@ -49,6 +48,17 @@ public: private: void set_mouse_within(bool mouse_within); + static gboolean expose_event_callback(GtkWidget *widget, + GdkEventExpose *event, gpointer data); + static gboolean enter_notify_event_callback(GtkWidget *widget, + GdkEventCrossing *event, + gpointer data); + static gboolean leave_notify_event_callback(GtkWidget *widget, + GdkEventCrossing *event, + gpointer data); + static gboolean button_press_event_callback(GtkWidget *widget, + GdkEventButton *event, + gpointer data); GtkStatsMonitor *_monitor; GtkStatsGraph *_graph; @@ -56,6 +66,9 @@ private: int _collector_index; string _text; GtkWidget *_widget; + GdkColor _fg_color; + GdkColor _bg_color; + PangoLayout *_layout; /* COLORREF _bg_color; diff --git a/pandatool/src/gtk-stats/gtkStatsLabelStack.cxx b/pandatool/src/gtk-stats/gtkStatsLabelStack.cxx index f1183f091b..d38d9df5b6 100755 --- a/pandatool/src/gtk-stats/gtkStatsLabelStack.cxx +++ b/pandatool/src/gtk-stats/gtkStatsLabelStack.cxx @@ -27,8 +27,7 @@ //////////////////////////////////////////////////////////////////// GtkStatsLabelStack:: GtkStatsLabelStack() { - _widget = NULL; - + _widget = gtk_vbox_new(FALSE, 0); _highlight_label = -1; } @@ -43,30 +42,15 @@ GtkStatsLabelStack:: } //////////////////////////////////////////////////////////////////// -// Function: GtkStatsLabelStack::setup +// Function: GtkStatsLabelStack::get_widget // Access: Public -// Description: Creates the actual widget object. +// Description: Returns the widget for this stack. //////////////////////////////////////////////////////////////////// GtkWidget *GtkStatsLabelStack:: -setup() { - if (_widget == NULL) { - _widget = gtk_vbox_new(FALSE, 0); - } - +get_widget() const { return _widget; } -//////////////////////////////////////////////////////////////////// -// Function: GtkStatsLabelStack::is_setup -// Access: Public -// Description: Returns true if the label stack has been set up, -// false otherwise. -//////////////////////////////////////////////////////////////////// -bool GtkStatsLabelStack:: -is_setup() const { - return (_widget != NULL); -} - //////////////////////////////////////////////////////////////////// // Function: GtkStatsLabelStack::get_label_collector_index // Access: Public @@ -107,8 +91,8 @@ add_label(GtkStatsMonitor *monitor, GtkStatsGraph *graph, GtkStatsLabel *label = new GtkStatsLabel(monitor, graph, thread_index, collector_index, use_fullname); - gtk_box_pack_start(GTK_BOX(_widget), label->setup(), - FALSE, FALSE, 0); + gtk_box_pack_end(GTK_BOX(_widget), label->get_widget(), + FALSE, FALSE, 0); int label_index = (int)_labels.size(); _labels.push_back(label); diff --git a/pandatool/src/gtk-stats/gtkStatsLabelStack.h b/pandatool/src/gtk-stats/gtkStatsLabelStack.h index 91622aa314..4e8563d218 100755 --- a/pandatool/src/gtk-stats/gtkStatsLabelStack.h +++ b/pandatool/src/gtk-stats/gtkStatsLabelStack.h @@ -38,8 +38,7 @@ public: GtkStatsLabelStack(); ~GtkStatsLabelStack(); - GtkWidget *setup(); - bool is_setup() const; + GtkWidget *get_widget() const; int get_label_collector_index(int label_index) const; diff --git a/pandatool/src/gtk-stats/gtkStatsMonitor.cxx b/pandatool/src/gtk-stats/gtkStatsMonitor.cxx index 0baa01af63..113bfaa490 100644 --- a/pandatool/src/gtk-stats/gtkStatsMonitor.cxx +++ b/pandatool/src/gtk-stats/gtkStatsMonitor.cxx @@ -91,8 +91,8 @@ GtkStatsMonitor:: } #ifdef DEVELOP_GTKSTATS - // For Gtkstats developers, exit when the first monitor closes. - gtk_main_quit(0); + // For GtkStats developers, exit when the first monitor closes. + gtk_main_quit(); #endif } diff --git a/pandatool/src/gtk-stats/gtkStatsStripChart.cxx b/pandatool/src/gtk-stats/gtkStatsStripChart.cxx index cfb2fd18fa..c3c7468bd5 100644 --- a/pandatool/src/gtk-stats/gtkStatsStripChart.cxx +++ b/pandatool/src/gtk-stats/gtkStatsStripChart.cxx @@ -62,10 +62,10 @@ GtkStatsStripChart(GtkStatsMonitor *monitor, int thread_index, _smooth_check_box = 0; GtkWidget *hbox = gtk_hbox_new(FALSE, 0); - gtk_box_pack_start(GTK_BOX(hbox), _label_stack.setup(), + gtk_box_pack_start(GTK_BOX(hbox), _label_stack.get_widget(), FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(hbox), _graph_window, - FALSE, FALSE, 0); + TRUE, TRUE, 0); gtk_container_add(GTK_CONTAINER(_window), hbox); @@ -237,10 +237,7 @@ void GtkStatsStripChart:: set_vertical_scale(float value_height) { PStatStripChart::set_vertical_scale(value_height); - GdkRectangle rect = { - 0, 0, get_xsize(), get_ysize() - }; - gdk_window_invalidate_rect(_graph_window->window, &rect, FALSE); + gtk_widget_queue_draw(_graph_window); } ////////////////////////////////////////////////////////////////////