Search code examples
c++gtkmm

Process only tiled event and not maximize event


I'm building a very basic calculator program using GTKMM

Basic Calculator screenshot

The layout is in landscape mode (buttons and display label in horizontal orientation) by design

I want to orient those two to portrait mode (ie., in vertical) when the user snaps/tiles the window to the right or left

Below is a sample code I used:

bool
BasicCalculator::on_calculator_window_state_changed(
        GdkEventWindowState     *window_state_event,
            Gtk::Box            *box)
{
        if (
                window_state_event->new_window_state &
                (Gdk::WINDOW_STATE_RIGHT_TILED | Gdk::WINDOW_STATE_LEFT_TILED)
        )
                box->set_orientation(Gtk::ORIENTATION_VERTICAL);
        else
                box->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
        return true;
}

The code works but my window orients vertically when maximized which is not my intention. I want it to be in horizontal orientation

How do I process only the tiled event and not the maximize event?



PS: My project repo in case you want to build & test


Solution

  • After hacking around a bit with the GdkEventWindowState enums, I figured I can compare against the tiled state with the window maximized state together

    ie., make sure to switch orientation to vertical only if the window is tiled and not maximized

    Because the code

    ...
            if (
                    window_state_event->new_window_state &
                    (Gdk::WINDOW_STATE_RIGHT_TILED | Gdk::WINDOW_STATE_LEFT_TILED)
            )
                    ...
    

    returns TRUE not just for the tiled state but also for maximized state also

    Below is the improved code that works:

    bool
    BasicCalculator::on_calculator_window_state_changed(
            GdkEventWindowState     *window_state_event,
                Gtk::Box            *box)
    {
            bool is_window_tiled = window_state_event->new_window_state &
                            (Gdk::WINDOW_STATE_RIGHT_TILED | Gdk::WINDOW_STATE_LEFT_TILED);
            bool is_window_maximized = window_state_event->new_window_state &
                            Gdk::WINDOW_STATE_MAXIMIZED;
    
            if (is_window_tiled && !is_window_maximized)
                    box->set_orientation(Gtk::ORIENTATION_VERTICAL);
            else
                    box->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
    
            return true;
    }
    

    Now the window keeps its default horizontal orientation even if maximized :)