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., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, 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 |
---|
38 | find_first (HTMLEngine *e) |
---|
39 | { |
---|
40 | guchar 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 |
---|
53 | upper_lower (HTMLObject *obj, HTMLEngine *e, gpointer data) |
---|
54 | { |
---|
55 | if (html_object_is_text (obj)) { |
---|
56 | gboolean up = GPOINTER_TO_INT (data); |
---|
57 | guchar *old_text; |
---|
58 | |
---|
59 | old_text = HTML_TEXT (obj)->text; |
---|
60 | HTML_TEXT (obj)->text = up ? g_utf8_strup (old_text, -1) : g_utf8_strdown (old_text, -1); |
---|
61 | g_free (old_text); |
---|
62 | HTML_TEXT (obj)->text_len = g_utf8_strlen (HTML_TEXT (obj)->text, -1); |
---|
63 | HTML_TEXT (obj)->text_bytes = strlen (HTML_TEXT (obj)->text); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void |
---|
68 | html_engine_capitalize_word (HTMLEngine *e) |
---|
69 | { |
---|
70 | if (find_first (e)) { |
---|
71 | guchar c; |
---|
72 | |
---|
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 | c = html_cursor_get_current_char (e->cursor); |
---|
81 | if (g_unichar_isalnum (c)) { |
---|
82 | html_engine_set_mark (e); |
---|
83 | html_engine_forward_word (e); |
---|
84 | html_engine_cut_and_paste (e, "down rest", "revert down rest", |
---|
85 | upper_lower, GINT_TO_POINTER (FALSE)); |
---|
86 | html_engine_disable_selection (e); |
---|
87 | } |
---|
88 | html_undo_level_end (e->undo); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | void |
---|
93 | html_engine_upcase_downcase_word (HTMLEngine *e, 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 |
---|
107 | set_link (HTMLObject *obj, HTMLEngine *e, gpointer data) |
---|
108 | { |
---|
109 | const char *complete_url = data; |
---|
110 | |
---|
111 | if (html_object_is_text (obj) || HTML_IS_IMAGE (obj)) { |
---|
112 | char *url = NULL; |
---|
113 | char *target = NULL; |
---|
114 | |
---|
115 | if (complete_url) { |
---|
116 | url = g_strdup (complete_url); |
---|
117 | target = strrchr (url, '#'); |
---|
118 | if (target) { |
---|
119 | *target = 0; |
---|
120 | target ++; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | if (html_object_is_text (obj)) { |
---|
125 | if (complete_url) |
---|
126 | html_text_add_link (HTML_TEXT (obj), e, url, target, 0, HTML_TEXT (obj)->text_len); |
---|
127 | else |
---|
128 | html_text_remove_links (HTML_TEXT (obj)); |
---|
129 | } else if (HTML_IS_IMAGE (obj)) { |
---|
130 | if (complete_url) |
---|
131 | html_object_set_link (obj, |
---|
132 | url && *url |
---|
133 | ? html_colorset_get_color (e->settings->color_set, HTMLLinkColor) |
---|
134 | : html_colorset_get_color (e->settings->color_set, HTMLTextColor), |
---|
135 | url, target); |
---|
136 | else |
---|
137 | html_object_remove_link (obj, |
---|
138 | html_colorset_get_color (e->settings->color_set, HTMLTextColor)); |
---|
139 | } |
---|
140 | |
---|
141 | g_free (url); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | void |
---|
146 | html_engine_set_link (HTMLEngine *e, const char *complete_url) |
---|
147 | { |
---|
148 | html_engine_cut_and_paste (e, |
---|
149 | complete_url ? "Set link" : "Remove link", |
---|
150 | complete_url ? "Remove link" : "Set link", |
---|
151 | set_link, (gpointer) complete_url); |
---|
152 | } |
---|