/* Code to print windows messages */
/* mdh 9/30/03 */

#ifdef DEBUGCHATTY

/* This table generated from cygwin /usr/include/w32api/winuser.h
 * with :
 * grep WM_ /usr/include/w32api/winuser.h |
 *          sed 's/#define \([^ ]*\) \(.*\)/{\2,\"\1\"},/' > foo
 * then edited!
 */
#define WM_USER 1024

struct wmsginfo { int num; char *msg };

struct wmsginfo msginfo[] = {
{6,"WM_ACTIVATE"},
{28,"WM_ACTIVATEAPP"},
/* FIXME/CHECK: Are WM_AFX{FIRST,LAST} valid for WINVER < 0x400? */
{864,"WM_AFXFIRST"},
{895,"WM_AFXLAST"},
{780,"WM_ASKCBFORMATNAME"},
{75,"WM_CANCELJOURNAL"},
{31,"WM_CANCELMODE"},
{533,"WM_CAPTURECHANGED"},
{781,"WM_CHANGECBCHAIN"},
{258,"WM_CHAR"},
{47,"WM_CHARTOITEM"},
{34,"WM_CHILDACTIVATE"},
{771,"WM_CLEAR"},
{16,"WM_CLOSE"},
{273,"WM_COMMAND"},
{68		/* obsolete */,"WM_COMMNOTIFY"},
{65,"WM_COMPACTING"},
{57,"WM_COMPAREITEM"},
{123,"WM_CONTEXTMENU"},
{769,"WM_COPY"},
{74,"WM_COPYDATA"},
{1,"WM_CREATE"},
{309,"WM_CTLCOLORBTN"},
{310,"WM_CTLCOLORDLG"},
{307,"WM_CTLCOLOREDIT"},
{308,"WM_CTLCOLORLISTBOX"},
{306,"WM_CTLCOLORMSGBOX"},
{311,"WM_CTLCOLORSCROLLBAR"},
{312,"WM_CTLCOLORSTATIC"},
{768,"WM_CUT"},
{259,"WM_DEADCHAR"},
{45,"WM_DELETEITEM"},
{2,"WM_DESTROY"},
{775,"WM_DESTROYCLIPBOARD"},
{537,"WM_DEVICECHANGE"},
{27,"WM_DEVMODECHANGE"},
{126,"WM_DISPLAYCHANGE"},
{776,"WM_DRAWCLIPBOARD"},
{43,"WM_DRAWITEM"},
{563,"WM_DROPFILES"},
{10,"WM_ENABLE"},
{22,"WM_ENDSESSION"},
{289,"WM_ENTERIDLE"},
{529,"WM_ENTERMENULOOP"},
{561,"WM_ENTERSIZEMOVE"},
{20,"WM_ERASEBKGND"},
{530,"WM_EXITMENULOOP"},
{562,"WM_EXITSIZEMOVE"},
{29,"WM_FONTCHANGE"},
{135,"WM_GETDLGCODE"},
{49,"WM_GETFONT"},
{51,"WM_GETHOTKEY"},
{127,"WM_GETICON"},
{36,"WM_GETMINMAXINFO"},
{13,"WM_GETTEXT"},
{14,"WM_GETTEXTLENGTH"},
/* FIXME/CHECK: Are WM_HANDHEL{FIRST,LAST} valid for WINVER < 0x400? */
{856,"WM_HANDHELDFIRST"},
{863,"WM_HANDHELDLAST"},
{83,"WM_HELP"},
{786,"WM_HOTKEY"},
{276,"WM_HSCROLL"},
{782,"WM_HSCROLLCLIPBOARD"},
{39,"WM_ICONERASEBKGND"},
{272,"WM_INITDIALOG"},
{278,"WM_INITMENU"},
{279,"WM_INITMENUPOPUP"},
{81,"WM_INPUTLANGCHANGE"},
{80,"WM_INPUTLANGCHANGEREQUEST"},
{256,"WM_KEYDOWN"},
{257,"WM_KEYUP"},
{8,"WM_KILLFOCUS"},
{546,"WM_MDIACTIVATE"},
{551,"WM_MDICASCADE"},
{544,"WM_MDICREATE"},
{545,"WM_MDIDESTROY"},
{553,"WM_MDIGETACTIVE"},
{552,"WM_MDIICONARRANGE"},
{549,"WM_MDIMAXIMIZE"},
{548,"WM_MDINEXT"},
{564,"WM_MDIREFRESHMENU"},
{547,"WM_MDIRESTORE"},
{560,"WM_MDISETMENU"},
{550,"WM_MDITILE"},
{44,"WM_MEASUREITEM"},
{290,"WM_MENURBUTTONUP"},
{288,"WM_MENUCHAR"},
{287,"WM_MENUSELECT"},
{531,"WM_NEXTMENU"},
{3,"WM_MOVE"},
{534,"WM_MOVING"},
{134,"WM_NCACTIVATE"},
{131,"WM_NCCALCSIZE"},
{129,"WM_NCCREATE"},
{130,"WM_NCDESTROY"},
{132,"WM_NCHITTEST"},
{163,"WM_NCLBUTTONDBLCLK"},
{161,"WM_NCLBUTTONDOWN"},
{162,"WM_NCLBUTTONUP"},
{169,"WM_NCMBUTTONDBLCLK"},
{167,"WM_NCMBUTTONDOWN"},
{168,"WM_NCMBUTTONUP"},
{160,"WM_NCMOUSEMOVE"},
{133,"WM_NCPAINT"},
{166,"WM_NCRBUTTONDBLCLK"},
{164,"WM_NCRBUTTONDOWN"},
{165,"WM_NCRBUTTONUP"},
{40,"WM_NEXTDLGCTL"},
{531,"WM_NEXTMENU"},
{78,"WM_NOTIFY"},
{85,"WM_NOTIFYFORMAT"},
{0,"WM_NULL"},
{15,"WM_PAINT"},
{777,"WM_PAINTCLIPBOARD"},
{38,"WM_PAINTICON"},
{785,"WM_PALETTECHANGED"},
{784,"WM_PALETTEISCHANGING"},
{528,"WM_PARENTNOTIFY"},
{770,"WM_PASTE"},
{896,"WM_PENWINFIRST"},
{911,"WM_PENWINLAST"},
{72,"WM_POWER"},
{536,"WM_POWERBROADCAST"},
{791,"WM_PRINT"},
{792,"WM_PRINTCLIENT"},
{55,"WM_QUERYDRAGICON"},
{17,"WM_QUERYENDSESSION"},
{783,"WM_QUERYNEWPALETTE"},
{19,"WM_QUERYOPEN"},
{35,"WM_QUEUESYNC"},
{18,"WM_QUIT"},
{774,"WM_RENDERALLFORMATS"},
{773,"WM_RENDERFORMAT"},
{32,"WM_SETCURSOR"},
{7,"WM_SETFOCUS"},
{48,"WM_SETFONT"},
{50,"WM_SETHOTKEY"},
{128,"WM_SETICON"},
{11,"WM_SETREDRAW"},
{12,"WM_SETTEXT"},
{26,"WM_SETTINGCHANGE"},
{24,"WM_SHOWWINDOW"},
{5,"WM_SIZE"},
{779,"WM_SIZECLIPBOARD"},
{532,"WM_SIZING"},
{42,"WM_SPOOLERSTATUS"},
{125,"WM_STYLECHANGED"},
{124,"WM_STYLECHANGING"},
{262,"WM_SYSCHAR"},
{21,"WM_SYSCOLORCHANGE"},
{274,"WM_SYSCOMMAND"},
{263,"WM_SYSDEADCHAR"},
{260,"WM_SYSKEYDOWN"},
{261,"WM_SYSKEYUP"},
{82,"WM_TCARD"},
{30,"WM_TIMECHANGE"},
{275,"WM_TIMER"},
{772,"WM_UNDO"},
{1024,"WM_USER"},
{84,"WM_USERCHANGED"},
{46,"WM_VKEYTOITEM"},
{277,"WM_VSCROLL"},
{778,"WM_VSCROLLCLIPBOARD"},
{71,"WM_WINDOWPOSCHANGED"},
{70,"WM_WINDOWPOSCHANGING"},
{26,"WM_WININICHANGE"},
{256,"WM_KEYFIRST"},
{264,"WM_KEYLAST"},
{ 136,"WM_SYNCPAINT"},
{33,"WM_MOUSEACTIVATE"},
{512,"WM_MOUSEMOVE"},
{513,"WM_LBUTTONDOWN"},
{514,"WM_LBUTTONUP"},
{515,"WM_LBUTTONDBLCLK"},
{516,"WM_RBUTTONDOWN"},
{517,"WM_RBUTTONUP"},
{518,"WM_RBUTTONDBLCLK"},
{519,"WM_MBUTTONDOWN"},
{520,"WM_MBUTTONUP"},
{521,"WM_MBUTTONDBLCLK"},
{522,"WM_MOUSEWHEEL"},
{512,"WM_MOUSEFIRST"},
{522,"WM_MOUSELAST"},
{0x2A1, "WM_MOUSEHOVER"},
{0x2A3, "WM_MOUSELEAVE"},
{WM_USER,"DM_GETDEFID"},
{(WM_USER+1),"DM_SETDEFID"},
{(WM_USER+2),"DM_REPOSITION"},
{(WM_USER+100),"PSM_PAGEINFO"},
{(WM_USER+101),"PSM_SHEETINFO"},
{0xc,"HELP_WM_HELP"},
{32768,"WM_APP"}
};

#define MAX_TEXT 2048

static char *msgstr[MAX_TEXT];
static char *unknown = "(unknown)";

static int need_init = 1;

char *msgtotext(int msg)
{
  int i;
  if (need_init) {
    printf("Init messages\n");
    for (i=0; i < MAX_TEXT; i++) msgstr[i] = unknown;
    for (i=0; msginfo[i].num < 32767; i++) {
      if (msginfo[i].num >= MAX_TEXT)
	printf("Warning: ignore %d, %s\n", msginfo[i].num,
	       msginfo[i].msg);
      else {
	if (msgstr[msginfo[i].num] != unknown)
	  printf("Warning: duplicate %d %s and %s\n", msginfo[i].num,
		 msgstr[msginfo[i].num], msginfo[i].msg);
	else
	  msgstr[msginfo[i].num] = msginfo[i].msg;
      }
    }
    need_init = 0;
  }
  if (msg >= MAX_TEXT)
    return unknown;
  else
    return msgstr[msg];
}

#else /* not DEBUGCHATTY */
char *msgtotext(int msg)
{
  return "(msgtotext)";
}
#endif /* DEBUGCHATTY */
