gspell-text-view.c 16.1 KB
Newer Older
1 2 3
/*
 * This file is part of gspell, a spell-checking library.
 *
4
 * Copyright 2015, 2016, 2017 - Sébastien Wilmet
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.1 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, see <http://www.gnu.org/licenses/>.
 */

20
#ifdef HAVE_CONFIG_H
21 22 23
#include <config.h>
#endif

24
#include "gspell-text-view.h"
25
#include <glib/gi18n-lib.h>
26
#include "gspell-inline-checker-text-buffer.h"
27 28 29
#include "gspell-checker.h"
#include "gspell-language.h"
#include "gspell-text-buffer.h"
30
#include "gspell-context-menu.h"
31 32 33 34 35 36 37

/**
 * SECTION:text-view
 * @Title: GspellTextView
 * @Short_description: Spell checking support for GtkTextView
 *
 * #GspellTextView extends the #GtkTextView class with inline spell checking.
38 39 40 41 42
 * Misspelled words are highlighted with a red %PANGO_UNDERLINE_SINGLE.
 * Right-clicking a misspelled word pops up a context menu of suggested
 * replacements. The context menu also contains an “Ignore All” item to add the
 * misspelled word to the session dictionary. And an “Add” item to add the word
 * to the personal dictionary.
43
 *
44 45 46
 * For a basic use-case, there is the gspell_text_view_basic_setup() convenience
 * function.
 *
47 48 49 50 51
 * The spell is checked only on the visible region of the #GtkTextView. Note
 * that if a same #GtkTextBuffer is used for several views, the misspelled words
 * are visible in all views, because the highlighting is achieved with a
 * #GtkTextTag added to the buffer.
 *
52 53
 * If you don't use the gspell_text_view_basic_setup() function, you need to
 * call gspell_text_buffer_set_spell_checker() to associate a #GspellChecker to
54
 * the #GtkTextBuffer.
55 56 57 58
 *
 * Note that #GspellTextView extends the #GtkTextView class but without
 * subclassing it, because the GtkSourceView library has already a #GtkTextView
 * subclass.
59 60 61
 *
 * If you want a %PANGO_UNDERLINE_ERROR instead (a wavy underline), please fix
 * [this bug](https://bugzilla.gnome.org/show_bug.cgi?id=763741) first.
62 63 64 65 66 67 68 69
 */

typedef struct _GspellTextViewPrivate GspellTextViewPrivate;

struct _GspellTextViewPrivate
{
	GtkTextView *view;
	GspellInlineCheckerTextBuffer *inline_checker;
70
	guint enable_language_menu : 1;
71 72 73 74 75 76
};

enum
{
	PROP_0,
	PROP_VIEW,
77
	PROP_INLINE_SPELL_CHECKING,
78
	PROP_ENABLE_LANGUAGE_MENU,
79 80
};

81
#define GSPELL_TEXT_VIEW_KEY "gspell-text-view-key"
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

G_DEFINE_TYPE_WITH_PRIVATE (GspellTextView, gspell_text_view, G_TYPE_OBJECT)

static void
create_inline_checker (GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;
	GtkTextBuffer *buffer;

	priv = gspell_text_view_get_instance_private (gspell_view);

	if (priv->inline_checker != NULL)
	{
		return;
	}

	buffer = gtk_text_view_get_buffer (priv->view);
	priv->inline_checker = _gspell_inline_checker_text_buffer_new (buffer);
	_gspell_inline_checker_text_buffer_attach_view (priv->inline_checker,
							priv->view);
}

static void
destroy_inline_checker (GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;

	priv = gspell_text_view_get_instance_private (gspell_view);

	if (priv->view == NULL || priv->inline_checker == NULL)
	{
		return;
	}

	_gspell_inline_checker_text_buffer_detach_view (priv->inline_checker,
							priv->view);
	g_clear_object (&priv->inline_checker);
}

static void
notify_buffer_cb (GtkTextView    *gtk_view,
		  GParamSpec     *pspec,
		  GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;

	priv = gspell_text_view_get_instance_private (gspell_view);

	if (priv->inline_checker == NULL)
	{
		return;
	}

	destroy_inline_checker (gspell_view);
	create_inline_checker (gspell_view);
}

139
static void
140 141
language_activated_cb (const GspellLanguage *lang,
		       gpointer              user_data)
142
{
143
	GspellTextView *gspell_view;
144 145 146 147 148
	GspellTextViewPrivate *priv;
	GtkTextBuffer *gtk_buffer;
	GspellTextBuffer *gspell_buffer;
	GspellChecker *checker;

149
	g_return_if_fail (GSPELL_IS_TEXT_VIEW (user_data));
150

151 152
	gspell_view = GSPELL_TEXT_VIEW (user_data);
	priv = gspell_text_view_get_instance_private (gspell_view);
153 154 155 156 157 158 159 160

	gtk_buffer = gtk_text_view_get_buffer (priv->view);
	gspell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (gtk_buffer);
	checker = gspell_text_buffer_get_spell_checker (gspell_buffer);

	gspell_checker_set_language (checker, lang);
}

161
static const GspellLanguage *
162
get_current_language (GspellTextView *gspell_view)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
	GspellTextViewPrivate *priv;
	GtkTextBuffer *gtk_buffer;
	GspellTextBuffer *gspell_buffer;
	GspellChecker *checker;

	priv = gspell_text_view_get_instance_private (gspell_view);

	if (priv->view == NULL)
	{
		return NULL;
	}

	gtk_buffer = gtk_text_view_get_buffer (priv->view);
	gspell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (gtk_buffer);
	checker = gspell_text_buffer_get_spell_checker (gspell_buffer);

	return gspell_checker_get_language (checker);
}

183 184
static void
populate_popup_cb (GtkTextView    *gtk_view,
185
		   GtkWidget      *popup,
186 187 188
		   GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;
189
	GtkMenu *menu;
190 191 192 193
	GtkWidget *menu_item;

	priv = gspell_text_view_get_instance_private (gspell_view);

194 195 196 197 198 199 200
	if (!GTK_IS_MENU (popup))
	{
		return;
	}

	menu = GTK_MENU (popup);

201 202 203 204 205 206 207 208 209 210 211 212 213
	if (!priv->enable_language_menu &&
	    priv->inline_checker == NULL)
	{
		return;
	}

	/* Prepend separator */
	menu_item = gtk_separator_menu_item_new ();
	gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), menu_item);
	gtk_widget_show (menu_item);

	if (priv->enable_language_menu)
	{
214 215 216 217 218 219 220 221
		const GspellLanguage *current_language;
		GtkMenuItem *lang_menu_item;

		current_language = get_current_language (gspell_view);
		lang_menu_item = _gspell_context_menu_get_language_menu_item (current_language,
									      language_activated_cb,
									      gspell_view);

222
		/* Prepend language sub-menu */
223 224
		gtk_menu_shell_prepend (GTK_MENU_SHELL (menu),
					GTK_WIDGET (lang_menu_item));
225 226 227 228 229 230 231 232 233
	}

	if (priv->inline_checker != NULL)
	{
		/* Prepend suggestions */
		_gspell_inline_checker_text_buffer_populate_popup (priv->inline_checker, menu);
	}
}

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static void
set_view (GspellTextView *gspell_view,
	  GtkTextView    *gtk_view)
{
	GspellTextViewPrivate *priv;

	g_return_if_fail (GTK_IS_TEXT_VIEW (gtk_view));

	priv = gspell_text_view_get_instance_private (gspell_view);

	g_assert (priv->view == NULL);
	g_assert (priv->inline_checker == NULL);

	priv->view = gtk_view;

	g_signal_connect_object (priv->view,
				 "notify::buffer",
				 G_CALLBACK (notify_buffer_cb),
				 gspell_view,
				 0);
254

255 256 257
	/* G_CONNECT_AFTER, so when menu items are prepended, they have more
	 * chances to be the first in the menu.
	 */
258 259 260 261
	g_signal_connect_object (priv->view,
				 "populate-popup",
				 G_CALLBACK (populate_popup_cb),
				 gspell_view,
262
				 G_CONNECT_AFTER);
263

264
	g_object_notify (G_OBJECT (gspell_view), "view");
265 266 267 268 269 270 271 272
}

static void
gspell_text_view_get_property (GObject    *object,
			       guint       prop_id,
			       GValue     *value,
			       GParamSpec *pspec)
{
273
	GspellTextView *gspell_view = GSPELL_TEXT_VIEW (object);
274 275 276 277

	switch (prop_id)
	{
		case PROP_VIEW:
278
			g_value_set_object (value, gspell_text_view_get_view (gspell_view));
279 280
			break;

281 282
		case PROP_INLINE_SPELL_CHECKING:
			g_value_set_boolean (value, gspell_text_view_get_inline_spell_checking (gspell_view));
283 284
			break;

285 286 287 288
		case PROP_ENABLE_LANGUAGE_MENU:
			g_value_set_boolean (value, gspell_text_view_get_enable_language_menu (gspell_view));
			break;

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
gspell_text_view_set_property (GObject      *object,
			       guint         prop_id,
			       const GValue *value,
			       GParamSpec   *pspec)
{
	GspellTextView *gspell_view = GSPELL_TEXT_VIEW (object);

	switch (prop_id)
	{
		case PROP_VIEW:
			set_view (gspell_view, g_value_get_object (value));
			break;

309 310
		case PROP_INLINE_SPELL_CHECKING:
			gspell_text_view_set_inline_spell_checking (gspell_view, g_value_get_boolean (value));
311 312
			break;

313 314 315 316
		case PROP_ENABLE_LANGUAGE_MENU:
			gspell_text_view_set_enable_language_menu (gspell_view, g_value_get_boolean (value));
			break;

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
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
gspell_text_view_dispose (GObject *object)
{
	GspellTextViewPrivate *priv;

	priv = gspell_text_view_get_instance_private (GSPELL_TEXT_VIEW (object));

	if (priv->view != NULL && priv->inline_checker != NULL)
	{
		_gspell_inline_checker_text_buffer_detach_view (priv->inline_checker,
								priv->view);
	}

	priv->view = NULL;
	g_clear_object (&priv->inline_checker);

	G_OBJECT_CLASS (gspell_text_view_parent_class)->dispose (object);
}

static void
gspell_text_view_class_init (GspellTextViewClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->get_property = gspell_text_view_get_property;
	object_class->set_property = gspell_text_view_set_property;
	object_class->dispose = gspell_text_view_dispose;

	/**
	 * GspellTextView:view:
	 *
	 * The #GtkTextView.
	 */
	g_object_class_install_property (object_class,
					 PROP_VIEW,
					 g_param_spec_object ("view",
							      "View",
							      "",
							      GTK_TYPE_TEXT_VIEW,
							      G_PARAM_READWRITE |
							      G_PARAM_CONSTRUCT_ONLY |
							      G_PARAM_STATIC_STRINGS));

	/**
367
	 * GspellTextView:inline-spell-checking:
368 369 370 371
	 *
	 * Whether the inline spell checking is enabled.
	 */
	g_object_class_install_property (object_class,
372 373 374
					 PROP_INLINE_SPELL_CHECKING,
					 g_param_spec_boolean ("inline-spell-checking",
							       "Inline Spell Checking",
375 376 377 378
							       "",
							       FALSE,
							       G_PARAM_READWRITE |
							       G_PARAM_STATIC_STRINGS));
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

	/**
	 * GspellTextView:enable-language-menu:
	 *
	 * When the context menu is shown, whether to add a sub-menu to select
	 * the language for the spell checking.
	 *
	 * Since: 1.2
	 */
	g_object_class_install_property (object_class,
					 PROP_ENABLE_LANGUAGE_MENU,
					 g_param_spec_boolean ("enable-language-menu",
							       "Enable Language Menu",
							       "",
							       FALSE,
							       G_PARAM_READWRITE |
							       G_PARAM_STATIC_STRINGS));
396 397 398 399 400 401 402 403
}

static void
gspell_text_view_init (GspellTextView *gspell_view)
{
}

/**
404
 * gspell_text_view_get_from_gtk_text_view:
405 406 407 408 409 410 411 412
 * @gtk_view: a #GtkTextView.
 *
 * Returns the #GspellTextView of @gtk_view. The returned object is guaranteed
 * to be the same for the lifetime of @gtk_view.
 *
 * Returns: (transfer none): the #GspellTextView of @gtk_view.
 */
GspellTextView *
413
gspell_text_view_get_from_gtk_text_view (GtkTextView *gtk_view)
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
{
	GspellTextView *gspell_view;

	g_return_val_if_fail (GTK_IS_TEXT_VIEW (gtk_view), NULL);

	gspell_view = g_object_get_data (G_OBJECT (gtk_view), GSPELL_TEXT_VIEW_KEY);

	if (gspell_view == NULL)
	{
		gspell_view = g_object_new (GSPELL_TYPE_TEXT_VIEW,
					    "view", gtk_view,
					    NULL);

		g_object_set_data_full (G_OBJECT (gtk_view),
					GSPELL_TEXT_VIEW_KEY,
					gspell_view,
					g_object_unref);
	}

	g_return_val_if_fail (GSPELL_IS_TEXT_VIEW (gspell_view), NULL);
	return gspell_view;
}

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
/**
 * gspell_text_view_basic_setup:
 * @gspell_view: a #GspellTextView.
 *
 * This function is a convenience function that does the following:
 * - Set a spell checker. The language chosen is the one returned by
 *   gspell_language_get_default().
 * - Set the #GspellTextView:inline-spell-checking property to %TRUE.
 * - Set the #GspellTextView:enable-language-menu property to %TRUE.
 *
 * Example:
 * |[
 * GtkTextView *gtk_view;
 * GspellTextView *gspell_view;
 *
 * gspell_view = gspell_text_view_get_from_gtk_text_view (gtk_view);
 * gspell_text_view_basic_setup (gspell_view);
 * ]|
 *
 * This is equivalent to:
 * |[
 * GtkTextView *gtk_view;
 * GspellTextView *gspell_view;
 * GspellChecker *checker;
 * GtkTextBuffer *gtk_buffer;
 * GspellTextBuffer *gspell_buffer;
 *
 * checker = gspell_checker_new (NULL);
 * gtk_buffer = gtk_text_view_get_buffer (gtk_view);
 * gspell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (gtk_buffer);
 * gspell_text_buffer_set_spell_checker (gspell_buffer, checker);
 * g_object_unref (checker);
 *
 * gspell_view = gspell_text_view_get_from_gtk_text_view (gtk_view);
 * gspell_text_view_set_inline_spell_checking (gspell_view, TRUE);
 * gspell_text_view_set_enable_language_menu (gspell_view, TRUE);
 * ]|
 *
 * Since: 1.2
 */
void
gspell_text_view_basic_setup (GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;
	GspellChecker *checker;
	GtkTextBuffer *gtk_buffer;
	GspellTextBuffer *gspell_buffer;

	g_return_if_fail (GSPELL_IS_TEXT_VIEW (gspell_view));

	priv = gspell_text_view_get_instance_private (gspell_view);

	checker = gspell_checker_new (NULL);
	gtk_buffer = gtk_text_view_get_buffer (priv->view);
	gspell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (gtk_buffer);
	gspell_text_buffer_set_spell_checker (gspell_buffer, checker);
	g_object_unref (checker);

	gspell_text_view_set_inline_spell_checking (gspell_view, TRUE);
	gspell_text_view_set_enable_language_menu (gspell_view, TRUE);
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/**
 * gspell_text_view_get_view:
 * @gspell_view: a #GspellTextView.
 *
 * Returns: (transfer none): the #GtkTextView of @gspell_view.
 */
GtkTextView *
gspell_text_view_get_view (GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;

	g_return_val_if_fail (GSPELL_IS_TEXT_VIEW (gspell_view), NULL);

	priv = gspell_text_view_get_instance_private (gspell_view);
	return priv->view;
}

516
/**
517
 * gspell_text_view_get_inline_spell_checking:
518
 * @gspell_view: a #GspellTextView.
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
 *
 * Returns: whether the inline spell checking is enabled.
 */
gboolean
gspell_text_view_get_inline_spell_checking (GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;

	g_return_val_if_fail (GSPELL_IS_TEXT_VIEW (gspell_view), FALSE);

	priv = gspell_text_view_get_instance_private (gspell_view);
	return priv->inline_checker != NULL;
}

/**
 * gspell_text_view_set_inline_spell_checking:
 * @gspell_view: a #GspellTextView.
 * @enable: the new state.
537 538 539 540
 *
 * Enables or disables the inline spell checking.
 */
void
541 542
gspell_text_view_set_inline_spell_checking (GspellTextView *gspell_view,
					    gboolean        enable)
543 544 545
{
	g_return_if_fail (GSPELL_IS_TEXT_VIEW (gspell_view));

546
	enable = enable != FALSE;
547

548
	if (enable == gspell_text_view_get_inline_spell_checking (gspell_view))
549 550 551 552
	{
		return;
	}

553
	if (enable)
554 555 556 557 558 559 560 561
	{
		create_inline_checker (gspell_view);
	}
	else
	{
		destroy_inline_checker (gspell_view);
	}

562
	g_object_notify (G_OBJECT (gspell_view), "inline-spell-checking");
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
/**
 * gspell_text_view_get_enable_language_menu:
 * @gspell_view: a #GspellTextView.
 *
 * Returns: whether the language context menu is enabled.
 * Since: 1.2
 */
gboolean
gspell_text_view_get_enable_language_menu (GspellTextView *gspell_view)
{
	GspellTextViewPrivate *priv;

	g_return_val_if_fail (GSPELL_IS_TEXT_VIEW (gspell_view), FALSE);

	priv = gspell_text_view_get_instance_private (gspell_view);
	return priv->enable_language_menu;
}

/**
 * gspell_text_view_set_enable_language_menu:
 * @gspell_view: a #GspellTextView.
 * @enable_language_menu: whether to enable the language context menu.
 *
 * Sets whether to enable the language context menu. If enabled, doing a right
 * click on the #GtkTextView will show a sub-menu to choose the language for the
 * spell checking. If another language is chosen, it changes the
 * #GspellChecker:language property of the #GspellTextBuffer:spell-checker of
 * the #GtkTextView:buffer of the #GspellTextView:view.
 *
 * Since: 1.2
 */
void
gspell_text_view_set_enable_language_menu (GspellTextView *gspell_view,
					   gboolean        enable_language_menu)
{
	GspellTextViewPrivate *priv;

	g_return_if_fail (GSPELL_IS_TEXT_VIEW (gspell_view));

	priv = gspell_text_view_get_instance_private (gspell_view);

	enable_language_menu = enable_language_menu != FALSE;

	if (priv->enable_language_menu != enable_language_menu)
	{
		priv->enable_language_menu = enable_language_menu;
		g_object_notify (G_OBJECT (gspell_view), "enable-language-menu");
	}
}

615
/* ex:set ts=8 noet: */