Changeset 25417

Show
Ignore:
Timestamp:
05/10/08 07:55:53 (6 days ago)
Author:
axeld
Message:
* Turned off replicant handle for the time being; it's not really usable as
  a replicant yet.
* Improved CSS producing code.
* Cleanup.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • haiku/trunk/src/apps/sudoku/SudokuView.cpp

    r25384 r25417  
    11/* 
    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. 
    33 * Distributed under the terms of the MIT License. 
    44 */ 
     
    4343        InitObject(&settings); 
    4444 
     45#if 0 
    4546        BRect rect(Bounds()); 
    4647        rect.top = rect.bottom - 7; 
    4748        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 
    5052} 
    5153 
     
    255257 
    256258status_t 
    257 SudokuView::SaveTo(entry_ref& ref, uint32 as) 
     259SudokuView::SaveTo(entry_ref& ref, uint32 exportAs) 
    258260{ 
    259261        BFile file; 
    260  
    261262        status_t status = file.SetTo(&ref, B_WRITE_ONLY | B_CREATE_FILE 
    262263                | B_ERASE_FILE); 
     
    264265                return status; 
    265266 
    266         status = SaveTo(file, as); 
    267          
    268         return status; 
     267        return SaveTo(file, exportAs); 
    269268} 
    270269 
    271270 
    272271status_t 
    273 SudokuView::SaveTo(BDataIO &stream, uint32 as) 
    274 
    275         BString text; 
    276         BFile *file = dynamic_cast<BFile *>(&stream); 
    277         char *line; 
     272SudokuView::SaveTo(BDataIO& stream, uint32 exportAs) 
     273
     274        BFile* file = dynamic_cast<BFile*>(&stream); 
    278275        uint32 i = 0; 
    279         status_t status = EINVAL; 
    280276        BNodeInfo nodeInfo; 
    281277 
     
    283279                nodeInfo.SetTo(file); 
    284280 
    285         switch (as) { 
     281        switch (exportAs) { 
    286282                case kExportAsText: 
    287                         text = "# Written by Sudoku\n\n"; 
     283                { 
     284                        BString text = "# Written by Sudoku\n\n"; 
    288285                        stream.Write(text.String(), text.Length()); 
    289286 
    290                         line = text.LockBuffer(1024); 
     287                        char* line = text.LockBuffer(1024); 
    291288                        memset(line, 0, 1024); 
    292289                        for (uint32 y = 0; y < fField->Size(); y++) { 
     
    304301                                nodeInfo.SetType("text/plain"); 
    305302                        return B_OK; 
     303                } 
     304 
    306305                case kExportAsHTML: 
    307306                { 
    308307                        bool netPositiveFriendly = false; 
    309                         text = "<html>\n<head>\n<!-- Written by Sudoku -->\n" 
     308                        BString text = "<html>\n<head>\n<!-- Written by Sudoku -->\n" 
    310309                                "<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" 
    313316                                "td.sudoku_initial {  }\n" 
    314317                                "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" 
    330340                                "</head>\n<body>\n\n"; 
    331341                        stream.Write(text.String(), text.Length()); 
     
    333343                        text = "<table"; 
    334344                        if (netPositiveFriendly) 
    335                         text << " border=\"1\""; 
     345                               text << " border=\"1\""; 
    336346                        text << " class=\"sudoku\">"; 
    337347                        stream.Write(text.String(), text.Length()); 
     
    346356                                        _SetText(buff, fField->ValueAt(x, y)); 
    347357 
    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; 
    354365                                        if (x == fField->Size() - 1) 
    355                                                 border_right = 'n'
     366                                                right = kNone
    356367                                        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]; 
    358374 
    359375                                        if (fField->ValueAt(x, y) == 0) { 
    360376                                                text << "<td width=\"" << divider << "\" "; 
    361                                                 text << "class=\"sudoku sudoku_empty sudoku_" << border_right << border_bottom << "\">\n"
    362                                                 text << "&nbsp;"; 
     377                                                text << "class=\"sudoku sudoku_empty " << style
     378                                                text << "\">\n&nbsp;"; 
    363379                                        } else if (fField->FlagsAt(x, y) & kInitialValue) { 
    364380                                                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"; 
    366383                                                if (netPositiveFriendly) 
    367384                                                        text << "<font color=\"#000000\">"; 
     
    371388                                        } else { 
    372389                                                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"; 
    374392                                                if (netPositiveFriendly) 
    375393                                                        text << "<font color=\"#0000ff\">"; 
     
    391409                        return B_OK; 
    392410                } 
     411 
    393412                case kExportAsBitmap: 
    394413                { 
    395                         BMallocIO mio
    396                         status = SaveTo(mio, kExportAsPicture); 
     414                        BMallocIO mallocIO
     415                        status_t status = SaveTo(mallocIO, kExportAsPicture); 
    397416                        if (status < B_OK) 
    398417                                return status; 
    399                         mio.Seek(0LL, SEEK_SET); 
     418 
     419                        mallocIO.Seek(0LL, SEEK_SET); 
    400420                        BPicture picture; 
    401                         status = picture.Unflatten(&mio); 
     421                        status = picture.Unflatten(&mallocIO); 
    402422                        if (status < B_OK) 
    403423                                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); 
    406429                        bitmap->AddChild(view); 
     430 
    407431                        if (bitmap->Lock()) { 
    408432                                view->DrawPicture(&picture); 
    409433                                view->Sync(); 
     434 
    410435                                view->RemoveSelf(); 
    411436                                delete view; 
     437                                        // it should not become part of the archive 
    412438                                bitmap->Unlock(); 
    413439                        } 
    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 
    419446                        delete bitmap; 
    420447                        return status; 
    421448                } 
     449 
    422450                case kExportAsPicture: 
    423451                { 
     
    425453                        BeginPicture(&picture); 
    426454                        Draw(Bounds()); 
    427                         if (EndPicture()) { 
     455 
     456                        status_t status = B_ERROR; 
     457                        if (EndPicture()) 
    428458                                status = picture.Flatten(&stream); 
    429                         } 
     459 
    430460                        return status; 
    431461                } 
     462 
    432463                default: 
    433                         return EINVAL; 
    434         } 
    435          
    436         return status; 
     464                        return B_BAD_VALUE; 
     465        } 
    437466} 
    438467