overlay.c 6.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/* Overlay
 *
 * Stack widgets in static positions over a main widget
 */

#include <gtk/gtk.h>

GtkWidget *
do_overlay (GtkWidget *do_widget)
{
  static GtkWidget *window = NULL;

  if (!window)
    {
      GtkWidget *view;
      GtkWidget *sw;
      GtkWidget *overlay;
      GtkWidget *entry;
      GtkWidget *label;

      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_screen (GTK_WINDOW (window),
                             gtk_widget_get_screen (do_widget));
      gtk_window_set_default_size (GTK_WINDOW (window),
                                   450, 450);
      
      g_signal_connect (window, "destroy",
                        G_CALLBACK (gtk_widget_destroyed), &window);

      gtk_window_set_title (GTK_WINDOW (window), "Overlay");
      gtk_container_set_border_width (GTK_CONTAINER (window), 0);

      view = gtk_text_view_new ();

      sw = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                      GTK_POLICY_AUTOMATIC,
                                      GTK_POLICY_AUTOMATIC);
      gtk_container_add (GTK_CONTAINER (sw), view);

      overlay = gtk_overlay_new ();
      gtk_container_add (GTK_CONTAINER (overlay), sw);
      gtk_container_add (GTK_CONTAINER (window), overlay);

      entry = gtk_entry_new ();
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);
      gtk_widget_set_halign (entry, GTK_ALIGN_END);
      gtk_widget_set_valign (entry, GTK_ALIGN_END);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_END);
      gtk_widget_set_valign (label, GTK_ALIGN_END);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 20);
      gtk_widget_set_margin_right (label, 20);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_START);
      gtk_widget_set_valign (entry, GTK_ALIGN_END);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_START);
      gtk_widget_set_valign (label, GTK_ALIGN_END);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 20);
      gtk_widget_set_margin_right (label, 20);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_END);
      gtk_widget_set_valign (entry, GTK_ALIGN_START);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_END);
      gtk_widget_set_valign (label, GTK_ALIGN_START);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 20);
      gtk_widget_set_margin_right (label, 20);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_START);
      gtk_widget_set_valign (entry, GTK_ALIGN_START);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_START);
      gtk_widget_set_valign (label, GTK_ALIGN_START);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 20);
      gtk_widget_set_margin_right (label, 20);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_END);
      gtk_widget_set_valign (entry, GTK_ALIGN_CENTER);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_END);
      gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 20);
      gtk_widget_set_margin_right (label, 20);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_START);
      gtk_widget_set_valign (entry, GTK_ALIGN_CENTER);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_START);
      gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 20);
      gtk_widget_set_margin_right (label, 20);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_CENTER);
      gtk_widget_set_valign (entry, GTK_ALIGN_START);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
      gtk_widget_set_valign (label, GTK_ALIGN_START);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 20);
      gtk_widget_set_margin_right (label, 20);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_CENTER);
      gtk_widget_set_valign (entry, GTK_ALIGN_END);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
      gtk_widget_set_valign (label, GTK_ALIGN_END);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);
      gtk_widget_set_margin_left (label, 10);
      gtk_widget_set_margin_right (label, 10);
      gtk_widget_set_margin_top (label, 5);
      gtk_widget_set_margin_bottom (label, 5);

      entry = gtk_entry_new ();
      gtk_widget_set_halign (entry, GTK_ALIGN_CENTER);
      gtk_widget_set_valign (entry, GTK_ALIGN_CENTER);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry);

      label = gtk_label_new ("Hello world");
      gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
      gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
      gtk_overlay_add_overlay (GTK_OVERLAY (overlay), label);

      gtk_widget_show_all (overlay);
    }

  if (!gtk_widget_get_visible (window))
    {
      gtk_widget_show (window);
    }
  else
    {
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
}