Browse Source

initial commit

Chris Cromer 2 months ago
commit
d34bd8f72e
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
14 changed files with 350 additions and 0 deletions
  1. 11
    0
      LICENSE
  2. 6
    0
      glade/main_resource.xml
  3. 8
    0
      glade/meson.build
  4. 91
    0
      glade/window_main.glade
  5. 10
    0
      meson.build
  6. 1
    0
      po/LINGUAS
  7. 2
    0
      po/POTFILES
  8. 39
    0
      po/addtax.pot
  9. 40
    0
      po/es.po
  10. 4
    0
      po/meson.build
  11. 95
    0
      src/addtax.c
  12. 23
    0
      src/meson.build
  13. 19
    0
      src/misc.c
  14. 1
    0
      src/misc.h

+ 11
- 0
LICENSE View File

@@ -0,0 +1,11 @@
1
+Copyright 2018 Chris Cromer
2
+
3
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+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.
8
+
9
+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.
10
+
11
+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
- 0
glade/main_resource.xml View File

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

+ 8
- 0
glade/meson.build View File

@@ -0,0 +1,8 @@
1
+gen = generator(
2
+	find_program('glib-compile-resources'),
3
+	arguments: [ '--sourcedir=@SOURCE_DIR@/glade', 
4
+		'--generate-source',
5
+		'--target=@OUTPUT@',
6
+		'@INPUT@'],
7
+	output: '@PLAINNAME@.c')
8
+main_resource_c = gen.process(['main_resource.xml'])

+ 91
- 0
glade/window_main.glade View File

@@ -0,0 +1,91 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!-- Generated with glade 3.22.1 -->
3
+<interface domain="cl.cromer.addtax">
4
+  <requires lib="gtk+" version="3.10"/>
5
+  <object class="GtkWindow" id="window_main">
6
+    <property name="can_focus">False</property>
7
+    <property name="title" translatable="yes">Add tax</property>
8
+    <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
9
+    <child>
10
+      <placeholder/>
11
+    </child>
12
+    <child>
13
+      <object class="GtkBox">
14
+        <property name="visible">True</property>
15
+        <property name="can_focus">False</property>
16
+        <property name="margin_left">12</property>
17
+        <property name="margin_right">12</property>
18
+        <property name="margin_top">12</property>
19
+        <property name="margin_bottom">12</property>
20
+        <property name="orientation">vertical</property>
21
+        <property name="spacing">12</property>
22
+        <property name="baseline_position">top</property>
23
+        <child>
24
+          <object class="GtkBox">
25
+            <property name="visible">True</property>
26
+            <property name="can_focus">False</property>
27
+            <property name="spacing">12</property>
28
+            <child>
29
+              <object class="GtkLabel">
30
+                <property name="visible">True</property>
31
+                <property name="can_focus">False</property>
32
+                <property name="label" translatable="yes">Price</property>
33
+                <attributes>
34
+                  <attribute name="font-desc" value="Sans Bold 10"/>
35
+                </attributes>
36
+              </object>
37
+              <packing>
38
+                <property name="expand">False</property>
39
+                <property name="fill">True</property>
40
+                <property name="position">0</property>
41
+              </packing>
42
+            </child>
43
+            <child>
44
+              <object class="GtkEntry" id="entry_price">
45
+                <property name="visible">True</property>
46
+                <property name="can_focus">True</property>
47
+                <signal name="activate" handler="on_entry_price_activate" swapped="no"/>
48
+              </object>
49
+              <packing>
50
+                <property name="expand">True</property>
51
+                <property name="fill">True</property>
52
+                <property name="position">1</property>
53
+              </packing>
54
+            </child>
55
+            <child>
56
+              <object class="GtkButton" id="button_calculate">
57
+                <property name="label" translatable="yes">Calculate</property>
58
+                <property name="visible">True</property>
59
+                <property name="can_focus">True</property>
60
+                <property name="receives_default">True</property>
61
+                <signal name="clicked" handler="on_button_calculate_clicked" object="entry_price" swapped="no"/>
62
+              </object>
63
+              <packing>
64
+                <property name="expand">False</property>
65
+                <property name="fill">True</property>
66
+                <property name="position">2</property>
67
+              </packing>
68
+            </child>
69
+          </object>
70
+          <packing>
71
+            <property name="expand">False</property>
72
+            <property name="fill">True</property>
73
+            <property name="position">0</property>
74
+          </packing>
75
+        </child>
76
+        <child>
77
+          <object class="GtkLabel" id="calculated_value">
78
+            <property name="visible">True</property>
79
+            <property name="can_focus">False</property>
80
+            <property name="label" translatable="yes">$0</property>
81
+          </object>
82
+          <packing>
83
+            <property name="expand">True</property>
84
+            <property name="fill">True</property>
85
+            <property name="position">1</property>
86
+          </packing>
87
+        </child>
88
+      </object>
89
+    </child>
90
+  </object>
91
+</interface>

+ 10
- 0
meson.build View File

@@ -0,0 +1,10 @@
1
+project('addtax',
2
+	'c',
3
+	version : '1.0.0',
4
+	license : 'BSD-3',
5
+	default_options : [ 'b_ofast=if-release', 'b_march_native=if-release', 'b_ndebug=if-release' ]
6
+)
7
+add_global_arguments('-DGETTEXT_PACKAGE="addtax"', language: 'c')
8
+subdir('po')
9
+subdir('glade')
10
+subdir('src')

+ 1
- 0
po/LINGUAS View File

@@ -0,0 +1 @@
1
+es

+ 2
- 0
po/POTFILES View File

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

+ 39
- 0
po/addtax.pot View File

@@ -0,0 +1,39 @@
1
+# SOME DESCRIPTIVE TITLE.
2
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3
+# This file is distributed under the same license as the addtax package.
4
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5
+#
6
+#, fuzzy
7
+msgid ""
8
+msgstr ""
9
+"Project-Id-Version: addtax\n"
10
+"Report-Msgid-Bugs-To: \n"
11
+"POT-Creation-Date: 2018-09-13 10:09-0300\n"
12
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
+"Language-Team: LANGUAGE <LL@li.org>\n"
15
+"Language: \n"
16
+"MIME-Version: 1.0\n"
17
+"Content-Type: text/plain; charset=CHARSET\n"
18
+"Content-Transfer-Encoding: 8bit\n"
19
+
20
+#: src/addtax.c:58 src/addtax.c:71
21
+#, c-format
22
+msgid "Usage: %s [prices...]\n"
23
+msgstr ""
24
+
25
+#: glade/window_main.glade:7
26
+msgid "Add tax"
27
+msgstr ""
28
+
29
+#: glade/window_main.glade:32
30
+msgid "Price"
31
+msgstr ""
32
+
33
+#: glade/window_main.glade:57
34
+msgid "Calculate"
35
+msgstr ""
36
+
37
+#: glade/window_main.glade:80
38
+msgid "$0"
39
+msgstr ""

+ 40
- 0
po/es.po View File

@@ -0,0 +1,40 @@
1
+# Spanish translations for addtax package.
2
+# Copyright (C) 2018 THE addtax'S COPYRIGHT HOLDER
3
+# This file is distributed under the same license as the addtax package.
4
+# Automatically generated, 2018.
5
+#
6
+msgid ""
7
+msgstr ""
8
+"Project-Id-Version: addtax\n"
9
+"Report-Msgid-Bugs-To: \n"
10
+"POT-Creation-Date: 2018-09-13 10:09-0300\n"
11
+"PO-Revision-Date: 2018-09-13 10:10-0300\n"
12
+"Last-Translator: Chris Cromer <chris@cromer.cl>\n"
13
+"Language-Team: none\n"
14
+"Language: es\n"
15
+"MIME-Version: 1.0\n"
16
+"Content-Type: text/plain; charset=ASCII\n"
17
+"Content-Transfer-Encoding: 8bit\n"
18
+"Plural-Forms: nplurals=2; plural=(n != 1);\n"
19
+"X-Generator: Poedit 2.0.6\n"
20
+
21
+#: src/addtax.c:58 src/addtax.c:71
22
+#, c-format
23
+msgid "Usage: %s [prices...]\n"
24
+msgstr "Uso: %s [precios...]\n"
25
+
26
+#: glade/window_main.glade:7
27
+msgid "Add tax"
28
+msgstr "Agregar impuestos"
29
+
30
+#: glade/window_main.glade:32
31
+msgid "Price"
32
+msgstr "Precio"
33
+
34
+#: glade/window_main.glade:57
35
+msgid "Calculate"
36
+msgstr "Calcular"
37
+
38
+#: glade/window_main.glade:80
39
+msgid "$0"
40
+msgstr ""

+ 4
- 0
po/meson.build View File

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

+ 95
- 0
src/addtax.c View File

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

+ 23
- 0
src/meson.build View File

@@ -0,0 +1,23 @@
1
+gtk_dep = dependency('gtk+-3.0', version : '>=3.0.0')
2
+# gmodule-export-2.0 is needed to connect the handlers
3
+gmodule_dep = dependency('gmodule-export-2.0', version : '>=2.0')
4
+
5
+# Check for the math library
6
+cc = meson.get_compiler('c')
7
+m_dep = cc.find_library('m', required : false)
8
+
9
+# -no-pie so that the executable is double clickable in gui
10
+c_args = ['-no-pie']
11
+link_args = ['-no-pie']
12
+
13
+sources = ['addtax.c']
14
+sources += ['misc.c']
15
+sources += ['misc.h']
16
+sources += [main_resource_c]
17
+
18
+executable('addtax',
19
+	sources,
20
+	c_args : c_args,
21
+	link_args : link_args,
22
+	dependencies : [gtk_dep, m_dep, gmodule_dep],
23
+	install : true)

+ 19
- 0
src/misc.c View File

@@ -0,0 +1,19 @@
1
+#include <stdbool.h>
2
+#include <unistd.h>
3
+#include <ctype.h>
4
+#include <stdlib.h>
5
+
6
+bool is_float(const char *s, float dest) {
7
+	if (s == NULL) {
8
+		return false;
9
+	}
10
+	char *endptr;
11
+	dest = (float) strtod(s, &endptr);
12
+	if (s == endptr) {
13
+		return false;
14
+	}
15
+	while (isspace((unsigned char ) *endptr)) {
16
+		endptr++;
17
+	}
18
+	return *endptr == '\0';
19
+}

+ 1
- 0
src/misc.h View File

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

Loading…
Cancel
Save