summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2011-02-04 20:19:01 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2011-02-04 20:19:01 +0100
commit2b0672a2cbb9731cfa79d4f41453c12e77f15360 (patch)
tree827b38b26806833e6b32ff9992c4a05a33bae1aa
parent6e8839d485050ba5551f8db70d7655cce5a786da (diff)
downloadlogdiag-2b0672a2cbb9731cfa79d4f41453c12e77f15360.tar.gz
logdiag-2b0672a2cbb9731cfa79d4f41453c12e77f15360.tar.xz
logdiag-2b0672a2cbb9731cfa79d4f41453c12e77f15360.zip
Implement zooming in LdWindowMain.
Add convenience methods to LdCanvas.
-rw-r--r--liblogdiag/ld-canvas.c60
-rw-r--r--liblogdiag/ld-canvas.h4
-rw-r--r--src/ld-window-main.c51
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"), "<Ctrl>plus",
Q_("Zoom into the diagram"),
- NULL},
+ G_CALLBACK (on_action_zoom_in)},
{"ZoomOut", GTK_STOCK_ZOOM_OUT, Q_("Zoom _Out"), "<Ctrl>minus",
Q_("Zoom out of the diagram"),
- NULL},
+ G_CALLBACK (on_action_zoom_out)},
{"NormalSize", GTK_STOCK_ZOOM_100, Q_("_Normal Size"), "<Ctrl>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);
}
@@ -785,6 +795,17 @@ on_symbol_chosen (LdLibraryToolbar *toolbar, LdSymbol *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)
{
diagram_new (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);
+}