File: main.cc

package info (click to toggle)
gelemental 1.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,412 kB
  • ctags: 705
  • sloc: sh: 11,797; cpp: 5,255; makefile: 197
file content (397 lines) | stat: -rw-r--r-- 10,531 bytes parent folder | download | duplicates (6)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * This file is part of gElemental, a periodic table viewer with detailed
 * information on elements.
 *
 * Copyright (C) 2006-2007 Kevin Daughtridge <kevin@kdau.com>
 * Copyright (C) 2003 Jonas Frantz <jonas.frantz@welho.com>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "private.hh"
#include <libelemental/table.hh>
#include "main.hh"

#include <clocale>
#include <iostream>
#include <locale>
#include <glib/goption.h>
#include <glib/gutils.h>
#include <glibmm/optionentry.h>
#include <gtk/gtkaboutdialog.h>
#include <gtk/gtkversion.h>
#include <gtk/gtkwindow.h>
#include <gtkmm/aboutdialog.h>
#include <gtkmm/icontheme.h>
#include <gtkmm/main.h>
#include <gtkmm/radioaction.h>
#include <gtkmm/stock.h>


//******************************************************************************
// Globals


int
main (int argc, char** argv)
{
	return gElemental::main (argc, argv);
}


//******************************************************************************

namespace gElemental {

	
int
main (int& argc, char**& argv)
{
	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	Glib::init ();
	Glib::set_prgname (PACKAGE);
	Glib::set_application_name (_("gElemental"));

	try
	{
		MainOptionGroup opts;
		Glib::OptionContext context;
		context.set_main_group (opts);
#if GLIB_CHECK_VERSION (2, 12, 0)
		g_option_context_set_summary (context.gobj (), _("A periodic table "
			"viewer with detailed information on elements."));
#endif

		Gtk::Main kit (argc, argv, context);
		gtk_window_set_default_icon_name ("gelemental"); // unwrapped function

		try
		{
			std::locale::global (std::locale (std::setlocale (LC_ALL, NULL)));
		}
		catch (std::runtime_error)
		{
			std::locale::global (std::locale::classic ());
		}

		initialize ();

		if (!opts.print.empty ())
		{
			opts.handle_print ();
			if (opts.show.empty () && !opts.interactive)
				return EXIT_SUCCESS;
		}
		
		MainWindow window;
		window.present ();

		if (!opts.show.empty ())
			opts.handle_show (window);

		Gtk::Main::run (window);
		return EXIT_SUCCESS;
	}
	catch (Glib::OptionError &e)
	{
		std::cerr << e.what () << std::endl;
		std::cerr << compose::ucompose (_("Run '%1 --help' to see a full list "
			"of available command line options."), argv[0]) << std::endl;
		return EXIT_FAILURE;
	}
#ifndef ENABLE_DEBUG
	catch (...)
	{
		std::cerr << _("An unknown error occurred.") << std::endl;
		return EXIT_FAILURE;
	}
#endif // !ENABLE_DEBUG
}


//******************************************************************************
// class MainOptionGroup


MainOptionGroup::MainOptionGroup ()
:	Glib::OptionGroup (ustring (), ustring ()), interactive (false)
{
	set_translation_domain (GETTEXT_PACKAGE);

	Glib::OptionEntry entry_print;
	entry_print.set_long_name ("print");
	entry_print.set_short_name ('p');
	entry_print.set_description (N_("Print information on the indicated "
		"element(s) to standard output"));
	entry_print.set_arg_description (N_("SYMBOL_OR_NUMBER"));
	add_entry (entry_print, print);

	Glib::OptionEntry entry_show;
	entry_show.set_long_name ("show");
	entry_show.set_short_name ('s');
	entry_show.set_description (N_("Show the properties dialog for the "
		"indicated element(s)"));
	entry_show.set_arg_description (N_("SYMBOL_OR_NUMBER"));
	add_entry (entry_show, show);

	Glib::OptionEntry entry_interactive;
	entry_interactive.set_long_name ("interactive");
	entry_interactive.set_short_name ('i');
	entry_interactive.set_description (N_("Display the table even if data "
		"have been printed to standard output"));
	add_entry (entry_interactive, interactive);
}


void
MainOptionGroup::handle_print ()
{
	EntriesStream view (std::cout);

	FOREACH (vecustrings, print, el)
	{
		try
			{ get_element (*el).make_entries (view); }
		catch (std::invalid_argument& e)
		{
			throw Glib::OptionError (Glib::OptionError::BAD_VALUE,
				compose::ucompose (_("Unknown element \"%1\"."), *el));
		}
	}
}


void
MainOptionGroup::handle_show (Gtk::Window& window)
{
	FOREACH (vecustrings, show, el)
	{
		try
		{
			ElementDialog *dialog = new ElementDialog (get_element (*el));
			dialog->set_transient_for (window);
			dialog->present ();
		}
		catch (std::invalid_argument& e)
		{
			throw Glib::OptionError (Glib::OptionError::BAD_VALUE,
				compose::ucompose (_("Unknown element \"%1\"."), *el));
		}
	}
}


//******************************************************************************
// class MainWindow


MainWindow::MainWindow ()
:	ui (Gtk::UIManager::create ()), actions (Gtk::ActionGroup::create ()),
	table (NULL), table_state (NULL), list (NULL), list_state (NULL),
	properties (NULL)
{
	add (vbox);
	add_accel_group (ui->get_accel_group ());
	set_title (_("Periodic Table"));

	actions->add (Gtk::Action::create ("FileMenu", _("_File")));
	actions->add (Gtk::Action::create ("FileQuit", Gtk::Stock::QUIT),
		sigc::mem_fun (*this, &MainWindow::hide));

	actions->add (Gtk::Action::create ("ViewMenu", _("_View")));
	Gtk::RadioButtonGroup view_group;
	actions->add (Gtk::RadioAction::create (view_group, "ViewAsTable",
		_("as _Table")), sigc::mem_fun (*this, &MainWindow::show_table));
	actions->add (Gtk::RadioAction::create (view_group, "ViewAsList",
		_("as L_ist")), sigc::mem_fun (*this, &MainWindow::show_list));

	actions->add (Gtk::Action::create ("HelpMenu", _("_Help")));
	actions->add (Gtk::Action::create ("HelpProperties",
		Gtk::Stock::HELP, _("_Properties")), Gtk::AccelKey ("F1"),
		sigc::mem_fun (*this, &MainWindow::show_properties));
	actions->add (Gtk::Action::create ("HelpAbout", Gtk::Stock::ABOUT),
		sigc::mem_fun (*this, &MainWindow::on_about));

	ui->insert_action_group (actions);
	ui->add_ui_from_string
	(
		"<ui><menubar name='MenuBar'>"
			"<menu action='FileMenu'>"
				"<menuitem action='FileQuit' />"
			"</menu>"
			"<menu action='ViewMenu'>"
				"<menuitem action='ViewAsTable' />"
				"<menuitem action='ViewAsList' />"
			"</menu>"
			"<menu action='HelpMenu'>"
				"<menuitem action='HelpProperties' />"
				"<menuitem action='HelpAbout' />"
			"</menu>"
		"</menubar></ui>"
	);

	Gtk::Widget *menu_bar = ui->get_widget ("/MenuBar");
	if (menu_bar != NULL)
		vbox.pack_start (*Gtk::manage (menu_bar), Gtk::PACK_SHRINK);

	show_all_children ();
	show_table ();
}


MainWindow::~MainWindow ()
{
	delete properties;

	delete list_state;
	delete list;

	delete table_state;
	delete table;
}


void
MainWindow::show_properties ()
{
	if (properties == NULL)
	{
		properties = new PropertiesDialog (*this);
		properties->signal_hide ().connect
			(sigc::mem_fun (*this, &MainWindow::hide_properties));
	}
	properties->present ();
}


void
MainWindow::hide_properties ()
{
	delete properties;
	properties = NULL;
}


void
MainWindow::show_table ()
{
	if (list != NULL)
	{
		delete list_state;
		list_state = new TableList::State (list->get_state ());

		vbox.remove (*list);
		delete list;
		list = NULL;
	}

	if (table == NULL)
	{
		table = new TableTable (ui);

		if (table_state != NULL)
		{
			table->set_state (*table_state);
			delete table_state;
			table_state = NULL;
		}

		vbox.pack_start (*table, Gtk::PACK_EXPAND_WIDGET);
		table->grab_focus ();
	}
}


void
MainWindow::show_list ()
{
	if (table != NULL)
	{
		delete table_state;
		table_state = new TableTable::State (table->get_state ());

		vbox.remove (*table);
		delete table;
		table = NULL;
	}

	if (list == NULL)
	{
		list = new TableList (ui);

		if (list_state != NULL)
		{
			list->set_state (*list_state);
			delete list_state;
			list_state = NULL;
		}

		vbox.pack_start (*list, Gtk::PACK_EXPAND_WIDGET);
		list->grab_focus ();
	}
}


void
MainWindow::on_about ()
{
	Gtk::AboutDialog about;
	about.set_transient_for (*this);

	about.set_name (_("gElemental"));
	about.set_version (VERSION);
	about.set_comments (_("A periodic table viewer with detailed "
		"information on elements."));
	about.set_website ("http://www.kdau.com/projects/gelemental/");
	about.set_logo_icon_name ("gelemental");

	about.set_copyright
	(
		"Copyright © 2006-2007 Kevin Daughtridge\n"
		"Copyright © 2003 Jonas Frantz"
	);
	about.set_license (_("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 3 of the License, or (at your option) any later version.\n\nThis 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.\n\nYou should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>."));
#if GTK_CHECK_VERSION (2, 8, 0)
	gtk_about_dialog_set_wrap_license (about.gobj (), true);
#endif

	std::list<ustring> authors;
	authors.push_back ("Kevin Daughtridge <kevin@kdau.com>");
	authors.push_back ("Jonas Frantz <jonas.frantz@welho.com>");
	authors.push_back ("Kyle R. Burton <mortis@voicenet.com>");
	authors.push_back ("Jon Abbott <?>");
	authors.push_back ("Costantino Ceoldo <antigone34@libero.it>");
	authors.push_back ("Sebastien Bacher <seb128@debian.org>");
	authors.push_back ("Andrew Dalke <info@dalkescientific.com>");
	authors.push_back ("Georges Khaznadar <khazuada@univ-littoral.fr>");
	authors.push_back ("Arkadiusz Lipiec <alipiec@elka.pw.edu.pl>");
	authors.push_back ("Jean Meloche <?>");
	authors.push_back ("Piotr Najman <picio@bogucice.pl>");
	authors.push_back ("Felix Natter <f.natter@ndh.net>");
	authors.push_back ("Ambrogio Oliva <ambrogio.oliva@ctimilano.com>");
	authors.push_back ("Arno W. Peters <a.w.peters@ieee.org>");

	about.set_authors (authors);
	// Translators: Replace this with your name(s) and address(es).
	about.set_translator_credits (_("translator-credits"));

	about.present ();
	about.run ();
}


} // namespace gElemental