* Reindent & style fix.

* Added copy-to-clipboard.
* Added picture and bitmap clipboard formats.
Now you can just paste into either a text or graphics app. Even Gobe uses the text in word processor and bitmap in picture editor :)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23855 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
François Revol
2008-02-03 21:37:23 +00:00
parent 967d72041f
commit bb00cd45ed
3 changed files with 187 additions and 113 deletions
+83 -12
View File
@@ -16,10 +16,13 @@
#include <Application.h> #include <Application.h>
#include <Beep.h> #include <Beep.h>
#include <Bitmap.h>
#include <Clipboard.h>
#include <DataIO.h> #include <DataIO.h>
#include <File.h> #include <File.h>
#include <NodeInfo.h> #include <NodeInfo.h>
#include <Path.h> #include <Path.h>
#include <Picture.h>
#include <String.h> #include <String.h>
@@ -247,13 +250,6 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
bool netPositiveFriendly = false; bool netPositiveFriendly = false;
text = "<html>\n<head>\n<!-- Written by Sudoku -->\n" 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; cellpadding: 10px; cellspacing: 1px; width: 300px; height: 300px; }\n"
"td.sudoku { background: #ffffff; border: 0; text-align: center; }\n"
"td.sudoku_initial { }\n"
"td.sudoku_filled { color: blue; }\n"
"td.sudoku_empty { }\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: 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 { 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"
@@ -274,7 +270,6 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
"td.sudoku_ll { border-right: " BS_L "; border-bottom: " BS_L "; }\n" "td.sudoku_ll { border-right: " BS_L "; border-bottom: " BS_L "; }\n"
"</style>\n" "</style>\n"
"</head>\n<body>\n\n"; "</head>\n<body>\n\n";
/*if (file)*/
stream.Write(text.String(), text.Length()); stream.Write(text.String(), text.Length());
text = "<table"; text = "<table";
@@ -283,8 +278,6 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
text << " class=\"sudoku\">"; text << " class=\"sudoku\">";
stream.Write(text.String(), text.Length()); stream.Write(text.String(), text.Length());
//XXX: make border larger on %3
text = ""; text = "";
BString divider; BString divider;
divider << (int)(100.0 / fField->Size()) << "%"; divider << (int)(100.0 / fField->Size()) << "%";
@@ -334,14 +327,50 @@ SudokuView::SaveTo(BDataIO &stream, uint32 as)
stream.Write(text.String(), text.Length()); stream.Write(text.String(), text.Length());
text = "</body></html>\n"; text = "</body></html>\n";
/*if (file)*/
stream.Write(text.String(), text.Length()); stream.Write(text.String(), text.Length());
if (file) if (file)
nodeInfo.SetType("text/html"); nodeInfo.SetType("text/html");
return B_OK; return B_OK;
} }
case kExportAsBitmap: case kExportAsBitmap:
{
BMallocIO mio;
status = SaveTo(mio, kExportAsPicture);
if (status < B_OK)
return status;
mio.Seek(0LL, SEEK_SET);
BPicture picture;
status = picture.Unflatten(&mio);
if (status < B_OK)
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);
bitmap->AddChild(view);
if (bitmap->Lock()) {
view->DrawPicture(&picture);
view->Sync();
view->RemoveSelf();
delete view;
bitmap->Unlock();
}
BMessage msg(B_OK);
status = bitmap->Archive(&msg);
if (status >= B_OK) {
status = msg.Flatten(&stream);
}
delete bitmap;
return status;
}
case kExportAsPicture: case kExportAsPicture:
{
BPicture picture;
BeginPicture(&picture);
Draw(Bounds());
if (EndPicture()) {
status = picture.Flatten(&stream);
}
return status;
}
default: default:
return EINVAL; return EINVAL;
} }
@@ -354,7 +383,49 @@ status_t
SudokuView::CopyToClipboard() SudokuView::CopyToClipboard()
{ {
status_t status = EINVAL; status_t status = EINVAL;
BMessage data; BMessage *clip;
if (be_clipboard->Lock()) {
BMallocIO mio;
be_clipboard->Clear();
clip = be_clipboard->Data();
if (clip) {
// first as bitmap as we need to archive to the message
if (SaveTo(mio, kExportAsBitmap) >= B_OK) {
mio.Seek(0LL, SEEK_SET);
// ShowImage wants this... nasty
clip->Unflatten(&mio);
// ArtPaint uses that
clip->AddData("image/bitmap", B_MESSAGE_TYPE, mio.Buffer(), mio.BufferLength());
// Becasso uses that ?
clip->AddData("image/x-be-bitmap", B_MESSAGE_TYPE, mio.Buffer(), mio.BufferLength());
// Gobe Productive uses that...
// QuickRes as well, with a rect field.
clip->AddData("image/x-vnd.Be-bitmap", B_MESSAGE_TYPE, mio.Buffer(), mio.BufferLength());
}
mio.Seek(0LL, SEEK_SET);
mio.SetSize(0LL);
if (SaveTo(mio, kExportAsHTML) >= B_OK)
clip->AddData("text/html", B_MIME_TYPE, mio.Buffer(), mio.BufferLength());
mio.Seek(0LL, SEEK_SET);
mio.SetSize(0LL);
if (SaveTo(mio, kExportAsText) >= B_OK)
clip->AddData("text/plain", B_MIME_TYPE, mio.Buffer(), mio.BufferLength());
mio.Seek(0LL, SEEK_SET);
mio.SetSize(0LL);
// flattened BPicture, anyone handles that ?
if (SaveTo(mio, kExportAsPicture) >= B_OK) {
clip->AddData("text/x-vnd.Be-picture", B_MIME_TYPE, mio.Buffer(), mio.BufferLength());
}
mio.SetSize(0LL);
be_clipboard->Commit();
}
be_clipboard->Unlock();
}
return status; return status;
} }
+2 -2
View File
@@ -39,8 +39,8 @@ public:
status_t SetTo(const char* data); status_t SetTo(const char* data);
status_t SetTo(SudokuField* field); status_t SetTo(SudokuField* field);
status_t SaveTo(entry_ref& ref, uint32 as=kExportAsText); status_t SaveTo(entry_ref& ref, uint32 as = kExportAsText);
status_t SaveTo(BDataIO &to, uint32 as=kExportAsText); status_t SaveTo(BDataIO &to, uint32 as = kExportAsText);
status_t CopyToClipboard(); status_t CopyToClipboard();
+3
View File
@@ -217,6 +217,9 @@ SudokuWindow::SudokuWindow()
*/ */
menu->AddItem(subMenu); menu->AddItem(subMenu);
menu->AddItem(item = new BMenuItem("Copy",
new BMessage(B_COPY), 'C'));
menu->AddSeparatorItem(); menu->AddSeparatorItem();
menu->AddItem(item = new BMenuItem("About Sudoku" B_UTF8_ELLIPSIS, menu->AddItem(item = new BMenuItem("About Sudoku" B_UTF8_ELLIPSIS,