home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / muihelp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-23  |  7.5 KB  |  347 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** MUI Helper
  36. */
  37.  
  38. #include <sys/types.h>
  39. #include <stdlib.h>
  40. #include <stdarg.h>
  41.  
  42. #include <libraries/gadtools.h>
  43.  
  44. #include <MUI/NListview_mcc.h>
  45.  
  46. #include "muihelp.h"
  47.  
  48. muidefpix *loadPix(char *filename);
  49.  
  50. #ifdef __GNUC__
  51. /* inline version of this function doesn't work well */
  52. /* so this is the vararg stub which calls MUI_NewObjectA */
  53. static ULONG var2stack(int more, Tag tag1, va_list ap)
  54. {
  55.     static ULONG mav[50];
  56.     int i = 0;
  57.  
  58.     if(more) i = more;
  59.     mav[i] = tag1;
  60.     if(mav[i] != TAG_DONE)
  61.     {
  62.         mav[i+1] = va_arg(ap, ULONG);
  63.         i += 2;
  64.         while(i<49)
  65.         {
  66.             mav[i] = va_arg(ap, ULONG);
  67.             if(mav[i] == TAG_DONE) break;
  68.             mav[i+1] = va_arg(ap, ULONG);
  69.             if(mav[i] == TAG_MORE) break;
  70.             i += 2;
  71.         }
  72.     }
  73.  
  74.     if(more) return((ULONG)&mav[more]);
  75.     return (ULONG)&mav[0];
  76. }
  77.  
  78.  
  79. Object *MUI_NewObject(char *classname, Tag tag1,...)
  80. {
  81.     struct TagItem *ti;
  82.     va_list ap;
  83.  
  84.     va_start(ap, tag1);
  85.     ti = (struct TagItem *)var2stack(0, tag1, ap);
  86.     va_end(ap);
  87.  
  88.     return((Object *) MUI_NewObjectA(classname, ti));
  89. }
  90. #endif
  91.  
  92.  
  93. #ifdef __MORPHOS__
  94. /* ppc passes varargs on both registers and stack, so this is necessary */
  95. ULONG DoSuperNew(struct IClass *cl, Object *obj, ...)
  96. {
  97.     ULONG mav[50];
  98.     ULONG ret;
  99.     va_list tags;
  100.     struct opSet myopSet;
  101.     int i=0;
  102.  
  103.     va_start(tags, obj);
  104.     while(i<50)
  105.     {
  106.         mav[i] = va_arg(tags, ULONG);
  107.         if(mav[i] == TAG_DONE) break;
  108.         mav[i+1] = va_arg(tags, ULONG);
  109.         if(mav[i] == TAG_MORE) break;
  110.         i += 2;
  111.     }
  112.     va_end(tags);
  113.  
  114.     myopSet.MethodID = OM_NEW;
  115.     myopSet.ops_AttrList = (struct TagItem *) &mav;
  116.     myopSet.ops_GInfo = NULL;
  117.     ret = DoSuperMethodA(cl, obj, (APTR)&myopSet);
  118.  
  119.     return ret;
  120. }
  121. #else
  122. ULONG __stdargs DoSuperNew(struct IClass *cl,Object *obj,ULONG tag1,...)
  123. {
  124.     return(DoSuperMethod(cl, obj, OM_NEW, &tag1, NULL));
  125. }
  126. #endif
  127.  
  128.  
  129. Object *mui_pix(char *fname, muidefpix *dp, ULONG tag1, ...)
  130. {
  131.     Object *obj;
  132. #ifdef __MORPHOS__
  133.     struct TagItem *ti;
  134.     va_list ap;
  135.  
  136.     va_start(ap, tag1);
  137.     ti = (struct TagItem *)var2stack(25, tag1, ap);
  138.     va_end(ap);
  139. #endif
  140.  
  141.     if(fname) {
  142.         muidefpix *filePix = loadPix(fname);
  143.         if (filePix) {
  144.             dp = filePix;
  145.         }
  146.     }
  147.  
  148.     if(!dp) return(NULL);
  149.  
  150.     obj = MUI_NewObject(MUIC_Bodychunk,
  151.         MUIA_FixWidth, dp->w,
  152.         MUIA_FixHeight, dp->h,
  153.         MUIA_Bitmap_UseFriend, TRUE,
  154.         MUIA_Bitmap_Width, dp->w,
  155.         MUIA_Bitmap_Height, dp->h,
  156.         MUIA_Bitmap_SourceColors, dp->colors,
  157.         MUIA_Bodychunk_Body, dp->body,
  158.         MUIA_Bodychunk_Depth, dp->d,
  159.         MUIA_Bodychunk_Compression, dp->comp,
  160.         MUIA_Bodychunk_Masking, dp->mask,
  161. #ifdef __MORPHOS__
  162.         TAG_MORE, ti,
  163. #else
  164.         TAG_MORE, &tag1,
  165. #endif
  166.         TAG_DONE);
  167.  
  168.     return obj;
  169. }
  170.  
  171.  
  172. int mui_classes_setup(muiclass mcl[])
  173. {
  174.     int i=0;
  175.     struct MUI_CustomClass *tmp;
  176.  
  177.     while(mcl[i].supername)
  178.     {
  179.         tmp = (struct MUI_CustomClass *) MUI_CreateCustomClass(NULL, mcl[i].supername, NULL, mcl[i].datasize, mcl[i].dispatcher);
  180.         if(!tmp) return 0;
  181.         *mcl[i].ptr = tmp;
  182.         i++;
  183.     }
  184.     return 1;
  185. }
  186.  
  187.  
  188. void mui_classes_cleanup(muiclass mcl[])
  189. {
  190.     int i=0;
  191.  
  192.     while(mcl[i].supername)
  193.     {
  194.         if(*mcl[i].ptr) MUI_DeleteCustomClass(*mcl[i].ptr);
  195.         i++;
  196.     }
  197. }
  198.  
  199.  
  200. /*
  201. char *mui_string_file(
  202. {
  203.  
  204. }
  205.  
  206.  
  207. char *mui_string(int max)
  208. {
  209.     Object *str;
  210.  
  211.     if(max == 0) max = 256;
  212.     str = StringObject,
  213.         StringFrame,
  214.         MUIA_String_AdvanceOnCR, TRUE,
  215.         MUIA_String_MaxLen, max,
  216.         MUIA_CycleChain, 1,
  217.         TAG_DONE);
  218.  
  219.     return str;
  220. }
  221. */
  222.  
  223.  
  224. char *mui_sget(Object *sobj)
  225. {
  226.     char *tmp;
  227.     GetAttr(MUIA_String_Contents, sobj, (u_long *)&tmp);
  228.     if(tmp && tmp[0]!='\0') return(tmp);
  229.     return NULL;
  230. }
  231.  
  232.  
  233. u_long mui_id(Object *app, u_char *pre)
  234. {
  235.     static const char values[] = "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
  236.     u_long bitmap = 0;
  237.     long id = 0;
  238.     struct List *wins;
  239.     struct Node *lala;
  240.     Object *win;
  241.     int i;
  242.  
  243.     if(!pre[0] || !pre[1] || !pre[2]) return(0);
  244.     id = (pre[0] << 24) | (pre[1] << 16) | (pre[2] << 8);
  245.  
  246.     GetAttr(MUIA_Application_WindowList, app, (u_long *)&wins);
  247.     if(!wins) return(0);
  248.     lala = wins->lh_Head;
  249.     while(win = NextObject(&lala))
  250.     {
  251.         long id2;
  252.         char *s;
  253.         GetAttr(MUIA_Window_ID, win, (u_long *)&id2);
  254.         if(id == (id2 & 0xFFFFFF00))
  255.         {
  256.             s = (STRPTR) strchr(values, id2 & 0xFF);
  257.             if(s) bitmap |= (1L << (s - values));
  258.         }
  259.     }
  260.  
  261.     for(i=0; i<sizeof(values); i++)
  262.     {
  263.         if((bitmap & (1L << i)) == 0)
  264.         {
  265.             id |= values[i];
  266.             return id;
  267.         }
  268.     }
  269.  
  270.     id |= values[0];
  271.     return id;
  272. }
  273.  
  274.  
  275. Object *mui_button(const UBYTE *label)
  276. {
  277.     Object *but;
  278.  
  279.     but = (Object *) MUI_MakeObject(MUIO_Button, label);
  280.     if(but) set(but, MUIA_CycleChain, 1);
  281.     return but;
  282. }
  283.  
  284.  
  285. Object *mui_tmenu(const UBYTE *msg)
  286. {
  287.     char *title = (char *)msg;
  288.  
  289.     if(title[1]==0)
  290.         return(MUI_NewObject(MUIC_Menuitem,
  291.                MUIA_Menuitem_Title, title+2,
  292.                MUIA_Menuitem_Shortcut, title,
  293.                MUIA_Menuitem_Toggle, TRUE,
  294.                MUIA_Menuitem_Checkit, TRUE,
  295.                TAG_DONE));
  296.     else
  297.         return(MUI_NewObject(MUIC_Menuitem,
  298.                MUIA_Menuitem_Title, title,
  299.                MUIA_Menuitem_Toggle, TRUE,
  300.                MUIA_Menuitem_Checkit, TRUE,
  301.                TAG_DONE));
  302. }
  303.  
  304.  
  305. Object *mui_menu(const UBYTE *msg)
  306. {
  307.     char *title = (char *)msg;
  308.  
  309.     if(msg==NULL) return(MUI_NewObject(MUIC_Menuitem, MUIA_Menuitem_Title, NM_BARLABEL, TAG_DONE));
  310.  
  311.     if(title[1]==0)
  312.         return(MUI_NewObject(MUIC_Menuitem,
  313.                MUIA_Menuitem_Title, title+2,
  314.                MUIA_Menuitem_Shortcut, title,
  315.                TAG_DONE));
  316.     else
  317.         return(MUI_NewObject(MUIC_Menuitem, MUIA_Menuitem_Title , title, TAG_DONE));
  318. }
  319.  
  320.  
  321. Object *mui_toggle(const UBYTE *msg, Object **tog)
  322. {
  323.     Object *obj,*obj2;
  324.  
  325.     obj = MUI_NewObject(MUIC_Group,
  326.         MUIA_Group_Horiz, TRUE,
  327.         Child, obj2 = MUI_NewObject(MUIC_Image,
  328.             ImageButtonFrame,
  329.             MUIA_Background, MUII_ButtonBack,
  330.             MUIA_InputMode, MUIV_InputMode_Toggle,
  331.             MUIA_Image_Spec, MUII_CheckMark,
  332.             MUIA_Image_FreeVert, TRUE,
  333.             MUIA_ShowSelState, FALSE,
  334.             MUIA_CycleChain, 1,
  335.         TAG_DONE),
  336.         Child, LLabel1(msg),
  337.         Child, HSpace(0),
  338.     TAG_DONE);
  339.  
  340.     if(obj) {
  341.         *tog = obj2;
  342.         return(obj);
  343.     } else
  344.         return(NULL);
  345. }
  346.  
  347.