add timer code for gtk from Frank Visser <fvisser@hgmp.mrc.ac.uk>

This commit is contained in:
blizzard%appliedtheory.com 1998-11-01 00:13:30 +00:00
Родитель 8427d0db3e
Коммит 384b8c7c1c
3 изменённых файлов: 234 добавлений и 0 удалений

31
base/src/gtk/Makefile Normal file
Просмотреть файл

@ -0,0 +1,31 @@
#
# The contents of this file are subject to the Netscape Public License
# Version 1.0 (the "NPL"); you may not use this file except in
# compliance with the NPL. You may obtain a copy of the NPL at
# http://www.mozilla.org/NPL/
#
# Software distributed under the NPL is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
# for the specific language governing rights and limitations under the
# NPL.
#
# The Initial Developer of this code under the NPL is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All Rights
# Reserved.
#
DEPTH = ../../..
LIBRARY_NAME = gmbaseunix
MODULE = raptor
REQUIRES = xpcom raptor
DEFINES = -D_IMPL_NS_WIDGET
CPPSRCS = nsTimer.cpp
include $(DEPTH)/config/rules.mk

36
base/src/gtk/Makefile.in Normal file
Просмотреть файл

@ -0,0 +1,36 @@
#
# The contents of this file are subject to the Netscape Public License
# Version 1.0 (the "NPL"); you may not use this file except in
# compliance with the NPL. You may obtain a copy of the NPL at
# http://www.mozilla.org/NPL/
#
# Software distributed under the NPL is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
# for the specific language governing rights and limitations under the
# NPL.
#
# The Initial Developer of this code under the NPL is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All Rights
# Reserved.
#
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
LIBRARY_NAME = gmbaseunix
MODULE = raptor
REQUIRES = xpcom raptor
DEFINES += -D_IMPL_NS_WIDGET
CPPSRCS = nsTimer.cpp
include $(topsrcdir)/config/rules.mk

167
base/src/gtk/nsTimer.cpp Normal file
Просмотреть файл

@ -0,0 +1,167 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsITimer.h"
#include "nsITimerCallback.h"
#include "nsCRT.h"
#include "prlog.h"
#include <stdio.h>
#include <limits.h>
#include <gtk/gtk.h>
static NS_DEFINE_IID(kITimerIID, NS_ITIMER_IID);
extern void nsTimerExpired(gpointer aCallData);
/*
* Implementation of timers using Gtk timer facility
*/
class TimerImpl : public nsITimer {
public:
public:
TimerImpl();
virtual ~TimerImpl();
virtual nsresult Init(nsTimerCallbackFunc aFunc,
void *aClosure,
// PRBool aRepeat,
PRUint32 aDelay);
virtual nsresult Init(nsITimerCallback *aCallback,
// PRBool aRepeat,
PRUint32 aDelay);
NS_DECL_ISUPPORTS
virtual void Cancel();
virtual PRUint32 GetDelay() { return mDelay; }
virtual void SetDelay(PRUint32 aDelay) { mDelay=aDelay; };
virtual void* GetClosure() { return mClosure; }
void FireTimeout();
private:
nsresult Init(PRUint32 aDelay);
PRUint32 mDelay;
nsTimerCallbackFunc mFunc;
void *mClosure;
nsITimerCallback *mCallback;
// PRBool mRepeat;
TimerImpl *mNext;
guint mTimerId;
};
void TimerImpl::FireTimeout()
{
if (mFunc != NULL) {
(*mFunc)(this, mClosure);
}
else if (mCallback != NULL) {
mCallback->Notify(this); // Fire the timer
}
// Always repeating here
// if (mRepeat)
// mTimerId = gtk_timeout_add(aDelay, (GtkFunction)nsTimerExpired, this);
}
TimerImpl::TimerImpl()
{
NS_INIT_REFCNT();
mFunc = NULL;
mCallback = NULL;
mNext = NULL;
mTimerId = 0;
mDelay = 0;
mClosure = NULL;
}
TimerImpl::~TimerImpl()
{
}
nsresult
TimerImpl::Init(nsTimerCallbackFunc aFunc,
void *aClosure,
// PRBool aRepeat,
PRUint32 aDelay)
{
mFunc = aFunc;
mClosure = aClosure;
// mRepeat = aRepeat;
mTimerId = gtk_timeout_add(aDelay, (GtkFunction)nsTimerExpired, this);
return Init(aDelay);
}
nsresult
TimerImpl::Init(nsITimerCallback *aCallback,
// PRBool aRepeat,
PRUint32 aDelay)
{
mCallback = aCallback;
// mRepeat = aRepeat;
mTimerId = gtk_timeout_add(aDelay, (GtkFunction)nsTimerExpired, this);
return Init(aDelay);
}
nsresult
TimerImpl::Init(PRUint32 aDelay)
{
mDelay = aDelay;
NS_ADDREF(this);
return NS_OK;
}
NS_IMPL_ISUPPORTS(TimerImpl, kITimerIID)
void
TimerImpl::Cancel()
{
gtk_timeout_remove(mTimerId);
}
NS_BASE nsresult NS_NewTimer(nsITimer** aInstancePtrResult)
{
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
if (nsnull == aInstancePtrResult) {
return NS_ERROR_NULL_POINTER;
}
TimerImpl *timer = new TimerImpl();
if (nsnull == timer) {
return NS_ERROR_OUT_OF_MEMORY;
}
return timer->QueryInterface(kITimerIID, (void **) aInstancePtrResult);
}
void nsTimerExpired(gpointer aCallData)
{
TimerImpl* timer = (TimerImpl *)aCallData;
timer->FireTimeout();
}