Hi guys,
I am trying to dynamically resize a browser to exactly fit the
width of the longest line currently in the browser. I cannot seem
to get the browser to resize correctly - the width is off by a couple
pixels, resulting in some of the text being clipped. The program
below shows the effect.
-bob
/* * * * * * * * * * begin resize.c * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include "forms.h"
/* Callback functions */
static void QuitCB (FL_OBJECT *ob, long data);
static void ResizeCB (FL_OBJECT *ob, long data);
FL_FORM *main_form;
FL_OBJECT *browser;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void main (int argc, char **argv)
{
FL_OBJECT *obj, *ret;
FL_IOPT control;
int version, revision;
fl_initialize (&argc, argv, "", 0, 0);
fl_set_border_width(-1);
main_form = fl_bgn_form(FL_UP_BOX, 400, 200);
browser = fl_add_browser(FL_HOLD_BROWSER,
20, 20, 190, 120, "");
fl_set_browser_hscrollbar(browser, FL_ON);
fl_set_browser_vscrollbar(browser, FL_ON);
fl_set_browser_scrollbarsize(browser, 15, 15);
fl_set_browser_fontstyle(browser, FL_BOLD_STYLE);
fl_set_browser_fontsize(browser, FL_NORMAL_SIZE);
fl_add_browser_line(browser, "Line 1");
fl_add_browser_line(browser, "Line 2");
fl_add_browser_line(browser, "Line 3 is a very long line, too long to fit in this browser");
fl_add_browser_line(browser, "Line 4");
obj = fl_add_button(FL_NORMAL_BUTTON,
20, 160, 70, 30, "Resize");
fl_set_object_callback(obj, ResizeCB, 0);
obj = fl_add_button(FL_NORMAL_BUTTON,
310, 160, 70, 30, "Dismiss");
fl_set_object_callback(obj, QuitCB, 0);
fl_end_form();
fl_show_form(main_form, FL_PLACE_CENTERFREE,
FL_FULLBORDER, "resize browser test");
do {
ret = fl_do_forms();
} while (ret != NULL);
fl_hide_form(main_form);
exit(0);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void ResizeCB(FL_OBJECT *ob, long data)
{
int i, numEntries;
const char *line;
int width_in_chars = 0;
int width_in_pixels = 0;
/* Default browser width, and scrollbar size, set when main form is built */
int min_browser_width = 175;
int sb_width = 15;
const char *long_line = "Line 3 is a very long line, too long to fit in this browser";
numEntries = fl_get_browser_maxline(browser);
for (i = 1; i <= numEntries; i++) {
line = fl_get_browser_line(browser, i);
/* Get the length of the longest entry */
if (strlen(line) > width_in_chars) {
width_in_chars = strlen(line);
width_in_pixels = fl_get_string_width(FL_BOLD_STYLE, FL_NORMAL_SIZE,
line, strlen(line));
}
} /* endfor */
printf("Longest browser line:\n");
printf(" Characters = %d\n", width_in_chars);
printf(" Pixels = %d\n", width_in_pixels);
printf("For line \"%s\"\n", long_line);
printf(" Characters = %d\n", strlen(long_line));
printf(" Pixels = %d\n",
fl_get_string_width(FL_BOLD_STYLE, FL_NORMAL_SIZE,
long_line, strlen(long_line)));
if (width_in_pixels > min_browser_width) {
fl_set_object_geometry(browser,
20, 20, width_in_pixels + sb_width, 120);
}
else {
fl_set_object_geometry(browser,
20, 20, 190, 120);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void QuitCB(FL_OBJECT *ob, long data)
{
fl_finish();
exit (0);
}
/* * * * * * * * * * end resize.c * * * * * * * * * */
-- Dr. Robert S. Mallozzi University of Alabama in Huntsville http://cspar.uah.edu/~mallozzir/_________________________________________________ To unsubscribe, send the message "unsubscribe" to xforms-request@bob.usuf2.usuhs.mil or see http://bob.usuf2.usuhs.mil/mailserv/xforms.html Xforms Home Page: http://bragg.phys.uwm.edu/xforms List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/