Page 1 of 1

Memory StreamIn EditorGadget

Posted: Fri May 08, 2009 4:42 am
by ts-soft

Code: Select all

; PB 4.xx
; Target OS Windows (x86, x64, ASCII and Unicode)
; Author: Thomas <ts-soft> Schulz
; 2009/05/08
; Memory in EditorGadget streamen


EnableExplicit

Procedure StreamMemoryIn_Callback(dwCookie, pbBuff, cb, *pcb.Integer)
  Static offset = 0
 
  If offset + cb <= MemorySize(dwCookie)
    ; we copy the complett buffer (pbBuff), size is cb
    CopyMemory(dwCookie + offset, pbBuff, cb)
    offset + cb ; offset
    *pcb\i = cb
  Else
    If offset = MemorySize(dwCookie)
      *pcb\i = 0 ; here is the finish
      offset = 0 ; offset to 0 for next order
    Else
      ; we copy the rest, smaller as pbbuff
      CopyMemory(dwCookie + offset, pbBuff, MemorySize(dwCookie) - offset)
      *pcb\i = MemorySize(dwCookie) - offset
      offset = MemorySize(dwCookie)
    EndIf
  EndIf
 
  ProcedureReturn 0
EndProcedure

Procedure Editor_StreamMemoryIn(ID, *mem, type = #SF_TEXT)
  Protected StreamData.EDITSTREAM
 
  StreamData\dwCookie = *mem
  StreamData\dwError = #Null
  StreamData\pfnCallback = @StreamMemoryIn_Callback()
  SendMessage_(GadgetID(ID), #EM_STREAMIN, type, @StreamData)
 
EndProcedure

; a not so good example ;)
OpenWindow(0, #PB_Ignore, #PB_Ignore, 640, 480, "void", #PB_Window_SystemMenu)
EditorGadget(0, 0, 0, 640, 480)

Define *mem = AllocateMemory(?readme_end - ?readme_start)
If *mem
  CopyMemory(?readme_start, *mem, MemorySize(*mem))
  Editor_StreamMemoryIn(0, *mem)
  FreeMemory(*mem)
EndIf

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow


DataSection
  readme_start: IncludeBinary #PB_Compiler_Home + "SDK\Readme.txt" : readme_end:
EndDataSection

Posted: Fri May 08, 2009 5:51 am
by pdwyer
/OT

Cool use of "IncludeBinary" 8) I'm gonna borrow that !

I'll take a peek at the gadget while I'm at it

Posted: Fri May 08, 2009 10:25 pm
by ts-soft
After writing this, i found a snippet by freak in the codearchiv. Is much
shorter, but it copied over the end of the memory. Is this sure? Seems to
work with RTF.

My Snippet work also with text. I require this in a short HexViewer.

Posted: Fri May 08, 2009 11:18 pm
by Joakim Christiansen
I also played around with this stuff once:
http://www.purebasic.fr/english/viewtopic.php?t=22443

Posted: Fri May 08, 2009 11:29 pm
by ts-soft
Joakim Christiansen wrote:I also played around with this stuff once:
http://www.purebasic.fr/english/viewtopic.php?t=22443
I saw your great code :D
After reading this, comes the idea to stream memory in as required for my hexviewer.

Posted: Sat May 09, 2009 1:04 pm
by akj
@ ts-soft:

Have I missed the point of this program?
I expected that streaming would take place as a direct result of my scrolling the EditorGadget, but this does not happen.

By inserting this extra line within the procedure StreamMemoryIn_Callback()

Code: Select all

Static counter: counter+1: Debug Str(counter)+"   "+Str(offset)
I see that all the streaming occurs on program startup and none as the result of any vertical scrolling I do.

So how does streaming help? I cannot see any advantage in using it and I would welcome your feedback.

Posted: Sat May 09, 2009 3:43 pm
by ts-soft
> So how does streaming help?
Is the fastest way to fill a editorgadget with large text.

Posted: Sat May 09, 2009 7:01 pm
by srod
You can also, with a very slight modification, stream in raw RTF format.

Posted: Sat May 09, 2009 7:36 pm
by ts-soft
srod wrote:You can also, with a very slight modification, stream in raw RTF format.
added to the code, thanks

you can optioal use #SF_RTF or other Types