Parent Directory | Revision Log
Initial import
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of GPXView. |
5 | * |
6 | * GPXView is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * GPXView 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 |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with GPXView. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include "gpxview.h" |
21 | #include <math.h> |
22 | |
23 | typedef struct { |
24 | appdata_t *appdata; |
25 | GtkTextBuffer *buffer; |
26 | GtkWidget *entry, *label_sum, *menu; |
27 | } geotext_context_t; |
28 | |
29 | /* buffer edited */ |
30 | static void callback_modified(GtkAction * action, gpointer data ) { |
31 | geotext_context_t *context = (geotext_context_t*)data; |
32 | |
33 | if(!context->label_sum) return; |
34 | |
35 | GtkTextIter start, end; |
36 | gtk_text_buffer_get_start_iter(context->buffer, &start); |
37 | gtk_text_buffer_get_end_iter(context->buffer, &end); |
38 | char *text = gtk_text_buffer_get_text(context->buffer, &start, &end, FALSE); |
39 | |
40 | int i=0, sum=0; |
41 | while(text[i]) { |
42 | if(text[i] >= 'A' && text[i] <= 'Z') sum += text[i]-'A'+1; |
43 | if(text[i] >= 'a' && text[i] <= 'z') sum += text[i]-'a'+1; |
44 | i++; |
45 | } |
46 | |
47 | char str[32]; |
48 | snprintf(str, sizeof(str), "%d", sum); |
49 | gtk_label_set_text(GTK_LABEL(context->label_sum), str); |
50 | } |
51 | |
52 | static unsigned char rot_chr(unsigned char chr, int dir) { |
53 | /* only characters can be shifted */ |
54 | if((chr >= 'A') && (chr <= 'Z')) chr = (chr - 'A' + dir)%26 + 'A'; |
55 | if((chr >= 'a') && (chr <= 'z')) chr = (chr - 'a' + dir)%26 + 'a'; |
56 | |
57 | return chr; |
58 | } |
59 | |
60 | static void text_shift(geotext_context_t *context, int dir) { |
61 | int step = atoi((char*)gtk_entry_get_text(GTK_ENTRY(context->entry))); |
62 | |
63 | if((step < 0) || (step > 26)) { |
64 | step %= 26; |
65 | if(step < 0) step += 26; |
66 | |
67 | char str[32]; |
68 | snprintf(str, sizeof(str), "%d", step); |
69 | gtk_entry_set_text(GTK_ENTRY(context->entry), str); |
70 | } |
71 | |
72 | dir *= step; |
73 | if(dir < 0) dir += 26; |
74 | |
75 | GtkTextIter start, end; |
76 | gtk_text_buffer_get_start_iter(context->buffer, &start); |
77 | gtk_text_buffer_get_end_iter(context->buffer, &end); |
78 | char *text = gtk_text_buffer_get_text(context->buffer, &start, &end, FALSE); |
79 | |
80 | int i; |
81 | for(i=0;i<strlen(text);i++) |
82 | text[i] = rot_chr(text[i], dir); |
83 | |
84 | gtk_text_buffer_set_text(context->buffer, text, -1); |
85 | free(text); |
86 | |
87 | callback_modified(NULL, context); |
88 | } |
89 | |
90 | static void on_left_shift_clicked(GtkButton *button, gpointer data) { |
91 | text_shift((geotext_context_t*)data, -1); |
92 | } |
93 | |
94 | static void on_right_shift_clicked(GtkButton *button, gpointer data) { |
95 | text_shift((geotext_context_t*)data, +1); |
96 | } |
97 | |
98 | static void |
99 | cb_cut(GtkWidget *widget, gpointer data) { |
100 | geotext_context_t *context = (geotext_context_t*)data; |
101 | |
102 | gtk_text_buffer_cut_clipboard(GTK_TEXT_BUFFER(context->buffer), |
103 | context->appdata->clipboard, TRUE); |
104 | } |
105 | |
106 | static void |
107 | cb_copy(GtkWidget *widget, gpointer data) { |
108 | geotext_context_t *context = (geotext_context_t*)data; |
109 | |
110 | gtk_text_buffer_copy_clipboard(GTK_TEXT_BUFFER(context->buffer), |
111 | context->appdata->clipboard); |
112 | } |
113 | |
114 | static void |
115 | cb_paste(GtkWidget *widget, gpointer data) { |
116 | geotext_context_t *context = (geotext_context_t*)data; |
117 | |
118 | gtk_text_buffer_paste_clipboard(GTK_TEXT_BUFFER(context->buffer), |
119 | context->appdata->clipboard, NULL, TRUE); |
120 | } |
121 | |
122 | static gint button_press(GtkWidget *widget, GdkEventButton *event, |
123 | gpointer data) { |
124 | geotext_context_t *context = (geotext_context_t*)data; |
125 | |
126 | if (event->type == GDK_BUTTON_PRESS) { |
127 | printf("button press %d %d\n", event->button, event->time); |
128 | |
129 | if(!context->menu) { |
130 | context->menu = gtk_menu_new(); |
131 | GtkWidget *item = gtk_menu_item_new_with_label(_("Cut")); |
132 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_cut), context); |
133 | gtk_menu_shell_append(GTK_MENU_SHELL(context->menu),item); |
134 | item = gtk_menu_item_new_with_label(_("Copy")); |
135 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_copy), context); |
136 | gtk_menu_shell_append(GTK_MENU_SHELL(context->menu),item); |
137 | item = gtk_menu_item_new_with_label(_("Paste")); |
138 | g_signal_connect(item, "activate", GTK_SIGNAL_FUNC(cb_paste), context); |
139 | gtk_menu_shell_append(GTK_MENU_SHELL(context->menu),item); |
140 | gtk_widget_show_all( GTK_WIDGET(context->menu) ); |
141 | } |
142 | |
143 | gtk_menu_popup(GTK_MENU(context->menu), NULL, NULL, NULL, NULL, |
144 | event->button, event->time); |
145 | /* Tell calling code that we have handled this event; the buck |
146 | * stops here. */ |
147 | return TRUE; |
148 | } |
149 | |
150 | /* Tell calling code that we have not handled this event; pass it on. */ |
151 | return FALSE; |
152 | } |
153 | |
154 | void geotext_dialog(appdata_t *appdata) { |
155 | geotext_context_t context; |
156 | |
157 | memset(&context, 0, sizeof(geotext_context_t)); |
158 | context.appdata = appdata; |
159 | |
160 | GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Geotext"), |
161 | GTK_WINDOW(appdata->window), |
162 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
163 | GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, |
164 | NULL); |
165 | |
166 | #if defined(USE_MAEMO) && defined(HILDON_HELP) |
167 | hildon_help_dialog_help_enable(GTK_DIALOG(dialog), HELP_ID_GEOTEXT, |
168 | appdata->osso_context); |
169 | #endif |
170 | |
171 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 350); |
172 | |
173 | GtkWidget *hbox = gtk_hbox_new(FALSE, 2); |
174 | |
175 | GtkWidget *label = gtk_label_new(_("Text:")); |
176 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); |
177 | gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); |
178 | |
179 | GtkWidget *but = gtk_button_new_with_label(_("Edit")); |
180 | gtk_widget_set_events(but, GDK_EXPOSURE_MASK); |
181 | gtk_widget_add_events(but, GDK_BUTTON_PRESS_MASK); |
182 | gtk_signal_connect(GTK_OBJECT(but), "button-press-event", |
183 | (GtkSignalFunc)button_press, &context); |
184 | gtk_box_pack_start(GTK_BOX(hbox), but, FALSE, FALSE, 0); |
185 | |
186 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
187 | hbox, FALSE, FALSE, 0); |
188 | |
189 | #ifndef USE_PANNABLE_AREA |
190 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
191 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
192 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
193 | #else |
194 | GtkWidget *pannable_area = hildon_pannable_area_new(); |
195 | #endif |
196 | |
197 | context.buffer = gtk_text_buffer_new(NULL); |
198 | g_signal_connect(G_OBJECT(context.buffer), "changed", |
199 | G_CALLBACK(callback_modified), &context); |
200 | |
201 | /* set saved text */ |
202 | if(appdata->geotext_text) |
203 | gtk_text_buffer_set_text(context.buffer, appdata->geotext_text, -1); |
204 | |
205 | #ifndef USE_MAEMO |
206 | GtkWidget *view = gtk_text_view_new_with_buffer(context.buffer); |
207 | #else |
208 | GtkWidget *view = hildon_text_view_new(); |
209 | hildon_text_view_set_buffer(HILDON_TEXT_VIEW(view), context.buffer); |
210 | #endif |
211 | |
212 | gtk_text_view_set_editable(GTK_TEXT_VIEW(view), TRUE); |
213 | |
214 | |
215 | #ifndef USE_PANNABLE_AREA |
216 | gtk_container_add(GTK_CONTAINER(scrolled_window), view); |
217 | gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window), |
218 | GTK_SHADOW_IN); |
219 | |
220 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
221 | scrolled_window, TRUE, TRUE, 0); |
222 | #else |
223 | gtk_container_add(GTK_CONTAINER(pannable_area), view); |
224 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
225 | pannable_area, TRUE, TRUE, 0); |
226 | #endif |
227 | |
228 | |
229 | /* ----------------- cesar/shift ------------------- */ |
230 | hbox = gtk_hbox_new(FALSE,2); |
231 | |
232 | gtk_box_pack_start(GTK_BOX(hbox), |
233 | gtk_label_new(_("Cesar/Shift:")), FALSE, FALSE, 0); |
234 | |
235 | GtkWidget *button = gtk_button_new_with_label(_("<<")); |
236 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
237 | (GtkSignalFunc)on_left_shift_clicked, &context); |
238 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); |
239 | |
240 | context.entry = gtk_entry_new(); |
241 | char str[32]; |
242 | snprintf(str, sizeof(str), "%d", appdata->geotext_shift); |
243 | gtk_entry_set_text(GTK_ENTRY(context.entry), str); |
244 | gtk_box_pack_start(GTK_BOX(hbox), context.entry, FALSE, FALSE, 0); |
245 | |
246 | button = gtk_button_new_with_label(_(">>")); |
247 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
248 | (GtkSignalFunc)on_right_shift_clicked, &context); |
249 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); |
250 | |
251 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
252 | hbox, FALSE, FALSE, 0); |
253 | |
254 | /* ----------------- character sum ------------------- */ |
255 | |
256 | hbox = gtk_hbox_new(FALSE,2); |
257 | |
258 | gtk_box_pack_start(GTK_BOX(hbox), |
259 | gtk_label_new(_("Character sum (A=1,B=2,...):")), FALSE, FALSE, 0); |
260 | |
261 | gtk_box_pack_start(GTK_BOX(hbox), |
262 | context.label_sum = gtk_label_new(""), FALSE, FALSE, 0); |
263 | callback_modified(NULL, &context); |
264 | |
265 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), |
266 | hbox, FALSE, FALSE, 0); |
267 | |
268 | /* ------------------------------------------------ */ |
269 | |
270 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); |
271 | |
272 | gtk_widget_show_all(dialog); |
273 | gtk_dialog_run(GTK_DIALOG(dialog)); |
274 | |
275 | /* ----------------- read state ---------------------- */ |
276 | GtkTextIter start, end; |
277 | gtk_text_buffer_get_start_iter(context.buffer, &start); |
278 | gtk_text_buffer_get_end_iter(context.buffer, &end); |
279 | appdata->geotext_text = |
280 | gtk_text_buffer_get_text(context.buffer, &start, &end, FALSE); |
281 | |
282 | int step = atoi((char*)gtk_entry_get_text(GTK_ENTRY(context.entry))); |
283 | if((step < 0) || (step > 26)) { step %= 26; if(step < 0) step += 26; } |
284 | appdata->geotext_shift = step; |
285 | |
286 | gtk_widget_destroy(dialog); |
287 | } |