~ubuntu-branches/debian/squeeze/geany-plugins/squeeze

« back to all changes in this revision

Viewing changes to geanylatex/src/geanylatex.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-07-10 22:56:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090710225641-xc1126t7pq0jmpos
Tags: upstream-0.17.1
ImportĀ upstreamĀ versionĀ 0.17.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      geanylatex.c - Plugin to let Geany better work together with LaTeX
 
3
 *
 
4
 *      Copyright 2007-2009 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
 
5
 *      For long list of friendly supporters please have a look at THANKS.
 
6
 *
 
7
 *      This program is free software; you can redistribute it and/or modify
 
8
 *      it under the terms of the GNU General Public License as published by
 
9
 *      the Free Software Foundation; either version 2 of the License, or
 
10
 *      (at your option) any later version.
 
11
 *
 
12
 *      This program is distributed in the hope that it will be useful,
 
13
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *      GNU General Public License for more details.
 
16
 *
 
17
 *      You should have received a copy of the GNU General Public License
 
18
 *      along with this program; if not, write to the Free Software
 
19
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
20
 */
 
21
 
 
22
/* LaTeX plugin */
 
23
/* This plugin improves the work with LaTeX and Geany.*/
 
24
 
 
25
#include "geanylatex.h"
 
26
 
 
27
PLUGIN_VERSION_CHECK(136)
 
28
 
 
29
PLUGIN_SET_INFO(_("LaTeX"), _("Plugin to provide better LaTeX support"),
 
30
        "0.4", "Frank Lanitz <frank@frank.uvena.de>")
 
31
 
 
32
GeanyPlugin             *geany_plugin;
 
33
GeanyData               *geany_data;
 
34
GeanyFunctions  *geany_functions;
 
35
 
 
36
 
 
37
static GtkWidget *menu_latex = NULL;
 
38
static GtkWidget *menu_latex_menu = NULL;
 
39
static GtkWidget *menu_latex_wizzard = NULL;
 
40
static GtkWidget *menu_latex_menu_special_char = NULL;
 
41
static GtkWidget *menu_latex_menu_special_char_submenu = NULL;
 
42
static GtkWidget *menu_latex_ref = NULL;
 
43
static GtkWidget *menu_latex_label = NULL;
 
44
static GtkWidget *menu_latex_bibtex = NULL;
 
45
static GtkWidget *menu_latex_bibtex_submenu = NULL;
 
46
static GtkWidget *menu_latex_format_insert = NULL;
 
47
static GtkWidget *menu_latex_format_insert_submenu = NULL;
 
48
static GtkWidget *menu_latex_insert_environment = NULL;
 
49
static GtkWidget *menu_latex_replacement = NULL;
 
50
static GtkWidget *menu_latex_replacement_submenu = NULL;
 
51
static GtkWidget *menu_latex_replace_selection = NULL;
 
52
static GtkWidget *menu_latex_replace_toggle = NULL;
 
53
 
 
54
/* Options for plugin */
 
55
static gboolean glatex_set_koma_active = FALSE;
 
56
static gboolean glatex_set_toolbar_active = FALSE;
 
57
 
 
58
/* Function will be deactivated, when only loaded */
 
59
static gboolean toggle_active = FALSE;
 
60
 
 
61
static GtkWidget *main_menu_item = NULL;
 
62
 
 
63
static GtkUIManager *uim;
 
64
static GtkActionGroup *group;
 
65
static GtkWidget *glatex_toolbar = NULL;
 
66
static GtkWidget *box = NULL;
 
67
 
 
68
/* Configuration file */
 
69
static gchar *config_file = NULL;
 
70
 
 
71
/* Doing some basic keybinding stuff */
 
72
enum
 
73
{
 
74
        KB_LATEX_WIZZARD,
 
75
        KB_LATEX_INSERT_LABEL,
 
76
        KB_LATEX_INSERT_REF,
 
77
        KB_LATEX_INSERT_NEWLINE,
 
78
        KB_LATEX_TOGGLE_ACTIVE,
 
79
        KB_LATEX_ENVIRONMENT_INSERT,
 
80
        KB_LATEX_INSERT_NEWITEM,
 
81
        KB_LATEX_REPLACE_SPECIAL_CHARS,
 
82
        KB_LATEX_FORMAT_BOLD,
 
83
        KB_LATEX_FORMAT_ITALIC,
 
84
        KB_LATEX_FORMAT_TYPEWRITER,
 
85
        KB_LATEX_FORMAT_CENTER,
 
86
        KB_LATEX_FORMAT_LEFT,
 
87
        KB_LATEX_FORMAT_RIGHT,
 
88
        COUNT_KB
 
89
};
 
90
 
 
91
PLUGIN_KEY_GROUP(geanylatex, COUNT_KB)
 
92
 
 
93
const GtkActionEntry format_icons[] =
 
94
{
 
95
        { "Italic", GTK_STOCK_ITALIC, NULL, NULL, N_("Marks selected text as italic"), G_CALLBACK(glatex_kb_format_italic) },
 
96
        { "Bold", GTK_STOCK_BOLD, NULL, NULL, N_("Marks selected text as bold"), G_CALLBACK(glatex_kb_format_bold) },
 
97
        { "Underline", GTK_STOCK_UNDERLINE, NULL, NULL, N_("Underlines selected text"), G_CALLBACK(glatex_kb_format_typewriter) },
 
98
        { "Centered", GTK_STOCK_JUSTIFY_CENTER, NULL, NULL, NULL, G_CALLBACK(glatex_kb_format_centering) },
 
99
        { "Left", GTK_STOCK_JUSTIFY_LEFT, NULL, NULL, NULL, G_CALLBACK(glatex_kb_format_left) },
 
100
        { "Right", GTK_STOCK_JUSTIFY_RIGHT, NULL, NULL, NULL, G_CALLBACK(glatex_kb_format_right) },
 
101
};
 
102
 
 
103
const guint ui_entries_n = G_N_ELEMENTS(format_icons);
 
104
 
 
105
 
 
106
/* fallback UI definition */
 
107
const gchar *toolbar_markup =
 
108
"<ui>"
 
109
        "<toolbar name='glatex_format_toolbar'>"
 
110
        "<toolitem action='Italic'/>"
 
111
        "<toolitem action='Bold'/>"
 
112
        "<toolitem action='Underline'/>"
 
113
        "<separator/>"
 
114
        "<toolitem action='Centered' />"
 
115
        "<toolitem action='Left' />"
 
116
        "<toolitem action='Right'/>"
 
117
        "</toolbar>"
 
118
"</ui>";
 
119
 
 
120
static struct
 
121
{
 
122
        GtkWidget *koma_active;
 
123
        GtkWidget *toolbar_active;
 
124
}
 
125
config_widgets;
 
126
 
 
127
 
 
128
static GtkWidget *init_toolbar()
 
129
{
 
130
        GtkWidget *toolbar = NULL;
 
131
 
 
132
        box = ui_lookup_widget(geany->main_widgets->window, "vbox1");
 
133
        uim = gtk_ui_manager_new();
 
134
        group = gtk_action_group_new("glatex_format_toolbar");
 
135
        gtk_action_group_set_translation_domain(group, GETTEXT_PACKAGE);
 
136
        gtk_action_group_add_actions(group, format_icons, ui_entries_n, NULL);
 
137
        gtk_ui_manager_insert_action_group(uim, group, 0);
 
138
        if (gtk_ui_manager_add_ui_from_string(uim, toolbar_markup, -1, NULL) > 0)
 
139
        {
 
140
                toolbar = gtk_ui_manager_get_widget(uim, "/ui/glatex_format_toolbar");
 
141
                gtk_box_pack_start(GTK_BOX(box), GTK_WIDGET(toolbar), FALSE, TRUE, 0);
 
142
                gtk_box_reorder_child(GTK_BOX(box), toolbar, 2);
 
143
                ui_add_document_sensitive(toolbar);
 
144
        }
 
145
        /* TODO maybe more error handling */
 
146
 
 
147
        return toolbar;
 
148
}
 
149
 
 
150
 
 
151
static void
 
152
on_configure_response(G_GNUC_UNUSED GtkDialog *dialog, gint response,
 
153
                                          G_GNUC_UNUSED gpointer user_data)
 
154
{
 
155
        if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
 
156
        {
 
157
                GKeyFile *config = g_key_file_new();
 
158
                gchar *data;
 
159
                gchar *config_dir = g_path_get_dirname(config_file);
 
160
 
 
161
                config_file = g_strconcat(geany->app->configdir,
 
162
                        G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
 
163
                        "geanyLaTeX", G_DIR_SEPARATOR_S, "general.conf", NULL);
 
164
                glatex_set_koma_active =
 
165
                        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(config_widgets.koma_active));
 
166
                glatex_set_toolbar_active =
 
167
                        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(config_widgets.toolbar_active));
 
168
 
 
169
                /* write stuff to file */
 
170
                g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
 
171
 
 
172
                g_key_file_set_boolean(config, "general", "glatex_set_koma_active",
 
173
                        glatex_set_koma_active);
 
174
                g_key_file_set_boolean(config, "general", "glatex_set_toolbar_active",
 
175
                        glatex_set_toolbar_active);
 
176
 
 
177
                if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
 
178
                    && utils_mkdir(config_dir, TRUE) != 0)
 
179
                {
 
180
                        dialogs_show_msgbox(GTK_MESSAGE_ERROR,
 
181
                                _("Plugin configuration directory could not be created."));
 
182
                }
 
183
                else
 
184
                {
 
185
                        /* write config to file */
 
186
                        data = g_key_file_to_data(config, NULL, NULL);
 
187
                        utils_write_file(config_file, data);
 
188
                        g_free(data);
 
189
                }
 
190
 
 
191
                g_free(config_dir);
 
192
                g_key_file_free(config);
 
193
 
 
194
                /* Apply changes to Geany */
 
195
                /* Add toolbar if requested */
 
196
                if (glatex_set_toolbar_active == TRUE)
 
197
                {
 
198
                        if (glatex_toolbar == NULL)
 
199
                        {
 
200
                                glatex_toolbar = init_toolbar();
 
201
                        }
 
202
                        else
 
203
                        {
 
204
                                gtk_widget_show(glatex_toolbar);
 
205
                        }
 
206
                }
 
207
                /* Hide toolbar */
 
208
                else if (glatex_set_toolbar_active == FALSE && glatex_toolbar != NULL)
 
209
                {
 
210
                        gtk_widget_hide(glatex_toolbar);
 
211
                }
 
212
        }
 
213
}
 
214
 
 
215
GtkWidget *
 
216
plugin_configure(GtkDialog * dialog)
 
217
{
 
218
        GtkWidget       *vbox;
 
219
 
 
220
        vbox = gtk_vbox_new(FALSE, 6);
 
221
 
 
222
        config_widgets.koma_active = gtk_check_button_new_with_label(
 
223
                _("Use KOMA script by default"));
 
224
        config_widgets.toolbar_active = gtk_check_button_new_with_label(
 
225
                _("Show extra plugin toolbar"));
 
226
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config_widgets.koma_active),
 
227
                glatex_set_koma_active);
 
228
        gtk_box_pack_start(GTK_BOX(vbox), config_widgets.koma_active, FALSE, FALSE, 2);
 
229
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(config_widgets.toolbar_active),
 
230
                glatex_set_toolbar_active);
 
231
        gtk_box_pack_start(GTK_BOX(vbox), config_widgets.toolbar_active, FALSE, FALSE, 2);
 
232
 
 
233
        gtk_widget_show_all(vbox);
 
234
        g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), NULL);
 
235
        return vbox;
 
236
}
 
237
 
 
238
/* Functions to toggle the status of plugin */
 
239
void glatex_set_latextoggle_status(gboolean new_status)
 
240
{
 
241
        /* No more function at the moment.*/
 
242
        if (toggle_active != new_status)
 
243
                toggle_active = new_status;
 
244
}
 
245
 
 
246
void glatex_toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)
 
247
{
 
248
        if (toggle_active == TRUE)
 
249
                glatex_set_latextoggle_status(FALSE);
 
250
        else
 
251
                glatex_set_latextoggle_status(TRUE);
 
252
}
 
253
 
 
254
 
 
255
static gboolean ht_editor_notify_cb(G_GNUC_UNUSED GObject *object, GeanyEditor *editor,
 
256
                                                                        SCNotification *nt, G_GNUC_UNUSED gpointer data)
 
257
{
 
258
        g_return_val_if_fail(editor != NULL, FALSE);
 
259
 
 
260
        if (toggle_active != TRUE)
 
261
                return FALSE;
 
262
 
 
263
        if (nt->nmhdr.code == SCN_CHARADDED)
 
264
        {
 
265
                gchar buf[7];
 
266
                gint len;
 
267
 
 
268
                len = g_unichar_to_utf8(nt->ch, buf);
 
269
                if (len > 0)
 
270
                {
 
271
                        const gchar *entity;
 
272
 
 
273
                        buf[len] = '\0';
 
274
                        entity = glatex_get_entity(buf);
 
275
 
 
276
                        if (entity != NULL)
 
277
                        {
 
278
                                gint pos = sci_get_current_position(editor->sci);
 
279
 
 
280
                                sci_set_selection_start(editor->sci, pos - len);
 
281
                                sci_set_selection_end(editor->sci, pos);
 
282
 
 
283
                                sci_replace_sel(editor->sci, entity);
 
284
                        }
 
285
                }
 
286
        }
 
287
        return FALSE;
 
288
}
 
289
 
 
290
 
 
291
void glatex_replace_special_character()
 
292
{
 
293
        GeanyDocument *doc = NULL;
 
294
        doc = document_get_current();
 
295
 
 
296
        if (doc != NULL && sci_has_selection(doc->editor->sci))
 
297
        {
 
298
                guint selection_len;
 
299
                gchar *selection = NULL;
 
300
                GString *replacement = g_string_new(NULL);
 
301
                guint i;
 
302
                gchar *new = NULL;
 
303
                const gchar *entity = NULL;
 
304
                gchar buf[7];
 
305
                gint len;
 
306
 
 
307
                selection = sci_get_selection_contents(doc->editor->sci);
 
308
 
 
309
                selection_len = strlen(selection);
 
310
 
 
311
                for (i = 0; i < selection_len; i++)
 
312
                {
 
313
                        len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
 
314
                        i = len - 1 + i;
 
315
                        buf[len] = '\0';
 
316
                        entity = glatex_get_entity(buf);
 
317
 
 
318
                        if (entity != NULL)
 
319
                        {
 
320
                                replacement = g_string_append(replacement, entity);
 
321
                        }
 
322
                        else
 
323
                        {
 
324
                                replacement = g_string_append(replacement, buf);
 
325
                        }
 
326
                }
 
327
                new = g_string_free(replacement, FALSE);
 
328
                sci_replace_sel(doc->editor->sci, new);
 
329
                g_free(selection);
 
330
                g_free(new);
 
331
        }
 
332
}
 
333
 
 
334
/* Called when keys were pressed */
 
335
void glatex_kblatex_toggle(G_GNUC_UNUSED guint key_id)
 
336
{
 
337
        if (toggle_active == TRUE)
 
338
                glatex_set_latextoggle_status(FALSE);
 
339
        else
 
340
                glatex_set_latextoggle_status(TRUE);
 
341
}
 
342
 
 
343
 
 
344
PluginCallback plugin_callbacks[] =
 
345
{
 
346
        { "editor-notify", (GCallback) &ht_editor_notify_cb, FALSE, NULL },
 
347
        { NULL, NULL, FALSE, NULL }
 
348
};
 
349
 
 
350
void
 
351
glatex_insert_string(gchar *string, gboolean reset_position)
 
352
{
 
353
        GeanyDocument *doc = NULL;
 
354
 
 
355
        doc = document_get_current();
 
356
 
 
357
        if (doc != NULL && string != NULL)
 
358
        {
 
359
                gint pos = sci_get_current_position(doc->editor->sci);
 
360
                sci_insert_text(doc->editor->sci, pos, string);
 
361
                if (reset_position == TRUE)
 
362
                {
 
363
                        gint len = strlen(string);
 
364
                        sci_set_current_position(doc->editor->sci, pos + len, TRUE);
 
365
                }
 
366
        }
 
367
}
 
368
 
 
369
 
 
370
inline gchar*
 
371
get_latex_command(gint tab_index)
 
372
{
 
373
        return glatex_char_array[tab_index].latex;
 
374
}
 
375
 
 
376
 
 
377
static void
 
378
char_insert_activated(G_GNUC_UNUSED GtkMenuItem * menuitem,
 
379
                                          G_GNUC_UNUSED gpointer gdata)
 
380
{
 
381
        glatex_insert_string(get_latex_command(GPOINTER_TO_INT(gdata)), TRUE);
 
382
}
 
383
 
 
384
 
 
385
void
 
386
glatex_insert_label_activated(G_GNUC_UNUSED GtkMenuItem * menuitem,
 
387
                                           G_GNUC_UNUSED gpointer gdata)
 
388
{
 
389
        GtkWidget *dialog = NULL;
 
390
        GtkWidget *vbox = NULL;
 
391
        GtkWidget *label = NULL;
 
392
        GtkWidget *textbox_label = NULL;
 
393
        GtkWidget *table = NULL;
 
394
 
 
395
        dialog = gtk_dialog_new_with_buttons(_("Insert Label"),
 
396
                                             GTK_WINDOW(geany->main_widgets->window),
 
397
                                             GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL,
 
398
                                             GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
 
399
                                             NULL);
 
400
        vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
 
401
        gtk_widget_set_name(dialog, "GeanyDialog");
 
402
        gtk_box_set_spacing(GTK_BOX(vbox), 10);
 
403
 
 
404
        table = gtk_table_new(1, 2, FALSE);
 
405
        gtk_table_set_col_spacings(GTK_TABLE(table), 6);
 
406
        gtk_table_set_row_spacings(GTK_TABLE(table), 6);
 
407
 
 
408
        label = gtk_label_new(_("Label name:"));
 
409
        textbox_label = gtk_entry_new();
 
410
 
 
411
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
 
412
 
 
413
        gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
 
414
        gtk_table_attach_defaults(GTK_TABLE(table), textbox_label, 1, 2, 0, 1);
 
415
        gtk_container_add(GTK_CONTAINER(vbox), table);
 
416
 
 
417
        gtk_widget_show_all(vbox);
 
418
 
 
419
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
 
420
        {
 
421
                gchar *label_str = NULL;
 
422
                label_str = g_strconcat("\\label{",g_strdup(gtk_entry_get_text(
 
423
                        GTK_ENTRY(textbox_label))), "}", NULL);
 
424
                glatex_insert_string(label_str, TRUE);
 
425
        }
 
426
 
 
427
        gtk_widget_destroy(dialog);
 
428
}
 
429
 
 
430
 
 
431
void
 
432
glatex_insert_ref_activated(G_GNUC_UNUSED GtkMenuItem * menuitem,
 
433
                                         G_GNUC_UNUSED gpointer gdata)
 
434
{
 
435
        GtkWidget *dialog;
 
436
        GtkWidget *vbox = NULL;
 
437
        GtkWidget *label_ref = NULL;
 
438
        GtkWidget *textbox_ref = NULL;
 
439
        GtkWidget *table = NULL;
 
440
        GtkWidget *radio1 = NULL;
 
441
        GtkWidget *radio2 = NULL;
 
442
        GtkTreeModel *model = NULL;
 
443
 
 
444
 
 
445
        dialog = gtk_dialog_new_with_buttons(_("Insert Reference"),
 
446
                                             GTK_WINDOW(geany->main_widgets->window),
 
447
                                             GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL,
 
448
                                             GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
 
449
                                             NULL);
 
450
        vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
 
451
        gtk_widget_set_name(dialog, "GeanyDialog");
 
452
        gtk_box_set_spacing(GTK_BOX(vbox), 10);
 
453
 
 
454
        table = gtk_table_new(1, 2, FALSE);
 
455
        gtk_table_set_col_spacings(GTK_TABLE(table), 6);
 
456
        gtk_table_set_row_spacings(GTK_TABLE(table), 6);
 
457
 
 
458
        label_ref = gtk_label_new(_("Reference name:"));
 
459
        textbox_ref = gtk_combo_box_entry_new_text();
 
460
        glatex_add_Labels(textbox_ref, glatex_get_aux_file());
 
461
        model = gtk_combo_box_get_model(GTK_COMBO_BOX(textbox_ref));
 
462
        gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model),
 
463
                0, GTK_SORT_ASCENDING);
 
464
 
 
465
        gtk_misc_set_alignment(GTK_MISC(label_ref), 0, 0.5);
 
466
 
 
467
        gtk_table_attach_defaults(GTK_TABLE(table), label_ref, 0, 1, 0, 1);
 
468
        gtk_table_attach_defaults(GTK_TABLE(table), textbox_ref, 1, 2, 0, 1);
 
469
        gtk_container_add(GTK_CONTAINER(vbox), table);
 
470
 
 
471
        radio1 = gtk_radio_button_new_with_mnemonic(NULL,
 
472
                _("_Standard Reference"));
 
473
        gtk_button_set_focus_on_click(GTK_BUTTON(radio1), FALSE);
 
474
        gtk_container_add(GTK_CONTAINER(vbox), radio1);
 
475
 
 
476
        radio2 = gtk_radio_button_new_with_mnemonic_from_widget(GTK_RADIO_BUTTON(radio1),
 
477
                _("_Page Reference"));
 
478
        gtk_button_set_focus_on_click(GTK_BUTTON(radio2), FALSE);
 
479
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2), FALSE);
 
480
        gtk_container_add(GTK_CONTAINER(vbox), radio2);
 
481
 
 
482
        gtk_widget_show_all(vbox);
 
483
 
 
484
 
 
485
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
 
486
        {
 
487
                gchar *ref_string = NULL;
 
488
 
 
489
                ref_string = g_strdup(gtk_combo_box_get_active_text(
 
490
                        GTK_COMBO_BOX(textbox_ref)));
 
491
 
 
492
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio2)) == FALSE)
 
493
                {
 
494
                        ref_string = g_strconcat("\\ref{", ref_string, "}", NULL);
 
495
                }
 
496
                else
 
497
                {
 
498
                        ref_string = g_strconcat("\\pageref{", ref_string, "}", NULL);
 
499
                }
 
500
 
 
501
                if (ref_string != NULL)
 
502
                {
 
503
                        glatex_insert_string(ref_string, TRUE);
 
504
                        g_free(ref_string);
 
505
                }
 
506
        }
 
507
 
 
508
        gtk_widget_destroy(dialog);
 
509
}
 
510
 
 
511
 
 
512
void glatex_character_create_menu_item(GtkWidget *menu, const gchar *label,
 
513
                                                                           gint letter, MenuCallback callback)
 
514
{
 
515
        GtkWidget *tmp;
 
516
 
 
517
        tmp = gtk_menu_item_new_with_label(label);
 
518
        gtk_widget_show(tmp);
 
519
        gtk_container_add(GTK_CONTAINER(menu), tmp);
 
520
        g_signal_connect(tmp, "activate",
 
521
                G_CALLBACK(callback), GINT_TO_POINTER(letter));
 
522
}
 
523
 
 
524
 
 
525
/* returns -1, if there are more than gint can work with or any other error
 
526
 * returns 0, if categorie is empty
 
527
 * if gint categorie is -1, function will count every element.
 
528
 * Useful, if there is no need for a categorie check.*/
 
529
gint
 
530
glatex_count_menu_entries(SubMenuTemplate *tmp, gint categorie)
 
531
{
 
532
        /* TODO: Reset max value to stop before it's too late */
 
533
        gint i;
 
534
        gint count = 0;
 
535
 
 
536
        if (categorie == -1)
 
537
        {
 
538
                for (i =1; tmp[i].label != NULL; i++)
 
539
                {
 
540
                        count = count + 1;
 
541
                }
 
542
        }
 
543
        else
 
544
        {
 
545
                for (i = 1; tmp[i].label != NULL; i++)
 
546
                {
 
547
                        if (tmp[i].cat == categorie)
 
548
                        {
 
549
                                count = count + 1;
 
550
                        }
 
551
                        if (i >= 256)
 
552
                        {
 
553
                                count = -1;
 
554
                                break;
 
555
                        }
 
556
                }
 
557
        }
 
558
        return count + 1;
 
559
}
 
560
 
 
561
static gint
 
562
count_menu_cat_entries(CategoryName *tmp)
 
563
{
 
564
        gint i;
 
565
 
 
566
        for (i = 0; tmp[i].label != NULL; i++);
 
567
        return i;
 
568
}
 
569
 
 
570
 
 
571
void glatex_sub_menu_init(GtkWidget *base_menu, SubMenuTemplate *menu_template,
 
572
                                   CategoryName *category_name,
 
573
                                   MenuCallback callback_function)
 
574
{
 
575
        gint i;
 
576
        gint j;
 
577
        gint categories = count_menu_cat_entries(category_name);
 
578
        GtkWidget *sub_menu = NULL;
 
579
        GtkWidget *sub_menu_cat[categories][2];
 
580
        GtkWidget *active_submenu = NULL;
 
581
 
 
582
        /* Creates sub menus based on information from letter.h */
 
583
        for (i = 0; i < categories; i++)
 
584
        {
 
585
                if (glatex_count_menu_entries(menu_template, i) > 0)
 
586
                {
 
587
                        create_sub_menu(base_menu, sub_menu_cat[i][0],
 
588
                         sub_menu_cat[i][1], category_name[i].label);
 
589
                }
 
590
        }
 
591
 
 
592
        /* Searching for all categories */
 
593
        for (i = 0; i < categories; i++)
 
594
        {
 
595
                gboolean split = FALSE;
 
596
                gboolean last_sub_menu = FALSE;
 
597
                gboolean sorted = category_name[i].sorted;
 
598
                /* To check whether we need to build up a new sub sub menu. */
 
599
                gint local_count = 0;
 
600
                gint item_count = glatex_count_menu_entries(menu_template, i);
 
601
 
 
602
                if (item_count < 1)
 
603
                        continue;
 
604
 
 
605
                /*  Default is, not to split anything to make menu not */
 
606
                /*  deeper than realy needed.  */
 
607
                if (item_count > MAX_MENU_ENTRIES)
 
608
                {
 
609
                        split = TRUE;
 
610
                }
 
611
 
 
612
                /*  Setting active sub menu to sub menu of category */
 
613
                sub_menu = sub_menu_cat[i][0];
 
614
                active_submenu = sub_menu;
 
615
                /*  Finding entries for each category */
 
616
 
 
617
                for (j = 0; menu_template[j].latex != NULL; j++)
 
618
                {
 
619
                        if (menu_template[j].cat == i)
 
620
                        {
 
621
                                /*  Creates a new sub sub menu if needed */
 
622
                                if (split == TRUE && (local_count % MAX_MENU_ENTRIES) == 0)
 
623
                                {
 
624
                                        gint next_split_point = 0;
 
625
                                        GtkWidget *tmp = NULL;
 
626
                                        GtkWidget *tmp_item = NULL;
 
627
 
 
628
                                        sub_menu = active_submenu;
 
629
 
 
630
                                        for (next_split_point = 0;
 
631
                                                next_split_point < MAX_MENU_ENTRIES ; next_split_point ++)
 
632
                                        {
 
633
                                                if (menu_template[j+next_split_point].cat != i)
 
634
                                                {
 
635
                                                        last_sub_menu = TRUE;
 
636
                                                        break;
 
637
                                                }
 
638
 
 
639
                                        }
 
640
 
 
641
                                        if (sorted == TRUE)
 
642
                                        {
 
643
                                                create_sub_menu(sub_menu_cat[i][0], tmp, tmp_item,
 
644
                                                g_strconcat(menu_template[j].label, " ... ",
 
645
                                                menu_template[j + next_split_point-1].label, NULL));
 
646
 
 
647
                                                sub_menu = tmp;
 
648
                                        }
 
649
                                        else if (sorted == FALSE)
 
650
                                        {
 
651
                                                if (last_sub_menu == FALSE)
 
652
                                                {
 
653
                                                        create_sub_menu(sub_menu, tmp, tmp_item, _("More"));
 
654
                                                        sub_menu = active_submenu;
 
655
                                                        active_submenu = tmp;
 
656
                                                }
 
657
                                        }
 
658
                                }
 
659
 
 
660
                                /*  Sets the counter to keep in track if a new ,,
 
661
                                 *  submenu needs to be build up */
 
662
                                local_count = local_count + 1;
 
663
                                glatex_character_create_menu_item(sub_menu, g_strconcat(
 
664
                                        menu_template[j].label, "\t", menu_template[j].latex,
 
665
                                        NULL), j, callback_function);
 
666
                        }
 
667
                }
 
668
        }
 
669
}
 
670
 
 
671
static int
 
672
find_latex_enc(gint l_geany_enc)
 
673
{
 
674
        guint i;
 
675
        for (i = 0; i < LATEX_ENCODINGS_MAX; i++)
 
676
        {
 
677
                if (latex_encodings[i].geany_enc == l_geany_enc)
 
678
                        return i;
 
679
        }
 
680
        return LATEX_ENCODING_NONE;
 
681
}
 
682
 
 
683
 
 
684
static void
 
685
show_output(const gchar * output, const gchar * name, const gint local_enc)
 
686
{
 
687
        GeanyDocument *doc = NULL;
 
688
        GeanyFiletype *ft = filetypes_lookup_by_name("LaTeX");
 
689
 
 
690
        if (output)
 
691
        {
 
692
                doc = document_new_file(name, ft, output);
 
693
                document_set_encoding(doc, encodings_get_charset_from_index
 
694
                        (latex_encodings[local_enc].geany_enc));
 
695
        }
 
696
}
 
697
 
 
698
void
 
699
glatex_wizard_activated(G_GNUC_UNUSED GtkMenuItem * menuitem,
 
700
                                 G_GNUC_UNUSED gpointer gdata)
 
701
{
 
702
        gint i;
 
703
        GString *code = NULL;
 
704
        gchar *author = NULL;
 
705
        gchar *date = NULL;
 
706
        gchar *output = NULL;
 
707
        gchar *classoptions = NULL;
 
708
        gchar *title = NULL;
 
709
        gchar *enc_latex_char = NULL;
 
710
        gchar *documentclass_str = NULL;
 
711
        gchar *papersize = NULL;
 
712
        gchar *draft = NULL;
 
713
        gchar *fontsize = NULL;
 
714
        gint documentclass_int;
 
715
        gint encoding_int;
 
716
        gint papersize_int;
 
717
        GtkWidget *dialog = NULL;
 
718
        GtkWidget *vbox = NULL;
 
719
        GtkWidget *label_documentclass = NULL;
 
720
        GtkWidget *documentclass_combobox = NULL;
 
721
        GtkWidget *label_encoding = NULL;
 
722
        GtkWidget *encoding_combobox = NULL;
 
723
        GtkWidget *fontsize_combobox = NULL;
 
724
        GtkWidget *label_fontsize = NULL;
 
725
        GtkWidget *table = NULL;
 
726
        GtkWidget *checkbox_KOMA = NULL;
 
727
        GtkWidget *author_textbox = NULL;
 
728
        GtkWidget *label_author = NULL;
 
729
        GtkWidget *date_textbox = NULL;
 
730
        GtkWidget *label_date = NULL;
 
731
        GtkWidget *title_textbox = NULL;
 
732
        GtkWidget *label_title = NULL;
 
733
        GtkWidget *papersize_combobox = NULL;
 
734
        GtkWidget *label_papersize = NULL;
 
735
        GtkWidget *checkbox_draft = NULL;
 
736
        gboolean KOMA_active;
 
737
        gboolean draft_active = FALSE;
 
738
 
 
739
        /*  Creating and formatting table */
 
740
        table = gtk_table_new(2, 6, FALSE);
 
741
        gtk_table_set_col_spacings(GTK_TABLE(table), 6);
 
742
        gtk_table_set_row_spacings(GTK_TABLE(table), 6);
 
743
 
 
744
        /*  Documentclass */
 
745
        label_documentclass = gtk_label_new(_("Documentclass:"));
 
746
        documentclass_combobox = gtk_combo_box_new_text();
 
747
        ui_widget_set_tooltip_text(documentclass_combobox,
 
748
                _("Choose the kind of document you want to write"));
 
749
        gtk_combo_box_insert_text(GTK_COMBO_BOX(documentclass_combobox), 0,
 
750
                _("Book"));
 
751
        gtk_combo_box_insert_text(GTK_COMBO_BOX(documentclass_combobox), 1,
 
752
                _("Article"));
 
753
        gtk_combo_box_insert_text(GTK_COMBO_BOX(documentclass_combobox), 2,
 
754
                _("Report"));
 
755
        gtk_combo_box_insert_text(GTK_COMBO_BOX(documentclass_combobox), 3,
 
756
                _("Letter"));
 
757
        gtk_combo_box_insert_text(GTK_COMBO_BOX(documentclass_combobox), 4,
 
758
                _("Presentation"));
 
759
 
 
760
        gtk_combo_box_set_active(GTK_COMBO_BOX(documentclass_combobox), 0);
 
761
 
 
762
        gtk_misc_set_alignment(GTK_MISC(label_documentclass), 0, 0.5);
 
763
 
 
764
        gtk_table_attach_defaults(GTK_TABLE(table), label_documentclass, 0, 1, 0, 1);
 
765
        gtk_table_attach_defaults(GTK_TABLE(table), documentclass_combobox, 1, 2, 0, 1);
 
766
 
 
767
        /*  Encoding */
 
768
        label_encoding = gtk_label_new(_("Encoding:"));
 
769
 
 
770
        encoding_combobox = gtk_combo_box_new_text();
 
771
        ui_widget_set_tooltip_text(encoding_combobox,
 
772
                _("Set the encoding for your new document"));
 
773
        for (i = 0; i < LATEX_ENCODINGS_MAX; i++)
 
774
        {
 
775
                gtk_combo_box_insert_text(GTK_COMBO_BOX(encoding_combobox), i,
 
776
                                          latex_encodings[i].name);
 
777
        }
 
778
 
 
779
        gtk_combo_box_set_active(GTK_COMBO_BOX(encoding_combobox), find_latex_enc(geany_data->file_prefs->default_new_encoding));
 
780
 
 
781
        gtk_misc_set_alignment(GTK_MISC(label_encoding), 0, 0.5);
 
782
 
 
783
        gtk_table_attach_defaults(GTK_TABLE(table), label_encoding, 0, 1, 1, 2);
 
784
        gtk_table_attach_defaults(GTK_TABLE(table), encoding_combobox, 1, 2, 1, 2);
 
785
 
 
786
        /*  fontsize */
 
787
 
 
788
        label_fontsize = gtk_label_new(_("Font size:"));
 
789
        fontsize_combobox = gtk_combo_box_entry_new_text();
 
790
        gtk_combo_box_append_text(GTK_COMBO_BOX(fontsize_combobox),"10pt");
 
791
        gtk_combo_box_append_text(GTK_COMBO_BOX(fontsize_combobox),"11pt");
 
792
        gtk_combo_box_append_text(GTK_COMBO_BOX(fontsize_combobox),"12pt");
 
793
        ui_widget_set_tooltip_text(fontsize_combobox,
 
794
                _("Set the default font size of your new document"));
 
795
 
 
796
        gtk_misc_set_alignment(GTK_MISC(label_fontsize), 0, 0.5);
 
797
 
 
798
        gtk_table_attach_defaults(GTK_TABLE(table), label_fontsize, 0, 1, 2, 3);
 
799
        gtk_table_attach_defaults(GTK_TABLE(table), fontsize_combobox, 1, 2, 2, 3);
 
800
 
 
801
        /*  Author */
 
802
        label_author = gtk_label_new(_("Author:"));
 
803
        author_textbox = gtk_entry_new();
 
804
        ui_widget_set_tooltip_text(author_textbox,
 
805
                _("Sets the value of the \\author command. In most cases this should be your name"));
 
806
        if (geany_data->template_prefs->developer != NULL)
 
807
        {
 
808
                author = geany_data->template_prefs->developer;
 
809
                gtk_entry_set_text(GTK_ENTRY(author_textbox), author);
 
810
        }
 
811
        gtk_misc_set_alignment(GTK_MISC(label_author), 0, 0.5);
 
812
        gtk_table_attach_defaults(GTK_TABLE(table), label_author, 0, 1, 3, 4);
 
813
        gtk_table_attach_defaults(GTK_TABLE(table), author_textbox, 1, 2, 3, 4);
 
814
 
 
815
        /*  Date */
 
816
        label_date = gtk_label_new(_("Date:"));
 
817
        date_textbox = gtk_entry_new();
 
818
        ui_widget_set_tooltip_text(date_textbox,
 
819
                _("Sets the value of the \\date command inside header of your\
 
820
                 newly created LaTeX-document. Keeping it at \\today is a good \
 
821
                 decision if you don't need any fixed date."));
 
822
        gtk_entry_set_text(GTK_ENTRY(date_textbox), "\\today");
 
823
        gtk_misc_set_alignment(GTK_MISC(label_date), 0, 0.5);
 
824
        gtk_table_attach_defaults(GTK_TABLE(table), label_date, 0, 1, 4, 5);
 
825
        gtk_table_attach_defaults(GTK_TABLE(table), date_textbox, 1, 2, 4, 5);
 
826
 
 
827
        /*  Title of the new document */
 
828
        label_title = gtk_label_new(_("Title:"));
 
829
        title_textbox = gtk_entry_new();
 
830
        ui_widget_set_tooltip_text(title_textbox,
 
831
                _("Sets the title of your new document."));
 
832
        gtk_misc_set_alignment(GTK_MISC(label_title), 0, 0.5);
 
833
        gtk_table_attach_defaults(GTK_TABLE(table), label_title, 0, 1, 5, 6);
 
834
        gtk_table_attach_defaults(GTK_TABLE(table), title_textbox, 1, 2, 5, 6);
 
835
 
 
836
        /*  Papersize */
 
837
        label_papersize = gtk_label_new(_("Paper size:"));
 
838
        papersize_combobox = gtk_combo_box_new_text();
 
839
        ui_widget_set_tooltip_text(papersize_combobox,
 
840
                _("Choose the paper format for the newly created document"));
 
841
        gtk_combo_box_insert_text(GTK_COMBO_BOX(papersize_combobox), 0, "A4");
 
842
        gtk_combo_box_insert_text(GTK_COMBO_BOX(papersize_combobox), 1, "A5");
 
843
        gtk_combo_box_insert_text(GTK_COMBO_BOX(papersize_combobox), 2, "A6");
 
844
 
 
845
        gtk_combo_box_set_active(GTK_COMBO_BOX(papersize_combobox), 0);
 
846
 
 
847
        gtk_misc_set_alignment(GTK_MISC(label_papersize), 0, 0.5);
 
848
 
 
849
        gtk_table_attach_defaults(GTK_TABLE(table), label_papersize, 0, 1, 6, 7);
 
850
        gtk_table_attach_defaults(GTK_TABLE(table), papersize_combobox, 1, 2, 6, 7);
 
851
 
 
852
        gtk_widget_show_all(table);
 
853
 
 
854
        /*  Building the wizard-dialog and showing it */
 
855
        dialog = gtk_dialog_new_with_buttons(_("LaTeX-Wizard"),
 
856
                                GTK_WINDOW(geany->main_widgets->window),
 
857
                                GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL,
 
858
                                GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
 
859
                                NULL);
 
860
        vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
 
861
        gtk_widget_set_name(dialog, "GeanyDialog");
 
862
        gtk_box_set_spacing(GTK_BOX(vbox), 10);
 
863
        gtk_container_add(GTK_CONTAINER(vbox), table);
 
864
 
 
865
        checkbox_KOMA = gtk_check_button_new_with_label(
 
866
                _("Use KOMA-script classes if possible"));
 
867
        ui_widget_set_tooltip_text(checkbox_KOMA,
 
868
                _("Uses the KOMA-script classes by Markus Kohm.\n"
 
869
                "Keep in mind: To compile your document these classes"
 
870
                "have to be installed before."));
 
871
        gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_KOMA), FALSE);
 
872
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_KOMA), glatex_set_koma_active);
 
873
        gtk_box_pack_start(GTK_BOX(vbox), checkbox_KOMA, FALSE, FALSE, 5);
 
874
 
 
875
        checkbox_draft = gtk_check_button_new_with_label(_("Use draft mode"));
 
876
        ui_widget_set_tooltip_text(checkbox_draft,
 
877
                _("Set the draft flag inside new created documents to get "
 
878
                "documents with a number of debugging helpers"));
 
879
        gtk_button_set_focus_on_click(GTK_BUTTON(checkbox_draft), FALSE);
 
880
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox_draft), draft_active);
 
881
        gtk_box_pack_start(GTK_BOX(vbox), checkbox_draft, FALSE, FALSE, 5);
 
882
 
 
883
        gtk_widget_show_all(vbox);
 
884
 
 
885
        if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
 
886
        {
 
887
                KOMA_active = gtk_toggle_button_get_active(
 
888
                        GTK_TOGGLE_BUTTON(checkbox_KOMA));
 
889
                draft_active = gtk_toggle_button_get_active(
 
890
                        GTK_TOGGLE_BUTTON(checkbox_draft));
 
891
                documentclass_int = gtk_combo_box_get_active(
 
892
                        GTK_COMBO_BOX(documentclass_combobox));
 
893
                encoding_int = gtk_combo_box_get_active(
 
894
                        GTK_COMBO_BOX(encoding_combobox));
 
895
                enc_latex_char = g_strconcat("\\usepackage[",
 
896
                        latex_encodings[encoding_int].latex,"]{inputenc}\n", NULL);
 
897
                author = g_strdup(gtk_entry_get_text(GTK_ENTRY(author_textbox)));
 
898
                date = g_strdup(gtk_entry_get_text(GTK_ENTRY(date_textbox)));
 
899
                title = g_strdup(gtk_entry_get_text(GTK_ENTRY(title_textbox)));
 
900
                papersize_int = gtk_combo_box_get_active(
 
901
                        GTK_COMBO_BOX(papersize_combobox));
 
902
                switch (papersize_int)
 
903
                {
 
904
                        case 0:
 
905
                        {
 
906
                                papersize = g_utf8_casefold("a4paper", -1);
 
907
                                break;
 
908
                        }
 
909
                        case 1:
 
910
                        {
 
911
                                papersize = g_utf8_casefold("a5paper", -1);
 
912
                                break;
 
913
                        }
 
914
                        case 2:
 
915
                        {
 
916
                                papersize = g_utf8_casefold("a6paper", -1);
 
917
                                break;
 
918
                        }
 
919
                }
 
920
 
 
921
                fontsize = gtk_combo_box_get_active_text(
 
922
                        GTK_COMBO_BOX(fontsize_combobox));
 
923
 
 
924
                if (papersize != NULL)
 
925
                {
 
926
                        classoptions = g_strdup(papersize);
 
927
                        g_free(papersize);
 
928
                }
 
929
                if (classoptions != NULL && draft_active == TRUE)
 
930
                {
 
931
                        draft = g_utf8_casefold("draft", -1);
 
932
                        classoptions = g_strconcat(classoptions,",", draft, NULL);
 
933
                        g_free(draft);
 
934
                }
 
935
                else if (classoptions == NULL && draft_active == TRUE)
 
936
                {
 
937
                        draft = g_utf8_casefold("draft", -1);
 
938
                        classoptions = g_strconcat(draft, NULL);
 
939
                        g_free(draft);
 
940
                }
 
941
                if (classoptions != NULL && NZV(fontsize))
 
942
                {
 
943
                        classoptions = g_strconcat(classoptions, ",", fontsize, NULL);
 
944
                }
 
945
                else if (classoptions == NULL && NZV(fontsize))
 
946
                {
 
947
                        classoptions = g_strdup(fontsize);
 
948
                }
 
949
                g_free(fontsize);
 
950
 
 
951
 
 
952
                switch (documentclass_int)
 
953
                {
 
954
                        case 0:
 
955
                        {
 
956
                                documentclass_str = g_utf8_casefold("book", -1);
 
957
                                break;
 
958
                        }
 
959
                        case 1:
 
960
                        {
 
961
                                documentclass_str = g_utf8_casefold("article", -1);
 
962
                                break;
 
963
                        }
 
964
                        case 2:
 
965
                        {
 
966
                                documentclass_str = g_utf8_casefold("report", -1);
 
967
                                break;
 
968
                        }
 
969
                        case 3:
 
970
                        {
 
971
                                documentclass_str = g_utf8_casefold("letter", -1);
 
972
                                break;
 
973
                        }
 
974
                        case 4:
 
975
                        {
 
976
                                documentclass_str = g_utf8_casefold("beamer", -1);
 
977
                        }
 
978
                }
 
979
 
 
980
                if (KOMA_active)
 
981
                {
 
982
                        switch (documentclass_int)
 
983
                        {
 
984
                                case 0:
 
985
                                {
 
986
                                        documentclass_str = g_utf8_casefold("scrbook", -1);
 
987
                                        break;
 
988
                                }
 
989
                                case 1:
 
990
                                {
 
991
                                        documentclass_str = g_utf8_casefold("scrartcl", -1);
 
992
                                        break;
 
993
                                }
 
994
                                case 2:
 
995
                                {
 
996
                                        documentclass_str = g_utf8_casefold("scrreprt", -1);
 
997
                                        break;
 
998
                                }
 
999
                        }
 
1000
 
 
1001
                }
 
1002
 
 
1003
                if (documentclass_int == 3)
 
1004
                        code = g_string_new(TEMPLATE_LATEX_LETTER);
 
1005
                else if (documentclass_int == 4)
 
1006
                        code = g_string_new(TEMPLATE_LATEX_BEAMER);
 
1007
                else
 
1008
                        code = g_string_new(TEMPLATE_LATEX);
 
1009
 
 
1010
                if (classoptions != NULL)
 
1011
                {
 
1012
                        utils_string_replace_all(code, "{CLASSOPTION}", classoptions);
 
1013
                        g_free(classoptions);
 
1014
                }
 
1015
                if (documentclass_str != NULL)
 
1016
                {
 
1017
                        utils_string_replace_all(code, "{DOCUMENTCLASS}", documentclass_str);
 
1018
                        g_free(documentclass_str);
 
1019
                }
 
1020
                if (enc_latex_char != NULL)
 
1021
                {
 
1022
                        utils_string_replace_all(code, "{ENCODING}", enc_latex_char);
 
1023
                        g_free(enc_latex_char);
 
1024
                }
 
1025
                if (author != NULL)
 
1026
                {
 
1027
                        if (author[0] != '\0')
 
1028
                        {
 
1029
                                if (documentclass_int == 3)
 
1030
                                {
 
1031
                                        author = g_strconcat("\\signature{", author, "}\n", NULL);
 
1032
                                }
 
1033
                                else
 
1034
                                {
 
1035
                                        author = g_strconcat("\\author{", author, "}\n", NULL);
 
1036
                                }
 
1037
 
 
1038
                                utils_string_replace_all(code, "{AUTHOR}", author);
 
1039
                        }
 
1040
                        else
 
1041
                                if (documentclass_int == 3)
 
1042
                                {
 
1043
                                        utils_string_replace_all(code, "{AUTHOR}", "\% \\signature{}\n");
 
1044
                                }
 
1045
                                else
 
1046
                                {
 
1047
                                        utils_string_replace_all(code, "{AUTHOR}", "\% \\author{}\n");
 
1048
                                }
 
1049
 
 
1050
                        g_free(author);
 
1051
                }
 
1052
                if (date != NULL)
 
1053
                {
 
1054
                        if (date[0] != '\0')
 
1055
                        {
 
1056
                                date = g_strconcat("\\date{", date, "}\n", NULL);
 
1057
                                utils_string_replace_all(code, "{DATE}", date);
 
1058
                        }
 
1059
                        else
 
1060
                                utils_string_replace_all(code, "{DATE}", "\% \\date{}\n");
 
1061
                        g_free(date);
 
1062
                }
 
1063
                if (title != NULL)
 
1064
                {
 
1065
                        if (title[0] != '\0')
 
1066
                        {
 
1067
                                if (documentclass_int == 3)
 
1068
                                {
 
1069
                                        title = g_strconcat("\\subject{", title, "}\n", NULL);
 
1070
                                }
 
1071
                                else
 
1072
                                {
 
1073
                                        title = g_strconcat("\\title{", title, "}\n", NULL);
 
1074
                                }
 
1075
 
 
1076
                                utils_string_replace_all(code, "{TITLE}", title);
 
1077
                        }
 
1078
                        else
 
1079
                                if (documentclass_int == 3)
 
1080
                                {
 
1081
                                        utils_string_replace_all(code, "{TITLE}", "\% \\subject{} \n");
 
1082
                                }
 
1083
                                else
 
1084
                                {
 
1085
                                        utils_string_replace_all(code, "{TITLE}", "\% \\title{} \n");
 
1086
                                }
 
1087
 
 
1088
                        g_free(title);
 
1089
                }
 
1090
 
 
1091
                utils_string_replace_all(code, "{OPENING}", _("Dear Sir or Madame"));
 
1092
                utils_string_replace_all(code, "{CLOSING}", _("With kind regards"));
 
1093
 
 
1094
                output = g_string_free(code, FALSE);
 
1095
                show_output(output, NULL, encoding_int);
 
1096
                g_free(output);
 
1097
        }
 
1098
        gtk_widget_destroy(dialog);
 
1099
}
 
1100
 
 
1101
 
 
1102
void init_keybindings()
 
1103
{
 
1104
        /* init keybindins */
 
1105
        keybindings_set_item(plugin_key_group, KB_LATEX_WIZZARD, glatex_kbwizard,
 
1106
                0, 0, "run_latex_wizard", _("Run LaTeX-Wizard"), menu_latex_wizzard);
 
1107
        keybindings_set_item(plugin_key_group, KB_LATEX_INSERT_LABEL, glatex_kblabel_insert,
 
1108
                0, 0, "insert_latex_label", _("Insert \\label"), menu_latex_label);
 
1109
        keybindings_set_item(plugin_key_group, KB_LATEX_INSERT_REF, glatex_kbref_insert,
 
1110
                0, 0, "insert_latex_ref", _("Insert \\ref"), menu_latex_ref);
 
1111
        keybindings_set_item(plugin_key_group, KB_LATEX_INSERT_NEWLINE, glatex_kb_insert_newline,
 
1112
                0, 0, "insert_new_line", _("Insert linebreak \\\\ "), NULL);
 
1113
        keybindings_set_item(plugin_key_group, KB_LATEX_TOGGLE_ACTIVE, glatex_kblatex_toggle,
 
1114
                0, 0, "latex_toggle_status", _("Turn input replacement on/off"),
 
1115
                menu_latex_replace_toggle);
 
1116
        keybindings_set_item(plugin_key_group, KB_LATEX_REPLACE_SPECIAL_CHARS,
 
1117
                glatex_kb_replace_special_chars, 0, 0, "latex_replace_chars",
 
1118
                _("Replace special characters"), NULL);
 
1119
        keybindings_set_item(plugin_key_group, KB_LATEX_ENVIRONMENT_INSERT,
 
1120
                glatex_kbref_insert_environment, 0, 0, "latex_insert_environment",
 
1121
                _("Run insert environment dialog"), menu_latex_insert_environment);
 
1122
        keybindings_set_item(plugin_key_group, KB_LATEX_INSERT_NEWITEM,
 
1123
                glatex_kb_insert_newitem, 0, 0, "latex_insert_item", _("Insert \\item"), NULL);
 
1124
        keybindings_set_item(plugin_key_group, KB_LATEX_FORMAT_BOLD, glatex_kb_format_bold,
 
1125
                0, 0, "format_bold", _("Format selection in bold font face"), NULL);
 
1126
        keybindings_set_item(plugin_key_group, KB_LATEX_FORMAT_ITALIC, glatex_kb_format_italic,
 
1127
                0, 0, "format_italic", _("Format selection in italic font face"), NULL);
 
1128
        keybindings_set_item(plugin_key_group, KB_LATEX_FORMAT_TYPEWRITER, glatex_kb_format_typewriter,
 
1129
                0, 0, "format_typewriter", _("Format selection in typewriter font face"), NULL);
 
1130
        keybindings_set_item(plugin_key_group, KB_LATEX_FORMAT_CENTER, glatex_kb_format_centering,
 
1131
                0, 0, "format_center", _("Format selection centered"), NULL);
 
1132
        keybindings_set_item(plugin_key_group, KB_LATEX_FORMAT_LEFT, glatex_kb_format_left,
 
1133
                0, 0, "format_left", _("Format selection left-aligned"), NULL);
 
1134
        keybindings_set_item(plugin_key_group, KB_LATEX_FORMAT_RIGHT, glatex_kb_format_right,
 
1135
                0, 0, "format_right", _("Format selection right-aligned "), NULL);
 
1136
}
 
1137
 
 
1138
void plugin_help()
 
1139
{
 
1140
        dialogs_show_msgbox(GTK_MESSAGE_INFO,
 
1141
                _("GeanyLaTeX is a plugin to improve support for LaTeX in Geany."
 
1142
                "\n\nPlease report all bugs or feature requests to one of the "
 
1143
                "authors."));
 
1144
}
 
1145
 
 
1146
void
 
1147
plugin_init(G_GNUC_UNUSED GeanyData * data)
 
1148
{
 
1149
        GtkWidget *tmp = NULL;
 
1150
        GKeyFile *config = g_key_file_new();
 
1151
        gint i;
 
1152
 
 
1153
        /* loading configurations from file ...*/
 
1154
        config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S,
 
1155
        "plugins", G_DIR_SEPARATOR_S,
 
1156
        "geanyLaTeX", G_DIR_SEPARATOR_S, "general.conf", NULL);
 
1157
 
 
1158
        /* ... and Initialising options from config file */
 
1159
        g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
 
1160
 
 
1161
        glatex_set_koma_active = utils_get_setting_boolean(config, "general",
 
1162
                "glatex_set_koma_active", FALSE);
 
1163
 
 
1164
        glatex_set_toolbar_active = utils_get_setting_boolean(config, "general",
 
1165
                "glatex_set_toolbar_active", FALSE);
 
1166
 
 
1167
        main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
 
1168
 
 
1169
        g_key_file_free(config);
 
1170
 
 
1171
 
 
1172
        glatex_init_encodings_latex();
 
1173
 
 
1174
        menu_latex = gtk_menu_item_new_with_mnemonic(_("_LaTeX"));
 
1175
        gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_latex);
 
1176
 
 
1177
        menu_latex_menu = gtk_menu_new();
 
1178
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_latex), menu_latex_menu);
 
1179
 
 
1180
        menu_latex_wizzard = ui_image_menu_item_new(GTK_STOCK_NEW, _("LaTeX-_Wizard"));
 
1181
        gtk_container_add(GTK_CONTAINER(menu_latex_menu), menu_latex_wizzard);
 
1182
        ui_widget_set_tooltip_text(menu_latex_wizzard,
 
1183
                             _("Starts a Wizard to easily create LaTeX-documents"));
 
1184
 
 
1185
        g_signal_connect(menu_latex_wizzard, "activate",
 
1186
                         G_CALLBACK(glatex_wizard_activated), NULL);
 
1187
 
 
1188
        menu_latex_menu_special_char = gtk_menu_item_new_with_mnemonic(_("Insert _Special Character"));
 
1189
        ui_widget_set_tooltip_text(menu_latex_menu_special_char,
 
1190
                             _("Helps to use some not very common letters and signs"));
 
1191
        gtk_container_add(GTK_CONTAINER(menu_latex_menu),
 
1192
                menu_latex_menu_special_char);
 
1193
 
 
1194
        menu_latex_menu_special_char_submenu = gtk_menu_new();
 
1195
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_latex_menu_special_char),
 
1196
                menu_latex_menu_special_char_submenu);
 
1197
        glatex_sub_menu_init(menu_latex_menu_special_char_submenu, glatex_char_array, glatex_cat_names,
 
1198
                char_insert_activated);
 
1199
 
 
1200
        menu_latex_ref = gtk_menu_item_new_with_mnemonic(_("Insert _Reference"));
 
1201
        ui_widget_set_tooltip_text(menu_latex_ref,
 
1202
                _("Inserting references to the document"));
 
1203
        gtk_container_add(GTK_CONTAINER(menu_latex_menu), menu_latex_ref);
 
1204
        g_signal_connect(menu_latex_ref, "activate",
 
1205
                G_CALLBACK(glatex_insert_ref_activated), NULL);
 
1206
 
 
1207
        menu_latex_label = gtk_menu_item_new_with_mnemonic(_("Insert _Label"));
 
1208
        ui_widget_set_tooltip_text(menu_latex_label,
 
1209
            _("Helps at inserting labels to a document"));
 
1210
        gtk_container_add(GTK_CONTAINER(menu_latex_menu), menu_latex_label);
 
1211
        g_signal_connect(menu_latex_label, "activate",
 
1212
                G_CALLBACK(glatex_insert_label_activated), NULL);
 
1213
 
 
1214
        menu_latex_insert_environment = gtk_menu_item_new_with_mnemonic(
 
1215
                _("Insert _Environment"));
 
1216
        ui_widget_set_tooltip_text(menu_latex_insert_environment,
 
1217
             _("Helps at inserting an environment a document"));
 
1218
        gtk_container_add(GTK_CONTAINER(menu_latex_menu), menu_latex_insert_environment);
 
1219
        g_signal_connect(menu_latex_insert_environment, "activate",
 
1220
                G_CALLBACK(glatex_insert_environment_dialog), NULL);
 
1221
 
 
1222
        menu_latex_bibtex = gtk_menu_item_new_with_mnemonic(_("_BibTeX"));
 
1223
        gtk_container_add(GTK_CONTAINER(menu_latex_menu), menu_latex_bibtex);
 
1224
 
 
1225
        menu_latex_bibtex_submenu = gtk_menu_new();
 
1226
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_latex_bibtex),
 
1227
                menu_latex_bibtex_submenu);
 
1228
 
 
1229
        for (i = 0; i < N_TYPES; i++)
 
1230
        {
 
1231
                tmp = NULL;
 
1232
                tmp = gtk_menu_item_new_with_mnemonic(_(glatex_label_types[i]));
 
1233
                gtk_container_add(GTK_CONTAINER(menu_latex_bibtex_submenu), tmp);
 
1234
                g_signal_connect(tmp, "activate",
 
1235
                        G_CALLBACK(glatex_insert_bibtex_entry), GINT_TO_POINTER(i));
 
1236
        }
 
1237
 
 
1238
        menu_latex_format_insert = gtk_menu_item_new_with_mnemonic(_("_Format"));
 
1239
        gtk_container_add(GTK_CONTAINER(menu_latex_menu), menu_latex_format_insert);
 
1240
 
 
1241
        menu_latex_format_insert_submenu = gtk_menu_new();
 
1242
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_latex_format_insert),
 
1243
                menu_latex_format_insert_submenu);
 
1244
 
 
1245
        for (i = 0; i < LATEX_STYLES_END; i++)
 
1246
        {
 
1247
                tmp = NULL;
 
1248
                tmp = gtk_menu_item_new_with_mnemonic(_(glatex_format_labels[i]));
 
1249
                gtk_container_add(GTK_CONTAINER(menu_latex_format_insert_submenu), tmp);
 
1250
                g_signal_connect(tmp, "activate",
 
1251
                        G_CALLBACK(glatex_insert_latex_format), GINT_TO_POINTER(i));
 
1252
        }
 
1253
 
 
1254
        /* Add menuitem for LaTeX replacement functions*/
 
1255
        menu_latex_replacement = gtk_menu_item_new_with_mnemonic(
 
1256
                _("_Special Character Replacement"));
 
1257
        menu_latex_replacement_submenu = gtk_menu_new();
 
1258
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_latex_replacement),
 
1259
                menu_latex_replacement_submenu);
 
1260
        gtk_container_add(GTK_CONTAINER(menu_latex_menu), menu_latex_replacement);
 
1261
 
 
1262
        /* Add menuitem for bulk replacment */
 
1263
        menu_latex_replace_selection = gtk_menu_item_new_with_mnemonic(
 
1264
                _("Bulk _Replace Special Characters"));
 
1265
        ui_widget_set_tooltip_text(menu_latex_replace_selection,
 
1266
                _("_Replace selected special cahracters with TeX substitutes"));
 
1267
        gtk_container_add(GTK_CONTAINER(menu_latex_replacement_submenu),
 
1268
                menu_latex_replace_selection);
 
1269
        g_signal_connect(menu_latex_replace_selection, "activate",
 
1270
                G_CALLBACK(glatex_replace_special_character), NULL);
 
1271
 
 
1272
        /* Add menu entry for toggling input replacment */
 
1273
        menu_latex_replace_toggle = gtk_check_menu_item_new_with_mnemonic(
 
1274
                _("Toggle _Special Character Replacement"));
 
1275
        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(menu_latex_replace_toggle),
 
1276
                                                                        toggle_active);
 
1277
        gtk_container_add(GTK_CONTAINER(menu_latex_replacement_submenu),
 
1278
                menu_latex_replace_toggle);
 
1279
 
 
1280
        g_signal_connect(menu_latex_replace_toggle, "activate",
 
1281
                                         G_CALLBACK(glatex_toggle_status), NULL);
 
1282
 
 
1283
        init_keybindings();
 
1284
        if (glatex_set_toolbar_active == TRUE)
 
1285
                glatex_toolbar = init_toolbar();
 
1286
        else
 
1287
                glatex_toolbar = NULL;
 
1288
 
 
1289
        ui_add_document_sensitive(menu_latex_menu_special_char);
 
1290
        ui_add_document_sensitive(menu_latex_ref);
 
1291
        ui_add_document_sensitive(menu_latex_label);
 
1292
        ui_add_document_sensitive(menu_latex_bibtex);
 
1293
        ui_add_document_sensitive(menu_latex_format_insert);
 
1294
        ui_add_document_sensitive(menu_latex_insert_environment);
 
1295
        ui_add_document_sensitive(menu_latex_replacement);
 
1296
 
 
1297
        gtk_widget_show_all(menu_latex);
 
1298
        main_menu_item = menu_latex;
 
1299
}
 
1300
 
 
1301
void
 
1302
plugin_cleanup()
 
1303
{
 
1304
        gtk_widget_destroy(main_menu_item);
 
1305
        if (glatex_toolbar != NULL)
 
1306
                gtk_widget_destroy(glatex_toolbar);
 
1307
 
 
1308
        g_free(config_file);
 
1309
}