"Fossies" - the Fresh Open Source Software Archive

Member "regexxer-0.10/src/main.cc" (6 Oct 2011, 6947 Bytes) of package /linux/privat/old/regexxer-0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.cc" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2002-2007  Daniel Elstner  <daniel.kitta@gmail.com>
    3  *
    4  * This file is part of regexxer.
    5  *
    6  * regexxer is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 2 of the License, or
    9  * (at your option) any later version.
   10  *
   11  * regexxer is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with regexxer; if not, write to the Free Software Foundation,
   18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   19  */
   20 
   21 #include "globalstrings.h"
   22 #include "mainwindow.h"
   23 #include "miscutils.h"
   24 #include "translation.h"
   25 
   26 #include <glib.h>
   27 #include <gtk/gtk.h> /* for gtk_window_set_default_icon_name() */
   28 #include <glibmm.h>
   29 #include <gtkmm/iconfactory.h>
   30 #include <gtkmm/iconset.h>
   31 #include <gtkmm/iconsource.h>
   32 #include <gtkmm/main.h>
   33 #include <gtkmm/stock.h>
   34 #include <gtkmm/stockitem.h>
   35 #include <gtkmm/window.h>
   36 #include <gtksourceviewmm/init.h>
   37 #include <giomm/init.h>
   38 
   39 #include <exception>
   40 #include <list>
   41 #include <memory>
   42 
   43 #include <config.h>
   44 
   45 namespace
   46 {
   47 
   48 /*
   49  * Include inlined raw pixbuf data generated by gdk-pixbuf-csource.
   50  */
   51 #include <ui/stockimages.h>
   52 
   53 struct StockIconData
   54 {
   55   const guint8*         data;
   56   int                   length;
   57   Gtk::BuiltinIconSize  size;
   58 };
   59 
   60 struct StockItemData
   61 {
   62   const char*           id;
   63   const StockIconData*  icons;
   64   int                   n_icons;
   65   const char*           label;
   66 };
   67 
   68 static
   69 const StockIconData stock_icon_save_all[] =
   70 {
   71   { stock_save_all_16, sizeof(stock_save_all_16), Gtk::ICON_SIZE_MENU          },
   72   { stock_save_all_24, sizeof(stock_save_all_24), Gtk::ICON_SIZE_SMALL_TOOLBAR }
   73 };
   74 
   75 static
   76 const StockItemData regexxer_stock_items[] =
   77 {
   78   { "regexxer-save-all", stock_icon_save_all, G_N_ELEMENTS(stock_icon_save_all), N_("Save _all") }
   79 };
   80 
   81 class RegexxerOptions
   82 {
   83 private:
   84   Regexxer::InitState   init_state_;
   85   Glib::OptionGroup     group_;
   86   Glib::OptionContext   context_;
   87 
   88   static Glib::OptionEntry entry(const char* long_name, char short_name,
   89                                  const char* description, const char* arg_description = 0);
   90   RegexxerOptions();
   91 
   92 public:
   93   static std::auto_ptr<RegexxerOptions> create();
   94   ~RegexxerOptions();
   95 
   96   Glib::OptionContext& context()    { return context_; }
   97   Regexxer::InitState& init_state() { return init_state_; }
   98 };
   99 
  100 // static
  101 Glib::OptionEntry RegexxerOptions::entry(const char* long_name, char short_name,
  102                                          const char* description, const char* arg_description)
  103 {
  104   Glib::OptionEntry option_entry;
  105 
  106   option_entry.set_long_name(long_name);
  107   option_entry.set_short_name(short_name);
  108 
  109   if (description)
  110     option_entry.set_description(description);
  111   if (arg_description)
  112     option_entry.set_arg_description(arg_description);
  113 
  114   return option_entry;
  115 }
  116 
  117 RegexxerOptions::RegexxerOptions()
  118 :
  119   init_state_ (),
  120   group_      (PACKAGE_TARNAME, Glib::ustring()),
  121   context_    ()
  122 {}
  123 
  124 RegexxerOptions::~RegexxerOptions()
  125 {}
  126 
  127 // static
  128 std::auto_ptr<RegexxerOptions> RegexxerOptions::create()
  129 {
  130   std::auto_ptr<RegexxerOptions> options (new RegexxerOptions());
  131 
  132   Glib::OptionGroup&  group = options->group_;
  133   Regexxer::InitState& init = options->init_state_;
  134 
  135   group.add_entry(entry("pattern", 'p', N_("Find files matching PATTERN"), N_("PATTERN")),
  136                   init.pattern);
  137   group.add_entry(entry("no-recursion", 'R', N_("Do not recurse into subdirectories")),
  138                   init.no_recursive);
  139   group.add_entry(entry("hidden", 'h', N_("Also find hidden files")),
  140                   init.hidden);
  141   group.add_entry(entry("regex", 'e', N_("Find text matching REGEX"), N_("REGEX")),
  142                   init.regex);
  143   group.add_entry(entry("no-global", 'G', N_("Find only the first match in a line")),
  144                   init.no_global);
  145   group.add_entry(entry("ignore-case", 'i', N_("Do case insensitive matching")),
  146                   init.ignorecase);
  147   group.add_entry(entry("substitution", 's', N_("Replace matches with STRING"), N_("STRING")),
  148                   init.substitution);
  149   group.add_entry(entry("line-number", 'n', N_("Print match location to standard output")),
  150                   init.feedback);
  151   group.add_entry(entry("no-autorun", 'A', N_("Do not automatically start search")),
  152                   init.no_autorun);
  153   group.add_entry_filename(entry(G_OPTION_REMAINING, '\0', 0, N_("[FOLDER]")),
  154                            init.folder);
  155 
  156   group.set_translation_domain(PACKAGE_TARNAME);
  157   options->context_.set_main_group(group);
  158 
  159   return options;
  160 }
  161 
  162 static
  163 void register_stock_items()
  164 {
  165   const Glib::RefPtr<Gtk::IconFactory> factory = Gtk::IconFactory::create();
  166   const Glib::ustring domain = PACKAGE_TARNAME;
  167 
  168   for (unsigned int item = 0; item < G_N_ELEMENTS(regexxer_stock_items); ++item)
  169   {
  170     const StockItemData& stock = regexxer_stock_items[item];
  171     Glib::RefPtr<Gtk::IconSet> icon_set = Gtk::IconSet::create();
  172 
  173     for (int icon = 0; icon < stock.n_icons; ++icon)
  174     {
  175       const StockIconData& icon_data = stock.icons[icon];
  176       Gtk::IconSource source;
  177 
  178       source.set_pixbuf(Gdk::Pixbuf::create_from_inline(icon_data.length, icon_data.data));
  179       source.set_size(icon_data.size);
  180 
  181       // Unset wildcarded for all but the the last icon.
  182       source.set_size_wildcarded(icon == stock.n_icons - 1);
  183 
  184       icon_set->add_source(source);
  185     }
  186 
  187     const Gtk::StockID stock_id (stock.id);
  188 
  189     factory->add(stock_id, icon_set);
  190     Gtk::Stock::add(Gtk::StockItem(stock_id, stock.label, Gdk::ModifierType(0), 0, domain));
  191   }
  192 
  193   factory->add_default();
  194 }
  195 
  196 } // anonymous namespace
  197 
  198 int main(int argc, char** argv)
  199 {
  200   try
  201   {
  202     Util::initialize_gettext(PACKAGE_TARNAME, REGEXXER_LOCALEDIR);
  203 
  204     std::auto_ptr<RegexxerOptions> options = RegexxerOptions::create();
  205     Gtk::Main main_instance (argc, argv, options->context());
  206     Gsv::init();
  207     Gio::init();
  208 
  209     Glib::set_application_name(PACKAGE_NAME);
  210     register_stock_items();
  211     gtk_window_set_default_icon_name(PACKAGE_TARNAME);
  212 
  213     Regexxer::MainWindow window;
  214 
  215     window.initialize(options->init_state());
  216     options.reset();
  217 
  218     Gtk::Main::run(*window.get_window());
  219 
  220     return 0;
  221   }
  222   catch (const Glib::OptionError& error)
  223   {
  224     const Glib::ustring what = error.what();
  225     g_printerr("%s: %s\n", g_get_prgname(), what.c_str());
  226   }
  227   catch (const Glib::Error& error)
  228   {
  229     const Glib::ustring what = error.what();
  230     g_error("unhandled exception: %s", what.c_str());
  231   }
  232   catch (const std::exception& ex)
  233   {
  234     g_error("unhandled exception: %s", ex.what());
  235   }
  236   catch (...)
  237   {
  238     g_error("unhandled exception: (type unknown)");
  239   }
  240 
  241   return 1;
  242 }