]> git.sur5r.net Git - ptouch-print/blob - src/ptouch-gtk.c
Initial commit
[ptouch-print] / src / ptouch-gtk.c
1 /*
2         ptouch-gtk - Simple GTK+ UI to print labels on a Brother P-Touch
3         
4         Copyright (C) 2015 Dominic Radermacher <dominic.radermacher@gmail.com>
5
6         This program is free software; you can redistribute it and/or modify it
7         under the terms of the GNU General Public License version 3 as
8         published by the Free Software Foundation
9         
10         This program is distributed in the hope that it will be useful, but
11         WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13         See the GNU General Public License for more details.
14         
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software Foundation,
17         Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <gtk/gtk.h>
21
22 #define BUILDER_XML_FILE "data/ptouch.ui"
23
24 typedef struct
25 {
26         GtkWidget       *window;
27         GtkWidget       *statusbar;
28         GtkWidget       *text_view;
29         guint           statusbar_context_id;
30 } PTouchEditor;
31
32 /* prototypes */
33 void error_message(const gchar *message);
34 void on_window_destroy(GtkWidget *object, PTouchEditor *editor);
35 gboolean on_window_delete_event(GtkWidget *widget, GdkEvent *event,
36         PTouchEditor *editor);
37 gboolean init_app(PTouchEditor *editor);
38
39 void error_message(const gchar *message)
40 {
41         GtkWidget *dialog;
42         g_warning(message);             /* log to terminal window */
43         /* create an error message dialog and display modally to the user */
44         dialog = gtk_message_dialog_new(NULL,
45                 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
46                 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, message);
47         gtk_window_set_title(GTK_WINDOW(dialog), "Error!");
48         gtk_dialog_run(GTK_DIALOG(dialog));
49         gtk_widget_destroy(dialog);
50 }
51
52 void on_window_destroy(GtkWidget *object, PTouchEditor *editor)
53 {
54         gtk_main_quit();
55 }
56
57 gboolean on_window_delete_event(GtkWidget *widget, GdkEvent *event, PTouchEditor *editor)
58 {
59         return FALSE;   /* propogate event */
60 }
61
62 void show_about(PTouchEditor *editor)
63 {
64         static const gchar * const authors[] = {
65                 "Dominic Radermacher <dominic.radermacher@gmail.com>",
66                 NULL
67         };
68         static const gchar copyright[] = "Copyright \xc2\xa9 2015 Dominic Radermacher";
69         static const gchar comments[] = "PTouch Print";
70
71         gtk_show_about_dialog(GTK_WINDOW(editor->window), "authors", authors,
72                 "comments", comments, "copyright", copyright,
73                 "version", "0.1",
74                 "website", "http://mockmoon-cybernetics.ch/",
75                 "program-name", "ptouch-gtk",
76                 "logo-icon-name", GTK_STOCK_EDIT, NULL);
77 }
78
79 gboolean init_app(PTouchEditor *editor)
80 {
81         GtkBuilder *builder;
82         GError *err=NULL;      
83         guint id;
84
85         /* use GtkBuilder to build our interface from the XML file */
86         builder = gtk_builder_new();
87         if (gtk_builder_add_from_file(builder, BUILDER_XML_FILE, &err) == 0) {
88                 error_message(err->message);
89                 g_error_free(err);
90                 return FALSE;
91         }
92         /* get the widgets which will be referenced in callbacks */
93         editor->window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
94         editor->statusbar = GTK_WIDGET(gtk_builder_get_object(builder, "statusbar"));
95         editor->text_view = GTK_WIDGET(gtk_builder_get_object(builder, "text_view"));
96         gtk_builder_connect_signals(builder, editor);
97         /* free memory used by GtkBuilder object */
98         g_object_unref(G_OBJECT(builder));
99         gtk_window_set_default_icon_name(GTK_STOCK_EDIT);
100         /* setup and initialize our statusbar */
101         id = gtk_statusbar_get_context_id(GTK_STATUSBAR(editor->statusbar),
102                 "PTouch Print GTK+");
103         editor->statusbar_context_id = id;
104         return TRUE;
105 }
106
107 int main(int argc, char *argv[])
108 {
109         PTouchEditor *editor;
110
111         editor = g_slice_new(PTouchEditor);
112         gtk_init(&argc, &argv);
113         if (init_app(editor) == FALSE) {
114                 return 1;       /* error loading UI */
115         }
116         gtk_widget_show(editor->window);
117         gtk_main();
118         g_slice_free(PTouchEditor, editor);
119 }