Back to index

lightning-sunbird  0.9+nobinonly
nsCoverage.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 //---- Factory Includes & Stuff -----// 
00039 #include "nsIFactory.h" 
00040 #include "nsIComponentManager.h" 
00041 #include "nsIServiceManager.h"
00042 #include "nsIEventQueueService.h"
00043 #include "nsIEventQueue.h"
00044 #include "nsGfxCIID.h" 
00045 
00046 #include "nsWidgetsCID.h" 
00047 #include "nsIWidget.h"
00048 #include "nsGUIEvent.h"
00049 #include "nsString.h"
00050 #include "nsRect.h"
00051 #include "nsIRenderingContext.h"
00052 
00053 #include "nsIDeviceContext.h"
00054 #include "nsFont.h"
00055 #include "nsIComponentManager.h"
00056 #include "nsWidgetsCID.h"
00057 #include "nsIAppShell.h"
00058 
00059 //------------------------------------------------------------------------------
00060 
00061 nsIWidget         *gWindow = NULL;
00062 
00063 #if defined(XP_WIN) || defined(XP_OS2)
00064 #define TEXT_HEIGHT 25
00065 #endif
00066 
00067 #if defined(XP_UNIX) || defined(XP_BEOS)
00068 #define TEXT_HEIGHT 30
00069 #endif
00070 
00071 #ifdef XP_MAC
00072 #define TEXT_HEIGHT 30
00073 #endif
00074 
00075 // class ids
00076 static NS_DEFINE_CID(kCWindowCID, NS_WINDOW_CID);
00077 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00078 static NS_DEFINE_CID(kCAppShellCID, NS_APPSHELL_CID);
00079 
00080 // Scroll offsets
00081 nscoord gOffsetX = 0;
00082 nscoord gOffsetY = 0;
00083 
00084 nscoord pixelLoc(nscoord aPixelValue, float aP2t, nscoord aOffset)
00085 {
00086   return NSToIntRound((aPixelValue + aOffset) * aP2t);
00087 }
00088 
00089 
00090 PRInt32
00091 drawtest(nsIRenderingContext *aSurface)
00092 {
00093 nsFont  *font;
00094 nsPoint *pointlist;
00095 
00096    // Get the size of a pixel.
00097   nsIDeviceContext* deviceContext;
00098   aSurface->GetDeviceContext(deviceContext);
00099   float p2t; // pixel to twips conversion
00100   p2t = deviceContext->DevUnitsToAppUnits();
00101   NS_RELEASE(deviceContext);
00102 
00103   font = new nsFont("Times", NS_FONT_STYLE_NORMAL,NS_FONT_VARIANT_NORMAL,NS_FONT_WEIGHT_BOLD,0,12);
00104   aSurface->SetFont(*font);
00105 
00106    // Clear surface.
00107   nsRect rect;
00108   gWindow->GetClientBounds(rect);
00109   aSurface->SetColor(NS_RGB(255, 255, 255));
00110   aSurface->FillRect(0,0,rect.width,rect.height);
00111 
00112   aSurface->SetColor(NS_RGB(255, 0, 0));
00113  
00114   nsAutoString strText6(NS_LITERAL_STRING("GFX - Pixel coverage test"));
00115   aSurface->DrawString(strText6,150,30);
00116   nsAutoString strText5(NS_LITERAL_STRING("(Use (u, d, r, l) keys to scroll the window contents)"));
00117   aSurface->DrawString(strText5,150,50);
00118 
00119 
00120             
00121   aSurface->Translate(gOffsetX, gOffsetY);
00122 
00123    // Starting coordinates
00124   nscoord ox = 90; // pixels
00125   nscoord oy = 100; // pixels
00126    // Spacing between tests
00127   nscoord yspacing = 50; // pixels
00128  
00129    //--------------
00130    // DrawLine TEST
00131    //--------------
00132 
00133   aSurface->SetColor(NS_RGB(255, 0, 0));
00134   aSurface->DrawLine(pixelLoc(12, p2t, ox),
00135                      pixelLoc(0,  p2t, oy), 
00136                      pixelLoc(12, p2t, ox), 
00137                      pixelLoc(10, p2t, oy));
00138 
00139   aSurface->SetColor(NS_RGB(0, 0, 0));
00140   aSurface->DrawLine(pixelLoc(0, p2t, ox),
00141                      pixelLoc(10,p2t, oy), 
00142                      pixelLoc(12,p2t, ox), 
00143                      pixelLoc(10, p2t, oy));
00144 
00145    nsAutoString strText4(NS_LITERAL_STRING("DrawLine - There should be a one pixel gap where the red and black lines meet"));
00146    aSurface->DrawString(strText4, ox + 30, oy);
00147 
00148 
00149   oy += yspacing;
00150 
00151  //------------------
00152   // DrawPolyline TEST
00153   //------------------
00154 
00155 
00156   pointlist = new nsPoint[5];
00157   pointlist[0].x = pixelLoc(0, p2t, ox);
00158   pointlist[0].y = pixelLoc(0, p2t, oy);
00159 
00160   pointlist[1].x = pixelLoc(10, p2t, ox);
00161   pointlist[1].y = pixelLoc(0, p2t, oy);
00162 
00163   pointlist[2].x = pixelLoc(10, p2t, ox);
00164   pointlist[2].y = pixelLoc(10, p2t, oy);
00165 
00166   pointlist[3].x = pixelLoc(0, p2t, ox);
00167   pointlist[3].y = pixelLoc(10, p2t, oy);
00168 
00169   pointlist[4].x = pixelLoc(0, p2t, ox);
00170   pointlist[4].y = pixelLoc(1, p2t, oy);
00171 
00172   aSurface->DrawPolyline(pointlist,5);
00173 
00174   nsAutoString strText3(NS_LITERAL_STRING("DrawPolyline - There should be a one pixel gap in the rectangle"));
00175   aSurface->DrawString(strText3, ox + 30, oy);
00176 
00177   delete [] pointlist;
00178 
00179   oy += yspacing;
00180 
00181 
00182    //--------------
00183    // FillRect TEST
00184    //--------------
00185    
00186   aSurface->SetColor(NS_RGB(255, 0, 0));
00187   aSurface->DrawLine(pixelLoc(9,p2t, ox),
00188                      pixelLoc(0,p2t, oy), 
00189                      pixelLoc(9,p2t,  ox), 
00190                      pixelLoc(30,p2t, oy));
00191 
00192   aSurface->SetColor(NS_RGB(0, 0, 0));
00193   aSurface->FillRect(pixelLoc(0,  p2t, ox),
00194                      pixelLoc(10,  p2t, oy),
00195                      pixelLoc(10, p2t,  0),
00196                      pixelLoc(10, p2t,  0));
00197 
00198 
00199   nsAutoString strText2(NS_LITERAL_STRING("FillRect - The red line should be at the right edge under the rectangle"));
00200   aSurface->DrawString(strText2, ox + 30, oy);
00201 
00202 
00203 
00204   oy += yspacing; 
00205 
00206   //--------------
00207   // DrawRect TEST
00208   //--------------
00209 
00210   aSurface->SetColor(NS_RGB(255, 0, 0));
00211   aSurface->DrawLine(pixelLoc(9,p2t, ox),
00212                      pixelLoc(0,p2t, oy), 
00213                      pixelLoc(9,p2t,  ox), 
00214                      pixelLoc(30,p2t, oy));
00215 
00216   aSurface->SetColor(NS_RGB(0, 0, 0));
00217   aSurface->DrawRect(pixelLoc(0,  p2t, ox),
00218                      pixelLoc(10,  p2t, oy),
00219                      pixelLoc(10, p2t,  0),
00220                      pixelLoc(10, p2t,  0));
00221 
00222 
00223   nsAutoString strText1(NS_LITERAL_STRING("DrawRect - The red line should be at the right edge under the rectangle"));
00224   aSurface->DrawString(strText1, ox + 30, oy);
00225 
00226   oy += yspacing;
00227 
00228 
00229  /*
00230 #ifdef WINDOWSBROKEN
00231   pointlist = new nsPoint[5];
00232   pointlist[0].x = 200;pointlist[0].y = 200;
00233   pointlist[1].x = 250;pointlist[1].y = 200;
00234   pointlist[2].x = 240;pointlist[2].y = 220;
00235   pointlist[3].x = 260;pointlist[3].y = 240;
00236   pointlist[4].x = 225;pointlist[4].y = 225;
00237   aSurface->DrawPolygon(pointlist,6);
00238   aSurface->DrawString("This is an open Polygon\0",250,200);
00239   delete [] pointlist;
00240 #endif  
00241 
00242   aSurface->DrawEllipse(30, 150,50,100);
00243   aSurface->DrawString("This is an Ellipse\0",30,140);
00244 */
00245 
00246   return(30);
00247 }
00248 
00249 
00250 
00251 
00256 nsEventStatus PR_CALLBACK HandleEvent(nsGUIEvent *aEvent)
00257 { 
00258    nsEventStatus result = nsEventStatus_eIgnore;
00259 
00260    switch(aEvent->message) {
00261 
00262         case NS_PAINT: 
00263            
00264               // paint the background
00265             if (aEvent->widget == gWindow) {
00266                 nsIRenderingContext *drawCtx = ((nsPaintEvent*)aEvent)->renderingContext;
00267                 drawCtx->SetColor(aEvent->widget->GetBackgroundColor());
00268                 drawCtx->FillRect(*(((nsPaintEvent*)aEvent)->rect));
00269                 drawtest(drawCtx);
00270 
00271                 return nsEventStatus_eIgnore;
00272             }
00273 
00274             break;
00275 
00276         case NS_KEY_UP: {
00277             nsKeyEvent * ke = (nsKeyEvent*)aEvent;
00278             char str[256];
00279             sprintf(str, "Key Event Key Code[%d] Key [%c] Shift [%s] Control [%s] Alt [%s]",
00280               ke->keyCode, ke->keyCode, 
00281               (ke->isShift?"Pressed":"Released"),
00282               (ke->isControl?"Pressed":"Released"),
00283               (ke->isAlt?"Pressed":"Released"));
00284             printf("%s\n", str);
00285             switch(ke->keyCode) {
00286                case 'U':
00287                  gOffsetY -= 9;
00288                  gWindow->Invalidate(PR_FALSE);
00289                break;
00290 
00291                case 'D':
00292                  gOffsetY += 10;
00293                  gWindow->Invalidate(PR_FALSE);
00294                 break;
00295 
00296                case 'R':
00297                  gOffsetX += 9;
00298                  gWindow->Invalidate(PR_FALSE);
00299                break;
00300 
00301                case 'L':
00302                  gOffsetX -= 10;
00303                  gWindow->Invalidate(PR_FALSE);
00304                break;
00305             }
00306             }
00307             break;
00308         
00309         case NS_DESTROY:
00310             exit(0); // for now
00311             break;
00312 
00313         default:
00314             result = nsEventStatus_eIgnore;
00315     }
00316 
00317 
00318     return result;
00319 }
00320 
00321 
00325 nsresult CoverageTest(int *argc, char **argv)
00326 {
00327     nsresult res = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00328     if (NS_FAILED(res))
00329         return res;
00330 
00331     // Create the Event Queue for the UI thread...
00332     nsCOMPtr<nsIEventQueueService> eventQService =
00333         do_GetService(kEventQueueServiceCID, &res);
00334 
00335     if (NS_OK != res) {
00336         NS_ASSERTION(PR_FALSE, "Could not obtain the event queue service");
00337         return res;
00338     }
00339 
00340       // Create a application shell
00341     nsIAppShell *appShell;
00342     CallCreateInstance(kCAppShellCID, &appShell);
00343     if (appShell != nsnull) {
00344       fputs("Created AppShell\n", stderr);
00345       appShell->Create(argc, argv);
00346     } else {
00347       printf("AppShell is null!\n");
00348     }
00349 
00350     nsIDeviceContext* deviceContext = 0;
00351 
00352     // Create a device context for the widgets
00353 
00354     static NS_DEFINE_IID(kDeviceContextCID, NS_DEVICE_CONTEXT_CID);
00355     static NS_DEFINE_IID(kDeviceContextIID, NS_IDEVICE_CONTEXT_IID);
00356 
00357     //
00358     // create the main window
00359     //
00360     CallCreateInstance(kCWindowCID, &gWindow);
00361     nsRect rect(100, 100, 600, 700);
00362     gWindow->Create((nsIWidget*) nsnull, rect, HandleEvent, 
00363                    (nsIDeviceContext *) nsnull,
00364                    appShell);
00365 
00366     nsAutoString strTitle(NS_LITERAL_STRING("Pixel coverage test"));
00367     gWindow->SetTitle(strTitle);
00368 
00369 
00370     //
00371     // Create Device Context based on main window
00372     //
00373     res = CallCreateInstance(kDeviceContextCID, &deviceContext);
00374 
00375     if (NS_OK == res)
00376     {
00377       deviceContext->Init(gWindow->GetNativeData(NS_NATIVE_WIDGET));
00378       NS_ADDREF(deviceContext);
00379     }
00380 
00381     gWindow->Show(PR_TRUE);
00382 
00383  
00384     return(appShell->Run());
00385 }
00386 
00387