summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 8a81771fe61e1a12b5a0016460287e4b72673d4e (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
/*
 * 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>
 *   Juan Pablo Ugarte <juanpablougarte@gmail.com>
 */

#include <config.h>

#include "glade-window.h"
#include "glade-resources.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 gboolean verbose = FALSE;

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},

  {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, N_("be verbose"), NULL},

  {NULL}
};

static void
startup (GApplication *application)
{
#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

  glade_setup_log_handlers ();

  g_set_application_name (APPLICATION_NAME);

  gtk_window_set_default_icon_name ("glade");

}

static void
activate (GApplication *application)

{
  GladeWindow *window;

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

  window = GLADE_WINDOW (glade_window_new ());
  gtk_application_add_window (GTK_APPLICATION (application),
                              GTK_WINDOW (window));

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

  gtk_widget_show (GTK_WIDGET (window));

  glade_window_registration_notify_user (window);
}

static void
open (GApplication  *application,
      GFile        **files,
      gint           n_files,
      const gchar   *hint)
{
  GTimer *timer = NULL;
  GtkWindow *window;
  gint i;

  g_application_activate (application);

  window = gtk_application_get_active_window (GTK_APPLICATION (application));
  
  if (verbose) timer = g_timer_new ();

  for (i = 0; i < n_files; i++)
    {
      gchar *path = g_file_get_path (files[i]);

      for (i = 0; files[i]; ++i)
        {
          if (verbose) g_timer_start (timer);

          if (g_file_test (path, G_FILE_TEST_EXISTS) != FALSE)
            glade_window_open_project (GLADE_WINDOW (window), path);
          else
            g_warning (_("Unable to open '%s', the file does not exist.\n"),
                       path);

          if (verbose)
            {
              g_timer_stop (timer);
              g_message ("Loading '%s' took %lf seconds", path,
                         g_timer_elapsed (timer, NULL));
            }
        }

      g_free (path);
    }

  if (verbose) g_timer_destroy (timer);
}

int
main (int argc, char *argv[])
{
  GtkApplication *app;
  int status;

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

  app = gtk_application_new ("org.gnome.Glade", G_APPLICATION_HANDLES_OPEN);

  g_application_set_option_context_summary (G_APPLICATION (app),
                                            N_("Create or edit user interface designs for GTK+ or GNOME applications."));
  g_application_add_main_option_entries (G_APPLICATION (app), option_entries);

  g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  g_signal_connect (app, "open", G_CALLBACK (open), NULL);

  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

#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