#include <stdlib.h>
#include <gtk/gtk.h>
GtkWidget *entry_a;
GtkWidget *entry_b;
GtkWidget *entry_c;
GtkWidget *history;
char c_str[1024];
char history_str[1024];
void add_history(const char *str) {
GtkTextIter iter;
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(history));
gtk_text_buffer_get_end_iter(buffer, &iter);
gtk_text_buffer_insert(buffer, &iter, str, -1);
gtk_text_buffer_insert(buffer, &iter, "\n", -1);
}
void clicked_add(GtkButton *button, gpointer data) {
double a = atof(gtk_entry_get_text(GTK_ENTRY(entry_a)));
double b = atof(gtk_entry_get_text(GTK_ENTRY(entry_b)));
double c = a + b;
sprintf(c_str, "%g", c);
sprintf(history_str, "%g + %g = %g", a, b, c);
gtk_entry_set_text(GTK_ENTRY(entry_c), c_str);
add_history(history_str);
}
void clicked_sub(GtkButton *button, gpointer data) {
double a = atof(gtk_entry_get_text(GTK_ENTRY(entry_a)));
double b = atof(gtk_entry_get_text(GTK_ENTRY(entry_b)));
double c = a - b;
sprintf(c_str, "%g", c);
sprintf(history_str, "%g - %g = %g", a, b, c);
gtk_entry_set_text(GTK_ENTRY(entry_c), c_str);
add_history(history_str);
}
void clicked_mul(GtkButton *button, gpointer data) {
double a = atof(gtk_entry_get_text(GTK_ENTRY(entry_a)));
double b = atof(gtk_entry_get_text(GTK_ENTRY(entry_b)));
double c = a * b;
sprintf(c_str, "%g", c);
sprintf(history_str, "%g * %g = %g", a, b, c);
gtk_entry_set_text(GTK_ENTRY(entry_c), c_str);
add_history(history_str);
}
void clicked_div(GtkButton *button, gpointer data) {
double a = atof(gtk_entry_get_text(GTK_ENTRY(entry_a)));
double b = atof(gtk_entry_get_text(GTK_ENTRY(entry_b)));
double c = a / b;
sprintf(c_str, "%g", c);
sprintf(history_str, "%g / %g = %g", a, b, c);
gtk_entry_set_text(GTK_ENTRY(entry_c), c_str);
add_history(history_str);
}
void activate(GtkApplication* app, gpointer data) {
GtkWidget *window = gtk_application_window_new(app);
gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_container_add(GTK_CONTAINER(window), hbox);
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add(GTK_CONTAINER(hbox), vbox);
entry_a = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(vbox), entry_a);
GtkWidget *buttons_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_set_homogeneous(GTK_BOX(buttons_box), TRUE);
gtk_container_add(GTK_CONTAINER(vbox), buttons_box);
GtkWidget *button_add = gtk_button_new_with_label("+");
g_signal_connect(button_add, "clicked", G_CALLBACK(clicked_add), NULL);
gtk_container_add(GTK_CONTAINER(buttons_box), button_add);
GtkWidget *button_sub = gtk_button_new_with_label("-");
g_signal_connect(button_sub, "clicked", G_CALLBACK(clicked_sub), NULL);
gtk_container_add(GTK_CONTAINER(buttons_box), button_sub);
GtkWidget *button_mul = gtk_button_new_with_label("*");
g_signal_connect(button_mul, "clicked", G_CALLBACK(clicked_mul), NULL);
gtk_container_add(GTK_CONTAINER(buttons_box), button_mul);
GtkWidget *button_div = gtk_button_new_with_label("/");
g_signal_connect(button_div, "clicked", G_CALLBACK(clicked_div), NULL);
gtk_container_add(GTK_CONTAINER(buttons_box), button_div);
entry_b = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(vbox), entry_b);
GtkWidget *separator = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
gtk_container_add(GTK_CONTAINER(vbox), separator);
entry_c = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(vbox), entry_c);
GtkWidget *frame = gtk_frame_new(NULL);
gtk_box_pack_end(GTK_BOX(hbox), frame, TRUE, TRUE, 0);
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(frame), scroll);
history = gtk_text_view_new();
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(history), 5);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(history), 5);
gtk_text_view_set_top_margin(GTK_TEXT_VIEW(history), 5);
gtk_text_view_set_bottom_margin(GTK_TEXT_VIEW(history), 5);
gtk_container_add(GTK_CONTAINER(scroll), history);
gtk_widget_show_all(window);
}
int main(int argc, char **argv) {
GtkApplication *application = gtk_application_new(NULL, 0);
g_signal_connect(application, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(application), argc, argv);
g_object_unref(application);
return status;
}