Adjustable tables

Adjustable tables.

Tables' default width gets computed based on the width of the tables' items. An optional table setting enables adjustable tables. Adjustable tables allow their width to expand, and their individual columns' relative width to be manually adjusted using the mouse pointer.

table2.C is a modified version of the example from this chapter that enables adjustable table features:

ntlm.adjustable_column_widths=true;
ntlm.table_width=200;
ntlm.maximum_table_width=500;

Setting x::w::new_tablelayoutmanager's adjustable_column_widths to true enables adjusting of the table's columns' widths. Moving the mouse pointer on top of a dividing border in the header row changes the pointer to a bi-directional horizontal arrow. Pressing pointer button #1 drags the border under the pointer horizontally and adjusts the relative widths of the columns on both sides of the border. Pressing pointer button #1 while holding down the Alt key restores all columns to their initial unadjusted width.

Setting table_width and maximum_table_width specifies the initial and the maximum width of the table, in millimeters. table2.C's window's width is expandable, this adjusts the size of its table until its table reaches its maximum width. Calling unlimited_table_width() removes the maximum limit for the table's width, completely.

Note

Other display elements in the same window affect the table's actual and maximum widths. The main window's grid layout manager (or the table's container's layout manager) has to calculate the other window elements' metrics. Unless the other display elements in the window can be sized accordingly, the table may not end up being sizable to its maximum width.

Adjusting table column widths using the keyboard

Each border between the columns in an adjustable table is draggable directly, by the pointer. This adjusts the relative sizes of the two columns on either sides of the border. The column sizes are also adjustable using the keyboard.

When the keyboard focus is on the table's list the Tab key highlights each border in the header row in turn the Tab key moves the keyboard focus out of the talbe list, as with other focusable display elements (Shift-Tab highlights the previous border in this mode). Cursor-Left and Cursor-Right move the highlighted border by a moderate amount, and by one precise pixel together with the Ctrl key.

Column width adjusting mode gets terminated immediately by the Esc key, or by repeatedly tabbing until the keyboard focus leaves the table list, or the header row. Pressing Esc while holding down the Alt key goes one step further and restores all columns to their initial unadjusted width.

Preserving table columns' widths

It's natural to expect that reopening an application opens the table and its columns using their original size, and the section called “Preserving window positions” describes how to preserve the position and the size of the main application window. But this only preserves size and the position of the main window itself. Any resizing of a table's columns' width does not get preserved; the reopened table's columns' widths get reset to their default sizes.

table2.C shows what additional steps are necessary for taking care of this last detail:

main_window->save("main", pos);

my_appdata appdata=main_window->appdata;

x::w::tablelayoutmanager tlm=appdata->main_table->get_layoutmanager();

tlm->save("main_table", pos);

pos.save(configfile);

A necessary prerequisite for preserving table column widths is that the table's main application window's size also gets preserved; so table2.C save()s its main window's size (and position), first, just like the word wrap label example.

The table layout manager's save() takes the table's column widths and puts it into the x::w::screen_positions object together with the window's size and position, before x::w::screen_positions's save() saves all of its contents into the application's configuration file.

It is necessary to use a unique identifier for each table's (across all windows) saved information. table2.C calls its sole table, unimaginatively, main_table.

ntlm.restore(saved_positions, "main_table");

table2.C reads the saved_positions from its configuration file, the next time it runs, and restores its main window's position and size, first. Then x::w::new_tablelayoutmanager's restore() reads the saved column widths called main_table, which coincidently references the previous column widths of this new table. If everything goes according to plan, table2.C's window and table reappear just as it was the last time it ran.