libyui
3.0.10
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YApplication.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 #include <locale.h> // setlocale() 00026 #include <map> 00027 00028 #define YUILogComponent "ui" 00029 #include "YUILog.h" 00030 00031 #include "YApplication.h" 00032 #include "YDialog.h" 00033 #include "YUIException.h" 00034 #include "YShortcut.h" 00035 #include "YUI.h" 00036 #include "YItem.h" 00037 #include "YCommandLine.h" 00038 00039 using std::endl; 00040 00041 typedef std::map<std::string, int> YFunctionKeyMap; 00042 00043 00044 struct YApplicationPrivate 00045 { 00046 YApplicationPrivate() 00047 : productName( "openSUSE" ) 00048 , reverseLayout( false ) 00049 {} 00050 00051 std::string productName; 00052 bool reverseLayout; 00053 std::string applicationTitle; 00054 std::string applicationIcon; 00055 YFunctionKeyMap defaultFunctionKey; 00056 YIconLoader* iconLoader; 00057 }; 00058 00059 00060 YApplication::YApplication() 00061 : priv( new YApplicationPrivate() ) 00062 { 00063 YUI_CHECK_NEW( priv ); 00064 priv->iconLoader = new YIconLoader(); 00065 YCommandLine cmdLine; // Retrieve command line args from /proc/<pid>/cmdline 00066 if ( cmdLine.argc() > 0 ) 00067 priv->applicationTitle = cmdLine.arg(0); 00068 } 00069 00070 00071 YApplication::~YApplication() 00072 { 00073 // NOP 00074 } 00075 00076 00077 YWidget * 00078 YApplication::findWidget( YWidgetID * id, bool doThrow ) const 00079 { 00080 YDialog * dialog = YDialog::currentDialog( doThrow ); 00081 00082 if ( ! dialog ) // has already thrown if doThrow == true 00083 return 0; 00084 00085 return dialog->findWidget( id, doThrow ); 00086 } 00087 00088 00089 std::string 00090 YApplication::iconBasePath() const 00091 { 00092 return priv->iconLoader->iconBasePath(); 00093 } 00094 00095 00096 void 00097 YApplication::setIconBasePath( const std::string & newIconBasePath ) 00098 { 00099 priv->iconLoader->setIconBasePath ( newIconBasePath ); 00100 } 00101 00102 YIconLoader * 00103 YApplication::iconLoader() 00104 { 00105 return priv->iconLoader; 00106 } 00107 00108 void 00109 YApplication::setProductName( const std::string & productName ) 00110 { 00111 priv->productName = productName; 00112 } 00113 00114 00115 std::string 00116 YApplication::productName() const 00117 { 00118 return priv->productName; 00119 } 00120 00121 00122 void 00123 YApplication::setReverseLayout( bool reverse ) 00124 { 00125 priv->reverseLayout = reverse; 00126 } 00127 00128 00129 bool YApplication::reverseLayout() const 00130 { 00131 return priv->reverseLayout; 00132 } 00133 00134 00135 int 00136 YApplication::defaultFunctionKey( const std::string & label ) const 00137 { 00138 YFunctionKeyMap::const_iterator result = 00139 priv->defaultFunctionKey.find( YShortcut::cleanShortcutString( label ) ); 00140 00141 if ( result == priv->defaultFunctionKey.end() ) 00142 return 0; 00143 else 00144 return result->second; 00145 } 00146 00147 00148 void 00149 YApplication::setDefaultFunctionKey( const std::string & label, int fkey ) 00150 { 00151 if ( fkey > 0 ) 00152 priv->defaultFunctionKey[ YShortcut::cleanShortcutString( label ) ] = fkey; 00153 else 00154 YUI_THROW( YUIException( "Bad function key number" ) ); 00155 } 00156 00157 00158 void 00159 YApplication::clearDefaultFunctionKeys() 00160 { 00161 priv->defaultFunctionKey.clear(); 00162 } 00163 00164 00165 void 00166 YApplication::setLanguage( const std::string & language, const std::string & encoding ) 00167 { 00168 std::string lang = language; 00169 00170 if ( ! encoding.empty() ) 00171 { 00172 lang += "."; 00173 lang += encoding; 00174 } 00175 00176 setenv( "LANG", lang.c_str(), 1 ); // 1 : replace 00177 setlocale( LC_NUMERIC, "C" ); // but always format numbers with "." 00178 00179 yuiMilestone() << "Setting language to " << lang << endl; 00180 } 00181 00182 00183 std::string 00184 YApplication::language( bool stripEncoding ) const 00185 { 00186 const char *lang_env = getenv( "LANG" ); 00187 00188 if ( ! lang_env ) 00189 return ""; 00190 00191 std::string lang( lang_env ); 00192 00193 if ( stripEncoding ) 00194 { 00195 std::string::size_type pos = lang.find_first_of( ".@" ); 00196 00197 if ( pos != std::string::npos ) // if encoding etc. specified 00198 { 00199 lang = lang.substr( 0, pos ); // remove it 00200 } 00201 } 00202 00203 return lang; 00204 } 00205 00206 00207 std::string 00208 YApplication::glyph( const std::string & sym ) 00209 { 00210 if ( sym == YUIGlyph_ArrowLeft ) return ( reverseLayout() ? "->" : "<-" ); 00211 else if ( sym == YUIGlyph_ArrowRight ) return ( reverseLayout() ? "<-" : "->" ); 00212 else if ( sym == YUIGlyph_ArrowUp ) return ( "^" ); 00213 else if ( sym == YUIGlyph_ArrowDown ) return ( "v" ); 00214 else if ( sym == YUIGlyph_CheckMark ) return ( "x" ); 00215 else if ( sym == YUIGlyph_BulletArrowRight ) return ( "=>" ); 00216 else if ( sym == YUIGlyph_BulletCircle ) return ( "o" ); 00217 else if ( sym == YUIGlyph_BulletSquare ) return ( "[]" ); 00218 else // unknown glyph symbol 00219 { 00220 yuiError() << "Unknown glyph `" << sym << endl; 00221 return ""; 00222 } 00223 } 00224 00225 bool 00226 YApplication::openContextMenu( const YItemCollection & itemCollection ) 00227 { 00228 YUI_THROW( YUIUnsupportedWidgetException( "ContextMenu" ) ); 00229 return false; 00230 } 00231 00232 00233 00234 int 00235 YApplication::deviceUnits( YUIDimension dim, float layoutUnits ) 00236 { 00237 return (int) ( layoutUnits + 0.5 ); 00238 } 00239 00240 00241 float 00242 YApplication::layoutUnits( YUIDimension dim, int deviceUnits ) 00243 { 00244 return (float) deviceUnits; 00245 } 00246 00247 00248 int 00249 YApplication::runInTerminal ( const std::string & module ) 00250 { 00251 yuiError() << "Not in text mode: Cannot run external program in terminal." << endl; 00252 00253 return -1; 00254 } 00255 00256 void YApplication::setApplicationTitle(const std::string &title) 00257 { 00258 priv->applicationTitle = title; 00259 } 00260 00261 const std::string &YApplication::applicationTitle() const 00262 { 00263 return priv->applicationTitle; 00264 } 00265 00266 void YApplication::setApplicationIcon(const std::string &icon) 00267 { 00268 priv->applicationIcon = icon; 00269 } 00270 const std::string &YApplication::applicationIcon() const 00271 { 00272 return priv->applicationIcon; 00273 } 00274