Block oncontextmenu

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

Block oncontextmenu

Post by utopiomania »

Hi, I use html for the user interface in my latest program, and this is the only
method i found to block the right click menu, the commented out code:

Code: Select all

   s + "<script language=JavaScript>" + Chr(13)
   s + "" + Chr(13)
;    s + "<!-- disable contextmenu -->" + Chr(13)
;    s + "" + Chr(13)
;    s + "function nocontextmenuIE(){" + Chr(13)
;    s + "if (navigator.appName == 'Microsoft Internet Explorer' && event.button == 2)" + Chr(13)
;    s + "  return false;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "function nocontextmenuNS(e){" + Chr(13)
;    s + "  if ((document.layers || document.getElementById && !document.all) && e.which == 2) {" + Chr(13)
;    s + "    return false;" + Chr(13)
;    s + "  }" + Chr(13)
;    s + "}" + Chr(13)
;    s + "if (document.layers){" + Chr(13)
;    s + "  document.captureEvents(Event.mousedown);" + Chr(13)
;    s + "  document.onmousedown = nocontextmenuNS;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "else if (document.all && !document.getElementById){" + Chr(13)
;    s + "  document.onmousedown = nocontextmenuIE;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "document.oncontextmenu = new Function('return false')" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- user interface event handler -->" + Chr(13)
   s + "function eventhandler(event) {" + Chr(13)
   s + "   window.navigate(event)" + Chr(13)
   s + "}" + Chr(13)
   s + "</script>" + Chr(13)
nicolaus
Enthusiast
Enthusiast
Posts: 456
Joined: Tue Aug 05, 2003 11:30 pm
Contact:

Re: Block oncontextmenu

Post by nicolaus »

Have you a working sample how do you use HTML as UI in PB?

Thanks,
Nico
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: Block oncontextmenu

Post by utopiomania »

Hi nicolaus, this is a working but very simple example. Right click and select view source to see the html,
the PB source reads the navigation, cancel it, then can reload the html user interface. With a bit of dhtml,
the possibilities are limitless, and you can build the interface any way you want. :)

Code: Select all

;-program notes
;-initialize
;-
Enumeration
  #WIN
  #WEB
EndEnumeration

Global winW, winH

Declare openMainWindow()
Declare navigationCallback(id, url.s)
Declare.s userInterfacePage1()

;-program entry
openMainWindow()

;-program event handler
Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_SizeWindow
;        winW = WindowWidth(#WIN)
;        winH = WindowHeight(#WIN)
;        ResizeGadget(#WEB, 0, 0, winW, winH)
    Case #PB_Event_CloseWindow
      ;-program exit
      ;-
      End
  EndSelect
ForEver

Procedure openMainWindow()
  ;$CF0001
  ExamineDesktops()
  winW = DesktopWidth(0)
  winH = DesktopHeight(0)
  If OpenWindow(#WIN, 0, 0, winW, winH, "", #PB_Window_BorderLess | #PB_Window_Maximize)
    WebGadget(#WEB, 0, 0, winW, winH, "")
    ;load the html
    SetGadgetItemText(#WEB, #PB_Web_HtmlCode, userInterfacePage1())
    ;callback to monitor window.navigate
    SetGadgetAttribute(#WEB, #PB_Web_NavigationCallback, @navigationCallback())
    ;SetGadgetAttribute(#WEB, #PB_Web_BlockPopupMenu, 1)
  EndIf
EndProcedure

Procedure navigationCallback(id, url.s)
  url = LCase(url)
  If FindString(url, "button01_clicked")
    MessageRequester("UI Event handler", url)
  ElseIf FindString(url, "button02_clicked")
    MessageRequester("UI Event handler", url)
    End
  EndIf
  ;block drag-drop
  ProcedureReturn 0
EndProcedure

Procedure.s userInterfacePage1()
  ;html
  s.s + "<!doctype html>" + Chr(13)
   s + "<html>" + Chr(13)
   s + "<head>" + Chr(13)
   s + "" + Chr(13)
   s + "<style>" + Chr(13)
   s + "<!-- body -->" + Chr(13)
   s + "body" + Chr(13)
   s + "{ background-color: #000000; color: #FFFFFF; " + Chr(13)
   s + "font-family:'consolas'; font-size:20px; font-style:bold }" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- div classes -->" + Chr(13)
   s + ".norm { background-color: #000000; color:#606060; position:fixed }" + Chr(13)
   s + ".over { background-color: #4040FF; color:#FFFFFF; position:fixed; cursor:hand }" + Chr(13)
   s + "</style>" + Chr(13)
   s + "" + Chr(13)
   s + "<script language=JavaScript>" + Chr(13)
   s + "" + Chr(13)
;    s + "<!-- disable contextmenu -->" + Chr(13)
;    s + "" + Chr(13)
;    s + "function nocontextmenuIE(){" + Chr(13)
;    s + "if (navigator.appName == 'Microsoft Internet Explorer' && event.button == 2)" + Chr(13)
;    s + "  return false;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "function nocontextmenuNS(e){" + Chr(13)
;    s + "  if ((document.layers || document.getElementById && !document.all) && e.which == 2) {" + Chr(13)
;    s + "    return false;" + Chr(13)
;    s + "  }" + Chr(13)
;    s + "}" + Chr(13)
;    s + "if (document.layers){" + Chr(13)
;    s + "  document.captureEvents(Event.mousedown);" + Chr(13)
;    s + "  document.onmousedown = nocontextmenuNS;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "else if (document.all && !document.getElementById){" + Chr(13)
;    s + "  document.onmousedown = nocontextmenuIE;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "document.oncontextmenu = new Function('return false')" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- user interface event handler -->" + Chr(13)
   s + "function eventhandler(event) {" + Chr(13)
   s + "   window.navigate(event)" + Chr(13)
   s + "}" + Chr(13)
   s + "</script>" + Chr(13)
   s + "" + Chr(13)
   s + "</head>" + Chr(13)
   s + "<body scroll = 'no'>" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- Button 1 -->" + Chr(13)
   s + "<div    style = " + Chr(34) + "left:40%; top:4px; width:150px; height:24px;" + Chr(34) + " " + Chr(13)
   s + "   class = " + Chr(34) + "norm" + Chr(34) + "; " + Chr(13)
   s + "   onmouseover = " + Chr(34) + "this.className = 'over'" + Chr(34) + "; " + Chr(13)
   s + "   onmouseout = " + Chr(34) + "this.className = 'norm'" + Chr(34) + ";" + Chr(13)
   s + "   onclick = eventhandler(" + Chr(34) + "Button01_CLICKED" + Chr(34) + "); >" + Chr(13)
   s + "   " + Chr(13)
   s + "   <center>Filer" + Chr(13)
   s + "</div>" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- Button 2 -->" + Chr(13)
   s + "<div style = " + Chr(34) + "left:48%; top:4px; width:150px; height:24px;" + Chr(34) + " " + Chr(13)
   s + "   class = " + Chr(34) + "norm" + Chr(34) + " " + Chr(13)
   s + "   onmouseover = " + Chr(34) + "this.className='over'" + Chr(34) + "; " + Chr(13)
   s + "   onmouseout = " + Chr(34) + "this.className='norm'" + Chr(34) + ";" + Chr(13)
   s + "   onclick = eventhandler(" + Chr(34) + "Button02_CLICKED" + Chr(34) + "); >" + Chr(13)
   s + "   " + Chr(13)
   s + "   <center>Innstill" + Chr(13)
   s + "</div>" + Chr(13)
   s + "" + Chr(13)
   s + "</body>" + Chr(13)
   s + "</html>" + Chr(13)   
   ProcedureReturn s
EndProcedure
nicolaus
Enthusiast
Enthusiast
Posts: 456
Joined: Tue Aug 05, 2003 11:30 pm
Contact:

Re: Block oncontextmenu

Post by nicolaus »

utopiomania wrote:Hi nicolaus, this is a working but very simple example. Right click and select view source to see the html,
the PB source reads the navigation, cancel it, then can reload the html user interface. With a bit of dhtml,
the possibilities are limitless, and you can build the interface any way you want. :)

Code: Select all

;-program notes
;-initialize
;-
Enumeration
  #WIN
  #WEB
EndEnumeration

Global winW, winH

Declare openMainWindow()
Declare navigationCallback(id, url.s)
Declare.s userInterfacePage1()

;-program entry
openMainWindow()

;-program event handler
Repeat
  event = WaitWindowEvent()
  Select event
    Case #PB_Event_SizeWindow
;        winW = WindowWidth(#WIN)
;        winH = WindowHeight(#WIN)
;        ResizeGadget(#WEB, 0, 0, winW, winH)
    Case #PB_Event_CloseWindow
      ;-program exit
      ;-
      End
  EndSelect
ForEver

Procedure openMainWindow()
  ;$CF0001
  ExamineDesktops()
  winW = DesktopWidth(0)
  winH = DesktopHeight(0)
  If OpenWindow(#WIN, 0, 0, winW, winH, "", #PB_Window_BorderLess | #PB_Window_Maximize)
    WebGadget(#WEB, 0, 0, winW, winH, "")
    ;load the html
    SetGadgetItemText(#WEB, #PB_Web_HtmlCode, userInterfacePage1())
    ;callback to monitor window.navigate
    SetGadgetAttribute(#WEB, #PB_Web_NavigationCallback, @navigationCallback())
    ;SetGadgetAttribute(#WEB, #PB_Web_BlockPopupMenu, 1)
  EndIf
EndProcedure

Procedure navigationCallback(id, url.s)
  url = LCase(url)
  If FindString(url, "button01_clicked")
    MessageRequester("UI Event handler", url)
  ElseIf FindString(url, "button02_clicked")
    MessageRequester("UI Event handler", url)
    End
  EndIf
  ;block drag-drop
  ProcedureReturn 0
EndProcedure

Procedure.s userInterfacePage1()
  ;html
  s.s + "<!doctype html>" + Chr(13)
   s + "<html>" + Chr(13)
   s + "<head>" + Chr(13)
   s + "" + Chr(13)
   s + "<style>" + Chr(13)
   s + "<!-- body -->" + Chr(13)
   s + "body" + Chr(13)
   s + "{ background-color: #000000; color: #FFFFFF; " + Chr(13)
   s + "font-family:'consolas'; font-size:20px; font-style:bold }" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- div classes -->" + Chr(13)
   s + ".norm { background-color: #000000; color:#606060; position:fixed }" + Chr(13)
   s + ".over { background-color: #4040FF; color:#FFFFFF; position:fixed; cursor:hand }" + Chr(13)
   s + "</style>" + Chr(13)
   s + "" + Chr(13)
   s + "<script language=JavaScript>" + Chr(13)
   s + "" + Chr(13)
;    s + "<!-- disable contextmenu -->" + Chr(13)
;    s + "" + Chr(13)
;    s + "function nocontextmenuIE(){" + Chr(13)
;    s + "if (navigator.appName == 'Microsoft Internet Explorer' && event.button == 2)" + Chr(13)
;    s + "  return false;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "function nocontextmenuNS(e){" + Chr(13)
;    s + "  if ((document.layers || document.getElementById && !document.all) && e.which == 2) {" + Chr(13)
;    s + "    return false;" + Chr(13)
;    s + "  }" + Chr(13)
;    s + "}" + Chr(13)
;    s + "if (document.layers){" + Chr(13)
;    s + "  document.captureEvents(Event.mousedown);" + Chr(13)
;    s + "  document.onmousedown = nocontextmenuNS;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "else if (document.all && !document.getElementById){" + Chr(13)
;    s + "  document.onmousedown = nocontextmenuIE;" + Chr(13)
;    s + "}" + Chr(13)
;    s + "document.oncontextmenu = new Function('return false')" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- user interface event handler -->" + Chr(13)
   s + "function eventhandler(event) {" + Chr(13)
   s + "   window.navigate(event)" + Chr(13)
   s + "}" + Chr(13)
   s + "</script>" + Chr(13)
   s + "" + Chr(13)
   s + "</head>" + Chr(13)
   s + "<body scroll = 'no'>" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- Button 1 -->" + Chr(13)
   s + "<div    style = " + Chr(34) + "left:40%; top:4px; width:150px; height:24px;" + Chr(34) + " " + Chr(13)
   s + "   class = " + Chr(34) + "norm" + Chr(34) + "; " + Chr(13)
   s + "   onmouseover = " + Chr(34) + "this.className = 'over'" + Chr(34) + "; " + Chr(13)
   s + "   onmouseout = " + Chr(34) + "this.className = 'norm'" + Chr(34) + ";" + Chr(13)
   s + "   onclick = eventhandler(" + Chr(34) + "Button01_CLICKED" + Chr(34) + "); >" + Chr(13)
   s + "   " + Chr(13)
   s + "   <center>Filer" + Chr(13)
   s + "</div>" + Chr(13)
   s + "" + Chr(13)
   s + "<!-- Button 2 -->" + Chr(13)
   s + "<div style = " + Chr(34) + "left:48%; top:4px; width:150px; height:24px;" + Chr(34) + " " + Chr(13)
   s + "   class = " + Chr(34) + "norm" + Chr(34) + " " + Chr(13)
   s + "   onmouseover = " + Chr(34) + "this.className='over'" + Chr(34) + "; " + Chr(13)
   s + "   onmouseout = " + Chr(34) + "this.className='norm'" + Chr(34) + ";" + Chr(13)
   s + "   onclick = eventhandler(" + Chr(34) + "Button02_CLICKED" + Chr(34) + "); >" + Chr(13)
   s + "   " + Chr(13)
   s + "   <center>Innstill" + Chr(13)
   s + "</div>" + Chr(13)
   s + "" + Chr(13)
   s + "</body>" + Chr(13)
   s + "</html>" + Chr(13)   
   ProcedureReturn s
EndProcedure

An dieser Stelle mal ein großen Dank an dich für die schnelle Antwort und das verständliche Sample!
Echt ne geniale Idee, sollte man ein PB-Framework drauß machen :-D
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: Block oncontextmenu

Post by utopiomania »

Thanks, go here http://www.dhteumeuleu.com/index for inspiration. Click the play button, then click in the demos, or drag the worlds around.. Click the name, select source to see it once you are in the demo. This source can be put in the webgadget too.
I use the webgadget to create a 'less is more' interface for my new program, text style with coloured fields, (and with limitless possibilities) to make it look like Windows 8 or 10. 8)
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Re: Block oncontextmenu

Post by utopiomania »

He he :oops: I also noticed this commented-out line in my sample code, which uncommented does what my tips & tricks code does,
but in a much simpler way:

Code: Select all

;SetGadgetAttribute(#WEB, #PB_Web_BlockPopupMenu, 1)
Way to go. I think i'm a bit rusty in the ways of PureBasic. :)
Post Reply