problem with #EM_GETLINE

Just starting out? Need help? Post your questions and find answers here.
User avatar
aston
User
User
Posts: 64
Joined: Wed Nov 18, 2009 11:18 pm

problem with #EM_GETLINE

Post by aston »

Hi ...
I don't get it why in my example win api message #EM_GETLINE not work.
problem is under For/next loop under button 2 event.
You must be asking why i use 'EDIT' instead of editor gadget.
Editor gadget is richedit control ,right and don't have by default popup contextmenu
like 'EDIT' control from api.
Like you may see #EM_GETLINECOUNT work fine.
Anyone might have idea what i do wrong?

Code: Select all

Global EditControl
Global font1.i,pos.i,count.i
Define sysMenu.i,minBox.i,maxBox.i
Define wMsg.i,CloseWin.i
Define win.i,CenterWin.i,editStyle.i
Global staticControl.i
staticControl=16
;Global line.s
sysMenu = #PB_Window_SystemMenu
minBox = #PB_Window_MinimizeGadget 
maxBox = #PB_Window_MaximizeGadget
CloseWin = #PB_Event_CloseWindow
CenterWin = #PB_Window_ScreenCentered
win=1 


Procedure CreateEditText() 
font1=LoadFont(1,"Courier New",10, 0)
;create EDIT control
 hInstance = GetModuleHandle_(0) 
 EditControl = CreateWindowEx_(131072,"EDIT","",#WS_VISIBLE|#WS_CHILDWINDOW|#ES_AUTOHSCROLL|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL| #ES_MULTILINE | #ES_WANTRETURN |#ES_NOHIDESEL, 50,60,640,480,WindowID(0),200,hInstance,0) 
 ;set font 
 SendMessage_(EditControl,#WM_SETFONT,font1, 0) 
 
EndProcedure

If OpenWindow(0,0,0,800,600,"Basic Text Control",SysMenu | CenterWin)
  CreateEditText()
  ;If CreatePopupMenu(0)
      ;MenuItem(1, "Undo")
      ;MenuItem(2, "Cut")
     ; MenuItem(3, "Copy")
      ;MenuItem(4, "Paste")
      ;MenuBar()
      ;OpenSubMenu("Options")
        ;MenuItem(5, "Window...")
        ;MenuItem(6, "Gadget...")
      ;CloseSubMenu()
     ; MenuBar()
     ; MenuItem( 7, "Quit")
 ; EndIf
TextGadget(staticControl, 10,  40, 250, 20, "Empty")
                    
  
  ButtonGadget(1,10,10,80,24,"LOAD")
  ButtonGadget(2,100,10,80,24,"RUN")
  
   EndIf
   
   Repeat
     
Select WaitWindowEvent()     
   Case #PB_Event_CloseWindow
          Q = 1     
   Case #PB_Event_Gadget
          
   Select EventGadget()
              
   Case 1 ;open file -----------------------------------------------------------------------  
   file$ = OpenFileRequester("Select a file","","Text (.txt)|*.txt|All files (*.*)|*.*",0)
   If file$
    If ReadFile(0, file$)
      length = Lof(0)                            ; get the length of opened file
      *mem = AllocateMemory(length)         ; allocate the needed memory
      If *mem
        ReadData(0, *mem, length)   ; read all data into the memory block
        text.s=PeekS(*mem, length)
       FreeMemory(*mem)
      EndIf
      CloseFile(0)
      ;set text
       SendMessage_(editControl,#WM_SETTEXT,0,text)
    EndIf
   EndIf
   ;--------------------------------------------------------------------------------------------
   Case 2
     ;read text line by line and show line in STATIC (text) control
     count=SendMessage_(editControl,#EM_GETLINECOUNT,0,0)
     MessageRequester("Number of Lines:", "Lines:"+Str(count), 0)
     ;next loop not work? ****************************************************
     
     For i = 0 To 10
      ;line.s = GetGadgetItemText(200, i)
    SendMessage_(editControl,#EM_GETLINE,i,line.s)
      
      SetGadgetText(16,line.s)
      SendMessage_(staticControl,#WM_SETTEXT,0,line)
      ;MessageRequester("Text",line.s, 0)
     Next 
  ;*********************************************************************

              
  EndSelect
                  
        Case #PB_Event_Menu
      
          Select EventMenu()  ; To see which menu has been selected
              
              Case 1
                  SendMessage_(GetFocus_(),#WM_UNDO,0,0)

              Case 2
                  SendMessage_(GetFocus_(),#WM_CUT,0,0)
    
              Case 3
                  SendMessage_(GetFocus_(),#WM_COPY,0,0)
    
              Case 4
                  SendMessage_(GetFocus_(),#WM_PASTE,0,0)
    
              Case 7
                Q = 1

               
            EndSelect
            
          Case #WM_RBUTTONDOWN
            DisplayPopupMenu(0, WindowID(0))
            
      EndSelect
    Until Q = 1
    CloseLibrary(0)
  End  
    
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: problem with #EM_GETLINE

Post by luis »

lParam
A pointer to the buffer that receives a copy of the line. Before sending the message, set the first word of this buffer to the size, in TCHARs, of the buffer. For ANSI text, this is the number of bytes; for Unicode text, this is the number of characters. The size in the first word is overwritten by the copied line.
Pretty weird way to avoid one more param.

Code: Select all

     For i = 0 To 10
      ;line.s = GetGadgetItemText(200, i)
     
     
     line.s = Space(255) ; or whatever, maybe better to allocate memory once instead of a string
     PokeW(@line,Len(line))
     SendMessage_(editControl,#EM_GETLINE,i,line)
     Debug line 
     Debug Len(Line)
     
     
      SetGadgetText(16,line)
      SendMessage_(staticControl,#WM_SETTEXT,0,line)
      ;MessageRequester("Text",line.s, 0)
     Next
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: problem with #EM_GETLINE

Post by Michael Vogel »

Some more weird things, I tried to replace the string buffer by using a memory block (to be reused multiple times)...

Code: Select all

#MaxTextLen=100
Global *Buffer;.String
*Buffer=AllocateMemory(1000)

PokeW(*Buffer,#MaxTextLen)
len=SendMessage_(Handle,#EM_GETLINE,#Null,*Buffer)
debug PeekS(*Buffer,len)
...with no success.

With strings, everything works fine in ASCII mode, but when using Unicode something is going wrong from time to time because no zero bytes are written at the end...

Code: Select all

	:
	#MaxTextLen=100
	line.s = Space(#MaxTextLen<<#PB_Compiler_Unicode)
	PokeW(@line,#MaxTextLen)
	len=SendMessage_(editControl,#EM_GETLINE,i,line)
	line=PeekS(@line,len)
	Debug "'"+Len(Line)+"'"

	SetGadgetText(16,line)
	SendMessage_(staticControl,#WM_SETTEXT,0,line)
	if i=0 : ShowMemoryViewer(@line,100) : endif
	MessageRequester("Text "+Str(i),"'"+line.s+"'", 0)
	:
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: problem with #EM_GETLINE

Post by luis »

I cannot reproduce what you are saying, it is working for me (unicode x86) replacing the for/next in the first post with this

Code: Select all

      Global *Buffer = AllocateMemory(1000)
         
      For i = 0 To count - 1   
        #MaxTextLen = 100        
        PokeW(*Buffer,#MaxTextLen)
        l = SendMessage_(editControl,#EM_GETLINE,i,*Buffer)
        ;ShowMemoryViewer(*Buffer, #MaxTextLen)
        Debug PeekS(*Buffer,l)     
      Next
"Have you tried turning it off and on again ?"
A little PureBasic review
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: problem with #EM_GETLINE

Post by RASHAD »

Hi guys
# 1:

Code: Select all

font1=LoadFont(1,"Courier New",10, 0)

If OpenWindow(0,0,0,800,600,"Basic Text Control",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(0,10,60,780,530)
  SetGadgetFont(0,FontID(1))
  If CreatePopupMenu(0)
      MenuItem(1, "Undo")
      MenuItem(2, "Cut")
     MenuItem(3, "Copy")
      MenuItem(4, "Paste")
      MenuBar()
      OpenSubMenu("Options")
        MenuItem(5, "Window...")
        MenuItem(6, "Gadget...")
      CloseSubMenu()
     MenuBar()
     MenuItem( 7, "Quit")
EndIf                    
  
  ButtonGadget(1,10,10,80,24,"LOAD")
  ButtonGadget(2,100,10,80,24,"RUN")
  
   EndIf
   
Repeat     
  Select WaitWindowEvent()     
    Case #PB_Event_CloseWindow
          Q = 1     
    Case #PB_Event_Gadget
          
    Select EventGadget()
              
    Case 1 ;open file ----------------------------------------------------------------------- 
      file$ = OpenFileRequester("Select a file","","Text (.txt)|*.txt|All files (*.*)|*.*",0)
      If file$
        ReadFile(0, file$)   
        While Eof(0) = 0           ; loop as long the 'end of file' isn't reached
          Text$ =  ReadString(0)      ; display line by line in the debug window
          AddGadgetItem(0, -1 ,text$)
        Wend
        CloseFile(0) 
      EndIf
   ;--------------------------------------------------------------------------------------------
    Case 2
      lCount = SendMessage_(GadgetID(0), #EM_GETLINECOUNT, 0,0) - 1
      For i = 0 To lCount
        lIndex = SendMessage_(GadgetID(0), #EM_LINEINDEX, i,0)     
        lLen = SendMessage_(GadgetID(0), #EM_LINELENGTH, lIndex, 0)
        lText$ = Space(lLen + 1)
        PokeW(@lText$,lLen)
        SendMessage_(GadgetID(0),#EM_GETLINE,i,@lText$)
        Debug lLen
        Debug lText$
        Debug ""     
      Next

  ;*********************************************************************

              
  EndSelect
                  
        Case #PB_Event_Menu
      
          Select EventMenu()  ; To see which menu has been selected
              
              Case 1
                  SendMessage_(GetFocus_(),#WM_UNDO,0,0)

              Case 2
                  SendMessage_(GetFocus_(),#WM_CUT,0,0)
    
              Case 3
                  SendMessage_(GetFocus_(),#WM_COPY,0,0)
    
              Case 4
                  SendMessage_(GetFocus_(),#WM_PASTE,0,0)
    
              Case 7
                Q = 1

               
            EndSelect
            
          Case #WM_RBUTTONDOWN
            DisplayPopupMenu(0, WindowID(0))
            
      EndSelect
    Until Q = 1
  End 
# 2:

Code: Select all


Global EditControl
Global font1.i,pos.i,count.i
Define sysMenu.i,minBox.i,maxBox.i
Define wMsg.i,CloseWin.i
Define win.i,CenterWin.i,editStyle.i
Global staticControl.i
staticControl=16
;Global line.s
sysMenu = #PB_Window_SystemMenu
minBox = #PB_Window_MinimizeGadget 
maxBox = #PB_Window_MaximizeGadget
CloseWin = #PB_Event_CloseWindow
CenterWin = #PB_Window_ScreenCentered
win=1 


Procedure CreateEditText() 
font1=LoadFont(1,"Courier New",10, 0)
;create EDIT control
hInstance = GetModuleHandle_(0) 
EditControl = CreateWindowEx_(131072,"EDIT","",#WS_VISIBLE|#WS_CHILDWINDOW|#ES_AUTOHSCROLL|#ES_AUTOVSCROLL|#WS_VSCROLL|#WS_HSCROLL| #ES_MULTILINE | #ES_WANTRETURN |#ES_NOHIDESEL, 10,60,780,530,WindowID(0),200,hInstance,0) 
;set font 
SendMessage_(EditControl,#WM_SETFONT,font1, 0) 

EndProcedure


If OpenWindow(0,0,0,800,600,"Basic Text Control",SysMenu | CenterWin)
  CreateEditText()
  ;If CreatePopupMenu(0)
      ;MenuItem(1, "Undo")
      ;MenuItem(2, "Cut")
     ; MenuItem(3, "Copy")
      ;MenuItem(4, "Paste")
      ;MenuBar()
      ;OpenSubMenu("Options")
        ;MenuItem(5, "Window...")
        ;MenuItem(6, "Gadget...")
      ;CloseSubMenu()
     ; MenuBar()
     ; MenuItem( 7, "Quit")
; EndIf
;TextGadget(staticControl, 10,  40, 250, 20, "Empty")
                    
  
  ButtonGadget(1,10,10,80,24,"LOAD")
  ButtonGadget(2,100,10,80,24,"RUN")
  
   EndIf
   
   Repeat
     
Select WaitWindowEvent()     
   Case #PB_Event_CloseWindow
          Q = 1     
   Case #PB_Event_Gadget
          
   Select EventGadget()
              
   Case 1 ;open file -----------------------------------------------------------------------  
    file$ = OpenFileRequester("Select a file","","Text (.txt)|*.txt|All files (*.*)|*.*",0)
    If file$
      ReadFile(0, file$)
      While Eof(0) = 0 
        Text$ =  ReadString(0)+#CRLF$
        SendMessage_(editControl,#EM_REPLACESEL,0,Text$)
      Wend
      CloseFile(0) 
    EndIf
   ;--------------------------------------------------------------------------------------------
   Case 2
     lCount = SendMessage_(editControl, #EM_GETLINECOUNT, 0,0)
     lCount - 1
    For i = 0 To lCount
     lIndex = SendMessage_(editControl, #EM_LINEINDEX, i,0)     
     lLen = SendMessage_(editControl, #EM_LINELENGTH, lIndex, 0)
     lText$ = Space(lLen + 1)
     PokeW(@lText$,lLen)
     SendMessage_(editControl,#EM_GETLINE,i,@lText$)
     Debug lLen
     Debug lText$
     Debug ""     
    Next

  ;*********************************************************************

              
  EndSelect
                  
        Case #PB_Event_Menu
      
          Select EventMenu()  ; To see which menu has been selected
              
              Case 1
                  SendMessage_(GetFocus_(),#WM_UNDO,0,0)

              Case 2
                  SendMessage_(GetFocus_(),#WM_CUT,0,0)
    
              Case 3
                  SendMessage_(GetFocus_(),#WM_COPY,0,0)
    
              Case 4
                  SendMessage_(GetFocus_(),#WM_PASTE,0,0)
    
              Case 7
                Q = 1

               
            EndSelect
            
          Case #WM_RBUTTONDOWN
            DisplayPopupMenu(0, WindowID(0))
            
      EndSelect
    Until Q = 1
  End 
Egypt my love
Post Reply