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