[go: up one dir, main page]

Menu

[f3709a]: / src / getname.c  Maximize  Restore  History

Download this file

138 lines (117 with data), 3.4 kB

  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
/*
roxterm - GTK+ 2.0 terminal emulator with tabs
Copyright (C) 2004 Tony Houghton <h@realh.co.uk>
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
*/
#include "defns.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "getname.h"
static gboolean name_clashes(const char *new_name, char **existing)
{
while (*existing)
{
if (!strcmp(new_name, *existing))
{
return TRUE;
}
++existing;
}
return FALSE;
}
static char *suggest_name(const char *orig, char **existing)
{
/* Does orig end in a number? */
size_t len = strlen(orig);
size_t numoff = len;
int num = 0;
char *suggest;
while (isdigit(orig[numoff - 1]))
--numoff;
if (numoff != len)
num = atoi(orig + numoff);
for (;;)
{
++num;
suggest = g_strdup_printf("%.*s%02d", (int) numoff, orig, num);
if (!name_clashes(suggest, existing))
break;
g_free(suggest);
}
return suggest;
}
char *getname_run_dialog(GtkWindow *parent, const char *old_name,
char **existing, const char *title, const char *button_label,
GtkWidget *icon, gboolean suggest_change)
{
GtkWidget *dialog;
GtkWidget *name_field;
char *new_name = NULL;
dialog = gtk_dialog_new_with_buttons(title, parent,
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
button_label, GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
name_field = gtk_entry_new();
gtk_box_pack_start(GTK_BOX((GTK_DIALOG(dialog))->vbox), name_field,
FALSE, FALSE, 16);
gtk_entry_set_activates_default(GTK_ENTRY(name_field), TRUE);
gtk_widget_show(name_field);
if (old_name)
{
if (suggest_change)
{
char *suggest = suggest_name(old_name, existing);
gtk_entry_set_text(GTK_ENTRY(name_field), suggest);
g_free(suggest);
}
else
{
gtk_entry_set_text(GTK_ENTRY(name_field), old_name);
}
}
while (!new_name)
{
int response;
gtk_editable_select_region(GTK_EDITABLE(name_field), 0, -1);
response = gtk_dialog_run(GTK_DIALOG(dialog));
if (response == GTK_RESPONSE_OK)
{
const char *read_name = gtk_entry_get_text(GTK_ENTRY(name_field));
if (name_clashes(read_name, existing))
{
GtkWidget *error_box = gtk_message_dialog_new(
GTK_WINDOW(dialog),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
_("An item with that name already exists"));
gtk_dialog_run(GTK_DIALOG(error_box));
gtk_widget_destroy(error_box);
new_name = NULL;
}
else
{
new_name = g_strdup(read_name);
}
}
else
{
break;
}
}
gtk_widget_destroy(dialog);
return new_name;
}
/* vi:set sw=4 ts=4 noet cindent cino= */