* 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:
@@ -190,21 +190,37 @@ SudokuView::SetTo(SudokuField* field)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
SudokuView::SaveTo(entry_ref& ref, bool asText)
|
SudokuView::SaveTo(entry_ref& ref, uint32 as)
|
||||||
{
|
{
|
||||||
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;
|
||||||
|
|
||||||
if (asText) {
|
status = SaveTo(file, as);
|
||||||
char line[1024];
|
|
||||||
strcpy(line, "# Written by Sudoku\n\n");
|
return status;
|
||||||
file.Write(line, strlen(line));
|
}
|
||||||
|
|
||||||
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 y = 0; y < fField->Size(); y++) {
|
||||||
for (uint32 x = 0; x < fField->Size(); x++) {
|
for (uint32 x = 0; x < fField->Size(); x++) {
|
||||||
if (x != 0 && x % fBlockSize == 0)
|
if (x != 0 && x % fBlockSize == 0)
|
||||||
@@ -213,15 +229,77 @@ SudokuView::SaveTo(entry_ref& ref, bool asText)
|
|||||||
}
|
}
|
||||||
line[i++] = '\n';
|
line[i++] = '\n';
|
||||||
}
|
}
|
||||||
|
text.UnlockBuffer();
|
||||||
|
|
||||||
file.Write(line, i);
|
stream.Write(text.String(), text.Length());
|
||||||
} else {
|
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 << " ";
|
||||||
|
} 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;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
SudokuView::CopyToClipboard()
|
||||||
|
{
|
||||||
|
status_t status = EINVAL;
|
||||||
|
BMessage data;
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SudokuView::ClearChanged()
|
SudokuView::ClearChanged()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,6 +19,13 @@ enum {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
enum {
|
||||||
|
kExportAsText,
|
||||||
|
kExportAsHTML,
|
||||||
|
kExportAsBitmap,
|
||||||
|
kExportAsPicture
|
||||||
|
};
|
||||||
|
|
||||||
class SudokuView : public BView {
|
class SudokuView : public BView {
|
||||||
public:
|
public:
|
||||||
SudokuView(BRect frame, const char* name, const BMessage& settings,
|
SudokuView(BRect frame, const char* name, const BMessage& settings,
|
||||||
@@ -31,7 +38,10 @@ 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, 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 ClearChanged();
|
||||||
void ClearAll();
|
void ClearAll();
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ const uint32 kMsgStoreState = 'stst';
|
|||||||
const uint32 kMsgRestoreState = 'rest';
|
const uint32 kMsgRestoreState = 'rest';
|
||||||
const uint32 kMsgNew = 'new ';
|
const uint32 kMsgNew = 'new ';
|
||||||
const uint32 kMsgStartAgain = 'stag';
|
const uint32 kMsgStartAgain = 'stag';
|
||||||
const uint32 kMsgExportAsText = 'extx';
|
const uint32 kMsgExportAs = 'expt';
|
||||||
|
|
||||||
|
|
||||||
class GenerateSudoku {
|
class GenerateSudoku {
|
||||||
@@ -134,9 +134,10 @@ GenerateSudoku::_GenerateThread(void* _self)
|
|||||||
|
|
||||||
SudokuWindow::SudokuWindow()
|
SudokuWindow::SudokuWindow()
|
||||||
: BWindow(BRect(100, 100, 500, 520), "Sudoku", B_TITLED_WINDOW,
|
: BWindow(BRect(100, 100, 500, 520), "Sudoku", B_TITLED_WINDOW,
|
||||||
B_ASYNCHRONOUS_CONTROLS),
|
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE),
|
||||||
fGenerator(NULL),
|
fGenerator(NULL),
|
||||||
fStoredState(NULL)
|
fStoredState(NULL),
|
||||||
|
fExportFormat(kExportAsText)
|
||||||
{
|
{
|
||||||
BMessage settings;
|
BMessage settings;
|
||||||
_LoadSettings(settings);
|
_LoadSettings(settings);
|
||||||
@@ -201,8 +202,20 @@ SudokuWindow::SudokuWindow()
|
|||||||
|
|
||||||
menu->AddSeparatorItem();
|
menu->AddSeparatorItem();
|
||||||
|
|
||||||
menu->AddItem(new BMenuItem("Export As Text" B_UTF8_ELLIPSIS,
|
subMenu = new BMenu("Export As" B_UTF8_ELLIPSIS);
|
||||||
new BMessage(kMsgExportAsText)));
|
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();
|
menu->AddSeparatorItem();
|
||||||
|
|
||||||
@@ -437,9 +450,17 @@ SudokuWindow::MessageReceived(BMessage* message)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case kMsgExportAsText:
|
case kMsgExportAs:
|
||||||
|
{
|
||||||
|
if (message->FindInt32("as", (int32 *)&fExportFormat) < B_OK)
|
||||||
|
fExportFormat = kExportAsText;
|
||||||
fSavePanel->Show();
|
fSavePanel->Show();
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case B_COPY:
|
||||||
|
fSudokuView->CopyToClipboard();
|
||||||
|
break;
|
||||||
|
|
||||||
case B_SAVE_REQUESTED:
|
case B_SAVE_REQUESTED:
|
||||||
{
|
{
|
||||||
@@ -454,7 +475,7 @@ SudokuWindow::MessageReceived(BMessage* message)
|
|||||||
|
|
||||||
entry_ref ref;
|
entry_ref ref;
|
||||||
if (entry.GetRef(&ref) == B_OK)
|
if (entry.GetRef(&ref) == B_OK)
|
||||||
fSudokuView->SaveTo(ref, true);
|
fSudokuView->SaveTo(ref, fExportFormat);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ private:
|
|||||||
BMenuItem* fUndoItem;
|
BMenuItem* fUndoItem;
|
||||||
BMenuItem* fRedoItem;
|
BMenuItem* fRedoItem;
|
||||||
BMessage* fStoredState;
|
BMessage* fStoredState;
|
||||||
|
uint32 fExportFormat;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SUDOKU_WINDOW_H
|
#endif // SUDOKU_WINDOW_H
|
||||||
|
|||||||
Reference in New Issue
Block a user