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
|
/*
* Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
* Copyright (C) 2010 David King <davidk@openismus.com>
* Copyright (C) 2012 Snicksie <none@none.com>
*
* 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 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, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <glib/gprintf.h>
#include "utility.h"
#include <libgda/libgda.h>
#include <glib/gi18n-lib.h>
#include <libgda-ui/gdaui-data-entry.h>
#include <libgda-ui/libgda-ui.h>
#include <libgda-ui/gdaui-decl.h>
/*
* _gdaui_utility_entry_build_actions_menu
* @obj_data:
* @attrs:
* @function:
*
* Creates a GtkMenu widget which contains the possible actions on a data entry which
* attributes are @attrs. After the menu has been displayed, and when an action is selected,
* the @function callback is called with the 'user_data' being @obj_data.
*
* The menu item which emits the signal has a "action" attribute which describes what action the
* user has requested.
*
* Returns: the new menu
*/
GtkWidget *
_gdaui_utility_entry_build_actions_menu (GObject *obj_data, guint attrs, GCallback function)
{
GtkWidget *menu, *mitem;
gchar *str;
gboolean nullact = FALSE;
gboolean defact = FALSE;
gboolean reset = FALSE;
gboolean value_is_null;
gboolean value_is_modified;
gboolean value_is_default;
menu = gtk_menu_new ();
/* which menu items to make sensitive ? */
value_is_null = attrs & GDA_VALUE_ATTR_IS_NULL;
value_is_modified = ! (attrs & GDA_VALUE_ATTR_IS_UNCHANGED);
value_is_default = attrs & GDA_VALUE_ATTR_IS_DEFAULT;
if (! (attrs & GDA_VALUE_ATTR_NO_MODIF)) {
if ((attrs & GDA_VALUE_ATTR_CAN_BE_NULL) &&
!(attrs & GDA_VALUE_ATTR_IS_NULL))
nullact = TRUE;
if ((attrs & GDA_VALUE_ATTR_CAN_BE_DEFAULT) &&
!(attrs & GDA_VALUE_ATTR_IS_DEFAULT))
defact = TRUE;
if (!(attrs & GDA_VALUE_ATTR_IS_UNCHANGED)) {
if (attrs & GDA_VALUE_ATTR_HAS_VALUE_ORIG)
reset = TRUE;
}
}
/* set to NULL item */
str = g_strdup (_("Unset"));
mitem = gtk_check_menu_item_new_with_label (str);
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mitem),
value_is_null);
gtk_widget_show (mitem);
g_object_set_data (G_OBJECT (mitem), "action", GUINT_TO_POINTER (GDA_VALUE_ATTR_IS_NULL));
g_signal_connect (G_OBJECT (mitem), "activate",
G_CALLBACK (function), obj_data);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
g_free (str);
gtk_widget_set_sensitive (mitem, nullact);
/* default value item */
str = g_strdup (_("Set to default value"));
mitem = gtk_check_menu_item_new_with_label (str);
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mitem),
value_is_default);
gtk_widget_show (mitem);
g_object_set_data (G_OBJECT (mitem), "action", GUINT_TO_POINTER (GDA_VALUE_ATTR_IS_DEFAULT));
g_signal_connect (G_OBJECT (mitem), "activate",
G_CALLBACK (function), obj_data);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
g_free (str);
gtk_widget_set_sensitive (mitem, defact);
/* reset to original value item */
str = g_strdup (_("Reset to original value"));
mitem = gtk_check_menu_item_new_with_label (str);
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mitem),
!value_is_modified);
gtk_widget_show (mitem);
g_object_set_data (G_OBJECT (mitem), "action", GUINT_TO_POINTER (GDA_VALUE_ATTR_IS_UNCHANGED));
g_signal_connect (G_OBJECT (mitem), "activate",
G_CALLBACK (function), obj_data);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), mitem);
g_free (str);
gtk_widget_set_sensitive (mitem, reset);
return menu;
}
/*
* _gdaui_utility_entry_build_info_colors_array_a
*
* Creates an array of colors for the different states of an entry:
* Valid <-> No special color
* Null <-> Green
* Default <-> Blue
* Invalid <-> Red
* Each status (except Valid) is represented by two colors for GTK_STATE_NORMAL and
* GTK_STATE_PRELIGHT.
*
* Returns: a new array of 6 colors
*/
GdkRGBA **
_gdaui_utility_entry_build_info_colors_array_a (void)
{
GdkRGBA **colors;
GdkRGBA *color;
colors = g_new0 (GdkRGBA *, 6);
/* Green color */
color = g_new0 (GdkRGBA, 1);
g_assert (gdk_rgba_parse (color, GDAUI_COLOR_NORMAL_NULL));
colors[0] = color;
color = g_new0 (GdkRGBA, 1);
g_assert (gdk_rgba_parse (color, GDAUI_COLOR_PRELIGHT_NULL));
colors[1] = color;
/* Blue color */
color = g_new0 (GdkRGBA, 1);
g_assert (gdk_rgba_parse (color, GDAUI_COLOR_NORMAL_DEFAULT));
colors[2] = color;
color = g_new0 (GdkRGBA, 1);
g_assert (gdk_rgba_parse (color, GDAUI_COLOR_PRELIGHT_DEFAULT));
colors[3] = color;
/* Red color */
color = g_new0 (GdkRGBA, 1);
g_assert (gdk_rgba_parse (color, GDAUI_COLOR_NORMAL_INVALID));
colors[4] = color;
color = g_new0 (GdkRGBA, 1);
g_assert (gdk_rgba_parse (color, GDAUI_COLOR_PRELIGHT_INVALID));
colors[5] = color;
return colors;
}
/*
* _gdaui_utility_markup_title
*/
gchar *
_gdaui_utility_markup_title (const gchar *title, gboolean optional)
{
if (!optional)
return g_strdup_printf ("%s <span foreground='red' weight='bold'>*</span>:", title);
else
return g_strdup_printf ("%s:", title);
}
/*
* _gdaui_utility_proxy_compute_attributes_for_group
*
* Computes an attributes from the individual attributes of the values stored in @store and
* corresponding to the columns for the parameters in @group (as described by @model_iter),
* at row pointed by @iter
*
* Returns: the attributes
*/
guint
_gdaui_utility_proxy_compute_attributes_for_group (GdauiSetGroup *group, GdauiDataStore *store,
GdaDataModelIter *model_iter, GtkTreeIter *tree_iter,
gboolean *to_be_deleted)
{
guint attributes = GDA_VALUE_ATTR_IS_NULL | GDA_VALUE_ATTR_CAN_BE_NULL |
GDA_VALUE_ATTR_IS_DEFAULT | GDA_VALUE_ATTR_CAN_BE_DEFAULT |
GDA_VALUE_ATTR_IS_UNCHANGED | GDA_VALUE_ATTR_HAS_VALUE_ORIG;
gboolean to_del = TRUE, local_to_del;
GSList *list;
gint col;
gint offset;
guint localattr;
GdaDataProxy *proxy;
proxy = gdaui_data_store_get_proxy (store);
offset = gda_data_proxy_get_proxied_model_n_cols (proxy);
/* list the values in proxy_model for each param in GDA_SET_NODE (group->group->nodes->data)->params */
attributes = 0;
for (list = gda_set_group_get_nodes (gdaui_set_group_get_group (group)); list; list = list->next) {
col = g_slist_index (((GdaSet*)model_iter)->holders, gda_set_node_get_holder (GDA_SET_NODE (list->data)));
gtk_tree_model_get (GTK_TREE_MODEL (store), tree_iter,
GDAUI_DATA_STORE_COL_TO_DELETE, &local_to_del,
offset + col, &localattr, -1);
if (list == gda_set_group_get_nodes (gdaui_set_group_get_group (group)))
attributes = localattr;
else
attributes &= localattr;
to_del = to_del && local_to_del;
}
if (to_be_deleted)
*to_be_deleted = to_del;
return attributes;
}
/*
* _gdaui_utility_proxy_compute_values_for_group:
*
* Computes a list of values containing the individual values stored in @store and
* corresponding to the columns for the parameters if @group, at row pointed by @tree_iter.
*
* If @model_values is TRUE, then the values in the returned list are the values of the
* @group->nodes_source->data_model, at the @group->nodes_source->shown_cols_index columns
*
* For both performances reasons and because the contents of the @group->nodes_source->data_model
* may change, this function uses the gda_data_proxy_get_model_row_value() function.
*
* The returned list must be freed by the caller, but the values in the list must not be modified or
* freed.
*/
GList *
_gdaui_utility_proxy_compute_values_for_group (GdauiSetGroup *group, GdauiDataStore *store,
GdaDataModelIter *model_iter,
GtkTreeIter *tree_iter, gboolean model_values)
{
GList *retval = NULL;
#ifdef PROXY_STORE_EXTRA_VALUES
GdaDataProxy *proxy;
proxy = gdaui_data_store_get_proxy (store);
#endif
if (!model_values) {
GSList *list;
GValue *value;
for (list = gda_set_group_get_nodes (gdaui_set_group_get_group (group)); list; list = list->next) {
gint col;
col = g_slist_index (((GdaSet*)model_iter)->holders, gda_set_node_get_holder (GDA_SET_NODE (list->data)));
gtk_tree_model_get (GTK_TREE_MODEL (store), tree_iter, col, &value, -1);
retval = g_list_append (retval, value);
}
}
else {
gint col, i;
#ifdef PROXY_STORE_EXTRA_VALUES
gint proxy_row;
#endif
GdauiSetSource *source;
const GValue *value;
gboolean slow_way = FALSE;
gboolean ret_null = FALSE;
#ifdef PROXY_STORE_EXTRA_VALUES
proxy_row = gdaui_data_store_get_row_from_iter (store, tree_iter);
#endif
source = gdaui_set_group_get_source (group);
for (i = 0 ; (i < gdaui_set_source_get_shown_n_cols (source)) && !ret_null; i++) {
col = (gdaui_set_source_get_shown_columns (source))[i];
#ifdef PROXY_STORE_EXTRA_VALUES
if (!slow_way) {
value = gda_data_proxy_get_model_row_value (proxy, source->data_model, proxy_row, col);
if (value)
retval = g_list_append (retval, (GValue *) value);
else {
if (gda_data_proxy_get_assigned_model_col (proxy, source->data_model, col) < 0)
slow_way = TRUE;
else
retval = g_list_append (retval, NULL);
}
}
#endif
slow_way = TRUE;
if (slow_way) {
/* may be a bit slow: try to find the values in the model, it's better if the user
* uses gda_data_proxy_assign_model_col()! */
GSList *key_values = NULL;
gint row, *cols_index;
GSList *list;
gint j;
cols_index = g_new0 (gint, gda_set_group_get_n_nodes (gdaui_set_group_get_group (group)));
for (list = gda_set_group_get_nodes (gdaui_set_group_get_group (group)), j = 0; list; list = list->next, j++) {
gint colno;
colno = g_slist_index (((GdaSet*)model_iter)->holders,
gda_set_node_get_holder (GDA_SET_NODE (list->data)));
cols_index [j] = gda_set_node_get_source_column (GDA_SET_NODE (list->data));
gtk_tree_model_get (GTK_TREE_MODEL (store), tree_iter,
colno, &value, -1);
key_values = g_slist_append (key_values, (GValue *) value);
}
row = gda_data_model_get_row_from_values (gda_set_source_get_data_model (gdaui_set_source_get_source (source)),
key_values, cols_index);
if (row >= 0) {
value = gda_data_model_get_value_at (gda_set_source_get_data_model (gdaui_set_source_get_source (source)),
col, row, NULL);
retval = g_list_append (retval, (GValue *) value);
}
else {
#ifdef DEBUG_WARNING
g_warning ("Could not find requested value in restricting data model");
g_print ("Requested: ");
list = key_values;
j = 0;
while (list) {
gchar *str;
if (value) {
str = gda_value_stringify ((GValue *) list->data);
g_print ("/%s @col %d", str, cols_index [j]);
g_free (str);
}
else
g_print ("/NULL @col %d", cols_index [j]);
list = g_slist_next (list);
j++;
}
g_print (" in data model: \n");
gda_data_model_dump (source->data_model, stdout);
#endif
ret_null = TRUE;
}
g_slist_free (key_values);
}
}
if (ret_null) {
g_list_free (retval);
retval = NULL;
}
}
return retval;
}
/*
* Errors reporting
*/
static GtkWidget *
create_data_error_dialog (GdauiDataProxy *form, gboolean with_question, gboolean can_discard, GError *filled_error)
{
GtkWidget *dlg;
const gchar *msg1 = NULL, *msg2 = NULL;
if (can_discard) {
msg1 = _("Current modified data is invalid");
if (with_question)
msg2 =_("You may now choose to correct it, or to discard "
"the modifications.\n\n"
"What do you want to do?");
else
msg2 = _("please correct it and try again, or discard "
"the modifications.");
}
else {
if (with_question)
g_warning ("Incoherence problem...\n");
else {
msg1 = _("Part of the current modified data was invalid");
msg2 = _("As no transaction was used, only a part of the valid data\n"
"has been written, and the remaining modification have been discarded.");
}
}
dlg = gtk_message_dialog_new_with_markup ((GtkWindow *) gtk_widget_get_toplevel (GTK_WIDGET (form)),
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
with_question ? GTK_BUTTONS_NONE : GTK_BUTTONS_CLOSE,
"<b>%s:</b>\n\n%s", msg1, msg2);
if (filled_error && filled_error->message) {
GtkWidget *label;
label = gtk_label_new (filled_error->message);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dlg))),
label, TRUE, TRUE, 0);
gtk_widget_show (label);
}
return dlg;
}
/*
* _gdaui_utility_display_error_with_keep_or_discard_choice
* @form: a #GdauiDataProxy
* @filled_error: a #GError containing the error to display
*
* Displays a dialog showing @filled_error's message and asks the user to either keep the data which
* led to the error, or to discard it.
*
* Returns: TRUE if current data can be discarded
*/
gboolean
_gdaui_utility_display_error_with_keep_or_discard_choice (GdauiDataProxy *form, GError *filled_error)
{
GtkWidget *dlg;
gint res;
if (filled_error && (filled_error->domain == GDA_DATA_PROXY_ERROR) &&
(filled_error->code == GDA_DATA_PROXY_COMMIT_CANCELLED))
return FALSE;
dlg = create_data_error_dialog (form, TRUE, TRUE, filled_error);
gtk_dialog_add_buttons (GTK_DIALOG (dlg),
_("Discard modified data"), GTK_RESPONSE_REJECT,
_("Correct data first"), GTK_RESPONSE_NONE, NULL);
res = gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
return (res == GTK_RESPONSE_REJECT) ? TRUE : FALSE;
}
/*
* _gdaui_utility_display_error
* @form: a #GdauiDataProxy
* @filled_error: a #GError containing the error to display
*
* Displays a dialog showing @filled_error's message and asks the user to either keep the data which
* led to the error.
*/
void
_gdaui_utility_display_error (GdauiDataProxy *form, gboolean can_discard, GError *filled_error)
{
GtkWidget *dlg;
if (filled_error && (filled_error->domain == GDA_DATA_PROXY_ERROR) &&
(filled_error->code == GDA_DATA_PROXY_COMMIT_CANCELLED))
return;
dlg = create_data_error_dialog (form, FALSE, can_discard, filled_error);
gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
}
/*
* _gdaui_utility_show_error
* @format:
* @...:
*
*/
void
_gdaui_utility_show_error (GtkWindow *parent, const gchar *format, ...)
{
va_list args;
gchar sz[2048];
GtkWidget *dialog;
/* build the message string */
va_start (args, format);
vsnprintf (sz, sizeof sz, format, args);
va_end (args);
/* create the error message dialog */
gchar *str;
str = g_strdup_printf ("<span weight=\"bold\">%s</span>%s\n", _("Error:"), sz);
dialog = gtk_message_dialog_new_with_markup (parent,
GTK_DIALOG_DESTROY_WITH_PARENT |
GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE, "%s", str);
g_free (str);
gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
gtk_button_new_from_stock (GTK_STOCK_OK),
GTK_RESPONSE_OK);
gtk_widget_show_all (dialog);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
/*
* Compares 2 GdaDataModelIter
*
* Returns: %TRUE if they have at least one difference
*/
gboolean
_gdaui_utility_iter_differ (GdaDataModelIter *iter1, GdaDataModelIter *iter2)
{
GSList *list1, *list2;
gboolean retval = TRUE;
for (list1 = GDA_SET (iter1)->holders, list2 = GDA_SET (iter2)->holders;
list1 && list2;
list1 = list1->next, list2 = list2->next) {
GdaHolder *oh, *nh;
oh = GDA_HOLDER (list1->data);
nh = GDA_HOLDER (list2->data);
if (gda_holder_get_not_null (oh) != gda_holder_get_not_null (nh))
goto out;
GType ot, nt;
ot = gda_holder_get_g_type (oh);
nt = gda_holder_get_g_type (nh);
if (ot && (ot != nt))
goto out;
if (ot != nt)
g_object_set (oh, "g-type", nt, NULL);
const gchar *oid, *nid;
oid = gda_holder_get_id (oh);
nid = gda_holder_get_id (nh);
if ((oid && !nid) || (!oid && nid) ||
(oid && strcmp (oid, nid)))
goto out;
}
if (list1 || list2)
goto out;
retval = FALSE; /* iters don't differ */
out:
return retval;
}
static gboolean tree_view_button_pressed_cb (GtkWidget *widget, GdkEventButton *event, gpointer unuseddata);
/*
* Setup a callback on the treeview to, on right click:
* - if there is no row under the cursor, then force an empty selection
* OR
* - if the row under the cursor is not selected, then force the selection of only that row
* OR
* - otherwise don't change anything
*/
void
_gdaui_setup_right_click_selection_on_treeview (GtkTreeView *tview)
{
g_return_if_fail (GTK_IS_TREE_VIEW (tview));
g_signal_connect (G_OBJECT (tview), "button-press-event",
G_CALLBACK (tree_view_button_pressed_cb), NULL);
}
static gboolean
tree_view_button_pressed_cb (GtkWidget *widget, GdkEventButton *event, G_GNUC_UNUSED gpointer data)
{
GtkTreeView *tree_view;
GtkTreeSelection *selection;
if (event->button != 3)
return FALSE;
tree_view = GTK_TREE_VIEW (widget);
selection = gtk_tree_view_get_selection (tree_view);
/* force selection of row on which clicked occurred */
if (event->window == gtk_tree_view_get_bin_window (tree_view)) {
GtkTreePath *path;
if (gtk_tree_view_get_path_at_pos (tree_view, event->x, event->y, &path,
NULL, NULL, NULL)) {
if (! gtk_tree_selection_path_is_selected (selection, path)) {
gtk_tree_selection_unselect_all (selection);
gtk_tree_selection_select_path (selection, path);
}
gtk_tree_path_free (path);
}
else
gtk_tree_selection_unselect_all (selection);
}
return FALSE;
}
|