From 2b0672a2cbb9731cfa79d4f41453c12e77f15360 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Fri, 4 Feb 2011 20:19:01 +0100 Subject: Implement zooming in LdWindowMain. Add convenience methods to LdCanvas. --- liblogdiag/ld-canvas.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++---- liblogdiag/ld-canvas.h | 4 ++++ src/ld-window-main.c | 51 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 105 insertions(+), 10 deletions(-) diff --git a/liblogdiag/ld-canvas.c b/liblogdiag/ld-canvas.c index de24513..60da2e5 100644 --- a/liblogdiag/ld-canvas.c +++ b/liblogdiag/ld-canvas.c @@ -33,8 +33,8 @@ #define ZOOM_MIN 0.01 #define ZOOM_MAX 100 #define ZOOM_DEFAULT 1 -/* Multiplication factor for zooming with mouse wheel. */ -#define ZOOM_WHEEL_STEP 1.4 +/* Multiplication factor for zooming. */ +#define ZOOM_STEP 1.4 /* When drawing is requested, extend all sides of * the rectangle to be drawn by this number of pixels. @@ -862,6 +862,58 @@ ld_canvas_set_zoom (LdCanvas *self, gdouble zoom) g_object_notify (G_OBJECT (self), "zoom"); } +/** + * ld_canvas_can_zoom_in: + * @self: an #LdCanvas object. + * + * Return value: %TRUE if the view can be zoomed in. + */ +gboolean +ld_canvas_can_zoom_in (LdCanvas *self) +{ + g_return_val_if_fail (LD_IS_CANVAS (self), FALSE); + return self->priv->zoom < ZOOM_MAX; +} + +/** + * ld_canvas_can_zoom_out: + * @self: an #LdCanvas object. + * + * Return value: %TRUE if the view can be zoomed out. + */ +gboolean +ld_canvas_can_zoom_out (LdCanvas *self) +{ + g_return_val_if_fail (LD_IS_CANVAS (self), FALSE); + return self->priv->zoom > ZOOM_MIN; +} + +/** + * ld_canvas_zoom_in: + * @self: an #LdCanvas object. + * + * Zoom the view in. + */ +void +ld_canvas_zoom_in (LdCanvas *self) +{ + g_return_if_fail (LD_IS_CANVAS (self)); + ld_canvas_set_zoom (self, self->priv->zoom * ZOOM_STEP); +} + +/** + * ld_canvas_zoom_out: + * @self: an #LdCanvas object. + * + * Zoom the view out. + */ +void +ld_canvas_zoom_out (LdCanvas *self) +{ + g_return_if_fail (LD_IS_CANVAS (self)); + ld_canvas_set_zoom (self, self->priv->zoom / ZOOM_STEP); +} + /* ===== Operations ======================================================== */ @@ -1288,10 +1340,10 @@ on_scroll (GtkWidget *widget, GdkEventScroll *event, gpointer user_data) switch (event->direction) { case GDK_SCROLL_UP: - ld_canvas_set_zoom (self, self->priv->zoom * ZOOM_WHEEL_STEP); + ld_canvas_zoom_in (self); break; case GDK_SCROLL_DOWN: - ld_canvas_set_zoom (self, self->priv->zoom / ZOOM_WHEEL_STEP); + ld_canvas_zoom_out (self); break; default: return FALSE; diff --git a/liblogdiag/ld-canvas.h b/liblogdiag/ld-canvas.h index ae42f26..9e2d105 100644 --- a/liblogdiag/ld-canvas.h +++ b/liblogdiag/ld-canvas.h @@ -80,6 +80,10 @@ void ld_canvas_diagram_to_widget_coords (LdCanvas *self, gdouble ld_canvas_get_zoom (LdCanvas *self); void ld_canvas_set_zoom (LdCanvas *self, gdouble zoom); +gboolean ld_canvas_can_zoom_in (LdCanvas *self); +void ld_canvas_zoom_in (LdCanvas *self); +gboolean ld_canvas_can_zoom_out (LdCanvas *self); +void ld_canvas_zoom_out (LdCanvas *self); void ld_canvas_add_object_begin (LdCanvas *self, LdDiagramObject *object); diff --git a/src/ld-window-main.c b/src/ld-window-main.c index 69b8fb2..1bb7827 100644 --- a/src/ld-window-main.c +++ b/src/ld-window-main.c @@ -59,6 +59,9 @@ static void update_title (LdWindowMain *self); static void action_set_sensitive (LdWindowMain *self, const gchar *name, gboolean sensitive); +static void on_canvas_zoom_changed (LdCanvas *canvas, + GParamSpec *pspec, LdWindowMain *self); + static void on_diagram_changed (LdDiagram *diagram, LdWindowMain *self); static void on_diagram_history_changed (LdDiagram *diagram, GParamSpec *pspec, LdWindowMain *self); @@ -97,6 +100,10 @@ static void on_action_redo (GtkAction *action, LdWindowMain *self); static void on_action_delete (GtkAction *action, LdWindowMain *self); static void on_action_select_all (GtkAction *action, LdWindowMain *self); +static void on_action_zoom_in (GtkAction *action, LdWindowMain *self); +static void on_action_zoom_out (GtkAction *action, LdWindowMain *self); +static void on_action_normal_size (GtkAction *action, LdWindowMain *self); + /* ===== Local variables =================================================== */ @@ -147,13 +154,13 @@ static GtkActionEntry wm_action_entries[] = {"ViewMenu", NULL, Q_("_View"), NULL, NULL, NULL}, {"ZoomIn", GTK_STOCK_ZOOM_IN, Q_("_Zoom In"), "plus", Q_("Zoom into the diagram"), - NULL}, + G_CALLBACK (on_action_zoom_in)}, {"ZoomOut", GTK_STOCK_ZOOM_OUT, Q_("Zoom _Out"), "minus", Q_("Zoom out of the diagram"), - NULL}, + G_CALLBACK (on_action_zoom_out)}, {"NormalSize", GTK_STOCK_ZOOM_100, Q_("_Normal Size"), "0", Q_("Reset zoom level back to the default"), - NULL}, + G_CALLBACK (on_action_normal_size)}, {"HelpMenu", NULL, Q_("_Help"), NULL, NULL, NULL}, {"About", GTK_STOCK_ABOUT, Q_("_About"), NULL, @@ -285,6 +292,9 @@ ld_window_main_init (LdWindowMain *self) ld_canvas_set_diagram (priv->canvas, priv->diagram); ld_canvas_set_library (priv->canvas, priv->library); + g_signal_connect (priv->canvas, "notify::zoom", + G_CALLBACK (on_canvas_zoom_changed), self); + ld_library_toolbar_set_library (LD_LIBRARY_TOOLBAR (priv->library_toolbar), priv->library); ld_library_toolbar_set_canvas (LD_LIBRARY_TOOLBAR (priv->library_toolbar), @@ -302,8 +312,6 @@ ld_window_main_init (LdWindowMain *self) action_set_sensitive (self, "Undo", FALSE); action_set_sensitive (self, "Redo", FALSE); action_set_sensitive (self, "Delete", FALSE); - action_set_sensitive (self, "ZoomIn", FALSE); - action_set_sensitive (self, "ZoomOut", FALSE); action_set_sensitive (self, "NormalSize", FALSE); gtk_widget_grab_focus (GTK_WIDGET (priv->canvas)); @@ -515,10 +523,12 @@ diagram_new (LdWindowMain *self) " closing it and creating a new one?")) return; - /* TODO: Reset canvas view to the center. */ ld_diagram_clear (self->priv->diagram); ld_diagram_set_modified (self->priv->diagram, FALSE); + /* TODO: Reset canvas view to the center. */ + ld_canvas_set_zoom (self->priv->canvas, 1); + diagram_set_filename (self, NULL); } @@ -784,6 +794,17 @@ on_symbol_chosen (LdLibraryToolbar *toolbar, LdSymbol *symbol, LD_DIAGRAM_OBJECT (diagram_symbol)); } +static void +on_canvas_zoom_changed (LdCanvas *canvas, GParamSpec *pspec, LdWindowMain *self) +{ + action_set_sensitive (self, "ZoomIn", + ld_canvas_can_zoom_in (self->priv->canvas)); + action_set_sensitive (self, "ZoomOut", + ld_canvas_can_zoom_out (self->priv->canvas)); + action_set_sensitive (self, "NormalSize", + ld_canvas_get_zoom (self->priv->canvas) != 1); +} + static void on_action_new (GtkAction *action, LdWindowMain *self) { @@ -848,3 +869,21 @@ on_action_select_all (GtkAction *action, LdWindowMain *self) { ld_diagram_select_all (self->priv->diagram); } + +static void +on_action_zoom_in (GtkAction *action, LdWindowMain *self) +{ + ld_canvas_zoom_in (self->priv->canvas); +} + +static void +on_action_zoom_out (GtkAction *action, LdWindowMain *self) +{ + ld_canvas_zoom_out (self->priv->canvas); +} + +static void +on_action_normal_size (GtkAction *action, LdWindowMain *self) +{ + ld_canvas_set_zoom (self->priv->canvas, 1); +} -- cgit v1.2.3-70-g09d2