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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* changecase.c
* This file is part of gedit
*
* Copyright (C) 2004 Paolo Borelli
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 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.
*/
/*
* Modified by the gedit Team, 2002. See the AUTHORS file for a
* list of people on the gedit Team.
* See the ChangeLog files for a list of changes.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-help.h>
#include <gedit/gedit-plugin.h>
#include <gedit/gedit-debug.h>
#include <gedit/gedit-menus.h>
#define SUBMENU_LABEL N_("C_hange Case")
#define SUBMENU_NAME "ChangeCase"
#define SUBMENU_PATH "/menu/Edit/EditOps_6/"
#define MENU_ITEMS_PATH "/menu/Edit/EditOps_6/ChangeCase/"
#define UPPER_ITEM_LABEL N_("All _Upper Case")
#define LOWER_ITEM_LABEL N_("All _Lower Case")
#define INVERT_ITEM_LABEL N_("_Invert Case")
#define TITLE_ITEM_LABEL N_("_Title Case")
#define UPPER_ITEM_TIP N_("Change selected text to upper case")
#define LOWER_ITEM_TIP N_("Change selected text to lower case")
#define INVERT_ITEM_TIP N_("Invert the case of selected text")
#define TITLE_ITEM_TIP N_("Capitalize the first letter of each selected word")
G_MODULE_EXPORT GeditPluginState update_ui (GeditPlugin *plugin, BonoboWindow *window);
G_MODULE_EXPORT GeditPluginState init (GeditPlugin *pd);
G_MODULE_EXPORT GeditPluginState destroy (GeditPlugin *pd);
G_MODULE_EXPORT GeditPluginState activate (GeditPlugin *pd);
G_MODULE_EXPORT GeditPluginState deactivate (GeditPlugin *pd);
typedef enum {
TO_UPPER_CASE,
TO_LOWER_CASE,
INVERT_CASE,
TITLE_CASE,
} ChangeCaseChoice;
static void
do_upper_case (GtkTextBuffer *buffer, GtkTextIter *start, GtkTextIter *end)
{
GString *s = g_string_new (NULL);
while (!gtk_text_iter_is_end (start) &&
!gtk_text_iter_equal (start, end))
{
gunichar c, nc;
c = gtk_text_iter_get_char (start);
nc = g_unichar_toupper (c);
g_string_append_unichar (s, nc);
gtk_text_iter_forward_char (start);
}
gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
gtk_text_buffer_insert_at_cursor (buffer, s->str, s->len);
g_string_free (s, TRUE);
}
static void
do_lower_case (GtkTextBuffer *buffer, GtkTextIter *start, GtkTextIter *end)
{
GString *s = g_string_new (NULL);
while (!gtk_text_iter_is_end (start) &&
!gtk_text_iter_equal (start, end))
{
gunichar c, nc;
c = gtk_text_iter_get_char (start);
nc = g_unichar_tolower (c);
g_string_append_unichar (s, nc);
gtk_text_iter_forward_char (start);
}
gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
gtk_text_buffer_insert_at_cursor (buffer, s->str, s->len);
g_string_free (s, TRUE);
}
static void
do_invert_case (GtkTextBuffer *buffer, GtkTextIter *start, GtkTextIter *end)
{
GString *s = g_string_new (NULL);
while (!gtk_text_iter_is_end (start) &&
!gtk_text_iter_equal (start, end))
{
gunichar c, nc;
c = gtk_text_iter_get_char (start);
if (g_unichar_islower (c))
nc = g_unichar_toupper (c);
else
nc = g_unichar_tolower (c);
g_string_append_unichar (s, nc);
gtk_text_iter_forward_char (start);
}
gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
gtk_text_buffer_insert_at_cursor (buffer, s->str, s->len);
g_string_free (s, TRUE);
}
static void
do_title_case (GtkTextBuffer *buffer, GtkTextIter *start, GtkTextIter *end)
{
GString *s = g_string_new (NULL);
while (!gtk_text_iter_is_end (start) &&
!gtk_text_iter_equal (start, end))
{
gunichar c, nc;
c = gtk_text_iter_get_char (start);
if (gtk_text_iter_starts_word (start))
nc = g_unichar_totitle (c);
else
nc = g_unichar_tolower (c);
g_string_append_unichar (s, nc);
gtk_text_iter_forward_char (start);
}
gtk_text_buffer_delete_selection (buffer, TRUE, TRUE);
gtk_text_buffer_insert_at_cursor (buffer, s->str, s->len);
g_string_free (s, TRUE);
}
static void
change_case (ChangeCaseChoice choice)
{
GeditDocument *doc;
GtkTextIter start, end;
gedit_debug (DEBUG_PLUGINS, "");
doc = gedit_get_active_document ();
g_return_if_fail (doc != NULL);
if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (doc),
&start, &end))
{
return;
}
gedit_document_begin_user_action (doc);
switch (choice)
{
case TO_UPPER_CASE:
do_upper_case (GTK_TEXT_BUFFER (doc), &start, &end);
break;
case TO_LOWER_CASE:
do_lower_case (GTK_TEXT_BUFFER (doc), &start, &end);
break;
case INVERT_CASE:
do_invert_case (GTK_TEXT_BUFFER (doc), &start, &end);
break;
case TITLE_CASE:
do_title_case (GTK_TEXT_BUFFER (doc), &start, &end);
break;
default:
g_return_if_reached ();
}
gedit_document_end_user_action (doc);
}
static void
upper_case_cb (BonoboUIComponent *uic, gpointer user_data, const gchar* verbname)
{
change_case (TO_UPPER_CASE);
}
static void
lower_case_cb (BonoboUIComponent *uic, gpointer user_data, const gchar* verbname)
{
change_case (TO_LOWER_CASE);
}
static void
invert_case_cb (BonoboUIComponent *uic, gpointer user_data, const gchar* verbname)
{
change_case (INVERT_CASE);
}
static void
title_case_cb (BonoboUIComponent *uic, gpointer user_data, const gchar* verbname)
{
change_case (TITLE_CASE);
}
gchar *changecase_sensible_verbs[] = {
"/commands/AllUpperCase",
"/commands/AllLowerCase",
"/commands/InvertCase",
"/commands/TitleCase",
NULL
};
G_MODULE_EXPORT GeditPluginState
update_ui (GeditPlugin *plugin, BonoboWindow *window)
{
BonoboUIComponent *uic;
GeditMDI *mdi;
GeditDocument *doc;
gedit_debug (DEBUG_PLUGINS, "");
g_return_val_if_fail (window != NULL, PLUGIN_ERROR);
mdi = gedit_get_mdi ();
g_return_val_if_fail (window != NULL, PLUGIN_ERROR);
uic = gedit_get_ui_component_from_window (window);
doc = gedit_get_active_document ();
if (doc == NULL ||
gedit_document_is_readonly (doc) ||
gedit_mdi_get_state (mdi) != GEDIT_STATE_NORMAL)
{
gedit_menus_set_verb_list_sensitive (uic, changecase_sensible_verbs, FALSE);
}
else
{
gedit_menus_set_verb_list_sensitive (uic, changecase_sensible_verbs, TRUE);
}
return PLUGIN_OK;
}
G_MODULE_EXPORT GeditPluginState
activate (GeditPlugin *plugin)
{
GList *top_windows;
gedit_debug (DEBUG_PLUGINS, "");
top_windows = gedit_get_top_windows ();
g_return_val_if_fail (top_windows != NULL, PLUGIN_ERROR);
while (top_windows)
{
gedit_menus_add_submenu (BONOBO_WINDOW (top_windows->data),
SUBMENU_PATH, SUBMENU_NAME,
SUBMENU_LABEL);
gedit_menus_add_menu_item (BONOBO_WINDOW (top_windows->data),
MENU_ITEMS_PATH, "AllUpperCase",
UPPER_ITEM_LABEL, UPPER_ITEM_TIP,
NULL, upper_case_cb);
gedit_menus_add_menu_item (BONOBO_WINDOW (top_windows->data),
MENU_ITEMS_PATH, "AllLowerCase",
LOWER_ITEM_LABEL, LOWER_ITEM_TIP,
NULL, lower_case_cb);
gedit_menus_add_menu_item (BONOBO_WINDOW (top_windows->data),
MENU_ITEMS_PATH, "InvertCase",
INVERT_ITEM_LABEL, INVERT_ITEM_TIP,
NULL, invert_case_cb);
gedit_menus_add_menu_item (BONOBO_WINDOW (top_windows->data),
MENU_ITEMS_PATH, "TitleCase",
TITLE_ITEM_LABEL, TITLE_ITEM_TIP,
NULL, title_case_cb);
plugin->update_ui (plugin, BONOBO_WINDOW (top_windows->data));
top_windows = g_list_next (top_windows);
}
return PLUGIN_OK;
}
G_MODULE_EXPORT GeditPluginState
deactivate (GeditPlugin *plugin)
{
gedit_menus_remove_submenu_all (SUBMENU_PATH, SUBMENU_NAME);
return PLUGIN_OK;
}
G_MODULE_EXPORT GeditPluginState
init (GeditPlugin *plugin)
{
gedit_debug (DEBUG_PLUGINS, "");
return PLUGIN_OK;
}
G_MODULE_EXPORT GeditPluginState
destroy (GeditPlugin *plugin)
{
gedit_debug (DEBUG_PLUGINS, "");
return PLUGIN_OK;
}
|