Posted: 20th Nov 2002 2:57
Ok, here are a few functions that may or may not be useful to you.

Instr()
This is one of those standard BASIC commands that for some mysterious reason has not been included in DBPro, or any other version of DB. I wonder why?
It will return the position of check$ in source$. By changing startpos you can choose where Instr() begins the search in source$. Set ignorecase to 1 to, well... ignore case
example: Instr("Raining Cats and Dogs","Cats",1,1) will return 9

Requires Midstr() to work.

Syntax
Return Integer = Instr(source$, check$, startpos, ignorecase)

+ Code Snippet
function Instr(source$,check$,startpos,ignorecase)
  rem --- Requires Midstr() function

  if ignorecase  0
    source$ = lower$(source$)
    check$ = lower$(check$)
  endif

  repeat
    if Midstr(source$,startpos,len(check$)) = check$ then exitfunction startpos
    inc startpos,1
  until startpos > len(source$)

endfunction 0



Midstr()
An slightly extended version of Mid$() where you can set the length of return.
example: Midstr("Raining Cats and Dogs",9,4) will return "Cats"

Syntax
Return String = Midstr(source$, pos, length)

+ Code Snippet
function Midstr(source$,pos,length)
   t$ = left$(right$(source$,len(source$)-(pos-1)),length)
endfunction t$


Shiftstr()
Will shift the contents of source$ 1 place with carry. Set shift to 1 to shift left or 0 for right.

Syntax
Return String = Shiftstr(source$, shift)

+ Code Snippet
function Shiftstr(source$,shift)
  if shift = 0
    rem --- shift right
    t$ = right$(source$,1) + left$(source$,len(source$)-1)
  else
    rem --- shift left
    t$ = right$(source$,len(source$)-1) + left$(source$,1)
  endif
endfunction t$



FlipStr()
Simply Reverses contents of source$

Syntax
Return String = Flipstr(source$)

+ Code Snippet
function Flipstr(source$)
  for i = len(source$) to 1 step -1
    t$ = t$ + mid$(source$,i)
  next i
endfunction t$



CircleFill()
Draws filled circles or ellipses. There are more accurate ways of drawing circles, but this is one of the fastest.
Have you noticed how badly DBPro draws circles?

Syntax
Circlefill(xcenter, ycenter, xradius, yradius)

+ Code Snippet
function CircleFill(xcenter,ycenter,xradius,yradius)
  rem --- find vertical ratio to help draw ellipses
  xr# = xradius : yr# = yradius
  vr# = yr# / xr#

  r2 = xradius^2
  for x = 1 to xradius
    y = sqrt(r2 - x^2) * vr#
    box xcenter - x, ycenter - y, xcenter + x, ycenter + y
  next x
endfunction



RoundBox()
A slightly modified version of the Box() command where you can draw boxes with rounded corners.

Syntax
RoundBox(left, top, right, bottom, cornerradius)

+ Code Snippet
function RoundBox(left,top,right,bottom,cornerradius)
  if cornerradius = 0
    box left,top,right,bottom
  else
    cornerradius = abs(cornerradius)
    inc left, cornerradius
    inc top, cornerradius
    dec right, cornerradius
    dec bottom, cornerradius

    r2 = cornerradius^2
    for x = 1 to cornerradius
      y = sqrt(r2 - x^2)
      box left - x, top - y, right + x, bottom + y
    next x
  endif
endfunction



MessageBox()
Displays a basic Windows like message box in the center of the current bitmap and returns button number clicked.
width, height defines the size. Note: Any text that is too long is clipped if MessageBox() is to small.
title$ is the erm...title.
txt$ can be divided into sepereate lines with the | character, so "line 1|line 2|line 3" will display 3 lines of text.
butt$ are the buttons which are divided in the same way, so "OK|Cancel" will display two buttons.
Setting centertitle or centertxt to 1 will center justify, 0 will left justify.
minbuttsize will force a minimum width for your buttons, set to 0 to accept largest width defined in butt$.

Will return -1 if MessageBox() fails.
Requires DrawBoxText()

Limitations of use:
Uses image 65535 as a backsave. Not realy a limitation but I mention it anyway.
Will halt your program until user clicks a button.

I could do more with the MessageBox(), but if used properly it's fine as it is.

Syntax
Return Integer = MessageBox(width, height, title$, txt$, butt$, centertitle, centertxt, minbuttsize)


+ Code Snippet
function MessageBox(width,height,title$,txt$,butt$,centertitle,centertxt,minbuttsize)
  rem --- Requires DrawBoxText() funtion ---

  rem --- parameter checks
  if (width=0) or (height=0) then exitfunction -1
  if title$="" then title$ = "Message Box"
  if butt$="" then butt$="OK"

  width = abs(width) : height = abs(height)
  left = (bitmap width() - width) / 2 : if left = bitmap width() then right = bitmap width()-1
  bottom = top + height : if bottom >= bitmap height() then bottom = bitmap height()-1
  th = text height(txt$)
  y = top + th + 2

  rem --- set array for a maximum of 10 buttons, probably wont need this many
  dim b$(9)

  rem --- preserve current user text settings
  txtstyle = text style() : txtback = text background type()

  set text transparent
  set text to normal

  rem --- store backsave
  Get image 65535,left,top,right+1,bottom+1,1

  rem --- draw body
  DrawBoxText(left,y,right,bottom,"",0,rgb(200,200,200),rgb(255,255,255),rgb(16,16,16),rgb(255,255,255))
  rem --- draw title bar
  DrawBoxText(left,top,right,y,title$,centertitle,rgb(0,0,200),rgb(255,255,255),rgb(16,16,16),rgb(255,255,255))
  rem --- show text
  DrawBoxText(left+1,y+1,right-1,bottom - th - 4,txt$,centertxt,rgb(160,160,160),rgb(16,16,16),rgb(255,255,255),0)

  rem --- buttons in b$()
  pos = len(butt$) : lastpos = pos
  repeat
    repeat
      dec pos,1
    until pos = 0 or mid$(butt$,pos) = "|"
    b$(i) = left$(right$(butt$,len(butt$)-pos),lastpos-pos)
    lastpos = pos
    if (text width(b$(i)) > buttwidth) then buttwidth = text width(b$(i))
    inc i,1
  until pos = 0 or i = 10
  if buttwidth  ""
      buttright = right - (buttwidth * i) - 4
      buttleft = buttright - buttwidth + 4
      if buttright >= left+4
        if buttleft = left and mousex() = (bottom - th - 2) and mousey()  ""
    ink txtcol,0
    th = text height(txt$)
    x = left + 4 : y = top
    centerx = left + ((right - left) / 2)
    pos = 0 : lastpos = 0

    repeat
      repeat : inc pos,1 : until pos > len(txt$) or mid$(txt$,pos) = "|"
      t$ = left$(right$(txt$,len(txt$)-lastpos),(pos-1)-lastpos)
      lastpos = pos

      rem --- clip text if too long
      if text width(t$) > (right - left)
        tw = 0 : lp = 0
        repeat : inc lp,1 : tw = tw + text width(mid$(t$,lp)) : until tw > (right - left)
        t$ = left$(t$,lp-1)
      endif

      rem --- display text
      if centertxt = 0 then text x,y,t$ else center text centerx,y,t$
      inc y,th
    until pos > len(txt$) or (y+th) > bottom

  endif
endfunction
Posted: 20th Nov 2002 3:08
sorry, MessageBox() did not copy correctly, will fix, back soon.....
Posted: 20th Nov 2002 3:33
Ok, hope it's copied properly now, here goes, fingers crossed...

+ Code Snippet
function MessageBox(width,height,title$,txt$,butt$,centertitle,centertxt,minbuttsize)
  rem --- Requires DrawBoxText() funtion ---

  if (width = 0) or (height = 0) then exitfunction -1

  rem --- parameter checks
  if width > bitmap width() then width = bitmap width()
  if height > bitmap height() then height = bitmap height()
  if title$="" then title$ = "Message Box"
  if butt$="" then butt$="OK"

  left = (bitmap width() - width)/2
  top = (bitmap height() - height)/2
  right = left + width
  bottom = top + height
  th = text height(txt$)
  y = top + th + 2

  rem --- set array for a maximum of 10 buttons, probably wont need this many
  dim b$(9)

  rem --- preserve user text settings
  txtstyle = text style()
  txtback = text background type()

  set text to normal
  set text transparent

  rem --- store backsave
  Get image 65535,left,top,right+1,bottom+1,1

  rem --- draw body
  DrawBoxText(left,y,right,bottom,"",0,rgb(200,200,200),rgb(255,255,255),rgb(16,16,16),rgb(255,255,255))
  rem --- draw title bar
  DrawBoxText(left,top,right,y,title$,centertitle,rgb(0,0,200),rgb(255,255,255),rgb(16,16,16),rgb(255,255,255))
  rem --- show text
  DrawBoxText(left+1,y+1,right-1,bottom - th - 4,txt$,centertxt,rgb(160,160,160),rgb(16,16,16),rgb(255,255,255),0)

  rem --- read and store buttons in b$()
  pos = len(butt$) : lastpos = pos
  repeat
    repeat : dec pos,1 : until pos = 0 or mid$(butt$,pos) = "|"
    b$(i) = left$(right$(butt$,len(butt$)-pos),lastpos-pos)
    lastpos = pos
    if (text width(b$(i)) > buttwidth) then buttwidth = text width(b$(i))
    inc i,1
  until pos = 0 or i = 10
  if buttwidth < minbuttsize then buttwidth = minbuttsize
  inc buttwidth,16

  rem --- show buttons
  buttlimit = -1
  for i = 0 to 9
    if b$(i) <> ""
      buttright = right - (buttwidth * i) - 4
      buttleft = buttright - buttwidth + 4
      DrawBoxText(buttleft,bottom - th - 2,buttright,bottom -2,b$(i),1,rgb(200,200,200),rgb(255,255,255),rgb(16,16,16),0)
      inc buttlimit,1
    endif
  next i

  do
    if mouseclick()
      if mousex() >= left and mousex() <= right and mousey() >= (bottom - th - 2) and mousey() <= (bottom - 2)
        rem --- have we clicked a button?
        button = (right - mousex()) / buttwidth

        if button <= buttlimit
          rem --- draw button pressed
          buttright = right - (buttwidth * button) - 4
          buttleft = buttright - buttwidth + 4
          DrawBoxText(buttleft,bottom - th - 2,buttright,bottom -2,b$(button),1,rgb(200,200,200),rgb(16,16,16),rgb(255,255,255),0)
          sync
          while mouseclick() : endwhile

          rem --- restore user text settings
          if txtback = 0 then set text opaque
          if txtstyle = 1 then set text to italic
          if txtstyle = 2 then set text to bold
          if txtstyle = 3 then set text to bolditalic

          rem --- clean up before exiting
          paste image 65535,left,top
          delete image 65535
          undim b$()
          sync
          exitfunction buttlimit - button
        endif

      endif
    endif
    sync
  loop

endfunction -1



...and it's support function.


+ Code Snippet
function DrawBoxText(left,top,right,bottom,txt$,centertxt,facecol,lightcol,darkcol,txtcol)
  ink facecol,0 : box left,top,right,bottom
  ink darkcol,0 : line left,bottom,right,bottom : line right,bottom,right,top
  ink lightcol,0 : line right,top,left,top : line left,top,left,bottom

  if txt$ <> ""
    ink txtcol,0
    th = text height(txt$)
    x = left + 4 : y = top
    pos = 0 : lastpos = 0
    centerx = left+((right - left) / 2)
    repeat
      repeat : inc pos,1 : until pos > len(txt$) or mid$(txt$,pos) = "|"
      t$ = left$(right$(txt$,len(txt$)-lastpos),(pos-1)-lastpos)
      lastpos = pos

      rem --- clip text if to long
      if text width(t$) > (right - left)
        tw = 0 : lp = 0
        repeat : inc lp,1 : tw = tw + text width(mid$(t$,lp)) : until tw > (right - left)
        t$ = left$(t$,lp-1)
      endif

      rem --- display text
      if centertxt = 0 then text x,y,t$ else center text centerx,y,t$
      inc y, th
    until pos > len(txt$) or (y+th) > bottom
  endif
endfunction
Posted: 20th Nov 2002 4:33
I made a string library with over 120 functions I dont think there is a DB programmer alive that hasn't improved upon the missing DB string statements.
Posted: 22nd Nov 2002 20:30
You just need replacestr$() now!
Posted: 22nd Nov 2002 22:49
Dang!, I knew i forgot one, so here it is

Replacestr()
Replaces any fragment of source$ with a replacement.
check$ is the string fragment to replace (if present).
replace$ is the replacement.
ignorecase, will do just that.

example: Replacestr("Raining Cats And Dogs","Cats","Elephants",0) will return "Raining Elephants and dogs" ( I would'nt like to see that )

Requires Instr() and Midstr() functions.

+ Code Snippet
Function Replacestr(source$,check$,replace$,ignorecase)
  rem --- requires Instr() and Midstr() functions

  pos = 1
  repeat
    pos = Instr(source$,check$,pos,ignorecase)
    if pos <> 0
      source$ = left$(source$,pos-1) + replace$ + right$(source$,len(source$) - (pos + len(check$) - 1))
      pos = pos + len(replace$)
    endif
  until pos = 0

endfunction source$
Posted: 22nd Nov 2002 23:18
Thanks, I've been needing a DBP compatible one for the programming language (made in DBP).