pdf-presenter/src/PresentationWindow.vala

127 lines
3.3 KiB
Vala

/*
* pdf-presenter - advanced pdf presentation util
*
* Copyright (C) 2009-2012 Sebastian Reichel <sre@ring0.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using Gtk;
class PresentationView : DrawingArea {
Poppler.Document doc;
int page;
bool shown = false;
public PresentationView(Poppler.Document document, int n) {
doc = document;
page = n;
this.expose_event.connect(draw_view);
this.show.connect(() => {shown = true;});
this.hide.connect(() => {shown = false;});
}
public void redraw() {
if(shown) {
unowned Gdk.Region r = this.window.get_clip_region();
this.window.invalidate_region(r, true);
this.window.process_updates(true);
}
}
public void set_page(int n) {
page = n;
redraw();
}
public bool draw_view(Widget w, Gdk.EventExpose e) {
int ww = 0, wh = 0;
int x = 0, y = 0;
var ctx = Gdk.cairo_create(w.window);
var poppler_page = doc.get_page(page);
/* get window size */
get_parent_window().get_size(out ww, out wh);
/* get document size */
double width2, height2;
poppler_page.get_size(out width2, out height2);
/* calculate faktor */
double wf = ww / width2;
double hf = wh / height2;
double faktor = wf > hf ? hf : wf;
/* center image */
int width = (int) (faktor*width2);
int height = (int) (faktor*height2);
if(ww > width)
x = (ww-width)/2;
if(wh > height)
y = (wh-height)/2;
ctx.translate(x,y);
/* scale page */
ctx.scale(faktor, faktor);
/* draw page */
poppler_page.render(ctx);
ctx.stroke();
/* black background */
ctx.translate(-x,-y);
ctx.set_source_rgb(0, 0, 0);
if(x > 0) {
ctx.rectangle(0, 0, x, (int)height2+1);
ctx.fill();
ctx.rectangle(x+(int)width2, 0, x, (int)height2+1);
ctx.fill();
}
if(y > 0) {
ctx.rectangle(0, 0, (int)width2+1, y);
ctx.fill();
ctx.rectangle(0, y+(int)height2, (int)width2+1, y);
ctx.fill();
}
return true;
}
}
class PresentationWindow : Window {
private PresentationView view;
public PresentationWindow(Poppler.Document doc, int page) {
title = "PDF Presentator (View)";
position = WindowPosition.CENTER;
view = new PresentationView(doc, page);
add(view);
set_default_size(800,600);
this.delete_event.connect(hide_on_delete);
return;
}
public void set_page(int n) {
view.set_page(n);
}
}