@@ -81,30 +81,38 @@ PageView::PageView ():
8181 // Create the scrolled window where the page image will be.
8282 m_PageScroll = gtk_scrolled_window_new ();
8383
84- // GTK4: Create event box for page image (not needed in GTK4, add controllers directly)
84+ // Configure scrolled window to allow both horizontal and vertical scrolling
8585 m_EventBox = m_PageScroll;
8686 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (m_PageScroll),
8787 GTK_POLICY_AUTOMATIC,
8888 GTK_POLICY_AUTOMATIC);
8989
90- // The actual page image - use GtkPicture in GTK4 for better image handling
91- m_PageImage = gtk_picture_new ();
92- gtk_widget_set_margin_start (m_PageImage, PAGE_VIEW_PADDING);
93- gtk_widget_set_margin_end (m_PageImage, PAGE_VIEW_PADDING);
94- gtk_widget_set_margin_top (m_PageImage, PAGE_VIEW_PADDING);
95- gtk_widget_set_margin_bottom (m_PageImage, PAGE_VIEW_PADDING);
90+ // Create a container box to hold the image
91+ GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0 );
92+ gtk_widget_set_hexpand (box, TRUE );
93+ gtk_widget_set_vexpand (box, TRUE );
9694
97- // Configure the image to expand and be scrollable
98- gtk_widget_set_hexpand (m_PageImage, TRUE );
99- gtk_widget_set_vexpand (m_PageImage, TRUE );
100- gtk_widget_set_halign (m_PageImage, GTK_ALIGN_FILL);
101- gtk_widget_set_valign (m_PageImage, GTK_ALIGN_FILL);
95+ // Create the actual page image
96+ m_PageImage = gtk_picture_new ();
10297
103- // Set the image as the child of the scrolled window
104- gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (m_PageScroll), m_PageImage);
98+ // Configure the image to maintain its size and not be constrained by the window
99+ gtk_widget_set_hexpand (m_PageImage, FALSE );
100+ gtk_widget_set_vexpand (m_PageImage, FALSE );
101+ gtk_widget_set_halign (m_PageImage, GTK_ALIGN_START);
102+ gtk_widget_set_valign (m_PageImage, GTK_ALIGN_START);
105103
106- // Set minimum size to ensure the scrolled window has some space
107- gtk_widget_set_size_request (m_PageScroll, 100 , 100 );
104+ // Add padding around the image
105+ gtk_widget_set_margin_start (m_PageImage, PAGE_VIEW_PADDING);
106+ gtk_widget_set_margin_end (m_PageImage, PAGE_VIEW_PADDING);
107+ gtk_widget_set_margin_top (m_PageImage, PAGE_VIEW_PADDING);
108+ gtk_widget_set_margin_bottom (m_PageImage, PAGE_VIEW_PADDING);
109+
110+ // Add the image to the box and the box to the scrolled window
111+ gtk_box_append (GTK_BOX (box), m_PageImage);
112+ gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (m_PageScroll), box);
113+
114+ // Set a minimum size for the scrolled window
115+ gtk_widget_set_size_request (m_PageScroll, 100 , 100 );
108116
109117 // In GTK4, widgets are visible by default - no need for gtk_widget_show_all
110118
@@ -222,51 +230,62 @@ PageView::makeRectangleVisible (DocumentRectangle &rect, gdouble scale)
222230void
223231PageView::resizePage (gint width, gint height)
224232{
225- // GTK4: Use stored pixbuf since gtk_image_get_pixbuf is removed
226- if ( NULL != m_CurrentPixbuf )
233+ if (m_CurrentPixbuf != NULL )
227234 {
228- // Get the current scroll position
235+ // Get the current scroll position before resizing
229236 GtkAdjustment *hAdjustment = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (m_PageScroll));
230237 GtkAdjustment *vAdjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (m_PageScroll));
231238 gdouble hscroll = gtk_adjustment_get_value (hAdjustment);
232239 gdouble vscroll = gtk_adjustment_get_value (vAdjustment);
233240
234- // Get the viewport size
235- gint viewportWidth = gtk_widget_get_allocated_width (GTK_WIDGET (m_PageScroll));
236- gint viewportHeight = gtk_widget_get_allocated_height (GTK_WIDGET (m_PageScroll));
241+ // Calculate the ratio of the current scroll position to the total size
242+ gdouble hratio = 0.0 , vratio = 0.0 ;
243+ gdouble hadj_upper = gtk_adjustment_get_upper (hAdjustment);
244+ gdouble vadj_upper = gtk_adjustment_get_upper (vAdjustment);
245+
246+ if (hadj_upper > 0 )
247+ hratio = hscroll / hadj_upper;
248+ if (vadj_upper > 0 )
249+ vratio = vscroll / vadj_upper;
237250
238251 // Calculate the new size with padding
239252 gint newWidth = width + (2 * PAGE_VIEW_PADDING);
240253 gint newHeight = height + (2 * PAGE_VIEW_PADDING);
241254
242- // Update the scrolled window's adjustments
243- gtk_adjustment_set_upper (hAdjustment, MAX (newWidth, viewportWidth));
244- gtk_adjustment_set_page_size (hAdjustment, viewportWidth);
245- gtk_adjustment_set_step_increment (hAdjustment, viewportWidth * 0.1 );
246- gtk_adjustment_set_page_increment (hAdjustment, viewportWidth * 0.9 );
247-
248- gtk_adjustment_set_upper (vAdjustment, MAX (newHeight, viewportHeight));
249- gtk_adjustment_set_page_size (vAdjustment, viewportHeight);
250- gtk_adjustment_set_step_increment (vAdjustment, viewportHeight * 0.1 );
251- gtk_adjustment_set_page_increment (vAdjustment, viewportHeight * 0.9 );
255+ // Scale the pixbuf to the new size
256+ GdkPixbuf *scaledPage = gdk_pixbuf_scale_simple (m_CurrentPixbuf,
257+ width, height,
258+ GDK_INTERP_BILINEAR);
252259
253- // Scale the pixbuf
254- GdkPixbuf *scaledPage = gdk_pixbuf_scale_simple (m_CurrentPixbuf,
255- width, height,
256- GDK_INTERP_BILINEAR);
257- if ( NULL != scaledPage )
260+ if (scaledPage != NULL )
258261 {
259- GdkTexture *texture = gdk_texture_new_for_pixbuf (scaledPage);
260- gtk_picture_set_paintable (GTK_PICTURE (m_PageImage), GDK_PAINTABLE (texture));
262+ // Create a texture from the scaled pixbuf
263+ GdkTexture *texture = gdk_texture_new_for_pixbuf (scaledPage);
264+
265+ // Set the texture to the picture
266+ gtk_picture_set_paintable (GTK_PICTURE (m_PageImage), GDK_PAINTABLE (texture));
267+
268+ // Update the scrolled window's adjustments
269+ gtk_widget_set_size_request (m_PageImage, newWidth, newHeight);
270+
271+ // Update the adjustments to match the new content size
272+ gtk_adjustment_set_upper (hAdjustment, MAX (newWidth, 1 ));
273+ gtk_adjustment_set_upper (vAdjustment, MAX (newHeight, 1 ));
274+
275+ // Restore scroll position based on the previous ratio
276+ hscroll = hratio * MAX (newWidth - gtk_adjustment_get_page_size (hAdjustment), 0 );
277+ vscroll = vratio * MAX (newHeight - gtk_adjustment_get_page_size (vAdjustment), 0 );
278+
279+ // Make sure the scroll position is within bounds
280+ hscroll = CLAMP (hscroll, 0 , MAX (newWidth - gtk_adjustment_get_page_size (hAdjustment), 0 ));
281+ vscroll = CLAMP (vscroll, 0 , MAX (newHeight - gtk_adjustment_get_page_size (vAdjustment), 0 ));
261282
262- // Restore scroll position
263- hscroll = CLAMP (hscroll, 0 , newWidth - viewportWidth);
264- vscroll = CLAMP (vscroll, 0 , newHeight - viewportHeight);
265283 gtk_adjustment_set_value (hAdjustment, hscroll);
266284 gtk_adjustment_set_value (vAdjustment, vscroll);
267285
268- g_object_unref (texture);
269- g_object_unref (scaledPage);
286+ // Clean up
287+ g_object_unref (texture);
288+ g_object_unref (scaledPage);
270289 }
271290 }
272291}
0 commit comments