GdkDrawingArea on GtkImage - c

How can I impose GdkDrawingArea on the GtkImage for painting on an image, for example?

GtkDrawingArea and GtkImage are different classes, so you must choose one of them. You can still draw on GtkImage (and on any other widget), by connecting to expose_event signal.
You could also use plain GtkDrawingArea - displaying image is a matter of calling gdk_draw_pixbuf function.

Related

Is there any event for shapes?

I have an ellipse which is drew on a window. I want to show a message when the pointer is on it (on the ellipse). How I do it? Is there any event for shapes? Like WM_MOVE or WM_SIZE.
I use TDM-GCC and C language.
When you draw on a device context, all knowledge of what shape you draw is lost, and the system just retains the pixel by pixel information of that device context. So there is no way for the system to give you any information about the shapes that you draw because it knows nothing of those shapes.
In order to do what you want you need to keep track in your program of the high level logic of where your shapes are. Then when you handle mouse messages you can map them onto your own data structures that represent the shapes.
There are no events for mouse activity over drawings. You are expected to remember where you draw, and then map the mouse coordinates to the drawing coordinates yourself. To help with this, have a look at the PtInRegion() function. Create an elliptical HRGN via CreateEllipticRgn() or CreateEllipticRgnIndirect() that matches your drawing (in fact, you can use the same HRGN to help facilitate the drawing, see the FillRgn() function), and when you want to test if the mouse is currently inside the drawing, such as in a WM_MOUSEMOVE handler, you can use PtInRegion() for that.

XCB xcb_window_t VS xcb_drawable_t

I'm trying to learn XCB through this link and write a little "library" so I don't forget what to do each time. I'm at the point where I have a window open and I'm almost ready to start drawing things. However something has confused me, in some of the earlier examples they use xcb_window_t but in the new ones the make the "window" variable a xcb_drawable_t. I just went with it before but now I'm at the section "Simple window operations" and the functions seem to use xcb_window_t as inputs. Can these be used interchangeably, or do they need to be their own thing?
There are two kinds of drawables in X11: Windows and pixmaps.
Put differently: A window is a drawable and a pixmap is a drawable. Nothing else is a drawable.
Every function that accepts a xcb_drawable_t can be called with either a window or a pixmap. Functions that want a window only accept xcb_window_t.

GTK3 Set GtkButton size

I have a very simple code, wich create a GtkWindow and place in it a GtkButton.
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(_window, 800, 450);
gtk_window_set_decorated(GTK_WINDOW (_window), FALSE);
gtk_window_set_position(GTK_WINDOW (_window),GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_set_resizable(GTK_WINDOW (_window), FALSE);
_startbutton = gtk_button_new_with_label("myLabel");
gtk_container_add(GTK_CONTAINER(_window), _startbutton);
gtk_widget_show_all(_window);
Yet, this doesn't work as expected because the button fills the whole window.
I tried to find a way to change the button size, but all the methods that i found use some methods that are deprecated...
Can someone explain to me the way to do this ?
Because the GtkButton is the only control in the GtkWindow, it will be given the entire area of the GtkWindow to fill. If you want to do anything more complicated, you will need to use layout containers like GtkBox and GtkGrid to explicitly lay out the button, usually in relation to other controls that you will also have in the window.
Once you do lay out your controls, you can use expansion and alignment to control how the button makes use of its allotted space.

How do I determine non-rectangular drawing regions in Gtk+ 3 with cairo?

The Gtk+ 3 migration guide shows how the GdkEventExpose.region field can be used to provide a fine-grained area for re-rendering widgets. We already do something like this in Inkscape to avoid rendering excessive amounts of complicated stuff on our drawing canvas.
However, the example in the guide shows how to do this for the old Gtk+ 2 expose_event handler.
How do I do the equivalent in a new Gtk+ 3 draw handler, which receives a "ready-clipped" Cairo context as a parameter, rather than a GdkEventExpose?
I guess one possibility is to use cairo_copy_clip_rectangle_list on the "ready-clipped" cairo context to obtain a list of rectangles that make up the region to draw. Does anyone have any experience of using this? Does it seem like a sensible approach?
Yes, you should use cairo_copy_clip_rectangle_list() on the cairo_t that you get in your widget's ::draw() signal handler. See this commit for an example:
http://git.gnome.org/browse/vte/commit/?id=21a064ac8b5925108b0ab9bd6516664c8cd3e268
Since I have not much clue, I decided to check the source code. GDK emits a GDK_EXPOSE event on a window and creates the GdkEventExpose instance for this.
This event is then handled in gtk/gtkmain.c via gtk_widget_send_expose():
http://git.gnome.org/browse/gtk+/tree/gtk/gtkwidget.c?id=eecb9607a5c0ee38eadb446545beccd0922cb0b8#n6104
This function clips the cairo_t to GdkEventExpose.region, as you already learned in the docs.
This then calls _gtk_widget_draw_internal() which emits the actual draw signal:
http://git.gnome.org/browse/gtk+/tree/gtk/gtkwidget.c?id=eecb9607a5c0ee38eadb446545beccd0922cb0b8#n5726
As far as I can see, nothing here let's you access the clip region directly. In gtk_widget_send_expose() the GdkEvent is added as userdata to the cairo context. However, this is not accessible, because all the involved functions and variables are static. So you'll have to use cairo_copy_clip_rectangle_list().
However, this sounds quite inefficent. First gdk_cairo_region transforms the region into a number of calls to cairo_rectangle and then cairo transforms this from its internal representation into a cairo_rectangle_list_t (which may fail if the clip is, for some reason, not a region). If you see this being slow, it might make sense to have some shortcut for this added to gtk directly.

drawing above gtkentry using cairo

I want to use cairo to enhance gtkentry look. For this, I have connected a callback to 'expose-event'. In callback, I call gtkentry's original expose-event handler. After that, I create cairo context and draw some lines and destroy the cairo. I return 'TRUE' as return value of callback function so that expose-event does not propagate.
Now my problem is, I am drawing line from (0,0) to (100,100). But line appears only over the border areas of the gtkentry. The place where text is, it does not appear.
Please help.
Kind Regards
-Durgesh O Mishra
GtkEntry uses an additional GdkWindow for the text area. It is sort-of-private, but you could access it using the following code:
GDK_WINDOW (gdk_window_peek_children (GTK_WIDGET (entry)->window)->data);
So, you can pass this window to gdk_cairo_create().
If you have problems applying this to your code, paste the code — it's hard to guess what to do without having any way to test.

Resources