GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_slider_value_calculate.c Lines: 18 18 100.0 %
Date: 2026-03-06 19:21:09 Branches: 10 10 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** GUIX Component                                                        */
17
/**                                                                       */
18
/**   Prompt Management (Slider)                                          */
19
/**                                                                       */
20
/**************************************************************************/
21
22
#define GX_SOURCE_CODE
23
24
/* Include necessary system files.  */
25
26
#include "gx_api.h"
27
#include "gx_system.h"
28
#include "gx_display.h"
29
#include "gx_context.h"
30
#include "gx_widget.h"
31
#include "gx_utility.h"
32
#include "gx_slider.h"
33
34
/**************************************************************************/
35
/*                                                                        */
36
/*  FUNCTION                                               RELEASE        */
37
/*                                                                        */
38
/*    _gx_slider_value_calculate                          PORTABLE C      */
39
/*                                                           6.1          */
40
/*  AUTHOR                                                                */
41
/*                                                                        */
42
/*    Kenneth Maxwell, Microsoft Corporation                              */
43
/*                                                                        */
44
/*  DESCRIPTION                                                           */
45
/*                                                                        */
46
/*    This service calculates the slider value.                           */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    slider                                Slider widget control block   */
51
/*    info                                  Pointer to slider info        */
52
/*    new_position                          New slider position           */
53
/*                                                                        */
54
/*  OUTPUT                                                                */
55
/*                                                                        */
56
/*    status                                Completion status             */
57
/*    None                                                                */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    _gx_slider_travel_get                 Get the slider travel         */
62
/*    _gx_slider_value_set                  Set the slider value          */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    Application Code                                                    */
67
/*    GUIX Internal Code                                                  */
68
/*                                                                        */
69
/**************************************************************************/
70
5359
UINT  _gx_slider_value_calculate(GX_SLIDER *slider, GX_SLIDER_INFO *info, INT new_position)
71
{
72
INT newval;
73
INT mintravel;
74
INT maxtravel;
75
76
    /* get travel limits */
77
5359
    _gx_slider_travel_get(slider, info, &mintravel, &maxtravel);
78
79
    /* make sure we don't allow travel too far */
80
5359
    if (new_position < mintravel)
81
    {
82
1726
        new_position = mintravel;
83
    }
84
    else
85
    {
86
3633
        if (new_position > maxtravel)
87
        {
88
1623
            new_position = maxtravel;
89
        }
90
    }
91
92
    /* use position to calculate new value */
93
5359
    if (maxtravel > mintravel)
94
    {
95
        /* do linear interpolation using input position to
96
           determine new slider value
97
         */
98
99
        /* get distance from starting point */
100
101
5214
        if (slider -> gx_widget_style & GX_STYLE_SLIDER_VERTICAL)
102
        {
103
2292
            newval = maxtravel - new_position;
104
        }
105
        else
106
        {
107
2922
            newval = new_position - mintravel;
108
        }
109
110
5214
        newval = GX_FIXED_VAL_MAKE(newval);
111
112
        /* multiply by value range */
113
5214
        newval *= info -> gx_slider_info_max_val - info -> gx_slider_info_min_val;
114
115
        /* divide by travel range */
116
5214
        newval /= (maxtravel - mintravel);
117
5214
        newval = GX_FIXED_VAL_RND(newval);
118
119
        /* offset by min value */
120
5214
        newval += info -> gx_slider_info_min_val;
121
122
        /* has the value changed? */
123
5214
        if (newval != info -> gx_slider_info_current_val)
124
        {
125
            /* yes, re-assign value which will move the needle */
126
2432
            _gx_slider_value_set(slider, info, newval);
127
        }
128
    }
129
5359
    return(GX_SUCCESS);
130
}
131