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
/*
 *  Copyright (C) 2003, 2004 Marco Pesenti Gritti
 *  Copyright (C) 2003, 2004 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 2, 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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "ev-navigation-action-widget.h"

#include <glib/gi18n.h>
#include <string.h>

static void ev_navigation_action_widget_toggled (CtkToggleToolButton *toggle);
static gboolean ev_navigation_action_widget_button_press_event (CtkWidget *widget,
        	        	        		        CdkEventButton    *event,
        	        	        		        gpointer data);

G_DEFINE_TYPE (EvNavigationActionWidget, ev_navigation_action_widget, CTK_TYPE_TOGGLE_TOOL_BUTTON)

enum
{
	SHOW_MENU,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static void
ev_navigation_action_widget_init (EvNavigationActionWidget *action_widget)
{
	CtkWidget *toggle_button;

	/* It's rather dirty hack but we need a child to connect to
	 * button press event
	 */

	toggle_button = ctk_bin_get_child (CTK_BIN (action_widget));

	g_signal_connect (toggle_button, "button-press-event",
			  G_CALLBACK (ev_navigation_action_widget_button_press_event),
		          action_widget);
	return;
}

static void
ev_navigation_action_widget_class_init (EvNavigationActionWidgetClass *klass)
{
	CtkToggleToolButtonClass *toggle_tool_button_class = CTK_TOGGLE_TOOL_BUTTON_CLASS (klass);

	toggle_tool_button_class->toggled = ev_navigation_action_widget_toggled;

	signals[SHOW_MENU] =
	          g_signal_new ("show-menu",
        		        G_OBJECT_CLASS_TYPE (klass),
                    		G_SIGNAL_RUN_FIRST,
                                G_STRUCT_OFFSET (EvNavigationActionWidgetClass, show_menu),
                                NULL, NULL,
                                g_cclosure_marshal_VOID__VOID,
                                G_TYPE_NONE, 0);
}

static int
menu_deactivate_cb (CtkMenuShell      *menu_shell,
		    EvNavigationActionWidget *widget)
{
         ctk_toggle_tool_button_set_active (CTK_TOGGLE_TOOL_BUTTON (widget), FALSE);
	 return TRUE;
}

static void
menu_detacher (CtkWidget *widget,
               CtkMenu   *menu)<--- Parameter 'menu' can be declared as pointer to const
{
	 EvNavigationActionWidget *button = EV_NAVIGATION_ACTION_WIDGET (widget);
         g_return_if_fail (button->menu == menu);
	 button->menu = NULL;
}

void
ev_navigation_action_widget_set_menu(EvNavigationActionWidget *button, CtkWidget *menu)
{

      if (button->menu == CTK_MENU (menu))
		return;

      if (button->menu && ctk_widget_get_visible (CTK_WIDGET (button->menu)))
	        ctk_menu_shell_deactivate (CTK_MENU_SHELL (button->menu));

      if (button->menu) {
    		g_signal_handlers_disconnect_by_func (button->menu,
						menu_deactivate_cb,
						button);
		ctk_menu_detach (button->menu);
       }

       button->menu = CTK_MENU (menu);

       if (button->menu) {
    		ctk_menu_attach_to_widget (button->menu, CTK_WIDGET (button),
            		                   menu_detacher);<--- You might need to cast the function pointer here
		g_signal_connect (button->menu, "deactivate",
				  G_CALLBACK (menu_deactivate_cb), button);
	}
}

static void
popup_menu_under_arrow (EvNavigationActionWidget *button,
                        CdkEventButton    *event)
{
	g_signal_emit (button, signals[SHOW_MENU], 0);

	if (!button->menu)
		return;

	ctk_menu_popup_at_pointer (button->menu,
	                           (const CdkEvent*) event);
}

static void
ev_navigation_action_widget_toggled (CtkToggleToolButton *toggle)
{
	EvNavigationActionWidget *button = EV_NAVIGATION_ACTION_WIDGET (toggle);
	if (!button->menu)
    		return;

	if (ctk_toggle_tool_button_get_active (CTK_TOGGLE_TOOL_BUTTON (button)) &&
	    !ctk_widget_get_visible (CTK_WIDGET (button->menu))) {
		      /* we get here only when the menu is activated by a key
		       * press, so that we can select the first menu item */
		      popup_menu_under_arrow (button, NULL);
		      ctk_menu_shell_select_first (CTK_MENU_SHELL (button->menu), FALSE);
        }
}

static gboolean
ev_navigation_action_widget_button_press_event (CtkWidget *widget,
                	            		CdkEventButton    *event,
                	            		gpointer data)
{
	EvNavigationActionWidget *button = EV_NAVIGATION_ACTION_WIDGET (data);

	if (event->button == 1) {
	         popup_menu_under_arrow (button, event);
	    	 ctk_toggle_tool_button_set_active (CTK_TOGGLE_TOOL_BUTTON (button), TRUE);
	    	 return TRUE;
	}
	return FALSE;
}