home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / ubrowser.exe / ubrowser / embeddedbrowserwindow.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-02-21  |  5.8 KB  |  186 lines

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  uBrowser - a program that illustrates one way of embedding the
  4. //  Mozilla Gecko (tm) Rendering Engine in an application, grabbing the
  5. //  rendered output and displaying it on the surface of a 3D polygon as
  6. //  texture in an OpenGL (tm) application.
  7. //
  8. //  uBrowser is free software; you can redistribute it and/or modify
  9. //  it under the terms of the GNU General Public License as published by
  10. //  the Free Software Foundation; either version 2 of the License, or
  11. //  (at your option) any later version.
  12. //
  13. //  uBrowser is distributed in the hope that it will be useful,
  14. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. //  GNU General Public License for more details.
  17. //
  18. //  You should have received a copy of the GNU General Public License
  19. //  along with uBrowser; if not, write to the Free Software
  20. //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. //
  22. //  Original code:  Copyright 2005 Linden Research Inc.
  23. //                  http://www.lindenlab.com
  24. //
  25. //  Primary author and site maintainer: Callum Prentice (callum@ubrowser.com)
  26. //
  27. //  See contributors.txt or http://ubrowser.com for a list of contributors
  28. //  without whose generous donation of time and effort, this application
  29. //  would not have been possible.
  30. //
  31. ////////////////////////////////////////////////////////////////////////////////
  32. #ifndef EMBEDDEDBROWSERWINDOW_H
  33. #define EMBEDDEDBROWSERWINDOW_H
  34.  
  35. #include "nsIBaseWindow.h"
  36. #include "nsIInterfaceRequestor.h"
  37. #include "nsIWebBrowserChrome.h"
  38. #include "nsIWebNavigation.h"
  39. #include "nsIWebProgressListener.h"
  40. #include "nsWeakReference.h"
  41. #include "nsIWebBrowser.h"
  42.  
  43. #include <algorithm>
  44. #include <typeinfo>
  45. #include <list>
  46.  
  47. // annoying, harmless warning we can't get rid of in MSVC.net
  48. // (no matching operator delete found; memory will not be freed if initialization throws an exception)
  49. #pragma warning ( disable : 4291 )
  50.  
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // manages the process of sotring and emitting events that the consumer
  53. // of the embedding class can observe
  54. template < class T >
  55. class embeddedBrowserWindowEmitter
  56. {
  57.     public:
  58.         embeddedBrowserWindowEmitter() { };
  59.         ~embeddedBrowserWindowEmitter() { };
  60.  
  61.         typedef typename T::EventType EventType;
  62.         typedef std::list< T* > ObserverContainer;
  63.         typedef void ( T::*observerMethod )( const EventType& );
  64.  
  65.         ///////////////////////////////////////////////////////////////////////////////
  66.         //
  67.         bool addObserver( T* observerIn )
  68.         {
  69.             if( ! observerIn )
  70.                 return false;
  71.  
  72.             if( std::find( observers.begin(), observers.end(), observerIn ) != observers.end() )
  73.                 return false;
  74.  
  75.             observers.push_back( observerIn );
  76.  
  77.             return true;
  78.         };
  79.  
  80.         ///////////////////////////////////////////////////////////////////////////////
  81.         //
  82.         bool remObserver( T* observerIn )
  83.         {
  84.             if( ! observerIn )
  85.                 return false;
  86.  
  87.             observers.remove( observerIn );
  88.  
  89.             return true;
  90.         };
  91.  
  92.         ///////////////////////////////////////////////////////////////////////////////
  93.         //
  94.         void update( observerMethod method, const EventType& msgIn )
  95.         {
  96.             typename std::list< T* >::iterator iter = observers.begin();
  97.  
  98.             while( iter != observers.end() )
  99.             {
  100.                 ( ( *iter )->*method )( msgIn );
  101.  
  102.                 ++iter;
  103.             };
  104.         };
  105.  
  106.     protected:
  107.         ObserverContainer observers;
  108. };
  109.  
  110. class embeddedBrowserWindow;
  111.  
  112. ////////////////////////////////////////////////////////////////////////////////
  113. // data class that is passed with an event - used in this case merely to
  114. // access the instance of the emitting class
  115. class embeddedBrowserWindowEvent
  116. {
  117.     public:
  118.         embeddedBrowserWindowEvent( embeddedBrowserWindow* payloadIn ):
  119.             payload( payloadIn )
  120.         {
  121.         };
  122.  
  123.         virtual ~embeddedBrowserWindowEvent()
  124.         {
  125.         };
  126.  
  127.         embeddedBrowserWindow* getPayload() const
  128.         {
  129.             return payload;
  130.         };
  131.  
  132.     private:
  133.         embeddedBrowserWindow* payload;
  134. };
  135.  
  136. ////////////////////////////////////////////////////////////////////////////////
  137. // the app class derrives from this and overrrides the events it wishes to observe
  138. class embeddedBrowserWindowObserver
  139. {
  140.     public:
  141.         typedef embeddedBrowserWindowEvent EventType;
  142.         virtual void onNavigateBegin( const EventType& eventIn ) { }
  143.         virtual void onNavigateComplete( const EventType& eventIn ) { }
  144.         virtual void onUpdateProgress( const EventType& eventIn ) { }
  145.         virtual void onStatusTextChange( const EventType& eventIn ) { }
  146.         virtual void onLocationChange( const EventType& eventIn ) { }
  147. };
  148.  
  149. class embeddedBrowser;
  150.  
  151. ////////////////////////////////////////////////////////////////////////////////
  152. // class for a "window" that holds a browser - eventually there will be lots of these
  153. class embeddedBrowserWindow :
  154.     public nsIInterfaceRequestor,
  155.     public nsIWebBrowserChrome,
  156.     public nsIWebProgressListener,
  157.     public nsSupportsWeakReference
  158. {
  159.     public:
  160.         embeddedBrowserWindow();
  161.         ~embeddedBrowserWindow();
  162.  
  163.         NS_DECL_ISUPPORTS
  164.         NS_DECL_NSIINTERFACEREQUESTOR
  165.         NS_DECL_NSIWEBBROWSERCHROME
  166.         NS_DECL_NSIWEBPROGRESSLISTENER
  167.  
  168.         NS_METHOD Init( embeddedBrowser* browserIn, nsIWebBrowser* webBrowserIn );
  169.  
  170.         const PRInt16 getPercentComplete();
  171.         const std::string& getStatusMsg();
  172.         const std::string& getCurrentUri();
  173.  
  174.         bool addObserver( embeddedBrowserWindowObserver* observerIn );
  175.         bool remObserver( embeddedBrowserWindowObserver* observerIn );
  176.  
  177.     private:
  178.         nsCOMPtr< nsIWebBrowser > mWebBrowser;
  179.         embeddedBrowser* mEmbedBrowser;
  180.         embeddedBrowserWindowEmitter< embeddedBrowserWindowObserver > mEventEmitter;
  181.         PRInt16 mPercentComplete;
  182.         std::string mStatusText;
  183.         std::string mCurrentUri;
  184. };
  185.  
  186. #endif // EMBEDEDDBROWSERWINDOW_H