Previously we only looked at how to display simple strings. In this section we will look at the two methods for displaying integer values: percentage bars (Section 3.5.1, “Percentage Bars”) and sliders (Section 3.5.2, “Sliders”). Both techniques can only display values between 0 and 100. The XOSD window will only display 100 if a value greater than 100 is passed; similarly a value less than 0 will cause 0 to be displayed.
A percentage bar is similar to the progress bar seen in many applications. It is made up of two parts (Figure 3.3):
Fewer pipes indicate a smaller value. You cannot set the total number of pipes and dashes that are displayed as it is dependent on the size the display.
The xosd_display is called to display a percentage bar. However, the arguments passed to xosd_display are different to what we have seen earlier (Section 3.1, “Our First XOSD Program”). For example, the following displays the value 77 using a percentage bar.
xosd_display (osd, 0, XOSD_percentage, 77);
The first argument in the above example is the XOSD window to use, as normal. However, instead of passing XOSD_string as the value for command the literal XOSD_percentage is passed, followed by the value to display.
Sliders were developed to display the balance for the XMMS plugin. A slider represents an interger value, like the percentage bar, but the value is shown as a point rather than a bar. Figure 3.4 is an example of a slider showing a small value.
To display a slider, call xosd_display with XOSD_slider as the second argument, such as the following example.
xosd_display (osd, 0, XOSD_percentage, 77);
The third argument is the distance along the bar the slider should appear, expressed as a percentage. So in the above example the slider appears 77% along the bar, which is near the right of the bar for left-aligned XOSD windows, or near the left for right-aligned windows.
Normally you will not have to change the length of a percentage bar or slider, as the default size that XOSD uses is sensible. However, there are two functions to call if you do want to change the bar dimensions.
The first is xosd_set_bar_length, which changes the horizontal length of the bar. It takes one augment, besides the XOSD window: the width of the display that the bar should take up. For example, if we wanted our slider from earlier (Section 3.5.2, “Sliders”) to only take up half the screen we would call xosd_set_bar_length like the following.
xosd_set_bar_length(osd, 50);
The vertical size of the bar is dependent on the size of the font used to display the text; see Section 3.2.1, “Changing the Font” for an example on how to make the font larger.
The following example shows how a progress bar can be implemented using a percentage bar. Most of the code is the same as the previous example as it creates the window and set the colour, size and position. The core part of the code runs through a loop which displays an ever-increasing value in the XOSD window.
Example 3.3. A Progress Bar in XOSD
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <xosd.h> int main (int argc, char *argv[]) { xosd *osd = NULL; int i = 0; char *font = "-adobe-helvetica-bold-r-normal-*-*-320-*-*-p-*-iso8859-1" osd = xosd_create (2); if (osd == NULL) { perror ("Could not create \"osd\""); exit (1); } xosd_set_font (osd, font); xosd_set_shadow_offset (osd, 2); xosd_set_colour (osd, "lawngreen"); xosd_set_pos (osd, XOSD_bottom); xosd_set_vertical_offset (osd, 48); xosd_set_align (osd, XOSD_left); xosd_set_horizontal_offset (osd, 48); xosd_set_bar_length(osd, 50); for (i = 0; i <= 100; i += 5) { xosd_display (osd, 0, XOSD_percentage, i); sleep(1); } xosd_destroy (osd); exit (0);