[gtk+] docs: don't use a loop for the docs shooter



commit 7796e7863ccec070edf84a5aa2a6ecc3b9010c59
Author: William Jon McCann <william jon mccann gmail com>
Date:   Thu Feb 13 11:37:46 2014 -0500

    docs: don't use a loop for the docs shooter

 docs/tools/shooter.c |  141 +++++++++++++++++++++++++++++++++-----------------
 docs/tools/widgets.c |   84 ------------------------------
 2 files changed, 93 insertions(+), 132 deletions(-)
---
diff --git a/docs/tools/shooter.c b/docs/tools/shooter.c
index 1d3eeb2..15e1daa 100644
--- a/docs/tools/shooter.c
+++ b/docs/tools/shooter.c
@@ -25,6 +25,8 @@
 #define _(x) (x)
 #endif
 
+static void queue_show (void);
+
 static Window
 find_toplevel_window (Window xid)
 {
@@ -127,7 +129,7 @@ take_window_shot (Window   child,
   gint width, height;
 
   GdkPixbuf *tmp, *tmp2;
-  GdkPixbuf *retval;
+  GdkPixbuf *retval = NULL;
 
   if (include_decoration)
     xid = find_toplevel_window (child);
@@ -163,70 +165,113 @@ take_window_shot (Window   child,
   tmp = gdk_pixbuf_get_from_window (window,
                                    x, y, width, height);
 
-  if (include_decoration)
-    tmp2 = remove_shaped_area (tmp, xid);
-  else
-    tmp2 = add_border_to_shot (tmp);
-
-  retval = create_shadowed_pixbuf (tmp2);
-  g_object_unref (tmp);
-  g_object_unref (tmp2);
+  if (tmp != NULL)
+    {
+      if (include_decoration)
+        tmp2 = remove_shaped_area (tmp, xid);
+      else
+        tmp2 = add_border_to_shot (tmp);
+
+      g_object_unref (tmp);
+
+      if (tmp2 != NULL)
+        {
+          retval = create_shadowed_pixbuf (tmp2);
+          g_object_unref (tmp2);
+        }
+    }
 
   return retval;
 }
 
-int main (int argc, char **argv)
+static GList *toplevels;
+static guint shot_id;
+
+static gboolean
+shoot_one (WidgetInfo *info)
 {
-  GList *toplevels;
+  GdkWindow *window;
+  XID id;
   GdkPixbuf *screenshot = NULL;
-  GList *node;
 
-  /* If there's no DISPLAY, we silently error out.  We don't want to break
-   * headless builds. */
-  if (! gtk_init_check (&argc, &argv))
-    return 0;
-
-  toplevels = get_all_widgets ();
+  if (g_list_find (toplevels, info) == NULL)
+    {
+      g_warning ("Widget not found in queue");
+      gtk_main_quit ();
+    }
 
-  for (node = toplevels; node; node = g_list_next (node))
+  window = gtk_widget_get_window (info->window);
+  id = gdk_x11_window_get_xid (window);
+  screenshot = take_window_shot (id, info->include_decorations);
+  if (screenshot != NULL)
     {
-      GtkAllocation allocation;
-      GdkWindow *window;
-      WidgetInfo *info;
-      XID id;
       char *filename;
+      filename = g_strdup_printf ("./%s.png", info->name);
+      gdk_pixbuf_save (screenshot, filename, "png", NULL, NULL);
+      g_free (filename);
+      g_object_unref (screenshot);
+    }
+  else
+    {
+      g_warning ("unable to save shot of %s", info->name);
+    }
+  gtk_widget_destroy (info->window);
 
-      info = node->data;
+  shot_id = 0;
 
-      gtk_widget_show (info->window);
+  /* remove from the queue and try to load up another */
+  toplevels = g_list_remove (toplevels, info);
+  if (toplevels == NULL)
+    gtk_main_quit ();
+  else
+    queue_show ();
 
-      window = gtk_widget_get_window (info->window);
-      gtk_widget_get_allocation (info->window, &allocation);
+  return G_SOURCE_REMOVE;
+}
 
-      gtk_widget_show_now (info->window);
-      gtk_widget_queue_draw_area (info->window,
-                                  allocation.x, allocation.y,
-                                  allocation.width, allocation.height);
-      gdk_window_process_updates (window, TRUE);
+static void
+on_show (WidgetInfo *info)
+{
+  if (shot_id != 0)
+    return;
 
-      while (gtk_events_pending ())
-       {
-         gtk_main_iteration ();
-       }
-      sleep (1);
+  shot_id = g_timeout_add (1000, (GSourceFunc) shoot_one, info);
+}
 
-      while (gtk_events_pending ())
-       {
-         gtk_main_iteration ();
-       }
+static gboolean
+show_one (void)
+{
+  WidgetInfo *info = toplevels->data;
 
-      id = gdk_x11_window_get_xid (window);
-      screenshot = take_window_shot (id, info->include_decorations);
-      filename = g_strdup_printf ("./%s.png", info->name);
-      gdk_pixbuf_save (screenshot, filename, "png", NULL, NULL);
-      g_free(filename);
-      gtk_widget_hide (info->window);
-    }
+  g_message ("shooting %s", info->name);
+
+  g_signal_connect_swapped (info->window,
+                            "show",
+                            G_CALLBACK (on_show),
+                            info);
+
+  gtk_widget_show (info->window);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+queue_show (void)
+{
+  g_idle_add ((GSourceFunc) show_one, NULL);
+}
+
+int main (int argc, char **argv)
+{
+  /* If there's no DISPLAY, we silently error out.  We don't want to break
+   * headless builds. */
+  if (! gtk_init_check (&argc, &argv))
+    return 0;
+
+  toplevels = get_all_widgets ();
+
+  queue_show ();
+  gtk_main ();
 
   return 0;
 }
diff --git a/docs/tools/widgets.c b/docs/tools/widgets.c
index 76bd20e..2db4bbf 100644
--- a/docs/tools/widgets.c
+++ b/docs/tools/widgets.c
@@ -16,88 +16,6 @@
 #define LARGE_WIDTH 240
 #define LARGE_HEIGHT 240
 
-static Window
-find_toplevel_window (Window xid)
-{
-  Window root, parent, *children;
-  guint nchildren;
-
-  do
-    {
-      if (XQueryTree (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xid, &root,
-                     &parent, &children, &nchildren) == 0)
-       {
-         g_warning ("Couldn't find window manager window");
-         return None;
-       }
-
-      if (root == parent)
-       return xid;
-
-      xid = parent;
-    }
-  while (TRUE);
-}
-
-
-static gboolean
-adjust_size_callback (WidgetInfo *info)
-{
-  Window toplevel;
-  Window root;
-  GdkWindow *window;
-  gint tx;
-  gint ty;
-  guint twidth;
-  guint theight;
-  guint tborder_width;
-  guint tdepth;
-  gint target_width = 0;
-  gint target_height = 0;
-
-  window = gtk_widget_get_window (info->window);
-  toplevel = find_toplevel_window (GDK_WINDOW_XID (window));
-  XGetGeometry (GDK_WINDOW_XDISPLAY (window),
-               toplevel,
-               &root, &tx, &ty, &twidth, &theight, &tborder_width, &tdepth);
-
-  switch (info->size)
-    {
-    case SMALL:
-      target_width = SMALL_WIDTH;
-      target_height = SMALL_HEIGHT;
-      break;
-    case MEDIUM:
-      target_width = MEDIUM_WIDTH;
-      target_height = MEDIUM_HEIGHT;
-      break;
-    case LARGE:
-      target_width = LARGE_WIDTH;
-      target_height = LARGE_HEIGHT;
-      break;
-    case ASIS:
-      target_width = twidth;
-      target_height = theight;
-      break;
-    }
-
-  if (twidth > target_width ||
-      theight > target_height)
-    {
-      gtk_widget_set_size_request (info->window,
-                                  2 + target_width - (twidth - target_width), /* Dunno why I need the +2 
fudge factor; */
-                                  2 + target_height - (theight - target_height));
-    }
-  return FALSE;
-}
-
-static void
-realize_callback (GtkWidget  *widget,
-                 WidgetInfo *info)
-{
-  gdk_threads_add_timeout (500, (GSourceFunc)adjust_size_callback, info);
-}
-
 static WidgetInfo *
 new_widget_info (const char *name,
                 GtkWidget  *widget,
@@ -113,7 +31,6 @@ new_widget_info (const char *name,
       info->window = widget;
       gtk_window_set_resizable (GTK_WINDOW (info->window), FALSE);
       info->include_decorations = TRUE;
-      g_signal_connect (info->window, "realize", G_CALLBACK (realize_callback), info);
     }
   else
     {
@@ -126,7 +43,6 @@ new_widget_info (const char *name,
     }
   info->no_focus = TRUE;
 
-  gtk_widget_set_app_paintable (info->window, TRUE);
   g_signal_connect (info->window, "focus", G_CALLBACK (gtk_true), NULL);
 
   switch (size)


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