Clean up GPS display
[fw/altos] / aoview / aoview_dev_dialog.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "aoview.h"
19
20 static void
21 aoview_dev_dialog_map(GtkWidget *widget, gpointer data)
22 {
23         GtkTreeView     *dev_list = data;
24         GtkListStore    *list_store;
25         GtkTreeIter     iter;
26         int             ndev, n;
27         struct usbdev   **devs;
28
29         list_store = gtk_list_store_new(3,
30                                         G_TYPE_STRING,
31                                         G_TYPE_STRING,
32                                         G_TYPE_STRING);
33
34         ndev = aoview_usb_scan(&devs);
35         for (n = 0; n < ndev; n++) {
36                 gtk_list_store_append(list_store, &iter);
37                 gtk_list_store_set(list_store, &iter,
38                                    0, devs[n]->product,
39                                    1, devs[n]->serial,
40                                    2, devs[n]->tty,
41                                    -1);
42         }
43         gtk_tree_view_set_model (dev_list, GTK_TREE_MODEL(list_store));
44         g_object_unref(G_OBJECT(list_store));
45         gtk_tree_view_columns_autosize(dev_list);
46 }
47
48 static void
49 aoview_dev_selected(GtkTreeModel *model,
50                     GtkTreePath *path,
51                     GtkTreeIter *iter,
52                     gpointer data)
53 {
54         gchar *string;
55         gtk_tree_model_get(model, iter,
56                            2, &string,
57                            -1);
58         aoview_monitor_connect(string);
59 }
60
61 static GtkWidget        *dialog;
62
63 static void
64 aoview_dev_dialog_connect(GtkWidget *widget, gpointer data)
65 {
66         GtkTreeView             *dev_list = data;
67         GtkListStore            *list_store;
68         GtkTreeSelection        *tree_selection;
69
70         list_store = GTK_LIST_STORE(gtk_tree_view_get_model(dev_list));
71         tree_selection = gtk_tree_view_get_selection(dev_list);
72         gtk_tree_selection_selected_foreach(tree_selection,
73                                             aoview_dev_selected,
74                                             data);
75
76         gtk_widget_hide(dialog);
77 }
78
79 static void
80 aoview_dev_disconnect(GtkWidget *widget)
81 {
82         aoview_monitor_disconnect();
83 }
84
85 #define _(a) a
86
87 void
88 aoview_dev_dialog_init(GladeXML *xml)
89 {
90         GtkTreeView     *dev_list;
91         GtkWidget       *connect_button;
92         GtkTreeSelection        *dev_selection;
93         GtkWidget       *ao_disconnect;
94
95         dialog = glade_xml_get_widget(xml, "device_connect_dialog");
96         assert(dialog);
97
98         dev_list = GTK_TREE_VIEW(glade_xml_get_widget(xml, "dev_list"));
99         assert(dev_list);
100
101         aoview_add_plain_text_column(dev_list, _("Product"), 0, 16);
102         aoview_add_plain_text_column(dev_list, _("Serial"),  1, 8);
103         aoview_add_plain_text_column(dev_list, _("Device"), 2, 13);
104
105         dev_selection = gtk_tree_view_get_selection(dev_list);
106         gtk_tree_selection_set_mode(dev_selection, GTK_SELECTION_SINGLE);
107
108         g_signal_connect(G_OBJECT(dialog), "map",
109                          G_CALLBACK(aoview_dev_dialog_map),
110                          dev_list);
111
112         connect_button = glade_xml_get_widget(xml, "connect_button");
113         assert(connect_button);
114
115         g_signal_connect(G_OBJECT(connect_button), "clicked",
116                          G_CALLBACK(aoview_dev_dialog_connect),
117                          dev_list);
118
119
120         ao_disconnect = glade_xml_get_widget(xml, "ao_disconnect");
121         assert(ao_disconnect);
122
123         g_signal_connect(G_OBJECT(ao_disconnect), "activate",
124                          G_CALLBACK(aoview_dev_disconnect),
125                          ao_disconnect);
126 }