summaryrefslogtreecommitdiff
path: root/endless/eostopbar.c
blob: 886d36ad8359fc5698e5f9a81f04ff09ac01a9c1 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Copyright 2013 Endless Mobile, Inc. */

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

#include <glib-object.h>
#include <gtk/gtk.h>

/*
 * SECTION:topbar
 * @short_description: The top bar of the window, above the main area of your
 * application.
 * @title: TopBar
 *
 * The #EosTopBar has two different areas that can be managed through this
 * class: a left widget and a center widget, which can both contain any other
 * widget.
 */
#define _EOS_TOP_BAR_HEIGHT_PX 36

typedef struct {
  GtkWidget *center_top_bar_attach;  /* needed to suppress default title */
  GtkWidget *left_top_bar_widget;
  GtkWidget *center_top_bar_widget;

  GtkWidget *credits_button;

  gboolean show_credits_button;
  guint credits_enter_handler;
  guint credits_leave_handler;
} EosTopBarPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (EosTopBar, eos_top_bar, GTK_TYPE_HEADER_BAR)

enum {
  CREDITS_CLICKED,
  LAST_SIGNAL
};

static guint top_bar_signals[LAST_SIGNAL] = { 0 };

enum {
  PROP_0,
  PROP_SHOW_CREDITS_BUTTON,
  NPROPS
};

static GParamSpec *eos_top_bar_props[NPROPS] = { NULL, };

static void
eos_top_bar_constructed (GObject *object)
{
  EosTopBar *self = EOS_TOP_BAR (object);
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);
  gtk_header_bar_set_custom_title (GTK_HEADER_BAR (self),
                                   priv->center_top_bar_attach);
}

static void
eos_top_bar_get_property (GObject    *object,
                          guint       property_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  EosTopBar *self = EOS_TOP_BAR (object);

  switch (property_id)
    {
    case PROP_SHOW_CREDITS_BUTTON:
      g_value_set_boolean (value, eos_top_bar_get_show_credits_button (self));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
eos_top_bar_set_property (GObject      *object,
                          guint         property_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  EosTopBar *self = EOS_TOP_BAR (object);

  switch (property_id)
    {
    case PROP_SHOW_CREDITS_BUTTON:
      eos_top_bar_set_show_credits_button (self, g_value_get_boolean (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
eos_top_bar_get_preferred_height (GtkWidget *widget,
                                  int *minimum,
                                  int *natural)
{
  gboolean left_widget_visible = FALSE, center_widget_visible = FALSE;
  EosTopBar *self = EOS_TOP_BAR (widget);
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);
  if (priv->left_top_bar_widget)
    {
      left_widget_visible = gtk_widget_get_visible (priv->left_top_bar_widget);
      gtk_widget_set_visible (priv->left_top_bar_widget, TRUE);
    }
  if (priv->center_top_bar_widget)
    {
      center_widget_visible = gtk_widget_get_visible (priv->center_top_bar_widget);
      gtk_widget_set_visible (priv->center_top_bar_widget, TRUE);
    }

  GTK_WIDGET_CLASS (eos_top_bar_parent_class)->get_preferred_height (widget,
                                                                     minimum,
                                                                     natural);
  if (minimum != NULL)
    *minimum = MAX (_EOS_TOP_BAR_HEIGHT_PX, *minimum);
  if (natural != NULL)
    *natural = MAX (_EOS_TOP_BAR_HEIGHT_PX, *natural);

  if (priv->left_top_bar_widget)
    {
      gtk_widget_set_visible (priv->left_top_bar_widget, left_widget_visible);
    }
  if (priv->center_top_bar_widget)
    {
      gtk_widget_set_visible (priv->center_top_bar_widget, center_widget_visible);
    }
}

/* Draw the edge finishing on the two lines inside the topbar; see
after_draw_cb() in eoswindow.c for the two lines outside the topbar */
static gboolean
eos_top_bar_draw (GtkWidget *self_widget,
                  cairo_t   *cr)
{
  GTK_WIDGET_CLASS (eos_top_bar_parent_class)->draw (self_widget, cr);

  gint width = gtk_widget_get_allocated_width (self_widget);
  gint height = gtk_widget_get_allocated_height (self_widget);
  cairo_set_line_width (cr, 1.0);
  /* Highlight: #ffffff, opacity 5% */
  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.05);
  cairo_move_to (cr, 0, height - 1.5);
  cairo_rel_line_to (cr, width, 0);
  cairo_stroke (cr);
  /* Baseline: #0a0a0a, opacity 100% */
  cairo_set_source_rgb (cr, 0.039, 0.039, 0.039);
  cairo_move_to (cr, 0, height - 0.5);
  cairo_rel_line_to (cr, width, 0);
  cairo_stroke (cr);

  return GDK_EVENT_PROPAGATE;
}

static void
on_credits_clicked (GtkButton *button,
                    EosTopBar *self)
{
  g_signal_emit (self, top_bar_signals[CREDITS_CLICKED], 0);
}

static void
eos_top_bar_class_init (EosTopBarClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->constructed = eos_top_bar_constructed;
  object_class->get_property = eos_top_bar_get_property;
  object_class->set_property = eos_top_bar_set_property;
  widget_class->get_preferred_height = eos_top_bar_get_preferred_height;
  widget_class->draw = eos_top_bar_draw;

  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/com/endlessm/sdk/widgets/topbar.ui");
  gtk_widget_class_bind_template_child_internal_private (widget_class,
                                                         EosTopBar,
                                                         center_top_bar_attach);
  gtk_widget_class_bind_template_child_internal_private (widget_class,
                                                         EosTopBar,
                                                         credits_button);
  gtk_widget_class_bind_template_callback (widget_class, on_credits_clicked);

  top_bar_signals[CREDITS_CLICKED] =
    g_signal_new ("credits-clicked", G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION, 0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  eos_top_bar_props[PROP_SHOW_CREDITS_BUTTON] =
    g_param_spec_boolean ("show-credits-button", "Show credits button",
                         "Whether the credits button is discoverable",
                         FALSE,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, NPROPS, eos_top_bar_props);
}

static void
eos_top_bar_init (EosTopBar *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
}

GtkWidget *
eos_top_bar_new (void)
{
  return GTK_WIDGET (g_object_new (EOS_TYPE_TOP_BAR, NULL));
}

/*
 * eos_top_bar_set_left_widget:
 * @self: the top bar
 * @left_top_bar_widget: the left top bar widget to be set
 * 
 * Sets the left widget in the top bar.
 */
void
eos_top_bar_set_left_widget (EosTopBar *self,
                             GtkWidget *left_top_bar_widget)
{
  g_return_if_fail (EOS_IS_TOP_BAR (self));
  g_return_if_fail (left_top_bar_widget == NULL || GTK_IS_WIDGET (left_top_bar_widget));

  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);

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

  if (priv->left_top_bar_widget)
    {
      gtk_container_remove (GTK_CONTAINER (self), priv->left_top_bar_widget);
    }

  priv->left_top_bar_widget = left_top_bar_widget;
  if (left_top_bar_widget)
    {
      gtk_widget_set_valign (priv->left_top_bar_widget, GTK_ALIGN_CENTER);
      gtk_header_bar_pack_start (GTK_HEADER_BAR (self),
                                 priv->left_top_bar_widget);
    }
}

/*
 * eos_top_bar_set_center_widget:
 * @self: the top bar
 * @center_top_bar_widget: the center top bar widget to be set
 * 
 * Sets the center widget in the top bar.
 */
void
eos_top_bar_set_center_widget (EosTopBar *self,
                               GtkWidget *center_top_bar_widget)
{
  g_return_if_fail (EOS_IS_TOP_BAR (self));
  g_return_if_fail (center_top_bar_widget == NULL || GTK_IS_WIDGET (center_top_bar_widget));

  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);

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

  if (priv->center_top_bar_widget)
    gtk_container_remove (GTK_CONTAINER (priv->center_top_bar_attach),
                          priv->center_top_bar_widget);

  priv->center_top_bar_widget = center_top_bar_widget;
  if (center_top_bar_widget)
    {
      gtk_container_add (GTK_CONTAINER (priv->center_top_bar_attach),
                         priv->center_top_bar_widget);
      gtk_widget_show (priv->center_top_bar_widget);
    }
}

/*
 * eos_top_bar_get_show_credits_button:
 * @self: the top bar
 *
 * See eos_top_bar_set_show_credits_button().
 *
 * Returns: %TRUE if credits button should be discoverable, %FALSE if not.
 */
gboolean
eos_top_bar_get_show_credits_button (EosTopBar *self)
{
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);
  return priv->show_credits_button;
}

/*
 * eos_top_bar_set_show_credits_button:
 * @self: the top bar
 * @show_credits_button: whether the credits button should be discoverable.
 *
 * Gets whether the credits button should be discoverable.
 * Note that the credits button is not visible as such, but when the mouse
 * hovers over it, it becomes visible if this is set to %TRUE.
 * If this is %FALSE, the button never becomes visible.
 */
void
eos_top_bar_set_show_credits_button (EosTopBar *self,
                                     gboolean   show_credits_button)
{
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);
  if (priv->show_credits_button == show_credits_button)
    return;

  priv->show_credits_button = show_credits_button;
  gtk_widget_set_visible (priv->credits_button, show_credits_button);
  g_object_notify_by_pspec (G_OBJECT (self),
                            eos_top_bar_props[PROP_SHOW_CREDITS_BUTTON]);
}