File: resize3.cc

package info (click to toggle)
yapet 2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,920 kB
  • sloc: cpp: 32,397; sh: 5,032; makefile: 880; ansic: 36; sed: 16
file content (135 lines) | stat: -rw-r--r-- 3,426 bytes parent folder | download | duplicates (4)
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
// $Id$
//
// Test basic functionality of Curses class
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif  // HAVE_SYS_IOCTL_H

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif  // HAVE_SYS_TYPES_H

#ifdef HAVE_STROPTS_H
#include <stropts.h>
#endif  // HAVE_STROPTS_H

#ifdef HAVE_TERMIOS_H
#include <termios.h>
#else  // HAVE_TERMIOS_H
#ifdef HAVE_SYS_TERMIOS_H
#include <sys/termios.h>
#endif  // HAVE_SYS_TERMIOS_H
#endif  // HAVE_TERMIOS_H

#include <iostream>

#include "yacurs.h"

// Used when preloading libtestpreload.so
#ifdef YACURS_USE_WCHAR
extern "C" int __test_wget_wch(void*, wint_t* i) {
    *i = 'q';
    return OK;
}
#else
extern "C" int __test_wgetch(void*) { return 'q'; }
#endif

class MyWindow : public YACURS::Window {
   protected:
    void resize_handler(YACURS::Event& _e) {
        YACURS::Window::resize_handler(_e);

        YACURS::EventEx<YACURS::Size>& winch =
            dynamic_cast<YACURS::EventEx<YACURS::Size>&>(_e);
        std::string status_msg("YACURS::Size: rows=");

        char buff[32];

        snprintf(buff, 32, "%d", winch.data().rows());
        status_msg += buff;

        status_msg += " cols=";

        snprintf(buff, 32, "%d", winch.data().cols());
        status_msg += buff;

        YACURS::Curses::statusbar()->push(status_msg);
    }

   public:
    MyWindow() : YACURS::Window() {
        YACURS::EventQueue::connect_event(
            YACURS::EventConnectorMethod1<MyWindow>(YACURS::EVT_SIGWINCH, this,
                                                    &MyWindow::resize_handler));
    }

    MyWindow(const YACURS::Margin& _m) : YACURS::Window(_m) {}
};

class HotKeyQuit : public YACURS::HotKey {
   public:
    HotKeyQuit(int k) : HotKey(k) {}

    HotKeyQuit(const HotKeyQuit& hk) : HotKey(hk) {}

    void action() { YACURS::EventQueue::submit(YACURS::EVT_QUIT); }

    HotKey* clone() const { return new HotKeyQuit(*this); }
};

int main() {
    // test will not be run if stdout or stdin is not a tty.
    if (isatty(STDOUT_FILENO) != 1 || isatty(STDIN_FILENO) != 1) exit(77);

#ifdef YACURS_USE_WCHAR
    if (setlocale(LC_ALL, "en_US.UTF-8") == 0) exit(77);
#endif

    try {
        YACURS::Curses::init();

        YACURS::TitleBar* title =
            new YACURS::TitleBar(YACURS::TitleBar::POS_TOP, "Resize 3");
        YACURS::Curses::title(title);

        // NOTE:
        //
        // The order the objects are created (MyWindow, StatusBar) is
        // important here. Because MyWindow calls
        // StatusBar::put_msg() on resize we have to make sure
        // StatusBar is resized first. Since YACURS::EventQueue calls the
        // last YACURS::EventConnector connected first, StatusBar has to be
        // created AFTER MyWindow.

        MyWindow* w1 = new MyWindow(YACURS::Margin(1, 0, 1, 0));
        w1->frame(true);
        w1->add_hotkey(HotKeyQuit('q'));
        w1->add_hotkey(HotKeyQuit('Q'));

        YACURS::StatusBar* sl = new YACURS::StatusBar();
        YACURS::Curses::statusbar(sl);
        sl->push("Press Q to quit");

        YACURS::Curses::mainwindow(w1);

        YACURS::Curses::run();

        delete title;
        delete w1;
        delete sl;
        YACURS::Curses::end();
    } catch (std::exception& e) {
        YACURS::Curses::end();
        std::cerr << e.what() << std::endl;
        return 1;
    }

    return 0;
}