summaryrefslogtreecommitdiffstats
path: root/subprojects/libhandy/glade/glade-hdy-utils.c
blob: 81b8ae5e9b3012fce9e69fedc9dbdd416778e32b (plain)
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
/*
 * Copyright (C) 2020 Alexander Mikhaylenko <alexm@gnome.org>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#include <config.h>
#include <glib/gi18n-lib.h>

#include "glade-hdy-utils.h"

#include <gladeui/glade.h>

void
glade_hdy_init (const gchar *name)
{
  g_assert (strcmp (name, "libhandy") == 0);

  gtk_init (NULL, NULL);
  hdy_init ();
}

/* This function has been copied and modified from:
 * glade-gtk-list-box.c - GladeWidgetAdaptor for GtkListBox widget
 *
 * Copyright (C) 2013 Kalev Lember
 *
 * Authors:
 *      Kalev Lember <kalevlember@gmail.com>
 */
void
glade_hdy_sync_child_positions (GtkContainer *container)
{
  g_autoptr (GList) children = NULL;
  GList *l;
  gint position;
  static gboolean recursion = FALSE;

  /* Avoid feedback loop */
  if (recursion)
    return;

  children = gtk_container_get_children (container);

  position = 0;
  for (l = children; l; l = l->next) {
    gint old_position;

    glade_widget_pack_property_get (glade_widget_get_from_gobject (l->data),
                                    "position", &old_position);
    if (position != old_position) {
      /* Update glade with the new value */
      recursion = TRUE;
      glade_widget_pack_property_set (glade_widget_get_from_gobject (l->data),
                                      "position", position);
      recursion = FALSE;
    }

    position++;
  }
}

gint
glade_hdy_get_child_index (GtkContainer *container,
                           GtkWidget    *child)
{
  g_autoptr (GList) children = gtk_container_get_children (container);

  return g_list_index (children, child);
}

void
glade_hdy_reorder_child (GtkContainer *container,
                         GtkWidget    *child,
                         gint          index)
{
  gint old_index = glade_hdy_get_child_index (container, child);
  gint i = 0, n;
  g_autoptr (GList) children = NULL;
  g_autoptr (GList) removed_children = NULL;
  GList *l;

  if (old_index == index)
    return;

  gtk_container_remove (container, g_object_ref (child));

  children = gtk_container_get_children (container);
  n = g_list_length (children);

  children = g_list_reverse (children);
  l = children;

  if (index > old_index)
    n--;

  for (i = n - 1; i >= index; i--) {
    GtkWidget *last_child = l->data;

    gtk_container_remove (container, g_object_ref (last_child));
    l = l->next;

    removed_children = g_list_prepend (removed_children, last_child);
  }

  removed_children = g_list_prepend (removed_children, child);

  for (l = removed_children; l; l = l->next) {
    gtk_container_add (container, l->data);
    g_object_unref (l->data);
  }
}

GtkWidget *
glade_hdy_get_nth_child (GtkContainer *container,
                         gint          n)
{
  g_autoptr (GList) children = gtk_container_get_children (container);

  return g_list_nth_data (children, n);
}