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
;}
yrreti