2

thanks in advance for the help!

So here's the situation:

  1. My app builds a layout of widgets that are fixed size (containing text, with the size of the widget determined by the length of the text inside). Let's call them "words" for simplicity.
  2. These widgets are packed left to right horizontally into a box, with a line break (creating a new line box widget below) when it realizes that adding the next widget would make the line longer than the window. (Lines are set to expand to the width of the window.)
  3. I have it set up to re-wrap the lines when the window is resized (with a callback to my resize function on the "notify::default-width" signal).
  4. This works when the window is expanded, but not when I attempt to reduce its size.
  5. Apparently, it's fine to expand the window because then the lines expand to fit and the words are neatly repacked. However one can't reduce the size of the window, because each line contains a number of fixed-width words.
  6. Therefore, I wonder whether it's possible to issue some kind of notification when the user "requests" to reduce the size of the window and use this to trigger a re-wrap?

Thanks again!

2
  • You can request/monitor notifications for just about any event. You check this in your event loop or a callback to your code. When you get a resize event, you update your data for the width/height and then issue a call to your redraw code. This is pretty standard stuff. Without your code, we really can't help too much with the specifics. Commented May 9, 2024 at 0:06
  • We would really like an MRE. For C, we would like a single .c file [with (e.g.) #include <stdio.h>]. But, no (e.g.) #include "myprogram.h" (For those, copy-and-paste in the .h contents.) The errors may be in code that you don't suspect, so we really want full code. For runtime errors, it should compile cleanly. We should be able to download and inspect/build/run the code on our systems. We/you might: (1) compile with -Wall -fsanitize=address -O0 -g (2) run it under gdb, or strace (3) add debug printf Commented May 9, 2024 at 0:26

1 Answer 1

0

To track a GtkWindow size, you can track the width and height properties of the underlying GdkSurface. Please note that the window shall have a surface only after it has been mapped.

A minimal example that tracks window size changes:

/* resize.c
 *
 * Compile: cc -ggdb resize.c $(pkg-config --cflags --libs gtk4) -o resize
 * Run: ./resize
 *
 * Author: Mohammed Sadiq <www.sadiqpk.org>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later OR CC0-1.0
 */

#include <gtk/gtk.h>

static void
window_size_changed_cb (GtkWidget *widget)
{
  g_assert (GTK_IS_WINDOW (widget));

  g_warning ("Window size changed, width: %d, height: %d",
             gtk_widget_get_width (widget),
             gtk_widget_get_height (widget));
}

static void
window_mapped_cb (GtkWidget *widget)
{
  GdkSurface *surface;

  g_assert (GTK_IS_WINDOW (widget));

  surface = gtk_native_get_surface (GTK_NATIVE (widget));
  g_signal_connect_object (surface,
                           "notify::height",
                           G_CALLBACK (window_size_changed_cb),
                           widget, G_CONNECT_SWAPPED);
  g_signal_connect_object (surface,
                           "notify::width",
                           G_CALLBACK (window_size_changed_cb),
                           widget, G_CONNECT_SWAPPED);
}

static void
app_activated_cb (GtkApplication *app)
{
  GtkWindow *window;

  window = GTK_WINDOW (gtk_application_window_new (app));

  g_signal_connect_object (window,
                           "map",
                           G_CALLBACK (window_mapped_cb),
                           window, G_CONNECT_SWAPPED | G_CONNECT_AFTER);

  gtk_window_present (window);
}

int
main (int   argc,
      char *argv[])
{
  g_autoptr(GtkApplication) app = gtk_application_new (NULL, 0);

  g_signal_connect (app, "activate", G_CALLBACK (app_activated_cb), NULL);

  return g_application_run (G_APPLICATION (app), argc, argv);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.