1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* This file is part of the GtkHTML library
3  *
4  * Copyright (C) 2000 Helix Code, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include "htmlcolorset.h"
26 #include "htmlcursor.h"
27 #include "htmlengine-edit-text.h"
28 #include "htmlengine-edit-movement.h"
29 #include "htmlengine-edit.h"
30 #include "htmlselection.h"
31 #include "htmltext.h"
32 #include "htmlengine.h"
33 #include "htmlimage.h"
34 #include "htmlsettings.h"
35 #include "htmlundo.h"
36 
37 static gboolean
find_first(HTMLEngine * e)38 find_first (HTMLEngine *e)
39 {
40 	gunichar c;
41 
42 	c = html_cursor_get_current_char (e->cursor);
43 	while (c == 0 || !g_unichar_isalnum (c) || c == ' ') {
44 		if (!html_cursor_forward (e->cursor, e))
45 			return FALSE;
46 		c = html_cursor_get_current_char (e->cursor);
47 	}
48 
49 	return TRUE;
50 }
51 
52 static void
upper_lower(HTMLObject * obj,HTMLEngine * e,gpointer data)53 upper_lower (HTMLObject *obj,
54              HTMLEngine *e,
55              gpointer data)
56 {
57 	if (html_object_is_text (obj)) {
58 		gboolean up = GPOINTER_TO_INT (data);
59 		gchar *old_text;
60 
61 		old_text = HTML_TEXT (obj)->text;
62 		HTML_TEXT (obj)->text = up ? g_utf8_strup (old_text, -1) : g_utf8_strdown (old_text, -1);
63 		g_free (old_text);
64 		HTML_TEXT (obj)->text_len = g_utf8_strlen (HTML_TEXT (obj)->text, -1);
65 		HTML_TEXT (obj)->text_bytes = strlen (HTML_TEXT (obj)->text);
66 	}
67 }
68 
69 void
html_engine_capitalize_word(HTMLEngine * e)70 html_engine_capitalize_word (HTMLEngine *e)
71 {
72 	if (find_first (e)) {
73 		html_undo_level_begin (e->undo, "Capitalize word", "Revert word capitalize");
74 		html_engine_set_mark (e);
75 		html_cursor_forward (e->cursor, e);
76 		html_engine_cut_and_paste (e, "up 1st", "revert up 1st",
77 					   upper_lower, GINT_TO_POINTER (TRUE));
78 		html_engine_disable_selection (e);
79 
80 		if (g_unichar_isalnum (html_cursor_get_current_char (e->cursor))) {
81 			html_engine_set_mark (e);
82 			html_engine_forward_word (e);
83 			html_engine_cut_and_paste (e, "down rest", "revert down rest",
84 						   upper_lower, GINT_TO_POINTER (FALSE));
85 			html_engine_disable_selection (e);
86 		}
87 		html_undo_level_end (e->undo, e);
88 	}
89 }
90 
91 void
html_engine_upcase_downcase_word(HTMLEngine * e,gboolean up)92 html_engine_upcase_downcase_word (HTMLEngine *e,
93                                   gboolean up)
94 {
95 	if (find_first (e)) {
96 		html_engine_set_mark (e);
97 		html_engine_forward_word (e);
98 		html_engine_cut_and_paste (e,
99 					   up ? "Upcase word" : "Downcase word",
100 					   up ? "Revert word upcase" : "Revert word downcase",
101 					   upper_lower, GINT_TO_POINTER (up));
102 		html_engine_disable_selection (e);
103 	}
104 }
105 
106 static void
set_link(HTMLObject * obj,HTMLEngine * e,gpointer data)107 set_link (HTMLObject *obj,
108           HTMLEngine *e,
109           gpointer data)
110 {
111 	const gchar *complete_url = data;
112 
113 	if (html_object_is_text (obj) || HTML_IS_IMAGE (obj)) {
114 		gchar *url = NULL;
115 		gchar *target = NULL;
116 
117 		if (complete_url) {
118 			url = g_strdup (complete_url);
119 			target = strrchr (url, '#');
120 			if (target) {
121 				*target = 0;
122 				target++;
123 			}
124 		}
125 
126 		if (html_object_is_text (obj)) {
127 			if (complete_url)
128 				html_text_add_link (HTML_TEXT (obj), e, url, target, 0, HTML_TEXT (obj)->text_len);
129 			else
130 				html_text_remove_links (HTML_TEXT (obj));
131 		} else if (HTML_IS_IMAGE (obj)) {
132 			if (complete_url)
133 				html_object_set_link (obj,
134 						      url && *url
135 						      ? html_colorset_get_color (e->settings->color_set, HTMLLinkColor)
136 						      : html_colorset_get_color (e->settings->color_set, HTMLTextColor),
137 						      url, target);
138 			else
139 				html_object_remove_link (obj,
140 							 html_colorset_get_color (e->settings->color_set, HTMLTextColor));
141 		}
142 
143 		g_free (url);
144 	}
145 }
146 
147 void
html_engine_set_link(HTMLEngine * e,const gchar * complete_url)148 html_engine_set_link (HTMLEngine *e,
149                       const gchar *complete_url)
150 {
151 	html_engine_cut_and_paste (e,
152 				   complete_url ? "Set link" : "Remove link",
153 				   complete_url ? "Remove link" : "Set link",
154 				   set_link, (gpointer) complete_url);
155 }
156