* Quit on window close.

* Added HTML export, and provision for more formats and clipboard export.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23848 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
François Revol 2008-02-03 17:57:55 +00:00
parent 3a78bc6b6c
commit dc6e60ec93
4 changed files with 126 additions and 16 deletions

View File

@ -190,21 +190,37 @@ SudokuView::SetTo(SudokuField* field)
status_t
SudokuView::SaveTo(entry_ref& ref, bool asText)
SudokuView::SaveTo(entry_ref& ref, uint32 as)
{
BFile file;
status_t status = file.SetTo(&ref, B_WRITE_ONLY | B_CREATE_FILE
| B_ERASE_FILE);
if (status < B_OK)
return status;
if (asText) {
char line[1024];
strcpy(line, "# Written by Sudoku\n\n");
file.Write(line, strlen(line));
status = SaveTo(file, as);
return status;
}
uint32 i = 0;
status_t
SudokuView::SaveTo(BDataIO &stream, uint32 as)
{
BString text;
//BFile *file;
char *line;
uint32 i = 0;
status_t status = EINVAL;
switch (as) {
case kExportAsText:
text = "# Written by Sudoku\n\n";
stream.Write(text.String(), text.Length());
line = text.LockBuffer(1024);
memset(line, 0, 1024);
for (uint32 y = 0; y < fField->Size(); y++) {
for (uint32 x = 0; x < fField->Size(); x++) {
if (x != 0 && x % fBlockSize == 0)
@ -213,15 +229,77 @@ SudokuView::SaveTo(entry_ref& ref, bool asText)
}
line[i++] = '\n';
}
text.UnlockBuffer();
file.Write(line, i);
} else {
stream.Write(text.String(), text.Length());
return B_OK;
case kExportAsHTML:
{
text = "<html>\n<head>\n<!-- Written by Sudoku -->\n"
"<style type=\"text/css\">\n"
"table.sudoku { border: 1px solid black; width=\"300px\"; height=\"300px\"; }\n"
"td.sudoku_initial { background: #f0f0f0; }\n"
"td.sudoku_filled { background: #f0f0f0; color: blue; }\n"
"td.sudoku_empty { background: #f0f0f0; }\n"
"</style>\n"
"</head>\n<body>\n\n"
"<table " /*"border=\"1\""*/ " class=\"sudoku\">";
stream.Write(text.String(), text.Length());
//XXX: make border larger on %3
text = "";
BString divider;
divider << (int)(100.0 / fField->Size()) << "%";
for (uint32 y = 0; y < fField->Size(); y++) {
text << "<tr height=\"" << divider << "\">\n";
for (uint32 x = 0; x < fField->Size(); x++) {
char buff[2];
_SetText(buff, fField->ValueAt(x, y));
if (fField->ValueAt(x, y) == 0) {
text << "<td width=\"" << divider << "\" class=\"sudoku_empty\">\n";
text << "&nbsp;";
} else if (fField->FlagsAt(x, y) & kInitialValue) {
text << "<td width=\"" << divider << "\" class=\"sudoku_initial\">\n";
text << "<font color=\"#000000\">";
text << buff;
text << "</font>";
} else {
text << "<td width=\"" << divider << "\" class=\"sudoku_filled\">\n";
text << "<font color=\"#0000ff\">";
text << buff;
text << "</font>";
}
text << "</td>\n";
}
text << "</tr>\n";
}
text << "</table>\n\n";
stream.Write(text.String(), text.Length());
text = "</body></html>\n";
stream.Write(text.String(), text.Length());
return B_OK;
}
case kExportAsBitmap:
case kExportAsPicture:
default:
return EINVAL;
}
return status;
}
status_t
SudokuView::CopyToClipboard()
{
status_t status = EINVAL;
BMessage data;
return status;
}
void
SudokuView::ClearChanged()
{

View File

@ -19,6 +19,13 @@ enum {
};
enum {
kExportAsText,
kExportAsHTML,
kExportAsBitmap,
kExportAsPicture
};
class SudokuView : public BView {
public:
SudokuView(BRect frame, const char* name, const BMessage& settings,
@ -31,7 +38,10 @@ public:
status_t SetTo(const char* data);
status_t SetTo(SudokuField* field);
status_t SaveTo(entry_ref& ref, bool asText);
status_t SaveTo(entry_ref& ref, uint32 as=kExportAsText);
status_t SaveTo(BDataIO &to, uint32 as=kExportAsText);
status_t CopyToClipboard();
void ClearChanged();
void ClearAll();

View File

@ -41,7 +41,7 @@ const uint32 kMsgStoreState = 'stst';
const uint32 kMsgRestoreState = 'rest';
const uint32 kMsgNew = 'new ';
const uint32 kMsgStartAgain = 'stag';
const uint32 kMsgExportAsText = 'extx';
const uint32 kMsgExportAs = 'expt';
class GenerateSudoku {
@ -134,9 +134,10 @@ GenerateSudoku::_GenerateThread(void* _self)
SudokuWindow::SudokuWindow()
: BWindow(BRect(100, 100, 500, 520), "Sudoku", B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS),
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE),
fGenerator(NULL),
fStoredState(NULL)
fStoredState(NULL),
fExportFormat(kExportAsText)
{
BMessage settings;
_LoadSettings(settings);
@ -201,8 +202,20 @@ SudokuWindow::SudokuWindow()
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem("Export As Text" B_UTF8_ELLIPSIS,
new BMessage(kMsgExportAsText)));
subMenu = new BMenu("Export As" B_UTF8_ELLIPSIS);
BMessage *msg;
msg = new BMessage(kMsgExportAs);
msg->AddInt32("as", kExportAsText);
subMenu->AddItem(new BMenuItem("Text", msg));
msg = new BMessage(kMsgExportAs);
msg->AddInt32("as", kExportAsHTML);
subMenu->AddItem(new BMenuItem("HTML", msg));
/*
msg = new BMessage(kMsgExportAs);
msg->AddInt32("as", kExportAsBitmap);
subMenu->AddItem(new BMenuItem("Bitmap" B_UTF8_ELLIPSIS, msg));
*/
menu->AddItem(subMenu);
menu->AddSeparatorItem();
@ -437,9 +450,17 @@ SudokuWindow::MessageReceived(BMessage* message)
break;
}
case kMsgExportAsText:
case kMsgExportAs:
{
if (message->FindInt32("as", (int32 *)&fExportFormat) < B_OK)
fExportFormat = kExportAsText;
fSavePanel->Show();
break;
}
case B_COPY:
fSudokuView->CopyToClipboard();
break;
case B_SAVE_REQUESTED:
{
@ -454,7 +475,7 @@ SudokuWindow::MessageReceived(BMessage* message)
entry_ref ref;
if (entry.GetRef(&ref) == B_OK)
fSudokuView->SaveTo(ref, true);
fSudokuView->SaveTo(ref, fExportFormat);
break;
}

View File

@ -42,6 +42,7 @@ private:
BMenuItem* fUndoItem;
BMenuItem* fRedoItem;
BMessage* fStoredState;
uint32 fExportFormat;
};
#endif // SUDOKU_WINDOW_H