aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2023-07-13 08:09:17 +0100
committerSimon Tatham <anakin@pobox.com>2023-07-13 08:09:17 +0100
commita95796ebca53a7b7e0412860f3a38ec518d838be (patch)
treec59f68efacd13f12de762112cf189afc10adadb8
parent61e9c782487ea982498955b93d1b94921131059e (diff)
downloadpuzzles-a95796ebca53a7b7e0412860f3a38ec518d838be.zip
puzzles-a95796ebca53a7b7e0412860f3a38ec518d838be.tar.gz
puzzles-a95796ebca53a7b7e0412860f3a38ec518d838be.tar.bz2
puzzles-a95796ebca53a7b7e0412860f3a38ec518d838be.tar.xz
osx.m: avoid division by zero in startConfigureSheet.
When we set up a configuration sheet, we track the minimum overall width that the controls will fit into (in a variable 'totalw'), and separately, the minimum width needed by each of the left and right columns containing control labels and actual controls ('leftw' and 'rightw'). If totalw > leftw+rightw at the end of the process, then we must expand the two columns so that they have the right sum. However, sometimes leftw+rightw can be zero, while totalw > 0. This occurs if _no_ control in the box was of a type that used the left and right columns for different things, so that the entire loop over the controls only incremented totalw, and not leftw or rightw. For example, in a puzzle such as Cube that defines no preferences of its own, the only control in the preferences pane is midend.c's standard "Keyboard shortcuts without Ctrl" preference, which is C_BOOLEAN and only uses totalw. In that situation, the code for proportionate distribution of the excess divides by zero. So it needs a special case.
-rw-r--r--osx.m12
1 files changed, 11 insertions, 1 deletions
diff --git a/osx.m b/osx.m
index 60299c9..44bb96f 100644
--- a/osx.m
+++ b/osx.m
@@ -1321,7 +1321,17 @@ struct frontend {
totalw = leftw + SPACING + rightw;
if (totalw > leftw + SPACING + rightw) {
int excess = totalw - (leftw + SPACING + rightw);
- int leftexcess = leftw * excess / (leftw + rightw);
+ /*
+ * Distribute the excess in proportion across the left and
+ * right columns of the sheet, by allocating a proportion
+ * leftw/(leftw+rightw) to the left one. An exception is if
+ * leftw+rightw == 0, which can happen if every control in the
+ * sheet was a C_BOOLEAN which only increments totalw; in that
+ * case it doesn't much matter what we do, so I just allocate
+ * the space half and half.
+ */
+ int leftexcess = (leftw + rightw == 0 ? excess / 2 :
+ leftw * excess / (leftw + rightw));
int rightexcess = excess - leftexcess;
leftw += leftexcess;
rightw += rightexcess;