summaryrefslogtreecommitdiff
path: root/endless/eostopbar.c
blob: 6975e392f9368af9d34d9b8316b9f4132f13af90 (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
/* 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 three different areas that can be managed through this 
 * class: a left widget, center widget, and action buttons area.
 * 
 * The action buttons area contain "minimize" and "close" buttons.
 */
#define _EOS_STYLE_CLASS_TOP_BAR "top-bar"
#define _EOS_TOP_BAR_HEIGHT_PX 36
#define _EOS_TOP_BAR_BUTTON_PADDING_PX 4
#define _EOS_TOP_BAR_ICON_SIZE_PX 16
#define _EOS_TOP_BAR_HORIZONTAL_BUTTON_MARGIN_PX 7
#define _EOS_TOP_BAR_BUTTON_SEPARATION_PX 8
#define _EOS_TOP_BAR_VERTICAL_BUTTON_MARGIN_PX 6
#define _EOS_TOP_BAR_MINIMIZE_ICON_NAME "window-minimize-symbolic"
#define _EOS_TOP_BAR_CLOSE_ICON_NAME "window-close-symbolic"

typedef struct {
  GtkWidget *actions_grid;
  GtkWidget *left_top_bar_attach;
  GtkWidget *center_top_bar_attach;

  GtkWidget *left_top_bar_widget;
  GtkWidget *center_top_bar_widget;

  GtkWidget *minimize_button;
  GtkWidget *minimize_icon;
  GtkWidget *close_button;
  GtkWidget *close_icon;
} EosTopBarPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (EosTopBar, eos_top_bar, GTK_TYPE_EVENT_BOX)

enum {
  CLOSE_CLICKED,
  MINIMIZE_CLICKED,
  LAST_SIGNAL
};

static guint top_bar_signals[LAST_SIGNAL] = { 0 };

static void
eos_top_bar_get_preferred_height (GtkWidget *widget,
                                  int *minimum,
                                  int *natural)
{
  if (minimum != NULL)
    *minimum = _EOS_TOP_BAR_HEIGHT_PX;
  if (natural != NULL)
    *natural = _EOS_TOP_BAR_HEIGHT_PX;
}

/* 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);
  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, _EOS_TOP_BAR_HEIGHT_PX - 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, _EOS_TOP_BAR_HEIGHT_PX - 0.5);
  cairo_rel_line_to (cr, width, 0);
  cairo_stroke (cr);

  return GDK_EVENT_PROPAGATE;
}

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

  widget_class->get_preferred_height = eos_top_bar_get_preferred_height;
  widget_class->draw = eos_top_bar_draw;

  /*
   * Emitted when the minimize button has been activated.
   */
  top_bar_signals[MINIMIZE_CLICKED] =
      g_signal_new ("minimize-clicked",
                    G_OBJECT_CLASS_TYPE (object_class),
                    G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                    0,
                    NULL, NULL, NULL,
                    G_TYPE_NONE, 0);

  /*
   * Emitted when the close button has been activated.
   */
  top_bar_signals[CLOSE_CLICKED] =
      g_signal_new ("close-clicked",
                    G_OBJECT_CLASS_TYPE (object_class),
                    G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
                    0,
                    NULL, NULL, NULL,
                    G_TYPE_NONE, 0);
}

static void
on_minimize_clicked_cb (GtkButton *button,
                        gpointer user_data)
{
  EosTopBar *self = EOS_TOP_BAR (user_data);
  g_signal_emit (self, top_bar_signals[MINIMIZE_CLICKED], 0);
}

static void
on_close_clicked_cb (GtkButton *button,
                     gpointer user_data)
{
  EosTopBar *self = EOS_TOP_BAR (user_data);
  g_signal_emit (self, top_bar_signals[CLOSE_CLICKED], 0);
}

static void
eos_top_bar_init (EosTopBar *self)
{
  GtkStyleContext *context;
  EosTopBarPrivate *priv = eos_top_bar_get_instance_private (self);

  context = gtk_widget_get_style_context (GTK_WIDGET (self));
  gtk_style_context_add_class (context, _EOS_STYLE_CLASS_TOP_BAR);

  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);

  priv->actions_grid =
    g_object_new (GTK_TYPE_GRID,
                  "orientation", GTK_ORIENTATION_HORIZONTAL,
                  "hexpand", TRUE,
                  "halign", GTK_ALIGN_FILL,
                  "column-spacing", _EOS_TOP_BAR_BUTTON_SEPARATION_PX,
                  "margin-top", _EOS_TOP_BAR_VERTICAL_BUTTON_MARGIN_PX,
                  "margin-bottom", _EOS_TOP_BAR_VERTICAL_BUTTON_MARGIN_PX,
                  "margin-left", _EOS_TOP_BAR_HORIZONTAL_BUTTON_MARGIN_PX,
                  "margin-right", _EOS_TOP_BAR_HORIZONTAL_BUTTON_MARGIN_PX,
                  NULL);

  priv->left_top_bar_attach = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
  priv->center_top_bar_attach = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
  gtk_widget_set_hexpand (priv->center_top_bar_attach, TRUE);
  gtk_widget_set_halign (priv->center_top_bar_attach, GTK_ALIGN_CENTER);

  /* TODO implement adding actions and widgets to the actions_grid */

  priv->minimize_button =
    g_object_new (GTK_TYPE_BUTTON,
                  "halign", GTK_ALIGN_END,
                  "valign", GTK_ALIGN_CENTER,
                  NULL);
  priv->minimize_icon =
    gtk_image_new_from_icon_name (_EOS_TOP_BAR_MINIMIZE_ICON_NAME,
                                  GTK_ICON_SIZE_SMALL_TOOLBAR);
  g_object_set(priv->minimize_icon,
               "pixel-size", _EOS_TOP_BAR_ICON_SIZE_PX,
               "margin", _EOS_TOP_BAR_BUTTON_PADDING_PX,
               NULL);
  gtk_container_add (GTK_CONTAINER (priv->minimize_button),
                     priv->minimize_icon);

  priv->close_button =
    g_object_new (GTK_TYPE_BUTTON,
                  "halign", GTK_ALIGN_END,
                  "valign", GTK_ALIGN_CENTER,
                  NULL);
  priv->close_icon =
      gtk_image_new_from_icon_name (_EOS_TOP_BAR_CLOSE_ICON_NAME,
                                    GTK_ICON_SIZE_SMALL_TOOLBAR);
  g_object_set(priv->close_icon,
               "pixel-size", _EOS_TOP_BAR_ICON_SIZE_PX,
               "margin", _EOS_TOP_BAR_BUTTON_PADDING_PX,
               NULL);
  gtk_container_add (GTK_CONTAINER (priv->close_button),
                     priv->close_icon);

  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->left_top_bar_attach);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->center_top_bar_attach);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->minimize_button);
  gtk_container_add (GTK_CONTAINER (priv->actions_grid),
                     priv->close_button);

  gtk_container_add (GTK_CONTAINER (self), priv->actions_grid);

  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
  gtk_widget_set_halign (GTK_WIDGET (self), GTK_ALIGN_FILL);

  g_signal_connect (priv->minimize_button, "clicked",
                    G_CALLBACK (on_minimize_clicked_cb), self);
  g_signal_connect (priv->close_button, "clicked",
                    G_CALLBACK (on_close_clicked_cb), 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 (priv->left_top_bar_attach),
                       priv->left_top_bar_widget);

  priv->left_top_bar_widget = left_top_bar_widget;
  if (left_top_bar_widget)
    {
      gtk_container_add (GTK_CONTAINER (priv->left_top_bar_attach),
                         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);
    }
}