summaryrefslogtreecommitdiff
path: root/endless/eosmainarea.c
blob: 1d57bb478b5ec2f54b3336cb2849c812bfbf37cb (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Copyright 2013 Endless Mobile, Inc. */

#include "config.h"
#include "eosmainarea-private.h"

#include <gtk/gtk.h>

/*
 * SECTION:main_area
 * @short_description: The main area for your application, under the top bar.
 * @title: EosMainArea
 *
 * Stub
 */

G_DEFINE_TYPE (EosMainArea, eos_main_area, GTK_TYPE_CONTAINER)

#define MAIN_AREA_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EOS_TYPE_MAIN_AREA, EosMainAreaPrivate))

struct _EosMainAreaPrivate
{
  GtkWidget *toolbox;
  GtkWidget *content;
};

static void
eos_main_area_get_preferred_width (GtkWidget *widget,
                                   gint      *minimal,
                                   gint      *natural)
{
  EosMainArea *self = EOS_MAIN_AREA (widget);
  GtkWidget *content = self->priv->content;

  if (content && gtk_widget_get_visible (content))
    gtk_widget_get_preferred_width(content, minimal, natural);
}

static void
eos_main_area_get_preferred_height (GtkWidget *widget,
                                    gint      *minimal,
                                    gint      *natural)
{
  EosMainArea *self = EOS_MAIN_AREA (widget);
  GtkWidget *content = self->priv->content;

  if (content && gtk_widget_get_visible (content))
    gtk_widget_get_preferred_height(content, minimal, natural);
}

static void
eos_main_area_get_preferred_width_for_height (GtkWidget *widget,
                                              gint       for_height,
                                              gint      *minimal,
                                              gint      *natural)
{
  EosMainArea *self = EOS_MAIN_AREA (widget);
  GtkWidget *content = self->priv->content;

  if (content && gtk_widget_get_visible (content))
    gtk_widget_get_preferred_width_for_height (content, for_height,
                                               minimal, natural);
}

static void
eos_main_area_get_preferred_height_for_width (GtkWidget *widget,
                                              gint       for_width,
                                              gint      *minimal,
                                              gint      *natural)
{
  EosMainArea *self = EOS_MAIN_AREA (widget);
  GtkWidget *content = self->priv->content;

  if (content && gtk_widget_get_visible (content))
    gtk_widget_get_preferred_width_for_height (content, for_width,
                                               minimal, natural);
}

static void
eos_main_size_allocate (GtkWidget     *widget,
                        GtkAllocation *allocation)
{
  EosMainArea *self = EOS_MAIN_AREA (widget);
  GtkWidget *content = self->priv->content;

  gtk_widget_set_allocation (widget, allocation);
  if (content && gtk_widget_get_visible (content))
    gtk_widget_size_allocate (content, allocation);
}

static void
eos_main_area_forall(GtkContainer *container,
                     gboolean      include_internals,
                     GtkCallback   callback,
                     gpointer      callback_data)
{
  EosMainArea *self = EOS_MAIN_AREA (container);
  EosMainAreaPrivate *priv = self->priv;

  if (priv->toolbox)
    (*callback) (priv->toolbox, callback_data);

  if (priv->content)
    (*callback) (priv->content, callback_data);
}

static void
eos_main_area_class_init (EosMainAreaClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  g_type_class_add_private (klass, sizeof (EosMainAreaPrivate));

  widget_class->get_preferred_width = eos_main_area_get_preferred_width;
  widget_class->get_preferred_height = eos_main_area_get_preferred_height;
  widget_class->get_preferred_width_for_height = eos_main_area_get_preferred_width_for_height;
  widget_class->get_preferred_height_for_width = eos_main_area_get_preferred_height_for_width;
  widget_class->size_allocate = eos_main_size_allocate;

  container_class->forall = eos_main_area_forall;
}

static void
eos_main_area_init (EosMainArea *self)
{
  gtk_widget_set_has_window(GTK_WIDGET(self), FALSE);
  self->priv = MAIN_AREA_PRIVATE (self);
}

/* Internal Public API */

/*
 * eos_main_area_new:
 *
 * Creates a main area. It is invisible by default.
 *
 * Returns: a pointer to the main area widget.
 */
GtkWidget *
eos_main_area_new (void)
{
  return GTK_WIDGET (g_object_new (EOS_TYPE_MAIN_AREA, NULL));
}


/*
 * eos_main_area_set_toolbox:
 * @self: a #EosMainArea
 * @toolbox: the toolbox widget to be displayed on left of content.
 *
 * Adds the toolbox widget to the main area. Passing %NULL will hide the
 * toolbox area.
 */
void
eos_main_area_set_toolbox (EosMainArea *self,
                           GtkWidget   *toolbox)
{
  g_return_if_fail (EOS_IS_MAIN_AREA (self));
  g_return_if_fail (toolbox == NULL || GTK_IS_WIDGET (toolbox));
  g_return_if_fail (toolbox == NULL || gtk_widget_get_parent (toolbox) == NULL);

  EosMainAreaPrivate *priv = self->priv;
  GtkWidget *self_widget = GTK_WIDGET (self);

  if (priv->toolbox == toolbox)
    return;

  if (priv->toolbox)
    gtk_widget_unparent (priv->toolbox);

  priv->toolbox = toolbox;
  if (toolbox)
    gtk_widget_set_parent (toolbox, self_widget);

  if (gtk_widget_get_visible (self_widget))
    gtk_widget_queue_resize (self_widget);
}

/*
 * eos_main_area_get_toolbox:
 * @self: a #EosMainArea
 *
 * Retrieves the toolbox widget for the main area.
 *
 * Return value: (transfer none): the toolbox widget,
 *     or %NULL if there is none
 */
GtkWidget *
eos_main_area_get_toolbox (EosMainArea *self)
{
  g_return_val_if_fail (EOS_IS_MAIN_AREA (self), NULL);
  return self->priv->toolbox;
}

/*
 * eos_main_area_set_content:
 * @self: a #EosMainArea
 * @content: the content widget to be displayed in the center.
 *
 * Adds the content widget to the main area.
 */
void
eos_main_area_set_content (EosMainArea *self, GtkWidget *content)
{
  g_return_if_fail (EOS_IS_MAIN_AREA (self));
  g_return_if_fail (content == NULL || GTK_IS_WIDGET (content));
  g_return_if_fail (content == NULL || gtk_widget_get_parent (content) == NULL);

  EosMainAreaPrivate *priv = self->priv;
  GtkWidget *self_widget = GTK_WIDGET (self);

  if (priv->content == content)
    return;

  if (priv->content)
    gtk_widget_unparent (priv->content);

  priv->content = content;
  if (content)
    gtk_widget_set_parent (content, self_widget);

  if (gtk_widget_get_visible (self_widget))
    gtk_widget_queue_resize (self_widget);
}

/*
 * eos_main_area_get_content:
 * @self: a #EosMainArea
 *
 * Retrieves the content widget for the main area.
 *
 * Return value: (transfer none): the content widget,
 *     or %NULL if there is none
 */
GtkWidget *
eos_main_area_get_content (EosMainArea *self)
{
  g_return_val_if_fail (EOS_IS_MAIN_AREA (self), NULL);
  return self->priv->content;
}