Parent Directory | Revision Log
Various fremantle fixes
1 | harbaum | 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 | harbaum | 2 | GtkWidget *entry, *label_sum; |
27 | #ifndef NO_COPY_N_PASTE | ||
28 | GtkWidget *menu; | ||
29 | #endif | ||
30 | harbaum | 1 | } 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 | harbaum | 2 | #ifndef NO_COPY_N_PASTE |
102 | harbaum | 1 | 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 | harbaum | 2 | #endif |
158 | harbaum | 1 | |
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 | harbaum | 133 | gtk_window_set_default_size(GTK_WINDOW(dialog), 400, 400); |
177 | harbaum | 1 | |
178 | harbaum | 133 | GtkWidget *hbox; |
179 | #ifndef NO_COPY_N_PASTE | ||
180 | hbox = gtk_hbox_new(FALSE, 2); | ||
181 | harbaum | 1 | |
182 | GtkWidget *label = gtk_label_new(_("Text:")); | ||
183 | gtk_misc_set_alignment(GTK_MISC(label), 0.f, 0.5f); | ||
184 | gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0); | ||
185 | |||
186 | GtkWidget *but = gtk_button_new_with_label(_("Edit")); | ||
187 | gtk_widget_set_events(but, GDK_EXPOSURE_MASK); | ||
188 | gtk_widget_add_events(but, GDK_BUTTON_PRESS_MASK); | ||
189 | gtk_signal_connect(GTK_OBJECT(but), "button-press-event", | ||
190 | (GtkSignalFunc)button_press, &context); | ||
191 | gtk_box_pack_start(GTK_BOX(hbox), but, FALSE, FALSE, 0); | ||
192 | |||
193 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
194 | hbox, FALSE, FALSE, 0); | ||
195 | harbaum | 133 | #endif |
196 | harbaum | 1 | |
197 | #ifndef USE_PANNABLE_AREA | ||
198 | GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL); | ||
199 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), | ||
200 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); | ||
201 | #else | ||
202 | GtkWidget *pannable_area = hildon_pannable_area_new(); | ||
203 | #endif | ||
204 | |||
205 | context.buffer = gtk_text_buffer_new(NULL); | ||
206 | g_signal_connect(G_OBJECT(context.buffer), "changed", | ||
207 | G_CALLBACK(callback_modified), &context); | ||
208 | |||
209 | /* set saved text */ | ||
210 | if(appdata->geotext_text) | ||
211 | gtk_text_buffer_set_text(context.buffer, appdata->geotext_text, -1); | ||
212 | |||
213 | harbaum | 8 | #ifndef USE_HILDON_TEXT_VIEW |
214 | harbaum | 1 | GtkWidget *view = gtk_text_view_new_with_buffer(context.buffer); |
215 | #else | ||
216 | GtkWidget *view = hildon_text_view_new(); | ||
217 | hildon_text_view_set_buffer(HILDON_TEXT_VIEW(view), context.buffer); | ||
218 | #endif | ||
219 | |||
220 | gtk_text_view_set_editable(GTK_TEXT_VIEW(view), TRUE); | ||
221 | |||
222 | |||
223 | #ifndef USE_PANNABLE_AREA | ||
224 | gtk_container_add(GTK_CONTAINER(scrolled_window), view); | ||
225 | gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window), | ||
226 | GTK_SHADOW_IN); | ||
227 | |||
228 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
229 | scrolled_window, TRUE, TRUE, 0); | ||
230 | #else | ||
231 | gtk_container_add(GTK_CONTAINER(pannable_area), view); | ||
232 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
233 | pannable_area, TRUE, TRUE, 0); | ||
234 | #endif | ||
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 | harbaum | 133 | GtkWidget *button = gtk_button_new(); |
243 | gtk_button_set_image(GTK_BUTTON(button), | ||
244 | gtk_image_new_from_stock(GTK_STOCK_GO_BACK, GTK_ICON_SIZE_BUTTON)); | ||
245 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5) | ||
246 | hildon_gtk_widget_set_theme_size(button, | ||
247 | (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); | ||
248 | #endif | ||
249 | harbaum | 1 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
250 | (GtkSignalFunc)on_left_shift_clicked, &context); | ||
251 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | ||
252 | |||
253 | context.entry = gtk_entry_new(); | ||
254 | char str[32]; | ||
255 | snprintf(str, sizeof(str), "%d", appdata->geotext_shift); | ||
256 | gtk_entry_set_text(GTK_ENTRY(context.entry), str); | ||
257 | gtk_box_pack_start(GTK_BOX(hbox), context.entry, FALSE, FALSE, 0); | ||
258 | |||
259 | harbaum | 133 | button = gtk_button_new(); |
260 | gtk_button_set_image(GTK_BUTTON(button), | ||
261 | gtk_image_new_from_stock(GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_BUTTON)); | ||
262 | #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5) | ||
263 | hildon_gtk_widget_set_theme_size(button, | ||
264 | (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH)); | ||
265 | #endif | ||
266 | harbaum | 1 | gtk_signal_connect(GTK_OBJECT(button), "clicked", |
267 | (GtkSignalFunc)on_right_shift_clicked, &context); | ||
268 | gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | ||
269 | |||
270 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
271 | hbox, FALSE, FALSE, 0); | ||
272 | |||
273 | /* ----------------- character sum ------------------- */ | ||
274 | |||
275 | hbox = gtk_hbox_new(FALSE,2); | ||
276 | |||
277 | gtk_box_pack_start(GTK_BOX(hbox), | ||
278 | gtk_label_new(_("Character sum (A=1,B=2,...):")), FALSE, FALSE, 0); | ||
279 | |||
280 | gtk_box_pack_start(GTK_BOX(hbox), | ||
281 | context.label_sum = gtk_label_new(""), FALSE, FALSE, 0); | ||
282 | callback_modified(NULL, &context); | ||
283 | |||
284 | gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), | ||
285 | hbox, FALSE, FALSE, 0); | ||
286 | |||
287 | /* ------------------------------------------------ */ | ||
288 | |||
289 | gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE); | ||
290 | |||
291 | gtk_widget_show_all(dialog); | ||
292 | gtk_dialog_run(GTK_DIALOG(dialog)); | ||
293 | |||
294 | /* ----------------- read state ---------------------- */ | ||
295 | GtkTextIter start, end; | ||
296 | gtk_text_buffer_get_start_iter(context.buffer, &start); | ||
297 | gtk_text_buffer_get_end_iter(context.buffer, &end); | ||
298 | appdata->geotext_text = | ||
299 | gtk_text_buffer_get_text(context.buffer, &start, &end, FALSE); | ||
300 | |||
301 | int step = atoi((char*)gtk_entry_get_text(GTK_ENTRY(context.entry))); | ||
302 | if((step < 0) || (step > 26)) { step %= 26; if(step < 0) step += 26; } | ||
303 | appdata->geotext_shift = step; | ||
304 | |||
305 | gtk_widget_destroy(dialog); | ||
306 | } |