This program builds upon the previous program and adds the feature of horizontal (as well as vertical) scrolling of the text. The output of the program is shown below (see also the full code listing).
Both vertical and horizontal scroll bars are available to facilitate scrolling lines of text into view. The window data contains several new variables and messages pertaining to scrolling the text horizontally and vertically. The fields of the window data structure are shown below.
struct window_data { int width_of_character, width_of_capitals, height_of_character, width_of_client, height_of_client, maximum_width, position_of_vertical_scroll, maximum_of_vertical_scroll, position_of_horizontal_scroll, maximum_of_horizontal_scroll; int increment_of_vertical_scroll, increment_of_horizontal_scroll; window_data() { position_of_vertical_scroll = 0; position_of_horizontal_scroll = 0; } };
The variables that have been added are shown in the table below.
width_of_client | The current width of the client area. |
maximum_width | The maximum width occupied by the text. |
position_of_horizontal_scroll | The current position of the horizontal scroll bar. |
maximum_of_horizontal_scroll | The current range of the horizontal scroll bar. |
Setting of the range and initial position of the scroll bars now occurs during the processing of the message message::size - which is shown below.
case message::size: { window_data* data = (window_data*)get_window_pointer(window_handle, 0); data->width_of_client = low_part(parameter2); data->height_of_client = high_part(parameter2); data->maximum_of_vertical_scroll = maximum(0, (int)lines + 2 - data->height_of_client / data->height_of_character); data->position_of_vertical_scroll = minimum(data->position_of_vertical_scroll, data->maximum_of_vertical_scroll); set_scroll_range(window_handle, scrollbar_identity::vertical, 0, data->maximum_of_vertical_scroll, false); set_scroll_position(window_handle, scrollbar_identity::vertical, data->position_of_vertical_scroll, true); data->maximum_of_horizontal_scroll = maximum(0, 2 + (data->maximum_width - data->width_of_client) / data->width_of_character); data->position_of_horizontal_scroll = minimum(data->position_of_horizontal_scroll, data->maximum_of_horizontal_scroll); set_scroll_range(window_handle, scrollbar_identity::horizontal, 0, data->maximum_of_horizontal_scroll, false); set_scroll_position(window_handle, scrollbar_identity::horizontal, data->position_of_horizontal_scroll, true); } break;
The range of the vertical scroll bar is set to be the number of lines plus 2, minus the number of lines that fit the current client area (rounded up to zero if necessary). The range of the horizontal scroll bar is 2 plus the maximum width of the text minus the width of the client divided by the width of a character.
Applications should emulate this final example rather than the previous two. The processing that occurs during message::size correctly performs scrolling calculations. Whilst the code is fairly voluminous, it provides a template for scrolling text in all sorts of applications. For example, editors and word processors require this type of logic. The code is sophisticated for being placed early in a text book. Most of the samples of subsequent chapters are a lot simpler.