HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AP_Interface.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: AP_Interface.h ( SI Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __AP_Interface__
12 #define __AP_Interface__
13 
14 #include "SI_API.h"
15 #include <stdio.h>
16 
17 #include <UI/UI_Object.h>
18 
19 #include <UT/UT_Error.h>
20 #include <UT/UT_Signal.h>
21 #include <UT/UT_ValArray.h>
22 #include <UT/UT_String.h>
23 #include <UT/UT_WorkBuffer.h>
24 #include <UT/UT_SharedPtr.h>
25 #include <UT/UT_UniquePtr.h>
26 
27 #include <utility>
28 
29 class UT_Url;
30 
31 class AP_Interface;
32 class UI_Value;
33 class UI_Manager;
34 template<typename T> class UT_SymbolMap;
35 class UI_Feel;
36 class UI_KeyDelegate;
39 class UI_Window;
43 
44 /// Base class for interfaces.
45 /// This class' main responsibility is to maintain hash tables of named
46 /// UI_Feel "object symbol" and UI_Value "value symbol" items.
47 class SI_API AP_Interface : public UI_Object {
48 public:
49  AP_Interface();
50 
51  /// Construct with given name and predefined value names with corresponding
52  /// interests. The actual values are not created until wireInteface() is
53  /// called. Typically, initApplication() is first called to create the
54  /// the values and then wireInterface() is called afterwards to attach the
55  /// methods to the corresponding values.
56  AP_Interface(const char *myname,
57  const char *const *names,
58  UI_EventMethod const *methods);
59 
60  ~AP_Interface() override;
61 
62  const char *className() const override;
63 
64  /// Return the name passed in the constructor
65  const UT_String &getName() const { return myName; }
66 
67  /// Attach values names to the methods passed in the constructor.
68  /// The actual UI_Value's are typically created by calling
69  /// initApplication() first. If the values do not exist yet, then they are
70  /// automatically created into uims (if not nullptr), else they will
71  /// created in this object.
72  ///
73  /// This method also calls processKeyBindingProxyRequests().
74  void wireInterface(UI_Manager *uims);
75 
76  // Removes interests from all the value names passed in the constructor.
77  void unwireInterface(UI_Manager *uims);
78 
79  /// Turns a .ui file into UI gadgets. If quiet is true, no errors or
80  /// warnings are printed. If defines is specified, this allows you to
81  /// pass CPP style symbols to the parser, in the form "SYMBOL=VALUE".
82  /// =VALUE may be omitted.
83  bool readUIFile(const char *ui_filename,
84  bool quiet = false);
85 
86  /// Create new preference file given its base name using standard rules
87  /// (eg. in $HOME/houdiniX.Y). This may return nullptr if a .nosave file is
88  /// found.
89  static FILE *createPreferenceFile(const char *filename);
90 
91  /// Read the given preference file given its base name using standard
92  /// rules. The values will be initialized according to the parsed file into
93  /// this object.
94  bool readPreferenceFile(const char *pref_filename,
95  bool quiet = false);
96 
97  /// Set the named value symbol to the corresponding pointer. If warn is
98  /// true, then a warning will be issued if the name already exists as an
99  /// object or key delegate symbol, or if the value symbol name already
100  /// previously existed.
101  void setValueSymbol(const char *symbol, UI_Value *value,
102  bool warn = true);
103 
104  /// Find the UI_Value for the given named value symbol
105  UI_Value *findValueSymbol(const char *symbol) const;
106 
107  /// Get the UI_Value for the given named value symbol, creating it if it
108  /// doesn't exist yet. If 'create' is false, then getValueSymbol() is the
109  /// same as findValueSymbol().
110  UI_Value *getValueSymbol(const char *symbol, int create = 1);
111 
112  /// Returns all the Symbol/UI_Value's pairs from the ValueSymbol table
113  /// return array will contain nullptr values if its symbol doesn't exist.
114  int getValueSymbols(UT_StringArray &symbols,
116 
117  /// Set the named object symbol to the corresponding pointer. If warn is
118  /// true, then a warning will be issued if the name already exists as a
119  /// value or key delegate symbol, or if the object symbol name already
120  /// previously existed.
121  void setObjectSymbol(const char *symbol, UI_Object *who,
122  int warn = 1);
123 
124  /// Find the UI_Object for the given object symbol name. Returns nullptr if
125  /// it doesn't exist.
126  UI_Object *getObjectSymbol(const char *symbol) const;
127 
128  /// Find the UI_Feel for the given object symbol name. Returns nullptr if
129  /// it doesn't exist. This is equivalent to findObject<UI_Feel*>(symbol).
130  UI_Feel *getFeelSymbol(const char *symbol) const;
131 
132  /// Find the UI_Feel for the given value pointer. Returns nullptr if
133  /// it doesn't exist.
134  UI_Feel *findValueFeel( UI_Value * value) const;
135 
136  /// Returns all the Symbol/UI_Feel's pairs from the FeelSymbol table
137  /// return array will contain nullptr values if its symbol doesn't exist.
138  int getFeelSymbols(UT_StringArray &symbols,
139  UT_Array<UI_Feel *> &feels) const;
140 
141  /// Set the named key delegate symbol to the corresponding pointer. If warn
142  /// is true, then a warning will be issued if the name already exists as an
143  /// object or value symbol, or if the key delegate symbol name already
144  /// previously existed.
145  void setKeyDelegateSymbol(const char *symbol,
146  UI_KeyDelegateSPtr delegate,
147  bool warn = true);
148 
149  /// Returns whether the current key delegate symbol is defined.
150  bool hasKeyDelegateSymbol(const char *symbol) const;
151 
152  /// Find the UI_KeyDelegate for the given named key delegate symbol.
153  UI_KeyDelegateSPtr findKeyDelegateSymbol(const char *symbol) const;
154 
155  /// Get the UI_KeyDelegate for the given named key delegate symbol,
156  /// creating it if it doesn't exist yet. If 'create' is false, then
157  /// getKeyDelegateSymbol() is the same as findKeyDelegateSymbol().
158  UI_KeyDelegateSPtr getKeyDelegateSymbol(const char *symbol,
159  int create = 1);
160 
161  /// Delete the entry for the given object symbol name
162  void removeObjectSymbol(const char *symbol);
163 
164  /// Delete the entry for the given value symbol name
165  void removeValueSymbol(const char *symbol);
166 
167  /// Find the pointer for the given object symbol name, dynamically casted
168  /// to the given template type.
169  template<typename T>
170  T *findObject(const char *name) const
171  {
172  return dynamic_cast<T *>(getObjectSymbol(name));
173  }
174 
175  /// Find the pointer for the given value symbol name, dynamically casted
176  /// to the given template type.
177  template<typename T>
178  T *findValue(const char *name) const
179  {
180  return dynamic_cast<T *>(findValueSymbol(name));
181  }
182 
183  /// This class maintains a table of requests to assign some feels as
184  /// binding proxies for other feels.
185  ///
186  /// Such requests often originate during the parsing of .ui files that
187  /// occurs in subclass initApplication() overrides. These requests are
188  /// identified by a name, and the order in which the source and references
189  /// are registered does not matter.
190  ///
191  /// The pointers to the feels are only temporarily stored until a call to
192  /// processKeyBindingProxyRequests() occurs. At the moment this occurs as
193  /// part of the wireInterface() method.
194  /// @{
195  void setKeyBindingProxySrc(const char *symbol, UI_Feel *src,
196  bool warn = true);
197  void addKeyBindingProxyRef(const char *symbol, UI_Feel *ref,
198  bool warn = true);
199  /// @}
200 
201  /// Initialize this object. This method is intended to be overriden by
202  /// subclasses since the base class implementation is empty. The subclass
203  /// typically calls readUIFile() here to create the interface which
204  /// populates the object and value symbols.
205  virtual void initApplication(UI_Manager *uims,
206  int argc,
207  const char **argv);
208 
209  // The outputUsage() method should simply output the usage text to
210  // the standard error stream.
211  // The application is terminated after invoking this method.
212  virtual void outputUsage(int argc, const char **argv) const;
213 
214  /// Resets this object by calling unwireInterface() and then destroying all
215  /// objects and values.
216  void resetApplication();
217 
218  void handleEvent(UI_Event *event) override;
219 
220  // These are methods which are called only if the APP is "the"
221  // registered main application in the program. By default, they
222  // do nothing.
223  virtual int saveAppData(const char *filename); // ret 1 on success
224  virtual int loadAppState(); // Allows app to load context
225  virtual int saveAppState(); // Allows app to save context
226  virtual void saveOnCoreDump(); // To do stuff when...yikes!
227  virtual bool getCoreDumpFileName(UT_WorkBuffer &name);
228 
229  // Utility methods for managing the common ui.pref file
230  // Load the UI pref file; should be used by Main.C only.
231  bool loadUIInitPrefs();
232  // Save the current UI state to the pref file. The layout level is
233  // the desired level to be saved.
234  void saveUIInitPrefs(
235  const char * layout_level,
236  fpreal uiscale,
237  int playbarui);
238  // Restore UI prefs from file. Similar to load but does not explicitly
239  // set the layout level, which can only be done by Main.C; so this method
240  // is used everywhere else to load the prefs from file.
241  bool restoreUIInitPrefs();
242 
243  /// Menu items and their mapping for file chooser style preference.
244  static const std::pair<const char *, int> theFileChooserStyleMap[];
245 
246  // Helper methods to convert enums to labels, and vice versa.
247  static int mapStrToInt(const char *label,
248  const std::pair<const char *, int> *map);
249  static const char * mapIntToStr(int index,
250  const std::pair<const char *, int> *map);
251 
252  // If a function key is pressed and a script is attached to it,
253  // this will return the script.
254  //
255  bool getFunctionKeyScript(int key, UI_Event *event,
256  UT_String &script);
257 
258  // These static methods will get/set the exit code that the UI application
259  // will exit with. The default exit code is 0. Note that these methods
260  // are implemented in AP_Main.C.
261  static void setAppExitCode(int exit_code);
262  static int getAppExitCode();
263 
264  /// Returns @c true if the application is exiting.
265  static bool isAppExiting();
266 
267  // Returns the desktop specified by the -desktop argument.
268  // Returns nullptr if no such argument was specified.
269  static const char *getStartupDesktop();
270 
271  // Helper class for being able to call the private installSignalHandlers().
272  class InstallSignalHandlersHelper;
273 
274 protected:
275  static void setTheMainApplication(AP_Interface *app);
276  static AP_Interface *getMainApplication() { return theMainApplication; }
277  static void clearTheMainApplication();
278 
280  const char * const *myValueNames;
282 
283  UI_NamedValueMap *getValueTable() const { return myValueTable; }
284  UI_NamedObjectMap *getObjectTable() const { return myObjectTable; }
285 
286  // Processes any registered binding proxy requests. After this call
287  // the key binding proxy request table will be cleared.
288  void processKeyBindingProxyRequests();
289 
290  // Updates the given UI_Window object using the -geometry or pref file
291  static void loadWindowGeometry(
292  UI_Window *window,
293  bool default_maximized,
294  bool use_pref);
295 
296  // Returns true if the -geometry arg was parsed out and false otherwise.
297  // NOTE: The values are returned in pixels!
298  static bool getWindowGeometry(int &width, int &height,
299  int &left, int &bottom,
300  bool &maximized,
301  bool use_pref);
302  static bool getSaveWindowGeoPref();
303  static void saveWindowGeoPref();
304  static bool loadWindowGeoPref(
305  int &w, int &h, int &left, int &bottom,
306  bool &maximized);
307 
308 private:
309  class si_CrashHandler;
310  friend class si_CrashHandler;
311 
312  // WARNING: The installSignalHandlers() method calls UTgetInterrupt(), so
313  // should not be called before the call to UTsetInterrupt().
314  static void installSignalHandlers();
315  static void getSignalList(UT_Array<int> &signals);
316 
317  void assignUIInitPrefs();
318 
319  static const char *mainApplicationName();
320 
321  static void createCrashLog(UTsignalHandlerArg sig_arg,
322  const char *appname, const char *filename);
323  static void terminateSignalHandler(UTsignalHandlerArg sig_arg);
324  static bool coredumpHandler(UTsignalHandlerArg sig_arg);
325  static void coreDumpChaser(UTsignalHandlerArg sig_arg);
326  static void installCallbacks();
327  static void powerFailHandlerSignalHandler(UTsignalHandlerArg);
328  static void powerFailHandler(
329  UT_String &saved_file_name,
330  const UT_StringRef &details);
331  static void terminationWarning(uint nseconds);
332 
333  static AP_Interface *theMainApplication;
334 
335  class ProxyRequest
336  {
337  public:
338  ProxyRequest() : myTarget(nullptr) {}
339 
340  UI_Feel *myTarget;
341  UT_Array<UI_Feel *> myReferences;
342  };
343  typedef UT_SymbolMap<ProxyRequest> NamedProxyRequestMap;
344 
345 private:
346  UI_NamedValueMap *myValueTable;
347  UI_NamedObjectMap *myObjectTable;
348  UI_NamedKeyDelegateMap *myKeyDelegateTable;
349  NamedProxyRequestMap *myKeyBindingProxyRequestTable;
350 };
351 
352 
353 /// Base class for bootstrapping code for executable applications, with
354 /// the main UI implemented by an AP_Interface.
355 ///
356 /// Instances are created as static objects so that the constructor runs
357 /// during static initialization before the main() function in AP_Main.C.
358 ///
359 /// The createApplication() method will be called on any existing bootstrap
360 /// objects during the execution of main() to allocate an AP_Interface that
361 /// main() will initialize and wire, and finally destroy before terminating.
363 {
364 public:
365  AP_Bootstrap();
366  virtual ~AP_Bootstrap();
367 
368  virtual AP_InterfaceUPtr createApplication() = 0;
369 
370 protected:
371  //
372  // Set the application to run in the fore or background. This MUST
373  // be called from a static constructor (i.e. before main is run).
374  //
375  static void setAppForeground();
376 };
377 
378 
379 // A convenience template for a trivial bootstrapper subclass that creates
380 // an application with a trivial constructor.
381 template <typename APP_T>
383 {
384 public:
386  { return UTmakeUnique<APP_T>(); }
387 };
388 
389 extern void APregister (AP_Interface *app);
390 extern void APderegister(AP_Interface *app);
391 
392 extern void APregister (AP_Bootstrap *bootstrap);
393 extern void APderegister(AP_Bootstrap *bootstrap);
394 
395 // These template functions allow you to get & cast to the appropriate
396 // object/value type in one step, ie:
397 //
398 // UI_Button *button = UIgetObject<UI_Button>(this, "button.gad");
399 // UI_StringList *menu = UIgetValue<UI_StringList>(this, "rmb.menu");
400 
401 template<class ObjectClass> inline
402 ObjectClass *SIgetObject(const AP_Interface *app, const char *name)
403 {
404  return dynamic_cast<ObjectClass *>(app->getObjectSymbol(name));
405 }
406 
407 template<class ValueClass> inline
408 ValueClass *SIgetValue(const AP_Interface *app, const char *name)
409 {
410  return dynamic_cast<ValueClass *>(app->findValueSymbol(name));
411 }
412 
413 #endif
ValueClass * SIgetValue(const AP_Interface *app, const char *name)
Definition: AP_Interface.h:408
GT_API const UT_StringHolder filename
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
T * findValue(const char *name) const
Definition: AP_Interface.h:178
#define SI_API
Definition: SI_API.h:10
GLint left
Definition: glcorearb.h:2005
AP_InterfaceUPtr createApplication() override
Definition: AP_Interface.h:385
void APregister(AP_Interface *app)
UT_SymbolMap< UI_Value * > UI_NamedValueMap
Definition: AP_Interface.h:39
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
struct _cl_event * event
Definition: glcorearb.h:2961
virtual void handleEvent(UI_Event *event)
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
GLint ref
Definition: glcorearb.h:124
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
const UT_String & getName() const
Return the name passed in the constructor.
Definition: AP_Interface.h:65
UT_UniquePtr< AP_Interface > AP_InterfaceUPtr
Definition: AP_Interface.h:38
static AP_Interface * getMainApplication()
Definition: AP_Interface.h:276
const char *const * myValueNames
Definition: AP_Interface.h:280
Definition: UT_Url.h:22
UI_EventMethod const * myMethods
Definition: AP_Interface.h:281
GLuint const GLchar * name
Definition: glcorearb.h:786
GLint GLint bottom
Definition: glcorearb.h:2005
UT_SymbolMap< UI_Object * > UI_NamedObjectMap
Definition: AP_Interface.h:41
UI_NamedObjectMap * getObjectTable() const
Definition: AP_Interface.h:284
UI_Object * getObjectSymbol(const char *symbol) const
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
fpreal64 fpreal
Definition: SYS_Types.h:277
T * findObject(const char *name) const
Definition: AP_Interface.h:170
GLuint index
Definition: glcorearb.h:786
void(UI_Object::* UI_EventMethod)(UI_Event *)
Definition: UI_Object.h:36
virtual const char * className() const
Definition: UI_Object.h:87
GLint GLsizei width
Definition: glcorearb.h:103
UT_String myName
Definition: AP_Interface.h:279
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
Definition: core.h:1131
UI_Value * findValueSymbol(const char *symbol) const
Find the UI_Value for the given named value symbol.
UT_SymbolMap< UI_KeyDelegateSPtr > UI_NamedKeyDelegateMap
Definition: AP_Interface.h:42
unsigned int uint
Definition: SYS_Types.h:45
void APderegister(AP_Interface *app)
UI_NamedValueMap * getValueTable() const
Definition: AP_Interface.h:283
GLenum src
Definition: glcorearb.h:1793
UT_SharedPtr< UI_KeyDelegate > UI_KeyDelegateSPtr
Definition: AP_Interface.h:37
ObjectClass * SIgetObject(const AP_Interface *app, const char *name)
Definition: AP_Interface.h:402