summaryrefslogtreecommitdiff
path: root/endless/eosactionmenu.c
blob: 4a0c5e83006a5b0a17e1b7f942163f73e20eb60a (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
/* Copyright 2013 Endless Mobile, Inc. */

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

#include "eosactionbutton.h"
#include <glib-object.h>
#include <gtk/gtk.h>
#include <math.h>

#define _EOS_STYLE_CLASS_ACTION_MENU "action-menu"

/*
 * SECTION:action-menu
 * @short_description: Adding actions to the page
 * @title: Action Menu
 */


G_DEFINE_TYPE (EosActionMenu, eos_action_menu, GTK_TYPE_FRAME)

#define EOS_ACTION_MENU_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EOS_TYPE_ACTION_MENU, EosActionMenuPrivate))

struct _EosActionMenuPrivate
{
  GtkWidget *grid;
  GtkActionGroup *action_group;
};

static void
eos_action_menu_dispose (GObject *object);

static void
eos_action_menu_finalize (GObject *object);

/* ******* INIT ******* */

static void
eos_action_menu_class_init (EosActionMenuClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (EosActionMenuPrivate));

  object_class->dispose = eos_action_menu_dispose;
  object_class->finalize = eos_action_menu_finalize;
}

static void
eos_action_menu_init (EosActionMenu *self)
{
  EosActionMenuPrivate *priv;
  GtkStyleContext *context;

  self->priv = EOS_ACTION_MENU_PRIVATE (self);
  priv = self->priv;

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

  priv->grid = gtk_grid_new ();
  g_object_set (G_OBJECT (priv->grid),
                "hexpand", TRUE,
                "hexpand", TRUE,
                "halign", GTK_ALIGN_CENTER,
                "valign", GTK_ALIGN_CENTER,
                NULL);
  gtk_container_add (GTK_CONTAINER (self),
                     GTK_WIDGET (priv->grid));

  // TODO : name?
  priv->action_group = gtk_action_group_new ("EosActionMenu");

  gtk_widget_set_hexpand (GTK_WIDGET (self), TRUE);
  gtk_widget_set_vexpand (GTK_WIDGET (self), TRUE);
}

/* ******* LIFECYCLE ******* */

/*
 * eos_action_menu_new:
 *
 * Returns: a new instance
 */
GtkWidget *
eos_action_menu_new ()
{
  return g_object_new (EOS_TYPE_ACTION_MENU, NULL);
}

static void
eos_action_menu_dispose (GObject *object)
{
  G_OBJECT_CLASS (eos_action_menu_parent_class)->dispose (object);
}

static void
eos_action_menu_finalize (GObject *object)
{
  G_OBJECT_CLASS (eos_action_menu_parent_class)->finalize (object);
}

/* ******* ACTION GROUP MGMT ******* */

/*
 * eos_action_menu_add_action:
 * @menu: a #EosActionMenu
 * @action: a #GtkAction: name, label, icon-name, is-important.
 *
 * Adds an action to the #EosActionMenu, using its name, label, icon-name and
 * is-important properties.
 */
void
eos_action_menu_add_action (EosActionMenu *menu,
                            GtkAction *action)
{
  EosActionMenuPrivate *priv;

  g_return_if_fail (EOS_IS_ACTION_MENU (menu));
  priv = menu->priv;

  if (action)
    {
      gtk_action_group_add_action (priv->action_group, action);

      EosActionButtonSize size = gtk_action_get_is_important (action) ?
                                             EOS_ACTION_BUTTON_SIZE_PRIMARY :
                                             EOS_ACTION_BUTTON_SIZE_SECONDARY;

      GtkWidget *action_button = eos_action_button_new (size,
                                                        gtk_action_get_label (action),
                                                        gtk_action_get_icon_name (action));

      gtk_activatable_set_related_action (GTK_ACTIVATABLE (action_button), action);

      // TODO : maybe we need a finer control, taking is-important into account?
      gtk_grid_attach_next_to (GTK_GRID (priv->grid), action_button, NULL,
                               GTK_POS_BOTTOM, 1, 1);
    }
}

/*
 * eos_action_menu_get_action:
 * @menu: an #EosActionMenu
 * @name: the name of the action to retrieve
 *
 * Retrieves an action.
 *
 * Returns: (transfer none): the #GtkAction
 */
GtkAction *
eos_action_menu_get_action (EosActionMenu *menu,
                            const gchar *name)
{
  EosActionMenuPrivate *priv;
  g_return_val_if_fail (EOS_IS_ACTION_MENU (menu), NULL);
  priv = menu->priv;

  return gtk_action_group_get_action (priv->action_group, name);
}

/*
 * eos_action_menu_list_actions:
 * @menu: an #EosActionMenu
 *
 * Returns: (element-type GList) (transfer container): an allocated list of the action objects in the action group
 */
GList *
eos_action_menu_list_actions (EosActionMenu *menu)
{
  EosActionMenuPrivate *priv;
  g_return_val_if_fail (EOS_IS_ACTION_MENU (menu), NULL);
  priv = menu->priv;

  return gtk_action_group_list_actions (priv->action_group);
}

/*
 * eos_action_menu_remove_action:
 * @menu: an #EosActionMenu
 * @action: the action to remove
 *
 * Removes an action
 */
void
eos_action_menu_remove_action (EosActionMenu *menu,
                               GtkAction *action)
{
  EosActionMenuPrivate *priv;
  g_return_if_fail (EOS_IS_ACTION_MENU (menu));
  priv = menu->priv;

  gtk_action_group_remove_action(priv->action_group, action);
}

/*
 * eos_action_menu_remove_action_by_name:
 * @menu: an #EosActionMenu
 * @name: the name of the action to remove
 *
 * Removes the action with the given name
 */
void
eos_action_menu_remove_action_by_name (EosActionMenu *menu,
                                       const gchar *name)
{
  GtkAction *action;
  EosActionMenuPrivate *priv;

  g_return_if_fail (EOS_IS_ACTION_MENU (menu));
  priv = menu->priv;

  action = gtk_action_group_get_action (priv->action_group, name);
  if (action)
    {
      eos_action_menu_remove_action (menu, action);
    }
}

/* ******* LAYOUT AND VISUALS ******* */