A help window for lazy coders / documenters...

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

A help window for lazy coders / documenters...

Post by blueznl »

Sometimes there's simply no need for massive amounts of text, real help files, etcetera... This procedure quickly adds a window to your program that allows you to embed a little 'help file', for example in command line tools.

Code: Select all


Procedure littlehelp(title.s,text.s,delta.i=0)
  Protected text_size.i, page_size.i, page_number.i, page_count.i
  Protected p.i, n.i, l.i,  h.i
  Protected width.i, height.i
  Protected w_showhelp.i, g_text.i, g_previous.i, g_next.i, g_done.i, font_nr.i
  Protected Dim page_start.i(10)
  Protected Dim page_size.i(10)
  Protected event.i
  ;
  ; displays a little window with the given text, and allows to browse through that text
  ;
  ; in:   title.s    - window title
  ;       text.s     - multi line text seperated by CRLF's
  ;       delta.i    - decreases page size by 'n' lines to beautify text
  ;
  ; note: if text shows up with inconvenient page breaks then try 1 or 2 for delta
  ;
  ExamineDesktops()
  text_size = CountString(text,#CRLF$)+1
  page_size = DesktopHeight(0)/23
  height = page_size*17
  page_size = page_size-delta
  page_count = 1+(text_size-1)/page_size
  width = 500
  ;
  p = 1
  n = 0
  Repeat
    n = n+1
    page_start.i(n) = p
    l = 0
    Repeat
      p = FindString(text,#CRLF$,p+1)
      l = l+1
    Until l >= page_size Or p = 0
    If p > 0
      page_size(n) = p-page_start(n)
      p = p+2
    Else
      page_size(n) = Len(text)-page_start(n)+1
    EndIf
  Until p <= 0
  ;
  font_nr = LoadFont(#PB_Any,"Courier New",9)
  w_showhelp = OpenWindow(#PB_Any,10,10,width+16,height+16,title,#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  AddKeyboardShortcut(w_showhelp,#PB_Shortcut_Return,1)
  AddKeyboardShortcut(w_showhelp,#PB_Shortcut_Escape,1)
  ;
  g_text = TextGadget(#PB_Any,16,16,width,height-50,"")
  g_previous = ButtonGadget(#PB_Any,width-140,height-12,50,22,"<")
  g_next = ButtonGadget(#PB_Any,width-90,height-12,50,22,">")
  g_done = ButtonGadget(#PB_Any,width-40,height-12,50,22,"Ok")
  SetGadgetFont(g_text,FontID(font_nr))
  ;
  page_number = 1
  SetGadgetText(g_text,Mid(text,page_start(page_number),page_size(page_number)))
  Repeat
    If page_number = 1
      DisableGadget(g_previous,1)
    Else
      DisableGadget(g_previous,0)
    EndIf
    If page_number = page_count
      DisableGadget(g_next,1)
    Else
      DisableGadget(g_next,0)
    EndIf
    event = WaitWindowEvent()
    Select event
    Case #PB_Event_Menu
      event = #PB_Event_CloseWindow
    Case #PB_Event_Gadget
      Select EventGadget()
      Case g_previous
        page_number = page_number-1
      Case g_next
        page_number = page_number+1
      Case g_done
        event = #PB_Event_CloseWindow
      EndSelect
      SetGadgetText(g_text,Mid(text,page_start(page_number),page_size(page_number)))
    EndSelect
  Until event = #PB_Event_CloseWindow
  CloseWindow(w_showhelp)
  FreeFont(font_nr)
  ;
EndProcedure
And here's how I use it, taken from a larger program... I first create a little text document, which I include in my program using IncludeBinary.

Code: Select all

  ...
  DataSection
    helptext:
    IncludeBinary("wallx.txt")
    Data.b 0, 0
  EndDataSection
  ;
  ...
  ;
  Restore helptext
  Read.s helptext
  littlehelp("Window title",helptext)
  ;
  ...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: A help window for lazy coders / documenters...

Post by NoahPhense »

Interesting.. I could find a use for this. Thanks.

- np
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: A help window for lazy coders / documenters...

Post by SFSxOI »

very nice, thank you :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Post Reply