@@ -119,25 +119,48 @@ static void pad_box_refresh(struct widget *w)
119119 ax + w -> lx + w -> w - 1 );
120120}
121121
122+ static bool widget_offset_in_ancestor (struct widget * ancestor , struct widget * w ,
123+ int * out_y , int * out_x )
124+ {
125+ int y = 0 , x = 0 ;
126+
127+ while (w && w != ancestor ) {
128+ y += w -> ly ;
129+ x += w -> lx ;
130+ w = w -> parent ;
131+ }
132+
133+ if (w != ancestor )
134+ return false;
135+
136+ * out_y = y ;
137+ * out_x = x ;
138+ return true;
139+ }
140+
122141static void pad_box_ensure_visible (struct widget * container , struct widget * child )
123142{
124143 struct widget_pad_box * st = container -> state .pad_box ;
125144
126- int top_y = st -> scroll_y ;
127- int bot_y = st -> scroll_y + container -> h ;
145+ int cy , cx ;
146+ if (!widget_offset_in_ancestor (container , child , & cy , & cx ))
147+ return ;
148+
149+ int view_top = st -> scroll_y ;
150+ int view_bottom = st -> scroll_y + container -> h ;
128151
129- int left_x = st -> scroll_x ;
130- int right_x = st -> scroll_x + container -> w ;
152+ int view_left = st -> scroll_x ;
153+ int view_right = st -> scroll_x + container -> w ;
131154
132- if (child -> ly < top_y )
133- st -> scroll_y = child -> ly ;
134- else if (child -> ly + child -> h > bot_y )
135- st -> scroll_y = child -> ly + child -> h - container -> h ;
155+ if (cy < view_top )
156+ st -> scroll_y = cy ;
157+ else if (cy + child -> h > view_bottom )
158+ st -> scroll_y = cy + child -> h - container -> h ;
136159
137- if (child -> lx < left_x )
138- st -> scroll_x = child -> lx ;
139- else if (child -> lx + child -> w > right_x )
140- st -> scroll_x = child -> lx + child -> w - container -> w ;
160+ if (cx < view_left )
161+ st -> scroll_x = cx ;
162+ else if (cx + child -> w > view_right )
163+ st -> scroll_x = cx + child -> w - container -> w ;
141164
142165 pad_box_clamp_scroll (container );
143166}
0 commit comments