Alex44
Alex44

Reputation: 3855

Gtkmm - change minimum size of Window

I try to change the minimum size of an Gtk::Window. It should be possible to shrink the window to a smaller size then the biggest container in it to a specific size.

I try several approaches you can see below. Nothing shows any effect. The minimum always defined by the image size. What did I do wrong?

main.cpp

#include "MainWindow.h"
#include <gtkmm.h>

int main (int argc, char* argv[])
{
  Glib::RefPtr<Gtk::Application> app =
    Gtk::Application::create(argc, argv, "org.gtkmm.example");

  MainWindow mainWindow;
  return app->run(mainWindow);
}

MainWindow.h

#ifndef MAINWINDOW_H_INCLUDED
#define MAINWINDOW_H_INCLUDED

#include <gtkmm.h>
#include <gdkmm.h>

class MainWindow : public Gtk::Window
{
  public:
    MainWindow();
  private:
    Gtk::Image   m_Image;
};

#endif // MAINWINDOW_H_INCLUDED

MainWindow.cpp

#include "MainWindow.h"

#define APPROACH_05

MainWindow::MainWindow() :
  m_Image( "image.png" )
{
  this->set_border_width(0);

#ifdef APPROACH_01
  this->add(m_Image);
  m_Image.set_size_request(5,5);
#endif // APPROACH_01

#ifdef APPROACH_02
  this->add(m_Image);
  this->set_size_request(5,5);
#endif // APPROACH_02

#ifdef APPROACH_03
  this->add(m_Image);
  Gtk::Allocation allocation = m_Image.get_allocation();
  allocation.set_width(5);
  allocation.set_height(5);
  m_Image.set_allocation(allocation);
#endif // APPROACH_03

#ifdef APPROACH_04
  this->add(m_Image);
  Gtk::Allocation allocation = this->get_allocation();
  allocation.set_width(5);
  allocation.set_height(5);
  this->set_allocation(allocation);
#endif // APPROACH_04

#ifdef APPROACH_05
  this->add(m_Image);
  Gdk::Geometry geom = {
    .min_width   = 5,
    .min_height  = 5,
  };
  Gtk::Window::set_geometry_hints(*this,geom,Gdk::HINT_MIN_SIZE);
#endif // APPROACH_05

  this->show_all_children();
}

compiled with:

g++ main.cpp MainWindow.cpp `pkg-config gtkmm-3.0 --cflags --libs` -o prog

@ptomato Thanks for your response. I have tried it this way:

#ifdef APPROACH_06
  this->add(m_ScrolledWindow);
  m_ScrolledWindow.set_border_width(0);
  m_ScrolledWindow.set_policy(Gtk::POLICY_NEVER,Gtk::POLICY_ALWAYS);
  m_ScrolledWindow.add(m_Image);
#endif // APPROACH_06

Now I can resize the window vertically, but I see the vertical scrollbar. If I set the policy to POLICY_NEVER like in the horizontal axis the window width is limited to the image width. Additionally the size of the slider limits the height too.

Upvotes: 0

Views: 1956

Answers (2)

snuk182
snuk182

Reputation: 1072

There is an approach to add a layer between the window and its contents, made of a custom container that lies to the parent window about its preferred size.

Here is my overridden GtkFixed that solves exactly the same problem:

reckless_fixed.h

#ifndef __RECKLESS_FIXED_H__
#define __RECKLESS_FIXED_H__

#ifndef NO_INCLUDE_WITHIN_HEADERS
#include <gtk/gtk.h>
#endif

#define RECKLESS_FIXED_TYPE                  (reckless_fixed_get_type ())
#define RECKLESS_FIXED(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), RECKLESS_FIXED_TYPE, RecklessFixed))
#define RECKLESS_FIXED_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST  ((klass), RECKLESS_FIXED_TYPE, RecklessFixedClass))
#define IS_RECKLESS_FIXED(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RECKLESS_FIXED_TYPE))
#define IS_RECKLESS_FIXED_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE  ((klass), RECKLESS_FIXED_TYPE))
#define RECKLESS_FIXED_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS  ((obj), RECKLESS_FIXED_TYPE, RecklessFixedClass))

typedef struct _RecklessFixed      RecklessFixed;
typedef struct _RecklessFixedClass RecklessFixedClass;

struct _RecklessFixed
{
    GtkFixed container;
};

struct _RecklessFixedClass
{
    GtkFixedClass container_class;
};

GType reckless_fixed_get_type(void);
GtkWidget* reckless_fixed_new(void);

static void reckless_fixed_get_preferred_width(GtkWidget *widget, int *minimal, int *natural);
static void reckless_fixed_get_preferred_height(GtkWidget *widget, int *minimal, int *natural);

#endif /* __RECKLESS_FIXED_H__ */

reckless_fixed.c

#include <gtk/gtk.h>
#include "reckless_fixed.h"

G_DEFINE_TYPE( RecklessFixed, reckless_fixed, GTK_TYPE_FIXED )

static void reckless_fixed_class_init( RecklessFixedClass* klass ) {
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
    widget_class->get_preferred_width = reckless_fixed_get_preferred_width;
    widget_class->get_preferred_height = reckless_fixed_get_preferred_height;
}
static void reckless_fixed_init( RecklessFixed* self ) {}

// here is where we are reckless about our content's size
static void reckless_fixed_get_preferred_width(GtkWidget *widget, int *minimal, int *natural) {
    *minimal = *natural = 1; 
}
//here too
static void reckless_fixed_get_preferred_height(GtkWidget *widget, int *minimal, int *natural) {
    *minimal = *natural = 1;
}
GtkWidget* reckless_fixed_new (void) {
  return g_object_new (RECKLESS_FIXED_TYPE, NULL);
}

Please pay attention to the GTK version - the example above is applicable to GTK-3.0. For <=2.0 the ::size-request signal exists, by connecting and answering to which the same goal is achieved.

Besides I'd warn about this approach - do it only for the cases where you're absolutely sure about what you are doing and why you need the content-independent window resize, as this breaks the inbuilt GTK layout system.

Upvotes: 1

ptomato
ptomato

Reputation: 57870

If you want to be able to shrink the window down to a smaller size than the image inside it, then you need to put the image inside of a Gtk::ScrolledWindow before adding it to the window. Without scrolling, then the image wouldn't know what part of itself to render when you made the window smaller than the image.

Upvotes: 1

Related Questions