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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* e-completion-view.c - A text completion selection widget
* Copyright 2000, 2001, Ximian, Inc.
*
* Authors:
* Miguel de Icaza <miguel@ximian.com>
* Adapted by Jon Trowbridge <trow@ximian.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License, version 2, as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <config.h>
#include <math.h>
#include <gdk/gdkkeysyms.h>
#include "table/e-table-scrolled.h"
#include "table/e-table-simple.h"
#include "e-util/e-i18n.h"
#include "e-util/e-util-marshal.h"
#include "e-completion-view.h"
enum {
E_COMPLETION_VIEW_NONEMPTY,
E_COMPLETION_VIEW_ADDED,
E_COMPLETION_VIEW_FULL,
E_COMPLETION_VIEW_BROWSE,
E_COMPLETION_VIEW_UNBROWSE,
E_COMPLETION_VIEW_ACTIVATE,
E_COMPLETION_VIEW_LAST_SIGNAL
};
static guint e_completion_view_signals[E_COMPLETION_VIEW_LAST_SIGNAL] = { 0 };
static void e_completion_view_disconnect (ECompletionView *cv);
static ETable *e_completion_view_table (ECompletionView *cv);
static void e_completion_view_clear_choices (ECompletionView *cv);
static void e_completion_view_set_cursor_row (ECompletionView *cv, gint r);
static void e_completion_view_select (ECompletionView *cv, gint r);
static gint e_completion_view_key_press_handler (GtkWidget *w, GdkEventKey *key_event, gpointer user_data);
static void e_completion_view_class_init (ECompletionViewClass *klass);
static void e_completion_view_init (ECompletionView *completion);
static void e_completion_view_dispose (GObject *object);
#define PARENT_TYPE GTK_TYPE_EVENT_BOX
static GtkObjectClass *parent_class;
static gint
e_completion_view_local_key_press_handler (GtkWidget *w, GdkEventKey *ev)
{
return e_completion_view_key_press_handler (w, ev, w);
}
static void
e_completion_view_paint (GtkWidget *widget, GdkRectangle *area)
{
gint i;
g_return_if_fail (widget != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
g_return_if_fail (area != NULL);
if (!GTK_WIDGET_DRAWABLE (widget))
return;
for (i = 0; i < E_COMPLETION_VIEW (widget)->border_width; ++i) {
gdk_draw_rectangle (widget->window,
widget->style->black_gc,
FALSE, i, i,
widget->allocation.width-1-2*i,
widget->allocation.height-1-2*i);
}
}
#if 0
static void
e_completion_view_draw (GtkWidget *widget, GdkRectangle *area)
{
GtkBin *bin;
GdkRectangle child_area;
g_return_if_fail (widget != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
g_return_if_fail (area != NULL);
if (GTK_WIDGET_DRAWABLE (widget)) {
bin = GTK_BIN (widget);
e_completion_view_paint (widget, area);
if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
gtk_widget_draw (bin->child, &child_area);
}
}
#endif
static gint
e_completion_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
{
GtkBin *bin;
GdkEventExpose child_event;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (E_IS_COMPLETION_VIEW (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
if (GTK_WIDGET_DRAWABLE (widget)) {
bin = GTK_BIN (widget);
e_completion_view_paint (widget, &event->area);
child_event = *event;
if (bin->child &&
GTK_WIDGET_NO_WINDOW (bin->child) &&
gtk_widget_intersect (bin->child, &event->area, &child_event.area))
gtk_widget_send_expose (bin->child, (GdkEvent*) &child_event);
}
return FALSE;
}
static void
e_completion_view_size_request (GtkWidget *widget, GtkRequisition *requisition)
{
GtkBin *bin;
g_return_if_fail (widget != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
g_return_if_fail (requisition != NULL);
bin = GTK_BIN (widget);
requisition->width = 2 * E_COMPLETION_VIEW (widget)->border_width;
requisition->height = 2 * E_COMPLETION_VIEW (widget)->border_width;
if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) {
GtkRequisition child_requisition;
gtk_widget_size_request (bin->child, &child_requisition);
requisition->width += child_requisition.width;
requisition->height += child_requisition.height;
}
requisition->height = MAX (100, requisition->height);
}
static void
e_completion_view_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
{
GtkBin *bin;
GtkAllocation child_allocation;
g_return_if_fail (widget != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (widget));
g_return_if_fail (allocation != NULL);
bin = GTK_BIN (widget);
widget->allocation = *allocation;
child_allocation.x = E_COMPLETION_VIEW (widget)->border_width;
child_allocation.width = MAX(0, (gint)allocation->width - child_allocation.x * 2);
child_allocation.y = E_COMPLETION_VIEW (widget)->border_width;
child_allocation.height = MAX (0, (gint)allocation->height - child_allocation.y * 2);
if (GTK_WIDGET_REALIZED (widget)) {
gdk_window_move_resize (widget->window,
allocation->x,
allocation->y,
allocation->width,
allocation->height);
}
if (bin->child) {
gtk_widget_size_allocate (bin->child, &child_allocation);
}
}
E_MAKE_TYPE (e_completion_view,
"ECompletionView",
ECompletionView,
e_completion_view_class_init,
e_completion_view_init,
PARENT_TYPE)
static void
e_completion_view_class_init (ECompletionViewClass *klass)
{
GObjectClass *object_class = (GObjectClass *) klass;
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
parent_class = g_type_class_ref (PARENT_TYPE);
e_completion_view_signals[E_COMPLETION_VIEW_NONEMPTY] =
g_signal_new ("nonempty",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ECompletionViewClass, nonempty),
NULL, NULL,
e_util_marshal_NONE__NONE,
G_TYPE_NONE, 0);
e_completion_view_signals[E_COMPLETION_VIEW_ADDED] =
g_signal_new ("added",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ECompletionViewClass, added),
NULL, NULL,
e_util_marshal_NONE__NONE,
G_TYPE_NONE, 0);
e_completion_view_signals[E_COMPLETION_VIEW_FULL] =
g_signal_new ("full",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ECompletionViewClass, full),
NULL, NULL,
e_util_marshal_NONE__NONE,
G_TYPE_NONE, 0);
e_completion_view_signals[E_COMPLETION_VIEW_BROWSE] =
g_signal_new ("browse",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ECompletionViewClass, browse),
NULL, NULL,
e_util_marshal_NONE__POINTER,
G_TYPE_NONE, 1,
G_TYPE_POINTER);
e_completion_view_signals[E_COMPLETION_VIEW_UNBROWSE] =
g_signal_new ("unbrowse",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ECompletionViewClass, unbrowse),
NULL, NULL,
e_util_marshal_NONE__NONE,
G_TYPE_NONE, 0);
e_completion_view_signals[E_COMPLETION_VIEW_ACTIVATE] =
g_signal_new ("activate",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ECompletionViewClass, activate),
NULL, NULL,
e_util_marshal_NONE__POINTER,
G_TYPE_NONE, 1,
G_TYPE_POINTER);
object_class->dispose = e_completion_view_dispose;
widget_class->key_press_event = e_completion_view_local_key_press_handler;
widget_class->expose_event = e_completion_view_expose_event;
widget_class->size_request = e_completion_view_size_request;
widget_class->size_allocate = e_completion_view_size_allocate;
}
static void
e_completion_view_init (ECompletionView *completion)
{
completion->border_width = 2;
completion->choices = g_ptr_array_new ();
}
static void
e_completion_view_dispose (GObject *object)
{
ECompletionView *cv = E_COMPLETION_VIEW (object);
e_completion_view_disconnect (cv);
if (cv->choices) {
e_completion_view_clear_choices (cv);
g_ptr_array_free (cv->choices, TRUE);
cv->choices = NULL;
}
if (cv->key_widget) {
g_signal_handler_disconnect (cv->key_widget, cv->key_signal_id);
g_object_unref (cv->key_widget);
cv->key_widget = NULL;
}
if (cv->completion)
g_object_unref (cv->completion);
cv->completion = NULL;
if (G_OBJECT_CLASS (parent_class)->dispose)
(G_OBJECT_CLASS (parent_class)->dispose) (object);
}
static void
e_completion_view_disconnect (ECompletionView *cv)
{
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
if (cv->begin_signal_id)
g_signal_handler_disconnect (cv->completion, cv->begin_signal_id);
if (cv->comp_signal_id)
g_signal_handler_disconnect (cv->completion, cv->comp_signal_id);
if (cv->end_signal_id)
g_signal_handler_disconnect (cv->completion, cv->end_signal_id);
cv->begin_signal_id = 0;
cv->comp_signal_id = 0;
cv->end_signal_id = 0;
}
static ETable *
e_completion_view_table (ECompletionView *cv)
{
return e_table_scrolled_get_table (E_TABLE_SCROLLED (cv->table));
}
static void
e_completion_view_clear_choices (ECompletionView *cv)
{
ECompletionMatch *match;
GPtrArray *m;
int i;
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
m = cv->choices;
for (i = 0; i < m->len; i++) {
match = g_ptr_array_index (m, i);
e_completion_match_unref (match);
}
g_ptr_array_set_size (m, 0);
}
static void
e_completion_view_set_cursor_row (ECompletionView *cv, gint r)
{
ETable *table;
GtkAdjustment *adj;
gint x, y1, y2, r1, r2, c;
double fracline;
gint iteration_count=0;
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
#ifndef G_DISABLE_CHECKS
/* choices->len is unsigned, but it is reasonable for r to be
* < 0 */
if (r > 0) {
g_return_if_fail (r < cv->choices->len);
}
#endif
adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (cv->table));
table = e_completion_view_table (cv);
if (r < 0) {
e_selection_model_clear (E_SELECTION_MODEL(table->selection));
/* Move back to the top when we clear the selection */
gtk_adjustment_set_value (adj, adj->lower);
return;
}
e_table_set_cursor_row (table, r);
/* OK, now the tricky bit. We try to insure that this row is
visible. */
/* If we are selecting the first or last row, then it is easy. We just
cram the vadjustment all the way up/down. */
if (r == 0) {
gtk_adjustment_set_value (adj, adj->lower);
return;
} else if (r == cv->choices->len - 1) {
gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
return;
}
fracline = ((adj->upper - adj->lower - adj->page_size) / (gint)cv->choices->len) / 4;
while (iteration_count < 100) {
x = GTK_LAYOUT(table->table_canvas)->hadjustment->value;
y1 = GTK_LAYOUT(table->table_canvas)->vadjustment->value;
y2 = y1 + cv->table->allocation.height;
e_table_group_compute_location (e_completion_view_table (cv)->group, &x, &y1, &r1, &c);
e_table_group_compute_location (e_completion_view_table (cv)->group, &x, &y2, &r2, &c);
if (r <= r1) {
gtk_adjustment_set_value (adj, adj->value - fracline);
} else if (r >= r2) {
gtk_adjustment_set_value (adj, adj->value + fracline);
} else
return;
++iteration_count;
}
g_assert_not_reached ();
}
static void
e_completion_view_select (ECompletionView *cv, gint r)
{
ECompletionMatch *match;
match = g_ptr_array_index (cv->choices, r);
cv->selection = r;
e_completion_view_set_cursor_row (cv, r);
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_ACTIVATE], 0, match);
}
static gint
e_completion_view_key_press_handler (GtkWidget *w, GdkEventKey *key_event, gpointer user_data)
{
ECompletionView *cv = E_COMPLETION_VIEW (user_data);
gint dir = 0;
gboolean key_handled = TRUE, complete_key = FALSE, uncomplete_key = FALSE, is_space = FALSE;
/* FIXME: This is totally lame.
The ECompletionView should be able to specify multiple completion/uncompletion keys, or just
have sensible defaults. */
if ((cv->complete_key && key_event->keyval == cv->complete_key)
|| ((key_event->keyval == GDK_n || key_event->keyval == GDK_N) && (key_event->state & GDK_CONTROL_MASK)))
complete_key = TRUE;
if ((cv->uncomplete_key && key_event->keyval == cv->uncomplete_key)
|| ((key_event->keyval == GDK_p || key_event->keyval == GDK_P) && (key_event->state & GDK_CONTROL_MASK)))
uncomplete_key = TRUE;
/* Start up a completion.*/
if (complete_key && !cv->editable) {
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_BROWSE], 0, NULL);
goto stop_emission;
}
/* Stop our completion. */
if (uncomplete_key && cv->editable && cv->selection < 0) {
e_completion_view_set_cursor_row (cv, -1);
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_UNBROWSE], 0);
goto stop_emission;
}
if (!cv->editable)
return FALSE;
switch (key_event->keyval) {
case GDK_n:
case GDK_N:
/* We (heart) emacs: treat ctrl-n as down */
if (! (key_event->state & GDK_CONTROL_MASK))
return FALSE;
case GDK_Down:
case GDK_KP_Down:
dir = 1;
break;
case GDK_p:
case GDK_P:
/* Treat ctrl-p as up */
if (! (key_event->state & GDK_CONTROL_MASK))
return FALSE;
case GDK_Up:
case GDK_KP_Up:
dir = -1;
break;
case GDK_Tab:
/* If our cursor is still up in the entry, move down into
the popup. Otherwise unbrowse. */
if (cv->choices->len > 0) {
if (cv->selection < 0) {
cv->selection = 0;
dir = 0;
} else {
cv->selection = -1;
dir = 0;
key_handled = FALSE;
}
}
break;
case GDK_space:
case GDK_KP_Space:
is_space = TRUE;
case GDK_Return:
case GDK_KP_Enter:
if (cv->selection < 0) {
/* We don't have a selection yet, move to the first selection if there is
more than one option. If there is only one option, select it automatically. */
/* Let space pass through. */
if (is_space)
return FALSE;
if (cv->choices->len == 1) {
e_completion_view_select (cv, 0);
goto stop_emission;
} else {
cv->selection = 0;
dir = 0;
}
} else {
/* Our cursor is down in the pop-up, so we make our selection. */
e_completion_view_select (cv, cv->selection);
goto stop_emission;
}
break;
case GDK_Escape:
/* Unbrowse hack */
cv->selection = -1;
dir = 0;
break;
default:
return FALSE;
}
cv->selection += dir;
if (cv->selection >= (int)cv->choices->len) {
cv->selection = cv->choices->len - 1;
/* Don't re-emit the browse signal */
goto stop_emission;
}
e_completion_view_set_cursor_row (cv, cv->selection);
if (cv->selection >= 0)
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_BROWSE], 0,
g_ptr_array_index (cv->choices, cv->selection));
else
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_UNBROWSE], 0);
stop_emission:
if (key_handled)
g_signal_stop_emission_by_name (w, "key_press_event");
return key_handled;
}
static void
begin_completion_cb (ECompletion *completion, const gchar *txt, gint pos, gint limit, gpointer user_data)
{
ECompletionView *cv = E_COMPLETION_VIEW (user_data);
e_table_model_pre_change (cv->model);
e_completion_view_clear_choices (cv);
cv->have_all_choices = FALSE;
e_table_model_changed (cv->model);
}
static void
completion_cb (ECompletion *completion, ECompletionMatch *match, gpointer user_data)
{
ECompletionView *cv = E_COMPLETION_VIEW (user_data);
gint r = cv->choices->len;
gboolean first = (cv->choices->len == 0);
e_table_model_pre_change (cv->model);
e_completion_match_ref (match);
g_ptr_array_add (cv->choices, match);
e_table_model_row_inserted (cv->model, r);
if (first)
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_NONEMPTY], 0);
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_ADDED], 0);
}
static void
end_completion_cb (ECompletion *completion, gpointer user_data)
{
ECompletionView *cv = E_COMPLETION_VIEW (user_data);
/* Do a final refresh of the table. */
e_table_model_pre_change (cv->model);
e_table_model_changed (cv->model);
cv->have_all_choices = TRUE;
g_signal_emit (cv, e_completion_view_signals[E_COMPLETION_VIEW_FULL], 0);
}
/*** Table Callbacks ***/
/* XXX toshok - we need to add sorting to this etable, through the use
of undisplayed fields of all the sort keys we want to use */
static char *simple_spec =
"<ETableSpecification no-headers=\"true\" draw-grid=\"false\" cursor-mode=\"line\" alternating-row-colors=\"false\" gettext-domain=\"" E_I18N_DOMAIN "\">"
" <ETableColumn model_col=\"0\" _title=\"Node\" expansion=\"1.0\" "
" minimum_width=\"16\" resizable=\"true\" cell=\"string\" "
" compare=\"string\"/> "
" <ETableState> "
" <column source=\"0\"/> "
" <grouping></grouping> "
" </ETableState> "
"</ETableSpecification>";
static gint
table_col_count (ETableModel *etm, gpointer data)
{
return 1;
}
static gint
table_row_count (ETableModel *etm, gpointer data)
{
ECompletionView *cv = E_COMPLETION_VIEW (data);
return cv->choices->len;
}
static gboolean
table_is_cell_editable (ETableModel *etm, gint c, gint r, gpointer data)
{
return FALSE;
}
static gpointer
table_value_at (ETableModel *etm, gint c, gint r, gpointer data)
{
ECompletionView *cv = E_COMPLETION_VIEW (data);
ECompletionMatch *match;
match = g_ptr_array_index (cv->choices, r);
return (gpointer) e_completion_match_get_menu_text (match);
}
static gchar *
table_value_to_string (ETableModel *em, gint col, gconstpointer val, gpointer data)
{
return (gchar *) val;
}
static void
table_click_cb (ETable *et, gint r, gint c, GdkEvent *ev, gpointer data)
{
ECompletionView *cv = E_COMPLETION_VIEW (data);
e_completion_view_select (cv, r);
}
void
e_completion_view_construct (ECompletionView *cv, ECompletion *completion)
{
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
g_return_if_fail (completion != NULL);
g_return_if_fail (E_IS_COMPLETION (completion));
/* Make sure we don't call construct twice. */
g_return_if_fail (cv->completion == NULL);
GTK_WIDGET_SET_FLAGS (GTK_WIDGET (cv), GTK_CAN_FOCUS);
cv->completion = completion;
g_object_ref (completion);
cv->begin_signal_id = g_signal_connect (completion,
"completion_started",
G_CALLBACK (begin_completion_cb),
cv);
cv->comp_signal_id = g_signal_connect (completion,
"completion_found",
G_CALLBACK (completion_cb),
cv);
cv->end_signal_id = g_signal_connect (completion,
"completion_finished",
G_CALLBACK (end_completion_cb),
cv);
cv->model = e_table_simple_new (table_col_count,
table_row_count,
NULL,
table_value_at,
NULL,
table_is_cell_editable,
NULL, NULL,
NULL, NULL, NULL, NULL,
table_value_to_string,
cv);
cv->table = e_table_scrolled_new (cv->model, NULL, simple_spec, NULL);
g_object_unref (cv->model);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (cv->table), GTK_SHADOW_NONE);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (cv->table), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (cv), cv->table);
gtk_widget_show_all (cv->table);
g_signal_connect (e_completion_view_table (cv),
"click",
G_CALLBACK (table_click_cb),
cv);
cv->selection = -1;
}
GtkWidget *
e_completion_view_new (ECompletion *completion)
{
gpointer p;
g_return_val_if_fail (completion != NULL, NULL);
g_return_val_if_fail (E_IS_COMPLETION (completion), NULL);
p = g_object_new (E_COMPLETION_VIEW_TYPE, NULL);
e_completion_view_construct (E_COMPLETION_VIEW (p), completion);
return GTK_WIDGET (p);
}
void
e_completion_view_connect_keys (ECompletionView *cv, GtkWidget *w)
{
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
g_return_if_fail (w == NULL || GTK_IS_WIDGET (w));
if (cv->key_widget) {
g_signal_handler_disconnect (cv->key_widget, cv->key_signal_id);
g_object_unref (cv->key_widget);
}
if (w) {
cv->key_widget = w;
g_object_ref (w);
cv->key_signal_id = g_signal_connect (w,
"key_press_event",
G_CALLBACK (e_completion_view_key_press_handler),
cv);
} else {
cv->key_widget = NULL;
cv->key_signal_id = 0;
}
}
void
e_completion_view_set_complete_key (ECompletionView *cv, gint keyval)
{
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
cv->complete_key = keyval;
}
void
e_completion_view_set_uncomplete_key (ECompletionView *cv, gint keyval)
{
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
cv->uncomplete_key = keyval;
}
void
e_completion_view_set_width (ECompletionView *cv, gint width)
{
GtkWidget *w;
gint y, r, dummy, line_height, final_height;
double drop_room, lines;
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
g_return_if_fail (width > 0);
w = GTK_WIDGET (cv);
if (! GTK_WIDGET_REALIZED (w)) {
gtk_widget_set_usize (w, width, -1);
return;
}
/* A Horrible Hack(tm) to figure out the height of a single table row */
for (line_height=5, r=0; r == 0 && line_height < 1000; line_height += 2) {
dummy = 0;
e_table_group_compute_location (e_completion_view_table (cv)->group,
&dummy, &line_height, &r, &dummy);
}
if (line_height >= 1000) {
/* Something went wrong, so we make a (possibly very lame) guess */
line_height = 30;
}
gdk_window_get_origin (w->window, NULL, &y);
y += w->allocation.y;
lines = 5; /* default maximum */
lines = MIN (lines, cv->choices->len);
drop_room = (gdk_screen_height () - y) / (double)line_height;
drop_room = MAX (drop_room, 1);
lines = MIN (lines, drop_room);
/* We reduce the total height by a bit; in practice, this seems to work out well. */
final_height = (gint) floor (line_height * (0.5 + (float)lines) * 0.97);
gtk_widget_set_usize (w, width, final_height);
}
void
e_completion_view_set_editable (ECompletionView *cv, gboolean x)
{
g_return_if_fail (cv != NULL);
g_return_if_fail (E_IS_COMPLETION_VIEW (cv));
if (x == cv->editable)
return;
cv->editable = x;
cv->selection = -1;
e_completion_view_set_cursor_row (cv, -1);
}
|