Mini webgadget tip

Share your advanced PureBasic knowledge/code with the community.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Mini webgadget tip

Post by utopiomania »

The webgadget callback can return #TRUE to allow, or #FALSE to deny navigation, but depending on the application
of the webgadget, a tip is to deny everything (return #FALSE) by default, like the snippet below do.

If you don't do this, the webgadget is wide open to drag-drop operations, which could easily slip through to a
'procedureReturn #TRUE' and mess up (replace) the contents of the webgadget. ..Which would look quite :oops: if the
contents was supposed to be a fancy GUI or something.

Code: Select all

procedure webCallback(id, url.s)
  url = lcase(url)
  ;handle specific events...
  if findString(url, "btn1_onClick", 1)
    messageRequester("", "btn1_onClick event")
  elseIf findString(url, "btn2_onClick", 1)
    messageRequester("", "btn2_onClick event")
  elseIf findString(url, "btn3_onClick", 1)
    messageRequester("", "btn3_onClick event")
  elseIf findString(url, "btn4_onClick", 1)
    messageRequester("", "btn4_onClick event")
  endIf
  ;deny any other events/navigation
  procedureReturn #FALSE
endProcedure
quasiperfect
Enthusiast
Enthusiast
Posts: 157
Joined: Tue Feb 13, 2007 6:16 pm
Location: Romania
Contact:

Post by quasiperfect »

can u post a little exemple please ? i tried to use the procedure u posted but coudn't make it to work.
thanks in advance
Registered user of PureBasic
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

This test I wrote for another purpose uses the modified callback to prevent
drag/drop into the webgadget:

Code: Select all

;-program notes
;-initialize
;-
enumeration
  #WIN
  #WEB1
  #WEB2
  #WEB3
  #WEB4
  #EDIT1
endEnumeration

global winW = 800, winH = 600
global web2W = 150
global web3H = 40, web4H = 40

declare openMainWindow()
declare webCallback(id, url.s)
declare.s web1Page1()
declare.s web2Page1()
declare.s web3Page1()
declare.s web4Page1()

;-program entry
openMainWindow()

;-program event handler
repeat
  event = waitWindowEvent()
  select event
    case #PB_EVENT_SIZEWINDOW
      winW = windowWidth(#WIN)
      winH = windowHeight(#WIN)
      ;main webgadget
      resizeGadget(#WEB1, 0, web3H, winW - web2W, winH - web3H - web4H)
      ;left webgadget
      resizeGadget(#WEB2, winW - web2W, web3H, web2W, winH - web3H - web4H)
      ;upper webgadget
      resizeGadget(#WEB3, 0, 0, winW, web3H)
      ;lower webgadget
      resizeGadget(#WEB4, 0, winH - web4H, winW, web4H)
    case #PB_EVENT_CLOSEWINDOW
      ;-program exit
      ;-
      end
  endSelect
forever

procedure openMainWindow()
  if openWindow(#WIN, 0, 0, winW, winH, "program template", $CF0001)
    if createGadgetList(windowID(#WIN))
      ;main webgadget
      webGadget(#WEB1, 0, web3H, winW - web2W, winH - web3H - web4H, "")
      setGadgetItemText(#WEB1, #PB_WEB_HTMLCODE, web1Page1())
      setGadgetAttribute(#WEB1, #PB_WEB_NAVIGATIONCALLBACK, @webCallback())
      ;left panel webgadget
      webGadget(#WEB2, winW - web2W, web3H, web2W, winH - web3H - web4H, "")
      setGadgetItemText(#WEB2, #PB_WEB_HTMLCODE, web2Page1())
      setGadgetAttribute(#WEB2, #PB_WEB_NAVIGATIONCALLBACK, @webCallback())
      ;upper panel webgadget
      webGadget(#WEB3, 0, 0, winW, web3H, "")
      setGadgetItemText(#WEB3, #PB_WEB_HTMLCODE, web3Page1())
      setGadgetAttribute(#WEB3, #PB_WEB_NAVIGATIONCALLBACK, @webCallback())
      ;lower panel webgadget
      webGadget(#WEB4, 0, winH - web4H, winW, web4H, "")
      setGadgetItemText(#WEB4, #PB_WEB_HTMLCODE, web4Page1())
      setGadgetAttribute(#WEB4, #PB_WEB_NAVIGATIONCALLBACK, @webCallback())
    endIf
  endIf
endProcedure

procedure webCallback(id, url.s)
  url = lcase(url)
  if findString(url, "web1", 1)
      ;handle event...
      messageRequester("", "web1 event")
    elseIf findString(url, "web2", 1)
      messageRequester("", "web2 event")
    elseIf findString(url, "web3", 1)
      messageRequester("", "web3 event")
    elseIf findString(url, "web4", 1)
      messageRequester("", "web4 event")
  endIf
  ;deny navigation
  procedureReturn 0
endProcedure

procedure.s web1Page1()
  ;html
  html.s + "<html><head>" + #CR$
  html + "<style type = 'text/css'>" + #CR$
  html + "  body{font-family:system; font-size:18px; font-weight:800}" + #CR$
  html + "</style>" + #CR$ + #CR$

  html + "<script language = 'vbscript'>" + #CR$
  html + "sub object1_onMouseOver()" + #CR$
  html + "  object1.style.cursor = " + #DQUOTE$ +"hand" + #DQUOTE$ + #CR$
	html + "  object1.style.color = " + #DQUOTE$ +"#FFFFFF" + #DQUOTE$ + #CR$
  html + "  object1.style.backgroundcolor = " + #DQUOTE$ +"#4080FF" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$ + #CR$

  html + "sub object1_onMouseOut()" + #CR$
  html + "  object1.style.cursor = " + #DQUOTE$ +"arrow" + #DQUOTE$ + #CR$
  html + "  object1.style.color = " + #DQUOTE$ +"#4080FF" + #DQUOTE$ + #CR$
  html + "  object1.style.backgroundcolor = " + #DQUOTE$ +"#FFFFFF" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$ + #CR$

  html + "sub object1_onClick()" + #CR$
  html + "  window.location = " + #DQUOTE$ +"web1" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$
  html + "</script>" + #CR$
  html + "</head>" + #CR$

  html + "<body bgcolor = '#FFFFFF' scroll = 'yes'>" + #CR$ + #CR$
  html + "<span id = 'object1'" + #CR$ 
  html + "  style = 'position:absolute; left:20; top:0; width:100; height:22; color:#4080FF'>" + #CR$
  html + "  main panel" + #CR$
  html + "</span>" + #CR$
  html + "</body></html>"
  procedureReturn html
endProcedure

procedure.s web2Page1()
  ;html
  html.s + "<html>" + #CR$
  html.s + "<head>" + #CR$
  html.s + "<title></title>" + #CR$

  html + "<style type = 'text/css'>" + #CR$
  html + "  body{font-family:system; font-size:18px; font-weight:800}" + #CR$
  html + "</style>" + #CR$ + #CR$

  html + "<script language=" + #DQUOTE$ +"vbscript" + #DQUOTE$ +">" + #CR$
  html + "dim visible :visible = " + #DQUOTE$ +"" + #DQUOTE$ +"" + #CR$
  html + "dim hidden :hidden = " + #DQUOTE$ +"none" + #DQUOTE$ +"" + #CR$

  html + "dim headerBg :headerBg = " + #DQUOTE$ +"#FAFAFA" + #DQUOTE$ +"" + #CR$
  html + "dim headerFg :headerFg = " + #DQUOTE$ +"#4080FF" + #DQUOTE$ +"" + #CR$

  html + "// WINDOW LOAD EVENT" + #CR$
  html + "sub window_onLoad()" + #CR$
  html + "	header1_onClick()" + #CR$
  html + "end sub" + #CR$

  html + "// HEADER1 EVENTS" + #CR$
  html + "sub header1_onClick()" + #CR$
  html + "	if content1.style.display = visible then" + #CR$
  html + "		//close it" + #CR$
  html + "		content1.style.display = hidden" + #CR$
  html + "	else" + #CR$
  html + "		//open it" + #CR$
  html + "		content1.style.display = visible" + #CR$
  html + "		//close the other headers" + #CR$
  html + "		content2.style.display = hidden" + #CR$
  html + "	end if" + #CR$
  html + "end sub" + #CR$

  html + "sub header1_onMouseOver()" + #CR$
  html + "	header1.style.cursor = " + #DQUOTE$ +"hand" + #DQUOTE$ +"" + #CR$
  html + "	header1.style.backgroundcolor = headerBg" + #CR$
  html + "	header1.style.color = headerFg" + #CR$
  html + "end sub" + #CR$

  html + "sub header1_onMouseOut()" + #CR$
  html + "	header1.style.cursor = " + #DQUOTE$ +"arrow" + #DQUOTE$ +"" + #CR$
  html + "	header1.style.backgroundcolor = " + #DQUOTE$ +"#FAFAFA" + #DQUOTE$ +"" + #CR$
  html + "	header1.style.color = " + #DQUOTE$ +"black" + #DQUOTE$ +"" + #CR$
  html + "end sub" + #CR$

  html + "// HEADER2 EVENTS" + #CR$
  html + "sub header2_onClick()" + #CR$
  html + "	if content2.style.display = visible then" + #CR$
  html + "		//close it" + #CR$
  html + "		content2.style.display = hidden" + #CR$
  html + "	else" + #CR$
  html + "		//open it" + #CR$
  html + "		content2.style.display = visible" + #CR$
  html + "		//close the other headers" + #CR$
  html + "		content1.style.display = hidden" + #CR$
  html + "	end if" + #CR$
  html + "end sub" + #CR$

  html + "sub header2_onMouseOver()" + #CR$
  html + "	header2.style.cursor = " + #DQUOTE$ +"hand" + #DQUOTE$ +"" + #CR$
  html + "	header2.style.backgroundcolor = headerBg" + #CR$
  html + "	header2.style.color = headerFg" + #CR$
  html + "end sub" + #CR$

  html + "sub header2_onMouseOut()" + #CR$
  html + "	header2.style.cursor = " + #DQUOTE$ +"arrow" + #DQUOTE$ +"" + #CR$
  html + "	header2.style.backgroundcolor = " + #DQUOTE$ +"#FAFAFA" + #DQUOTE$ +"" + #CR$
  html + "	header2.style.color = " + #DQUOTE$ +"black" + #DQUOTE$ +"" + #CR$
  html + "end sub" + #CR$

  html + "</script>" + #CR$
  html + "</head>" + #CR$

  html + "<body bgcolor = '#FAFAFA' scroll = 'no'>" + #CR$
  html + "<span id = 'header1'>ITEM 1</span>" + #CR$
  html + "	<span id = 'content1' style = " + #DQUOTE$ +"display:none; background-color:'#fafafa'" + #DQUOTE$ +">" + #CR$
  html + "	<br><a href = '#'>1.1</a>" + #CR$
  html + "	<br><a href = '#'>1.2</a>" + #CR$
  html + "	<br><a href = '#'>1.3</a>" + #CR$
  html + "	<br><a href = '#'>1.4</a>" + #CR$
  html + "</span>" + #CR$
  html + "<br>" + #CR$
  html + "<span id = 'header2'>ITEM 2</span>" + #CR$
  html + "	<span id = 'content2' style = " + #DQUOTE$ +"display:none; background-color:'#fafafa'" + #DQUOTE$ +">" + #CR$
  html + "	<br><a href = '#'>2.1</a>" + #CR$
  html + "	<br><a href = '#'>2.2</a>" + #CR$
  html + "	<br><a href = '#'>2.3</a>" + #CR$
  html + "	<br><a href = '#'>2.4</a>" + #CR$
  html + "</span>" + #CR$


  html + "</body>" + #CR$
  html + "</html>" + #CR$
  
  procedureReturn html
endProcedure

procedure.s web3Page1()
  ;html
  html.s + "<html><head>" + #CR$
  html + "<style type = 'text/css'>" + #CR$
  html + "  body{font-family:system; font-size:18px; font-weight:800}" + #CR$
  html + "</style>" + #CR$ + #CR$

  html + "<script language = 'vbscript'>" + #CR$
  html + "sub object1_onMouseOver()" + #CR$
  html + "  object1.style.cursor = " + #DQUOTE$ +"hand" + #DQUOTE$ + #CR$
	html + "  object1.style.color = " + #DQUOTE$ +"#FFFFFF" + #DQUOTE$ + #CR$
  html + "  object1.style.backgroundcolor = " + #DQUOTE$ +"#4080FF" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$ + #CR$

  html + "sub object1_onMouseOut()" + #CR$
  html + "  object1.style.cursor = " + #DQUOTE$ +"arrow" + #DQUOTE$ + #CR$
  html + "  object1.style.color = " + #DQUOTE$ +"#4080FF" + #DQUOTE$ + #CR$
  html + "  object1.style.backgroundcolor = " + #DQUOTE$ +"#FFFFFF" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$ + #CR$

  html + "sub object1_onClick()" + #CR$
  html + "  window.location = " + #DQUOTE$ +"web3" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$
  html + "</script>" + #CR$
  html + "</head>" + #CR$

  html + "<body bgcolor = '#FFFFFF' scroll = 'no'>" + #CR$ + #CR$
  html + "<span id = 'object1'" + #CR$ 
  html + "  style = 'position:absolute; left:20; top:0; width:100; height:22; color:#4080FF'>" + #CR$
  html + "  upper panel" + #CR$
  html + "</span>" + #CR$
  html + "</body></html>"
  procedureReturn html
endProcedure

procedure.s web4Page1()
  ;html
  html.s + "<html><head>" + #CR$
  html + "<style type = 'text/css'>" + #CR$
  html + "  body{font-family:system; font-size:18px; font-weight:800}" + #CR$
  html + "</style>" + #CR$ + #CR$

  html + "<script language = 'vbscript'>" + #CR$
  html + "sub object1_onMouseOver()" + #CR$
  html + "  object1.style.cursor = " + #DQUOTE$ +"hand" + #DQUOTE$ + #CR$
	html + "  object1.style.color = " + #DQUOTE$ +"#FFFFFF" + #DQUOTE$ + #CR$
  html + "  object1.style.backgroundcolor = " + #DQUOTE$ +"#4080FF" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$ + #CR$

  html + "sub object1_onMouseOut()" + #CR$
  html + "  object1.style.cursor = " + #DQUOTE$ +"arrow" + #DQUOTE$ + #CR$
  html + "  object1.style.color = " + #DQUOTE$ +"#4080FF" + #DQUOTE$ + #CR$
  html + "  object1.style.backgroundcolor = " + #DQUOTE$ +"#FFFFFF" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$ + #CR$

  html + "sub object1_onClick()" + #CR$
  html + "  window.location = " + #DQUOTE$ +"web4" + #DQUOTE$ + #CR$
  html + "end sub" + #CR$
  html + "</script>" + #CR$
  html + "</head>" + #CR$

  html + "<body bgcolor = '#FFFFFF' scroll = 'no'>" + #CR$ + #CR$
  html + "<span id = 'object1'" + #CR$ 
  html + "  style = 'position:absolute; left:20; top:0; width:100; height:22; color:#4080FF'>" + #CR$
  html + "  lower panel" + #CR$
  html + "</span>" + #CR$
  html + "</body></html>"
  procedureReturn html
endProcedure
Post Reply