;- Top ;Norm's simple text editor ;created 9 April 2007 ;modified 12 April 2007 ;to add in external cool things #IncPath$="C:\Program Files\PB42\Projects\Dialogs\" IncludeFile #IncPath$+"procs.pbi" IncludeFile #IncPath$+"dialogs.pbi" IncludeFile #IncPath$+"rtf.pbi" IncludeFile #IncPath$+"myrich.pbi" ;- Constants Enumeration #File_New=1 #File_Load #File_Save #File_As #File_Exit #Edit_Cut #Edit_copy #Edit_Paste #Edit_Undo #Edit_All #Edit_Search #Edit_Again #Text_Font #Text_Color #Text_Bold #Text_Italics #Text_Underline #Text_Wrap #Text_NoWrap #Help_About #Help_Help #STAT1 #Editor EndEnumeration ;- OpenWindow winflag.l=#PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered If OpenWindow(0,0,0,400,300,"Norm's Text Editor",winflag) width.l=WindowWidth(0) height.l=WindowHeight(0) ;create the menu If CreateMenu(0, WindowID(0)) MenuTitle("&File") MenuItem( #File_New,"&New") MenuItem( #File_Load, "&Load...") MenuItem( #File_Save, "&Save") MenuItem( #File_As, "Save &As...") MenuBar() MenuItem( #File_Exit, "E&xit") MenuTitle("&Edit") MenuItem( #Edit_Cut, "Cu&t"+Chr(9)+"Ctrl + X") MenuItem( #Edit_Copy, "&Copy"+Chr(9)+"Ctrl + C") MenuItem( #Edit_Paste, "&Paste"+Chr(9)+"Ctrl + V") MenuBar() MenuItem( #Edit_Undo,"&Undo"+Chr(9)+"Ctrl + Z") MenuItem( #Edit_All,"Sellect &All") MenuItem( #Edit_Search,"&Find") MenuItem( #Edit_Again,"Find &Next"+Chr(9)+"F3") MenuTitle("&Text") MenuItem( #Text_Font,"&Font") MenuItem( #Text_Color,"&Color") MenuItem( #Text_Bold ,"&Bold") MenuItem( #Text_Italics,"&Italics") MenuItem( #Text_Underline,"&Underline") MenuBar() MenuItem( #Text_Wrap,"&Word Wrap") MenuItem( #Text_NoWrap,"&No line wrap") MenuTitle("&Help") MenuItem( #Help_Help,"&Help"+Chr(9)+"F1") MenuItem( #Help_About, "&About") EndIf ;- create tool bar If CreateToolBar(0, WindowID(0)) ToolBarStandardButton(#File_New, #PB_ToolBarIcon_New) ToolBarStandardButton(#File_Load, #PB_ToolBarIcon_Open) ToolBarStandardButton(#File_Save, #PB_ToolBarIcon_Save) ToolBarSeparator() ToolBarStandardButton(#Edit_Cut,#PB_ToolBarIcon_Cut) ToolBarStandardButton(#Edit_Copy,#PB_ToolBarIcon_Copy) ToolBarStandardButton(#Edit_Paste,#PB_ToolBarIcon_Paste) ToolBarStandardButton(#Edit_Undo,#PB_ToolBarIcon_Undo) ToolBarSeparator() ToolBarStandardButton(#Edit_Search,#PB_ToolBarIcon_Find) ToolBarSeparator() ToolBarStandardButton(#Text_Font,#PB_ToolBarIcon_Properties) ;special toolbars Bold_UP=CatchImage(#PB_Any,?BoldUP) ResizeImage(Bold_Up,16,16) Bold_DN=CatchImage(#PB_Any,?BoldDN) ResizeImage(Bold_DN,16,16) Italics_UP=CatchImage(#PB_Any,?ItalicsUP) ResizeImage(Italics_Up,16,16) Italics_DN=CatchImage(#PB_Any,?ItalicsDN) ResizeImage(Italics_DN,16,16) ULine_UP=CatchImage(#PB_Any,?ULineUP) ResizeImage(ULine_Up,16,16) ULine_DN=CatchImage(#PB_Any,?ULineDN) ResizeImage(ULine_DN,16,16) TColor=CatchImage(#PB_Any,?ColorIcon) ToolBarImageButton(#Text_Color,ImageID(TColor)) ToolBarSeparator() ToolBarImageButton(#Text_Bold,ImageID(Bold_UP),#PB_ToolBar_Toggle) ToolBarImageButton(#Text_Italics,ImageID(Italics_UP),#PB_ToolBar_Toggle) ToolBarImageButton(#Text_UnderLine,ImageID(ULine_UP),#PB_ToolBar_Toggle) EndIf AddKeyboardShortcut(0, #PB_Shortcut_F3, #Edit_Again) AddKeyboardShortcut(0, #PB_Shortcut_F1, #Help_Help) height=height-MenuHeight()-ToolBarHeight(0) top.l=MenuHeight()+10 ;- statusbar If CreateStatusBar(#STAT1, WindowID(0)) AddStatusBarField(150) StatusBarText(#STAT1, 0, " Line: 1") AddStatusBarField(400) StatusBarText(#STAT1, 1, "") EndIf height=height-StatusBarHeight(#Stat1) ;and the editor gadget If CreateGadgetList(WindowID(0)) EditorGadget(#Editor,0,top,width,height-3) EndIf Else ;No Window? End EndIf PrepareEditor(#Editor,#STAT1) ;- globals Global IsDirty.l=0 Global IsNew.l=1 Global FileName$="Untitled.txt" Global RunPath$=Space(255) Global MyName$ hMod.l = GetModuleHandle_(0) GetModuleFileName_(hMod,@RunPath$,255) MyName$=GetFilePart(RunPath$) RunPath$=GetPathPart(RunPath$) Global DefFN$=RunPath$+"\Untitled.txt" Global Pattern$="Text files (*.txt)|*.txt|All Files (*.*)|*.*" Global OldText$ Global FindText$ Global FoundLine.l Global FoundChar.l Global FontName$="Arial" Global FontSize=10 Global FontID=LoadFont(0, FontName$,FontSize) Global FontFlags Global WFlags ;- procedures Procedure SaveFile() Shared FileName$ Shared IsDirty.l Shared IsNew.l Shared OldText$ If IsNew FN$=SaveFileRequester("Save File As",DefFN$,Pattern$,0) If FN$="" ProcedureReturn EndIf FileName$=FN$ SetWindowTitle(0, FN$) IsNew=0 EndIf If UCase(GetExtensionPart(FileName$))="RTF" If SaveRTF(#Editor, filename$)=1 MessageRequester("TextPad","Unable to save file"+Chr(10)+FileName$,#MB_ICONSTOP) Else OldText$=GetGadgetText(#Editor) IsDirty=0 EndIf Else If CreateFile(1,FileName$) ;lines.l=CountGadgetItems(#Editor)-1 ;For x=0 To lines ; WriteStringN(1,GetGadgetItemText(#Editor,x,0)) ;Next x ;try using raw data .... WriteString(1,GetGadgetText(#Editor) ) CloseFile(1) OldText$=GetGadgetText(#Editor) IsDirty=0 Else MessageRequester("TextPad","Unable to save file"+Chr(10)+FileName$,#MB_ICONSTOP) EndIf EndIf EndProcedure Procedure LoadFile() Shared FileName$ Shared IsDirty.l Shared IsNew.l Shared OldText$ If IsDirty reply=MessageRequester("TextPad","Do you wish to save your changes?",#MB_ICONQUESTION | #PB_MessageRequester_YesNoCancel ) If reply= #PB_MessageRequester_Yes SaveFile() ElseIf reply=#PB_MessageRequester_Cancel ProcedureReturn EndIf EndIf ;see if we can find a file to load fn$=OpenFileRequester("Open a text file",DefFN$,Pattern$,0) If fn$ If UCase(GetExtensionPart(FileName$))="RTF" If LoadRTF(#Editor, filename$)=1 MessageRequester("TextPad","Unable to load file"+Chr(10)+FileName$,#MB_ICONSTOP) Else OldText$=GetGadgetText(#Editor) IsDirty=0 EndIf Else If ReadFile(1,fn$) ;clear the window ClearGadgetItemList(#Editor) ;LN.l=0 ;Repeat ; aline.s=ReadString(1) ; AddGadgetItem(#Editor,LN,aline) ; LN+1 ;Until Eof(1) *memorybuffer=AllocateMemory(100000) Length = ReadData(1, *MemoryBuffer, Lof(1)) SetGadgetText(#Editor,PeekS(*Memorybuffer,length)) CloseFile(1) ;scroll to top FVL.l=SendMessage_(GadgetID(#Editor), #EM_GETFIRSTVISIBLELINE,0,0) If FVL<>0 SendMessage_(GadgetID(#Editor),#EM_LINESCROLL,0,-FVL) EndIf IsDirty=0 IsNew=0 SetWindowTitle(0, FN$) FileName$=FN$ DefFN$=FileName$ OldText$=GetGadgetText(#Editor) Else MessageRequester("Text Pad","Can't open"+Chr(10)+FN$,#MB_ICONSTOP) EndIf EndIf EndIf EndProcedure ;- event loop ;extend the memory area od the editor gadget SendMessage_(GadgetID(#Editor), #EM_EXLIMITTEXT, 0, 64000) Repeat event.l=WindowEvent() If event If event=#PB_Event_Menu MenuID = EventMenu() Select MenuID Case #File_New reply.l=0 If IsDirty reply=MessageRequester("TextPad","Do you wish to save your changes?",#MB_ICONQUESTION | #PB_MessageRequester_YesNoCancel ) If reply= #PB_MessageRequester_Yes SaveFile() EndIf EndIf If reply<> #PB_MessageRequester_Cancel ClearGadgetItemList(#Editor) IsDirty=0 IsNew=1 FileName$="" OldText$="" LastFound=0 DefFN$=RunPath$+"\Untitled.txt" EndIf Case #File_Load LoadFile() LastFound=0 Case #File_Save SaveFile() Case #File_As IsDirty=1 IsNew=1 SaveFile() Case #File_Exit reply.l=0 If IsDirty reply=MessageRequester("TextPad","Do you wish to save your changes?",#MB_ICONQUESTION | #PB_MessageRequester_YesNoCancel ) If reply= #PB_MessageRequester_Yes SaveFile() EndIf EndIf If reply<>#PB_MessageRequester_Cancel event=#PB_Event_CloseWindow EndIf Case #Edit_Cut SendMessage_(GadgetID(#Editor),#WM_CUT,0,0) Case #Edit_copy SendMessage_(GadgetID(#Editor),#WM_COPY,0,0) Case #Edit_Paste SendMessage_(GadgetID(#Editor),#WM_PASTE,0,0) Case #Edit_Undo SendMessage_(GadgetID(#Editor),#WM_UNDO,0,0) Case #Edit_All SendMessage_(GadgetID(#EDitor), #EM_SETSEL, 0, -1) Case #Edit_Search ;- search menu ;SearchText$=InputRequester("TextPad","Text to find",FindText$) ;If SearchText$ ; FindText$=SearchText$ ; ;editor gadget forces us to search line by line ; ;CLn line under mouse, CPs chars under mouse ; CLn2=SendMessage_(GadgetID(#Editor), #EM_LINEFROMCHAR,-1,0) ; FoundChar2=SendMessage_(GadgetID(#Editor), #EM_GETSEL,0,0)&$ffff - SendMessage_(GadgetID(#Editor), #EM_LINEINDEX,-1,0) ; If CLn=CLn2 ; If FoundChar2<=FoundChar ; FoundChar=FoundChar+1 ; Else ; FoundChar=FoundChar2 ; EndIf ; Else ; CLn=CLn2 ; FoundChar=FoundChar2 ; EndIf ; ;LCv top line in list, LC number of lines visible ; LCv=SendMessage_(GadgetID(#Editor), #EM_GETFIRSTVISIBLELINE,0,0) ;where am I? ; LC=SendMessage_(GadgetID(#Editor), #EM_GETLINECOUNT ,0,0) ; count.l=CountGadgetItems(#Editor) ; If CLn>=count-1 ; CLn=0 ; EndIf ; ; ; For n.l=CLn To count-1 ; newtext$=GetGadgetItemText(#Editor,n,0) ; FoundChar.l=FindString(UCase(newtext$),UCase(findtext$),foundchar) ; If FoundChar ; Break ; ; EndIf ; Next n ; ; If n=count-1 ; CLn=0 ; EndIf ; For n.l=CLn To count-1 ; newtext$=GetGadgetItemText(#Editor,n,0) ; FoundChar.l=FindString(UCase(newtext$),UCase(findtext$),FoundChar) ; If FoundChar ; Break ; ; EndIf ; Next n ; ; If n-1 FontColor=newcolor format.CHARFORMAT format\cbSize = SizeOf(CHARFORMAT) format\dwMask = #CFM_COLOR format\crTextColor = FontColor SendMessage_(GadgetID(#Editor), #EM_SETCHARFORMAT, #SCF_SELECTION, @format) EndIf Case #Text_Wrap SetMenuItemState(0,#Text_Wrap,1) SetMenuItemState(0,#Text_NoWrap,0) SendMessage_(GadgetID(#EDITor), #EM_SETTARGETDEVICE, #Null, 0) Case #Text_NoWrap SetMenuItemState(0,#Text_Wrap,0) SetMenuItemState(0,#Text_NoWrap,1) SendMessage_(GadgetID(#EDITor), #EM_SETTARGETDEVICE, #Null, $FFFFFF) Case #Help_About ;msg.s="TextPad"+Chr(10) ;msg=msg+"Build="+Str(#PB_Compiler_Date )+Chr(10) msg.s="By Norman Perry"+Chr(10) msg=msg+"With help by Freak and Tomio and"+Chr(10) msg=msg+" Stig h. Johansen And Utopiomania.com" ;MessageRequester("TextPad",msg,#MB_ICONINFORMATION) WrapAbout(0,"ArchonRPG.com","Just a simple text editor","2.0",msg,"www.archonrpg.com") Case #Help_Help Msg.s="This is just a simple text editor"+Chr(10) msg=msg+"A little more useful than MS Notepad."+Chr(10) msg=msg+"I am sure you can figure this out ....."+Chr(10) msg=msg+"the font and color settings are not saved with the text ..."+Chr(10) msg=msg+"unless you load and save in *.RTF format."+Chr(10) msg=msg+"This is just to demonstrate the rich text control in a real setting."+Chr(10) msg=msg+"Freak provided an example for selecting and coloring text"+Chr(10) msg=msg+"Tomio provided examples for the search function"+Chr(10) msg=msg+"Stig h. Johansen And Utopiomania.com provided a text editor with alot of good"+Chr(10) msg=msg+"code to copy from. Naturally, the About box and the Search-Replace box are his."+Chr(10) msg=msg+"Although I improved functionality a little bit here and there"+Chr(10) msg=msg+"http://www.hellobasic.com/ by Edwin Knoppert and Stephen Rodriguez provided"+Chr(10) msg=msg+"the RTF stream code to use OLE with PB editor gadget"+Chr(10) msg=msg+"netmaestro donated some code too ... did I really code any of this ????" MessageRequester("TextPad",msg,#MB_ICONINFORMATION) Default If MenuID>#Text_Color If MenuID<=#Text_Underline WFlags=0 ls=1-GetMenuItemState(0,MenuID) SetMenuItemState(0,MenuID,ls) SetToolBarButtonState(0,MenuID,ls) If GetToolBarButtonState(0,#Text_Bold) WFlags=WFlags | #CFM_BOLD EndIf If GetToolBarButtonState(0,#Text_Italics) WFlags=WFlags | #CFM_ITALIC EndIf If GetToolBarButtonState(0,#Text_Underline) WFlags=WFlags | #CFM_UNDERLINE EndIf format.CHARFORMAT format\cbSize = SizeOf(CHARFORMAT) format\dwMask = #CFM_ITALIC|#CFM_BOLD|#CFM_STRIKEOUT|#CFM_UNDERLINE format\dwEffects = WFlags SendMessage_(GadgetID(#Editor), #EM_SETCHARFORMAT, #SCF_SELECTION, @format) EndIf EndIf EndSelect ElseIf event=#PB_Event_Gadget NewText$=GetGadgetText(#Editor) If NewText$<>OldText$ IsDirty=1 EndIf ElseIf event=#PB_Event_SizeWindow width.l=WindowWidth(0) height.l=WindowHeight(0)-MenuHeight()-ToolBarHeight(0)-StatusBarHeight(#Stat1) top.l=MenuHeight()+10 ResizeGadget(#Editor,0,top,width,height-3) ElseIf event=#PB_Event_CloseWindow reply.l=0 If IsDirty reply=MessageRequester("TextPad","Do you wish to save your changes?",#MB_ICONQUESTION | #PB_MessageRequester_YesNoCancel ) If reply= #PB_MessageRequester_Yes SaveFile() EndIf EndIf If reply=#PB_MessageRequester_Cancel event=0 EndIf EndIf Else Delay(1) EndIf Until event=#PB_Event_CloseWindow End ;- Datasection ColorIcon: IncludeBinary "PENS04.ICO" BoldUP: IncludeBinary "BLD-UP.BMP" BoldDN: IncludeBinary "BLD-DWN.BMP" ItalicsUP: IncludeBinary "ITL-UP.BMP" ItalicsDN: IncludeBinary "ITL-DWN.BMP" ULineUP: IncludeBinary "ULIN-UP.BMP" ULineDN: IncludeBinary "ITL-DWN.BMP" ; IDE Options = PureBasic v4.02 (Windows - x86) ; CursorPosition = 528 ; FirstLine = 501 ; Folding = - ; EnableXP ; UseIcon = PENS04.ICO ; Executable = textpad.exe ; Watchlist = FoundChar ; IncludeVersionInfo ; VersionField0 = 1,1,1,1 ; VersionField1 = 1,1,1,1 ; VersionField2 = archonrpg ; VersionField3 = textpad ; VersionField4 = demo ; VersionField5 = demo ; VersionField6 = text editor ; VersionField7 = textpad ; VersionField8 = textpad ; VersionField14 = www.archonrpg.com ; VersionField15 = VOS_DOS_WINDOWS32 ; VersionField16 = VFT_APP ; VersionField17 = 0409 English (United States)