summaryrefslogtreecommitdiffstats
path: root/src/terminal-headerbar.cc
blob: 38bae5ae4b214ce1dc449d2dad4c1e6b21051219 (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
/*
 * Copyright © 2018 Christian Persch
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib/gi18n.h>

#include "terminal-headerbar.hh"
#include "terminal-app.hh"
#include "terminal-libgsystem.hh"

typedef struct _TerminalHeaderbarPrivate TerminalHeaderbarPrivate;

struct _TerminalHeaderbar
{
  GtkHeaderBar parent_instance;
};

struct _TerminalHeaderbarClass
{
  GtkHeaderBarClass parent_class;
};

struct _TerminalHeaderbarPrivate
{
  GtkWidget *profilebutton;
  GtkWidget *menubutton;
};

enum {
  PROP_0,
  LAST_PROP
};

enum {
  LAST_SIGNAL
};

/* static guint signals[LAST_SIGNAL]; */
/* static GParamSpec *pspecs[LAST_PROP]; */

G_DEFINE_TYPE_WITH_PRIVATE (TerminalHeaderbar, terminal_headerbar, GTK_TYPE_HEADER_BAR)

#define PRIV(obj) ((TerminalHeaderbarPrivate *) terminal_headerbar_get_instance_private ((TerminalHeaderbar *)(obj)))

static void
profilemenu_items_changed_cb (GMenuModel *menu,
                              int position G_GNUC_UNUSED,
                              int removed G_GNUC_UNUSED,
                              int added G_GNUC_UNUSED,
                              TerminalHeaderbarPrivate *priv)
{
  if (g_menu_model_get_n_items (menu) > 0)
    gtk_widget_show (priv->profilebutton);
  else
    gtk_widget_hide (priv->profilebutton);
}

/* Class implementation */

static void
terminal_headerbar_init (TerminalHeaderbar *headerbar)
{

  TerminalHeaderbarPrivate *priv = PRIV (headerbar);
  GtkWidget *widget = GTK_WIDGET (headerbar);
  TerminalApp *app = terminal_app_get ();
  GMenuModel *profilemenu;

  gtk_widget_init_template (widget);

  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (priv->menubutton),
                                  terminal_app_get_headermenu (app));

  profilemenu = terminal_app_get_profilemenu (app);
  gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (priv->profilebutton),
                                  profilemenu);

  g_signal_connect (profilemenu, "items-changed",
                    G_CALLBACK (profilemenu_items_changed_cb), priv);
  profilemenu_items_changed_cb (profilemenu, 0, 0, 0, priv);
}

static void
terminal_headerbar_dispose (GObject *object)
{
  TerminalHeaderbar *headerbar = TERMINAL_HEADERBAR (object);
  TerminalHeaderbarPrivate *priv = PRIV (headerbar);
  TerminalApp *app = terminal_app_get ();

  GMenuModel *profilemenu = terminal_app_get_profilemenu (app);
  g_signal_handlers_disconnect_by_func (profilemenu,
                                        (void*)profilemenu_items_changed_cb,
                                        priv);

  G_OBJECT_CLASS (terminal_headerbar_parent_class)->dispose (object);
}

static void
terminal_headerbar_get_property (GObject *object,
                                 guint prop_id,
                                 GValue *value,
                                 GParamSpec *pspec)
{
  //  TerminalHeaderbar *headerbar = TERMINAL_HEADERBAR (object);

  switch (prop_id) {
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    break;
  }
}

static void
terminal_headerbar_set_property (GObject *object,
                                 guint prop_id,
                                 const GValue *value,
                                 GParamSpec *pspec)
{
  switch (prop_id) {
  default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
terminal_headerbar_class_init (TerminalHeaderbarClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gobject_class->dispose = terminal_headerbar_dispose;
  gobject_class->get_property = terminal_headerbar_get_property;
  gobject_class->set_property = terminal_headerbar_set_property;

  /* g_object_class_install_properties (gobject_class, G_N_ELEMENTS (pspecs), pspecs); */

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/terminal/ui/headerbar.ui");
  gtk_widget_class_bind_template_child_private (widget_class, TerminalHeaderbar, menubutton);
  gtk_widget_class_bind_template_child_private (widget_class, TerminalHeaderbar, profilebutton);
}

/* public API */

/**
 * terminal_headerbar_new:
 *
 * Returns: a new #TerminalHeaderbar
 */
GtkWidget *
terminal_headerbar_new (void)
{
  return reinterpret_cast<GtkWidget*>
    (g_object_new (TERMINAL_TYPE_HEADERBAR, nullptr));
}