Back to index

unity  6.0.0
ShortcutHintPrivate.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Andrea Azzarone <azzaronea@gmail.com>
00017  */
00018 
00019 #include <glib/gi18n-lib.h>
00020 #include <gtk/gtk.h>
00021 
00022 #include "ShortcutHintPrivate.h"
00023 #include "UnityCore/GLibWrapper.h"
00024 
00025 #include <boost/algorithm/string/replace.hpp>
00026 
00027 namespace unity
00028 {
00029 namespace shortcut
00030 {
00031 namespace impl
00032 {
00033 
00034 std::string GetMetaKey(std::string const& scut)
00035 {
00036   size_t index = scut.find_last_of( ">");
00037   if (index >= 0)
00038     return std::string(scut.begin(), scut.begin() + index + 1);
00039   else
00040     return "";
00041 }
00042 
00043 std::string FixShortcutFormat(std::string const& scut)
00044 {
00045   std::string ret(scut.begin(), scut.end() - 1);
00046   
00047   boost::replace_all(ret, "<", "");
00048   boost::replace_all(ret, ">", " + ");
00049   
00050   if (scut[scut.size()-1] != '>')
00051     ret += scut[scut.size()-1];
00052     
00053   return ret;
00054 }
00055 
00056 std::string GetTranslatableLabel(std::string const& scut)
00057 {
00058   guint accelerator_key;
00059   GdkModifierType accelerator_mods;
00060 
00061   gtk_accelerator_parse(scut.c_str(),
00062                         &accelerator_key,
00063                         &accelerator_mods);
00064 
00065   std::string temp(glib::String(gtk_accelerator_get_label(accelerator_key, accelerator_mods)).Str());
00066 
00067   // gtk_accelerator_get_label adds an extra '+' at the end of the label.
00068   if (temp.length() > 0)
00069   {
00070     std::string::iterator it = temp.end() - 1;
00071     if (*it == '+')
00072       temp.erase(it);
00073   }
00074 
00075   // Adds an extra space around the '+'.
00076   boost::replace_all(temp, "+", " + ");
00077     
00078   return temp;
00079 }
00080 
00081 std::string FixMouseShortcut(std::string const& scut)
00082 {
00083   std::string ret(scut);
00084   
00085   boost::replace_all(ret, "Button1", _("Left Mouse"));
00086   boost::replace_all(ret, "Button2", _("Middle Mouse"));
00087   boost::replace_all(ret, "Button3", _("Right Mouse"));
00088       
00089   return ret;
00090 }
00091 
00092 std::string ProperCase(std::string const& str)
00093 {
00094   std::string ret = str;
00095   
00096        bool cap_next = true;
00097 
00098        for (unsigned int i = 0; i < ret.length(); ++i)
00099   {
00100     if (cap_next and isalpha(ret[i]))
00101     {
00102       ret[i]=toupper(ret[i]);
00103       cap_next = false;
00104     }
00105     else
00106     {
00107       cap_next = ispunct(ret[i]) || isspace(ret[i]);
00108     }
00109   }
00110   
00111   return ret;
00112 }
00113 
00114 } // namespace impl
00115 } // namespace shortcut
00116 } // namespace unity
00117