libyui-ncurses  2.54.5
NCApplication.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCApplication.cc
20 
21  Authors: Gabriele Mohr <gs@suse.de>
22  Stefan Hundhammer <sh@suse.de>
23 
24 /-*/
25 
26 #include <ncursesw/curses.h>
27 
28 #define YUILogComponent "ncurses"
29 #include <yui/YUILog.h>
30 #include "NCurses.h"
31 #include "YNCursesUI.h"
32 #include "NCApplication.h"
33 #include "NCAskForDirectory.h"
34 #include "NCAskForFile.h"
35 
36 
38 {
39 
40 }
41 
42 
44 {
45 
46 }
47 
48 void
49 NCApplication::setLanguage( const std::string & language,
50  const std::string & encoding )
51 {
52  // Intentionally NOT calling
53  // YApplication::setLanguage( language, encoding );
54  // This would implicitly overwrite LC_CTYPE which might result in encoding bugs.
55 
56  setlocale( LC_NUMERIC, "C" ); // always format numbers with "."
57  NCurses::Refresh();
58 
59  yuiDebug() << "Language: " << language << " Encoding: " << (( encoding != "" ) ? encoding : "NOT SET" ) << std::endl;
60 
61 }
62 
63 
64 std::string
65 NCApplication::askForSaveFileName( const std::string & startDir,
66  const std::string & filter,
67  const std::string & headline )
68 {
69  NCAskForSaveFileName * filePopup = new NCAskForSaveFileName( wpos( 1, 1 ), startDir, filter, headline );
70  YUI_CHECK_NEW( filePopup );
71 
72  NCursesEvent retEvent = filePopup->showDirPopup( );
73  YDialog::deleteTopmostDialog();
74 
75  yuiMilestone() << "Returning: " << retEvent.result << std::endl;
76  return retEvent.result;
77 }
78 
79 
80 std::string
81 NCApplication::askForExistingFile( const std::string & startDir,
82  const std::string & filter,
83  const std::string & headline )
84 {
85  NCAskForExistingFile * filePopup = new NCAskForExistingFile( wpos( 1, 1 ), startDir, filter, headline );
86  YUI_CHECK_NEW( filePopup );
87 
88  NCursesEvent retEvent = filePopup->showDirPopup( );
89  YDialog::deleteTopmostDialog();
90 
91  yuiMilestone() << "Returning: " << retEvent.result << std::endl;
92  return retEvent.result;
93 }
94 
95 
96 std::string
97 NCApplication::askForExistingDirectory( const std::string & startDir,
98  const std::string & headline )
99 {
100  NCAskForExistingDirectory * dirPopup = new NCAskForExistingDirectory( wpos( 1, 1 ), startDir, headline );
101  YUI_CHECK_NEW( dirPopup );
102 
103  NCursesEvent retEvent = dirPopup->showDirPopup( );
104  YDialog::deleteTopmostDialog();
105 
106  yuiMilestone() << "Returning: " << retEvent.result << std::endl;
107  return retEvent.result;
108 }
109 
110 
111 void
113 {
114  ::beep();
115 }
116 
117 
119 {
120  YNCursesUI::ui()->Refresh();
121 }
122 
123 
124 void
126 {
127  /*
128  * Following code breaks the console keyboard e.g. for czech language during
129  * installation (bnc #433016). According to bnc #367801 comment #18/#19 the
130  * line isn't needed at all.
131  * "dumpkeys | loadkeys -C "$KBD_TTY" --unicode" has been also removed from kbd
132  * initscript. If dumpkeys has to be called for any reason it definitely needs
133  * the codepage argument, otherwise it cannot work.
134  */
135 #if 0
136  std::string cmd = "/bin/dumpkeys | /bin/loadkeys --unicode";
137 
138  if ( NCstring::terminalEncoding() == "UTF-8" )
139  {
140  int ret = system(( cmd + " >/dev/null 2>&1" ).c_str() );
141 
142  if ( ret != 0 )
143  {
144  yuiError() << "ERROR: /bin/dumpkeys | /bin/loadkeys --unicode returned: " << ret << std::endl;
145  }
146  }
147 #endif
148 }
149 
150 
151 void
152 NCApplication::setConsoleFont( const std::string & console_magic,
153  const std::string & font,
154  const std::string & screen_map,
155  const std::string & unicode_map,
156  const std::string & language )
157 {
158  /**
159  * Moving that code from YNCursesUI to this class turned out to be
160  * impossible (or at least a lot more work than it's worth) that I finally
161  * gave it up.
162  *
163  * - sh@suse.de 2008-02-06
164  **/
165  YNCursesUI::ui()->setConsoleFont( console_magic,
166  font,
167  screen_map,
168  unicode_map,
169  language );
170 }
171 
172 void
173 NCApplication::closeUI() {
174  // Save tty modes and end ncurses mode temporarily
175  ::def_prog_mode();
176  ::endwin();
177 
178  // Regenerate saved stdout and stderr, so that app called
179  // via system() can use them and draw something to the terminal
180  dup2( YNCursesUI::ui()->stdout_save, 1 );
181  dup2( YNCursesUI::ui()->stderr_save, 2 );
182 }
183 
184 void
186  // Redirect stdout and stderr to y2log again
187  YNCursesUI::ui()->RedirectToLog();
188 
189  // Resume tty modes and refresh the screen
190  ::reset_prog_mode();
191 
192  ::refresh();
193 }
194 
195 int
196 NCApplication::runInTerminal( const std::string & cmd )
197 {
198  int ret = 0;
199 
200  closeUI();
201 
202  // Call external program
203  ret = system( cmd.c_str() );
204 
205  if ( ret != 0 )
206  {
207  yuiError() << cmd << " returned:" << ret << std::endl;
208  }
209 
210  openUI();
211 
212  return ret;
213 }
214 
215 
216 int
217 NCApplication::displayWidth()
218 {
219  return ::COLS; // exported from ncurses.h
220 }
221 
222 
223 int
224 NCApplication::displayHeight()
225 {
226  return ::LINES; // exported from ncurses.h
227 }
228 
229 
230 int
231 NCApplication::displayDepth()
232 {
233  return -1;
234 }
235 
236 
237 long
238 NCApplication::displayColors()
239 {
240  return NCattribute::colors();
241 }
242 
243 
244 int
245 NCApplication::defaultWidth()
246 {
247  return ::COLS; // exported from ncurses.h
248 }
249 
250 
251 int
252 NCApplication::defaultHeight()
253 {
254  return ::LINES; // exported from ncurses.h
255 }
256 
257 
258 bool
259 NCApplication::hasFullUtf8Support()
260 {
261  return ( NCstring::terminalEncoding() == "UTF-8" );
262 }
263 
264 void NCApplication::setApplicationTitle ( const std::string& title )
265 {
266  YApplication::setApplicationTitle ( title );
267  NCurses::SetTitle(title);
268 }
269 
virtual void redrawScreen() override
Redraw the screen.
virtual void setApplicationTitle(const std::string &title) override
Set the application title.
virtual void setConsoleFont(const std::string &console_magic, const std::string &font, const std::string &screen_map, const std::string &unicode_map, const std::string &lang)
Set the (text) console font according to the current encoding etc.
Definition: YNCursesUI.cc:344
virtual ~NCApplication()
Destructor.
virtual int runInTerminal(const std::string &command) override
Run a shell command (typically an interactive program using NCurses) in a terminal (window)...
virtual std::string askForExistingDirectory(const std::string &startDir, const std::string &headline) override
Open a directory selection box and prompt the user for an existing directory.
static YNCursesUI * ui()
Access the global Y2NCursesUI.
Definition: YNCursesUI.h:93
Definition: position.h:109
virtual void setLanguage(const std::string &language, const std::string &encoding=std::string()) override
Set language and encoding for the locale environment ($LANG).
NCApplication()
Constructor.
virtual std::string askForSaveFileName(const std::string &startWith, const std::string &filter, const std::string &headline) override
Open a file selection box and prompt the user for a file to save data to.
virtual void setConsoleFont(const std::string &console_magic, const std::string &font, const std::string &screen_map, const std::string &unicode_map, const std::string &language) override
Set the (text) console font according to the current encoding etc.
virtual std::string askForExistingFile(const std::string &startWith, const std::string &filter, const std::string &headline) override
Open a file selection box and prompt the user for an existing file.
virtual void beep() override
Beep.
virtual void openUI() override
To mix TUI (NCurses) with stdio, enclose the UI parts within openUI/closeUI.
NCursesEvent & showDirPopup()
Shows the popup with the std::list of directories.
NCursesEvent & showDirPopup()
Shows the popup with the std::list of directories.
virtual void initConsoleKeyboard() override
Initialize the (text) console keyboard.