diff src/gtk/main.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/gtk/main.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,177 @@
     1.4 +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
     1.5 +// Copyright (C) 1999-2003 Forgotten
     1.6 +// Copyright (C) 2004 Forgotten and the VBA development team
     1.7 +
     1.8 +// This program is free software; you can redistribute it and/or modify
     1.9 +// it under the terms of the GNU General Public License as published by
    1.10 +// the Free Software Foundation; either version 2, or(at your option)
    1.11 +// any later version.
    1.12 +//
    1.13 +// This program is distributed in the hope that it will be useful,
    1.14 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +// GNU General Public License for more details.
    1.17 +//
    1.18 +// You should have received a copy of the GNU General Public License
    1.19 +// along with this program; if not, write to the Free Software Foundation,
    1.20 +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.21 +
    1.22 +#include <limits.h>
    1.23 +#include <stdlib.h>
    1.24 +#include "../getopt.h"
    1.25 +
    1.26 +#include <list>
    1.27 +
    1.28 +#include <libglademm.h>
    1.29 +#include <gtkmm/main.h>
    1.30 +#include <gtkmm/window.h>
    1.31 +#include <gtkmm/messagedialog.h>
    1.32 +
    1.33 +#include "images/vba-wm-pixbufs.h"
    1.34 +
    1.35 +#include "window.h"
    1.36 +#include "intl.h"
    1.37 +
    1.38 +using Gnome::Glade::Xml;
    1.39 +
    1.40 +static const char * csProgramName;
    1.41 +
    1.42 +static int iShowHelp;
    1.43 +static int iShowVersion;
    1.44 +
    1.45 +// Non-characters used for long options that have no equivalent short option
    1.46 +enum
    1.47 +{
    1.48 +  IGNORED_OPTION = CHAR_MAX + 1
    1.49 +};
    1.50 +
    1.51 +static const char csShortOptions[] = "V";
    1.52 +
    1.53 +static const struct option astLongOptions[] =
    1.54 +{
    1.55 +  { "help",    no_argument, &iShowHelp, IGNORED_OPTION },
    1.56 +  { "version", no_argument, NULL,       'V'            },
    1.57 +  { 0, 0, 0, 0 }
    1.58 +};
    1.59 +
    1.60 +static void vUsage(int iStatus)
    1.61 +{
    1.62 +  if (iStatus != 0)
    1.63 +  {
    1.64 +    g_printerr(_("Try `%s --help' for more information.\n"), csProgramName);
    1.65 +  }
    1.66 +  else
    1.67 +  {
    1.68 +    g_print(_("Usage: %s [option ...] [file]\n"), csProgramName);
    1.69 +    g_print(_("\
    1.70 +\n\
    1.71 +Options:\n\
    1.72 +      --help            Output this help.\n\
    1.73 +  -V, --version         Output version information.\n\
    1.74 +"));
    1.75 +  }
    1.76 +
    1.77 +  exit(iStatus);
    1.78 +}
    1.79 +
    1.80 +static void vSetDefaultWindowIcon()
    1.81 +{
    1.82 +  const guint8 * apuiInlinePixbuf[] =
    1.83 +  {
    1.84 +    stock_vba_wm_16,
    1.85 +    stock_vba_wm_32,
    1.86 +    stock_vba_wm_48,
    1.87 +    stock_vba_wm_64
    1.88 +  };
    1.89 +
    1.90 +  std::list<Glib::RefPtr<Gdk::Pixbuf> > listPixbuf;
    1.91 +  for (guint i = 0; i < G_N_ELEMENTS(apuiInlinePixbuf); i++)
    1.92 +  {
    1.93 +    listPixbuf.push_back(
    1.94 +      Gdk::Pixbuf::create_from_inline(-1, apuiInlinePixbuf[i]));
    1.95 +  }
    1.96 +
    1.97 +  Gtk::Window::set_default_icon_list(listPixbuf);
    1.98 +}
    1.99 +
   1.100 +int main(int argc, char * argv[])
   1.101 +{
   1.102 +  csProgramName = argv[0];
   1.103 +
   1.104 +#ifdef ENABLE_NLS
   1.105 +  setlocale(LC_ALL, "");
   1.106 +  bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
   1.107 +  textdomain(GETTEXT_PACKAGE);
   1.108 +  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
   1.109 +#endif // ENABLE_NLS
   1.110 +
   1.111 +  Gtk::Main oKit(argc, argv);
   1.112 +
   1.113 +  int iOpt;
   1.114 +  while ((iOpt = getopt_long(argc, argv, csShortOptions, astLongOptions, NULL))
   1.115 +         != -1)
   1.116 +  {
   1.117 +    switch (iOpt)
   1.118 +    {
   1.119 +    case 'V':
   1.120 +      iShowVersion = 1;
   1.121 +      break;
   1.122 +    case 0:
   1.123 +      // Long options
   1.124 +      break;
   1.125 +    default:
   1.126 +      vUsage(1);
   1.127 +      break;
   1.128 +    }
   1.129 +  }
   1.130 +
   1.131 +  if (iShowVersion)
   1.132 +  {
   1.133 +    g_print(_("VisualBoyAdvance version %s [GTK+]\n"), VERSION);
   1.134 +    exit(0);
   1.135 +  }
   1.136 +
   1.137 +  if (iShowHelp)
   1.138 +  {
   1.139 +    vUsage(0);
   1.140 +  }
   1.141 +
   1.142 +  vSetDefaultWindowIcon();
   1.143 +
   1.144 +  Glib::RefPtr<Xml> poXml;
   1.145 +  try
   1.146 +  {
   1.147 +    poXml = Xml::create(PKGDATADIR "/vba.glade", "MainWindow");
   1.148 +  }
   1.149 +  catch (const Xml::Error & e)
   1.150 +  {
   1.151 +    Gtk::MessageDialog oDialog(e.what(),
   1.152 +#ifndef GTKMM20
   1.153 +                               false,
   1.154 +#endif // ! GTKMM20
   1.155 +                               Gtk::MESSAGE_ERROR,
   1.156 +                               Gtk::BUTTONS_OK);
   1.157 +    oDialog.run();
   1.158 +    return 1;
   1.159 +  }
   1.160 +
   1.161 +  VBA::Window * poWindow = NULL;
   1.162 +  poXml->get_widget_derived<VBA::Window>("MainWindow", poWindow);
   1.163 +
   1.164 +  if (optind < argc)
   1.165 +  {
   1.166 +    // Display the window before loading the file
   1.167 +    poWindow->show();
   1.168 +    while (Gtk::Main::events_pending())
   1.169 +    {
   1.170 +      Gtk::Main::iteration();
   1.171 +    }
   1.172 +
   1.173 +    poWindow->bLoadROM(argv[optind]);
   1.174 +  }
   1.175 +
   1.176 +  Gtk::Main::run(*poWindow);
   1.177 +  delete poWindow;
   1.178 +
   1.179 +  return 0;
   1.180 +}