1 /* gbp-buildui-runtime-row.c
2  *
3  * Copyright 2018-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "gbp-buildui-runtime-row"
22 
23 #include "config.h"
24 
25 #include "gbp-buildui-runtime-row.h"
26 
27 struct _GbpBuilduiRuntimeRow
28 {
29   GtkListBoxRow  parent_instance;
30 
31   gchar         *runtime_id;
32 
33   GtkLabel      *label;
34   GtkImage      *image;
35 };
36 
G_DEFINE_FINAL_TYPE(GbpBuilduiRuntimeRow,gbp_buildui_runtime_row,GTK_TYPE_LIST_BOX_ROW)37 G_DEFINE_FINAL_TYPE (GbpBuilduiRuntimeRow, gbp_buildui_runtime_row, GTK_TYPE_LIST_BOX_ROW)
38 
39 static void
40 gbp_buildui_runtime_row_finalize (GObject *object)
41 {
42   GbpBuilduiRuntimeRow *self = (GbpBuilduiRuntimeRow *)object;
43 
44   g_clear_pointer (&self->runtime_id, g_free);
45 
46   G_OBJECT_CLASS (gbp_buildui_runtime_row_parent_class)->finalize (object);
47 }
48 
49 static void
gbp_buildui_runtime_row_class_init(GbpBuilduiRuntimeRowClass * klass)50 gbp_buildui_runtime_row_class_init (GbpBuilduiRuntimeRowClass *klass)
51 {
52   GObjectClass *object_class = G_OBJECT_CLASS (klass);
53 
54   object_class->finalize = gbp_buildui_runtime_row_finalize;
55 }
56 
57 static void
gbp_buildui_runtime_row_init(GbpBuilduiRuntimeRow * self)58 gbp_buildui_runtime_row_init (GbpBuilduiRuntimeRow *self)
59 {
60   GtkWidget *box;
61 
62   box = g_object_new (GTK_TYPE_BOX,
63                       "margin", 10,
64                       "orientation", GTK_ORIENTATION_HORIZONTAL,
65                       "spacing", 6,
66                       "visible", TRUE,
67                       NULL);
68   gtk_container_add (GTK_CONTAINER (self), box);
69 
70   self->label = g_object_new (GTK_TYPE_LABEL,
71                               "visible", TRUE,
72                               "use-markup", TRUE,
73                               "xalign", 0.0f,
74                               NULL);
75   gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (self->label));
76 
77   self->image = g_object_new (GTK_TYPE_IMAGE,
78                               "visible", TRUE,
79                               "halign", GTK_ALIGN_START,
80                               "hexpand", TRUE,
81                               "icon-name", "object-select-symbolic",
82                               NULL);
83   gtk_container_add (GTK_CONTAINER (box), GTK_WIDGET (self->image));
84 }
85 
86 static void
notify_config_runtime_id(GbpBuilduiRuntimeRow * self,GParamSpec * pspec,IdeConfig * config)87 notify_config_runtime_id (GbpBuilduiRuntimeRow *self,
88                           GParamSpec           *pspec,
89                           IdeConfig     *config)
90 {
91   g_assert (GBP_IS_BUILDUI_RUNTIME_ROW (self));
92   g_assert (IDE_IS_CONFIG (config));
93 
94   gtk_widget_set_visible (GTK_WIDGET (self->image),
95                           ide_str_equal0 (self->runtime_id,
96                                           ide_config_get_runtime_id (config)));
97 }
98 
99 GtkWidget *
gbp_buildui_runtime_row_new(IdeRuntime * runtime,IdeConfig * config)100 gbp_buildui_runtime_row_new (IdeRuntime       *runtime,
101                              IdeConfig *config)
102 {
103   GbpBuilduiRuntimeRow *self;
104   gboolean sensitive;
105 
106   g_return_val_if_fail (IDE_IS_RUNTIME (runtime), NULL);
107   g_return_val_if_fail (IDE_IS_CONFIG (config), NULL);
108 
109   sensitive = ide_config_supports_runtime (config, runtime);
110 
111   self = g_object_new (GBP_TYPE_BUILDUI_RUNTIME_ROW,
112                        "sensitive", sensitive,
113                        "visible", TRUE,
114                        NULL);
115   self->runtime_id = g_strdup (ide_runtime_get_id (runtime));
116   gtk_label_set_label (self->label,
117                        ide_runtime_get_display_name (runtime));
118 
119   g_signal_connect_object (config,
120                            "notify::runtime-id",
121                            G_CALLBACK (notify_config_runtime_id),
122                            self,
123                            G_CONNECT_SWAPPED);
124   gtk_widget_set_visible (GTK_WIDGET (self->image),
125                           ide_str_equal0 (self->runtime_id,
126                                           ide_config_get_runtime_id (config)));
127 
128   return GTK_WIDGET (self);
129 }
130 
131 const gchar *
gbp_buildui_runtime_row_get_id(GbpBuilduiRuntimeRow * self)132 gbp_buildui_runtime_row_get_id (GbpBuilduiRuntimeRow *self)
133 {
134   g_return_val_if_fail (GBP_IS_BUILDUI_RUNTIME_ROW (self), NULL);
135 
136   return self->runtime_id;
137 }
138