/
UnitConversionGTK2.c
285 lines (244 loc) · 9.53 KB
/
UnitConversionGTK2.c
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <gtk/gtk.h>
#include <stdlib.h>
#include <glib.h>
#include <glib/gprintf.h>
//gcc -Wall -O2 UnitConversionGTK2.c -o UnitConversionGTK2 `pkg-config --libs --cflags gtk+-2.0`
typedef struct{
GtkWidget *combo;
GtkWidget *combo2;
GtkWidget *mainCombo;
GtkWidget *entry1;
GtkWidget *entry2;
GtkWidget *container;
}WidgetData;
void convert(GtkEntry *entry, gpointer user_data){
WidgetData *data = (WidgetData *)user_data;
int index = gtk_combo_box_get_active(GTK_COMBO_BOX(data->combo));
const char* input = gtk_entry_get_text(GTK_ENTRY(data->entry1));
int index2 = gtk_combo_box_get_active(GTK_COMBO_BOX(data->combo2));
const char* input2 = gtk_entry_get_text(GTK_ENTRY(data->entry2));
if(index == index2){
gtk_entry_set_text(GTK_ENTRY(data->entry2), input);
}
else{
if(gtk_combo_box_get_active(GTK_COMBO_BOX(data->mainCombo)) == 0)/*Mass*/{
if(index == 0 ) /*lbs*/{
if(index2 == 1)/*kg*/{
double temp;
if( (input != NULL) && (input[0] == '\0') ){
temp =0;
gtk_entry_set_text(GTK_ENTRY(data->entry2), "0.00");
}
else{
temp = atof(input);
}
temp = temp * 0.44444444;
if( abs( temp - atof(input2) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry2), ans);
free(ans);
}
}
}
else if(index == 1 )/*kg*/{
if(index2 == 0)/*lbs*/{
double temp = atof(input);
temp = temp * 2.25;
if( abs( temp - atof(input2) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry2), ans);
free(ans);
}
}
}
}
else if(gtk_combo_box_get_active(GTK_COMBO_BOX(data->mainCombo)) == 1)/*Temperature*/{
if(index == 0)/*Fahrenheit*/{
if(index2 == 1)/*Celcius*/{
double temp = atof(input);
/*if( (input != NULL) && (input[0] == '\0') ){
temp =0;
gtk_entry_set_text(GTK_ENTRY(data->entry2), "0.00");
}
else{
temp = atof(input);
}*/
temp = (temp-32)/1.8;
if( abs( temp - atof(input2) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry2), ans);
free(ans);
}
}
}
else if(index == 1)/*Celcius*/{
if(index2 == 0)/*Fahrenheit*/{
double temp = atof(input);
/*if( (input != NULL) && (input[0] == '\0') ){
temp =0;
gtk_entry_set_text(GTK_ENTRY(data->entry2), "0.00");
}
else{
temp = atof(input);
}*/
temp = temp*1.8+32;
if( abs( temp - atof(input2) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry2), ans);
free(ans);
}
}
}
}
}
}
void convert2(GtkEntry *entry, gpointer user_data){
WidgetData *data = (WidgetData *)user_data;
int index = gtk_combo_box_get_active(GTK_COMBO_BOX(data->combo));
const char* input = gtk_entry_get_text(GTK_ENTRY(data->entry1));
int index2 = gtk_combo_box_get_active(GTK_COMBO_BOX(data->combo2));
const char* input2 = gtk_entry_get_text(GTK_ENTRY(data->entry2));
if(index2 == index){
gtk_entry_set_text(GTK_ENTRY(data->entry1), input2);
}
else{
if(gtk_combo_box_get_active(GTK_COMBO_BOX(data->mainCombo)) == 0)/*mass*/{
if(index2 == 1) /*kgs*/{
if(index == 0) /*lbs*/{
double temp = atof(input2);
temp = temp * 2.25;
if( abs( temp - atof(input) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry1), ans);
free(ans);
}
}
}
else if(index2 == 0) /*lbs*/{
if(index == 1) /*kgs*/{
double temp = atof(input2);
temp = temp * 0.44444444;
if( abs( temp - atof(input) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry1), ans);
free(ans);
}
}
}
}
else if(gtk_combo_box_get_active(GTK_COMBO_BOX(data->mainCombo)) == 1)/*Temperature*/{
if(index2 == 1)/*Celcius*/{
if(index == 0)/*Fahrenheit*/{
double temp = atof(input2);
temp = temp*1.8+32;
if( abs( temp - atof(input) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry1), ans);
free(ans);
}
}
}
else if(index2 == 0)/*Fahrenheit*/{
if(index == 1)/*Celcius*/{
double temp = atof(input2);
temp = (temp-32)/1.8;;
if( abs( temp - atof(input) ) > .001){
gchar* ans =g_strdup_printf ("%.2lf",temp);
gtk_entry_set_text(GTK_ENTRY(data->entry1), ans);
free(ans);
}
}
}
}
}
}
void changeType(GtkComboBox *widget, gpointer user_data){
WidgetData *data = (WidgetData *)user_data;
if(gtk_combo_box_get_active(GTK_COMBO_BOX(data->mainCombo)) == 0){
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo),0);
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo),0);
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo2),0);
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo2),0);
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo), "Pounds (lbs)");
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo), "Kilograms (kgs)");
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo2), "Pounds (lbs)");
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo2), "Kilograms (kgs)");
gtk_combo_box_set_active(GTK_COMBO_BOX(data->combo),0);
gtk_combo_box_set_active(GTK_COMBO_BOX(data->combo2),1);
gtk_entry_set_text(GTK_ENTRY(data->entry1), "2.25");
gtk_entry_set_text(GTK_ENTRY(data->entry2), "1.00");
}
else if (gtk_combo_box_get_active(GTK_COMBO_BOX(data->mainCombo)) == 1){
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo),0);
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo),0);
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo2),0);
gtk_combo_box_remove_text (GTK_COMBO_BOX(data->combo2),0);
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo), "Fahrenheit (F)");
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo), "Celcius (C)");
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo2), "Fahrenheit (F)");
gtk_combo_box_append_text(GTK_COMBO_BOX(data->combo2), "Celcius (C)");
gtk_combo_box_set_active(GTK_COMBO_BOX(data->combo),0);
gtk_combo_box_set_active(GTK_COMBO_BOX(data->combo2),1);
gtk_entry_set_text(GTK_ENTRY(data->entry1), "32");
gtk_entry_set_text(GTK_ENTRY(data->entry2), "0");
}
}
int main(int argc, char *argv[]) {
//The gtk_init() function initializes GTK+ and parses some standard command line options.
//This function must be called before using any other GTK+ functions
gtk_init(&argc, &argv);
WidgetData *DataSet = g_slice_new(WidgetData);
GtkWidget *window;
DataSet->combo = gtk_combo_box_new_text();
DataSet->combo2 = gtk_combo_box_new_text();
DataSet->mainCombo = gtk_combo_box_new_text();
DataSet->container = gtk_fixed_new();
DataSet->entry1 = gtk_entry_new();
DataSet->entry2 = gtk_entry_new();
//gtk_window_new creates new gtk window
//GTK_WINDOW_TOPLEVEL is the window type.
//toplevel windows have a titlebar and border
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Unit Converter");
//sets window size
gtk_window_set_default_size(GTK_WINDOW(window), 280, 80);
//sets starting position
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
//adding options to combo box
gtk_combo_box_append_text(GTK_COMBO_BOX(DataSet->combo), "Pounds (lbs)");
gtk_combo_box_append_text(GTK_COMBO_BOX(DataSet->combo), "Kilograms (kgs)");
gtk_combo_box_append_text(GTK_COMBO_BOX(DataSet->combo2), "Pounds (lbs)");
gtk_combo_box_append_text(GTK_COMBO_BOX(DataSet->combo2), "Kilograms (kgs)");
gtk_combo_box_append_text(GTK_COMBO_BOX(DataSet->mainCombo), "Mass");
gtk_combo_box_append_text(GTK_COMBO_BOX(DataSet->mainCombo), "Temperature");
//setting the default settings
gtk_combo_box_set_active(GTK_COMBO_BOX(DataSet->mainCombo),0);
gtk_combo_box_set_active(GTK_COMBO_BOX(DataSet->combo),0);
gtk_combo_box_set_active(GTK_COMBO_BOX(DataSet->combo2),1);
gtk_entry_set_text(GTK_ENTRY(DataSet->entry1), "2.25");
gtk_entry_set_text(GTK_ENTRY(DataSet->entry2), "1.00");
//setting containter border and then adding widgets to containers
gtk_container_set_border_width(GTK_CONTAINER(window), 5);
gtk_container_add(GTK_CONTAINER(window), DataSet->container);
//adding the widgets to container and setting coordinates
//set_size_request is setting minimum size the widget will be
gtk_fixed_put(GTK_FIXED(DataSet->container),DataSet->mainCombo, 70, 0);
gtk_widget_set_size_request(DataSet->mainCombo, 130, 30);
gtk_fixed_put(GTK_FIXED(DataSet->container),DataSet->combo, 0, 40);
gtk_widget_set_size_request(DataSet->combo, 130, 30);
gtk_fixed_put(GTK_FIXED(DataSet->container),DataSet->combo2, 140,40);
gtk_widget_set_size_request(DataSet->combo2,130,30);
gtk_fixed_put(GTK_FIXED(DataSet->container),DataSet->entry1,0,80);
gtk_widget_set_size_request(DataSet->entry1, 130, 30);
gtk_fixed_put(GTK_FIXED(DataSet->container),DataSet->entry2,140,80);
gtk_widget_set_size_request(DataSet->entry2, 130, 30);
//g_signal_connect(GtkWidget, signal, G_CALLBACK(method it will call), parameters for the method)
g_signal_connect(window, "destroy",G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(DataSet->entry1, "changed", G_CALLBACK(convert), DataSet);
g_signal_connect(DataSet->entry2, "changed", G_CALLBACK(convert2), DataSet);
g_signal_connect(DataSet->mainCombo, "changed", G_CALLBACK(changeType), DataSet);
//flags all widget to be shown
gtk_widget_show_all(window);
gtk_main();
return 0;
}