diff --git a/DockLayout.vcxproj b/DockLayout.vcxproj index 1e44c98..9ca6302 100644 --- a/DockLayout.vcxproj +++ b/DockLayout.vcxproj @@ -95,6 +95,7 @@ + @@ -103,6 +104,7 @@ + diff --git a/DockLayout.vcxproj.filters b/DockLayout.vcxproj.filters index 2e6bb02..e78acfc 100644 --- a/DockLayout.vcxproj.filters +++ b/DockLayout.vcxproj.filters @@ -28,6 +28,9 @@ ソース ファイル + + ソース ファイル + ソース ファイル @@ -45,6 +48,9 @@ ヘッダー ファイル + + ヘッダー ファイル + ヘッダー ファイル diff --git a/src/PanelLayout.cpp b/src/PanelLayout.cpp new file mode 100644 index 0000000..d6778f6 --- /dev/null +++ b/src/PanelLayout.cpp @@ -0,0 +1,148 @@ +#include "PanelLayout.h" + +#include + +#include "HostContext.h" +#include "StyleMetrics.h" + +namespace dl { + +static const RECT kEmptyBand = {}; + +int PanelLayout::ClassifyRow(int y, int x0, int x1) const { + const StyleMetrics& style = Style(); + int borders = 0; + for (int x = x0; x < x1; x++) { + const int color = surface_.At(x, y); + if (color == style.windowBorder) borders++; + else if (color != style.windowSeparator) return Content; + } + return borders * 2 >= (x1 - x0) ? Line : Gap; +} + +int PanelLayout::ClassifyColumn(int x, int y0, int y1) const { + const StyleMetrics& style = Style(); + int borders = 0; + for (int y = y0; y < y1; y++) { + const int color = surface_.At(x, y); + if (color == style.windowBorder) borders++; + else if (color != style.windowSeparator) return Content; + } + return borders * 2 >= (y1 - y0) ? Line : Gap; +} + +bool PanelLayout::RowMostly(int y, int x0, int x1, int color, bool* full) const { + int hits = 0; + for (int x = x0; x < x1; x++) + if (surface_.At(x, y) == color) hits++; + *full = hits == (x1 - x0); + return hits * 2 > (x1 - x0); +} + +std::vector PanelLayout::SplitBands(const RECT& area, StackAxis axis) const { + const bool vertical = axis == StackAxis::Vertical; + const int lo = vertical ? area.top : area.left; + const int hi = vertical ? area.bottom : area.right; + const int from = vertical ? area.left : area.top; + const int to = vertical ? area.right : area.bottom; + + std::vector bands; + int start = lo; + for (int at = lo + 1; at + 2 < hi; at++) { + const int kind = vertical ? ClassifyRow(at, from, to) : ClassifyColumn(at, from, to); + if (kind != Line) continue; + int gap = 0; + while (at + 1 + gap < hi && + (vertical ? ClassifyRow(at + 1 + gap, from, to) + : ClassifyColumn(at + 1 + gap, from, to)) == Gap) + gap++; + if (gap == 0 || at + 1 + gap >= hi) continue; + if ((vertical ? ClassifyRow(at + 1 + gap, from, to) + : ClassifyColumn(at + 1 + gap, from, to)) != Line) + continue; + RECT band = area; + (vertical ? band.top : band.left) = start; + (vertical ? band.bottom : band.right) = at + 1; + bands.push_back(band); + start = at + 1 + gap; + at = start; + } + if (bands.empty()) return bands; + RECT last = area; + (vertical ? last.top : last.left) = start; + bands.push_back(last); + return bands; +} + +RECT PanelLayout::TitleBand(const RECT& area) const { + const StyleMetrics& style = Style(); + const int left = area.left + 1; + const int right = area.right - 1; + if (right - left <= 0) return kEmptyBand; + + const int top = surface_.At(left, area.top) == style.windowBorder ? area.top + 1 : area.top; + const int colors[2] = { style.titleHeader, style.grouping }; + const int heights[2] = { style.titleHeaderHeight, style.settingItemHeight }; + for (int i = 0; i < 2; i++) { + int rows = 0; + bool glyph = false; + bool full = false; + while (top + rows < area.bottom && RowMostly(top + rows, left, right, colors[i], &full)) { + if (!full) glyph = true; + rows++; + } + if (rows > 0 && glyph && abs(rows - heights[i]) <= 1) + return RECT{ area.left, top, area.right, top + rows }; + } + return kEmptyBand; +} + +void PanelLayout::Divide(const RECT& area, int stack, int order) { + for (const StackAxis axis : { StackAxis::Vertical, StackAxis::Horizontal }) { + const std::vector bands = SplitBands(area, axis); + if (bands.size() < 2) continue; + const int id = static_cast(stacks_.size()); + stacks_.push_back(axis); + for (size_t i = 0; i < bands.size(); i++) + Divide(bands[i], id, static_cast(i)); + return; + } + panels_.push_back(Panel{ area, TitleBand(area), stack, order }); +} + +bool PanelLayout::Build() { + panels_.clear(); + stacks_.clear(); + region_ = kEmptyBand; + + HWND host = HostWindow(); + if (!host) return false; + RECT client = {}; + if (!GetClientRect(host, &client)) return false; + + RefreshStyle(); + const RECT region = { 0, 0, client.right, client.bottom - Style().footerHeight }; + if (region.right <= 0 || region.bottom <= 0) return false; + if (!surface_.Capture(host, client.right, client.bottom)) return false; + + region_ = region; + Divide(region_, -1, 0); + surface_.Release(); + return !panels_.empty(); +} + +int PanelLayout::IndexAt(POINT pt) const { + for (size_t i = 0; i < panels_.size(); i++) { + const RECT& r = panels_[i].rect; + if (pt.x >= r.left && pt.x < r.right && pt.y >= r.top && pt.y < r.bottom) + return static_cast(i); + } + return -1; +} + +StackAxis PanelLayout::AxisOf(int stack) const { + if (stack < 0 || stack >= static_cast(stacks_.size())) return StackAxis::Vertical; + return stacks_[stack]; +} + +} diff --git a/src/PanelLayout.h b/src/PanelLayout.h new file mode 100644 index 0000000..bc98e1b --- /dev/null +++ b/src/PanelLayout.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include + +#include "HostSurface.h" + +namespace dl { + +enum class StackAxis { Vertical, Horizontal }; + +struct Panel { + RECT rect; + RECT title; + int stack; + int order; + + bool HasTitle() const { return title.bottom > title.top; } +}; + +class PanelLayout { +public: + bool Build(); + + const RECT& Region() const { return region_; } + const std::vector& Panels() const { return panels_; } + int IndexAt(POINT pt) const; + StackAxis AxisOf(int stack) const; + +private: + enum RowClass { Content = -1, Gap = 0, Line = 1 }; + + int ClassifyRow(int y, int x0, int x1) const; + int ClassifyColumn(int x, int y0, int y1) const; + bool RowMostly(int y, int x0, int x1, int color, bool* full) const; + std::vector SplitBands(const RECT& area, StackAxis axis) const; + RECT TitleBand(const RECT& area) const; + void Divide(const RECT& area, int stack, int order); + + HostSurface surface_; + RECT region_ = {}; + std::vector panels_; + std::vector stacks_; +}; + +}