summaryrefslogtreecommitdiff
path: root/panels/common/list-box-helper.c
blob: 415cdde94e51f9c3d6daa8ef27cb6db2c5f7b7d3 (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
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
/*
 * Copyright (C) 2014 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "list-box-helper.h"

#define MAX_ROWS_VISIBLE 5

struct _CcListBoxRow
{
  GtkListBoxRow parent_instance;
};
G_DEFINE_TYPE (CcListBoxRow, cc_list_box_row, GTK_TYPE_LIST_BOX_ROW)
enum
{
  BOX_ROW_ACTIVATED,
  LAST_BOX_ROW_SIGNAL
};
static guint cc_list_box_row_signals[LAST_BOX_ROW_SIGNAL] = { 0 };
static void
cc_list_box_row_class_init (CcListBoxRowClass *klass)
{
  cc_list_box_row_signals[BOX_ROW_ACTIVATED] =
    g_signal_new ("activated",
                  CC_TYPE_LIST_BOX_ROW,
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
}
static void cc_list_box_row_init (CcListBoxRow *self) {}

struct _CcListBox
{
  GtkListBox parent_instance;
};
G_DEFINE_TYPE (CcListBox, cc_list_box, GTK_TYPE_LIST_BOX)
static void
cc_list_box_row_activated (GtkListBox *box, GtkListBoxRow *row)
{
  if (CC_IS_LIST_BOX_ROW (row))
    g_signal_emit (row, cc_list_box_row_signals[BOX_ROW_ACTIVATED], 0);
}
static void
cc_list_box_class_init (CcListBoxClass *klass)
{
  GtkListBoxClass *parent_class = GTK_LIST_BOX_CLASS (klass);
  parent_class->row_activated = cc_list_box_row_activated;
}
static void cc_list_box_init (CcListBox *self) {}


void
cc_list_box_update_header_func (GtkListBoxRow *row,
                                GtkListBoxRow *before,
                                gpointer user_data)
{
  GtkWidget *current;

  if (before == NULL)
    {
      gtk_list_box_row_set_header (row, NULL);
      return;
    }

  current = gtk_list_box_row_get_header (row);
  if (current == NULL)
    {
      current = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
      gtk_widget_show (current);
      gtk_list_box_row_set_header (row, current);
    }
}

void
cc_list_box_adjust_scrolling (GtkListBox *listbox)
{
  GtkWidget *scrolled_window;
  GList *children;
  guint n_rows, num_max_rows;

  scrolled_window = g_object_get_data (G_OBJECT (listbox), "cc-scrolling-scrolled-window");
  if (!scrolled_window)
    return;

  children = gtk_container_get_children (GTK_CONTAINER (listbox));
  n_rows = g_list_length (children);

  num_max_rows = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (listbox), "cc-max-rows-visible"));

  if (n_rows >= num_max_rows)
    {
      gint total_row_height = 0;
      GList *l;
      guint i;

      for (l = children, i = 0; l != NULL && i < num_max_rows; l = l->next, i++) {
        gint row_height;
        gtk_widget_get_preferred_height (GTK_WIDGET (l->data), &row_height, NULL);
        total_row_height += row_height;
      }

      gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (scrolled_window), total_row_height);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                      GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    }
  else
    {
      gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (scrolled_window), -1);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                      GTK_POLICY_NEVER, GTK_POLICY_NEVER);
    }

  g_list_free (children);
}

void
cc_list_box_setup_scrolling (GtkListBox *listbox,
                             guint       num_max_rows)
{
  GtkWidget *parent;
  GtkWidget *scrolled_window;

  parent = gtk_widget_get_parent (GTK_WIDGET (listbox));
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (scrolled_window);

  g_object_ref (listbox);
  gtk_container_remove (GTK_CONTAINER (parent), GTK_WIDGET (listbox));
  gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (listbox));
  g_object_unref (listbox);

  gtk_container_add (GTK_CONTAINER (parent), scrolled_window);

  if (num_max_rows == 0)
    num_max_rows = MAX_ROWS_VISIBLE;

  g_object_set_data (G_OBJECT (listbox), "cc-scrolling-scrolled-window", scrolled_window);
  g_object_set_data (G_OBJECT (listbox), "cc-max-rows-visible", GUINT_TO_POINTER (num_max_rows));
}