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:


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.


Other widgets 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 widgets 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 widgets (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:

x::w::new_tablelayoutmanager ntlm{
                    // ...

ntlm.restore(saved_positions, "main_table");

     (const x::w::focusable_container &)
         // ...
     }, ntlm);

// ...

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

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), just like the word wrap label example.

Each preserved table has a unique identifier. table2.C calls its sole table, unimaginatively, main_table.

Before creating the pane container 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. This has no effect if the configuration does not have previously saved table column sizes, otherwise they get restored. If everything goes according to plan, table2.C's window and table reappear just as it was the last time it ran.

When the main window save()s its position and size, the table's then column widths get automatically saved together with it, to be restore()d the next time.