Create Comments Editor - Updated

Developed or developing a new product in PureBasic? Tell the world about it.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Create Comments Editor - Updated

Post by yrreti »

To make it simpler with less confusion, I will always put my latest version that I am using here.
Latest version: 1.9

Code: Select all

;version: 1.9
;Also works with PB 5.10
;Credits also to:
;Demivec for his suggestion to improve the un-comment part. 
;Tenaja for addition to be able to change comment characters used.
;Lubos for his idea to save the clipboard to a file for later use idea.

#DefaultCommentString = "; "

Enumeration
  #CommentWin
EndEnumeration

Enumeration
  #PreCommentEditor
  #Container_1
  #CommentEditor
  #ClrTxt_btn
  #CpyFromClpBd_btn
  #Cut_btn
  #Copy_btn
  #Paste_btn
  #Undo_txt
  #CmntChars_txt
  #CmntChars_str
  #CmntLines_btn
  #CmntLines_txt
  #Cpy2ClpBd_btn
  #Exit_btn
  #POPUP
  #Ctrl_v;disable these to allow only copy, paste, and cut functions through the pop up menu
  #Ctrl_C;so it uses the paste input formating there.
  #Ctrl_x
  
  #SaveFile
EndEnumeration

Define.l Event, EventWindow, EventGadget, EventType, EventMenu
Global point.POINT

;-
Procedure OpenWindow_CommentWin()
  xx=10 ;nice idea for group shifting gadgets
  xxx=10
  If OpenWindow(#CommentWin, 450, 198, 615, 397, Space(65)+"Create Comments Editor", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_WindowCentered)
    ButtonGadget(#ClrTxt_btn, 8, 355, 40, 32, "Clear Text", #PB_Button_MultiLine)
    ButtonGadget(#CpyFromClpBd_btn, 65-xx, 352, 58, 32, "Copy from Clipboard", #PB_Button_MultiLine)
    ButtonGadget(#Copy_btn, 200-xx, 352, 35, 32, "Copy", #PB_Button_MultiLine)
    ButtonGadget(#Paste_btn, 240-xx, 352, 35, 32, "Paste", #PB_Button_MultiLine)
    ButtonGadget(#Cut_btn, 280-xx, 352, 35, 32, "Cut", #PB_Button_MultiLine)
    TextGadget(#Undo_txt,225-xx,384,80,16,"Ctrl z = Undo");,#PB_Text_Border )
    
    TextGadget(#CmntChars_txt, 320-xxx, 350, 35, 16, "Chars:", #PB_Text_Center)
    StringGadget(#CmntChars_str, 320-xxx, 365, 35, 16, #DefaultCommentString)
    ButtonGadget(#CmntLines_btn, 360-xxx, 352, 74, 32, "Comment/Un Lines", #PB_Button_MultiLine)
    TextGadget(#CmntLines_txt, 350-xxx, 384, 120, 16, "Lines UnCommented");,#PB_Text_Border )
    
    ButtonGadget(#Cpy2ClpBd_btn, 434, 352, 58, 32, "Copy to Clipboard", #PB_Button_MultiLine)
    ButtonGadget(#SaveFile, 499, 352, 54, 32, "Save as File", #PB_Button_MultiLine)
    ButtonGadget(#Exit_btn, 565, 355, 40, 32, "Exit")
    
    DisableGadget(#SaveFile,1)
    
    Font1 = LoadFont(#PB_Any, "Lucida Console"  ,  8)
    ;Both EditorGadgets are set to the same dimentions.
    ;See the notes by the comment button code below, to see why and how they are both needed and used.
    EditorGadget(#PreCommentEditor, 621, 2, 592, 335)
    W=592 ;Window width
    H=335 ;Window height
    LM=25 ;Left margin
    TM=0 ;Top margin
    RM=W-LM ;Right margin (and wordwrap if enabled)
    BM=H ;Bottom margin
    Editor2Rect.RECT\left = LM
    Editor2Rect\top = TM
    Editor2Rect\right = RM
    Editor2Rect\bottom = BM
    SendMessage_(GadgetID(#PreCommentEditor),#EM_SETRECT,0,Editor2Rect)
    SendMessage_(GadgetID(#PreCommentEditor), #EM_SETTARGETDEVICE, 0, 0)
    SetGadgetFont(#PreCommentEditor, FontID(Font1))
    ;For testing the #PreCommentEditor window, comment out this next line.
    HideGadget(#PreCommentEditor, 1)
    
    ContainerGadget(#Container_1, 6, 1, 600, 345, #PB_Container_Raised)
    EditorGadget(#CommentEditor, 1, 2, 592, 335)
    W=592 ;Window width
    H=335 ;Window height
    LM=25 ;Left margin
    TM=0 ;Top margin
    RM=W-LM ;Right margin (and wordwrap if enabled)
    BM=H ;Bottom margin
    EditorRect.RECT\left = LM
    EditorRect\top = TM
    EditorRect\right = RM
    EditorRect\bottom = BM
    SendMessage_(GadgetID(#CommentEditor),#EM_SETRECT,0,EditorRect)
    SendMessage_(GadgetID(#CommentEditor), #EM_SETTARGETDEVICE, 0, 0)
  CloseGadgetList()
    ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
    SetGadgetFont(#CommentEditor, FontID(Font1))
    
    If CreatePopupMenu(#POPUP)
      MenuItem(1, "Copy")
      MenuItem(2, "Paste")
      MenuItem(3, "Cut")
    EndIf
    
    ;This section is used to disable Ctrl V, Ctrl C, and Ctrl X, so you have to use pop up menu
    AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_V, #Ctrl_v);dissable Ctrl V so you have to use pop up menu
    AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_C, #Ctrl_C);dissable Ctrl C so you have to use pop up menu
    AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_X, #Ctrl_x);dissable Ctrl X so you have to use pop up menu
    
  EndIf
EndProcedure

OpenWindow_CommentWin()

comment=0

;{- Event loop
;-Event loop
Repeat
  
  ;NOTE 1:  This part keeps original pasted text when you use Ctrl z or undo, up to the original edit.
  lc = CountGadgetItems(#CommentEditor)
  If lc=0 Or Trim(GetGadgetText(#CommentEditor))=""
    ClearGadgetItems(#CommentEditor)
    SetGadgetText(#CommentEditor, OrgText$)
  EndIf
  
  Event = WaitWindowEvent(5)
  Select Event
    
  Case #WM_RBUTTONDOWN
    ;Debug "RBUTTON"
    GetCursorPos_(@point)
    DisplayPopupMenu(#POPUP, WindowID(#CommentWin))
    
  Case #PB_Event_Gadget
    EventGadget = EventGadget()
    EventType = EventType()
    
    If EventGadget = #Container_1
    ElseIf EventGadget = #CommentEditor
      
    ElseIf EventGadget = #ClrTxt_btn  ;clear the text window
      ClearGadgetItems(#CommentEditor)
      ;set CommentEditor to normal margins
      W=592 ;Window width
      H=335 ;Window height
      LM=25 ;Left margin
      TM=0 ;Top margin
      RM=W-LM ;Right margin (and wordwrap if enabled)
      BM=H ;Bottom margin
      EditorRect.RECT\left = LM
      EditorRect\top = TM
      EditorRect\right = RM
      EditorRect\bottom = BM
      SendMessage_(GadgetID(#CommentEditor),#EM_SETRECT,0,EditorRect)
      SendMessage_(GadgetID(#CommentEditor), #EM_SETTARGETDEVICE, 0, 0)
      comment=0
      SetGadgetText(#CmntLines_txt,"Lines UnCommented")      
      OrgText$=""      
      
    ElseIf EventGadget= #SaveFile ; SAVE TO A TXTFILE ++++++++++++++++++++
      RunProgram("notepad.exe")
      DisableGadget(#SaveFile,1)
      
    ElseIf EventGadget = #CpyFromClpBd_btn  ;paste clipboard text into edit window
      Text$ = GetClipboardText()
      If FindString(Text$,"\qc",1)>0
        Text$=ReplaceString(Text$,"\qc","",1)
        ;"\qc" is the rtf command for centering text. 
        ;If pasting from some rtf text that has some text centered, this removes it 
        ;from the text so it won't mess with your editing.
      EndIf
      
      If Trim(Text$)<>""
        lc = CountGadgetItems(#CommentEditor)
        If lc=0 Or Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
          ClearGadgetItems(#CommentEditor)
          SetGadgetText(#CommentEditor, Text$)
          OrgText$=Text$
        Else
          SendMessage_(GadgetID(#CommentEditor), #WM_PASTE,0,0) ;this method allows you to choose where you want to paste the text.
        EndIf
      EndIf
      
    ElseIf EventGadget = #Cut_btn
      SendMessage_(GadgetID(#CommentEditor), #WM_COPY,0,0)
      SendMessage_(GadgetID(#CommentEditor), #WM_CUT,0,0)
      
    ElseIf EventGadget = #Copy_btn
      SendMessage_(GadgetID(#CommentEditor), #WM_COPY,0,0)
      
    ElseIf EventGadget = #Paste_btn
      Font1 = LoadFont(#PB_Any, "Lucida Console"  ,  8)
      ;Paste text from clipboard
      CopyCBText$ = GetClipboardText()                 ;Get the text from the clipboard
      If Trim(CopyCBText$)<>""
        If Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
          SetGadgetText(#CommentEditor,CopyCBText$)
          ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
          SetGadgetFont(#CommentEditor, FontID(Font1))
          OrgText$=GetGadgetText(#CommentEditor)
        Else
          SendMessage_(GadgetID(#CommentEditor), #WM_CLEAR,0,0)   ;If text is selected in destination, remove it
          SendMessage_(GadgetID(#CommentEditor), #WM_PASTE,0,0)   ;Paste the clipboard text at cursor posn
          ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
          SetGadgetFont(#CommentEditor, FontID(Font1))
        EndIf
        ;EndIf
        SetActiveGadget(#CommentEditor)
      EndIf
      
      
    ElseIf EventGadget = #CmntLines_btn ;comment the lines
      CommentString$ = GetGadgetText(#CmntChars_str)
      CommentLen = Len(CommentString$)
      comment=comment+1
      If comment=1
        
        ; This whole part works in an unusual and unique way when you Comment and
        ; Uncomment the lines. Word wrap is accomplished by the margin settings.
        
        ; When you select Comment. It copies line by line, from the Comment Editor
        ; window, removing any found Chr(10)'s and Chr(13)'s, from each line. And then
        ; and copies each line with a Chr(10) at the end of each line to the PreComment
        ; Editor window.
        ; This removes any line continuations from the text, caused by pasting and
        ; editing and the action of the margin adjustments.
        ; Then the Comment Editor window is cleared, and then its margin is decreased
        ; by the length of the comments character your using * 5.  (min = 5 or @ 5
        ; characters max can be used for comments.  Default is "; ")
        ; Then the lines from the PreComment Editor are copied with the comment
        ; characters added and copied line by line to the Comment Editor window.
        
        ; When you select UnComment, it just removes the added comment characters and
        ; sets the Comment Editor window margins back to their default setting.
        
        ; It seems to work pretty well, as long as the comment characters your using
        ; are 5 or less. Otherwise you will need to do some additional editing as they
        ; will cause any line that's at the max length to word wrap because the minimum
        ;margin is set to 5.
        
        
        SetGadgetText(#CmntLines_txt,"Lines Commented")
        ClearGadgetItems(#PreCommentEditor)
        lc = CountGadgetItems(#CommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#CommentEditor,x)
          dat$=ReplaceString(dat$,Chr(13)," ",1);have to remove LF's and CR's
          dat$=ReplaceString(dat$,Chr(10)," ",1)
          SetGadgetItemText(#PreCommentEditor,x,dat$+Chr(10))
        Next x
        ClearGadgetItems(#CommentEditor)
        lc = CountGadgetItems(#PreCommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#PreCommentEditor,x)
          SetGadgetItemText(#CommentEditor,x,dat$+Chr(10))
        Next x
        W=592 ;Window width
        H=335 ;Window height
        LM=25-(CommentLen*5) ;Left margin
        If LM<5:LM=5:EndIf
        TM=0 ;Top margin
        RM=W-LM ;Right margin (and wordwrap if enabled)
        BM=H ;Bottom margin
        EditorRect.RECT\left = LM
        EditorRect\top = TM
        EditorRect\right = RM
        EditorRect\bottom = BM
        SendMessage_(GadgetID(#CommentEditor),#EM_SETRECT,0,EditorRect)
        SendMessage_(GadgetID(#CommentEditor), #EM_SETTARGETDEVICE, 0, 0)
        
        lc = CountGadgetItems(#CommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#CommentEditor,x); Get the specified line text.
          If Left(dat$,CommentLen)<>CommentString$ And Trim(dat$)<>"" ;just add comment only if line isn't commented already.
            dat$=CommentString$+dat$
          EndIf
          SetGadgetItemText(#CommentEditor, x, dat$)
        Next x
      Else
        SetGadgetText(#CmntLines_txt,"Lines UnCommented")
        lc = CountGadgetItems(#CommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#CommentEditor,x); Get the specified line text.
          If Left(dat$,CommentLen)=CommentString$ ;just remove comment that was added. Note: existing indented comments are not removed.
            dat$=Mid(dat$,1 + CommentLen)   ; CG: was (...,2)
          EndIf
          SetGadgetItemText(#CommentEditor, x, dat$)
        Next x
        W=592 ;Window width
        H=335 ;Window height
        LM=25 ;Left margin
        TM=0 ;Top margin
        RM=W-LM ;Right margin (and wordwrap if enabled)
        BM=H ;Bottom margin
        EditorRect.RECT\left = LM
        EditorRect\top = TM
        EditorRect\right = RM
        EditorRect\bottom = BM
        SendMessage_(GadgetID(#CommentEditor),#EM_SETRECT,0,EditorRect)
        SendMessage_(GadgetID(#CommentEditor), #EM_SETTARGETDEVICE, 0, 0)
        comment=0
      EndIf
      
    ElseIf EventGadget = #Cpy2ClpBd_btn
      DisableGadget(#SaveFile,0)
      Text$ = GetGadgetText(#CommentEditor)
      ClearClipboard()
      SetClipboardText(Text$)
      
    ElseIf EventGadget = #Exit_btn
      CloseWindow(#CommentWin)
      End
    EndIf
    
  Case #PB_Event_Menu
    ;I could have used the direct ID method like I did above for the Copy Cut and Paste Buttons.
    ;But I'd like to keep this next code here for a known future reference, as it's some useful code.
    gdc_ID = GetDlgCtrlID_(WindowFromPoint_(point\y << 32 + point\x))
    Select EventMenu()
    Case 1  ;copy
      If IsGadget(gdc_ID)
        Gadget_is = GadgetType(gdc_ID)
        If Gadget_is = #PB_GadgetType_Editor
          ;Copy selected text only to clipboard
          SendMessage_(GadgetID(gdc_ID), #WM_COPY,0,0)
        EndIf
      EndIf
      
    Case 2  ;paste
      If IsGadget(gdc_ID)
        Gadget_is = GadgetType(gdc_ID)
        If Gadget_is = #PB_GadgetType_Editor
          Font1 = LoadFont(#PB_Any, "Lucida Console"  ,  8)
          ;Paste text from clipboard
          CopyCBText$ = GetClipboardText()                 ;Get the text from the clipboard
          If FindString(CopyCBText$,"\qc",1)>0
            CopyCBText$ =  ReplaceString(CopyCBText$,"\qc","",1)
            ;"\qc" is the rtf command for centering text.
            ;If pasting from some rtf text that has some text centered, this removes it
            ;from the text so it won't mess with your editing.
          EndIf
          If Trim(CopyCBText$)<>""
            If Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
              SetGadgetText(#CommentEditor,CopyCBText$)
              ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
              SetGadgetFont(#CommentEditor, FontID(Font1))
              OrgText$=GetGadgetText(#CommentEditor)
            Else
              SendMessage_(GadgetID(gdc_ID), #WM_CLEAR,0,0)   ;If text is selected in destination, remove it
              SendMessage_(GadgetID(gdc_ID), #WM_PASTE,0,0)   ;Paste the clipboard text at cursor posn
              ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
              SetGadgetFont(#CommentEditor, FontID(Font1))
            EndIf
            ;EndIf
            SetActiveGadget(#CommentEditor)
          EndIf
        EndIf
      EndIf
      
    Case 3  ;cut
      If IsGadget(gdc_ID)
        Gadget_is = GadgetType(gdc_ID)
        If Gadget_is = #PB_GadgetType_Editor
          ;Copy selected text only to clipboard
          SendMessage_(GadgetID(gdc_ID), #WM_COPY,0,0)
          SendMessage_(GadgetID(gdc_ID), #WM_CUT,0,0)
        EndIf
      EndIf
      
    EndSelect
    
  Case #PB_Event_CloseWindow
    EventWindow = EventWindow()
    If EventWindow = #CommentWin
      CloseWindow(#CommentWin)
      Break
    EndIf
  EndSelect
ForEver
;}
Thanks

yrreti
Last edited by yrreti on Sun Feb 17, 2013 10:31 pm, edited 15 times in total.
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Create Comments Editor

Post by Tenaja »

yrreti wrote:..
But it is a tedious process because you always have to add the ; comment character and edit
each line carefully, while uncaptalizing all the special words that get automatically captilized.
eg: And Or For Select etc
So I wrote this program to make it easier and faster to edit and paste this info, into my code.
Excellent! I have not tried it yet, but this sounds like a great tool--thanks for sharing. I often paste thread notes into code examples, and this will come in handy.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Create Comments Editor

Post by Demivec »

Excellent! I previously had to pasted the lines into Utra-Edit and add the ';' there with column editing before copying it all into a PureBasic source code.


Since you've already covered the basics, I got a few feature requests, all of them low priority.

The first is to be able to select a range of lines to comment/uncomment. This has to be gotten around by doing all text through multiple separate series of copy/paste/comment/copy/paste/clear operations instead of copy/past/comment/uncomment-selected/copy/past/clear. This may be only a specialized need that I have. :wink:

The second is to enhance the undo history. Currently it removes the last ';' from lines sequentially when the previous operation was 'Comment All'. I think it should remove all of the commenting that was performed as part of the group operation.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Create Comments Editor

Post by yrreti »

:wink: Up dated per Demivec's second request. Changed the Comment button to also Un Comment.
Comment comments the lines. Un Comment just removes the leading comment that was added.
(Just click on the Comment/Un again to Comment or Un Comment the lines)
Note: This does not remove existing 'indented' comments in the original text.

There is a draw back to this approach though, in that using Ctrl z to undue does just that.
It undue's just what you just did. So It will comment all the lines again. I don't know how to clear
out the Ctrl z undue buffer to prevent this. But you can at least start with a clean text again.

(The Code is undated in the first post to reflect the changes)
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Create Comments Editor

Post by Kwai chang caine »

Thanks to your works 8)
Excuse me, but i don't understand the difference between your tools, and the CTRL + B for commented a text in the IDE and ALT + B for decommented the selected text ??? :oops:
Just the the special words that get automatically captilized ??
ImageThe happiness is a road...
Not a destination
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Create Comments Editor

Post by yrreti »

Kwaï chang caïne

When you paste several lines in PB's IDE that you want to add as comments in your code.
There are many times that the lines are too long, and you need to cut and paste and adjust.
Then you can highlight the text and use Ctrl b to add the comment ; characters like you
stated. But ! - Suppose the text contains 20 or more special words that get capitalized.
It's a real head ache every time to have to look through the text each time to correct the
changed text. Now suppose you need to add to or edit those comments again. If you forget
to place the ; at the line start, well there you go again. I wrote this tool because I got
fed up dealing with this frustration, and I use it a lot. Because it has been very helpful for
me, I wanted to share it with others on the forum so they could be able to use it too if they
wanted to.
Just for example: Copy this text above directly into the PB IDE editor. Right away you can
see 20 special words of which 19 need to be changed. And you haven't even edited it yet.
Now copy and paste the same text into my comment editor, do what ever editing you need to do,
click the Comment button, click the Copy to Clipboard button, then paste where you want
in your programs code. Your done, and with a lot less frustration. It's just to make dealing
with comments a lot easier.

yrreti
marc_256
Enthusiast
Enthusiast
Posts: 749
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: Create Comments Editor

Post by marc_256 »

Hey yrreti,

This is the tool I need, I go use this every day...

Thanks,
Marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Create Comments Editor

Post by yrreti »

:oops: My apologies. I discovered an intermittent bug because I miss-entered the WindowFromPoint_(point\y << 32 + point\x))
part which caused the pop-up part to intermittently not work. I have corrected the code and cleaned it up a little.
Please replace your code with the new code in the first post. Updated 10/02/12
It seems to work pretty good for me right now. At least I didn't see or observe any more major bug after testing it for awhile.
But if you find any other bugs, or see any needed improvements, please help fix it to make it better. Post your suggested
changes here to make this better for everyones use.
Thank you

yrreti
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Create Comments Editor

Post by Kwai chang caine »

Thanks YRRETI for your long and good explanation :wink:
I have understand now, again thanks for creating and sharing this tools 8)
ImageThe happiness is a road...
Not a destination
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Create Comments Editor

Post by yrreti »

Hi Kwaï chang caïne
Your welcome, and I hope it works well for you.
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Create Comments Editor

Post by Tenaja »

Finally got around to using this with my new pc; it is very handy when pasting regular text (such as code requirements or forum descriptions) into code. Thanks again for it.

My personal preference is to insert a space after the comment character. I also use a few other languages that do not necessarily use the semicolon, so I made the commenting string an editable textbox (defaulting to "; ").

Code: Select all

#DefaultCommentString = "; "

Enumeration
	#CommentWin
EndEnumeration

Enumeration
	#Container_1
	#CommentEditor
	#ClrTxt_btn
	#CpyFromClpBd_btn
	#Cut_btn
	#Copy_btn
	#Paste_btn
	#Undo_txt
	#CmntChars_txt
	#CmntChars_str
	#CmntLines_btn
	#Cpy2ClpBd_btn
	#Exit_btn
	#POPUP
EndEnumeration

Define.l Event, EventWindow, EventGadget, EventType, EventMenu
Global point.POINT


Procedure Editor_Select(Gadget, LineStart.i, CharStart.i, LineEnd.i, CharEnd.i)
	sel.CHARRANGE
	sel\cpMin = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, LineStart, 0) + CharStart - 1
	
	If LineEnd = -1
		LineEnd = SendMessage_(GadgetID(Gadget), #EM_GETLINECOUNT, 0, 0)-1
	EndIf
	sel\cpMax = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, LineEnd, 0)
	
	If CharEnd = -1
		sel\cpMax + SendMessage_(GadgetID(Gadget), #EM_LINELENGTH, sel\cpMax, 0)
	Else
		sel\cpMax + CharEnd - 1
	EndIf
	SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel)
EndProcedure

Procedure OpenWindow_CommentWin()
	If OpenWindow(#CommentWin, 450, 198, 604, 397, Space(65)+"Create Comments Editor", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
		ButtonGadget(#ClrTxt_btn, 8, 355, 40, 32, "Clear Text", #PB_Button_MultiLine)
		ButtonGadget(#CpyFromClpBd_btn, 65, 355, 66, 32, "Copy from Clipboard", #PB_Button_MultiLine)
		
		ButtonGadget(#Copy_btn, 200, 352, 35, 32, "Copy", #PB_Button_MultiLine)
		ButtonGadget(#Paste_btn, 240, 352, 35, 32, "Paste", #PB_Button_MultiLine)
		ButtonGadget(#Cut_btn, 280, 352, 35, 32, "Cut", #PB_Button_MultiLine)
		TextGadget(#Undo_txt,225,384,265,16,"Ctrl z = Undo")
		
		TextGadget(#CmntChars_txt, 335, 355, 35, 16, "Chars:", #PB_Text_Center)
		StringGadget(#CmntChars_str, 335, 370, 35, 16, #DefaultCommentString)
		ButtonGadget(#CmntLines_btn, 381, 355, 70, 32, "Comment/Un Lines", #PB_Button_MultiLine)
		ButtonGadget(#Cpy2ClpBd_btn, 470, 355, 66, 32, "Copy to Clipboard", #PB_Button_MultiLine)
		ButtonGadget(#Exit_btn, 553, 355, 40, 32, "Exit")
		ContainerGadget(#Container_1, 6, 1, 587, 345, #PB_Container_Raised)
		EditorGadget(#CommentEditor, 1, 2, 579, 335)
		CloseGadgetList()
		; Gadget Colors  not necessary, but makes the buttons stand out better.
; 		PureCOLOR_SetButtonColor(#ClrTxt_btn, #PureCOLOR_SystemColor, $C0C0C0)
; 		PureCOLOR_SetButtonColor(#CpyFromClpBd_btn, #PureCOLOR_SystemColor, $C0C0C0) 
; 		PureCOLOR_SetButtonColor(#Copy_btn, #PureCOLOR_SystemColor, $C0C0C0)
; 		PureCOLOR_SetButtonColor(#Paste_btn, #PureCOLOR_SystemColor, $C0C0C0)
; 		PureCOLOR_SetButtonColor(#Cut_btn, #PureCOLOR_SystemColor, $C0C0C0)   
; 		PureCOLOR_SetButtonColor(#CmntLines_btn, #PureCOLOR_SystemColor, $C0C0C0)
; 		PureCOLOR_SetButtonColor(#Cpy2ClpBd_btn, #PureCOLOR_SystemColor, $C0C0C0)
; 		PureCOLOR_SetButtonColor(#Exit_btn, #PureCOLOR_SystemColor, $C0C0C0)
		
		
		
		If CreatePopupMenu(#POPUP)
			MenuItem(1, "Copy")
			MenuItem(2, "Paste")
			MenuItem(3, "Cut")
		EndIf
	EndIf
EndProcedure

OpenWindow_CommentWin()

comment=0

;{- Event loop
Repeat
	;NOTE 1:  This part keeps original pasted text when you use Ctrl z or undo, up to the original edit.
	lc = CountGadgetItems(#CommentEditor)
	If lc=0 Or Trim(GetGadgetText(#CommentEditor))=""
		ClearGadgetItems(#CommentEditor)
		SetGadgetText(#CommentEditor, OrgText$)
	EndIf
	
	Event = WaitWindowEvent(5)
	Select Event
			
		Case #WM_RBUTTONDOWN
			;Debug "RBUTTON"
			GetCursorPos_(@point)
			DisplayPopupMenu(#POPUP, WindowID(#CommentWin))
			
		Case #PB_Event_Gadget
			EventGadget = EventGadget()
			EventType = EventType()
			If EventGadget = #Container_1
			ElseIf EventGadget = #CommentEditor
				
			ElseIf EventGadget = #ClrTxt_btn  ;clear the text window
				ClearGadgetItems(#CommentEditor)
				OrgText$=""
				
			ElseIf EventGadget = #CpyFromClpBd_btn  ;paste clipboard text into edit window
				Text$ = GetClipboardText()
				If Trim(Text$)<>""
					lc = CountGadgetItems(#CommentEditor)
					If lc=0 Or Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
						ClearGadgetItems(#CommentEditor)
						SetGadgetText(#CommentEditor, Text$)
						OrgText$=Text$
					Else
						SendMessage_(GadgetID(#CommentEditor), #WM_PASTE,0,0) ;this method allows you to choose where you want to paste the text.
					EndIf
				EndIf
				
			ElseIf EventGadget = #Cut_btn
				SendMessage_(GadgetID(#CommentEditor), #WM_COPY,0,0)
				SendMessage_(GadgetID(#CommentEditor), #WM_CUT,0,0)
				
			ElseIf EventGadget = #Copy_btn
				SendMessage_(GadgetID(#CommentEditor), #WM_COPY,0,0)
				
			ElseIf EventGadget = #Paste_btn
				Text$ = GetClipboardText()
				If Trim(Text$)<>""
					lc = CountGadgetItems(#CommentEditor)
					If lc=0 Or Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
						ClearGadgetItems(#CommentEditor)
						SetGadgetText(#CommentEditor, Text$)
						OrgText$=Text$
						SetActiveGadget(#CommentEditor)
					Else
						SendMessage_(GadgetID(#CommentEditor), #WM_CLEAR,0,0)   ;If text is selected in destination, remove it
						SendMessage_(GadgetID(#CommentEditor), #WM_PASTE,0,0)   ;Paste the clipboard text at cursor posn
					EndIf
					SetActiveGadget(#CommentEditor)
				EndIf
				
			ElseIf EventGadget = #CmntLines_btn ;comment the lines
				CommentString$ = GetGadgetText(#CmntChars_str)
				CommentLen = Len(CommentString$)
				If comment=0
					lc = CountGadgetItems(#CommentEditor)
					For x=0 To lc
						dat$=GetGadgetItemText(#CommentEditor,x); Get the specified line text.
						If Left(dat$,CommentLen)<>CommentString$ And Trim(dat$)<>"" ;just add comment only if line isn't commented already.
							dat$=CommentString$+dat$
						EndIf
						SetGadgetItemText(#CommentEditor, x, dat$)
					Next x
					comment=1
				Else
					lc = CountGadgetItems(#CommentEditor)     
					For x=0 To lc
						dat$=GetGadgetItemText(#CommentEditor,x); Get the specified line text.
						If Left(dat$,CommentLen)=CommentString$ ;just remove comment that was added. This does not remove existing indented comments in the original text.
							dat$=Mid(dat$,1 + CommentLen)	; CG: was (...,2)
						EndIf
						SetGadgetItemText(#CommentEditor, x, dat$)
					Next x
					comment=0
				EndIf
				
			ElseIf EventGadget = #Cpy2ClpBd_btn
				Text$ = GetGadgetText(#CommentEditor)
				ClearClipboard()
				SetClipboardText(Text$)
				
			ElseIf EventGadget = #Exit_btn
				CloseWindow(#CommentWin)
				End
			EndIf
			
		Case #PB_Event_Menu
			gdc_ID = GetDlgCtrlID_(WindowFromPoint_(point\y << 32 + point\x))
			Select EventMenu()
				Case 1  ;copy
					If IsGadget(gdc_ID)
						Gadget_is = GadgetType(gdc_ID)
						If Gadget_is = #PB_GadgetType_Editor
							;Copy selected text only to clipboard
							SendMessage_(GadgetID(gdc_ID), #WM_COPY,0,0)
						EndIf
					EndIf
					
				Case 2  ;paste
					If IsGadget(gdc_ID)
						Gadget_is = GadgetType(gdc_ID)
						If Gadget_is = #PB_GadgetType_Editor
							;Paste text from clipboard
							CopyCBText$ = GetClipboardText()                 ;Get the text from the clipboard
							If Trim(CopyCBText$)<>""
								lc = CountGadgetItems(#CommentEditor)
								If lc=0 Or Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
									ClearGadgetItems(#CommentEditor)
									SetGadgetText(#CommentEditor, CopyCBText$)
									OrgText$=CopyCBText$           
								Else
									SendMessage_(GadgetID(gdc_ID), #WM_CLEAR,0,0)   ;If text is selected in destination, remove it
									SendMessage_(GadgetID(gdc_ID), #WM_PASTE,0,0)   ;Paste the clipboard text at cursor posn
								EndIf
								SetActiveGadget(#CommentEditor)
							EndIf
						EndIf
					EndIf
					
				Case 3  ;cut
					If IsGadget(gdc_ID)
						Gadget_is = GadgetType(gdc_ID)
						If Gadget_is = #PB_GadgetType_Editor
							;Copy selected text only to clipboard
							SendMessage_(GadgetID(gdc_ID), #WM_COPY,0,0)
							SendMessage_(GadgetID(gdc_ID), #WM_CUT,0,0)
						EndIf
					EndIf
			EndSelect
			
		Case #PB_Event_CloseWindow
			EventWindow = EventWindow()
			If EventWindow = #CommentWin
				CloseWindow(#CommentWin)
				Break
			EndIf
	EndSelect
ForEver
;}

Edited to remove test$ typos.
Last edited by Tenaja on Mon Feb 11, 2013 4:47 pm, edited 1 time in total.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Create Comments Editor

Post by yrreti »

Hi Tenaja

That was a nice idea and a good addition. I especially like the idea of being able to quickly change the
character you need when using with other languages, and to be able to insert a space after the comment
character if you like as well. Good idea, and thanks for sharing your modifications.
I'm also very glad that the program has been useful for you too. I use it all the time myself, and that's
why I wanted to share it with others on this forum.

yrreti
Lubos
Enthusiast
Enthusiast
Posts: 167
Joined: Tue Feb 03, 2004 12:32 am
Contact:

Re: Create Comments Editor

Post by Lubos »

Nice work. Thanks yrreti. I'd like two more buttons: Copy from file + Copy to file. Sometimes it can be handy. I can probably do that, but the author can do better. :wink:
Windows 7 Professional / Service Pack 1 - 32bit, PureBasic 5.46 LTS (x86)
My mother tongue is Czech. I have a Czech version of Windows.
Who is not afraid of GOTO, the one need not afraid any things!
Lubos
Enthusiast
Enthusiast
Posts: 167
Joined: Tue Feb 03, 2004 12:32 am
Contact:

Re: Create Comments Editor

Post by Lubos »

I studied a bit of source code. I don´t understand using variable Test$ (line 145 and line 165 or line 121 and 141). I would expect there Text$. I may have overlooked something. :(
Windows 7 Professional / Service Pack 1 - 32bit, PureBasic 5.46 LTS (x86)
My mother tongue is Czech. I have a Czech version of Windows.
Who is not afraid of GOTO, the one need not afraid any things!
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Create Comments Editor - Updated

Post by yrreti »

Note: Leaving this Reply here for reference.
But I decided to always put the latest code that I am using in the first post on page 1 to lessen confusion.
Thanks
yrreti


Hi Lubos

I'm afraid I don't really understand your question? Only line 121 and 141 line up close to the
string text$, which is used to hold the text from the clipboard and it saves a copy in OrgText$=Test$,
so you could go back to your original posted text by using Ctrl z (undo) if you needed. In line 176 and
178. text$ is used to transfer the text from the comment editor window to the clipboard so you can paste
the commented text where you want too.

Aside from that. I have made some changes to the program, because I wanted to have it automatically
handle word wrapping on long lines and during editing, so I wouldn't have to manually have to do it.
I found out that I couldn't do it using PB5.10's #PB_Editor_WordWrap
or SendMessage_(GadgetID(#Editor_1),#EM_SETTARGETDEVICE,#Null,0), as they only worked on complete
text transfers, but dropped words on line by line.

The way the following code works, is in a kind of an unusual and unique way when you
Comment and Uncomment the lines. Word wrap is accomplished by the margin
settings.

When you select Comment. It copies line by line, from the Comment Editor
window, removing any found Chr(10)'s and Chr(13)'s, from each
line. And then and copies each line with a Chr(10) at the end of each line to
the PreComment Editor window.
This removes any line continuations from the text, caused by pasting and
editing and the action of the margin adjustments.
Then the Comment Editor window is cleared, and then its margin is
decreased by the length of the comments character your using * 5.
(min = 5 or @ 5 characters max can be used for comments. Default is "; ")
Then the lines from the PreComment Editor are copied with the comment characters added and copied line by line to the Comment Editor window.

When you select UnComment, it just removes the added comment
characters and sets the Comment Editor window margins back to their default
setting.

It seems to work pretty well, as long as the comment characters your using
are 5 or less. Otherwise you will need to do some additional editing as they
will cause any line that's at the max length to word wrap because the minimum margin is set to 5.

Any way you have all the different sources here to decide and change to, or to change as you need to your liking.
Some credit would be appreciated though. And with that, I do like Tenaja idea that he had, which is added in too.

Code: Select all


#DefaultCommentString = "; "

Enumeration
  #CommentWin
EndEnumeration

Enumeration
  #PreCommentEditor
  #Container_1
  #CommentEditor
  #ClrTxt_btn
  #CpyFromClpBd_btn
  #Cut_btn
  #Copy_btn
  #Paste_btn
  #Undo_txt
  #CmntChars_txt
  #CmntChars_str
  #CmntLines_btn
  #CmntLines_txt
  #Cpy2ClpBd_btn
  #Exit_btn
  #POPUP
  #Ctrl_v;disable these to allow only copy, paste, and cut functions through the pop up menu
  #Ctrl_C;so it uses the paste input formating there.
  #Ctrl_x
  
  #CtrlAltv;To display version info
EndEnumeration

Define.l Event, EventWindow, EventGadget, EventType, EventMenu
Global point.POINT

;-
Procedure OpenWindow_CommentWin()
  ;First line was used for testing, so I could see what was happening in the #PreCommentEditor window.
  ;If OpenWindow(#CommentWin, 10, 198, 1230, 397, Space(65)+"Create Comments Editor", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
  If OpenWindow(#CommentWin, 450, 198, 615, 397, Space(65)+"Create Comments Editor", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_WindowCentered)
    ButtonGadget(#ClrTxt_btn, 8, 355, 40, 32, "Clear Text", #PB_Button_MultiLine)
    ButtonGadget(#CpyFromClpBd_btn, 65, 352, 66, 32, "Copy from Clipboard", #PB_Button_MultiLine)
    ButtonGadget(#Copy_btn, 200, 352, 35, 32, "Copy", #PB_Button_MultiLine)
    ButtonGadget(#Paste_btn, 240, 352, 35, 32, "Paste", #PB_Button_MultiLine)
    ButtonGadget(#Cut_btn, 280, 352, 35, 32, "Cut", #PB_Button_MultiLine)
    TextGadget(#Undo_txt,225,384,80,16,"Ctrl z = Undo");,#PB_Text_Border )
    
    TextGadget(#CmntChars_txt, 335, 355, 35, 16, "Chars:", #PB_Text_Center)
    StringGadget(#CmntChars_str, 335, 370, 35, 16, #DefaultCommentString)
    ButtonGadget(#CmntLines_btn, 381, 352, 74, 32, "Comment/Un Lines", #PB_Button_MultiLine)
    TextGadget(#CmntLines_txt, 371, 384, 120, 16, "Lines UnCommented");,#PB_Text_Border )
    
    ButtonGadget(#Cpy2ClpBd_btn, 480, 352, 66, 32, "Copy to Clipboard", #PB_Button_MultiLine)
    ButtonGadget(#Exit_btn, 565, 355, 40, 32, "Exit")
    
    Font1 = LoadFont(#PB_Any, "Lucida Console"  ,  8)
    ;Both EditorGadgets are set to the same dimentions.
    ;See the notes by the comment button code below, to see why and how they are both needed and used.
    EditorGadget(#PreCommentEditor, 621, 2, 592, 335)
    W=592 ;Window width
    H=335 ;Window height
    LM=25 ;Left margin
    TM=0 ;Top margin
    RM=W-LM ;Right margin (and wordwrap if enabled)
    BM=H ;Bottom margin
    Editor2Rect.RECT\left = LM
    Editor2Rect\top = TM
    Editor2Rect\right = RM
    Editor2Rect\bottom = BM
    SendMessage_(GadgetID(#PreCommentEditor),#EM_SETRECT,0,Editor2Rect)
    SendMessage_(GadgetID(#PreCommentEditor), #EM_SETTARGETDEVICE, 0, 0)
    SetGadgetFont(#PreCommentEditor, FontID(Font1))
    ;For testing the #PreCommentEditor window, comment out this next line.
    HideGadget(#PreCommentEditor, 1)
    
    ContainerGadget(#Container_1, 6, 1, 600, 345, #PB_Container_Raised)
    EditorGadget(#CommentEditor, 1, 2, 592, 335)
    W=592 ;Window width
    H=335 ;Window height
    LM=25 ;Left margin
    TM=0 ;Top margin
    RM=W-LM ;Right margin (and wordwrap if enabled)
    BM=H ;Bottom margin
    EditorRect.RECT\left = LM
    EditorRect\top = TM
    EditorRect\right = RM
    EditorRect\bottom = BM
    SendMessage_(GadgetID(#CommentEditor),#EM_SETRECT,0,EditorRect)
    SendMessage_(GadgetID(#CommentEditor), #EM_SETTARGETDEVICE, 0, 0)
  CloseGadgetList()
    ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
    SetGadgetFont(#CommentEditor, FontID(Font1))
    
    If CreatePopupMenu(#POPUP)
      MenuItem(1, "Copy")
      MenuItem(2, "Paste")
      MenuItem(3, "Cut")
    EndIf
    
    ;This section is used to disable Ctrl V, Ctrl C, and Ctrl X, so you have to use pop up menu
    AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_V, #Ctrl_v);dissable Ctrl V so you have to use pop up menu
    AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_C, #Ctrl_C);dissable Ctrl C so you have to use pop up menu
    AddKeyboardShortcut(0, #PB_Shortcut_Control|#PB_Shortcut_X, #Ctrl_x);dissable Ctrl X so you have to use pop up menu
 
  EndIf
EndProcedure

OpenWindow_CommentWin()

comment=0

;{- Event loop
;-Event loop
Repeat
    
  ;NOTE 1:  This part keeps original pasted text when you use Ctrl z or undo, up to the original edit.
  lc = CountGadgetItems(#CommentEditor)
  If lc=0 Or Trim(GetGadgetText(#CommentEditor))=""
    ClearGadgetItems(#CommentEditor)
    SetGadgetText(#CommentEditor, OrgText$)
  EndIf
  
  Event = WaitWindowEvent(5)
  Select Event
    
  Case #WM_RBUTTONDOWN
    ;Debug "RBUTTON"
    GetCursorPos_(@point)
    DisplayPopupMenu(#POPUP, WindowID(#CommentWin))
    
  Case #PB_Event_Gadget
    EventGadget = EventGadget()
    EventType = EventType()
    If EventGadget = #Container_1
    ElseIf EventGadget = #CommentEditor
      
    ElseIf EventGadget = #ClrTxt_btn  ;clear the text window
      ClearGadgetItems(#CommentEditor)
      OrgText$=""
      
    ElseIf EventGadget = #CpyFromClpBd_btn  ;paste clipboard text into edit window
      Text$ = GetClipboardText()
      If Trim(Text$)<>""
        lc = CountGadgetItems(#CommentEditor)
        If lc=0 Or Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
          ClearGadgetItems(#CommentEditor)
          SetGadgetText(#CommentEditor, Text$)
          OrgText$=Test$
        Else
          SendMessage_(GadgetID(#CommentEditor), #WM_PASTE,0,0) ;this method allows you to choose where you want to paste the text.
        EndIf
      EndIf
      
    ElseIf EventGadget = #Cut_btn
      SendMessage_(GadgetID(#CommentEditor), #WM_COPY,0,0)
      SendMessage_(GadgetID(#CommentEditor), #WM_CUT,0,0)
      
    ElseIf EventGadget = #Copy_btn
      SendMessage_(GadgetID(#CommentEditor), #WM_COPY,0,0)
      
    ElseIf EventGadget = #Paste_btn
      Font1 = LoadFont(#PB_Any, "Lucida Console"  ,  8)
      ;Paste text from clipboard
      CopyCBText$ = GetClipboardText()                 ;Get the text from the clipboard
      If Trim(CopyCBText$)<>""
        If Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
          SetGadgetText(#CommentEditor,CopyCBText$)
          ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
          SetGadgetFont(#CommentEditor, FontID(Font1))
          OrgText$=GetGadgetText(#CommentEditor)
        Else
          SendMessage_(GadgetID(#CommentEditor), #WM_CLEAR,0,0)   ;If text is selected in destination, remove it
          SendMessage_(GadgetID(#CommentEditor), #WM_PASTE,0,0)   ;Paste the clipboard text at cursor posn
          ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
          SetGadgetFont(#CommentEditor, FontID(Font1))
        EndIf
        ;EndIf
        SetActiveGadget(#CommentEditor)
      EndIf
      
      
    ElseIf EventGadget = #CmntLines_btn ;comment the lines
      CommentString$ = GetGadgetText(#CmntChars_str)
      CommentLen = Len(CommentString$)
      comment=comment+1
      If comment=1
        
        ; This whole part works in an unusual and unique way when you Comment and
        ; Uncomment the lines. Word wrap is accomplished by the margin settings.
        
        ; When you select Comment. It copies line by line, from the Comment Editor
        ; window, removing any found Chr(10)'s and Chr(13)'s, from each line. And then
        ; and copies each line with a Chr(10) at the end of each line to the PreComment
        ; Editor window.
        ; This removes any line continuations from the text, caused by pasting and
        ; editing and the action of the margin adjustments.
        ; Then the Comment Editor window is cleared, and then its margin is decreased
        ; by the length of the comments character your using * 5.  (min = 5 or @ 5
        ; characters max can be used for comments.  Default is "; ")
        ; Then the lines from the PreComment Editor are copied with the comment
        ; characters added and copied line by line to the Comment Editor window.
        
        ; When you select UnComment, it just removes the added comment characters and
        ; sets the Comment Editor window margins back to their default setting.
        
        ; It seems to work pretty well, as long as the comment characters your using
        ; are 5 or less. Otherwise you will need to do some additional editing as they
        ; will cause any line that's at the max length to word wrap because the minimum
        ;margin is set to 5.
        
        
        SetGadgetText(#CmntLines_txt,"Lines Commented")
        ClearGadgetItems(#PreCommentEditor)
        lc = CountGadgetItems(#CommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#CommentEditor,x)
          dat$=ReplaceString(dat$,Chr(13)," ",1);have to remove LF's and CR's
          dat$=ReplaceString(dat$,Chr(10)," ",1)
          SetGadgetItemText(#PreCommentEditor,x,dat$+Chr(10))
        Next x
        ClearGadgetItems(#CommentEditor)
        lc = CountGadgetItems(#PreCommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#PreCommentEditor,x)
          SetGadgetItemText(#CommentEditor,x,dat$+Chr(10))
        Next x
        W=592 ;Window width
        H=335 ;Window height
        LM=25-(CommentLen*5) ;Left margin
        If LM<5:LM=5:EndIf
        TM=0 ;Top margin
        RM=W-LM ;Right margin (and wordwrap if enabled)
        BM=H ;Bottom margin
        EditorRect.RECT\left = LM
        EditorRect\top = TM
        EditorRect\right = RM
        EditorRect\bottom = BM
        SendMessage_(GadgetID(#CommentEditor),#EM_SETRECT,0,EditorRect)
        SendMessage_(GadgetID(#CommentEditor), #EM_SETTARGETDEVICE, 0, 0)
        
        lc = CountGadgetItems(#CommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#CommentEditor,x); Get the specified line text.
          If Left(dat$,CommentLen)<>CommentString$ And Trim(dat$)<>"" ;just add comment only if line isn't commented already.
            dat$=CommentString$+dat$
          EndIf
          SetGadgetItemText(#CommentEditor, x, dat$)
        Next x
      Else
        SetGadgetText(#CmntLines_txt,"Lines UnCommented")
        lc = CountGadgetItems(#CommentEditor)
        For x=0 To lc-1;  -1 because  lc is numbered from 1, where as the EditorGadget items are numbered from 0.
          dat$=GetGadgetItemText(#CommentEditor,x); Get the specified line text.
          If Left(dat$,CommentLen)=CommentString$ ;just remove comment that was added. Note: existing indented comments are not removed.
            dat$=Mid(dat$,1 + CommentLen)   ; CG: was (...,2)
          EndIf
          SetGadgetItemText(#CommentEditor, x, dat$)
        Next x
        W=592 ;Window width
        H=335 ;Window height
        LM=25 ;Left margin
        TM=0 ;Top margin
        RM=W-LM ;Right margin (and wordwrap if enabled)
        BM=H ;Bottom margin
        EditorRect.RECT\left = LM
        EditorRect\top = TM
        EditorRect\right = RM
        EditorRect\bottom = BM
        SendMessage_(GadgetID(#CommentEditor),#EM_SETRECT,0,EditorRect)
        SendMessage_(GadgetID(#CommentEditor), #EM_SETTARGETDEVICE, 0, 0)
        comment=0
      EndIf
      
    ElseIf EventGadget = #Cpy2ClpBd_btn
      Text$ = GetGadgetText(#CommentEditor)
      ClearClipboard()
      SetClipboardText(Text$)
      
    ElseIf EventGadget = #Exit_btn
      CloseWindow(#CommentWin)
      End
    EndIf
    
  Case #PB_Event_Menu
    ;I could have used the direct ID method like I did above for the Copy Cut and Paste Buttons.
    ;But I'd like to keep this next code here for a known future reference, as it's some useful code.
    gdc_ID = GetDlgCtrlID_(WindowFromPoint_(point\y << 32 + point\x))
    Select EventMenu()
    Case 1  ;copy
      If IsGadget(gdc_ID)
        Gadget_is = GadgetType(gdc_ID)
        If Gadget_is = #PB_GadgetType_Editor
          ;Copy selected text only to clipboard
          SendMessage_(GadgetID(gdc_ID), #WM_COPY,0,0)
        EndIf
      EndIf
      
    Case 2  ;paste
      If IsGadget(gdc_ID)
        Gadget_is = GadgetType(gdc_ID)
        If Gadget_is = #PB_GadgetType_Editor
          Font1 = LoadFont(#PB_Any, "Lucida Console"  ,  8)
          ;Paste text from clipboard
          CopyCBText$ = GetClipboardText()                 ;Get the text from the clipboard
          If Trim(CopyCBText$)<>""
            If Trim(GetGadgetText(#CommentEditor))="" ;see NOTE 1:
              SetGadgetText(#CommentEditor,CopyCBText$)
              ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
              SetGadgetFont(#CommentEditor, FontID(Font1))
              OrgText$=GetGadgetText(#CommentEditor)
            Else
              SendMessage_(GadgetID(gdc_ID), #WM_CLEAR,0,0)   ;If text is selected in destination, remove it
              SendMessage_(GadgetID(gdc_ID), #WM_PASTE,0,0)   ;Paste the clipboard text at cursor posn
              ; If you don't set the Font after the paste, the pasted text will be the font it was pasted with.
              SetGadgetFont(#CommentEditor, FontID(Font1))
            EndIf
            ;EndIf
            SetActiveGadget(#CommentEditor)
          EndIf
        EndIf
      EndIf
      
    Case 3  ;cut
      If IsGadget(gdc_ID)
        Gadget_is = GadgetType(gdc_ID)
        If Gadget_is = #PB_GadgetType_Editor
          ;Copy selected text only to clipboard
          SendMessage_(GadgetID(gdc_ID), #WM_COPY,0,0)
          SendMessage_(GadgetID(gdc_ID), #WM_CUT,0,0)
        EndIf
      EndIf
      
    EndSelect
    
  Case #PB_Event_CloseWindow
    EventWindow = EventWindow()
    If EventWindow = #CommentWin
      CloseWindow(#CommentWin)
      Break
    EndIf
  EndSelect
ForEver
;}
Last edited by yrreti on Tue Feb 12, 2013 2:33 pm, edited 1 time in total.
Post Reply