add new sigc++2 directory
[ardour.git] / libs / gtkmm2 / tests / main_with_options / main.cc
1 //$Id: main.cc 616 2006-05-11 11:40:25Z murrayc $ -*- c++ -*-
2
3 /* gtkmm example Copyright (C) 2002 gtkmm development team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18
19 #include <gtkmm.h>
20 #include <iomanip>
21 #include <iostream>
22
23
24 class ExampleOptionGroup : public Glib::OptionGroup
25
26 public:
27   ExampleOptionGroup();
28
29   virtual bool on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
30   virtual bool on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group);
31   virtual void on_error(Glib::OptionContext& context, Glib::OptionGroup& group);
32   
33   //These int instances should live as long as the OptionGroup to which they are added, 
34   //and as long as the OptionContext to which those OptionGroups are added.
35   int m_arg_foo;
36   std::string m_arg_filename;
37   Glib::ustring m_arg_goo;
38   bool m_arg_boolean;
39   Glib::OptionGroup::vecustrings m_arg_list;
40 };
41
42 ExampleOptionGroup::ExampleOptionGroup()
43 : Glib::OptionGroup("example_group", "description of example group", "help description of example group"),
44   m_arg_foo(0), m_arg_boolean(false)
45 {
46   Glib::OptionEntry entry1;
47   entry1.set_long_name("foo");
48   entry1.set_short_name('f');
49   entry1.set_description("The Foo");
50   add_entry(entry1, m_arg_foo);
51       
52   Glib::OptionEntry entry2;
53   entry2.set_long_name("file");
54   entry2.set_short_name('F');
55   entry2.set_description("The Filename");
56   add_entry_filename(entry2, m_arg_filename);
57  
58   Glib::OptionEntry entry3;
59   entry3.set_long_name("goo");
60   entry3.set_short_name('g');
61   entry3.set_description("The Goo");
62   add_entry(entry3, m_arg_goo);
63   
64   Glib::OptionEntry entry4;
65   entry4.set_long_name("activate_something");
66   entry4.set_description("Activate something");
67   add_entry(entry4, m_arg_boolean);
68   
69   Glib::OptionEntry entry5;
70   entry5.set_long_name("list");
71   entry5.set_short_name('l');
72   entry5.set_description("The List");
73   add_entry(entry5, m_arg_list);
74 }
75
76 bool ExampleOptionGroup::on_pre_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
77 {
78   //This is called before the m_arg_* instances are given their values.
79   return Glib::OptionGroup::on_pre_parse(context, group);
80 }
81
82 bool ExampleOptionGroup::on_post_parse(Glib::OptionContext& context, Glib::OptionGroup& group)
83 {
84   //This is called after the m_arg_* instances are given their values.
85   return Glib::OptionGroup::on_post_parse(context, group);
86 }
87
88 void ExampleOptionGroup::on_error(Glib::OptionContext& context, Glib::OptionGroup& group)
89 {
90   Glib::OptionGroup::on_error(context, group);
91 }
92
93 int main(int argc, char *argv[])
94 {
95   //This example should be executed like so:
96   //./example --foo=1 --bar=2 --goo=abc
97   //./example --help
98    
99   Glib::OptionContext context;
100   
101   ExampleOptionGroup group;
102   context.set_main_group(group);
103   
104   #ifdef GLIBMM_EXCEPTIONS_ENABLED
105   try
106   {
107   #endif //GLIBMM_EXCEPTIONS_ENABLED
108     Gtk::Main main_instance(argc, argv, context);
109   
110     //Here we can see the parsed values of our custom command-line arguments:
111
112     std::cout << "parsed values: " << std::endl <<
113       "  foo = " << group.m_arg_foo << std::endl << 
114       "  filename = " << group.m_arg_filename << std::endl <<
115       "  activate_something = " << (group.m_arg_boolean ? "enabled" : "disabled") << std::endl <<
116       "  goo = " << group.m_arg_goo << std::endl;
117     
118     //This one shows the results of multiple instance of the same option, such as --list=1 --list=a --list=b
119     std::cout << "  list = ";
120     for(Glib::OptionGroup::vecustrings::const_iterator iter = group.m_arg_list.begin(); iter != group.m_arg_list.end(); ++iter)
121     {
122       std::cout << *iter << ", ";
123     }
124     std::cout << std::endl;
125  
126
127     //Any standard GTK+ command-line arguments will have an effect on this window:
128     //Try --name="bobble" to change the window's title to "bobble", for instance.
129     Gtk::Window testWindow;
130     main_instance.run(testWindow); //Shows the window and returns when it is closed.
131   #ifdef GLIBMM_EXCEPTIONS_ENABLED
132   }
133   catch(const Glib::Error& ex)
134   {
135     std::cout << "Exception: " << ex.what() << std::endl;
136   }
137   #endif //GLIBMM_EXCEPTIONS_ENABLED
138
139
140   return 0;
141 }