Getting the html link when dropping an image from a website

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Getting the html link when dropping an image from a webs

Post by Shardik »

This is a cross-platform example for Linux, MacOS and Windows which I tested successfully on
  1. - Lubuntu 16.04 x86 with LXDE using PB 5.43 x86 with both GTK 2 and GTK 3 and in ASCII and Unicode mode
    - Linux Mint 18 x86 "Sarah" with Cinnamon using PB 5.43 x86 with both GTK 2 and GTK 3 and in ASCII and Unicode mode
    - Windows XP SP3 Professional with PB 5.43 x86 in ASCII and Unicode mode
    - Windows 8.1 x64 with PB 5.43 x86 and x64 in both ASCII and Unicode mode

Code: Select all

EnableExplicit

Define ImagePath.S

OpenWindow(0, 100, 100, 800, 600,
  "Drag the PureBasic logo onto the right ImageGadget")
WebGadget(0, 10, 10, 380, 580 ,"http://www.purebasic.com")
ImageGadget(1, 400, 10, 390, 290, 0, #PB_Image_Border)
EditorGadget(2, 400, 300, 390, 290)

EnableGadgetDrop(1, #PB_Drop_Image, #PB_Drag_Copy)

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux ; -------------------------------------------------
    ProcedureC DragDataCallback(*Widget.GtkWidget,
      *DragContext.GdkDragContext, x.I, y.I, *Data.GtkSelectionData, Info.I,
      Time.i, *UserData)
      Shared ImagePath.S

      If *Data
        If ImagePath = ""
          ImagePath = PeekS(gtk_selection_data_get_text_(*Data), -1, #PB_UTF8)
        EndIf
      EndIf
    EndProcedure

    g_signal_connect_(GadgetID(0), "drag-data-received", @DragDataCallback(), 0)
  CompilerCase #PB_OS_MacOS ; -------------------------------------------------
    Define SubclassedImageGadget.I

    Procedure SubclassGadget(GadgetID.I, NewClassName.S)
      Protected GadgetClass.I = CocoaMessage(0, GadgetID(GadgetID), "class")
      Protected NewGadgetClass.I

      NewGadgetClass = objc_allocateClassPair_(GadgetClass, NewClassName, 0)
      objc_registerClassPair_(NewGadgetClass)
      object_setClass_(GadgetID(GadgetID), NewGadgetClass)

      ProcedureReturn NewGadgetClass
    EndProcedure
    
    ProcedureC PrepareForDragOperation(Object.I, Selector.I, Sender.I)
      Shared ImagePath.S

      Protected Pasteboard.I
      Protected URL.I
      
      Pasteboard = CocoaMessage(0, Sender, "draggingPasteboard")
      URL = CocoaMessage(0, 0, "NSURL URLFromPasteboard:", Pasteboard)

      If URL
        ImagePath = PeekS(CocoaMessage(0,
          CocoaMessage(0, URL, "absoluteString"),
          "UTF8String"), -1, #PB_UTF8)
      EndIf

      ProcedureReturn #True
    EndProcedure

    SubclassedImageGadget = SubclassGadget(1, "SubclassedImageGadget")
    class_addMethod_(SubclassedImageGadget,
      sel_registerName_("prepareForDragOperation:"),
      @PrepareForDragOperation(), "v@:@")
  CompilerCase #PB_OS_Windows ; -----------------------------------------------
    Prototype AccessibleObjectFromPoint(Point.Q, *IAccessable, *VarChild)

    Define AccessibleObject.AccessibleObjectFromPoint
    Define CursorPos.POINT
    Define OLELib.I
    
    OLELib = OpenLibrary(#PB_Any,"OLEAcc.DLL")

    If OLELib
      AccessibleObject = GetFunction(OLELib, "AccessibleObjectFromPoint")
    Else
      MessageRequester("Error", "Loading of library OLEAcc.DLL failed!",
        #MB_ICONERROR)
      End
    EndIf
    
    Procedure ObjectFromPoint(x, y)
      Shared AccessibleObject.AccessibleObjectFromPoint
      Shared ImagePath.S

      Protected Name.I
      Protected *IAcc.IAccessible
      Protected Length.I
      Protected Path.S
      Protected VT.VARIANT

      If AccessibleObject(y << 32 | x, @*IAcc, @VT) = #S_OK
        If *IAcc\get_accValue(VT, @Name) = #S_OK
          Length = SysStringLen_(Name)
          Path = Space(Length)
          WideCharToMultiByte_(#CP_ACP, 0, Name, -1, @Path, Length, 0, 0)
          ImagePath = PeekS(@Path, Length, #PB_UTF8)
          SysFreeString_(Name)
        EndIf

        *IAcc\Release()
      EndIf

      ProcedureReturn #True
    EndProcedure
  CompilerEndSelect ; ---------------------------------------------------------

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      Case #WM_LBUTTONDOWN
        GetCursorPos_(@CursorPos.POINT)
        ObjectFromPoint(CursorPos\x, CursorPos\y)
    CompilerEndIf
    Case #PB_Event_GadgetDrop
      Select EventGadget()
        Case 1
          If EventDropImage(0)
            SetGadgetState(1, ImageID(0))
            AddGadgetItem(2, -1, ImagePath)
            ImagePath = ""
          EndIf
      EndSelect
  EndSelect
ForEver

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  CloseLibrary(OLELib)
CompilerEndIf
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Getting the html link when dropping an image from a webs

Post by Andre »

Thanks a lot, guys! :mrgreen:

Both your latest examples work here on Windows x64.

Now I have a final wish: while embedding the WebGadget in the application is one site, it's also possible to get the html link of the image when dropping it from the website displayed in an external WebBrowser (e.g. FireFox)?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Getting the html link when dropping an image from a webs

Post by RASHAD »

Hi Andre
It seems that we spent a lot of time while it is so simple :)
Cross platform PB x86,x64 Firefox v48
I just used PB WebGadget to display any image format no need to image decoder

Code: Select all

LoadFont(0,"Georgia",12)
If OpenWindow(0,0,0,800,600,"Drag with image!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
  WebGadget(0,10,10,450,580,"")
  EditorGadget(1,470,10,320,580)
  SetGadgetFont(1,FontID(0))
  AddGadgetItem(1,-1,"Drop here V :"+#CRLF$)
  
  EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)
  Repeat 
    eventID = WaitWindowEvent() 
    Select eventID 
      Case #PB_Event_GadgetDrop 
        If EventGadget() = 1
          AddGadgetItem(1,-1,EventDropText())
          SetGadgetText(0, EventDropText())
        EndIf

  EndSelect 
  Until eventID = #PB_Event_CloseWindow 
EndIf
For your first request :P
Fred and freak now are laughing hard :wink:
No problem

Code: Select all

LoadFont(0,"Georgia",12)
If OpenWindow(0,0,0,800,600,"Drag with image!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
  WebGadget(0,10,10,300,580,"http://www.purebasic.com")
  EditorGadget(1,320,10,300,580)
  SetGadgetFont(1,FontID(0))
  AddGadgetItem(1,-1,"Drop here V :"+#CRLF$)
  
  WebGadget(2,620,20,160,580,"")
  
  EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy) 
  Repeat 
    eventID = WaitWindowEvent() 
    Select eventID 
      Case #PB_Event_GadgetDrop 
        If EventGadget() = 1
          AddGadgetItem(1,-1,EventDropText())
          SetGadgetText(2, EventDropText())
        EndIf

  EndSelect 
  Until eventID = #PB_Event_CloseWindow 
EndIf
Egypt my love
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Getting the html link when dropping an image from a webs

Post by Marc56us »

Great! and simple :o I like! 8)

Juste need a way in sample 1 to avoid auto open filerequester "save/open image as/with" when drop :?:
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Getting the html link when dropping an image from a webs

Post by Andre »

Wow, RASHAD! :lol:

I would have neither thought, that using #PB_Drop_Text and EventDropText() can be used for handling the dragging of images from a website! :?
That's definitively something, which should be stated in the PB docs I thinks. Else anyone would think, that using #PB_Drop_Image and EventDropImage() is the way to go (which is probably the right thing, when doing drag & drop of images between gadgets inside of the same application). Or?

Anyway it's not so simple as I thought... :?
When I tried to implement your 'simple' solution in my application (which is also able to handle dropped images from harddisk, using #PB_Drop_Files and EventDropFiles()) all dragged images from a website will also be handled as files instead of text. Resulting in returning the filepath in the tempory internet folder for the dragged image from a website (e.g. "C:\Users\Andre\AppData\Local\Microsoft\Windows\INetCache\IE\PZYEYX5E\logopb[1].gif", instead of giving its original html address (e.g. "http://www.purebasic.com/images/logopb.gif").

Your extended example should illustrate this. The part for handling 'text' will neither be executed, so EventDropFiles() is always returning a path from disk.

Code: Select all

LoadFont(0, "Georgia", 12)
If OpenWindow(0, 0, 0, 800, 600, "Drag with image!", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  WebGadget(0, 10, 10, 300, 580, "http://www.purebasic.com")
  EditorGadget(1, 320, 10, 300, 580)
  SetGadgetFont(1, FontID(0))
  AddGadgetItem(1, -1, "Drop here V :"+#CRLF$)
  
  WebGadget(2, 620, 20, 160, 580, "")
  
  EnableGadgetDrop(1, #PB_Drop_Text, #PB_Drag_Copy)    ; needed for dragging images from a website
  EnableGadgetDrop(1, #PB_Drop_Files, #PB_Drag_Copy)   ; needed for dragging image from desktop / harddisk
  
  Repeat
    eventID = WaitWindowEvent()
    Select eventID
      Case #PB_Event_GadgetDrop
        If EventGadget() = 1
          If EventDropType() = #PB_Drop_Text
            AddGadgetItem(1, -1, EventDropText())
            SetGadgetText(2, EventDropText())
          ElseIf EventDropType() = #PB_Drop_Files
            AddGadgetItem(1, -1, EventDropFiles())
            SetGadgetText(2, EventDropFiles())
          EndIf            
        EndIf
        
    EndSelect
  Until eventID = #PB_Event_CloseWindow
EndIf
Any more idea to solve this? (or a PB bug?)

Currently I'm at the same point as before. My original question was only about getting the html address of dragged images (which I thought was the only problem, as dragging images from disk was already working), but now it must be extended to "How can I retrieve the html address of a dragged image from website, when also dragging of images from disk (and retrieving their file path) should be possible?" :wink:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Getting the html link when dropping an image from a webs

Post by RASHAD »

Hi Marc56us
Hi Andre
Quick hack till I find a better solution

Code: Select all

LoadFont(0,"Georgia",12)
If OpenWindow(0,0,0,800,600,"Drag with image!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  ContainerGadget(10,10,10,450,540,#PB_Container_Flat)
    WebGadget(0,0,0,450,540,"")
  CloseGadgetList()  
  EditorGadget(1,470,10,320,540)
  SetGadgetFont(1,FontID(0))
  AddGadgetItem(1,-1,"Drop here V :"+#CRLF$)
  EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)                
  EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_None)
  OptionGadget(2,10,565,80,22,"URL Links")
  OptionGadget(3,100,565,80,22,"PC Files")
  OptionGadget(4,190,565,100,22,"Save URL Images")
  SetGadgetState(2,1) 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 2            
                EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)                
                EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_None)
          Case 3            
                EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_None)                
                EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_Copy)
          Case 4           
                EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_None)                
                EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_Copy)
        EndSelect
        
      Case #PB_Event_GadgetDrop
          If EventGadget() = 1
            Select EventDropType()
              Case #PB_Drop_Text
                Text$ = "<center><b><font size=4 color=red> "+EventDropText()+" </font></b></center>"
                SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
                AddGadgetItem(1,-1,EventDropText())
                flag$ = Right(EventDropText(),3)
                If flag$ <> "bmp" And flag$ <> "jpg" And flag$ <> "png" And flag$ <> "gif"
                    SetGadgetText(0,EventDropText())
                EndIf      
                 
              Case #PB_Drop_Image
              
              Case #PB_Drop_Files
                Text$ = "<center><b><font size=4 color=red> ** </font></b></center>"
                SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
                If GetGadgetState(4) = 1
                  LoadImage(0,EventDropFiles())
                  SetGadgetText(0,EventDropFiles())
                  File$ = SaveFileRequester("Please choose file to save", "Rashad.bmp", "Bitmap (*.bmp)", 0)
                  If File$
                    MessageRequester("Information", "You have selected following file:"+Chr(10)+File$, 0)
                    SaveImage(0,File$)
                  Else
                    MessageRequester("Information", "The requester was canceled.", 0) 
                  EndIf                    
                  ;no + 1
                  ;MessageRequester("OK","Image Saved at "+GetHomeDirectory(),#MB_OK|#MB_SYSTEMMODAL)
                ElseIf GetGadgetState(3) = 1
                  AddGadgetItem(1,-1,EventDropFiles())
                  SetGadgetText(0,EventDropFiles())
                EndIf
                             
              Case #PB_Drop_Private
            EndSelect            
        EndIf
  EndSelect
  Until Quit = 1
EndIf
Edit :Modified
Edit 2 :Modified
Egypt my love
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Getting the html link when dropping an image from a webs

Post by Andre »

Hi RASHAD,
thanks - this works of course :D

But I really would like to see an automatic detection of the link (if the image is coming from PC then the filepath, if coming from a website the html-link), if this is possible.... :P

Maybe also Fred should take a look, if there is something to do on the Drag & Drop lib, because I do not really understand why activating the file drag & drop (EnableGadgetDrop(...,#PB_Drop_Files,...) is blocking the other ones!?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Getting the html link when dropping an image from a webs

Post by RASHAD »

Hi Andre
Step forward
I am trying to keep it cross platform

Code: Select all


LoadFont(0,"Georgia",12)
If OpenWindow(0,0,0,800,600,"Drag with image!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  ContainerGadget(10,10,10,450,540,#PB_Container_Flat)
    WebGadget(0,0,0,450,540,"")
  CloseGadgetList() 
  EditorGadget(1,470,10,320,540)
  SetGadgetFont(1,FontID(0))
  AddGadgetItem(1,-1,"Drop here V :"+#CRLF$)
 
  EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_Copy)
  EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)  

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1        
                
      
      Case #PB_Event_GadgetDrop
          If EventGadget() = 1              
              If EventDropType() = #PB_Drop_Text
                Text$ = "<center><b><font size=4 color=red> "+EventDropText()+" </font></b></center>"
                SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
                AddGadgetItem(1,-1,EventDropText())
                SetGadgetText(0,EventDropText())
              EndIf
             
              If EventDropType() = #PB_Drop_Files
                Text$ = "<center><b><font size=4 color=red> ** </font></b></center>"
                SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
                AddGadgetItem(1,-1,EventDropFiles())
                SetGadgetText(0,EventDropFiles())
              EndIf         
        EndIf
  EndSelect
  Until Quit = 1
EndIf
Egypt my love
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Getting the html link when dropping an image from a webs

Post by Andre »

RASHAD wrote:Hi Andre
Step forward
I am trying to keep it cross platform
Thank you, RASHAD! This works now on my Win10 machine.... :mrgreen:

Couldn't believe to haven't tried this myself before... :idea:

Another test gave the result, that's important in which order the EnableGadgetDrop(...) options will be called.
If changing the order of activating 'Text' and 'Files' support like below, the code example is giving again the wrong result (temporary file-path of the image on disk instead of the real html-link) :!:

Code: Select all

  EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy) 
  EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_Copy)
If that's the official behaviour (Fred?) some of such tips as well an example should be added to the docs.

By the way: I did a small test of your latest code on MacOS 10.6.8, but there I don't get any link. Probably the result of the known general problems with Drag & Drop lib on OS X... :x


Edit:
While I'm getting the real html-link now, which could be easily used for loading the dropped image in a WebGadget, what is the easiest way for dropping/loading the image in a CanvasGadget?
I noticed here, that since the html-link is returned I can't use it for loading the image with LoadImage() anymore, which wasn't a problem before (when the file-path of temporary downloaded image was returned). Is now downloading the image file myself and then loading it via LoadImage() the only solution?
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Getting the html link when dropping an image from a webs

Post by RASHAD »

Hi Andre
About the order of EnableGadgetDrop()
Multiple formats can be allowed on the same gadget. If the drag source provides multiple formats that match the list of accepted formats, the one that was added last will be accepted. So the preferred format in which to receive data should be enabled last.
You wrote that in the Doc :P right ?
Egypt my love
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Getting the html link when dropping an image from a webs

Post by Andre »

RASHAD wrote:About the order of EnableGadgetDrop()
Multiple formats can be allowed on the same gadget. If the drag source provides multiple formats that match the list of accepted formats, the one that was added last will be accepted. So the preferred format in which to receive data should be enabled last.
You wrote that in the Doc :P right ?
Oh, no.... :lol:

Not added by me (probably translated to german longer time ago, and now overseen...), as I hadn't any experiences with Drag & Drop before. Currently implementing such functionality for the first time myself... :oops:

Any idea about my 'Edit' comment above?

(For now it's time to go to bed, good night for today!)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Getting the html link when dropping an image from a webs

Post by RASHAD »

Hope it will answer your request

Code: Select all

UseJPEG2000ImageDecoder()
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

;You can use Wilbert fantastic ImagePlugin Module instead of the above decoders(more formats you will gain)

CreateImage(10,320,240,24,$FFFFFF)

LoadFont(0,"Georgia",12)
If OpenWindow(0,0,0,800,600,"Drag with image!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  ContainerGadget(10,10,10,450,540,#PB_Container_Flat)
    WebGadget(0,0,0,450,540,"")
  CloseGadgetList() 
  EditorGadget(1,470,30,320,250)
  SetGadgetFont(1,FontID(0))
  TextGadget(#PB_Any,470,10,320,20,"Drop URLs & Files here V :"+#CRLF$)
  TextGadget(#PB_Any,470,290,320,20,"Drop Images here V :"+#CRLF$)
  CanvasGadget(2,470,310,320,240)  
  
  EnableGadgetDrop(2,#PB_Drop_Text, #PB_Drag_Copy)
  EnableGadgetDrop(2,#PB_Drop_Files, #PB_Drag_Copy) 
  
  EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_Copy)
  EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)  

TText$ = "<center><b><font size=4 color=red> ** </font></b></center>"

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1          
    
    Case #PB_Event_GadgetDrop
      If EventGadget() = 1 
              If EventDropType() = #PB_Drop_Text
                Text$ = "<center><b><font size=4 color=red> "+EventDropText()+" </font></b></center>"                
                AddGadgetItem(1,-1,EventDropText())
                Tail$ = Right(EventDropText(),3)
                If (Tail$ = "bmp" Or Tail$ = "jpg" Or Tail$ = "png" Or Tail$ = "tif" Or Tail$ = "tga" Or Tail$ = "gif" Or Tail$ = "jp2") And FindString(EventDropText(),"?") <> 0
                     SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
                Else
                    SetGadgetText(0,EventDropText())
                EndIf 
              EndIf
             
              If EventDropType() = #PB_Drop_Files
                Text$ = "<center><b><font size=4 color=red> ** </font></b></center>"
                SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
                AddGadgetItem(1,-1,EventDropFiles())
                LoadImage(0, EventDropFiles())
                SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(0))
                SetGadgetText(0,EventDropFiles())
              EndIf                 
      EndIf
          
      If EventGadget() = 2
          If EventDropType() = #PB_Drop_Files          
            SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(10))
            Tail$ = Right(EventDropFiles(),3)
            If Tail$ = "bmp" Or Tail$ = "jpg" Or Tail$ = "png" Or Tail$ = "tif" Or Tail$ = "tga" Or Tail$ = "gif" Or Tail$ = "jp2"
              AddGadgetItem(1,-1,EventDropFiles())
              LoadImage(0, EventDropFiles())              
              SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(0))
            EndIf
          EndIf
          If EventDropType() = #PB_Drop_Text
            SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(10))
            Tail$ = Right(EventDropText(),3)
            If Tail$ = "bmp" Or Tail$ = "jpg" Or Tail$ = "png" Or Tail$ = "tif" Or Tail$ = "tga" Or Tail$ = "gif" Or Tail$ = "jp2"
              AddGadgetItem(1,-1,EventDropText())
              LoadImage(0, EventDropText())
              SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(0))           
            EndIf
          EndIf
          ;*******************  If you plan to save the image to your Home directory  *******************
          imgW = ImageWidth(0)
          imgH = ImageHeight(0)
          imgID = GetGadgetAttribute(2,#PB_Canvas_Image)
          img = CreateImage( #PB_Any,320,230)
          StartDrawing(ImageOutput(img))
            DrawImage(imgID,0,0)
          StopDrawing()
          GrabImage(img,imgSAVE,0,0,imgW,imgH)
          SaveImage(imgSAVE,GetHomeDirectory()+"Andre "+Str(imgn)+".bmp",#PB_ImagePlugin_BMP)
          imgn + 1                  
      EndIf
  EndSelect
Until Quit = 1
EndIf
Egypt my love
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Getting the html link when dropping an image from a webs

Post by Andre »

Hi RASHAD,
thanks again for your continous help! :D

But if I'm not too stupid, the mentioned problem still exists even with the newest code example:

While all is working (like before), when dropping an image from a website onto the EditorGadget in the top-right corner (it will then be displayed in the WebGadget on the left side + the real web-link is displayed in the EditorGadget), the problem with the Canvas is still the same.

I tried to drop an image from a website (e.g. Wikipedia Commons) onto the CanvasGadget in the bottom-right corner.
You will again get not the real html-link of the image (e.g. https://upload.wikimedia.org/wikipedia/ ... aurant.jpg), but the filepath of the temporary saved image on disk.

:?:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Getting the html link when dropping an image from a webs

Post by RASHAD »

Hi Andre
1- Drop in the EditorGadget to get the real link
2- Drop in the CanvasGadget to get the Image and it will be saved in your Home Directory (All images format are supported)
The EditorGadget do not support #PB_Drop_Image
The CanvasGadget do not support either #PB_Drop_Files & #PB_Drop_Text

Code: Select all

ExamineDesktops()

UseJPEG2000ImageDecoder()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UsePNGImageDecoder()
UseTGAImageDecoder()
UseTIFFImageDecoder()

CreateImage(10,DesktopWidth(0),DesktopHeight(0),24,$FFFFFF)

LoadFont(0,"Georgia",12)
If OpenWindow(0,0,0,800,600,"Drag with image!",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  ContainerGadget(10,10,10,450,540,#PB_Container_Flat)
    WebGadget(0,0,0,450,540,"")
  CloseGadgetList() 
  EditorGadget(1,470,30,320,250)
  SetGadgetFont(1,FontID(0))
  TextGadget(#PB_Any,470,10,320,20,"Drop URLs & Files here V :"+#CRLF$)
  TextGadget(#PB_Any,470,290,320,20,"Drop Images here V :"+#CRLF$)
  ScrollAreaGadget(#PB_Any,470,310,320,240,DesktopWidth(0), DesktopHeight(0),10,#PB_ScrollArea_Flat)
    CanvasGadget(2,0,0,DesktopWidth(0),DesktopHeight(0))
  CloseGadgetList()

  EnableGadgetDrop(1,#PB_Drop_Files, #PB_Drag_Copy)
  EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy) 
  
  EnableGadgetDrop(2,#PB_Drop_Image, #PB_Drag_Copy)

TText$ = "<center><b><font size=4 color=red> ** </font></b></center>"

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1      
  
    Case #PB_Event_GadgetDrop
      If EventGadget() = 1 
        If EventDropType() = #PB_Drop_Text
          Text$ = "<center><b><font size=4 color=red> "+EventDropText()+" </font></b></center>"                
          AddGadgetItem(1,-1,EventDropText())
          Tail$ = Right(EventDropText(),3)
          If (Tail$ = "bmp" Or Tail$ = "jpg" Or Tail$ = "png" Or Tail$ = "tif" Or Tail$ = "tga" Or Tail$ = "gif" Or Tail$ = "jp2") And FindString(EventDropText(),"HTTP") = 0
              SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
          Else
              SetGadgetText(0,EventDropText())
          EndIf 
        EndIf
       
        If EventDropType() = #PB_Drop_Files
          Text$ = "<center><b><font size=4 color=red> ** </font></b></center>"
          SetGadgetItemText(0, #PB_Web_HtmlCode, Text$)
          AddGadgetItem(1,-1,EventDropFiles())
          Tail$ = Right(EventDropFiles(),3)
          If Tail$ = "bmp" Or Tail$ = "jpg" Or Tail$ = "png" Or Tail$ = "tif" Or Tail$ = "tga" Or Tail$ = "gif" Or Tail$ = "jp2"
            img$ = EventDropFiles() 
          EndIf
          SetGadgetText(0,EventDropFiles())
        EndIf                 
      EndIf
      If img$ <> ""
          result = LoadImage(#PB_Any,img$)
          SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(10))
          SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(result))
          img$ = ""
      EndIf
;           
      If EventGadget() = 2
        If EventDropType() = #PB_Drop_Image
          SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(10))
          Result = EventDropImage(#PB_Any)
          SetGadgetAttribute(2,#PB_Canvas_Image ,ImageID(result))
        EndIf
;           ;*******************  If you plan to save the image to your Home directory  *******************
        imgW = ImageWidth(Result)
        imgH = ImageHeight(Result)
        imgID = GetGadgetAttribute(2,#PB_Canvas_Image)
        img = CreateImage( #PB_Any,imgW,imgH)
        StartDrawing(ImageOutput(img))                   
          DrawImage(imgID,0,0)
        StopDrawing()      
        SaveImage(img,GetHomeDirectory()+"Andre "+Str(imgn)+".jpg",#PB_ImagePlugin_JPEG )
        imgn + 1                  
      EndIf
  EndSelect
Until Quit = 1
EndIf
Edit :Code modified
Egypt my love
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Getting the html link when dropping an image from a webs

Post by Andre »

RASHAD wrote:Hi Andre
1- Drop in the EditorGadget to get the real link
2- Drop in the CanvasGadget to get the Image and it will be saved in your Home Directory (All images format are supported)
The EditorGadget do not support #PB_Drop_Image
The CanvasGadget do not support either #PB_Drop_Files & #PB_Drop_Text
Hi RASHAD,

thanks again for the further tests and for this (final?) example, showing what's possible and what's not. :D

As it seems, you can't have all in one (Canvas)Gadget... dropping images from websites and getting their URL, and support for dropping files from disk too. So we have to live two different gadgets as "destination" for the image dropping.

Probably I'm doing it now also the way, that directly dropping the image from website on the CanvasGadget is possible (but the image will be downloaded to Temp/Home folder first, which you will get returned then as filepath instead of the real URL), and the real URL could be copied & pasted into another StringGadget...

Anyway it would be good to have an information from Fred/freak, if direct support on CanvasGadget could be implemented.
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply