Could someone explain gtk2hs drag and drop to me,

2019-08-04 19:21发布

问题:

As the title says, I just don't get DND (or rather I understand the concept and I understand the order of callbacks, I just don't understand how to setup DND for actual usage.) I'd like to say that I've done DND stuff before in C, but considering I never really got that working...

So I'm trying (and mostly succeeding, save DND) to write a text editor (using gtksourceview, because it has built in code highlighting.) Reasons are below if you want them. Anyways, there's not really a good DND demo or tutorial available for gtk2hs (listDND.hs just doesn't translate well in my head.) So what I'm asking is for code that demonstrates simple DND on a window widget (for example.) Ideally, it should accept drops from other windows (such as Thunar) and print out the information in string form. I think I can take it from there...


Reasons: I'm running a fairly light weight setup, dwm and a few gtk+2 programs. I really don't want to have to pull in gtk+3 to get the current gedit from the repos (Arch Linux.) Currently, I'm using geany for all of my text editing needs, however, geany is a bit heavy for editing config files. Further, geany doesn't care for my terminal of choice (st;) so I don't even get the benefit of using it as an IDE. Meaning I'd like a lightweight text editor with syntax highlighting. I could configure emacs or vim or something, but that seems to me to be more of a hack then a proper solution. Thus my project was born. It's mostly working (aside from DND, all that's left is proper multi-tab support.) Admittedly, I could probably work this out if I wrote it in C, but there isn't that much state in a text editor so Haskell's been working fine with almost no need for mutable variables.

回答1:

Following the tutorial I linked and the gtk2hs documentation, I have written the following minimal application that receives text. I will add it to the gtk2hs demos directory shortly.

import Control.Monad.IO.Class
import Graphics.UI.Gtk

main = do
    initGUI
    w <- windowNew
    l <- labelNew $ Just "drag here lol"
    onDestroy w mainQuit
    containerAdd w l
    dragDestSet w [DestDefaultMotion, DestDefaultDrop] [ActionCopy]
    dragDestAddTextTargets w
    w `on` dragDataReceived $ \dc pos id ts -> do
        s <- selectionDataGetText
        liftIO . putStrLn $ case s of
            Nothing -> "didn't understand the drop"
            Just s  -> "understood, here it is: <" ++ s ++ ">"
    widgetShowAll w
    mainGUI

I have no idea whether thunar offers text as one of its selection formats, but if not, the formats it does offer are surely documented somewhere.