1 #define __SP_SVG_VIEW_C__
3 /** \file
4 * Functions and callbacks for generic SVG view and widget
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Ralf Stephan <ralf@ark.in-berlin.de>
9 *
10 * Copyright (C) 2001-2002 Lauris Kaplinski
11 * Copyright (C) 2001 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <gtk/gtkscrolledwindow.h>
17 #include "display/canvas-arena.h"
18 #include "document.h"
19 #include "svg-view.h"
20 #include "svg-view-widget.h"
22 static void sp_svg_view_widget_class_init (SPSVGSPViewWidgetClass *klass);
23 static void sp_svg_view_widget_init (SPSVGSPViewWidget *widget);
24 static void sp_svg_view_widget_destroy (GtkObject *object);
26 static void sp_svg_view_widget_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
27 static void sp_svg_view_widget_size_request (GtkWidget *widget, GtkRequisition *req);
29 static void sp_svg_view_widget_view_resized (SPViewWidget *vw, Inkscape::UI::View::View *view, gdouble width, gdouble height);
31 static SPViewWidgetClass *widget_parent_class;
33 /**
34 * Registers SPSVGSPViewWidget class with Gtk and returns its type number.
35 */
36 GType sp_svg_view_widget_get_type(void)
37 {
38 static GType type = 0;
39 if (!type) {
40 GTypeInfo info = {
41 sizeof(SPSVGSPViewWidgetClass),
42 0, // base_init
43 0, // base_finalize
44 (GClassInitFunc)sp_svg_view_widget_class_init,
45 0, // class_finalize
46 0, // class_data
47 sizeof(SPSVGSPViewWidget),
48 0, // n_preallocs
49 (GInstanceInitFunc)sp_svg_view_widget_init,
50 0 // value_table
51 };
52 type = g_type_register_static(SP_TYPE_VIEW_WIDGET, "SPSVGSPViewWidget", &info, static_cast<GTypeFlags>(0));
53 }
54 return type;
55 }
57 /**
58 * Callback to initialize SPSVGSPViewWidget vtable.
59 */
60 static void
61 sp_svg_view_widget_class_init (SPSVGSPViewWidgetClass *klass)
62 {
63 GtkObjectClass *object_class;
64 GtkWidgetClass *widget_class;
65 SPViewWidgetClass *vw_class;
67 object_class = GTK_OBJECT_CLASS (klass);
68 widget_class = GTK_WIDGET_CLASS (klass);
69 vw_class = SP_VIEW_WIDGET_CLASS (klass);
71 widget_parent_class = (SPViewWidgetClass*)gtk_type_class (SP_TYPE_VIEW_WIDGET);
73 object_class->destroy = sp_svg_view_widget_destroy;
75 widget_class->size_allocate = sp_svg_view_widget_size_allocate;
76 widget_class->size_request = sp_svg_view_widget_size_request;
78 vw_class->view_resized = sp_svg_view_widget_view_resized;
79 }
81 /**
82 * Callback to initialize SPSVGSPViewWidget object.
83 */
84 static void
85 sp_svg_view_widget_init (SPSVGSPViewWidget *vw)
86 {
87 GtkStyle *style;
88 SPCanvasItem *parent;
90 /* Settings */
91 vw->resize = FALSE;
92 vw->maxwidth = 400.0;
93 vw->maxheight = 400.0;
95 /* ScrolledWindow */
96 vw->sw = gtk_scrolled_window_new (NULL, NULL);
97 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(vw->sw), GTK_SHADOW_NONE);
98 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (vw->sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
99 gtk_container_add (GTK_CONTAINER (vw), vw->sw);
100 gtk_widget_show (vw->sw);
102 /* Canvas */
103 gtk_widget_push_visual (gdk_rgb_get_visual ());
104 gtk_widget_push_colormap (gdk_rgb_get_cmap ());
105 vw->canvas = sp_canvas_new_aa ();
106 gtk_widget_pop_colormap ();
107 gtk_widget_pop_visual ();
108 style = gtk_style_copy (vw->canvas->style);
109 style->bg[GTK_STATE_NORMAL] = style->white;
110 gtk_widget_set_style (vw->canvas, style);
111 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (vw->sw), vw->canvas);
112 gtk_widget_show (vw->canvas);
114 /* View */
115 parent = sp_canvas_item_new (sp_canvas_root (SP_CANVAS (vw->canvas)), SP_TYPE_CANVAS_GROUP, NULL);
116 Inkscape::UI::View::View *view = Inkscape::GC::release(new SPSVGView (SP_CANVAS_GROUP (parent)));
117 sp_view_widget_set_view (SP_VIEW_WIDGET (vw), view);
118 }
120 /*
121 * Destructor callback for SPSVGSPViewWidget objects.
122 */
123 static void
124 sp_svg_view_widget_destroy (GtkObject *object)
125 {
126 SPSVGSPViewWidget *vw = SP_SVG_VIEW_WIDGET (object);
128 vw->canvas = NULL;
130 if (((GtkObjectClass *) (widget_parent_class))->destroy)
131 (* ((GtkObjectClass *) (widget_parent_class))->destroy) (object);
132 }
134 /**
135 * Callback connected with size_request signal.
136 */
137 static void
138 sp_svg_view_widget_size_request (GtkWidget *widget, GtkRequisition *req)
139 {
140 SPSVGSPViewWidget *vw = SP_SVG_VIEW_WIDGET (widget);
141 Inkscape::UI::View::View *v = SP_VIEW_WIDGET_VIEW (widget);
143 if (((GtkWidgetClass *) (widget_parent_class))->size_request)
144 (* ((GtkWidgetClass *) (widget_parent_class))->size_request) (widget, req);
146 if (v->doc()) {
147 SPSVGView *svgv;
148 GtkPolicyType hpol, vpol;
149 gdouble width, height;
151 svgv = static_cast<SPSVGView*> (v);
152 width = sp_document_width (v->doc()) * svgv->_hscale;
153 height = sp_document_height (v->doc()) * svgv->_vscale;
155 if (width <= vw->maxwidth) {
156 hpol = GTK_POLICY_NEVER;
157 req->width = (gint) (width + 0.5);
158 } else {
159 hpol = GTK_POLICY_AUTOMATIC;
160 req->width = (gint) (vw->maxwidth + 0.5);
161 }
162 if (height <= vw->maxheight) {
163 vpol = GTK_POLICY_NEVER;
164 req->height = (gint) (height + 8.0);
165 } else {
166 vpol = GTK_POLICY_AUTOMATIC;
167 req->height = (gint) (vw->maxheight + 2.0);
168 }
169 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (vw->sw), hpol, vpol);
170 }
171 }
173 /**
174 * Callback connected with size_allocate signal.
175 */
176 static void
177 sp_svg_view_widget_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
178 {
179 SPSVGSPViewWidget *svgvw = SP_SVG_VIEW_WIDGET (widget);
181 if (((GtkWidgetClass *) (widget_parent_class))->size_allocate)
182 (* ((GtkWidgetClass *) (widget_parent_class))->size_allocate) (widget, allocation);
184 if (!svgvw->resize) {
185 static_cast<SPSVGView*>(SP_VIEW_WIDGET_VIEW (svgvw))->setRescale (TRUE, TRUE,
186 (gdouble) allocation->width - 1.0, (gdouble) allocation->height - 1.0);
187 }
188 }
190 /**
191 * Callback connected with view_resized signal.
192 */
193 static void
194 sp_svg_view_widget_view_resized (SPViewWidget *vw, Inkscape::UI::View::View */*view*/, gdouble width, gdouble height)
195 {
196 SPSVGSPViewWidget *svgvw = SP_SVG_VIEW_WIDGET (vw);
198 if (svgvw->resize) {
199 gtk_widget_set_size_request (svgvw->canvas, (int)width, (int)height);
200 gtk_widget_queue_resize (GTK_WIDGET (vw));
201 }
202 }
204 /**
205 * Constructs new SPSVGSPViewWidget object and returns pointer to it.
206 */
207 GtkWidget *
208 sp_svg_view_widget_new (SPDocument *doc)
209 {
210 GtkWidget *widget;
212 g_return_val_if_fail (doc != NULL, NULL);
214 widget = (GtkWidget*)gtk_type_new (SP_TYPE_SVG_VIEW_WIDGET);
216 reinterpret_cast<SPSVGView*>(SP_VIEW_WIDGET_VIEW (widget))->setDocument (doc);
218 return widget;
219 }
221 /**
222 * Flags the SPSVGSPViewWidget to have its size renegotiated with Gtk.
223 */
224 void
225 sp_svg_view_widget_set_resize (SPSVGSPViewWidget *vw, bool resize, gdouble width, gdouble height)
226 {
227 g_return_if_fail (vw != NULL);
229 g_return_if_fail (SP_IS_SVG_VIEW_WIDGET (vw));
230 g_return_if_fail (!resize || (width > 0.0));
231 g_return_if_fail (!resize || (height > 0.0));
233 vw->resize = resize;
234 vw->maxwidth = width;
235 vw->maxheight = height;
237 if (resize) {
238 gtk_widget_queue_resize (GTK_WIDGET (vw));
239 }
240 }
243 /*
244 Local Variables:
245 mode:c++
246 c-file-style:"stroustrup"
247 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
248 indent-tabs-mode:nil
249 fill-column:99
250 End:
251 */
252 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :