Blame c/gtk3/calc/calc.c

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