~vcs-imports/gconfmm/main

« back to all changes in this revision

Viewing changes to examples/client/window_test.cc

  • Committer: murrayc
  • Date: 2007-01-09 15:43:50 UTC
  • Revision ID: vcs-imports@canonical.com-20070109154350-lczyek6p0f2bqgva
moving

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- C++ -*-
 
2
 
 
3
/* window_test.cc
 
4
 *
 
5
 * Copyright (C) 2000-2003 gnomemm Development Team
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the Free
 
19
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
 
 
22
#include "window_test.h"
 
23
 
 
24
#include <iostream>
 
25
#include <gtkmm/window.h>
 
26
#include <gtkmm/box.h>
 
27
 
 
28
Window_Test::Window_Test()
 
29
: m_VBox(false, 10),
 
30
  m_Label("Maindir added 1 times\nSubdir added 0 times"),
 
31
  m_Button_Quit("Quit"),
 
32
  m_Button_RemoveSubDir("Remove subdir"),
 
33
  m_Button_AddSubDir("Add subdir"),
 
34
  m_Button_RemoveMainDir("Remove maindir"),
 
35
  m_Button_AddMainDir("Add maindir")
 
36
{
 
37
  m_bBlockEntryHandlers = false;
 
38
 
 
39
  m_iSubDir = 0;
 
40
  m_iMainDir = 0;
 
41
 
 
42
  set_title("Gnome::Conf::Client Demo");
 
43
  set_default_size(200, 200);
 
44
 
 
45
  m_strMainDir = "/apps/gnome/testgconfclient";
 
46
  m_refClient = Gnome::Conf::Client::get_default_client();
 
47
 
 
48
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
49
  m_refClient->add_dir(m_strMainDir);
 
50
  #else
 
51
  std::auto_ptr<Glib::Error> error;
 
52
  m_refClient->add_dir(m_strMainDir, Gnome::Conf::CLIENT_PRELOAD_NONE, error);
 
53
  if(error.get())
 
54
    std::cerr << error->what() << std::endl;
 
55
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
56
 
 
57
  // test code to figure out show that list of floats work:
 
58
  /*
 
59
  Glib::ustring testkey = m_strMainDir + "/test_floattype_list";
 
60
  typedef gdouble floattype;
 
61
 
 
62
  std::list<floattype> testListFloat;
 
63
  testListFloat.push_back(1.1);
 
64
  testListFloat.push_back(2.2);
 
65
  testListFloat.push_back(3.3);
 
66
  m_refClient->set_float_list(testkey, testListFloat);
 
67
 
 
68
  std::list<floattype> testListFloatFromGet = m_refClient->get_float_list(testkey);
 
69
  for(std::list<floattype>::iterator iter = testListFloatFromGet.begin(); iter != testListFloatFromGet.end(); ++iter)
 
70
  {
 
71
    floattype a = *iter;
 
72
    //std::cout << "float list item:" << a << std::endl;
 
73
    g_warning("floattype list item: %f", a);
 
74
  }
 
75
  */
 
76
 
 
77
  
 
78
 
 
79
  add(m_VBox);
 
80
 
 
81
  m_VBox.pack_end(m_Label, Gtk::PACK_SHRINK);
 
82
 
 
83
  m_VBox.pack_end(m_Button_Quit, Gtk::PACK_SHRINK);
 
84
  m_VBox.pack_end(m_Button_RemoveSubDir, Gtk::PACK_SHRINK);
 
85
  m_VBox.pack_end(m_Button_AddSubDir, Gtk::PACK_SHRINK);
 
86
  m_VBox.pack_end(m_Button_RemoveMainDir, Gtk::PACK_SHRINK);
 
87
  m_VBox.pack_end(m_Button_AddMainDir, Gtk::PACK_SHRINK);
 
88
 
 
89
  //Connect button signals:
 
90
  m_Button_Quit.signal_clicked().connect(sigc::mem_fun(*this, &Window_Test::on_Button_Quit));
 
91
  m_Button_RemoveSubDir.signal_clicked().connect(sigc::mem_fun(*this, &Window_Test::on_Button_RemoveSubDir));
 
92
  m_Button_AddSubDir.signal_clicked().connect(sigc::mem_fun(*this, &Window_Test::on_Button_AddSubDir));
 
93
  m_Button_RemoveMainDir.signal_clicked().connect(sigc::mem_fun(*this, &Window_Test::on_Button_RemoveMainDir));
 
94
  m_Button_AddMainDir.signal_clicked().connect(sigc::mem_fun(*this, &Window_Test::on_Button_AddMainDir));
 
95
 
 
96
 
 
97
  Gtk::HBox* pBox = 0;
 
98
  pBox = create_entry_attached_to(m_strMainDir + "/blah");
 
99
  m_VBox.pack_start(*pBox, Gtk::PACK_SHRINK);
 
100
 
 
101
  pBox = create_entry_attached_to(m_strMainDir + "/foo");
 
102
  m_VBox.pack_start(*pBox, Gtk::PACK_SHRINK);
 
103
 
 
104
  pBox = create_entry_attached_to(m_strMainDir + "/bar");
 
105
  m_VBox.pack_start(*pBox, Gtk::PACK_SHRINK);
 
106
 
 
107
  pBox = create_entry_attached_to(m_strMainDir + "/subdir/testsub1");
 
108
  m_VBox.pack_start(*pBox, Gtk::PACK_SHRINK);
 
109
 
 
110
  pBox = create_entry_attached_to(m_strMainDir + "/subdir/testsub2");
 
111
  m_VBox.pack_start(*pBox, Gtk::PACK_SHRINK);
 
112
 
 
113
  show_all();
 
114
}
 
115
 
 
116
Window_Test::~Window_Test()
 
117
{
 
118
  if(m_refClient)
 
119
  {
 
120
    //Remove callbacks (Is this really necessary)
 
121
    for(type_listNotifyIDs::iterator iter = m_listNotifyIDs.begin(); iter != m_listNotifyIDs.end(); iter++)
 
122
    {
 
123
      guint notify_id = *iter;
 
124
 
 
125
      #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
126
      m_refClient->notify_remove(notify_id);
 
127
      #else
 
128
      std::auto_ptr<Glib::Error> error;
 
129
      m_refClient->notify_remove(notify_id, error);
 
130
      if(error.get())
 
131
        std::cerr << error->what() << std::endl;
 
132
      #endif //GLIBMM_EXCEPTIONS_ENABLED
 
133
    }
 
134
    m_listNotifyIDs.clear();
 
135
  }
 
136
}
 
137
 
 
138
void Window_Test::on_Button_Quit()
 
139
{
 
140
  Gtk::Main::quit();
 
141
}
 
142
 
 
143
void Window_Test::on_Entry_changed(Gtk::Entry* pEntry, Glib::ustring key)
 
144
{
 
145
  if(!m_bBlockEntryHandlers)
 
146
  {
 
147
    if(pEntry)
 
148
    {
 
149
      const Glib::ustring text = pEntry->get_text();
 
150
 
 
151
      #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
152
      m_refClient->set(key, text);
 
153
      #else
 
154
      std::auto_ptr<Glib::Error> error;
 
155
      m_refClient->set(key, text, error);
 
156
      if(error.get())
 
157
        std::cerr << error->what() << std::endl;
 
158
      #endif //GLIBMM_EXCEPTIONS_ENABLED
 
159
    }
 
160
  }
 
161
}
 
162
 
 
163
Gtk::HBox* Window_Test::create_entry_attached_to(const Glib::ustring& key)
 
164
{
 
165
  Gtk::HBox* pHBox = Gtk::manage(new Gtk::HBox(false, 10));
 
166
  Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(key));
 
167
 
 
168
  pHBox->pack_start(*pLabel, Gtk::PACK_SHRINK);
 
169
 
 
170
  Gtk::Entry* pEntry = Gtk::manage(new Gtk::Entry());
 
171
 
 
172
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
173
  pEntry->set_text( m_refClient->get_string(key) );
 
174
  #else
 
175
  std::auto_ptr<Glib::Error> error;
 
176
  pEntry->set_text( m_refClient->get_string(key, error ) );
 
177
  if(error.get())
 
178
    std::cerr << error->what() << std::endl;
 
179
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
180
 
 
181
  //Connect callback and remember the ID for later:
 
182
  guint notify_id = m_refClient->notify_add(key, sigc::bind<Gtk::Entry*>(sigc::mem_fun(*this, &Window_Test::Client_notify_func), pEntry));
 
183
  m_listNotifyIDs.push_back(notify_id);
 
184
 
 
185
  pEntry->signal_changed().connect(sigc::bind(
 
186
                            sigc::mem_fun(*this, &Window_Test::on_Entry_changed),
 
187
                            pEntry,
 
188
                            key)
 
189
                         );
 
190
 
 
191
  /* Set sensitive according to whether the key is writable or not. */
 
192
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
193
  pEntry->set_sensitive( m_refClient->key_is_writable(key) );
 
194
  #else
 
195
  {
 
196
    std::auto_ptr<Glib::Error> error;
 
197
    pEntry->set_sensitive( m_refClient->key_is_writable(key, error) );
 
198
    if(error.get())
 
199
      std::cerr << error->what() << std::endl;
 
200
  }
 
201
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
202
 
 
203
  pHBox->pack_end(*pEntry, Gtk::PACK_SHRINK);
 
204
 
 
205
  return pHBox;
 
206
}
 
207
 
 
208
void Window_Test::update_label_text()
 
209
{
 
210
  gchar* s = g_strdup_printf("Maindir added %d times\nSubdir added %d times", m_iMainDir, m_iSubDir);
 
211
  Glib::ustring str = s;
 
212
  g_free(s);
 
213
 
 
214
  m_Label.set_text(str);
 
215
}
 
216
 
 
217
void Window_Test::on_Button_RemoveSubDir()
 
218
{
 
219
  m_iSubDir--;
 
220
 
 
221
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
222
  m_refClient->remove_dir(m_strMainDir + "/subdir");
 
223
  #else
 
224
  std::auto_ptr<Glib::Error> error;
 
225
  m_refClient->remove_dir(m_strMainDir + "/subdir", error);
 
226
  if(error.get())
 
227
    std::cerr << error->what() << std::endl;
 
228
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
229
 
 
230
  update_label_text();
 
231
}
 
232
 
 
233
 
 
234
 
 
235
void Window_Test::on_Button_AddSubDir()
 
236
{
 
237
  m_iSubDir++;
 
238
  
 
239
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
240
  m_refClient->add_dir(m_strMainDir + "/subdir");
 
241
  #else
 
242
  std::auto_ptr<Glib::Error> error;
 
243
  m_refClient->add_dir(m_strMainDir + "/subdir", Gnome::Conf::CLIENT_PRELOAD_NONE, error);
 
244
  if(error.get())
 
245
    std::cerr << error->what() << std::endl;
 
246
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
247
 
 
248
  update_label_text();
 
249
}
 
250
 
 
251
void Window_Test::on_Button_RemoveMainDir()
 
252
{
 
253
  m_iMainDir--;
 
254
 
 
255
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
256
  m_refClient->remove_dir(m_strMainDir);
 
257
  #else
 
258
  std::auto_ptr<Glib::Error> error;
 
259
  m_refClient->remove_dir(m_strMainDir, error);
 
260
  if(error.get())
 
261
    std::cerr << error->what() << std::endl;
 
262
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
263
 
 
264
  update_label_text();
 
265
}
 
266
 
 
267
void Window_Test::on_Button_AddMainDir()
 
268
{
 
269
  m_iMainDir++;
 
270
 
 
271
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
 
272
  m_refClient->add_dir(m_strMainDir);
 
273
  #else
 
274
  std::auto_ptr<Glib::Error> error;
 
275
  m_refClient->add_dir(m_strMainDir, Gnome::Conf::CLIENT_PRELOAD_NONE, error);
 
276
  if(error.get())
 
277
    std::cerr << error->what() << std::endl;
 
278
  #endif //GLIBMM_EXCEPTIONS_ENABLED
 
279
 
 
280
  update_label_text();
 
281
}
 
282
 
 
283
//notify callback.
 
284
//The Gtk::Entry* pEntry callback is extra. I used sigc::bind in the connection.
 
285
void Window_Test::Client_notify_func(guint cnxn_id, Gnome::Conf::Entry conf_entry, Gtk::Entry* pEntry)
 
286
{
 
287
  if(pEntry)
 
288
  {
 
289
    //gtk_signal_handler_block_by_data(GTK_OBJECT(pEntry->gtkobj()), m_refClient);
 
290
    //Our alternative to gtk_signal_handler_block_by_data(),
 
291
    //Because we couldn't have connected the signal handler with that data:
 
292
    m_bBlockEntryHandlers = true;
 
293
 
 
294
    pEntry->set_text(conf_entry.get_value().get_string());
 
295
 
 
296
    //gtk_signal_handler_unblock_by_data(GTK_OBJECT(pEntry->gtkobj()), m_refClient);
 
297
    m_bBlockEntryHandlers = false;
 
298
  }
 
299
}
 
300
 
 
301