Gtk Window to Gdk Surface in GTK4 - c

I was wondering how I could get the X11 window ID for a Gtk Window in gtk4. I have seen other answers before regarding gtk3, but they don't seem to work in the latest gtk4. I found gdk_x11_surface_get_xid, but I can't pass a GtkWidget to that. I tried using the GDK_SURFACE cast, but I get:
invalid cast from 'GtkApplicationWindow' to 'GdkSurface'
Is there any way to resolve this, and get the x11 window id from a GtkWindow?

Related

How to implement custom drawing in GtkButton in GTK4 using C

I'm porting some of my Objective C (macOS, Cocoa) applications to Linux/Windows. I decided to use GTK4. I'd appreciate an example of how to overload the GtkButton drawing method to do my custom drawing. I'd like to draw some symbols on a button using Cairo.

embed a X11 window in a gtk+3 widget

I would like to embed a simple X11 window in a gtk widget.
I have found this gist https://gist.github.com/pastapojken/69f1e223fd926425f7ae770a95ec79a2 which does almost but not quite what I am looking for: It allows to add gtk widgets inside an X window, not the contrary.
I don't know if I am being clear as I am quite a newbie in both X11 and gtk. So here is a quick schema:
So is this even possible, any advice on how to make this work ? I feel like all I need to do is inherit from GtkWidget and set its GdkWindow to a wrapper of the XID like this:
win = gdk_x11_window_foreign_new_for_display(display, xid);
gtk_widget_set_window(myWidget, win);
But I was not sucessful this way, all I get is two top level windows as shown below.
I am running Fedora 32 on X11, NOT wayland
thanks!

SDL_Surface into GTK Window

I'm currently making a project in my IT School and I work with SDL (The project is image processing and stuff like that), and currently I just display the image with SDL without interface (buttons etc...). I know a little bit about GTK so I want to know if I can display an image (here a SDL_Surface) into a GTK window
I made some research but nothing was very clear...
Thank you !
Drawing widgets using SDL
Drawing widgets(menu, buttons etc) using SDL drawing functions and handling actions by tracking the cursor position during the occurrence of event(on which item the cursor was while the event was performed).
This becomes very complicated and would be better to use an existing GUI library with some simple hacks. You can find code example for drawing a button here.
Copying SDL surface into target GUI widget
This involves copying pixel by pixel(predefined functions might be available to do the same, gdk_draw_rgb_image in case of gtk) of the sdl surface into target gui widget(can be a drawingArea in case of GTK).
Merging SDL window into a GUI widget
In X11 and win32 systems each windows are given with a window id and by default sdl and gtk would have separate window id resulting two different windows, we can exploit SDL_CreateWindowFrom function to use single window for sdl and gtk in which we will force both the libraries to use a single window id.
You can find similar question here.
GTK Plug and Socket
Plug and Socket enables embeeding widget from one process to another. GTK will create a socket and pass the socket id to SDL and SDL would create window from that socket ID.
Event loop handling:
After merging SDL with GTK you might find gtk and sdl have their own event loops hence handling events might not be as expected. To solve that issue you can handle
events in GTK and propagate the event to SDL using SDL_PushEvent function and vice versa in case you are using user defined events.
.
.
static SDL_Event SDLevent;
switch(event->keyval)
{
case GDK_KEY_m:
SDLevent.type = SDL_KEYUP;
SDLevent.key.keysym.sym = SDLK_m;
SDL_PushEvent(&SDLevent);
break;
.
.
.
This link has a nice explanation for above methods.

How to create transparent button using gtk in c

I want to add transparent button in my application and i am using gtk. How to do it?
I am using gtk_color_button_set_alpha():
gtk_color_button_set_alpha(GtkColorButton *button, guint16 100);
But it is giving error as
error: expected expression before ‘GtkColorButton’
error: too few arguments to function ‘gtk_color_button_set_alpha’
With a fairly recent GTK+ (3.8) this should be quite easy: See opacity property and gtk_widget_set_opacity(). Note that this depends on display manager features and you cannot rely on the opacity working everywhere. You can check if it works though: gtk_widget_is_composited() should tell you that.

GTK - Set widget's window fullscreen

I have been trying to set a widget's window to fullscreen.
I tried getting the root window of the widget using gtk_widget_get_root(_window) and then using gtk_window_fullscreen() to set it to fullscreen, but the documentation says that gtk_widget_get_root_window() and gtk_widget_get_parent_window() (which I tried too) return a GdkWindow *, not a GtkWindow * as needed.
I tried casting GdkWindow* to GtkWindow* but it gives me this error:
Gtk-CRITICAL **: IA__gtk_window_fullscreen: assertion `GTK_IS_WINDOW
(window)' failed
The code would look like this:
gtk_window_fullscreen (GTK_WINDOW(gtk_widget_get_root_window (widget)));
I've also noticed that a widget has a field "window" but it is also GdkWindow * type.
I don't have the window widget in the function where I want to set it fullscreen...
GtkWindow and GdkWindow are totally different things.
When we think about "window", we usually mean a graphical component - the toplevel, resizable "box" with minimize/maximize buttons etc. This is what is represented by GtkWindow.
GDK however has another meaning for "window". The GdkWindow is not a graphical component, but an abstract resource inside GDK representing some rectangular region on the screen. It is probably closely related to some low-level resource inside Xorg/Win32 etc.
If you want to obtain the toplevel GtkWindow containing a given widget, you could probably use gtk_widget_get_toplevel or gtk_widget_get_ancestor

Resources