Changeset 25417
- Timestamp:
- 05/10/08 07:55:53 (6 days ago)
- Files:
-
- haiku/trunk/src/apps/sudoku/SudokuView.cpp (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
haiku/trunk/src/apps/sudoku/SudokuView.cpp
r25384 r25417 1 1 /* 2 * Copyright 2007 , Axel Dörfler, axeld@pinc-software.de. All rights reserved.2 * Copyright 2007-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 3 * Distributed under the terms of the MIT License. 4 4 */ … … 43 43 InitObject(&settings); 44 44 45 #if 0 45 46 BRect rect(Bounds()); 46 47 rect.top = rect.bottom - 7; 47 48 rect.left = rect.right - 7; 48 BDragger *dw = new BDragger(rect, this); 49 AddChild(dw); 49 BDragger* dragger = new BDragger(rect, this); 50 AddChild(dragger); 51 #endif 50 52 } 51 53 … … 255 257 256 258 status_t 257 SudokuView::SaveTo(entry_ref& ref, uint32 as)259 SudokuView::SaveTo(entry_ref& ref, uint32 exportAs) 258 260 { 259 261 BFile file; 260 261 262 status_t status = file.SetTo(&ref, B_WRITE_ONLY | B_CREATE_FILE 262 263 | B_ERASE_FILE); … … 264 265 return status; 265 266 266 status = SaveTo(file, as); 267 268 return status; 267 return SaveTo(file, exportAs); 269 268 } 270 269 271 270 272 271 status_t 273 SudokuView::SaveTo(BDataIO &stream, uint32 as) 274 { 275 BString text; 276 BFile *file = dynamic_cast<BFile *>(&stream); 277 char *line; 272 SudokuView::SaveTo(BDataIO& stream, uint32 exportAs) 273 { 274 BFile* file = dynamic_cast<BFile*>(&stream); 278 275 uint32 i = 0; 279 status_t status = EINVAL;280 276 BNodeInfo nodeInfo; 281 277 … … 283 279 nodeInfo.SetTo(file); 284 280 285 switch ( as) {281 switch (exportAs) { 286 282 case kExportAsText: 287 text = "# Written by Sudoku\n\n"; 283 { 284 BString text = "# Written by Sudoku\n\n"; 288 285 stream.Write(text.String(), text.Length()); 289 286 290 line = text.LockBuffer(1024);287 char* line = text.LockBuffer(1024); 291 288 memset(line, 0, 1024); 292 289 for (uint32 y = 0; y < fField->Size(); y++) { … … 304 301 nodeInfo.SetType("text/plain"); 305 302 return B_OK; 303 } 304 306 305 case kExportAsHTML: 307 306 { 308 307 bool netPositiveFriendly = false; 309 text = "<html>\n<head>\n<!-- Written by Sudoku -->\n"308 BString text = "<html>\n<head>\n<!-- Written by Sudoku -->\n" 310 309 "<style type=\"text/css\">\n" 311 "table.sudoku { background: #000000; border:0; border-collapse: collapse; cellpadding: 10px; cellspacing: 10px; width: 300px; height: 300px; }\n" 312 "td.sudoku { background: #ffffff; border-color: black; border-left: none ; border-top: none ; /*border: none;*/ text-align: center; }\n" 310 "table.sudoku { background: #000000; border:0; border-collapse: " 311 "collapse; cellpadding: 10px; cellspacing: 10px; width: " 312 "300px; height: 300px; }\n" 313 "td.sudoku { background: #ffffff; border-color: black; " 314 "border-left: none ; border-top: none ; /*border: none;*/ " 315 "text-align: center; }\n" 313 316 "td.sudoku_initial { }\n" 314 317 "td.sudoku_filled { color: blue; }\n" 315 "td.sudoku_empty { }\n" 316 // border styles: right bottom (none, solid or large) 317 #define BS_N "none" 318 #define BS_S "solid 1px black" 319 #define BS_L "solid 3px black" 320 "td.sudoku_nn { border-right: " BS_N "; border-bottom: " BS_N "; }\n" 321 "td.sudoku_sn { border-right: " BS_S "; border-bottom: " BS_N "; }\n" 322 "td.sudoku_ns { border-right: " BS_N "; border-bottom: " BS_S "; }\n" 323 "td.sudoku_ss { border-right: " BS_S "; border-bottom: " BS_S "; }\n" 324 "td.sudoku_nl { border-right: " BS_N "; border-bottom: " BS_L "; }\n" 325 "td.sudoku_sl { border-right: " BS_S "; border-bottom: " BS_L "; }\n" 326 "td.sudoku_ln { border-right: " BS_L "; border-bottom: " BS_N "; }\n" 327 "td.sudoku_ls { border-right: " BS_L "; border-bottom: " BS_S "; }\n" 328 "td.sudoku_ll { border-right: " BS_L "; border-bottom: " BS_L "; }\n" 329 "</style>\n" 318 "td.sudoku_empty { }\n"; 319 320 // border styles: right bottom (none, small or large) 321 const char* kStyles[] = {"none", "small", "large"}; 322 const char* kCssStyles[] = {"none", "solid 1px black", 323 "solid 3px black"}; 324 enum style_type { kNone = 0, kSmall, kLarge }; 325 326 for (int32 right = 0; right < 3; right++) { 327 for (int32 bottom = 0; bottom < 3; bottom++) { 328 text << "td.sudoku_"; 329 if (right != bottom) 330 text << kStyles[right] << "_" << kStyles[bottom]; 331 else 332 text << kStyles[right]; 333 334 text << " { border-right: " << kCssStyles[right] 335 << "; border-bottom: " << kCssStyles[bottom] << "; }\n"; 336 } 337 } 338 339 text << "</style>\n" 330 340 "</head>\n<body>\n\n"; 331 341 stream.Write(text.String(), text.Length()); … … 333 343 text = "<table"; 334 344 if (netPositiveFriendly) 335 text << " border=\"1\"";345 text << " border=\"1\""; 336 346 text << " class=\"sudoku\">"; 337 347 stream.Write(text.String(), text.Length()); … … 346 356 _SetText(buff, fField->ValueAt(x, y)); 347 357 348 char border_right = 's'; 349 char border_bottom = 's'; 350 if ((x+1) % fField->BlockSize() == 0) 351 border_right = 'l'; 352 if ((y+1) % fField->BlockSize() == 0) 353 border_bottom = 'l'; 358 BString style = "sudoku_"; 359 style_type right = kSmall; 360 style_type bottom = kSmall; 361 if ((x + 1) % fField->BlockSize() == 0) 362 right = kLarge; 363 if ((y + 1) % fField->BlockSize() == 0) 364 bottom = kLarge; 354 365 if (x == fField->Size() - 1) 355 border_right = 'n';366 right = kNone; 356 367 if (y == fField->Size() - 1) 357 border_bottom = 'n'; 368 bottom = kNone; 369 370 if (right != bottom) 371 style << kStyles[right] << "_" << kStyles[bottom]; 372 else 373 style << kStyles[right]; 358 374 359 375 if (fField->ValueAt(x, y) == 0) { 360 376 text << "<td width=\"" << divider << "\" "; 361 text << "class=\"sudoku sudoku_empty sudoku_" << border_right << border_bottom << "\">\n";362 text << " ";377 text << "class=\"sudoku sudoku_empty " << style; 378 text << "\">\n "; 363 379 } else if (fField->FlagsAt(x, y) & kInitialValue) { 364 380 text << "<td width=\"" << divider << "\" "; 365 text << "class=\"sudoku sudoku_initial sudoku_" << border_right << border_bottom << "\">\n"; 381 text << "class=\"sudoku sudoku_initial " << style 382 << "\">\n"; 366 383 if (netPositiveFriendly) 367 384 text << "<font color=\"#000000\">"; … … 371 388 } else { 372 389 text << "<td width=\"" << divider << "\" "; 373 text << "class=\"sudoku sudoku_filled sudoku_" << border_right << border_bottom << "\">\n"; 390 text << "class=\"sudoku sudoku_filled sudoku_" << style 391 << "\">\n"; 374 392 if (netPositiveFriendly) 375 393 text << "<font color=\"#0000ff\">"; … … 391 409 return B_OK; 392 410 } 411 393 412 case kExportAsBitmap: 394 413 { 395 BMallocIO m io;396 status = SaveTo(mio, kExportAsPicture);414 BMallocIO mallocIO; 415 status_t status = SaveTo(mallocIO, kExportAsPicture); 397 416 if (status < B_OK) 398 417 return status; 399 mio.Seek(0LL, SEEK_SET); 418 419 mallocIO.Seek(0LL, SEEK_SET); 400 420 BPicture picture; 401 status = picture.Unflatten(&m io);421 status = picture.Unflatten(&mallocIO); 402 422 if (status < B_OK) 403 423 return status; 404 BBitmap *bitmap = new BBitmap(Bounds(), B_BITMAP_ACCEPTS_VIEWS, B_RGB32); 405 BView *view = new BView(Bounds(), "bitmap", B_FOLLOW_NONE, B_WILL_DRAW); 424 425 BBitmap* bitmap = new BBitmap(Bounds(), B_BITMAP_ACCEPTS_VIEWS, 426 B_RGB32); 427 BView* view = new BView(Bounds(), "bitmap", B_FOLLOW_NONE, 428 B_WILL_DRAW); 406 429 bitmap->AddChild(view); 430 407 431 if (bitmap->Lock()) { 408 432 view->DrawPicture(&picture); 409 433 view->Sync(); 434 410 435 view->RemoveSelf(); 411 436 delete view; 437 // it should not become part of the archive 412 438 bitmap->Unlock(); 413 439 } 414 BMessage msg((uint32)B_OK); 415 status = bitmap->Archive(&msg); 416 if (status >= B_OK) { 417 status = msg.Flatten(&stream); 418 } 440 441 BMessage archive; 442 status = bitmap->Archive(&archive); 443 if (status >= B_OK) 444 status = archive.Flatten(&stream); 445 419 446 delete bitmap; 420 447 return status; 421 448 } 449 422 450 case kExportAsPicture: 423 451 { … … 425 453 BeginPicture(&picture); 426 454 Draw(Bounds()); 427 if (EndPicture()) { 455 456 status_t status = B_ERROR; 457 if (EndPicture()) 428 458 status = picture.Flatten(&stream); 429 } 459 430 460 return status; 431 461 } 462 432 463 default: 433 return EINVAL; 434 } 435 436 return status; 464 return B_BAD_VALUE; 465 } 437 466 } 438 467
