1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * gcugtk/application.cc
6  *
7  * Copyright (C) 2005-2011 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA
23  */
24 
25 #include "config.h"
26 #include "application.h"
27 #include "cmd-context-gtk.h"
28 #include "ui-builder.h"
29 #include <gcu/object.h>
30 #include <glib/gi18n-lib.h>
31 
32 namespace gcugtk {
33 
34 WindowState Application::DefaultWindowState = NormalWindowState;
35 static Application *Default = NULL;
36 
37 class ApplicationPrivate
38 {
39 public:
40 	static void MaximizeWindows ();
41 	static void FullScreenWindows ();
42 };
43 
MaximizeWindows()44 void ApplicationPrivate::MaximizeWindows ()
45 {
46 	Application::DefaultWindowState = MaximizedWindowState;
47 }
48 
FullScreenWindows()49 void ApplicationPrivate::FullScreenWindows ()
50 {
51 	Application::DefaultWindowState = FullScreenWindowState;
52 }
53 
on_res_changed(GtkSpinButton * btn,Application * app)54 static void on_res_changed (GtkSpinButton *btn, Application *app)
55 {
56 	app->SetImageResolution (gtk_spin_button_get_value_as_int (btn));
57 }
58 
on_width_changed(GtkSpinButton * btn,Application * app)59 static void on_width_changed (GtkSpinButton *btn, Application *app)
60 {
61 	app->SetImageWidth (gtk_spin_button_get_value_as_int (btn));
62 }
63 
on_height_changed(GtkSpinButton * btn,Application * app)64 static void on_height_changed (GtkSpinButton *btn, Application *app)
65 {
66 	app->SetImageHeight (gtk_spin_button_get_value_as_int (btn));
67 }
68 
on_transparency_changed(GtkToggleButton * btn,Application * app)69 static void on_transparency_changed (GtkToggleButton *btn, Application *app)
70 {
71 	app->SetTransparentBackground (gtk_toggle_button_get_active (btn));
72 }
73 
74 static GOptionEntry options[] =
75 {
76   {"full-screen", 'F', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (void *)ApplicationPrivate::MaximizeWindows, N_("Open new windows full screen"), NULL},
77   {"maximize", 'M', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (void *) ApplicationPrivate::FullScreenWindows, N_("Maximize new windows"), NULL},
78   {NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
79 };
80 
Application(std::string name,std::string datadir,char const * help_name,char const * icon_name,CmdContextGtk * cc)81 Application::Application (std::string name, std::string datadir, char const *help_name, char const *icon_name, CmdContextGtk *cc):
82 	gcu::Application (name, datadir, help_name, icon_name, cc)
83 {
84 	m_RecentManager = gtk_recent_manager_get_default ();
85 	RegisterOptions (options);
86 	GdkScreen *screen = gdk_screen_get_default ();
87 	m_ScreenResolution = (unsigned) rint (gdk_screen_get_width (screen) * 25.4 / gdk_screen_get_width_mm (screen));
88 }
89 
~Application()90 Application::~Application ()
91 {
92 }
93 
CreateDefaultCmdContext()94 void Application::CreateDefaultCmdContext ()
95 {
96 	if (!m_CmdContext)
97 		m_CmdContext = new CmdContextGtk (this);
98 }
99 
GetImageResolutionWidget()100 GtkWidget *Application::GetImageResolutionWidget ()
101 {
102 	UIBuilder *builder = new UIBuilder (UIDIR"/image-resolution.ui", GETTEXT_PACKAGE);
103 	GtkWidget *w = builder->GetWidget ("screen-lbl");
104 	char *buf = g_strdup_printf (_("(screen resolution is %u)"), GetScreenResolution ());
105 	gtk_label_set_text (GTK_LABEL (w), buf);
106 	g_free (buf);
107 	w = builder->GetWidget ("res-btn");
108 	gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), GetImageResolution ());
109 	g_signal_connect (G_OBJECT (w), "value-changed", G_CALLBACK (on_res_changed), this);
110 	w = builder->GetWidget ("transparent-btn");
111 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), GetTransparentBackground ());
112 	g_signal_connect (G_OBJECT (w), "toggled", G_CALLBACK (on_transparency_changed), this);
113 	w = builder->GetRefdWidget ("res-grid");
114 	delete builder;
115 	return w;
116 }
117 
GetImageSizeWidget()118 GtkWidget *Application::GetImageSizeWidget ()
119 {
120 	UIBuilder *builder = new UIBuilder (UIDIR"/image-size.ui", GETTEXT_PACKAGE);
121 	GtkWidget *w = builder->GetWidget ("width");
122 	gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), GetImageWidth ());
123 	g_signal_connect (G_OBJECT (w), "value-changed", G_CALLBACK (on_width_changed), this);
124 	w = builder->GetWidget ("height");
125 	gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), GetImageHeight ());
126 	g_signal_connect (G_OBJECT (w), "value-changed", G_CALLBACK (on_height_changed), this);
127 	w = builder->GetWidget ("transparent-btn");
128 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), GetTransparentBackground ());
129 	g_signal_connect (G_OBJECT (w), "toggled", G_CALLBACK (on_transparency_changed), this);
130 	w = builder->GetRefdWidget ("size-grid");
131 	delete builder;
132 	return w;
133 }
134 
OnMail(GdkScreen * screen,char const * MailAddress)135 void Application::OnMail (GdkScreen *screen, char const *MailAddress)
136 {
137 	go_gtk_url_show (MailAddress, screen);
138 }
139 
ShowURI(GdkScreen * screen,std::string & uri)140 void Application::ShowURI (GdkScreen *screen, std::string& uri)
141 {
142 	go_gtk_url_show (uri.c_str (), screen);
143 }
144 
OnLiveAssistance(GdkScreen * screen)145 void Application::OnLiveAssistance (GdkScreen *screen)
146 {
147 	go_gtk_url_show ("irc://irc.gimp.net/gchemutils", screen);
148 }
149 
GetDefaultApplication()150 Application *Application::GetDefaultApplication ()
151 {
152 	if (!Default)
153 		Default = new Application ("gcugtk"); // the name is just arbitrary
154 	return Default;
155 }
156 
157 }	//	namespace gcugtk
158