home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / EVENTHAN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  13.1 KB  |  379 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.6  $
  6. //
  7. // Implementation of class TEventHandler
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_EVENTHAN_H)
  11. # include <owl/eventhan.h>
  12. #endif
  13. #include <stdlib.h>
  14.  
  15. OWL_DIAGINFO;
  16. DIAG_DEFINE_GROUP_INIT(OWL_INI, OwlMsg, 1, 0);
  17.                              // diagnostic group for message tracing
  18.  
  19. #if defined(__TRACE) || defined(__WARN)
  20.  
  21. #if defined(BI_NAMESPACE)
  22. namespace OWL {
  23. #endif
  24.  
  25.   struct TWMStr {
  26.     uint    msg;
  27.     char*   str;
  28.   };
  29.  
  30.   static  TWMStr  StrTab[] = {
  31.     { 0x0000, "WM_NULL"                  },
  32.     { 0x0001, "WM_CREATE"                },
  33.     { 0x0002, "WM_DESTROY"               },
  34.     { 0x0003, "WM_MOVE"                  },
  35.     { 0x0004, "wm_sizewait"              },
  36.     { 0x0005, "WM_SIZE"                  },
  37.     { 0x0006, "WM_ACTIVATE"              },
  38.     { 0x0007, "WM_SETFOCUS"              },
  39.     { 0x0008, "WM_KILLFOCUS"             },
  40.     { 0x0009, "wm_setvisible"            },
  41.     { 0x000A, "WM_ENABLE"                },
  42.     { 0x000B, "WM_SETREDRAW"             },
  43.     { 0x000C, "WM_SETTEXT"               },
  44.     { 0x000D, "WM_GETTEXT"               },
  45.     { 0x000E, "WM_GETTEXTLENGTH"         },
  46.     { 0x000F, "WM_PAINT"                 },
  47.     { 0x0010, "WM_CLOSE"                 },
  48.     { 0x0011, "WM_QUERYENDSESSION"       },
  49.     { 0x0012, "WM_QUIT"                  },
  50.     { 0x0013, "WM_QUERYOPEN"             },
  51.     { 0x0014, "WM_ERASEBKGND"            },
  52.     { 0x0015, "WM_SYSCOLORCHANGE"        },
  53.     { 0x0016, "WM_ENDSESSION"            },
  54.     { 0x0017, "wm_systemerror"           },
  55.     { 0x0018, "WM_SHOWWINDOW"            },
  56.     { 0x0019, "WM_CTLCOLOR"              },
  57.     { 0x001A, "WM_SETTINGCHANGE"         },
  58.     { 0x001B, "WM_DEVMODECHANGE"         },
  59.     { 0x001C, "WM_ACTIVATEAPP"           },
  60.     { 0x001D, "WM_FONTCHANGE"            },
  61.     { 0x001E, "WM_TIMECHANGE"            },
  62.     { 0x001F, "WM_CANCELMODE"            },
  63.     { 0x0020, "WM_SETCURSOR"             },
  64.     { 0x0021, "WM_MOUSEACTIVATE"         },
  65.     { 0x0022, "WM_CHILDACTIVATE"         },
  66.     { 0x0023, "WM_QUEUESYNC"             },
  67.     { 0x0024, "WM_GETMINMAXINFO"         },
  68.    // 0x0025
  69.     { 0x0026, "WM_PAINTICON"             },
  70.     { 0x0027, "WM_ICONERASEBKGND"        },
  71.     { 0x0028, "WM_NEXTDLGCTL"            },
  72.     { 0x0029, "wm_alttabactive"          },
  73.     { 0x002A, "WM_SPOOLERSTATUS"         },
  74.     { 0x002B, "WM_DRAWITEM"              },
  75.     { 0x002C, "WM_MEASUREITEM"           },
  76.     { 0x002D, "WM_DELETEITEM"            },
  77.     { 0x002E, "WM_VKEYTOITEM"            },
  78.     { 0x002F, "WM_CHARTOITEM"            },
  79.     { 0x0030, "WM_SETFONT"               },
  80.     { 0x0031, "WM_GETFONT"               },
  81.     { 0x0032, "WM_SETHOTKEY"             },
  82.     { 0x0033, "WM_GETHOTKEY"             },
  83.     { 0x0034, "wm_filesyschange"         },
  84.     { 0x0035, "wm_isactiveicon"          },
  85.     { 0x0036, "wm_queryparkicon"         },
  86.     { 0x0037, "WM_QUERYDRAGICON"         },
  87.     { 0x0038, "wm_querysavestate"        },
  88.     { 0x0039, "WM_COMPAREITEM"           },
  89.     { 0x0040, "wm_testing"               },
  90.     { 0x0041, "WM_COMPACTING"            },
  91.     { 0x0042, "wm_otherwindowcreated"    },
  92.     { 0x0043, "wm_otherwindowdestroyed"  },
  93.     { 0x0044, "WM_COMMNOTIFY"            },
  94.     { 0x0045, "wm_hotkeyevent"           },
  95.     { 0x0046, "WM_WINDOWPOSCHANGING"     },
  96.     { 0x0047, "WM_WINDOWPOSCHANGED"      },
  97.     { 0x0048, "WM_POWER"                 },
  98.     { 0x004A, "WM_COPYDATA"              },
  99.     { 0x004B, "WM_CANCELJOURNAL"         },
  100.    // 0x004C
  101.    // 0x004D
  102.     { 0x004E, "WM_NOTIFY"                },
  103.    // 0x004F
  104.     { 0x0050, "WM_INPUTLANGCHANGEREQUEST"},
  105.     { 0x0051, "WM_INPUTLANGCHANGE"       },
  106.     { 0x0052, "WM_TCARD"                 },
  107.     { 0x0053, "WM_HELP"                  },
  108.     { 0x0054, "WM_USERCHANGED"           },
  109.     { 0x0055, "WM_NOTIFYFORMAT"          },
  110.    // ...
  111.     { 0x007B, "WM_CONTEXTMENU"           },
  112.     { 0x007C, "WM_STYLECHANGING"         },
  113.     { 0x007D, "WM_STYLECHANGED"          },
  114.     { 0x007E, "WM_DISPLAYCHANGED"        },
  115.     { 0x007F, "WM_GETICON"               },
  116.     { 0x0080, "WM_SETICON"               },
  117.     { 0x0081, "WM_NCCREATE"              },
  118.     { 0x0082, "WM_NCDESTROY"             },
  119.     { 0x0083, "WM_NCCALCSIZE"            },
  120.     { 0x0084, "WM_NCHITTEST"             },
  121.     { 0x0085, "WM_NCPAINT"               },
  122.     { 0x0086, "WM_NCACTIVATE"            },
  123.     { 0x0087, "WM_GETDLGCODE"            },
  124.     { 0x0088, "wm_syncpaint"             },
  125.     { 0x0089, "wm_synctask"              },
  126.     { 0x00A0, "WM_NCMOUSEMOVE"           },
  127.     { 0x00A1, "WM_NCLBUTTONDOWN"         },
  128.     { 0x00A2, "WM_NCLBUTTONUP"           },
  129.     { 0x00A3, "WM_NCLBUTTONDBLCLK"       },
  130.     { 0x00A4, "WM_NCRBUTTONDOWN"         },
  131.     { 0x00A5, "WM_NCRBUTTONUP"           },
  132.     { 0x00A6, "WM_NCRBUTTONDBLCLK"       },
  133.     { 0x00A7, "WM_NCMBUTTONDOWN"         },
  134.     { 0x00A8, "WM_NCMBUTTONUP"           },
  135.     { 0x00A9, "WM_NCMBUTTONDBLCLK"       },
  136.    // ...
  137.     { 0x0100, "WM_KEYDOWN"               },
  138.     { 0x0101, "WM_KEYUP"                 },
  139.     { 0x0102, "WM_CHAR"                  },
  140.     { 0x0103, "WM_DEADCHAR"              },
  141.     { 0x0104, "WM_SYSKEYDOWN"            },
  142.     { 0x0105, "WM_SYSKEYUP"              },
  143.     { 0x0106, "WM_SYSCHAR"               },
  144.     { 0x0107, "WM_SYSDEADCHAR"           },
  145.     { 0x0108, "wm_yomichar"              },
  146.    // ...
  147.     { 0x010A, "wm_convertrequest"        },
  148.     { 0x010B, "wm_convertresult"         },
  149.     { 0x010C, "wm_interim"               },
  150.     { 0x010D, "WM_IME_STARTCOMPOSITION"  },
  151.     { 0x010E, "WM_IME_ENDCOMPOSITION"    },
  152.     { 0x010F, "WM_IME_COMPOSITION"       },
  153.     { 0x010F, "WM_IME_KEYLAST"           },
  154.     { 0x0110, "WM_INITDIALOG"            },
  155.     { 0x0111, "WM_COMMAND"               },
  156.     { 0x0112, "WM_SYSCOMMAND"            },
  157.     { 0x0113, "WM_TIMER"                 },
  158.     { 0x0114, "WM_HSCROLL"               },
  159.     { 0x0115, "WM_VSCROLL"               },
  160.     { 0x0116, "WM_INITMENU"              },
  161.     { 0x0117, "WM_INITMENUPOPUP"         },
  162.     { 0x0118, "wm_systimer"              },
  163.    // ...
  164.     { 0x011F, "WM_MENUSELECT"            },
  165.     { 0x0120, "WM_MENUCHAR"              },
  166.     { 0x0121, "WM_ENTERIDLE"             },
  167.     { 0x0131, "wm_lbtrackpoint"          },
  168.     { 0x0132, "WM_CTLCOLORMSGBOX"        },
  169.     { 0x0133, "WM_CTLCOLOREDIT"          },
  170.     { 0x0134, "WM_CTLCOLORLISTBOX"       },
  171.     { 0x0135, "WM_CTLCOLORBTN"           },
  172.     { 0x0136, "WM_CTLCOLORDLG"           },
  173.     { 0x0137, "WM_CTLCOLORSCROLLBAR"     },
  174.     { 0x0138, "WM_CTLCOLORSTATIC"        },
  175.    // ...
  176.     { 0x0200, "WM_MOUSEMOVE"             },
  177.     { 0x0201, "WM_LBUTTONDOWN"           },
  178.     { 0x0202, "WM_LBUTTONUP"             },
  179.     { 0x0203, "WM_LBUTTONDBLCLK"         },
  180.     { 0x0204, "WM_RBUTTONDOWN"           },
  181.     { 0x0205, "WM_RBUTTONUP"             },
  182.     { 0x0206, "WM_RBUTTONDBLCLK"         },
  183.     { 0x0207, "WM_MBUTTONDOWN"           },
  184.     { 0x0208, "WM_MBUTTONUP"             },
  185.     { 0x0209, "WM_MBUTTONDBLCLK"         },
  186.     { 0x0210, "WM_PARENTNOTIFY"          },
  187.     { 0x0211, "WM_ENTERMENULOOP"         },
  188.     { 0x0212, "WM_EXITMENULOOP"          },
  189.     { 0x0213, "WM_NEXTMENU"              },
  190.     { 0x0214, "WM_SIZING"                },
  191.     { 0x0215, "WM_CAPTURECHANGED"        },
  192.     { 0x0216, "WM_MOVING"                },
  193.     { 0x0218, "WM_POWERBROADCAST"        },
  194.     { 0x0219, "WM_DEVICECHANGE"          },
  195.    // ...
  196.     { 0x0220, "WM_MDICREATE"             },
  197.     { 0x0221, "WM_MDIDESTROY"            },
  198.     { 0x0222, "WM_MDIACTIVATE"           },
  199.     { 0x0223, "WM_MDIRESTORE"            },
  200.     { 0x0224, "WM_MDINEXT"               },
  201.     { 0x0225, "WM_MDIMAXIMIZE"           },
  202.     { 0x0226, "WM_MDITILE"               },
  203.     { 0x0227, "WM_MDICASCADE"            },
  204.     { 0x0228, "WM_MDIICONARRANGE"        },
  205.     { 0x0229, "WM_MDIGETACTIVE"          },
  206.     { 0x022A, "wm_dropobject"            },
  207.     { 0x022B, "wm_querydropobject"       },
  208.     { 0x022C, "wm_begindrag"             },
  209.     { 0x022D, "wm_dragloop"              },
  210.     { 0x022E, "wm_dragselect"            },
  211.     { 0x022F, "wm_dragmove"              },
  212.     { 0x0230, "WM_MDISETMENU"            },
  213.     { 0x0231, "WM_ENTERSIZEMOVE"         },
  214.     { 0x0232, "WM_EXITSIZEMOVE"          },
  215.     { 0x0233, "WM_DROPFILES"             },
  216.     { 0x0234, "WM_MDIREFRESHMENU"        },
  217.    // ...
  218.     { 0x0281, "WM_IME_SETCONTEXT"        },
  219.     { 0x0282, "WM_IME_NOTIFY"            },
  220.     { 0x0283, "WM_IME_CONTROL"           },
  221.     { 0x0284, "WM_IME_COMPOSITIONFULL"   },
  222.     { 0x0285, "WM_IME_SELECT"            },
  223.     { 0x0286, "WM_IME_CHAR"              },
  224.     { 0x0290, "WM_IME_KEYDOWN"           },
  225.     { 0x0291, "WM_IME_KEYUP"             },
  226.    // ...
  227.     { 0x0300, "WM_CUT"                   },
  228.     { 0x0301, "WM_COPY"                  },
  229.     { 0x0302, "WM_PASTE"                 },
  230.     { 0x0303, "WM_CLEAR"                 },
  231.     { 0x0304, "WM_UNDO"                  },
  232.     { 0x0305, "WM_RENDERFORMAT"          },
  233.     { 0x0306, "WM_RENDERALLFORMATS"      },
  234.     { 0x0307, "WM_DESTROYCLIPBOARD"      },
  235.     { 0x0308, "WM_DRAWCLIPBOARD"         },
  236.     { 0x0309, "WM_PAINTCLIPBOARD"        },
  237.     { 0x030A, "WM_VSCROLLCLIPBOARD"      },
  238.     { 0x030B, "WM_SIZECLIPBOARD"         },
  239.     { 0x030C, "WM_ASKCBFORMATNAME"       },
  240.     { 0x030D, "WM_CHANGECBCHAIN"         },
  241.     { 0x030E, "WM_HSCROLLCLIPBOARD"      },
  242.     { 0x030F, "WM_QUERYNEWPALETTE"       },
  243.     { 0x0310, "WM_PALETTEISCHANGING"     },
  244.     { 0x0311, "WM_PALETTECHANGED"        },
  245.     { 0x0312, "WM_HOTKEY"                },
  246.    // ...
  247.     { 0x0317, "WM_PRINT"                 },
  248.     { 0x0318, "WM_PRINTCLIENT"           },
  249.    // ...
  250.     { 0x0358, "WM_HANDHELDFIRST"         },
  251.    // ...
  252.     { 0x035F, "WM_HANDHELDLAST"          },
  253.     { 0x0360, "WM_AFXFIRST"              },
  254.    // ...
  255.     { 0x037F, "WM_AFXLAST"               },
  256.     { 0x0380, "WM_PENWINFIRST"           },
  257.    // ...
  258.     { 0x038F, "WM_PENWINLAST"            },
  259.    // ...
  260.     { 0x0400, "WM_USER"                  },
  261.    // ...
  262.     { 0x8000, "WM_APP"                   },
  263.   };
  264.  
  265.   //
  266.   // MsgCompare: this internal function is used by the qsort() and
  267.   // bsearch() functions to compare two StrTab elements.
  268.   //
  269.   static int
  270.   MsgCompare(const void* e1, const void* e2)
  271.   {
  272.     return ((TWMStr*)e1)->msg - ((TWMStr*)e2)->msg;
  273.   }
  274.  
  275.   ostream& operator <<(ostream& os, const MsgName& msg)
  276.   {
  277.     TWMStr  key;
  278.     TWMStr* item;
  279.  
  280.     // if it's a user message, then just show the offset value
  281.     if (msg.Message >= WM_USER)
  282.       return os << "WM_USER+" << hex << (msg.Message - WM_USER);
  283.  
  284.     // otherwise, search for it in the table
  285.     else {
  286.       // initialize the search key
  287.       key.msg = msg.Message;
  288.  
  289.       // let bsearch() do the work
  290.       item = (TWMStr*)bsearch(&key, StrTab, COUNTOF(StrTab), sizeof(TWMStr), MsgCompare);
  291.  
  292.       // if message found, then insert the name
  293.       if (item)
  294.         return os << item->str;
  295.  
  296.       // otherwise, just insert the message number
  297.       else
  298.         return os << "Message " << hex << msg.Message;
  299.     }
  300.   }
  301.  
  302. #if defined(BI_NAMESPACE)
  303. } // namespace OWL
  304. #endif
  305.  
  306. #endif // __TRACE || __WARN
  307.  
  308. //
  309. // Searches the list of response table entries looking for a match
  310. //
  311. // Since class TEventHandler doesn't have any entries, this default
  312. // implementation just returns false
  313. //
  314. bool
  315. TEventHandler::Find(TEventInfo&, TEqualOperator)
  316. {
  317.   return false;
  318. }
  319.  
  320. //
  321. // Dispatch a message to an event handler given a found response table entry
  322. //
  323. TResult
  324. TEventHandler::Dispatch(TEventInfo& eventInfo, TParam1 param1, TParam2 param2)
  325. {
  326.   PRECONDITION(eventInfo.Entry);
  327.   return (*eventInfo.Entry->Dispatcher)(
  328.            *eventInfo.Object,
  329.            (TAnyPMF __RTFAR&)eventInfo.Entry->Pmf,
  330.            param1, param2
  331.          );
  332. }
  333.  
  334. //
  335. // Search for the event given the message and it and dispatch to the
  336. // event handler if found.
  337. //
  338. TResult
  339. TEventHandler::DispatchMsg(uint msg, uint id, TParam1 p1, TParam2 p2)
  340. {
  341.   TEventInfo eventInfo(msg, id);
  342.   if (Find(eventInfo))
  343.     return Dispatch(eventInfo, p1, p2);
  344.   return 0;
  345. }
  346.  
  347. //
  348. // Low-level response table search function. Allows optional equal operator
  349. // function to be provided
  350. //
  351. // Fills in Entry member of eventInfo and returns true if found.
  352. //
  353. bool
  354. TEventHandler::SearchEntries(TGenericTableEntry __RTFAR* entries,
  355.                              TEventInfo&         eventInfo,
  356.                              TEqualOperator      equal)
  357. {
  358.   if (equal) {
  359.     while (entries->Dispatcher != 0) {
  360.       if (equal(*entries, eventInfo)) {
  361.         eventInfo.Entry = entries;
  362.         return true;
  363.       }
  364.       entries++;
  365.     }
  366.   }
  367.   else {
  368.     while (entries->Dispatcher != 0) {
  369.       if (entries->Msg == eventInfo.Msg && entries->Id == eventInfo.Id) {
  370.         eventInfo.Entry = entries;
  371.         return true;
  372.       }
  373.       entries++;
  374.     }
  375.   }
  376.  
  377.   return false;
  378. }
  379.