1 /*
2     Gpredict: Real-time satellite tracking and orbit prediction program
3 
4     Copyright (C)  2001-2017  Alexandru Csete, OZ9AEC.
5 
6     This program 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 2 of the License, or
9     (at your option) any later version.
10 
11     This program 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 this program; if not, visit http://www.fsf.org/
18 */
19 #ifdef HAVE_CONFIG_H
20 #include <build-config.h>
21 #endif
22 #include <glib/gi18n.h>
23 #include <gtk/gtk.h>
24 
25 #include "sat-cfg.h"
26 #include "sat-pass-dialogs.h"
27 #include "sat-pref-single-pass.h"
28 
29 
30 #define Y0          1           /* First row where checkboxes are placed */
31 #define COLUMNS     3           /* Number of columns in the table */
32 
33 static GtkWidget *check[SINGLE_PASS_COL_NUMBER];
34 static guint    startflags;
35 static guint    flags;
36 static gboolean dirty = FALSE;
37 static gboolean reset = FALSE;
38 
39 extern const gchar *SINGLE_PASS_COL_HINT[];
40 
41 static void     toggle_cb(GtkToggleButton * toggle, gpointer data);
42 static void     create_reset_button(GtkBox * vbox);
43 static void     reset_cb(GtkWidget * button, gpointer data);
44 
45 
46 /* User pressed cancel. Any changes to config must be cancelled. */
sat_pref_single_pass_cancel()47 void sat_pref_single_pass_cancel()
48 {
49     dirty = FALSE;
50     reset = FALSE;
51 }
52 
53 /* User pressed OK. Any changes should be stored in config. */
sat_pref_single_pass_ok()54 void sat_pref_single_pass_ok()
55 {
56     if (dirty)
57     {
58         sat_cfg_set_int(SAT_CFG_INT_PRED_SINGLE_COL, flags);
59         dirty = FALSE;
60     }
61     else if (reset)
62     {
63         sat_cfg_reset_int(SAT_CFG_INT_PRED_SINGLE_COL);
64         reset = FALSE;
65     }
66 }
67 
toggle_cb(GtkToggleButton * toggle,gpointer data)68 static void toggle_cb(GtkToggleButton * toggle, gpointer data)
69 {
70     if (gtk_toggle_button_get_active(toggle))
71         flags |= (1 << GPOINTER_TO_UINT(data));
72     else
73         flags &= ~(1 << GPOINTER_TO_UINT(data));
74 
75     /* clear dirty flag if we are back where we started */
76     dirty = (flags != startflags);
77 }
78 
reset_cb(GtkWidget * button,gpointer data)79 static void reset_cb(GtkWidget * button, gpointer data)
80 {
81     guint           i;
82 
83     (void)button;
84     (void)data;
85 
86     /* get defaults */
87     flags = sat_cfg_get_int_def(SAT_CFG_INT_PRED_SINGLE_COL);
88 
89     for (i = 0; i < MULTI_PASS_COL_NUMBER; i++)
90     {
91         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check[i]),
92                                      flags & (1 << i));
93     }
94 
95     /* reset flags */
96     reset = TRUE;
97     dirty = FALSE;
98 }
99 
create_reset_button(GtkBox * vbox)100 static void create_reset_button(GtkBox * vbox)
101 {
102     GtkWidget      *button;
103     GtkWidget      *butbox;
104 
105     button = gtk_button_new_with_label(_("Reset"));
106     g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(reset_cb), NULL);
107 
108     gtk_widget_set_tooltip_text(button,
109                                 _("Reset settings to the default values."));
110 
111     butbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
112     gtk_button_box_set_layout(GTK_BUTTON_BOX(butbox), GTK_BUTTONBOX_END);
113     gtk_box_pack_end(GTK_BOX(butbox), button, FALSE, TRUE, 10);
114 
115     gtk_box_pack_end(vbox, butbox, FALSE, TRUE, 0);
116 }
117 
sat_pref_single_pass_create()118 GtkWidget      *sat_pref_single_pass_create()
119 {
120     GtkWidget      *table;
121     GtkWidget      *label;
122     GtkWidget      *vbox;
123     guint           i;
124 
125     /* create the table */
126     table = gtk_grid_new();
127     gtk_grid_set_row_spacing(GTK_GRID(table), 10);
128     gtk_grid_set_column_spacing(GTK_GRID(table), 5);
129     gtk_container_set_border_width(GTK_CONTAINER(table), 20);
130 
131     /* create header */
132     label = gtk_label_new(NULL);
133     g_object_set(label, "xalign", 0.0, "yalign", 0.5, NULL);
134     gtk_label_set_markup(GTK_LABEL(label), _("<b>Visible Columns:</b>"));
135     gtk_grid_attach(GTK_GRID(table), label, 0, 0, 2, 1);
136 
137     /* get visible column flags */
138     flags = sat_cfg_get_int(SAT_CFG_INT_PRED_SINGLE_COL);
139     for (i = 0; i < SINGLE_PASS_COL_NUMBER; i++)
140     {
141         check[i] = gtk_check_button_new_with_label(SINGLE_PASS_COL_HINT[i]);
142         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check[i]),
143                                      flags & (1 << i));
144         g_signal_connect(check[i], "toggled",
145                          G_CALLBACK(toggle_cb), GUINT_TO_POINTER(i));
146         gtk_grid_attach(GTK_GRID(table), check[i],
147                         i % COLUMNS, Y0 + i / COLUMNS, 1, 1);
148     }
149 
150     /* create vertical box */
151     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
152     gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
153     gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);
154     gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
155 
156     /* create RESET button */
157     create_reset_button(GTK_BOX(vbox));
158 
159     startflags = flags;
160     dirty = FALSE;
161     reset = FALSE;
162 
163     return vbox;
164 }
165