Re: Null Ptr In Gdk::Window Initializer?



Sorry for double email, Ed, but I forgot to put the gtkmm-list in CC.

2010/7/20 Ed Rippy <ed rippy mindspring com>:
> I'm sure I'll be embarrassed when someone shows me what I'm missing, but. . .
>
> I'm writing a Scrabble (R) program in GTKMM & need to display board squares/tiles. It's pretty simple: a square w/ a colored background, a border, one big letter in the middle, & a small number in the lower right corner.
>
> I'm using a Gdk::Window (Gtk::Image needs an event box to capture mouse clicks), planning to use a PangoLayout to draw the characters, but I can't even construct the window! The Gdk::Window constructor is protected, so I derived ImageSquot1, & this is where I am stuck.

Wouldn't it be better if you put your image inside Gtk::EventBox and
that EventBox inside Gtk::Window and then setup it to receive some
kind of events?

>
> Code:
>
> (header for derived class:)
> #include <gtkmm.h>
>
> class ImageSquot1 : public Gdk::Window
> {
> public:
>  ImageSquot1( const Glib::RefPtr<Window> &parent,              // pass on
>                                GdkWindowAttr *attributes,      // Gdk::Window
>                                int attributes_mask );          // constructor
>
> private:
>  char _letter;
> };
>
> (implementation:)
> #include "imagesquot1.h"
>
> ImageSquot1::ImageSquot1( const Glib::RefPtr<Window> &parent,
>                                GdkWindowAttr *attributes,
>                                int attributes_mask )
>        : Gdk::Window( parent, attributes, attributes_mask )
> {
> }
>
> (header for testwindow, a Gtk toplevel to hold the Gdk::Window-derived ImageSquot1 object:)
> #include "imagesquot1.h"
>
> class ImageSquot1TestWindow : public Gtk::Window
> {
> public:
>  ImageSquot1TestWindow();
> protected:
>  //Signal handlers:
>
> private:        //children:
>  ImageSquot1 *_squot;
>
> };
>
> (implementation:)
> #include "imagesquot1testwindow.h"
>
> ImageSquot1TestWindow::ImageSquot1TestWindow()
> {
>  GdkWindowAttr attrs;
>
>  attrs.title = NULL;                   // title; unused
>  attrs.event_mask = 0;                 // all events?
>  attrs.x = 0;                          // x co-ord, masked off
>  attrs.y = 0;                          // y co-ord; masked off
>  attrs.width = 48;                     // width
>  attrs.height = 48;                    // height
>  attrs.wclass = GDK_INPUT_OUTPUT;      // window class
>  attrs.visual = NULL;                  // visual -- will inherit
>  attrs.colormap = NULL;                // colormap, will inherit
>  attrs.window_type = GDK_WINDOW_CHILD; // not a toplevel
>  attrs.cursor = NULL;                  // cursor, will inherit
>  attrs.wmclass_name = NULL;            // unused
>  attrs.wmclass_class = NULL;           // unused
>  attrs.override_redirect = FALSE;      // no-redirect
>  attrs.type_hint = (GdkWindowTypeHint) 0;      // window type hint, don't use
>
>  set_title("ImageSquot1 Test");
>
>  Glib::RefPtr<Gdk::Window> window = get_window();
>  _squot = new ImageSquot1( window, &attrs, 0 );
>
>  show_all_children();
> }
>

Glib::RefPtr<Gdk::Window> window is an empty refptr, because
gtk_widget_get_window() returns NULL, when widget is not realized. And
your window is not realized, when in stage of construction.

> (main program:)
> #include "imagesquot1testwindow.h"
>
> int main(int argc, char *argv[])
> {
>  Gtk::Main kit(argc, argv);
>
>  ImageSquot1TestWindow window;
>  window.resize( 500, 250 );
>  //Shows the window and returns when it is closed.
>  Gtk::Main::run(window);
>
>  return 0;
> }
>
> (end of code)
>
> -- Unfortunately the initializer blows up w/ a "segmentation fault" message, which probably means trying to use a null pointer. The backtrace shows:
>
> (gdb) bt
> #0  0xb73686d3 in Gdk::Window::Window () from /usr/lib/libgdkmm-2.4.so.1
> #1  0x0804d6f4 in ImageSquot1 (this=0x8178708, parent= 0xbfb21028,
>    attributes=0xbfb20fec, attributes_mask=0) at imagesquot1.cpp:6
> #2  0x0804cb3d in ImageSquot1TestWindow (this=0xbfb2105c)
>    at imagesquot1testwindow.cpp:26
> #3  0x0804d056 in main (argc=0, argv=0x0) at imagesquot1testwindow_main.cpp:7
>
> -- which are all initializers.
>
> If I have read the docs correctly, with attributes_mask = 0, only gint x, gint y, GdkWindowClass wclass, and GdkWindowType window_type are required (I'm not sure abt event_mask, but I think zero means "all events," and the window manager will get the events anyway because override_redirect is masked off -- and it's not a pointer, anyway).
>
> Can anyone help?
>
> Thanks,
> Ed Rippy

Krzesimir.


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