Repository navigation
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
713 lines (619 loc) · 18.2 KB
/
Copy pathMainWindow.cpp
File metadata and controls
713 lines (619 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
* Copyright 2009-2026 Scott McCreary
* Copyright 2018 Humdinger
* Copyright 2017-2018 Owen
* Copyright 2014-2016 Puck Meerburg
* Copyright 2013 Luke (noryb009)
* Based on BeVexed by DarkWyrm Copyright 2007-2009
*
* Distributed under terms of the MIT License.
*
*/
#include "MainWindow.h"
#include <Alert.h>
#include <Box.h>
#include <Bitmap.h>
#include <Directory.h>
#include <Entry.h>
#include <Menu.h>
#include <MenuItem.h>
#include <Path.h>
#include <PathFinder.h>
#include <Roster.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <StringList.h>
#include <TranslationUtils.h>
#include <View.h>
#include "AboutWindow.h"
#include "BestTimes.h"
#include "HexGrid.h"
#include "HexTile.h"
#include "HexTileView.h"
#include "ImageAlert.h"
#include "Preferences.h"
enum
{
M_NEW_GAME='nwgm',
M_SIZE1_TILES,
M_SIZE2_TILES,
M_SIZE3_TILES,
M_SIZE4_TILES,
M_SIZE5_TILES,
M_SET_BACKGROUND,
M_SET_TILE_COUNT_3,
M_SET_TILE_COUNT_4,
M_SET_TILE_COUNT_5,
M_SET_TILE_COUNT_6,
M_SET_TILE_COUNT_7,
M_SET_TILE_COUNT_8,
M_SET_BINARY,
M_SET_QUARTERNARY,
M_SET_HEXIMAL,
M_SET_OCTAL,
M_SET_DECIMAL,
M_SET_HEXADECIMAL,
M_HOW_TO_PLAY,
M_BEST_TIMES
};
static const float cos30 = 0.866;
static const uint8 tileSizes[] = {
TILESIZE_1, TILESIZE_2, TILESIZE_3, TILESIZE_4, TILESIZE_5
};
MainWindow::MainWindow(void)
: BWindow(BRect(100,100,500,400),"HexVexed",B_TITLED_WINDOW_LOOK,
B_NORMAL_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS | B_NOT_RESIZABLE),
fGrid(NULL),
fWorkGrid(NULL),
fGameOver(false)
{
BPath path;
BPathFinder pathFinder;
BStringList paths;
status_t error = pathFinder.FindPaths(B_FIND_PATH_DATA_DIRECTORY,
"hexvexed/backgrounds", B_FIND_PATH_EXISTING_ONLY, paths);
for (int i = 0; i < paths.CountStrings(); ++i) {
// grab 1st path, to have non-packaged override the bundled backgrounds
if (error == B_OK && path.SetTo(paths.StringAt(0)) == B_OK)
fBackPath = path.Path();
}
fBackPath << "/";
Preferences::Init();
Preferences::LockPreferences();
Preferences::Load();
BestTimes::Init();
if(Preferences::Message().FindInt8("gridsize",(int8*)&fGridSize)!=B_OK
|| Preferences::Message().FindInt8("tilesize", (int8 *)&fTileSize)!=B_OK
|| Preferences::Message().FindInt8("numberbase", (int8 *)&fNumberBase)!=B_OK)
{
printf("Loading defaults\n");
// Preferences not working, so load these as the defaults
fGridSize = 3;
Preferences::Message().AddInt8("gridsize",fGridSize);
fTileSize = TILESIZE_3;
Preferences::Message().AddInt8("tilesize",fTileSize);
fNumberBase = NUMBERBASE_HEXADECIMAL;
Preferences::Message().AddInt8("numberbase", fNumberBase);
}
fBack = new BView(Bounds(),"background",B_FOLLOW_ALL,B_WILL_DRAW | B_DRAW_ON_CHILDREN);
AddChild(fBack);
fMenuBar = new BMenuBar(BRect(0,0,Bounds().Width(),20),"menubar");
fBack->AddChild(fMenuBar);
BMenu *menu = new BMenu("Game");
fMenuBar->AddItem(menu);
menu->AddItem(new BMenuItem("New",new BMessage(M_NEW_GAME),'N'));
BMenu *submenu = new BMenu("Difficulty");
submenu->AddItem(new BMenuItem("Beginner",new BMessage(M_SET_TILE_COUNT_3)));
submenu->AddItem(new BMenuItem("Easy",new BMessage(M_SET_TILE_COUNT_4)));
submenu->AddItem(new BMenuItem("Medium",new BMessage(M_SET_TILE_COUNT_5)));
submenu->AddItem(new BMenuItem("Hard",new BMessage(M_SET_TILE_COUNT_6)));
submenu->AddItem(new BMenuItem("Master",new BMessage(M_SET_TILE_COUNT_7)));
submenu->SetRadioMode(true);
menu->AddSeparatorItem();
menu->AddItem(submenu);
switch (fGridSize)
{
case 3:
submenu->ItemAt(0)->SetMarked(true);
break;
case 4:
submenu->ItemAt(1)->SetMarked(true);
break;
case 5:
submenu->ItemAt(2)->SetMarked(true);
break;
case 6:
submenu->ItemAt(3)->SetMarked(true);
break;
case 7:
submenu->ItemAt(4)->SetMarked(true);
break;
}
submenu = new BMenu("Tile size");
submenu->AddItem(new BMenuItem("Small",new BMessage(M_SIZE1_TILES)));
submenu->AddItem(new BMenuItem("Medium",new BMessage(M_SIZE2_TILES)));
submenu->AddItem(new BMenuItem("Large",new BMessage(M_SIZE3_TILES)));
submenu->AddItem(new BMenuItem("Extra large",new BMessage(M_SIZE4_TILES)));
submenu->AddItem(new BMenuItem("Jumbo",new BMessage(M_SIZE5_TILES)));
submenu->SetRadioMode(true);
menu->AddItem(submenu);
for (int32 index = 0; index < 5; index++)
if (fTileSize == tileSizes[index]) {
submenu->ItemAt(index)->SetMarked(true);
break;
}
submenu = new BMenu("Number base");
submenu->AddItem(new BMenuItem("Binary",new BMessage(M_SET_BINARY)));
submenu->AddItem(new BMenuItem("Quarternary",new BMessage(M_SET_QUARTERNARY)));
submenu->AddItem(new BMenuItem("Heximal",new BMessage(M_SET_HEXIMAL)));
submenu->AddItem(new BMenuItem("Octal",new BMessage(M_SET_OCTAL)));
submenu->AddItem(new BMenuItem("Decimal",new BMessage(M_SET_DECIMAL)));
submenu->AddItem(new BMenuItem("Hexadecimal",new BMessage(M_SET_HEXADECIMAL)));
submenu->SetRadioMode(true);
menu->AddItem(submenu);
switch(fNumberBase)
{
case NUMBERBASE_BINARY:
submenu->ItemAt(0)->SetMarked(true);
break;
case NUMBERBASE_QUARTERNARY:
submenu->ItemAt(1)->SetMarked(true);
break;
case NUMBERBASE_HEXIMAL:
submenu->ItemAt(2)->SetMarked(true);
break;
case NUMBERBASE_OCTAL:
submenu->ItemAt(3)->SetMarked(true);
break;
case NUMBERBASE_DECIMAL:
submenu->ItemAt(4)->SetMarked(true);
break;
case NUMBERBASE_HEXADECIMAL:
submenu->ItemAt(5)->SetMarked(true);
break;
}
fBackMenu = new BMenu("Background");
menu->AddItem(fBackMenu);
ScanBackgrounds();
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem("How to play" B_UTF8_ELLIPSIS,new BMessage(M_HOW_TO_PLAY)));
menu->AddItem(new BMenuItem("Best times",new BMessage(M_BEST_TIMES)));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem("About HexVexed",new BMessage(B_ABOUT_REQUESTED)));
BBox *timeBox = new BBox("box");
timeBox->ResizeTo(70, 34);
timeBox->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fBack->AddChild(timeBox);
fTimer = new TimerView();
fTimer->ResizeTo(70, 30);
BFont font = be_bold_font;
font.SetSize(20);
fTimer->SetFontAndColor(&font);
fTimer->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fTimer->SetAlignment(B_ALIGN_CENTER);
timeBox->AddChild(fTimer);
SetPulseRate(500000);
GenerateGrid(fGridSize, true);
BPoint corner;
if(Preferences::Message().FindPoint("corner",&corner)!=B_OK)
{
corner = Frame().LeftTop();
Preferences::Message().AddPoint("corner",Frame().LeftTop());
}
BRect r(Frame());
r.OffsetTo(corner.x,corner.y);
ConstrainWindowFrameToScreen(&r);
MoveTo(r.left,r.top);
if(Preferences::Message().FindString("background",&fBackName) == B_OK)
SetBackground(fBackName.String());
else
SetBackground("Grass.png"); //default
Preferences::UnlockPreferences();
}
bool MainWindow::QuitRequested(void)
{
Preferences::LockPreferences();
Preferences::Message().ReplacePoint("corner",Frame().LeftTop());
Preferences::Save();
Preferences::UnlockPreferences();
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
}
void MainWindow::MessageReceived(BMessage *msg)
{
Preferences::LockPreferences();
switch(msg->what)
{
case B_ABOUT_REQUESTED:
{
AboutWindow *ab = new AboutWindow(Frame());
ab->Show();
break;
}
case M_BEST_TIMES:
{
BestTimesWindow *bt = new BestTimesWindow(Frame(),fNumberBase ,fGridSize);
bt->Show();
break;
}
case M_NEW_GAME:
{
GenerateGrid(fGridSize, true);
break;
}
case M_SIZE1_TILES:
case M_SIZE2_TILES:
case M_SIZE3_TILES:
case M_SIZE4_TILES:
case M_SIZE5_TILES:
{
const uint8 tileSize = tileSizes[msg->what - M_SIZE1_TILES];
if (fTileSize != tileSize) {
fTileSize = tileSize;
Preferences::Message().ReplaceInt8("tilesize", fTileSize);
HexTileView::CalcLayout(fTileSize);
GenerateGrid(fGridSize, false);
}
break;
}
case M_SET_TILE_COUNT_3:
{
fGridSize = 3;
Preferences::Message().ReplaceInt8("gridsize",3);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_TILE_COUNT_4:
{
fGridSize = 4;
Preferences::Message().ReplaceInt8("gridsize",4);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_TILE_COUNT_5:
{
fGridSize = 5;
Preferences::Message().ReplaceInt8("gridsize",5);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_TILE_COUNT_6:
{
fGridSize = 6;
Preferences::Message().ReplaceInt8("gridsize",6);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_TILE_COUNT_7:
{
fGridSize = 7;
Preferences::Message().ReplaceInt8("gridsize",7);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_BINARY:
{
fNumberBase = NUMBERBASE_BINARY;
Preferences::Message().ReplaceInt8("numberbase",NUMBERBASE_BINARY);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_QUARTERNARY:
{
fNumberBase = NUMBERBASE_QUARTERNARY;
Preferences::Message().ReplaceInt8("numberbase",NUMBERBASE_QUARTERNARY);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_HEXIMAL:
{
fNumberBase = NUMBERBASE_HEXIMAL;
Preferences::Message().ReplaceInt8("numberbase",NUMBERBASE_HEXIMAL);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_OCTAL:
{
fNumberBase = NUMBERBASE_OCTAL;
Preferences::Message().ReplaceInt8("numberbase",NUMBERBASE_OCTAL);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_DECIMAL:
{
fNumberBase = NUMBERBASE_DECIMAL;
Preferences::Message().ReplaceInt8("numberbase",NUMBERBASE_DECIMAL);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_HEXADECIMAL:
{
fNumberBase = NUMBERBASE_HEXADECIMAL;
Preferences::Message().ReplaceInt8("numberbase",NUMBERBASE_HEXADECIMAL);
GenerateGrid(fGridSize, true);
break;
}
case M_SET_BACKGROUND:
{
BString name;
if (msg->FindString("name",&name) == B_OK)
SetBackground(name.String());
else
SetBackground(NULL);
break;
}
case M_HOW_TO_PLAY:
{
BString string =
"How to play HexVexed:\n\n"
"Place the tiles in the grid on the right into the "
"grid on the left so that the numbers match wherever "
"one tile touches another. Easy to learn, tough to master.\n";
BAlert *help = new BAlert("HexVexed",string.String(),"OK");
help->Go();
break;
}
case M_CHECK_DROP:
{
HexTile *tile;
HexTileView *to;
BMessage originalMsg;
if(msg->FindPointer("to",(void**)&to)!=B_OK ||
msg->FindPointer("tile",(void**)&tile)!=B_OK ||
msg->FindMessage("original", &originalMsg)!=B_OK) {
msg->SendReply(B_MESSAGE_NOT_UNDERSTOOD);
break;
}
if (fGameOver) {
originalMsg.SendReply(B_MESSAGE_NOT_UNDERSTOOD);
break;
}
if(to->GridId() == fGrid->Id())
{
if (!to->GetTile()->IsEmpty()) {
originalMsg.SendReply(B_MESSAGE_NOT_UNDERSTOOD);
break;
}
*to->GetTile() = *tile;
tile->gridid = to->GridId();
UpdateTileBackup(to->GetTile());
to->Invalidate();
VerifyBoardIntegrity();
}
else if(to->GridId() == fWorkGrid->Id())
{
if(HexGrid::TryTile(tile, to->GetTile()))
{
*to->GetTile() = *tile;
tile->gridid = to->GridId();
UpdateTileBackup(to->GetTile());
to->Invalidate();
VerifyBoardIntegrity();
if(fWorkGrid->IsSolved() && !fGameOver)
{
fGameOver = true;
fTimer->Stop();
int32 elapsed = fTimer->Elapsed();
BestTimes::AddTime(fNumberBase, fGridSize, elapsed);
BestTimes::PrintBestTimes(fNumberBase, fGridSize);
ImageAlert *alert = new ImageAlert("HexVexedYouWin.png",'PNG ', Frame());
alert->Show();
}
} else {
originalMsg.SendReply(B_MESSAGE_NOT_UNDERSTOOD);
}
}
else
debugger("Programmer Error: Orphaned Tile");
break;
}
default:
BWindow::MessageReceived(msg);
}
Preferences::UnlockPreferences();
}
float
MainWindow::GenerateTiles(const BPoint& point, HexGrid* grid)
{
float x = point.x;
for (uint8 col = 0; col < fGridSize; col++, x += fTileSize * cos30) {
float y = point.y;
if (col % 2)
y += fTileSize * 0.5;
for (uint8 row = 0; row < fGridSize; row++, y += fTileSize) {
HexTileView* tile = new HexTileView(BPoint(x, y), fTileSize, "tile",
B_FOLLOW_NONE, B_WILL_DRAW);
tile->SetGridId(grid->Id());
fBack->AddChild(tile);
tile->Invalidate(tile->Bounds());
tile->SetTile(grid->TileAt(row * fGridSize + col));
}
}
return x;
}
void MainWindow::GenerateGrid(uint8 size, bool newGame)
{
if (newGame) {
if (fTimer->Running()) {
BAlert* alert = new BAlert("",
"Do you want to abort the current game?", "Continue game", "Abort game");
alert->SetShortcut(0, B_ESCAPE);
if (alert->Go() == 0)
return;
fTimer->Stop();
}
fGameOver = false;
delete fGrid;
delete fWorkGrid;
fGrid = new HexGrid(size, 0);
fGrid->SetNumberBase(fNumberBase);
fGrid->GeneratePuzzle();
fMaxTileCount = (uint16)size * size;
fTileBackup.clear();
for(int32 i = 0; i < (int32)size * size; i++) {
HexTile *t = fGrid->TileAt(i);
if(t && !t->IsEmpty())
fTileBackup[t->id] = *t;
}
fWorkGrid = new HexGrid(size, 1);
fTimer->Start();
}
while (fBack->CountChildren() > 2) {
BView *child = fBack->ChildAt(2);
child->RemoveSelf();
delete child;
}
const float inset = 10;
const float w = fTileSize * cos30 * (size * 2 + 1);
const float h = fTileSize * (size + 0.5);
const BRect timerBox(FindView("box")->Bounds());
const BPoint leftTop(inset, fMenuBar->Frame().bottom + inset);
const float timerY = leftTop.y + h + inset;
const float windowWidth = leftTop.x + w + inset;
const float windowHeight = timerY + timerBox.Height() + inset;
ResizeTo(windowWidth, windowHeight);
const float x = GenerateTiles(leftTop, fWorkGrid);
GenerateTiles(BPoint(x + fTileSize * 0.75, leftTop.y), fGrid);
fBack->ChildAt(1)->MoveTo((windowWidth - timerBox.Width()) / 2, timerY);
fBack->Invalidate();
}
void MainWindow::UpdateTileBackup(HexTile *tile)
{
if(!tile || tile->IsEmpty())
return;
fTileBackup[tile->id] = *tile;
}
void MainWindow::VerifyBoardIntegrity(void)
{
std::map<uint16, std::vector<HexTileView *> > occupied;
for (int32 i = 0; i < fBack->CountChildren(); i++) {
HexTileView *view = dynamic_cast<HexTileView *>(fBack->ChildAt(i));
if (!view)
continue;
HexTile *tile = view->GetTile();
if (tile && !tile->IsEmpty())
occupied[tile->id].push_back(view);
}
// A given piece id should only ever occupy one slot. If it shows
// up more than once, keep the first copy and clear the rest.
for (std::map<uint16, std::vector<HexTileView *> >::iterator it = occupied.begin();
it != occupied.end(); ++it) {
std::vector<HexTileView *> &views = it->second;
for (size_t i = 1; i < views.size(); i++) {
printf("VerifyBoardIntegrity: duplicate tile id=%u found, clearing extra copy\n",
it->first);
views[i]->GetTile()->MakeEmpty();
views[i]->Invalidate();
}
}
// Restore any tile that's missing entirely from the board.
for (std::map<uint16, HexTile>::const_iterator it = fTileBackup.begin();
it != fTileBackup.end(); ++it) {
if (occupied.find(it->first) != occupied.end())
continue;
printf("VerifyBoardIntegrity: tile id=%u missing from board, restoring\n",
it->first);
HexTileView *fallback = NULL;
bool restored = false;
for (int32 i = 0; i < fBack->CountChildren() && !restored; i++) {
HexTileView *view = dynamic_cast<HexTileView *>(fBack->ChildAt(i));
if (!view)
continue;
HexTile *slot = view->GetTile();
if (!slot || !slot->IsEmpty())
continue;
if (view->GridId() == fGrid->Id()) {
*slot = it->second;
slot->gridid = fGrid->Id();
view->Invalidate();
restored = true;
} else if (!fallback) {
fallback = view;
}
}
if (!restored && fallback) {
HexTile *slot = fallback->GetTile();
*slot = it->second;
slot->gridid = fallback->GridId();
fallback->Invalidate();
}
}
// Final safety net: total occupied slots should never exceed the
// number of pieces this game started with, regardless of cause.
int32 totalOccupied = 0;
for (int32 i = 0; i < fBack->CountChildren(); i++) {
HexTileView *view = dynamic_cast<HexTileView *>(fBack->ChildAt(i));
if (!view)
continue;
HexTile *tile = view->GetTile();
if (tile && !tile->IsEmpty())
totalOccupied++;
}
if (totalOccupied > fMaxTileCount) {
printf("VerifyBoardIntegrity: %ld tiles active, expected at most %u - clearing excess\n",
(long)totalOccupied, fMaxTileCount);
int32 toRemove = totalOccupied - fMaxTileCount;
for (int32 i = 0; i < fBack->CountChildren() && toRemove > 0; i++) {
HexTileView *view = dynamic_cast<HexTileView *>(fBack->ChildAt(i));
if (!view)
continue;
HexTile *tile = view->GetTile();
if (tile && !tile->IsEmpty()) {
tile->MakeEmpty();
view->Invalidate();
toRemove--;
}
}
}
}
void MainWindow::ScanBackgrounds(void)
{
while (fBackMenu->CountItems() > 0)
{
BMenuItem *item = fBackMenu->ItemAt(0L);
delete item;
}
BMessage *msg = new BMessage(M_SET_BACKGROUND);
msg->AddString("name","");
BDirectory dir(fBackPath.String());
dir.Rewind();
entry_ref ref;
while (dir.GetNextRef(&ref) == B_OK)
{
BPath path(&ref);
BString name(path.Leaf());
int32 ext = name.FindLast(".");
if (ext > 0)
name.Truncate(ext);
msg = new BMessage(M_SET_BACKGROUND);
msg->AddString("name",path.Leaf());
fBackMenu->AddItem(new BMenuItem(name.String(),msg));
}
}
void MainWindow::SetBackground(const char *name)
{
Preferences::LockPreferences();
if (!name || strlen(name) == 0)
{
fBack->ClearViewBitmap();
fBack->Invalidate();
fBackName = "";
Preferences::Message().RemoveData("background");
Preferences::UnlockPreferences();
return;
}
BString path(fBackPath);
path << name;
BBitmap *bmp = BTranslationUtils::GetBitmapFile(path.String());
if (bmp)
{
fBack->SetViewBitmap(bmp);
delete bmp;
fBackName = name;
Preferences::Message().RemoveData("background");
Preferences::Message().AddString("background", fBackName);
fBack->Invalidate();
}
Preferences::UnlockPreferences();
}