home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 June / CHIP_CD_2004-06.iso / software / miranda_hit / files / popup.mir / m_popup.h < prev    next >
C/C++ Source or Header  |  2003-03-11  |  12KB  |  270 lines

  1. /*
  2. ===============================================================================
  3.                                 PopUp plugin
  4. Plugin Name: PopUp
  5. Plugin author: hrk, Luca Santarelli, hrk@users.sourceforge.net
  6. ===============================================================================
  7.  
  8. The purpose of this plugin is to give developers a common "platform/interface" to show PopUps. It is born from the source code of NewStatusNotify, another plugin I've made.
  9.  
  10. Remember that users *must* have this plugin enabled, or they won't get any popup. Write this in the requirements, do whatever you wish ;-)... but tell them!
  11. ===============================================================================
  12. */
  13. #ifndef M_POPUP_H
  14. #define M_POPUP_H
  15.  
  16. /*
  17. NOTE! Since Popup 1.0.1.2 there is a main meun group called "PopUps" where I have put a "Enable/Disable" item.
  18. You can add your own "enable/disable" items by adding these lines before you call MS_CLIST_ADDMAINMENUITEM:
  19. mi.pszPopUpName = Translate("PopUps");
  20. mi.position = 0; //You don't need it and it's better if you put it to zero.
  21. */
  22.  
  23. //#define MAX_CONTACTNAME 32
  24. //#define MAX_SECONDLINE 40
  25. #define MAX_CONTACTNAME 2048
  26. #define MAX_SECONDLINE 2048
  27.  
  28. //This is the basic data you'll need to fill and pass to the service function.
  29. typedef struct  {
  30.     HANDLE lchContact;                                        //Handle to the contact, can be NULL (main contact).
  31.     HICON lchIcon;                                                //Handle to a icon to be shown. Cannot be NULL.
  32.     char lpzContactName[MAX_CONTACTNAME];    //This is the contact name or the first line in the plugin. Cannot be NULL.
  33.     char lpzText[MAX_SECONDLINE];                    //This is the second line text. Users can choose to hide it. Cannot be NULL.
  34.     COLORREF colorBack;                                        //COLORREF to be used for the background. Can be NULL, default will be used.
  35.     COLORREF colorText;                                        //COLORREF to be used for the text. Can be NULL, default will be used.
  36.     WNDPROC PluginWindowProc;                            //Read below. Can be NULL; default will be used.
  37.     void * PluginData;                                        //Read below. Can be NULL.
  38. } POPUPDATA, * LPPOPUPDATA;
  39.  
  40. typedef struct {
  41.     HANDLE lchContact;
  42.     HICON lchIcon;
  43.     char lpzContactName[MAX_CONTACTNAME];
  44.     char lpzText[MAX_SECONDLINE];
  45.     COLORREF colorBack;
  46.     COLORREF colorText;
  47.     WNDPROC PluginWindowProc;
  48.     void * PluginData;
  49.     int iSeconds;    //Custom delay time in seconds. -1 means "forever", 0 means "default time".
  50.     char cZero[16];    //16 unused bytes which may come useful in the future.
  51. } POPUPDATAEX, *LPPOPUPDATAEX;
  52.  
  53. /*
  54. When you call MS_POPUP_ADDPOPUP, my plugin will check if the given POPUPDATA structure is filled with acceptable values. If not, the data will be rejected and no popup will be shown.
  55.  
  56. - lpzText should be given, because it's really bad if a user chooses to have the second line displayed
  57. and it's empty :-) Just write it and let the user choose if it will be displayed or not.
  58.  
  59. - PluginWindowProc is a WNDPROC address you have to give me. Why? What? Where? Calm down 8)
  60. My plugin will take care of the creation of the popup, of the destruction of the popup, of the come into
  61. view and the hiding of the popup. Transparency, animations... all this stuff.
  62. My plugin will not (as example) open the MessageWindow when you left click on a popup.
  63. Why? Because I don't know if your popup desires to open the MessageWindow :))))
  64. This means that you need to make a WNDPROC which takes care of the WM_messages you need.
  65. For example, WM_COMMAND or WM_CONTEXTMENU or WM_LMOUSEUP or whatever.
  66. At the end of your WNDPROC remember to "return DefWindowProc(hwnd, msg, wParam, lParam);"
  67. When you process a message that needs a return value (an example could be WM_CTLCOLORSTATIC,
  68. but you don't need to catch it 'cause it's my plugin's job), simply return the nedeed value. :)
  69. The default WNDPROC does nothing.
  70.  
  71. - PluginData is a pointer to a void, which means a pointer to anything. You can make your own structure
  72. to store the data you need (example: a status information, a date, your name, whatever) and give me a
  73. pointer to that struct.
  74. You will need to destroy that structure and free the memory when the PopUp is going to be destroyed. You'll know this when you receive a UM_FREEPLUGINDATA. The name tells it all: free your own plugin data.
  75.  
  76. Appendix A: Messages my plugin will handle and your WNDPROC will never see.
  77. WM_CREATE, WM_DESTROY, WM_TIMER, WM_ERASEBKGND
  78. WM_CTLCOLOR* [whatever it may be: WM_CTLCOLORDLG, WM_CTLCOLORSTATIC...]
  79. WM_PAINT, WM_PRINT, WM_PRINTCLIENT
  80.  
  81. Appendix B: "What do I need to do?!?".
  82. Here is an example in C.
  83.  
  84. //Your plugin is in /plugins/myPlugin/ or in miranda32/something/
  85. #include "../../plugins/PopUp/m_popup.h"
  86.  
  87. Define your own plugin data if you need it. In this example, we need it and we'll use NewStatusNotify as example: thsi plugin shows a popup when someone in your contact list changes his/hers status. We'll need to know his status, both current and old one.
  88. typedef struct {
  89.     WORD oldStatus;
  90.     WORD newStatus;
  91. } MY_PLUGIN_DATA;
  92.  
  93. When we need to show the popup, we do:
  94. {
  95.     POPUPDATA ppd;
  96.     hContact = A_VALID_HANDLE_YOU_GOT_FROM_SOMEWHERE;
  97.     hIcon = A_VALID_HANDLE_YOU_GOT_SOMEWHERE;
  98.     char * lpzContactName = (char*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME,(WPARAM)lhContact,0);
  99.     //99% of the times you'll just copy this line.
  100.     //1% of the times you may wish to change the contact's name. I don't know why you should, but you can.
  101.     char * lpzText;
  102.     //The text for the second line. You could even make something like: char lpzText[128]; lstrcpy(lpzText, "Hello world!"); It's your choice.
  103.     COLORREF colorBack = GetSysColor(COLOR_BTNFACE); //The colour of Miranda's option Pages (and many other windows...)
  104.     COLORREF colorText = RGB(255,255,255); //White.
  105.     MY_PLUGIN_DATA * mpd = (MY_PLUGIN_DATA*)malloc(sizeof(MY_PLUGIN_DATA));
  106.  
  107.     ZeroMemory(ppd, sizeof(ppd)); //This is always a good thing to do.
  108.     ppd.lchContact = (HANDLE)hContact; //Be sure to use a GOOD handle, since this will not be checked.
  109.     ppd.lchIcon = hIcon;
  110.     lstrcpy(ppd.lpzContactName, lpzContactName);
  111.     lstrcpy(ppd.lpzText, lpzText);
  112.     ppd.colorBack = colorBack;
  113.     ppd.colorText = colorText;
  114.     ppd.PluginWindowProc = (WNDPROC)PopupDlgProc;
  115.  
  116.     //Now the "additional" data.
  117.     mpd->oldStatus = ID_STATUS_OFFLINE;
  118.     mpd->newStatus = ID_STATUS_ONLINE;
  119.  
  120.     //Now that the plugin data has been filled, we add it to the PopUpData.
  121.     ppd.PluginData = mpd;
  122.  
  123.     //Now that every field has been filled, we want to see the popup.
  124.     CallService(MS_POPUP_ADDPOPUP, (WPARAM)&ppd, 0);
  125. }
  126.  
  127. Obviously, you have previously declared some:
  128. static int CALLBACK PopupDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  129. {
  130.     switch(message) {
  131.         case WM_COMMAND:
  132.             if ((HIWORD)wParam == STN_CLICKED) { //It was a click on the Popup.
  133.                 PUDeletePopUp(hWnd);
  134.                 return TRUE;
  135.             }
  136.             break;
  137.         case UM_FREEPLUGINDATA: {
  138.             MY_PLUGIN_DATA * mpd = NULL;
  139.             mpd = (MY_PLUGIN_DATA*)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd,(LPARAM)mpd);
  140.             if (mdp > 0) free(mpd);
  141.             return TRUE; //TRUE or FALSE is the same, it gets ignored.
  142.         }
  143.         default:
  144.             break;
  145.     }
  146.     return DefWindowProc(hWnd, message, wParam, lParam);
  147. }
  148. */
  149.  
  150. /*
  151. Creates, adds and shows a popup, given a (valid) POPUPDATA structure pointer.
  152. wParam = (WPARAM)(*POPUPDATA)PopUpDataAddress
  153. lParam = 0
  154. Returns: > 0 on success, 0 if creation went bad, -1 if the PopUpData contained unacceptable values.
  155. NOTE: it returns -1 if the PopUpData was not valid, if there were already too many popups, if the module was disabled.
  156. Otherwise, it can return anything else...
  157. */
  158. #define MS_POPUP_ADDPOPUP "PopUp/AddPopUp"
  159. static int __inline PUAddPopUp(POPUPDATA* ppdp) {
  160.     return CallService(MS_POPUP_ADDPOPUP, (WPARAM)ppdp,0);
  161. }
  162.  
  163. #define MS_POPUP_ADDPOPUPEX "PopUp/AddPopUpEx"
  164. static int __inline PUAddPopUpEx(POPUPDATAEX* ppdp) {
  165.     return CallService(MS_POPUP_ADDPOPUPEX, (WPARAM)ppdp,0);
  166. }
  167.  
  168. /*
  169. Returns the handle to the contact associated to the specified PopUpWindow.
  170. You will probably need to know this handle inside your WNDPROC. Exampole: you want to open the MessageWindow. :-)
  171. Call MS_POPUP_GETCONTACT on the hWnd you were given in the WNDPROC.
  172. wParam = (WPARAM)(HWND)hPopUpWindow
  173. lParam = 0;
  174. Returns: the HANDLE of the contact. Can return NULL, meaning it's the main contact. -1 means failure.
  175. */
  176. #define MS_POPUP_GETCONTACT "PopUp/GetContact"
  177. static HANDLE __inline PUGetContact(HWND hPopUpWindow) {
  178.     return (HANDLE)CallService(MS_POPUP_GETCONTACT, (WPARAM)hPopUpWindow,0);
  179. }
  180.  
  181. /*
  182. wParam = (WPARAM)(HWND)hPopUpWindow
  183. lParam = (LPARAM)(PLUGINDATA*)PluginDataAddress;
  184. Returns: the address of the PLUGINDATA structure. Can return NULL, meaning nothing was given. -1 means failure.
  185. IMPORTANT NOTE: it doesn't seem to work if you do:
  186. CallService(..., (LPARAM)aPointerToAStruct);
  187. and then use that struct.
  188. Do this, instead:
  189. aPointerToStruct = CallService(..., (LPARAM)aPointerToAStruct);
  190. and it will work. Just look at the example I've written above (PopUpDlgProc).
  191. */
  192. #define MS_POPUP_GETPLUGINDATA "PopUp/GetPluginData"
  193. static void __inline * PUGetPluginData(HWND hPopUpWindow) {
  194.     long * uselessPointer = NULL;
  195.     return (void*)CallService(MS_POPUP_GETPLUGINDATA,(WPARAM)hPopUpWindow,(LPARAM)uselessPointer);
  196. }
  197.  
  198. /*
  199. wParam = 0
  200. lParam = 0
  201. Returns: 0 if the user has chosen not to have the second line, 1 if he choose to have the second line.
  202. */
  203. #define MS_POPUP_ISSECONDLINESHOWN "PopUp/IsSecondLineShown"
  204. static BOOL __inline PUIsSecondLineShown() {
  205.     return (BOOL)CallService(MS_POPUP_ISSECONDLINESHOWN,0,0);
  206. }
  207.  
  208. /*
  209. Requests an action or an answer from PopUp module.
  210. wParam = (WPARAM)wpQuery
  211. returns 0 on success, -1 on error, 1 on stupid calls ;-)
  212. */
  213. #define PUQS_ENABLEPOPUPS 1 //returns 0 if state was changed, 1 if state wasn't changed
  214. #define PUQS_DISABLEPOPUPS 2 // " "
  215. #define PUQS_GETSTATUS 3 //Returns 1 (TRUE) if popups are enabled, 0 (FALSE) if popups are disabled.
  216.  
  217. #define MS_POPUP_QUERY "PopUp/Query"
  218.  
  219. /*
  220. UM_FREEPLUGINDATA
  221. wParam = lParam = 0. Process this message if you have allocated your own memory. (i.e.: POPUPDATA.PluginData != NULL)
  222. */
  223. #define UM_FREEPLUGINDATA        (WM_USER + 0x0200)
  224.  
  225. /*
  226. UM_DESTROYPOPUP
  227. wParam = lParam = 0. Send this message when you want to destroy the popup, or use the function below.
  228. */
  229. #define UM_DESTROYPOPUP          (WM_USER + 0x0201)
  230. static int __inline PUDeletePopUp(HWND hWndPopUp) {
  231.     return (int)SendMessage(hWndPopUp, UM_DESTROYPOPUP,0,0);
  232. }
  233.  
  234. /*
  235. UM_INITPOPUP
  236. wParam = (WPARAM)(HWND)hPopUpWindow (but this is useless, since I'll directly send it to your hPopUpWindow
  237. lParam = 0.
  238. This message is sent to the PopUp when its creation has been finished, so POPUPDATA (and thus your PluginData) is reachable.
  239. Catch it if you needed to catch WM_CREATE or WM_INITDIALOG, which you'll never ever get in your entire popup-life.
  240. Return value: if you process this message, return 0. If you don't process it, return 0. Do whatever you like ;-)
  241. */
  242. #define UM_INITPOPUP             (WM_USER + 0x0202)
  243.  
  244. /*
  245. wParam = (WPARAM)(HWND)hPopUpWindow
  246. lParam = (LPARAM)(char*)lpzNewText
  247. returns: > 0 for success, -1 for failure, 0 if the failure is due to second line not being shown. (but you could call PUIsSecondLineShown() before changing the text...)
  248. Changes the text displayed in the second line of the popup.
  249. */
  250. #define MS_POPUP_CHANGETEXT "PopUp/Changetext"
  251. static int __inline PUChangeText(HWND hWndPopUp, LPCTSTR lpzNewText) {
  252.     return (int)CallService(MS_POPUP_CHANGETEXT, (WPARAM)hWndPopUp, (LPARAM)lpzNewText);
  253. }
  254.  
  255. /*
  256. This is mainly for developers.
  257. Shows a warning message in a PopUp. It's useful if you need a "MessageBox" like function, but you don't want a modal window (which will interfere with a DialogProcedure. MessageBox steals focus and control, this one not.
  258. wParam = (char*) lpzMessage
  259. lParam = 0;
  260. Returns: 0 if the popup was shown, -1 in case of failure.
  261. */
  262. #define SM_WARNING         0x01 //Triangle icon.
  263. #define SM_NOTIFY     0x02 //Exclamation mark icon.
  264. #define MS_POPUP_SHOWMESSAGE "PopUp/ShowMessage"
  265.  
  266. static int __inline PUShowMessage(char* lpzText, BYTE kind) {
  267.     return (int)CallService(MS_POPUP_SHOWMESSAGE, (WPARAM)lpzText,(LPARAM)kind);
  268. }
  269.  
  270. #endif