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 #include <UI/UI_Object.h>
17 #include <UT/UT_Signal.h>
18 #include <UT/UT_ValArray.h>
19 #include <UT/UT_String.h>
20 #include <UT/UT_WorkBuffer.h>
21 #include <UT/UT_SharedPtr.h>
22 
23 #include <utility>
24 
25 class AP_Interface;
26 class UI_Value;
27 class UI_Manager;
28 template<typename T> class UT_SymbolMap;
29 class UI_Feel;
30 class UI_KeyDelegate;
32 class UI_Window;
36 
37 /// Base class for interfaces.
38 /// This class' main responsibility is to maintain hash tables of named
39 /// UI_Feel "object symbol" and UI_Value "value symbol" items.
40 class SI_API AP_Interface : public UI_Object {
41 public:
42  AP_Interface();
43 
44  /// Construct with given name and predefined value names with corresponding
45  /// interests. The actual values are not created until wireInteface() is
46  /// called. Typically, initApplication() is first called to create the
47  /// the values and then wireInterface() is called afterwards to attach the
48  /// methods to the corresponding values.
49  AP_Interface(const char *myname,
50  const char *const *names,
51  UI_EventMethod const *methods);
52 
53  ~AP_Interface() override;
54 
55  const char *className() const override;
56 
57  /// Return the name passed in the constructor
58  const UT_String &getName() const { return myName; }
59 
60  /// Attach values names to the methods passed in the constructor.
61  /// The actual UI_Value's are typically created by calling
62  /// initApplication() first. If the values do not exist yet, then they are
63  /// automatically created into uims (if not nullptr), else they will
64  /// created in this object.
65  void wireInterface(UI_Manager *uims);
66 
67  // Removes interests from all the value names passed in the constructor.
68  void unwireInterface(UI_Manager *uims);
69 
70  /// Turns a .ui file into UI gadgets. If quiet is true, no errors or
71  /// warnings are printed. If defines is specified, this allows you to
72  /// pass CPP style symbols to the parser, in the form "SYMBOL=VALUE".
73  /// =VALUE may be omitted.
74  bool readUIFile(const char *ui_filename,
75  bool quiet = false);
76 
77  /// Create new preference file given its base name using standard rules
78  /// (eg. in $HOME/houdiniX.Y). This may return nullptr if a .nosave file is
79  /// found.
80  static FILE *createPreferenceFile(const char *filename);
81 
82  /// Read the given preference file given its base name using standard
83  /// rules. The values will be initialized according to the parsed file into
84  /// this object.
85  bool readPreferenceFile(const char *pref_filename,
86  bool quiet = false);
87 
88  /// Set the named value symbol to the corresponding pointer. If warn is
89  /// true, then a warning will be issued if the name already exists as an
90  /// object or key delegate symbol, or if the value symbol name already
91  /// previously existed.
92  void setValueSymbol(const char *symbol, UI_Value *value,
93  bool warn = true);
94 
95  /// Find the UI_Value for the given named value symbol
96  UI_Value *findValueSymbol(const char *symbol) const;
97 
98  /// Get the UI_Value for the given named value symbol, creating it if it
99  /// doesn't exist yet. If 'create' is false, then getValueSymbol() is the
100  /// same as findValueSymbol().
101  UI_Value *getValueSymbol(const char *symbol, int create = 1);
102 
103  /// Returns all the Symbol/UI_Value's pairs from the ValueSymbol table
104  /// return array will contain nullptr values if its symbol doesn't exist.
105  int getValueSymbols(UT_StringArray &symbols,
107 
108  /// Set the named object symbol to the corresponding pointer. If warn is
109  /// true, then a warning will be issued if the name already exists as a
110  /// value or key delegate symbol, or if the object symbol name already
111  /// previously existed.
112  void setObjectSymbol(const char *symbol, UI_Object *who,
113  int warn = 1);
114 
115  /// Find the UI_Object for the given object symbol name. Returns nullptr if
116  /// it doesn't exist.
117  UI_Object *getObjectSymbol(const char *symbol) const;
118 
119  /// Find the UI_Feel for the given object symbol name. Returns nullptr if
120  /// it doesn't exist. This is equivalent to findObject<UI_Feel*>(symbol).
121  UI_Feel *getFeelSymbol(const char *symbol) const;
122 
123  /// Find the UI_Feel for the given value pointer. Returns nullptr if
124  /// it doesn't exist.
125  UI_Feel *findValueFeel( UI_Value * value) const;
126 
127  /// Returns all the Symbol/UI_Feel's pairs from the FeelSymbol table
128  /// return array will contain nullptr values if its symbol doesn't exist.
129  int getFeelSymbols(UT_StringArray &symbols,
130  UT_Array<UI_Feel *> &feels) const;
131 
132  /// Set the named key delegate symbol to the corresponding pointer. If warn
133  /// is true, then a warning will be issued if the name already exists as an
134  /// object or value symbol, or if the key delegate symbol name already
135  /// previously existed.
136  void setKeyDelegateSymbol(const char *symbol,
137  UI_KeyDelegateSPtr delegate,
138  bool warn = true);
139 
140  /// Returns whether the current key delegate symbol is defined.
141  bool hasKeyDelegateSymbol(const char *symbol) const;
142 
143  /// Find the UI_KeyDelegate for the given named key delegate symbol.
144  UI_KeyDelegateSPtr findKeyDelegateSymbol(const char *symbol) const;
145 
146  /// Get the UI_KeyDelegate for the given named key delegate symbol,
147  /// creating it if it doesn't exist yet. If 'create' is false, then
148  /// getKeyDelegateSymbol() is the same as findKeyDelegateSymbol().
149  UI_KeyDelegateSPtr getKeyDelegateSymbol(const char *symbol,
150  int create = 1);
151 
152  /// Delete the entry for the given object symbol name
153  void removeObjectSymbol(const char *symbol);
154 
155  /// Delete the entry for the given value symbol name
156  void removeValueSymbol(const char *symbol);
157 
158  /// Find the pointer for the given object symbol name, dynamically casted
159  /// to the given template type.
160  template<typename T>
161  T *findObject(const char *name) const
162  {
163  return dynamic_cast<T *>(getObjectSymbol(name));
164  }
165 
166  /// Find the pointer for the given value symbol name, dynamically casted
167  /// to the given template type.
168  template<typename T>
169  T *findValue(const char *name) const
170  {
171  return dynamic_cast<T *>(findValueSymbol(name));
172  }
173 
174  /// Initialize this object. This method is intended to be overriden by
175  /// subclasses since the base class implementation is empty. The subclass
176  /// typically calls readUIFile() here to create the interface which
177  /// populates the object and value symbols.
178  virtual void initApplication(UI_Manager *uims,
179  int argc,
180  const char **argv);
181 
182  /// Resets this object by calling unwireInterface() and then destroying all
183  /// objects and values.
184  void resetApplication();
185 
186  void handleEvent(UI_Event *event) override;
187 
188  // These are methods which are called only if the APP is "the"
189  // registered main application in the program. By default, they
190  // do nothing.
191  virtual int saveAppData(const char *filename); // ret 1 on success
192  virtual int loadAppState(); // Allows app to load context
193  virtual int saveAppState(); // Allows app to save context
194  virtual void saveOnCoreDump(); // To do stuff when...yikes!
195  virtual bool getCoreDumpFileName(UT_WorkBuffer &name);
196 
197  // Utility methods for managing the common ui.pref file
198  // Load the UI pref file; should be used by Main.C only.
199  bool loadUIInitPrefs();
200  // Save the current UI state to the pref file. The layout level is
201  // the desired level to be saved.
202  void saveUIInitPrefs(
203  const char * layout_level,
204  fpreal uiscale,
205  int playbarui);
206  // Restore UI prefs from file. Similar to load but does not explicitly
207  // set the layout level, which can only be done by Main.C; so this method
208  // is used everywhere else to load the prefs from file.
209  bool restoreUIInitPrefs();
210 
211  /// Menu items and their mapping for file chooser style preference.
212  static const std::pair<const char *, int> theFileChooserStyleMap[];
213 
214  // Helper methods to convert enums to labels, and vice versa.
215  static int mapStrToInt(const char *label,
216  const std::pair<const char *, int> *map);
217  static const char * mapIntToStr(int index,
218  const std::pair<const char *, int> *map);
219 
220  // If a function key is pressed and a script is attached to it,
221  // this will return the script.
222  //
223  bool getFunctionKeyScript(int key, UI_Event *event,
224  UT_String &script);
225 
226  // These static methods will get/set the exit code that the UI application
227  // will exit with. The default exit code is 0. Note that these methods
228  // are implemented in AP_Main.C.
229  static void setAppExitCode(int exit_code);
230  static int getAppExitCode();
231 
232  /// Returns @c true if the application is exiting.
233  static bool isAppExiting();
234 
235  // Returns the desktop specified by the -desktop argument.
236  // Returns nullptr if no such argument was specified.
237  static const char *getStartupDesktop();
238 
239  // Helper class for being able to call the private installSignalHandlers().
240  class InstallSignalHandlersHelper;
241 
242 protected:
243  //
244  // Set the application to run in the fore or background. This MUST
245  // be called from a static constructor (i.e. before main is run).
246  //
247  static void setAppForeground();
248 
249  static void setTheMainApplication(AP_Interface *app);
250  static AP_Interface *getMainApplication() { return theMainApplication; }
251 
253  const char * const *myValueNames;
255 
256  UI_NamedValueMap *getValueTable() const { return myValueTable; }
257  UI_NamedObjectMap *getObjectTable() const { return myObjectTable; }
258 
259  // Updates the given UI_Window object using the -geometry or pref file
260  static void loadWindowGeometry(
261  UI_Window *window,
262  bool default_maximized,
263  bool use_pref);
264 
265  // Returns true if the -geometry arg was parsed out and false otherwise.
266  // NOTE: The values are returned in pixels!
267  static bool getWindowGeometry(int &width, int &height,
268  int &left, int &bottom,
269  bool &maximized,
270  bool use_pref);
271  static bool getSaveWindowGeoPref();
272  static void saveWindowGeoPref();
273  static bool loadWindowGeoPref(
274  int &w, int &h, int &left, int &bottom,
275  bool &maximized);
276 
277 private:
278  class si_CrashHandler;
279  friend class si_CrashHandler;
280 
281  // WARNING: The installSignalHandlers() method calls UTgetInterrupt(), so
282  // should not be called before the call to UTsetInterrupt().
283  static void installSignalHandlers();
284  static void getSignalList(UT_Array<int> &signals);
285 
286  void assignUIInitPrefs();
287 
288  static const char *mainApplicationName();
289 
290  static void createCrashLog(UTsignalHandlerArg sig_arg,
291  const char *appname, const char *filename);
292  static void terminateSignalHandler(UTsignalHandlerArg sig_arg);
293  static bool coredumpHandler(UTsignalHandlerArg sig_arg);
294  static void coreDumpChaser(UTsignalHandlerArg sig_arg);
295  static void powerFailHandlerSignalHandler(UTsignalHandlerArg);
296  static void powerFailHandler(
297  UT_String &saved_file_name,
298  const UT_StringRef &details);
299  static void terminationWarning(uint nseconds);
300  static void betaExpiryWarning(const char* date_info);
301 
302  static AP_Interface *theMainApplication;
303 
304 private:
305  UI_NamedValueMap *myValueTable;
306  UI_NamedObjectMap *myObjectTable;
307  UI_NamedKeyDelegateMap *myKeyDelegateTable;
308 };
309 
310 extern void APregister (AP_Interface *app);
311 extern void APderegister(AP_Interface *app);
312 
313 // These template functions allow you to get & cast to the appropriate
314 // object/value type in one step, ie:
315 //
316 // UI_Button *button = UIgetObject<UI_Button>(this, "button.gad");
317 // UI_StringList *menu = UIgetValue<UI_StringList>(this, "rmb.menu");
318 
319 template<class ObjectClass> inline
320 ObjectClass *SIgetObject(const AP_Interface *app, const char *name)
321 {
322  return dynamic_cast<ObjectClass *>(app->getObjectSymbol(name));
323 }
324 
325 template<class ValueClass> inline
326 ValueClass *SIgetValue(const AP_Interface *app, const char *name)
327 {
328  return dynamic_cast<ValueClass *>(app->findValueSymbol(name));
329 }
330 
331 #endif
ValueClass * SIgetValue(const AP_Interface *app, const char *name)
Definition: AP_Interface.h:326
GT_API const UT_StringHolder filename
T * findValue(const char *name) const
Definition: AP_Interface.h:169
#define SI_API
Definition: SI_API.h:10
GLint left
Definition: glcorearb.h:2004
GLuint const GLchar * name
Definition: glcorearb.h:785
void APregister(AP_Interface *app)
UT_SymbolMap< UI_Value * > UI_NamedValueMap
Definition: AP_Interface.h:32
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
struct _cl_event * event
Definition: glcorearb.h:2960
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
virtual void handleEvent(UI_Event *event)
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:30
const UT_String & getName() const
Return the name passed in the constructor.
Definition: AP_Interface.h:58
GLint GLint bottom
Definition: glcorearb.h:2004
static AP_Interface * getMainApplication()
Definition: AP_Interface.h:250
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2544
const char *const * myValueNames
Definition: AP_Interface.h:253
UI_EventMethod const * myMethods
Definition: AP_Interface.h:254
GLuint const GLuint * names
Definition: glew.h:2695
GLint GLsizei width
Definition: glcorearb.h:102
GLint GLsizei GLsizei height
Definition: glcorearb.h:102
UT_SymbolMap< UI_Object * > UI_NamedObjectMap
Definition: AP_Interface.h:34
UI_NamedObjectMap * getObjectTable() const
Definition: AP_Interface.h:257
UI_Object * getObjectSymbol(const char *symbol) const
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
fpreal64 fpreal
Definition: SYS_Types.h:277
T * findObject(const char *name) const
Definition: AP_Interface.h:161
GLuint index
Definition: glcorearb.h:785
GLsizei const GLfloat * value
Definition: glcorearb.h:823
void(UI_Object::* UI_EventMethod)(UI_Event *)
Definition: UI_Object.h:36
virtual const char * className() const
Definition: UI_Object.h:87
UT_String myName
Definition: AP_Interface.h:252
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:35
unsigned int uint
Definition: SYS_Types.h:45
void APderegister(AP_Interface *app)
UI_NamedValueMap * getValueTable() const
Definition: AP_Interface.h:256
UT_SharedPtr< UI_KeyDelegate > UI_KeyDelegateSPtr
Definition: AP_Interface.h:31
ObjectClass * SIgetObject(const AP_Interface *app, const char *name)
Definition: AP_Interface.h:320