File: sat-pref-formats.c

package info (click to toggle)
gpredict 1.1-7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,280 kB
  • ctags: 2,873
  • sloc: ansic: 34,903; sh: 10,151; makefile: 417
file content (250 lines) | stat: -rw-r--r-- 8,088 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
  Gpredict: Real-time satellite tracking and orbit prediction program

  Copyright (C)  2001-2009  Alexandru Csete, OZ9AEC.

  Authors: Alexandru Csete <oz9aec@gmail.com>

  Comments, questions and bugreports should be submitted via
  http://sourceforge.net/projects/gpredict/
  More details can be found at the project home page:

  http://gpredict.oz9aec.net/
 
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, visit http://www.fsf.org/
*/
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <time.h>
//#include <sys/time.h>
#ifdef HAVE_CONFIG_H
#  include <build-config.h>
#endif
#include "sat-cfg.h"
#include "sat-pref-qth.h"
#include "sat-pref-formats.h"

//#ifdef G_OS_WIN32
//#  include "libc_internal.h"
//#  include "libc_interface.h"
//#endif





static const gchar *tztips = N_("Display local time instead of UTC. Note: The local "\
								"time is that of your operating system and not the "\
								"local time at the location, which you select as "\
								"tracking reference.");

static const gchar *tftips = N_("Enter a format string using the following codes:\n\n"\
								"\t%Y\tYear with century.\n"\
								"\t%m\tMonth (01-12).\n"
								"\t%d\tDay of the month (01-31).\n"
								"\t%j\tDay of the year (001-366).\n"
								"\t%H\tHour (00-23).\n"\
								"\t%M\tMinute (00-59).\n"\
								"\t%S\tSeconds (00-59).\n\n"\
								"See the user manual for more codes and examples.");

static const gchar *nsewtips = N_("Checking this box will cause geographical "\
								  "coordinates to be displayed using a suffix "\
								  "instead of sign (eg. 23.43\302\260W "\
								  "instead of -23.43\302\260).");

static const gchar *imptips = N_("Display distances using Imperial units, for "\
								 "example miles instead of kilometres.");


static GtkWidget *tzcheck;       /* "time zone" check button */
static GtkWidget *tfentry;       /* time format entry */
static GtkWidget *tflabel;       /* time format label, preview */
static GtkWidget *tfreset;       /* time format reset button */
static GtkWidget *nsewcheck;     /* N/S/W/E check button */
static GtkWidget *impcheck;      /* Use imperial units */
static guint      timer;
static gboolean   useimporg;     /* original value for use imperial */


static gboolean tfprev_cb   (gpointer data);
static void     systog_cb   (GtkToggleButton *togglebutton, gpointer user_data);
static void     reset_cb    (GtkWidget *button, gpointer data);

/** \brief Create and initialise widgets for number formats tab.
 *
 */
GtkWidget *sat_pref_formats_create ()
{
	GtkWidget   *vbox,*tfbox;
	GtkTooltips *tips;
	gchar       *text;

	/* use local time */
	tzcheck = gtk_check_button_new_with_label (_("Show local time instead of UTC."));
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tzcheck),
								  sat_cfg_get_bool (SAT_CFG_BOOL_USE_LOCAL_TIME));
	tips = gtk_tooltips_new ();
	gtk_tooltips_set_tip (tips, tzcheck, _(tztips), NULL);

	/* time format */
	tfentry = gtk_entry_new ();
	gtk_entry_set_max_length (GTK_ENTRY (tfentry), TIME_FORMAT_MAX_LENGTH);
	tips = gtk_tooltips_new ();
	gtk_tooltips_set_tip (tips, tfentry, _(tftips), NULL);

	text = sat_cfg_get_str (SAT_CFG_STR_TIME_FORMAT);
	gtk_entry_set_text (GTK_ENTRY (tfentry), text);
	g_free (text);

	tflabel = gtk_label_new ("--/--/-- --:--:--");

	/* periodic update of preview label */
	timer = g_timeout_add (1000,tfprev_cb, NULL);

	/* reset button */
	tfreset = gtk_button_new_with_label (_("Reset"));
	g_signal_connect (tfreset, "clicked", G_CALLBACK (reset_cb), NULL);
	tips = gtk_tooltips_new ();
	gtk_tooltips_set_tip (tips, tfreset, _("Reset to default value"), NULL);

	tfbox = gtk_hbox_new (FALSE, 5);
	gtk_box_pack_start (GTK_BOX (tfbox), gtk_label_new (_("Time format:")),
						FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (tfbox), tfentry, TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (tfbox), tflabel, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (tfbox), tfreset, FALSE, FALSE, 5);

	/* N/S/W/E */
	nsewcheck = gtk_check_button_new_with_label (_("Use N/S/E/W for geographical coordinates."));
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (nsewcheck),
								  sat_cfg_get_bool (SAT_CFG_BOOL_USE_NSEW));	
	tips = gtk_tooltips_new ();
	gtk_tooltips_set_tip (tips, nsewcheck, _(nsewtips), NULL);

	/* unit */
	useimporg = sat_cfg_get_bool (SAT_CFG_BOOL_USE_IMPERIAL);
	impcheck = gtk_check_button_new_with_label (_("Use Imperial units instead of Metric."));
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (impcheck), useimporg);	
	tips = gtk_tooltips_new ();
	gtk_tooltips_set_tip (tips, impcheck, _(imptips), NULL);
	/* connect sat-pref-qth hook */
	g_signal_connect (impcheck, "toggled", G_CALLBACK (systog_cb), NULL);
			 

	vbox = gtk_vbox_new (FALSE, 10);
	gtk_container_set_border_width (GTK_CONTAINER (vbox), 20);
	gtk_box_pack_start (GTK_BOX (vbox), tzcheck, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), tfbox, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), nsewcheck, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (vbox), impcheck, FALSE, FALSE, 0);

	return vbox;
}



/** \brief User pressed cancel. Any changes to config must be cancelled.
 */
void
sat_pref_formats_cancel ()
{
	/* restore imperial setting to it's original value */
	sat_cfg_set_bool (SAT_CFG_BOOL_USE_IMPERIAL, useimporg);

	g_source_remove (timer);
}


/** \brief User pressed OK. Any changes should be stored in config.
 */
void
sat_pref_formats_ok     ()
{
	g_source_remove (timer);

	sat_cfg_set_bool (SAT_CFG_BOOL_USE_LOCAL_TIME,
					  gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (tzcheck)));
	sat_cfg_set_str (SAT_CFG_STR_TIME_FORMAT,
					 gtk_entry_get_text (GTK_ENTRY (tfentry)));
	sat_cfg_set_bool (SAT_CFG_BOOL_USE_NSEW,
					  gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (nsewcheck)));
	sat_cfg_set_bool (SAT_CFG_BOOL_USE_IMPERIAL,
					  gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (impcheck)));
}



static gboolean
tfprev_cb (gpointer data)
{
	const gchar *fmtstr;
	//struct timeval tval;
	//struct timezone tzone;
	GTimeVal tval;
	time_t t;
	guint size;
	gchar buff[TIME_FORMAT_MAX_LENGTH+1];


	/* Unix time in sec since 01-Jan-1970 */
	//x = gettimeofday (&tval, &tzone);
	g_get_current_time (&tval);
	t = (time_t ) tval.tv_sec;

	fmtstr = gtk_entry_get_text (GTK_ENTRY (tfentry));
	
	/* format either local time or UTC depending on check box */
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (tzcheck)))
		size = strftime (buff, TIME_FORMAT_MAX_LENGTH, fmtstr, localtime (&t));
	else
		size = strftime (buff, TIME_FORMAT_MAX_LENGTH, fmtstr, gmtime (&t));

	if (size < TIME_FORMAT_MAX_LENGTH)
		buff[size]='\0';
	else
		buff[TIME_FORMAT_MAX_LENGTH]='\0';

	gtk_label_set_text (GTK_LABEL (tflabel), buff);

	return TRUE;
}


/** \brief Manage system toggle button signals */
static void
systog_cb   (GtkToggleButton *togglebutton, gpointer user_data)
{
	sat_cfg_set_bool (SAT_CFG_BOOL_USE_IMPERIAL,
					  gtk_toggle_button_get_active (togglebutton));

	sat_pref_qth_sys_changed (gtk_toggle_button_get_active (togglebutton));
}


/** \brief Reset time format string to default */
static void
reset_cb    (GtkWidget *button, gpointer data)
{
	gchar *fmtstr;


	fmtstr = sat_cfg_get_str_def (SAT_CFG_STR_TIME_FORMAT);
	gtk_entry_set_text (GTK_ENTRY (tfentry), fmtstr);
	g_free (fmtstr);
}