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 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
|
/* mg-form.c
*
* Copyright (C) 2002 - 2004 Vivien Malerba
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <string.h>
#ifdef has_libgnomedb
#include <libgnomedb/libgnomedb.h>
#endif
#include "mg-form.h"
#include "mg-server.h"
#include "marshal.h"
#include "mg-parameter.h"
#include "mg-data-handler.h"
#include "mg-data-entry.h"
#include "mg-server-data-type.h"
#include "mg-query.h"
#include "mg-field.h"
#include "mg-context.h"
#include <libmergeant/handlers/mg-entry-combo.h>
static void mg_form_class_init (MgFormClass * class);
static void mg_form_init (MgForm * wid);
static void mg_form_dispose (GObject * object);
static void entry_contents_modified (MgDataEntry *entry, MgForm *form);
static void parameter_changed_cb (MgParameter *param, MgDataEntry *entry);
static void mark_not_null_entry_labels (MgForm *form, gboolean show_mark);
enum
{
PARAM_CHANGED,
LAST_SIGNAL
};
struct _MgFormPriv
{
MgConf *conf;
MgContext *context;/* parameters, etc */
GSList *entries;/* list of MgDataEntry widgets */
GSList *not_null_labels;/* list of GtkLabel widgets corresponding to NOT NULL entries */
gulong *signal_ids; /* array of signal ids */
GtkWidget *entries_table;
GSList *hidden_entries;
gboolean forward_param_updates; /* forward them to the MgDataEntry widgets ? */
GtkTooltips *tooltips;
};
static gint mg_form_signals[LAST_SIGNAL] = { 0 };
/* get a pointer to the parents to be able to call their destructor */
static GObjectClass *parent_class = NULL;
guint
mg_form_get_type (void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof (MgFormClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) mg_form_class_init,
NULL,
NULL,
sizeof (MgForm),
0,
(GInstanceInitFunc) mg_form_init
};
type = g_type_register_static (GTK_TYPE_VBOX, "MgForm", &info, 0);
}
return type;
}
static void
mg_form_class_init (MgFormClass * class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
parent_class = g_type_class_peek_parent (class);
mg_form_signals[PARAM_CHANGED] =
g_signal_new ("param_changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (MgFormClass, param_changed),
NULL, NULL,
marshal_VOID__OBJECT_BOOLEAN, G_TYPE_NONE, 2,
G_TYPE_OBJECT, G_TYPE_BOOLEAN);
class->param_changed = NULL;
object_class->dispose = mg_form_dispose;
}
static void
mg_form_init (MgForm * wid)
{
wid->priv = g_new0 (MgFormPriv, 1);
wid->priv->conf = NULL;
wid->priv->context = NULL;
wid->priv->entries = NULL;
wid->priv->not_null_labels = NULL;
wid->priv->entries_table = NULL;
wid->priv->hidden_entries = NULL;
wid->priv->signal_ids = NULL;
wid->priv->forward_param_updates = TRUE;
wid->priv->tooltips = NULL;
}
static void mg_form_initialize (MgForm *form, MgConf *conf, GtkWidget *layout, GHashTable *box_widgets);
static void widget_shown_cb (GtkWidget *wid, MgForm *form);
static void mg_conf_weak_notify (MgForm *form, MgConf *conf);
/**
* mg_form_new
* @conf: a #MgConf object
* @context: a #MgContext structure
*
* Creates a new #MgForm widget using all the parameters provided in @context.
* @context is copied in the process.
*
* The global layout is rendered using a table (a #GtkTable), and an entry is created for each
* node of @context.
*
* Returns: the new widget
*/
GtkWidget *
mg_form_new (MgConf *conf, MgContext *context)
{
return mg_form_new_in_layout (conf, context, NULL, NULL);
}
static void context_translate_boxes (gpointer key, gpointer value, GHashTable *data_hash);
/**
* mg_form_new_in_layout
* @conf: a #MgConf object
* @context: a #MgContext structure
* @layout: a #GtkWidget container of all the widgets in @box_widgets
* @box_widgets: a #GHashTable of #GtkBox widgets
*
* Creates a new #MgForm widget using all the parameters provided in @context.
* @context is copied in the process.
*
* This function is identical to mg_form_new() except that the layout is not done using a table, but
* using the @layout widget; and each entry is packed into one of the #GtkBox widgets of @box_widgets
* (specifically each entry corresponds to a #MgContextNode of @context, and that context node
* is used a the key to @box_widgets to find the box to pack the entry in).
*
* If any of @layout or @box_widgets is %NULL, then this function is equivalent to mg_form_new().
*
* Returns: the new widget
*/
GtkWidget *
mg_form_new_in_layout (MgConf *conf, MgContext *context, GtkWidget *layout, GHashTable *box_widgets)
{
GObject *obj;
MgForm *form;
GHashTable *repl = NULL;
g_return_val_if_fail (conf && IS_MG_CONF (conf), NULL);
if (context)
g_return_val_if_fail (mg_context_is_coherent (context, NULL), NULL);
obj = g_object_new (MG_FORM_TYPE, NULL);
form = MG_FORM (obj);
form->priv->conf = conf;
/* context copy, parameters are not copied */
if (context) {
if (box_widgets) {
repl = g_hash_table_new (NULL, NULL);
form->priv->context = MG_CONTEXT (mg_context_new_copy (context, FALSE, repl));
g_hash_table_foreach (box_widgets, (GHFunc) context_translate_boxes, repl);
box_widgets = repl;
}
else
form->priv->context = MG_CONTEXT (mg_context_new_copy (context, FALSE, repl));
}
else
form->priv->context = MG_CONTEXT (mg_context_new (conf, NULL));
g_object_weak_ref (G_OBJECT (form->priv->conf),
(GWeakNotify) mg_conf_weak_notify, form);
mg_form_initialize (form, conf, layout, box_widgets);
if (repl)
g_hash_table_destroy (repl);
return GTK_WIDGET (obj);
}
static void
context_translate_boxes (gpointer key, gpointer value, GHashTable *data_hash)
{
gpointer newkey = g_hash_table_lookup (data_hash, key);
g_hash_table_insert (data_hash, newkey, value);
}
static void
widget_shown_cb (GtkWidget *wid, MgForm *form)
{
if (g_slist_find (form->priv->hidden_entries, wid)) {
if (form->priv->entries_table && g_slist_find (form->priv->entries, wid)) {
gint row = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (wid), "row_no"));
gtk_table_set_row_spacing (GTK_TABLE (form->priv->entries_table), row, 0);
}
gtk_widget_hide (wid);
}
}
static void
mg_conf_weak_notify (MgForm *form, MgConf *conf)
{
GSList *list;
/* render all the entries non sensitive */
list = form->priv->entries;
while (list) {
gtk_widget_set_sensitive (GTK_WIDGET (list->data), FALSE);
list = g_slist_next (list);
}
/* Tell that we don't need to weak unref the MgConf */
form->priv->conf = NULL;
}
static void
mg_form_dispose (GObject *object)
{
MgForm *form;
g_return_if_fail (object != NULL);
g_return_if_fail (IS_MG_FORM (object));
form = MG_FORM (object);
if (form->priv) {
GSList *list;
/* context */
if (form->priv->context) {
gint i = 0;
list = form->priv->context->parameters;;
while (list) {
g_signal_handler_disconnect (G_OBJECT (list->data), form->priv->signal_ids[i]);
list = g_slist_next (list);
i++;
}
g_object_unref (G_OBJECT (form->priv->context));
form->priv->context = NULL;
}
/* list of entries */
list = form->priv->hidden_entries;
if (list) {
while (list) {
g_signal_handlers_disconnect_by_func (G_OBJECT (list->data),
G_CALLBACK (widget_shown_cb), form);
list = g_slist_next (list);
}
g_slist_free (form->priv->hidden_entries);
form->priv->hidden_entries = NULL;
}
list = form->priv->entries;
if (list) {
while (list) {
g_object_set_data (G_OBJECT (list->data), "param", NULL);
list = g_slist_next (list);
}
g_slist_free (form->priv->entries);
form->priv->entries = NULL;
}
if (form->priv->not_null_labels)
g_slist_free (form->priv->not_null_labels);
/* Weak unref the MgConf is necessary */
if (form->priv->conf)
g_object_weak_unref (G_OBJECT (form->priv->conf),
(GWeakNotify) mg_conf_weak_notify, form);
/* tooltips */
if (form->priv->tooltips) {
gtk_object_destroy (GTK_OBJECT (form->priv->tooltips));
form->priv->tooltips = NULL;
}
/* the private area itself */
g_free (form->priv);
form->priv = NULL;
}
/* for the parent class */
parent_class->dispose (object);
}
/*
* create the entries in the widget
*/
static void
mg_form_initialize (MgForm *form, MgConf *conf, GtkWidget *layout, GHashTable *box_widgets)
{
GSList *list;
MgParameter *param;
MgContextNode *node;
gint i;
/* tooltips */
form->priv->tooltips = gtk_tooltips_new ();
/* context management */
if (form->priv->context && !form->priv->context->nodes) {
MgContext *ncontext = MG_CONTEXT (mg_context_new (conf, form->priv->context->parameters));
g_object_unref (G_OBJECT (form->priv->context));
form->priv->context = ncontext;
}
/* allocating space for the signal ids */
form->priv->signal_ids = g_new0 (gulong, g_slist_length (form->priv->context->parameters));
i = 0;
/* creating all the entries */
list = form->priv->context->nodes;
while (list) {
GtkWidget *entry = NULL;
node = MG_CONTEXT_NODE (list->data);
if ((param = node->param)) { /* single direct parameter */
MgServerDataType *type;
const GdaValue *val, *default_val, *value;
gboolean nnul;
MgDataHandler *dh = NULL;
gchar *plugin = NULL;
type = mg_parameter_get_data_type (param);
g_assert (type);
g_object_get (G_OBJECT (param), "handler_plugin", &plugin, NULL);
if (plugin) {
dh = mg_server_get_handler_by_name (mg_conf_get_server (conf), plugin);
/* test if plugin can handle the parameter's data type */
if (!mg_data_handler_accepts_gda_type (dh, mg_server_data_type_get_gda_type (type)))
dh = NULL;
}
else
dh = mg_server_data_type_get_handler (type);
val = mg_parameter_get_value (param);
default_val = mg_parameter_get_default_value (param);
nnul = mg_parameter_get_not_null (param);
/* determine initial value */
value = val;
if (!value && default_val &&
(gda_value_get_type (default_val) == mg_server_data_type_get_gda_type (type)))
value = default_val;
/* create entry */
entry = GTK_WIDGET (mg_data_handler_get_entry_from_value (dh, value,
mg_server_data_type_get_gda_type (type)));
/* set current value */
mg_data_entry_set_value (MG_DATA_ENTRY (entry), val);
if (!nnul ||
(nnul && value && (gda_value_get_type (value) != GDA_VALUE_TYPE_NULL)))
mg_data_entry_set_value_orig (MG_DATA_ENTRY (entry), value);
if (default_val) {
mg_data_entry_set_value_default (MG_DATA_ENTRY (entry), default_val);
mg_data_entry_set_attributes (MG_DATA_ENTRY (entry),
MG_DATA_ENTRY_CAN_BE_DEFAULT,
MG_DATA_ENTRY_CAN_BE_DEFAULT);
}
mg_data_entry_set_attributes (MG_DATA_ENTRY (entry),
nnul ? 0 : MG_DATA_ENTRY_CAN_BE_NULL,
MG_DATA_ENTRY_CAN_BE_NULL);
g_object_set_data (G_OBJECT (entry), "param", param);
g_object_set_data (G_OBJECT (entry), "form", form);
form->priv->entries = g_slist_append (form->priv->entries, entry);
/* connect to the parameter's changes */
form->priv->signal_ids[i] = g_signal_connect (G_OBJECT (param), "changed",
G_CALLBACK (parameter_changed_cb), entry);
i++;
}
else { /* parameter depending on a sub query */
GSList *plist;
gboolean nnul = TRUE;
entry = mg_entry_combo_new (conf, form->priv->context, node);
g_object_set_data (G_OBJECT (entry), "node", node);
g_object_set_data (G_OBJECT (entry), "form", form);
form->priv->entries = g_slist_append (form->priv->entries, entry);
/* connect to the parameter's changes */
plist = node->params;
while (plist) {
if (!mg_parameter_get_not_null (plist->data))
nnul = FALSE;
form->priv->signal_ids[i] = g_signal_connect (G_OBJECT (plist->data), "changed",
G_CALLBACK (parameter_changed_cb), entry);
i++;
plist = g_slist_next (plist);
}
mg_data_entry_set_attributes (MG_DATA_ENTRY (entry),
nnul ? 0 : MG_DATA_ENTRY_CAN_BE_NULL,
MG_DATA_ENTRY_CAN_BE_NULL);
}
/* connect the entry's changes */
g_signal_connect (G_OBJECT (entry), "contents_modified",
G_CALLBACK (entry_contents_modified), form);
list = g_slist_next (list);
}
if (layout) {
/* there is actually a layout, test it */
/* tests: GtkWindow? already parented widget? */
/* set layout to NULL on error */
if (!box_widgets)
layout = NULL;
}
if (!layout) {
GtkWidget *table, *label;
/* creating a table for all the entries */
table = gtk_table_new (g_slist_length (form->priv->entries), 2, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 5);
form->priv->entries_table = table;
gtk_box_pack_start (GTK_BOX (form), table, FALSE, TRUE, 0);
list = form->priv->entries;
i = 0;
while (list) {
gboolean expand;
GtkWidget *entry_label;
/* label for the entry */
param = g_object_get_data (G_OBJECT (list->data), "param");
if (param) {
const gchar *str;
gchar *str2;
GtkWidget *evbox;
str = mg_base_get_name (MG_BASE (param));
if (!str)
str = _("Value");
str2 = g_strdup_printf ("%s:", str);
evbox = gtk_event_box_new ();
label = gtk_label_new (str2);
if (mg_parameter_get_not_null (param))
form->priv->not_null_labels = g_slist_append (form->priv->not_null_labels, label);
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
g_free (str2);
gtk_container_add (GTK_CONTAINER (evbox), label);
gtk_table_attach (GTK_TABLE (table), evbox, 0, 1, i, i+1,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 0);
gtk_widget_show (evbox);
gtk_widget_show (label);
entry_label = evbox;
str = mg_base_get_description (MG_BASE (param));
if (str && *str)
gtk_tooltips_set_tip (form->priv->tooltips, evbox, str, NULL);
}
else {
/* FIXME: find a better label and tooltip and improve data entry attributes */
const gchar *str = NULL;
gchar *str2;
GtkWidget *evbox;
gboolean nullok = TRUE;
GSList *params;
node = g_object_get_data (G_OBJECT (list->data), "node");
params = node->params;
while (params && nullok) {
if (mg_parameter_get_not_null (MG_PARAMETER (params->data)))
nullok = FALSE;
params = g_slist_next (params);
}
str = mg_base_get_name (MG_BASE (node->query));
if (!str)
str = _("Value");
str2 = g_strdup_printf ("%s:", str);
evbox = gtk_event_box_new ();
label = gtk_label_new (str2);
if (!nullok)
form->priv->not_null_labels = g_slist_append (form->priv->not_null_labels, label);
gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
g_free (str2);
gtk_container_add (GTK_CONTAINER (evbox), label);
gtk_table_attach (GTK_TABLE (table), evbox, 0, 1, i, i+1,
GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 0, 0);
gtk_widget_show (evbox);
gtk_widget_show (label);
entry_label = evbox;
str = mg_base_get_description (MG_BASE (node->query));
if (str && *str)
gtk_tooltips_set_tip (form->priv->tooltips, evbox, str, NULL);
}
/* add the entry itself to the table */
expand = mg_data_entry_expand_in_layout (MG_DATA_ENTRY (list->data));
gtk_table_attach (GTK_TABLE (table), GTK_WIDGET (list->data), 1, 2, i, i+1,
GTK_FILL | GTK_SHRINK | GTK_EXPAND,
GTK_FILL | GTK_SHRINK | expand? GTK_EXPAND : 0, 0, 0);
gtk_widget_show (GTK_WIDGET (list->data));
g_object_set_data (G_OBJECT (list->data), "entry_label", entry_label);
g_object_set_data (G_OBJECT (list->data), "row_no", GINT_TO_POINTER (i));
list = g_slist_next (list);
i++;
}
mark_not_null_entry_labels (form, TRUE);
gtk_widget_show (table);
}
else {
/* really use the provided layout */
GtkWidget *box;
GSList *nodes;
gtk_box_pack_start (GTK_BOX (form), layout, FALSE, TRUE, 0);
list = form->priv->entries;
nodes = form->priv->context->nodes;
while (nodes && list) {
box = g_hash_table_lookup (box_widgets, nodes->data);
if (box) {
gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (list->data),
mg_data_entry_expand_in_layout (MG_DATA_ENTRY (list->data)),
TRUE, 0);
gtk_widget_show (GTK_WIDGET (list->data));
if (! g_object_get_data (G_OBJECT (box), "show_actions"))
mg_data_entry_set_attributes (MG_DATA_ENTRY (list->data),
0, MG_DATA_ENTRY_ACTIONS_SHOWN);
}
list = g_slist_next (list);
nodes = g_slist_next (nodes);
}
g_assert (!nodes && !list);
gtk_widget_show (layout);
}
}
/*
* if @show_mark is TRUE, display the label as bold */
static void
mark_not_null_entry_labels (MgForm *form, gboolean show_mark)
{
PangoAttrList *attrs = NULL;
PangoAttribute *att;
if (show_mark) {
attrs = pango_attr_list_new ();
att = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
att->start_index = 0;
att->end_index = G_MAXUINT;
pango_attr_list_insert (attrs, att);
}
GSList *list = form->priv->not_null_labels;
while (list) {
gtk_label_set_attributes (GTK_LABEL (list->data), attrs);
list = g_slist_next (list);
}
if (show_mark)
pango_attr_list_unref (attrs);
}
static void
entry_contents_modified (MgDataEntry *entry, MgForm *form)
{
MgParameter *param;
MgContextNode *node;
guint attr;
attr = mg_data_entry_get_attributes (entry);
param = g_object_get_data (G_OBJECT (entry), "param");
if (param) { /* single parameter */
GdaValue *value;
form->priv->forward_param_updates = FALSE;
/* parameter's value */
value = mg_data_entry_get_value (entry);
if ((!value || gda_value_is_null (value)) &&
(attr & MG_DATA_ENTRY_IS_DEFAULT))
g_object_set (G_OBJECT (param), "use_default_value", TRUE, NULL);
else
g_object_set (G_OBJECT (param), "use_default_value", FALSE, NULL);
mg_parameter_set_value (param, value);
#ifdef debug_signal
g_print (">> 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
g_signal_emit (G_OBJECT (form), mg_form_signals[PARAM_CHANGED], 0, param, TRUE);
#ifdef debug_signal
g_print ("<< 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
form->priv->forward_param_updates = TRUE;
gda_value_free (value);
}
else { /* multiple parameters */
GSList *params;
GList *values, *list;
node = g_object_get_data (G_OBJECT (entry), "node");
params = node->params;
values = mg_entry_combo_get_values (MG_ENTRY_COMBO (entry));
g_assert (g_slist_length (params) == g_list_length (values));
list = values;
while (list) {
/* REM: if there is more than one value in 'params', then a signal is emitted for each
param that is changed, and there is no way for the listener of that signal to know if it
the end of the "param_changed" sequence. What could be done is:
- adding another boolean to tell if that signal is the last one in the "param_changed" sequence, or
- modify the signal to add a list of parameters which are changed and emit only one signal.
*/
form->priv->forward_param_updates = FALSE;
/* parameter's value */
mg_parameter_set_value (MG_PARAMETER (params->data), (GdaValue *)(list->data));
#ifdef debug_signal
g_print (">> 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
g_signal_emit (G_OBJECT (form), mg_form_signals[PARAM_CHANGED], 0, param, TRUE);
#ifdef debug_signal
g_print ("<< 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
form->priv->forward_param_updates = TRUE;;
gda_value_free (list->data);
list = g_list_next (list);
params = g_slist_next (params);
}
g_list_free (values);
}
}
/*
* Called when a parameter changes
* We emit a "param_changed" signal only if the 'form->priv->forward_param_updates' is TRUE, which means
* the param change does not come from a MgDataEntry change.
*/
static void
parameter_changed_cb (MgParameter *param, MgDataEntry *entry)
{
MgForm *form = g_object_get_data (G_OBJECT (entry), "form");
MgContextNode *node = g_object_get_data (G_OBJECT (entry), "node");
const GdaValue *value = mg_parameter_get_value (param);
if (form->priv->forward_param_updates) {
gboolean param_valid;
gboolean default_if_invalid = FALSE;
/* There can be a feedback from the entry if the param is invalid and "set_default_if_invalid"
exists and is TRUE */
param_valid = mg_parameter_is_valid (param);
if (!param_valid)
if (g_object_class_find_property (G_OBJECT_GET_CLASS (entry), "set_default_if_invalid"))
g_object_get (G_OBJECT (entry), "set_default_if_invalid", &default_if_invalid, NULL);
/* updating the corresponding entry */
if (! default_if_invalid)
g_signal_handlers_block_by_func (G_OBJECT (entry),
G_CALLBACK (entry_contents_modified), form);
if (node) {
GList *values = NULL;
GSList *list = node->params;
gboolean allnull = TRUE;
while (list) {
const GdaValue *pvalue = mg_parameter_get_value (MG_PARAMETER (list->data));
values = g_list_append (values, pvalue);
if (allnull && pvalue && (gda_value_get_type (pvalue) != GDA_VALUE_TYPE_NULL))
allnull = FALSE;
list = g_slist_next (list);
}
if (!allnull)
mg_entry_combo_set_values_orig (MG_ENTRY_COMBO (entry), values);
else
mg_entry_combo_set_values_orig (MG_ENTRY_COMBO (entry), NULL);
g_list_free (values);
}
else
mg_data_entry_set_value_orig (entry, value);
if (! default_if_invalid)
g_signal_handlers_unblock_by_func (G_OBJECT (entry),
G_CALLBACK (entry_contents_modified), form);
#ifdef debug_signal
g_print (">> 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
g_signal_emit (G_OBJECT (form), mg_form_signals[PARAM_CHANGED], 0, param, FALSE);
#ifdef debug_signal
g_print ("<< 'PARAM_CHANGED' from %s\n", __FUNCTION__);
#endif
}
}
/**
* mg_form_set_current_as_orig
* @form: a #MgForm widget
*
* Tells @form that the current values in the different entries are
* to be considered as the original values for all the entries; the immediate
* consequence is that any sub-sequent call to mg_form_has_been_changed()
* will return FALSE (of course until any entry is changed).
*/
void
mg_form_set_current_as_orig (MgForm *form)
{
GSList *list;
MgParameter *param;
g_return_if_fail (form && IS_MG_FORM (form));
g_return_if_fail (form->priv);
list = form->priv->entries;
while (list) {
MgContextNode *node = g_object_get_data (G_OBJECT (list->data), "node");
if (node) {
/* Combo entry */
GList *values = NULL;
GSList *params = node->params;
gboolean allnull = TRUE;
while (params) {
const GdaValue *pvalue = mg_parameter_get_value (MG_PARAMETER (params->data));
values = g_list_append (values, pvalue);
if (allnull && pvalue && (gda_value_get_type (pvalue) != GDA_VALUE_TYPE_NULL))
allnull = FALSE;
params = g_slist_next (params);
}
if (!allnull)
mg_entry_combo_set_values_orig (MG_ENTRY_COMBO (list->data), values);
else
mg_entry_combo_set_values_orig (MG_ENTRY_COMBO (list->data), NULL);
g_list_free (values);
}
else {
/* non combo entry */
param = g_object_get_data (G_OBJECT (list->data), "param");
mg_data_entry_set_value_orig (MG_DATA_ENTRY (list->data), mg_parameter_get_value (param));
}
list = g_slist_next (list);
}
}
/**
* mg_form_is_valid
* @form: a #MgForm widget
*
* Tells if the form can be used as-is (if all the parameters do have some valid values)
*
* Returns: TRUE if the form is valid
*/
gboolean
mg_form_is_valid (MgForm *form)
{
g_return_val_if_fail (form && IS_MG_FORM (form), FALSE);
g_return_val_if_fail (form->priv, FALSE);
return mg_context_is_valid (form->priv->context);
}
/**
* mg_form_has_been_changed
* @form: a #MgForm widget
*
* Tells if the form has had at least on entry changed, or not
*
* Returns:
*/
gboolean
mg_form_has_been_changed (MgForm *form)
{
gboolean changed = FALSE;
GSList *list;
g_return_val_if_fail (form && IS_MG_FORM (form), FALSE);
g_return_val_if_fail (form->priv, FALSE);
list = form->priv->entries;
while (list && !changed) {
if (! (mg_data_entry_get_attributes (MG_DATA_ENTRY (list->data)) & MG_DATA_ENTRY_IS_UNCHANGED))
changed = TRUE;
list = g_slist_next (list);
}
return changed;
}
/**
* mg_form_show_entries_actions
* @form: a #MgForm widget
* @show_actions: a boolean
*
* Show or hide the actions button available at the end of each data entry
* in the form
*/
void
mg_form_show_entries_actions (MgForm *form, gboolean show_actions)
{
GSList *entries;
guint show;
g_return_if_fail (form && IS_MG_FORM (form));
g_return_if_fail (form->priv);
show = show_actions ? MG_DATA_ENTRY_ACTIONS_SHOWN : 0;
entries = form->priv->entries;
while (entries) {
mg_data_entry_set_attributes (MG_DATA_ENTRY (entries->data), show, MG_DATA_ENTRY_ACTIONS_SHOWN);
entries = g_slist_next (entries);
}
mark_not_null_entry_labels (form, show_actions);
}
/**
* mg_form_reset
* @form: a #MgForm widget
*
* Resets all the entries in the form to their
* original values
*/
void
mg_form_reset (MgForm *form)
{
GSList *list;
g_return_if_fail (form && IS_MG_FORM (form));
g_return_if_fail (form->priv);
list = form->priv->entries;
while (list) {
MgContextNode *node = g_object_get_data (G_OBJECT (list->data), "node");
if (node) {
/* Combo entry */
GList *values = NULL;
values = mg_entry_combo_get_values_orig (MG_ENTRY_COMBO (list->data));
mg_entry_combo_set_values (MG_ENTRY_COMBO (list->data), values);
g_list_free (values);
}
else {
/* non combo entry */
const GdaValue *value;
value = mg_data_entry_get_value_orig (MG_DATA_ENTRY (list->data));
mg_data_entry_set_value (MG_DATA_ENTRY (list->data), value);
}
list = g_slist_next (list);
}
}
/**
* mg_form_entry_show
* @form: a #MgForm widget
* @param: a #MgParameter object
* @show:
*
* Shows or hides the #MgDataEntry in @form which corresponds to the
* @param parameter
*/
void
mg_form_entry_show (MgForm *form, MgParameter *param, gboolean show)
{
GSList *entries;
g_return_if_fail (form && IS_MG_FORM (form));
g_return_if_fail (form->priv);
entries = form->priv->entries;
while (entries) {
GtkWidget *entry = NULL;
MgParameter *thisparam = g_object_get_data (G_OBJECT (entries->data), "param");
if (thisparam) {
if (thisparam == param)
entry = GTK_WIDGET (entries->data);
}
else {
/* multiple parameters */
GSList *params;
MgContextNode *node;
node = g_object_get_data (G_OBJECT (entries->data), "node");
params = node->params;
while (params && !entry) {
if (params->data == (gpointer) param)
entry = GTK_WIDGET (entries->data);
params = g_slist_next (params);
}
}
if (entry) {
gint row = -1;
GtkWidget *entry_label = g_object_get_data (G_OBJECT (entry), "entry_label");
if (form->priv->entries_table)
row = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (entry), "row_no"));
if (show) {
if (g_slist_find (form->priv->hidden_entries, entry)) {
form->priv->hidden_entries = g_slist_remove (form->priv->hidden_entries, entry);
g_signal_handlers_disconnect_by_func (G_OBJECT (entry),
G_CALLBACK (widget_shown_cb), form);
}
gtk_widget_show (entry);
if (entry_label) {
if (g_slist_find (form->priv->hidden_entries, entry_label)) {
form->priv->hidden_entries = g_slist_remove (form->priv->hidden_entries,
entry_label);
g_signal_handlers_disconnect_by_func (G_OBJECT (entry_label),
G_CALLBACK (widget_shown_cb), form);
}
gtk_widget_show (entry_label);
}
if (row > -1)
gtk_table_set_row_spacing (GTK_TABLE (form->priv->entries_table), row, 5);
}
else {
if (!g_slist_find (form->priv->hidden_entries, entry)) {
form->priv->hidden_entries = g_slist_append (form->priv->hidden_entries, entry);
g_signal_connect_after (G_OBJECT (entry), "show",
G_CALLBACK (widget_shown_cb), form);
}
gtk_widget_hide (entry);
if (entry_label) {
if (!g_slist_find (form->priv->hidden_entries, entry_label)) {
form->priv->hidden_entries = g_slist_append (form->priv->hidden_entries,
entry_label);
g_signal_connect_after (G_OBJECT (entry_label), "show",
G_CALLBACK (widget_shown_cb), form);
}
gtk_widget_hide (entry_label);
}
if (row > -1)
gtk_table_set_row_spacing (GTK_TABLE (form->priv->entries_table), row, 0);
}
}
entries = g_slist_next (entries);
}
}
/**
* mg_form_entry_set_sensitive
* @form: a #MgForm widget
* @param: a #MgParameter object
* @sensitive:
*
* Shows or hides the #MgDataEntry in @form which corresponds to the
* @param parameter
*/
void
mg_form_entry_set_sensitive (MgForm *form, MgParameter *param, gboolean sensitive)
{
GSList *entries;
g_return_if_fail (form && IS_MG_FORM (form));
g_return_if_fail (form->priv);
entries = form->priv->entries;
while (entries) {
GtkWidget *entry = NULL;
MgParameter *thisparam = g_object_get_data (G_OBJECT (entries->data), "param");
if (thisparam) {
if (thisparam == param)
entry = GTK_WIDGET (entries->data);
}
else {
/* multiple parameters */
GSList *params;
MgContextNode *node;
node = g_object_get_data (G_OBJECT (entries->data), "node");
params = node->params;
while (params && !entry) {
if (params->data == (gpointer) param)
entry = GTK_WIDGET (entries->data);
params = g_slist_next (params);
}
}
if (entry) {
GtkWidget *entry_label = g_object_get_data (G_OBJECT (entry), "entry_label");
gtk_widget_set_sensitive (entry, sensitive);
if (entry_label)
gtk_widget_set_sensitive (entry_label, sensitive);
}
entries = g_slist_next (entries);
}
}
/**
* mg_form_set_entries_auto_default
* @form: a #MgForm widget
* @auto_default:
*
* Sets weather all the #MgDataEntry entries in the form must default
* to a default value if they are assigned a non valid value.
* Depending on the real type of entry, it will provide a default value
* which the user does not need to modify if it is OK.
*
* For example a date entry can by default display the current date.
*/
void
mg_form_set_entries_auto_default (MgForm *form, gboolean auto_default)
{
GSList *entries;
g_return_if_fail (form && IS_MG_FORM (form));
g_return_if_fail (form->priv);
entries = form->priv->entries;
while (entries) {
if (g_object_class_find_property (G_OBJECT_GET_CLASS (entries->data), "set_default_if_invalid"))
g_object_set (G_OBJECT (entries->data), "set_default_if_invalid", auto_default, NULL);
entries = g_slist_next (entries);
}
}
/**
* mg_form_set_entries_default
* @form: a #MgForm widget
*
* For each entry in the form, sets it to a default value if it is possible to do so.
*/
void
mg_form_set_entries_default (MgForm *form)
{
GSList *entries;
guint attrs;
g_return_if_fail (form && IS_MG_FORM (form));
g_return_if_fail (form->priv);
entries = form->priv->entries;
while (entries) {
attrs = mg_data_entry_get_attributes (MG_DATA_ENTRY (entries->data));
if (attrs & MG_DATA_ENTRY_CAN_BE_DEFAULT)
mg_data_entry_set_attributes (MG_DATA_ENTRY (entries->data),
MG_DATA_ENTRY_IS_DEFAULT, MG_DATA_ENTRY_IS_DEFAULT);
entries = g_slist_next (entries);
}
}
static void form_param_changed (MgForm *form, MgParameter *param, gboolean is_user_modif, GtkDialog *dlg);
/**
* mg_form_new_in_dialog
* @conf: a #MgConf object
* @context: a #MgContext structure
* @parent: the parent window for the new dialog, or %NULL
* @title: the title of the dialog window, or %NULL
* @header: a helper text displayed at the top of the dialog, or %NULL
*
* Creates a new #MgForm widget in the same way as mg_form_new()
* and puts it into a #GtkDialog widget.
*
* The #MgForm widget is attached to the dialog using the user property
* "form".
*
* Returns: the new #GtkDialog widget
*/
GtkWidget *
mg_form_new_in_dialog (MgConf *conf, MgContext *context, GtkWindow *parent,
const gchar *title, const gchar *header)
{
GtkWidget *form;
GtkWidget *dlg;
const gchar *rtitle;
form = mg_form_new (conf, context);
rtitle = title;
if (!rtitle)
rtitle = _("Values to be filled");
dlg = gtk_dialog_new_with_buttons (rtitle, parent,
GTK_DIALOG_MODAL,
GTK_STOCK_OK,
GTK_RESPONSE_ACCEPT,
GTK_STOCK_CANCEL,
GTK_RESPONSE_REJECT,
NULL);
if (header && *header) {
GtkWidget *label;
label = gtk_label_new (NULL);
gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
gtk_label_set_markup (GTK_LABEL (label), header);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), label, FALSE, FALSE, 5);
gtk_widget_show (label);
}
gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dlg)->vbox), 4);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), form, TRUE, TRUE, 10);
g_signal_connect (G_OBJECT (form), "param_changed",
G_CALLBACK (form_param_changed), dlg);
g_object_set_data (G_OBJECT (dlg), "form", form);
gtk_widget_show_all (form);
form_param_changed (MG_FORM (form), NULL, FALSE, GTK_DIALOG (dlg));
return dlg;
}
static void
form_param_changed (MgForm *form, MgParameter *param, gboolean is_user_modif, GtkDialog *dlg)
{
gboolean valid;
valid = mg_form_is_valid (form);
gtk_dialog_set_response_sensitive (dlg, GTK_RESPONSE_ACCEPT, valid);
}
|