Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/browser-plugin/plugin.cpp
blob: 15f39ea62e609f96743082c372e542d3abec3ef5 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/* this file is part of evince, a gnome document viewer
 *
 *  Copyright (C) 2009 litl, LLC
 *
 * Evince 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.
 *
 * Evince 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include "evince-view.h"
#include "plugin.h"
#include "scriptable.h"

Plugin::Plugin (NPP instance) : mInstance (instance),
				mWindow (0),
				mScriptableObject (0)
{
	mScrolledWindow = gtk_scrolled_window_new (NULL, NULL);

	mView = ev_view_new ();
	g_signal_connect (G_OBJECT (mView),
			  "notify::sizing-mode",
			  G_CALLBACK (Plugin::SizingModeCallback),
			  reinterpret_cast<void*>(this));

	/* FIXME should be set automatically in EvView. It seem to be currently
	   used only to calculate the minimum and maximux allowed zoom level. */
	ev_view_set_screen_dpi (EV_VIEW(mView), 96);

	gtk_container_add (GTK_CONTAINER(mScrolledWindow), mView);
	gtk_widget_show (mView);

	UpdateSizingMode ();
}

Plugin::~Plugin ()
{
}

void
Plugin::ZoomIn ()
{
	ev_view_set_sizing_mode (EV_VIEW (mView), EV_SIZING_FREE);
	ev_view_zoom_in (EV_VIEW (mView));
}

void
Plugin::ZoomOut ()
{
	ev_view_set_sizing_mode (EV_VIEW (mView), EV_SIZING_FREE);
	ev_view_zoom_out (EV_VIEW (mView));
}

void
Plugin::FindNext (const char *text)
{
}

void
Plugin::FindPrevious (const char *text)
{
}

void
Plugin::SetWindow (GdkNativeWindow window)
{
	if (mWindow != 0) {
		return;
	}

	mWindow = window;

	GtkWidget *plug = gtk_plug_new (window);
	gtk_container_add (GTK_CONTAINER (plug), mScrolledWindow);
	gtk_widget_show (mScrolledWindow);

	gtk_widget_show (plug);
}

void
Plugin::Load (const char *fname)
{
	GError *error = NULL;

	GFile *file = g_file_new_for_path (fname);
	char *uri = g_file_get_uri (file);
	g_object_unref (file);

	/* FIXME display loading error in the UI */

	EvDocument *document = ev_document_factory_get_document (uri, &error);
	if (error) {
		g_warning ("Cannot create document for %s", uri);
		g_error_free (error);
		return;
	}

	ev_view_set_document (EV_VIEW (mView), document);
	g_object_unref (G_OBJECT (document));

	ev_document_load (document, uri, &error);
	if (error) {
		g_warning ("Cannot load %s", uri);
		g_error_free (error);
		return;
	}

	g_free (uri);
}

NPObject * 
Plugin::GetScriptableNPObject () 
{
	if (!mScriptableObject) {
		mScriptableObject = NPN_CreateObject (
			mInstance, GET_NPOBJECT_CLASS (ScriptablePluginObject));
	}

	if (mScriptableObject) {
		NPN_RetainObject (mScriptableObject);
	}

	return mScriptableObject;
}

void
Plugin::UpdateSizingMode ()
{
	EvSizingMode sizingMode;

	g_signal_handlers_disconnect_by_func (
		mView, (gpointer)ev_view_update_view_size, mScrolledWindow);

	g_object_get (G_OBJECT (mView),
		      "sizing-mode", &sizingMode,
		      NULL);

	if (sizingMode != EV_SIZING_FREE)
		ev_view_update_view_size (EV_VIEW (mView),
					  GTK_SCROLLED_WINDOW (mScrolledWindow));

	switch (sizingMode) {
		case EV_SIZING_BEST_FIT:
		case EV_SIZING_FIT_WIDTH:
			g_object_set (G_OBJECT (mScrolledWindow),
				      "hscrollbar-policy", GTK_POLICY_NEVER,
				      "vscrollbar-policy", GTK_POLICY_ALWAYS,
				      NULL);
			g_signal_connect (mView, "zoom_invalid",
					  G_CALLBACK (ev_view_update_view_size),
					  mScrolledWindow);
			break;

	case EV_SIZING_FREE:
		g_object_set (G_OBJECT (mScrolledWindow),
			      "hscrollbar-policy", GTK_POLICY_AUTOMATIC,
			      "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
			      NULL);
		break;
	}
}

void
Plugin::SizingModeCallback (EvView *view, GParamSpec *pspec, gpointer data)
{
	Plugin *plugin = reinterpret_cast<Plugin*> (data);
	plugin->UpdateSizingMode ();
}