summaryrefslogtreecommitdiff
path: root/src/emacsgtkfixed.c
blob: fe3514bce936addfe6ab51f9b337f09c34817852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* A Gtk Widget that inherits GtkFixed, but can be shrinked. 

Copyright (C) 2011  Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */

#include "emacsgtkfixed.h"


struct _EmacsFixedPrivate
{
  int minwidth, minheight;
};


static void emacs_fixed_get_preferred_width  (GtkWidget *widget,
                                              gint      *minimum,
                                              gint      *natural);
static void emacs_fixed_get_preferred_height (GtkWidget *widget,
                                              gint      *minimum,
                                              gint      *natural);
G_DEFINE_TYPE (EmacsFixed, emacs_fixed, GTK_TYPE_FIXED)

static void
emacs_fixed_class_init (EmacsFixedClass *klass)
{
  GtkWidgetClass *widget_class;
  GtkFixedClass *fixed_class;

  widget_class = (GtkWidgetClass*) klass;
  fixed_class = (GtkFixedClass*) klass;

  widget_class->get_preferred_width = emacs_fixed_get_preferred_width;
  widget_class->get_preferred_height = emacs_fixed_get_preferred_height;
  g_type_class_add_private (klass, sizeof (EmacsFixedPrivate));
}

static GType
emacs_fixed_child_type (GtkFixed *container)
{
  return GTK_TYPE_WIDGET;
}

static void
emacs_fixed_init (EmacsFixed *fixed)
{
  fixed->priv = G_TYPE_INSTANCE_GET_PRIVATE (fixed, EMACS_TYPE_FIXED,
                                             EmacsFixedPrivate);
  fixed->priv->minwidth = fixed->priv->minheight = 0;
}

/**
 * emacs_fixed_new:
 *
 * Creates a new #EmacsFixed.
 *
 * Returns: a new #EmacsFixed.
 */
GtkWidget*
emacs_fixed_new (void)
{
  return g_object_new (EMACS_TYPE_FIXED, NULL);
}

static GtkWidgetClass *
get_parent_class (EmacsFixed *fixed)
{
  EmacsFixedClass *klass = EMACS_FIXED_GET_CLASS (fixed);
  GtkFixedClass *parent_class = g_type_class_peek_parent (klass);
  return (GtkWidgetClass*) parent_class;
}

static void
emacs_fixed_get_preferred_width (GtkWidget *widget,
                                 gint      *minimum,
                                 gint      *natural)
{
  EmacsFixed *fixed = EMACS_FIXED (widget);
  EmacsFixedPrivate *priv = fixed->priv;
  GtkWidgetClass *widget_class = get_parent_class (fixed);
  widget_class->get_preferred_width (widget, minimum, natural);
  if (minimum) *minimum = priv->minwidth;
}

static void
emacs_fixed_get_preferred_height (GtkWidget *widget,
                                  gint      *minimum,
                                  gint      *natural)
{
  EmacsFixed *fixed = EMACS_FIXED (widget);
  EmacsFixedPrivate *priv = fixed->priv;
  GtkWidgetClass *widget_class = get_parent_class (fixed);
  widget_class->get_preferred_height (widget, minimum, natural);
  if (minimum) *minimum = priv->minheight;
}

void
emacs_fixed_set_min_size (EmacsFixed *widget, int width, int height)
{
  EmacsFixedPrivate *priv = widget->priv;
  GtkWidgetClass *widget_class = get_parent_class (widget);
  int mw, nw, mh, nh;

  widget_class->get_preferred_height (GTK_WIDGET (widget), &mh, &nh);
  widget_class->get_preferred_width (GTK_WIDGET (widget), &mw, &nw);

  /* Gtk complains if min size is less than natural size.  */
  if (width <= nw) priv->minwidth = width;
  if (height <= nh) priv->minheight = height;
}