From 295c83c8d8f0e7bd2833f7b7c1238458b7819fcd Mon Sep 17 00:00:00 2001 From: Alex Merry Date: Tue, 22 May 2012 19:11:32 +0100 Subject: Fix how preview size determination is done PreviewRenderer should always scale to fit the surface size, rather than having to be manually resized. --- tikzit/src/gtk/PreviewRenderer.h | 6 ++-- tikzit/src/gtk/PreviewRenderer.m | 15 ++++----- tikzit/src/gtk/PreviewWindow.m | 68 ++++++++++------------------------------ 3 files changed, 24 insertions(+), 65 deletions(-) diff --git a/tikzit/src/gtk/PreviewRenderer.h b/tikzit/src/gtk/PreviewRenderer.h index c9e89a7..d691722 100644 --- a/tikzit/src/gtk/PreviewRenderer.h +++ b/tikzit/src/gtk/PreviewRenderer.h @@ -30,14 +30,12 @@ TikzDocument *document; PopplerDocument *pdfDocument; PopplerPage *pdfPage; - double width; - double height; } @property (readonly) Preambles *preambles; @property (retain) TikzDocument *document; -@property (assign) double height; -@property (assign) double width; +@property (readonly) double height; +@property (readonly) double width; - (id) initWithPreambles:(Preambles*)p config:(Configuration*)c; diff --git a/tikzit/src/gtk/PreviewRenderer.m b/tikzit/src/gtk/PreviewRenderer.m index cb8e4f8..43fbe98 100644 --- a/tikzit/src/gtk/PreviewRenderer.m +++ b/tikzit/src/gtk/PreviewRenderer.m @@ -25,7 +25,6 @@ @implementation PreviewRenderer @synthesize preambles, document; -@synthesize width, height; - (id) init { [self release]; @@ -42,8 +41,6 @@ preambles = [p retain]; pdfDocument = NULL; pdfPage = NULL; - width = 150.0; - height = 150.0; } return self; @@ -200,7 +197,7 @@ return pdfPage ? YES : NO; } -/*- (double) width { +- (double) width { double w = 0.0; if (pdfPage) poppler_page_get_size(pdfPage, &w, NULL); @@ -212,7 +209,7 @@ if (pdfPage) poppler_page_get_size(pdfPage, NULL, &h); return h; -}*/ +} - (void) renderWithContext:(id)c onSurface:(id)surface { if (document != nil && pdfPage) { @@ -224,15 +221,15 @@ if (w==0) w = 1.0; if (h==0) h = 1.0; - double scale = ([self height] / h) * 0.95; - if (w * scale > [self width]) scale = [self width] / w; + double scale = ([surface height] / h) * 0.95; + if (w * scale > [surface width]) scale = [surface width] / w; [[surface transformer] setScale:scale]; NSPoint origin; w *= scale; h *= scale; - origin.x = ([self width] - w) / 2; - origin.y = ([self height] - h) / 2; + origin.x = ([surface width] - w) / 2; + origin.y = ([surface height] - h) / 2; [[surface transformer] setOrigin:origin]; diff --git a/tikzit/src/gtk/PreviewWindow.m b/tikzit/src/gtk/PreviewWindow.m index 2540dda..5443e8d 100644 --- a/tikzit/src/gtk/PreviewWindow.m +++ b/tikzit/src/gtk/PreviewWindow.m @@ -26,14 +26,10 @@ @interface PreviewWindow (Private) - (BOOL) updateOrShowError; +- (void) updateDefaultSize; @end // {{{ API -// {{{ Signals -static gboolean window_configure_event_cb (GtkWindow *window, - GdkEvent *event, - PreviewWindow *preview); -// }}} @implementation PreviewWindow @@ -52,19 +48,15 @@ static gboolean window_configure_event_cb (GtkWindow *window, window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL)); gtk_window_set_title (window, "Preview"); - //gtk_window_set_resizable (window, FALSE); + gtk_window_set_resizable (window, TRUE); + gtk_window_set_default_size (window, 150.0, 150.0); g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK (gtk_widget_hide_on_delete), NULL); - g_signal_connect (G_OBJECT(window), - "configure-event", - G_CALLBACK (window_configure_event_cb), - self); GtkWidget *pdfArea = gtk_drawing_area_new (); gtk_container_add (GTK_CONTAINER (window), pdfArea); - //gtk_widget_set_size_request (pdfArea, 150.0, 150.0); gtk_widget_show (pdfArea); surface = [[WidgetSurface alloc] initWithWidget:pdfArea]; [surface setRenderDelegate:previewer]; @@ -91,38 +83,9 @@ static gboolean window_configure_event_cb (GtkWindow *window, [previewer setDocument:doc]; } -// - (void) updateSize { -// double width = 150; -// double height = 150; -// if ([previewer isValid]) { -// double pWidth = [previewer width]; -// double pHeight = [previewer height]; -// width = (width < pWidth + 4) ? pWidth + 4 : width; -// height = (height < pHeight + 4) ? pHeight + 4 : height; -// NSPoint offset; -// offset.x = (width-pWidth)/2.0; -// offset.y = (height-pHeight)/2.0; -// [[surface transformer] setOrigin:offset]; -// } -// [surface setSizeRequestWidth:width height:height]; -// } - -- (void) updateSize { - gint w, h; - gtk_window_get_size(window, &w, &h); - double width = (double)w; - double height = (double)h; - [previewer setWidth:width]; - [previewer setHeight:height]; - gdk_window_resize(gtk_widget_get_window([surface widget]), w, h); - [surface invalidate]; - - //NSLog(@"got that resize event! --> (%d, %d)", [surface width], [surface height]); -} - - (BOOL) update { if ([self updateOrShowError]) { - [self updateSize]; + [self updateDefaultSize]; return YES; } @@ -131,7 +94,7 @@ static gboolean window_configure_event_cb (GtkWindow *window, - (void) show { if ([self updateOrShowError]) { - [self updateSize]; + [self updateDefaultSize]; gtk_widget_show (GTK_WIDGET (window)); [surface invalidate]; } @@ -208,17 +171,18 @@ static gboolean window_configure_event_cb (GtkWindow *window, } return YES; } -@end -// {{{ GTK+ callbacks -static gboolean window_configure_event_cb (GtkWindow *window, - GdkEvent *event, - PreviewWindow *preview) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - [preview updateSize]; - [pool drain]; - return TRUE; // we dealt with this event +- (void) updateDefaultSize { + double width = 150; + double height = 150; + if ([previewer isValid]) { + double pWidth = [previewer width]; + double pHeight = [previewer height]; + width = (width < pWidth + 4) ? pWidth + 4 : width; + height = (height < pHeight + 4) ? pHeight + 4 : height; + } + gtk_window_set_default_size (window, width, height); } -// }}} +@end // vim:ft=objc:ts=8:et:sts=4:sw=4 -- cgit v1.2.3