Images in Editor gadgets!

Share your advanced PureBasic knowledge/code with the community.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

yep, that seems to be a way to determine the drop.

Was just entering the world of EN_DRAGDROPDONE. This saved me some time.


Thanks again.
Nico
Enthusiast
Enthusiast
Posts: 274
Joined: Sun Jan 11, 2004 11:34 am
Location: France

Post by Nico »

Here is a modification of the code to drag and drop a text file or image file to the editor:

Code: Select all

Procedure.l RichEdit_QueryAcceptData(*pObject.RichEditOle, *lpdataobj.IDataObject, *lpcfFormat, reco, fReally, hMetaPict)
    Protected pEnumFormats.IEnumFORMATETC,fmtetc.FORMATETC,stgmed.STGMEDIUM
    Protected Extension$,lpszFile.s,hdrop.l,drop.l,nFiles.l,nNames.l,id.l,*Buffer.l
   
    Debug "RichEdit_QueryAcceptData"

; Predefined Clipboard Formats
; Private Const CF_TEXT = 1
; Private Const CF_BITMAP = 2
; Private Const CF_METAFILEPICT = 3
; Private Const CF_SYLK = 4
; Private Const CF_DIF = 5
; Private Const CF_TIFF = 6
; Private Const CF_OEMTEXT = 7
; Private Const CF_DIB = 8
; Private Const CF_PALETTE = 9
; Private Const CF_PENDATA = 10
; Private Const CF_RIFF = 11
; Private Const CF_WAVE = 12
; Private Const CF_UNICODETEXT = 13
; Private Const CF_ENHMETAFILE = 14
; Private Const CF_HDROP = 15
; Private Const CF_LOCALE = 16
; Private Const CF_MAX = 17

    drop=0
    ;// Get the COM Interface For format Enumeration
     If *lpdataobj\EnumFormatEtc(#DATADIR_GET, @pEnumFormats.IEnumFORMATETC)=0
     ;// Enumerate each type of Data supported by this IDataObject, one-by-one
       While pEnumFormats\Next(1, @fmtetc.FORMATETC, @num.l)=0

        If fmtetc\cfFormat=#CF_HDROP
            lpszFile.s=Space(#MAX_PATH + 1)
            If *lpdataobj\GetData(fmtetc,@stgmed.STGMEDIUM)=0
                hdrop.l = GlobalLock_(stgmed\hGlobal)
                If hdrop
                    nFiles.l = DragQueryFile_(hdrop, -1, #Null, 0)
                    If nFiles=1 ; ici je n'accepte que le glisser déposer d'un seul fichier
                        ZeroMemory_(@lpszFile, #MAX_PATH + 1)
                        DragQueryFile_(hdrop, nNames, @lpszFile, #MAX_PATH + 1)
                        Extension$ = GetExtensionPart(lpszFile)
                        Extension$ = LCase(Extension$)
                        If Extension$="bmp" Or Extension$="png" Or Extension$="jpeg" Or Extension$="jpg"
                            drop=4
                        ElseIf Extension$="rtf"
                            drop=3
                        ElseIf Extension$="txt"
                            drop=2
                        Else
                            drop=1
                        EndIf
                    Else
                          drop=5
                    EndIf
                    GlobalUnlock_(hdrop)
                EndIf
                ReleaseStgMedium_(@stgmed)
            EndIf
        EndIf
       Wend
     pEnumFormats\Release()
     EndIf
   
   If fReally=0 And drop=1
       ProcedureReturn -1
   EndIf
   
  If fReally=1  And drop>1
      Select drop
          Case 2
             LoadRTF(GetDlgCtrlID_(*pObject\hwnd), lpszFile)
             
          Case 3
             LoadRTF(GetDlgCtrlID_(*pObject\hwnd), lpszFile,#SF_RTF)
             
          Case 4
             id=LoadImage(#PB_Any,lpszFile)
             SetClipboardImage(id)
             SendMessage_(*pObject\hwnd,#WM_PASTE,0,0)
             FreeImage(id)
             
          Case 5
              MessageRequester("Information", "Ne glisser-déposer qu'un seul fichier à la fois!", 0)   
       EndSelect
       ProcedureReturn -1
   EndIf
 
  ProcedureReturn #S_OK
EndProcedure
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Very nice code Nico - works brilliantly here. 8)
I may look like a mule, but I'm not a complete ass.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Images in Editor gadgets!

Post by cas »

Is it possible to use CatchRTF() with #SF_RTF to append new rtf formatted text to existing rtf text in EditorGadget?
I tried with replaceall=0 but it still replaces all data in EditorGadget with new data. :(

Thanks
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Images in Editor gadgets!

Post by srod »

You need to set replaceall = #SFF_SELECTION.

E.g.

Code: Select all

;**************MUST INCLUDE***************************************************************** 
XIncludeFile "OLEedit.pbi" 
;******************************************************************************************* 

a$ = "{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}"
a$ + "{\*\generator Msftedit 5.41.21.2508;}\viewkind4\uc1\pard\f0\fs20{\pict\wmetafile8\picwgoal210\pichgoal240 "
a$ + "010009000003f500000000007d00000000000400000003010800050000000b0200000000050000"
a$ + "000c0210001000030000001e0004000000070104000800000026060f000600544e505006014300"
a$ + "0000410b8600ee000f000f00000000000f000f0000000000280000000f0000000f000000010001"
a$ + "0000000000000000000000000000000000000000000000000000000000ffffff0007c0d9031ff0"
a$ + "494c3ff8bf037ffc00007ffc0000fffe0000fffe0000fffe0000fffe0000fffe00007ffcffff7f"
a$ + "fc00003ff8ffff1ff0bf0307c000007d000000410bc60088000f000f00000000000f000f000000"
a$ + "0000280000000f0000000f00000001000400000000000000000000000000000000000000000000"
a$ + "00000000000000ffffff0045454500ebffff00c7ffff0093feff0000ceff0013fdff0000eaff00"
a$ + "009dfe0000c9ff0000b4ff0000e5ff00000000000000000000000000111112222211111011122b"
a$ + "bbb922111011266aaaaa6921101266c00000a6921012680111110a6210268011111110a9202780"
a$ + "11111110ab202500000000000b202508888888880b20247880888088a920125780888088621012"
a$ + "457888888692101123477888692110111223455622111011111222221111100800000026060f00"
a$ + "0600544e50500701040000002701ffff030000000000"
a$ + "}\par"
a$ + "}"


If OpenWindow(0, 0, 0, 600, 400, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0, 8, 8, 500, 300) 
  ButtonGadget(1,520,30,60,20,"Insert") 
  ;Set up the com interface for the editor gadget. 
    RichEdit_SetInterface(GadgetID(0)) 

  Repeat 
    EV=WaitWindowEvent() 
    Select EV 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case 1 
            CatchRTF(0, @a$, @a$ + Len(a$)-1, #SF_RTF, #SFF_SELECTION) 
        EndSelect 
    EndSelect 
    
  Until EV = #PB_Event_CloseWindow 
EndIf 
End
I may look like a mule, but I'm not a complete ass.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Images in Editor gadgets!

Post by cas »

Thanks srod. replaceall=#SFF_SELECTION solved my problem.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Images in Editor gadgets!

Post by cas »

I have a question about CatchRTF() LoadRTF() and SaveRTF() .
In comments before procedures it says:
;Returns zero if no error encountered.
But here always returns -2147024858.
I changed

Code: Select all

ProcedureReturn edstr\dwError
to

Code: Select all

ProcedureReturn edstr\dwError<<31
to get return 0. But after adding GetLastError_() between SendMessage_() and ProcedureReturn i get error code 6 which means "The handle is invalid".

So does it mean that there always some error happen even if it successfully displays content in EditorGadget? Or did i miss something (probably :oops: ).

Thanks
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Images in Editor gadgets!

Post by srod »

I get no error codes here.

The value in edstr\dwError can be set by Windows (if it encounters a problem whist streaming) or it is simply the return value from the streaming callback function (which again will be placed there by Windows). For the majority of cases this should be zero.

Now, the code in this thread has been chopped and changed by a few people over the years so I would first have a good look at the edit-stream callback functions to make sure there is nothing obviously amis with the code. After that, I would ask as to the source of the rft files which you are streaming? If they were created by MS Word, for example, then expect problems! :) The rich-edit control simply does not like MS Word created rtf files!

Beyond this.... I do not know. As I say, I can not reproduce the problem here.
I may look like a mule, but I'm not a complete ass.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Images in Editor gadgets!

Post by cas »

Thanks for replying. I get these error returned with any data streamed in, even with your example here which inserts data to end (3 posts up). It outputs smileys here but it's just that odd error message, everything else looks right. Maybe the problem is with ansicpg1252 or deflang2057 so it can't find it on my system and it automatically changes to default one for my system and that error message is just a warning that original rtf data is not displayed but changed one. I tested it on Windows XP x86 and Windows 7 x64 with PB 4.40, 4.41 and 4.50 x86 and i always get same result.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Images in Editor gadgets!

Post by srod »

That sounds plausible yes (the code page). :)
I may look like a mule, but I'm not a complete ass.
User avatar
OldSkoolGamer
Enthusiast
Enthusiast
Posts: 150
Joined: Mon Dec 15, 2008 11:15 pm
Location: Nashville, TN
Contact:

Re: Images in Editor gadgets!

Post by OldSkoolGamer »

For anyone that's interested, here's how to set the start indent & spacing for the bullets:

Change this:

Code: Select all

Procedure Editor_Bulleted(Gadget) 
  format.PARAFORMAT 
  format\cbSize = SizeOf(PARAFORMAT) 
  format\dwMask = #PFM_NUMBERING
  format\wNumbering = #PFN_BULLET
  SendMessage_(GadgetID(Gadget), #EM_SETPARAFORMAT, 0, @format) 
EndProcedure 
to this:

Code: Select all

Procedure Editor_Bulleted(Gadget) 
  format.PARAFORMAT 
  format\cbSize = SizeOf(PARAFORMAT) 
  format\dwMask = #PFM_NUMBERING|#PFM_OFFSET|#PFM_STARTINDENT
  format\wNumbering = #PFN_BULLET
  format\dxStartIndent = 200; Space between left margin & bullet
  format\dxOffset = 300; Space between bullet & start of text
  SendMessage_(GadgetID(Gadget), #EM_SETPARAFORMAT, 0, @format) 
EndProcedure
Post Reply