graph_displayer_canvas-zoom.cc
author ladanyi
Fri, 04 Nov 2005 19:07:15 +0000
branchgui
changeset 87 b44281e4cca7
parent 59 c38925cc6a4d
child 89 4042761b21e3
permissions -rwxr-xr-x
improved zooming
ladanyi@53
     1
#include "graph_displayer_canvas.h"
ladanyi@53
     2
#include "broken_edge.h"
alpar@59
     3
#include <cmath>
hegyi@27
     4
hegyi@27
     5
void GraphDisplayerCanvas::zoomIn()
hegyi@27
     6
{
hegyi@27
     7
  set_pixels_per_unit(
hegyi@27
     8
      (1.0 + (double) zoom_step / 100.0) * get_pixels_per_unit());
hegyi@27
     9
}
hegyi@27
    10
hegyi@27
    11
void GraphDisplayerCanvas::zoomOut()
hegyi@27
    12
{
hegyi@27
    13
  set_pixels_per_unit(
hegyi@27
    14
      (1.0 - (double) zoom_step / 100.0) * get_pixels_per_unit());
hegyi@27
    15
}
hegyi@27
    16
hegyi@27
    17
void GraphDisplayerCanvas::zoomFit()
hegyi@27
    18
{
ladanyi@87
    19
  updateScrollRegion();
ladanyi@87
    20
hegyi@27
    21
  // get the height and width of the canvas
hegyi@27
    22
  Gtk::Allocation a = get_allocation();
hegyi@27
    23
  int aw = a.get_width();
hegyi@27
    24
  int ah = a.get_height();
hegyi@27
    25
hegyi@27
    26
  // get the bounding box of the graph
ladanyi@87
    27
  update_now();
ladanyi@87
    28
  double x1, y1, x2, y2;
ladanyi@87
    29
  root()->get_bounds(x1, y1, x2, y2);
hegyi@27
    30
hegyi@27
    31
  // fit the graph to the window
ladanyi@87
    32
  double ppu1 = (double) aw / fabs(x2 - x1);
ladanyi@87
    33
  double ppu2 = (double) ah / fabs(y2 - y1);
hegyi@27
    34
  set_pixels_per_unit((ppu1 < ppu2) ? ppu1 : ppu2);
hegyi@27
    35
}
hegyi@27
    36
hegyi@27
    37
void GraphDisplayerCanvas::zoom100()
hegyi@27
    38
{
ladanyi@87
    39
  updateScrollRegion();
hegyi@27
    40
  set_pixels_per_unit(1.0);
hegyi@27
    41
}
hegyi@27
    42
hegyi@27
    43
void GraphDisplayerCanvas::updateScrollRegion()
hegyi@27
    44
{
ladanyi@87
    45
  // get_bounds() yields something sane only when no updates are pending
ladanyi@87
    46
  // and it returns a sufficient, not an exact bounding box
ladanyi@87
    47
  update_now();
ladanyi@87
    48
  double x1, y1, x2, y2;
ladanyi@87
    49
  root()->get_bounds(x1, y1, x2, y2);
ladanyi@87
    50
  set_scroll_region(x1, y1, x2, y2);
hegyi@27
    51
}