summaryrefslogtreecommitdiff
path: root/include/ncurses/window.hpp
blob: fc1c87c67d5d9db7fb81c67de4e014728fb67350 (plain)
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
#ifndef INCLUDE_NCURSES_WINDOW_HPP_
#define INCLUDE_NCURSES_WINDOW_HPP_

#include <vector>

#include <ncurses/utils/preamble.hpp>

namespace NCURSES_CPP_NAMESPACE {

class window {
public:
  // basic
  window(int lines, int cols, int begin_y, int begin_x)
      : win_(newwin(lines, cols, begin_y, begin_x)), parent_(nullptr) {}

  window(window &parent, int lines, int cols, int begin_y, int begin_x)
      : win_(subwin(parent.win_, lines, cols, begin_y, begin_x)),
        parent_(&parent) {}

  window(const window &other)
      : win_(dupwin(other.win_)), parent_(other.parent_) {}

  window(window &&other) : win_(other.win_) { other.win_ = nullptr; }

  window &operator=(const window &other) {
    delwin(win_);
    win_ = dupwin(other.win_);
    return *this;
  }

  window &operator=(window &&other) {
    delwin(win_);
    win_ = other.win_;
    other.win_ = nullptr;
    return *this;
  }

  explicit operator WINDOW *() const { return win_; }

  virtual ~window() { free_window(); }

  // interface

private:
  void add_subwin(window &win) { subwins_.push_back(&win); }
  void free_window() {
    if (win_) {
      delwin(win_);
      for (auto it : subwins_) {
        it->free_window();
      }
    }
    win_ = nullptr;
  }

  WINDOW *win_;
  window *parent_;
  std::vector<window *> subwins_;
};

} // namespace NCURSES_CPP_NAMESPACE

#endif // INCLUDE_NCURSES_WINDOW_HPP_