diff --git a/src/apps/sudoku/CenteredViewContainer.cpp b/src/apps/sudoku/CenteredViewContainer.cpp new file mode 100644 index 0000000000..3ce38673f3 --- /dev/null +++ b/src/apps/sudoku/CenteredViewContainer.cpp @@ -0,0 +1,44 @@ +/* + * Copyright 2007, Michael Pfeiffer, laplace@users.sourceforge.net. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#include "CenteredViewContainer.h" + +CenteredViewContainer::CenteredViewContainer(BView *target, BRect frame, const char* name, uint32 resizingMode) + : BView(frame, name, resizingMode, B_WILL_DRAW | B_FRAME_EVENTS) + , fTarget(target) +{ + SetViewColor(B_TRANSPARENT_COLOR); + // to avoid flickering + AddChild(fTarget); + _CenterTarget(frame.Width(), frame.Height()); +} + +CenteredViewContainer::~CenteredViewContainer() +{ +} + +void +CenteredViewContainer::Draw(BRect updateRect) +{ + FillRect(updateRect); +} + +void +CenteredViewContainer::FrameResized(float width, float height) +{ + BView::FrameResized(width, height); + _CenterTarget(width, height); +} + +void CenteredViewContainer::_CenterTarget(float width, float height) +{ + float size = width < height ? width : height; + float left = floor((width - size) / 2); + float top = floor((height - size) / 2); + fTarget->MoveTo(left, top); + fTarget->ResizeTo(size, size); + fTarget->FrameResized(size, size); + // in BeOS R5 BView::FrameResized is not (always) called automatically + // after ResizeTo() +} diff --git a/src/apps/sudoku/CenteredViewContainer.h b/src/apps/sudoku/CenteredViewContainer.h new file mode 100644 index 0000000000..a281a86c2e --- /dev/null +++ b/src/apps/sudoku/CenteredViewContainer.h @@ -0,0 +1,27 @@ +/* + * Copyright 2007, Michael Pfeiffer, laplace@users.sourceforge.net. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef CENTERED_VIEW_CONTAINER_H +#define CENTERED_VIEW_CONTAINER_H + + +#include + + + +class CenteredViewContainer : public BView { +public: + CenteredViewContainer(BView *target, BRect frame, const char* name, uint32 resizingMode); + virtual ~CenteredViewContainer(); + + void Draw(BRect updateRect); + void FrameResized(float width, float height); + +private: + void _CenterTarget(float width, float height); + + BView *fTarget; +}; + +#endif // CENTERED_VIEW_CONTAINER_H \ No newline at end of file diff --git a/src/apps/sudoku/Jamfile b/src/apps/sudoku/Jamfile index d216d3afc7..038f5384fc 100644 --- a/src/apps/sudoku/Jamfile +++ b/src/apps/sudoku/Jamfile @@ -3,6 +3,7 @@ SubDir HAIKU_TOP src apps sudoku ; SetSubDirSupportedPlatformsBeOSCompatible ; Application Sudoku : + CenteredViewContainer.cpp ProgressWindow.cpp Sudoku.cpp SudokuField.cpp diff --git a/src/apps/sudoku/SudokuGenerator.cpp b/src/apps/sudoku/SudokuGenerator.cpp index 06d650a8d9..6ca45aa96b 100644 --- a/src/apps/sudoku/SudokuGenerator.cpp +++ b/src/apps/sudoku/SudokuGenerator.cpp @@ -10,6 +10,7 @@ #include "SudokuField.h" #include "SudokuSolver.h" +#include SudokuGenerator::SudokuGenerator() { diff --git a/src/apps/sudoku/SudokuView.cpp b/src/apps/sudoku/SudokuView.cpp index 61cf1fdd47..85fded11cc 100644 --- a/src/apps/sudoku/SudokuView.cpp +++ b/src/apps/sudoku/SudokuView.cpp @@ -55,8 +55,11 @@ SudokuView::SudokuView(BRect frame, const char* name, if (settings.FindBool("show cursor", &fShowCursor) != B_OK) fShowCursor = false; - SetViewColor(255, 255, 240); - SetLowColor(ViewColor()); + SetViewColor(B_TRANSPARENT_COLOR); + // to avoid flickering + rgb_color color = { 255, 255, 240 }; + fBackgroundColor = color; + SetLowColor(color); FrameResized(0, 0); } @@ -99,7 +102,7 @@ SudokuView::_FilterString(const char* data, size_t dataLength, char* buffer, continue; if (!_ValidCharacter(data[i])) { - return B_BAD_DATA; + return B_BAD_VALUE; } buffer[out++] = data[i]; @@ -737,6 +740,11 @@ SudokuView::_DrawHints(uint32 x, uint32 y) void SudokuView::Draw(BRect /*updateRect*/) { + // draw one pixel border otherwise not covered + // by lines and fields + SetLowColor(fBackgroundColor); + StrokeRect(Bounds(), B_SOLID_LOW); + // draw lines uint32 size = fField->Size(); @@ -778,9 +786,11 @@ SudokuView::Draw(BRect /*updateRect*/) //SetLowColor(tint_color(ViewColor(), B_DARKEN_1_TINT)); SetLowColor(255, 255, 210); FillRect(_Frame(x, y), B_SOLID_LOW); - } else - SetLowColor(ViewColor()); - + } else { + SetLowColor(fBackgroundColor); + FillRect(_Frame(x, y), B_SOLID_LOW); + } + if (fShowKeyboardFocus && x == fKeyboardX && y == fKeyboardY) _DrawKeyboardFocus(); diff --git a/src/apps/sudoku/SudokuView.h b/src/apps/sudoku/SudokuView.h index f384ace9d9..4ccb7d1afe 100644 --- a/src/apps/sudoku/SudokuView.h +++ b/src/apps/sudoku/SudokuView.h @@ -75,6 +75,7 @@ private: void _DrawKeyboardFocus(); void _DrawHints(uint32 x, uint32 y); + rgb_color fBackgroundColor; SudokuField* fField; uint32 fBlockSize; float fWidth, fHeight, fBaseline; diff --git a/src/apps/sudoku/SudokuWindow.cpp b/src/apps/sudoku/SudokuWindow.cpp index 559acaea33..e3d8721b66 100644 --- a/src/apps/sudoku/SudokuWindow.cpp +++ b/src/apps/sudoku/SudokuWindow.cpp @@ -6,6 +6,7 @@ #include "SudokuWindow.h" +#include "CenteredViewContainer.h" #include "ProgressWindow.h" #include "Sudoku.h" #include "SudokuField.h" @@ -156,9 +157,13 @@ SudokuWindow::SudokuWindow() top->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); AddChild(top); - fSudokuView = new SudokuView(top->Bounds().InsetByCopy(10, 10), - "sudoku view", settings, B_FOLLOW_ALL); - top->AddChild(fSudokuView); + fSudokuView = new SudokuView(top->Bounds().InsetByCopy(10, 10).OffsetToSelf(0, 0), + "sudoku view", settings, B_FOLLOW_NONE); + CenteredViewContainer * container = new CenteredViewContainer(fSudokuView, + top->Bounds().InsetByCopy(10, 10), + "center", B_FOLLOW_ALL); + container->SetHighColor(top->ViewColor()); + top->AddChild(container); // add menu