Simple_Help_Creater

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

Simple_Help_Creater

Post by yrreti »

Just though I'd share this piece of code I wrote that I use once in awhile.
(the drag and drop part I think I got from Netmaestro)
There have been times that I needed to create a simple help window for quick
viewing and printing if needed. But it was always a hassle to do all the carefull
string and (" ") coding manually. So I wrote this program that takes a simple help text
file that you create in notepad, and then you drag and drop it on this program, and
codes it for use in an help Window's EditorGadget.
It's not fancy like the ones with all the links etc., but it works. And you can always make
any changes you like to it.

For Example: Take the following text and copy it to note pad and save it as helptest.txt

Code: Select all

This program takes a help text file that you drag and drop on it,
and codes it for use in an help Window's EditorGadget.

You first use notepad and type out what you want to put in your simple help message.
You can even use " characters in your help file.
Then you run this program and drag and drop that file on this programs window.
The converted file is created with the same name, but as a pb file, and is 
displayed in Notepad when done.
You can copy and paste it to where you need it in your code, or load the file
in the editor to run and test it, and handle it from there.
You will just need to add the Help screen title to the help_title$ string below.
Now run the code below and drop the helptest.txt file you created on it.
The created code helptest.pb, will open up in notepad for your viewing, but you can
also run it in the editor to check it too.

Code: Select all

;This program takes a help text file that you drag and drop on it,
;and codes it for use in an help Window's EditorGadget.

;You first use notepad and type out what you want to put in your simple help message.
;You can even use " characters in your help file.
;Then you run this program and drag and drop that file on this programs window.
;The converted file is created with the same name, but as a pb file, and is 
;displayed in Notepad when done.
;You can copy and paste it to where you need it in your code, or load the file
;in the editor to run and test it, and handle it from there.
;You will just need to add the Help screen title to the help_title$ string below.

#MainWin=0
#WinX=300
#WinY=200
#WinW=220
#WinH=100

#BufferLength = 1000
#MEM_DROPFILES = 10
*Buffer = AllocateMemory(#BufferLength)

hWnd = OpenWindow(#MainWin, #WinX,#WinY, #WinW,#WinH, "Drag & Drop Help txt file here", #PB_Window_SystemMenu)
If hWnd
  DragAcceptFiles_(WindowID(#MainWin), 1)
  Repeat
    Event= WaitWindowEvent()
    Select Event
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #WM_DROPFILES
      *DropHandle = EventwParam()
      ;Debug *DropHandle
      If *DropHandle
        NbDroppedFiles = DragQueryFile_ (*DropHandle, $FFFFFFFF, *Buffer, #BufferLength)
        DragQueryFile_(*DropHandle, k, *Buffer, #BufferLength)
        DragFinish_(*DropHandle)
        fn$=PeekS(*Buffer)
        Goto convert
      EndIf
    EndSelect
  Until Quit = 1
EndIf
End

convert:


If ReadFile(1,fn$)  ; if file exists
  ;fn$=file name    nf$=new file
  ;"+chr(34)+"
  nf$=Left(fn$,Len(fn$)-3)+"pb"
  
  CreateFile(2,nf$)
  WriteStringN(2, "Enumeration")
  WriteStringN(2, "  #helpwin")
  WriteStringN(2, "  #help_EG")
  WriteStringN(2, "  #help_Print_btn")
  WriteStringN(2, "  #help_Close_btn")
  WriteStringN(2, "EndEnumeration")
  WriteStringN(2, " ")
  WriteStringN(2, "Procedure helpwin(help_title$)")
  
  WriteStringN(2, "   OpenWindow(#helpwin,0,0,550,520,help_title$,#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget|#PB_Window_SystemMenu)")
  WriteStringN(2, "   EditorGadget(#help_EG, 5, 5, 540, 480 , #PB_Editor_ReadOnly)")
  WriteStringN(2, "   ButtonGadget(#help_Print_btn, 10, 490, 50, 20, "+Chr(34)+"Print"+Chr(34)+");have it first clear all")
  WriteStringN(2, "   ButtonGadget(#help_Close_btn, 275, 490, 50, 20, "+Chr(34)+"Close"+Chr(34)+")")
  WriteStringN(2, "   Define lf$=Chr(10)")
  WriteStringN(2, "   ls$="+Chr(34)+"   "+Chr(34)+";leading space adjust")
  WriteStringN(2, "   lf$=Chr(10)")
  WriteStringN(2, "   hm$="+Chr(34)+Chr(34))
  
  
  Repeat
    dat$=ReadString(1)
    If Trim(dat$)=""
      WriteStringN(2,"   hm$=hm$"+Chr(43)+"lf$")
    Else
      ;String$ = ReplaceString(String$, StringToFind$, StringToReplace$ [, Mode [, StartPosition]])
      dat$ = ReplaceString(dat$, Chr(34), Chr(34)+"+Chr(34)+"+Chr(34))
      WriteStringN(2,"   hm$=hm$+ls$"+Chr(43)+Chr(34)+dat$+Chr(34)+Chr(43)+"lf$")
    EndIf
    
  Until Eof(1)<>0
  CloseFile(1)
  
  WriteStringN(2, "   SetGadgetText(#help_EG,hm$)")
  WriteStringN(2, "EndProcedure")
  WriteStringN(2, " ")
  WriteStringN(2, "help_title$="+Chr(34)+Chr(34)+";put help title name here.")
  WriteStringN(2, "helpwin(help_title$)")
  WriteStringN(2, " ")
  WriteStringN(2, ";Use this part just to test the created helpwin, or use in your code.")
  WriteStringN(2, "Repeat")
  WriteStringN(2, "   Select WaitWindowEvent(5)")
  WriteStringN(2, "      Case #PB_Event_Gadget")
  WriteStringN(2, "      Select EventGadget()")
  WriteStringN(2, "         Case #help_Print_btn;-Print Help")
  WriteStringN(2, "            If GetActiveWindow()=#helpwin")
  WriteStringN(2, "               If Print_OpenPrinter("+Chr(34)+Chr(34)+","+Chr(34)+"PAPERSIZE="+Chr(34)+"+Str(#DMPAPER_LETTER)+"+Chr(34)+",ORIENTATION="+Chr(34)+"+Str(#DMORIENT_PORTRAIT))")
  WriteStringN(2, "                 Print_StartPrinting("+Chr(34)+"MyData"+Chr(34)+")")
  WriteStringN(2, "                 ")
  WriteStringN(2, "                 y = 6 ; initial Y position")
  WriteStringN(2, "                 Print_Font("+Chr(34)+"Lucida Console"+Chr(34)+",9);, #PB_Font_Default)")
  WriteStringN(2, "                 ")
  WriteStringN(2, "                 tlc=CountGadgetItems(#help_EG)")
  WriteStringN(2, "                 If tlc>0")
  WriteStringN(2, "                   For pl=0 To tlc-1")
  WriteStringN(2, "                     ;If y<100;save as a test to save paper")
  WriteStringN(2, "                     Result$ = GetGadgetItemText(#help_EG, pl,1)")
  WriteStringN(2, "                     If Result$ ="+Chr(34)+"-pb-"+Chr(34)+";page break")
  WriteStringN(2, "                       y=6")
  WriteStringN(2, "                       Print_NewPage()")
  WriteStringN(2, "                     Else")
  WriteStringN(2, "                       Print_Text(0,y, Result$) ; 0,y is the line position")
  WriteStringN(2, "                       y = y + 3 ; move down... for my font, 3 seemed about right")
  WriteStringN(2, "                       If y>240")
  WriteStringN(2, "                         ;The following takes care of the printer spooler never clears bug if the last")
  WriteStringN(2, "                         ;print line would trigger the Print_NewPage() command.")
  WriteStringN(2, "                         If pl<tlc-1")
  WriteStringN(2, "                           Print_NewPage()")
  WriteStringN(2, "                           y=6")
  WriteStringN(2, "                         EndIf")
  WriteStringN(2, "                       EndIf")
  WriteStringN(2, "                     EndIf")
  WriteStringN(2, "                   Next pl")
  WriteStringN(2, "                   Print_StopPrinting()")
  WriteStringN(2, "                 Else")
  WriteStringN(2, "                   MessageRequester("+Chr(34)+"Print Data"+Chr(34)+","+Chr(34)+"Nothing To Print ..."+Chr(34)+",0)")
  WriteStringN(2, "                 EndIf")
  WriteStringN(2, "               Else")
  WriteStringN(2, "                 err$=Print_GetLastError()")
  WriteStringN(2, "                 MessageRequester("+Chr(34)+"Print_StartPrinting()"+Chr(34)+",err$,0)")
  WriteStringN(2, "               EndIf")
  WriteStringN(2, "               Delay(1000);just to show printing else it's too fast.")
  WriteStringN(2, "             EndIf")
  WriteStringN(2, "             ")
  WriteStringN(2, "           Case #help_Close_btn")
  WriteStringN(2, "             If GetActiveWindow()=#helpwin")
  WriteStringN(2, "               CloseWindow(#helpwin)")
  WriteStringN(2, "               Break")  
  WriteStringN(2, "               ;SetActiveWindow(Window_0)")
  WriteStringN(2, "               ;UseGadgetList(#Window_0)")
  WriteStringN(2, "             EndIf")  
  WriteStringN(2, "         EndSelect")
  WriteStringN(2, "      Case #PB_Event_CloseWindow")
  WriteStringN(2, "         Select EventWindow()")
  WriteStringN(2, "            Case #helpwin")
  WriteStringN(2, "               CloseWindow(#helpwin)")
  WriteStringN(2, "               Break")
  WriteStringN(2, "         EndSelect")
  WriteStringN(2, "   EndSelect")
  WriteStringN(2, "ForEver")
  
  CloseFile(2)
  RunProgram("notepad.exe" , nf$, Left(nf$,1))
  
EndIf
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Simple_Help_Creater

Post by Tenaja »

Neat tool. It ran but would not work, when not an exe.
As an exe, it worked great...

except running the output file, I get an error:
Print_OpenPrinter() is not a function, array, macro or linked list.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: Simple_Help_Creater

Post by yrreti »

I'm using PB4.51 with XP Pro
As to running from the IDE or as a created exe, both work fine for me.

I'm sorry I couldn't get back to answer back right away, had to work. I just realized that your
print problem is caused because I use ABBKlaus's Printer_Lib
http://www.purebasicpower.de/
I had installed it long ago because I liked it much better then the built in ones. I forgot all
about it because I've always used it. You will either need to change the printing code to
reflect the commands that come with PB, or install and use ABBKlaus's Printer_Lib.
That is why I also submitted the code, so you could modify it to use what ever you want.
Maybe some one could post the necessary modifications here so that you can use PB's built
in print commands. I'm sticking with ABBKlaus's Printer_Lib :wink:
Post Reply