1 /**
2  * @file   gui-import.c
3  * @brief
4  *
5  * Copyright (C) 2009 Gummi Developers
6  * All Rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following
15  * conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include "gui-import.h"
31 
32 #include <string.h>
33 
34 #include <glib.h>
35 #include <gtk/gtk.h>
36 
37 #include "environment.h"
38 #include "utils.h"
39 #include "gui/gui-main.h"
40 
41 extern Gummi* gummi;
42 extern GummiGui* gui;
43 
importgui_init(GtkBuilder * builder)44 GuImportGui* importgui_init (GtkBuilder* builder) {
45     g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
46 
47     GuImportGui* i = g_new0 (GuImportGui, 1);
48 
49     i->import_panel =
50         GTK_BOX (gtk_builder_get_object (builder, "import_panel"));
51 
52     i->image_pane =
53         GTK_VIEWPORT (gtk_builder_get_object (builder, "imp_pane_image"));
54     i->table_pane =
55         GTK_VIEWPORT (gtk_builder_get_object (builder, "imp_pane_table"));
56     i->matrix_pane =
57         GTK_VIEWPORT (gtk_builder_get_object (builder, "imp_pane_matrix"));
58     i->biblio_pane =
59         GTK_VIEWPORT (gtk_builder_get_object (builder, "imp_pane_biblio"));
60 
61     i->image_file =
62         GTK_ENTRY (gtk_builder_get_object (builder, "image_file"));
63     i->image_caption =
64         GTK_ENTRY (gtk_builder_get_object (builder, "image_caption"));
65     i->image_label =
66         GTK_ENTRY (gtk_builder_get_object (builder, "image_label"));
67     i->image_scale =
68         GTK_SPIN_BUTTON (gtk_builder_get_object (builder, "image_scale"));
69     i->scaler =
70         GTK_ADJUSTMENT (gtk_builder_get_object (builder, "image_scaler"));
71 
72     i->table_comboalign =
73         GTK_COMBO_BOX (gtk_builder_get_object (builder, "table_comboalign"));
74     i->table_comboborder =
75         GTK_COMBO_BOX (gtk_builder_get_object (builder, "table_comboborder"));
76     i->table_rows =
77         GTK_ADJUSTMENT (gtk_builder_get_object (builder, "table_rows"));
78     i->table_cols =
79         GTK_ADJUSTMENT (gtk_builder_get_object (builder, "table_cols"));
80 
81     i->matrix_rows =
82         GTK_ADJUSTMENT (gtk_builder_get_object (builder, "matrix_rows"));
83     i->matrix_cols =
84         GTK_ADJUSTMENT (gtk_builder_get_object (builder, "matrix_cols"));
85     i->matrix_combobracket =
86         GTK_COMBO_BOX (gtk_builder_get_object (builder,"matrix_combobracket"));
87 
88     i->biblio_file =
89          GTK_ENTRY (gtk_builder_get_object (builder, "biblio_file"));
90 
91     gtk_adjustment_set_value (i->table_cols, 3);
92     gtk_adjustment_set_value (i->table_rows, 3);
93     gtk_adjustment_set_value (i->matrix_cols, 3);
94     gtk_adjustment_set_value (i->matrix_rows, 3);
95     return i;
96 }
97 
importgui_remove_all_panels()98 void importgui_remove_all_panels () {
99     GList* list = NULL;
100 
101     list = gtk_container_get_children (
102             GTK_CONTAINER (g_importgui->import_panel));
103     while (list) {
104         gtk_container_remove (GTK_CONTAINER (g_importgui->import_panel),
105                 GTK_WIDGET (list->data));
106         list = list->next;
107     }
108 }
109 
110 G_MODULE_EXPORT
on_imp_panel_image_clicked(GtkWidget * widget,void * user)111 void on_imp_panel_image_clicked (GtkWidget* widget, void* user) {
112     importgui_remove_all_panels ();
113     gtk_container_add (GTK_CONTAINER (g_importgui->import_panel),
114                     GTK_WIDGET (g_importgui->image_pane));
115 }
116 
117 G_MODULE_EXPORT
on_imp_panel_table_clicked(GtkWidget * widget,void * user)118 void on_imp_panel_table_clicked (GtkWidget* widget, void* user) {
119     importgui_remove_all_panels ();
120     gtk_container_add (GTK_CONTAINER (g_importgui->import_panel),
121                     GTK_WIDGET (g_importgui->table_pane));
122 }
123 
124 G_MODULE_EXPORT
on_imp_panel_matrix_clicked(GtkWidget * widget,void * user)125 void on_imp_panel_matrix_clicked (GtkWidget* widget, void* user) {
126     importgui_remove_all_panels ();
127     gtk_container_add (GTK_CONTAINER (g_importgui->import_panel),
128                     GTK_WIDGET (g_importgui->matrix_pane));
129 }
130 
131 G_MODULE_EXPORT
on_imp_panel_biblio_clicked(GtkWidget * widget,void * user)132 void on_imp_panel_biblio_clicked (GtkWidget* widget, void* user) {
133     importgui_remove_all_panels ();
134     gtk_container_add (GTK_CONTAINER (g_importgui->import_panel),
135                     GTK_WIDGET (g_importgui->biblio_pane));
136 }
137 
138 G_MODULE_EXPORT
on_imp_minimize_clicked(GtkWidget * widget,void * user)139 void on_imp_minimize_clicked (GtkWidget* widget, void* user) {
140     importgui_remove_all_panels ();
141 }
142 
143 G_MODULE_EXPORT
on_import_table_apply_clicked(GtkWidget * widget,void * user)144 void on_import_table_apply_clicked (GtkWidget* widget, void* user) {
145     GtkTextIter current;
146     gint rows = gtk_adjustment_get_value (g_importgui->table_rows);
147     gint cols = gtk_adjustment_get_value (g_importgui->table_cols);
148     gint border = gtk_combo_box_get_active (g_importgui->table_comboborder);
149     gint align = gtk_combo_box_get_active (g_importgui->table_comboalign);
150     const gchar* text = importer_generate_table (rows, cols, border, align);
151 
152     editor_get_current_iter (g_active_editor, &current);
153     gtk_text_buffer_begin_user_action (g_e_buffer);
154     gtk_text_buffer_insert (g_e_buffer, &current, text, strlen (text));
155     gtk_text_buffer_end_user_action (g_e_buffer);
156     gtk_text_buffer_set_modified (g_e_buffer, TRUE);
157     importgui_remove_all_panels ();
158 }
159 
160 G_MODULE_EXPORT
on_import_image_apply_clicked(GtkWidget * widget,void * user)161 void on_import_image_apply_clicked (GtkWidget* widget, void* user) {
162     GtkTextIter current;
163     const gchar* imagefile = gtk_entry_get_text (g_importgui->image_file);
164     const gchar* caption = gtk_entry_get_text (g_importgui->image_caption);
165     const gchar* label = gtk_entry_get_text (g_importgui->image_label);
166     gdouble scale = gtk_adjustment_get_value (g_importgui->scaler);
167     gchar* root_path = NULL;
168     gchar* relative_path = NULL;
169     const gchar* text = 0;
170 
171     if (0 != strlen (imagefile)) {
172         if (!utils_path_exists (imagefile)) {
173             slog (L_G_ERROR, _("%s: No such file or directory\n"), imagefile);
174         } else {
175             if (g_active_editor->filename)
176                 root_path = g_path_get_dirname (g_active_editor->filename);
177             relative_path = utils_path_to_relative (root_path, imagefile);
178             text =importer_generate_image(relative_path, caption, label, scale);
179             editor_insert_package (g_active_editor, "graphicx", NULL);
180             editor_get_current_iter (g_active_editor, &current);
181             gtk_text_buffer_begin_user_action (g_e_buffer);
182             gtk_text_buffer_insert (g_e_buffer, &current,text,strlen (text));
183             gtk_text_buffer_end_user_action (g_e_buffer);
184             gtk_text_buffer_set_modified (g_e_buffer, TRUE);
185             importer_imagegui_set_sensitive ("", FALSE);
186         }
187     }
188     importgui_remove_all_panels ();
189     g_free (relative_path);
190     g_free (root_path);
191 }
192 
193 G_MODULE_EXPORT
on_import_matrix_apply_clicked(GtkWidget * widget,void * user)194 void on_import_matrix_apply_clicked (GtkWidget* widget, void* user) {
195     GtkTextIter current;
196     gint bracket =
197         gtk_combo_box_get_active (g_importgui->matrix_combobracket);
198     gint rows = gtk_adjustment_get_value (g_importgui->matrix_rows);
199     gint cols = gtk_adjustment_get_value (g_importgui->matrix_cols);
200     const gchar* text = importer_generate_matrix (bracket, rows, cols);
201     editor_insert_package (g_active_editor, "amsmath", NULL);
202     editor_get_current_iter (g_active_editor, &current);
203     gtk_text_buffer_begin_user_action (g_e_buffer);
204     gtk_text_buffer_insert (g_e_buffer, &current, text, strlen (text));
205     gtk_text_buffer_end_user_action (g_e_buffer);
206     gtk_text_buffer_set_modified (g_e_buffer, TRUE);
207     importgui_remove_all_panels ();
208 }
209 
210 G_MODULE_EXPORT
on_import_biblio_apply_clicked(GtkWidget * widget,void * user)211 void on_import_biblio_apply_clicked (GtkWidget* widget, void* user) {
212     gchar* basename = NULL;
213     gchar* root_path = NULL;
214     gchar* relative_path = NULL;
215 
216     const gchar* filename = gtk_entry_get_text (g_importgui->biblio_file);
217 
218     if ((filename) && (strlen(filename) != 0)) {
219         if (g_active_editor->filename)
220             root_path = g_path_get_dirname (g_active_editor->filename);
221         relative_path = utils_path_to_relative (root_path, filename);
222         editor_insert_bib (g_active_editor, relative_path);
223         basename = g_path_get_basename (filename);
224         gtk_label_set_text (gummi->biblio->filenm_label, basename);
225         g_free (relative_path);
226         g_free (root_path);
227         g_free (basename);
228         gtk_entry_set_text (g_importgui->biblio_file, "");
229     }
230     importgui_remove_all_panels ();
231 }
232 
233 G_MODULE_EXPORT
on_image_file_activate(void)234 void on_image_file_activate (void) {
235     gchar* filename = NULL;
236 
237     filename = get_open_filename (TYPE_IMAGE);
238     if (filename) {
239         importer_imagegui_set_sensitive (filename, TRUE);
240     }
241     g_free (filename);
242 }
243 
244 G_MODULE_EXPORT
on_biblio_file_activate(GtkWidget * widget,void * user)245 void on_biblio_file_activate (GtkWidget *widget, void * user) {
246     gchar* filename = NULL;
247 
248     filename = get_open_filename (TYPE_BIBLIO);
249     if (filename) {
250         gtk_entry_set_text (g_importgui->biblio_file, filename);
251     }
252     g_free (filename);
253 }
254 
255 G_MODULE_EXPORT
importer_imagegui_set_sensitive(const gchar * name,gboolean mode)256 void importer_imagegui_set_sensitive (const gchar* name, gboolean mode) {
257     gtk_widget_set_sensitive (GTK_WIDGET (g_importgui->image_label), mode);
258     gtk_widget_set_sensitive (GTK_WIDGET (g_importgui->image_caption), mode);
259     gtk_widget_set_sensitive (GTK_WIDGET (g_importgui->image_scale), mode);
260     gtk_entry_set_text (g_importgui->image_file, name);
261     gtk_entry_set_text (g_importgui->image_label, "");
262     gtk_entry_set_text (g_importgui->image_caption, "");
263     gtk_adjustment_set_value (g_importgui->scaler, 1.00);
264 }
265