1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 //---- Factory Includes & Stuff -----//
39 #include "nsIFactory.h"
40 #include "nsIComponentManager.h"
41 #include "nsIServiceManager.h"
42 #include "nsIEventQueueService.h"
43 #include "nsIEventQueue.h"
44 #include "nsGfxCIID.h"
46 #include "nsWidgetsCID.h"
47 #include "nsIWidget.h"
48 #include "nsGUIEvent.h"
51 #include "nsIRenderingContext.h"
53 #include "nsIDeviceContext.h"
55 #include "nsIComponentManager.h"
56 #include "nsWidgetsCID.h"
57 #include "nsIAppShell.h"
59 //------------------------------------------------------------------------------
61 nsIWidget
*gWindow
= NULL
;
63 #if defined(XP_WIN) || defined(XP_OS2)
64 #define TEXT_HEIGHT 25
67 #if defined(XP_UNIX) || defined(XP_BEOS)
68 #define TEXT_HEIGHT 30
72 #define TEXT_HEIGHT 30
76 static NS_DEFINE_CID(kCWindowCID
, NS_WINDOW_CID
);
77 static NS_DEFINE_CID(kEventQueueServiceCID
, NS_EVENTQUEUESERVICE_CID
);
78 static NS_DEFINE_CID(kCAppShellCID
, NS_APPSHELL_CID
);
84 nscoord
pixelLoc(nscoord aPixelValue
, float aP2t
, nscoord aOffset
)
86 return NSToIntRound((aPixelValue
+ aOffset
) * aP2t
);
91 drawtest(nsIRenderingContext
*aSurface
)
96 // Get the size of a pixel.
97 nsCOMPtr
<nsIDeviceContext
> deviceContext
;
98 aSurface
->GetDeviceContext(*getter_AddRefs(deviceContext
));
99 float p2t
; // pixel to twips conversion
100 p2t
= deviceContext
->DevUnitsToAppUnits();
102 font
= new nsFont("Times", NS_FONT_STYLE_NORMAL
,NS_FONT_VARIANT_NORMAL
,NS_FONT_WEIGHT_BOLD
,0,12);
103 aSurface
->SetFont(*font
);
107 gWindow
->GetClientBounds(rect
);
108 aSurface
->SetColor(NS_RGB(255, 255, 255));
109 aSurface
->FillRect(0,0,rect
.width
,rect
.height
);
111 aSurface
->SetColor(NS_RGB(255, 0, 0));
113 nsAutoString
strText6(NS_LITERAL_STRING("GFX - Pixel coverage test"));
114 aSurface
->DrawString(strText6
,150,30);
115 nsAutoString
strText5(NS_LITERAL_STRING("(Use (u, d, r, l) keys to scroll the window contents)"));
116 aSurface
->DrawString(strText5
,150,50);
120 aSurface
->Translate(gOffsetX
, gOffsetY
);
122 // Starting coordinates
123 nscoord ox
= 90; // pixels
124 nscoord oy
= 100; // pixels
125 // Spacing between tests
126 nscoord yspacing
= 50; // pixels
132 aSurface
->SetColor(NS_RGB(255, 0, 0));
133 aSurface
->DrawLine(pixelLoc(12, p2t
, ox
),
134 pixelLoc(0, p2t
, oy
),
135 pixelLoc(12, p2t
, ox
),
136 pixelLoc(10, p2t
, oy
));
138 aSurface
->SetColor(NS_RGB(0, 0, 0));
139 aSurface
->DrawLine(pixelLoc(0, p2t
, ox
),
140 pixelLoc(10,p2t
, oy
),
141 pixelLoc(12,p2t
, ox
),
142 pixelLoc(10, p2t
, oy
));
144 nsAutoString
strText4(NS_LITERAL_STRING("DrawLine - There should be a one pixel gap where the red and black lines meet"));
145 aSurface
->DrawString(strText4
, ox
+ 30, oy
);
155 pointlist
= new nsPoint
[5];
156 pointlist
[0].x
= pixelLoc(0, p2t
, ox
);
157 pointlist
[0].y
= pixelLoc(0, p2t
, oy
);
159 pointlist
[1].x
= pixelLoc(10, p2t
, ox
);
160 pointlist
[1].y
= pixelLoc(0, p2t
, oy
);
162 pointlist
[2].x
= pixelLoc(10, p2t
, ox
);
163 pointlist
[2].y
= pixelLoc(10, p2t
, oy
);
165 pointlist
[3].x
= pixelLoc(0, p2t
, ox
);
166 pointlist
[3].y
= pixelLoc(10, p2t
, oy
);
168 pointlist
[4].x
= pixelLoc(0, p2t
, ox
);
169 pointlist
[4].y
= pixelLoc(1, p2t
, oy
);
171 aSurface
->DrawPolyline(pointlist
,5);
173 nsAutoString
strText3(NS_LITERAL_STRING("DrawPolyline - There should be a one pixel gap in the rectangle"));
174 aSurface
->DrawString(strText3
, ox
+ 30, oy
);
185 aSurface
->SetColor(NS_RGB(255, 0, 0));
186 aSurface
->DrawLine(pixelLoc(9,p2t
, ox
),
189 pixelLoc(30,p2t
, oy
));
191 aSurface
->SetColor(NS_RGB(0, 0, 0));
192 aSurface
->FillRect(pixelLoc(0, p2t
, ox
),
193 pixelLoc(10, p2t
, oy
),
194 pixelLoc(10, p2t
, 0),
195 pixelLoc(10, p2t
, 0));
198 nsAutoString
strText2(NS_LITERAL_STRING("FillRect - The red line should be at the right edge under the rectangle"));
199 aSurface
->DrawString(strText2
, ox
+ 30, oy
);
209 aSurface
->SetColor(NS_RGB(255, 0, 0));
210 aSurface
->DrawLine(pixelLoc(9,p2t
, ox
),
213 pixelLoc(30,p2t
, oy
));
215 aSurface
->SetColor(NS_RGB(0, 0, 0));
216 aSurface
->DrawRect(pixelLoc(0, p2t
, ox
),
217 pixelLoc(10, p2t
, oy
),
218 pixelLoc(10, p2t
, 0),
219 pixelLoc(10, p2t
, 0));
222 nsAutoString
strText1(NS_LITERAL_STRING("DrawRect - The red line should be at the right edge under the rectangle"));
223 aSurface
->DrawString(strText1
, ox
+ 30, oy
);
230 pointlist = new nsPoint[5];
231 pointlist[0].x = 200;pointlist[0].y = 200;
232 pointlist[1].x = 250;pointlist[1].y = 200;
233 pointlist[2].x = 240;pointlist[2].y = 220;
234 pointlist[3].x = 260;pointlist[3].y = 240;
235 pointlist[4].x = 225;pointlist[4].y = 225;
236 aSurface->DrawPolygon(pointlist,6);
237 aSurface->DrawString("This is an open Polygon\0",250,200);
241 aSurface->DrawEllipse(30, 150,50,100);
242 aSurface->DrawString("This is an Ellipse\0",30,140);
251 /**--------------------------------------------------------------------------------
253 *--------------------------------------------------------------------------------
255 nsEventStatus
HandleEvent(nsGUIEvent
*aEvent
)
257 nsEventStatus result
= nsEventStatus_eIgnore
;
259 switch(aEvent
->message
) {
263 // paint the background
264 if (aEvent
->widget
== gWindow
) {
265 nsIRenderingContext
*drawCtx
= ((nsPaintEvent
*)aEvent
)->renderingContext
;
266 drawCtx
->SetColor(aEvent
->widget
->GetBackgroundColor());
267 drawCtx
->FillRect(*(((nsPaintEvent
*)aEvent
)->rect
));
270 return nsEventStatus_eIgnore
;
276 nsKeyEvent
* ke
= (nsKeyEvent
*)aEvent
;
278 sprintf(str
, "Key Event Key Code[%d] Key [%c] Shift [%s] Control [%s] Alt [%s]",
279 ke
->keyCode
, ke
->keyCode
,
280 (ke
->isShift
?"Pressed":"Released"),
281 (ke
->isControl
?"Pressed":"Released"),
282 (ke
->isAlt
?"Pressed":"Released"));
284 switch(ke
->keyCode
) {
287 gWindow
->Invalidate(PR_FALSE
);
292 gWindow
->Invalidate(PR_FALSE
);
297 gWindow
->Invalidate(PR_FALSE
);
302 gWindow
->Invalidate(PR_FALSE
);
313 result
= nsEventStatus_eIgnore
;
321 /**--------------------------------------------------------------------------------
324 nsresult
CoverageTest(int *argc
, char **argv
)
326 nsresult res
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
330 // Create the Event Queue for the UI thread...
331 nsCOMPtr
<nsIEventQueueService
> eventQService
=
332 do_GetService(kEventQueueServiceCID
, &res
);
335 NS_ASSERTION(PR_FALSE
, "Could not obtain the event queue service");
339 // Create a application shell
340 nsIAppShell
*appShell
;
341 CallCreateInstance(kCAppShellCID
, &appShell
);
342 if (appShell
!= nsnull
) {
343 fputs("Created AppShell\n", stderr
);
344 appShell
->Create(argc
, argv
);
346 printf("AppShell is null!\n");
349 nsIDeviceContext
* deviceContext
= 0;
351 // Create a device context for the widgets
353 static NS_DEFINE_IID(kDeviceContextCID
, NS_DEVICE_CONTEXT_CID
);
354 static NS_DEFINE_IID(kDeviceContextIID
, NS_IDEVICE_CONTEXT_IID
);
357 // create the main window
359 CallCreateInstance(kCWindowCID
, &gWindow
);
360 nsRect
rect(100, 100, 600, 700);
361 gWindow
->Create((nsIWidget
*) nsnull
, rect
, HandleEvent
,
362 (nsIDeviceContext
*) nsnull
,
365 nsAutoString
strTitle(NS_LITERAL_STRING("Pixel coverage test"));
366 gWindow
->SetTitle(strTitle
);
370 // Create Device Context based on main window
372 res
= CallCreateInstance(kDeviceContextCID
, &deviceContext
);
376 deviceContext
->Init(gWindow
->GetNativeData(NS_NATIVE_WIDGET
));
377 NS_ADDREF(deviceContext
);
380 gWindow
->Show(PR_TRUE
);
383 return(appShell
->Run());