* Turned off replicant handle for the time being; it's not really usable as

a replicant yet.
* Improved CSS producing code.
* Cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25417 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-05-10 12:55:53 +00:00
parent ba64d8928a
commit 14cc96293c
+92 -63
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2007, Axel Dörfler, [email protected]. All rights reserved. * Copyright 2007-2008, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
@@ -42,11 +42,13 @@ SudokuView::SudokuView(BRect frame, const char* name,
{ {
InitObject(&settings); InitObject(&settings);
#if 0
BRect rect(Bounds()); BRect rect(Bounds());
rect.top = rect.bottom - 7; rect.top = rect.bottom - 7;
rect.left = rect.right - 7; rect.left = rect.right - 7;
BDragger *dw = new BDragger(rect, this); BDragger* dragger = new BDragger(rect, this);
AddChild(dw); AddChild(dragger);
#endif
} }
@@ -254,40 +256,35 @@ SudokuView::SetTo(SudokuField* field)
status_t status_t
SudokuView::SaveTo(entry_ref& ref, uint32 as) SudokuView::SaveTo(entry_ref& ref, uint32 exportAs)
{ {
BFile file; BFile file;
status_t status = file.SetTo(&ref, B_WRITE_ONLY | B_CREATE_FILE status_t status = file.SetTo(&ref, B_WRITE_ONLY | B_CREATE_FILE
| B_ERASE_FILE); | B_ERASE_FILE);
if (status < B_OK) if (status < B_OK)
return status; return status;
status = SaveTo(file, as); return SaveTo(file, exportAs);
return status;
} }
status_t status_t
SudokuView::SaveTo(BDataIO &stream, uint32 as) SudokuView::SaveTo(BDataIO& stream, uint32 exportAs)
{ {
BString text; BFile* file = dynamic_cast<BFile*>(&stream);
BFile *file = dynamic_cast<BFile *>(&stream);
char *line;
uint32 i = 0; uint32 i = 0;
status_t status = EINVAL;
BNodeInfo nodeInfo; BNodeInfo nodeInfo;
if (file) if (file)
nodeInfo.SetTo(file); nodeInfo.SetTo(file);
switch (as) { switch (exportAs) {
case kExportAsText: case kExportAsText:
text = "# Written by Sudoku\n\n"; {
BString text = "# Written by Sudoku\n\n";
stream.Write(text.String(), text.Length()); stream.Write(text.String(), text.Length());
line = text.LockBuffer(1024); char* line = text.LockBuffer(1024);
memset(line, 0, 1024); memset(line, 0, 1024);
for (uint32 y = 0; y < fField->Size(); y++) { for (uint32 y = 0; y < fField->Size(); y++) {
for (uint32 x = 0; x < fField->Size(); x++) { for (uint32 x = 0; x < fField->Size(); x++) {
@@ -303,36 +300,49 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
if (file) if (file)
nodeInfo.SetType("text/plain"); nodeInfo.SetType("text/plain");
return B_OK; return B_OK;
}
case kExportAsHTML: case kExportAsHTML:
{ {
bool netPositiveFriendly = false; bool netPositiveFriendly = false;
text = "<html>\n<head>\n<!-- Written by Sudoku -->\n" BString text = "<html>\n<head>\n<!-- Written by Sudoku -->\n"
"<style type=\"text/css\">\n" "<style type=\"text/css\">\n"
"table.sudoku { background: #000000; border:0; border-collapse: collapse; cellpadding: 10px; cellspacing: 10px; width: 300px; height: 300px; }\n" "table.sudoku { background: #000000; border:0; border-collapse: "
"td.sudoku { background: #ffffff; border-color: black; border-left: none ; border-top: none ; /*border: none;*/ text-align: center; }\n" "collapse; cellpadding: 10px; cellspacing: 10px; width: "
"300px; height: 300px; }\n"
"td.sudoku { background: #ffffff; border-color: black; "
"border-left: none ; border-top: none ; /*border: none;*/ "
"text-align: center; }\n"
"td.sudoku_initial { }\n" "td.sudoku_initial { }\n"
"td.sudoku_filled { color: blue; }\n" "td.sudoku_filled { color: blue; }\n"
"td.sudoku_empty { }\n" "td.sudoku_empty { }\n";
// border styles: right bottom (none, solid or large)
#define BS_N "none" // border styles: right bottom (none, small or large)
#define BS_S "solid 1px black" const char* kStyles[] = {"none", "small", "large"};
#define BS_L "solid 3px black" const char* kCssStyles[] = {"none", "solid 1px black",
"td.sudoku_nn { border-right: " BS_N "; border-bottom: " BS_N "; }\n" "solid 3px black"};
"td.sudoku_sn { border-right: " BS_S "; border-bottom: " BS_N "; }\n" enum style_type { kNone = 0, kSmall, kLarge };
"td.sudoku_ns { border-right: " BS_N "; border-bottom: " BS_S "; }\n"
"td.sudoku_ss { border-right: " BS_S "; border-bottom: " BS_S "; }\n" for (int32 right = 0; right < 3; right++) {
"td.sudoku_nl { border-right: " BS_N "; border-bottom: " BS_L "; }\n" for (int32 bottom = 0; bottom < 3; bottom++) {
"td.sudoku_sl { border-right: " BS_S "; border-bottom: " BS_L "; }\n" text << "td.sudoku_";
"td.sudoku_ln { border-right: " BS_L "; border-bottom: " BS_N "; }\n" if (right != bottom)
"td.sudoku_ls { border-right: " BS_L "; border-bottom: " BS_S "; }\n" text << kStyles[right] << "_" << kStyles[bottom];
"td.sudoku_ll { border-right: " BS_L "; border-bottom: " BS_L "; }\n" else
"</style>\n" text << kStyles[right];
text << " { border-right: " << kCssStyles[right]
<< "; border-bottom: " << kCssStyles[bottom] << "; }\n";
}
}
text << "</style>\n"
"</head>\n<body>\n\n"; "</head>\n<body>\n\n";
stream.Write(text.String(), text.Length()); stream.Write(text.String(), text.Length());
text = "<table"; text = "<table";
if (netPositiveFriendly) if (netPositiveFriendly)
text << " border=\"1\""; text << " border=\"1\"";
text << " class=\"sudoku\">"; text << " class=\"sudoku\">";
stream.Write(text.String(), text.Length()); stream.Write(text.String(), text.Length());
@@ -345,24 +355,31 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
char buff[2]; char buff[2];
_SetText(buff, fField->ValueAt(x, y)); _SetText(buff, fField->ValueAt(x, y));
char border_right = 's'; BString style = "sudoku_";
char border_bottom = 's'; style_type right = kSmall;
if ((x+1) % fField->BlockSize() == 0) style_type bottom = kSmall;
border_right = 'l'; if ((x + 1) % fField->BlockSize() == 0)
if ((y+1) % fField->BlockSize() == 0) right = kLarge;
border_bottom = 'l'; if ((y + 1) % fField->BlockSize() == 0)
bottom = kLarge;
if (x == fField->Size() - 1) if (x == fField->Size() - 1)
border_right = 'n'; right = kNone;
if (y == fField->Size() - 1) if (y == fField->Size() - 1)
border_bottom = 'n'; bottom = kNone;
if (right != bottom)
style << kStyles[right] << "_" << kStyles[bottom];
else
style << kStyles[right];
if (fField->ValueAt(x, y) == 0) { if (fField->ValueAt(x, y) == 0) {
text << "<td width=\"" << divider << "\" "; text << "<td width=\"" << divider << "\" ";
text << "class=\"sudoku sudoku_empty sudoku_" << border_right << border_bottom << "\">\n"; text << "class=\"sudoku sudoku_empty " << style;
text << "&nbsp;"; text << "\">\n&nbsp;";
} else if (fField->FlagsAt(x, y) & kInitialValue) { } else if (fField->FlagsAt(x, y) & kInitialValue) {
text << "<td width=\"" << divider << "\" "; text << "<td width=\"" << divider << "\" ";
text << "class=\"sudoku sudoku_initial sudoku_" << border_right << border_bottom << "\">\n"; text << "class=\"sudoku sudoku_initial " << style
<< "\">\n";
if (netPositiveFriendly) if (netPositiveFriendly)
text << "<font color=\"#000000\">"; text << "<font color=\"#000000\">";
text << buff; text << buff;
@@ -370,7 +387,8 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
text << "</font>"; text << "</font>";
} else { } else {
text << "<td width=\"" << divider << "\" "; text << "<td width=\"" << divider << "\" ";
text << "class=\"sudoku sudoku_filled sudoku_" << border_right << border_bottom << "\">\n"; text << "class=\"sudoku sudoku_filled sudoku_" << style
<< "\">\n";
if (netPositiveFriendly) if (netPositiveFriendly)
text << "<font color=\"#0000ff\">"; text << "<font color=\"#0000ff\">";
text << buff; text << buff;
@@ -390,50 +408,61 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
nodeInfo.SetType("text/html"); nodeInfo.SetType("text/html");
return B_OK; return B_OK;
} }
case kExportAsBitmap: case kExportAsBitmap:
{ {
BMallocIO mio; BMallocIO mallocIO;
status = SaveTo(mio, kExportAsPicture); status_t status = SaveTo(mallocIO, kExportAsPicture);
if (status < B_OK) if (status < B_OK)
return status; return status;
mio.Seek(0LL, SEEK_SET);
mallocIO.Seek(0LL, SEEK_SET);
BPicture picture; BPicture picture;
status = picture.Unflatten(&mio); status = picture.Unflatten(&mallocIO);
if (status < B_OK) if (status < B_OK)
return status; return status;
BBitmap *bitmap = new BBitmap(Bounds(), B_BITMAP_ACCEPTS_VIEWS, B_RGB32);
BView *view = new BView(Bounds(), "bitmap", B_FOLLOW_NONE, B_WILL_DRAW); BBitmap* bitmap = new BBitmap(Bounds(), B_BITMAP_ACCEPTS_VIEWS,
B_RGB32);
BView* view = new BView(Bounds(), "bitmap", B_FOLLOW_NONE,
B_WILL_DRAW);
bitmap->AddChild(view); bitmap->AddChild(view);
if (bitmap->Lock()) { if (bitmap->Lock()) {
view->DrawPicture(&picture); view->DrawPicture(&picture);
view->Sync(); view->Sync();
view->RemoveSelf(); view->RemoveSelf();
delete view; delete view;
// it should not become part of the archive
bitmap->Unlock(); bitmap->Unlock();
} }
BMessage msg((uint32)B_OK);
status = bitmap->Archive(&msg); BMessage archive;
if (status >= B_OK) { status = bitmap->Archive(&archive);
status = msg.Flatten(&stream); if (status >= B_OK)
} status = archive.Flatten(&stream);
delete bitmap; delete bitmap;
return status; return status;
} }
case kExportAsPicture: case kExportAsPicture:
{ {
BPicture picture; BPicture picture;
BeginPicture(&picture); BeginPicture(&picture);
Draw(Bounds()); Draw(Bounds());
if (EndPicture()) {
status_t status = B_ERROR;
if (EndPicture())
status = picture.Flatten(&stream); status = picture.Flatten(&stream);
}
return status; return status;
} }
default: default:
return EINVAL; return B_BAD_VALUE;
} }
return status;
} }