home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Mac / Menus.xs < prev    next >
Text File  |  1998-04-05  |  9KB  |  541 lines

  1. /* $Header: /home/neeri/MacCVS/MacPerl/perl/ext/Mac/Menus/Menus.xs,v 1.2 1997/11/18 00:52:37 neeri Exp $
  2.  *
  3.  *    Copyright (c) 1996 Matthias Neeracher
  4.  *
  5.  *    You may distribute under the terms of the Perl Artistic License,
  6.  *    as specified in the README file.
  7.  *
  8.  * $Log: Menus.xs,v $
  9.  * Revision 1.2  1997/11/18 00:52:37  neeri
  10.  * MacPerl 5.1.5
  11.  *
  12.  * Revision 1.1  1997/04/07 20:49:59  neeri
  13.  * Synchronized with MacPerl 5.1.4a1
  14.  *
  15.  */
  16.  
  17. #define MAC_CONTEXT
  18.  
  19. #include "EXTERN.h"
  20. #include "perl.h"
  21. #include "XSUB.h"
  22. #include <Types.h>
  23. #include <Memory.h>
  24. #include <Menus.h>
  25.  
  26.  
  27. static void WipeFilter(void)
  28. {
  29.     gFilterMenu = nil;
  30. }
  31.  
  32. static Boolean PrepareMenus()
  33. {
  34.     dSP ;
  35.     
  36.     PUSHMARK(sp) ;
  37.     
  38.     perl_call_pv("Mac::Menus::_PrepareMenus", G_DISCARD|G_NOARGS);
  39.  
  40.     return true;
  41. }
  42.  
  43. static Boolean FilterMenu(long menuSelection)
  44. {
  45.     HV *    menus;
  46.     SV **   handler;
  47.     char    code[10];
  48.     
  49.     if (menuSelection == -1)
  50.         return PrepareMenus();
  51.         
  52.     sprintf(code, "%08X", menuSelection);
  53.     menus   = perl_get_hv("Mac::Menus::Menu", 2);
  54.     (handler = hv_fetch(menus, code, 4, 0))
  55.         || (handler = hv_fetch(menus, code, 8, 0));
  56.         
  57.     if (handler) {
  58.         int res ;
  59.         SV * result;
  60.         dSP ;
  61.  
  62.         ENTER ;
  63.         SAVETMPS;
  64.  
  65.         PUSHMARK(sp) ;
  66.         XPUSHs(sv_2mortal(newSViv((menuSelection >> 16) & 0x0FFFF)));
  67.         XPUSHs(sv_2mortal(newSViv(menuSelection         & 0x0FFFF)));
  68.         PUTBACK ;
  69.  
  70.         res = perl_call_pv("Mac::Menus::_HandleMenu", G_SCALAR);
  71.  
  72.         SPAGAIN ;
  73.  
  74.         result  = POPs;
  75.         res     = SvTRUE(result);
  76.  
  77.         PUTBACK ;
  78.         FREETMPS ;
  79.         LEAVE ;
  80.         
  81.         return res != 0;
  82.     }
  83.     return false;
  84. }
  85.  
  86. static pascal void CallMDEF(
  87.     short message, MenuHandle menu, Rect * menuRect, Point hitPt, short * item)
  88. {
  89.     dSP ;
  90.  
  91.     ENTER ;
  92.     SAVETMPS;
  93.  
  94.     PUSHMARK(sp) ;
  95.     XS_XPUSH(short, message);
  96.     XS_XPUSH(MenuHandle, menu);
  97.     switch (message) {
  98.     case mDrawMsg:
  99.         XS_XPUSH(Rect, *menuRect);
  100.         break;
  101.     case mChooseMsg:
  102.         XS_XPUSH(Rect, *menuRect);
  103.         XS_XPUSH(Point, hitPt);
  104.         XS_XPUSH(short, *item);
  105.         break;
  106.     case mSizeMsg:
  107.         break;
  108.     case mPopUpMsg:
  109.         XS_XPUSH(Point, hitPt);
  110.         break;      
  111.     }
  112.     PUTBACK ;
  113.  
  114.     perl_call_pv("Mac::Menus::_MenuDefProc", G_SCALAR);
  115.  
  116.     SPAGAIN ;
  117.  
  118.     switch (message) {
  119.     case mDrawMsg:
  120.     case mSizeMsg:
  121.         break;
  122.     case mChooseMsg:
  123.         XS_POP(short, *item);
  124.         break;
  125.     case mPopUpMsg:
  126.         XS_POP(Rect, *menuRect);
  127.         break;      
  128.     }
  129.  
  130.     PUTBACK ;
  131.     FREETMPS ;
  132.     LEAVE ;
  133. }
  134.  
  135. #if GENERATINGCFM
  136. RoutineDescriptor sCallMDEF = 
  137.     BUILD_ROUTINE_DESCRIPTOR(uppMenuDefProcInfo, CallMDEF);
  138. #else
  139. struct {
  140.     short   jmp;
  141.     void *  addr;
  142. } sCallMDEF = {0x4EF9, CallMDEF};
  143. #endif
  144. static Handle   sMDEF;
  145. static int      sMDEFRefCount;
  146.  
  147. MODULE = Mac::Menus PACKAGE = Mac::Menus
  148.  
  149. BOOT:
  150. gFilterMenu = FilterMenu;
  151. atexit(WipeFilter);
  152.  
  153. STRUCT ** MenuHandle
  154.     short       menuID;
  155.     short       menuWidth;
  156.     short       menuHeight;
  157.     long        enableFlags;
  158.     Str255      menuData;
  159.  
  160. =head2 Functions
  161.  
  162. =over 4
  163.  
  164. =item HEIGHT = GetMBarHeight()
  165.  
  166. =cut
  167. short
  168. GetMBarHeight()
  169.  
  170. =item MENU = NewMenu ID, TITLE
  171.  
  172. =cut
  173. MenuHandle
  174. NewMenu(menuID, menuTitle)
  175.     short   menuID
  176.     Str255  menuTitle
  177.  
  178. =item MENU = GetMenu ID
  179.  
  180. =cut
  181. MenuHandle
  182. GetMenu(resourceID)
  183.     short   resourceID
  184.  
  185. =item DisposeMenu MENU
  186.  
  187. =cut
  188. void
  189. _DisposeMenu(theMenu)
  190.     MenuHandle  theMenu
  191.     CODE:
  192.     if (sMDEFRefCount && theMenu[0]->menuProc == sMDEF)
  193.         if (!--sMDEFRefCount)
  194.             DisposeHandle(sMDEF);
  195.     DisposeMenu(theMenu);
  196.  
  197. =item AppendMenu MENU, DATA
  198.  
  199. =cut
  200. void
  201. AppendMenu(menu, data)
  202.     MenuHandle  menu
  203.     Str255      data
  204.  
  205. =item AppendResMenu MENU, TYPE
  206.  
  207. =cut
  208. void
  209. AppendResMenu(theMenu, theType)
  210.     MenuHandle  theMenu
  211.     OSType      theType
  212.  
  213. =item InsertResMenu MENU, TYPE, AFTERITEM
  214.  
  215. =cut
  216. void
  217. InsertResMenu(theMenu, theType, afterItem)
  218.     MenuHandle  theMenu
  219.     OSType  theType
  220.     short   afterItem
  221.  
  222. =item InsertMenu MENU [, BEFOREID]
  223.  
  224. =cut
  225. void
  226. InsertMenu(theMenu, beforeID=0)
  227.     MenuHandle  theMenu
  228.     short       beforeID
  229.  
  230. =item DrawMenuBar()
  231.  
  232. =cut
  233. void
  234. DrawMenuBar()
  235.  
  236. =item InvalMenuBar()
  237.  
  238. =cut
  239. void
  240. InvalMenuBar()
  241.  
  242. =item DeleteMenu ID
  243.  
  244. =cut
  245. void
  246. DeleteMenu(menuID)
  247.     short   menuID
  248.  
  249. =item ClearMenuBar()
  250.  
  251. =cut
  252. void
  253. ClearMenuBar()
  254.  
  255. =item MENUBAR = GetNewMBar ID
  256.  
  257. =cut
  258. Handle
  259. GetNewMBar(menuBarID)
  260.     short   menuBarID
  261.  
  262. =item MENUBAR = GetMenuBar()
  263.  
  264. =cut
  265. Handle
  266. GetMenuBar()
  267.  
  268. =item SetMenuBar MENUBAR
  269.  
  270. =cut
  271. void
  272. SetMenuBar(menuList)
  273.     Handle  menuList
  274.  
  275. =item InsertMenuItem MENU, ITEMS, AFTERITEM
  276.  
  277. =cut
  278. void
  279. InsertMenuItem(theMenu, itemString, afterItem)
  280.     MenuHandle  theMenu
  281.     Str255      itemString
  282.     short       afterItem
  283.  
  284. =item DeleteMenuItem MENU, ITEM
  285.  
  286. =cut
  287. void
  288. DeleteMenuItem(theMenu, item)
  289.     MenuHandle  theMenu
  290.     short       item
  291.  
  292. =item HiliteMenu ID
  293.  
  294. =cut
  295. void
  296. HiliteMenu(menuID)
  297.     short   menuID
  298.  
  299. =item SetMenuItemText MENU, ITEM, TEXT
  300.  
  301. =cut
  302. void
  303. SetMenuItemText(theMenu, item, itemString)
  304.     MenuHandle  theMenu
  305.     short       item
  306.     Str255      itemString
  307.  
  308. =item TEXT = GetMenuItemText MENU, ITEM
  309.  
  310. =cut
  311. Str255
  312. GetMenuItemText(theMenu, item)
  313.     MenuHandle  theMenu
  314.     short       item
  315.     CODE:
  316.     GetMenuItemText(theMenu, item, RETVAL);
  317.     OUTPUT:
  318.     RETVAL
  319.  
  320. =item DisableItem MENU [, ITEM]
  321.  
  322. =cut
  323. void
  324. DisableItem(theMenu, item=0)
  325.     MenuHandle  theMenu
  326.     short       item
  327.  
  328. =item EnableItem MENU [, ITEM]
  329.  
  330. =cut
  331. void
  332. EnableItem(theMenu, item=0)
  333.     MenuHandle  theMenu
  334.     short       item
  335.  
  336. =item CheckItem MENU, ITEM, CHECKED
  337.  
  338. =cut
  339. void
  340. CheckItem(theMenu, item, checked)
  341.     MenuHandle  theMenu
  342.     short       item
  343.     Boolean     checked
  344.  
  345. =item SetItemMark MENU, ITEM, MARK
  346.  
  347. =cut
  348. void
  349. SetItemMark(theMenu, item, markChar)
  350.     MenuHandle  theMenu
  351.     short       item
  352.     char        markChar
  353.  
  354. =item MARK = GetItemMark MENU, ITEM
  355.  
  356. =cut
  357. char
  358. GetItemMark(theMenu, item)
  359.     MenuHandle  theMenu
  360.     short       item
  361.     CODE:
  362.     {
  363.         short markChar;
  364.         GetItemMark(theMenu, item, &markChar);
  365.         RETVAL = (char) markChar;
  366.     }
  367.     OUTPUT:
  368.     RETVAL
  369.  
  370. =item SetItemIcon MENU, ITEM, ICON
  371.  
  372. =cut
  373. void
  374. SetItemIcon(theMenu, item, iconIndex)
  375.     MenuHandle  theMenu
  376.     short       item
  377.     short       iconIndex
  378.  
  379. =item ICON = GetItemIcon MENU, ITEM
  380.  
  381. =cut
  382. short
  383. GetItemIcon(theMenu, item)
  384.     MenuHandle  theMenu
  385.     short   item
  386.     CODE:
  387.     GetItemIcon(theMenu, item, &RETVAL);
  388.     OUTPUT:
  389.     RETVAL
  390.  
  391. =item SetItemStyle MENU, ITEM, STYLE
  392.  
  393. =cut
  394. void
  395. SetItemStyle(theMenu, item, chStyle)
  396.     MenuHandle  theMenu
  397.     short       item
  398.     short       chStyle
  399.  
  400. =item STYLE = GetItemStyle MENU, ITEM
  401.  
  402. =cut
  403. short
  404. GetItemStyle(theMenu, item)
  405.     MenuHandle  theMenu
  406.     short   item
  407.     CODE:
  408.     {
  409.         Style   chStyle;
  410.         GetItemStyle(theMenu, item, &chStyle);
  411.         RETVAL = chStyle;
  412.     }
  413.     OUTPUT:
  414.     RETVAL
  415.  
  416. =item CalcMenuSize MENU
  417.  
  418. =cut
  419. void
  420. CalcMenuSize(theMenu)
  421.     MenuHandle  theMenu
  422.  
  423. =item COUNT = CountMItems MENU
  424.  
  425. =cut
  426. short
  427. CountMItems(theMenu)
  428.     MenuHandle  theMenu
  429.  
  430. =item MENU = GetMenuHandle ID
  431.  
  432. =cut
  433. MenuHandle
  434. GetMenuHandle(menuID)
  435.     short   menuID
  436.  
  437. =item FlashMenuBar ID
  438.  
  439. =cut
  440. void
  441. FlashMenuBar(menuID)
  442.     short   menuID
  443.  
  444. =item SetMenuFlash COUNT
  445.  
  446. =cut
  447. void
  448. SetMenuFlash(count)
  449.     short   count
  450.  
  451. =item CMD = GetItemCmd MENU, ITEM
  452.  
  453. =cut
  454. char
  455. GetItemCmd(theMenu, item)
  456.     MenuHandle  theMenu
  457.     short       item
  458.     CODE:
  459.     {
  460.         short   cmdChar;
  461.         GetItemCmd(theMenu, item, &cmdChar);
  462.         RETVAL = (char) cmdChar;
  463.     }
  464.     OUTPUT:
  465.     RETVAL
  466.  
  467. =item SetItemCmd MENU, ITEM, COMMAND
  468.  
  469. =cut
  470. void
  471. SetItemCmd(theMenu, item, cmdChar)
  472.     MenuHandle  theMenu
  473.     short       item
  474.     char        cmdChar
  475.  
  476. void
  477. _PopUpMenuSelect(menu, top, left, popUpItem)
  478.     MenuHandle  menu
  479.     short   top
  480.     short   left
  481.     short   popUpItem
  482.     PPCODE:
  483.     {
  484.         long res = PopUpMenuSelect(menu, top, left, popUpItem);
  485.         if (!res) {
  486.             XSRETURN_EMPTY;
  487.         } else {
  488.             EXTEND(sp,2);
  489.             PUSHs(sv_2mortal(newSViv((res >> 16) & 0x0FFFF)));
  490.             PUSHs(sv_2mortal(newSViv(res & 0x0FFFF)));
  491.         }
  492.     }
  493.  
  494. void
  495. _MenuChoice()
  496.     PPCODE:
  497.     {
  498.         long res = MenuChoice();
  499.         if (!res) {
  500.             XSRETURN_EMPTY;
  501.         } else {
  502.             EXTEND(sp,2);
  503.             PUSHs(sv_2mortal(newSViv((res >> 16) & 0x0FFFF)));
  504.             PUSHs(sv_2mortal(newSViv(res & 0x0FFFF)));
  505.         }
  506.     }
  507.  
  508. =item InsertFontResMenu MENU, AFTERITEM, SCRIPTFILTER
  509.  
  510. =cut
  511. void
  512. InsertFontResMenu(theMenu, afterItem, scriptFilter)
  513.     MenuHandle  theMenu
  514.     short       afterItem
  515.     short       scriptFilter
  516.  
  517. =item InsertIntlResMenu MENU, TYPE, AFTERITEM, SCRIPTFILTER
  518.  
  519. =cut
  520. void
  521. InsertIntlResMenu(theMenu, theType, afterItem, scriptFilter)
  522.     MenuHandle  theMenu
  523.     OSType      theType
  524.     short       afterItem
  525.     short       scriptFilter
  526.  
  527. void
  528. _SetMDEFProc(theMenu)
  529.     MenuHandle  theMenu
  530.     CODE:
  531.     if (theMenu[0]->menuProc != sMDEF) {
  532.         if (!sMDEFRefCount++) {
  533.             PtrToHand((Ptr)&sCallMDEF, &sMDEF, sizeof(sCallMDEF));
  534. #if !GENERATINGCFM
  535.             FlushInstructionCache();
  536.             FlushDataCache();
  537. #endif
  538.         }
  539.         theMenu[0]->menuProc = sMDEF;
  540.     }
  541.