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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
/*
* $Id: resource-editor.c 25044 2022-09-23 09:27:47Z yeti-dn $
* Copyright (C) 2005 David Necas (Yeti), Petr Klapetek.
* E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
*
* 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 "config.h"
#include <string.h>
#include <gtk/gtk.h>
#include <libgwyddion/gwymacros.h>
#include <app/menu.h>
#include <app/settings.h>
#include <app/app.h>
#include <app/resource-editor.h>
#include <libgwydgets/gwyoptionmenus.h>
#include <libgwydgets/gwyinventorystore.h>
#include <libgwydgets/gwydgetutils.h>
enum {
COMMIT_TIMEOUT = 120
};
enum {
SELECTED_ANY = 1 << 0,
SELECTED_EDITABLE = 1 << 1,
SELECTED_MASK = 0x03
};
static void gwy_resource_editor_finalize (GObject *object);
static void gwy_resource_editor_cell_name (GtkTreeViewColumn *column,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data);
static void gwy_resource_editor_changed (GtkTreeSelection *selection,
GwyResourceEditor *editor);
static void gwy_resource_editor_unmap (GtkWidget *widget);
static void gwy_resource_editor_destroy (GtkObject *object);
static void gwy_resource_editor_new (GwyResourceEditor *editor);
static void gwy_resource_editor_duplicate (GwyResourceEditor *editor);
static void gwy_resource_editor_copy (GwyResourceEditor *editor,
const gchar *name,
const gchar *newname);
static void gwy_resource_editor_delete (GwyResourceEditor *editor);
static void gwy_resource_editor_set_default (GwyResourceEditor *editor);
static void gwy_resource_editor_edit (GwyResourceEditor *editor);
static void gwy_resource_editor_row_activated (GwyResourceEditor *editor,
GtkTreePath *path,
GtkTreeViewColumn *column);
static void gwy_resource_editor_edit_resource (GwyResourceEditor *editor,
const gchar *name);
static void gwy_resource_editor_editor_closed (GwyResourceEditor *editor);
static void gwy_resource_editor_name_edited (GwyResourceEditor *editor,
const gchar *strpath,
const gchar *text);
static gboolean gwy_resource_editor_save (GwyResourceEditor *editor,
const gchar *name);
static void gwy_resource_editor_update_title (GwyResourceEditor *editor);
static GwyResource* gwy_resource_editor_get_active(GwyResourceEditor *editor,
GtkTreeModel **model,
GtkTreeIter *iter,
const gchar *warnwhat);
G_DEFINE_ABSTRACT_TYPE(GwyResourceEditor, gwy_resource_editor,
GTK_TYPE_WINDOW)
static void
gwy_resource_editor_class_init(GwyResourceEditorClass *klass)
{
GtkObjectClass *object_class = GTK_OBJECT_CLASS(klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = gwy_resource_editor_finalize;
object_class->destroy = gwy_resource_editor_destroy;
widget_class->unmap = gwy_resource_editor_unmap;
}
static void
gwy_resource_editor_finalize(GObject *object)
{
GwyResourceEditorClass *klass;
klass = GWY_RESOURCE_EDITOR_GET_CLASS(object);
klass->instance = NULL;
G_OBJECT_CLASS(gwy_resource_editor_parent_class)->finalize(object);
}
static void
gwy_resource_editor_init(G_GNUC_UNUSED GwyResourceEditor *editor)
{
}
/**
* gwy_resource_editor_setup:
* @editor: A resource editor.
*
* Sets up particular resource editor.
*
* Helper method only intended for resource editor implementation.
* To be called in particular resource initialization methods.
**/
void
gwy_resource_editor_setup(GwyResourceEditor *editor)
{
static const struct {
const gchar *stock_id;
const gchar *tooltip;
GCallback callback;
guint sens_flags;
}
toolbar_buttons[] = {
{
GTK_STOCK_EDIT,
N_("Edit selected item"),
G_CALLBACK(gwy_resource_editor_edit),
SELECTED_EDITABLE,
},
{
GTK_STOCK_NEW,
N_("Create a new item"),
G_CALLBACK(gwy_resource_editor_new),
0,
},
{
GTK_STOCK_COPY,
N_("Create a new item based on selected one"),
G_CALLBACK(gwy_resource_editor_duplicate),
SELECTED_ANY,
},
{
GTK_STOCK_DELETE,
N_("Delete selected item"),
G_CALLBACK(gwy_resource_editor_delete),
SELECTED_EDITABLE,
},
{
"gwy_favourite",
N_("Set selected item as default"),
G_CALLBACK(gwy_resource_editor_set_default),
SELECTED_ANY,
},
};
GwyResourceEditorClass *klass;
GwyResourceClass *rklass;
GtkTreeViewColumn *column;
GwyInventory *inventory;
GtkTreeModel *model;
GwyContainer *settings;
GtkWidget *hbox, *button, *image;
GtkWidget *scwin;
const guchar *name;
GList *rlist;
gchar *key;
guint i;
klass = GWY_RESOURCE_EDITOR_GET_CLASS(editor);
if (klass->instance) {
g_warning("An instance of this editor already exists. "
"This is not going to work.");
}
klass->instance = editor;
settings = gwy_app_settings_get();
name = klass->base_resource;
key = g_strconcat(klass->settings_prefix, "/current", NULL);
gwy_container_gis_string_by_name(settings, key, &name);
g_free(key);
rklass = g_type_class_ref(klass->resource_type);
/* TODO: handle errors */
gwy_resource_class_mkdir(rklass);
g_type_class_unref(rklass);
/* Window setup */
gtk_window_set_resizable(GTK_WINDOW(editor), TRUE);
gtk_window_set_title(GTK_WINDOW(editor), klass->window_title);
gwy_app_add_main_accel_group(GTK_WINDOW(editor));
editor->sensgroup = gwy_sensitivity_group_new();
editor->vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(editor), editor->vbox);
/* Treeview */
scwin = gtk_scrolled_window_new(NULL, NULL);
gtk_box_pack_start(GTK_BOX(editor->vbox), scwin, TRUE, TRUE, 0);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
editor->treeview
= klass->construct_treeview(G_CALLBACK(gwy_resource_editor_changed),
editor, NULL);
model = gtk_tree_view_get_model(GTK_TREE_VIEW(editor->treeview));
inventory = gwy_inventory_store_get_inventory(GWY_INVENTORY_STORE(model));
column = gtk_tree_view_get_column(GTK_TREE_VIEW(editor->treeview), 2);
rlist = gtk_tree_view_column_get_cell_renderers(column);
g_assert(rlist && !rlist->next);
g_object_set(rlist->data, "editable-set", TRUE, NULL);
gtk_tree_view_column_set_cell_data_func(column,
GTK_CELL_RENDERER(rlist->data),
gwy_resource_editor_cell_name,
inventory, NULL);
g_signal_connect_swapped(rlist->data, "edited",
G_CALLBACK(gwy_resource_editor_name_edited),
editor);
g_signal_connect_swapped(editor->treeview, "row-activated",
G_CALLBACK(gwy_resource_editor_row_activated),
editor);
g_list_free(rlist);
gtk_container_add(GTK_CONTAINER(scwin), editor->treeview);
/* Controls */
hbox = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start(GTK_BOX(editor->vbox), hbox, FALSE, FALSE, 0);
for (i = 0; i < G_N_ELEMENTS(toolbar_buttons); i++) {
image = gtk_image_new_from_stock(toolbar_buttons[i].stock_id,
GTK_ICON_SIZE_LARGE_TOOLBAR);
button = gtk_button_new();
gtk_container_add(GTK_CONTAINER(button), image);
gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
gtk_widget_set_tooltip_text(button, gettext(toolbar_buttons[i].tooltip));
gwy_sensitivity_group_add_widget(editor->sensgroup, button,
toolbar_buttons[i].sens_flags);
g_signal_connect_swapped(button, "clicked",
toolbar_buttons[i].callback, editor);
}
g_object_unref(editor->sensgroup);
gtk_window_set_default_size(GTK_WINDOW(editor), -1, 400);
gwy_app_restore_window_position(GTK_WINDOW(editor), klass->settings_prefix,
TRUE);
gtk_widget_show_all(editor->vbox);
gwy_resource_tree_view_set_active(editor->treeview, name);
}
static void
gwy_resource_editor_cell_name(G_GNUC_UNUSED GtkTreeViewColumn *column,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
GwyResource *item;
gpointer defitem;
defitem = gwy_inventory_get_default_item(GWY_INVENTORY(data));
gtk_tree_model_get(model, iter, 0, &item, -1);
g_object_set(renderer,
"editable",
gwy_resource_get_is_modifiable(item),
"weight",
(item == defitem) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL,
NULL);
}
static void
gwy_resource_editor_changed(GtkTreeSelection *selection,
GwyResourceEditor *editor)
{
GwyResource *resource;
GtkTreeModel *model;
GtkTreeIter iter;
guint state = 0;
gwy_debug("");
if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
state |= SELECTED_ANY;
gtk_tree_model_get(model, &iter, 0, &resource, -1);
if (gwy_resource_get_is_modifiable(resource))
state |= SELECTED_EDITABLE;
}
gwy_sensitivity_group_set_state(editor->sensgroup, SELECTED_MASK, state);
}
static void
gwy_resource_editor_unmap(GtkWidget *widget)
{
GwyResourceEditorClass *klass;
GwyResourceEditor *editor;
GwyContainer *settings;
GwyResource *resource;
gchar *key;
editor = GWY_RESOURCE_EDITOR(widget);
klass = GWY_RESOURCE_EDITOR_GET_CLASS(widget);
gwy_app_save_window_position(GTK_WINDOW(widget), klass->settings_prefix,
FALSE, TRUE);
if ((resource = gwy_resource_editor_get_active(editor, NULL, NULL, NULL))) {
settings = gwy_app_settings_get();
key = g_strconcat(klass->settings_prefix, "/current", NULL);
gwy_container_set_string_by_name
(settings, key, g_strdup(gwy_resource_get_name(resource)));
g_free(key);
}
GTK_WIDGET_CLASS(gwy_resource_editor_parent_class)->unmap(widget);
}
static void
gwy_resource_editor_destroy(GtkObject *object)
{
GwyResourceEditor *editor;
editor = GWY_RESOURCE_EDITOR(object);
if (editor->edit_window)
gtk_widget_destroy(editor->edit_window);
gwy_resource_editor_commit(editor);
if (editor->edited_resource) {
g_string_free(editor->edited_resource, TRUE);
editor->edited_resource = NULL;
}
GTK_OBJECT_CLASS(gwy_resource_editor_parent_class)->destroy(object);
}
static void
gwy_resource_editor_set_default(GwyResourceEditor *editor)
{
GtkTreeModel *model;
GwyResource *resource;
GwyInventory *inventory;
resource = gwy_resource_editor_get_active(editor, &model, NULL,
"Set Default");
inventory = gwy_inventory_store_get_inventory(GWY_INVENTORY_STORE(model));
gwy_inventory_set_default_item_name(inventory,
gwy_resource_get_name(resource));
}
static void
gwy_resource_editor_new(GwyResourceEditor *editor)
{
GwyResourceEditorClass *klass;
gwy_debug("");
klass = GWY_RESOURCE_EDITOR_GET_CLASS(editor);
gwy_resource_editor_copy(editor, klass->base_resource, _("Untitled"));
}
static void
gwy_resource_editor_duplicate(GwyResourceEditor *editor)
{
GwyResource *resource;
gwy_debug("");
resource = gwy_resource_editor_get_active(editor, NULL, NULL, "Copy");
gwy_resource_editor_copy(editor, gwy_resource_get_name(resource), NULL);
}
static void
gwy_resource_editor_copy(GwyResourceEditor *editor,
const gchar *name,
const gchar *newname)
{
GtkTreeModel *model;
GwyInventory *inventory;
GwyResource *resource;
gwy_debug("<%s> -> <%s>", name, newname);
model = gtk_tree_view_get_model(GTK_TREE_VIEW(editor->treeview));
inventory = gwy_inventory_store_get_inventory(GWY_INVENTORY_STORE(model));
resource = gwy_inventory_new_item(inventory, name, newname);
gwy_resource_tree_view_set_active(editor->treeview,
gwy_resource_get_name(resource));
gwy_resource_editor_save(editor, gwy_resource_get_name(resource));
}
static void
gwy_resource_editor_delete(GwyResourceEditor *editor)
{
GtkTreeModel *model;
GtkTreeIter iter;
GwyResource *resource;
const gchar *name;
gwy_resource_editor_commit(editor);
/* Get selected resource, and the inventory it belongs to: */
resource = gwy_resource_editor_get_active(editor, &model, &iter, "Delete");
name = gwy_resource_get_name(resource);
if (editor->edited_resource
&& gwy_strequal(editor->edited_resource->str, name)) {
gtk_widget_destroy(editor->edit_window);
}
gwy_resource_delete(resource);
}
static void
gwy_resource_editor_edit(GwyResourceEditor *editor)
{
GwyResource *resource;
gwy_debug("");
resource = gwy_resource_editor_get_active(editor, NULL, NULL, "Edit");
gwy_resource_editor_edit_resource(editor, gwy_resource_get_name(resource));
}
static void
gwy_resource_editor_row_activated(GwyResourceEditor *editor,
GtkTreePath *path,
G_GNUC_UNUSED GtkTreeViewColumn *column)
{
GwyResource *resource;
GtkTreeModel *model;
GtkTreeIter iter;
gwy_debug("");
model = gtk_tree_view_get_model(GTK_TREE_VIEW(editor->treeview));
gtk_tree_model_get_iter(model, &iter, path);
gtk_tree_model_get(model, &iter, 0, &resource, -1);
if (!gwy_resource_get_is_modifiable(resource))
return;
gwy_resource_editor_edit_resource(editor, gwy_resource_get_name(resource));
}
static void
gwy_resource_editor_edit_resource(GwyResourceEditor *editor,
const gchar *name)
{
GwyResourceEditorClass *klass;
klass = GWY_RESOURCE_EDITOR_GET_CLASS(editor);
gwy_resource_editor_commit(editor);
if (!editor->edit_window) {
editor->edited_resource = g_string_new(name);
editor->edit_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gwy_resource_editor_update_title(editor);
klass->construct_editor(editor);
g_signal_connect_swapped(editor->edit_window, "destroy",
G_CALLBACK(gwy_resource_editor_editor_closed),
editor);
gtk_widget_show_all(editor->edit_window);
}
else {
g_string_assign(editor->edited_resource, name);
gwy_resource_editor_update_title(editor);
klass->switch_resource(editor);
gtk_window_present(GTK_WINDOW(editor->edit_window));
}
}
static void
gwy_resource_editor_editor_closed(GwyResourceEditor *editor)
{
gwy_resource_editor_commit(editor);
g_assert(editor->edited_resource);
g_string_free(editor->edited_resource, TRUE);
editor->edited_resource = NULL;
editor->edit_window = NULL;
}
static void
gwy_resource_editor_name_edited(GwyResourceEditor *editor,
const gchar *strpath,
const gchar *text)
{
GwyResource *resource;
GwyInventory *inventory;
GtkTreeModel *model;
GtkTreePath *path;
GtkTreeIter iter;
gboolean is_being_edited;
gwy_resource_editor_commit(editor);
model = gtk_tree_view_get_model(GTK_TREE_VIEW(editor->treeview));
inventory = gwy_inventory_store_get_inventory(GWY_INVENTORY_STORE(model));
if (gwy_inventory_get_item(inventory, text))
return;
path = gtk_tree_path_new_from_string(strpath);
gtk_tree_model_get_iter(model, &iter, path);
gtk_tree_path_free(path);
gtk_tree_model_get(model, &iter, 0, &resource, -1);
is_being_edited = (editor->edited_resource
&& gwy_strequal(gwy_resource_get_name(resource),
editor->edited_resource->str));
gwy_resource_rename(resource, text);
if (is_being_edited) {
g_string_assign(editor->edited_resource, text);
gwy_resource_editor_update_title(editor);
}
gwy_resource_tree_view_set_active(editor->treeview, text);
}
/**
* gwy_resource_editor_queue_commit:
* @editor: A resource editor.
*
* Queues commit of resource changes, marking the currently edited resource
* `dirty'.
*
* Call this method in particular resource editor subclass whenever user
* changes some editor property.
*
* To flush pending commit, call gwy_resource_editor_commit(). To immediately
* commit a change, call this method and then gwy_resource_editor_commit().
**/
void
gwy_resource_editor_queue_commit(GwyResourceEditor *editor)
{
g_return_if_fail(GWY_IS_RESOURCE_EDITOR(editor));
if (editor->commit_id)
g_source_remove(editor->commit_id);
editor->commit_id
= g_timeout_add_full(G_PRIORITY_LOW, COMMIT_TIMEOUT,
(GSourceFunc)&gwy_resource_editor_commit, editor,
NULL);
}
/**
* gwy_resource_editor_commit:
* @editor: A resource editor.
*
* Commits pending resource changes, if there are any.
*
* It calls @apply_changes method first (if it exists), then saves resource to
* disk.
*
* Changes are always immediately committed (if there are any pending): before
* the editor is destroyed, when a resource stops being edited, before a
* resource is deleted, before a resource is renamed. When a resource is newly
* created, it is immediately created on disk too.
*
* Returns: Always %FALSE (to be usable as #GSourceFunc).
**/
gboolean
gwy_resource_editor_commit(GwyResourceEditor *editor)
{
GwyResourceEditorClass *klass;
g_return_val_if_fail(GWY_IS_RESOURCE_EDITOR(editor), FALSE);
gwy_debug("%u", editor->commit_id);
if (!editor->commit_id)
return FALSE;
g_return_val_if_fail(editor->edited_resource
&& *editor->edited_resource->str,
FALSE);
klass = GWY_RESOURCE_EDITOR_GET_CLASS(editor);
if (klass->apply_changes)
klass->apply_changes(editor);
gwy_resource_editor_save(editor, editor->edited_resource->str);
editor->commit_id = 0;
return FALSE;
}
static gboolean
gwy_resource_editor_save(GwyResourceEditor *editor,
const gchar *name)
{
GwyInventory *inventory;
GwyResource *resource;
GtkTreeModel *model;
GError *error = NULL;
gwy_debug("<%s>", name);
model = gtk_tree_view_get_model(GTK_TREE_VIEW(editor->treeview));
inventory = gwy_inventory_store_get_inventory(GWY_INVENTORY_STORE(model));
resource = gwy_inventory_get_item(inventory, name);
if (!resource) {
g_warning("Trying to save `%s' that isn't in inventory", name);
return FALSE;
}
if (!gwy_resource_save(resource, &error)) {
/* FIXME: GUIze this */
g_warning("Cannot save resource %s: %s", name, error->message);
g_clear_error(&error);
return FALSE;
}
return TRUE;
}
static void
gwy_resource_editor_update_title(GwyResourceEditor *editor)
{
GwyResourceEditorClass *klass;
gchar *title;
klass = GWY_RESOURCE_EDITOR_GET_CLASS(editor);
title = g_strdup_printf(klass->editor_title, editor->edited_resource->str);
gtk_window_set_title(GTK_WINDOW(editor->edit_window), title);
g_free(title);
}
static GwyResource*
gwy_resource_editor_get_active(GwyResourceEditor *editor,
GtkTreeModel **model,
GtkTreeIter *iter,
const gchar *warnwhat)
{
GtkTreeSelection *selection;
GwyResource *resource;
GtkTreeModel *treemodel;
GtkTreeIter treeiter;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(editor->treeview));
if (!gtk_tree_selection_get_selected(selection, &treemodel, &treeiter)) {
if (warnwhat)
g_critical("Something must be selected for `%s'!", warnwhat);
if (model)
*model = NULL;
return NULL;
}
gtk_tree_model_get(treemodel, &treeiter, 0, &resource, -1);
if (model)
*model = treemodel;
if (iter)
*iter = treeiter;
return resource;
}
/**
* gwy_resource_editor_class_setup:
* @klass: A resource editor class.
*
* Sets up particular resource editor class.
*
* Helper class method only intended for resource editor implementation.
* To be called in particular class initialization methods.
**/
void
gwy_resource_editor_class_setup(GwyResourceEditorClass *klass)
{
GwyResourceClass *rklass;
const gchar *name;
gwy_debug("");
g_return_if_fail(GWY_IS_RESOURCE_EDITOR_CLASS(klass));
rklass = g_type_class_ref(klass->resource_type);
name = gwy_resource_class_get_name(rklass);
g_type_class_unref(rklass);
klass->settings_prefix = g_strdup_printf("/app/%s/editor", name);
}
/**
* gwy_resource_editor_get_edited:
* @editor: A resource editor.
*
* Gets the currently edited resource.
*
* It is an error to call this method when no resource is being edited.
*
* Returns: The currently edited resource.
**/
GwyResource*
gwy_resource_editor_get_edited(GwyResourceEditor *editor)
{
GwyInventory *inventory;
GtkTreeModel *model;
g_return_val_if_fail(GWY_IS_RESOURCE_EDITOR(editor), NULL);
g_return_val_if_fail(editor->edited_resource, NULL);
model = gtk_tree_view_get_model(GTK_TREE_VIEW(editor->treeview));
inventory = gwy_inventory_store_get_inventory(GWY_INVENTORY_STORE(model));
return (GwyResource*)gwy_inventory_get_item(inventory,
editor->edited_resource->str);
}
/************************** Documentation ****************************/
/**
* SECTION:resource-editor
* @title: GwyResourceEditor
* @short_description: Base/helper class for resource editors
*
* The base class contains all the methods that handle the resource list, set
* defaults, or sync resource list with disk. Particular editors (subclasses)
* have to override (in fact, to fill, because #GwyResourceEditor does not
* define them) the methods and class data in their class init methods. Then
* they generally only need to care about widgets inside their edit windows.
**/
/**
* GwyResourceEditorClass:
* @resource_type: The #GType of edited resources. It must be a type derived
* from #GwyResource.
* @base_resource: The name of vanilla, default resource. The `New' button
* creates a new resource as a copy of this one.
* @window_title: Resource list window title. It should be already translated.
* @editor_title: Editor window title template. It must contain one
* <literal>%s</literal> that will be replaced with edited
* resource name. It should be already translated.
* @settings_prefix: Settings prefix for saved state, filled by
* gwy_resource_editor_class_setup().
* @construct_treeview: Method to create the resource list widget, it is
* of the gwy_gradient_tree_view_new() signature.
* There are currently some hardcoded assumptions about
* the tree view columns, so it cannot be completely
* arbitrary.
* @construct_editor: Method to construct editor window contents (the window
* itself is managed by #GwyResourceEditor, its contents
* by a particular editor).
* @apply_changes: Method called on commit (before resource is written to
* disk). It should obtain values from the editor widgets
* and actually update the resource.
* @switch_resource: Method to update editor window widgets to display another
* resource.
* @instance: Editor instance. Resource editors are singletons so if an
* instance exists, pointer to it is kept here.
*
* The resource editor class.
**/
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|