Contents of /trunk/src/geotext.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 216 - (hide annotations)
Thu Nov 26 14:32:41 2009 UTC (14 years, 6 months ago) by harbaum
File MIME type: text/plain
File size: 10251 byte(s)
Picker test
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 216
196 harbaum 133 #endif
197 harbaum 1
198     #ifndef USE_PANNABLE_AREA
199     GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
200     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
201     GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
202     #else
203     GtkWidget *pannable_area = hildon_pannable_area_new();
204     #endif
205    
206     context.buffer = gtk_text_buffer_new(NULL);
207     g_signal_connect(G_OBJECT(context.buffer), "changed",
208     G_CALLBACK(callback_modified), &context);
209    
210     /* set saved text */
211     if(appdata->geotext_text)
212     gtk_text_buffer_set_text(context.buffer, appdata->geotext_text, -1);
213    
214 harbaum 8 #ifndef USE_HILDON_TEXT_VIEW
215 harbaum 1 GtkWidget *view = gtk_text_view_new_with_buffer(context.buffer);
216     #else
217     GtkWidget *view = hildon_text_view_new();
218     hildon_text_view_set_buffer(HILDON_TEXT_VIEW(view), context.buffer);
219     #endif
220    
221     gtk_text_view_set_editable(GTK_TEXT_VIEW(view), TRUE);
222    
223    
224     #ifndef USE_PANNABLE_AREA
225     gtk_container_add(GTK_CONTAINER(scrolled_window), view);
226     gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(scrolled_window),
227     GTK_SHADOW_IN);
228    
229     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
230     scrolled_window, TRUE, TRUE, 0);
231     #else
232     gtk_container_add(GTK_CONTAINER(pannable_area), view);
233     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
234     pannable_area, TRUE, TRUE, 0);
235     #endif
236    
237     /* ----------------- cesar/shift ------------------- */
238     hbox = gtk_hbox_new(FALSE,2);
239    
240     gtk_box_pack_start(GTK_BOX(hbox),
241     gtk_label_new(_("Cesar/Shift:")), FALSE, FALSE, 0);
242    
243 harbaum 133 GtkWidget *button = gtk_button_new();
244     gtk_button_set_image(GTK_BUTTON(button),
245     gtk_image_new_from_stock(GTK_STOCK_GO_BACK, GTK_ICON_SIZE_BUTTON));
246     #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
247     hildon_gtk_widget_set_theme_size(button,
248     (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
249     #endif
250 harbaum 1 gtk_signal_connect(GTK_OBJECT(button), "clicked",
251     (GtkSignalFunc)on_left_shift_clicked, &context);
252     gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
253    
254 harbaum 212 context.entry = entry_new();
255 harbaum 1 char str[32];
256     snprintf(str, sizeof(str), "%d", appdata->geotext_shift);
257     gtk_entry_set_text(GTK_ENTRY(context.entry), str);
258     gtk_box_pack_start(GTK_BOX(hbox), context.entry, FALSE, FALSE, 0);
259    
260 harbaum 133 button = gtk_button_new();
261     gtk_button_set_image(GTK_BUTTON(button),
262     gtk_image_new_from_stock(GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_BUTTON));
263     #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR == 5)
264     hildon_gtk_widget_set_theme_size(button,
265     (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
266     #endif
267 harbaum 1 gtk_signal_connect(GTK_OBJECT(button), "clicked",
268     (GtkSignalFunc)on_right_shift_clicked, &context);
269     gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
270    
271     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
272     hbox, FALSE, FALSE, 0);
273    
274     /* ----------------- character sum ------------------- */
275    
276     hbox = gtk_hbox_new(FALSE,2);
277    
278     gtk_box_pack_start(GTK_BOX(hbox),
279     gtk_label_new(_("Character sum (A=1,B=2,...):")), FALSE, FALSE, 0);
280    
281     gtk_box_pack_start(GTK_BOX(hbox),
282     context.label_sum = gtk_label_new(""), FALSE, FALSE, 0);
283     callback_modified(NULL, &context);
284    
285     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
286     hbox, FALSE, FALSE, 0);
287    
288     /* ------------------------------------------------ */
289    
290 harbaum 216 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
291     picker_button_new(), FALSE, FALSE, 0);
292    
293 harbaum 1 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
294    
295     gtk_widget_show_all(dialog);
296     gtk_dialog_run(GTK_DIALOG(dialog));
297    
298     /* ----------------- read state ---------------------- */
299     GtkTextIter start, end;
300     gtk_text_buffer_get_start_iter(context.buffer, &start);
301     gtk_text_buffer_get_end_iter(context.buffer, &end);
302     appdata->geotext_text =
303     gtk_text_buffer_get_text(context.buffer, &start, &end, FALSE);
304    
305     int step = atoi((char*)gtk_entry_get_text(GTK_ENTRY(context.entry)));
306     if((step < 0) || (step > 26)) { step %= 26; if(step < 0) step += 26; }
307     appdata->geotext_shift = step;
308    
309     gtk_widget_destroy(dialog);
310     }