A Gtk3 app that added 10% tax to a price
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

addtax.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <gtk/gtk.h>
  2. #include <math.h>
  3. #include <libintl.h>
  4. #include <locale.h>
  5. #include <stdbool.h>
  6. #include <getopt.h>
  7. #include "include/addtax.h"
  8. #include "include/misc.h"
  9. GtkLabel *calculated_value = NULL;
  10. void calculate_tax(GtkWidget *entry) {
  11. char *entry_text;
  12. float money;
  13. const gchar *label_text;
  14. entry_text = (char*) gtk_entry_get_text(GTK_ENTRY(entry));
  15. money = atof(entry_text);
  16. if (is_float(entry_text)) {
  17. g_print("$%d * %f = ", (int) round(money), TAX);
  18. money = money * TAX;
  19. g_print("$%d\n", (int) round(money));
  20. label_text = g_strconcat((const gchar *) "$", g_strdup_printf("%i", (int) round(money)), NULL);
  21. gtk_label_set_text(calculated_value, label_text);
  22. }
  23. }
  24. int main(int argc, char *argv[]) {
  25. int opt;
  26. float money;
  27. int long_index = 0;
  28. static struct option long_options[] = {
  29. {"version", no_argument, 0, 'v'},
  30. {0, 0, 0, 0}
  31. };
  32. setlocale(LC_ALL, "");
  33. bindtextdomain("addtax", "/usr/share/locale/");
  34. textdomain("addtax");
  35. if (argc > 1) {
  36. // Process options
  37. while ((opt = getopt_long(argc, argv,"v", long_options, &long_index)) != -1) {
  38. switch (opt) {
  39. case 'v':
  40. printf("%s %s\n", argv[0], VERSION);
  41. break;
  42. default:
  43. fprintf(stderr, _("Usage: %s [prices...]\n"), argv[0]);
  44. return 1;
  45. }
  46. }
  47. // Process arguments that are not options
  48. while (optind < argc) {
  49. money = atof(argv[optind]);
  50. if (is_float(argv[optind++])) {
  51. printf("$%d * %f = ", (int) round(money), TAX);
  52. money = money * TAX;
  53. printf("$%d\n", (int) round(money));
  54. }
  55. else {
  56. fprintf(stderr, _("Usage: %s [prices...]\n"), argv[0]);
  57. return 1;
  58. }
  59. }
  60. return 0;
  61. }
  62. GtkBuilder *builder;
  63. GtkWidget *window;
  64. GError *error = NULL;
  65. gtk_init(&argc, &argv);
  66. builder = gtk_builder_new();
  67. gtk_builder_add_from_resource(builder, "/cl/cromer/addtax/window_main.glade", &error);
  68. if (error != NULL) {
  69. fprintf(stderr, "Error: %s\n", error->message);
  70. return 1;
  71. }
  72. window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
  73. gtk_builder_connect_signals(builder, NULL);
  74. calculated_value = (GtkLabel*) gtk_builder_get_object (builder, "calculated_value");
  75. g_object_unref(builder);
  76. gtk_widget_show(window);
  77. gtk_main();
  78. return 0;
  79. }
  80. // Called when calculate is clicked
  81. void on_button_calculate_clicked(__attribute__((unused)) GtkWidget *button, GtkWidget *entry) {
  82. calculate_tax(entry);
  83. }
  84. // Called when the user presses enter on the entry
  85. void on_entry_price_activate(GtkWidget *entry) {
  86. calculate_tax(entry);
  87. }
  88. // Called when window is closed
  89. void on_window_main_destroy() {
  90. gtk_main_quit();
  91. }