[gtkmm] Problems adding a custom widget



I'm having some problems when I try to add a custom drawingarea into a container (I've try with Hbox and Hpaned/Vpaned with poor results).

I've a custom drawingarea with this code:

drawingarea_sonda.hh
--------------------

#ifndef _DRAWINGAREA_SONDA_HH
#define _DRAWINGAREA_SONDA_HH

#include <iostream>
#include <sys/shm.h>
#include <glib-2.0/glib.h>
#include <math.h>
#include <stdio.h>

#include <gtkmm/drawingarea.h>

// Numero de colores del array de colores
#define NUM_COLOR_SONDA 6

class drawingarea_sonda : public Gtk::DrawingArea
{
public:

 // Variables para pintar
 Glib::RefPtr<Gdk::GC> gc_;
 Gdk::Color fg,bg;
 Glib::RefPtr<Gdk::Window> window;
 Glib::RefPtr < Gdk::Colormap > colormap;

 Gdk::Color colores[NUM_COLOR_SONDA]; // Colores varios

 // Para guardar la imagen y evitar el parpadeo
 Glib::RefPtr<Gdk::Pixmap> pixmap;

 // window geometry: x, y, width, height, depth
 gint winx,winy,winw,winh,wind;

 // Funcion a q a partir del osciloscopio genera el pixmap
 gint configurar_sonda(void);

 // Para mandar repintar segun los datos almacenados
 gint repintar(void);

 // Funcion q entrega el window por el q se realiza la visualizacion
 gint entregar_window(Glib::RefPtr<Gdk::Window> window);

 // Funcion q se llama cuando se muestra por primero vez el objeto
 void on_drawingarea_sonda_realize();

 // Funcion q se llama cuando algo se quita de encima
 bool on_drawingarea_sonda_expose_event(GdkEventExpose*);

protected:

private:

 // Funcion q permite crear el pixmap del objeto
 gint crea_pixmap();

 // Funcion para el tratamiento de errores
 gint error(gchar *);

};
#endif



drawingarea_sonda.cc
--------------------

#include "config.h"
#include "drawingarea_sonda.hh"

/* ********************
  entregar_window
  ***************** */
// Funcion q entrega el window por el q se va ha visualizar
gint drawingarea_sonda::entregar_window(Glib::RefPtr<Gdk::Window> window)
{
 // Comprobamos q el window esa definido
 if(!window)
 {
   this->error("entregar_window : Window no definida");
   return -1;
 }

 // El window es correcto
 this->window=window;

 return 0; // Todo bien
} // Fin de entregar_window

/* **********************************
  on_drawingarea_sonda_realize
  ******************************* */
// Funcion q se ejecuta al visualizarse por primera vez la drawingarea
void drawingarea_sonda::on_drawingarea_sonda_realize()
{
 // Esta funcion permite configurar el entorno grafico del objeto

 // We need to call the base on_realize()
 Gtk::DrawingArea::on_realize ();

 fprintf(stderr,"\nDENTRO DE on_drawingarea_sonda_realize\n\n");

 if(!this->window)
 {
   // we need a ref to the gdkmm window
   this->window = this->get_window ();
 }
 if(!this->window)
 {
   this->error("on_drawingarea_sonda_realize : Window no definida");
   return;
 }

this->window->get_geometry (this->winx,this->winy,this->winw,this->winh,this->wind);

 this->gc_ = Gdk::GC::create (this->window);
 this->colormap = get_default_colormap ();

 gint i=0;
 this->colores[i] = Gdk::Color ("white"); // Definimos un color del array
 this->colormap->alloc_color (this->colores[i]);
 i++;
 this->colores[i] = Gdk::Color ("blue"); // Definimos un color del array
 this->colormap->alloc_color (this->colores[i]);
 i++;
 this->colores[i] = Gdk::Color ("green"); // Definimos un color del array
 this->colormap->alloc_color (this->colores[i]);
 i++;
 this->colores[i] = Gdk::Color ("red"); // Definimos un color del array
 this->colormap->alloc_color (this->colores[i]);
 i++;
 this->colores[i] = Gdk::Color ("black"); // Definimos un color del array
 this->colormap->alloc_color (this->colores[i]);
 i++;
 this->colores[i] = Gdk::Color ("orange"); // Definimos un color del array
 this->colormap->alloc_color (this->colores[i]);
 i++;

 this->fg = this->colores[0]; // Definimos el color activo
 this->bg = this->colores[1]; // Definimos el color de fondo

 this->gc_->set_foreground(this->fg);
 this->gc_->set_background(this->bg);

 // Creamos el pixmap necesario para evitar el parpadeo
 this->crea_pixmap();
} // Fin de on_drawingarea_sonda_realize

/* ****************************************
  on_drawingarea_sonda_expose_event
  ************************************* */
// Funcion q se ejecuta cuando algo se quita de encima
bool drawingarea_sonda::on_drawingarea_sonda_expose_event(GdkEventExpose * ev)
{
 this->repintar(); // Repintamos
 return 0; // Permitimos q el evento suba al siguiente nivel
} // Fin de drawingarea_sonda_expose_event

/* *****************
  crea_pixmap
  ************** */
// Funcion q crea el pixmap necesario para realizar la visualizacion
gint drawingarea_sonda::crea_pixmap()
{
 // Comprobamos q los datos necesarios existen y son correctos
 if(!this->window)
 {
this->error("crea_pixmap: ERROR no se puede crear pixmap, window no definido");
   return -1; // Window no definido
 }
 if((this->winw<=0)||(this->winh<=0))
 {
this->error("crea_pixmap: ERROR no se puede crear pixmap, tamanyos incorrectos");
   return -2; // Tamaños del pixmap incorrectos
 }
 if(this->colormap==0)
 {
this->error("crea_pixmap: ERROR no se puede crear pixmap, colormap no definido");
   return -3; // Colormap no definido
 }

 // Definimos la imagen
 // La imagen es del tamanyo de la ventana
this->pixmap = Gdk::Pixmap::create(this->window,this->winw,this->winh,this->window->get_depth());

 // Le ponemos un colormap especifico
 this->pixmap->set_colormap(this->colormap);

 return 0; // Todo bien
} // Fin de crea_pixmap

/* ********************
  configurar_sonda
  ***************** */
// Funcion q a partir del osciloscopio genera el pixmap
gint drawingarea_sonda::configurar_sonda(void)
{
 // Comprobamos posibles errores
 // del entorno grafico
 if(!this->window)
 {
   this->on_drawingarea_sonda_realize();
 }
 if(!this->window)
 {
   this->error("configurar_sonda : Window no definido");
   return -1;
 }
 if(this->pixmap==0)
 {
   this->error("configurar_sonda : Pixmap no definido");
   return -2;
 }
 if(this->gc_==0)
 {
   this->error("configurar_sonda : GC no definido");
   return -3;
 }
 if((this->winw<=0)||(this->winw<=0))
 {
   this->error("configurar_sonda : Tamanyos incorrectos");
   return -4;
 }

 // ATENCION : Solo ponemos a blanco

 // Limpiamos el pixmap
this->gc_->set_foreground(this->bg); // Ponemos el color activo al color bg
 this->pixmap->draw_rectangle(this->gc_,true,0,0,this->winw,this->winh);
fprintf(stderr,"configurar_sonda : winw %d winh %d\n",this->winw,this->winh);


 return 0; // Todo bien
} // Fin de configurar_sonda

/* ***************
  repintar
  ************ */
// Funcion q repinta la sonda
gint drawingarea_sonda::repintar(void)
{
 gint dev;

 // Reconstruimos el pixmap
 dev=this->configurar_sonda();

 // Dibujamos en el window el pixmap
 if(dev==0) // solo si la configuracion del pixmap ha ido bien
 {
this->window->draw_drawable(this->gc_,this->pixmap,0,0,0,0,this->winw,this->winh);
   fprintf(stderr,"drawingarea_sonda repintar todo bien\n");
 }
 else
 {
   this->error("repintar : Error al configurar la sonda");
 }

 return dev; // Si es 0 todo bien, si es menor q 0 mal
} // Fin de repintar

/* **************
  error
  *********** */
// Funcion para el tratamiento de errores
gint drawingarea_sonda::error(gchar *linea)
{
 // En esta funcion podemos decidir q tratamiento dar a los errores
 // sacarlos por pantalla a un fichero ...etc.

 std::cout<<"ERROR EN drawingarea_sonda.cc\n";
 std::cout<<linea<<"\n";

 return 0; // Todo bien
} // Fin de error


I also have a window generated with glade with this code:

window_sonda_red.cc
-------------------


/* **********************
  window_sonda_red
  ******************* */
// Constructor
window_sonda_red::window_sonda_red()
{
 this->inicializa();
} // Fin de window_sonda_red
// Sobrecarga del constructor con la referencia al padre
window_sonda_red::window_sonda_red(window1 *padre)
{
 this->inicializa();
 this->mostrar_como_hijo(padre);
} // Fin de window_sonda_red(window1 *padre)

/* *****************
  inicializa
  ************** */
// Funcion q inicializa los valores de la clase, para el constructor
gint window_sonda_red::inicializa(void)
{
 this->padre=NULL; // Inicializamos la referencia al padre

 // Creamos los drawingarea para representar las sondas
 this->ds = Gtk::manage(new class drawingarea_sonda());
 this->dsz = Gtk::manage(new class drawingarea_sonda());
 this->dh = Gtk::manage(new class drawingarea_sonda());
 this->dhz = Gtk::manage(new class drawingarea_sonda());

 this->dp = Gtk::manage(new class drawingarea_sonda());

 this->ds->set_size_request(-1,-1);
 this->dsz->set_size_request(-1,-1);
 this->dh->set_size_request(-1,-1);
 this->dhz->set_size_request(-1,-1);
 this->dp->set_size_request(-1,-1);

 // Anyadimos los drawingarea de las sondas al panel deslizante
 this->hbox_s->pack_start(*this->ds);
 this->hbox_sz->pack_start(*this->dsz);
 this->hbox_h->pack_start(*this->dh);
 this->hbox_hz->pack_start(*this->dhz);

 this->hbox_prueba->pack_start(*this->dp);

 // Anyadimos los eventos q los q tienen q reaccionar
this->ds->signal_expose_event().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_expose_event)); this->ds->signal_realize().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_realize)); this->dsz->signal_expose_event().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_expose_event)); this->dsz->signal_realize().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_realize)); this->dh->signal_expose_event().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_expose_event)); this->dh->signal_realize().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_realize)); this->dhz->signal_expose_event().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_expose_event)); this->dhz->signal_realize().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_realize));

this->dp->signal_expose_event().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_expose_event)); this->dp->signal_realize().connect(SigC::slot(*this->ds, &drawingarea_sonda::on_drawingarea_sonda_realize));

 return 0; // Todo bien
} // Fin de inicializa

/* **********************
  mostrar_como_hijo
  ******************* */
// Funcion q activa la ventana de inf. general recogiendo la informacion
// de los objetos q lo llamaron
void window_sonda_red::mostrar_como_hijo(window1 *obj_padre)
{
 if(!obj_padre)
 {
   this->error("mostrar_como_hijo : Padre no definido");
   this->cerrar();
   return;
 }

 this->padre = obj_padre; // Guardamos el puntero a la ventana padre

 // Mandamos poner esta ventana como "transient" a la ventana principal
// esto hace q siempre este visible pero se pueda interactuar con la ventana principal
 this->set_transient_for(*this->padre);

 this->raise(); // Mostramos la ventana

 this->ds->entregar_window(this->hbox_s->get_window());
 this->dsz->entregar_window(this->hbox_sz->get_window());
 this->dh->entregar_window(this->hbox_h->get_window());
 this->dhz->entregar_window(this->hbox_hz->get_window());

 // Obligamos a coger el entorno grafico
 this->ds->on_drawingarea_sonda_realize();
 this->dsz->on_drawingarea_sonda_realize();
 this->dh->on_drawingarea_sonda_realize();
 this->dhz->on_drawingarea_sonda_realize();

 this->dp->on_drawingarea_sonda_realize();

 this->ds->repintar();
 this->dsz->repintar();
 this->dh->repintar();
 this->dhz->repintar();

 this->dp->repintar();

 this->hpaned1->show();
 this->hpaned2->show();
 this->vpaned1->show();
 this->show_all();
} // Fin de mostrar_como_hijo

Sorry for putting so much code and leaving the comments in spanish ;-), if someone needs to understand any text in spanish, please told me and I will translate it.

The Hbox-es are putted into Hpaned/Vpaned by the glade.

When the window is created the custom drawingareas don't get correctly the Gdk::Window to do the visualization, instead it makes really strange with: when I move the division of the Hpaned/Vpaned more separated windows appear and they change their size according with the paned size, but none of then is repainted in blue as the code tries to do.

All the custom drawingarea have winw=1 and winh=1 after this->window->get_geometry(this->winx,this->winy,this->winw,this->winh,this->wind); on the on_drawingarea_sonda_realize function.


Surelly is something obvious but I can't see the problem.



By the way: when I try to create the window_sonda_red window as a TopLevel from a PopUp window, the window_sonda_red is always "under" the parent window.
Is there a way to spawn a TopLevel window over a PopUp window?

_________________________________________________________________
Déjanos tu CV y recibe ofertas de trabajo en tu buzón. Multiplica tus oportunidades con MSN Empleo. http://www.msn.es/Empleo/




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