Fix crash on startup if an LV2 plugin has a bad .ttl file.
[ardour.git] / libs / glibmm2 / glib / src / optioncontext.ccg
1 // -*- c++ -*-
2 /* $Id: optioncontext.ccg,v 1.4 2004/10/30 14:25:45 murrayc Exp $ */
3
4 /* Copyright (C) 2002 The gtkmm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <glibmm/utility.h>
22 #include <glibmm/exceptionhandler.h>
23 #include <glib.h>
24
25 namespace Glib
26 {
27
28   namespace Private
29   {
30     static const gchar* SignalProxy_translate_gtk_callback (const gchar* str, gpointer data)
31     {
32       Glib::ustring translated_str;
33       Glib::OptionContext::SlotTranslate* the_slot =
34         static_cast<Glib::OptionContext::SlotTranslate*>(data);
35
36 #ifdef GLIBMM_EXCEPTIONS_ENABLED
37       try
38       {
39 #endif //GLIBMM_EXCEPTIONS_ENABLED
40         translated_str = (*the_slot)(str);
41 #ifdef GLIBMM_EXCEPTIONS_ENABLED
42       }
43       catch(...)
44       {
45         Glib::exception_handlers_invoke();
46       }
47 #endif //GLIBMM_EXCEPTIONS_ENABLED
48       return translated_str.c_str ();
49     }
50
51     static void SignalProxy_translate_gtk_callback_destroy (gpointer data)
52     {
53       delete static_cast<Glib::OptionContext::SlotTranslate*>(data);
54     }
55
56   } //namespace Private
57
58 OptionContext::OptionContext(const Glib::ustring& parameter_string)
59 : gobject_( g_option_context_new(parameter_string.c_str()) ),
60   has_ownership_(true)
61 {
62 }
63
64 OptionContext::OptionContext(GOptionContext* castitem, bool take_ownership)
65 : gobject_(castitem),
66   has_ownership_(take_ownership)
67 {
68 }
69
70 OptionContext::~OptionContext()
71 {
72   if(has_ownership_)
73     g_option_context_free(gobj());
74     
75   gobject_ = 0;
76 }
77
78 void OptionContext::add_group(OptionGroup& group)
79 {
80   //Strangely, GObjectContext actually takes ownership of the GOptionGroup, deleting it later.
81   g_option_context_add_group(gobj(), (group).gobj_give_ownership());
82 }
83
84 void OptionContext::set_main_group(OptionGroup& group)
85 {
86   //Strangely, GObjectContext actually takes ownership of the GOptionGroup, deleting it later.
87   g_option_context_set_main_group(gobj(), (group).gobj_give_ownership());
88 }
89
90
91 /*
92 OptionGroup OptionContext::get_main_group() const
93 {
94   const GOptionGroup* cobj = g_option_context_get_main_group(const_cast<GOptionContext*>( gobj()) );
95   OptionGroup cppObj(const_cast<GOptionGroup*>(cobj), true); // take_copy
96   return cppObj;
97 }
98
99 */
100
101 void OptionContext::set_translate_func (const SlotTranslate& slot)
102 {
103   //Create a copy of the slot. A pointer to this will be passed through the callback's data parameter.
104   //It will be deleted when SignalProxy_translate_gtk_callback_destroy() is called.
105   SlotTranslate* slot_copy = new SlotTranslate(slot);
106
107   g_option_context_set_translate_func(
108       gobj(), &Private::SignalProxy_translate_gtk_callback, slot_copy,
109       &Private::SignalProxy_translate_gtk_callback_destroy);
110 }
111
112 Glib::ustring OptionContext::get_help(bool main_help) const
113 {
114   return Glib::convert_return_gchar_ptr_to_ustring(g_option_context_get_help(const_cast<GOptionContext*>(gobj()), static_cast<int>(main_help), NULL));
115 }
116
117 } // namespace Glib