]> ncurses.scripts.mit.edu Git - ncurses.git/blob - c++/cursesapp.cc
ncurses 6.2 - patch 20200301
[ncurses.git] / c++ / cursesapp.cc
1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3  * Copyright 2019,2020 Thomas E. Dickey                                     *
4  * Copyright 1998-2007,2008 Free Software Foundation, Inc.                  *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30
31 /****************************************************************************
32  *   Author: Juergen Pfeifer, 1997                                          *
33  *      and: Thomas E. Dickey                                               *
34  ****************************************************************************/
35
36 #include "internal.h"
37 #include "cursesapp.h"
38
39 MODULE_ID("$Id: cursesapp.cc,v 1.17 2020/02/02 23:34:34 tom Exp $")
40
41 void
42 NCursesApplication::init(bool bColors)
43 {
44   if (bColors)
45     NCursesWindow::useColors();
46
47   if (Root_Window->colors() > 1) {
48     b_Colors = TRUE;
49     Root_Window->setcolor(1);
50     Root_Window->setpalette(COLOR_YELLOW,COLOR_BLUE);
51     Root_Window->setcolor(2);
52     Root_Window->setpalette(COLOR_CYAN,COLOR_BLUE);
53     Root_Window->setcolor(3);
54     Root_Window->setpalette(COLOR_BLACK,COLOR_BLUE);
55     Root_Window->setcolor(4);
56     Root_Window->setpalette(COLOR_BLACK,COLOR_CYAN);
57     Root_Window->setcolor(5);
58     Root_Window->setpalette(COLOR_BLUE,COLOR_YELLOW);
59     Root_Window->setcolor(6);
60     Root_Window->setpalette(COLOR_BLACK,COLOR_GREEN);
61   }
62   else
63     b_Colors = FALSE;
64
65   Root_Window->bkgd(' '|window_backgrounds());
66 }
67
68 NCursesApplication* NCursesApplication::theApp = 0;
69 NCursesWindow* NCursesApplication::titleWindow = 0;
70 NCursesApplication::SLK_Link* NCursesApplication::slk_stack = 0;
71
72 NCursesApplication::~NCursesApplication() THROWS(NCursesException)
73 {
74   Soft_Label_Key_Set* S;
75
76   delete titleWindow;
77   titleWindow = 0;
78
79   while( (S=top()) ) {
80     pop();
81     delete S;
82   }
83
84   delete Root_Window;
85   Root_Window = 0;
86
87   ::endwin();
88 }
89
90 int NCursesApplication::rinit(NCursesWindow& w)
91 {
92   titleWindow = &w;
93   return OK;
94 }
95
96 void NCursesApplication::push(Soft_Label_Key_Set& S)
97 {
98   SLK_Link* L = new SLK_Link;
99   assert(L != 0);
100   L->prev = slk_stack;
101   L->SLKs = &S;
102   slk_stack = L;
103   if (Root_Window)
104     S.show();
105 }
106
107 bool NCursesApplication::pop()
108 {
109   if (slk_stack) {
110     SLK_Link* L = slk_stack;
111     slk_stack = slk_stack->prev;
112     delete L;
113     if (Root_Window) {
114       Soft_Label_Key_Set* xx = top();
115       if (xx != 0)
116         xx->show();
117     }
118   }
119   return (slk_stack ? FALSE : TRUE);
120 }
121
122 Soft_Label_Key_Set* NCursesApplication::top() const
123 {
124   if (slk_stack)
125     return slk_stack->SLKs;
126   else
127     return static_cast<Soft_Label_Key_Set*>(0);
128 }
129
130 int NCursesApplication::operator()(void)
131 {
132   bool bColors = b_Colors;
133   Soft_Label_Key_Set* S = 0;
134
135   int ts = titlesize();
136   if (ts>0)
137     NCursesWindow::ripoffline(ts,rinit);
138   Soft_Label_Key_Set::Label_Layout fmt = useSLKs();
139   if (fmt!=Soft_Label_Key_Set::None) {
140     S = new Soft_Label_Key_Set(fmt);
141     assert(S != 0);
142     init_labels(*S);
143   }
144
145   Root_Window = new NCursesWindow(::stdscr);
146   init(bColors);
147
148   if (ts>0)
149     title();
150   if (fmt!=Soft_Label_Key_Set::None) {
151     push(*S);
152   }
153
154   return run();
155 }
156
157 NCursesApplication::NCursesApplication(bool bColors)
158   : b_Colors(bColors),
159     Root_Window(NULL)
160 {
161   if (theApp)
162     THROW(new NCursesException("Application object already created."));
163   else
164     theApp = this;
165 }