|
Packit Service |
3bdf47 |
/*
|
|
Packit Service |
3bdf47 |
* Copyright © 2017 Christian Persch
|
|
Packit Service |
3bdf47 |
*
|
|
Packit Service |
3bdf47 |
* This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
3bdf47 |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
3bdf47 |
* the Free Software Foundation, either version 3 of the License, or
|
|
Packit Service |
3bdf47 |
* (at your option) any later version.
|
|
Packit Service |
3bdf47 |
*
|
|
Packit Service |
3bdf47 |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
3bdf47 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
3bdf47 |
* MERCHANMENUILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
3bdf47 |
* GNU General Public License for more details.
|
|
Packit Service |
3bdf47 |
*
|
|
Packit Service |
3bdf47 |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
3bdf47 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
Packit Service |
3bdf47 |
*/
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
#include "config.h"
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
#include "terminal-menu-button.h"
|
|
Packit Service |
3bdf47 |
#include "terminal-intl.h"
|
|
Packit Service |
3bdf47 |
#include "terminal-libgsystem.h"
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
/* All this just because GtkToggleButton:toggled is RUN_FIRST (and the
|
|
Packit Service |
3bdf47 |
* notify::active comes after the toggled signal). :-(
|
|
Packit Service |
3bdf47 |
*/
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
enum
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
UPDATE_MENU,
|
|
Packit Service |
3bdf47 |
LAST_SIGNAL
|
|
Packit Service |
3bdf47 |
};
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static guint signals[LAST_SIGNAL];
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void popup_menu_selection_done_cb (GtkMenu *menu,
|
|
Packit Service |
3bdf47 |
GtkMenuButton *button);
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
/* The menu button sets itself insensitive when it has no menu.
|
|
Packit Service |
3bdf47 |
* Work around this by using an empty menu.
|
|
Packit Service |
3bdf47 |
*/
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
set_empty_menu (GtkMenuButton *button)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
gs_unref_object GMenu *menu = g_menu_new ();
|
|
Packit Service |
3bdf47 |
gtk_menu_button_set_menu_model (button, G_MENU_MODEL (menu));
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
disconnect_popup_menu (GtkMenuButton *button)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
GtkMenu *popup_menu = gtk_menu_button_get_popup (button);
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
if (popup_menu)
|
|
Packit Service |
3bdf47 |
g_signal_handlers_disconnect_by_func
|
|
Packit Service |
3bdf47 |
(popup_menu, G_CALLBACK (popup_menu_selection_done_cb), button);
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
popup_menu_selection_done_cb (GtkMenu *menu,
|
|
Packit Service |
3bdf47 |
GtkMenuButton *button)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
disconnect_popup_menu (button);
|
|
Packit Service |
3bdf47 |
set_empty_menu (button);
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
/* Class implementation */
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
G_DEFINE_TYPE (TerminalMenuButton, terminal_menu_button, GTK_TYPE_MENU_BUTTON);
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
terminal_menu_button_init (TerminalMenuButton *button_)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
GtkButton *button = GTK_BUTTON (button_);
|
|
Packit Service |
3bdf47 |
GtkMenuButton *menu_button = GTK_MENU_BUTTON (button_);
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
gtk_button_set_relief (button, GTK_RELIEF_NONE);
|
|
Packit Service |
3bdf47 |
gtk_button_set_focus_on_click (button, FALSE);
|
|
Packit Service |
3bdf47 |
gtk_menu_button_set_use_popover (menu_button, FALSE);
|
|
Packit Service |
3bdf47 |
set_empty_menu (menu_button);
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
terminal_menu_button_toggled (GtkToggleButton *button)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
gboolean active = gtk_toggle_button_get_active (button); /* this is already the new state */
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
/* On activate, update the menu */
|
|
Packit Service |
3bdf47 |
if (active)
|
|
Packit Service |
3bdf47 |
g_signal_emit (button, signals[UPDATE_MENU], 0);
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
GTK_TOGGLE_BUTTON_CLASS (terminal_menu_button_parent_class)->toggled (button);
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
terminal_menu_button_update_menu (TerminalMenuButton *button)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
GtkMenuButton *gtk_button = GTK_MENU_BUTTON (button);
|
|
Packit Service |
3bdf47 |
GtkMenu *popup_menu = gtk_menu_button_get_popup (gtk_button);
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
if (popup_menu)
|
|
Packit Service |
3bdf47 |
g_signal_connect (popup_menu, "selection-done",
|
|
Packit Service |
3bdf47 |
G_CALLBACK (popup_menu_selection_done_cb), button);
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
terminal_menu_button_dispose (GObject *object)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
disconnect_popup_menu (GTK_MENU_BUTTON (object));
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
G_OBJECT_CLASS (terminal_menu_button_parent_class)->dispose (object);
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
static void
|
|
Packit Service |
3bdf47 |
terminal_menu_button_class_init (TerminalMenuButtonClass *klass)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
klass->update_menu = terminal_menu_button_update_menu;
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
Packit Service |
3bdf47 |
object_class->dispose = terminal_menu_button_dispose;
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
|
|
Packit Service |
3bdf47 |
toggle_button_class->toggled = terminal_menu_button_toggled;
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
signals[UPDATE_MENU] =
|
|
Packit Service |
3bdf47 |
g_signal_new (I_("update-menu"),
|
|
Packit Service |
3bdf47 |
G_OBJECT_CLASS_TYPE (klass),
|
|
Packit Service |
3bdf47 |
G_SIGNAL_RUN_LAST,
|
|
Packit Service |
3bdf47 |
G_STRUCT_OFFSET (TerminalMenuButtonClass, update_menu),
|
|
Packit Service |
3bdf47 |
NULL, NULL,
|
|
Packit Service |
3bdf47 |
g_cclosure_marshal_VOID__VOID,
|
|
Packit Service |
3bdf47 |
G_TYPE_NONE,
|
|
Packit Service |
3bdf47 |
0);
|
|
Packit Service |
3bdf47 |
}
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
/* public API */
|
|
Packit Service |
3bdf47 |
|
|
Packit Service |
3bdf47 |
/**
|
|
Packit Service |
3bdf47 |
* terminal_menu_button_new:
|
|
Packit Service |
3bdf47 |
*
|
|
Packit Service |
3bdf47 |
* Returns: a new #TerminalMenuButton
|
|
Packit Service |
3bdf47 |
*/
|
|
Packit Service |
3bdf47 |
GtkWidget *
|
|
Packit Service |
3bdf47 |
terminal_menu_button_new (void)
|
|
Packit Service |
3bdf47 |
{
|
|
Packit Service |
3bdf47 |
return g_object_new (TERMINAL_TYPE_MENU_BUTTON, NULL);
|
|
Packit Service |
3bdf47 |
}
|