Fix synfig::ChangleLocale class.
[synfig.git] / synfig-core / src / synfig / general.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file general.h
3 **      \brief General macros, classes, and procedure declarations
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_GENERAL_H
26 #define __SYNFIG_GENERAL_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include <ETL/stringf>
31 #include "string.h"
32 #include "version.h"
33 #ifdef ENABLE_NLS
34  #include <locale.h>
35  #include <libintl.h>
36 #endif
37
38 /* === M A C R O S ========================================================= */
39
40 #ifdef ENABLE_NLS
41 #define _(x) dgettext("synfig",x)
42 #define gettext_noop(x) x
43 #define N_(x) gettext_noop(x)
44 #else
45 #define dgettext(a,x) (x)
46 #define _(x) (x)
47 #define N_(x) (x)
48 #endif
49
50 #define SYNFIG_COPYRIGHT "Copyright (c) 2001-2005 Robert B. Quattlebaum Jr., Adrian Bentley"
51
52
53 #ifdef _DEBUG
54 #ifdef __FUNC__
55 #define DEBUGPOINT()    synfig::warning(etl::strprintf(__FILE__":"__FUNC__":%d DEBUGPOINT",__LINE__))
56 #define DEBUGINFO(x)    synfig::warning(etl::strprintf(__FILE__":"__FUNC__":%d:DEBUGINFO:",__LINE__)+x)
57 #else
58 #define DEBUGPOINT()    synfig::warning(etl::strprintf(__FILE__":%d DEBUGPOINT",__LINE__))
59 #define DEBUGINFO(x)    synfig::warning(etl::strprintf(__FILE__":%d:DEBUGINFO:",__LINE__)+x)
60 #endif
61
62 #else
63 #define DEBUGPOINT()
64 #define DEBUGINFO(x)
65 #endif
66
67 /* === C L A S S E S & S T R U C T S ======================================= */
68
69 namespace synfig {
70
71 class ChangeLocale {
72         const String previous;
73         const int category;
74 public:
75         ChangeLocale(int category, const char *locale):
76         // This backups the old locale
77         previous(setlocale(category,NULL)),category(category)
78         {
79                 // This effectively changes the locale
80                 setlocale(category, locale);
81         }
82         ~ChangeLocale() {
83                 // This restores the locale
84                 setlocale(category,previous.c_str());
85         }
86 };
87
88 /*!     \class ProgressCallback
89 **      \todo writeme
90 */
91 class ProgressCallback
92 {
93 public:
94
95         virtual ~ProgressCallback() { }
96         virtual bool task(const String &/*task*/) { return true; }
97         virtual bool error(const String &/*task*/) { return true; }
98         virtual bool warning(const String &/*task*/) { return true; }
99         virtual bool amount_complete(int /*current*/, int /*total*/) { return true; }
100
101         virtual bool valid() const { return true; }
102 };
103
104 typedef ProgressCallback ProgressManager;
105
106 /*!     \class SuperCallback
107 **      \todo writeme
108 */
109 class SuperCallback : public ProgressCallback
110 {
111         ProgressCallback *cb;
112         int start,end,tot;
113         int w;
114 public:
115
116         SuperCallback() { cb=NULL; }
117         SuperCallback(ProgressCallback *cb,int start, int end, int total):cb(cb),start(start),end(end),tot(total)
118         {
119                 //make sure we don't "inherit" if our subcallback is invalid
120                 if(!cb || !cb->valid())
121                         cb = NULL;
122                 w=end-start;
123         }
124         virtual bool task(const String &task) { if(cb)return cb->task(task); return true; }
125         virtual bool error(const String &task) { if(cb)return cb->error(task); return true; }
126         virtual bool warning(const String &task) { if(cb)return cb->warning(task); return true; }
127         virtual bool amount_complete(int cur, int total) { if(cb)return cb->amount_complete(start+cur*w/total,tot); return true; }
128
129         virtual bool valid() const { return cb != 0; }
130 };
131
132
133 /*
134 extern bool add_to_module_search_path(const std:string &path);
135 extern bool add_to_config_search_path(const std:string &path);
136 */
137
138 //! Shutdown the synfig environment
139 extern void shutdown();
140
141 //! Reports an error
142 /*! Call this when an error occurs, describing what happened */
143 extern void error(const char *format,...);
144 extern void error(const String &str);
145
146 //! Reports a warning
147 /*! Call this when something questionable occurs, describing what happened */
148 extern void warning(const char *format,...);
149 extern void warning(const String &str);
150
151 //! Reports some information
152 /*! Call this to report various information. Please be sparse... */
153 extern void info(const char *format,...);
154 extern void info(const String &str);
155
156 }; // END of namespace synfig
157
158 /* === E N D =============================================================== */
159
160 #endif