gimpdisplayshell-rulers.c 5.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

Ell's avatar
Ell committed
20 21
#include <math.h>

22 23 24 25
#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
Ell's avatar
Ell committed
26
#include "libgimpmath/gimpmath.h"
27 28 29 30 31 32 33 34 35
#include "libgimpwidgets/gimpwidgets.h"

#include "display-types.h"

#include "core/gimpimage.h"

#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-rulers.h"
Ell's avatar
Ell committed
36
#include "gimpdisplayshell-scale.h"
37 38 39 40 41 42 43 44 45 46 47 48 49


/**
 * gimp_display_shell_rulers_update:
 * @shell:
 *
 **/
void
gimp_display_shell_rulers_update (GimpDisplayShell *shell)
{
  GimpImage *image;
  gint       image_width;
  gint       image_height;
Ell's avatar
Ell committed
50 51 52 53
  gdouble    offset_x     = 0.0;
  gdouble    offset_y     = 0.0;
  gdouble    scale_x      = 1.0;
  gdouble    scale_y      = 1.0;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  gdouble    resolution_x = 1.0;
  gdouble    resolution_y = 1.0;
  gdouble    horizontal_lower;
  gdouble    horizontal_upper;
  gdouble    horizontal_max_size;
  gdouble    vertical_lower;
  gdouble    vertical_upper;
  gdouble    vertical_max_size;

  if (! shell->display)
    return;

  image = gimp_display_get_image (shell->display);

  if (image)
    {
Ell's avatar
Ell committed
70 71 72 73 74 75 76 77 78 79 80
      gint    image_x, image_y;
      gdouble res_x, res_y;

      gimp_display_shell_scale_get_image_bounds (shell,
                                                 &image_x, &image_y,
                                                 &image_width, &image_height);

      gimp_display_shell_get_rotated_scale (shell, &scale_x, &scale_y);

      image_width  /= scale_x;
      image_height /= scale_y;
81

Ell's avatar
Ell committed
82 83 84 85 86
      offset_x = shell->offset_x - image_x;
      offset_y = shell->offset_y - image_y;

      gimp_image_get_resolution (image, &res_x, &res_y);

87
      if (shell->rotate_angle == 0.0 || res_x == res_y)
Ell's avatar
Ell committed
88
        {
89 90
          resolution_x = res_x;
          resolution_y = res_y;
Ell's avatar
Ell committed
91 92 93
        }
      else
        {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
          gdouble cos_a = cos (G_PI * shell->rotate_angle / 180.0);
          gdouble sin_a = sin (G_PI * shell->rotate_angle / 180.0);

          if (shell->dot_for_dot)
            {
              resolution_x = 1.0 / sqrt (SQR (cos_a / res_x) +
                                         SQR (sin_a / res_y));
              resolution_y = 1.0 / sqrt (SQR (cos_a / res_y) +
                                         SQR (sin_a / res_x));
            }
          else
            {
              resolution_x = sqrt (SQR (res_x * cos_a) + SQR (res_y * sin_a));
              resolution_y = sqrt (SQR (res_y * cos_a) + SQR (res_x * sin_a));
            }
Ell's avatar
Ell committed
109
        }
110 111 112 113 114 115 116 117 118 119 120 121 122 123
    }
  else
    {
      image_width  = shell->disp_width;
      image_height = shell->disp_height;
    }

  /* Initialize values */

  horizontal_lower = 0;
  vertical_lower   = 0;

  if (image)
    {
Ell's avatar
Ell committed
124
      horizontal_upper    = gimp_pixels_to_units (shell->disp_width / scale_x,
125 126 127 128 129 130 131
                                                  shell->unit,
                                                  resolution_x);
      horizontal_max_size = gimp_pixels_to_units (MAX (image_width,
                                                       image_height),
                                                  shell->unit,
                                                  resolution_x);

Ell's avatar
Ell committed
132
      vertical_upper      = gimp_pixels_to_units (shell->disp_height / scale_y,
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                                                  shell->unit,
                                                  resolution_y);
      vertical_max_size   = gimp_pixels_to_units (MAX (image_width,
                                                       image_height),
                                                  shell->unit,
                                                  resolution_y);
    }
  else
    {
      horizontal_upper    = image_width;
      horizontal_max_size = MAX (image_width, image_height);

      vertical_upper      = image_height;
      vertical_max_size   = MAX (image_width, image_height);
    }


  /* Adjust due to scrolling */

  if (image)
    {
Ell's avatar
Ell committed
154 155
      offset_x *= horizontal_upper / shell->disp_width;
      offset_y *= vertical_upper   / shell->disp_height;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

      horizontal_lower += offset_x;
      horizontal_upper += offset_x;

      vertical_lower   += offset_y;
      vertical_upper   += offset_y;
    }

  /* Finally setup the actual rulers */

  gimp_ruler_set_range (GIMP_RULER (shell->hrule),
                        horizontal_lower,
                        horizontal_upper,
                        horizontal_max_size);

  gimp_ruler_set_unit  (GIMP_RULER (shell->hrule),
                        shell->unit);

  gimp_ruler_set_range (GIMP_RULER (shell->vrule),
                        vertical_lower,
                        vertical_upper,
                        vertical_max_size);

  gimp_ruler_set_unit  (GIMP_RULER (shell->vrule),
                        shell->unit);
}