initial commit

This commit is contained in:
Chris Cromer 2018-09-13 16:25:44 -03:00
commit d34bd8f72e
Signed by: cromer
GPG Key ID: 39CC813FF3C8708A
14 changed files with 350 additions and 0 deletions

11
LICENSE Normal file
View File

@ -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.

6
glade/main_resource.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/cl/cromer/addtax">
<file preprocess="xml-stripblanks">window_main.glade</file>
</gresource>
</gresources>

8
glade/meson.build Normal file
View File

@ -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'])

91
glade/window_main.glade Normal file
View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface domain="cl.cromer.addtax">
<requires lib="gtk+" version="3.10"/>
<object class="GtkWindow" id="window_main">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Add tax</property>
<signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">12</property>
<property name="margin_right">12</property>
<property name="margin_top">12</property>
<property name="margin_bottom">12</property>
<property name="orientation">vertical</property>
<property name="spacing">12</property>
<property name="baseline_position">top</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">12</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Price</property>
<attributes>
<attribute name="font-desc" value="Sans Bold 10"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry_price">
<property name="visible">True</property>
<property name="can_focus">True</property>
<signal name="activate" handler="on_entry_price_activate" swapped="no"/>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button_calculate">
<property name="label" translatable="yes">Calculate</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_button_calculate_clicked" object="entry_price" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="calculated_value">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">$0</property>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>

10
meson.build Normal file
View File

@ -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')

1
po/LINGUAS Normal file
View File

@ -0,0 +1 @@
es

2
po/POTFILES Normal file
View File

@ -0,0 +1,2 @@
src/addtax.c
glade/window_main.glade

39
po/addtax.pot Normal file
View File

@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 ""

40
po/es.po Normal file
View File

@ -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 <chris@cromer.cl>\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 ""

4
po/meson.build Normal file
View File

@ -0,0 +1,4 @@
i18n = import('i18n')
i18n.gettext(meson.project_name(),
args: '--directory=' + meson.source_root()
)

95
src/addtax.c Normal file
View File

@ -0,0 +1,95 @@
#include <gtk/gtk.h>
#include <math.h>
#include <libintl.h>
#include <locale.h>
#include <stdbool.h>
#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();
}

23
src/meson.build Normal file
View File

@ -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)

19
src/misc.c Normal file
View File

@ -0,0 +1,19 @@
#include <stdbool.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
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';
}

1
src/misc.h Normal file
View File

@ -0,0 +1 @@
bool is_float(const char *s, float dest);