Page 1 of 1

Set Focus On WebGadget

Posted: Sat Sep 30, 2006 12:49 pm
by Christian
Hi there! :)

I got a problem with the WebGadget. As the title of this thread says I want to put the focus on a WebGadget when the gadget is created or a specific event has happend. Unfortunately the SetActiveGadget() function doesn't work with the WebGadget. So I did some research in the German and English board and found some code that shows how you can deal with the WebGadget using interfaces.
I used a code snippet that shows how to scroll in a WebGadget and changed it a bit to get a procedure with which I can put the focus on the WebGadget:

Code: Select all


DataSection 
  IID_IHTMLElement: ; {3050F1FF-98B5-11CF-BB82-00AA00BDCE0B} 
    Data.l $3050F1FF 
    Data.w $98B5, $11CF 
    Data.b $BB, $82, $00, $AA, $00, $BD, $CE, $0B 
  IID_IHTMLDocument2: ; {332C4425-26CB-11D0-B483-00C04FD90119} 
    Data.l $332C4425 
    Data.w $26CB, $11D0 
    Data.b $B4, $83, $00, $C0, $4F, $D9, $01, $19 
EndDataSection 



;- Webgadget Functions 
Procedure ScrollWebGadget(Gadget.l, x.l, y.l) 
  Browser.IWebBrowser2 = GetWindowLong_(GadgetID(Gadget), #GWL_USERDATA) 
  If Browser 
    If Browser\get_document(@DocumentDispatch.IDispatch) = #S_OK 
      If DocumentDispatch\QueryInterface(?IID_IHTMLDocument2, @Document.IHTMLDocument2) = #S_OK            
        If Document\get_parentWindow(@window.IHTMLWindow2) = #S_OK 
          
          While WindowEvent(): Wend ; wichtig! 
          window\scrollTo(x, y)          
          
          window\Release() 
        EndIf                    
        Document\Release() 
      EndIf          
      DocumentDispatch\Release() 
    EndIf 
  EndIf    
EndProcedure 

Procedure SetActiveWebGadget(Gadget.l) 
  Browser.IWebBrowser2 = GetWindowLong_(GadgetID(Gadget), #GWL_USERDATA) 
  If Browser 
    If Browser\get_document(@DocumentDispatch.IDispatch) = #S_OK 
      If DocumentDispatch\QueryInterface(?IID_IHTMLDocument2, @Document.IHTMLDocument2) = #S_OK            
        If Document\get_parentWindow(@window.IHTMLWindow2) = #S_OK 
          
          While WindowEvent(): Wend ; wichtig! 
          window\focus()          
          
          window\Release() 
        EndIf                    
        Document\Release() 
      EndIf          
      DocumentDispatch\Release() 
    EndIf 
  EndIf    
EndProcedure 

#WebGadget = 0 
#ButtonGadget = 1 

If OpenWindow(0, 0, 0, 800, 600,"WebGadget example", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
  If CreateGadgetList(WindowID(0)) 
      WebGadget(#WebGadget, 0, 0, 800, 600-25, "http://www.purebasic.fr/german/viewtopic.php?p=113667&sid=e5f3d3139620333841d907a61837e211#113667")      

      ScrollWebGadget(#WebGadget, 0, 250) 
      SetActiveWebGadget(#WebGadget) 
    
    
    Repeat 
      WindowEvent = WaitWindowEvent() 
    Until WindowEvent = #PB_Event_CloseWindow    
    
  EndIf 
EndIf 

This code works perfectly ... under PureBasic 3.94. But it does not on the new version 4.00. Unfortunately I really need it to work under the latter version. Does anyone have a clue were the problem is? Is it a bug of PB 4 or do have to change something to get it working under PB4?

Would be great if anyone could help me because I really need this SetFocus stuff for a WebGadget!

Thanks in advance,
Christian

Posted: Sat Sep 30, 2006 2:37 pm
by freak
Two mistakes:

> If Browser\get_document(@DocumentDispatch.IDispatch) = #S_OK

You do not only need to check for the S_OK, but also if DocumentDispatch is 0 or not after the call.
That is because when there is no document, or it is not fully loaded, you get a S_OK
result, but the pointer remains empty. (very weird behaviour, but thats how it is)

Second: You need to wait until the document is loaded before using this type of code with it.
Thats why it does not work. (should be the same in 3.94 though actually)

Try this to wait for the document to load:

Code: Select all

      Browser.IWebBrowser2 = GetWindowLong_(GadgetID(#WebGadget), #GWL_USERDATA)
      Repeat
        While WindowEvent(): Wend 
        Browser\get_Busy(@busy.l)
        If busy = #VARIANT_TRUE
          Delay(10)
        EndIf
      Until busy = #VARIANT_FALSE

Posted: Sat Sep 30, 2006 2:50 pm
by Joakim Christiansen
I got invalid memory access, that's the problem or is it only me?
I think you also have this bug in your WebGadgetExtra "lib" Freak, since one user reported a error at exactly that line.

Posted: Sat Sep 30, 2006 3:17 pm
by Christian
Thanks freak! This helped me a lot! It works perfectly now with external links (http://), but it still has some problems with local .htm files. So if I define a link like "file:///C:/Programme/NetMeeting/netmeet.htm" for example the focus isn't set ...

If I call the defined procedure SetActiveWebGadget() a second time then the focus is set.

Any idea what could be the problem here?

@Joakim Christiansen:
With which of the Code snippets do you get a invalid memory access? With mine? Then that probably is because of the mistake that freak mentioned. Try this:

Code: Select all

DataSection 
  IID_IHTMLElement: ; {3050F1FF-98B5-11CF-BB82-00AA00BDCE0B} 
    Data.l $3050F1FF 
    Data.w $98B5, $11CF 
    Data.b $BB, $82, $00, $AA, $00, $BD, $CE, $0B 
  IID_IHTMLDocument2: ; {332C4425-26CB-11D0-B483-00C04FD90119} 
    Data.l $332C4425 
    Data.w $26CB, $11D0 
    Data.b $B4, $83, $00, $C0, $4F, $D9, $01, $19 
EndDataSection 



;- Webgadget Functions 
Procedure ScrollWebGadget(Gadget.l, x.l, y.l) 
  Browser.IWebBrowser2 = GetWindowLong_(GadgetID(Gadget), #GWL_USERDATA) 
  ; - Warten bis das Dokument vollständig geladen wurde
  Repeat 
    While WindowEvent(): Wend 
    Browser\get_Busy(@busy.l) 
    If busy = #VARIANT_TRUE 
        Delay(10) 
    EndIf 
  Until busy = #VARIANT_FALSE

  If Browser <> 0
    If Browser\get_document(@DocumentDispatch.IDispatch) = #S_OK And DocumentDispatch <> 0
        If DocumentDispatch\QueryInterface(?IID_IHTMLDocument2, @Document.IHTMLDocument2) = #S_OK And Document <> 0
          If Document\get_parentWindow(@window.IHTMLWindow2) = #S_OK And window <> 0
              While WindowEvent(): Wend

              ; - Fokus setzen
              window\ScrollTo(x, y)
  
            window\Release() 
          EndIf                    
          Document\Release() 
        EndIf          
        DocumentDispatch\Release() 
    EndIf 
  EndIf       
EndProcedure 

Procedure SetActiveWebGadget(Gadget.l) 
  Browser.IWebBrowser2 = GetWindowLong_(GadgetID(Gadget), #GWL_USERDATA) 
  ; - Warten bis das Dokument vollständig geladen wurde
  Repeat 
    While WindowEvent(): Wend 
    Browser\get_Busy(@busy.l) 
    If busy = #VARIANT_TRUE 
      Delay(10) 
    EndIf 
  Until busy = #VARIANT_FALSE

  If Browser <> 0
    If Browser\get_document(@DocumentDispatch.IDispatch) = #S_OK And DocumentDispatch <> 0
        If DocumentDispatch\QueryInterface(?IID_IHTMLDocument2, @Document.IHTMLDocument2) = #S_OK And Document <> 0
          If Document\get_parentWindow(@window.IHTMLWindow2) = #S_OK And window <> 0
              ;While WindowEvent(): Wend

              ; - Fokus setzen
              window\focus()

            window\Release() 
          EndIf                    
          Document\Release() 
        EndIf          
        DocumentDispatch\Release() 
    EndIf 
  EndIf    
EndProcedure 

#WebGadget = 0 
#ButtonGadget = 1 

If OpenWindow(0, 0, 0, 800, 600,"WebGadget example", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
  If CreateGadgetList(WindowID(0)) 
      WebGadget(#WebGadget, 0, 0, 800, 600, "http://www.purebasic.fr/english/viewtopic.php?p=163708#163708")     

      SetActiveWebGadget(#WebGadget)

    Repeat 
      WindowEvent = WaitWindowEvent() 
    Until WindowEvent = #PB_Event_CloseWindow    
    
  EndIf 
EndIf
Best regards,
Christian

Posted: Sat Sep 30, 2006 3:26 pm
by Joakim Christiansen
Christian wrote:@Joakim Christiansen:
With which of the Code snippets do you get a invalid memory access? With mine? Then that probably is because of the mistake that freak mentioned.
With your code, but a user of my internet tv program also had this invalid memory access bug with some code I used from Freak.
I'm glad I saw this thread because I can finnaly fix that bug in my program, and I hope Freak updates his "lib".

And your new code works great!

Re: Set Focus On WebGadget

Posted: Wed Jun 25, 2008 2:57 pm
by PB
I just needed to use SetActiveGadget with a WebGadget today, but the code
above is a bit much. Any chance of getting SetActiveGadget to work with it
natively, Fred/Freak? :)