[gnome-latex: 8/205] Better organistation: - the callbacks are in a separated file - 3 functions in error.c to display an



commit b2aa325c3d6af8e5bdf67683197356b251c2793d
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Sat Aug 1 20:18:44 2009 +0200

    Better organistation:
    - the callbacks are in a separated file
    - 3 functions in error.c to display an information, or a warning, or an
      error.
    - for the menubar and the toolbar: accelerators, and a different way to
      add the menu and the toolbar to the main vertical box.

 TODO            |  15 ++++---
 src/Makefile    |   8 +++-
 src/callbacks.c |  76 +++++++++++++++++++++++++++++++++
 src/callbacks.h |   8 ++++
 src/error.c     |  34 +++++++++++++++
 src/error.h     |   8 ++++
 src/main.c      | 130 +++++++++++++++++++++-----------------------------------
 src/main.h      |   5 +--
 src/ui.xml      |   4 +-
 9 files changed, 192 insertions(+), 96 deletions(-)
---
diff --git a/TODO b/TODO
index 35702ce..394cf05 100644
--- a/TODO
+++ b/TODO
@@ -2,13 +2,14 @@ TODO LaTeXila
 
 Jul 31, 2009 to Aug 7, 2009
 
-[x] about
+[x] about dialog
 
-[-] files
-       - new (with tabs)
+[-] GtkSourceView
+       - syntaxic color
+       - show/hide line numbers
+       - undo/redo
+
+[-] files (with tabs)
+       - new
        - open
        - save and save as
-
-[-] syntaxic color
-
-[-] show/hide line numbers
diff --git a/src/Makefile b/src/Makefile
index 7209fdc..d8409d9 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,14 +1,18 @@
 CC = gcc
 CFLAGS = -g -W -Wall -std=c99 $(shell pkg-config --cflags gtk+-2.0) -DGTK_DISABLE_DEPRECATED=1
 LDFLAGS = $(shell pkg-config --libs gtk+-2.0)
-OBJ = main.o
+OBJ = main.o callbacks.o error.o
 
 .PHONY: clean
 
 latexila: $(OBJ)
        $(CC) $(OBJ) $(LDFLAGS) -o latexila
 
-main.o: main.c
+main.o: main.c main.h callbacks.h error.h
+
+callbacks.o: callbacks.c callbacks.h main.h error.h
+
+error.o: error.c error.h
 
 clean:
        rm -f $(OBJ) latexila
diff --git a/src/callbacks.c b/src/callbacks.c
new file mode 100644
index 0000000..1163512
--- /dev/null
+++ b/src/callbacks.c
@@ -0,0 +1,76 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <locale.h>
+#include <libintl.h>
+#include <gtk/gtk.h>
+
+#include "main.h"
+#include "callbacks.h"
+#include "error.h"
+
+void
+cb_open (void)
+{
+       GtkWidget *dialog = gtk_file_chooser_dialog_new (
+                       _("Open File"),
+                       NULL,
+                       GTK_FILE_CHOOSER_ACTION_OPEN,
+                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                       NULL
+       );
+
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+       {
+               char *filename;
+               filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+
+               //TODO open the file in a new tab
+               //open_file (filename);
+               
+               print_info ("Open file: \"%s\"", filename);
+               g_free (filename);
+       }
+
+       gtk_widget_destroy (dialog);
+}
+
+void
+cb_about_dialog (void)
+{
+       gchar *comments = _(PROGRAM_NAME " is a LaTeX development environment for the GNOME Desktop");
+       gchar *copyright = "Copyright © 2009 Sébastien Wilmet";
+
+       //TODO show the appropriate text for the GPL 3 licence
+       gchar *licence = "GPL 3 or later";
+
+       //TODO set the url hook
+       gchar *website = "http://latexila.sourceforge.net/";;
+
+       const gchar * const authors[] =
+       {
+               "Sébastien Wilmet <sebastien wilmet gmail com>",
+               NULL
+       };
+
+       gtk_show_about_dialog (
+                       NULL,
+                       "program-name", PROGRAM_NAME,
+                       "authors", authors,
+                       "comments", comments,
+                       "copyright", copyright,
+                       "license", licence,
+                       "version", PROGRAM_VERSION,
+                       "title", _("About " PROGRAM_NAME),
+                       "translator-credits", _("translator-credits"),
+                       "website", website,
+                       NULL
+       );
+}
+
+void
+cb_quit (void)
+{
+       print_info ("Bye bye");
+       gtk_main_quit ();
+}
diff --git a/src/callbacks.h b/src/callbacks.h
new file mode 100644
index 0000000..a1e4573
--- /dev/null
+++ b/src/callbacks.h
@@ -0,0 +1,8 @@
+#ifndef CALLBACKS_H
+#define CALLBACKS_H
+
+void cb_open (void);
+void cb_about_dialog (void);
+void cb_quit (void);
+
+#endif /* CALLBACKS_H */
diff --git a/src/error.c b/src/error.c
new file mode 100644
index 0000000..cec3f35
--- /dev/null
+++ b/src/error.c
@@ -0,0 +1,34 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "error.h"
+
+void
+print_info (char *format, ...)
+{
+       va_list va;
+       va_start (va, format);
+       vprintf (format, va);
+       printf ("\n");
+}
+
+void
+print_warning (char *format, ...)
+{
+       va_list va;
+       va_start (va, format);
+       fprintf (stderr, "Warning: ");
+       vfprintf (stderr, format, va);
+       fprintf (stderr, "\n");
+}
+
+void
+print_error (char *format, ...)
+{
+       va_list va;
+       va_start (va, format);
+       fprintf (stderr, "Error: ");
+       vfprintf (stderr, format, va);
+       fprintf (stderr, "\n");
+       exit (EXIT_FAILURE);
+}
diff --git a/src/error.h b/src/error.h
new file mode 100644
index 0000000..b0307f9
--- /dev/null
+++ b/src/error.h
@@ -0,0 +1,8 @@
+#ifndef ERROR_H
+#define ERROR_H
+
+void print_info (char *, ...);
+void print_warning (char *, ...);
+void print_error (char *, ...);
+
+#endif /* ERROR_H */
diff --git a/src/main.c b/src/main.c
index 922a0d1..510a4d8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,38 +1,56 @@
 #include <stdlib.h>
 #include <stdio.h>
-#include <stdbool.h>
 #include <locale.h>
 #include <libintl.h>
 #include <gtk/gtk.h>
 
 #include "main.h"
+#include "callbacks.h"
+#include "error.h"
 
 int
 main (int argc, char *argv[])
 {
-       /* localisation */      
+       gtk_init (&argc, &argv);
+
+       /* localisation */
        setlocale (LC_ALL, "");
        bindtextdomain ("latexila", "/usr/share/locale");
        textdomain ("latexila");
 
-       char *xml_file = "ui.xml";
+       /* main window */
+       GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       g_signal_connect (G_OBJECT (window), "destroy",
+                       G_CALLBACK (cb_quit), NULL);
+       gtk_window_set_title (GTK_WINDOW (window), PROGRAM_NAME);
+       gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
+       gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
+
+       /* boxes */
+       GtkWidget *main_vbox = gtk_vbox_new (FALSE, 0);
+       gtk_container_add (GTK_CONTAINER (window), main_vbox);
 
+       /* menubar and toolbar */
+       
        // all the actions (for the menu and the toolbar)
        // name, stock_id, label, accelerator, tooltip, callback
        // the names come from the XML file
+       //
+       //TODO try to place "entries" out of the main function without errors
+       //because of gettext (the _() macro)
        GtkActionEntry entries[] =
        {
                {"File", NULL, _("File"), NULL, NULL, NULL},
                {"FileNew", GTK_STOCK_NEW, _("New"), "<Control>N",
                        _("New file"), NULL},
                {"FileOpen", GTK_STOCK_OPEN, _("Open..."), "<Control>O",
-                       _("Open a file"), NULL},
+                       _("Open a file"), G_CALLBACK (cb_open)},
                {"FileSave", GTK_STOCK_SAVE, _("Save..."), "<Control>S",
                        _("Save the current file"), NULL},
                {"FileSaveAs", GTK_STOCK_SAVE_AS, _("Save as..."), "<Shift><Control>S",
                        _("Save the current file with a different name"), NULL},
                {"FileQuit", GTK_STOCK_QUIT, _("Quit"), "<Control>Q",
-                       _("Quit the program"), gtk_main_quit},
+                       _("Quit the program"), G_CALLBACK (cb_quit)},
                
                {"Edit", NULL, _("Edit"), NULL, NULL, NULL},
                {"EditUndo", GTK_STOCK_UNDO, _("Undo"), "<Control>Z",
@@ -52,47 +70,40 @@ main (int argc, char *argv[])
                
                {"Help", NULL, _("Help"), NULL, NULL, NULL},
                {"HelpAbout", GTK_STOCK_ABOUT, _("About"), NULL,
-                       _("About LaTeXila"), about_dialog}
+                       _("About LaTeXila"), G_CALLBACK (cb_about_dialog)}
        };
 
-       int nb_entries = sizeof (entries) / sizeof (entries[0]);
+       guint nb_entries = G_N_ELEMENTS (entries);
 
+       GtkActionGroup *action_group = gtk_action_group_new ("menuActionGroup");
+       gtk_action_group_add_actions (action_group, entries, nb_entries, NULL);
+       GtkUIManager *ui_manager = gtk_ui_manager_new ();
+       gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
 
-       gtk_init (&argc, &argv);
+       // load the xml file
+       char *xml_file = "ui.xml";
+       GError *error = NULL;
+       gtk_ui_manager_add_ui_from_file (ui_manager, xml_file, &error);
+       if (error)
+       {
+               print_error ("building menubar and toolbar failed: %s", error->message);
+               g_error_free (error);
+       }
 
-       /* main window */
-       GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-       g_signal_connect (G_OBJECT (window), "destroy",
-                       G_CALLBACK (gtk_main_quit), NULL);
-       g_signal_connect (G_OBJECT (window), "delete_event",
-                       G_CALLBACK (gtk_main_quit), NULL);
-       gtk_window_set_title (GTK_WINDOW (window), PROGRAM_NAME);
-       gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
-       gtk_window_set_default_size (GTK_WINDOW (window), 500, 400);
+       // get and put the menubar and the toolbar to the main vbox
+       GtkWidget *menubar = gtk_ui_manager_get_widget (ui_manager, "/MainMenu");
+       gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, FALSE, 0);
+       GtkWidget *toolbar = gtk_ui_manager_get_widget (ui_manager, "/MainToolbar");
+       gtk_box_pack_start (GTK_BOX (main_vbox), toolbar, FALSE, FALSE, 0);
 
-       /* boxes and panes */
-       GtkWidget *vbox1 = gtk_vbox_new (FALSE, 0);
-       gtk_container_add (GTK_CONTAINER (window), vbox1);
+       // accelerators
+       gtk_window_add_accel_group (GTK_WINDOW (window), 
+                       gtk_ui_manager_get_accel_group (ui_manager));
 
-       GtkWidget *vbox2 = gtk_vbox_new (FALSE, 0);
-       gtk_box_pack_end (GTK_BOX (vbox1), vbox2, TRUE, TRUE, 0);
 
+       /* vertical pane for the source view and the log zone */
        GtkWidget *vpaned = gtk_vpaned_new ();
-       gtk_box_pack_start (GTK_BOX (vbox2), vpaned, TRUE, TRUE, 0);
-
-       /* menubar and toolbar */
-       GtkUIManager *ui_manager = gtk_ui_manager_new ();
-       GtkActionGroup *action_group = gtk_action_group_new ("menuActionGroup");
-       gtk_action_group_add_actions (action_group, entries, nb_entries, NULL);
-       gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
-       int tmp = gtk_ui_manager_add_ui_from_file (ui_manager, xml_file, NULL);
-       if (tmp == 0)
-       {
-               fprintf (stderr, "Error with %s\n", xml_file);
-               return EXIT_FAILURE;
-       }
-       g_signal_connect (ui_manager, "add-widget", G_CALLBACK (menu_add_widget),
-                       vbox1);
+       gtk_box_pack_start (GTK_BOX (main_vbox), vpaned, TRUE, TRUE, 0);
 
        /* source view */
        GtkWidget *source_view = gtk_text_view_new ();
@@ -126,7 +137,7 @@ main (int argc, char *argv[])
 
        /* statusbar */
        GtkWidget *statusbar = gtk_statusbar_new ();
-       gtk_box_pack_start (GTK_BOX (vbox2), statusbar, FALSE, FALSE, 0);
+       gtk_box_pack_start (GTK_BOX (main_vbox), statusbar, FALSE, FALSE, 0);
 
 
        gtk_widget_show_all (window);
@@ -135,46 +146,3 @@ main (int argc, char *argv[])
        return EXIT_SUCCESS;
 }
 
-static void
-menu_add_widget (GtkUIManager *ui_manager, GtkWidget *widget, GtkContainer *box)
-{
-   gtk_box_pack_start (GTK_BOX (box), widget, FALSE, FALSE, 0);
-   gtk_widget_show (widget);
-}
-
-static void
-about_dialog (void)
-{
-       char *comments = _(PROGRAM_NAME " is a LaTeX development environment for the GNOME Desktop");
-       char *copyright = "Copyright © 2009 Sébastien Wilmet";
-
-       //TODO show the appropriate text for the GPL 3 licence (from a file?)
-       char *licence = "GPL 3 or later";
-
-       //TODO set the url hook
-       char *website = "http://latexila.sourceforge.net/";;
-
-       //TODO list of translators
-       const char * const authors[] =
-       {
-               "Sébastien Wilmet <sebastien wilmet gmail com>",
-               NULL
-       };
-
-       gtk_show_about_dialog (
-                       NULL,
-                       "program-name", PROGRAM_NAME,
-                       "authors", authors,
-                       "comments", comments,
-                       "copyright", copyright,
-                       "license", licence,
-                       "version", PROGRAM_VERSION,
-                       "title", _("About " PROGRAM_NAME),
-                       "website", website,
-                       NULL
-       );
-}
-
-
-
-
diff --git a/src/main.h b/src/main.h
index 64c53ce..2c72579 100644
--- a/src/main.h
+++ b/src/main.h
@@ -6,7 +6,4 @@
 #define PROGRAM_NAME "LaTeXila"
 #define PROGRAM_VERSION "0.0.1"
 
-static void menu_add_widget (GtkUIManager *ui_manager, GtkWidget *widget, GtkContainer *box);
-static void about_dialog (void);
-
-#endif // MAIN_H
+#endif /* MAIN_H */
diff --git a/src/ui.xml b/src/ui.xml
index 0deceee..a9ca677 100644
--- a/src/ui.xml
+++ b/src/ui.xml
@@ -4,7 +4,7 @@ In the code, GtkUIManager is used to construct them.
 -->
 
 <ui>
-  <menubar>
+  <menubar name="MainMenu">
     <menu action="File">
       <menuitem action="FileNew" />
       <menuitem action="FileOpen" />
@@ -31,7 +31,7 @@ In the code, GtkUIManager is used to construct them.
     </menu>
   </menubar>
   
-  <toolbar>
+  <toolbar name="MainToolbar">
     <toolitem action="FileNew" />
     <toolitem action="FileOpen" />
     <toolitem action="FileSave" />


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]