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
|
/*
* Copyright © 2020 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Matthias Clasen <mclasen@redhat.com>
*/
#include "config.h"
#include "gtkstringlist.h"
#include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
#include "gtkprivate.h"
/**
* GtkStringList:
*
* A list model that wraps an array of strings.
*
* The objects in the model are of type [class@Gtk.StringObject] and have
* a "string" property that can be used inside expressions.
*
* `GtkStringList` is well-suited for any place where you would
* typically use a `char*[]`, but need a list model.
*
* ## GtkStringList as GtkBuildable
*
* The `GtkStringList` implementation of the `GtkBuildable` interface
* supports adding items directly using the `<items>` element and
* specifying `<item>` elements for each item. Each `<item>` element
* supports the regular translation attributes “translatable”,
* “context” and “comments”.
*
* Here is a UI definition fragment specifying a `GtkStringList`
*
* ```xml
* <object class="GtkStringList">
* <items>
* <item translatable="yes">Factory</item>
* <item translatable="yes">Home</item>
* <item translatable="yes">Subway</item>
* </items>
* </object>
* ```
*/
/* {{{ GtkStringObject */
/**
* GtkStringObject:
*
* The type of items in a `GtkStringList`.
*
* A `GtkStringObject` is a wrapper around a `const char*`; it has
* a [property@Gtk.StringObject:string] property that can be used
* for property bindings and expressions.
*/
#define GDK_ARRAY_ELEMENT_TYPE GtkStringObject *
#define GDK_ARRAY_NAME objects
#define GDK_ARRAY_TYPE_NAME Objects
#define GDK_ARRAY_FREE_FUNC g_object_unref
#include "gdk/gdkarrayimpl.c"
struct _GtkStringObject
{
GObject parent_instance;
char *string;
};
enum {
PROP_STRING = 1,
PROP_NUM_PROPERTIES
};
G_DEFINE_TYPE (GtkStringObject, gtk_string_object, G_TYPE_OBJECT);
static void
gtk_string_object_init (GtkStringObject *object)
{
}
static void
gtk_string_object_finalize (GObject *object)
{
GtkStringObject *self = GTK_STRING_OBJECT (object);
g_free (self->string);
G_OBJECT_CLASS (gtk_string_object_parent_class)->finalize (object);
}
static void
gtk_string_object_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GtkStringObject *self = GTK_STRING_OBJECT (object);
switch (property_id)
{
case PROP_STRING:
g_value_set_string (value, self->string);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gtk_string_object_class_init (GtkStringObjectClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GParamSpec *pspec;
object_class->finalize = gtk_string_object_finalize;
object_class->get_property = gtk_string_object_get_property;
/**
* GtkStringObject:string:
*
* The string.
*/
pspec = g_param_spec_string ("string", NULL, NULL,
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_STRING, pspec);
}
static GtkStringObject *
gtk_string_object_new_take (char *string)
{
GtkStringObject *obj;
obj = g_object_new (GTK_TYPE_STRING_OBJECT, NULL);
obj->string = string;
return obj;
}
/**
* gtk_string_object_new:
* @string: (not nullable): The string to wrap
*
* Wraps a string in an object for use with `GListModel`.
*
* Returns: a new `GtkStringObject`
*/
GtkStringObject *
gtk_string_object_new (const char *string)
{
return gtk_string_object_new_take (g_strdup (string));
}
/**
* gtk_string_object_get_string:
* @self: a `GtkStringObject`
*
* Returns the string contained in a `GtkStringObject`.
*
* Returns: the string of @self
*/
const char *
gtk_string_object_get_string (GtkStringObject *self)
{
g_return_val_if_fail (GTK_IS_STRING_OBJECT (self), NULL);
return self->string;
}
/* }}} */
/* {{{ List model implementation */
struct _GtkStringList
{
GObject parent_instance;
Objects items;
};
struct _GtkStringListClass
{
GObjectClass parent_class;
};
static GType
gtk_string_list_get_item_type (GListModel *list)
{
return G_TYPE_OBJECT;
}
static guint
gtk_string_list_get_n_items (GListModel *list)
{
GtkStringList *self = GTK_STRING_LIST (list);
return objects_get_size (&self->items);
}
static gpointer
gtk_string_list_get_item (GListModel *list,
guint position)
{
GtkStringList *self = GTK_STRING_LIST (list);
if (position >= objects_get_size (&self->items))
return NULL;
return g_object_ref (objects_get (&self->items, position));
}
static void
gtk_string_list_model_init (GListModelInterface *iface)
{
iface->get_item_type = gtk_string_list_get_item_type;
iface->get_n_items = gtk_string_list_get_n_items;
iface->get_item = gtk_string_list_get_item;
}
/* }}} */
/* {{{ Buildable implementation */
typedef struct
{
GtkBuilder *builder;
GtkStringList *list;
GString *string;
const char *domain;
char *context;
guint translatable : 1;
guint is_text : 1;
} ItemParserData;
static void
item_start_element (GtkBuildableParseContext *context,
const char *element_name,
const char **names,
const char **values,
gpointer user_data,
GError **error)
{
ItemParserData *data = (ItemParserData*)user_data;
if (strcmp (element_name, "items") == 0)
{
if (!_gtk_builder_check_parent (data->builder, context, "object", error))
return;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_INVALID, NULL, NULL,
G_MARKUP_COLLECT_INVALID))
_gtk_builder_prefix_error (data->builder, context, error);
}
else if (strcmp (element_name, "item") == 0)
{
gboolean translatable = FALSE;
const char *msg_context = NULL;
if (!_gtk_builder_check_parent (data->builder, context, "items", error))
return;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "translatable", &translatable,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "comments", NULL,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "context", &msg_context,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
data->is_text = TRUE;
data->translatable = translatable;
data->context = g_strdup (msg_context);
}
else
{
_gtk_builder_error_unhandled_tag (data->builder, context,
"GtkStringList", element_name,
error);
}
}
static void
item_text (GtkBuildableParseContext *context,
const char *text,
gsize text_len,
gpointer user_data,
GError **error)
{
ItemParserData *data = (ItemParserData*)user_data;
if (data->is_text)
g_string_append_len (data->string, text, text_len);
}
static void
item_end_element (GtkBuildableParseContext *context,
const char *element_name,
gpointer user_data,
GError **error)
{
ItemParserData *data = (ItemParserData*)user_data;
/* Append the translated strings */
if (data->string->len)
{
if (data->translatable)
{
const char *translated;
translated = _gtk_builder_parser_translate (data->domain,
data->context,
data->string->str);
g_string_assign (data->string, translated);
}
gtk_string_list_append (data->list, data->string->str);
}
data->translatable = FALSE;
g_string_set_size (data->string, 0);
g_clear_pointer (&data->context, g_free);
data->is_text = FALSE;
}
static const GtkBuildableParser item_parser =
{
item_start_element,
item_end_element,
item_text
};
static gboolean
gtk_string_list_buildable_custom_tag_start (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const char *tagname,
GtkBuildableParser *parser,
gpointer *parser_data)
{
if (strcmp (tagname, "items") == 0)
{
ItemParserData *data;
gtk_buildable_tag_deprecation_warning (buildable, builder, "items", "strings");
data = g_new0 (ItemParserData, 1);
data->builder = g_object_ref (builder);
data->list = g_object_ref (GTK_STRING_LIST (buildable));
data->domain = gtk_builder_get_translation_domain (builder);
data->string = g_string_new ("");
*parser = item_parser;
*parser_data = data;
return TRUE;
}
return FALSE;
}
static void
gtk_string_list_buildable_custom_finished (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const char *tagname,
gpointer user_data)
{
if (strcmp (tagname, "items") == 0)
{
ItemParserData *data;
data = (ItemParserData*)user_data;
g_object_unref (data->list);
g_object_unref (data->builder);
g_string_free (data->string, TRUE);
g_free (data);
}
}
static void
gtk_string_list_buildable_init (GtkBuildableIface *iface)
{
iface->custom_tag_start = gtk_string_list_buildable_custom_tag_start;
iface->custom_finished = gtk_string_list_buildable_custom_finished;
}
/* }}} */
/* {{{ GObject implementation */
enum {
PROP_0,
PROP_ITEM_TYPE,
PROP_N_ITEMS,
PROP_STRINGS,
N_PROPS
};
static GParamSpec *properties[N_PROPS] = { NULL, };
G_DEFINE_TYPE_WITH_CODE (GtkStringList, gtk_string_list, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_string_list_buildable_init)
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,
gtk_string_list_model_init))
static void
gtk_string_list_dispose (GObject *object)
{
GtkStringList *self = GTK_STRING_LIST (object);
objects_clear (&self->items);
G_OBJECT_CLASS (gtk_string_list_parent_class)->dispose (object);
}
static void
gtk_string_list_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkStringList *self = GTK_STRING_LIST (object);
switch (prop_id)
{
case PROP_ITEM_TYPE:
g_value_set_gtype (value, gtk_string_list_get_item_type (G_LIST_MODEL (self)));
break;
case PROP_N_ITEMS:
g_value_set_uint (value, gtk_string_list_get_n_items (G_LIST_MODEL (self)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_string_list_set_property (GObject *object,
unsigned int prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkStringList *self = GTK_STRING_LIST (object);
switch (prop_id)
{
case PROP_STRINGS:
gtk_string_list_splice (self, 0, 0,
(const char * const *) g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_string_list_class_init (GtkStringListClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
gobject_class->dispose = gtk_string_list_dispose;
gobject_class->get_property = gtk_string_list_get_property;
gobject_class->set_property = gtk_string_list_set_property;
/**
* GtkStringList:item-type:
*
* The type of items. See [method@Gio.ListModel.get_item_type].
*
* Since: 4.14
**/
properties[PROP_ITEM_TYPE] =
g_param_spec_gtype ("item-type", NULL, NULL,
G_TYPE_OBJECT,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GtkStringList:n-items:
*
* The number of items. See [method@Gio.ListModel.get_n_items].
*
* Since: 4.14
**/
properties[PROP_N_ITEMS] =
g_param_spec_uint ("n-items", NULL, NULL,
0, G_MAXUINT, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GtkStringList:strings:
*
* The strings in the model.
*
* Since: 4.10
*/
properties[PROP_STRINGS] =
g_param_spec_boxed ("strings", NULL, NULL,
G_TYPE_STRV,
G_PARAM_WRITABLE|G_PARAM_STATIC_STRINGS|G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_properties (gobject_class, N_PROPS, properties);
}
static void
gtk_string_list_init (GtkStringList *self)
{
objects_init (&self->items);
}
/* }}} */
/* {{{ Public API */
/**
* gtk_string_list_new:
* @strings: (array zero-terminated=1) (nullable): The strings to put in the model
*
* Creates a new `GtkStringList` with the given @strings.
*
* Returns: a new `GtkStringList`
*/
GtkStringList *
gtk_string_list_new (const char * const *strings)
{
return g_object_new (GTK_TYPE_STRING_LIST,
"strings", strings,
NULL);
}
/**
* gtk_string_list_splice:
* @self: a `GtkStringList`
* @position: the position at which to make the change
* @n_removals: the number of strings to remove
* @additions: (array zero-terminated=1) (nullable): The strings to add
*
* Changes @self by removing @n_removals strings and adding @additions
* to it.
*
* This function is more efficient than [method@Gtk.StringList.append]
* and [method@Gtk.StringList.remove], because it only emits the
* ::items-changed signal once for the change.
*
* This function copies the strings in @additions.
*
* The parameters @position and @n_removals must be correct (ie:
* @position + @n_removals must be less than or equal to the length
* of the list at the time this function is called).
*/
void
gtk_string_list_splice (GtkStringList *self,
guint position,
guint n_removals,
const char * const *additions)
{
guint i, n_additions;
g_return_if_fail (GTK_IS_STRING_LIST (self));
g_return_if_fail (position + n_removals >= position); /* overflow */
g_return_if_fail (position + n_removals <= objects_get_size (&self->items));
if (additions)
n_additions = g_strv_length ((char **) additions);
else
n_additions = 0;
objects_splice (&self->items, position, n_removals, FALSE, NULL, n_additions);
for (i = 0; i < n_additions; i++)
{
*objects_index (&self->items, position + i) = gtk_string_object_new (additions[i]);
}
if (n_removals || n_additions)
g_list_model_items_changed (G_LIST_MODEL (self), position, n_removals, n_additions);
if (n_removals != n_additions)
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
}
/**
* gtk_string_list_append:
* @self: a `GtkStringList`
* @string: the string to insert
*
* Appends @string to @self.
*
* The @string will be copied. See
* [method@Gtk.StringList.take] for a way to avoid that.
*/
void
gtk_string_list_append (GtkStringList *self,
const char *string)
{
g_return_if_fail (GTK_IS_STRING_LIST (self));
objects_append (&self->items, gtk_string_object_new (string));
g_list_model_items_changed (G_LIST_MODEL (self), objects_get_size (&self->items) - 1, 0, 1);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
}
/**
* gtk_string_list_take:
* @self: a `GtkStringList`
* @string: (transfer full): the string to insert
*
* Adds @string to self at the end, and takes
* ownership of it.
*
* This variant of [method@Gtk.StringList.append]
* is convenient for formatting strings:
*
* ```c
* gtk_string_list_take (self, g_strdup_print ("%d dollars", lots));
* ```
*/
void
gtk_string_list_take (GtkStringList *self,
char *string)
{
g_return_if_fail (GTK_IS_STRING_LIST (self));
objects_append (&self->items, gtk_string_object_new_take (string));
g_list_model_items_changed (G_LIST_MODEL (self), objects_get_size (&self->items) - 1, 0, 1);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_N_ITEMS]);
}
/**
* gtk_string_list_remove:
* @self: a `GtkStringList`
* @position: the position of the string that is to be removed
*
* Removes the string at @position from @self.
*
* @position must be smaller than the current
* length of the list.
*/
void
gtk_string_list_remove (GtkStringList *self,
guint position)
{
g_return_if_fail (GTK_IS_STRING_LIST (self));
gtk_string_list_splice (self, position, 1, NULL);
}
/**
* gtk_string_list_get_string:
* @self: a `GtkStringList`
* @position: the position to get the string for
*
* Gets the string that is at @position in @self.
*
* If @self does not contain @position items, %NULL is returned.
*
* This function returns the const char *. To get the
* object wrapping it, use g_list_model_get_item().
*
* Returns: (nullable): the string at the given position
*/
const char *
gtk_string_list_get_string (GtkStringList *self,
guint position)
{
g_return_val_if_fail (GTK_IS_STRING_LIST (self), NULL);
if (position >= objects_get_size (&self->items))
return NULL;
return objects_get (&self->items, position)->string;
}
/**
* gtk_string_list_find:
* @self: a `GtkStringList`
* @string: the string to find
*
* Gets the position of the @string in @self.
*
* If @self does not contain @string item, `G_MAXUINT` is returned.
*
* Returns: the position of the string
*
* Since: 4.18
*/
guint
gtk_string_list_find (GtkStringList *self,
const char *string)
{
guint position;
guint items_size;
g_return_val_if_fail (GTK_IS_STRING_LIST (self), G_MAXUINT);
position = G_MAXUINT;
items_size = objects_get_size (&self->items);
for (guint i = 0; i < items_size; i++)
{
if (strcmp (string, objects_get (&self->items, i)->string) == 0)
{
position = i;
break;
}
}
return position;
}
/* }}} */
/* vim:set foldmethod=marker: */
|