Implement zooming in LdWindowMain.

Add convenience methods to LdCanvas.
This commit is contained in:
Přemysl Eric Janouch 2011-02-04 20:19:01 +01:00
parent 6e8839d485
commit 2b0672a2cb
3 changed files with 105 additions and 10 deletions

View File

@ -33,8 +33,8 @@
#define ZOOM_MIN 0.01 #define ZOOM_MIN 0.01
#define ZOOM_MAX 100 #define ZOOM_MAX 100
#define ZOOM_DEFAULT 1 #define ZOOM_DEFAULT 1
/* Multiplication factor for zooming with mouse wheel. */ /* Multiplication factor for zooming. */
#define ZOOM_WHEEL_STEP 1.4 #define ZOOM_STEP 1.4
/* When drawing is requested, extend all sides of /* When drawing is requested, extend all sides of
* the rectangle to be drawn by this number of pixels. * 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"); 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 ======================================================== */ /* ===== Operations ======================================================== */
@ -1288,10 +1340,10 @@ on_scroll (GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
switch (event->direction) switch (event->direction)
{ {
case GDK_SCROLL_UP: case GDK_SCROLL_UP:
ld_canvas_set_zoom (self, self->priv->zoom * ZOOM_WHEEL_STEP); ld_canvas_zoom_in (self);
break; break;
case GDK_SCROLL_DOWN: case GDK_SCROLL_DOWN:
ld_canvas_set_zoom (self, self->priv->zoom / ZOOM_WHEEL_STEP); ld_canvas_zoom_out (self);
break; break;
default: default:
return FALSE; return FALSE;

View File

@ -80,6 +80,10 @@ void ld_canvas_diagram_to_widget_coords (LdCanvas *self,
gdouble ld_canvas_get_zoom (LdCanvas *self); gdouble ld_canvas_get_zoom (LdCanvas *self);
void ld_canvas_set_zoom (LdCanvas *self, gdouble zoom); 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); void ld_canvas_add_object_begin (LdCanvas *self, LdDiagramObject *object);

View File

@ -59,6 +59,9 @@ static void update_title (LdWindowMain *self);
static void action_set_sensitive (LdWindowMain *self, const gchar *name, static void action_set_sensitive (LdWindowMain *self, const gchar *name,
gboolean sensitive); 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_changed (LdDiagram *diagram, LdWindowMain *self);
static void on_diagram_history_changed (LdDiagram *diagram, static void on_diagram_history_changed (LdDiagram *diagram,
GParamSpec *pspec, LdWindowMain *self); 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_delete (GtkAction *action, LdWindowMain *self);
static void on_action_select_all (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 =================================================== */ /* ===== Local variables =================================================== */
@ -147,13 +154,13 @@ static GtkActionEntry wm_action_entries[] =
{"ViewMenu", NULL, Q_("_View"), NULL, NULL, NULL}, {"ViewMenu", NULL, Q_("_View"), NULL, NULL, NULL},
{"ZoomIn", GTK_STOCK_ZOOM_IN, Q_("_Zoom In"), "<Ctrl>plus", {"ZoomIn", GTK_STOCK_ZOOM_IN, Q_("_Zoom In"), "<Ctrl>plus",
Q_("Zoom into the diagram"), Q_("Zoom into the diagram"),
NULL}, G_CALLBACK (on_action_zoom_in)},
{"ZoomOut", GTK_STOCK_ZOOM_OUT, Q_("Zoom _Out"), "<Ctrl>minus", {"ZoomOut", GTK_STOCK_ZOOM_OUT, Q_("Zoom _Out"), "<Ctrl>minus",
Q_("Zoom out of the diagram"), Q_("Zoom out of the diagram"),
NULL}, G_CALLBACK (on_action_zoom_out)},
{"NormalSize", GTK_STOCK_ZOOM_100, Q_("_Normal Size"), "<Ctrl>0", {"NormalSize", GTK_STOCK_ZOOM_100, Q_("_Normal Size"), "<Ctrl>0",
Q_("Reset zoom level back to the default"), Q_("Reset zoom level back to the default"),
NULL}, G_CALLBACK (on_action_normal_size)},
{"HelpMenu", NULL, Q_("_Help"), NULL, NULL, NULL}, {"HelpMenu", NULL, Q_("_Help"), NULL, NULL, NULL},
{"About", GTK_STOCK_ABOUT, Q_("_About"), 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_diagram (priv->canvas, priv->diagram);
ld_canvas_set_library (priv->canvas, priv->library); 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), ld_library_toolbar_set_library (LD_LIBRARY_TOOLBAR (priv->library_toolbar),
priv->library); priv->library);
ld_library_toolbar_set_canvas (LD_LIBRARY_TOOLBAR (priv->library_toolbar), 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, "Undo", FALSE);
action_set_sensitive (self, "Redo", FALSE); action_set_sensitive (self, "Redo", FALSE);
action_set_sensitive (self, "Delete", 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); action_set_sensitive (self, "NormalSize", FALSE);
gtk_widget_grab_focus (GTK_WIDGET (priv->canvas)); gtk_widget_grab_focus (GTK_WIDGET (priv->canvas));
@ -515,10 +523,12 @@ diagram_new (LdWindowMain *self)
" closing it and creating a new one?")) " closing it and creating a new one?"))
return; return;
/* TODO: Reset canvas view to the center. */
ld_diagram_clear (self->priv->diagram); ld_diagram_clear (self->priv->diagram);
ld_diagram_set_modified (self->priv->diagram, FALSE); 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); diagram_set_filename (self, NULL);
} }
@ -784,6 +794,17 @@ on_symbol_chosen (LdLibraryToolbar *toolbar, LdSymbol *symbol,
LD_DIAGRAM_OBJECT (diagram_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 static void
on_action_new (GtkAction *action, LdWindowMain *self) 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); 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);
}