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
|
/*
* Copyright © 2017 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
* MERCHANMENUILITY 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 "terminal-menu-button.h"
#include "terminal-intl.h"
#include "terminal-libgsystem.h"
/* All this just because GtkToggleButton:toggled is RUN_FIRST (and the
* notify::active comes after the toggled signal). :-(
*/
enum
{
UPDATE_MENU,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
/* The menu button sets itself insensitive when it has no menu.
* Work around this by using an empty menu.
*/
static void
set_empty_menu (GtkMenuButton *button)
{
gs_unref_object GMenu *menu = g_menu_new ();
gtk_menu_button_set_menu_model (button, G_MENU_MODEL (menu));
}
/* Class implementation */
G_DEFINE_TYPE (TerminalMenuButton, terminal_menu_button, GTK_TYPE_MENU_BUTTON);
static void
terminal_menu_button_init (TerminalMenuButton *button_)
{
GtkButton *button = GTK_BUTTON (button_);
GtkMenuButton *menu_button = GTK_MENU_BUTTON (button_);
gtk_button_set_relief (button, GTK_RELIEF_NONE);
gtk_button_set_focus_on_click (button, FALSE);
gtk_menu_button_set_use_popover (menu_button, FALSE);
set_empty_menu (menu_button);
}
static void
terminal_menu_button_toggled (GtkToggleButton *button)
{
gboolean active = gtk_toggle_button_get_active (button); /* this is already the new state */
/* On activate, update the menu */
if (active)
g_signal_emit (button, signals[UPDATE_MENU], 0);
GTK_TOGGLE_BUTTON_CLASS (terminal_menu_button_parent_class)->toggled (button);
/* On deactivate, clear the menu */
if (!active)
set_empty_menu (GTK_MENU_BUTTON (button));
}
static void
terminal_menu_button_class_init (TerminalMenuButtonClass *klass)
{
GtkToggleButtonClass *toggle_button_class = GTK_TOGGLE_BUTTON_CLASS (klass);
toggle_button_class->toggled = terminal_menu_button_toggled;
signals[UPDATE_MENU] =
g_signal_new (I_("update-menu"),
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (TerminalMenuButtonClass, update_menu),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
}
/* public API */
/**
* terminal_menu_button_new:
*
* Returns: a new #TerminalMenuButton
*/
GtkWidget *
terminal_menu_button_new (void)
{
return g_object_new (TERMINAL_TYPE_MENU_BUTTON, NULL);
}
|