summaryrefslogtreecommitdiff
path: root/src/trg-tree-view.c
blob: 4f091ea4bb470dc7904bbd46455c2b6a72a909dd (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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
 * transmission-remote-gtk - Transmission RPC client for GTK
 * Copyright (C) 2011  Alan Fitton

 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdlib.h>
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include <glib/gi18n.h>

#include "trg-prefs.h"
#include "trg-tree-view.h"
#include "trg-cell-renderer-speed.h"
#include "trg-cell-renderer-size.h"
#include "trg-cell-renderer-ratio.h"
#include "trg-cell-renderer-eta.h"
#include "trg-cell-renderer-epoch.h"
#include "trg-cell-renderer-priority.h"
#include "trg-cell-renderer-numgteqthan.h"

/* A subclass of GtkTreeView which allows the user to change column visibility
 * by right clicking on any column for a menu to hide the clicked column, or
 * insert any hidden column after.
 *
 * This class persists these choices to TrgConf, and restores them when it is
 * initialised. Column widths are also saved/restored.
 *
 * All the columns must be preregistered so it knows what model column,
 * renderers etc to use if it should be created, and what columns are available.
 */

enum {
    PROP_0, PROP_PREFS
};

G_DEFINE_TYPE(TrgTreeView, trg_tree_view, GTK_TYPE_TREE_VIEW)
#define TRG_TREE_VIEW_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_TREE_VIEW, TrgTreeViewPrivate))
typedef struct _TrgTreeViewPrivate TrgTreeViewPrivate;

struct _TrgTreeViewPrivate {
    GList *columns;
    TrgPrefs *prefs;
};

gboolean trg_tree_view_is_column_showing(TrgTreeView *tv, gint index) {
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(tv);

    GList *li;
    for (li = priv->columns; li; li = g_list_next(li)) {
        trg_column_description *cd = (trg_column_description*) li->data;
        if (cd->model_column == index) {
            if (cd->flags & TRG_COLUMN_SHOWING
            )
                return TRUE;
            else
                break;
        }
    }

    return FALSE;
}

static void trg_tree_view_get_property(GObject * object, guint property_id,
        GValue * value, GParamSpec * pspec) {
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}

static void trg_tree_view_set_property(GObject * object, guint property_id,
        const GValue * value, GParamSpec * pspec) {
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(object);
    switch (property_id) {
    case PROP_PREFS:
        priv->prefs = g_value_get_object(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        break;
    }
}

static GObject *trg_tree_view_constructor(GType type,
        guint n_construct_properties, GObjectConstructParam * construct_params) {
    GObject *obj = G_OBJECT_CLASS
    (trg_tree_view_parent_class)->constructor(type,
            n_construct_properties, construct_params);

    return obj;
}

static JsonObject* trg_prefs_get_tree_view_props(TrgTreeView *tv) {
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(tv);
    JsonObject *root = trg_prefs_get_root(priv->prefs);
    const gchar *className = G_OBJECT_TYPE_NAME(tv);
    JsonObject *obj;
    JsonObject *tvProps = NULL;

    if (!json_object_has_member(root, TRG_PREFS_KEY_TREE_VIEWS)) {
        obj = json_object_new();
        json_object_set_object_member(root, TRG_PREFS_KEY_TREE_VIEWS, obj);
    } else {
        obj = json_object_get_object_member(root, TRG_PREFS_KEY_TREE_VIEWS);
    }

    if (!json_object_has_member(obj, className)) {
        tvProps = json_object_new();
        json_object_set_object_member(obj, className, tvProps);
    } else {
        tvProps = json_object_get_object_member(obj, className);
    }

    return tvProps;
}

static void trg_tree_view_add_column_after(TrgTreeView * tv,
        trg_column_description * desc, gint64 width,
        GtkTreeViewColumn * after_col);

trg_column_description *trg_tree_view_reg_column(TrgTreeView * tv, gint type,
        gint model_column, gchar * header, gchar * id, gint flags) {
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(tv);
    trg_column_description *desc = g_new0(trg_column_description, 1);

    desc->type = type;
    desc->model_column = model_column;
    desc->header = g_strdup(header);
    desc->id = g_strdup(id);
    desc->flags = flags;

    priv->columns = g_list_append(priv->columns, desc);

    return desc;
}

static trg_column_description *trg_tree_view_find_column(TrgTreeView * tv,
        const gchar * id) {
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(tv);
    GList *li;
    trg_column_description *desc;

    for (li = priv->columns; li; li = g_list_next(li)) {
        desc = (trg_column_description *) li->data;
        if (!g_strcmp0(desc->id, id))
            return desc;
    }

    return NULL;
}

static void trg_tree_view_hide_column(GtkWidget * w, GtkTreeViewColumn * col) {
    trg_column_description *desc = g_object_get_data(G_OBJECT(col),
            "column-desc");
    GtkWidget *tv = gtk_tree_view_column_get_tree_view(col);
    desc->flags &= ~TRG_COLUMN_SHOWING;
    gtk_tree_view_remove_column(GTK_TREE_VIEW(tv), col);
}

static void trg_tree_view_add_column(TrgTreeView * tv,
        trg_column_description * desc, gint64 width) {
    trg_tree_view_add_column_after(tv, desc, width, NULL);
}

static void trg_tree_view_user_add_column_cb(GtkWidget * w,
        trg_column_description * desc) {
    GtkTreeViewColumn *col = g_object_get_data(G_OBJECT(w), "parent-col");
    TrgTreeView *tv = TRG_TREE_VIEW(gtk_tree_view_column_get_tree_view(col));

    trg_tree_view_add_column_after(tv, desc, -1, col);
}

static void view_popup_menu(GtkButton * button, GdkEventButton * event,
        GtkTreeViewColumn * column) {
    GtkWidget *tv = gtk_tree_view_column_get_tree_view(column);
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(tv);
    GtkWidget *menu, *menuitem;
    trg_column_description *desc;
    GList *li;

    menu = gtk_menu_new();

    desc = g_object_get_data(G_OBJECT(column), "column-desc");
    menuitem = gtk_check_menu_item_new_with_label(desc->header);
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menuitem), TRUE);
    g_signal_connect(menuitem, "activate",
            G_CALLBACK(trg_tree_view_hide_column), column);
    gtk_widget_set_sensitive(menuitem, !(desc->flags & TRG_COLUMN_UNREMOVABLE));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);

    for (li = priv->columns; li; li = g_list_next(li)) {
        trg_column_description *desc = (trg_column_description *) li->data;
        if (!(desc->flags & TRG_COLUMN_SHOWING)) {
            menuitem = gtk_check_menu_item_new_with_label(desc->header);
            g_object_set_data(G_OBJECT(menuitem), "parent-col", column);
            g_signal_connect(menuitem, "activate",
                    G_CALLBACK(trg_tree_view_user_add_column_cb), desc);
            gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
        }
    }
    gtk_widget_show_all(menu);

    gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
            (event != NULL) ? event->button : 0,
            gdk_event_get_time((GdkEvent *) event));
        }

static gboolean col_onButtonPressed(GtkButton * button, GdkEventButton * event,
        GtkTreeViewColumn * col) {
    if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
        view_popup_menu(button, event, col);
        return TRUE;
    }

    return FALSE;
}

static GtkTreeViewColumn *trg_tree_view_icontext_column_new(
        trg_column_description *desc, gchar *renderer_property) {
    GtkTreeViewColumn *column = gtk_tree_view_column_new();
    GtkCellRenderer *renderer = gtk_cell_renderer_pixbuf_new();

    gtk_tree_view_column_set_title(column, desc->header);
    gtk_tree_view_column_pack_start(column, renderer, FALSE);
    gtk_tree_view_column_set_attributes(column, renderer, renderer_property,
            desc->model_column_icon, NULL);

    renderer = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(column, renderer, TRUE);
    gtk_tree_view_column_set_attributes(column, renderer, "text",
            desc->model_column, NULL);

    return column;
}

static void trg_tree_view_add_column_after(TrgTreeView * tv,
        trg_column_description * desc, gint64 width,
        GtkTreeViewColumn * after_col) {
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column = NULL;

    switch (desc->type) {
    case TRG_COLTYPE_TEXT:
        renderer =
                desc->customRenderer ? desc->customRenderer :
                        gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "text", desc->model_column, NULL);

        break;
    case TRG_COLTYPE_SPEED:
        renderer = trg_cell_renderer_speed_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "speed-value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_EPOCH:
        renderer = trg_cell_renderer_epoch_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "epoch-value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_ETA:
        renderer = trg_cell_renderer_eta_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "eta-value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_SIZE:
        renderer = trg_cell_renderer_size_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "size-value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_PROG:
        renderer = gtk_cell_renderer_progress_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_RATIO:
        renderer = trg_cell_renderer_ratio_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "ratio-value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_ICON:
        column = gtk_tree_view_column_new();
        renderer = gtk_cell_renderer_pixbuf_new();
        gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), 0.5f, 0.0);
        gtk_tree_view_column_set_title(column, desc->header);
        gtk_tree_view_column_pack_start(column, renderer, TRUE);
        gtk_tree_view_column_set_attributes(column, renderer, "stock-id",
                desc->model_column, NULL);
        break;
    case TRG_COLTYPE_STOCKICONTEXT:
        column = trg_tree_view_icontext_column_new(desc, "stock-id");
        break;
    case TRG_COLTYPE_GICONTEXT:
        column = trg_tree_view_icontext_column_new(desc, "gicon");
        break;
    case TRG_COLTYPE_PRIO:
        renderer = trg_cell_renderer_priority_new();
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "priority-value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_NUMGTZERO:
        renderer = trg_cell_renderer_numgteqthan_new(1);
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "value", desc->model_column, NULL);
        break;
    case TRG_COLTYPE_NUMGTEQZERO:
        renderer = trg_cell_renderer_numgteqthan_new(0);
        column = gtk_tree_view_column_new_with_attributes(desc->header,
                renderer, "value", desc->model_column, NULL);
        break;
    }

    gtk_tree_view_column_set_min_width(column, 0);
    gtk_tree_view_column_set_resizable(column, TRUE);
    gtk_tree_view_column_set_reorderable(column, TRUE);
    gtk_tree_view_column_set_sort_column_id(column, desc->model_column);

    if (width < 0 && desc->defaultWidth > 0) {
        gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
        gtk_tree_view_column_set_fixed_width(column, desc->defaultWidth);
    } else if (width > 0) {
        gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
        gtk_tree_view_column_set_fixed_width(column, width);
    }

    g_object_set_data(G_OBJECT(column), "column-desc", desc);

    gtk_tree_view_append_column(GTK_TREE_VIEW(tv), column);

    if (after_col)
        gtk_tree_view_move_column_after(GTK_TREE_VIEW(tv), column, after_col);

#if GTK_CHECK_VERSION( 3,0,0 )
    g_signal_connect(gtk_tree_view_column_get_button(column), "button-press-event",
            G_CALLBACK(col_onButtonPressed), column);
#else
    g_signal_connect(column->button, "button-press-event",
            G_CALLBACK(col_onButtonPressed), column);
#endif

    if (desc->out)
        *(desc->out) = column;

    desc->flags |= TRG_COLUMN_SHOWING;
}

void trg_tree_view_persist(TrgTreeView * tv, gboolean parentIsSortable) {
    JsonObject *props = trg_prefs_get_tree_view_props(tv);
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tv));
    GList *cols = gtk_tree_view_get_columns(GTK_TREE_VIEW(tv));

    GList *li;
    gint sort_column_id;
    GtkSortType sort_type;

    JsonArray *widths, *columns;
    if (json_object_has_member(props, TRG_PREFS_KEY_TV_WIDTHS))
        json_object_remove_member(props, TRG_PREFS_KEY_TV_WIDTHS);

    widths = json_array_new();
    json_object_set_array_member(props, TRG_PREFS_KEY_TV_WIDTHS, widths);

    if (json_object_has_member(props, TRG_PREFS_KEY_TV_COLUMNS))
        json_object_remove_member(props, TRG_PREFS_KEY_TV_COLUMNS);

    columns = json_array_new();
    json_object_set_array_member(props, TRG_PREFS_KEY_TV_COLUMNS, columns);

    for (li = cols; li; li = g_list_next(li)) {
        GtkTreeViewColumn *col = (GtkTreeViewColumn *) li->data;
        trg_column_description *desc = g_object_get_data(G_OBJECT(li->data),
                "column-desc");

        json_array_add_string_element(columns, desc->id);
        json_array_add_int_element(widths, gtk_tree_view_column_get_width(col));
    }

    g_list_free(cols);

    gtk_tree_sortable_get_sort_column_id(
            GTK_TREE_SORTABLE(parentIsSortable ? gtk_tree_model_filter_get_model(
                            GTK_TREE_MODEL_FILTER(model)) :
                    model), &sort_column_id, &sort_type);

    if (json_object_has_member(props, TRG_PREFS_KEY_TV_SORT_COL))
        json_object_remove_member(props, TRG_PREFS_KEY_TV_SORT_COL);

    if (json_object_has_member(props, TRG_PREFS_KEY_TV_SORT_TYPE))
        json_object_remove_member(props, TRG_PREFS_KEY_TV_SORT_TYPE);

    json_object_set_int_member(props, TRG_PREFS_KEY_TV_SORT_COL,
            (gint64) sort_column_id);
    json_object_set_int_member(props, TRG_PREFS_KEY_TV_SORT_TYPE,
            (gint64) sort_type);
}

void trg_tree_view_restore_sort(TrgTreeView * tv, gboolean parentIsSortable) {
    JsonObject *props = trg_prefs_get_tree_view_props(tv);
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(tv));

    if (json_object_has_member(props, TRG_PREFS_KEY_TV_SORT_COL)
            && json_object_has_member(props, TRG_PREFS_KEY_TV_SORT_TYPE)) {
        gint64 sort_col = json_object_get_int_member(props,
                TRG_PREFS_KEY_TV_SORT_COL);
        gint64 sort_type = json_object_get_int_member(props,
                TRG_PREFS_KEY_TV_SORT_TYPE);
        gtk_tree_sortable_set_sort_column_id(
                GTK_TREE_SORTABLE(parentIsSortable ? gtk_tree_model_filter_get_model(
                                GTK_TREE_MODEL_FILTER(model)) :
                        model), sort_col, (GtkSortType) sort_type);

    }
}

void trg_tree_view_set_prefs(TrgTreeView *tv, TrgPrefs *prefs) {
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(tv);
    priv->prefs = prefs;
}

void trg_tree_view_setup_columns(TrgTreeView * tv) {
    TrgTreeViewPrivate *priv = TRG_TREE_VIEW_GET_PRIVATE(tv);
    JsonObject *props = trg_prefs_get_tree_view_props(tv);

    if (!json_object_has_member(props, TRG_PREFS_KEY_TV_COLUMNS)
            || !json_object_has_member(props, TRG_PREFS_KEY_TV_WIDTHS)) {
        GList *li;
        for (li = priv->columns; li; li = g_list_next(li)) {
            trg_column_description *desc = (trg_column_description *) li->data;
            if (desc && !(desc->flags & TRG_COLUMN_EXTRA))
                trg_tree_view_add_column(tv, desc, -1);
        }
        return;
    }

    GList *columns = json_array_get_elements(
            json_object_get_array_member(props, TRG_PREFS_KEY_TV_COLUMNS));
    GList *widths = json_array_get_elements(
            json_object_get_array_member(props, TRG_PREFS_KEY_TV_WIDTHS));

    GList *wli = widths;
    GList *cli = columns;

    for (; cli; cli = g_list_next(cli)) {
        trg_column_description *desc = trg_tree_view_find_column(tv,
                json_node_get_string((JsonNode*) cli->data));
        if (desc) {
            gint64 width = json_node_get_int((JsonNode*) wli->data);
            trg_tree_view_add_column(tv, desc, width);
        }
        wli = g_list_next(wli);
    }

    g_list_free(columns);
    g_list_free(widths);
}

GList *trg_tree_view_get_selected_refs_list(GtkTreeView * tv) {
    GtkTreeModel *model = gtk_tree_view_get_model(tv);
    GtkTreeSelection *selection = gtk_tree_view_get_selection(tv);
    GList *li, *selectionList;
    GList *refList = NULL;

    selectionList = gtk_tree_selection_get_selected_rows(selection, NULL);
    for (li = selectionList; li != NULL; li = g_list_next(li)) {
        GtkTreePath *path = (GtkTreePath *) li->data;
        GtkTreeRowReference *ref = gtk_tree_row_reference_new(model, path);
        gtk_tree_path_free(path);
        refList = g_list_append(refList, ref);
    }
    g_list_free(selectionList);

    return refList;
}

static void trg_tree_view_class_init(TrgTreeViewClass * klass) {
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    g_type_class_add_private(klass, sizeof(TrgTreeViewPrivate));

    object_class->get_property = trg_tree_view_get_property;
    object_class->set_property = trg_tree_view_set_property;
    object_class->constructor = trg_tree_view_constructor;

    g_object_class_install_property(
            object_class,
            PROP_PREFS,
            g_param_spec_object(
                    "prefs",
                    "Trg Prefs",
                    "Trg Prefs",
                    TRG_TYPE_PREFS,
                    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
                            | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK
                            | G_PARAM_STATIC_BLURB));
}

static void trg_tree_view_init(TrgTreeView * tv) {
    gtk_tree_view_set_rubber_banding(GTK_TREE_VIEW(tv), TRUE);
    gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(tv), TRUE);
    gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tv), TRUE);
    gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(tv)),
            GTK_SELECTION_MULTIPLE);

    gtk_widget_set_sensitive(GTK_WIDGET(tv), FALSE);
}

GtkWidget *trg_tree_view_new(void) {
    return GTK_WIDGET(g_object_new(TRG_TYPE_TREE_VIEW, NULL));
}