commit d34bd8f72e4499a1ee2ffc68bb9a5c286b071deb Author: Chris Cromer Date: Thu Sep 13 16:25:44 2018 -0300 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..026867a --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2018 Chris Cromer + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/glade/main_resource.xml b/glade/main_resource.xml new file mode 100644 index 0000000..59542be --- /dev/null +++ b/glade/main_resource.xml @@ -0,0 +1,6 @@ + + + + window_main.glade + + diff --git a/glade/meson.build b/glade/meson.build new file mode 100644 index 0000000..b40dc0e --- /dev/null +++ b/glade/meson.build @@ -0,0 +1,8 @@ +gen = generator( + find_program('glib-compile-resources'), + arguments: [ '--sourcedir=@SOURCE_DIR@/glade', + '--generate-source', + '--target=@OUTPUT@', + '@INPUT@'], + output: '@PLAINNAME@.c') +main_resource_c = gen.process(['main_resource.xml']) diff --git a/glade/window_main.glade b/glade/window_main.glade new file mode 100644 index 0000000..f2702fc --- /dev/null +++ b/glade/window_main.glade @@ -0,0 +1,91 @@ + + + + + + False + Add tax + + + + + + + True + False + 12 + 12 + 12 + 12 + vertical + 12 + top + + + True + False + 12 + + + True + False + Price + + + + + + False + True + 0 + + + + + True + True + + + + True + True + 1 + + + + + Calculate + True + True + True + + + + False + True + 2 + + + + + False + True + 0 + + + + + True + False + $0 + + + True + True + 1 + + + + + + diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..e61cf64 --- /dev/null +++ b/meson.build @@ -0,0 +1,10 @@ +project('addtax', + 'c', + version : '1.0.0', + license : 'BSD-3', + default_options : [ 'b_ofast=if-release', 'b_march_native=if-release', 'b_ndebug=if-release' ] +) +add_global_arguments('-DGETTEXT_PACKAGE="addtax"', language: 'c') +subdir('po') +subdir('glade') +subdir('src') diff --git a/po/LINGUAS b/po/LINGUAS new file mode 100644 index 0000000..8357fca --- /dev/null +++ b/po/LINGUAS @@ -0,0 +1 @@ +es diff --git a/po/POTFILES b/po/POTFILES new file mode 100644 index 0000000..85da9d7 --- /dev/null +++ b/po/POTFILES @@ -0,0 +1,2 @@ +src/addtax.c +glade/window_main.glade diff --git a/po/addtax.pot b/po/addtax.pot new file mode 100644 index 0000000..087d781 --- /dev/null +++ b/po/addtax.pot @@ -0,0 +1,39 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the addtax package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: addtax\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-09-13 10:09-0300\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: src/addtax.c:58 src/addtax.c:71 +#, c-format +msgid "Usage: %s [prices...]\n" +msgstr "" + +#: glade/window_main.glade:7 +msgid "Add tax" +msgstr "" + +#: glade/window_main.glade:32 +msgid "Price" +msgstr "" + +#: glade/window_main.glade:57 +msgid "Calculate" +msgstr "" + +#: glade/window_main.glade:80 +msgid "$0" +msgstr "" diff --git a/po/es.po b/po/es.po new file mode 100644 index 0000000..e063500 --- /dev/null +++ b/po/es.po @@ -0,0 +1,40 @@ +# Spanish translations for addtax package. +# Copyright (C) 2018 THE addtax'S COPYRIGHT HOLDER +# This file is distributed under the same license as the addtax package. +# Automatically generated, 2018. +# +msgid "" +msgstr "" +"Project-Id-Version: addtax\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2018-09-13 10:09-0300\n" +"PO-Revision-Date: 2018-09-13 10:10-0300\n" +"Last-Translator: Chris Cromer \n" +"Language-Team: none\n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=ASCII\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Generator: Poedit 2.0.6\n" + +#: src/addtax.c:58 src/addtax.c:71 +#, c-format +msgid "Usage: %s [prices...]\n" +msgstr "Uso: %s [precios...]\n" + +#: glade/window_main.glade:7 +msgid "Add tax" +msgstr "Agregar impuestos" + +#: glade/window_main.glade:32 +msgid "Price" +msgstr "Precio" + +#: glade/window_main.glade:57 +msgid "Calculate" +msgstr "Calcular" + +#: glade/window_main.glade:80 +msgid "$0" +msgstr "" diff --git a/po/meson.build b/po/meson.build new file mode 100644 index 0000000..7ee0972 --- /dev/null +++ b/po/meson.build @@ -0,0 +1,4 @@ +i18n = import('i18n') +i18n.gettext(meson.project_name(), + args: '--directory=' + meson.source_root() +) diff --git a/src/addtax.c b/src/addtax.c new file mode 100644 index 0000000..6c31310 --- /dev/null +++ b/src/addtax.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include "misc.h" + +#define _(STRING) gettext(STRING) +#define TAX 1.111111111 + +GtkLabel *calculated_value = NULL; + +void calculate_tax(GtkWidget *entry) { + char *entry_text; + float money; + const gchar *label_text; + entry_text = (char*) gtk_entry_get_text(GTK_ENTRY(entry)); + money = atof(entry_text); + if (is_float(entry_text, money)) { + g_print("$%d * %f = ", (int) round(money), TAX); + money = money * TAX; + g_print("$%d\n", (int) round(money)); + label_text = g_strconcat((const gchar *) "$", g_strdup_printf("%i", (int) round(money)), NULL); + gtk_label_set_text(calculated_value, label_text); + } +} + +int main(int argc, char *argv[]) { + int opt; + + float money; + + setlocale(LC_ALL, ""); + bindtextdomain("addtax", "/usr/share/locale/"); + textdomain("addtax"); + + if (argc > 1) { + while ((opt = getopt(argc, argv, "")) != -1) { + switch (opt) { + default: + fprintf(stderr, _("Usage: %s [prices...]\n"), argv[0]); + return 1; + } + } + + for (opt = 1; opt < argc; opt++) { + money = atof(argv[opt]); + if (is_float(argv[opt], money)) { + printf("$%d * %f = ", (int) round(money), TAX); + money = money * TAX; + printf("$%d\n", (int) round(money)); + } + else { + fprintf(stderr, _("Usage: %s [prices...]\n"), argv[0]); + return 1; + } + } + return 0; + } + + GtkBuilder *builder; + GtkWidget *window; + + gtk_init(&argc, &argv); + + builder = gtk_builder_new(); + gtk_builder_add_from_resource(builder, "/cl/cromer/addtax/window_main.glade", NULL); + + window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main")); + gtk_builder_connect_signals(builder, NULL); + + calculated_value = (GtkLabel*) gtk_builder_get_object (builder, "calculated_value"); + + g_object_unref(builder); + + gtk_widget_show(window); + gtk_main(); + + return 0; +} + +// Called when calculate is clicked +void on_button_calculate_clicked(GtkWidget *button, GtkWidget *entry) { + calculate_tax(entry); +} + +// Called when the user presses enter on the entry +void on_entry_price_activate(GtkWidget *entry) { + calculate_tax(entry); +} + +// Called when window is closed +void on_window_main_destroy() { + gtk_main_quit(); +} diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 0000000..b707d0e --- /dev/null +++ b/src/meson.build @@ -0,0 +1,23 @@ +gtk_dep = dependency('gtk+-3.0', version : '>=3.0.0') +# gmodule-export-2.0 is needed to connect the handlers +gmodule_dep = dependency('gmodule-export-2.0', version : '>=2.0') + +# Check for the math library +cc = meson.get_compiler('c') +m_dep = cc.find_library('m', required : false) + +# -no-pie so that the executable is double clickable in gui +c_args = ['-no-pie'] +link_args = ['-no-pie'] + +sources = ['addtax.c'] +sources += ['misc.c'] +sources += ['misc.h'] +sources += [main_resource_c] + +executable('addtax', + sources, + c_args : c_args, + link_args : link_args, + dependencies : [gtk_dep, m_dep, gmodule_dep], + install : true) diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 0000000..a9a8243 --- /dev/null +++ b/src/misc.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +bool is_float(const char *s, float dest) { + if (s == NULL) { + return false; + } + char *endptr; + dest = (float) strtod(s, &endptr); + if (s == endptr) { + return false; + } + while (isspace((unsigned char ) *endptr)) { + endptr++; + } + return *endptr == '\0'; +} diff --git a/src/misc.h b/src/misc.h new file mode 100644 index 0000000..cf81e48 --- /dev/null +++ b/src/misc.h @@ -0,0 +1 @@ +bool is_float(const char *s, float dest);