1

This is a piece of code which creates a window:

#include <gtk/gtk.h>

static GtkWidget* createWindow()
{
    GtkWidget *window;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
    gtk_widget_set_name(window, "GtkLauncher");
    g_signal_connect_swapped(G_OBJECT(window), "destroy",
    G_CALLBACK(gtk_main_quit), NULL);
    return window;
}

int main(int argc, char* argv[])
{
    GtkWidget *main_window;
    gtk_init(&argc, &argv);
    if (!g_thread_supported())
        g_thread_init(NULL);
    main_window = createWindow();
    gtk_widget_grab_focus(main_window);
    gtk_widget_show_all(main_window); 
    gtk_main();
    return 0;
}

And in here: Convert a GTK python script to C , i got how to take a screenshot.

gdk_get_default_root_window() will give me the screenshot of the desktop.

gdk_screen_get_active_window (gdk_screen_get_default()) will give me the screenshot of any active window.

Is there any way to take the screenshot of the window being created in the code above??

Community
  • 1
  • 1
shru14
  • 21
  • 2
  • 3
    `gdk_get_default_root_window` doesn't give you a screenshot. It gives you a handle to the root window. The program in [this answer](http://stackoverflow.com/questions/3045850/convert-a-gtk-python-script-to-c/3046325#3046325) solves your problem. Just replace the `*w` with a pointer to your newly created window. I'm not familiar with the exact function you need but that should be in the manual somewhere. – Noufal Ibrahim Apr 26 '11 at 08:16
  • Yes, got it. gdk_get_default_root_window will give handle to root window.It would be helpful if you explain the later part of replacing the pointer of the window being created. – shru14 Apr 26 '11 at 10:57

1 Answers1

1

I think this should do it, although you may need to iterate the main loop after showing the window to get it to paint properly, in which case you'll need some more code (I haven't tested this)

#include <unistd.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <cairo.h>


static GtkWidget* createWindow()
{
    GtkWidget *window;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
    gtk_widget_set_name(window, "GtkLauncher");
    g_signal_connect_swapped(G_OBJECT(window), "destroy",
    G_CALLBACK(gtk_main_quit), NULL);
    return window;
}


int main(int argc, char **argv)
{
    gdk_init(&argc, &argv);

    GtkWidget *main_window = createWindow();
    gtk_widget_show_all(main_window);

    // may not need this, it also may not be enough either
    while (gtk_events_pending ()) gtk_main_iteration (); 

    GdkWindow *w = GDK_WINDOW(main_window);


    gint width, height;
    gdk_drawable_get_size(GDK_DRAWABLE(w), &width, &height);

    GdkPixbuf *pb = gdk_pixbuf_get_from_drawable(NULL, 
                       GDK_DRAWABLE(w), 
                       NULL, 
                       0,0,0,0,width,height);

    if(pb != NULL) {
        gdk_pixbuf_save(pb, "screenshot.png", "png", NULL);
        g_print("Screenshot saved to screenshot.png.\n");
    } else {
        g_print("Unable to get the screenshot.\n");
    }
    return 0;
}

If it doesn't work you'll have to move the screenshot taking into an event handler that connects to some event (I'm not sure which probably window-state-event then you have to look at the event to figure out when to take the screenshot)

Spudd86
  • 2,986
  • 22
  • 20
  • Thanks,but this doesn't work. Now currently for my project it is fine with gdk_screen_get_active_window (gdk_screen_get_default()) . Now whenever a webpage is being loaded, i want to get the list of focusable items such as hyperlinks, checkbox and write a function which takes the focusable items whenever clicked to get me the corresponding web page. Thanks in advance – shru14 May 10 '11 at 06:35