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 
_gx_horizontal_list_selected_set(GX_HORIZONTAL_LIST * horizontal_list,INT index)83 UINT _gx_horizontal_list_selected_set(GX_HORIZONTAL_LIST *horizontal_list, INT index)
84 {
85 GX_WIDGET *child;
86 GX_WIDGET *found = GX_NULL;
87 INT        page_index = horizontal_list -> gx_horizontal_list_top_index;
88 INT        left_index;
89 INT        right_index;
90 
91     if (horizontal_list -> gx_horizontal_list_selected == index)
92     {
93         if ((horizontal_list -> gx_widget_style & GX_STYLE_REPEAT_SELECT) == 0)
94         {
95             return GX_SUCCESS;
96         }
97     }
98 
99     if (index < 0)
100     {
101         _gx_horizontal_list_selected_widget_get(horizontal_list, &child);
102 
103         if (child)
104         {
105             child -> gx_widget_style &= ~GX_STYLE_DRAW_SELECTED;
106             _gx_system_dirty_mark(child);
107         }
108         horizontal_list -> gx_horizontal_list_selected = index;
109         return GX_SUCCESS;
110     }
111 
112     /* Calculate page index. */
113     child = _gx_widget_first_client_child_get((GX_WIDGET *)horizontal_list);
114 
115     while (child && (child -> gx_widget_size.gx_rectangle_right <= horizontal_list -> gx_widget_size.gx_rectangle_left))
116     {
117         page_index++;
118         child = _gx_widget_next_client_child_get(child);
119     }
120 
121     left_index = horizontal_list -> gx_horizontal_list_top_index;
122     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     if (right_index >= horizontal_list -> gx_horizontal_list_total_columns)
126     {
127         right_index -= horizontal_list -> gx_horizontal_list_total_columns;
128 
129         if (index >= left_index)
130         {
131             page_index = index - horizontal_list -> gx_horizontal_list_top_index;
132         }
133         else if (index <= right_index)
134         {
135             page_index = horizontal_list -> gx_horizontal_list_total_columns + index - horizontal_list -> gx_horizontal_list_top_index;
136         }
137         else
138         {
139             _gx_horizontal_list_page_index_set(horizontal_list, index);
140             page_index = index - horizontal_list -> gx_horizontal_list_top_index;
141         }
142     }
143     else
144     {
145         if (index < left_index)
146         {
147             _gx_horizontal_list_page_index_set(horizontal_list, index);
148         }
149         else if (index > right_index)
150         {
151             _gx_horizontal_list_page_index_set(horizontal_list, index - horizontal_list -> gx_horizontal_list_visible_columns + 1);
152         }
153 
154         page_index = index - horizontal_list -> gx_horizontal_list_top_index;
155     }
156 
157     if (page_index < 0)
158     {
159         /* consider the situation that top index is bigger than current select index.  */
160         page_index += horizontal_list -> gx_horizontal_list_total_columns;
161     }
162 
163     child = _gx_widget_first_client_child_get((GX_WIDGET *)horizontal_list);
164 
165     /* Select new index and update widget draw style.  */
166     while (child)
167     {
168         if (page_index == 0)
169         {
170             found = child;
171 
172             horizontal_list -> gx_horizontal_list_selected = index;
173             _gx_horizontal_list_selected_visible(horizontal_list, found);
174             found -> gx_widget_style |= GX_STYLE_DRAW_SELECTED;
175             _gx_system_dirty_mark(found);
176 
177             if (horizontal_list -> gx_widget_id)
178             {
179                 _gx_widget_event_generate((GX_WIDGET *)horizontal_list, GX_EVENT_LIST_SELECT, horizontal_list -> gx_horizontal_list_selected);
180             }
181         }
182         else
183         {
184             if (child -> gx_widget_style & GX_STYLE_DRAW_SELECTED)
185             {
186                 child -> gx_widget_style &= ~GX_STYLE_DRAW_SELECTED;
187                 _gx_system_dirty_mark(child);
188             }
189         }
190         child = _gx_widget_next_client_child_get(child);
191         page_index--;
192     }
193 
194     if (found)
195     {
196         return GX_SUCCESS;
197     }
198     return GX_FAILURE;
199 }
200 
201