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