commit 2926ee30c00760c0e430c2cae1d6a258c38505dc
parent 7424b358a5f083e20d70e00cc0022f2075703068
Author: Kyle Milz <krwmilz@gmail.com>
Date: Tue, 21 Sep 2021 15:34:20 +0000
take addr_widget into account when calculating window size
First, fix an old bug where the addr_widget was set way too wide and
caused issues at shrink levels 0 and 1. Then modify the window size
calculation to take the address bar height into account. This helps
get a window size that will use the least amount of scroll bars.
Also add a check after realization to add to the width of the window if
a vertical scroll bar has appeared.
Diffstat:
M | xdvi.c | | | 164 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 99 insertions(+), 65 deletions(-)
diff --git a/xdvi.c b/xdvi.c
@@ -581,6 +581,83 @@ compile_wheel_actions()
*wactpp = NULL;
}
+void
+set_default_winsize()
+{
+ Dimension addr_bwidth;
+ Dimension bwidth;
+ Dimension screen_w, screen_h;
+ XtWidgetGeometry addr_reply;
+ XtWidgetGeometry constraints;
+ XtWidgetGeometry reply;
+ int i;
+ int addr_height;
+
+ XtQueryGeometry(addr_widget, NULL, &addr_reply);
+ XtVaGetValues(addr_widget, XtNborderWidth, &addr_bwidth, NULL);
+ addr_height = addr_reply.height + 2 * addr_bwidth;
+
+ /*
+ * The total screen_{w,h} is what is left over after the address bar
+ * and borders have been taken into account.
+ */
+ XtVaGetValues(top_level, XtNborderWidth, &bwidth, NULL);
+ screen_w = WidthOfScreen(SCRN) - 2 * bwidth;
+ screen_h = HeightOfScreen(SCRN) - 2 * bwidth - addr_height;
+
+ Arg set_wh_args[] = {
+ {XtNwidth, (XtArgVal) 0},
+ {XtNheight, (XtArgVal) 0},
+ };
+ for (;;) { /* actually, at most two passes */
+ constraints.request_mode = reply.request_mode = 0;
+ constraints.width = page_w;
+ if (page_w > screen_w) {
+ constraints.request_mode = CWWidth;
+ constraints.width = screen_w;
+ }
+ constraints.height = page_h;
+ if (page_h > screen_h) {
+ constraints.request_mode |= CWHeight;
+ constraints.height = screen_h;
+ }
+ if (constraints.request_mode != 0
+ && constraints.request_mode != (CWWidth | CWHeight))
+ XtQueryGeometry(vport_widget, &constraints, &reply);
+ if (!(reply.request_mode & CWWidth))
+ reply.width = constraints.width;
+ if (reply.width >= screen_w)
+ reply.width = screen_w;
+ if (!(reply.request_mode & CWHeight))
+ reply.height = constraints.height;
+ if (reply.height >= screen_h)
+ reply.height = screen_h;
+
+ /* now reply.{width,height} contain max. usable window size */
+
+ if (shrink_factor != 0)
+ break;
+
+ shrink_factor = ROUNDUP(unshrunk_page_w, reply.width - 2);
+ i = ROUNDUP(unshrunk_page_h, reply.height - 2);
+ if (i >= shrink_factor)
+ shrink_factor = i;
+ if (shrink_factor > 1)
+ bak_shrink = shrink_factor;
+ mane.shrinkfactor = shrink_factor;
+ init_page();
+ set_wh_args[0].value = (XtArgVal) page_w;
+ set_wh_args[1].value = (XtArgVal) page_h;
+ XtSetValues(draw_widget, set_wh_args, XtNumber(set_wh_args));
+ }
+ set_wh_args[0].value = reply.width;
+ set_wh_args[1].value = reply.height + addr_height;
+ XtSetValues(top_level, set_wh_args, XtNumber(set_wh_args));
+
+ set_wh_args[0].value = reply.width - (2 * addr_bwidth);
+ XtSetValues(addr_widget, set_wh_args, 1);
+}
+
/*
* main program
*/
@@ -591,7 +668,6 @@ main(int argc, char **argv)
const char *url = NULL;
const char *arg;
XrmDatabase db;
- Dimension screen_w, screen_h;
int i;
@@ -772,10 +848,10 @@ main(int argc, char **argv)
vport_widget = XtVaCreateManagedWidget("vport", viewportWidgetClass,
form_widget,
XtNborderWidth, 0,
- XtNtop, XtChainTop,
- XtNbottom, XtChainBottom,
- XtNleft, XtChainLeft,
- XtNright, XtChainRight,
+ XtNtop, XawChainTop,
+ XtNbottom, XawChainBottom,
+ XtNleft, XawChainLeft,
+ XtNright, XawChainRight,
XtNallowHoriz, True,
XtNallowVert, True,
NULL);
@@ -793,7 +869,6 @@ main(int argc, char **argv)
addr_widget = XtVaCreateManagedWidget("address", asciiTextWidgetClass,
form_widget,
- XtNwidth, page_w,
XtNfromVert, (XtArgVal) vport_widget,
XtNdataCompression, False,
XtNeditType, XawtextEdit,
@@ -834,65 +909,7 @@ main(int argc, char **argv)
XtSetValues(draw_widget, &back_args, 1);
XtSetValues(clip_widget, &back_args, 1);
- /* determine default window size */
-#define xtra_wid 15
- XtWidgetGeometry constraints;
- XtWidgetGeometry reply;
-
- Dimension bwidth;
- XtVaGetValues(top_level, XtNborderWidth, &bwidth, NULL);
- screen_w = WidthOfScreen(SCRN) - 2 * bwidth - xtra_wid;
- screen_h = HeightOfScreen(SCRN) - 2 * bwidth;
-
- Arg set_wh_args[] = {
- {XtNwidth, (XtArgVal) 0},
- {XtNheight, (XtArgVal) 0},
- };
- for (;;) { /* actually, at most two passes */
- constraints.request_mode = reply.request_mode = 0;
- constraints.width = page_w;
- if (page_w > screen_w) {
- constraints.request_mode = CWWidth;
- constraints.width = screen_w;
- }
- constraints.height = page_h;
- if (page_h > screen_h) {
- constraints.request_mode |= CWHeight;
- constraints.height = screen_h;
- }
- if (constraints.request_mode != 0
- && constraints.request_mode != (CWWidth | CWHeight))
- XtQueryGeometry(vport_widget, &constraints, &reply);
- if (!(reply.request_mode & CWWidth))
- reply.width = constraints.width;
- if (reply.width >= screen_w)
- reply.width = screen_w;
- if (!(reply.request_mode & CWHeight))
- reply.height = constraints.height;
- if (reply.height >= screen_h)
- reply.height = screen_h;
-
- /* now reply.{width,height} contain max. usable window size */
-
- if (shrink_factor != 0)
- break;
-
- shrink_factor = ROUNDUP(unshrunk_page_w, reply.width - 2);
- i = ROUNDUP(unshrunk_page_h, reply.height - 2);
- if (i >= shrink_factor)
- shrink_factor = i;
- if (shrink_factor > 1)
- bak_shrink = shrink_factor;
- mane.shrinkfactor = shrink_factor;
- init_page();
- set_wh_args[0].value = (XtArgVal) page_w;
- set_wh_args[1].value = (XtArgVal) page_h;
- XtSetValues(draw_widget, set_wh_args, XtNumber(set_wh_args));
- }
- set_wh_args[0].value = (XtArgVal) (reply.width + xtra_wid);
- set_wh_args[1].value = (XtArgVal) reply.height;
- XtSetValues(top_level, set_wh_args, XtNumber(set_wh_args));
-
+ set_default_winsize();
/*
* Step 5: Realize the widgets (or windows).
@@ -905,6 +922,23 @@ main(int argc, char **argv)
&mane);
XtRealizeWidget(top_level);
+ /*
+ * Assume that if we have a vertical scroll bar its because of
+ * vertical overflow. Make the window wider to account for the
+ * bars width.
+ */
+ Widget y_bar = XtNameToWidget(vport_widget, "vertical");
+ if (y_bar != NULL) {
+ Dimension width;
+ XtWidgetGeometry reply;
+
+ XtVaGetValues(y_bar, XtNwidth, &width, NULL);
+ XtQueryGeometry(top_level, NULL, &reply);
+
+ XtVaSetValues(top_level, XtNwidth, reply.width + width + 1,
+ NULL);
+ }
+
XSetWMProtocols(DISP, XtWindow(top_level), &XA_WM_DELETE_WINDOW, 1);
XtAddEventHandler(top_level, NoEventMask, True, handle_messages, NULL);