GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_horizontal_list_selected_set.c Lines: 52 52 100.0 %
Date: 2024-12-05 08:52:37 Branches: 32 34 94.1 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** GUIX Component                                                        */
16
/**                                                                       */
17
/**   Horizontal List (List)                                              */
18
/**                                                                       */
19
/**************************************************************************/
20
21
#define GX_SOURCE_CODE
22
23
24
/* Include necessary system files.  */
25
26
#include "gx_api.h"
27
#include "gx_widget.h"
28
#include "gx_window.h"
29
#include "gx_system.h"
30
#include "gx_scrollbar.h"
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _gx_horizontal_list_selected_set                    PORTABLE C      */
37
/*                                                           6.1.12       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Kenneth Maxwell, Microsoft Corporation                              */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This service sets the list entry at the current list index.         */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    horizontal_list                       Horizontal list widget        */
49
/*                                            control block               */
50
/*    index                                 Index based position of new   */
51
/*                                            list entry                  */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    status                                Completion status             */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _gx_widget_first_client_child_get                                   */
60
/*    _gx_widget_next_client_child_get                                    */
61
/*    _gx_horizontal_list_page_index_set                                  */
62
/*    _gx_widget_event_generate                                           */
63
/*    _gx_system_dirty_mark                                               */
64
/*                                                                        */
65
/*  CALLED BY                                                             */
66
/*                                                                        */
67
/*    _gx_horizontal_list_event_process                                   */
68
/*                                                                        */
69
/*  RELEASE HISTORY                                                       */
70
/*                                                                        */
71
/*    DATE              NAME                      DESCRIPTION             */
72
/*                                                                        */
73
/*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
74
/*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
75
/*                                            resulting in version 6.1    */
76
/*  07-29-2022     Kenneth Maxwell          Modified comment(s),          */
77
/*                                            added support for           */
78
/*                                            GX_STYLE_REPEAT_SELECT,     */
79
/*                                            resulting in version 6.1.12 */
80
/*                                                                        */
81
/**************************************************************************/
82
83
76
UINT _gx_horizontal_list_selected_set(GX_HORIZONTAL_LIST *horizontal_list, INT index)
84
{
85
GX_WIDGET *child;
86
76
GX_WIDGET *found = GX_NULL;
87
76
INT        page_index = horizontal_list -> gx_horizontal_list_top_index;
88
INT        left_index;
89
INT        right_index;
90
91
76
    if (horizontal_list -> gx_horizontal_list_selected == index)
92
    {
93
11
        if ((horizontal_list -> gx_widget_style & GX_STYLE_REPEAT_SELECT) == 0)
94
        {
95
11
            return GX_SUCCESS;
96
        }
97
    }
98
99
65
    if (index < 0)
100
    {
101
8
        _gx_horizontal_list_selected_widget_get(horizontal_list, &child);
102
103
8
        if (child)
104
        {
105
8
            child -> gx_widget_style &= ~GX_STYLE_DRAW_SELECTED;
106
8
            _gx_system_dirty_mark(child);
107
        }
108
8
        horizontal_list -> gx_horizontal_list_selected = index;
109
8
        return GX_SUCCESS;
110
    }
111
112
    /* Calculate page index. */
113
57
    child = _gx_widget_first_client_child_get((GX_WIDGET *)horizontal_list);
114
115

103
    while (child && (child -> gx_widget_size.gx_rectangle_right <= horizontal_list -> gx_widget_size.gx_rectangle_left))
116
    {
117
46
        page_index++;
118
46
        child = _gx_widget_next_client_child_get(child);
119
    }
120
121
57
    left_index = horizontal_list -> gx_horizontal_list_top_index;
122
57
    right_index = left_index + horizontal_list -> gx_horizontal_list_child_count - 1;
123
124
    /* Reset page index when needed and calculate the child count from the top index to new index.  */
125
57
    if (right_index >= horizontal_list -> gx_horizontal_list_total_columns)
126
    {
127
12
        right_index -= horizontal_list -> gx_horizontal_list_total_columns;
128
129
12
        if (index >= left_index)
130
        {
131
3
            page_index = index - horizontal_list -> gx_horizontal_list_top_index;
132
        }
133
9
        else if (index <= right_index)
134
        {
135
6
            page_index = horizontal_list -> gx_horizontal_list_total_columns + index - horizontal_list -> gx_horizontal_list_top_index;
136
        }
137
        else
138
        {
139
3
            _gx_horizontal_list_page_index_set(horizontal_list, index);
140
3
            page_index = index - horizontal_list -> gx_horizontal_list_top_index;
141
        }
142
    }
143
    else
144
    {
145
45
        if (index < left_index)
146
        {
147
6
            _gx_horizontal_list_page_index_set(horizontal_list, index);
148
        }
149
39
        else if (index > right_index)
150
        {
151
10
            _gx_horizontal_list_page_index_set(horizontal_list, index - horizontal_list -> gx_horizontal_list_visible_columns + 1);
152
        }
153
154
45
        page_index = index - horizontal_list -> gx_horizontal_list_top_index;
155
    }
156
157
57
    if (page_index < 0)
158
    {
159
        /* consider the situation that top index is bigger than current select index.  */
160
2
        page_index += horizontal_list -> gx_horizontal_list_total_columns;
161
    }
162
163
57
    child = _gx_widget_first_client_child_get((GX_WIDGET *)horizontal_list);
164
165
    /* Select new index and update widget draw style.  */
166
571
    while (child)
167
    {
168
514
        if (page_index == 0)
169
        {
170
52
            found = child;
171
172
52
            horizontal_list -> gx_horizontal_list_selected = index;
173
52
            _gx_horizontal_list_selected_visible(horizontal_list, found);
174
52
            found -> gx_widget_style |= GX_STYLE_DRAW_SELECTED;
175
52
            _gx_system_dirty_mark(found);
176
177
52
            if (horizontal_list -> gx_widget_id)
178
            {
179
5
                _gx_widget_event_generate((GX_WIDGET *)horizontal_list, GX_EVENT_LIST_SELECT, horizontal_list -> gx_horizontal_list_selected);
180
            }
181
        }
182
        else
183
        {
184
462
            if (child -> gx_widget_style & GX_STYLE_DRAW_SELECTED)
185
            {
186
35
                child -> gx_widget_style &= ~GX_STYLE_DRAW_SELECTED;
187
35
                _gx_system_dirty_mark(child);
188
            }
189
        }
190
514
        child = _gx_widget_next_client_child_get(child);
191
514
        page_index--;
192
    }
193
194
57
    if (found)
195
    {
196
52
        return GX_SUCCESS;
197
    }
198
5
    return GX_FAILURE;
199
}
200