Re: Implementing a LED-like widget



Marcus Lundblad wrote:



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:

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

void
desk_indicator::led_area::on_realize()
{

      //This should take care of your SIGSEGV
      Gtk::DrawingArea::on_realize();
      ///////////////////////////////////////////////////////////

   Glib::RefPtr<Gdk::Window> win = get_window();
   gc = Gdk::GC::create(win);

//I'm not sure about this, but i think you should specify a colormap for your GC
      gc->set_colormap(get_default_colormap());
////////////////////////////////////////////////////////////////////////

   gc->set_foreground(Gdk::Color("Yellow"));
}

Hope this helps,
Matthias Langer (alias Antonio)




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