1 /*
2  * This is a plug-in for GIMP.
3  *
4  * Generates clickable image maps.
5  *
6  * Copyright (C) 1998-2004 Maurits Rijk  m.rijk@chello.nl
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include "config.h"
24 
25 #include "libgimp/gimp.h"
26 #include "libgimp/gimpui.h"
27 
28 #include "imap_browse.h"
29 #include "imap_main.h"
30 #include "imap_settings.h"
31 #include "imap_string.h"
32 #include "imap_table.h"
33 
34 #include "libgimp/stdplugins-intl.h"
35 
36 typedef struct {
37   DefaultDialog_t *dialog;
38   BrowseWidget_t *imagename;
39   GtkWidget     *filename;
40   GtkWidget     *title;
41   GtkWidget     *author;
42   GtkWidget     *default_url;
43   GtkWidget     *ncsa;
44   GtkWidget     *cern;
45   GtkWidget     *csim;
46   GtkTextBuffer *description;
47 } SettingsDialog_t;
48 
49 static MapFormat_t _map_format = CSIM;
50 
51 static void
settings_ok_cb(gpointer data)52 settings_ok_cb(gpointer data)
53 {
54    SettingsDialog_t *param = (SettingsDialog_t*) data;
55    MapInfo_t *info = get_map_info();
56    gchar *description;
57    GtkTextIter start, end;
58 
59    g_strreplace(&info->image_name, gtk_entry_get_text(
60       GTK_ENTRY(param->imagename->file)));
61    g_strreplace(&info->title, gtk_entry_get_text(GTK_ENTRY(param->title)));
62    g_strreplace(&info->author, gtk_entry_get_text(GTK_ENTRY(param->author)));
63    g_strreplace(&info->default_url,
64                 gtk_entry_get_text(GTK_ENTRY(param->default_url)));
65    gtk_text_buffer_get_bounds(param->description, &start, &end);
66    description = gtk_text_buffer_get_text(param->description, &start, &end,
67                                           FALSE);
68    g_strreplace(&info->description, description);
69    g_free(description);
70 
71    info->map_format = _map_format;
72 }
73 
74 static void
type_toggled_cb(GtkWidget * widget,gpointer data)75 type_toggled_cb(GtkWidget *widget, gpointer data)
76 {
77   if (gtk_widget_get_state (widget) & GTK_STATE_SELECTED)
78     _map_format = (MapFormat_t) data;
79 }
80 
81 static SettingsDialog_t*
create_settings_dialog(void)82 create_settings_dialog(void)
83 {
84    SettingsDialog_t *data = g_new(SettingsDialog_t, 1);
85    GtkWidget *table, *view, *frame, *hbox, *label, *swin;
86    DefaultDialog_t *dialog;
87 
88    dialog = data->dialog = make_default_dialog(_("Settings for this Mapfile"));
89    default_dialog_set_ok_cb(dialog, settings_ok_cb, (gpointer) data);
90    table = default_dialog_add_table(dialog, 9, 2);
91 
92    create_label_in_table(table, 0, 0, _("Filename:"));
93    data->filename = create_label_in_table(table, 0, 1, "");
94 
95    create_label_in_table(table, 1, 0, _("Image name:"));
96    data->imagename = browse_widget_new(_("Select Image File"));
97    gtk_table_attach_defaults(GTK_TABLE(table), data->imagename->hbox, 1, 2,
98                              1, 2);
99 
100    label = create_label_in_table(table, 2, 0, _("_Title:"));
101    data->title = create_entry_in_table(table, label, 2, 1);
102    label = create_label_in_table(table, 3, 0, _("Aut_hor:"));
103    data->author = create_entry_in_table(table, label, 3, 1);
104    label = create_label_in_table(table, 4, 0, _("Default _URL:"));
105    data->default_url = create_entry_in_table(table, label, 4, 1);
106    label = create_label_in_table(table, 5, 0, _("_Description:"));
107 
108    data->description = gtk_text_buffer_new(NULL);
109 
110    view = gtk_text_view_new_with_buffer(data->description);
111    gtk_widget_set_size_request(view, -1, 128);
112    gtk_widget_show(view);
113 
114    gtk_label_set_mnemonic_widget (GTK_LABEL (label), view);
115 
116    swin = gtk_scrolled_window_new(NULL, NULL);
117    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin),
118                                        GTK_SHADOW_IN);
119    gtk_table_attach(GTK_TABLE(table), swin, 1, 2, 5, 8,
120                     GTK_EXPAND | GTK_SHRINK | GTK_FILL,
121                     GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
122    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
123                                   GTK_POLICY_NEVER,
124                                   GTK_POLICY_AUTOMATIC);
125    gtk_widget_show(swin);
126    gtk_container_add(GTK_CONTAINER(swin), view);
127 
128    frame = gimp_frame_new(_("Map File Format"));
129    gtk_widget_show(frame);
130    gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 2, 9, 10);
131    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
132    gtk_container_add(GTK_CONTAINER(frame), hbox);
133    gtk_widget_show(hbox);
134 
135    data->ncsa = gtk_radio_button_new_with_mnemonic_from_widget(NULL, "_NCSA");
136    g_signal_connect(data->ncsa, "toggled",
137                     G_CALLBACK(type_toggled_cb), (gpointer) NCSA);
138    gtk_box_pack_start(GTK_BOX(hbox), data->ncsa, FALSE, FALSE, 0);
139    gtk_widget_show(data->ncsa);
140 
141    data->cern = gtk_radio_button_new_with_mnemonic_from_widget(
142       GTK_RADIO_BUTTON(data->ncsa), "C_ERN");
143    g_signal_connect(data->cern, "toggled",
144                     G_CALLBACK(type_toggled_cb), (gpointer) CERN);
145    gtk_box_pack_start(GTK_BOX(hbox), data->cern, FALSE, FALSE, 0);
146    gtk_widget_show(data->cern);
147 
148    data->csim = gtk_radio_button_new_with_mnemonic_from_widget(
149       GTK_RADIO_BUTTON(data->cern), "C_SIM");
150    g_signal_connect(data->csim, "toggled",
151                     G_CALLBACK(type_toggled_cb), (gpointer) CSIM);
152    gtk_box_pack_start(GTK_BOX(hbox), data->csim, FALSE, FALSE, 0);
153    gtk_widget_show(data->csim);
154 
155    return data;
156 }
157 
158 void
do_settings_dialog(void)159 do_settings_dialog(void)
160 {
161    static SettingsDialog_t *dialog;
162    const char *filename = get_filename();
163    MapInfo_t *info = get_map_info();
164 
165    if (!dialog)
166       dialog = create_settings_dialog();
167 
168    if (!filename)
169      filename = _("<Untitled>");
170 
171    gtk_label_set_text(GTK_LABEL(dialog->filename), filename);
172    browse_widget_set_filename(dialog->imagename, info->image_name);
173    gtk_entry_set_text(GTK_ENTRY(dialog->title), info->title);
174    gtk_entry_set_text(GTK_ENTRY(dialog->author), info->author);
175    gtk_entry_set_text(GTK_ENTRY(dialog->default_url), info->default_url);
176 
177    if (info->map_format == NCSA)
178       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->ncsa), TRUE);
179    else if (info->map_format == CERN)
180       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->cern), TRUE);
181    else
182       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->csim), TRUE);
183 
184    gtk_widget_grab_focus(dialog->imagename->file);
185    default_dialog_show(dialog->dialog);
186 
187    gtk_text_buffer_set_text (dialog->description, info->description, -1);
188 }
189