summaryrefslogtreecommitdiff
path: root/src/main.c
blob: a9fd84ba221a74aafae5e9cc3ce13388a160ef41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2001 Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 *   Vincent Geddes <vgeddes@gnome.org>
 */

#include <config.h>

#include "glade-window.h"
#include "glade-resources.h"
#include "glade-splash.h"

#include <gladeui/glade.h>
#include <gladeui/glade-app.h>
#include <gladeui/glade-debug.h>

#include <stdlib.h>
#include <locale.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gmodule.h>

#ifdef G_OS_WIN32
#  include <windows.h>
#endif

#define APPLICATION_NAME (_("Glade"))


/* Application arguments */
static gboolean version = FALSE, without_devhelp = FALSE;
static gchar **files = NULL;

static GOptionEntry option_entries[] = {
  {"version", '\0', 0, G_OPTION_ARG_NONE, &version,
   N_("Output version information and exit"), NULL},

  {"without-devhelp", '\0', 0, G_OPTION_ARG_NONE, &without_devhelp,
   N_("Disable Devhelp integration"), NULL},

  {G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &files,
   NULL, N_("[FILE...]")},

  {NULL}
};

/* Debugging arguments */
static gboolean verbose = FALSE;

static GOptionEntry debug_option_entries[] = {
  {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, N_("be verbose"), NULL},
  {NULL}
};

int
main (int argc, char *argv[])
{
  GladeWindow *window;
  GOptionContext *option_context;
  GOptionGroup *option_group;
  GError *error = NULL;
  gboolean opened_project = FALSE;
  GTimer *timer = NULL;
  GtkWidget *splash;

#ifdef ENABLE_NLS
  setlocale (LC_ALL, "");
  bindtextdomain (GETTEXT_PACKAGE, glade_app_get_locale_dir ());
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  /* Set up option groups */
  option_context = g_option_context_new (NULL);

  g_option_context_set_summary (option_context,
                                N_("Create or edit user interface designs for GTK+ or GNOME applications."));
  g_option_context_set_translation_domain (option_context, GETTEXT_PACKAGE);

  option_group = g_option_group_new ("glade",
                                     N_("Glade options"),
                                     N_("Glade options"), NULL, NULL);
  g_option_group_add_entries (option_group, option_entries);
  g_option_context_set_main_group (option_context, option_group);
  g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);

  option_group = g_option_group_new ("debug",
                                     N_("Glade debug options"),
                                     N_("Show Glade debug options"),
                                     NULL, NULL);
  g_option_group_add_entries (option_group, debug_option_entries);
  g_option_group_set_translation_domain (option_group, GETTEXT_PACKAGE);
  g_option_context_add_group (option_context, option_group);

  /* Add Gtk option group */
  g_option_context_add_group (option_context, gtk_get_option_group (FALSE));

  /* Parse command line */
  if (!g_option_context_parse (option_context, &argc, &argv, &error))
    {
      g_option_context_free (option_context);

      if (error)
        {
          g_print ("%s\n", error->message);
          g_error_free (error);
        }
      else
        g_print ("An unknown error occurred\n");

      return -1;
    }

  g_option_context_free (option_context);
  option_context = NULL;

  if (version != FALSE)
    {
      /* Print version information and exit */
      g_print ("%s\n", PACKAGE_STRING);
      return 0;
    }

  /* Pass NULL here since we parsed the gtk+ args already...
   * from this point on we need a DISPLAY variable to be set.
   */
  gtk_init (NULL, NULL);

  /* Check for gmodule support */
  if (!g_module_supported ())
    {
      g_warning (_("gmodule support not found. gmodule support is required "
                   "for glade to work"));
      return -1;
    }

  g_set_application_name (APPLICATION_NAME);
  gtk_window_set_default_icon_name ("glade");

  glade_setup_log_handlers ();

  /* Splash Screen */
  if ((splash = glade_splash_screen_new ()))
      glade_splash_window_show_immediately (splash);

  /* Main Window */
  window = GLADE_WINDOW (glade_window_new ());

  if (without_devhelp == FALSE)
    glade_window_check_devhelp (window);

  if (splash)
    gtk_widget_destroy (splash);

  /* Update UI before loading files */
  glade_splash_window_show_immediately (GTK_WIDGET (window));

  if (verbose) timer = g_timer_new ();
  
  /* load files specified on commandline */
  if (files != NULL)
    {
      guint i;

      for (i = 0; files[i]; ++i)
        {
          if (verbose) g_timer_start (timer);
          
          if (g_file_test (files[i], G_FILE_TEST_EXISTS) != FALSE)
	    {
	      if (glade_window_open_project (window, files[i]))
		opened_project = TRUE;
	    }
          else
            g_warning (_("Unable to open '%s', the file does not exist.\n"),
                       files[i]);

          if (verbose)
            {
              g_timer_stop (timer);
              g_message ("Loading '%s' took %lf seconds", files[i],
                         g_timer_elapsed (timer, NULL));
            }
        }
      g_strfreev (files);
    }

  if (verbose) g_timer_destroy (timer);
  
  if (!opened_project)
    glade_window_new_project (window);
  
  gtk_main ();

  return 0;
}

#ifdef G_OS_WIN32

/* In case we build this as a windowed application. */

#ifdef __GNUC__
#  ifndef _stdcall
#    define _stdcall  __attribute__((stdcall))
#  endif
#endif

int _stdcall
WinMain (struct HINSTANCE__ *hInstance,
         struct HINSTANCE__ *hPrevInstance, char *lpszCmdLine, int nCmdShow)
{
  return main (__argc, __argv);
}

#endif