Re: Implementing a LED-like widget



Marcus Lundblad wrote:
-----------------------------------------------------------

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;
}

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

Marcus, I've been working a lot with Gtk::DrawingArea widgets lately and I have been looking at your code. I noticed in this example here you never explicitly show() the led object. Did I miss where that is happening?

Also I've never tried to use a temporary Gdk::Color object in the set_foreground() call. I've always used alloc_color() from the colormap object (my method of getting the colormap is for an old 2.4 bug):

	_colormap = Gdk::Screen::get_default()->get_default_colormap();
	_bgcolor.set_rgb(43690, 43690, 43690);
	_colormap->alloc_color(_bgcolor);

and then called free_colors() when I am done:

	_colormap->free_colors(_bgcolor, 1);

When you step through your code with GDB, how much of your widgets get shown?



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