| 1 | /* |
| 2 | * Copyright (C) 2015 Igalia S.L. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "WebContextMenuItemGlib.h" |
| 28 | |
| 29 | #include "APIObject.h" |
| 30 | #include <gio/gio.h> |
| 31 | |
| 32 | #if PLATFORM(GTK) |
| 33 | #include <gtk/gtk.h> |
| 34 | #endif |
| 35 | |
| 36 | namespace WebKit { |
| 37 | using namespace WebCore; |
| 38 | |
| 39 | WebContextMenuItemGlib::(ContextMenuItemType type, ContextMenuAction action, const String& title, bool enabled, bool checked) |
| 40 | : WebContextMenuItemData(type, action, title, enabled, checked) |
| 41 | { |
| 42 | ASSERT(type != SubmenuType); |
| 43 | createActionIfNeeded(); |
| 44 | } |
| 45 | |
| 46 | WebContextMenuItemGlib::(const WebContextMenuItemData& data) |
| 47 | : WebContextMenuItemData(data.type() == SubmenuType ? ActionType : data.type(), data.action(), data.title(), data.enabled(), data.checked()) |
| 48 | { |
| 49 | createActionIfNeeded(); |
| 50 | } |
| 51 | |
| 52 | WebContextMenuItemGlib::(const WebContextMenuItemGlib& data, Vector<WebContextMenuItemGlib>&& ) |
| 53 | : WebContextMenuItemData(ActionType, data.action(), data.title(), data.enabled(), false) |
| 54 | { |
| 55 | m_gAction = data.gAction(); |
| 56 | m_submenuItems = WTFMove(submenu); |
| 57 | #if PLATFORM(GTK) |
| 58 | m_gtkAction = data.gtkAction(); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | static bool isGActionChecked(GAction* action) |
| 63 | { |
| 64 | if (!g_action_get_state_type(action)) |
| 65 | return false; |
| 66 | |
| 67 | ASSERT(g_variant_type_equal(g_action_get_state_type(action), G_VARIANT_TYPE_BOOLEAN)); |
| 68 | GRefPtr<GVariant> state = adoptGRef(g_action_get_state(action)); |
| 69 | return g_variant_get_boolean(state.get()); |
| 70 | } |
| 71 | |
| 72 | WebContextMenuItemGlib::(GAction* action, const String& title, GVariant* target) |
| 73 | : WebContextMenuItemData(g_action_get_state_type(action) ? CheckableActionType : ActionType, ContextMenuItemBaseApplicationTag, title, g_action_get_enabled(action), isGActionChecked(action)) |
| 74 | , m_gAction(action) |
| 75 | , m_gActionTarget(target) |
| 76 | { |
| 77 | createActionIfNeeded(); |
| 78 | } |
| 79 | |
| 80 | #if PLATFORM(GTK) |
| 81 | WebContextMenuItemGlib::(GtkAction* action) |
| 82 | : WebContextMenuItemData(GTK_IS_TOGGLE_ACTION(action) ? CheckableActionType : ActionType, ContextMenuItemBaseApplicationTag, String::fromUTF8(gtk_action_get_label(action)), gtk_action_get_sensitive(action), GTK_IS_TOGGLE_ACTION(action) ? gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action)) : false) |
| 83 | { |
| 84 | m_gtkAction = action; |
| 85 | createActionIfNeeded(); |
| 86 | g_object_set_data_full(G_OBJECT(m_gAction.get()), "webkit-gtk-action" , g_object_ref(m_gtkAction), g_object_unref); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | WebContextMenuItemGlib::() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | GUniquePtr<char> WebContextMenuItemGlib::() const |
| 95 | { |
| 96 | #if PLATFORM(GTK) |
| 97 | if (m_gtkAction) |
| 98 | return GUniquePtr<char>(g_strdup(gtk_action_get_name(m_gtkAction))); |
| 99 | #endif |
| 100 | |
| 101 | static uint64_t actionID = 0; |
| 102 | return GUniquePtr<char>(g_strdup_printf("action-%" PRIu64, ++actionID)); |
| 103 | } |
| 104 | |
| 105 | void WebContextMenuItemGlib::() |
| 106 | { |
| 107 | if (type() == SeparatorType) |
| 108 | return; |
| 109 | |
| 110 | if (!m_gAction) { |
| 111 | auto actionName = buildActionName(); |
| 112 | if (type() == CheckableActionType) |
| 113 | m_gAction = adoptGRef(G_ACTION(g_simple_action_new_stateful(actionName.get(), nullptr, g_variant_new_boolean(checked())))); |
| 114 | else |
| 115 | m_gAction = adoptGRef(G_ACTION(g_simple_action_new(actionName.get(), nullptr))); |
| 116 | g_simple_action_set_enabled(G_SIMPLE_ACTION(m_gAction.get()), enabled()); |
| 117 | } |
| 118 | |
| 119 | #if PLATFORM(GTK) |
| 120 | // Create the GtkAction for backwards compatibility only. |
| 121 | if (!m_gtkAction) { |
| 122 | if (type() == CheckableActionType) { |
| 123 | m_gtkAction = GTK_ACTION(gtk_toggle_action_new(g_action_get_name(m_gAction.get()), title().utf8().data(), nullptr, nullptr)); |
| 124 | gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(m_gtkAction), checked()); |
| 125 | } else |
| 126 | m_gtkAction = gtk_action_new(g_action_get_name(m_gAction.get()), title().utf8().data(), 0, nullptr); |
| 127 | gtk_action_set_sensitive(m_gtkAction, enabled()); |
| 128 | g_object_set_data_full(G_OBJECT(m_gAction.get()), "webkit-gtk-action" , m_gtkAction, g_object_unref); |
| 129 | } |
| 130 | |
| 131 | g_signal_connect_object(m_gAction.get(), "activate" , G_CALLBACK(gtk_action_activate), m_gtkAction, G_CONNECT_SWAPPED); |
| 132 | #endif |
| 133 | } |
| 134 | |
| 135 | } // namespace WebKit |
| 136 | |