1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is XMLterm.
*
* The Initial Developer of the Original Code is
* Ramalingam Saravanan.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// mozXMLTerminal.cpp: implementation of mozIXMLTerminal interface
// to manage all XMLTerm operations.
// Creates a new mozXMLTermSession object to manage session input/output.
// Creates a mozLineTermAux object to access LineTerm operations.
// Creates key/text/mouse/drag listener objects to handle user events.
#include "nscore.h"
#include "nspr.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsIDocument.h"
#include "nsIDOMHTMLDocument.h"
#include "nsIDocumentViewer.h"
#include "nsIObserver.h"
#include "nsISelectionController.h"
#include "nsPresContext.h"
#include "nsICaret.h"
#include "nsRect.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsIDOMEventReceiver.h"
#include "nsIDOMEventListener.h"
#include "nsIServiceManager.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsWidgetsCID.h"
#include "nsIClipboard.h"
#include "nsITransferable.h"
#include "nsFont.h"
#include "nsIFontMetrics.h"
#include "nsILookAndFeel.h"
#include "mozXMLT.h"
#include "mozXMLTermUtils.h"
#include "mozXMLTerminal.h"
#include "nsIWebNavigation.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIWebProgress.h"
////////////////////////////////////////////////////////////////////////
static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);
static NS_DEFINE_CID(kCTransferableCID, NS_TRANSFERABLE_CID);
static NS_DEFINE_CID(kLookAndFeelCID, NS_LOOKANDFEEL_CID);
/////////////////////////////////////////////////////////////////////////
// mozXMLTerminal implementation
/////////////////////////////////////////////////////////////////////////
NS_IMPL_THREADSAFE_ISUPPORTS4(mozXMLTerminal,
mozIXMLTerminal,
nsIWebProgressListener,
nsIObserver,
nsISupportsWeakReference)
mozXMLTerminal::mozXMLTerminal() :
mInitialized(PR_FALSE),
mCookie(EmptyString()),
mCommand(EmptyString()),
mPromptExpr(EmptyString()),
mInitInput(EmptyString()),
mXMLTermShell(nsnull),
mDocShell(nsnull),
mPresShell(nsnull),
mDOMDocument(nsnull),
mXMLTermSession(nsnull),
mLineTermAux(nsnull),
mNeedsResizing(PR_FALSE),
mKeyListener(nsnull),
mTextListener(nsnull),
mMouseListener(nsnull),
mDragListener(nsnull)
{
}
mozXMLTerminal::~mozXMLTerminal()
{
Finalize();
}
NS_IMETHODIMP mozXMLTerminal::GetCurrentEntryNumber(PRInt32 *aNumber)
{
if (!mXMLTermSession)
return NS_ERROR_FAILURE;
return mXMLTermSession->GetCurrentEntryNumber(aNumber);
}
NS_IMETHODIMP mozXMLTerminal::GetHistory(PRInt32 *aHistory)
{
if (!mXMLTermSession)
return NS_ERROR_FAILURE;
return mXMLTermSession->GetHistory(aHistory);
}
NS_IMETHODIMP mozXMLTerminal::SetHistory(PRInt32 aHistory)
{
if (!mXMLTermSession)
return NS_ERROR_FAILURE;
return mXMLTermSession->SetHistory(aHistory);
}
NS_IMETHODIMP mozXMLTerminal::GetPrompt(PRUnichar **aPrompt)
{
if (!mXMLTermSession)
return NS_ERROR_FAILURE;
return mXMLTermSession->GetPrompt(aPrompt);
}
NS_IMETHODIMP mozXMLTerminal::SetPrompt(const PRUnichar* aPrompt)
{
if (!mXMLTermSession)
return NS_ERROR_FAILURE;
return mXMLTermSession->SetPrompt(aPrompt);
}
NS_IMETHODIMP mozXMLTerminal::GetKeyIgnore(PRBool* aIgnore)
{
if (!mKeyListener)
return NS_ERROR_FAILURE;
nsCOMPtr<mozIXMLTermSuspend> suspend= do_QueryInterface(mKeyListener);
if (!suspend)
return NS_ERROR_FAILURE;
return suspend->GetSuspend(aIgnore);
}
NS_IMETHODIMP mozXMLTerminal::SetKeyIgnore(const PRBool aIgnore)
{
if (!mKeyListener)
return NS_ERROR_FAILURE;
nsCOMPtr<mozIXMLTermSuspend> suspend= do_QueryInterface(mKeyListener);
if (!suspend)
return NS_ERROR_FAILURE;
return suspend->SetSuspend(aIgnore);
}
// Initialize by starting load of Init page for XMLTerm
NS_IMETHODIMP mozXMLTerminal::Init(nsIDocShell* aDocShell,
mozIXMLTermShell* aXMLTermShell,
const PRUnichar* aURL,
const PRUnichar* args)
{
XMLT_LOG(mozXMLTerminal::Init,20,("\n"));
if (!aDocShell)
return NS_ERROR_NULL_POINTER;
if (mInitialized)
return NS_ERROR_ALREADY_INITIALIZED;
// Initialization flag
mInitialized = PR_TRUE;
// Containing docshell
mDocShell = do_GetWeakReference(aDocShell); // weak ref
mXMLTermShell = aXMLTermShell; // containing xmlterm shell; no addref
nsresult result = NS_OK;
// NOTE: Need to parse args string!!!
mCommand.SetLength(0);
mPromptExpr.SetLength(0);
mInitInput = args;
if ((aURL != nsnull) && (*aURL != 0)) {
// Load URL and activate XMLTerm after loading
XMLT_LOG(mozXMLTerminal::Init,22,("setting DocLoaderObs\n"));
// About to create owning reference to this
nsCOMPtr<nsIWebProgress> progress(do_GetInterface(aDocShell, &result));
if (NS_FAILED(result)) return result;
result = progress->AddProgressListener((nsIWebProgressListener*)this,
nsIWebProgress::NOTIFY_STATE_REQUEST);
if (NS_FAILED(result))
return NS_ERROR_FAILURE;
XMLT_LOG(mozXMLTerminal::Init,22,("done setting DocLoaderObs\n"));
// Load initial XMLterm background document
nsCOMPtr<nsIURI> uri;
result = NS_NewURI(getter_AddRefs(uri), nsDependentString(aURL));
if (NS_FAILED(result))
return NS_ERROR_FAILURE;
result = aDocShell->LoadURI(uri, nsnull, nsIWebNavigation::LOAD_FLAGS_NONE, PR_TRUE);
if (NS_FAILED(result))
return NS_ERROR_FAILURE;
} else {
// Document already loaded; activate XMLTerm
result = Activate();
if (NS_FAILED(result))
return NS_ERROR_FAILURE;
}
XMLT_LOG(mozXMLTerminal::Init,21,("exiting\n"));
return result;
}
// De-initialize XMLTerminal
NS_IMETHODIMP mozXMLTerminal::Finalize(void)
{
if (!mInitialized)
return NS_OK;
XMLT_LOG(mozXMLTerminal::Finalize,20,("\n"));
mInitialized = PR_FALSE;
if (mXMLTermSession) {
// Finalize XMLTermSession object and delete it (it is not ref. counted)
mXMLTermSession->Finalize();
delete mXMLTermSession;
mXMLTermSession = nsnull;
}
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryReferent(mDOMDocument);
if (domDoc) {
// Release any event listeners for the document
nsCOMPtr<nsIDOMEventReceiver> eventReceiver;
nsresult result = domDoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), getter_AddRefs(eventReceiver));
if (NS_SUCCEEDED(result) && eventReceiver) {
if (mKeyListener) {
eventReceiver->RemoveEventListenerByIID(mKeyListener,
NS_GET_IID(nsIDOMKeyListener));
mKeyListener = nsnull;
}
if (mTextListener) {
eventReceiver->RemoveEventListenerByIID(mTextListener,
NS_GET_IID(nsIDOMTextListener));
mTextListener = nsnull;
}
if (mMouseListener) {
eventReceiver->RemoveEventListenerByIID(mMouseListener,
NS_GET_IID(nsIDOMMouseListener));
mMouseListener = nsnull;
}
if (mDragListener) {
eventReceiver->RemoveEventListenerByIID(mDragListener,
NS_GET_IID(nsIDOMDragListener));
mDragListener = nsnull;
}
}
}
mDOMDocument = nsnull;
if (mLineTermAux) {
// Finalize and release reference to LineTerm object owned by us
mLineTermAux->CloseAux();
mLineTermAux = nsnull;
}
mDocShell = nsnull;
mPresShell = nsnull;
mXMLTermShell = nsnull;
XMLT_LOG(mozXMLTerminal::Finalize,22,("END\n"));
return NS_OK;
}
/** Activates XMLterm and instantiates LineTerm;
* called at the the end of Init page loading.
*/
NS_IMETHODIMP mozXMLTerminal::Activate(void)
{
nsresult result = NS_OK;
#if 0
// TEMPORARY: Testing mozIXMLTermStream
nsAutoString streamData( "<HTML><HEAD><TITLE>Stream Title</TITLE>"
"<SCRIPT language='JavaScript'>"
"function clik(){ dump('click\\n');return(false);}"
"</SCRIPT></HEAD>"
"<BODY><B>Stream Body "
"<SPAN STYLE='color: blue' onClick='return clik();'>Clik</SPAN></B><BR>"
"<TABLE WIDTH=720><TR><TD WIDTH=700 BGCOLOR=maroon> </TABLE>"
"<BR>ABCD<BR>EFGH<BR>JKLM<BR>"
"</BODY></HTML>" );
nsCOMPtr<mozIXMLTermStream> stream;
result = NS_NewXMLTermStream(getter_AddRefs(stream));
if (NS_FAILED(result)) {
XMLT_ERROR("mozXMLTerminal::Activate: Failed to create stream\n");
return result;
}
nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mDocShell);
if (!docShell)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDOMWindowInternal> outerDOMWindow;
result = mozXMLTermUtils::ConvertDocShellToDOMWindow(docShell,
getter_AddRefs(outerDOMWindow));
if (NS_FAILED(result) || !outerDOMWindow) {
XMLT_ERROR("mozXMLTerminal::Activate: Failed to convert docshell\n");
return NS_ERROR_FAILURE;
}
result = stream->Open(outerDOMWindow, "iframet", "chrome://dummy",
"text/html", 800);
if (NS_FAILED(result)) {
XMLT_ERROR("mozXMLTerminal::Activate: Failed to open stream\n");
return result;
}
result = stream->Write(streamData.get());
if (NS_FAILED(result)) {
XMLT_ERROR("mozXMLTerminal::Activate: Failed to write to stream\n");
return result;
}
result = stream->Close();
if (NS_FAILED(result)) {
XMLT_ERROR("mozXMLTerminal::Activate: Failed to close stream\n");
return result;
}
#endif
XMLT_LOG(mozXMLTerminal::Activate,20,("\n"));
if (!mInitialized)
return NS_ERROR_NOT_INITIALIZED;
PR_ASSERT(mDocShell != nsnull);
if ((mDOMDocument != nsnull) || (mPresShell != nsnull))
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mDocShell);
if (!docShell)
return NS_ERROR_FAILURE;
// Get reference to presentation shell
nsCOMPtr<nsPresContext> presContext;
result = docShell->GetPresContext(getter_AddRefs(presContext));
if (NS_FAILED(result) || !presContext)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIPresShell> presShell;
result = docShell->GetPresShell(getter_AddRefs(presShell));
if (NS_FAILED(result) || !presShell)
return NS_ERROR_FAILURE;
// Get reference to DOMDocument
nsIDocument *document = presShell->GetDocument();
if (!document)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(document);
if (!domDoc)
return NS_ERROR_FAILURE;
// Save weak references to presentation shell and DOMDocument
mPresShell = do_GetWeakReference(presShell); // weak ref
mDOMDocument = do_GetWeakReference(domDoc); // weak ref
// Show caret
ShowCaret();
// Determine current screen dimensions
PRInt32 nRows, nCols, xPixels, yPixels;
result = ScreenSize(&nRows, &nCols, &xPixels, &yPixels);
if (NS_FAILED(result))
return result;
// The above call does not return the correct screen dimensions.
// (because rendering is still in progress?)
// As a workaround, explicitly set screen dimensions
nRows = 24;
nCols = 80;
xPixels = 0;
yPixels = 0;
// Instantiate and initialize XMLTermSession object
mXMLTermSession = new mozXMLTermSession();
if (!mXMLTermSession) {
return NS_ERROR_OUT_OF_MEMORY;
}
result = mXMLTermSession->Init(this, presShell, domDoc, nRows, nCols);
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to initialize XMLTermSession\n");
return NS_ERROR_FAILURE;
}
// Instantiate LineTerm
XMLT_LOG(mozXMLTerminal::Activate,22,
("instantiating lineterm, nRows=%d, nCols=%d\n", nRows, nCols));
mLineTermAux = do_CreateInstance(MOZLINETERM_CONTRACTID, &result);
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to instantiate LineTermAux\n");
return result;
}
// Open LineTerm to execute command
// Non-owning reference to this; delete LineTerm before deleting self
PRInt32 options = 0;
XMLT_LOG(mozXMLTerminal::Activate,22,("Opening LineTerm\n"));
nsCOMPtr<nsIObserver> anObserver = this;
#ifdef NO_CALLBACK
anObserver = nsnull;
#endif
nsAutoString cookie;
result = mLineTermAux->OpenAux(mCommand.get(),
mInitInput.get(),
mPromptExpr.get(),
options, LTERM_DETERMINE_PROCESS,
nRows, nCols, xPixels, yPixels,
domDoc, anObserver, cookie);
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to open LineTermAux\n");
return result;
}
XMLT_LOG(mozXMLTerminal::Activate,22,("Opened LineTerm\n"));
// Save cookie
mCookie = cookie;
// Get the DOM event receiver for document
nsCOMPtr<nsIDOMEventReceiver> eventReceiver;
result = domDoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver),
getter_AddRefs(eventReceiver));
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to get DOM receiver\n");
return result;
}
// Create a key listener
result = NS_NewXMLTermKeyListener(getter_AddRefs(mKeyListener), this);
if (NS_OK != result) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to get key listener\n");
return result;
}
// Register the key listener with the DOM event receiver
result = eventReceiver->AddEventListenerByIID(mKeyListener,
NS_GET_IID(nsIDOMKeyListener));
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to register key listener\n");
return result;
}
// Create a text listener
result = NS_NewXMLTermTextListener(getter_AddRefs(mTextListener), this);
if (NS_OK != result) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to get text listener\n");
return result;
}
// Register the text listener with the DOM event receiver
result = eventReceiver->AddEventListenerByIID(mTextListener,
NS_GET_IID(nsIDOMTextListener));
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to register text listener\n");
return result;
}
// Create a mouse listener
result = NS_NewXMLTermMouseListener(getter_AddRefs(mMouseListener), this);
if (NS_OK != result) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to get mouse listener\n");
return result;
}
// Register the mouse listener with the DOM event receiver
result = eventReceiver->AddEventListenerByIID(mMouseListener,
NS_GET_IID(nsIDOMMouseListener));
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to register mouse listener\n");
return result;
}
// Create a drag listener
result = NS_NewXMLTermDragListener(getter_AddRefs(mDragListener), this);
if (NS_OK != result) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to get drag listener\n");
return result;
}
// Register the drag listener with the DOM event receiver
result = eventReceiver->AddEventListenerByIID(mDragListener,
NS_GET_IID(nsIDOMDragListener));
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Activate: Warning - Failed to register drag listener\n");
return result;
}
return NS_OK;
}
/** Returns current screen size in rows/cols and in pixels
* @param (output) rows
* @param (output) cols
* @param (output) xPixels
* @param (output) yPixels
*/
NS_IMETHODIMP mozXMLTerminal::ScreenSize(PRInt32* rows, PRInt32* cols,
PRInt32* xPixels, PRInt32* yPixels)
{
nsresult result;
XMLT_LOG(mozXMLTerminal::ScreenSize,70,("\n"));
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
if (!presShell)
return NS_ERROR_FAILURE;
// Get presentation context
nsPresContext *presContext = presShell->GetPresContext();
// Get the default fixed pitch font
const nsFont* defaultFixedFont =
presContext->GetDefaultFont(kPresContext_DefaultFixedFont_ID);
// Get metrics for fixed font
nsCOMPtr<nsIFontMetrics> fontMetrics =
presContext->GetMetricsFor(*defaultFixedFont);
if (!fontMetrics)
return NS_ERROR_FAILURE;
// Get font height (includes leading?)
nscoord fontHeight, fontWidth;
result = fontMetrics->GetHeight(fontHeight);
result = fontMetrics->GetMaxAdvance(fontWidth);
// Determine docshell size in twips
nsRect shellArea = presContext->GetVisibleArea();
// Determine twips to pixels conversion factor
float pixelScale;
pixelScale = presContext->TwipsToPixels();
// Convert dimensions to pixels
float xdel, ydel;
xdel = pixelScale * fontWidth;
ydel = pixelScale * fontHeight + 2;
*xPixels = (int) (pixelScale * shellArea.width);
*yPixels = (int) (pixelScale * shellArea.height);
// Determine number of rows/columns
*rows = (int) ((*yPixels-16) / ydel);
*cols = (int) ((*xPixels-20) / xdel);
if (*rows < 1) *rows = 1;
if (*cols < 1) *cols = 1;
XMLT_LOG(mozXMLTerminal::ScreenSize,72,
("rows=%d, cols=%d, xPixels=%d, yPixels=%d\n",
*rows, *cols, *xPixels, *yPixels));
return NS_OK;
}
// Transmit string to LineTerm (use saved cookie)
NS_IMETHODIMP mozXMLTerminal::SendTextAux(const PRUnichar* aString)
{
return SendText(aString, mCookie.get());
}
// Transmit string to LineTerm
NS_IMETHODIMP mozXMLTerminal::SendText(const PRUnichar* aString,
const PRUnichar* aCookie)
{
nsresult result;
if (!mLineTermAux)
return NS_ERROR_FAILURE;
nsAutoString sendStr(aString);
// Preprocess string and check if it is to be consumed
PRBool consumed, checkSize;
result = mXMLTermSession->Preprocess(sendStr, consumed, checkSize);
PRBool screenMode;
GetScreenMode(&screenMode);
if (!screenMode && (checkSize || mNeedsResizing)) {
// Resize terminal, if need be
mXMLTermSession->Resize(mLineTermAux);
mNeedsResizing = PR_FALSE;
}
if (!consumed) {
result = mLineTermAux->Write(sendStr.get(), aCookie);
if (NS_FAILED(result)) {
// Abort XMLterm session
nsAutoString abortCode;
abortCode.AssignLiteral("SendText");
mXMLTermSession->Abort(mLineTermAux, abortCode);
return NS_ERROR_FAILURE;
}
}
return NS_OK;
}
/** Shows the caret and make it editable.
*/
NS_IMETHODIMP mozXMLTerminal::ShowCaret(void)
{
// In principle, this method needs to be called only once;
// in practice, certain operations seem to hide the caret
// especially when one starts mucking around with the display:
// style property.
// Under those circumstances, call this method to re-display the caret.
XMLT_LOG(mozXMLTerminal::ShowCaret,70,("\n"));
if (!mPresShell)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
if (!presShell)
return NS_ERROR_FAILURE;
nsCOMPtr<nsISelectionController> selCon = do_QueryInterface(presShell);
if (!selCon) {
XMLT_WARNING("mozXMLTerminal::ShowCaret: Warning - Failed to get SelectionController\n");
return NS_ERROR_FAILURE;
}
selCon->SetCaretEnabled(PR_TRUE);
selCon->SetCaretReadOnly(PR_FALSE);
nsCOMPtr<nsICaret> caret;
if (NS_SUCCEEDED(presShell->GetCaret(getter_AddRefs(caret))) && caret) {
caret->SetCaretVisible(PR_TRUE);
caret->SetCaretReadOnly(PR_FALSE);
nsCOMPtr<nsISelection> sel;
if (NS_SUCCEEDED(selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(sel))) && sel) {
caret->SetCaretDOMSelection(sel);
}
} else {
XMLT_WARNING("mozXMLTerminal::ShowCaret: Warning - Failed to get caret\n");
}
return NS_OK;
}
// Paste data from clipboard to terminal
NS_IMETHODIMP mozXMLTerminal::Paste()
{
nsresult result;
nsAutoString pasteString;
XMLT_LOG(mozXMLTerminal::Paste,20,("\n"));
// Get Clipboard service
nsCOMPtr<nsIClipboard> clipboard(do_GetService(kCClipboardCID, &result));
if ( NS_FAILED(result) )
return result;
// Generic transferable for getting clipboard data
nsCOMPtr<nsITransferable> trans = do_CreateInstance(kCTransferableCID, &result);
if (NS_FAILED(result) || !trans)
return NS_ERROR_FAILURE;
// DataFlavors to get out of transferable
// trans->AddDataFlavor(kHTMLMime); // Cannot handle HTML yet
trans->AddDataFlavor(kUnicodeMime);
// Get data from clipboard
result = clipboard->GetData(trans, nsIClipboard::kSelectionClipboard);
if (NS_FAILED(result))
return result;
char* bestFlavor = nsnull;
nsCOMPtr<nsISupports> genericDataObj;
PRUint32 objLen = 0;
result = trans->GetAnyTransferData(&bestFlavor,
getter_AddRefs(genericDataObj), &objLen);
if (NS_FAILED(result))
return result;
XMLT_LOG(mozXMLTerminal::Paste,20,("flavour=%s\n", bestFlavor));
if (strcmp(bestFlavor, kHTMLMime) == 0 || strcmp(bestFlavor, kUnicodeMime) == 0) {
nsCOMPtr<nsISupportsString> textDataObj ( do_QueryInterface(genericDataObj) );
if (textDataObj && objLen > 0) {
PRUnichar* text = nsnull;
textDataObj->ToString ( &text );
pasteString.Assign( text, objLen / 2 );
result = SendTextAux(pasteString.get());
}
}
nsMemory::Free(bestFlavor);
return NS_OK;
}
// Poll for readable data from LineTerm
NS_IMETHODIMP mozXMLTerminal::Poll(void)
{
if (!mLineTermAux)
return NS_ERROR_NOT_INITIALIZED;
XMLT_LOG(mozXMLTerminal::Poll,20,("\n"));
nsresult result;
PRBool processedData;
result = mXMLTermSession->ReadAll(mLineTermAux, processedData);
if (NS_FAILED(result)) {
XMLT_WARNING("mozXMLTerminal::Poll: Warning - Error return 0x%x from ReadAll\n", result);
return result;
}
return NS_OK;
}
// Handle callback from LineTerm when new input/output needs to be displayed
NS_IMETHODIMP mozXMLTerminal::Observe(nsISupports *aSubject,
const char *aTopic,
const PRUnichar *someData)
{
nsCOMPtr<mozILineTermAux> lineTermAux = do_QueryInterface(aSubject);
PR_ASSERT(lineTermAux != nsnull);
PR_ASSERT(lineTermAux.get() == mLineTermAux.get());
return Poll();
}
// Returns document associated with XMLTerminal
NS_IMETHODIMP mozXMLTerminal::GetDocument(nsIDOMDocument** aDoc)
{
if (!aDoc)
return NS_ERROR_NULL_POINTER;
*aDoc = nsnull;
NS_PRECONDITION(mDOMDocument, "bad state, null mDOMDocument");
if (!mDOMDocument)
return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryReferent(mDOMDocument);
if (!domDoc)
return NS_ERROR_FAILURE;
return domDoc->QueryInterface(NS_GET_IID(nsIDOMDocument),
(void **)aDoc);
}
// Returns doc shell associated with XMLTerm
NS_IMETHODIMP mozXMLTerminal::GetDocShell(nsIDocShell** aDocShell)
{
if (!aDocShell)
return NS_ERROR_NULL_POINTER;
*aDocShell = nsnull;
NS_PRECONDITION(mDocShell, "bad state, null mDocShell");
if (!mDocShell)
return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mDocShell);
if (!docShell) {
XMLT_ERROR("mozXMLTerminal::GetDocShell: Error - Invalid weak reference\n");
return NS_ERROR_FAILURE;
}
return docShell->QueryInterface(NS_GET_IID(nsIDocShell),
(void **)aDocShell);
}
// Returns presentation shell associated with XMLTerm
NS_IMETHODIMP mozXMLTerminal::GetPresShell(nsIPresShell** aPresShell)
{
if (!aPresShell)
return NS_ERROR_NULL_POINTER;
*aPresShell = nsnull;
NS_PRECONDITION(mPresShell, "bad state, null mPresShell");
if (!mPresShell)
return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
if (!presShell) {
XMLT_ERROR("mozXMLTerminal::GetPresShell: Error - Invalid weak reference\n");
return NS_ERROR_FAILURE;
}
return presShell->QueryInterface(NS_GET_IID(nsIPresShell),
(void **)aPresShell);
}
// Returns DOMDocument associated with XMLTerm
NS_IMETHODIMP mozXMLTerminal::GetDOMDocument(nsIDOMDocument** aDOMDocument)
{
if (!aDOMDocument)
return NS_ERROR_NULL_POINTER;
*aDOMDocument = nsnull;
NS_PRECONDITION(mDOMDocument, "bad state, null mDOMDocument");
if (!mDOMDocument)
return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryReferent(mDOMDocument);
if (!domDoc) {
XMLT_ERROR("mozXMLTerminal::GetDOMDocument: Error - Invalid weak reference\n");
return NS_ERROR_FAILURE;
}
return domDoc->QueryInterface(NS_GET_IID(nsIDOMDocument),
(void **)aDOMDocument);
}
// Returns SelectionController associated with XMLTerm
NS_IMETHODIMP mozXMLTerminal::GetSelectionController(nsISelectionController** aSelectionController)
{
if (!aSelectionController)
return NS_ERROR_NULL_POINTER;
*aSelectionController = nsnull;
NS_PRECONDITION(mPresShell, "bad state, null mPresShell");
if (!mPresShell)
return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
if (!presShell) {
XMLT_ERROR("mozXMLTerminal::GetSelectionControlle: Error - Invalid weak reference\n");
return NS_ERROR_FAILURE;
}
return presShell->QueryInterface(NS_GET_IID(nsISelectionController),
(void **)aSelectionController);
}
/** Gets flag denoting whether terminal is in full screen mode
* @param aFlag (output) screen mode flag
*/
NS_IMETHODIMP mozXMLTerminal::GetScreenMode(PRBool* aFlag)
{
return mXMLTermSession->GetScreenMode(aFlag);
}
/** Checks if supplied cookie is valid for XMLTerm
* @param aCookie supplied cookie string
* @param _retval PR_TRUE if supplied cookie matches XMLTerm cookie
*/
NS_IMETHODIMP mozXMLTerminal::MatchesCookie(const PRUnichar* aCookie,
PRBool *_retval)
{
XMLT_LOG(mozXMLTerminal::MatchesCookie,20,("\n"));
if (!_retval)
return NS_ERROR_NULL_POINTER;
// Check if supplied cookie matches XMLTerm cookie
*_retval = mCookie.Equals(aCookie);
if (!(*_retval)) {
XMLT_ERROR("mozXMLTerminal::MatchesCookie: Error - Cookie mismatch\n");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
/** Resizes XMLterm to match a resized window.
*/
NS_IMETHODIMP mozXMLTerminal::Resize(void)
{
nsresult result;
XMLT_LOG(mozXMLTerminal::Resize,20,("\n"));
if (!mXMLTermSession)
return NS_ERROR_FAILURE;
PRBool screenMode;
GetScreenMode(&screenMode);
if (screenMode) {
// Delay resizing until next input processing
mNeedsResizing = PR_TRUE;
} else {
// Resize session
result = mXMLTermSession->Resize(mLineTermAux);
if (NS_FAILED(result))
return result;
}
return NS_OK;
}
/** Exports HTML to file, with META REFRESH, if refreshSeconds is non-zero.
* Nothing is done if display has not changed since last export, unless
* forceExport is true. Returns true if export actually takes place.
* If filename is a null string, HTML is written to STDERR.
*/
NS_IMETHODIMP mozXMLTerminal::ExportHTML(const PRUnichar* aFilename,
PRInt32 permissions,
const PRUnichar* style,
PRUint32 refreshSeconds,
PRBool forceExport,
PRBool* exported)
{
if (!mXMLTermSession)
return NS_ERROR_FAILURE;
return mXMLTermSession->ExportHTML( aFilename, permissions, style,
refreshSeconds, forceExport,
exported);
}
// nsIWebProgressListener methods
NS_IMETHODIMP
mozXMLTerminal::OnStateChange(nsIWebProgress* aWebProgress,
nsIRequest *aRequest,
PRUint32 progressStateFlags,
nsresult aStatus) {
if (progressStateFlags & nsIWebProgressListener::STATE_IS_REQUEST)
if (progressStateFlags & nsIWebProgressListener::STATE_START) {
XMLT_LOG(mozXMLTerminal::OnStateChange,20,("\n"));
// Activate XMLTerm
Activate();
}
return NS_OK;
}
NS_IMETHODIMP
mozXMLTerminal::OnProgressChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRInt32 aCurSelfProgress,
PRInt32 aMaxSelfProgress,
PRInt32 aCurTotalProgress,
PRInt32 aMaxTotalProgress) {
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
return NS_OK;
}
NS_IMETHODIMP
mozXMLTerminal::OnLocationChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
nsIURI *location) {
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
return NS_OK;
}
NS_IMETHODIMP
mozXMLTerminal::OnStatusChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
nsresult aStatus,
const PRUnichar* aMessage) {
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
return NS_OK;
}
NS_IMETHODIMP
mozXMLTerminal::OnSecurityChange(nsIWebProgress *aWebProgress,
nsIRequest *aRequest,
PRUint32 state) {
NS_NOTREACHED("notification excluded in AddProgressListener(...)");
return NS_OK;
}
|