| 1 |
/* Copyright (C) 1998-2003 The gtkmm Development Team |
| 2 |
* |
| 3 |
* This library is free software; you can redistribute it and/or |
| 4 |
* modify it under the terms of the GNU Lesser General Public |
| 5 |
* License as published by the Free Software Foundation; either |
| 6 |
* version 2.1 of the License, or (at your option) any later version. |
| 7 |
* |
| 8 |
* This library is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
* Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public |
| 14 |
* License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
_CONFIGINCLUDE(gtkmmconfig.h) |
| 18 |
|
| 19 |
#include <gtk/gtk.h> |
| 20 |
|
| 21 |
#include <sigc++/sigc++.h> |
| 22 |
#include <gdkmm/types.h> |
| 23 |
#include <glibmm/optioncontext.h> |
| 24 |
|
| 25 |
_DEFS(gtkmm,gtk) |
| 26 |
|
| 27 |
#m4 _PUSH(SECTION_CC_PRE_INCLUDES) |
| 28 |
#undef GTK_DISABLE_DEPRECATED |
| 29 |
#define GDK_DISABLE_DEPRECATION_WARNINGS 1 |
| 30 |
#m4 _POP() |
| 31 |
|
| 32 |
namespace Gtk |
| 33 |
{ |
| 34 |
|
| 35 |
_DEPRECATE_IFDEF_START |
| 36 |
|
| 37 |
class GTKMM_API Widget; |
| 38 |
class GTKMM_API Window; |
| 39 |
|
| 40 |
//********************************************************************** |
| 41 |
|
| 42 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 43 |
//Actually, I'd like to just keep these out of the alphabetical list. murrayc. |
| 44 |
|
| 45 |
|
| 46 |
/// KeySnooper Signal Class (internal) |
| 47 |
class GTKMM_API KeySnooperSig |
| 48 |
{ |
| 49 |
public: |
| 50 |
typedef sigc::slot<int, Widget*, GdkEventKey*> SlotType; |
| 51 |
|
| 52 |
sigc::connection connect(const SlotType& slot); |
| 53 |
|
| 54 |
protected: |
| 55 |
static int gtk_callback(GtkWidget* widget, GdkEventKey* event, gpointer data); |
| 56 |
}; |
| 57 |
|
| 58 |
#endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 59 |
|
| 60 |
_DEPRECATE_IFDEF_END |
| 61 |
|
| 62 |
|
| 63 |
//********************************************************************** |
| 64 |
|
| 65 |
/** Main application class. |
| 66 |
* Every application must have one of these objects. |
| 67 |
* It may not be global and must be the first gtkmm object created. |
| 68 |
* It is a singleton so declaring more than one will simply access the first |
| 69 |
* created. |
| 70 |
* |
| 71 |
* You would normally use this class in your main() function to initialize gtkmm |
| 72 |
* and optionally to give argc and argv to the GTK+ initialization. |
| 73 |
* After calling Gtk::Main::run(), you may use Gtk::Main::quit() to exit from |
| 74 |
* the application, or just pass your main window to run(), to make run() |
| 75 |
* return when that window closes. |
| 76 |
* |
| 77 |
* A minimal gtkmm application would be something like this: |
| 78 |
* @code |
| 79 |
* int main(int argc, char *argv[]) |
| 80 |
* { |
| 81 |
* Gtk::Main kit(argc, argv); |
| 82 |
* ... create some widgets and windows... |
| 83 |
* kit.run(window); |
| 84 |
* } |
| 85 |
* @endcode |
| 86 |
* |
| 87 |
* @deprecated Use Gtk::Application instead. |
| 88 |
*/ |
| 89 |
class GTKMM_API Main : public sigc::trackable |
| 90 |
{ |
| 91 |
public: |
| 92 |
|
| 93 |
_DEPRECATE_IFDEF_START |
| 94 |
//This offers the same functionality as gtk_init_with_args(): |
| 95 |
/** Scans the argument vector, and strips off all parameters parsed by GTK+ or your @a option_context. |
| 96 |
* Add a Glib::OptionGroup to the Glib::OptionContext to parse your own command-line arguments. |
| 97 |
* |
| 98 |
* Note: The argument strings themself won't be modified, although the |
| 99 |
* pointers to them might change. This makes it possible to create your |
| 100 |
* own argv of string literals, which have the type 'const char[]' in |
| 101 |
* standard C++. (You might need to use const_cast<>, though.) |
| 102 |
* |
| 103 |
* This function automatically generates nicely formatted |
| 104 |
* <tt>--help</tt> output. Note that your program will |
| 105 |
* be terminated after writing out the help output. |
| 106 |
* |
| 107 |
* @param argc a reference to the number of command line arguments. |
| 108 |
* @param argv a reference to the array of command line arguments. |
| 109 |
* @param option_context A Glib::OptionContext containing Glib::OptionGroups which described the command-line arguments taken by your program. |
| 110 |
* |
| 111 |
* @throw Glib::OptionError |
| 112 |
* |
| 113 |
* @deprecated Use Gtk::Application instead. |
| 114 |
*/ |
| 115 |
Main(int& argc, char**& argv, Glib::OptionContext& option_context); |
| 116 |
|
| 117 |
/** Scans the argument vector, and strips off all parameters known to GTK+. |
| 118 |
* Your application may then handle the remaining arguments. |
| 119 |
* |
| 120 |
* Note: The argument strings themself won't be modified, although the |
| 121 |
* pointers to them might change. This makes it possible to create your |
| 122 |
* own argv of string literals, which have the type 'const char[]' in |
| 123 |
* standard C++. (You might need to use const_cast<>, though.) |
| 124 |
* |
| 125 |
* @param argc a pointer to the number of command line arguments. |
| 126 |
* @param argv a pointer to the array of command line arguments. |
| 127 |
* @param set_locale Passing false prevents GTK+ from automatically calling |
| 128 |
* setlocale(LC_ALL, ""). You would want to pass false if you wanted to set |
| 129 |
* the locale for your program to something other than the user's locale, or |
| 130 |
* if you wanted to set different values for different locale categories. |
| 131 |
* |
| 132 |
* @deprecated Use Gtk::Application instead. |
| 133 |
*/ |
| 134 |
Main(int* argc, char*** argv, bool set_locale = true); |
| 135 |
|
| 136 |
/** Scans the argument vector, and strips off all parameters known to GTK+. |
| 137 |
* Your application may then handle the remaining arguments. |
| 138 |
* |
| 139 |
* @param argc a reference to the number of command line arguments. |
| 140 |
* @param argv a reference to the array of command line arguments. |
| 141 |
* @param set_locale Passing false prevents GTK+ from automatically calling |
| 142 |
* setlocale(LC_ALL, ""). You would want to pass false if you wanted to set |
| 143 |
* the locale for your program to something other than the user's locale, or |
| 144 |
* if you wanted to set different values for different locale categories. |
| 145 |
* |
| 146 |
* @deprecated Use Gtk::Application instead. |
| 147 |
*/ |
| 148 |
Main(int& argc, char**& argv, bool set_locale = true); |
| 149 |
|
| 150 |
/** Initialization without command-line arguments. |
| 151 |
* |
| 152 |
* @param set_locale Passing false prevents GTK+ from automatically calling |
| 153 |
* setlocale(LC_ALL, ""). You would want to pass false if you wanted to set |
| 154 |
* the locale for your program to something other than the user's locale, or |
| 155 |
* if you wanted to set different values for different locale categories. |
| 156 |
* |
| 157 |
* @deprecated Use Gtk::Application instead. |
| 158 |
*/ |
| 159 |
explicit Main(bool set_locale = true); |
| 160 |
|
| 161 |
/** |
| 162 |
* @deprecated Use Gtk::Application instead. |
| 163 |
*/ |
| 164 |
virtual ~Main(); |
| 165 |
|
| 166 |
/** Access to the one global instance of Gtk::Main. |
| 167 |
* |
| 168 |
* @deprecated Use Gtk::Application instead. |
| 169 |
*/ |
| 170 |
static Gtk::Main* instance(); |
| 171 |
|
| 172 |
/** Start the event loop. |
| 173 |
* This begins the event loop which handles events. No |
| 174 |
* events propagate until this has been called. It may be |
| 175 |
* called recursively to popup dialogs |
| 176 |
* |
| 177 |
* @deprecated Use Gtk::Application instead. |
| 178 |
*/ |
| 179 |
static void run(); |
| 180 |
|
| 181 |
/** Returns from the main loop when the window is closed. |
| 182 |
* When using this override, you should not use Gtk::Main::quit() to close |
| 183 |
* the application, but just call hide() on your Window class. |
| 184 |
* |
| 185 |
* @param window The window to show. This method will return when the window is hidden. |
| 186 |
* |
| 187 |
* @deprecated Use Gtk::Application instead. |
| 188 |
*/ |
| 189 |
static void run(Window& window); |
| 190 |
|
| 191 |
/** Makes the innermost invocation of the main loop return when it regains control. |
| 192 |
* |
| 193 |
* @deprecated Use Gtk::Application instead. |
| 194 |
*/ |
| 195 |
static void quit(); |
| 196 |
|
| 197 |
/** |
| 198 |
* @deprecated Use Gtk::Application instead. |
| 199 |
*/ |
| 200 |
static guint level(); |
| 201 |
|
| 202 |
_DEPRECATE_IFDEF_END |
| 203 |
|
| 204 |
//TODO: Move these general methods out of Main when we really remove Main, |
| 205 |
//one day when Gtk::Application can really do everything, which should probably |
| 206 |
//include handling OptionContext properly. murrayc. |
| 207 |
|
| 208 |
//This attempts to provide the same functionality as gtk_get_option_group(): |
| 209 |
/** Add a Glib::OptionGroup, for the commandline arguments recognized |
| 210 |
* by GTK+ and GDK, to a Glib::OptionContext, so that these commandline arguments will |
| 211 |
* be processed in addition to the existing commandline arguments specified by the Glib::OptionContext. |
| 212 |
* |
| 213 |
* You do not need to use this method if you pass your Glib::OptionContext to the Main constructor, because |
| 214 |
* it adds the gtk option group automatically. |
| 215 |
* |
| 216 |
* @param option_context Option Context to which the group will be added. |
| 217 |
* @param open_default_display Whether to open the default display when parsing the commandline arguments. |
| 218 |
* |
| 219 |
* @deprecated Use Gtk::Application instead. |
| 220 |
*/ |
| 221 |
static void add_gtk_option_group(Glib::OptionContext& option_context, bool open_default_display = true); |
| 222 |
|
| 223 |
|
| 224 |
/** Runs a single iteration of the main loop. |
| 225 |
* If no events are waiting to be processed GTK+ will block until the next event is noticed. |
| 226 |
* If you don't want to block then pass false for @a blocking or check if any events are pending with |
| 227 |
* pending() first. |
| 228 |
* |
| 229 |
* @param blocking Whether the caller must wait until the next event is noticed, or return immediately if there are no events. |
| 230 |
* @result true if quit() has been called for the innermost main loop. |
| 231 |
*/ |
| 232 |
static bool iteration(bool blocking = true); |
| 233 |
|
| 234 |
/** Checks if any events are pending. This can be used to update the GUI and invoke timeouts etc. while doing some time intensive computation. |
| 235 |
* |
| 236 |
* Example: Updating the GUI during a long computation. |
| 237 |
* @code |
| 238 |
* // computation going on |
| 239 |
* while( Gtk::Main::events_pending() ) |
| 240 |
* Gtk::Main::iteration(); |
| 241 |
* |
| 242 |
* // computation continued |
| 243 |
* @endcode |
| 244 |
* |
| 245 |
* @result true if any events are pending, false otherwise. |
| 246 |
*/ |
| 247 |
static bool events_pending(); |
| 248 |
|
| 249 |
_DEPRECATE_IFDEF_START |
| 250 |
|
| 251 |
/** KeySnooper signal |
| 252 |
* Allows you to channel keypresses to a signal handler |
| 253 |
* without registering with the widget. |
| 254 |
* |
| 255 |
* @return KeySnooperSig A Signal to which you can connect a sigc::slot< int, Widget *, GdkEventKey * > |
| 256 |
* |
| 257 |
* It is the responsibility of the snooper to pass the keypress |
| 258 |
* to the widget, however, care must be taken that the keypress is |
| 259 |
* not passed twice. |
| 260 |
* |
| 261 |
* @deprecated Key snooping should not be done. Events should be handled by widgets. |
| 262 |
*/ |
| 263 |
static KeySnooperSig& signal_key_snooper(); |
| 264 |
_DEPRECATE_IFDEF_END |
| 265 |
|
| 266 |
/** Initialize the table of wrap_new functions. |
| 267 |
* This doesn't need an instance of Gtk::Main. |
| 268 |
* This would usually only be used by the init() methods of libraries that depend on gtkmm. |
| 269 |
*/ |
| 270 |
static void init_gtkmm_internals(); |
| 271 |
|
| 272 |
protected: |
| 273 |
|
| 274 |
_DEPRECATE_IFDEF_START |
| 275 |
void init(int* argc, char*** argv, bool set_locale); |
| 276 |
// TODO: implement this to use the new Glib::OptionEntry argument parsing classes. |
| 277 |
//void init(int* argc, char*** argv, const std::string& parameter_string, const std::vector<const Glib::OptionEntry&>& entries, const std::string& translation_domain); |
| 278 |
|
| 279 |
virtual void run_impl(); |
| 280 |
virtual void quit_impl(); |
| 281 |
virtual guint level_impl(); |
| 282 |
virtual bool iteration_impl(bool blocking); |
| 283 |
virtual bool events_pending_impl(); |
| 284 |
|
| 285 |
// Signal handlers: |
| 286 |
virtual void on_window_hide(); |
| 287 |
|
| 288 |
// Signal proxies: |
| 289 |
static KeySnooperSig signal_key_snooper_; |
| 290 |
|
| 291 |
private: |
| 292 |
static Main* instance_; |
| 293 |
_DEPRECATE_IFDEF_END |
| 294 |
}; |
| 295 |
|
| 296 |
} // namespace Gtk |