• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • About
  • Life
  • Tech
  • Travel
  • Work
  • Questions
  • Contact

Welcome

.

Connecting to the realize signal: strange window sizing behavior

April 10, 2020 by

Questions › Connecting to the realize signal: strange window sizing behavior
0
Vote Up
Vote Down
Garmaine asked 4 years ago

I am using Gtkmm3 (Ubuntu) to make a small GUI application. In this application, I have a couple windows to create which basically all follow the same pattern:

  1. registering a main layout (Gtk::Grid);
  2. configuring window properties (icon, title, etc);
  3. configuring layout properties (expanding, sub-layouts, etc);
  4. configuring window widgets (adding them to layouts, labels, etc).

In hope of not having to rewrite all of this logic every time I create a new window, I have written the following base class:

template<typename GtkmmWindow>
class Window
{

public:

    Window();

    virtual int Show() = 0;


protected:

    virtual void ConfigureWindow() = 0;
    virtual void ConfigureLayouts() = 0;
    virtual void ConfigureWidgets() = 0;

    void Init();


    Gtk::Grid m_mainLayout;
    GtkmmWindow m_window;

};

template<typename GtkmmWindow>
Window<GtkmmWindow>::Window()
{
    m_window.add(m_mainLayout);

    // When signal 'realize' is sent, 'Init' will be triggered.
    // This happens after construction, so virtual methods can
    // be used safely:
    m_window.signal_realize().connect([this](){Init();});
}

// Initialize child window according to its own needs:
template<typename GtkmmWindow>
void Window<GtkmmWindow>::Init()
{
    ConfigureWindow();
    ConfigureLayouts();
    ConfigureWidgets();

    // If this line is removed, no widgets are shown.
    m_window.show_all_children();
}

The goal of this class is to make sure points 1 to 4 are implemented by all windows the same way. It does so by calling appropriate virtual methods (to be redefined in concrete child classes) when the realize signal is sent. This is because when the realize signal is sent, I know the window constructors have been called and that I can safely use virtual methods.

For example, here is how I use it to create an application main window:

class MyWindow : public Window<Gtk::ApplicationWindow>
{

public:

    MyWindow(Gtk::Application& p_app) : m_app{p_app} {}

    int Show() override
    {
        m_window.show_all();
        return m_app.run(m_window);
    }

private:

    Gtk::Application& m_app;
    Gtk::Button m_button;

    void ConfigureWindow() override
    {
        m_window.set_title("SO Question");

        // If I set this to false, the window shrinks to fit the button size:
        m_window.set_resizable(false);
    }

    void ConfigureLayouts() override
    {
        m_mainLayout.override_background_color(Gdk::RGBA("yellow"));
    }

    void ConfigureWidgets() override
    {
        m_mainLayout.attach(m_button, 0, 0, 1, 1);
        m_button.set_label("Hello");
    }

};

This main window sets the main layout to have a yellow background, has a Gtk::Button with label "Hello" registered in the main layout. The problem I have with this strategy is that when I run the code, I get weird window/layout sizing:

enter image description here

Notice the yellow layout is way bigger than the only widget (the button) contained in it. This is the result I would have expected:

enter image description here

That is, the window and main layout should shrink to the size of their only contained widget. Weirdly, if I make the window set_resizable(false), I get the sizing I want, but then I can't resize it anymore, which is often not acceptable.

Questions:

  1. Why is it no so?
  2. How can I achieve this without duplicating the base code for every window?

You can build this code using g++ by adding it to:

#include <memory>
#include <gtkmm.h>

// Add here...

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create(argc, argv, "so.realize");

  std::unique_ptr<MyWindow> mainWindow = std::make_unique<MyWindow>(*(app.get()));

  return mainWindow->Show();
}

and running:

g++ -std=c++17 main.cpp -o example.out `pkg-config gtkmm-3.0 --cflags --libs`
Are you looking for the answer?
Original Question and Possible Answers can be found on `http://stackoverflow.com`

Question Tags: c++, gtk, gtk3, gtkmm, gtkmm3

Please login or Register to submit your answer




Primary Sidebar

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Newsletter

Complete the form below, and we'll send you all the latest news.

Footer

Footer Funnies

Who knew that reading the footer could be such a hilarious adventure? As we navigate websites, books, and documents, we often stumble upon the unassuming space at the bottom, only to discover a treasure trove of amusement. In this side-splitting compilation, we present 100 jokes that celebrate the unsung hero of content – the footer. Get ready to chuckle, giggle, and maybe even snort as we dive into the world of footnotes, disclaimers, and hidden comedic gems. Brace yourself for a wild ride through the footer!

Recent

  • Unveiling the Enigma: Almost-Magical Lamp Lights Highway Turns
  • The Impact of Young Onset Dementia on Employment and Finances: Optimizing Post-Diagnostic Approaches
  • 11 Wonders of 2023 Technological Breakthrough – Unveiling the Future
  • Work from Home and Stay Mentally Sane – Achieve Productivity and Well-being
  • Hidden Gems of New York City – Uncover the Must-See Treasures!

Search

Tags

Advancements best Business strategies commercial convenience economic Finances Cognitive decline Financial growth firm Future Hidden Gems Home hydration Impact Innovations lighting line of work Mental health Must-See New York City office patronage Productivity profession Profitability tips Profit optimization pursuit recreation Revenue enhancement romance sippy cups social station Technological breakthroughs technology toddlers trading transaction Treasures Uncover undertaking Well-being Wonders Work Young onset dementia

Copyright © 2023