Index: app/app.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/app/app.c,v
retrieving revision 1.244
retrieving revision 1.245
diff -u -p -r1.244 -r1.245
--- app/app.c	2 Aug 2006 22:27:47 -0000	1.244
+++ app/app.c	7 Sep 2006 18:55:42 -0000	1.245
@@ -454,9 +454,14 @@ gwy_app_menu_data_popup_create(GtkAccelG
             mask = gwy_process_func_get_sensitivity_mask(menu_items[i].cbdata);
             gwy_sensitivity_group_add_widget(sensgroup, item, mask);
         }
-        else
+        else {
             item = gtk_menu_item_new_with_mnemonic(_(menu_items[i].label));
 
+            if (menu_items[i].callback == gwy_app_change_mask_color_cb)
+                gwy_sensitivity_group_add_widget(sensgroup, item,
+                                                 GWY_MENU_FLAG_DATA_MASK);
+        }
+
         if (menu_items[i].key)
             gtk_widget_add_accelerator(item, "activate", accel_group,
                                        menu_items[i].key, menu_items[i].mods,
Index: app/toolbox.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/app/toolbox.c,v
retrieving revision 1.136
retrieving revision 1.139
diff -u -p -r1.136 -r1.139
--- app/toolbox.c	5 Aug 2006 19:24:09 -0000	1.136
+++ app/toolbox.c	9 Sep 2006 11:24:41 -0000	1.139
@@ -115,8 +115,10 @@ set_sensitivity(GtkItemFactory *item_fac
     while ((path = va_arg(ap, const gchar*))) {
         mask = va_arg(ap, guint);
         widget = gtk_item_factory_get_widget(item_factory, path);
-        if (!widget)
-            break;
+        if (!widget) {
+            g_warning("Cannot find menu item %s", path);
+            continue;
+        }
         gwy_sensitivity_group_add_widget(sensgroup, widget, mask);
     }
     va_end(ap);
@@ -604,7 +614,7 @@ gwy_app_menu_create_file_menu(GtkAccelGr
     set_sensitivity(item_factory,
                     "<file>/Save",         GWY_MENU_FLAG_DATA,
                     "<file>/Save As",      GWY_MENU_FLAG_DATA,
-                    "<file>/Close Window", GWY_MENU_FLAG_DATA,
+                 /* "<file>/Close Window", GWY_MENU_FLAG_DATA, */
                     NULL);
 
     gwy_app_menu_set_recent_files_menu
@@ -705,8 +715,6 @@ gwy_app_menu_create_edit_menu(GtkAccelGr
                     "<edit>/Duplicate", GWY_MENU_FLAG_DATA,
                     "<edit>/Undo", GWY_MENU_FLAG_UNDO,
                     "<edit>/Redo", GWY_MENU_FLAG_REDO,
-                    "<edit>/Remove Mask", GWY_MENU_FLAG_DATA_MASK,
-                    "<edit>/Remove Presentation", GWY_MENU_FLAG_DATA_SHOW,
                     "<edit>/Mask Color...", GWY_MENU_FLAG_DATA_MASK,
                     NULL);
 
Index: libgwydgets/gwycoloraxis.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/libgwydgets/gwycoloraxis.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -p -r1.42 -r1.43
--- libgwydgets/gwycoloraxis.c	28 Jul 2006 13:20:35 -0000	1.42
+++ libgwydgets/gwycoloraxis.c	8 Sep 2006 12:26:42 -0000	1.43
@@ -584,7 +584,11 @@ gwy_color_axis_draw_ticks(GwyColorAxis *
         size = width;
     }
 
-    if (axis->min == axis->max)
+    /* Don't attempt to draw anything if rounding errors are too large or
+     * scale calculation can overflow */
+    x = axis->max - axis->min;
+    max = MAX(fabs(axis->min), fabs(axis->max));
+    if (x < 1e-15*max || x <= 1e4*G_MINDOUBLE || max >= 1e-4*G_MAXDOUBLE)
         return;
 
     switch (axis->ticks_style) {
Index: modules/process/shade.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/modules/process/shade.c,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -p -r1.61 -r1.62
--- modules/process/shade.c	31 Aug 2006 12:42:13 -0000	1.61
+++ modules/process/shade.c	7 Sep 2006 15:20:26 -0000	1.62
@@ -71,8 +71,7 @@ static void        theta_changed_cb     
                                                  ShadeControls *controls);
 static void        phi_changed_cb               (GtkObject *adj,
                                                  ShadeControls *controls);
-static void        mix_changed_cb               (GtkAdjustment *adj,
-                                                 ShadeControls *controls);
+static void        mix_changed_cb               (ShadeControls *controls);
 static void        shade_dialog_update          (ShadeControls *controls,
                                                  ShadeArgs *args);
 static void        shade_mix_with_plane         (GwyDataField *shaded,
@@ -97,7 +96,7 @@ static GwyModuleInfo module_info = {
     &module_register,
     N_("Creates a shaded presentation of data."),
     "Petr Klapetek <klapetek@gwyddion.net>",
-    "2.2",
+    "2.3",
     "David Nečas (Yeti) & Petr Klapetek",
     "2003",
 };
@@ -264,11 +263,11 @@ shade_dialog(ShadeArgs *args,
                                  ! args->do_mix);
     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.do_mix),
                                  args->do_mix);
-    g_signal_connect(controls.mix, "value-changed",
-                     G_CALLBACK(mix_changed_cb), &controls);
+    g_signal_connect_swapped(controls.mix, "value-changed",
+                             G_CALLBACK(mix_changed_cb), &controls);
 
-    g_signal_connect(controls.do_mix, "toggled",
-                     G_CALLBACK(mix_changed_cb), &controls);
+    g_signal_connect_swapped(controls.do_mix, "toggled",
+                             G_CALLBACK(mix_changed_cb), &controls);
 
     row++;
     controls.data_view = gwy_data_view_new(controls.data);
@@ -374,8 +373,7 @@ phi_changed_cb(GtkObject *adj,
 }
 
 static void
-mix_changed_cb(GtkAdjustment *adj,
-               ShadeControls *controls)
+mix_changed_cb(ShadeControls *controls)
 {
     ShadeArgs *args;
 
@@ -384,7 +382,7 @@ mix_changed_cb(GtkAdjustment *adj,
 
     controls->in_update = TRUE;
     args = controls->args;
-    args->mix = gtk_adjustment_get_value(adj);
+    args->mix = gtk_adjustment_get_value(GTK_ADJUSTMENT(controls->mix));
     args->do_mix = gtk_toggle_button_get_active(
                                          GTK_TOGGLE_BUTTON(controls->do_mix));
     shade_dialog_update(controls, args);
Index: modules/tools/crop.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/modules/tools/crop.c,v
retrieving revision 1.91
retrieving revision 1.92
diff -u -p -r1.91 -r1.92
--- modules/tools/crop.c	31 Aug 2006 12:37:31 -0000	1.91
+++ modules/tools/crop.c	8 Sep 2006 12:45:01 -0000	1.92
@@ -80,7 +80,7 @@ static GwyModuleInfo module_info = {
     &module_register,
     N_("Crop tool, crops data to smaller size."),
     "Yeti <yeti@gwyddion.net>",
-    "2.3",
+    "2.4",
     "David Nečas (Yeti) & Petr Klapetek",
     "2003",
 };
@@ -362,8 +362,14 @@ gwy_tool_crop_apply(GwyToolCrop *tool)
 
     isel[0] = gwy_data_field_rtoj(plain_tool->data_field, sel[0]);
     isel[1] = gwy_data_field_rtoi(plain_tool->data_field, sel[1]);
-    isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]) + 1;
-    isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]) + 1;
+    isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]);
+    isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]);
+    if (isel[2] < isel[0])
+        GWY_SWAP(gdouble, isel[0], isel[2]);
+    if (isel[3] < isel[1])
+        GWY_SWAP(gdouble, isel[1], isel[3]);
+    isel[2]++;
+    isel[3]++;
 
     container = plain_tool->container;
     oldid = plain_tool->id;
Index: modules/tools/filter.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/modules/tools/filter.c,v
retrieving revision 1.76
retrieving revision 1.77
diff -u -p -r1.76 -r1.77
--- modules/tools/filter.c	12 Jul 2006 12:53:16 -0000	1.76
+++ modules/tools/filter.c	8 Sep 2006 12:45:01 -0000	1.77
@@ -92,7 +92,7 @@ static GwyModuleInfo module_info = {
     N_("Filter tool, processes selected part of data with a filter "
        "(conservative denoise, mean, median. Kuwahara, minimum, maximum)."),
     "Petr Klapetek <klapetek@gwyddion.net>",
-    "3.1",
+    "3.2",
     "David Nečas (Yeti) & Petr Klapetek",
     "2003",
 };
@@ -359,15 +359,15 @@ gwy_tool_filter_apply(GwyToolFilter *too
     if (gwy_selection_get_object(plain_tool->selection, 0, sel)) {
         isel[0] = gwy_data_field_rtoj(plain_tool->data_field, sel[0]);
         isel[1] = gwy_data_field_rtoi(plain_tool->data_field, sel[1]);
-        isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]) + 1;
-        isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]) + 1;
+        isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]);
+        isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]);
 
         if (sel[0] > sel[2])
             GWY_SWAP(gdouble, sel[0], sel[2]);
         if (sel[1] > sel[3])
             GWY_SWAP(gdouble, sel[1], sel[3]);
-        isel[2] -= isel[0];
-        isel[3] -= isel[1];
+        isel[2] -= isel[0] + 1;
+        isel[3] -= isel[1] + 1;
     }
     else {
         isel[0] = isel[1] = 0;
Index: modules/tools/icolorange.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/modules/tools/icolorange.c,v
retrieving revision 1.73
retrieving revision 1.75
diff -u -p -r1.73 -r1.75
--- modules/tools/icolorange.c	9 Aug 2006 21:59:07 -0000	1.73
+++ modules/tools/icolorange.c	8 Sep 2006 12:54:01 -0000	1.75
@@ -20,6 +20,7 @@
 
 #include "config.h"
 #include <string.h>
+#include <stdlib.h>
 #include <gtk/gtk.h>
 #include <libgwyddion/gwymacros.h>
 #include <libgwyddion/gwymath.h>
@@ -84,6 +85,8 @@ static void   gwy_tool_color_range_final
 static void   gwy_tool_color_range_init_dialog      (GwyToolColorRange *tool);
 static void   gwy_tool_color_range_data_switched    (GwyTool *gwytool,
                                                      GwyDataView *data_view);
+static void   gwy_tool_color_range_make_keys        (GwyToolColorRange *tool,
+                                                     GwyDataView *data_view);
 static void   gwy_tool_color_range_data_changed     (GwyPlainTool *plain_tool);
 static void   gwy_tool_color_range_selection_changed(GwyPlainTool *plain_tool,
                                                      gint hint);
@@ -108,7 +111,7 @@ static GwyModuleInfo module_info = {
        "color scale should map to, either on data or on height distribution "
        "histogram."),
     "Yeti <yeti@gwyddion.net>",
-    "3.2",
+    "3.3",
     "David Nečas (Yeti) & Petr Klapetek",
     "2004",
 };
@@ -306,7 +309,9 @@ gwy_tool_color_range_data_switched(GwyTo
     GwyPlainTool *plain_tool;
     GwyToolColorRange *tool;
     GwyLayerBasicRangeType range_type;
-    gchar key[32];
+
+    tool = GWY_TOOL_COLOR_RANGE(gwytool);
+    gwy_tool_color_range_make_keys(tool, data_view);
 
     GWY_TOOL_CLASS(gwy_tool_color_range_parent_class)->data_switched(gwytool,
                                                                      data_view);
@@ -314,7 +319,6 @@ gwy_tool_color_range_data_switched(GwyTo
     if (plain_tool->init_failed)
         return;
 
-    tool = GWY_TOOL_COLOR_RANGE(gwytool);
     if (data_view) {
         gwy_object_set_or_reset(plain_tool->layer,
                                 tool->layer_type_rect,
@@ -322,15 +326,9 @@ gwy_tool_color_range_data_switched(GwyTo
                                 "focus", -1,
                                 NULL);
         gwy_selection_set_max_objects(plain_tool->selection, 1);
-
-        g_snprintf(key, sizeof(key), "/%d/base/min", plain_tool->id);
-        tool->key_min = g_quark_from_string(key);
-        g_snprintf(key, sizeof(key), "/%d/base/max", plain_tool->id);
-        tool->key_max = g_quark_from_string(key);
     }
     else {
         gtk_widget_set_sensitive(GTK_WIDGET(tool->histogram), FALSE);
-        tool->key_min = tool->key_max = 0;
     }
 
     /* TODO: make sure we do update when switching to a FIXED data (even from
@@ -344,6 +342,32 @@ gwy_tool_color_range_data_switched(GwyTo
 }
 
 static void
+gwy_tool_color_range_make_keys(GwyToolColorRange *tool,
+                               GwyDataView *data_view)
+{
+    GwyPixmapLayer *layer;
+    const gchar *dkey;
+    gchar key[32];
+    gint id;
+
+    if (!data_view) {
+        tool->key_min = tool->key_max = 0;
+        return;
+    }
+
+    layer = gwy_data_view_get_base_layer(data_view);
+    g_return_if_fail(GWY_IS_PIXMAP_LAYER(layer));
+    dkey = gwy_pixmap_layer_get_data_key(layer);
+    g_return_if_fail(dkey && dkey[0] == '/' && g_ascii_isdigit(dkey[1]));
+    id = atoi(dkey + 1);
+
+    g_snprintf(key, sizeof(key), "/%d/base/min", id);
+    tool->key_min = g_quark_from_string(key);
+    g_snprintf(key, sizeof(key), "/%d/base/max", id);
+    tool->key_max = g_quark_from_string(key);
+}
+
+static void
 gwy_tool_color_range_data_changed(GwyPlainTool *plain_tool)
 {
     gwy_rect_selection_labels_fill(GWY_TOOL_COLOR_RANGE(plain_tool)->rlabels,
@@ -544,14 +568,14 @@ gwy_tool_color_range_set_min_max(GwyTool
         }
         isel[0] = gwy_data_field_rtoj(plain_tool->data_field, sel[0]);
         isel[1] = gwy_data_field_rtoi(plain_tool->data_field, sel[1]);
-        isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]) + 1;
-        isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]) + 1;
+        isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]);
+        isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]);
 
         gwy_data_field_area_get_min_max(plain_tool->data_field, NULL,
                                         MIN(isel[0], isel[2]),
                                         MIN(isel[1], isel[3]),
-                                        ABS(isel[2] - isel[0]),
-                                        ABS(isel[3] - isel[1]),
+                                        ABS(isel[2] - isel[0]) + 1,
+                                        ABS(isel[3] - isel[1]) + 1,
                                         &sel[0], &sel[1]);
         break;
 
Index: modules/tools/maskedit.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/modules/tools/maskedit.c,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -p -r1.43 -r1.44
--- modules/tools/maskedit.c	12 Jul 2006 12:53:16 -0000	1.43
+++ modules/tools/maskedit.c	8 Sep 2006 12:45:02 -0000	1.44
@@ -113,7 +113,7 @@ static GwyModuleInfo module_info = {
     N_("Mask editor tool, allows to interactively add or remove parts "
        "of mask."),
     "Yeti <yeti@gwyddion.net>",
-    "2.1",
+    "2.2",
     "David Nečas (Yeti) & Petr Klapetek",
     "2004",
 };
@@ -782,11 +782,15 @@ gwy_tool_mask_editor_selection_finished(
 
     isel[0] = gwy_data_field_rtoj(plain_tool->data_field, sel[0]);
     isel[1] = gwy_data_field_rtoi(plain_tool->data_field, sel[1]);
-    isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]) + 1;
-    isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]) + 1;
+    isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]);
+    isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]);
+    if (isel[2] < isel[0])
+        GWY_SWAP(gdouble, isel[0], isel[2]);
+    if (isel[3] < isel[1])
+        GWY_SWAP(gdouble, isel[1], isel[3]);
     gwy_debug("(%d,%d) (%d,%d)", isel[0], isel[1], isel[2], isel[3]);
-    isel[2] -= isel[0];
-    isel[3] -= isel[1];
+    isel[2] -= isel[0] - 1;
+    isel[3] -= isel[1] - 1;
 
     switch (tool->args.shape) {
         case MASK_SHAPE_RECTANGLE:
Index: modules/tools/sfunctions.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/modules/tools/sfunctions.c,v
retrieving revision 1.101
retrieving revision 1.102
diff -u -p -r1.101 -r1.102
--- modules/tools/sfunctions.c	12 Jul 2006 12:53:17 -0000	1.101
+++ modules/tools/sfunctions.c	8 Sep 2006 12:45:02 -0000	1.102
@@ -1,5 +1,5 @@
 /*
- *  @(#) $Id: sfunctions.c,v 1.101 2006/07/12 12:53:17 yeti-dn Exp $
+ *  @(#) $Id: sfunctions.c,v 1.102 2006/09/08 12:45:02 yeti-dn Exp $
  *  Copyright (C) 2003-2006 David Necas (Yeti), Petr Klapetek.
  *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
  *
@@ -135,7 +135,7 @@ static GwyModuleInfo module_info = {
        "functions (height distribution, correlations, PSDF, Minkowski "
        "functionals) of selected part of data."),
     "Petr Klapetek <klapetek@gwyddion.net>",
-    "2.1",
+    "2.2",
     "David Nečas (Yeti) & Petr Klapetek",
     "2004",
 };
@@ -543,11 +543,11 @@ gwy_tool_sfunctions_update_curve(GwyTool
         else {
             isel[0] = gwy_data_field_rtoj(plain_tool->data_field, sel[0]);
             isel[1] = gwy_data_field_rtoi(plain_tool->data_field, sel[1]);
-            isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]) + 1;
-            isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]) + 1;
+            isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]);
+            isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]);
 
-            w = ABS(isel[2] - isel[0]);
-            h = ABS(isel[3] - isel[1]);
+            w = ABS(isel[2] - isel[0]) + 1;
+            h = ABS(isel[3] - isel[1]) + 1;
             isel[0] = MIN(isel[0], isel[2]);
             isel[1] = MIN(isel[1], isel[3]);
             if (w >= 4 && h >= 4)
Index: modules/tools/stats.c
===================================================================
RCS file: /cvsroot/gwyddion/gwyddion/modules/tools/stats.c,v
retrieving revision 1.74
retrieving revision 1.75
diff -u -p -r1.74 -r1.75
--- modules/tools/stats.c	11 Aug 2006 15:31:11 -0000	1.74
+++ modules/tools/stats.c	8 Sep 2006 12:45:02 -0000	1.75
@@ -136,7 +136,7 @@ static GwyModuleInfo module_info = {
     &module_register,
     N_("Statistics tool."),
     "Petr Klapetek <klapetek@gwyddion.net>",
-    "2.2",
+    "2.3",
     "David Nečas (Yeti) & Petr Klapetek",
     "2003",
 };
@@ -560,8 +560,8 @@ gwy_tool_stats_calculate(GwyToolStats *t
     if (!gwy_selection_get_object(plain_tool->selection, 0, sel)
         || sel[0] == sel[2] || sel[1] == sel[3]) {
         isel[0] = isel[1] = 0;
-        isel[2] = gwy_data_field_get_xres(plain_tool->data_field);
-        isel[3] = gwy_data_field_get_yres(plain_tool->data_field);
+        isel[2] = w = gwy_data_field_get_xres(plain_tool->data_field);
+        isel[3] = h = gwy_data_field_get_yres(plain_tool->data_field);
         sel[0] = sel[1] = 0.0;
         sel[2] = gwy_data_field_get_xreal(plain_tool->data_field);
         sel[3] = gwy_data_field_get_yreal(plain_tool->data_field);
@@ -569,13 +569,13 @@ gwy_tool_stats_calculate(GwyToolStats *t
     else {
         isel[0] = gwy_data_field_rtoj(plain_tool->data_field, sel[0]);
         isel[1] = gwy_data_field_rtoi(plain_tool->data_field, sel[1]);
-        isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]) + 1;
-        isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]) + 1;
+        isel[2] = gwy_data_field_rtoj(plain_tool->data_field, sel[2]);
+        isel[3] = gwy_data_field_rtoi(plain_tool->data_field, sel[3]);
+        w = ABS(isel[2] - isel[0]) + 1;
+        h = ABS(isel[3] - isel[1]) + 1;
+        isel[0] = MIN(isel[0], isel[2]);
+        isel[1] = MIN(isel[1], isel[3]);
     }
-    w = ABS(isel[2] - isel[0]);
-    h = ABS(isel[3] - isel[1]);
-    isel[0] = MIN(isel[0], isel[2]);
-    isel[1] = MIN(isel[1], isel[3]);
 
     if (!w || !h)
         return FALSE;
