home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / progect-src-0.20.tar.gz / progect-src-0.20.tar / progect-0.20 / icon.c < prev    next >
C/C++ Source or Header  |  2000-10-26  |  8KB  |  358 lines

  1. /* -*-Mode:C; tab-width:4; indent-tabs-mode:t; c-file-style:"stroustrup";-*- */
  2. //
  3. // Project : progect
  4. // Desc.   : Icon support code.
  5. // $Id: icon.c,v 1.9 2000/10/09 06:18:26 seagull_kamome Exp $
  6. //
  7.  
  8. #include "progect.h"
  9. #include "task.h"
  10. #include "icon.h"
  11. #include "MemoDB.h"
  12.  
  13. #include "progectRsc.h"
  14.  
  15. UInt8 numIconLoaded = 0;
  16. IconType loadedIcon[MAX_ICONS];
  17.  
  18.  
  19.  
  20.  
  21. /****************************************************************************
  22.  * Name : initializeIcon
  23.  * Desc : initialize and load icon
  24.  * Parm :
  25.  * Out  : 
  26.  * Auth : seagull, 23.09.2000 JST
  27.  ***************************************************************************/
  28. void initializeIcon(void)
  29. {
  30.     DmOpenRef memodb;
  31.  
  32.     if (! MemoGetDatabase(&memodb, dmModeReadOnly))
  33.     {
  34.         UInt16 i;
  35.         UInt16 numRec = DmNumRecords(memodb);
  36.  
  37.         for (i = 0; i < numRec; i++)
  38.         {
  39.             MemHandle h;
  40.             MemoDBRecordType *rcd;
  41.  
  42.             UInt16 attr;
  43.             // get the memo's attr
  44.             DmRecordInfo(memodb, i, &attr, NULL, NULL);
  45.             // if it's not deleted
  46.             if (attr & dmRecAttrDelete)
  47.                 continue;
  48.  
  49.             h = DmQueryRecord(memodb, i);
  50.             if (!h)
  51.                 continue;
  52.  
  53.             rcd = MemHandleLock(h);
  54.  
  55.  
  56.             if (! StrNCompare(&rcd->note, "DATEBK3\n", 8))
  57.             {
  58.                 UInt8* p = &rcd->note + 8;
  59.                 while (*p)
  60.                 {
  61.                     UInt16 n;
  62.                     if (*p != '#')
  63.                     {  // skip invalid line
  64.                         while (*p && *p != '\n')
  65.                             p++;
  66.                         if (! *p) break;
  67.                         p++;
  68.                         continue;
  69.                     }
  70.  
  71.                     // copy and skip description
  72.                     //  NOTE: 1st char is shortcut.
  73.                     p++;
  74.                     loadedIcon[numIconLoaded].shortcut = *p++;
  75.                     for (n = 0; *p && *p != '\n' && *p != '=' && n < 31; n++)
  76.                         loadedIcon[numIconLoaded].desc[n] = *p++;
  77.                     loadedIcon[numIconLoaded].desc[n] = '\0';
  78.  
  79.                     if (*p == '=')
  80.                     {   // copy image
  81.                         p++;
  82.                         for (n = 0; n < 8; n++)
  83.                         {
  84.                             UInt8 bits;
  85.                             // load higher 4 bits
  86.                             if (*p >= '0' && *p <= '9')
  87.                                 bits = *p - '0';
  88.                             else if (*p >= 'a' && *p <= 'f')
  89.                                 bits = *p - 'a' + 10;
  90.                             else if (*p >= 'A' && *p <= 'F')
  91.                                 bits = *p - 'A' + 10;
  92.                             else
  93.                                 break; // found invalid character
  94.                             p++;
  95.  
  96.                             // load lower 4 bits
  97.                             bits <<= 4;
  98.                             if (*p >= '0' && *p <= '9')
  99.                                 bits += *p - '0';
  100.                             else if (*p >= 'a' && *p <= 'f')
  101.                                 bits += *p - 'a' + 10;
  102.                             else if (*p >= 'A' && *p <= 'F')
  103.                                 bits += *p - 'A' + 10;
  104.                             else
  105.                                 break; // found invalid character
  106.                             p++;
  107.  
  108.                             loadedIcon[numIconLoaded].image[n] = bits;
  109.                         }
  110.  
  111.                         if (n == 8) // all succeded
  112.                         {
  113.                             if (++numIconLoaded >= MAX_ICONS)
  114.                                 break;
  115.                         }
  116.                     }
  117.                     while (*p && *p != '\n') // skip junk
  118.                         p++;
  119.                     if (*p == '\n') p++;
  120.                 }
  121.                 MemHandleUnlock(h);
  122.                 break;
  123.             }
  124.  
  125.             MemHandleUnlock(h);
  126.         }
  127.         DmCloseDatabase(memodb);
  128.     }
  129. } // void initializeIcon(void)
  130.  
  131.  
  132.  
  133.  
  134.  
  135. /****************************************************************************
  136.  * Name : DrawIcon
  137.  * Desc : Draw an icon image
  138.  * Parm : 
  139.  *         -> icon id
  140.  *         -> x, y
  141.  * Out  : 
  142.  * Auth : seagull, 25.09.2000 JST
  143.  ***************************************************************************/
  144. void drawIcon(UInt8 id, int x, int y)
  145. {
  146.     if (id >= getNumIconsLoaded())
  147.         return ;
  148.  
  149.     // currently MONO image only.
  150.     if (OSCaps.ver35)
  151.     {
  152.         UInt16 err;
  153.         UInt8 n;
  154.  
  155.         BitmapType* bmp = BmpCreate(getIconWidth(id), getIconHeight(id),
  156.                                     getIconColorDepth(id), NULL, &err);
  157.         UInt16* dstbits = BmpGetBits(bmp);
  158.         UInt8* srcbits = getIconImage(id);
  159.  
  160.         for (n = 0; n < getIconDataLength(id); n++)
  161.             *(dstbits++) = *(srcbits++) << 8;
  162.     
  163.         WinDrawBitmap(bmp, x, y);
  164.         BmpDelete(bmp);
  165.     }
  166.     else
  167.     {
  168.         UInt8 *srcbits = getIconImage(id);
  169.         RectangleType pixel;
  170.         UInt8 n, i;
  171.         for (n = 0; n < 8; n++)
  172.         {
  173.             for (i = 0; i < 8; i++)
  174.             {
  175.                 if (srcbits[n] & 1 << (7 - i))
  176.                 {
  177.                     RctSetRectangle(&pixel, x + i, y + n, 1, 1);
  178.                     WinDrawRectangle(&pixel, 0);
  179.                 }
  180.             }
  181.         }
  182.     }
  183. } // void DrawIcon(UInt8 id, int x, int y)
  184.  
  185.  
  186.  
  187.  
  188.  
  189. /****************************************************************************
  190.  ** Icon select dialog functions
  191.  ****************************************************************************/
  192.  
  193.  
  194.  
  195. /****************************************************************************
  196.  * Name : onDrawIconColumn
  197.  * Desc : Custom draw procedure for icon select table
  198.  * Parm : all parameters is pass from PalmOS
  199.  *         -> table object
  200.  *         -> row, column
  201.  *         -> clipping rect
  202.  * Out  : 
  203.  * Auth : seagull, 23.09.2000 JST
  204.  ***************************************************************************/
  205. static void onDrawIconColumn(void *tableP,
  206.                                   Int16 row, Int16 column,
  207.                                   RectangleType *bounds)
  208. {
  209.     UInt16 id = TblGetItemInt(tableP, row, column);
  210.  
  211.     if (id >= getNumIconsLoaded())
  212.         return ;
  213.     
  214.     // erase the row
  215.     if (!OSCaps.ver35)
  216.     {
  217.         WinEraseRectangle(bounds, 0);
  218.     }
  219.  
  220.     drawIcon(id, bounds->topLeft.x, bounds->topLeft.y);
  221.     FntSetFont(stdFont);
  222.     DrawTruncText(getIconDesc(id),
  223.                   bounds->topLeft.x + getIconWidth(id) + 1,
  224.                   bounds->topLeft.y,
  225.                   bounds->extent.x - getIconWidth(id) - 1);
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. /****************************************************************************
  238.  * Name : UpdateIconTable
  239.  * Desc : update icon select table
  240.  * Parm : 
  241.  *         -> top visible row
  242.  * Out  : 
  243.  * Auth : seagull, 24.09.2000 JST
  244.  ***************************************************************************/
  245. static void UpdateIconTable(UInt16 toprow)
  246. {
  247.     TablePtr table = GetObjectPtr(IconSelectTable);
  248.  
  249.     int i;
  250.     int item = toprow * 2;
  251.     int rows = TblGetNumberOfRows(table);
  252.     for (i = 0; i < rows; i++)
  253.     {
  254.         TblSetItemInt(table, i, 0, item++);
  255.         TblSetItemStyle(table, i, 0, customTableItem);
  256.  
  257.         TblSetItemInt(table, i, 1, item++);
  258.         TblSetItemStyle(table, i, 1, customTableItem);
  259.  
  260.         TblSetRowSelectable(table, i, true);
  261.     }
  262.     TblSetColumnUsable(table, 0, true);
  263.     TblSetColumnUsable(table, 1, true);
  264.     TblSetCustomDrawProcedure(table, 0, onDrawIconColumn);
  265.     TblSetCustomDrawProcedure(table, 1, onDrawIconColumn);
  266.     TblHasScrollBar(table, true);
  267.  
  268.     SclSetScrollBar(GetObjectPtr(Scr_SelectIcon),
  269.                     toprow,
  270.                     0, (getNumIconsLoaded() + 1) / 2 - 11, rows);
  271.  
  272.     TblDrawTable(table);
  273.     SclDrawScrollBar(GetObjectPtr(Scr_SelectIcon) );
  274. }
  275.  
  276.  
  277.  
  278.  
  279.  
  280. /****************************************************************************
  281.  * Name : FrmIconSelectHandleEvent
  282.  * Desc : event hander for icon select dialog
  283.  * Parm : 
  284.  *         -> event object
  285.  * Out  : 
  286.  * Auth : seagull, 23.09.2000 JST
  287.  ***************************************************************************/
  288. Boolean FrmIconSelectHandleEvent (EventPtr e)
  289. {
  290.     FormPtr frm;
  291.     TablePtr table;
  292.  
  293.     frm = FrmGetActiveForm();
  294.     table = GetObjectPtr(IconSelectTable);
  295.  
  296.     switch (e->eType)
  297.     {
  298.     case frmOpenEvent:
  299.         FrmDrawForm(frm);
  300.         UpdateIconTable(0);
  301.         return true;
  302.  
  303.     case sclRepeatEvent:
  304.         UpdateIconTable(e->data.sclRepeat.newValue);
  305.         return true;
  306.  
  307.     case ctlSelectEvent:
  308.         switch (e->data.ctlSelect.controlID)
  309.         {
  310.         case Btn_Remove:
  311.             TaskRemoveExtraChunk(gdbP, gActualTask, Extra_Icon, 0);
  312.         default:
  313.             break;
  314.  
  315.         case Btn_Ok:
  316.             {
  317.                 Int16 row, col;
  318.                 UInt16 iconid;
  319.                 TblGetSelection(table, &row, &col);
  320.                 iconid = TblGetItemInt(table, row, col);
  321.                 TaskSetExtraChunk(gdbP, gActualTask, Extra_Icon, 0,
  322.                                   &iconid, sizeof(iconid));
  323.             }
  324.         }
  325.         FrmUpdateForm(FrmTaskEdit, frmTaskEditReturnFromIconSelect);
  326.         FrmReturnToForm(0);
  327.         return true;
  328.  
  329.     case tblSelectEvent:
  330.         if (TblGetItemInt(e->data.tblSelect.pTable,
  331.                           e->data.tblSelect.row, e->data.tblSelect.column)
  332.             >= getNumIconsLoaded())
  333.         {
  334.             TblUnhighlightSelection(e->data.tblSelect.pTable);
  335.             return true;
  336.         }
  337.         break;
  338.  
  339.     case keyDownEvent:
  340.         if (e->data.keyDown.chr == vchrPageDown ||
  341.             e->data.keyDown.chr == vchrPageUp)
  342.         {
  343.             ScrollBarPtr scrl = GetObjectPtr(Scr_SelectIcon);
  344.             Int16 curr, min, max, page, newpos;
  345.             SclGetScrollBar(scrl, &curr, &min, &max, &page);
  346.             newpos = (e->data.keyDown.chr == vchrPageDown)?
  347.                 (curr + page) : (curr - page);
  348.             if (newpos < min) newpos = min;
  349.             if (newpos > max) newpos = max;
  350.             if (newpos != curr) UpdateIconTable(newpos);
  351.         }
  352.     default:
  353.         break;
  354.     }
  355.  
  356.     return false;
  357. } // static Boolean FrmMainHandleEvent (EventPtr e)
  358.