svc_action.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef _SVC_ACTION_H
  2. #define _SVC_ACTION_H
  3. #include <bfc/dispatch.h>
  4. #include <bfc/string/bfcstring.h>
  5. #include <bfc/ptrlist.h>
  6. #include <api/service/services.h>
  7. class ifc_window;
  8. class NOVTABLE svc_action : public Dispatchable {
  9. protected:
  10. svc_action() { }
  11. public:
  12. static FOURCC getServiceType() { return WaSvc::ACTION; }
  13. int hasAction(const wchar_t *name);
  14. int onAction(const wchar_t *action, const wchar_t *param=NULL, intptr_t p1=0, intptr_t p2=0, void *data=NULL, size_t datalen=0, ifc_window *source=NULL);
  15. enum {
  16. HASACTION=10,
  17. ONACTION=20,
  18. };
  19. };
  20. inline int svc_action::hasAction(const wchar_t *name) {
  21. return _call(HASACTION, 0, name);
  22. }
  23. inline int svc_action::onAction(const wchar_t *action, const wchar_t *param, intptr_t p1, intptr_t p2, void *data, size_t datalen, ifc_window *source) {
  24. return _call(ONACTION, 0, action, param, p1, p2, data, datalen, source);
  25. }
  26. class ActionEntry {
  27. public:
  28. ActionEntry(const wchar_t *_action, int _id) : action(_action), id(_id) {}
  29. virtual ~ActionEntry() { }
  30. const wchar_t *getAction() { return action; }
  31. int getId() { return id; }
  32. private:
  33. StringW action;
  34. int id;
  35. };
  36. class SortActions {
  37. public:
  38. static int compareItem(ActionEntry *p1, ActionEntry *p2) {
  39. return WCSICMP(p1->getAction(), p2->getAction());
  40. }
  41. static int compareAttrib(const wchar_t *attrib, ActionEntry *item) {
  42. return WCSICMP(attrib, item->getAction());
  43. }
  44. };
  45. class NOVTABLE svc_actionI : public svc_action {
  46. public:
  47. virtual ~svc_actionI();
  48. void registerAction(const wchar_t *actionid, int pvtid);
  49. virtual int onActionId(int pvtid, const wchar_t *action, const wchar_t *param=NULL, int p1=0, int p2=0, void *data=NULL, int datalen=0, ifc_window *source=NULL)=0;
  50. protected:
  51. virtual int hasAction(const wchar_t *name);
  52. virtual int onAction(const wchar_t *action, const wchar_t *param=NULL, intptr_t p1=0, intptr_t p2=0, void *data=NULL, size_t datalen=0, ifc_window *source=NULL);
  53. PtrListQuickSorted<ActionEntry, SortActions> actions;
  54. RECVS_DISPATCH;
  55. };
  56. #include <api/service/servicei.h>
  57. template <class T>
  58. class ActionCreator : public waServiceFactoryT<svc_action, T> {};
  59. template <class T>
  60. class ActionCreatorSingle : public waServiceFactoryTSingle<svc_action, T> {
  61. public:
  62. svc_action *getHandler() {
  63. return waServiceFactoryT<svc_action, T>::getSingleService();
  64. }
  65. };
  66. #include <api/service/svc_enum.h>
  67. #include <bfc/string/StringW.h>
  68. class ActionEnum : public SvcEnumT<svc_action> {
  69. public:
  70. ActionEnum(const wchar_t *_action) : action(_action) { }
  71. protected:
  72. virtual int testService(svc_action *svc) {
  73. return (!action.isempty() && svc->hasAction(action));
  74. }
  75. private:
  76. StringW action;
  77. };
  78. class FireAction {
  79. public:
  80. enum {
  81. ACTION_NOT_HANDLED = 0x80000000
  82. };
  83. /**
  84. Fire a named action out into the system with the given parameters.
  85. This method will only send the action to the first registered handler for that action.
  86. This prevents the action from being overridden or handled by newer wacs.
  87. The content and syntax of the generalized params are defined by the handler of the action string.
  88. Read: Using Wasabi: General Development: Actions
  89. @see svc_actionI
  90. @param action The action string.
  91. @param param A string parameter to the action.
  92. @param p1 The first integer parameter to the action.
  93. @param p2 The second integer parameter to the action.
  94. @param data An untyped data buffer parameter to the action.
  95. @param datalen The size in bytes of the data buffer parameter.
  96. @param source A window object that can be given as the source object, if the action handler is expecting one. Actions bound to guiobjects use that guiobject's rootwnd pointer as the source.
  97. @param apply_to_all Send the action to everyone. (If false only sends to first registered)
  98. */
  99. FireAction(const wchar_t *action, const wchar_t *param = NULL, intptr_t p1 = 0, intptr_t p2 = 0, void *data = NULL, size_t datalen = 0, ifc_window *source = NULL, int apply_to_all = TRUE) {
  100. lastretval = ACTION_NOT_HANDLED;
  101. ActionEnum ae(action);
  102. svc_action *act;
  103. while ((act = ae.getNext()) != NULL) {
  104. lastretval = act->onAction(action, param, p1, p2, data, datalen, source);
  105. ae.release(act);
  106. if (!apply_to_all) break;
  107. }
  108. }
  109. /**
  110. More robust retval handling is needed.
  111. I ought to be grabbing all of the return values into a list an exposing that.
  112. Later.
  113. Read: Using Wasabi: General Development: Actions
  114. @see svc_actionI
  115. @ret The return code of the action sent.
  116. */
  117. int getLastReturnValue() {
  118. return lastretval;
  119. }
  120. private:
  121. int lastretval;
  122. };
  123. #endif