Files
haiku-beta6/src/kits/interface/Picture.cpp
T

611 lines
13 KiB
C++
Raw Normal View History

2003-05-14 17:21:46 +00:00
//------------------------------------------------------------------------------
// Copyright (c) 2001-2005, Haiku
2003-05-14 17:21:46 +00:00
//
// 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.
//
// File Name: Picture.cpp
// Author: Marc Flerackers ([email protected])
// Description: BPicture records a series of drawing instructions that can
// be "replayed" later.
//------------------------------------------------------------------------------
2003-05-14 17:21:46 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2003-05-14 17:21:46 +00:00
#include <Picture.h>
#include <Message.h>
#include <ByteOrder.h>
#include <TPicture.h>
#include <Errors.h>
#include <List.h>
#include <AppServerLink.h>
#include <ServerProtocol.h>
struct _BPictureExtent_ {
void *fNewData;
int32 fNewSize;
void *fOldData;
int32 fOldSize;
BList fPictures; // In R5 this is a BArray<BPicture*> which is completely
// inline.
};
status_t do_playback(void * data, int32 size, BList& pictures,
void **callBackTable, int32 tableEntries, void *user);
2003-05-14 17:21:46 +00:00
BPicture::BPicture()
:
token(-1),
extent(NULL),
usurped(NULL)
2003-05-14 17:21:46 +00:00
{
2005-12-17 21:45:41 +00:00
init_data();
2003-05-14 17:21:46 +00:00
}
BPicture::BPicture(const BPicture &otherPicture)
:
token(-1),
extent(NULL),
usurped(NULL)
2003-05-14 17:21:46 +00:00
{
init_data();
if (otherPicture.token != -1) {
BPrivate::AppServerLink link;
link.StartMessage(AS_CLONE_PICTURE);
link.Attach<int32>(otherPicture.token);
int32 code;
if (link.FlushWithReply(code) == B_OK
&& code == SERVER_TRUE)
link.Read<int32>(&token);
2003-05-14 17:21:46 +00:00
}
if (otherPicture.extent->fNewData != NULL) {
extent->fNewSize = otherPicture.extent->fNewSize;
extent->fNewData = malloc(extent->fNewSize);
memcpy(extent->fNewData, otherPicture.extent->fNewData, extent->fNewSize);
2003-05-14 17:21:46 +00:00
for (int32 i = 0; i < otherPicture.extent->fPictures.CountItems(); i++) {
BPicture *picture = new BPicture(*(BPicture*)otherPicture.extent->fPictures.ItemAt(i));
extent->fPictures.AddItem(picture);
2003-05-14 17:21:46 +00:00
}
} else if (otherPicture.extent->fOldData != NULL) {
extent->fOldSize = otherPicture.extent->fOldSize;
2003-05-14 17:21:46 +00:00
extent->fOldData = malloc(extent->fOldSize);
memcpy(extent->fOldData, otherPicture.extent->fOldData, extent->fOldSize);
2003-05-14 17:21:46 +00:00
// In old data the sub pictures are inside the data
}
}
2003-05-14 17:21:46 +00:00
BPicture::BPicture(BMessage *archive)
:
token(-1),
extent(NULL),
usurped(NULL)
2003-05-14 17:21:46 +00:00
{
init_data();
int32 version, size;
int8 endian;
const void *data;
if (archive->FindInt32("_ver", &version) != B_OK)
version = 0;
if (archive->FindInt8("_endian", &endian) != B_OK)
endian = 0;
if (archive->FindData("_data", B_RAW_TYPE, &data, (ssize_t*)&size) != B_OK)
return;
// Load sub pictures
BMessage picMsg;
int32 i = 0;
while (archive->FindMessage("piclib", i++, &picMsg) == B_OK)
{
BPicture *pic = new BPicture(&picMsg);
extent->fPictures.AddItem(pic);
}
if (version == 0)
import_old_data(data, size);
else if (version == 1) {
2003-05-14 17:21:46 +00:00
extent->fNewSize = size;
extent->fNewData = malloc(extent->fNewSize);
memcpy(extent->fNewData, data, extent->fNewSize);
// swap_data(extent->fNewData, extent->fNewSize);
if (extent->fNewSize != 0 && extent->fNewData != 0) {
BPrivate::AppServerLink link;
link.StartMessage(AS_CREATE_PICTURE);
link.Attach<int32>(extent->fPictures.CountItems());
for (int32 i = 0; i < extent->fPictures.CountItems(); i++) {
BPicture *picture = (BPicture *)extent->fPictures.ItemAt(i);
if (picture != NULL)
link.Attach<int32>(picture->token);
2003-05-14 17:21:46 +00:00
}
link.Attach<int32>(extent->fNewSize);
link.Attach(extent->fNewData, extent->fNewSize);
int32 code;
if (link.FlushWithReply(code) == B_OK
&& code == SERVER_TRUE)
link.Read<int32>(&token);
2003-05-14 17:21:46 +00:00
}
}
// Do we just free the data now?
if (extent->fNewData) {
2003-05-14 17:21:46 +00:00
free(extent->fNewData);
extent->fNewData = NULL;
extent->fNewSize = 0;
}
if (extent->fOldData)
{
free(extent->fOldData);
extent->fOldData = NULL;
extent->fOldSize = 0;
}
// What with the sub pictures?
for (i = 0; i < extent->fPictures.CountItems(); i++)
delete (BPicture *)extent->fPictures.ItemAt(i);
2003-05-14 17:21:46 +00:00
extent->fPictures.MakeEmpty();
}
2003-05-14 17:21:46 +00:00
BPicture::~BPicture()
{
if (token != -1) {
BPrivate::AppServerLink link;
2003-05-14 17:21:46 +00:00
link.StartMessage(AS_DELETE_PICTURE);
link.Attach<int32>(token);
link.Flush();
2003-05-14 17:21:46 +00:00
}
if (extent->fNewData != NULL) {
2003-05-14 17:21:46 +00:00
free(extent->fNewData);
extent->fNewData = NULL;
extent->fNewSize = 0;
}
if (extent->fOldData != NULL) {
2003-05-14 17:21:46 +00:00
free(extent->fOldData);
extent->fOldData = NULL;
extent->fOldSize = 0;
}
for (int32 i = 0; i < extent->fPictures.CountItems(); i++)
delete (BPicture *)extent->fPictures.ItemAt(i);
2003-05-14 17:21:46 +00:00
extent->fPictures.MakeEmpty();
free(extent);
}
BArchivable *
BPicture::Instantiate(BMessage *archive)
2003-05-14 17:21:46 +00:00
{
if (validate_instantiation(archive, "BPicture"))
return new BPicture(archive);
return NULL;
2003-05-14 17:21:46 +00:00
}
status_t
BPicture::Archive(BMessage *archive, bool deep) const
2003-05-14 17:21:46 +00:00
{
if (!const_cast<BPicture*>(this)->assert_local_copy())
return B_ERROR;
status_t err = BArchivable::Archive(archive, deep);
if (err != B_OK)
return err;
err = archive->AddInt32("_ver", 1);
if (err != B_OK)
return err;
err = archive->AddInt8("_endian", B_HOST_IS_BENDIAN);
if (err != B_OK)
return err;
2003-05-14 17:21:46 +00:00
err = archive->AddData("_data", B_RAW_TYPE, extent->fNewData,
extent->fNewSize);
for (int32 i = 0; i < extent->fPictures.CountItems(); i++) {
2003-05-14 17:21:46 +00:00
BMessage picMsg;
2003-05-14 17:21:46 +00:00
((BPicture*)extent->fPictures.ItemAt(i))->Archive(&picMsg, deep);
archive->AddMessage("piclib", &picMsg);
}
return err;
}
status_t
BPicture::Perform(perform_code d, void *arg)
2003-05-14 17:21:46 +00:00
{
return BArchivable::Perform(d, arg);
}
status_t
BPicture::Play(void **callBackTable, int32 tableEntries, void *user)
2003-05-14 17:21:46 +00:00
{
if (!assert_local_copy())
return B_ERROR;
return do_playback(extent->fNewData, extent->fNewSize, extent->fPictures,
callBackTable, tableEntries, user);
}
status_t
BPicture::Flatten(BDataIO *stream)
2003-05-14 17:21:46 +00:00
{
if (!assert_local_copy())
return B_ERROR;
// TODO check the header
int32 bla1 = 2;
int32 bla2 = 0;
int32 count = 0;
stream->Write(&bla1, 4);
stream->Write(&bla2, 4);
count = extent->fPictures.CountItems();
stream->Write(&count, 4);
for (int32 i = 0; i < extent->fPictures.CountItems(); i++)
((BPicture*)extent->fPictures.ItemAt(i))->Flatten(stream);
stream->Write(&extent->fNewSize, 4);
stream->Write(extent->fNewData, extent->fNewSize);
return B_OK;
}
status_t
BPicture::Unflatten(BDataIO *stream)
2003-05-14 17:21:46 +00:00
{
// TODO check the header
int32 bla1 = 2;
int32 bla2 = 0;
int32 count = 0;
stream->Read(&bla1, 4);
stream->Read(&bla2, 4);
stream->Read(&count, 4);
for (int32 i = 0; i < count; i++) {
2003-05-14 17:21:46 +00:00
BPicture *pic = new BPicture;
pic->Unflatten(stream);
extent->fPictures.AddItem(pic);
}
stream->Read(&extent->fNewSize, 4);
extent->fNewData = malloc(extent->fNewSize);
stream->Read(extent->fNewData, extent->fNewSize);
// swap_data(extent->fNewData, extent->fNewSize);
BPrivate::AppServerLink link;
2003-05-14 17:21:46 +00:00
link.StartMessage(AS_CREATE_PICTURE);
link.Attach<int32>(extent->fPictures.CountItems());
for (int32 i = 0; i < extent->fPictures.CountItems(); i++) {
BPicture *picture = (BPicture *)extent->fPictures.ItemAt(i);
if (picture)
link.Attach<int32>(picture->token);
2003-05-14 17:21:46 +00:00
}
link.Attach<int32>(extent->fNewSize);
link.Attach(extent->fNewData, extent->fNewSize);
int32 code;
if (link.FlushWithReply(code) == B_OK
&& code == SERVER_TRUE)
link.Read<int32>(&token);
2003-05-14 17:21:46 +00:00
if (extent->fNewData) {
2003-05-14 17:21:46 +00:00
free(extent->fNewData);
extent->fNewData = NULL;
extent->fNewSize = 0;
}
return B_OK;
}
2003-05-14 17:21:46 +00:00
void BPicture::_ReservedPicture1() {}
void BPicture::_ReservedPicture2() {}
void BPicture::_ReservedPicture3() {}
BPicture &
BPicture::operator=(const BPicture &)
2003-05-14 17:21:46 +00:00
{
return *this;
}
void
BPicture::init_data()
2003-05-14 17:21:46 +00:00
{
token = -1;
usurped = NULL;
extent = new _BPictureExtent_;
extent->fNewData = NULL;
extent->fNewSize = 0;
extent->fOldData = NULL;
extent->fOldSize = 0;
}
void
BPicture::import_data(const void *data, int32 size, BPicture **subs,
int32 subCount)
2003-05-14 17:21:46 +00:00
{
if (data == NULL || size == 0)
return;
BPrivate::AppServerLink link;
link.StartMessage(AS_CREATE_PICTURE);
link.Attach<int32>(subCount);
2003-05-14 17:21:46 +00:00
for (int32 i = 0; i < subCount; i++)
link.Attach<int32>(subs[i]->token);
2003-05-14 17:21:46 +00:00
link.Attach<int32>(size);
link.Attach(data, size);
int32 code;
if (link.FlushWithReply(code) == B_OK
&& code == SERVER_TRUE)
link.Read<int32>(&token);
2003-05-14 17:21:46 +00:00
}
void
BPicture::import_old_data(const void *data, int32 size)
2003-05-14 17:21:46 +00:00
{
// TODO: do we need to support old data, what is old data?
/*if (data == NULL)
return;
if (size == 0)
return;
convert_old_to_new(data, size, &extent->fNewData, &extent->fNewSize);
BPrivate::BAppServerLink link;
link.StartMessage(AS_CREATE_PICTURE);
link.Attach<int32>(0L);
link.Attach<int32>(extent->fNewSize);
link.Attach(extent->fNewData,extent->fNewSize);
link.FlushWithReply(&code);
if(code==SERVER_TRUE)
link.Read<int32>(&token)
2003-05-14 17:21:46 +00:00
// Do we free all data now?
free(extent->fNewData);
extent->fNewData = 0;
extent->fNewSize = 0;
free(extent->fOldData);
extent->fOldData = 0;
extent->fOldSize = 0;*/
}
void
BPicture::set_token(int32 _token)
2003-05-14 17:21:46 +00:00
{
token = _token;
}
bool
BPicture::assert_local_copy()
2003-05-14 17:21:46 +00:00
{
if (extent->fNewData != NULL)
return true;
if (token == -1)
return false;
/* BPrivate::BAppServerLink link;
int32 count;
link.StartMessage(AS_DOWNLOAD_PICTURE);
link.Attach<int32>(token);
link.FlushWithReply(&code);
2003-05-14 17:21:46 +00:00
count=*((int32*)replydata.buffer);
// Read sub picture tokens
for (int32 i = 0; i < count; i++)
{
BPicture *pic = new BPicture;
link.sread(4, &pic->token);
extent->fPictures.AddItem(pic);
}
link.sread(4, &extent->fNewSize);
extent->fNewData = malloc(extent->fNewSize);
link.sread(extent->fNewSize, &extent->fNewData);*/
return true;
}
bool
BPicture::assert_old_local_copy()
2003-05-14 17:21:46 +00:00
{
if (extent->fOldData != NULL)
return true;
if (!assert_local_copy())
return false;
// convert_new_to_old(extent->fNewData, extent->fNewSize, extent->fOldData,
// extent->fOldSize);
return true;
}
bool
BPicture::assert_server_copy()
2003-05-14 17:21:46 +00:00
{
if (token != -1)
return true;
if (extent->fNewData == NULL)
return false;
/* for (int32 i = 0; i < extent->fPictures.CountItems(); i++)
extent->fPictures.ItemAt(i)->assert_server_copy();
BPrivate::BAppServerLink link;
link.StartMessage(AS_CREATE_PICTURE);
link.Attach<int32>(extent->fPictures.CountItems());
2003-05-14 17:21:46 +00:00
for (int32 i = 0; i < extent->fPictures.CountItems(); i++)
link.Attach<int32>(extent->fPictures.ItemAt(i)->token);
link.Attach<int32>(extent->fNewSize);
link.Attach(extent->fNewData,extent->fNewSize);
link.FlushWithReply(&code);
if(code==SERVER_TRUE)
link.Read<int32>(&token);
2003-05-14 17:21:46 +00:00
return token != -1;*/
return true;
}
2003-05-14 17:21:46 +00:00
BPicture::BPicture(const void *data, int32 size)
{
init_data();
import_old_data(data, size);
}
const void *
BPicture::Data() const
2003-05-14 17:21:46 +00:00
{
if (extent->fNewData == NULL) {
2003-05-14 17:21:46 +00:00
const_cast<BPicture*>(this)->assert_local_copy();
//convert_new_to_old(void *, long, void **, long *);
}
return extent->fNewData;
}
int32
BPicture::DataSize() const
2003-05-14 17:21:46 +00:00
{
if (extent->fNewData == NULL) {
2003-05-14 17:21:46 +00:00
const_cast<BPicture*>(this)->assert_local_copy();
//convert_new_to_old(void *, long, void **, long *);
}
return extent->fNewSize;
}
void
BPicture::usurp(BPicture *lameDuck)
2003-05-14 17:21:46 +00:00
{
if (token != -1) {
BPrivate::AppServerLink link;
2003-05-14 17:21:46 +00:00
link.StartMessage(AS_DELETE_PICTURE);
link.Attach<int32>(token);
link.Flush();
2003-05-14 17:21:46 +00:00
}
if (extent->fNewData != NULL) {
2003-05-14 17:21:46 +00:00
free(extent->fNewData);
extent->fNewData = NULL;
extent->fNewSize = 0;
}
if (extent->fOldData != NULL) {
2003-05-14 17:21:46 +00:00
free(extent->fOldData);
extent->fOldData = NULL;
extent->fOldSize = 0;
}
for (int32 i = 0; i < extent->fPictures.CountItems(); i++)
delete (BPicture *)extent->fPictures.ItemAt(i);
2003-05-14 17:21:46 +00:00
extent->fPictures.MakeEmpty();
free(extent);
init_data();
// Do the usurping
usurped = lameDuck;
}
BPicture *
BPicture::step_down()
2003-05-14 17:21:46 +00:00
{
BPicture *lameDuck = usurped;
usurped = NULL;
return lameDuck;
}
status_t
do_playback(void * data, int32 size, BList& pictures,
2003-05-14 17:21:46 +00:00
void **callBackTable, int32 tableEntries, void *user)
{
TPicture picture(data, size, pictures);
2003-05-14 17:21:46 +00:00
return picture.Play(callBackTable, tableEntries, user);
2003-05-14 17:21:46 +00:00
}