Nemiver  0.3
nmv-ui-utils.h
Go to the documentation of this file.
1 /* -*- Mode: C++; indent-tabs-mode:nil; c-basic-offset:4; -*- */
2 
3 // Author: Dodji Seketeli
4 
5 /*
6  *This file is part of the Nemiver Project.
7  *
8  *Nemiver is free software; you can redistribute
9  *it and/or modify it under the terms of
10  *the GNU General Public License as published by the
11  *Free Software Foundation; either version 2,
12  *or (at your option) any later version.
13  *
14  *Nemiver is distributed in the hope that it will
15  *be useful, but WITHOUT ANY WARRANTY;
16  *without even the implied warranty of
17  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *See the GNU General Public License for more details.
19  *
20  *You should have received a copy of the
21  *GNU General Public License along with Nemiver;
22  *see the file COPYING.
23  *If not, write to the Free Software Foundation,
24  *Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  *
26  *See COPYRIGHT file copyright information.
27  */
28 #ifndef __NMV_UI_UTILS_H__
29 #define __NMV_UI_UTILS_H__
30 
31 #include "common/nmv-api-macros.h"
32 #include <gtkmm.h>
33 #include "common/nmv-env.h"
34 #include "common/nmv-ustring.h"
36 
37 #ifndef NEMIVER_CATCH
38 #define NEMIVER_CATCH \
39 } catch (Glib::Exception &e) { \
40  LOG_ERROR (std::string ("caught exception: '") + e.what () + "'"); \
41  nemiver::ui_utils::display_error_not_transient (e.what ()); \
42 } catch (std::exception &e) { \
43  LOG_ERROR (std::string ("caught exception: '") + e.what () + "'"); \
44  nemiver::ui_utils::display_error_not_transient (e.what ()); \
45 } catch (...) { \
46  LOG_ERROR ("caught unknown exception"); \
47  nemiver::ui_utils::display_error_not_transient ("An unknown error occured"); \
48 }
49 #endif
50 
51 #ifndef NEMIVER_CATCH_AND_RETURN
52 #define NEMIVER_CATCH_AND_RETURN(a_value) \
53 } catch (Glib::Exception &e) { \
54  LOG_ERROR (std::string ("caught exception: '") + e.what () + "'"); \
55  nemiver::ui_utils::display_error_not_transient (e.what ()); \
56  return a_value; \
57 } catch (std::exception &e) { \
58  LOG_ERROR (std::string ("caught exception: '") + e.what () + "'"); \
59  nemiver::ui_utils::display_error_not_transient (e.what ()); \
60  return a_value; \
61 } catch (...) { \
62  LOG_ERROR ("Caught unknown exception"); \
63  nemiver::ui_utils::display_error_not_transient ("An unknown error occured"); \
64  return a_value; \
65 }
66 #endif
67 
69 
70 namespace nemiver {
71 namespace ui_utils {
72 
73 class ActionEntry {
74 
75 public:
76  enum Type {
79  };
80 
82  Gtk::StockID m_stock_id;
85  sigc::slot<void> m_activate_slot;
89 
90  Glib::RefPtr<Gtk::Action> to_action () const
91  {
92  Glib::RefPtr<Gtk::Action> result;
93  switch (m_type) {
94  case DEFAULT:
95  if (m_stock_id.get_string () != "") {
96  result =
97  Gtk::Action::create (m_name, m_stock_id,
99  } else {
100  result =
101  Gtk::Action::create (m_name, m_label, m_tooltip);
102  }
103  break;
104  case TOGGLE:
105  if (m_stock_id.get_string () != "") {
106  result =
107  Gtk::ToggleAction::create (m_name, m_stock_id,
108  m_label, m_tooltip);
109  } else {
110  result =
111  Gtk::ToggleAction::create (m_name,
112  m_label,
113  m_tooltip);
114  }
115  break;
116 
117  default:
118  THROW ("should never reach this point");
119  }
120 
121  if (result)
122  result->set_is_important (m_is_important);
123 
124  return result;
125  }
126 };//end class ActionEntry
127 
129  (const ActionEntry a_tab[],
130  int a_num_entries,
131  Glib::RefPtr<Gtk::ActionGroup> &a_group);
132 
133 NEMIVER_API int display_info (Gtk::Window &a_parent_window,
134  const common::UString &a_message);
135 
136 NEMIVER_API int display_warning (Gtk::Window &a_parent_window,
137  const common::UString &a_message);
138 
139 NEMIVER_API int display_error (Gtk::Window &a_parent_window,
140  const common::UString &a_message);
141 
142 NEMIVER_API int display_error_not_transient (const UString &a_message);
143 
144 NEMIVER_API int ask_yes_no_question (Gtk::Window &a_parent_window,
145  const common::UString &a_message);
146 
147 NEMIVER_API int ask_yes_no_question (Gtk::Window &a_parent_window,
148  const common::UString &a_message,
149  bool a_propose_dont_ask_question,
150  bool &a_dont_ask_this_again);
151 
152 NEMIVER_API int ask_yes_no_cancel_question (Gtk::Window &a_parent_window,
153  const common::UString &a_message);
154 
155 NEMIVER_API bool ask_user_to_select_file (Gtk::Window &a_parent,
156  const UString &a_file_name,
157  const UString &a_default_dir,
158  UString &a_selected_file_path);
159 
160 NEMIVER_API bool find_file_or_ask_user (Gtk::Window &a_parent_window,
161  const UString& a_file_path,
162  const list<UString> &a_where_to_look,
163  list<UString> &a_session_dirs,
164  map<UString, bool> &a_ignore_paths,
165  bool a_ignore_if_not_found,
166  UString& a_absolute_path);
167 
168 
169 bool find_file_and_read_line (Gtk::Window &a_parent_window,
170  const UString &a_file_path,
171  const list<UString> &a_where_to_look,
172  list<UString> &a_sess_dirs,
173  map<UString, bool> &a_ignore_paths,
174  int a_line_number,
175  string &a_line);
176 
177 template <class T>
178 T*
179 get_widget_from_gtkbuilder (const Glib::RefPtr<Gtk::Builder> &a_gtkbuilder,
180  const UString &a_widget_name)
181 {
182  T *widget;
183  a_gtkbuilder->get_widget (a_widget_name, widget);
184  if (!widget) {
185  THROW ("couldn't find widget '"
186  + a_widget_name);
187  }
188  return widget;
189 }
190 
191 
192 struct WidgetRef {
193  void operator () (Gtk::Widget *a_widget)
194  {
195  if (a_widget) {
196  a_widget->reference ();
197  }
198  }
199 };//end struct WidgetRef
200 
201 struct WidgetUnref {
202  void operator () (Gtk::Widget *a_widget)
203  {
204  if (a_widget) {
205  a_widget->unreference ();
206  }
207  }
208 };//end struct WidgetUnref
209 }//end namespace ui_utils
210 }//end namespace nemiver
211 
212 #endif// __NMV_UI_UTILS_H__
213 
nmv-safe-ptr-utils.h
nemiver::ui_utils::display_warning
NEMIVER_API int display_warning(Gtk::Window &a_parent_window, const common::UString &a_message)
nemiver
Definition: nmv-address.h:31
nemiver::ui_utils::ActionEntry
Definition: nmv-ui-utils.h:73
THROW
#define THROW(a_reason)
Definition: nmv-exception.h:99
nmv-ustring.h
NEMIVER_API
#define NEMIVER_API
Definition: nmv-api-macros.h:53
nmv-api-macros.h
nemiver::ui_utils::WidgetRef::operator()
void operator()(Gtk::Widget *a_widget)
Definition: nmv-ui-utils.h:193
nemiver::ui_utils::WidgetRef
Definition: nmv-ui-utils.h:192
nemiver::ui_utils::ActionEntry::to_action
Glib::RefPtr< Gtk::Action > to_action() const
Definition: nmv-ui-utils.h:90
nemiver::common::UString
Definition: nmv-ustring.h:45
nemiver::ui_utils::ActionEntry::TOGGLE
@ TOGGLE
Definition: nmv-ui-utils.h:78
nemiver::ui_utils::display_error_not_transient
NEMIVER_API int display_error_not_transient(const UString &a_message)
nemiver::ui_utils::ActionEntry::m_is_important
bool m_is_important
Definition: nmv-ui-utils.h:88
nemiver::ui_utils::WidgetUnref
Definition: nmv-ui-utils.h:201
nemiver::ui_utils::ActionEntry::Type
Type
Definition: nmv-ui-utils.h:76
nemiver::ui_utils::ask_yes_no_question
NEMIVER_API int ask_yes_no_question(Gtk::Window &a_parent_window, const common::UString &a_message)
nemiver::ui_utils::display_info
NEMIVER_API int display_info(Gtk::Window &a_parent_window, const common::UString &a_message)
nemiver::ui_utils::display_error
NEMIVER_API int display_error(Gtk::Window &a_parent_window, const common::UString &a_message)
nemiver::ui_utils::find_file_and_read_line
bool find_file_and_read_line(Gtk::Window &a_parent_window, const UString &a_file_path, const list< UString > &a_where_to_look, list< UString > &a_sess_dirs, map< UString, bool > &a_ignore_paths, int a_line_number, string &a_line)
nmv-env.h
nemiver::ui_utils::ActionEntry::m_activate_slot
sigc::slot< void > m_activate_slot
Definition: nmv-ui-utils.h:85
nemiver::ui_utils::ActionEntry::m_accel
common::UString m_accel
Definition: nmv-ui-utils.h:87
nemiver::ui_utils::ActionEntry::m_stock_id
Gtk::StockID m_stock_id
Definition: nmv-ui-utils.h:82
nemiver::ui_utils::ActionEntry::m_type
Type m_type
Definition: nmv-ui-utils.h:86
nemiver::ui_utils::ActionEntry::m_tooltip
common::UString m_tooltip
Definition: nmv-ui-utils.h:84
nemiver::ui_utils::find_file_or_ask_user
NEMIVER_API bool find_file_or_ask_user(Gtk::Window &a_parent_window, const UString &a_file_path, const list< UString > &a_where_to_look, list< UString > &a_session_dirs, map< UString, bool > &a_ignore_paths, bool a_ignore_if_not_found, UString &a_absolute_path)
nemiver::ui_utils::ask_yes_no_cancel_question
NEMIVER_API int ask_yes_no_cancel_question(Gtk::Window &a_parent_window, const common::UString &a_message)
nemiver::ui_utils::ActionEntry::m_name
common::UString m_name
Definition: nmv-ui-utils.h:81
nemiver::ui_utils::ask_user_to_select_file
NEMIVER_API bool ask_user_to_select_file(Gtk::Window &a_parent, const UString &a_file_name, const UString &a_default_dir, UString &a_selected_file_path)
nemiver::ui_utils::ActionEntry::DEFAULT
@ DEFAULT
Definition: nmv-ui-utils.h:77
nemiver::ui_utils::add_action_entries_to_action_group
NEMIVER_API void add_action_entries_to_action_group(const ActionEntry a_tab[], int a_num_entries, Glib::RefPtr< Gtk::ActionGroup > &a_group)
nemiver::ui_utils::get_widget_from_gtkbuilder
T * get_widget_from_gtkbuilder(const Glib::RefPtr< Gtk::Builder > &a_gtkbuilder, const UString &a_widget_name)
Definition: nmv-ui-utils.h:179
nemiver::ui_utils::WidgetUnref::operator()
void operator()(Gtk::Widget *a_widget)
Definition: nmv-ui-utils.h:202
nemiver::ui_utils::ActionEntry::m_label
common::UString m_label
Definition: nmv-ui-utils.h:83