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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gtkhtml-editor-private.c
*
* Copyright (C) 2008 Novell, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* 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 Lesser 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 "gtkhtml-editor-private.h"
#include <stdlib.h>
/* Regular expressions */
#define CTL "\\x00-\\x1f\\x7f"
#define SEP "\\x09\\x20\\(\\)<>@,;:\\\\\"/\\[\\]\\?=\\{\\}"
#define TOKEN "[^" CTL SEP "]+"
/* Fatal error message given when a data file is not found. */
#define DATA_FILE_NOT_FOUND_MESSAGE \
"\n*** FATAL ERROR ***\n\n" \
PACKAGE " could not find the required file \"%s\" in any of\n" \
"the system-wide data directories given by the XDG_DATA_DIRS environment\n" \
"variable. " PACKAGE " looked for:\n\n"
#define MORE_INFORMATION_MESSAGE \
"\nSee http://www.freedesktop.org/Standards/basedir-spec for more\n" \
"information about standard base directories.\n\n"
/* This controls how spelling suggestions are divided between the primary
* context menu and a secondary menu. The idea is to prevent the primary
* menu from growing too long.
*
* The constants below are used as follows:
*
* if TOTAL_SUGGESTIONS <= MAX_LEVEL1_SUGGETIONS:
* LEVEL1_SUGGESTIONS = TOTAL_SUGGESTIONS
* elif TOTAL_SUGGESTIONS - MAX_LEVEL1_SUGGESTIONS < MIN_LEVEL2_SUGGESTIONS:
* LEVEL1_SUGGESTIONS = TOTAL_SUGGESTIONS
* else
* LEVEL1_SUGGESTIONS = MAX_LEVEL1_SUGGETIONS
*
* LEVEL2_SUGGESTIONS = TOTAL_SUGGESTIONS - LEVEL1_SUGGESTIONS
*
* Note that MAX_LEVEL1_SUGGETIONS is not a hard maximum.
*/
#define MAX_LEVEL1_SUGGESTIONS 4
#define MIN_LEVEL2_SUGGESTIONS 3
/************************ Begin Spell Dialog Callbacks ***********************/
static void
editor_set_word (GtkhtmlEditor *editor,
GtkhtmlSpellDialog *dialog)
{
GtkHTML *html;
gchar *word;
html = gtkhtml_editor_get_html (editor);
html_engine_select_spell_word_editable (html->engine);
word = html_engine_get_spell_word (html->engine);
gtkhtml_spell_dialog_set_word (dialog, word);
g_free (word);
}
static void
editor_next_word_cb (GtkhtmlEditor *editor,
GtkhtmlSpellDialog *dialog)
{
if (gtkhtml_editor_next_spell_error (editor))
editor_set_word (editor, dialog);
else
gtkhtml_spell_dialog_close (dialog);
}
static void
editor_prev_word_cb (GtkhtmlEditor *editor,
GtkhtmlSpellDialog *dialog)
{
if (gtkhtml_editor_prev_spell_error (editor))
editor_set_word (editor, dialog);
else
gtkhtml_spell_dialog_close (dialog);
}
static void
editor_recheck_cb (GtkhtmlEditor *editor,
GtkhtmlSpellDialog *dialog)
{
GtkHTML *html;
html = gtkhtml_editor_get_html (editor);
html_engine_spell_check (html->engine);
}
static void
editor_replace_cb (GtkhtmlEditor *editor,
const gchar *correction,
GtkhtmlSpellDialog *dialog)
{
GtkHTML *html;
html = gtkhtml_editor_get_html (editor);
html_engine_replace_spell_word_with (html->engine, correction);
gtkhtml_spell_dialog_next_word (dialog);
}
static void
editor_replace_all_cb (GtkhtmlEditor *editor,
const gchar *correction,
GtkhtmlSpellDialog *dialog)
{
GtkHTML *html;
gchar *misspelled;
html = gtkhtml_editor_get_html (editor);
/* Replace this occurrence. */
misspelled = html_engine_get_spell_word (html->engine);
html_engine_replace_spell_word_with (html->engine, correction);
/* Replace all subsequent occurrences. */
while (gtkhtml_editor_next_spell_error (editor)) {
gchar *word;
word = html_engine_get_spell_word (html->engine);
if (g_str_equal (word, misspelled))
html_engine_replace_spell_word_with (
html->engine, correction);
g_free (word);
}
/* Jump to beginning of document (XXX is this right?) */
html_engine_beginning_of_document (html->engine);
gtkhtml_spell_dialog_next_word (dialog);
g_free (misspelled);
}
/************************* End Spell Dialog Callbacks ************************/
void
gtkhtml_editor_private_init (GtkhtmlEditor *editor)
{
GtkhtmlEditorPrivate *priv = editor->priv;
gchar *filename;
GError *error = NULL;
priv->manager = gtk_ui_manager_new ();
priv->core_actions = gtk_action_group_new ("core");
priv->html_actions = gtk_action_group_new ("html");
priv->context_actions = gtk_action_group_new ("core-context");
priv->html_context_actions = gtk_action_group_new ("html-context");
priv->language_actions = gtk_action_group_new ("language");
priv->spell_check_actions = gtk_action_group_new ("spell-check");
priv->suggestion_actions = gtk_action_group_new ("suggestion");
/* GtkhtmlSpellLanguage -> GtkhtmlSpellChecker */
priv->available_spell_checkers = g_hash_table_new_full (
g_direct_hash, g_direct_equal,
(GDestroyNotify) NULL,
(GDestroyNotify) g_object_unref);
/* GtkhtmlSpellLanguage -> UI Merge ID */
priv->spell_suggestion_menus =
g_hash_table_new (g_direct_hash, g_direct_equal);
filename = gtkhtml_editor_find_data_file ("gtkhtml-editor-manager.ui");
if (!gtk_ui_manager_add_ui_from_file (priv->manager, filename, &error)) {
g_critical ("Couldn't load builder file: %s\n", error->message);
g_clear_error (&error);
}
g_free (filename);
filename = gtkhtml_editor_find_data_file ("gtkhtml-editor-builder.ui");
priv->builder = gtk_builder_new ();
/* To keep translated strings in subclasses */
gtk_builder_set_translation_domain (priv->builder, GETTEXT_PACKAGE);
if (!gtk_builder_add_from_file (priv->builder, filename, &error)) {
g_critical ("Couldn't load builder file: %s\n", error->message);
g_clear_error (&error);
}
g_free (filename);
gtkhtml_editor_actions_init (editor);
gtk_window_add_accel_group (
GTK_WINDOW (editor),
gtk_ui_manager_get_accel_group (priv->manager));
gtk_builder_connect_signals (priv->builder, NULL);
/* Wait to construct the main window widgets
* until the 'html' property is initialized. */
}
void
gtkhtml_editor_private_constructed (GtkhtmlEditor *editor)
{
GtkhtmlEditorPrivate *priv = editor->priv;
GtkHTML *html;
GtkWidget *widget;
GtkToolbar *toolbar;
GtkToolItem *tool_item;
/* Construct main window widgets. */
widget = gtkhtml_editor_get_managed_widget (editor, "/main-menu");
gtk_box_pack_start (GTK_BOX (editor->vbox), widget, FALSE, FALSE, 0);
priv->main_menu = g_object_ref (widget);
gtk_widget_show (widget);
widget = gtkhtml_editor_get_managed_widget (editor, "/main-toolbar");
gtk_box_pack_start (GTK_BOX (editor->vbox), widget, FALSE, FALSE, 0);
priv->main_toolbar = g_object_ref (widget);
gtk_widget_show (widget);
gtk_style_context_add_class (
gtk_widget_get_style_context (widget),
GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
widget = gtkhtml_editor_get_managed_widget (editor, "/edit-toolbar");
gtk_toolbar_set_style (GTK_TOOLBAR (widget), GTK_TOOLBAR_BOTH_HORIZ);
gtk_box_pack_start (GTK_BOX (editor->vbox), widget, FALSE, FALSE, 0);
priv->edit_toolbar = g_object_ref (widget);
gtk_widget_show (widget);
widget = gtkhtml_editor_get_managed_widget (editor, "/html-toolbar");
gtk_toolbar_set_style (GTK_TOOLBAR (widget), GTK_TOOLBAR_BOTH_HORIZ);
gtk_box_pack_start (GTK_BOX (editor->vbox), widget, FALSE, FALSE, 0);
priv->html_toolbar = g_object_ref (widget);
gtk_widget_show (widget);
widget = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (
GTK_SCROLLED_WINDOW (widget),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (
GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (editor->vbox), widget, TRUE, TRUE, 0);
priv->scrolled_window = g_object_ref (widget);
gtk_widget_show (widget);
widget = GTK_WIDGET (gtkhtml_editor_get_html (editor));
gtk_container_add (GTK_CONTAINER (priv->scrolled_window), widget);
gtk_widget_show (widget);
/* Add some combo boxes to the "edit" toolbar. */
toolbar = GTK_TOOLBAR (priv->edit_toolbar);
tool_item = gtk_tool_item_new ();
widget = gtkhtml_combo_box_new_with_action (
GTK_RADIO_ACTION (ACTION (STYLE_NORMAL)));
gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (widget), FALSE);
gtk_container_add (GTK_CONTAINER (tool_item), widget);
gtk_widget_set_tooltip_text (widget, _("Paragraph Style"));
gtk_toolbar_insert (toolbar, tool_item, 0);
priv->style_combo_box = g_object_ref (widget);
gtk_widget_show_all (GTK_WIDGET (tool_item));
tool_item = gtk_separator_tool_item_new ();
gtk_toolbar_insert (toolbar, tool_item, 0);
gtk_widget_show_all (GTK_WIDGET (tool_item));
tool_item = gtk_tool_item_new ();
widget = gtkhtml_combo_box_new_with_action (
GTK_RADIO_ACTION (ACTION (MODE_HTML)));
gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (widget), FALSE);
gtk_container_add (GTK_CONTAINER (tool_item), widget);
gtk_widget_set_tooltip_text (widget, _("Editing Mode"));
gtk_toolbar_insert (toolbar, tool_item, 0);
priv->mode_combo_box = g_object_ref (widget);
gtk_widget_show_all (GTK_WIDGET (tool_item));
/* Add some combo boxes to the "html" toolbar. */
toolbar = GTK_TOOLBAR (priv->html_toolbar);
tool_item = gtk_tool_item_new ();
widget = gtkhtml_color_combo_new ();
gtk_container_add (GTK_CONTAINER (tool_item), widget);
gtk_widget_set_tooltip_text (widget, _("Font Color"));
gtk_toolbar_insert (toolbar, tool_item, 0);
priv->color_combo_box = g_object_ref (widget);
gtk_widget_show_all (GTK_WIDGET (tool_item));
tool_item = gtk_tool_item_new ();
widget = gtkhtml_combo_box_new_with_action (
GTK_RADIO_ACTION (ACTION (SIZE_PLUS_ZERO)));
gtk_combo_box_set_focus_on_click (GTK_COMBO_BOX (widget), FALSE);
gtk_container_add (GTK_CONTAINER (tool_item), widget);
gtk_widget_set_tooltip_text (widget, _("Font Size"));
gtk_toolbar_insert (toolbar, tool_item, 0);
priv->size_combo_box = g_object_ref (widget);
gtk_widget_show_all (GTK_WIDGET (tool_item));
/* Initialize painters (requires "edit_area"). */
html = gtkhtml_editor_get_html (editor);
priv->html_painter = g_object_ref (html->engine->painter);
priv->plain_painter = html_plain_painter_new (priv->edit_area, TRUE);
/* Add input methods to the context menu. */
widget = gtkhtml_editor_get_managed_widget (
editor, "/context-menu/context-input-methods-menu");
widget = gtk_menu_item_get_submenu (GTK_MENU_ITEM (widget));
gtk_im_multicontext_append_menuitems (
GTK_IM_MULTICONTEXT (html->priv->im_context),
GTK_MENU_SHELL (widget));
/* Configure color stuff. */
priv->palette = gtkhtml_color_palette_new ();
priv->text_color = gtkhtml_color_state_new ();
gtkhtml_color_state_set_default_label (
priv->text_color, _("Automatic"));
gtkhtml_color_state_set_palette (
priv->text_color, priv->palette);
/* Text color widgets share state. */
widget = priv->color_combo_box;
gtkhtml_color_combo_set_state (
GTKHTML_COLOR_COMBO (widget), priv->text_color);
widget = WIDGET (TEXT_PROPERTIES_COLOR_COMBO);
gtkhtml_color_combo_set_state (
GTKHTML_COLOR_COMBO (widget), priv->text_color);
/* These color widgets share a custom color palette. */
widget = WIDGET (CELL_PROPERTIES_COLOR_COMBO);
gtkhtml_color_combo_set_palette (
GTKHTML_COLOR_COMBO (widget), priv->palette);
widget = WIDGET (PAGE_PROPERTIES_BACKGROUND_COLOR_COMBO);
gtkhtml_color_combo_set_palette (
GTKHTML_COLOR_COMBO (widget), priv->palette);
widget = WIDGET (PAGE_PROPERTIES_LINK_COLOR_COMBO);
gtkhtml_color_combo_set_palette (
GTKHTML_COLOR_COMBO (widget), priv->palette);
widget = WIDGET (TABLE_PROPERTIES_COLOR_COMBO);
gtkhtml_color_combo_set_palette (
GTKHTML_COLOR_COMBO (widget), priv->palette);
}
void
gtkhtml_editor_private_dispose (GtkhtmlEditor *editor)
{
GtkhtmlEditorPrivate *priv = editor->priv;
/* Disconnect signal handlers from the color
* state object since it may live on. */
if (priv->text_color != NULL) {
g_signal_handlers_disconnect_matched (
priv->text_color, G_SIGNAL_MATCH_DATA,
0, 0, NULL, NULL, editor);
}
DISPOSE (priv->manager);
DISPOSE (priv->core_actions);
DISPOSE (priv->html_actions);
DISPOSE (priv->context_actions);
DISPOSE (priv->html_context_actions);
DISPOSE (priv->language_actions);
DISPOSE (priv->spell_check_actions);
DISPOSE (priv->suggestion_actions);
DISPOSE (priv->builder);
DISPOSE (priv->html_painter);
DISPOSE (priv->plain_painter);
g_hash_table_remove_all (priv->available_spell_checkers);
g_list_foreach (
priv->active_spell_checkers,
(GFunc) g_object_unref, NULL);
g_list_free (priv->active_spell_checkers);
priv->active_spell_checkers = NULL;
DISPOSE (priv->main_menu);
DISPOSE (priv->main_toolbar);
DISPOSE (priv->edit_toolbar);
DISPOSE (priv->html_toolbar);
DISPOSE (priv->edit_area);
DISPOSE (priv->color_combo_box);
DISPOSE (priv->mode_combo_box);
DISPOSE (priv->size_combo_box);
DISPOSE (priv->style_combo_box);
DISPOSE (priv->scrolled_window);
DISPOSE (priv->palette);
DISPOSE (priv->text_color);
}
void
gtkhtml_editor_private_finalize (GtkhtmlEditor *editor)
{
GtkhtmlEditorPrivate *priv = editor->priv;
/* All URI requests should be complete or cancelled by now. */
if (priv->requests != NULL)
g_warning ("Finalizing GtkhtmlEditor with active URI requests");
g_hash_table_destroy (priv->available_spell_checkers);
g_hash_table_destroy (priv->spell_suggestion_menus);
g_free (priv->filename);
}
gchar *
gtkhtml_editor_find_data_file (const gchar *basename)
{
const gchar * const *datadirs;
gchar *filename;
g_return_val_if_fail (basename != NULL, NULL);
/* Support running directly from the source tree. */
filename = g_build_filename (".", basename, NULL);
if (g_file_test (filename, G_FILE_TEST_EXISTS))
return filename;
g_free (filename);
/* Check our own installation prefix. */
filename = g_build_filename (
DATADIR, GTKHTML_RELEASE_STRING, basename, NULL);
if (g_file_test (filename, G_FILE_TEST_EXISTS))
return filename;
g_free (filename);
/* Check the standard system data directories. */
datadirs = g_get_system_data_dirs ();
while (*datadirs != NULL) {
filename = g_build_filename (
*datadirs++, GTKHTML_RELEASE_STRING, basename, NULL);
if (g_file_test (filename, G_FILE_TEST_EXISTS))
return filename;
g_free (filename);
}
/* Print a helpful message and die. */
g_printerr (DATA_FILE_NOT_FOUND_MESSAGE, basename);
filename = g_build_filename (
DATADIR, GTKHTML_RELEASE_STRING, basename, NULL);
g_printerr ("\t%s\n", filename);
g_free (filename);
datadirs = g_get_system_data_dirs ();
while (*datadirs != NULL) {
filename = g_build_filename (
*datadirs++, GTKHTML_RELEASE_STRING, basename, NULL);
g_printerr ("\t%s\n", filename);
g_free (filename);
}
g_printerr (MORE_INFORMATION_MESSAGE);
abort ();
return NULL; /* never gets here */
}
GFile *
gtkhtml_editor_run_open_dialog (GtkhtmlEditor *editor,
const gchar *title,
GtkCallback customize_func,
gpointer customize_data)
{
GtkFileChooser *file_chooser;
GFile *chosen_file = NULL;
GtkWidget *dialog;
g_return_val_if_fail (GTKHTML_IS_EDITOR (editor), NULL);
dialog = gtk_file_chooser_dialog_new (
title, GTK_WINDOW (editor),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
file_chooser = GTK_FILE_CHOOSER (dialog);
gtk_dialog_set_default_response (
GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
gtk_file_chooser_set_local_only (file_chooser, FALSE);
/* Allow further customizations before running the dialog. */
if (customize_func != NULL)
customize_func (dialog, customize_data);
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
chosen_file = gtk_file_chooser_get_file (file_chooser);
gtk_widget_destroy (dialog);
return chosen_file;
}
void
gtkhtml_editor_show_uri (GtkWindow *parent,
const gchar *uri)
{
GtkWidget *dialog;
GdkScreen *screen = NULL;
GError *error = NULL;
guint32 timestamp;
g_return_if_fail (uri != NULL);
timestamp = gtk_get_current_event_time ();
if (parent != NULL)
screen = gtk_widget_get_screen (GTK_WIDGET (parent));
if (gtk_show_uri (screen, uri, timestamp, &error))
return;
dialog = gtk_message_dialog_new_with_markup (
parent, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
"<big><b>%s</b></big>",
_("Could not open the link."));
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
g_error_free (error);
}
void
gtkhtml_editor_spell_check (GtkhtmlEditor *editor,
gboolean whole_document)
{
GtkHTML *html;
gboolean spelling_errors;
g_return_if_fail (GTKHTML_IS_EDITOR (editor));
html = gtkhtml_editor_get_html (editor);
if (whole_document) {
html_engine_disable_selection (html->engine);
html_engine_beginning_of_document (html->engine);
gtk_html_set_inline_spelling (html, TRUE);
}
spelling_errors =
!html_engine_spell_word_is_valid (html->engine) ||
gtkhtml_editor_next_spell_error (editor);
if (spelling_errors) {
GtkWidget *dialog;
dialog = gtkhtml_spell_dialog_new (GTK_WINDOW (editor));
gtkhtml_spell_dialog_set_spell_checkers (
GTKHTML_SPELL_DIALOG (dialog),
editor->priv->active_spell_checkers);
editor_set_word (editor, GTKHTML_SPELL_DIALOG (dialog));
g_signal_connect_swapped (
dialog, "added",
G_CALLBACK (editor_recheck_cb), editor);
g_signal_connect_swapped (
dialog, "ignored",
G_CALLBACK (editor_recheck_cb), editor);
g_signal_connect_swapped (
dialog, "next-word",
G_CALLBACK (editor_next_word_cb), editor);
g_signal_connect_swapped (
dialog, "prev-word",
G_CALLBACK (editor_prev_word_cb), editor);
g_signal_connect_swapped (
dialog, "replace",
G_CALLBACK (editor_replace_cb), editor);
g_signal_connect_swapped (
dialog, "replace-all",
G_CALLBACK (editor_replace_all_cb), editor);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
/* XXX Restore original inline spelling value. */
}
gboolean
gtkhtml_editor_next_spell_error (GtkhtmlEditor *editor)
{
GtkHTML *html;
gboolean found = FALSE;
g_return_val_if_fail (GTKHTML_IS_EDITOR (editor), FALSE);
html = gtkhtml_editor_get_html (editor);
html_engine_disable_selection (html->engine);
while (!found && html_engine_forward_word (html->engine))
found = !html_engine_spell_word_is_valid (html->engine);
return found;
}
gboolean
gtkhtml_editor_prev_spell_error (GtkhtmlEditor *editor)
{
GtkHTML *html;
gboolean found = FALSE;
g_return_val_if_fail (GTKHTML_IS_EDITOR (editor), FALSE);
html = gtkhtml_editor_get_html (editor);
html_engine_disable_selection (html->engine);
html_engine_backward_word (html->engine);
while (!found && html_engine_backward_word (html->engine))
found = !html_engine_spell_word_is_valid (html->engine);
return found;
}
/* Action callback for context menu spelling suggestions.
* XXX This should really be in gtkhtml-editor-actions.c */
static void
action_context_spell_suggest_cb (GtkAction *action,
GtkhtmlEditor *editor)
{
GtkHTML *html;
const gchar *word;
html = gtkhtml_editor_get_html (editor);
word = g_object_get_data (G_OBJECT (action), "word");
g_return_if_fail (word != NULL);
html_engine_replace_spell_word_with (html->engine, word);
}
static void
editor_inline_spelling_suggestions (GtkhtmlEditor *editor,
GtkhtmlSpellChecker *checker)
{
GtkActionGroup *action_group;
GtkUIManager *manager;
GtkHTML *html;
GList *list;
const gchar *path;
gchar *word;
guint count = 0;
guint length;
guint merge_id;
guint threshold;
html = gtkhtml_editor_get_html (editor);
word = html_engine_get_spell_word (html->engine);
list = gtkhtml_spell_checker_get_suggestions (checker, word, -1);
path = "/context-menu/context-spell-suggest/";
manager = gtkhtml_editor_get_ui_manager (editor);
action_group = editor->priv->suggestion_actions;
merge_id = editor->priv->spell_suggestions_merge_id;
/* Calculate how many suggestions to put directly in the
* context menu. The rest will go in a secondary menu. */
length = g_list_length (list);
if (length <= MAX_LEVEL1_SUGGESTIONS)
threshold = length;
else if (length - MAX_LEVEL1_SUGGESTIONS < MIN_LEVEL2_SUGGESTIONS)
threshold = length;
else
threshold = MAX_LEVEL1_SUGGESTIONS;
while (list != NULL) {
gchar *suggestion = list->data;
gchar *action_name;
gchar *action_label;
GtkAction *action;
GtkWidget *child;
GSList *proxies;
/* Once we reach the threshold, put all subsequent
* spelling suggestions in a secondary menu. */
if (count == threshold)
path = "/context-menu/context-more-suggestions-menu/";
/* Action name just needs to be unique. */
action_name = g_strdup_printf ("suggest-%d", count++);
action_label = g_markup_printf_escaped (
"<b>%s</b>", suggestion);
action = gtk_action_new (
action_name, action_label, NULL, NULL);
g_object_set_data_full (
G_OBJECT (action), "word",
g_strdup (suggestion), g_free);
g_signal_connect (
action, "activate", G_CALLBACK (
action_context_spell_suggest_cb), editor);
gtk_action_group_add_action (action_group, action);
gtk_ui_manager_add_ui (
manager, merge_id, path,
action_name, action_name,
GTK_UI_MANAGER_AUTO, FALSE);
/* XXX GtkAction offers no support for Pango markup,
* so we have to manually set "use-markup" on the
* child of the proxy widget. */
gtk_ui_manager_ensure_update (manager);
proxies = gtk_action_get_proxies (action);
child = gtk_bin_get_child (proxies->data);
g_object_set (child, "use-markup", TRUE, NULL);
g_free (suggestion);
g_free (action_name);
g_free (action_label);
list = g_list_delete_link (list, list);
}
g_free (word);
}
/* Helper for gtkhtml_editor_update_context() */
static void
editor_spell_checkers_foreach (GtkhtmlSpellChecker *checker,
GtkhtmlEditor *editor)
{
const GtkhtmlSpellLanguage *language;
const gchar *language_code;
GtkActionGroup *action_group;
GtkUIManager *manager;
GtkHTML *html;
GList *list;
gchar *path;
gchar *word;
gint count = 0;
guint merge_id;
language = gtkhtml_spell_checker_get_language (checker);
language_code = gtkhtml_spell_language_get_code (language);
html = gtkhtml_editor_get_html (editor);
word = html_engine_get_spell_word (html->engine);
list = gtkhtml_spell_checker_get_suggestions (checker, word, -1);
manager = gtkhtml_editor_get_ui_manager (editor);
action_group = editor->priv->suggestion_actions;
merge_id = editor->priv->spell_suggestions_merge_id;
path = g_strdup_printf (
"/context-menu/context-spell-suggest/"
"context-spell-suggest-%s-menu", language_code);
while (list != NULL) {
gchar *suggestion = list->data;
gchar *action_name;
gchar *action_label;
GtkAction *action;
GtkWidget *child;
GSList *proxies;
/* Action name just needs to be unique. */
action_name = g_strdup_printf (
"suggest-%s-%d", language_code, count++);
action_label = g_markup_printf_escaped (
"<b>%s</b>", suggestion);
action = gtk_action_new (
action_name, action_label, NULL, NULL);
g_object_set_data_full (
G_OBJECT (action), "word",
g_strdup (suggestion), g_free);
g_signal_connect (
action, "activate", G_CALLBACK (
action_context_spell_suggest_cb), editor);
gtk_action_group_add_action (action_group, action);
gtk_ui_manager_add_ui (
manager, merge_id, path,
action_name, action_name,
GTK_UI_MANAGER_AUTO, FALSE);
/* XXX GtkAction offers no supports for Pango markup,
* so we have to manually set "use-markup" on the
* child of the proxy widget. */
gtk_ui_manager_ensure_update (manager);
proxies = gtk_action_get_proxies (action);
child = gtk_bin_get_child (proxies->data);
g_object_set (child, "use-markup", TRUE, NULL);
g_free (suggestion);
g_free (action_name);
g_free (action_label);
list = g_list_delete_link (list, list);
}
g_free (path);
g_free (word);
}
void
gtkhtml_editor_update_context (GtkhtmlEditor *editor)
{
GtkHTML *html;
HTMLType type;
HTMLObject *object;
GtkUIManager *manager;
GtkActionGroup *action_group;
GList *list;
gboolean visible;
guint merge_id;
html = gtkhtml_editor_get_html (editor);
manager = gtkhtml_editor_get_ui_manager (editor);
gtk_html_update_styles (html);
/* Update context menu item visibility. */
object = html->engine->cursor->object;
if (object != NULL)
type = HTML_OBJECT_TYPE (object);
else
type = HTML_TYPE_NONE;
visible = (type == HTML_TYPE_IMAGE);
gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_IMAGE), visible);
visible = (type == HTML_TYPE_LINKTEXT);
gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_LINK), visible);
visible = (type == HTML_TYPE_RULE);
gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_RULE), visible);
visible = (type == HTML_TYPE_TEXT);
gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_TEXT), visible);
visible =
gtk_action_get_visible (ACTION (CONTEXT_PROPERTIES_IMAGE)) ||
gtk_action_get_visible (ACTION (CONTEXT_PROPERTIES_LINK)) ||
gtk_action_get_visible (ACTION (CONTEXT_PROPERTIES_TEXT));
gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_PARAGRAPH), visible);
/* Set to visible if any of these are true:
* - Selection is active and contains a link.
* - Cursor is on a link.
* - Cursor is on an image that has a URL or target.
*/
visible =
(html_engine_is_selection_active (html->engine) &&
html_engine_selection_contains_link (html->engine)) ||
(type == HTML_TYPE_LINKTEXT) ||
(type == HTML_TYPE_IMAGE &&
(HTML_IMAGE (object)->url != NULL ||
HTML_IMAGE (object)->target != NULL));
gtk_action_set_visible (ACTION (CONTEXT_REMOVE_LINK), visible);
/* Get the parent object. */
object = (object != NULL) ? object->parent : NULL;
/* Get the grandparent object. */
object = (object != NULL) ? object->parent : NULL;
if (object != NULL)
type = HTML_OBJECT_TYPE (object);
else
type = HTML_TYPE_NONE;
visible = (type == HTML_TYPE_TABLECELL);
gtk_action_set_visible (ACTION (CONTEXT_DELETE_CELL), visible);
gtk_action_set_visible (ACTION (CONTEXT_DELETE_COLUMN), visible);
gtk_action_set_visible (ACTION (CONTEXT_DELETE_ROW), visible);
gtk_action_set_visible (ACTION (CONTEXT_DELETE_TABLE), visible);
gtk_action_set_visible (ACTION (CONTEXT_INSERT_COLUMN_AFTER), visible);
gtk_action_set_visible (ACTION (CONTEXT_INSERT_COLUMN_BEFORE), visible);
gtk_action_set_visible (ACTION (CONTEXT_INSERT_ROW_ABOVE), visible);
gtk_action_set_visible (ACTION (CONTEXT_INSERT_ROW_BELOW), visible);
gtk_action_set_visible (ACTION (CONTEXT_INSERT_TABLE), visible);
gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_CELL), visible);
/* Get the great grandparent object. */
object = (object != NULL) ? object->parent : NULL;
if (object != NULL)
type = HTML_OBJECT_TYPE (object);
else
type = HTML_TYPE_NONE;
/* Note the |= (cursor must be in a table cell). */
visible |= (type == HTML_TYPE_TABLE);
gtk_action_set_visible (ACTION (CONTEXT_PROPERTIES_TABLE), visible);
/********************** Spell Check Suggestions **********************/
object = html->engine->cursor->object;
action_group = editor->priv->suggestion_actions;
/* Remove the old content from the context menu. */
merge_id = editor->priv->spell_suggestions_merge_id;
if (merge_id > 0) {
gtk_ui_manager_remove_ui (manager, merge_id);
editor->priv->spell_suggestions_merge_id = 0;
}
/* Clear the action group for spelling suggestions. */
list = gtk_action_group_list_actions (action_group);
while (list != NULL) {
GtkAction *action = list->data;
gtk_action_group_remove_action (action_group, action);
list = g_list_delete_link (list, list);
}
/* Decide if we should show spell checking items. */
visible =
!html_engine_is_selection_active (html->engine) &&
object != NULL && html_object_is_text (object) &&
!html_engine_spell_word_is_valid (html->engine);
action_group = editor->priv->spell_check_actions;
gtk_action_group_set_visible (action_group, visible);
/* Exit early if spell checking items are invisible. */
if (!visible)
return;
list = editor->priv->active_spell_checkers;
merge_id = gtk_ui_manager_new_merge_id (manager);
editor->priv->spell_suggestions_merge_id = merge_id;
/* Handle a single active language as a special case. */
if (g_list_length (list) == 1) {
editor_inline_spelling_suggestions (editor, list->data);
return;
}
/* Add actions and context menu content for active languages. */
g_list_foreach (list, (GFunc) editor_spell_checkers_foreach, editor);
}
|