home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / cdrom / compactplayer / source / listwindow.c < prev    next >
C/C++ Source or Header  |  1995-12-28  |  16KB  |  479 lines

  1. #include "sysheaders.h"
  2. #include "cdpanel.h"
  3. #include "CompactPlayer.h"
  4.  
  5. /* Define as 0 to get the more traditional "click on line, edit it in a
  6.  * string gadget" editing. Default is using listbrowser's in place editing.
  7.  */
  8. #define INPLACEEDIT 1
  9.  
  10. /********************************************************
  11.  * CD Index editor
  12.  */
  13.  
  14. struct Window *ListWin;
  15. Object *ListWinObj;
  16. struct Gadget *ListLayout;
  17.  
  18. static struct List *TitleList;
  19.  
  20. static UBYTE artist[40], title[40], work1[40], work2[40];
  21. #if !INPLACEEDIT
  22. static UBYTE track[40];
  23. #endif
  24.  
  25. STRPTR IndexPath = "Disks";
  26.  
  27. static UBYTE cd[20];
  28.  
  29. BOOL
  30. GetIndex( ULONG t, ULONG a1, ULONG a2 )
  31. {
  32.     UBYTE buf[256];
  33.     BPTR f;
  34.  
  35.     /* Read an index file from the disk */
  36.  
  37.     strcpy( buf, IndexPath );
  38.     Sprintf( cd, "ID%02.2ld%06.6lx%06.6lx", t, a1, a2 );
  39.     AddPart( buf, cd, sizeof(buf)-1 );
  40.  
  41.     TOCP[0] = ~0;
  42.  
  43.     if (f = Open(buf, MODE_OLDFILE))
  44.     {
  45.         ULONG i;
  46.  
  47.         FGets(f, buf, sizeof(buf)-1);
  48.         stccpy(TITLE[0] = &TitleBuffer[0], buf, 40);
  49.         FGets(f, buf, sizeof(buf)-1);
  50.         stccpy(TITLE[1] = &TitleBuffer[40], buf, 40);
  51.  
  52.         for (i = 0 ; i < t ; i++)
  53.         {
  54.             FGets(f, buf, sizeof(buf)-1);
  55.             stccpy(TOCS[i] = &TitleBuffer[(i+2)*40], buf, 40);
  56.         }
  57.         TOCS[i] = NULL;
  58.  
  59.         for (i = 0 ; i < (t+2)*40 ; i++)
  60.         {
  61.             if (TitleBuffer[i] == '\n')
  62.                 TitleBuffer[i] = 0;
  63.         }
  64.  
  65.         Close(f);
  66.  
  67.         return TRUE;
  68.     }
  69.     return FALSE;
  70. }
  71.  
  72. void
  73. SaveIndex(void)
  74. {
  75.     UBYTE name[256];
  76.     BPTR f;
  77.  
  78.     /* Save an index file to disk */
  79.  
  80.     strcpy( name, IndexPath );
  81.     AddPart( name, cd, 256 );
  82.  
  83.     if (f = Open(name, MODE_NEWFILE))
  84.     {
  85.         ULONG i;
  86.  
  87.         FPuts(f, TITLE[0]);
  88.         FPutC(f, '\n');
  89.         FPuts(f, TITLE[1]);
  90.         FPutC(f, '\n');
  91.  
  92.         for (i = 0 ; TOCS[i] ; i++)
  93.         {
  94.             FPuts(f, TOCS[i]);
  95.             FPutC(f, '\n');
  96.         }
  97.  
  98.         Close(f);
  99.     }
  100. }
  101.  
  102. struct Window *
  103. OpenListWindow(ULONG X, ULONG Y)
  104. {
  105.     if (ListWin)
  106.         return ListWin;
  107.  
  108.     /* A very simple layout here. I won't bother even explaining it */
  109.  
  110.     ListLayout = VGroupObject, LAYOUT_SpaceOuter, TRUE,
  111.         GA_DrawInfo, Dri,
  112.         LAYOUT_DeferLayout, TRUE,
  113.         StartMember, GList[G_Artist] = StringObject,
  114.             STRINGA_WorkBuffer, work1,
  115.             STRINGA_UndoBuffer, work2,
  116.             STRINGA_Buffer, artist,
  117.             STRINGA_MaxChars, 39,
  118.             STRINGA_TextVal, "",
  119.             GA_RelVerify, TRUE,
  120.             GA_ID, G_Artist,
  121.             GA_TabCycle, TRUE,
  122.             End,
  123.             CHILD_Label, LabelObject, LABEL_Text, GS(STR_ARTIST), End,
  124.         StartMember, GList[G_Title] = StringObject,
  125.             STRINGA_WorkBuffer, work1,
  126.             STRINGA_UndoBuffer, work2,
  127.             STRINGA_Buffer, title,
  128.             STRINGA_MaxChars, 39,
  129.             STRINGA_TextVal, "",
  130.             GA_RelVerify, TRUE,
  131.             GA_ID, G_Title,
  132.             GA_TabCycle, TRUE,
  133.             End,
  134.             CHILD_Label, LabelObject, LABEL_Text, GS(STR_TITLE), End,
  135.         StartMember, GList[G_TitleList] = ListBrowserObject,
  136.             LISTBROWSER_Labels, &dummyList,
  137. #if !INPLACEEDIT
  138.             LISTBROWSER_ShowSelected, TRUE,
  139. #else
  140.             LISTBROWSER_Editable, TRUE,
  141. #endif
  142.             GA_RelVerify, TRUE,
  143.             GA_ID, G_TitleList,
  144.             End,
  145.             CHILD_MinHeight, ScreenFont->tf_YSize * 6,
  146.             CHILD_MinWidth, ScreenFont->tf_XSize * 20,
  147. #if !INPLACEEDIT
  148.         StartMember, GList[G_Track] = StringObject,
  149.             STRINGA_WorkBuffer, work1,
  150.             STRINGA_UndoBuffer, work2,
  151.             STRINGA_Buffer, track,
  152.             STRINGA_MaxChars, 39,
  153.             STRINGA_TextVal, "",
  154.             GA_Disabled, TRUE,
  155.             GA_RelVerify, TRUE,
  156.             GA_ID, G_Track,
  157.             GA_TabCycle, TRUE,
  158.             End,
  159. #endif
  160.         StartMember, GList[G_SaveTitles] = ButtonObject,
  161.             GA_Text, GS(SAVE_TITLES),
  162.             GA_RelVerify, TRUE,
  163.             GA_Disabled, TRUE,
  164.             GA_ID, G_SaveTitles,
  165.             End,
  166.             CHILD_NominalSize, TRUE,
  167.             CHILD_WeightedWidth, TRUE,
  168.         End;
  169.  
  170.     if (ListLayout)
  171.     {
  172.         if (ListWinObj = WindowObject,
  173.                 WA_DepthGadget,     TRUE,
  174.                 WA_CloseGadget,     TRUE,
  175.                 WA_DragBar,         TRUE,
  176.                 WA_Activate,        TRUE,
  177.                 WA_Top,             Y,
  178.                 WA_Left,            X,
  179.                 WA_PubScreen,       Scr,
  180.                 WA_Title,           GS(TITLE_EDITOR),
  181.                 WA_ScreenTitle,     GS(TITLE_COPYRIGHT),
  182.                 WA_AutoAdjust,      TRUE,
  183.                 WINDOW_Layout,      ListLayout,
  184.                 WINDOW_AppPort,     AppPort,
  185.                 WINDOW_SharedPort,  WinPort,
  186.                 TAG_END))
  187.         {
  188.             if (ListWin = CA_OpenWindow(ListWinObj))
  189.                 ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Artist]);
  190.  
  191.             return ListWin;
  192.         }
  193.         DisposeObject(ListLayout);
  194.         ListLayout = NULL;
  195.     }
  196.     ErrorMsg(GS(GUI_FAIL));
  197. }
  198.  
  199. void
  200. CloseListWindow(void)
  201. {
  202.     if (ListLayout)
  203.     {
  204.         SetGadgetAttrs(GList[G_TitleList], Win, NULL, LISTBROWSER_Labels, ~0, TAG_END);
  205.         FreeBrowserNodes(TitleList);
  206.         TitleList = NULL;
  207.     }
  208.     if (ListWinObj)
  209.     {
  210.         DisposeObject(ListWinObj);
  211.         ListWin = NULL;
  212.         ListWinObj = NULL;
  213.         ListLayout = NULL;
  214.         memset(&GList[G_Artist], 0, (ULONG)&GList[GG_MAX2]-(ULONG)&GList[G_Artist]);
  215.     }
  216. }
  217.  
  218. void
  219. ListWindowIDCMP(void)
  220. {
  221.     BOOL close = FALSE;
  222.     ULONG result;
  223.  
  224.     if (!ListWinObj)
  225.         return;
  226.  
  227.     while ((result = CA_HandleInput(ListWinObj,NULL)) != WMHI_LASTMSG)
  228.     {
  229.         /* CA_HandleInput (aka. WM_HANDLEINPUT) will translate keyboard events
  230.          * into GADGETUPs on its own */
  231.         switch (result & WMHI_CLASSMASK)
  232.         {
  233.         case WMHI_CLOSEWINDOW:
  234.             close = TRUE;
  235.             break;
  236.  
  237.         case WMHI_GADGETUP:
  238.             /* changing a gadget this window is reflected in the other window */
  239.             switch(result & WMHI_GADGETMASK)
  240.             {
  241.               case G_Artist:
  242.                 SetGadgetAttrs(GList[G_Panel], Win, NULL,
  243.                                CDPANEL_Artist, "",
  244.                                TAG_END);
  245.                 strcpy(TITLE[0], artist);
  246.                 SetGadgetAttrs(GList[G_Panel], Win, NULL,
  247.                                CDPANEL_Artist, TITLE[0],
  248.                                TAG_END);
  249.                 ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Title]);
  250.                 break;
  251.  
  252.               case G_Title:
  253.                 {
  254.                     struct Node *n = TitleList->lh_Head;
  255.                     ULONG status = 0;
  256.  
  257.                     strcpy(TITLE[1], title);
  258.  
  259.                     GetAttr(CDPANEL_Status, GList[G_Panel], &status);
  260.                     if (status != CDP_PLAYING && status != CDP_PAUSED)
  261.                         SetGadgetAttrs(GList[G_Panel], Win, NULL,
  262.                                        CDPANEL_Title, TITLE[1],
  263.                                        TAG_END);
  264.  
  265.                     if (SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  266.                                         LISTBROWSER_Selected, 0,
  267.                                         LISTBROWSER_MakeVisible, 0,
  268.                                         TAG_END))
  269.                         RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
  270. #if !INPLACEEDIT
  271.                     if (SetGadgetAttrs(GList[G_Track], ListWin, NULL,
  272.                                         STRINGA_TextVal, n->ln_Name,
  273.                                         GA_Disabled, FALSE,
  274.                                         TAG_END));
  275.                         RefreshGList(GList[G_Track], ListWin, NULL, 1);
  276.  
  277.                     ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Track]);
  278. #else
  279.                     SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  280.                                     LISTBROWSER_EditNode, 0,
  281.                                     LISTBROWSER_EditColumn, 0,
  282.                                     TAG_END);
  283.  
  284.                     ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_TitleList]);
  285. #endif
  286.                     break;
  287.                 }
  288.  
  289. #if !INPLACEEDIT
  290.               case G_TitleList:
  291.                 {
  292.                     struct Node *n;
  293.                     GetAttr(LISTBROWSER_SelectedNode, GList[G_TitleList], (ULONG *)&n);
  294.                     if (n)
  295.                     {
  296.                         if (SetGadgetAttrs(GList[G_Track], ListWin, NULL,
  297.                                             STRINGA_TextVal, n->ln_Name,
  298.                                             STRINGA_BufferPos, 0,
  299.                                             STRINGA_DispPos, 0,
  300.                                             GA_Disabled, FALSE,
  301.                                             TAG_END));
  302.                             RefreshGList(GList[G_Track], ListWin, NULL, 1);
  303.  
  304.                         ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Track]);
  305.                     }
  306.                     else
  307.                     {
  308.                         if (SetGadgetAttrs(GList[G_Track], ListWin, NULL,
  309.                                             STRINGA_TextVal, "",
  310.                                             GA_Disabled, TRUE,
  311.                                             TAG_END));
  312.                             RefreshGList(GList[G_Track], ListWin, NULL, 1);
  313.                     }
  314.                 }
  315.                 break;
  316.  
  317.               case G_Track:
  318.                 {
  319. #else
  320.               case G_TitleList:
  321.                 {
  322.                     STRPTR track;
  323.                     ULONG event = 0;
  324. #endif
  325.                     struct Node *n;
  326.                     ULONG i, ctrack;
  327.                     GetAttr(LISTBROWSER_SelectedNode, GList[G_TitleList], (ULONG *)&n);
  328.                     GetAttr(LISTBROWSER_Selected, GList[G_TitleList], &i);
  329.                     GetAttr(CDPANEL_Track, GList[G_Panel], &ctrack);
  330.  
  331.                     if (i == ~0 || !n)
  332.                         break;
  333.  
  334. #if !INPLACEEDIT
  335.                     SetGadgetAttrs(GList[G_List], Win, NULL,
  336.                                     LISTBROWSER_Labels, ~0,
  337.                                     TAG_END);
  338.  
  339.                     SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  340.                                     LISTBROWSER_Labels, ~0,
  341.                                     TAG_END);
  342. #else
  343.                     GetListBrowserNodeAttrs( n, LBNCA_Text, &track );
  344.                     GetAttr( LISTBROWSER_RelEvent, GList[G_TitleList], &event );
  345.  
  346.                     if (event == LBRE_EDIT)
  347.                     {
  348. #endif
  349.                     strcpy(TOCS[i], track);
  350.  
  351.                     if (i + 1 == ctrack) /* make panel gadget update itself */
  352.                     {
  353.                         SetGadgetAttrs(GList[G_Panel], Win, NULL,
  354.                                         CDPANEL_Track, 0,
  355.                                         CDPANEL_Track, i + 1,
  356.                                         TAG_END);
  357.                     }
  358.  
  359.                     if (TrackList)
  360.                     {
  361.                         FreeBrowserNodes(TrackList);
  362.  
  363.                         TrackList = BrowserNodesA(TOCS);
  364.  
  365.                         if (SetGadgetAttrs(GList[G_List], Win, NULL,
  366.                                             LISTBROWSER_Labels, TrackList,
  367.                                             TAG_END))
  368.                             RefreshGList(GList[G_List], Win, NULL, 1);
  369.                     }
  370. #if !INPLACEEDIT
  371.                     SetListBrowserNodeAttrs(n, LBNCA_Text, TOCS[i], TAG_END);
  372.  
  373.                     if (SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  374.                                         LISTBROWSER_Labels, TitleList,
  375.                                         TAG_END))
  376.                         RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
  377. #endif
  378.                     n = n->ln_Succ;
  379.                     if (n->ln_Succ)
  380.                     {
  381.                         if (SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  382.                                             LISTBROWSER_Selected, i+1,
  383.                                             LISTBROWSER_MakeVisible, i+1,
  384.                                             TAG_END))
  385.                             RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
  386. #if !INPLACEEDIT
  387.                         if (SetGadgetAttrs(GList[G_Track], ListWin, NULL,
  388.                                             STRINGA_TextVal, n->ln_Name,
  389.                                             STRINGA_BufferPos, 0,
  390.                                             STRINGA_DispPos, 0,
  391.                                             TAG_END));
  392.                             RefreshGList(GList[G_Track], ListWin, NULL, 1);
  393.  
  394.                         ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_Track]);
  395. #else
  396.                         SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  397.                                         LISTBROWSER_EditNode, i+1,
  398.                                         LISTBROWSER_EditColumn, 0,
  399.                                         TAG_END);
  400.  
  401.                         ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_TitleList]);
  402. #endif
  403.                     }
  404.                     else
  405.                     {
  406.                         if (SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  407.                                             LISTBROWSER_Selected, ~0,
  408.                                             TAG_END))
  409.                             RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
  410. #if !INPLACEEDIT
  411.                         if (SetGadgetAttrs(GList[G_Track], ListWin, NULL,
  412.                                             STRINGA_TextVal, "",
  413.                                             GA_Disabled, TRUE,
  414.                                             TAG_END));
  415.                             RefreshGList(GList[G_Track], ListWin, NULL, 1);
  416. #endif
  417.                     }
  418. #if INPLACEEDIT
  419.                     }
  420.                     else
  421.                     {
  422.                         SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  423.                                         LISTBROWSER_MakeVisible, i,
  424.                                         LISTBROWSER_EditNode, i,
  425.                                         LISTBROWSER_EditColumn, 0,
  426.                                         TAG_END);
  427.  
  428.                         ActivateLayoutGadget(ListLayout, ListWin, NULL, GList[G_TitleList]);
  429.                     }
  430. #endif
  431.                 }
  432.                 break;
  433.  
  434.               case G_SaveTitles:
  435.                 SaveIndex();
  436.                 break;
  437.             }
  438.             break;
  439.         }
  440.     }
  441.     if (close)
  442.         CloseListWindow();
  443. }
  444.  
  445. void
  446. UpdateTitleEditor(void)
  447. {
  448.     /* Changing a CD will trigger this function to update the contents of the
  449.      * editor. */
  450.     if (SetGadgetAttrs(GList[G_Artist], ListWin, NULL,
  451.                         STRINGA_TextVal, TITLE[0],
  452.                         TAG_END));
  453.         RefreshGList(GList[G_Artist], ListWin, NULL, 1);
  454.     if (SetGadgetAttrs(GList[G_Title], ListWin, NULL,
  455.                         STRINGA_TextVal, TITLE[1],
  456.                         TAG_END));
  457.         RefreshGList(GList[G_Title], ListWin, NULL, 1);
  458.     SetGadgetAttrs(GList[G_TitleList], Win, NULL,
  459.                     LISTBROWSER_Labels, ~0,
  460.                     TAG_END);
  461.     FreeBrowserNodes(TitleList);
  462.     TitleList = EditBrowserNodesA(TOCS);
  463.     if (SetGadgetAttrs(GList[G_TitleList], ListWin, NULL,
  464.                         LISTBROWSER_Labels, TitleList,
  465.                         LISTBROWSER_Selected, ~0,
  466.                         TAG_END))
  467.         RefreshGList(GList[G_TitleList], ListWin, NULL, 1);
  468. #if !INPLACEEDIT
  469.     if (SetGadgetAttrs(GList[G_Track], ListWin, NULL,
  470.                         GA_Disabled, TRUE,
  471.                         TAG_END));
  472.         RefreshGList(GList[G_Track], ListWin, NULL, 1);
  473. #endif
  474.     if (SetGadgetAttrs(GList[G_SaveTitles], ListWin, NULL,
  475.                         GA_Disabled, FALSE,
  476.                         TAG_END ))
  477.         RefreshGList(GList[G_SaveTitles], ListWin, NULL, 1);
  478. }
  479.