]> git.street.me.uk Git - andy/viking.git/blob - src/main.c
Homogeneous icons management.
[andy/viking.git] / src / main.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG
23 #include "config.h"
24 #endif /* HAVE_CONFIG */
25
26 #include "viking.h"
27 #include "icons/icons.h"
28 #include "mapcache.h"
29 #include "background.h"
30 #include "dems.h"
31 #include "curl_download.h"
32 #include "preferences.h"
33
34 #ifdef VIK_CONFIG_GEOCACHES
35 void a_datasource_gc_init();
36 #endif
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include <glib/gprintf.h>
42 #include <glib/gi18n.h>
43
44 #include "modules.h"
45
46 #define MAX_WINDOWS 1024
47
48 /* FIXME LOCALEDIR must be configured by ./configure --localedir */
49 /* But something does not work actually. */
50 /* So, we need to redefine this variable on windows. */
51 #ifdef WINDOWS
52 #undef LOCALEDIR
53 #define LOCALEDIR "locale"
54 #endif
55
56 static guint window_count = 0;
57
58 static VikWindow *new_window ();
59 static void open_window ( VikWindow *vw, const gchar **files );
60 static void destroy( GtkWidget *widget,
61                      gpointer   data );
62
63 /* Callback to mute log message */
64 static void mute_log(const gchar *log_domain,
65                      GLogLevelFlags log_level,
66                      const gchar *message,
67                      gpointer user_data)
68 {
69   /* Nothing to do, we just want to mute */
70 }
71
72 /* Another callback */
73 static void destroy( GtkWidget *widget,
74                      gpointer   data )
75 {
76     if ( ! --window_count )
77       gtk_main_quit ();
78 }
79
80 static VikWindow *new_window ()
81 {
82   if ( window_count < MAX_WINDOWS )
83   {
84     VikWindow *vw = vik_window_new ();
85
86     g_signal_connect (G_OBJECT (vw), "destroy",
87                       G_CALLBACK (destroy), NULL);
88     g_signal_connect (G_OBJECT (vw), "newwindow",
89                       G_CALLBACK (new_window), NULL);
90     g_signal_connect (G_OBJECT (vw), "openwindow",
91                       G_CALLBACK (open_window), NULL);
92
93     gtk_widget_show_all ( GTK_WIDGET(vw) );
94
95     window_count++;
96
97     return vw;
98   }
99   return NULL;
100 }
101
102 static void open_window ( VikWindow *vw, const gchar **files )
103 {
104   VikWindow *newvw = new_window();
105   gboolean change_fn = (!files[1]); /* only change fn if one file */
106   if ( newvw )
107     while ( *files ) {
108       vik_window_open_file ( newvw, *(files++), change_fn );
109     }
110 }
111
112 /* Options */
113 static GOptionEntry entries[] = 
114 {
115   { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL },
116   { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL },
117   { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL },
118   { NULL }
119 };
120
121 int main( int argc, char *argv[] )
122 {
123   VikWindow *first_window;
124   GdkPixbuf *main_icon;
125   gboolean dashdash_already = FALSE;
126   int i = 0;
127   GError *error = NULL;
128   gboolean gui_initialized;
129         
130   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);  
131   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
132   textdomain (GETTEXT_PACKAGE);
133
134   g_thread_init ( NULL );
135   gdk_threads_init ();
136
137   gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
138   if (!gui_initialized)
139   {
140     /* check if we have an error message */
141     if (error == NULL)
142     {
143       /* no error message, the GUI initialization failed */
144       const gchar *display_name = gdk_get_display_arg_name ();
145       g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
146     }
147     else
148     {
149       /* yep, there's an error, so print it */
150       g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
151       g_error_free (error);
152       g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
153     }
154     return EXIT_FAILURE;
155   }
156    
157   if (vik_version)
158   {
159     g_printf ("%s %s, Copyright (c) 2003-2007 Evan Battaglia\n", PACKAGE_NAME, PACKAGE_VERSION);
160     return EXIT_SUCCESS;
161   }
162
163   if (!vik_debug)
164     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
165
166   curl_download_init();
167
168   a_preferences_init ();
169
170   /* Init modules/plugins */
171   modules_init();
172
173   a_mapcache_init ();
174   a_background_init ();
175
176 #ifdef VIK_CONFIG_GEOCACHES
177   a_datasource_gc_init();
178 #endif
179
180   /* Set the icon */
181   main_icon = gdk_pixbuf_from_pixdata(&viking_icon_pixbuf, FALSE, NULL);
182   gtk_window_set_default_icon(main_icon);
183
184   /* Create the first window */
185   first_window = new_window();
186
187   gdk_threads_enter ();
188   while ( ++i < argc ) {
189     if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
190       dashdash_already = TRUE; /* hack to open '-' */
191     else
192       vik_window_open_file ( first_window, argv[i], argc == 2 );
193   }
194
195   gtk_main ();
196   gdk_threads_leave ();
197
198   a_mapcache_uninit ();
199   a_dems_uninit ();
200   a_preferences_uninit ();
201
202   return 0;
203 }