Home | History | Annotate | Download | only in WebCoreSupport
      1 /*
      2  *  This library is free software; you can redistribute it and/or
      3  *  modify it under the terms of the GNU Lesser General Public
      4  *  License as published by the Free Software Foundation; either
      5  *  version 2 of the License, or (at your option) any later version.
      6  *
      7  *  This library is distributed in the hope that it will be useful,
      8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     10  *  Lesser General Public License for more details.
     11  *
     12  *  You should have received a copy of the GNU Lesser General Public
     13  *  License along with this library; if not, write to the Free Software
     14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     15  */
     16 
     17 #include "config.h"
     18 #include "DragClientGtk.h"
     19 
     20 #include "Document.h"
     21 #include "Element.h"
     22 #include "Frame.h"
     23 #include "NotImplemented.h"
     24 #include "RenderObject.h"
     25 #include "webkitwebview.h"
     26 
     27 #include <gtk/gtk.h>
     28 #if !GTK_CHECK_VERSION(2, 14, 0)
     29 #define gtk_widget_get_window(widget) (widget)->window
     30 #endif
     31 
     32 using namespace WebCore;
     33 
     34 namespace WebKit {
     35 
     36 DragClient::DragClient(WebKitWebView* webView)
     37     : m_webView(webView)
     38     , m_startPos(0, 0)
     39 {
     40 }
     41 
     42 void DragClient::willPerformDragDestinationAction(DragDestinationAction, DragData*)
     43 {
     44     notImplemented();
     45 }
     46 
     47 void DragClient::willPerformDragSourceAction(DragSourceAction, const IntPoint& startPos, Clipboard*)
     48 {
     49     m_startPos = startPos;
     50 }
     51 
     52 DragDestinationAction DragClient::actionMaskForDrag(DragData*)
     53 {
     54     notImplemented();
     55     return DragDestinationActionAny;
     56 }
     57 
     58 DragSourceAction DragClient::dragSourceActionMaskForPoint(const IntPoint&)
     59 {
     60     notImplemented();
     61     return DragSourceActionAny;
     62 }
     63 
     64 void DragClient::startDrag(DragImageRef image, const IntPoint& dragImageOrigin, const IntPoint& eventPos, Clipboard*, Frame* frame, bool linkDrag)
     65 {
     66     Element* targetElement = frame->document()->elementFromPoint(m_startPos.x(), m_startPos.y());
     67     bool imageDrag = false;
     68 
     69     if (targetElement)
     70         imageDrag = targetElement->renderer()->isImage();
     71 
     72     GdkAtom textHtml = gdk_atom_intern_static_string("text/html");
     73     GdkAtom netscapeUrl = gdk_atom_intern_static_string("_NETSCAPE_URL");
     74 
     75     GtkTargetList* targetList = gtk_target_list_new(NULL, 0);
     76     gtk_target_list_add(targetList, textHtml, 0, WEBKIT_WEB_VIEW_TARGET_INFO_HTML);
     77     gtk_target_list_add_text_targets(targetList, WEBKIT_WEB_VIEW_TARGET_INFO_TEXT);
     78 
     79     if (linkDrag || imageDrag) {
     80         gtk_target_list_add(targetList, netscapeUrl, 0, WEBKIT_WEB_VIEW_TARGET_INFO_NETSCAPE_URL);
     81         gtk_target_list_add_uri_targets(targetList, WEBKIT_WEB_VIEW_TARGET_INFO_URI_LIST);
     82     }
     83 
     84     if (imageDrag)
     85         gtk_target_list_add_image_targets(targetList, WEBKIT_WEB_VIEW_TARGET_INFO_IMAGE, false);
     86 
     87     GdkDragAction dragAction = GDK_ACTION_COPY;
     88     if (linkDrag) {
     89         dragAction = GDK_ACTION_LINK;
     90         if (imageDrag)
     91             dragAction = (GdkDragAction)(dragAction | GDK_ACTION_COPY);
     92     }
     93 
     94     GdkEvent* event = gdk_event_new(GDK_BUTTON_PRESS);
     95     reinterpret_cast<GdkEventButton*>(event)->window = gtk_widget_get_window(GTK_WIDGET(m_webView));
     96     reinterpret_cast<GdkEventButton*>(event)->time = GDK_CURRENT_TIME;
     97 
     98     GdkDragContext* context = gtk_drag_begin(GTK_WIDGET(m_webView),
     99                                              targetList, dragAction, 1, event);
    100     g_object_ref(context);
    101 
    102     if (image)
    103         gtk_drag_set_icon_pixbuf(context, image, eventPos.x() - dragImageOrigin.x(), eventPos.y() - dragImageOrigin.y());
    104     else
    105         gtk_drag_set_icon_default(context);
    106 
    107     gtk_target_list_unref(targetList);
    108 }
    109 
    110 DragImageRef DragClient::createDragImageForLink(KURL&, const String&, Frame*)
    111 {
    112     notImplemented();
    113     return 0;
    114 }
    115 
    116 void DragClient::dragControllerDestroyed()
    117 {
    118     delete this;
    119 }
    120 }
    121