popover.c 5.46 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
/* Popovers
 *
 * A bubble-like window containing contextual information or options.
 * GtkPopovers can be attached to any widget, and will be displayed
 * within the same window, but on top of all its content.
 */

#include <gtk/gtk.h>

static void
toggle_changed_cb (GtkToggleButton *button,
                   GtkWidget       *popover)
{
  gtk_widget_set_visible (popover,
                          gtk_toggle_button_get_active (button));
}

static GtkWidget *
create_popover (GtkWidget       *parent,
                GtkWidget       *child,
                GtkPositionType  pos)
{
  GtkWidget *popover;

  popover = gtk_popover_new (parent);
  gtk_popover_set_position (GTK_POPOVER (popover), pos);
  gtk_container_add (GTK_CONTAINER (popover), child);
  gtk_container_set_border_width (GTK_CONTAINER (popover), 6);
  gtk_widget_show (child);

  return popover;
}

static GtkWidget *
create_complex_popover (GtkWidget       *parent,
                        GtkPositionType  pos)
{
38
  GtkWidget *popover, *window, *content;
39 40 41 42
  GtkBuilder *builder;

  builder = gtk_builder_new ();
  gtk_builder_add_from_resource (builder, "/popover/popover.ui", NULL);
43 44
  window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
  content = gtk_bin_get_child (GTK_BIN (window));
45
  g_object_ref (content);
46 47 48
  gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (content)),
                        content);
  gtk_widget_destroy (window);
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
  g_object_unref (builder);

  popover = create_popover (parent, content, GTK_POS_BOTTOM);
  g_object_unref (content);

  return popover;
}

static void
entry_size_allocate_cb (GtkEntry      *entry,
                        GtkAllocation *allocation,
                        gpointer       user_data)
{
  GtkEntryIconPosition popover_pos;
  GtkPopover *popover = user_data;
  cairo_rectangle_int_t rect;

  if (gtk_widget_is_visible (GTK_WIDGET (popover)))
    {
      popover_pos =
        GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (entry),
                                             "popover-icon-pos"));
      gtk_entry_get_icon_area (entry, popover_pos, &rect);
      gtk_popover_set_pointing_to (GTK_POPOVER (popover), &rect);
    }
}

static void
entry_icon_press_cb (GtkEntry             *entry,
                     GtkEntryIconPosition  icon_pos,
                     GdkEvent             *event,
                     gpointer              user_data)
{
  GtkWidget *popover = user_data;
  cairo_rectangle_int_t rect;

  gtk_entry_get_icon_area (entry, icon_pos, &rect);
  gtk_popover_set_pointing_to (GTK_POPOVER (popover), &rect);
  gtk_widget_show (popover);

  g_object_set_data (G_OBJECT (entry), "popover-icon-pos",
                     GUINT_TO_POINTER (icon_pos));
}

static void
day_selected_cb (GtkCalendar *calendar,
                 gpointer     user_data)
{
  cairo_rectangle_int_t rect;
  GtkAllocation allocation;
  GtkWidget *popover;
  GdkEvent *event;

  event = gtk_get_current_event ();

  if (event->type != GDK_BUTTON_PRESS)
    return;

  gdk_window_coords_to_parent (event->button.window,
                               event->button.x, event->button.y,
                               &event->button.x, &event->button.y);
  gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
  rect.x = event->button.x - allocation.x;
  rect.y = event->button.y - allocation.y;
  rect.width = rect.height = 1;

  popover = create_popover (GTK_WIDGET (calendar),
                            gtk_entry_new (),
                            GTK_POS_BOTTOM);
  gtk_popover_set_pointing_to (GTK_POPOVER (popover), &rect);

  gtk_widget_show (popover);

  gdk_event_free (event);
}

GtkWidget *
do_popover (GtkWidget *do_widget)
{
  static GtkWidget *window = NULL;
  GtkWidget *popover, *box, *widget;

  if (!window)
    {
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 24);
      gtk_container_set_border_width (GTK_CONTAINER (box), 24);
      gtk_container_add (GTK_CONTAINER (window), box);

      g_signal_connect (window, "destroy",
                        G_CALLBACK (gtk_widget_destroyed), &window);

      widget = gtk_toggle_button_new_with_label ("Button");
      popover = create_popover (widget,
                                gtk_label_new ("This popover does not grab input"),
                                GTK_POS_TOP);
145
      gtk_popover_set_modal (GTK_POPOVER (popover), FALSE);
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
      g_signal_connect (widget, "toggled",
                        G_CALLBACK (toggle_changed_cb), popover);
      gtk_container_add (GTK_CONTAINER (box), widget);

      widget = gtk_entry_new ();
      popover = create_complex_popover (widget, GTK_POS_TOP);
      gtk_entry_set_icon_from_icon_name (GTK_ENTRY (widget),
                                         GTK_ENTRY_ICON_PRIMARY, "edit-find");
      gtk_entry_set_icon_from_icon_name (GTK_ENTRY (widget),
                                         GTK_ENTRY_ICON_SECONDARY, "edit-clear");

      g_signal_connect (widget, "icon-press",
                        G_CALLBACK (entry_icon_press_cb), popover);
      g_signal_connect (widget, "size-allocate",
                        G_CALLBACK (entry_size_allocate_cb), popover);
      gtk_container_add (GTK_CONTAINER (box), widget);

      widget = gtk_calendar_new ();
      g_signal_connect (widget, "day-selected",
                        G_CALLBACK (day_selected_cb), NULL);
      gtk_container_add (GTK_CONTAINER (box), widget);
    }

  if (!gtk_widget_get_visible (window))
    gtk_widget_show_all (window);
  else
172
    gtk_widget_destroy (window);
173 174 175

  return window;
}