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
|
/* mg-entry-time.c
*
* Copyright (C) 2003 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 "mg-entry-time.h"
#include <libmergeant/mg-data-handler.h>
#include <gdk/gdkkeysyms.h>
#include <string.h>
/*
* Main static functions
*/
static void mg_entry_time_class_init (MgEntryTimeClass * class);
static void mg_entry_time_init (MgEntryTime * srv);
static void mg_entry_time_dispose (GObject *object);
static void mg_entry_time_finalize (GObject *object);
static void mg_entry_time_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void mg_entry_time_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
/* properties */
enum
{
PROP_0,
PROP_EDITING_CANCELLED
};
/* GtkCellEditable interface */
static void mg_entry_time_cell_editable_init (GtkCellEditableIface *iface);
static void mg_entry_time_start_editing (GtkCellEditable *iface, GdkEvent *event);
/* virtual functions */
static GtkWidget *create_entry (MgEntryWrapper *mgwrap);
static void real_set_value (MgEntryWrapper *mgwrap, const GdaValue *value);
static GdaValue *real_get_value (MgEntryWrapper *mgwrap);
static void connect_signals(MgEntryWrapper *mgwrap, GCallback callback);
static gboolean expand_in_layout (MgEntryWrapper *mgwrap);
/* get a pointer to the parents to be able to call their destructor */
static GObjectClass *parent_class = NULL;
/* private structure */
struct _MgEntryTimePrivate
{
/* for date */
GtkWidget *entry_date;
GtkWidget *date;
GtkWidget *window;
GtkWidget *date_button;
/* for time */
GtkWidget *entry_time;
GtkWidget *legend;
/* for timestamp */
GtkWidget *hbox;
/* Last value set */
GdaValue *last_value_set;
};
static void
mg_entry_time_cell_editable_init (GtkCellEditableIface *iface)
{
iface->start_editing = mg_entry_time_start_editing;
}
guint
mg_entry_time_get_type (void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof (MgEntryTimeClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) mg_entry_time_class_init,
NULL,
NULL,
sizeof (MgEntryTime),
0,
(GInstanceInitFunc) mg_entry_time_init
};
static const GInterfaceInfo cell_editable_info = {
(GInterfaceInitFunc) mg_entry_time_cell_editable_init, /* interface_init */
NULL, /* interface_finalize */
NULL /* interface_data */
};
type = g_type_register_static (MG_ENTRY_WRAPPER_TYPE, "MgEntryTime", &info, 0);
g_type_add_interface_static (type, GTK_TYPE_CELL_EDITABLE, &cell_editable_info);
}
return type;
}
static void
mg_entry_time_class_init (MgEntryTimeClass * class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
parent_class = g_type_class_peek_parent (class);
object_class->dispose = mg_entry_time_dispose;
object_class->finalize = mg_entry_time_finalize;
MG_ENTRY_WRAPPER_CLASS (class)->create_entry = create_entry;
MG_ENTRY_WRAPPER_CLASS (class)->real_set_value = real_set_value;
MG_ENTRY_WRAPPER_CLASS (class)->real_get_value = real_get_value;
MG_ENTRY_WRAPPER_CLASS (class)->connect_signals = connect_signals;
MG_ENTRY_WRAPPER_CLASS (class)->expand_in_layout = expand_in_layout;
/* Properties */
object_class->set_property = mg_entry_time_set_property;
object_class->get_property = mg_entry_time_get_property;
g_object_class_install_property (object_class, PROP_EDITING_CANCELLED,
g_param_spec_boolean ("editing_cancelled", NULL, NULL, FALSE, G_PARAM_READABLE));
}
static void
mg_entry_time_init (MgEntryTime * mg_entry_time)
{
mg_entry_time->priv = g_new0 (MgEntryTimePrivate, 1);
mg_entry_time->priv->entry_date = NULL;
mg_entry_time->priv->entry_time = NULL;
mg_entry_time->priv->date = NULL;
mg_entry_time->priv->window = NULL;
mg_entry_time->priv->date_button = NULL;
mg_entry_time->priv->legend = NULL;
mg_entry_time->priv->hbox = NULL;
mg_entry_time->priv->last_value_set = NULL;
}
/**
* mg_entry_time_new
* @dh: the data handler to be used by the new widget
* @type: the requested data type (compatible with @dh)
*
* Creates a new widget which is mainly a GtkEntry
*
* Returns: the new widget
*/
GtkWidget *
mg_entry_time_new (MgDataHandler *dh, GdaValueType type)
{
GObject *obj;
MgEntryTime *mgtim;
g_return_val_if_fail (dh && IS_MG_DATA_HANDLER (dh), NULL);
g_return_val_if_fail (type != GDA_VALUE_TYPE_UNKNOWN, NULL);
g_return_val_if_fail (mg_data_handler_accepts_gda_type (dh, type), NULL);
obj = g_object_new (MG_ENTRY_TIME_TYPE, "handler", dh, NULL);
mgtim = MG_ENTRY_TIME (obj);
mg_data_entry_set_value_type (MG_DATA_ENTRY (mgtim), type);
return GTK_WIDGET (obj);
}
static void
mg_entry_time_dispose (GObject * object)
{
MgEntryTime *mg_entry_time;
g_return_if_fail (object != NULL);
g_return_if_fail (IS_MG_ENTRY_TIME (object));
mg_entry_time = MG_ENTRY_TIME (object);
if (mg_entry_time->priv) {
}
/* parent class */
parent_class->dispose (object);
}
static void
mg_entry_time_finalize (GObject * object)
{
MgEntryTime *mg_entry_time;
g_return_if_fail (object != NULL);
g_return_if_fail (IS_MG_ENTRY_TIME (object));
mg_entry_time = MG_ENTRY_TIME (object);
if (mg_entry_time->priv) {
if (mg_entry_time->priv->last_value_set)
gda_value_free (mg_entry_time->priv->last_value_set);
g_free (mg_entry_time->priv);
mg_entry_time->priv = NULL;
}
/* parent class */
parent_class->finalize (object);
}
static void
mg_entry_time_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
MgEntryTime *mgtim;
mgtim = MG_ENTRY_TIME (object);
if (mgtim->priv) {
switch (param_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
}
static void
mg_entry_time_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
MgEntryTime *mgtim;
gboolean cancelled;
mgtim = MG_ENTRY_TIME (object);
if (mgtim->priv) {
switch (param_id) {
case PROP_EDITING_CANCELLED:
cancelled = FALSE;
/* FIXME */
if (mgtim->priv->entry_date)
cancelled = GTK_ENTRY (mgtim->priv->entry_date)->editing_canceled;
if (!cancelled && mgtim->priv->entry_time)
cancelled = GTK_ENTRY (mgtim->priv->entry_time)->editing_canceled;
g_value_set_boolean (value, cancelled);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
}
static GtkWidget *create_entry_date (MgEntryTime *mgtim);
static GtkWidget *create_entry_time (MgEntryTime *mgtim);
static GtkWidget *create_entry_ts (MgEntryTime *mgtim);
static GtkWidget *
create_entry (MgEntryWrapper *mgwrap)
{
MgEntryTime *mgtim;
GtkWidget *entry = NULL;
g_return_val_if_fail (mgwrap && IS_MG_ENTRY_TIME (mgwrap), NULL);
mgtim = MG_ENTRY_TIME (mgwrap);
g_return_val_if_fail (mgtim->priv, NULL);
switch (mg_data_entry_get_value_type (MG_DATA_ENTRY (mgtim))) {
case GDA_VALUE_TYPE_DATE:
entry = create_entry_date (mgtim);
break;
case GDA_VALUE_TYPE_TIME:
entry = create_entry_time (mgtim);
break;
case GDA_VALUE_TYPE_TIMESTAMP:
entry = create_entry_ts (mgtim);
break;
default:
g_assert_not_reached ();
break;
}
return entry;
}
static void
real_set_value (MgEntryWrapper *mgwrap, const GdaValue *value)
{
MgEntryTime *mgtim;
GdaValueType type;
MgDataHandler *dh;
g_return_if_fail (mgwrap && IS_MG_ENTRY_TIME (mgwrap));
mgtim = MG_ENTRY_TIME (mgwrap);
g_return_if_fail (mgtim->priv);
dh = mg_data_entry_get_handler (MG_DATA_ENTRY (mgwrap));
type = mg_data_entry_get_value_type (MG_DATA_ENTRY (mgtim));
switch (type) {
case GDA_VALUE_TYPE_DATE:
if (value) {
if (gda_value_is_null (value))
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_date), "");
else {
gchar *str;
str = mg_data_handler_get_str_from_value (dh, value);
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_date), str);
g_free (str);
}
}
else
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_date), "");
break;
case GDA_VALUE_TYPE_TIME:
if (value) {
if (gda_value_is_null (value))
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_time), "");
else {
gchar *str;
str = mg_data_handler_get_str_from_value (dh, value);
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_time), str);
g_free (str);
}
}
else
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_time), "");
break;
case GDA_VALUE_TYPE_TIMESTAMP:
if (value) {
if (gda_value_is_null (value))
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_time), "");
else {
gchar *str, *ptr;
str = mg_data_handler_get_str_from_value (dh, value);
ptr = strtok (str, " ");
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_date), ptr);
ptr = strtok (NULL, " ");
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_time), ptr);
g_free (str);
}
}
else {
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_date), "");
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_time), "");
}
break;
default:
g_assert_not_reached ();
break;
}
/* keep track of the last value set */
if (mgtim->priv->last_value_set) {
gda_value_free (mgtim->priv->last_value_set);
mgtim->priv->last_value_set = NULL;
}
if (value)
mgtim->priv->last_value_set = gda_value_copy (value);
}
static GdaValue *
real_get_value (MgEntryWrapper *mgwrap)
{
GdaValue *value = NULL;
MgEntryTime *mgtim;
MgDataHandler *dh;
const gchar *str;
gchar *str2;
GdaValueType type;
g_return_val_if_fail (mgwrap && IS_MG_ENTRY_TIME (mgwrap), NULL);
mgtim = MG_ENTRY_TIME (mgwrap);
g_return_val_if_fail (mgtim->priv, NULL);
type = mg_data_entry_get_value_type (MG_DATA_ENTRY (mgtim));
dh = mg_data_entry_get_handler (MG_DATA_ENTRY (mgwrap));
switch (type) {
case GDA_VALUE_TYPE_DATE:
str = gtk_entry_get_text (GTK_ENTRY (mgtim->priv->entry_date));
value = mg_data_handler_get_value_from_str (dh, str, type); /* FIXME: not SQL but STR */
break;
case GDA_VALUE_TYPE_TIME:
str = gtk_entry_get_text (GTK_ENTRY (mgtim->priv->entry_time));
value = mg_data_handler_get_value_from_str (dh, str, type);
if (mgtim->priv->last_value_set) {
/* copy the 'timezone' part, we we have not modified */
const GdaTime *dgatime_set = gda_value_get_time (mgtim->priv->last_value_set);
GdaTime *gdatime = g_new (GdaTime, 1);
*gdatime = *(gda_value_get_time (value));
gdatime->timezone = dgatime_set->timezone;
gda_value_set_time (value, gdatime);
g_free (gdatime);
}
break;
case GDA_VALUE_TYPE_TIMESTAMP:
str2 = g_strdup_printf ("%s %s",
gtk_entry_get_text (GTK_ENTRY (mgtim->priv->entry_date)),
gtk_entry_get_text (GTK_ENTRY (mgtim->priv->entry_time)));
value = mg_data_handler_get_value_from_str (dh, str2, type);
g_free (str2);
if (mgtim->priv->last_value_set) {
/* copy the 'fraction' and 'timezone' parts, we we have not modified */
const GdaTimestamp *dgatime_set = gda_value_get_timestamp (mgtim->priv->last_value_set);
GdaTimestamp *gdatime = g_new (GdaTimestamp, 1);
*gdatime = *(gda_value_get_timestamp (value));
gdatime->fraction = dgatime_set->fraction;
gdatime->timezone = dgatime_set->timezone;
gda_value_set_timestamp (value, gdatime);
g_free (gdatime);
}
break;
default:
g_assert_not_reached ();
break;
}
if (!value) {
/* in case the mg_data_handler_get_value_from_str() returned an error because
the contents of the GtkEntry cannot be interpreted as a GdaValue */
value = gda_value_new_null ();
}
return value;
}
static void
connect_signals(MgEntryWrapper *mgwrap, GCallback callback)
{
MgEntryTime *mgtim;
GdaValueType type;
g_return_if_fail (mgwrap && IS_MG_ENTRY_TIME (mgwrap));
mgtim = MG_ENTRY_TIME (mgwrap);
g_return_if_fail (mgtim->priv);
type = mg_data_entry_get_value_type (MG_DATA_ENTRY (mgtim));
switch (type) {
case GDA_VALUE_TYPE_DATE:
g_signal_connect (G_OBJECT (mgtim->priv->entry_date), "changed",
callback, mgwrap);
break;
case GDA_VALUE_TYPE_TIME:
g_signal_connect (G_OBJECT (mgtim->priv->entry_time), "changed",
callback, mgwrap);
break;
case GDA_VALUE_TYPE_TIMESTAMP:
g_signal_connect (G_OBJECT (mgtim->priv->entry_date), "changed",
callback, mgwrap);
g_signal_connect (G_OBJECT (mgtim->priv->entry_time), "changed",
callback, mgwrap);
break;
default:
g_assert_not_reached ();
break;
}
}
static gboolean
expand_in_layout (MgEntryWrapper *mgwrap)
{
return FALSE;
}
/*
* callbacks for the date
*/
static gint date_delete_popup (GtkWidget *widget, MgEntryTime *mgtim);
static gint date_key_press_popup (GtkWidget *widget, GdkEventKey *event, MgEntryTime *mgtim);
static gint date_button_press_popup (GtkWidget *widget, GdkEventButton *event, MgEntryTime *mgtim);
static void date_day_selected (GtkCalendar *calendar, MgEntryTime *mgtim);
static void date_day_selected_double_click (GtkCalendar *calendar, MgEntryTime *mgtim);
static void date_calendar_choose_cb (GtkWidget *button, MgEntryTime *mgtim);
static GtkWidget *
create_entry_date (MgEntryTime *mgtim)
{
GtkWidget *wid, *hb, *window;
/* top widget */
hb = gtk_hbox_new (FALSE, 3);
/* text entry */
wid = gtk_entry_new ();
gtk_entry_set_max_length (GTK_ENTRY (wid), 10); /* for a numerical date format */
gtk_entry_set_width_chars (GTK_ENTRY (wid), 10);
gtk_box_pack_start (GTK_BOX (hb), wid, FALSE, FALSE, 0);
gtk_widget_show (wid);
mgtim->priv->entry_date = wid;
/* window to hold the calendar popup */
window = gtk_window_new (GTK_WINDOW_POPUP);
gtk_widget_set_events (window, gtk_widget_get_events (window) | GDK_KEY_PRESS_MASK);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (date_delete_popup), mgtim);
g_signal_connect (G_OBJECT (window), "key_press_event",
G_CALLBACK (date_key_press_popup), mgtim);
g_signal_connect (G_OBJECT (window), "button_press_event",
G_CALLBACK (date_button_press_popup), mgtim);
mgtim->priv->window = window;
/* calendar */
wid = gtk_calendar_new ();
mgtim->priv->date = wid;
gtk_container_add (GTK_CONTAINER (window), wid);
gtk_widget_show (wid);
g_signal_connect (G_OBJECT (wid), "day_selected",
G_CALLBACK (date_day_selected), mgtim);
g_signal_connect (G_OBJECT (wid), "day_selected_double_click",
G_CALLBACK (date_day_selected_double_click), mgtim);
/* button to pop up the calendar */
wid = gtk_button_new_with_label (_("Choose"));
gtk_box_pack_start (GTK_BOX (hb), wid, FALSE, FALSE, 0);
gtk_widget_show (wid);
g_signal_connect (G_OBJECT (wid), "clicked",
G_CALLBACK (date_calendar_choose_cb), mgtim);
mgtim->priv->date_button = wid;
/* padding */
wid = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX (hb), wid, TRUE, TRUE, 0);
gtk_widget_show (wid);
return hb;
}
static void hide_popup (MgEntryTime *mgtim)
{
gtk_widget_hide (mgtim->priv->window);
gtk_grab_remove (mgtim->priv->window);
}
static gint
date_delete_popup (GtkWidget *widget, MgEntryTime *mgtim)
{
hide_popup (mgtim);
return TRUE;
}
static gint
date_key_press_popup (GtkWidget *widget, GdkEventKey *event, MgEntryTime *mgtim)
{
if (event->keyval != GDK_Escape)
return FALSE;
g_signal_stop_emission_by_name (widget, "key_press_event");
hide_popup (mgtim);
return TRUE;
}
static gint
date_button_press_popup (GtkWidget *widget, GdkEventButton *event, MgEntryTime *mgtim)
{
GtkWidget *child;
child = gtk_get_event_widget ((GdkEvent *) event);
/* We don't ask for button press events on the grab widget, so
* if an event is reported directly to the grab widget, it must
* be on a window outside the application (and thus we remove
* the popup window). Otherwise, we check if the widget is a child
* of the grab widget, and only remove the popup window if it
* is not.
*/
if (child != widget) {
while (child) {
if (child == widget)
return FALSE;
child = child->parent;
}
}
hide_popup (mgtim);
return TRUE;
}
static void
date_day_selected (GtkCalendar *calendar, MgEntryTime *mgtim)
{
char buffer [256];
guint year, month, day;
struct tm mtm = {0};
char *str_utf8;
gtk_calendar_get_date (calendar, &year, &month, &day);
mtm.tm_mday = day;
mtm.tm_mon = month;
if (year > 1900)
mtm.tm_year = year - 1900;
else
mtm.tm_year = year;
if (strftime (buffer, sizeof (buffer), "%x", &mtm) == 0)
strcpy (buffer, "???");
buffer[sizeof(buffer)-1] = '\0';
str_utf8 = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
gtk_entry_set_text (GTK_ENTRY (mgtim->priv->entry_date),
str_utf8 ? str_utf8 : "");
g_free (str_utf8);
}
static void
date_day_selected_double_click (GtkCalendar *calendar, MgEntryTime *mgtim)
{
hide_popup (mgtim);
}
static gboolean popup_grab_on_window (GdkWindow *window, guint32 activate_time);
static void position_popup (MgEntryTime *mgtim);
static void
date_calendar_choose_cb (GtkWidget *button, MgEntryTime *mgtim)
{
GdaValue *value;
guint year=0, month=0, day=0;
MgDataHandler *dh;
/* setting the calendar to the latest displayed date */
dh = mg_data_entry_get_handler (MG_DATA_ENTRY (mgtim));
value = mg_data_entry_get_value (MG_DATA_ENTRY (mgtim));
if (value && !gda_value_is_null (value)) {
const GdaDate *date;
const GdaTimestamp *ts;
switch (mg_data_entry_get_value_type (MG_DATA_ENTRY (mgtim))) {
case GDA_VALUE_TYPE_DATE:
date = gda_value_get_date (value);
year = date->year;
month = date->month - 1;
day = date->day;
break;
case GDA_VALUE_TYPE_TIMESTAMP:
ts = gda_value_get_timestamp (value);
year = ts->year;
month = ts->month - 1;
day = ts->day;
break;
default:
g_assert_not_reached ();
break;
}
}
else {
time_t now;
struct tm *stm;
now = time (NULL);
stm = localtime (&now);
year = stm->tm_year + 1900;
month = stm->tm_mon;
day = stm->tm_mday;
}
gtk_calendar_select_month (GTK_CALENDAR (mgtim->priv->date), month, year);
gtk_calendar_select_day (GTK_CALENDAR (mgtim->priv->date), day);
/* popup window */
/* Temporarily grab pointer and keyboard, copied from GnomeDateEdit */
if (!popup_grab_on_window (button->window, gtk_get_current_event_time ()))
return;
position_popup (mgtim);
gtk_grab_add (mgtim->priv->window);
gtk_widget_show (mgtim->priv->window);
gtk_widget_grab_focus (mgtim->priv->date);
popup_grab_on_window (mgtim->priv->window->window,
gtk_get_current_event_time ());
}
static gboolean
popup_grab_on_window (GdkWindow *window, guint32 activate_time)
{
if ((gdk_pointer_grab (window, TRUE,
GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK,
NULL, NULL, activate_time) == 0)) {
if (gdk_keyboard_grab (window, TRUE,
activate_time) == 0)
return TRUE;
else {
gdk_pointer_ungrab (activate_time);
return FALSE;
}
}
return FALSE;
}
static void
position_popup (MgEntryTime *mgtim)
{
gint x, y;
gint bwidth, bheight;
GtkRequisition req;
gtk_widget_size_request (mgtim->priv->window, &req);
gdk_window_get_origin (mgtim->priv->date_button->window, &x, &y);
x += mgtim->priv->date_button->allocation.x;
y += mgtim->priv->date_button->allocation.y;
bwidth = mgtim->priv->date_button->allocation.width;
bheight = mgtim->priv->date_button->allocation.height;
x += bwidth - req.width;
y += bheight;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
gtk_window_move (GTK_WINDOW (mgtim->priv->window), x, y);
}
/*
* callbacks for the time
*/
static GtkWidget *
create_entry_time (MgEntryTime *mgtim)
{
GtkWidget *wid, *hb;
hb = gtk_hbox_new (FALSE, 3);
/* text entry */
wid = gtk_entry_new ();
gtk_box_pack_start (GTK_BOX (hb), wid, FALSE, FALSE, 0);
gtk_entry_set_max_length (GTK_ENTRY (wid), 8); /* max for HH:MM:SS format */
gtk_entry_set_width_chars (GTK_ENTRY (wid), 8);
gtk_widget_show (wid);
mgtim->priv->entry_time = wid;
/* small label */
wid = gtk_label_new (_("hh:mm:ss"));
gtk_box_pack_start (GTK_BOX (hb), wid, FALSE, FALSE, 0);
gtk_widget_show (wid);
mgtim->priv->legend = wid;
return hb;
}
/*
* callbacks for the timestamp
*/
static GtkWidget *
create_entry_ts (MgEntryTime *mgtim)
{
GtkWidget *hb, *wid;
hb = gtk_hbox_new (FALSE, 0);
/* date part */
wid = create_entry_date (mgtim);
gtk_box_pack_start (GTK_BOX (hb), wid, FALSE, FALSE, 0);
gtk_widget_show (wid);
/* time part */
wid = create_entry_time (mgtim);
gtk_box_pack_start (GTK_BOX (hb), wid, FALSE, FALSE, 0);
gtk_widget_show (wid);
mgtim->priv->hbox = hb;
return hb;
}
/*
* GtkCellEditable interface
*/
static void
gtk_cell_editable_entry_editing_done_cb (GtkEntry *entry, MgEntryTime *mgtim)
{
gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (mgtim));
}
static void
gtk_cell_editable_entry_remove_widget_cb (GtkEntry *entry, MgEntryTime *mgtim)
{
gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (mgtim));
}
static void
mg_entry_time_start_editing (GtkCellEditable *iface, GdkEvent *event)
{
MgEntryTime *mgtim;
g_return_if_fail (iface && IS_MG_ENTRY_TIME (iface));
mgtim = MG_ENTRY_TIME (iface);
g_return_if_fail (mgtim->priv);
if (mgtim->priv->date_button) {
gtk_widget_destroy (mgtim->priv->date_button);
mgtim->priv->date_button = NULL;
}
if (mgtim->priv->legend) {
gtk_widget_destroy (mgtim->priv->legend);
mgtim->priv->legend = NULL;
}
if (mgtim->priv->hbox) {
gtk_box_set_spacing (GTK_BOX (mgtim->priv->hbox), 0);
gtk_container_set_border_width (GTK_CONTAINER (mgtim->priv->hbox), 0);
}
if (mgtim->priv->entry_date) {
g_object_set (G_OBJECT (mgtim->priv->entry_date), "has_frame", FALSE, NULL);
gtk_cell_editable_start_editing (GTK_CELL_EDITABLE (mgtim->priv->entry_date), event);
g_signal_connect (G_OBJECT (mgtim->priv->entry_date), "editing_done",
G_CALLBACK (gtk_cell_editable_entry_editing_done_cb), mgtim);
g_signal_connect (G_OBJECT (mgtim->priv->entry_date), "remove_widget",
G_CALLBACK (gtk_cell_editable_entry_remove_widget_cb), mgtim);
}
if (mgtim->priv->entry_time) {
g_object_set (G_OBJECT (mgtim->priv->entry_time), "has_frame", FALSE, NULL);
gtk_cell_editable_start_editing (GTK_CELL_EDITABLE (mgtim->priv->entry_time), event);
g_signal_connect (G_OBJECT (mgtim->priv->entry_time), "editing_done",
G_CALLBACK (gtk_cell_editable_entry_editing_done_cb), mgtim);
g_signal_connect (G_OBJECT (mgtim->priv->entry_time), "remove_widget",
G_CALLBACK (gtk_cell_editable_entry_remove_widget_cb), mgtim);
}
mg_entry_shell_refresh (MG_ENTRY_SHELL (mgtim));
if (mgtim->priv->entry_date)
gtk_widget_grab_focus (mgtim->priv->entry_date);
else
gtk_widget_grab_focus (mgtim->priv->entry_time);
gtk_widget_queue_draw (GTK_WIDGET (mgtim));
}
|