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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** 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 Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of 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 ***** */
#include "nsThrobber.h"
#include "nsIFactory.h"
#include "nsIWidget.h"
#include "nsVoidArray.h"
#include "nsITimer.h"
#include "nsFont.h"
#include "nsIRenderingContext.h"
#include "nsIFontMetrics.h"
#include "nsIComponentManager.h"
#include "nsWidgetsCID.h"
#include "nsCRT.h"
#include "prprf.h"
#include "nsIDeviceContext.h"
#include "nsGUIEvent.h"
#include "nsIImage.h"
#include "nsReadableUtils.h"
static NS_DEFINE_IID(kChildCID, NS_CHILD_CID);
static NS_DEFINE_IID(kIWidgetIID, NS_IWIDGET_IID);
#define THROB_NUM 14
#define THROBBER_AT "resource:/res/throbber/anims%02d.gif"
nsThrobber* nsThrobber::NewThrobber()
{
nsThrobber* t = new nsThrobber();
if (t) {
NS_ADDREF(t);
}
return t;
}
//----------------------------------------------------------------------
nsVoidArray* nsThrobber::gThrobbers;
nsThrobber*
nsThrobber::FindThrobberFor(nsIWidget* aWidget)
{
if (gThrobbers) {
PRInt32 i, n = gThrobbers->Count();
for (i = 0; i < n; i++) {
nsThrobber* th = (nsThrobber*) gThrobbers->ElementAt(i);
if (nsnull != th) {
if (th->mWidget == aWidget) {
return th;
}
}
}
}
return nsnull;
}
void
nsThrobber::AddThrobber(nsThrobber* aThrobber)
{
if (gThrobbers) {
gThrobbers->AppendElement(aThrobber);
}
}
void
nsThrobber::RemoveThrobber(nsThrobber* aThrobber)
{
if (gThrobbers) {
gThrobbers->RemoveElement(aThrobber);
}
}
nsEventStatus PR_CALLBACK
nsThrobber::HandleThrobberEvent(nsGUIEvent *aEvent)
{
nsThrobber* throbber = FindThrobberFor(aEvent->widget);
if (nsnull == throbber) {
return nsEventStatus_eIgnore;
}
switch (aEvent->message)
{
case NS_PAINT:
{
#if 0
nsPaintEvent *pe = (nsPaintEvent *)aEvent;
nsIRenderingContext *cx = pe->renderingContext;
nsRect bounds;
nsIImageRequest *imgreq;
nsIImage *img;
PRBool clipState;
pe->widget->GetClientBounds(bounds);
cx->SetClipRect(*pe->rect, nsClipCombine_kReplace, clipState);
cx->SetColor(NS_RGB(255, 255, 255));
cx->DrawLine(0, bounds.height - 1, 0, 0);
cx->DrawLine(0, 0, bounds.width, 0);
cx->SetColor(NS_RGB(128, 128, 128));
cx->DrawLine(bounds.width - 1, 1, bounds.width - 1, bounds.height - 1);
cx->DrawLine(bounds.width - 1, bounds.height - 1, 0, bounds.height - 1);
imgreq = (nsIImageRequest *)
throbber->mImages->ElementAt(throbber->mIndex);
if ((nsnull == imgreq) || (nsnull == (img = imgreq->GetImage())))
{
char str[10];
nsFont tfont = nsFont("monospace", 0, 0, 0, 0, 10);
nsIFontMetrics *met;
nscoord w, h;
cx->SetColor(NS_RGB(0, 0, 0));
cx->FillRect(1, 1, bounds.width - 2, bounds.height - 2);
PR_snprintf(str, sizeof(str), "%02d", throbber->mIndex);
cx->SetColor(NS_RGB(255, 255, 255));
cx->SetFont(tfont);
cx->GetFontMetrics(met);
if (nsnull != met)
{
cx->GetWidth(str, w);
met->GetHeight(h);
cx->DrawString(str, PRUint32(2), (bounds.width - w) >> 1, (bounds.height - h) >> 1);
NS_RELEASE(met);
}
}
else
{
cx->DrawImage(img, 1, 1);
NS_RELEASE(img);
}
#endif
break;
}
case NS_MOUSE_LEFT_BUTTON_UP:
// XXX wire up to API
//gTheViewer->GoTo(nsString("http://www.mozilla.org"));
break;
case NS_MOUSE_ENTER:
aEvent->widget->SetCursor(eCursor_hyperlink);
break;
case NS_MOUSE_EXIT:
aEvent->widget->SetCursor(eCursor_standard);
break;
}
return nsEventStatus_eIgnore;
}
//----------------------------------------------------------------------
PRInt32 nsThrobber::gNumThrobbers;
// Note: operator new zeros our memory
nsThrobber::nsThrobber()
{
if (0 == gNumThrobbers++) {
gThrobbers = new nsVoidArray;
}
AddThrobber(this);
}
nsThrobber::~nsThrobber()
{
Destroy();
}
void
nsThrobber::Destroy()
{
if (mWidget) {
mWidget->Destroy();
NS_RELEASE(mWidget);
}
RemoveThrobber(this);
DestroyThrobberImages();
if (0 == --gNumThrobbers) {
delete gThrobbers;
// Do this in case an event shows up later for the throbber...
gThrobbers = nsnull;
}
}
NS_IMPL_ISUPPORTS0(nsThrobber)
nsresult
nsThrobber::Init(nsIWidget* aParent, const nsRect& aBounds, const nsString& aFileNameMask, PRInt32 aNumImages)
{
mWidth = aBounds.width;
mHeight = aBounds.height;
mNumImages = aNumImages;
// Create widget
nsresult rv = CallCreateInstance(kChildCID, &mWidget);
if (NS_OK != rv) {
return rv;
}
mWidget->Create(aParent, aBounds, HandleThrobberEvent, NULL);
return LoadThrobberImages(aFileNameMask, aNumImages);
}
nsresult
nsThrobber::MoveTo(PRInt32 aX, PRInt32 aY)
{
NS_PRECONDITION(nsnull != mWidget, "no widget");
mWidget->Resize(aX, aY, mWidth, mHeight, PR_TRUE);
return NS_OK;
}
nsresult
nsThrobber::Show()
{
mWidget->Show(PR_TRUE);
return NS_OK;
}
nsresult
nsThrobber::Hide()
{
mWidget->Show(PR_FALSE);
return NS_OK;
}
nsresult
nsThrobber::Start()
{
mRunning = PR_TRUE;
return NS_OK;
}
nsresult
nsThrobber::Stop()
{
mRunning = PR_FALSE;
mIndex = 0;
mWidget->Invalidate(PR_FALSE);
return NS_OK;
}
#if 0
void
nsThrobber::Notify(nsIImageRequest *aImageRequest,
nsIImage *aImage,
nsImageNotification aNotificationType,
PRInt32 aParam1, PRInt32 aParam2,
void *aParam3)
{
if (aNotificationType == nsImageNotification_kImageComplete) {
mCompletedImages++;
// Remove ourselves as an observer of the image request object, because
// the image request objects each hold a reference to us. This avoids a
// circular reference problem. If we don't, our ref count will never reach
// 0 and we won't get destroyed and neither will the image request objects
aImageRequest->RemoveObserver((nsIImageRequestObserver*)this);
}
}
void
nsThrobber::NotifyError(nsIImageRequest *aImageRequest,
nsImageError aErrorType)
{
}
#endif
void
nsThrobber::ThrobTimerCallback(nsITimer *aTimer, void *aClosure)
{
nsThrobber* throbber = (nsThrobber*)aClosure;
throbber->Tick();
}
void
nsThrobber::Tick()
{
if (mRunning) {
mIndex++;
if (mIndex >= mNumImages)
mIndex = 0;
mWidget->Invalidate(PR_TRUE);
} else if (mCompletedImages == (PRUint32)mNumImages) {
mWidget->Invalidate(PR_TRUE);
mCompletedImages = 0;
}
}
nsresult
nsThrobber::LoadThrobberImages(const nsString& aFileNameMask, PRInt32 aNumImages)
{
#if 0
nsresult rv;
char url[2000];
mImages = new nsVoidArray(mNumImages);
if (nsnull == mImages) {
return NS_ERROR_OUT_OF_MEMORY;
}
rv = NS_NewImageGroup(&mImageGroup);
if (NS_OK != rv) {
return rv;
}
nsIDeviceContext *deviceCtx = mWidget->GetDeviceContext();
mImageGroup->Init(deviceCtx, nsnull);
NS_RELEASE(deviceCtx);
mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
if (NS_OK != rv) {
return rv;
}
mTimer->InitWithFuncCallback(ThrobTimerCallback, this, 33, nsITimer::TYPE_REPEATING_SLACK);
char * mask = ToNewCString(aFileNameMask);
for (PRInt32 cnt = 0; cnt < mNumImages; cnt++)
{
PR_snprintf(url, sizeof(url), mask, cnt);
nscolor bgcolor = NS_RGB(0, 0, 0);
mImages->InsertElementAt(mImageGroup->GetImage(url,
(nsIImageRequestObserver *)this,
&bgcolor,
mWidth - 2,
mHeight - 2, 0),
cnt);
}
if (nsnull != mask)
nsMemory::Free(mask);
mWidget->Invalidate(PR_TRUE);
return rv;
#endif
return NS_OK;
}
void
nsThrobber::DestroyThrobberImages()
{
if (mTimer) {
mTimer->Cancel();
}
#if 0
if (mImageGroup) {
mImageGroup->Interrupt();
for (PRInt32 cnt = 0; cnt < mNumImages; cnt++) {
nsIImageRequest *imgreq = (nsIImageRequest*) mImages->ElementAt(cnt);
NS_IF_RELEASE(imgreq);
}
NS_RELEASE(mImageGroup);
}
if (mImages) {
delete mImages;
mImages = nsnull;
}
#endif
}
|