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 /**   Vertical 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_utility.h"
31 #include "gx_scrollbar.h"
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _gx_vertical_list_total_rows_set                    PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Kenneth Maxwell, Microsoft Corporation                              */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function assigns total number of list rows.                    */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    vertical_list                         Vertical list control block   */
50 /*    count                                 Number of rows                */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _gx_system_lock                       Obtain GUIX system lock       */
59 /*    _gx_system_unlock                     Release GUIX system lock      */
60 /*    _gx_first_client_child_get            Get the first client child    */
61 /*    [gx_vertical_list_callback]           Vertical list callback        */
62 /*    _gx_window_scrollbar_find             Find the scrollbar            */
63 /*    _gx_scrollbar_reset                   Reset the schollbar           */
64 /*    _gx_system_dirty_mark                 Mark the widget dirty         */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    GUIX Internal Code                                                  */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
75 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*  01-31-2022     Ting Zhu                 Modified comment(s),          */
78 /*                                            improved logic,             */
79 /*                                            resulting in version 6.1.10 */
80 /*                                                                        */
81 /**************************************************************************/
_gx_vertical_list_total_rows_set(GX_VERTICAL_LIST * list,INT count)82 UINT _gx_vertical_list_total_rows_set(GX_VERTICAL_LIST *list, INT count)
83 {
84 INT           page_index;
85 INT           index;
86 GX_WIDGET    *test;
87 GX_SCROLLBAR *pScroll;
88 
89     _gx_system_lock();
90 
91     /* Update total count of rows. */
92     list -> gx_vertical_list_total_rows = count;
93 
94     /* Update selected index. */
95     if (list -> gx_vertical_list_selected < 0)
96     {
97         list -> gx_vertical_list_selected = 0;
98     }
99 
100     if (list -> gx_vertical_list_selected > count - 1)
101     {
102         list -> gx_vertical_list_selected = count - 1;
103     }
104 
105     /* Calculate current page index. */
106     page_index = list -> gx_vertical_list_top_index;
107 
108     test = _gx_widget_first_client_child_get((GX_WIDGET *)list);
109 
110     while (test && (test -> gx_widget_size.gx_rectangle_bottom <= list -> gx_widget_size.gx_rectangle_top))
111     {
112         page_index++;
113         test = _gx_widget_next_client_child_get(test);
114     }
115 
116     /* Calculate new page index */
117     if (page_index + list -> gx_vertical_list_visible_rows > count)
118     {
119         if (count > list -> gx_vertical_list_visible_rows)
120         {
121             page_index = count - list -> gx_vertical_list_visible_rows;
122         }
123         else
124         {
125             page_index = 0;
126         }
127     }
128 
129     /* Add idle children back to vertical list.  */
130     if (list -> gx_vertical_list_idle_child_list)
131     {
132         while (list -> gx_vertical_list_idle_child_list)
133         {
134             test = list -> gx_vertical_list_idle_child_list;
135             list -> gx_vertical_list_idle_child_list = list -> gx_vertical_list_idle_child_list -> gx_widget_next;
136 
137             _gx_widget_attach((GX_WIDGET *)list, test);
138             list -> gx_vertical_list_child_count++;
139         }
140     }
141 
142     /* Check whether list child count is larger than count. */
143     while (list -> gx_vertical_list_child_count > count)
144     {
145         test = _gx_widget_last_client_child_get((GX_WIDGET *)list);
146 
147         if (test)
148         {
149             _gx_widget_detach(test);
150 
151             /* Put detached widget to idle list.  */
152             test -> gx_widget_next = list -> gx_vertical_list_idle_child_list;
153             list -> gx_vertical_list_idle_child_list = test;
154             list -> gx_vertical_list_child_count--;
155         }
156         else
157         {
158             return GX_FAILURE;
159         }
160     }
161 
162     list -> gx_vertical_list_top_index = 0;
163     index = 0;
164     test = _gx_widget_first_client_child_get((GX_WIDGET *)list);
165 
166     while (test)
167     {
168         list -> gx_vertical_list_callback(list, test, index++);
169 
170         test = _gx_widget_next_client_child_get(test);
171     }
172 
173     /* Reposition child widgets.  */
174     _gx_vertical_list_children_position(list);
175 
176     /* Make new page index visible */
177     _gx_vertical_list_page_index_set(list, page_index);
178 
179     _gx_window_scrollbar_find((GX_WINDOW *)list, GX_TYPE_VERTICAL_SCROLL, &pScroll);
180 
181     if (pScroll)
182     {
183         _gx_scrollbar_reset(pScroll, GX_NULL);
184     }
185 
186     _gx_system_unlock();
187 
188     /* Refresh screen. */
189     if (list -> gx_widget_status & GX_STATUS_VISIBLE)
190     {
191         _gx_system_dirty_mark((GX_WIDGET *)list);
192     }
193     return GX_SUCCESS;
194 }
195 
196