~ubuntu-branches/ubuntu/utopic/plotdrop/utopic

« back to all changes in this revision

Viewing changes to droplist.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-12-10 09:44:40 UTC
  • Revision ID: james.westby@ubuntu.com-20051210094440-3cva7z1cgmhmvxxw
Tags: upstream-0.5
ImportĀ upstreamĀ versionĀ 0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* PlotDrop is free software, released under the GNU General Public License
 
3
 * See the COPYING file for licensing details.
 
4
 *
 
5
 * Copyright 2005 John Spray
 
6
 */
 
7
 
 
8
 
 
9
// For exit ()
 
10
#include <stdlib.h>
 
11
 
 
12
#include <libgnomevfs/gnome-vfs.h>
 
13
#include <glade/glade.h>
 
14
#include <string.h>
 
15
#include "droplist.h"
 
16
#include "gnuplot.h"
 
17
 
 
18
static struct {
 
19
        GtkListStore *store;
 
20
        GtkWidget *view;
 
21
        GtkWidget *plotmenu;
 
22
        GtkWidget *plotbutton;
 
23
        GtkWidget *clearmenu;
 
24
        GtkWidget *deletemenu;
 
25
        GtkWidget *clearbutton;
 
26
        GtkWidget *deletebutton;
 
27
        GtkWidget *exportbutton;
 
28
        GtkWidget *renamebutton;
 
29
        GtkWidget *stylecombo;
 
30
        GtkWidget *zeroaxischeck;
 
31
        GtkWidget *errorbarscheck;
 
32
        GtkWidget *gridcheck;
 
33
        GtkWidget *enhancedmodecheck;
 
34
        GtkWidget *titleentry;
 
35
        GtkWidget *xlabelentry;
 
36
        GtkWidget *ylabelentry;
 
37
        GtkAdjustment *xmin;
 
38
        GtkAdjustment *xmax;
 
39
        GtkAdjustment *ymin;
 
40
        GtkAdjustment *ymax;
 
41
        GtkWidget *xminspin;
 
42
        GtkWidget *xmaxspin;
 
43
        GtkWidget *yminspin;
 
44
        GtkWidget *ymaxspin;
 
45
        GtkWidget *xminset;
 
46
        GtkWidget *xmaxset;
 
47
        GtkWidget *yminset;
 
48
        GtkWidget *ymaxset;
 
49
        GtkWidget *extra;
 
50
 
 
51
        GtkWidget *dropherebox;
 
52
        GtkWidget *filebox;
 
53
 
 
54
        GtkWidget *aboutdialog;
 
55
 
 
56
        gchar *exportpath;
 
57
} droplist;
 
58
 
 
59
enum
 
60
{
 
61
  COL_FILENAME = 0,
 
62
  COL_PATH,
 
63
  COL_TITLE,
 
64
  COL_SERIES,
 
65
  NUM_COLS
 
66
};
 
67
 
 
68
enum
 
69
{
 
70
        DRAG_TEXT,
 
71
        DRAG_URILIST
 
72
};
 
73
 
 
74
static GtkTargetEntry drag_types[] = {
 
75
                //{"text/*", 0, DRAG_TEXT },
 
76
                //{"STRING", 0, DRAG_TEXT },
 
77
                {"text/uri-list", 0, DRAG_URILIST }
 
78
};
 
79
static int const n_drag_types = sizeof (drag_types) / sizeof (drag_types[0]);
 
80
 
 
81
void droplist_init_gui ()
 
82
{
 
83
        GladeXML *xml;
 
84
 
 
85
        xml = glade_xml_new ("droplist.glade", NULL, NULL);
 
86
        if (!xml) {
 
87
                xml = glade_xml_new (DATADIR "/droplist.glade", NULL, NULL);
 
88
                if (!xml)
 
89
                        g_error ("Couldn't load glade file!\n");
 
90
        }
 
91
 
 
92
        GtkWidget *window;
 
93
        window = glade_xml_get_widget (xml, "DropList");
 
94
        g_signal_connect (G_OBJECT (window), "delete-event",
 
95
                G_CALLBACK (droplist_exit), NULL);
 
96
 
 
97
        GdkPixbuf *icon = gdk_pixbuf_new_from_file ("plotdrop.png", NULL);
 
98
        if (icon) {
 
99
                gtk_window_set_icon (GTK_WINDOW (window), icon);
 
100
                g_object_unref (G_OBJECT (icon));
 
101
        } else {
 
102
                icon = gdk_pixbuf_new_from_file (DATADIR "/plotdrop.png", NULL);
 
103
                if (icon) {
 
104
                        gtk_window_set_icon (GTK_WINDOW (window), icon);
 
105
                        g_object_unref (G_OBJECT (icon));
 
106
                }
 
107
        }
 
108
 
 
109
        droplist.aboutdialog = glade_xml_get_widget (xml, "AboutDialog");
 
110
 
 
111
        GtkListStore *liststore = gtk_list_store_new (NUM_COLS,
 
112
                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
 
113
        droplist.store = liststore;
 
114
 
 
115
        GtkCellRenderer *cellRO = gtk_cell_renderer_text_new ();
 
116
        //g_object_set (G_OBJECT (cellRO), "ellipsize", TRUE, NULL);
 
117
        GtkCellRenderer *cell_title = gtk_cell_renderer_text_new ();
 
118
        g_object_set (G_OBJECT (cell_title), "editable", TRUE, NULL);
 
119
        GtkCellRenderer *cell_series = gtk_cell_renderer_text_new ();
 
120
        g_object_set (G_OBJECT (cell_series), "editable", TRUE, NULL);
 
121
        //g_object_set (G_OBJECT (cellRW), "ellipsize", TRUE, NULL);
 
122
        g_signal_connect (G_OBJECT (cell_title), "edited",
 
123
                G_CALLBACK (droplist_title_edited), NULL);
 
124
        g_signal_connect (G_OBJECT (cell_series), "edited",
 
125
                G_CALLBACK (droplist_series_edited), NULL);
 
126
 
 
127
        GtkWidget *treeview;
 
128
        treeview = glade_xml_get_widget (xml, "FileView");
 
129
        gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
 
130
                                 GTK_TREE_MODEL (liststore));
 
131
 
 
132
        gtk_tree_view_insert_column_with_attributes (
 
133
                GTK_TREE_VIEW (treeview),
 
134
                -1,
 
135
                "Filename",
 
136
                cellRO,
 
137
                "text", COL_FILENAME,
 
138
                NULL);
 
139
        gtk_tree_view_column_set_resizable (
 
140
                gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 0), TRUE);
 
141
        //gtk_tree_view_column_set_sizing (gtk_tree_view_get_column (treeview, 0), GTK_TREE_VIEW_COLUMN_AUTOSIZE);
 
142
 
 
143
 
 
144
        gtk_tree_view_insert_column_with_attributes (
 
145
                GTK_TREE_VIEW (treeview),
 
146
                -1,
 
147
                "Display title",
 
148
                cell_title,
 
149
                "markup", COL_TITLE,
 
150
                NULL);
 
151
        gtk_tree_view_column_set_resizable (
 
152
                gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 1), TRUE);
 
153
        //gtk_tree_view_column_set_sizing (gtk_tree_view_get_column (treeview, 1), GTK_TREE_VIEW_COLUMN_AUTOSIZE);
 
154
 
 
155
        gtk_tree_view_insert_column_with_attributes (
 
156
                GTK_TREE_VIEW (treeview),
 
157
                -1,
 
158
                "Series",
 
159
                cell_series,
 
160
                "text", COL_SERIES,
 
161
                NULL);
 
162
        gtk_tree_view_column_set_resizable (
 
163
                gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 2), TRUE);
 
164
 
 
165
        droplist.view = treeview;
 
166
 
 
167
        gtk_drag_dest_set (
 
168
                treeview,
 
169
                GTK_DEST_DEFAULT_ALL,
 
170
                drag_types,
 
171
                n_drag_types,
 
172
                GDK_ACTION_COPY | GDK_ACTION_LINK | GDK_ACTION_MOVE);
 
173
 
 
174
        g_signal_connect (G_OBJECT (treeview), "drag-data-received",
 
175
                G_CALLBACK (droplist_drag_data_received), NULL);
 
176
        g_signal_connect (G_OBJECT (treeview), "row-activated",
 
177
                G_CALLBACK (droplist_row_activated), NULL);
 
178
 
 
179
        GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
 
180
        g_signal_connect (G_OBJECT (sel), "changed",
 
181
                G_CALLBACK (droplist_selection_changed), NULL);
 
182
 
 
183
        droplist.dropherebox = glade_xml_get_widget (xml, "DropFilesHereBox");
 
184
        droplist.filebox = glade_xml_get_widget (xml, "FileListBox");
 
185
        GtkWidget *dropfileshere = glade_xml_get_widget (xml, "DropFilesHere");
 
186
 
 
187
        gtk_drag_dest_set (
 
188
                dropfileshere,
 
189
                GTK_DEST_DEFAULT_ALL,
 
190
                drag_types,
 
191
                n_drag_types,
 
192
                GDK_ACTION_COPY | GDK_ACTION_LINK | GDK_ACTION_MOVE);
 
193
        g_signal_connect (G_OBJECT (dropfileshere), "drag-data-received",
 
194
                G_CALLBACK (droplist_drag_data_received), NULL);
 
195
 
 
196
        droplist.stylecombo = glade_xml_get_widget (xml, "Style");
 
197
        gtk_combo_box_set_active (GTK_COMBO_BOX (droplist.stylecombo), STYLE_POINTS);
 
198
 
 
199
        droplist.zeroaxischeck = glade_xml_get_widget (xml, "ShowZeroAxes");
 
200
        droplist.errorbarscheck = glade_xml_get_widget (xml, "ShowErrorbars");
 
201
        droplist.gridcheck = glade_xml_get_widget (xml, "ShowGrid");
 
202
        droplist.enhancedmodecheck = glade_xml_get_widget (xml, "EnableSuperSubScripts");
 
203
 
 
204
        droplist.xlabelentry = glade_xml_get_widget (xml, "XLabel");
 
205
        droplist.ylabelentry = glade_xml_get_widget (xml, "YLabel");
 
206
        droplist.titleentry = glade_xml_get_widget (xml, "Title");
 
207
 
 
208
        droplist.xminset = glade_xml_get_widget (xml, "XMinCheck");
 
209
        g_signal_connect (G_OBJECT (droplist.xminset), "toggled",
 
210
                G_CALLBACK (droplist_update_limit_sensitivities), NULL);
 
211
        droplist.xmaxset = glade_xml_get_widget (xml, "XMaxCheck");
 
212
        g_signal_connect (G_OBJECT (droplist.xmaxset), "toggled",
 
213
                G_CALLBACK (droplist_update_limit_sensitivities), NULL);
 
214
        droplist.yminset = glade_xml_get_widget (xml, "YMinCheck");
 
215
        g_signal_connect (G_OBJECT (droplist.yminset), "toggled",
 
216
                G_CALLBACK (droplist_update_limit_sensitivities), NULL);
 
217
        droplist.ymaxset = glade_xml_get_widget (xml, "YMaxCheck");
 
218
        g_signal_connect (G_OBJECT (droplist.ymaxset), "toggled",
 
219
                G_CALLBACK (droplist_update_limit_sensitivities), NULL);
 
220
 
 
221
        droplist.xminspin = glade_xml_get_widget (xml, "XMin");
 
222
        droplist.xmin = gtk_spin_button_get_adjustment
 
223
                (GTK_SPIN_BUTTON (droplist.xminspin));
 
224
        droplist.xmaxspin = glade_xml_get_widget (xml, "XMax");
 
225
        droplist.xmax = gtk_spin_button_get_adjustment
 
226
                (GTK_SPIN_BUTTON (droplist.xmaxspin));
 
227
        droplist.yminspin = glade_xml_get_widget (xml, "YMin");
 
228
        droplist.ymin = gtk_spin_button_get_adjustment
 
229
                (GTK_SPIN_BUTTON (droplist.yminspin));
 
230
        droplist.ymaxspin = glade_xml_get_widget (xml, "YMax");
 
231
        droplist.ymax = gtk_spin_button_get_adjustment
 
232
                (GTK_SPIN_BUTTON (droplist.ymaxspin));
 
233
 
 
234
        droplist.extra = glade_xml_get_widget (xml, "Extra");
 
235
        GtkWidget *extra_infobutton = glade_xml_get_widget (xml, "ExtraInfo");
 
236
        g_signal_connect (G_OBJECT (extra_infobutton), "clicked",
 
237
                G_CALLBACK (droplist_extra_help), NULL);
 
238
 
 
239
        GtkWidget *addfile = glade_xml_get_widget (xml, "AddFile");
 
240
        g_signal_connect (G_OBJECT (addfile), "activate",
 
241
                G_CALLBACK (droplist_add_file_UI), NULL);
 
242
 
 
243
        droplist.deletemenu = glade_xml_get_widget (xml, "Remove");
 
244
        g_signal_connect (G_OBJECT (droplist.deletemenu), "activate",
 
245
                G_CALLBACK (droplist_delete), NULL);
 
246
        droplist.deletebutton = glade_xml_get_widget (xml, "RemoveButton");
 
247
        g_signal_connect (G_OBJECT (droplist.deletebutton), "clicked",
 
248
                G_CALLBACK (droplist_delete), NULL);
 
249
 
 
250
        droplist.clearmenu = glade_xml_get_widget (xml, "Clear");
 
251
        g_signal_connect (G_OBJECT (droplist.clearmenu), "activate",
 
252
                G_CALLBACK (droplist_clear), NULL);
 
253
        droplist.clearbutton = glade_xml_get_widget (xml, "ClearButton");
 
254
        g_signal_connect (G_OBJECT (droplist.clearbutton), "clicked",
 
255
                G_CALLBACK (droplist_clear), NULL);
 
256
 
 
257
        droplist.plotmenu = glade_xml_get_widget (xml, "Plot");
 
258
        g_signal_connect (G_OBJECT (droplist.plotmenu), "activate",
 
259
                G_CALLBACK (droplist_plot), NULL);
 
260
        droplist.plotbutton = glade_xml_get_widget (xml, "PlotButton");
 
261
        g_signal_connect (G_OBJECT (droplist.plotbutton), "clicked",
 
262
                G_CALLBACK (droplist_plot), NULL);
 
263
 
 
264
        droplist.exportbutton = glade_xml_get_widget (xml, "ExportPS");
 
265
        g_signal_connect (G_OBJECT (droplist.exportbutton), "activate",
 
266
                G_CALLBACK (droplist_export), NULL);
 
267
 
 
268
        droplist.renamebutton = glade_xml_get_widget (xml, "Rename");
 
269
        g_signal_connect (G_OBJECT (droplist.renamebutton), "activate",
 
270
                G_CALLBACK (droplist_rename), NULL);
 
271
 
 
272
#if GTK_MINOR_VERSION < 6
 
273
        GtkWidget *helpmenu = glade_xml_get_widget (xml, "HelpMenu");
 
274
        gtk_widget_hide (helpmenu);
 
275
#endif
 
276
        GtkWidget *aboutbutton = glade_xml_get_widget (xml, "About");
 
277
        g_signal_connect (G_OBJECT (aboutbutton), "activate",
 
278
                G_CALLBACK (droplist_about), NULL);
 
279
 
 
280
        GtkWidget *quitbutton = glade_xml_get_widget (xml, "Quit");
 
281
        g_signal_connect (G_OBJECT (quitbutton), "activate",
 
282
                G_CALLBACK (droplist_exit), NULL);
 
283
 
 
284
        droplist_clear ();
 
285
        droplist_update_limit_sensitivities ();
 
286
        droplist_selection_changed (sel, NULL);
 
287
}
 
288
 
 
289
 
 
290
void droplist_add_file (char const *filename)
 
291
{
 
292
        gtk_widget_hide_all (droplist.dropherebox);
 
293
        gtk_widget_show_all (droplist.filebox);
 
294
 
 
295
        GtkTreeIter it;
 
296
        gtk_list_store_append (droplist.store, &it);
 
297
 
 
298
        gchar *basename = g_path_get_basename (filename);
 
299
        /*gchar *tmp = basename;
 
300
        basename = g_filename_to_utf8 ();*/
 
301
        gtk_list_store_set (droplist.store, &it,
 
302
                COL_FILENAME, basename,
 
303
                COL_PATH, filename,
 
304
                COL_TITLE, basename,
 
305
                COL_SERIES, "2",
 
306
                -1);
 
307
 
 
308
        g_free (basename);
 
309
 
 
310
        droplist_update_sensitivities ();
 
311
}
 
312
 
 
313
 
 
314
void droplist_clear ()
 
315
{
 
316
        gtk_list_store_clear (droplist.store);
 
317
        droplist_update_sensitivities ();
 
318
 
 
319
        /*GtkTreeIter it;
 
320
        gtk_list_store_append (droplist.store, &it);
 
321
        gtk_list_store_set (droplist.store, &it,
 
322
                COL_FILENAME, "",
 
323
                COL_PATH, "",
 
324
                COL_TITLE, "<i>Drop files here</i>",
 
325
                COL_SERIES, "",
 
326
                -1);*/
 
327
 
 
328
        /*GtkTreeIter it;
 
329
        gtk_list_store_append (droplist.store, &it);
 
330
        gtk_list_store_set (droplist.store, &it, COL_FILENAME, "<i>Drag files here</i>", -1);*/
 
331
}
 
332
 
 
333
 
 
334
void droplist_getdata (plotdata *data)
 
335
{
 
336
        GtkTreeModel *model = GTK_TREE_MODEL (droplist.store);
 
337
 
 
338
        int n_rows = gtk_tree_model_iter_n_children (model, NULL);
 
339
        char **paths = (char**) g_malloc (n_rows * sizeof (char*));
 
340
        char **titles = (char**) g_malloc (n_rows * sizeof (char*));
 
341
        int *series = (int*) g_malloc (n_rows * sizeof (int));
 
342
 
 
343
        GtkTreeIter it;
 
344
        if (!gtk_tree_model_get_iter_first (model, &it))
 
345
                return;
 
346
 
 
347
        int i = 0;
 
348
        do {
 
349
                gtk_tree_model_get (model, &it, COL_PATH, &paths[i], -1);
 
350
                gtk_tree_model_get (model, &it, COL_TITLE, &titles[i], -1);
 
351
                gchar *seriestmp;
 
352
                gtk_tree_model_get (model, &it, COL_SERIES, &seriestmp, -1);
 
353
                //g_print ("'%s'\n", seriestmp);
 
354
                sscanf (seriestmp, "%d", &series[i]);
 
355
                g_free (seriestmp);
 
356
                i++;
 
357
        } while (gtk_tree_model_iter_next (model, &it));
 
358
 
 
359
 
 
360
        // Files
 
361
        data->paths = paths;
 
362
        data->titles = titles;
 
363
        data->count = n_rows;
 
364
        data->series = series;
 
365
 
 
366
        // Apearance
 
367
        data->style = gtk_combo_box_get_active (GTK_COMBO_BOX (droplist.stylecombo));
 
368
        data->zeroaxis = gtk_toggle_button_get_active (
 
369
                GTK_TOGGLE_BUTTON (droplist.zeroaxischeck));
 
370
        data->errorbars = gtk_toggle_button_get_active (
 
371
                GTK_TOGGLE_BUTTON (droplist.errorbarscheck));
 
372
        data->grid = gtk_toggle_button_get_active (
 
373
                GTK_TOGGLE_BUTTON (droplist.gridcheck));
 
374
        data->enhancedmode = gtk_toggle_button_get_active (
 
375
                GTK_TOGGLE_BUTTON (droplist.enhancedmodecheck));
 
376
 
 
377
        // Captions
 
378
        data->xlabel = gtk_entry_get_text (GTK_ENTRY (droplist.xlabelentry));
 
379
        data->ylabel = gtk_entry_get_text (GTK_ENTRY (droplist.ylabelentry));
 
380
        data->title = gtk_entry_get_text (GTK_ENTRY (droplist.titleentry));
 
381
 
 
382
        // Limits
 
383
        data->xmin = gtk_adjustment_get_value (droplist.xmin);
 
384
        data->xmax = gtk_adjustment_get_value (droplist.xmax);
 
385
        data->ymin = gtk_adjustment_get_value (droplist.ymin);
 
386
        data->ymax = gtk_adjustment_get_value (droplist.ymax);
 
387
        data->xminset = gtk_toggle_button_get_active
 
388
                (GTK_TOGGLE_BUTTON (droplist.xminset));
 
389
        data->xmaxset = gtk_toggle_button_get_active
 
390
                (GTK_TOGGLE_BUTTON (droplist.xmaxset));
 
391
        data->yminset = gtk_toggle_button_get_active
 
392
                (GTK_TOGGLE_BUTTON (droplist.yminset));
 
393
        data->ymaxset = gtk_toggle_button_get_active
 
394
                (GTK_TOGGLE_BUTTON (droplist.ymaxset));
 
395
 
 
396
        // Arbitrary commands
 
397
        GtkTextIter start;
 
398
        GtkTextIter end;
 
399
        GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (droplist.extra));
 
400
        gtk_text_buffer_get_start_iter (buf, &start);
 
401
        gtk_text_buffer_get_end_iter (buf, &end);
 
402
        data->extra = gtk_text_buffer_get_text (buf, &start, &end, TRUE);
 
403
 
 
404
        // File me!
 
405
        //data->seriescount = 2;
 
406
}
 
407
 
 
408
 
 
409
void droplist_freedata (plotdata *data)
 
410
{
 
411
        for (int i = 0; i < data->count; ++i) {
 
412
                g_free (data->paths[i]);
 
413
                g_free (data->titles[i]);
 
414
        }
 
415
 
 
416
        g_free (data->paths);
 
417
        g_free (data->titles);
 
418
        g_free (data->extra);
 
419
        g_free (data->series);
 
420
}
 
421
 
 
422
 
 
423
void droplist_handle_plot (
 
424
        plotdata const *data,
 
425
        gboolean export,
 
426
        gchar *outfile,
 
427
        exportformat format)
 
428
{
 
429
        char *retval = gnuplot_plot (data, export, outfile, format);
 
430
        if (retval) {
 
431
                gchar *errorstring = g_convert (retval, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
 
432
                GtkWidget *dialog = gtk_message_dialog_new_with_markup (
 
433
                        NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
 
434
                        "<b><big>Plotting error!</big></b>\n\n"
 
435
                        "The following error(s) were experienced when running gnuplot:\n");
 
436
                gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
 
437
 
 
438
                GtkWidget *vbox = GTK_DIALOG(dialog)->vbox;
 
439
 
 
440
                GtkTextBuffer *buffer = gtk_text_buffer_new (NULL);
 
441
                gtk_text_buffer_set_text (buffer, errorstring, -1);
 
442
                g_free (errorstring);
 
443
 
 
444
                // The output probably won't fit in the window, and we don't want
 
445
                // to wrap it.
 
446
                GtkWidget *scrolled = gtk_scrolled_window_new (NULL, NULL);
 
447
                gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
 
448
                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 
449
                gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
 
450
                        GTK_SHADOW_IN);
 
451
                gtk_box_pack_start_defaults (GTK_BOX (vbox), scrolled);
 
452
 
 
453
                GtkWidget *textview = gtk_text_view_new_with_buffer (buffer);
 
454
                gtk_text_view_set_editable (GTK_TEXT_VIEW (textview), FALSE);
 
455
                // Use a monospace font, because GNUPlot formats output accordingly
 
456
                PangoFontDescription *font_desc;
 
457
                font_desc = pango_font_description_from_string("Monospace");
 
458
                gtk_widget_modify_font(textview, font_desc);
 
459
                pango_font_description_free(font_desc);
 
460
                gtk_container_add (GTK_CONTAINER (scrolled), textview);
 
461
                gtk_widget_show_all (scrolled);
 
462
 
 
463
                gtk_widget_set_size_request (GTK_WIDGET (dialog), 500, 300);
 
464
                gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
 
465
 
 
466
                gtk_dialog_run (GTK_DIALOG (dialog));
 
467
                gtk_widget_destroy (GTK_WIDGET (dialog));
 
468
        }
 
469
}
 
470
 
 
471
 
 
472
void droplist_plot ()
 
473
{
 
474
        plotdata data;
 
475
 
 
476
        droplist_getdata (&data);
 
477
        droplist_handle_plot (&data, NULL, NULL, 0);
 
478
        droplist_freedata (&data);
 
479
}
 
480
 
 
481
 
 
482
void droplist_export ()
 
483
{
 
484
        // Save the user's format preference in between calls
 
485
        static int formatrow = 0;
 
486
 
 
487
        GtkWidget *chooser = gtk_file_chooser_dialog_new (
 
488
                "Plot to File",
 
489
                NULL,
 
490
                GTK_FILE_CHOOSER_ACTION_SAVE,
 
491
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
492
                GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
 
493
                NULL);
 
494
        gtk_dialog_set_default_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
 
495
 
 
496
        #if GTK_MINOR_VERSION >= 8
 
497
                gtk_file_chooser_set_do_overwrite_confirmation (
 
498
                        GTK_FILE_CHOOSER (chooser), TRUE);
 
499
        #endif
 
500
 
 
501
        if (droplist.exportpath)
 
502
                gtk_file_chooser_set_current_folder (
 
503
                        GTK_FILE_CHOOSER (chooser), droplist.exportpath);
 
504
        else {
 
505
                // Go to the folder of the first file in the list
 
506
                GtkTreeIter it;
 
507
                GtkTreeModel *model = GTK_TREE_MODEL (droplist.store);
 
508
                if (!gtk_tree_model_get_iter_first (model, &it))
 
509
                        return;
 
510
                gchar *path;
 
511
                gchar *pathdir;
 
512
                gtk_tree_model_get (model, &it, COL_PATH, &path, -1);
 
513
                pathdir = g_path_get_dirname (path);
 
514
                gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), pathdir);
 
515
                g_free (pathdir);
 
516
        }
 
517
 
 
518
 
 
519
        static char* format_names[] = {
 
520
                "Postscript", "Color Postscript",
 
521
                "Encapsulated Postscript (EPS)", "Color EPS",
 
522
                "Portable Network Graphics (PNG)",
 
523
                "Scalable Vector Graphcs (SVG)",
 
524
                "LaTeX-embedded Postscript (pslatex)",
 
525
                "Color LaTeX-embedded Postscript (pslatex)",
 
526
                "XFig"};
 
527
        static char* format_extensions[] = {
 
528
                ".ps", ".ps", ".eps", ".eps", ".png", ".svg", ".tex", ".tex", ".fig"};
 
529
 
 
530
        GtkWidget *formatcombo = gtk_combo_box_new_text ();
 
531
        gtk_combo_box_append_text (GTK_COMBO_BOX (formatcombo), "Automatic from file name");
 
532
        for (int i = 0; i < FORMAT_COUNT; ++i)
 
533
                gtk_combo_box_append_text (GTK_COMBO_BOX (formatcombo), format_names[i]);
 
534
        gtk_combo_box_set_active (GTK_COMBO_BOX (formatcombo), formatrow);
 
535
 
 
536
        GtkWidget *formatbox = gtk_hbox_new (FALSE, 12);
 
537
        GtkWidget *formatlabel = gtk_label_new ("Format:");
 
538
        gtk_box_pack_start (GTK_BOX (formatbox), formatlabel, FALSE, FALSE, 0);
 
539
        gtk_box_pack_start_defaults (GTK_BOX (formatbox), formatcombo);
 
540
 
 
541
        gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (chooser), formatbox);
 
542
        gtk_widget_show_all (formatbox);
 
543
 
 
544
        if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) {
 
545
                gchar *filename = g_strdup (
 
546
                        gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser)));
 
547
 
 
548
                formatrow = gtk_combo_box_get_active (GTK_COMBO_BOX (formatcombo));
 
549
                exportformat format;
 
550
                gboolean guessformat = FALSE;
 
551
                if (formatrow != 0) {
 
552
                        format = formatrow - 1;
 
553
                } else {
 
554
                        format = FORMAT_PS;
 
555
                        guessformat = TRUE;
 
556
                }
 
557
 
 
558
                gchar *basename = g_path_get_basename (filename);
 
559
                gchar *extension = strchr (basename, '.');
 
560
                if (extension == NULL) {
 
561
                        // Append appropriate extension because the filename has no extension
 
562
                        char *strtmp;
 
563
                        strtmp = (char*)filename;
 
564
            filename = g_strconcat (filename, format_extensions[format], NULL);
 
565
                  g_free (strtmp);
 
566
                } else if (guessformat) {
 
567
                        // Guess at what format to used based on the extension the user gave the file
 
568
                        for (int i = 0; i < FORMAT_COUNT; ++i) {
 
569
                                if (!strcmp (format_extensions[i], extension)) {
 
570
                                        format = i;
 
571
                                        break;
 
572
                                }
 
573
                        }
 
574
                        // Falling through to the default format set above.
 
575
                }
 
576
                g_free (basename);
 
577
 
 
578
                plotdata data;
 
579
                droplist_getdata (&data);
 
580
                droplist_handle_plot (&data, TRUE, filename, format);
 
581
                droplist_freedata (&data);
 
582
 
 
583
                g_free (filename);
 
584
 
 
585
                if (droplist.exportpath)
 
586
                        g_free (droplist.exportpath);
 
587
                droplist.exportpath = g_path_get_dirname (filename);
 
588
        }
 
589
 
 
590
        gtk_widget_destroy (chooser);
 
591
}
 
592
 
 
593
 
 
594
gint droplist_exit (GtkWidget *widget, gpointer data)
 
595
{
 
596
        if (droplist_really_quit ()) {
 
597
                gtk_main_quit ();
 
598
        }
 
599
 
 
600
        return TRUE;
 
601
}
 
602
 
 
603
 
 
604
void droplist_drag_data_received (GtkWidget *widget,
 
605
                                  GdkDragContext *drag_context,
 
606
                                  gint x,
 
607
                                  gint y,
 
608
                                  GtkSelectionData *data,
 
609
                                  guint info,
 
610
                                  guint time,
 
611
                                  gpointer user_data)
 
612
{
 
613
        GList *list = NULL;
 
614
        GSList *file_list = NULL;
 
615
        GList *p = NULL;
 
616
 
 
617
        list = gnome_vfs_uri_list_parse (data->data);
 
618
        p = list;
 
619
 
 
620
        while (p != NULL)
 
621
        {
 
622
                file_list = g_slist_prepend (file_list,
 
623
                        gnome_vfs_uri_to_string ((const GnomeVFSURI*)(p->data),
 
624
                        GNOME_VFS_URI_HIDE_NONE));
 
625
 
 
626
                gchar *uripath = gnome_vfs_uri_to_string ((const GnomeVFSURI*)(p->data), 0);
 
627
                gchar *filename = g_filename_from_uri (uripath, NULL, NULL);
 
628
                droplist_add_file (filename);
 
629
                g_free (filename);
 
630
                g_free (uripath);
 
631
 
 
632
                p = g_list_next (p);
 
633
        }
 
634
 
 
635
        gnome_vfs_uri_list_free (list);
 
636
 
 
637
        file_list = g_slist_reverse (file_list);
 
638
}
 
639
 
 
640
 
 
641
void droplist_selection_changed (GtkTreeSelection *sel, gpointer data)
 
642
{
 
643
        // Update sensitivity of delete & rename buttons
 
644
        gboolean enable = gtk_tree_selection_get_selected (sel, NULL, NULL);
 
645
        gtk_widget_set_sensitive (droplist.deletemenu, enable);
 
646
        gtk_widget_set_sensitive (droplist.deletebutton, enable);
 
647
        gtk_widget_set_sensitive (droplist.renamebutton, enable);
 
648
}
 
649
 
 
650
void droplist_update_sensitivities ()
 
651
{
 
652
        gboolean enable = gtk_tree_model_iter_n_children
 
653
                (GTK_TREE_MODEL (droplist.store), NULL);
 
654
        gtk_widget_set_sensitive (droplist.plotmenu, enable);
 
655
        gtk_widget_set_sensitive (droplist.plotbutton, enable);
 
656
        gtk_widget_set_sensitive (droplist.exportbutton, enable);
 
657
        gtk_widget_set_sensitive (droplist.clearbutton, enable);
 
658
        gtk_widget_set_sensitive (droplist.clearmenu, enable);
 
659
}
 
660
 
 
661
void droplist_delete ()
 
662
{
 
663
        GtkTreeModel *model;
 
664
        GtkTreeIter it;
 
665
        if (!gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (droplist.view)), &model, &it))
 
666
                return;
 
667
 
 
668
        gtk_list_store_remove (droplist.store, &it);
 
669
 
 
670
        // Broken - we need to use gtk_tree_row_reference stuff to modify
 
671
        /*GList *selected = gtk_tree_selection_get_selected_rows (
 
672
                gtk_tree_view_get_selection (GTK_TREE_VIEW (droplist.view)));
 
673
 
 
674
        while (selected) {
 
675
                gtk_tree_model_get_iter (gtk_tree_view_get_model (GTK_TREE_VIEW (droplist.view)), &it, selected->data);
 
676
                //gtk_list_store_remove (droplist.store, &it);
 
677
                selected = selected->next;
 
678
        }
 
679
 
 
680
        g_list_foreach (list, gtk_tree_path_free, NULL);
 
681
        g_list_free (list);*/
 
682
 
 
683
        droplist_update_sensitivities ();
 
684
}
 
685
 
 
686
 
 
687
void droplist_rename ()
 
688
{
 
689
        GtkTreeModel *model;
 
690
        GtkTreeIter it;
 
691
 
 
692
        if (!gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (droplist.view)), &model, &it))
 
693
                return;
 
694
 
 
695
        GtkTreePath *path = gtk_tree_model_get_path (model, &it);
 
696
        gtk_tree_view_set_cursor (GTK_TREE_VIEW (droplist.view), path,
 
697
                gtk_tree_view_get_column (GTK_TREE_VIEW (droplist.view), 1), TRUE);
 
698
        gtk_tree_path_free (path);
 
699
}
 
700
 
 
701
 
 
702
void droplist_title_edited (GtkCellRendererText *cell,
 
703
                           gchar               *path_string,
 
704
                           gchar               *new_text,
 
705
                           gpointer             user_data)
 
706
{
 
707
        GtkTreeIter it;
 
708
        if (gtk_tree_model_get_iter_from_string (
 
709
            GTK_TREE_MODEL (droplist.store), &it, path_string))
 
710
                gtk_list_store_set (droplist.store, &it, COL_TITLE, new_text, -1);
 
711
}
 
712
 
 
713
 
 
714
void droplist_series_edited (GtkCellRendererText *cell,
 
715
                           gchar               *path_string,
 
716
                           gchar               *new_text,
 
717
                           gpointer             user_data)
 
718
{
 
719
 
 
720
        int scratch;
 
721
        // Make sure it's an integer
 
722
        if (sscanf (new_text, "%d", &scratch) != 1)
 
723
                return;
 
724
        // Field 1 is always the x axis
 
725
        if (scratch < 2)
 
726
                return;
 
727
 
 
728
        // Enter it into the treemodel
 
729
        GtkTreeIter it;
 
730
        if (gtk_tree_model_get_iter_from_string (
 
731
            GTK_TREE_MODEL (droplist.store), &it, path_string))
 
732
                gtk_list_store_set (droplist.store, &it, COL_SERIES, new_text, -1);
 
733
}
 
734
 
 
735
 
 
736
void droplist_row_activated (GtkTreeView *treeview,
 
737
                             GtkTreePath *arg1,
 
738
                             GtkTreeViewColumn *arg2,
 
739
                             gpointer user_data)
 
740
{
 
741
        GtkTreeModel *model = GTK_TREE_MODEL (droplist.store);
 
742
        GtkTreeIter it;
 
743
 
 
744
        if (!gtk_tree_selection_get_selected (
 
745
            gtk_tree_view_get_selection (treeview), &model, &it))
 
746
                return;
 
747
 
 
748
        gchar *path;
 
749
        gtk_tree_model_get (model, &it, COL_PATH, &path, -1);
 
750
        path = g_strdup_printf ("file://%s", path);
 
751
 
 
752
        gnome_vfs_url_show (path);
 
753
 
 
754
        g_free (path);
 
755
}
 
756
 
 
757
void droplist_update_limit_sensitivities ()
 
758
{
 
759
        gtk_widget_set_sensitive (droplist.xminspin,
 
760
                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (droplist.xminset)));
 
761
        gtk_widget_set_sensitive (droplist.xmaxspin,
 
762
                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (droplist.xmaxset)));
 
763
        gtk_widget_set_sensitive (droplist.yminspin,
 
764
                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (droplist.yminset)));
 
765
        gtk_widget_set_sensitive (droplist.ymaxspin,
 
766
                gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (droplist.ymaxset)));
 
767
}
 
768
 
 
769
 
 
770
void droplist_extra_help (GtkWidget *widg, gpointer data)
 
771
{
 
772
        GtkWidget *dialog = gtk_message_dialog_new_with_markup (
 
773
                NULL,
 
774
                GTK_DIALOG_DESTROY_WITH_PARENT,
 
775
                GTK_MESSAGE_INFO,
 
776
                GTK_BUTTONS_CLOSE,
 
777
                "<b><big>Using extra commands</big></b>\n\n"
 
778
                "The commands entered here are executed by gnuplot after plotdrop "
 
779
                "sets up the environment according to the requested settings, but "
 
780
                "before the plot command itself.  Thus, for example, if you select the "
 
781
                "\"Lines\" appearance and put \"set style data points\" here, then "
 
782
                "the resulting plot will be with points rather than lines."
 
783
                );
 
784
 
 
785
        gtk_dialog_run (GTK_DIALOG (dialog));
 
786
        gtk_widget_destroy (dialog);
 
787
}
 
788
 
 
789
 
 
790
void droplist_no_gnuplot ()
 
791
{
 
792
        GtkWidget *dialog = gtk_message_dialog_new_with_markup (
 
793
                NULL,
 
794
                GTK_DIALOG_DESTROY_WITH_PARENT,
 
795
                GTK_MESSAGE_ERROR,
 
796
                GTK_BUTTONS_NONE,
 
797
                "<b><big>Gnuplot not found!</big></b>\n\n"
 
798
                "PlotDrop cannot find gnuplot.  Please make sure that you have gnuplot "
 
799
                "installed, and that it is in a directory referred to in the PATH "
 
800
                "environment variable.  PlotDrop will not work.  Do you want to quit "
 
801
                "now, or continue anyway?"
 
802
                );
 
803
 
 
804
        gtk_dialog_add_buttons (GTK_DIALOG (dialog),
 
805
                "Continue Anyway", GTK_RESPONSE_OK,
 
806
                GTK_STOCK_QUIT, GTK_RESPONSE_CANCEL,
 
807
                NULL);
 
808
 
 
809
        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
 
810
 
 
811
 
 
812
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_CANCEL) {
 
813
                exit (-1);
 
814
        }
 
815
        gtk_widget_destroy (dialog);
 
816
}
 
817
 
 
818
gboolean droplist_really_quit ()
 
819
{
 
820
        if (!gtk_tree_model_iter_n_children
 
821
            (GTK_TREE_MODEL (droplist.store), NULL))
 
822
                return TRUE;
 
823
 
 
824
 
 
825
        GtkWidget *dialog = gtk_message_dialog_new_with_markup (
 
826
                NULL,
 
827
                GTK_DIALOG_MODAL,
 
828
                GTK_MESSAGE_QUESTION,
 
829
                GTK_BUTTONS_NONE,
 
830
                "<b><big>Really quit?</big></b>\n\n"
 
831
                "Are you certain you wish to quit?  If you quit now, you will lose "
 
832
                "the contents of the file list and all parameters."
 
833
                );
 
834
 
 
835
        gtk_dialog_add_buttons (GTK_DIALOG (dialog),
 
836
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
837
                GTK_STOCK_QUIT, GTK_RESPONSE_OK,
 
838
                NULL);
 
839
 
 
840
        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
 
841
 
 
842
        gboolean retval;
 
843
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
 
844
                retval = TRUE;
 
845
        else
 
846
                retval = FALSE;
 
847
 
 
848
        gtk_widget_destroy (dialog);
 
849
 
 
850
        return retval;
 
851
}
 
852
 
 
853
 
 
854
void droplist_about ()
 
855
{
 
856
#if GTK_MINOR_VERSION >= 6
 
857
        gchar *extrastring = g_strdup_printf (
 
858
                "%s\nUsing gnuplot version %s",
 
859
                gtk_about_dialog_get_comments (GTK_ABOUT_DIALOG (droplist.aboutdialog)),
 
860
                gnuplot_get_version ());
 
861
        gtk_about_dialog_set_comments (
 
862
                GTK_ABOUT_DIALOG (droplist.aboutdialog), extrastring);
 
863
 
 
864
        gtk_about_dialog_set_version (
 
865
                GTK_ABOUT_DIALOG (droplist.aboutdialog), VERSION);
 
866
 
 
867
        gtk_dialog_run (GTK_DIALOG (droplist.aboutdialog));
 
868
#endif
 
869
}
 
870
 
 
871
 
 
872
void droplist_add_file_UI ()
 
873
{
 
874
        GtkWidget *chooser = gtk_file_chooser_dialog_new (
 
875
                "Add data file",
 
876
                NULL,
 
877
                GTK_FILE_CHOOSER_ACTION_OPEN,
 
878
                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
879
                GTK_STOCK_ADD, GTK_RESPONSE_ACCEPT,
 
880
                NULL);
 
881
        gtk_dialog_set_default_response (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT);
 
882
        gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
 
883
 
 
884
        // Go to the folder of the first file in the list
 
885
        GtkTreeIter it;
 
886
        GtkTreeModel *model = GTK_TREE_MODEL (droplist.store);
 
887
        if (gtk_tree_model_get_iter_first (model, &it)) {
 
888
                gchar *path;
 
889
                gchar *pathdir;
 
890
                gtk_tree_model_get (model, &it, COL_PATH, &path, -1);
 
891
                pathdir = g_path_get_dirname (path);
 
892
                gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), pathdir);
 
893
                g_free (pathdir);
 
894
        }
 
895
 
 
896
        if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT) {
 
897
                /*droplist_add_file (
 
898
                        gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser)));*/
 
899
                GSList *files = gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (chooser));
 
900
                GSList *it = files;
 
901
                while (it != NULL) {
 
902
                        gchar *filename = (gchar*)it->data;
 
903
                        droplist_add_file (filename);
 
904
                        g_free (filename);
 
905
                        it = it->next;
 
906
                }
 
907
                g_slist_free (files);
 
908
        }
 
909
 
 
910
        gtk_widget_destroy (chooser);
 
911
 
 
912
}