File: obsbutton.c

package info (click to toggle)
gretl 2019a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 53,708 kB
  • sloc: ansic: 367,137; sh: 4,416; makefile: 2,636; cpp: 2,499; xml: 580; perl: 364
file content (141 lines) | stat: -rw-r--r-- 3,805 bytes parent folder | download | duplicates (2)
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
/* 
 *  gretl -- Gnu Regression, Econometrics and Time-series Library
 *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
 * 
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 * 
 */

#include "gretl.h"
#include "obsbutton.h"

static gboolean obs_button_input (GtkSpinButton *spin, 
				  gdouble *new_val,
				  gpointer p)
{
    const gchar *obs = gtk_entry_get_text(GTK_ENTRY(spin));
    int n;

    if (g_object_get_data(G_OBJECT(spin), "newdata")) {
	n = merge_dateton(obs, (DATASET *) p);
    } else {
	n = dateton(obs, (DATASET *) p);
    }

    *new_val = n;

    return TRUE;
}

static gboolean obs_button_output (GtkSpinButton *spin, gpointer p)
{
    gpointer rset;
    gchar buf[OBSLEN];
    int n = gtk_spin_button_get_value_as_int(spin);

    ntodate(buf, n, (DATASET *) p);

    if (strcmp(buf, gtk_entry_get_text(GTK_ENTRY(spin)))) {
	gtk_entry_set_text(GTK_ENTRY(spin), buf);
    }

    rset = g_object_get_data(G_OBJECT(spin), "rset");
    if (rset != NULL) {
	update_obs_label(NULL, rset);
    }

    return TRUE;
}

GtkWidget *obs_button_new (GtkAdjustment *adj, DATASET *dset,
			   ObsButtonRole role) 
{
    GtkWidget *spinner;
    int n = strlen(dset->endobs);

    spinner = gtk_spin_button_new(adj, 1, 0);
    gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spinner), FALSE);
    gtk_entry_set_width_chars(GTK_ENTRY(spinner), (n < 2)? 2 : n);
#if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION >= 12
    /* remedy required for gtk3 */
    gtk_entry_set_max_width_chars(GTK_ENTRY(spinner), (n < 2)? 2 : n);
#endif
    
    g_signal_connect(G_OBJECT(spinner), "input",
		     G_CALLBACK(obs_button_input), dset);
    g_signal_connect(G_OBJECT(spinner), "output",
		     G_CALLBACK(obs_button_output), dset);
    
    if (role) {
	g_object_set_data(G_OBJECT(spinner), "role", GINT_TO_POINTER(role));
    }

    return spinner;
}

GtkWidget *data_start_button (GtkAdjustment *adj, DATASET *dset) 
{
    GtkWidget *spinner = obs_button_new(adj, dset, 0);

    g_object_set_data(G_OBJECT(spinner), "newdata", GINT_TO_POINTER(1));

    return spinner;
}

int obs_button_get_value (GtkWidget *button)
{
    return gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(button));
}

const gchar *obs_button_get_string (GtkWidget *button)
{
    return gtk_entry_get_text(GTK_ENTRY(button));
}

static void alert_partner (GtkWidget *b, GtkWidget *partner)
{
    GtkSpinButton *b1, *b2;
    int b_role, t1, t2;

    b_role = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(b), "role"));

    if (b_role == OBS_BUTTON_T1) {
	b1 = GTK_SPIN_BUTTON(b);
	b2 = GTK_SPIN_BUTTON(partner);
    } else {
	b1 = GTK_SPIN_BUTTON(partner);
	b2 = GTK_SPIN_BUTTON(b);
    }
	
    t1 = gtk_spin_button_get_value_as_int(b1);
    t2 = gtk_spin_button_get_value_as_int(b2);

    if (t2 < t1) {
	if (b_role == OBS_BUTTON_T1) {
	    /* force t2 to adjust upward */
	    gtk_spin_button_set_value(b2, (gdouble) t1);   
	} else {
	    /* force t1 to adjust downward */
	    gtk_spin_button_set_value(b1, (gdouble) t2);   
	}
    }
}

void obs_button_set_partner (GtkWidget *button, GtkWidget *partner)
{
    g_signal_connect(G_OBJECT(button), "value-changed",
		     G_CALLBACK(alert_partner), partner);
}