2013-06-10 11:49:14 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-10-08 00:59:10 +00:00
|
|
|
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
|
|
|
|
2013-06-10 11:49:14 +00:00
|
|
|
typedef struct _Row Row;
|
|
|
|
typedef struct _RowClass RowClass;
|
|
|
|
|
|
|
|
struct _Row
|
|
|
|
{
|
|
|
|
GtkListBoxRow parent_instance;
|
|
|
|
GtkWidget *label;
|
2020-07-24 13:54:49 +00:00
|
|
|
int sort_id;
|
2013-06-10 11:49:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _RowClass
|
|
|
|
{
|
|
|
|
GtkListBoxRowClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *css =
|
2017-05-09 17:38:58 +00:00
|
|
|
"list row {"
|
2013-06-10 11:49:14 +00:00
|
|
|
" border-width: 1px;"
|
|
|
|
" border-style: solid;"
|
|
|
|
" border-color: blue;"
|
|
|
|
"}"
|
2017-05-09 17:38:58 +00:00
|
|
|
"list row:hover {"
|
2013-06-10 11:49:14 +00:00
|
|
|
"background-color: green;"
|
|
|
|
"}"
|
2017-05-09 17:38:58 +00:00
|
|
|
"list row:active {"
|
2013-06-10 11:49:14 +00:00
|
|
|
"background-color: red;"
|
|
|
|
"}";
|
|
|
|
|
2020-03-06 17:05:43 +00:00
|
|
|
static GType row_get_type (void);
|
2013-06-10 11:49:14 +00:00
|
|
|
G_DEFINE_TYPE (Row, row, GTK_TYPE_LIST_BOX_ROW)
|
|
|
|
|
|
|
|
static void
|
|
|
|
row_init (Row *row)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
row_class_init (RowClass *class)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-06 17:05:43 +00:00
|
|
|
static GtkWidget *
|
2020-07-24 18:40:36 +00:00
|
|
|
row_new (const char * text, int sort_id) {
|
2013-06-10 11:49:14 +00:00
|
|
|
Row *row;
|
|
|
|
|
|
|
|
row = g_object_new (row_get_type (), NULL);
|
|
|
|
if (text != NULL)
|
|
|
|
{
|
|
|
|
row->label = gtk_label_new (text);
|
2020-05-03 04:09:18 +00:00
|
|
|
gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), row->label);
|
2013-06-10 11:49:14 +00:00
|
|
|
}
|
|
|
|
row->sort_id = sort_id;
|
|
|
|
|
|
|
|
return GTK_WIDGET (row);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2013-06-11 12:09:19 +00:00
|
|
|
update_header_cb (Row *row, Row *before, gpointer data)
|
2013-06-10 11:49:14 +00:00
|
|
|
{
|
|
|
|
GtkWidget *hbox, *l, *b;
|
|
|
|
|
|
|
|
if (before == NULL ||
|
|
|
|
(row->label != NULL &&
|
|
|
|
strcmp (gtk_label_get_text (GTK_LABEL (row->label)), "blah3") == 0))
|
|
|
|
{
|
2013-06-11 12:09:19 +00:00
|
|
|
/* Create header if needed */
|
|
|
|
if (gtk_list_box_row_get_header (GTK_LIST_BOX_ROW (row)) == NULL)
|
2013-06-10 11:49:14 +00:00
|
|
|
{
|
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
2013-06-11 12:09:19 +00:00
|
|
|
l = gtk_label_new ("Header");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), l);
|
2013-06-10 11:49:14 +00:00
|
|
|
b = gtk_button_new_with_label ("button");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), b);
|
2013-06-11 12:09:19 +00:00
|
|
|
gtk_list_box_row_set_header (GTK_LIST_BOX_ROW (row), hbox);
|
2013-06-10 11:49:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 12:09:19 +00:00
|
|
|
hbox = gtk_list_box_row_get_header(GTK_LIST_BOX_ROW (row));
|
2013-06-10 11:49:14 +00:00
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
l = gtk_widget_get_first_child (hbox);
|
2013-06-11 12:09:19 +00:00
|
|
|
gtk_label_set_text (GTK_LABEL (l), g_strdup_printf ("Header %d", row->sort_id));
|
2013-06-10 11:49:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-11 12:09:19 +00:00
|
|
|
gtk_list_box_row_set_header(GTK_LIST_BOX_ROW (row), NULL);
|
2013-06-10 11:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sort_cb (Row *a, Row *b, gpointer data)
|
|
|
|
{
|
|
|
|
return a->sort_id - b->sort_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
reverse_sort_cb (Row *a, Row *b, gpointer data)
|
|
|
|
{
|
|
|
|
return b->sort_id - a->sort_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
filter_cb (Row *row, gpointer data)
|
|
|
|
{
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
if (row->label != NULL)
|
|
|
|
{
|
|
|
|
text = gtk_label_get_text (GTK_LABEL (row->label));
|
|
|
|
return strcmp (text, "blah3") != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
row_activated_cb (GtkListBox *list_box,
|
|
|
|
GtkListBoxRow *row)
|
|
|
|
{
|
|
|
|
g_print ("activated row %p\n", row);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
row_selected_cb (GtkListBox *list_box,
|
|
|
|
GtkListBoxRow *row)
|
|
|
|
{
|
|
|
|
g_print ("selected row %p\n", row);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sort_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
|
|
|
gtk_list_box_set_sort_func (list, (GtkListBoxSortFunc)sort_cb, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reverse_sort_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
|
|
|
gtk_list_box_set_sort_func (list, (GtkListBoxSortFunc)reverse_sort_cb, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
filter_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
|
|
|
gtk_list_box_set_filter_func (list, (GtkListBoxFilterFunc)filter_cb, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unfilter_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
|
|
|
gtk_list_box_set_filter_func (list, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
change_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
Row *row = data;
|
|
|
|
|
|
|
|
if (strcmp (gtk_label_get_text (GTK_LABEL (row->label)), "blah3") == 0)
|
|
|
|
{
|
|
|
|
gtk_label_set_text (GTK_LABEL (row->label), "blah5");
|
|
|
|
row->sort_id = 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_label_set_text (GTK_LABEL (row->label), "blah3");
|
|
|
|
row->sort_id = 3;
|
|
|
|
}
|
|
|
|
gtk_list_box_row_changed (GTK_LIST_BOX_ROW (row));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
GtkWidget *new_row;
|
|
|
|
static int new_button_nr = 1;
|
|
|
|
|
|
|
|
new_row = row_new( g_strdup_printf ("blah2 new %d", new_button_nr), new_button_nr);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), new_row, -1);
|
2013-06-10 11:49:14 +00:00
|
|
|
new_button_nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
separate_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
2013-06-11 12:09:19 +00:00
|
|
|
gtk_list_box_set_header_func (list, (GtkListBoxUpdateHeaderFunc)update_header_cb, NULL, NULL);
|
2013-06-10 11:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unseparate_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
2013-06-11 12:09:19 +00:00
|
|
|
gtk_list_box_set_header_func (list, NULL, NULL, NULL);
|
2013-06-10 11:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
visibility_clicked_cb (GtkButton *button,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *row = data;
|
|
|
|
|
|
|
|
gtk_widget_set_visible (row, !gtk_widget_get_visible (row));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
selection_mode_changed (GtkComboBox *combo, gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
|
|
|
gtk_list_box_set_selection_mode (list, gtk_combo_box_get_active (combo));
|
|
|
|
}
|
|
|
|
|
2013-06-17 13:14:29 +00:00
|
|
|
static void
|
|
|
|
single_click_clicked (GtkButton *check, gpointer data)
|
|
|
|
{
|
|
|
|
GtkListBox *list = data;
|
|
|
|
|
2020-08-30 23:36:27 +00:00
|
|
|
g_print ("single: %d\n", gtk_check_button_get_active (GTK_CHECK_BUTTON (check)));
|
|
|
|
gtk_list_box_set_activate_on_single_click (list, gtk_check_button_get_active (GTK_CHECK_BUTTON (check)));
|
2013-06-17 13:14:29 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 11:49:14 +00:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
GtkCssProvider *provider;
|
|
|
|
GtkWidget *window, *hbox, *vbox, *list, *row, *row3, *row_vbox, *row_hbox, *l;
|
2013-06-10 12:15:56 +00:00
|
|
|
GtkWidget *check, *button, *combo, *scrolled;
|
2013-06-10 11:49:14 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2013-06-10 11:49:14 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2013-06-10 11:49:14 +00:00
|
|
|
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), hbox);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
provider = gtk_css_provider_new ();
|
2016-10-17 14:09:12 +00:00
|
|
|
gtk_css_provider_load_from_data (provider, css, -1);
|
2017-10-31 01:35:21 +00:00
|
|
|
gtk_style_context_add_provider_for_display (gtk_widget_get_display (window),
|
|
|
|
GTK_STYLE_PROVIDER (provider),
|
|
|
|
GTK_STYLE_PROVIDER_PRIORITY_USER);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
list = gtk_list_box_new ();
|
|
|
|
|
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), vbox);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
combo = gtk_combo_box_text_new ();
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo),
|
|
|
|
"GTK_SELECTION_NONE");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo),
|
|
|
|
"GTK_SELECTION_SINGLE");
|
|
|
|
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo),
|
|
|
|
"GTK_SELECTION_BROWSE");
|
|
|
|
g_signal_connect (combo, "changed", G_CALLBACK (selection_mode_changed), list);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), combo);
|
2013-06-10 12:15:56 +00:00
|
|
|
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), gtk_list_box_get_selection_mode (GTK_LIST_BOX (list)));
|
2013-06-17 13:14:29 +00:00
|
|
|
check = gtk_check_button_new_with_label ("single click mode");
|
2020-08-30 23:36:27 +00:00
|
|
|
gtk_check_button_set_active (GTK_CHECK_BUTTON (check), gtk_list_box_get_activate_on_single_click (GTK_LIST_BOX (list)));
|
2013-06-17 13:14:29 +00:00
|
|
|
g_signal_connect (check, "toggled", G_CALLBACK (single_click_clicked), list);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), check);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
scrolled = gtk_scrolled_window_new ();
|
2013-06-10 12:15:56 +00:00
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled), list);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), scrolled);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
g_signal_connect (list, "row-activated", G_CALLBACK (row_activated_cb), NULL);
|
|
|
|
g_signal_connect (list, "row-selected", G_CALLBACK (row_selected_cb), NULL);
|
|
|
|
|
|
|
|
row = row_new ("blah4", 4);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
|
2013-06-10 11:49:14 +00:00
|
|
|
row3 = row = row_new ("blah3", 3);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
|
2013-06-10 11:49:14 +00:00
|
|
|
row = row_new ("blah1", 1);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
|
2013-06-10 11:49:14 +00:00
|
|
|
row = row_new ("blah2", 2);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
row = row_new (NULL, 0);
|
|
|
|
row_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
|
|
|
row_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
l = gtk_label_new ("da box for da man");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (row_hbox), l);
|
2013-06-10 11:49:14 +00:00
|
|
|
check = gtk_check_button_new ();
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (row_hbox), check);
|
2013-06-10 11:49:14 +00:00
|
|
|
button = gtk_button_new_with_label ("ya!");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (row_hbox), button);
|
|
|
|
gtk_box_append (GTK_BOX (row_vbox), row_hbox);
|
2013-06-10 11:49:14 +00:00
|
|
|
check = gtk_check_button_new ();
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (row_vbox), check);
|
2020-05-03 04:09:18 +00:00
|
|
|
gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), row_vbox);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
row = row_new (NULL, 0);
|
|
|
|
button = gtk_button_new_with_label ("focusable row");
|
|
|
|
gtk_widget_set_hexpand (button, FALSE);
|
|
|
|
gtk_widget_set_halign (button, GTK_ALIGN_START);
|
2020-05-03 04:09:18 +00:00
|
|
|
gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), button);
|
2020-05-08 06:19:18 +00:00
|
|
|
gtk_list_box_insert (GTK_LIST_BOX (list), row, -1);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (hbox), vbox);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("sort");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (sort_clicked_cb), list);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("reverse");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (reverse_sort_clicked_cb), list);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("change");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (change_clicked_cb), row3);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("filter");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (filter_clicked_cb), list);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("unfilter");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (unfilter_clicked_cb), list);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("add");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (add_clicked_cb), list);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("separate");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (separate_clicked_cb), list);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("unseparate");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (unseparate_clicked_cb), list);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("visibility");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), button);
|
2013-06-10 11:49:14 +00:00
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (visibility_clicked_cb), row3);
|
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (TRUE)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2013-06-10 11:49:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|