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