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