BrowserWindow.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 $Id$
00004 
00005 */
00006 
00007 #include <gtk/gtk.h>
00008 #include <math.h>
00009 
00010 #include <nsCOMPtr.h>
00011 #include <nsStringAPI.h>
00012 #include <nsIDOMDocument.h>
00013 #include <nsIDOMWindowInternal.h>
00014 #include <nsIWebBrowser.h>
00015 #include <nsIScrollable.h>
00016 
00017 #include "Error.h"
00018 
00019 #include "BrowserWindow.h"
00020 
00021 // Public methods.
00022 
00023 BrowserWindow::BrowserWindow(const String &profilePath, const unsigned int chromeMask) : Object(),
00024   _topLevelWindow(NULL),
00025   _topLevelVBox(NULL),
00026   _mozEmbed(NULL),
00027   _progressAreaHBox(NULL),
00028   _statusAlign(NULL),
00029   _statusBar(NULL),
00030   _progressAlign(NULL),
00031   _progressBar(NULL),
00032   _statusBarVisible(true),
00033   _progressBarVisible(true),
00034   _progressBarAutoMode(false),
00035   _status()
00036 {
00037   gtk_moz_embed_set_profile_path(CSTRING(profilePath), ".");
00038 
00039   if (!(_topLevelWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL))) {
00040     failure(ERROR(MSG_OBJECT_CANNOT_CREATE, "GtkWindow"));
00041     return;
00042   }
00043 
00044   _topLevelVBox = gtk_vbox_new(FALSE, 0);
00045   gtk_container_add(GTK_CONTAINER(_topLevelWindow), _topLevelVBox);
00046 
00047   if (!(_mozEmbed = gtk_moz_embed_new())) {
00048     failure(ERROR(MSG_OBJECT_CANNOT_CREATE, "GtkMozEmbed"));
00049     return;
00050   }
00051   gtk_box_pack_start(GTK_BOX(_topLevelVBox), _mozEmbed, TRUE, TRUE, 0);
00052 
00053   _progressAreaHBox = gtk_hbox_new(FALSE, 0);
00054   gtk_box_pack_start(GTK_BOX(_topLevelVBox), _progressAreaHBox, FALSE, FALSE, 0);
00055 
00056   _statusAlign = gtk_alignment_new(0, 0, 1, 1);
00057   gtk_widget_set_usize(_statusAlign, 1, -1);
00058   _statusBar = gtk_statusbar_new();
00059   gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(_statusBar), FALSE);
00060   gtk_container_add(GTK_CONTAINER(_statusAlign), _statusBar);
00061   gtk_box_pack_start(GTK_BOX(_progressAreaHBox), _statusAlign, TRUE, TRUE, 0);
00062 
00063   _progressAlign = gtk_alignment_new(1, 0, 1, 1);
00064   gtk_widget_set_usize(_progressAlign, -1, -1);
00065   _progressBar = gtk_progress_bar_new();
00066   gtk_container_add(GTK_CONTAINER(_progressAlign), _progressBar);
00067   gtk_box_pack_start(GTK_BOX(_progressAreaHBox), _progressAlign, FALSE, FALSE, 0);
00068 
00069   // Catch the destruction of the toplevel window.
00070   gtk_signal_connect(GTK_OBJECT(_topLevelWindow), "delete_event", GTK_SIGNAL_FUNC(BrowserWindow::_onDelete), this);
00071   // Hook up the location change.
00072   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "location", GTK_SIGNAL_FUNC(BrowserWindow::_onLocationChanged), this);
00073   // Hook up the title change to update the window title.
00074   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "title", GTK_SIGNAL_FUNC(BrowserWindow::_onTitleChanged), this);
00075   // Hook up the start and stop signals.
00076   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "net_start", GTK_SIGNAL_FUNC(BrowserWindow::_onLoadStarted), this);
00077   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "net_stop", GTK_SIGNAL_FUNC(BrowserWindow::_onLoadFinished), this);
00078   // Hook up to the change in network status.
00079   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "net_state", GTK_SIGNAL_FUNC(BrowserWindow::_onNetStateChange), this);
00080   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "net_state_all", GTK_SIGNAL_FUNC(BrowserWindow::_onNetStateChangeAll), this);
00081   // Hookup to changes in progress.
00082   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "progress", GTK_SIGNAL_FUNC(BrowserWindow::_onProgressChange), this);
00083   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "progress_all", GTK_SIGNAL_FUNC(BrowserWindow::_onProgressChangeAll), this);
00084   // Hookup to changes in over-link message.
00085   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "link_message", GTK_SIGNAL_FUNC(BrowserWindow::_onLinkMessage), this);
00086   // Hookup to changes in js status message.
00087   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "js_status", GTK_SIGNAL_FUNC(BrowserWindow::_onJSStatus), this);
00088   // Hookup to see whenever a new window is requested.
00089   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "new_window", GTK_SIGNAL_FUNC(BrowserWindow::_onNewWindow), this);
00090   // Hookup to any requested visibility changes.
00091   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "visibility", GTK_SIGNAL_FUNC(BrowserWindow::_onVisibility), this);
00092   // Hookup to the signal that says that the browser requested to be destroyed.
00093   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "destroy_browser", GTK_SIGNAL_FUNC(BrowserWindow::_onDestroyBrowser), this);
00094   // Hookup to the signal that is called when someone clicks on a link to load a new uri.
00095   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "open_uri", GTK_SIGNAL_FUNC(BrowserWindow::_onOpenUri), this);
00096   // This signal is emitted when there's a request to change the
00097   // containing browser window to a certain height, like with width
00098   // and height args for a window.open in javascript.
00099   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "size_to", GTK_SIGNAL_FUNC(BrowserWindow::_onSizeTo), this);
00100 
00101   // Keys and mouse event signals.
00102   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_key_down", GTK_SIGNAL_FUNC(BrowserWindow::_onKeyDown), this);
00103   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_key_press", GTK_SIGNAL_FUNC(BrowserWindow::_onKeyPress), this);
00104   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_key_up", GTK_SIGNAL_FUNC(BrowserWindow::_onKeyUp), this);
00105   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_mouse_down", GTK_SIGNAL_FUNC(BrowserWindow::_onMouseDown), this);
00106   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_mouse_up", GTK_SIGNAL_FUNC(BrowserWindow::_onMouseUp), this);
00107   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_mouse_click", GTK_SIGNAL_FUNC(BrowserWindow::_onMouseClick), this);
00108   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_mouse_dbl_click", GTK_SIGNAL_FUNC(BrowserWindow::_onMouseDoubleClick), this);
00109   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_mouse_over", GTK_SIGNAL_FUNC(BrowserWindow::_onMouseOver), this);
00110   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_mouse_out", GTK_SIGNAL_FUNC(BrowserWindow::_onMouseOut), this);
00111 
00112   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "security_change", GTK_SIGNAL_FUNC(BrowserWindow::_onSecurityChange), this);
00113 
00114   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_activate", GTK_SIGNAL_FUNC(BrowserWindow::_onActivate), this);
00115   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_focus_in", GTK_SIGNAL_FUNC(BrowserWindow::_onFocusIn), this);
00116   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "dom_focus_out", GTK_SIGNAL_FUNC(BrowserWindow::_onFocusOut), this);
00117 
00118   // Hookup to when the window is destroyed.
00119   gtk_signal_connect(GTK_OBJECT(_mozEmbed), "destroy", GTK_SIGNAL_FUNC(BrowserWindow::_onDestroy), this);
00120 
00121   // Set the chrome type so it's stored in the object.
00122   gtk_moz_embed_set_chrome_mask(GTK_MOZ_EMBED(_mozEmbed), chromeMask);
00123 
00124   // Get the singleton object and hook up to its new window callback
00125   // so we can create orphaned windows.
00126   GtkMozEmbedSingle *single;
00127 
00128   if (!(single = gtk_moz_embed_single_get())) {
00129     failure(ERROR(MSG_BROWSER_WINDOW_CANNOT_GET_SINGLETON_EMBED_OBJECT));
00130     return;
00131   }
00132 
00133   gtk_signal_connect(GTK_OBJECT(single), "new_window_orphan", GTK_SIGNAL_FUNC(BrowserWindow::_onNewWindowOrphan), this);
00134 }
00135 
00136 BrowserWindow::~BrowserWindow()
00137 {
00138   gtk_widget_destroy(_topLevelWindow);
00139 }
00140 
00141 int BrowserWindow::mozillaWidget(GtkWidget **mozilla)
00142 {
00143   *mozilla = _mozEmbed;
00144   return OK;
00145 }
00146 
00147 int BrowserWindow::setVisibility(const bool visible)
00148 {
00149   int res;
00150 
00151   if (FAILED(res = visible ? show() : hide()))
00152     return ERROR_BACKTRACE(res);
00153 
00154   return OK;
00155 }
00156 
00157 int BrowserWindow::show()
00158 {
00159   if (_statusBarVisible)
00160     gtk_widget_show_all(_statusAlign);
00161   if (_progressBarVisible)
00162     gtk_widget_show_all(_progressAlign);
00163   gtk_widget_show(_progressAreaHBox);
00164   gtk_widget_show(_mozEmbed);
00165   gtk_widget_show(_topLevelVBox);
00166   gtk_widget_show(_topLevelWindow);
00167   EMIT(shown);
00168   return OK;
00169 }
00170 
00171 int BrowserWindow::hide()
00172 {
00173   gtk_widget_hide(_topLevelWindow);
00174   EMIT(hidden);
00175   return OK;
00176 }
00177 
00178 int BrowserWindow::setScrollBarsVisibility(const bool visible)
00179 {
00180   nsCOMPtr<nsIWebBrowser> b;
00181   nsCOMPtr<nsIScrollable> scrollable;
00182   int res;
00183 
00184   if (FAILED(res = browser(getter_AddRefs(b))))
00185     return ERROR_BACKTRACE(res);
00186 
00187   if (!(scrollable = do_QueryInterface(b)))
00188     return ERROR(MSG_NO_INTERFACE, "nsIScrollable");
00189 
00190   scrollable->SetDefaultScrollbarPreferences((PRInt32)nsIScrollable::ScrollOrientation_X,
00191                                              visible ? (PRInt32)nsIScrollable::Scrollbar_Auto : (PRInt32)nsIScrollable::Scrollbar_Never);
00192   scrollable->SetDefaultScrollbarPreferences((PRInt32)nsIScrollable::ScrollOrientation_Y,
00193                                              visible ? (PRInt32)nsIScrollable::Scrollbar_Auto : (PRInt32)nsIScrollable::Scrollbar_Never);
00194 
00195   return OK;
00196 }
00197 
00198 int BrowserWindow::setStatusBarVisibility(const bool visible)
00199 {
00200   int res;
00201 
00202   if (FAILED(res = visible ? showStatusBar() : hideStatusBar()))
00203     return ERROR_BACKTRACE(res);
00204 
00205   return OK;
00206 }
00207 
00208 int BrowserWindow::showStatusBar()
00209 {
00210   gtk_widget_show_all(_statusAlign);
00211   gtk_widget_show(_progressAreaHBox);
00212   gtk_box_set_child_packing(GTK_BOX(_progressAreaHBox), _progressAlign, FALSE, FALSE, 0, GTK_PACK_START);
00213   _statusBarVisible = true;
00214   return OK;
00215 }
00216 
00217 int BrowserWindow::hideStatusBar()
00218 {
00219   gtk_widget_hide_all(_statusAlign);
00220   gtk_box_set_child_packing(GTK_BOX(_progressAreaHBox), _progressAlign, TRUE, TRUE, 0, GTK_PACK_START);
00221   if (!_progressBarVisible)
00222     gtk_widget_hide(_progressAreaHBox);
00223   _statusBarVisible = false;
00224   return OK;
00225 }
00226 
00227 int BrowserWindow::setProgressBarVisibility(const bool visible)
00228 {
00229   int res;
00230 
00231   if (FAILED(res = visible ? showProgressBar() : hideProgressBar()))
00232     return ERROR_BACKTRACE(res);
00233 
00234   return OK;
00235 }
00236 
00237 int BrowserWindow::showProgressBar()
00238 {
00239   gtk_widget_show_all(_progressBar);
00240   gtk_widget_show(_progressAreaHBox);
00241   _progressBarVisible = true;
00242   return OK;
00243 }
00244 
00245 int BrowserWindow::hideProgressBar()
00246 {
00247   gtk_widget_hide_all(_progressBar);
00248   if (!_statusBarVisible)
00249     gtk_widget_hide(_progressAreaHBox);
00250   _progressBarVisible = false;
00251   return OK;
00252 }
00253 
00254 int BrowserWindow::setProgressBarAutoMode(const bool enabled)
00255 {
00256   _progressBarAutoMode = enabled;
00257   return OK;
00258 }
00259 
00260 int BrowserWindow::size(int &width, int &height)
00261 {
00262   gdk_window_get_size(_topLevelWindow->window, &width, &height);
00263   return OK;
00264 }
00265 
00266 int BrowserWindow::setSize(const int width, const int height)
00267 {
00268   int w, h;
00269 
00270   gdk_window_get_size(_topLevelWindow->window, &w, &h);
00271   if (width > 0)
00272     w = width;
00273   if (height > 0)
00274     h = height;
00275   gdk_window_resize(_topLevelWindow->window, w, h);
00276   return OK;
00277 }
00278 
00279 int BrowserWindow::position(int &x, int &y)
00280 {
00281   gdk_window_get_position(_topLevelWindow->window, &x, &y);
00282   return OK;
00283 }
00284 
00285 int BrowserWindow::setPos(const int x, const int y)
00286 {
00287   gdk_window_move(_topLevelWindow->window, x, y);
00288   return OK;
00289 }
00290 
00291 int BrowserWindow::setXPos(const int x)
00292 {
00293   int xPos, yPos;
00294 
00295   gdk_window_get_position(_topLevelWindow->window, &xPos, &yPos);
00296   gdk_window_move(_topLevelWindow->window, x, yPos);
00297   return OK;
00298 }
00299 
00300 int BrowserWindow::setYPos(const int y)
00301 {
00302   int xPos, yPos;
00303 
00304   gdk_window_get_position(_topLevelWindow->window, &xPos, &yPos);
00305   gdk_window_move(_topLevelWindow->window, xPos, y);
00306   return OK;
00307 }
00308 
00309 int BrowserWindow::textScale(unsigned int &scale)
00310 {
00311   nsCOMPtr<nsIDOMWindow> domWindow;
00312   nsresult res;
00313   float s;
00314 
00315   if (FAILED(res = topDomWindow(getter_AddRefs(domWindow))))
00316     return ERROR_BACKTRACE(res);
00317 
00318   if (NS_FAILED(res = domWindow->GetTextZoom(&s)))
00319     return ERROR_BACKTRACE(res);
00320 
00321   scale = (int)round(s * 100.0f);
00322 
00323   return OK;
00324 }
00325 
00326 int BrowserWindow::setTextScale(const unsigned int scale)
00327 {
00328   nsCOMPtr<nsIDOMWindow> domWindow;
00329   nsresult res;
00330 
00331   DEBUG("Setting text scale to %f.", (double)scale / 100.0f);
00332 
00333   if (FAILED(res = topDomWindow(getter_AddRefs(domWindow))))
00334     return ERROR_BACKTRACE(res);
00335 
00336   if (NS_FAILED(res = domWindow->SetTextZoom((double)scale / 100.0f)))
00337     return ERROR_BACKTRACE(res);
00338 
00339   return OK;
00340 }
00341 
00342 int BrowserWindow::load(const String& url)
00343 {
00344   DEBUG("Loading URL '%s'.", CSTRING(url));
00345   gtk_moz_embed_load_url(GTK_MOZ_EMBED(_mozEmbed), CSTRING(url));
00346 
00347   return OK;
00348 }
00349 
00350 int BrowserWindow::close()
00351 {
00352   int res;
00353   if (FAILED(res = onDelete()))
00354     return ERROR_BACKTRACE(res);
00355   return OK;
00356 }
00357 
00358 int BrowserWindow::focus()
00359 {
00360   nsCOMPtr<nsIDOMWindow> domWindow;
00361   nsCOMPtr<nsIDOMWindowInternal> domWindowInternal;
00362   nsresult res;
00363 
00364   if (FAILED(res = topDomWindow(getter_AddRefs(domWindow))))
00365     return ERROR_BACKTRACE(res);
00366 
00367   if (!(domWindowInternal = do_QueryInterface(domWindow)))
00368     return ERROR(MSG_NO_INTERFACE, "nsIDOMWindowInternal");
00369 
00370   if (NS_FAILED(res = domWindowInternal->Focus()))
00371     return ERROR_BACKTRACE(res);
00372   return OK;
00373 }
00374 
00375 int BrowserWindow::stop()
00376 {
00377   gtk_moz_embed_stop_load(GTK_MOZ_EMBED(_mozEmbed));
00378   return OK;
00379 }
00380 
00381 int BrowserWindow::canGoBack(bool &yes)
00382 {
00383   yes = gtk_moz_embed_can_go_back(GTK_MOZ_EMBED(_mozEmbed));
00384   return OK;
00385 }
00386 
00387 int BrowserWindow::back()
00388 {
00389   gtk_moz_embed_go_back(GTK_MOZ_EMBED(_mozEmbed));
00390   return OK;
00391 }
00392 
00393 int BrowserWindow::canGoForward(bool &yes)
00394 {
00395   yes = gtk_moz_embed_can_go_forward(GTK_MOZ_EMBED(_mozEmbed));
00396   return OK;
00397 }
00398 
00399 int BrowserWindow::forward()
00400 {
00401   gtk_moz_embed_go_forward(GTK_MOZ_EMBED(_mozEmbed));
00402   return OK;
00403 }
00404 
00405 int BrowserWindow::reload(const bool useCache)
00406 {
00407   gtk_moz_embed_reload(GTK_MOZ_EMBED(_mozEmbed),
00408     useCache ? GTK_MOZ_EMBED_FLAG_RELOADNORMAL : GTK_MOZ_EMBED_FLAG_RELOADBYPASSCACHE);
00409   return OK;
00410 }
00411 
00412 String BrowserWindow::status() const
00413 {
00414   return _status.message;
00415 }
00416 
00417 int BrowserWindow::loadPercent() const
00418 {
00419   return _status.loadPercent;
00420 }
00421 
00422 int BrowserWindow::bytesLoaded() const
00423 {
00424   return _status.bytesLoaded;
00425 }
00426 
00427 int BrowserWindow::maxBytesLoaded() const
00428 {
00429   return _status.maxBytesLoaded;
00430 }
00431 
00432 bool BrowserWindow::loadFinished() const
00433 {
00434   return _status.loadFinished;
00435 }
00436 
00437 int BrowserWindow::renderData(const String &data)
00438 {
00439   gtk_moz_embed_open_stream(GTK_MOZ_EMBED(_mozEmbed), "file://", "text/html");
00440   gtk_moz_embed_append_data(GTK_MOZ_EMBED(_mozEmbed), CSTRING(data), data.length());
00441   gtk_moz_embed_close_stream(GTK_MOZ_EMBED(_mozEmbed));
00442   return OK;
00443 }
00444 
00445 // Protected methods.
00446 
00447 int BrowserWindow::onDelete()
00448 {
00449   int res;
00450   if (FAILED(res = hide()))
00451     return ERROR_BACKTRACE(res);
00452   EMIT(closed);
00453   return OK;
00454 }
00455 
00456 int BrowserWindow::onDestroy()
00457 {
00458   return OK;
00459 }
00460 
00461 int BrowserWindow::onLocationChanged(const String &newLocation)
00462 {
00463   return OK;
00464 }
00465 
00466 int BrowserWindow::onTitleChanged(const String &newTitle)
00467 {
00468   gtk_window_set_title(GTK_WINDOW(_topLevelWindow), CSTRING(newTitle));
00469   return OK;
00470 }
00471 
00472 int BrowserWindow::onLoadStarted()
00473 {
00474   int res;
00475   DEBUG("Document loading started.");
00476   _status.loadPercent = 0;
00477   _status.bytesLoaded = 0;
00478   _status.maxBytesLoaded = 0;
00479   _status.loadFinished = false;
00480   if (FAILED(res = updateStatusBar()))
00481     return ERROR_BACKTRACE(res);
00482   EMIT(started);
00483   return OK;
00484 }
00485 
00486 int BrowserWindow::onLoadFinished()
00487 {
00488   int res;
00489 
00490   DEBUG("Document loading finished.");
00491   _status.loadPercent = 0;
00492   _status.bytesLoaded = 0;
00493   _status.maxBytesLoaded = 0;
00494   _status.loadFinished = true;
00495 
00496   if (FAILED(res = updateStatusBar()))
00497     return ERROR_BACKTRACE(res);
00498 
00499   gtk_progress_set_percentage(GTK_PROGRESS(_progressBar), 0);
00500 
00501   if (_progressBarAutoMode)
00502     if (FAILED(res = hideProgressBar()))
00503       return ERROR_BACKTRACE(res);
00504 
00505   EMIT(finished);
00506 
00507   return OK;
00508 }
00509 
00510 int BrowserWindow::onNetStateChange(const int flags, const unsigned int status)
00511 {
00512   int res;
00513 
00514   if (flags & GTK_MOZ_EMBED_FLAG_IS_REQUEST)
00515     if (flags & GTK_MOZ_EMBED_FLAG_REDIRECTING)
00516       _status.message = "Redirecting to site...";
00517     else
00518       if (flags & GTK_MOZ_EMBED_FLAG_TRANSFERRING)
00519         _status.message = "Transferring data from site...";
00520       else
00521         if (flags & GTK_MOZ_EMBED_FLAG_NEGOTIATING)
00522           _status.message = "Waiting for authorization...";
00523 
00524   if (status == GTK_MOZ_EMBED_STATUS_FAILED_DNS)
00525     _status.message = "Site not found.";
00526   else
00527     if (status == GTK_MOZ_EMBED_STATUS_FAILED_CONNECT)
00528       _status.message = "Failed to connect to site.";
00529     else
00530       if (status == GTK_MOZ_EMBED_STATUS_FAILED_TIMEOUT)
00531         _status.message = "Failed due to connection timeout.";
00532       else
00533         if (status == GTK_MOZ_EMBED_STATUS_FAILED_USERCANCELED)
00534           _status.message = "User canceled connecting to site.";
00535 
00536   if (flags & GTK_MOZ_EMBED_FLAG_IS_DOCUMENT)
00537     if (flags & GTK_MOZ_EMBED_FLAG_START)
00538       _status.message = "Loading site...";
00539     else
00540       if (flags & GTK_MOZ_EMBED_FLAG_STOP)
00541         _status.message = "Done.";
00542 
00543   if (FAILED(res = updateStatusBar()))
00544     return ERROR_BACKTRACE(res);
00545 
00546   return OK;
00547 }
00548 
00549 int BrowserWindow::onNetStateChangeAll(const char *uri, const int flags, unsigned int status)
00550 {
00551   return OK;
00552 }
00553 
00554 int BrowserWindow::onProgressChange(const int cur, const int max)
00555 {
00556   int res;
00557   if (max < 1) {
00558     gtk_progress_set_activity_mode(GTK_PROGRESS(_progressBar), FALSE);
00559     _status.loadPercent = 0;
00560     _status.bytesLoaded = cur;
00561     _status.maxBytesLoaded = 0;
00562     if (_progressBarAutoMode)
00563       if (FAILED(res = hideProgressBar()))
00564         return ERROR_BACKTRACE(res);
00565   } else {
00566       _status.bytesLoaded = cur;
00567       _status.maxBytesLoaded = max;
00568       if (cur > max)
00569         _status.loadPercent = 100;
00570       else
00571         _status.loadPercent = (cur * 100) / max;
00572       if (_progressBarAutoMode && !_progressBarVisible)
00573         if (FAILED(res = showProgressBar()))
00574           return ERROR_BACKTRACE(res);
00575       gtk_progress_set_percentage(GTK_PROGRESS(_progressBar), _status.loadPercent / 100.0);
00576     }
00577   if (FAILED(res = updateStatusBar()))
00578     return ERROR_BACKTRACE(res);
00579   return OK;
00580 }
00581 
00582 int BrowserWindow::onProgressChangeAll(const char *uri, const int cur, const int max)
00583 {
00584   return OK;
00585 }
00586 
00587 int BrowserWindow::onLinkMessage(const String &message)
00588 {
00589   int res;
00590   _status.tempMessage = message;
00591   if (FAILED(res = updateStatusBar()))
00592     return ERROR_BACKTRACE(res);
00593   return OK;
00594 }
00595 
00596 int BrowserWindow::onJSStatus(const String &message)
00597 {
00598   _status.tempMessage = message;
00599   return OK;
00600 }
00601 
00602 int BrowserWindow::onNewWindow(const unsigned int chromeMask, BrowserWindow **newBrowser)
00603 {
00604   if (!(*newBrowser = newWindow(chromeMask)))
00605     return ERROR(MSG_NULL_POINTER);
00606   return OK;
00607 }
00608 
00609 int BrowserWindow::onVisibility(const bool visibility)
00610 {
00611   int res;
00612   if (FAILED(res = setVisibility(visibility)))
00613     return ERROR_BACKTRACE(res);
00614   return OK;
00615 }
00616 
00617 int BrowserWindow::onDestroyBrowser()
00618 {
00619   int res;
00620   if (FAILED(res = close()))
00621     return ERROR_BACKTRACE(res);
00622   return OK;
00623 }
00624 
00625 int BrowserWindow::onOpenUri(const char *uri, bool &interrupt)
00626 {
00627   interrupt = false;
00628   return OK;
00629 }
00630 
00631 int BrowserWindow::onSizeTo(const int width, const int height)
00632 {
00633   gtk_widget_set_usize(_mozEmbed, width, height);
00634   return OK;
00635 }
00636 
00637 int BrowserWindow::onKeyDown(nsIDOMKeyEvent *event)
00638 {
00639   return OK;
00640 }
00641 
00642 int BrowserWindow::onKeyPress(nsIDOMKeyEvent *event)
00643 {
00644   return OK;
00645 }
00646 
00647 int BrowserWindow::onKeyUp(nsIDOMKeyEvent *event)
00648 {
00649   return OK;
00650 }
00651 
00652 int BrowserWindow::onMouseDown(nsIDOMMouseEvent *event)
00653 {
00654   return OK;
00655 }
00656 
00657 int BrowserWindow::onMouseUp(nsIDOMMouseEvent *event)
00658 {
00659   return OK;
00660 }
00661 
00662 int BrowserWindow::onMouseClick(nsIDOMMouseEvent *event)
00663 {
00664   return OK;
00665 }
00666 
00667 int BrowserWindow::onMouseDoubleClick(nsIDOMMouseEvent *event)
00668 {
00669   return OK;
00670 }
00671 
00672 int BrowserWindow::onMouseOver(nsIDOMMouseEvent *event)
00673 {
00674   return OK;
00675 }
00676 
00677 int BrowserWindow::onMouseOut(nsIDOMMouseEvent *event)
00678 {
00679   return OK;
00680 }
00681 
00682 int BrowserWindow::onActivate(nsIDOMUIEvent *event)
00683 {
00684   return OK;
00685 }
00686 
00687 int BrowserWindow::onFocusIn(nsIDOMUIEvent *event)
00688 {
00689   return OK;
00690 }
00691 
00692 int BrowserWindow::onFocusOut(nsIDOMUIEvent *event)
00693 {
00694   return OK;
00695 }
00696 
00697 BrowserWindow *BrowserWindow::newWindow(const unsigned int chromeMask)
00698 {
00699   BrowserWindow *newBrowser = new BrowserWindow("", chromeMask);
00700   int res;
00701 
00702   if (!newBrowser) {
00703     ERROR(MSG_OBJECT_CANNOT_CREATE, "BrowserWindow");
00704     return NULL;
00705   }
00706   if (FAILED(res = newBrowser->failureCode())) {
00707     ERROR_BACKTRACE(res);
00708     delete newBrowser;
00709     return NULL;
00710   }
00711   return newBrowser;
00712 }
00713 
00714 // Private methods.
00715 
00716 int BrowserWindow::browser(nsIWebBrowser **webBrowser)
00717 {
00718   nsCOMPtr<nsIWebBrowser> b;
00719 
00720   gtk_moz_embed_get_nsIWebBrowser(GTK_MOZ_EMBED(_mozEmbed), getter_AddRefs(b));
00721   if (!b)
00722     return ERROR(MSG_NULL_POINTER);
00723   *webBrowser = b;
00724   NS_ADDREF(*webBrowser);
00725 
00726   return OK;
00727 }
00728 
00729 int BrowserWindow::topDomWindow(nsIDOMWindow **window)
00730 {
00731   nsCOMPtr<nsIWebBrowser> b;
00732   nsCOMPtr<nsIDOMWindow> domWindow;
00733   nsresult res;
00734 
00735   if (FAILED(res = browser(getter_AddRefs(b))))
00736     return ERROR_BACKTRACE(res);
00737 
00738   if (NS_FAILED(res = b->GetContentDOMWindow(getter_AddRefs(domWindow))))
00739     return ERROR_BACKTRACE(res);
00740 
00741   *window = domWindow;
00742   NS_ADDREF(*window);
00743 
00744   return OK;
00745 }
00746 
00747 // Event handlers.
00748 
00749 gboolean BrowserWindow::_onDelete(GtkWidget *widget, GdkEventAny *event, BrowserWindow *browser)
00750 {
00751   int res;
00752   if (FAILED(res = browser->onDelete())) {
00753     ERROR_BACKTRACE(res);
00754     return FALSE;
00755   }
00756   return TRUE;
00757 }
00758 
00759 void BrowserWindow::_onDestroy(GtkWidget *widget, BrowserWindow *browser)
00760 {
00761   int res;
00762   if (FAILED(res = browser->onDestroy()))
00763     ERROR_BACKTRACE(res);
00764 }
00765 
00766 void BrowserWindow::_onLocationChanged(GtkMozEmbed *embed, BrowserWindow *browser)
00767 {
00768   char *newLocation = gtk_moz_embed_get_location(embed);
00769   int res;
00770 
00771   if (newLocation) {
00772     if (FAILED(res = browser->onLocationChanged(newLocation)))
00773       ERROR_BACKTRACE(res);
00774     g_free(newLocation);
00775   } else
00776       ERROR(MSG_BROWSER_WINDOW_LOCATION_CANNOT_GET);
00777 }
00778 
00779 void BrowserWindow::_onTitleChanged(GtkMozEmbed *embed, BrowserWindow *browser)
00780 {
00781   char *newTitle = gtk_moz_embed_get_title(embed);
00782   int res;
00783 
00784   if (FAILED(res = browser->onTitleChanged(newTitle)))
00785     ERROR_BACKTRACE(res);
00786   if (newTitle)
00787     g_free(newTitle);
00788 }
00789 
00790 void BrowserWindow::_onLoadStarted(GtkMozEmbed *embed, BrowserWindow *browser)
00791 {
00792   int res;
00793   if (FAILED(res = browser->onLoadStarted()))
00794     ERROR_BACKTRACE(res);
00795 }
00796 
00797 void BrowserWindow::_onLoadFinished(GtkMozEmbed *embed, BrowserWindow *browser)
00798 {
00799   int res;
00800   if (FAILED(res = browser->onLoadFinished()))
00801     ERROR_BACKTRACE(res);
00802 }
00803 
00804 void BrowserWindow::_onNetStateChange(GtkMozEmbed *embed, gint flags, guint status, BrowserWindow *browser)
00805 {
00806   int res;
00807   if (FAILED(res = browser->onNetStateChange(flags, status)))
00808     ERROR_BACKTRACE(res);
00809 }
00810 
00811 void BrowserWindow::_onNetStateChangeAll(GtkMozEmbed *embed, const char *uri, gint flags, guint status, BrowserWindow *browser)
00812 {
00813   int res;
00814   if (FAILED(res = browser->onNetStateChangeAll(uri, flags, status)))
00815     ERROR_BACKTRACE(res);
00816 }
00817 
00818 void BrowserWindow::_onProgressChange(GtkMozEmbed *embed, gint cur, gint max, BrowserWindow *browser)
00819 {
00820   int res;
00821   if (FAILED(res = browser->onProgressChange(cur, max)))
00822     ERROR_BACKTRACE(res);
00823 }
00824 
00825 void BrowserWindow::_onProgressChangeAll(GtkMozEmbed *embed, const char *uri, gint cur, gint max, BrowserWindow *browser)
00826 {
00827   int res;
00828   if (FAILED(res = browser->onProgressChangeAll(uri, cur, max)))
00829     ERROR_BACKTRACE(res);
00830 }
00831 
00832 void BrowserWindow::_onLinkMessage(GtkMozEmbed *embed, BrowserWindow *browser)
00833 {
00834   char *message = gtk_moz_embed_get_link_message(embed);
00835   int res;
00836   if (FAILED(res = browser->onLinkMessage(message)))
00837     ERROR_BACKTRACE(res);
00838   if (message)
00839     g_free(message);
00840 }
00841 
00842 void BrowserWindow::_onJSStatus(GtkMozEmbed *embed, BrowserWindow *browser)
00843 {
00844   char *message = gtk_moz_embed_get_js_status(embed);
00845   int res;
00846   if (FAILED(res = browser->onJSStatus(message)))
00847     ERROR_BACKTRACE(res);
00848   if (message)
00849     g_free(message);
00850 }
00851 
00852 void BrowserWindow::_onNewWindow(GtkMozEmbed *embed, GtkMozEmbed **newEmbed, guint chromeMask, BrowserWindow *browser)
00853 {
00854   BrowserWindow *newBrowser;
00855   GtkWidget *mozilla;
00856   int res;
00857 
00858   *newEmbed = NULL;
00859   if (FAILED(res = browser->onNewWindow(chromeMask, &newBrowser))) {
00860     ERROR_BACKTRACE(res);
00861     return;
00862   }
00863   if (newBrowser) {
00864     if (FAILED(res = newBrowser->mozillaWidget(&mozilla))) {
00865       ERROR_BACKTRACE(res);
00866       return;
00867     }
00868     *newEmbed = GTK_MOZ_EMBED(mozilla);
00869   }
00870 }
00871 
00872 void BrowserWindow::_onVisibility(GtkMozEmbed *embed, gboolean visibility, BrowserWindow *browser)
00873 {
00874   int res;
00875   if (FAILED(res = browser->onVisibility(visibility)))
00876     ERROR_BACKTRACE(res);
00877 }
00878 
00879 void BrowserWindow::_onDestroyBrowser(GtkMozEmbed *embed, BrowserWindow *browser)
00880 {
00881   int res;
00882   if (FAILED(res = browser->onDestroyBrowser()))
00883     ERROR_BACKTRACE(res);
00884 }
00885 
00886 gint BrowserWindow::_onOpenUri(GtkMozEmbed *embed, const char *uri, BrowserWindow *browser)
00887 {
00888   bool interrupt;
00889   int res;
00890   if (FAILED(res = browser->onOpenUri(uri, interrupt))) {
00891     ERROR_BACKTRACE(res);
00892     return TRUE;
00893   }
00894   return interrupt;
00895 }
00896 
00897 void BrowserWindow::_onSizeTo(GtkMozEmbed *embed, gint width, gint height, BrowserWindow *browser)
00898 {
00899   int res;
00900   if (FAILED(res = browser->onSizeTo(width, height)))
00901     ERROR_BACKTRACE(res);
00902 }
00903 
00904 gint BrowserWindow::_onKeyDown(GtkMozEmbed *embed, nsIDOMKeyEvent *event, BrowserWindow *browser)
00905 {
00906   int res;
00907   if (FAILED(res = browser->onKeyDown(event))) {
00908     ERROR_BACKTRACE(res);
00909     return NS_ERROR_FAILURE;
00910   }
00911   return NS_OK;
00912 }
00913 
00914 gint BrowserWindow::_onKeyPress(GtkMozEmbed *embed, nsIDOMKeyEvent *event, BrowserWindow *browser)
00915 {
00916   int res;
00917   if (FAILED(res = browser->onKeyPress(event))) {
00918     ERROR_BACKTRACE(res);
00919     return NS_ERROR_FAILURE;
00920   }
00921   return NS_OK;
00922 }
00923 
00924 gint BrowserWindow::_onKeyUp(GtkMozEmbed *embed, nsIDOMKeyEvent *event, BrowserWindow *browser)
00925 {
00926   int res;
00927   if (FAILED(res = browser->onKeyUp(event))) {
00928     ERROR_BACKTRACE(res);
00929     return NS_ERROR_FAILURE;
00930   }
00931   return NS_OK;
00932 }
00933 
00934 gint BrowserWindow::_onMouseDown(GtkMozEmbed *embed, nsIDOMMouseEvent *event, BrowserWindow *browser)
00935 {
00936   int res;
00937   if (FAILED(res = browser->onMouseDown(event))) {
00938     ERROR_BACKTRACE(res);
00939     return NS_ERROR_FAILURE;
00940   }
00941   return NS_OK;
00942 }
00943 
00944 gint BrowserWindow::_onMouseUp(GtkMozEmbed *embed, nsIDOMMouseEvent *event, BrowserWindow *browser)
00945 {
00946   int res;
00947   if (FAILED(res = browser->onMouseUp(event))) {
00948     ERROR_BACKTRACE(res);
00949     return NS_ERROR_FAILURE;
00950   }
00951   return NS_OK;
00952 }
00953 
00954 gint BrowserWindow::_onMouseClick(GtkMozEmbed *embed, nsIDOMMouseEvent *event, BrowserWindow *browser)
00955 {
00956   int res;
00957   if (FAILED(res = browser->onMouseClick(event))) {
00958     ERROR_BACKTRACE(res);
00959     return NS_ERROR_FAILURE;
00960   }
00961   return NS_OK;
00962 }
00963 
00964 gint BrowserWindow::_onMouseDoubleClick(GtkMozEmbed *embed, nsIDOMMouseEvent *event, BrowserWindow *browser)
00965 {
00966   int res;
00967   if (FAILED(res = browser->onMouseDoubleClick(event))) {
00968     ERROR_BACKTRACE(res);
00969     return NS_ERROR_FAILURE;
00970   }
00971   return NS_OK;
00972 }
00973 
00974 gint BrowserWindow::_onMouseOver(GtkMozEmbed *embed, nsIDOMMouseEvent *event, BrowserWindow *browser)
00975 {
00976   int res;
00977   if (FAILED(res = browser->onMouseOver(event))) {
00978     ERROR_BACKTRACE(res);
00979     return NS_ERROR_FAILURE;
00980   }
00981   return NS_OK;
00982 }
00983 
00984 gint BrowserWindow::_onMouseOut(GtkMozEmbed *embed, nsIDOMMouseEvent *event, BrowserWindow *browser)
00985 {
00986   int res;
00987   if (FAILED(res = browser->onMouseOut(event))) {
00988     ERROR_BACKTRACE(res);
00989     return NS_ERROR_FAILURE;
00990   }
00991   return NS_OK;
00992 }
00993 
00994 void BrowserWindow::_onSecurityChange(GtkMozEmbed *embed, gpointer request, guint state, BrowserWindow *browser)
00995 {
00996 }
00997 
00998 gint BrowserWindow::_onActivate(GtkMozEmbed *embed, nsIDOMUIEvent *event, BrowserWindow *browser)
00999 {
01000   int res;
01001   if (FAILED(res = browser->onActivate(event))) {
01002     ERROR_BACKTRACE(res);
01003     return NS_ERROR_FAILURE;
01004   }
01005   return NS_OK;
01006 }
01007 
01008 gint BrowserWindow::_onFocusIn(GtkMozEmbed *embed, nsIDOMUIEvent *event, BrowserWindow *browser)
01009 {
01010   int res;
01011   if (FAILED(res = browser->onFocusIn(event))) {
01012     ERROR_BACKTRACE(res);
01013     return NS_ERROR_FAILURE;
01014   }
01015   return NS_OK;
01016 }
01017 
01018 gint BrowserWindow::_onFocusOut(GtkMozEmbed *embed, nsIDOMUIEvent *event, BrowserWindow *browser)
01019 {
01020   int res;
01021   if (FAILED(res = browser->onFocusOut(event))) {
01022     ERROR_BACKTRACE(res);
01023     return NS_ERROR_FAILURE;
01024   }
01025   return NS_OK;
01026 }
01027 
01028 void BrowserWindow::_onNewWindowOrphan(GtkMozEmbedSingle *embed, GtkMozEmbed **newEmbed, guint chromeMask, BrowserWindow *browser)
01029 {
01030   _onNewWindow(NULL, newEmbed, chromeMask, browser);
01031 }
01032 
01033 int BrowserWindow::updateStatusBar()
01034 {
01035   gtk_statusbar_pop(GTK_STATUSBAR(_statusBar), 1);
01036   if (_status.tempMessage.empty()) {
01037     String message;
01038 
01039     if (_status.loadPercent)
01040       message.sprintf("%s (%d%% complete, %d bytes of %d loaded).", CSTRING(_status.message), _status.loadPercent, _status.bytesLoaded, _status.maxBytesLoaded);
01041     else
01042       if (_status.bytesLoaded)
01043         message.sprintf("%s (%d bytes loaded).", CSTRING(_status.message), _status.bytesLoaded);
01044       else
01045         message = _status.message;
01046     gtk_statusbar_push(GTK_STATUSBAR(_statusBar), 1, CSTRING(message));
01047   } else
01048       gtk_statusbar_push(GTK_STATUSBAR(_statusBar), 1, CSTRING(_status.tempMessage));
01049   return OK;
01050 }

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1