Re: Implementing a LED-like widget





On Tue, 23 Aug 2005, Antonio Coralles wrote:

Marcus Lundblad wrote:

I'm trying to implement a widget simulating a LED.
What I have is a widget derived from Gtk::Frame (with style SHADOW_IN).
This frame holds a Gtk::DrawingArea (display in the follwing code..)

I have the following code intended to turn the inside of the widget yellow
(as an example).

--------------------------------

Constructor contains this:

   set_shadow_type(Gtk::SHADOW_IN);
   set_size_request(10,10);
   display = new Gtk::DrawingArea;
   display->set_size_request(8,8);
   add(*display);

As far as I can tell your constructor should be ok ...


The method setting the LED in on-state:

From the nature of the code, I think you are calling this in on_realize() ...


     int width, height;
     Glib::RefPtr<Gdk::Window> win = display->get_window();
     win->get_size(width, height);
     Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(win);
     gc->set_foreground(Gdk::Color("Yellow"));
     win->draw_rectangle(gc, true, 0, 0, width, height);

But as far as I know, you should only do

//////////////////////////////////////////////////////////////////////////////////////////////////
Glib::RefPtr<Gdk::Window> win = display->get_window();
Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(win);
gc->set_foreground(Gdk::Color("Yellow"));
//////////////////////////////////////////////////////////////////////////////////////////////////

in your overiden version of Gtk::DrawingArea::on_realize().
Assuming that gc is a member variable, calling

//////////////////////////////////////////////////////////////////////////////////////////////////
int width, height;
Glib::RefPtr<Gdk::Window> win = display->get_window();
win->get_size(width, height);
win->draw_rectangle(gc, true, 0, 0, width, height);
/////////////////////////////////////////////////////////////////////////////////////////////////
from your version of Gtk::DrawingArea::on_expose_event(GdkEventExpose*)
should have the desired effect.

Antonio


Thanks for your help, but it's still not working correctly.
Now I have the following:

-----------------------------------------------------------

desk_indicator.hh:

#include <gtkmm.h>
#include <gdkmm.h>

#ifndef DESK_INDICATOR
#define DESK_INDICATOR

class desk_indicator : public Gtk::Frame {
public:
  desk_indicator();
  ~desk_indicator();

  void set_active(bool active = true);
  bool active() const { return is_active; }

private:

  class led_area : public Gtk::DrawingArea {
  public:
    led_area();
    void set_active(bool active = true) { this->is_active = active; }
    bool active() const { return is_active; }

    void on_realize();
    bool on_expose_event(GdkEventExpose* ev);

  private:
    bool is_active;
    Glib::RefPtr<Gdk::GC> gc;

  };

  bool is_active;

  led_area led;
};

#endif //DESK_INDICATOR

------------------------------------------------------------------
desk_indicator.cc:

#include "desk_indicator.hh"

#include <gdkmm.h>
#include <iostream>

desk_indicator::desk_indicator()
{
  std::cerr << "desk_indicator::desk_indicator" << std::endl;
  set_shadow_type(Gtk::SHADOW_IN);
  set_size_request(10,10);
  add(led);
  is_active = false;
}

desk_indicator::~desk_indicator()
{
}

void
desk_indicator::set_active(bool active)
{
  this->is_active = active;
  led.set_active(active);
}

desk_indicator::led_area::led_area()
{
  is_active = false;
  set_size_request(10, 10);
}

void
desk_indicator::led_area::on_realize()
{
  Glib::RefPtr<Gdk::Window> win = get_window();
  gc = Gdk::GC::create(win);
  gc->set_foreground(Gdk::Color("Yellow"));
}

bool
desk_indicator::led_area::on_expose_event(GdkEventExpose* event)
{
  std::cerr << "desk_indicator::on_expose_event" << std::endl;
  if(active()) {
    int width, height;
    Glib::RefPtr<Gdk::Window> win = get_window();
    win->get_size(width, height);
    win->draw_rectangle(gc, true, 0, 0, width, height);
  }
  return true;
}

---------------------------------------------------------------------

but now the code segfaults in desk_indicator::led_area::on_realize
at the assignment to gc inside the GC::create function.

I use gtkmm 2.4.11-2 under Debian GNU/Linux kernel 2.6.8, if this information would be useful.

Output from GDB follows:

Program received signal SIGSEGV, Segmentation fault.
0x402d1d0b in Gdk::GC::GC () from /usr/lib/libgdkmm-2.4.so.1
(gdb) bt
#0  0x402d1d0b in Gdk::GC::GC () from /usr/lib/libgdkmm-2.4.so.1
#1  0x402d26b7 in Gdk::GC::create () from /usr/lib/libgdkmm-2.4.so.1
#2  0x080610ba in desk_indicator::led_area::on_realize (this=0x80ccd5c)
    at desk_indicator.cc:36
#3  0x401f979f in Gtk::Widget_Class::realize_callback ()
   from /usr/lib/libgtkmm-2.4.so.1
#4  0x407972a6 in g_cclosure_marshal_VOID__VOID ()
   from /usr/lib/libgobject-2.0.so.0
#5  0x407859c9 in g_cclosure_new_swap () from /usr/lib/libgobject-2.0.so.0
#6  0x40785736 in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#7 0x40796651 in g_signal_emit_by_name () from /usr/lib/libgobject-2.0.so.0 #8 0x40795e9c in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#9  0x40796126 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#10 0x4053dfcf in gtk_widget_realize () from /usr/lib/libgtk-x11-2.0.so.0
#11 0x4053de1c in gtk_widget_map () from /usr/lib/libgtk-x11-2.0.so.0
#12 0x403c6c2c in gtk_container_get_focus_hadjustment ()
   from /usr/lib/libgtk-x11-2.0.so.0
#13 0x4041225a in gtk_frame_new () from /usr/lib/libgtk-x11-2.0.so.0
#14 0x4016c1ee in Gtk::Container::forall_vfunc ()
   from /usr/lib/libgtkmm-2.4.so.1
#15 0x4016a9c3 in Gtk::Container_Class::forall_vfunc_callback ()
   from /usr/lib/libgtkmm-2.4.so.1
#16 0x403c500d in gtk_container_forall () from /usr/lib/libgtk-x11-2.0.so.0
#17 0x403c6c69 in gtk_container_get_focus_hadjustment ()
   from /usr/lib/libgtk-x11-2.0.so.0
#18 0x40201be3 in Gtk::Widget::on_map () from /usr/lib/libgtkmm-2.4.so.1
#19 0x401f961f in Gtk::Widget_Class::map_callback ()
   from /usr/lib/libgtkmm-2.4.so.1
#20 0x407972a6 in g_cclosure_marshal_VOID__VOID ()
   from /usr/lib/libgobject-2.0.so.0
#21 0x407859c9 in g_cclosure_new_swap () from /usr/lib/libgobject-2.0.so.0
#22 0x40785736 in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#23 0x40796651 in g_signal_emit_by_name () from /usr/lib/libgobject-2.0.so.0 #24 0x40795e9c in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#25 0x40796126 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#26 0x4053dde5 in gtk_widget_map () from /usr/lib/libgtk-x11-2.0.so.0
#27 0x403c6c2c in gtk_container_get_focus_hadjustment ()
   from /usr/lib/libgtk-x11-2.0.so.0
#28 0x40388ff5 in gtk_box_set_child_packing ()
   from /usr/lib/libgtk-x11-2.0.so.0
#29 0x4016a95e in Gtk::Container_Class::forall_vfunc_callback ()
   from /usr/lib/libgtkmm-2.4.so.1
#30 0x403c500d in gtk_container_forall () from /usr/lib/libgtk-x11-2.0.so.0
#31 0x403c6c69 in gtk_container_get_focus_hadjustment ()
   from /usr/lib/libgtk-x11-2.0.so.0
#32 0x401f95d6 in Gtk::Widget_Class::map_callback ()
   from /usr/lib/libgtkmm-2.4.so.1
#33 0x407972a6 in g_cclosure_marshal_VOID__VOID ()
   from /usr/lib/libgobject-2.0.so.0
---Type <return> to continue, or q <return> to quit---




//Marcus




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]