Initial checking for shared translator code. Many of the translators share many lines of the same code. This code helps eliminate duplicate code, and should help the process of making translators easier

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6817 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber 2004-02-29 19:29:57 +00:00
parent afed986dc0
commit c56079fb22
8 changed files with 1873 additions and 0 deletions

View File

@ -0,0 +1,687 @@
/*****************************************************************************/
// BaseTranslator
// Written by Michael Wilber, OBOS Translation Kit Team
//
// BaseTranslator.cpp
//
// The BaseTranslator class implements functionality common to most
// Translators so that this functionality need not be implemented over and
// over in each Translator.
//
//
// Copyright (c) 2004 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include <string.h>
#include <stdio.h>
#include "BaseTranslator.h"
// ---------------------------------------------------------------
// Constructor
//
// Sets up the version info and the name of the translator so that
// these values can be returned when they are requested.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
BaseTranslator::BaseTranslator(const char *name, const char *info,
const int32 version, const translation_format *inFormats,
int32 inCount, const translation_format *outFormats, int32 outCount,
const char *settingsFile, TranSetting *defaults, int32 defCount,
uint32 tranGroup, uint32 tranType)
: BTranslator()
{
fSettings = new TranslatorSettings(settingsFile, defaults, defCount);
fSettings->LoadSettings();
// load settings from the Base Translator settings file
fVersion = version;
fName = new char[strlen(name) + 1];
strcpy(fName, name);
fInfo = new char[strlen(info) + 41];
sprintf(fInfo, "%s v%d.%d.%d %s", info,
static_cast<int>(B_TRANSLATION_MAJOR_VER(fVersion)),
static_cast<int>(B_TRANSLATION_MINOR_VER(fVersion)),
static_cast<int>(B_TRANSLATION_REVSN_VER(fVersion)),
__DATE__);
fInputFormats = inFormats;
fInputCount = (fInputFormats) ? inCount : 0;
fOutputFormats = outFormats;
fOutputCount = (fOutputFormats) ? outCount : 0;
fTranGroup = tranGroup;
fTranType = tranType;
}
// ---------------------------------------------------------------
// Destructor
//
// Does nothing
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
//
// NOTE: It may be the case, that under Be's libtranslation.so,
// that this destructor will never be called
BaseTranslator::~BaseTranslator()
{
fSettings->Release();
delete[] fName;
delete[] fInfo;
}
// ---------------------------------------------------------------
// TranslatorName
//
// Returns the short name of the translator.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: a const char * to the short name of the translator
// ---------------------------------------------------------------
const char *
BaseTranslator::TranslatorName() const
{
return fName;
}
// ---------------------------------------------------------------
// TranslatorInfo
//
// Returns a more verbose name for the translator than the one
// TranslatorName() returns. This usually includes version info.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: a const char * to the verbose name of the translator
// ---------------------------------------------------------------
const char *
BaseTranslator::TranslatorInfo() const
{
return fInfo;
}
// ---------------------------------------------------------------
// TranslatorVersion
//
// Returns the integer representation of the current version of
// this translator.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
int32
BaseTranslator::TranslatorVersion() const
{
return fVersion;
}
// ---------------------------------------------------------------
// InputFormats
//
// Returns a list of input formats supported by this translator.
//
// Preconditions:
//
// Parameters: out_count, The number of input formats
// support is returned here.
//
// Postconditions:
//
// Returns: the array of input formats and the number of input
// formats through the out_count parameter
// ---------------------------------------------------------------
const translation_format *
BaseTranslator::InputFormats(int32 *out_count) const
{
if (out_count) {
*out_count = fInputCount;
return fInputFormats;
} else
return NULL;
}
// ---------------------------------------------------------------
// OutputFormats
//
// Returns a list of output formats supported by this translator.
//
// Preconditions:
//
// Parameters: out_count, The number of output formats
// support is returned here.
//
// Postconditions:
//
// Returns: the array of output formats and the number of output
// formats through the out_count parameter
// ---------------------------------------------------------------
const translation_format *
BaseTranslator::OutputFormats(int32 *out_count) const
{
if (out_count) {
*out_count = fOutputCount;
return fOutputFormats;
} else
return NULL;
}
// ---------------------------------------------------------------
// identify_bits_header
//
// Determines if the data in inSource is in the
// B_TRANSLATOR_BITMAP ('bits') format. If it is, it returns
// info about the data in inSource to outInfo and pheader.
//
// Preconditions:
//
// Parameters: inSource, The source of the image data
//
// outInfo, Information about the translator
// is copied here
//
// amtread, Amount of data read from inSource
// before this function was called
//
// read, Pointer to the data that was read
// in before this function was called
//
// pheader, The bits header is copied here after
// it is read in from inSource
//
// Postconditions:
//
// Returns: B_NO_TRANSLATOR, if the data does not look like
// bits format data
//
// B_ERROR, if the header data could not be converted to host
// format
//
// B_OK, if the data looks like bits data and no errors were
// encountered
// ---------------------------------------------------------------
status_t
BaseTranslator::identify_bits_header(BPositionIO *inSource,
translator_info *outInfo, TranslatorBitmap *pheader = NULL)
{
TranslatorBitmap header;
// read in the header
ssize_t size = sizeof(TranslatorBitmap);
if (inSource->Read(
(reinterpret_cast<uint8 *> (&header)), size) != size)
return B_NO_TRANSLATOR;
// convert to host byte order
if (swap_data(B_UINT32_TYPE, &header, sizeof(TranslatorBitmap),
B_SWAP_BENDIAN_TO_HOST) != B_OK)
return B_ERROR;
// check if header values are reasonable
if (header.colors != B_RGB32 &&
header.colors != B_RGB32_BIG &&
header.colors != B_RGBA32 &&
header.colors != B_RGBA32_BIG &&
header.colors != B_RGB24 &&
header.colors != B_RGB24_BIG &&
header.colors != B_RGB16 &&
header.colors != B_RGB16_BIG &&
header.colors != B_RGB15 &&
header.colors != B_RGB15_BIG &&
header.colors != B_RGBA15 &&
header.colors != B_RGBA15_BIG &&
header.colors != B_CMAP8 &&
header.colors != B_GRAY8 &&
header.colors != B_GRAY1 &&
header.colors != B_CMYK32 &&
header.colors != B_CMY32 &&
header.colors != B_CMYA32 &&
header.colors != B_CMY24)
return B_NO_TRANSLATOR;
if (header.rowBytes * (header.bounds.Height() + 1) != header.dataSize)
return B_NO_TRANSLATOR;
if (outInfo) {
outInfo->type = B_TRANSLATOR_BITMAP;
outInfo->group = B_TRANSLATOR_BITMAP;
outInfo->quality = 0.2;
outInfo->capability = 0.2;
strcpy(outInfo->name, "Be Bitmap Format");
strcpy(outInfo->MIME, "image/x-be-bitmap");
// Look for quality / capability info in fInputFormats
for (int32 i = 0; i < fInputCount; i++) {
if (fInputFormats[i].type == B_TRANSLATOR_BITMAP &&
fInputFormats[i].group == B_TRANSLATOR_BITMAP) {
outInfo->quality = fInputFormats[i].quality;
outInfo->capability = fInputFormats[i].capability;
strcpy(outInfo->name, fInputFormats[i].name);
break;
}
}
}
if (pheader) {
pheader->magic = header.magic;
pheader->bounds = header.bounds;
pheader->rowBytes = header.rowBytes;
pheader->colors = header.colors;
pheader->dataSize = header.dataSize;
}
return B_OK;
}
// ---------------------------------------------------------------
// BitsCheck
//
// Examines the input stream for B_TRANSLATOR_BITMAP format
// information and determines if BaseTranslator can handle
// the translation entirely, if it must pass the task of
// translation to the derived translator or if the stream cannot
// be decoded by the BaseTranslator or the derived translator.
//
// Preconditions:
//
// Parameters: inSource, where the data to examine is
//
// ioExtension, configuration settings for the
// translator
//
// outType, The format that the user wants
// the data in inSource to be
// converted to. NOTE: This is passed by
// reference so that it can modify the
// outType that is seen by the
// BaseTranslator and the derived
// translator
//
// Postconditions:
//
// Returns: B_NO_TRANSLATOR, if this translator can't handle
// the data in inSource
//
// B_ERROR, if there was an error converting the data to the host
// format
//
// B_BAD_VALUE, if the settings in ioExtension are bad
//
// B_OK, if this translator understand the data and there were
// no errors found
// ---------------------------------------------------------------
status_t
BaseTranslator::BitsCheck(BPositionIO *inSource, BMessage *ioExtension,
uint32 &outType)
{
if (!outType)
outType = B_TRANSLATOR_BITMAP;
if (outType != B_TRANSLATOR_BITMAP && outType != fTranType)
return B_NO_TRANSLATOR;
// Convert the magic numbers to the various byte orders so that
// I won't have to convert the data read in to see whether or not
// it is a supported type
uint32 nbits = B_HOST_TO_BENDIAN_INT32(B_TRANSLATOR_BITMAP);
// Read in the magic number and determine if it
// is a supported type
uint8 ch[4];
if (inSource->Read(ch, 4) != 4)
return B_NO_TRANSLATOR;
inSource->Seek(-4, SEEK_CUR);
// seek backward becuase functions used after this one
// expect the stream to be at the beginning
// Read settings from ioExtension
if (ioExtension && fSettings->LoadSettings(ioExtension) < B_OK)
return B_BAD_VALUE;
uint32 n32ch;
memcpy(&n32ch, ch, sizeof(uint32));
if (n32ch == nbits)
return B_OK;
else
return B_OK + 1;
}
status_t
BaseTranslator::BitsIdentify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType)
{
status_t result;
result = BitsCheck(inSource, ioExtension, outType);
if (result == B_OK)
result = identify_bits_header(inSource, outInfo);
else if (result == B_OK + 1)
// if NOT B_TRANSLATOR_BITMAP, it could be an image in the
// derived format
result = DerivedIdentify(inSource, inFormat, ioExtension,
outInfo, outType);
return result;
}
// ---------------------------------------------------------------
// Identify
//
// Examines the data from inSource and determines if it is in a
// format that this translator knows how to work with.
//
// Preconditions:
//
// Parameters: inSource, where the data to examine is
//
// inFormat, a hint about the data in inSource,
// it is ignored since it is only a hint
//
// ioExtension, configuration settings for the
// translator
//
// outInfo, information about what data is in
// inSource and how well this translator
// can handle that data is stored here
//
// outType, The format that the user wants
// the data in inSource to be
// converted to
//
// Postconditions:
//
// Returns: B_NO_TRANSLATOR, if this translator can't handle
// the data in inSource
//
// B_ERROR, if there was an error converting the data to the host
// format
//
// B_BAD_VALUE, if the settings in ioExtension are bad
//
// B_OK, if this translator understand the data and there were
// no errors found
// ---------------------------------------------------------------
status_t
BaseTranslator::Identify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType)
{
switch (fTranGroup) {
case B_TRANSLATOR_BITMAP:
return BitsIdentify(inSource, inFormat, ioExtension,
outInfo, outType);
default:
return DerivedIdentify(inSource, inFormat, ioExtension,
outInfo, outType);
}
}
// ---------------------------------------------------------------
// translate_from_bits_to_bits
//
// Convert the data in inSource from the Be Bitmap format ('bits')
// to the format specified in outType (either bits or Base).
//
// Preconditions:
//
// Parameters: inSource, the bits data to translate
//
// amtread, the amount of data already read from
// inSource
//
// read, pointer to the data already read from
// inSource
//
// outType, the type of data to convert to
//
// outDestination, where the output is written to
//
// Postconditions:
//
// Returns: B_NO_TRANSLATOR, if the data is not in a supported
// format
//
// B_ERROR, if there was an error allocating memory or some other
// error
//
// B_OK, if successfully translated the data from the bits format
// ---------------------------------------------------------------
status_t
BaseTranslator::translate_from_bits_to_bits(BPositionIO *inSource,
uint32 outType, BPositionIO *outDestination)
{
TranslatorBitmap bitsHeader;
bool bheaderonly = false, bdataonly = false;
status_t result;
result = identify_bits_header(inSource, NULL, &bitsHeader);
if (result != B_OK)
return result;
// Translate B_TRANSLATOR_BITMAP to B_TRANSLATOR_BITMAP, easy enough :)
if (outType == B_TRANSLATOR_BITMAP) {
// write out bitsHeader (only if configured to)
if (bheaderonly || (!bheaderonly && !bdataonly)) {
if (swap_data(B_UINT32_TYPE, &bitsHeader,
sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN) != B_OK)
return B_ERROR;
if (outDestination->Write(&bitsHeader,
sizeof(TranslatorBitmap)) != sizeof(TranslatorBitmap))
return B_ERROR;
}
// write out the data (only if configured to)
if (bdataonly || (!bheaderonly && !bdataonly)) {
uint8 buf[1024];
uint32 remaining = B_BENDIAN_TO_HOST_INT32(bitsHeader.dataSize);
ssize_t rd, writ;
rd = inSource->Read(buf, 1024);
while (rd > 0) {
writ = outDestination->Write(buf, rd);
if (writ < 0)
break;
remaining -= static_cast<uint32>(writ);
rd = inSource->Read(buf, min(1024, remaining));
}
if (remaining > 0)
return B_ERROR;
else
return B_OK;
} else
return B_OK;
} else
return B_NO_TRANSLATOR;
}
status_t
BaseTranslator::BitsTranslate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension, uint32 outType,
BPositionIO *outDestination)
{
status_t result;
result = BitsCheck(inSource, ioExtension, outType);
if (result == B_OK && outType == B_TRANSLATOR_BITMAP)
result = translate_from_bits_to_bits(inSource, outType,
outDestination);
else if (result >= B_OK)
// If NOT B_TRANSLATOR_BITMAP type it could be the derived format
result = DerivedTranslate(inSource, inInfo, ioExtension, outType,
outDestination, (result == B_OK));
return result;
}
// ---------------------------------------------------------------
// Translate
//
// Translates the data in inSource to the type outType and stores
// the translated data in outDestination.
//
// Preconditions:
//
// Parameters: inSource, the data to be translated
//
// inInfo, hint about the data in inSource (not used)
//
// ioExtension, configuration options for the
// translator
//
// outType, the type to convert inSource to
//
// outDestination, where the translated data is
// put
//
// Postconditions:
//
// Returns: B_BAD_VALUE, if the options in ioExtension are bad
//
// B_NO_TRANSLATOR, if this translator doesn't understand the data
//
// B_ERROR, if there was an error allocating memory or converting
// data
//
// B_OK, if all went well
// ---------------------------------------------------------------
status_t
BaseTranslator::Translate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension, uint32 outType,
BPositionIO *outDestination)
{
switch (fTranGroup) {
case B_TRANSLATOR_BITMAP:
return BitsTranslate(inSource, inInfo, ioExtension, outType,
outDestination);
default:
return DerivedTranslate(inSource, inInfo, ioExtension, outType,
outDestination, -1);
}
}
// returns the current translator settings into ioExtension
status_t
BaseTranslator::GetConfigurationMessage(BMessage *ioExtension)
{
return fSettings->GetConfigurationMessage(ioExtension);
}
// ---------------------------------------------------------------
// MakeConfigurationView
//
// Makes a BView object for configuring / displaying info about
// this translator.
//
// Preconditions:
//
// Parameters: ioExtension, configuration options for the
// translator
//
// outView, the view to configure the
// translator is stored here
//
// outExtent, the bounds of the view are
// stored here
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
status_t
BaseTranslator::MakeConfigurationView(BMessage *ioExtension, BView **outView,
BRect *outExtent)
{
if (!outView || !outExtent)
return B_BAD_VALUE;
if (ioExtension && fSettings->LoadSettings(ioExtension) != B_OK)
return B_BAD_VALUE;
BView *view = NewConfigView(AcquireSettings());
// implemented in derived class
if (view) {
*outView = view;
*outExtent = view->Bounds();
return B_OK;
} else
return BTranslator::MakeConfigurationView(ioExtension, outView,
outExtent);
}
TranslatorSettings *
BaseTranslator::AcquireSettings()
{
return fSettings->Acquire();
}
///////////////////////////////////////////////////////////
// Functions to be implemented by derived classes
status_t
BaseTranslator::DerivedIdentify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType)
{
return B_NO_TRANSLATOR;
}
status_t
BaseTranslator::DerivedTranslate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension, uint32 outType,
BPositionIO *outDestination, int32 baseType)
{
return B_NO_TRANSLATOR;
}
BView *
BaseTranslator::NewConfigView(TranslatorSettings *settings)
{
return NULL;
}

View File

@ -0,0 +1,154 @@
/*****************************************************************************/
// BaseTranslator
// Written by Michael Wilber, OBOS Translation Kit Team
//
// BaseTranslator.h
//
// The BaseTranslator class implements functionality common to most
// Translators so that this functionality need not be implemented over and
// over in each Translator.
//
//
// Copyright (c) 2004 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef BASE_TRANSLATOR_H
#define BASE_TRANSLATOR_H
#include <Translator.h>
#include <TranslatorFormats.h>
#include <TranslationDefs.h>
#include <GraphicsDefs.h>
#include <InterfaceDefs.h>
#include <DataIO.h>
#include <ByteOrder.h>
#include <View.h>
#include "TranslatorSettings.h"
#ifndef min
#define min(a,b) ((a < b) ? (a) : (b))
#endif
#ifndef max
#define max(a,b) ((a > b) ? (a) : (b))
#endif
class BaseTranslator : public BTranslator {
public:
BaseTranslator(const char *name, const char *info,
const int32 version, const translation_format *inFormats,
int32 inCount, const translation_format *outFormats, int32 outCount,
const char *settingsFile, TranSetting *defaults, int32 defCount,
uint32 tranGroup, uint32 tranType);
virtual const char *TranslatorName() const;
// returns the short name of the translator
virtual const char *TranslatorInfo() const;
// returns a verbose name/description for the translator
virtual int32 TranslatorVersion() const;
// returns the version of the translator
virtual const translation_format *InputFormats(int32 *out_count)
const;
// returns the input formats and the count of input formats
// that this translator supports
virtual const translation_format *OutputFormats(int32 *out_count)
const;
// returns the output formats and the count of output formats
// that this translator supports
virtual status_t Identify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType);
// determines whether or not this translator can convert the
// data in inSource to the type outType
virtual status_t Translate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination);
// this function is the whole point of the Translation Kit,
// it translates the data in inSource to outDestination
// using the format outType
virtual status_t GetConfigurationMessage(BMessage *ioExtension);
// write the current state of the translator into
// the supplied BMessage object
virtual status_t MakeConfigurationView(BMessage *ioExtension,
BView **outView, BRect *outExtent);
// creates and returns the view for displaying information
// about this translator
TranslatorSettings *AcquireSettings();
// Functions to be implmemented by derived class
virtual status_t DerivedIdentify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType);
virtual status_t DerivedTranslate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension,
uint32 outType, BPositionIO *outDestination, int32 baseType);
virtual BView *NewConfigView(TranslatorSettings *settings);
protected:
status_t BitsCheck(BPositionIO *inSource, BMessage *ioExtension,
uint32 &outType);
status_t BitsIdentify(BPositionIO *inSource,
const translation_format *inFormat, BMessage *ioExtension,
translator_info *outInfo, uint32 outType);
status_t identify_bits_header(BPositionIO *inSource,
translator_info *outInfo, TranslatorBitmap *pheader = NULL);
status_t BitsTranslate(BPositionIO *inSource,
const translator_info *inInfo, BMessage *ioExtension, uint32 outType,
BPositionIO *outDestination);
status_t translate_from_bits_to_bits(BPositionIO *inSource,
uint32 outType, BPositionIO *outDestination);
virtual ~BaseTranslator();
// this is protected because the object is deleted by the
// Release() function instead of being deleted directly by
// the user
TranslatorSettings *fSettings;
private:
int32 fVersion;
char *fName;
char *fInfo;
const translation_format *fInputFormats;
int32 fInputCount;
const translation_format *fOutputFormats;
int32 fOutputCount;
uint32 fTranGroup;
uint32 fTranType;
};
#endif // #ifndef BASE_TRANSLATOR_H

View File

@ -0,0 +1,230 @@
/*****************************************************************************/
// StreamBuffer
// Written by Michael Wilber, OBOS Translation Kit Team
//
// StreamBuffer.cpp
//
// This class is for buffering data from a BPositionIO object in order to
// improve performance for cases when small amounts of data are frequently
// read from a BPositionIO object.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "StreamBuffer.h"
#ifndef min
#define min(x,y) (((x) < (y)) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) (((x) > (y)) ? (x) : (y))
#endif
// ---------------------------------------------------------------
// Constructor
//
// Initializes the StreamBuffer to read from pstream, buffering
// nbuffersize bytes of data at a time. Note that if nbuffersize
// is smaller than MIN_BUFFER_SIZE, MIN_BUFFER_SIZE is used
// as the buffer size.
//
// Preconditions:
//
// Parameters: pstream, the stream to be buffered
//
// nbuffersize, number of bytes to be read from
// pstream at a time
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
StreamBuffer::StreamBuffer(BPositionIO *pstream, size_t nbuffersize,
bool binitialread)
{
fpStream = pstream;
fpBuffer = NULL;
fnBufferSize = 0;
fnLen = 0;
fnPos = 0;
if (!pstream)
return;
fnBufferSize = max(nbuffersize, MIN_BUFFER_SIZE);
fpBuffer = new uint8[fnBufferSize];
if (fpBuffer && binitialread)
ReadStream();
// Fill the buffer with data so that
// object is prepared for first call to
// Read()
}
// ---------------------------------------------------------------
// Destructor
//
// Destroys data allocated for this object
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
StreamBuffer::~StreamBuffer()
{
fnBufferSize = 0;
fnLen = 0;
fnPos = 0;
fpStream = NULL;
delete[] fpBuffer;
fpBuffer = NULL;
}
// ---------------------------------------------------------------
// InitCheck
//
// Determines whether the constructor failed or not
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: B_OK if object has been initialized successfully,
// B_ERROR if not
// ---------------------------------------------------------------
status_t
StreamBuffer::InitCheck()
{
if (fpStream && fpBuffer)
return B_OK;
else
return B_ERROR;
}
// ---------------------------------------------------------------
// Read
//
// Copies up to nbytes of data from the stream into pinto
//
// Preconditions: ReadStream() must be called once before this
// function is called (the constructor does this)
//
// Parameters: pinto, the buffer to be copied to
//
// nbytes, the maximum number of bytes to copy
//
// Postconditions:
//
// Returns: the number of bytes successfully read or an
// error code returned by BPositionIO::Read()
// ---------------------------------------------------------------
ssize_t
StreamBuffer::Read(uint8 *pinto, size_t nbytes)
{
ssize_t result = B_ERROR;
size_t rd1 = 0, rd2 = 0;
rd1 = min(nbytes, fnLen - fnPos);
memcpy(pinto, fpBuffer + fnPos, rd1);
fnPos += rd1;
if (rd1 < nbytes) {
pinto += rd1;
result = ReadStream();
if (result > 0) {
rd2 = min(nbytes - rd1, fnLen);
memcpy(pinto, fpBuffer, rd2);
fnPos += rd2;
} else
// return error code or zero
return result;
}
return rd1 + rd2;
}
// ---------------------------------------------------------------
// Seek
//
// Seeks the stream to the given position and refreshes the
// read buffer. If the seek operation fails, the read buffer
// will be reset.
//
// Preconditions: fpBuffer must be allocated and fnBufferSize
// must be valid
//
// Parameters:
//
// Postconditions:
//
// Returns: true if the seek was successful,
// false if the seek operation failed
// ---------------------------------------------------------------
bool
StreamBuffer::Seek(off_t position)
{
fnLen = 0;
fnPos = 0;
if (fpStream->Seek(position, SEEK_SET) == position) {
ReadStream();
return true;
}
return false;
}
// ---------------------------------------------------------------
// ReadStream
//
// Fills the stream buffer with data read in from the stream
//
// Preconditions: fpBuffer must be allocated and fnBufferSize
// must be valid
//
// Parameters:
//
// Postconditions:
//
// Returns: the number of bytes successfully read or an
// error code returned by BPositionIO::Read()
// ---------------------------------------------------------------
ssize_t
StreamBuffer::ReadStream()
{
ssize_t rd;
rd = fpStream->Read(fpBuffer, fnBufferSize);
if (rd >= 0) {
fnLen = rd;
fnPos = 0;
}
return rd;
}

View File

@ -0,0 +1,70 @@
/*****************************************************************************/
// StreamBuffer
// Written by Michael Wilber, OBOS Translation Kit Team
//
// StreamBuffer.h
//
// This class is for buffering data from a BPositionIO object in order to
// improve performance for cases when small amounts of data are frequently
// read from a BPositionIO object.
//
//
// Copyright (c) 2003 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef STREAM_BUFFER_H
#define STREAM_BUFFER_H
#include <DataIO.h>
#define MIN_BUFFER_SIZE 512
class StreamBuffer {
public:
StreamBuffer(BPositionIO *pstream, size_t nbuffersize, bool binitialread);
~StreamBuffer();
status_t InitCheck();
// Determines whether the constructor failed or not
ssize_t Read(uint8 *pinto, size_t nbytes);
// copy nbytes from the stream into pinto
bool Seek(off_t position);
// seek the stream to the given position
private:
ssize_t ReadStream();
// Load the stream buffer from the stream
BPositionIO *fpStream;
// stream object this object is buffering
uint8 *fpBuffer;
// buffered data from fpStream
size_t fnBufferSize;
// number of bytes of memory allocated for fpBuffer
size_t fnLen;
// number of bytes of actual data in fpBuffer
size_t fnPos;
// current position in the buffer
};
#endif

View File

@ -0,0 +1,469 @@
/*****************************************************************************/
// TranslatorSettings
// Written by Michael Wilber, OBOS Translation Kit Team
//
// TranslatorSettings.cpp
//
// This class manages (saves/loads/locks/unlocks) the settings
// for a Translator.
//
//
// Copyright (c) 2004 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include <string.h>
#include <File.h>
#include <FindDirectory.h>
#include <TranslatorFormats.h>
// for B_TRANSLATOR_EXT_*
#include "TranslatorSettings.h"
// ---------------------------------------------------------------
// Constructor
//
// Sets the default settings, location for the settings file
// and sets the reference count to 1
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
TranslatorSettings::TranslatorSettings(const char *settingsFile,
TranSetting *defaults, int32 defCount)
: fLock("TranslatorSettings Lock")
{
if (find_directory(B_USER_SETTINGS_DIRECTORY, &fSettingsPath))
fSettingsPath.SetTo("/tmp");
fSettingsPath.Append(settingsFile);
fRefCount = 1;
if (defCount > 0) {
fDefaults = defaults;
fDefCount = defCount;
} else {
fDefaults = NULL;
fDefCount = 0;
}
// Add Default Settings
// (Used when loading from the settings file or from
// a BMessage fails)
const TranSetting *defs = fDefaults;
for (int32 i = 0; i < fDefCount; i++) {
switch (defs[i].dataType) {
case TRAN_SETTING_BOOL:
fSettingsMsg.AddBool(defs[i].name,
static_cast<bool>(defs[i].defaultVal));
break;
case TRAN_SETTING_INT32:
fSettingsMsg.AddInt32(defs[i].name, defs[i].defaultVal);
break;
default:
// ASSERT here? Erase the bogus setting entry instead?
break;
}
}
}
// ---------------------------------------------------------------
// Acquire
//
// Returns a pointer to the TranslatorSettings and increments
// the reference count.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: pointer to this TranslatorSettings object
// ---------------------------------------------------------------
TranslatorSettings *
TranslatorSettings::Acquire()
{
TranslatorSettings *psettings = NULL;
fLock.Lock();
fRefCount++;
psettings = this;
fLock.Unlock();
return psettings;
}
// ---------------------------------------------------------------
// Release
//
// Decrements the reference count and deletes the
// TranslatorSettings if the reference count is zero.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: pointer to this TranslatorSettings object if
// the reference count is greater than zero, returns NULL
// if the reference count is zero and the TranslatorSettings
// object has been deleted
// ---------------------------------------------------------------
TranslatorSettings *
TranslatorSettings::Release()
{
TranslatorSettings *psettings = NULL;
fLock.Lock();
fRefCount--;
if (fRefCount > 0) {
psettings = this;
fLock.Unlock();
} else
delete this;
// delete this object and
// release locks
return psettings;
}
// ---------------------------------------------------------------
// Destructor
//
// Does nothing!
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
TranslatorSettings::~TranslatorSettings()
{
}
// ---------------------------------------------------------------
// LoadSettings
//
// Loads the settings by reading them from the default
// settings file.
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: B_OK if there were no errors or an error code from
// BFile::SetTo() or BMessage::Unflatten() if there were errors
// ---------------------------------------------------------------
status_t
TranslatorSettings::LoadSettings()
{
status_t result;
fLock.Lock();
// Don't try to open the settings file if there are
// no settings that need to be loaded
if (fDefCount > 0) {
BFile settingsFile;
result = settingsFile.SetTo(fSettingsPath.Path(), B_READ_ONLY);
if (result == B_OK) {
BMessage msg;
result = msg.Unflatten(&settingsFile);
if (result == B_OK)
result = LoadSettings(&msg);
}
} else
result = B_OK;
fLock.Unlock();
return result;
}
// ---------------------------------------------------------------
// LoadSettings
//
// Loads the settings from a BMessage passed to the function.
//
// Preconditions:
//
// Parameters: pmsg pointer to BMessage that contains the
// settings
//
// Postconditions:
//
// Returns: B_BAD_VALUE if pmsg is NULL or invalid options
// have been found, B_OK if there were no
// errors or an error code from BMessage::FindBool() or
// BMessage::ReplaceBool() if there were other errors
// ---------------------------------------------------------------
status_t
TranslatorSettings::LoadSettings(BMessage *pmsg)
{
status_t result = B_OK;
if (pmsg) {
fLock.Lock();
const TranSetting *defs = fDefaults;
for (int32 i = 0; i < fDefCount; i++) {
bool tempBool;
int32 tempInt32;
status_t ret;
switch (defs[i].dataType) {
case TRAN_SETTING_BOOL:
ret = pmsg->FindBool(defs[i].name, &tempBool);
if (ret < B_OK)
tempBool = static_cast<bool>(defs[i].defaultVal);
fSettingsMsg.ReplaceBool(defs[i].name, tempBool);
break;
case TRAN_SETTING_INT32:
ret = pmsg->FindInt32(defs[i].name, &tempInt32);
if (ret < B_OK)
tempInt32 = defs[i].defaultVal;
fSettingsMsg.ReplaceInt32(defs[i].name, tempInt32);
break;
default:
// ASSERT here? Erase the bogus setting entry instead?
break;
}
}
fLock.Unlock();
}
return result;
}
// ---------------------------------------------------------------
// SaveSettings
//
// Saves the settings as a flattened BMessage to the default
// settings file
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns: B_OK if no errors or an error code from BFile::SetTo()
// or BMessage::Flatten() if there were errors
// ---------------------------------------------------------------
status_t
TranslatorSettings::SaveSettings()
{
status_t result;
fLock.Lock();
// Only write out settings file if there are
// actual settings stored by this object
if (fDefCount > 0) {
BFile settingsFile;
result = settingsFile.SetTo(fSettingsPath.Path(),
B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (result == B_OK)
result = fSettingsMsg.Flatten(&settingsFile);
} else
result = B_OK;
fLock.Unlock();
return result;
}
// ---------------------------------------------------------------
// GetConfigurationMessage
//
// Saves the current settings to the BMessage passed to the
// function
//
// Preconditions:
//
// Parameters: pmsg pointer to BMessage where the settings
// will be stored
//
// Postconditions:
//
// Returns: B_OK if there were no errors or an error code from
// BMessage::RemoveName() or BMessage::AddBool() if there were
// errors
// ---------------------------------------------------------------
status_t
TranslatorSettings::GetConfigurationMessage(BMessage *pmsg)
{
status_t result = B_BAD_VALUE;
if (pmsg) {
int32 i;
for (i = 0; i < fDefCount; i++) {
result = pmsg->RemoveName(fDefaults[i].name);
if (result != B_OK && result != B_NAME_NOT_FOUND)
break;
}
if (i == fDefCount) {
fLock.Lock();
result = B_OK;
const TranSetting *defs = fDefaults;
for (i = 0; i < fDefCount && result >= B_OK; i++) {
switch (defs[i].dataType) {
case TRAN_SETTING_BOOL:
result = pmsg->AddBool(defs[i].name,
SetGetBool(defs[i].name));
break;
case TRAN_SETTING_INT32:
result = pmsg->AddInt32(defs[i].name,
SetGetInt32(defs[i].name));
break;
default:
// ASSERT here? Erase the bogus setting entry instead?
break;
}
}
fLock.Unlock();
}
}
return result;
}
// ---------------------------------------------------------------
// FindTranSetting
//
// Returns a pointer to the TranSetting with the given name
//
//
// Preconditions:
//
// Parameters: name name of the TranSetting to find
//
//
// Postconditions:
//
// Returns: NULL if the TranSetting cannot be found, or a pointer
// to the desired TranSetting if it is found
// ---------------------------------------------------------------
const TranSetting *
TranslatorSettings::FindTranSetting(const char *name)
{
for (int32 i = 0; i < fDefCount; i++) {
if (!strcmp(fDefaults[i].name, name))
return fDefaults + i;
}
return NULL;
}
// ---------------------------------------------------------------
// SetGetBool
//
// Sets the state of the bool setting identified by the given name
//
//
// Preconditions:
//
// Parameters: name identifies the setting to set or get
//
// pbool the new value for the bool, or, if null,
// it indicates that the caller wants to Get
// rather than Set
//
// Postconditions:
//
// Returns: the prior value of the setting
// ---------------------------------------------------------------
bool
TranslatorSettings::SetGetBool(const char *name, bool *pbool)
{
bool bprevValue;
fLock.Lock();
const TranSetting *def = FindTranSetting(name);
if (def) {
fSettingsMsg.FindBool(def->name, &bprevValue);
if (pbool)
fSettingsMsg.ReplaceBool(def->name, *pbool);
} else
bprevValue = false;
fLock.Unlock();
return bprevValue;
}
// ---------------------------------------------------------------
// SetGetInt32
//
// Sets the state of the int32 setting identified by the given name
//
//
// Preconditions:
//
// Parameters: name identifies the setting to set or get
//
// pint32 the new value for the setting, or, if null,
// it indicates that the caller wants to Get
// rather than Set
//
// Postconditions:
//
// Returns: the prior value of the setting
// ---------------------------------------------------------------
int32
TranslatorSettings::SetGetInt32(const char *name, int32 *pint32)
{
int32 prevValue;
fLock.Lock();
const TranSetting *def = FindTranSetting(name);
if (def) {
fSettingsMsg.FindInt32(def->name, &prevValue);
if (pint32)
fSettingsMsg.ReplaceInt32(def->name, *pint32);
} else
prevValue = 0;
fLock.Unlock();
return prevValue;
}

View File

@ -0,0 +1,91 @@
/*****************************************************************************/
// TranslatorSettings
// Written by Michael Wilber, OBOS Translation Kit Team
//
// TranslatorSettings.h
//
// This class manages (saves/loads/locks/unlocks) the settings
// for a Translator.
//
//
// Copyright (c) 2004 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef TRANSLATOR_SETTINGS_H
#define TRANSLATOR_SETTINGS_H
#include <Locker.h>
#include <Path.h>
#include <Message.h>
enum TranSettingType {
TRAN_SETTING_INT32 = 0,
TRAN_SETTING_BOOL
};
struct TranSetting {
const char *name;
TranSettingType dataType;
int32 defaultVal;
};
class TranslatorSettings {
public:
TranslatorSettings(const char *settingsFile, TranSetting *defaults,
int32 defCount);
TranslatorSettings *Acquire();
// increments the reference count, returns this
TranslatorSettings *Release();
// decrements the reference count, deletes this
// when count reaches zero, returns this when
// ref count is greater than zero, NULL when
// ref count is zero
status_t LoadSettings();
status_t LoadSettings(BMessage *pmsg);
status_t SaveSettings();
status_t GetConfigurationMessage(BMessage *pmsg);
bool SetGetBool(const char *name, bool *pbool = NULL);
int32 SetGetInt32(const char *name, int32 *pint32 = NULL);
private:
const TranSetting *FindTranSetting(const char *name);
~TranslatorSettings();
// private so that Release() must be used
// to delete the object
BLocker fLock;
int32 fRefCount;
BPath fSettingsPath;
// where the settings file will be loaded from /
// saved to
BMessage fSettingsMsg;
// the actual settings
const TranSetting *fDefaults;
int32 fDefCount;
};
#endif // #ifndef TRANSLATOR_SETTTINGS_H

View File

@ -0,0 +1,118 @@
/*****************************************************************************/
// TranslatorWindow
// Written by Michael Wilber, OBOS Translation Kit Team
//
// TranslatorWindow.cpp
//
// This BWindow based object is used to hold the Translator's BView object
// when the user runs the Translator as an application.
//
//
// Copyright (c) 2004 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#include <Screen.h>
#include <Alert.h>
#include "TranslatorWindow.h"
// ---------------------------------------------------------------
// Constructor
//
// Sets up the BWindow for holding a Translator's BView object
//
// Preconditions:
//
// Parameters: area, The bounds of the window
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
TranslatorWindow::TranslatorWindow(BRect area, const char *title)
: BWindow(area, title, B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
{
}
// ---------------------------------------------------------------
// Destructor
//
// Posts a quit message so that the application is close properly
//
// Preconditions:
//
// Parameters:
//
// Postconditions:
//
// Returns:
// ---------------------------------------------------------------
TranslatorWindow::~TranslatorWindow()
{
be_app->PostMessage(B_QUIT_REQUESTED);
}
status_t
LaunchTranslatorWindow(BTranslator *translator, const char *title, BRect rect)
{
BView *view = NULL;
if (translator->MakeConfigurationView(NULL, &view, &rect)) {
BAlert *err = new BAlert("Error",
"Unable to create the view.", "OK");
err->Go();
return B_ERROR;
}
// release the translator even though I never really used it anyway
translator->Release();
translator = NULL;
TranslatorWindow *wnd = new TranslatorWindow(rect, title);
view->ResizeTo(rect.Width(), rect.Height());
wnd->AddChild(view);
BPoint wndpt = B_ORIGIN;
{
BScreen scrn;
BRect frame = scrn.Frame();
frame.InsetBy(10, 23);
// if the point is outside of the screen frame,
// use the mouse location to find a better point
if (!frame.Contains(wndpt)) {
uint32 dummy;
view->GetMouse(&wndpt, &dummy, false);
wndpt.x -= rect.Width() / 2;
wndpt.y -= rect.Height() / 2;
// clamp location to screen
if (wndpt.x < frame.left)
wndpt.x = frame.left;
if (wndpt.y < frame.top)
wndpt.y = frame.top;
if (wndpt.x > frame.right)
wndpt.x = frame.right;
if (wndpt.y > frame.bottom)
wndpt.y = frame.bottom;
}
}
wnd->MoveTo(wndpt);
wnd->Show();
return B_OK;
}

View File

@ -0,0 +1,54 @@
/*****************************************************************************/
// TranslatorWindow
// Written by Michael Wilber, OBOS Translation Kit Team
//
// TranslatorWindow.h
//
// This BWindow based object is used to hold a Translator's BView object when
// the user runs the Translator as an application.
//
//
// Copyright (c) 2004 OpenBeOS Project
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef TRANSLATORWINDOW_H
#define TRANSLATORWINDOW_H
#include <Application.h>
#include <Window.h>
#include <View.h>
#include <Rect.h>
#include <Translator.h>
class TranslatorWindow : public BWindow {
public:
TranslatorWindow(BRect area, const char *title);
// Sets up a BWindow with bounds area
~TranslatorWindow();
// Posts a quit message so that the application closes properly
};
status_t
LaunchTranslatorWindow(BTranslator *translator, const char *title, BRect rect);
#endif // #ifndef TRANSLATORWINDOW_H