ide-build-perspective.c 14.4 KB
Newer Older
1
/* ide-build-perspective.c
2
 *
3
 * Copyright © 2016 Christian Hergert <chergert@redhat.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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 3 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/>.
 */

19 20
#define G_LOG_DOMAIN "ide-build-perspective"

21 22
#include "config.h"

23 24
#include <glib/gi18n.h>

25 26 27
#include "buildui/ide-build-configuration-row.h"
#include "buildui/ide-build-configuration-view.h"
#include "buildui/ide-build-perspective.h"
28

29
struct _IdeBuildPerspective
30 31 32 33 34 35 36
{
  GtkBin                     parent_instance;

  IdeConfiguration          *configuration;
  IdeConfigurationManager   *configuration_manager;

  GtkListBox                *list_box;
37
  IdeBuildConfigurationView *view;
38 39 40 41 42 43 44 45 46 47 48
};

enum {
  PROP_0,
  PROP_CONFIGURATION,
  PROP_CONFIGURATION_MANAGER,
  LAST_PROP
};

static void perspective_iface_init (IdePerspectiveInterface *iface);

49
G_DEFINE_TYPE_EXTENDED (IdeBuildPerspective, ide_build_perspective, GTK_TYPE_BIN, 0,
50 51 52 53 54 55 56 57 58 59
                        G_IMPLEMENT_INTERFACE (IDE_TYPE_PERSPECTIVE, perspective_iface_init))

static GParamSpec *properties [LAST_PROP];

static gboolean
map_pointer_to (GBinding     *binding,
                const GValue *from_value,
                GValue       *to_value,
                gpointer      user_data)
{
60
  g_value_set_boolean (to_value, user_data == g_value_get_object (from_value));
61 62 63
  return TRUE;
}

64 65 66 67 68 69
static void
select_first_row (GtkWidget *widget,
                  gpointer   user_data)
{
  gboolean *selected = user_data;

70
  g_assert (IDE_IS_BUILD_CONFIGURATION_ROW (widget));
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
  g_assert (selected != NULL);

  if (*selected == FALSE)
    {
      *selected = TRUE;
      gtk_list_box_select_row (GTK_LIST_BOX (gtk_widget_get_parent (widget)),
                               GTK_LIST_BOX_ROW (widget));
    }
}

static gboolean
update_selection_in_main (gpointer data)
{
  g_autoptr(GtkListBox) list_box = data;
  gboolean selected = FALSE;

  g_assert (GTK_IS_LIST_BOX (list_box));

  if (!gtk_widget_in_destruction (GTK_WIDGET (list_box)))
    {
      if (NULL == gtk_list_box_get_selected_row (list_box))
        {
          gtk_container_foreach (GTK_CONTAINER (list_box),
                                 select_first_row,
                                 &selected);
        }
    }

  return G_SOURCE_REMOVE;
}

102 103 104 105 106 107
static GtkWidget *
create_configuration_row (gpointer item,
                          gpointer user_data)
{
  IdeConfigurationManager *manager = user_data;
  IdeConfiguration *configuration = item;
108
  GtkWidget *ret;
109 110 111 112

  g_assert (IDE_IS_CONFIGURATION (configuration));
  g_assert (IDE_IS_CONFIGURATION_MANAGER (manager));

113
  ret = g_object_new (IDE_TYPE_BUILD_CONFIGURATION_ROW,
114
                      "configuration", configuration,
115 116 117
                      "visible", TRUE,
                      NULL);

118
  g_object_bind_property_full (manager, "current", ret, "active",
119 120
                               G_BINDING_SYNC_CREATE,
                               map_pointer_to, NULL, configuration, NULL);
121

122
  return ret;
123 124 125
}

static void
126
ide_build_perspective_set_configuration_manager (IdeBuildPerspective     *self,
127 128
                                                 IdeConfigurationManager *manager)
{
129
  g_assert (IDE_IS_BUILD_PERSPECTIVE (self));
130 131 132 133 134 135 136 137
  g_assert (IDE_IS_CONFIGURATION_MANAGER (manager));

  g_set_object (&self->configuration_manager, manager);
  gtk_list_box_bind_model (self->list_box,
                           G_LIST_MODEL (manager),
                           create_configuration_row,
                           g_object_ref (manager),
                           g_object_unref);
138 139 140 141 142 143 144 145

  update_selection_in_main (g_object_ref (self->list_box));
}

static void
update_selected_state (GtkWidget *widget,
                       gpointer   user_data)
{
146
  IdeBuildConfigurationRow *row = (IdeBuildConfigurationRow *)widget;
147 148 149
  IdeConfiguration *selected = user_data;
  IdeConfiguration *config;

150
  g_assert (IDE_IS_BUILD_CONFIGURATION_ROW (row));
151 152
  g_assert (IDE_IS_CONFIGURATION (selected));

153
  config = ide_build_configuration_row_get_configuration (row);
154 155 156 157

  g_object_set (row,
                "selected", (config == selected),
                NULL);
158 159 160
}

static void
161 162
ide_build_perspective_row_selected (IdeBuildPerspective      *self,
                                    IdeBuildConfigurationRow *row,
163
                                    GtkListBox               *list_box)
164
{
165 166
  g_assert (IDE_IS_BUILD_PERSPECTIVE (self));
  g_assert (!row || IDE_IS_BUILD_CONFIGURATION_ROW (row));
167 168 169 170 171 172
  g_assert (GTK_IS_LIST_BOX (list_box));

  if (row != NULL)
    {
      IdeConfiguration *configuration;

173
      configuration = ide_build_configuration_row_get_configuration (row);
174
      g_set_object (&self->configuration, configuration);
175
      ide_build_configuration_view_set_configuration (self->view, configuration);
176 177 178 179 180 181 182 183 184 185 186

      gtk_container_foreach (GTK_CONTAINER (list_box),
                             update_selected_state,
                             configuration);
    }
  else
    {
      /* Possibly wait for a new row to be added (the new default config)
       * and select it in the main loop.
       */
      g_timeout_add (0, update_selection_in_main, g_object_ref (list_box));
187 188 189 190
    }
}

static void
191 192
ide_build_perspective_row_activated (IdeBuildPerspective      *self,
                                     IdeBuildConfigurationRow *row,
193
                                     GtkListBox               *list_box)
194 195 196
{
  IdeConfiguration *configuration;

197 198
  g_assert (IDE_IS_BUILD_PERSPECTIVE (self));
  g_assert (IDE_IS_BUILD_CONFIGURATION_ROW (row));
199 200
  g_assert (GTK_IS_LIST_BOX (list_box));

201
  configuration = ide_build_configuration_row_get_configuration (row);
202 203 204 205 206 207 208 209
  ide_configuration_manager_set_current (self->configuration_manager, configuration);
}

static void
duplicate_configuration (GSimpleAction *action,
                         GVariant      *variant,
                         gpointer       user_data)
{
210
  IdeBuildPerspective *self = user_data;
211

212
  g_assert (IDE_IS_BUILD_PERSPECTIVE (self));
213 214 215

  if (self->configuration != NULL)
    {
216 217
      IdeContext *context;
      IdeConfigurationManager *config_manager;
218

219 220 221
      context = ide_widget_get_context (GTK_WIDGET (self));
      config_manager = ide_context_get_configuration_manager (context);
      ide_configuration_manager_duplicate (config_manager, self->configuration);
222 223 224 225 226 227 228 229
    }
}

static void
delete_configuration (GSimpleAction *action,
                      GVariant      *variant,
                      gpointer       user_data)
{
230
  IdeBuildPerspective *self = user_data;
231

232
  g_assert (IDE_IS_BUILD_PERSPECTIVE (self));
233 234 235 236 237 238 239 240 241 242

  if (self->configuration != NULL)
    {
      g_autoptr(IdeConfiguration) config = NULL;

      /*
       * Make sure we hold onto a reference during the call, as it is likely
       * self->configuration will change during this call.
       */
      config = g_object_ref (self->configuration);
243
      ide_configuration_manager_delete (self->configuration_manager, config);
244 245 246 247 248 249 250 251 252 253 254 255

      /*
       * Switch to the first configuration in the list. The configuration
       * manager should have added a new "default" configuration if we
       * deleted the last configuration, so we should just get the 0th
       * index.
       */
      if (g_list_model_get_n_items (G_LIST_MODEL (self->configuration_manager)) > 0)
        {
          g_autoptr(IdeConfiguration) first = NULL;

          first = g_list_model_get_item (G_LIST_MODEL (self->configuration_manager), 0);
256
          ide_build_perspective_set_configuration (self, first);
257 258 259 260
        }
    }
}

261 262 263 264 265 266 267 268 269 270
static void
ide_build_perspective_grab_focus (GtkWidget *widget)
{
  IdeBuildPerspective *self = (IdeBuildPerspective *)widget;

  g_assert (IDE_IS_BUILD_PERSPECTIVE (self));

  gtk_widget_child_focus (GTK_WIDGET (self->list_box), GTK_DIR_TAB_FORWARD);
}

271
static void
272
ide_build_perspective_finalize (GObject *object)
273
{
274
  IdeBuildPerspective *self = (IdeBuildPerspective *)object;
275 276 277

  g_clear_object (&self->configuration);

278
  G_OBJECT_CLASS (ide_build_perspective_parent_class)->finalize (object);
279 280 281
}

static void
282
ide_build_perspective_get_property (GObject    *object,
283 284 285 286
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
287
  IdeBuildPerspective *self = IDE_BUILD_PERSPECTIVE (object);
288 289 290 291

  switch (prop_id)
    {
    case PROP_CONFIGURATION:
292
      g_value_set_object (value, ide_build_perspective_get_configuration (self));
293 294 295 296 297 298 299 300 301 302 303 304
      break;

    case PROP_CONFIGURATION_MANAGER:
      g_value_set_object (value, self->configuration_manager);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
305
ide_build_perspective_set_property (GObject      *object,
306 307 308 309
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
310
  IdeBuildPerspective *self = IDE_BUILD_PERSPECTIVE (object);
311 312 313 314

  switch (prop_id)
    {
    case PROP_CONFIGURATION:
315
      ide_build_perspective_set_configuration (self, g_value_get_object (value));
316 317 318
      break;

    case PROP_CONFIGURATION_MANAGER:
319
      ide_build_perspective_set_configuration_manager (self, g_value_get_object (value));
320 321 322 323 324 325 326 327
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
328
ide_build_perspective_class_init (IdeBuildPerspectiveClass *klass)
329 330 331 332
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

333 334 335
  object_class->finalize = ide_build_perspective_finalize;
  object_class->get_property = ide_build_perspective_get_property;
  object_class->set_property = ide_build_perspective_set_property;
336

337 338
  widget_class->grab_focus = ide_build_perspective_grab_focus;

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  properties [PROP_CONFIGURATION_MANAGER] =
    g_param_spec_object ("configuration-manager",
                         "Configuration Manager",
                         "Configuration Manager",
                         IDE_TYPE_CONFIGURATION_MANAGER,
                         (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  properties [PROP_CONFIGURATION] =
    g_param_spec_object ("configuration",
                         "Configuration",
                         "The configuration to edit",
                         IDE_TYPE_CONFIGURATION,
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, LAST_PROP, properties);

355
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/builder/plugins/buildui/ide-build-perspective.ui");
356
  gtk_widget_class_set_css_name (widget_class, "buildperspective");
357 358
  gtk_widget_class_bind_template_child (widget_class, IdeBuildPerspective, list_box);
  gtk_widget_class_bind_template_child (widget_class, IdeBuildPerspective, view);
359

360
  g_type_ensure (IDE_TYPE_BUILD_CONFIGURATION_VIEW);
361 362
}

363 364 365 366 367
static const GActionEntry actions[] = {
  { "delete-configuration", delete_configuration },
  { "duplicate-configuration", duplicate_configuration },
};

368
static void
369
ide_build_perspective_init (IdeBuildPerspective *self)
370
{
371
  g_autoptr(GSimpleActionGroup) group = NULL;
372 373 374 375 376

  gtk_widget_init_template (GTK_WIDGET (self));

  g_signal_connect_object (self->list_box,
                           "row-selected",
377
                           G_CALLBACK (ide_build_perspective_row_selected),
378 379 380 381 382
                           self,
                           G_CONNECT_SWAPPED);

  g_signal_connect_object (self->list_box,
                           "row-activated",
383
                           G_CALLBACK (ide_build_perspective_row_activated),
384 385 386
                           self,
                           G_CONNECT_SWAPPED);

387 388 389
  group = g_simple_action_group_new ();
  g_action_map_add_action_entries (G_ACTION_MAP (group), actions, G_N_ELEMENTS (actions), self);
  gtk_widget_insert_action_group (GTK_WIDGET (self), "build-preferences", G_ACTION_GROUP (group));
390 391 392
}

GtkWidget *
393
ide_build_perspective_new (void)
394
{
395
  return g_object_new (IDE_TYPE_BUILD_PERSPECTIVE, NULL);
396 397 398
}

IdeConfiguration *
399
ide_build_perspective_get_configuration (IdeBuildPerspective *self)
400
{
401
  g_return_val_if_fail (IDE_IS_BUILD_PERSPECTIVE (self), NULL);
402 403 404 405 406 407 408 409 410 411 412 413

  return self->configuration;
}

static void
find_configuration_row (GtkWidget *widget,
                        gpointer   data)
{
  struct {
    IdeConfiguration *config;
    GtkWidget        *row;
  } *lookup = data;
414
  IdeBuildConfigurationRow *row = (IdeBuildConfigurationRow *)widget;
415

416
  g_assert (IDE_IS_BUILD_CONFIGURATION_ROW (row));
417 418 419 420

  if (lookup->row != NULL)
    return;

421
  if (lookup->config == ide_build_configuration_row_get_configuration (row))
422 423 424 425
    lookup->row = widget;
}

void
426
ide_build_perspective_set_configuration (IdeBuildPerspective *self,
427 428 429 430 431 432 433
                                         IdeConfiguration    *configuration)
{
  struct {
    IdeConfiguration *config;
    GtkWidget        *row;
  } lookup = { configuration, NULL };

434
  g_return_if_fail (IDE_IS_BUILD_PERSPECTIVE (self));
435 436 437 438 439 440 441 442 443 444 445
  g_return_if_fail (!configuration || IDE_IS_CONFIGURATION (configuration));

  gtk_container_foreach (GTK_CONTAINER (self->list_box),
                         find_configuration_row,
                         &lookup);

  if (GTK_IS_LIST_BOX_ROW (lookup.row))
    gtk_list_box_select_row (self->list_box, GTK_LIST_BOX_ROW (lookup.row));
}

static gchar *
446
ide_build_perspective_get_icon_name (IdePerspective *perspective)
447 448 449 450 451
{
  return g_strdup ("builder-build-configure-symbolic");
}

static gchar *
452
ide_build_perspective_get_title (IdePerspective *perspective)
453
{
454
  return g_strdup (_("Build Preferences"));
455 456 457
}

static gchar *
458
ide_build_perspective_get_id (IdePerspective *perspective)
459 460 461 462
{
  return g_strdup ("buildperspective");
}

463
static gchar *
464
ide_build_perspective_get_accelerator (IdePerspective *perspective)
465 466 467 468
{
  return g_strdup ("<alt>comma");
}

469 470 471
static void
perspective_iface_init (IdePerspectiveInterface *iface)
{
472 473 474 475
  iface->get_icon_name = ide_build_perspective_get_icon_name;
  iface->get_title = ide_build_perspective_get_title;
  iface->get_id = ide_build_perspective_get_id;
  iface->get_accelerator = ide_build_perspective_get_accelerator;
476
}