summaryrefslogtreecommitdiff
path: root/glib/src/optioncontext.ccg
blob: ad86903ec93473e9cc975f671b12357ded73de7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glibmm/utility.h>
#include <glibmm/exceptionhandler.h>
#include <glib.h>

namespace Glib
{

namespace OptionContextPrivate
{
extern "C"
{
static const gchar*
SignalProxy_translate_gtk_callback(const gchar* str, gpointer data)
{
  Glib::ustring translated_str;
  Glib::OptionContext::SlotTranslate* the_slot =
    static_cast<Glib::OptionContext::SlotTranslate*>(data);

  try
  {
    translated_str = (*the_slot)(str);
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return translated_str.c_str();
}

static void
SignalProxy_translate_gtk_callback_destroy(gpointer data)
{
  delete static_cast<Glib::OptionContext::SlotTranslate*>(data);
}
} // extern "C"
} // namespace OptionContextPrivate

OptionContext::OptionContext(const Glib::ustring& parameter_string)
: gobject_(g_option_context_new(parameter_string.c_str())), has_ownership_(true)
{
}

OptionContext::OptionContext(GOptionContext* castitem, bool take_ownership)
: gobject_(castitem), has_ownership_(take_ownership)
{
}

OptionContext::OptionContext(OptionContext&& other) noexcept
  : gobject_(std::move(other.gobject_)),
    has_ownership_(std::move(other.has_ownership_))
{
  other.gobject_ = nullptr;
  other.has_ownership_ = false;
}

OptionContext&
OptionContext::operator=(OptionContext&& other) noexcept
{
  if (has_ownership_)
    g_option_context_free(gobj());

  gobject_ = std::move(other.gobject_);
  has_ownership_ = std::move(other.has_ownership_);

  other.gobject_ = nullptr;
  other.has_ownership_ = false;

  return *this;
}

OptionContext::~OptionContext()
{
  if (has_ownership_)
    g_option_context_free(gobj());

  gobject_ = nullptr;
}

void
OptionContext::add_group(OptionGroup& group)
{
  // GObjectContext takes ownership of the GOptionGroup, unrefing it later.
  g_option_context_add_group(gobj(), group.gobj_copy());
}

void
OptionContext::set_main_group(OptionGroup& group)
{
  // GObjectContext takes ownership of the GOptionGroup, unrefing it later.
  g_option_context_set_main_group(gobj(), group.gobj_copy());
}

/*
OptionGroup OptionContext::get_main_group() const
{
  const auto cobj = g_option_context_get_main_group(const_cast<GOptionContext*>( gobj()) );
  OptionGroup cppObj(const_cast<GOptionGroup*>(cobj), true); // take_copy
  return cppObj;
}

*/

void
OptionContext::set_translate_func(const SlotTranslate& slot)
{
  // Create a copy of the slot. A pointer to this will be passed through the callback's data
  // parameter.
  // It will be deleted when SignalProxy_translate_gtk_callback_destroy() is called.
  auto slot_copy = new SlotTranslate(slot);

  g_option_context_set_translate_func(gobj(),
    &OptionContextPrivate::SignalProxy_translate_gtk_callback, slot_copy,
    &OptionContextPrivate::SignalProxy_translate_gtk_callback_destroy);
}

Glib::ustring
OptionContext::get_help(bool main_help) const
{
  return Glib::convert_return_gchar_ptr_to_ustring(g_option_context_get_help(
    const_cast<GOptionContext*>(gobj()), static_cast<int>(main_help), nullptr));
}

} // namespace Glib