NCBI C Toolkit Cross Reference

C/vibrant/table.c


  1 /*   table.c
  2 * ===========================================================================
  3 *
  4 *                            PUBLIC DOMAIN NOTICE
  5 *            National Center for Biotechnology Information (NCBI)
  6 *
  7 *  This software/database is a "United States Government Work" under the
  8 *  terms of the United States Copyright Act.  It was written as part of
  9 *  the author's official duties as a United States Government employee and
 10 *  thus cannot be copyrighted.  This software/database is freely available
 11 *  to the public for use. The National Library of Medicine and the U.S.
 12 *  Government do not place any restriction on its use or reproduction.
 13 *  We would, however, appreciate having the NCBI and the author cited in
 14 *  any work or product based on this material
 15 *
 16 *  Although all reasonable efforts have been taken to ensure the accuracy
 17 *  and reliability of the software and data, the NLM and the U.S.
 18 *  Government do not and cannot warrant the performance or results that
 19 *  may be obtained by using this software or data. The NLM and the U.S.
 20 *  Government disclaim all warranties, express or implied, including
 21 *  warranties of performance, merchantability or fitness for any particular
 22 *  purpose.
 23 *
 24 * ===========================================================================
 25 *
 26 * File Name:  table.c
 27 *
 28 * Author:  Jonathan Kans
 29 *
 30 * Version Creation Date:   1/1/91
 31 *
 32 * $Revision: 6.1 $
 33 *
 34 * File Description:   Active tabular text
 35 *
 36 * Modifications:  
 37 * --------------------------------------------------------------------------
 38 * Date     Name        Description of modification
 39 * -------  ----------  -----------------------------------------------------
 40 *
 41 *
 42 * $Log: table.c,v $
 43 * Revision 6.1  1998/07/02 18:24:32  vakatov
 44 * Cleaned the code & made it pass through the C++ compilation
 45 *
 46 * Revision 6.0  1997/08/25 18:56:44  madden
 47 * Revision changed to 6.0
 48 *
 49 * Revision 5.0  1996/05/28 13:45:08  ostell
 50 * Set to revision 5.0
 51 *
 52  * Revision 4.0  1995/07/26  13:51:04  ostell
 53  * force revision to 4.0
 54  *
 55  * Revision 2.4  1995/05/17  15:15:14  kans
 56  * added Log line
 57  *
 58 *
 59 * ==========================================================================
 60 */
 61 
 62 #include "panels.h"
 63 
 64 #define numCellsPerPage 256
 65 
 66 typedef  struct  tableData {
 67   Handle     title;
 68   Int2       numPages;
 69   Handle     pageHandles;
 70   Int2       numColumns;
 71   Handle     colHandles;
 72   FonT       font;
 73   Int2       topRow;
 74   Int2       leftMargin;
 75   Int2       rightMargin;
 76   Int2       width;
 77   Int2       lineHeight;
 78   Int2       visLines;
 79   Int2       numLines;
 80   Int2       currentRow;
 81   Int2       tabRow;
 82   Int2       returnRow;
 83   Int2       currentColumn;
 84   TableProc  action;
 85 } TableData;
 86 
 87 typedef  Handle PageRec, PNTR PagePtr;
 88 
 89 typedef  struct  cellRec {
 90   Uint4    start;
 91   size_t   count;
 92   Boolean  hilight;
 93   Boolean  gray;
 94 } CellRec, PNTR CellPtr;
 95 
 96 typedef  struct  colRec {
 97   TableProc  click;
 98   Int2       pos;
 99   Int2       width;
100   Char       just;
101   Boolean    wrap;
102   Boolean    bar;
103 } ColRec, PNTR ColPtr;
104 
105 static Boolean SetCellRec (TablE t, Int2 row, Int2 column, CellRec * cellRec)
106 
107 {
108   Int4       cell;
109   CellPtr    cellPtr;
110   Int2       index;
111   Int2       numColumns;
112   Int2       numPages;
113   Int2       page;
114   Handle     pageHandles;
115   PagePtr    pagePtr;
116   Boolean    rsult;
117   TableData  tdata;
118 
119   rsult = FALSE;
120   if (t != NULL && cellRec != NULL) {
121     GetPanelExtra ((PaneL) t, &tdata);
122     numPages = tdata.numPages;
123     pageHandles = tdata.pageHandles;
124     numColumns = tdata.numColumns;
125     cell = (Int4) row * (Int4) numColumns + (Int4) column;
126     page = (Int2) (cell / numCellsPerPage);
127     index = (Int2) (cell % numCellsPerPage);
128     if (pageHandles != NULL && page < numPages && column < numColumns) {
129       pagePtr = (PagePtr) HandLock (pageHandles);
130       if (pagePtr != NULL && pagePtr [page] != NULL) {
131         cellPtr = (CellPtr) HandLock (pagePtr [page]);
132         if (cellPtr != NULL) {
133           cellPtr [index] = *cellRec;
134           rsult = TRUE;
135         }
136         HandUnlock (pagePtr [page]);
137       }
138       HandUnlock (pageHandles);
139     }
140   }
141   return rsult;
142 }
143 
144 static Boolean GetCellRec (TablE t, Int2 row, Int2 column, CellRec * cellRec)
145 
146 {
147   Int4       cell;
148   CellPtr    cellPtr;
149   Int2       index;
150   Int2       numColumns;
151   Int2       numPages;
152   Int2       page;
153   Handle     pageHandles;
154   PagePtr    pagePtr;
155   Boolean    rsult;
156   TableData  tdata;
157 
158   rsult = FALSE;
159   if (t != NULL && cellRec != NULL) {
160     GetPanelExtra ((PaneL) t, &tdata);
161     numPages = tdata.numPages;
162     pageHandles = tdata.pageHandles;
163     numColumns = tdata.numColumns;
164     cell = (Int4) row * (Int4) numColumns + (Int4) column;
165     page = (Int2) (cell / numCellsPerPage);
166     index = (Int2) (cell % numCellsPerPage);
167     if (pageHandles != NULL && page < numPages && column < numColumns) {
168       pagePtr = (PagePtr) HandLock (pageHandles);
169       if (pagePtr != NULL && pagePtr [page] != NULL) {
170         cellPtr = (CellPtr) HandLock (pagePtr [page]);
171         if (cellPtr != NULL) {
172           *cellRec = cellPtr [index];
173           rsult = TRUE;
174         }
175         HandUnlock (pagePtr [page]);
176       }
177       HandUnlock (pageHandles);
178     }
179   }
180   return rsult;
181 }
182 
183 #ifdef XXX_UNUSED_STATIC_FUNC
184 static Boolean SetColRec (TablE t, Int2 column, ColRec * colRec)
185 {
186   Handle     colHandles;
187   ColPtr     colPtr;
188   Int2       numColumns;
189   Boolean    rsult;
190   TableData  tdata;
191 
192   rsult = FALSE;
193   if (t != NULL && colRec != NULL) {
194     GetPanelExtra ((PaneL) t, &tdata);
195     numColumns = tdata.numColumns;
196     colHandles = tdata.colHandles;
197     if (colHandles != NULL && column < numColumns) {
198       colPtr = (ColPtr) HandLock (colHandles);
199       if (colPtr != NULL) {
200         colPtr [column] = *colRec;
201         rsult = TRUE;
202       }
203       HandUnlock (colHandles);
204     }
205   }
206   return rsult;
207 }
208 #endif /* XXX_UNUSED_STATIC_FUNC */
209 
210 
211 static Boolean GetColRec (TablE t, Int2 column, ColRec * colRec)
212 
213 {
214   Handle     colHandles;
215   ColPtr     colPtr;
216   Int2       numColumns;
217   Boolean    rsult;
218   TableData  tdata;
219 
220   rsult = FALSE;
221   if (t != NULL && colRec != NULL) {
222     GetPanelExtra ((PaneL) t, &tdata);
223     numColumns = tdata.numColumns;
224     colHandles = tdata.colHandles;
225     if (colHandles != NULL && column < numColumns) {
226       colPtr = (ColPtr) HandLock (colHandles);
227       if (colPtr != NULL) {
228         *colRec = colPtr [column];
229         rsult = TRUE;
230       }
231       HandUnlock (colHandles);
232     }
233   }
234   return rsult;
235 }
236 
237 static void DrawTable (PaneL t)
238 
239 {
240   CellPtr    cellPtr;
241   CharPtr    charPtr;
242   ColPtr     colPtr;
243   Int2       column;
244   Handle     colHandles;
245   RecT       dr;
246   Handle     h;
247   Int2       index;
248   Int2       numColumns;
249   Int2       numPages;
250   Int2       off;
251   Int2       page;
252   Handle     pageHandles;
253   PagePtr    pagePtr;
254   RecT       q;
255   RecT       r;
256   Int2       row;
257   SlatE      s;
258   RecT       sr;
259   TableData  tdata;
260 
261   s = (SlatE) Parent (t);
262   GetPanelExtra (t, &tdata);
263   h = tdata.title;
264   numPages = tdata.numPages;
265   pageHandles = tdata.pageHandles;
266   numColumns = tdata.numColumns;
267   colHandles = tdata.colHandles;
268   SelectFont (tdata.font);
269   ObjectRect (s, &sr);
270   InsetRect (&sr, 4, 4);
271   GetOffset (t, NULL, &off);
272   if (h != NULL && pageHandles != NULL && colHandles != NULL && numColumns > 0) {
273     charPtr = (CharPtr) HandLock (h);
274     pagePtr = (PagePtr) HandLock (pageHandles);
275     colPtr = (ColPtr) HandLock (colHandles);
276     row = 0;
277     column = 0;
278     for (page = 0; page < numPages; page++) {
279       if (pagePtr [page] != NULL) {
280         cellPtr = (CellPtr) HandLock (pagePtr [page]);
281         if (cellPtr != NULL) {
282           for (index = 0; index < numCellsPerPage; index++) {
283             if (colPtr [column].width > 0) {
284               r.left = colPtr [column].pos;
285               r.top = tdata.topRow + row * tdata.lineHeight;
286               r.right = r.left + colPtr [column].width;
287               r.bottom = r.top + tdata.lineHeight;
288               OffsetRect (&r, 0, -off);
289               if (RectInRect (&r, &sr) &&
290                   (updateRgn == NULL || RectInRgn (&r, updateRgn))) {
291                 if (cellPtr [index].hilight) {
292                   InvertColors ();
293                 }
294                 EraseRect (&r);
295                 q = r;
296                 InsetRect (&q, 5, 0);
297                 if (cellPtr [index].count > 0) {
298                   DrawText (&q, charPtr + cellPtr [index].start,
299                             cellPtr [index].count, colPtr [column].just,
300                             cellPtr [index].gray);
301                 }
302                 if (cellPtr [index].hilight) {
303                   InvertColors ();
304                 }
305               }
306             }
307             column++;
308             if (column >= numColumns) {
309               row++;
310               column = 0;
311             }
312           }
313         }
314         HandUnlock (pagePtr [page]);
315       }
316     }
317     HandUnlock (colHandles);
318     HandUnlock (pageHandles);
319     HandUnlock (h);
320   }
321   if (colHandles != NULL) {
322     InvertMode ();
323     colPtr = (ColPtr) HandLock (colHandles);
324     for (column = 0; column < numColumns; column++) {
325       if (colPtr [column].bar) {
326         ObjectRect (t, &r);
327         r.left = colPtr [column].pos;
328         SectRect (&r, &sr, &dr);
329         dr.bottom = dr.top + tdata.visLines * tdata.lineHeight - 1;
330         MoveTo (dr.left, dr.top);
331         LineTo (dr.left, dr.bottom);
332       }
333     }
334     HandUnlock (colHandles);
335     CopyMode ();
336   }
337   SelectFont (systemFont);
338 }
339 
340 extern void RecordColumn (TablE t, Int2 wid,
341                           Char just, Boolean wrap,
342                           Boolean bar, TableProc actn)
343 
344 {
345   Int2       chunk;
346   ColPtr     colPtr;
347   Handle     colHandles;
348   Int2       numColumns;
349   RecT       r;
350   Int2       rightMargin;
351   TableData  tdata;
352 
353   if (t != NULL) {
354     ObjectRect (t, &r);
355     GetPanelExtra ((PaneL) t, &tdata);
356     numColumns = tdata.numColumns;
357     colHandles = tdata.colHandles;
358     chunk = 8;
359     if (colHandles != NULL) {
360       if (numColumns % chunk == 0) {
361         colHandles = HandMore (colHandles, sizeof (ColRec) *
362                                  (numColumns / chunk + 1) * chunk);
363       }
364     } else {
365       colHandles = HandNew (sizeof (ColRec) * chunk);
366     }
367     numColumns++;
368     colPtr = (ColPtr) HandLock (colHandles);
369     colPtr [numColumns - 1].click = actn;
370     if (numColumns == 1) {
371       colPtr [numColumns - 1].pos = r.left;
372     } else if (numColumns > 1) {
373       colPtr [numColumns - 1].pos = colPtr [numColumns - 2].pos +
374                                     colPtr [numColumns - 2].width;
375     }
376     colPtr [numColumns - 1].width = wid * MaxCharWidth ();
377     colPtr [numColumns - 1].just = just;
378     colPtr [numColumns - 1].wrap = wrap;
379     colPtr [numColumns - 1].bar = bar;
380     HandUnlock (colHandles);
381     rightMargin = colPtr [numColumns - 1].pos + colPtr [numColumns - 1].width;
382     if (tdata.rightMargin < rightMargin) {
383       tdata.rightMargin = rightMargin;
384     }
385     tdata.numColumns = numColumns;
386     tdata.colHandles = colHandles;
387     SetPanelExtra ((PaneL) t, &tdata);
388   }
389 }
390 
391 static void RecordSegment (TablE t, Uint4 start,
392                            Uint4 prev, Int2 len,
393                            Int2 row, Int2 column)
394 
395 {
396   Int4       cell;
397   CellPtr    cellPtr;
398   Int2       index;
399   Int2       numColumns;
400   Int2       numPages;
401   Int2       page;
402   Handle     pageHandles;
403   PagePtr    pagePtr;
404   TableData  tdata;
405 
406   if (t != NULL) {
407     GetPanelExtra ((PaneL) t, &tdata);
408     numPages = tdata.numPages;
409     pageHandles = tdata.pageHandles;
410     numColumns = tdata.numColumns;
411     cell = (Int4) row * (Int4) numColumns + (Int4) column;
412     page = (Int2) (cell / numCellsPerPage);
413     index = (Int2) (cell % numCellsPerPage);
414     if (page >= numPages) {
415       numPages = page + 1;
416       if (pageHandles != NULL) {
417         pageHandles = HandMore (pageHandles, sizeof (PageRec) * numPages);
418       } else {
419         pageHandles = HandNew (sizeof (PageRec));
420       }
421       tdata.numPages = numPages;
422       tdata.pageHandles = pageHandles;
423       SetPanelExtra ((PaneL) t, &tdata);
424       pagePtr = (PagePtr) HandLock (pageHandles);
425       if (pagePtr != NULL) {
426         pagePtr [page] = HandNew (sizeof (CellRec) * numCellsPerPage);
427       }
428       HandUnlock (pageHandles);
429     }
430     if (pageHandles != NULL && column < numColumns) {
431       pagePtr = (PagePtr) HandLock (pageHandles);
432       if (pagePtr [page] != NULL) {
433         cellPtr = (CellPtr) HandLock (pagePtr [page]);
434         if (cellPtr != NULL) {
435           cellPtr [index].start = start + prev;
436           cellPtr [index].count = len;
437           cellPtr [index].hilight = FALSE;
438           cellPtr [index].gray = FALSE;
439         }
440         HandUnlock (pagePtr [page]);
441       }
442       HandUnlock (pageHandles);
443     }
444   }
445 }
446 
447 static Int2 GetNextBlock (CharPtr title, Int2 maxwid)
448 
449 {
450   Int2  i;
451   Int2  j;
452   Int2  wid;
453 
454   i = 0;
455   j = 0;
456   wid = 0;
457   if (title != NULL && maxwid > 0) {
458     while (title [i] != '\0' && title [i] != '\n' &&
459            title [i] != '\r' && title [i] != '\t' &&
460            wid <= maxwid) {
461       wid += CharWidth (title [i]);
462       i++;
463     }
464     j = i;
465     if (wid > maxwid) {
466       j--;
467       while (TextWidth (title, i) > maxwid) {
468         i--;
469       }
470       while (i > 0 && (! IS_WHITESP(title [i])) && title [i - 1] != '-') {
471         i--;
472       }
473       while (i > 0 && (IS_WHITESP(title [i - 1])) && title [i - 1] != '-') {
474         i--;
475       }
476     } else if (title [i] != '\0') {
477       while (i > 0 && (IS_WHITESP(title [i - 1]))) {
478         i--;
479       }
480     }
481   }
482   if (i > 0 && i < j) {
483     return i;
484   } else if (j > 0) {
485     return j;
486   } else {
487     return 0;
488   }
489 }
490 
491 static Uint4 CopyToHandle (TablE t, CharPtr title)
492 
493 {
494   CharPtr    charPtr;
495   size_t     chunk;
496   Handle     h;
497   size_t     len;
498   size_t     prev;
499   size_t     size;
500   TableData  tdata;
501 
502   prev = 0;
503   if (t != NULL && title != NULL) {
504     GetPanelExtra ((PaneL) t, &tdata);
505     chunk = 4096;
506     h = tdata.title;
507     if (h != NULL) {
508       charPtr = (CharPtr) HandLock (h);
509       prev = StringLen (charPtr);
510       HandUnlock (h);
511       len = prev + StringLen (title);
512       size = ((len + 1) / chunk + 1) * chunk;
513       if ((len + 1) / chunk > (prev + 1) / chunk) {
514         h = HandMore (h, size);
515       }
516       charPtr = (CharPtr) HandLock (h);
517       StringNCat (charPtr, title, size);
518       HandUnlock (h);
519       tdata.title = h;
520       SetPanelExtra ((PaneL) t, &tdata);
521     } else {
522       len = StringLen (title);
523       size = ((len + 1) / chunk + 1) * chunk;
524       h = HandNew (size);
525       charPtr = (CharPtr) HandLock (h);
526       StringNCpy (charPtr, title, size);
527       HandUnlock (h);
528       tdata.title = h;
529       SetPanelExtra ((PaneL) t, &tdata);
530     }
531   }
532   return prev;
533 }
534 
535 extern void AppendTableText (TablE t, CharPtr title)
536 
537 {
538   Char       ch;
539   ColPtr     colPtr;
540   Int2       column;
541   Handle     colHandles;
542   RecT       dr;
543   Int2       len;
544   Int2       maxwid;
545   Int2       newLines;
546   Int2       numColumns;
547   Int2       off;
548   Uint4      prev;
549   RecT       r;
550   Int2       returnRow;
551   Int2       row;
552   SlatE      s;
553   RecT       sr;
554   Uint4      start;
555   Int2       tabRow;
556   TableData  tdata;
557   WindoW     tempPort;
558 
559   if (t != NULL && title != NULL && title [0] != '\0') {
560     tempPort = SavePort (t);
561     prev = CopyToHandle (t, title);
562     GetPanelExtra ((PaneL) t, &tdata);
563     SelectFont (tdata.font);
564     numColumns = tdata.numColumns;
565     colHandles = tdata.colHandles;
566     s = (SlatE) Parent (t);
567     start = 0;
568     len = 0;
569     row = tdata.currentRow;
570     tabRow = tdata.tabRow;
571     returnRow = tdata.returnRow;
572     column = tdata.currentColumn;
573     if (colHandles != NULL) {
574       colPtr = (ColPtr) HandLock (colHandles);
575       while (title [start] != '\0') {
576         while (title [start] == ' ') {
577           start++;
578         }
579         if (title [start] != '\0') {
580           ch = title [start];
581           if (ch != '\0' && ch != '\n' && ch != '\r' && ch != '\t') {
582             maxwid = INT2_MAX;
583             if (numColumns > 0 && column < numColumns &&
584                 colPtr [column].wrap && colPtr [column].width >= 12) {
585               maxwid = colPtr [column].width - 12;
586             }
587             len = GetNextBlock (title + start, maxwid);
588             if (len > 0) {
589               RecordSegment (t, start, prev, len, row, column);
590               start += len;
591             } else if (colPtr [column].wrap) {
592               while (TextWidth (title + start, len) <= maxwid) {
593                 len++;
594               }
595               len--;
596               if (len > 0) {
597                 RecordSegment (t, start, prev, len, row, column);
598               }
599               ch = title [start];
600               while (ch != '\0' && ch != '\n' && ch != '\r' && ch != '\t') {
601                 start++;
602                 ch = title [start];
603               }
604             }
605           }
606           ch = title [start];
607           if (ch == '\n') {
608             start++;
609             row = returnRow;
610             tabRow = row;
611             returnRow = MAX (returnRow, row + 1);
612             column = 0;
613           } else if (ch == '\r') {
614             start++;
615             row++;
616             returnRow = MAX (returnRow, row + 1);
617           } else if (ch == '\t') {
618             start++;
619             row = tabRow;
620             column++;
621           } else if (ch != '\0' && colPtr [column].wrap) {
622             if (len == 0) {
623               start++;
624             }
625             row++;
626             returnRow = MAX (returnRow, row + 1);
627           } else if (ch != '\0') {
628             start++;
629           }
630         }
631       }
632       HandUnlock (colHandles);
633     }
634     newLines = row - tdata.numLines;
635     r.left = tdata.leftMargin;
636     r.top = tdata.topRow;
637     r.right = tdata.rightMargin;
638     r.bottom = tdata.topRow + row * tdata.lineHeight;
639     RegisterRect ((PaneL) t, &r);
640     r.top = tdata.topRow + tdata.numLines * tdata.lineHeight;
641     RegisterRow (s, r.top, tdata.lineHeight, newLines);
642     r.bottom = r.top + newLines * tdata.lineHeight;
643     GetPanelExtra ((PaneL) t, &tdata);
644     tdata.currentRow = row;
645     tdata.tabRow = tabRow;
646     tdata.returnRow = returnRow;
647     tdata.currentColumn = column;
648     tdata.numLines += newLines;
649     SetPanelExtra ((PaneL) t, &tdata);
650     SelectFont (systemFont);
651     if (Enabled (t) && AllParentsEnabled (t) &&
652         Visible (t) && AllParentsVisible (t)) {
653       ObjectRect (s, &sr);
654       InsetRect (&sr, 4, 4);
655       GetOffset (t, NULL, &off);
656       OffsetRect (&r, 0, -off);
657       SectRect (&r, &sr, &dr);
658       if (RectInRect (&dr, &sr)) {
659         Select (t);
660         InvalRect (&dr);
661       }
662     }
663     RestorePort (tempPort);
664   }
665 }
666 
667 extern Boolean GetTableText (TablE t, Int2 row,
668                              Int2 column, CharPtr text,
669                              Uint4 maxsize)
670 
671 {
672   CellRec    cellRec;
673   CharPtr    charPtr;
674   Uint4      count;
675   Handle     h;
676   Boolean    rsult;
677   Uint4      start;
678   TableData  tdata;
679 
680   rsult = FALSE;
681   if (text != NULL && maxsize > 0) {
682     *text = '\0';
683   }
684   if (t != NULL && text != NULL && maxsize > 0) {
685     h = NULL;
686     count = 0;
687     start = 0;
688     if (row > 0 && column > 0) {
689       row--;
690       column--;
691       GetPanelExtra ((PaneL) t, &tdata);
692       h = tdata.title;
693       GetCellRec (t, row, column, &cellRec);
694       start = cellRec.start;
695       count = cellRec.count;
696     } else if (row > 0) {
697       row--;
698       GetPanelExtra ((PaneL) t, &tdata);
699       h = tdata.title;
700       GetCellRec (t, row, 0, &cellRec);
701       start = cellRec.start;
702       if (GetCellRec (t, row + 1, 0, &cellRec)) {
703         if (cellRec.count > 0) {
704           count = cellRec.start - start;
705         } else {
706           h = tdata.title;
707           if (h != NULL) {
708             charPtr = (CharPtr) HandLock (h);
709             count = StringLen (charPtr) - start;
710             HandUnlock (h);
711           }
712         }
713       } else {
714         h = tdata.title;
715         if (h != NULL) {
716           charPtr = (CharPtr) HandLock (h);
717           count = StringLen (charPtr) - start;
718           HandUnlock (h);
719         }
720       }
721     } else {
722       GetPanelExtra ((PaneL) t, &tdata);
723       h = tdata.title;
724       if (h != NULL) {
725         charPtr = (CharPtr) HandLock (h);
726         count = StringLen (charPtr);
727         HandUnlock (h);
728       }
729     }
730     if (count > maxsize - 1) {
731       count = maxsize - 1;
732     }
733     if (h != NULL) {
734       charPtr = (CharPtr) HandLock (h);
735       charPtr += start;
736       while (count > 0) {
737         *text = *charPtr;
738         text++;
739         charPtr++;
740         count--;
741       }
742       *text = '\0';
743       HandUnlock (h);
744       rsult = TRUE;
745     }
746   }
747   return rsult;
748 }
749 
750 extern Uint4 TableTextLength (TablE t, Int2 row, Int2 column)
751 
752 {
753   CellRec    cellRec;
754   CharPtr    charPtr;
755   Uint4      count;
756   Handle     h;
757   Uint4      start;
758   TableData  tdata;
759 
760   count = 0;
761   if (t != NULL) {
762     count = 0;
763     start = 0;
764     if (row > 0 && column > 0) {
765       row--;
766       column--;
767       GetPanelExtra ((PaneL) t, &tdata);
768       GetCellRec (t, row, column, &cellRec);
769       count = cellRec.count;
770     } else if (row > 0) {
771       row--;
772       GetPanelExtra ((PaneL) t, &tdata);
773       GetCellRec (t, row, 0, &cellRec);
774       start = cellRec.start;
775       if (GetCellRec (t, row + 1, 0, &cellRec)) {
776         if (cellRec.count > 0) {
777           count = cellRec.start - start;
778         } else {
779           h = tdata.title;
780           if (h != NULL) {
781             charPtr = (CharPtr) HandLock (h);
782             count = StringLen (charPtr) - start;
783             HandUnlock (h);
784           }
785         }
786       } else {
787         h = tdata.title;
788         if (h != NULL) {
789           charPtr = (CharPtr) HandLock (h);
790           count = StringLen (charPtr) - start;
791           HandUnlock (h);
792         }
793       }
794     } else {
795       GetPanelExtra ((PaneL) t, &tdata);
796       h = tdata.title;
797       if (h != NULL) {
798         charPtr = (CharPtr) HandLock (h);
799         count = StringLen (charPtr);
800         HandUnlock (h);
801       }
802     }
803   }
804   return count;
805 }
806 
807 extern void SetTableBlockHilight (TablE t, Int2 firstRow,
808                                   Int2 lastRow, Int2 firstColumn,
809                                   Int2 lastColumn, Boolean hilight)
810 
811 {
812   CellRec    cellRec;
813   ColRec     colRec;
814   Int2       column;
815   Int2       off;
816   RecT       r;
817   Int2       row;
818   SlatE      s;
819   RecT       sr;
820   TableData  tdata;
821   WindoW     tempPort;
822 
823   if (t != NULL && firstRow > 0 && lastRow > 0 &&
824       firstColumn > 0 && lastColumn > 0) {
825     tempPort = SavePort (t);
826     s = (SlatE) Parent (t);
827     GetPanelExtra ((PaneL) t, &tdata);
828     ObjectRect (s, &sr);
829     InsetRect (&sr, 4, 4);
830     GetOffset (t, NULL, &off);
831     firstRow--;
832     lastRow--;
833     firstColumn--;
834     lastColumn--;
835     for (column = firstColumn; column <= lastColumn; column++) {
836       GetColRec (t, column, &colRec);
837       for (row = firstRow; row <= lastRow; row++) {
838         GetCellRec (t, row, column, &cellRec);
839         cellRec.hilight = hilight;
840         SetCellRec (t, row, column, &cellRec);
841         r.left = colRec.pos;
842         r.top = tdata.topRow + row * tdata.lineHeight;
843         r.right = r.left + colRec.width;
844         r.bottom = r.top + tdata.lineHeight;
845         OffsetRect (&r, 0, -off);
846         if (Enabled (t) && AllParentsEnabled (t) &&
847             Visible (t) && AllParentsVisible (t) &&
848             RectInRect (&r, &sr)) {
849           Select (t);
850           InvalRect (&r);
851         }
852       }
853     }
854     RestorePort (tempPort);
855   }
856 }
857 
858 extern void SetTableHilight (TablE t, Int2 row,
859                              Int2 column, Boolean hilight)
860 
861 {
862   SetTableBlockHilight (t, row, row, column, column, hilight);
863 }
864 
865 extern Boolean GetTableHilight (TablE t, Int2 row, Int2 column)
866 
867 {
868   CellRec  cellRec;
869   Boolean  rsult;
870 
871   rsult = FALSE;
872   if (t != NULL && row > 0 && column > 0) {
873     row--;
874     column--;
875     GetCellRec (t, row, column, &cellRec);
876     rsult = cellRec.hilight;
877   }
878   return rsult;
879 }
880 
881 extern void SetTableBlockGray (TablE t, Int2 firstRow,
882                                Int2 lastRow, Int2 firstColumn,
883                                Int2 lastColumn, Boolean gray)
884 
885 {
886   CellRec    cellRec;
887   ColRec     colRec;
888   Int2       column;
889   Int2       off;
890   RecT       r;
891   Int2       row;
892   SlatE      s;
893   RecT       sr;
894   TableData  tdata;
895   WindoW     tempPort;
896 
897   if (t != NULL && firstRow > 0 && lastRow > 0 &&
898       firstColumn > 0 && lastColumn > 0) {
899     tempPort = SavePort (t);
900     s = (SlatE) Parent (t);
901     GetPanelExtra ((PaneL) t, &tdata);
902     ObjectRect (s, &sr);
903     InsetRect (&sr, 4, 4);
904     GetOffset (t, NULL, &off);
905     firstRow--;
906     lastRow--;
907     firstColumn--;
908     lastColumn--;
909     for (column = firstColumn; column <= lastColumn; column++) {
910       GetColRec (t, column, &colRec);
911       for (row = firstRow; row <= lastRow; row++) {
912         GetCellRec (t, row, column, &cellRec);
913         cellRec.gray = gray;
914         SetCellRec (t, row, column, &cellRec);
915         r.left = colRec.pos;
916         r.top = tdata.topRow + row * tdata.lineHeight;
917         r.right = r.left + colRec.width;
918         r.bottom = r.top + tdata.lineHeight;
919         OffsetRect (&r, 0, -off);
920         if (Enabled (t) && AllParentsEnabled (t) &&
921             Visible (t) && AllParentsVisible (t) &&
922             RectInRect (&r, &sr)) {
923           Select (t);
924           InvalRect (&r);
925         }
926       }
927     }
928     RestorePort (tempPort);
929   }
930 }
931 
932 extern void SetTableGray (TablE t, Int2 row,
933                           Int2 column, Boolean gray)
934 
935 {
936   SetTableBlockGray (t, row, row, column, column, gray);
937 }
938 
939 extern Boolean GetTableGray (TablE t, Int2 row, Int2 column)
940 
941 {
942   CellRec  cellRec;
943   Boolean  rsult;
944 
945   rsult = FALSE;
946   if (t != NULL && row > 0 && column > 0) {
947     row--;
948     column--;
949     GetCellRec (t, row, column, &cellRec);
950     rsult = cellRec.gray;
951   }
952   return rsult;
953 }
954 
955 extern Int2 TableNumLines (TablE t)
956 
957 {
958   Int2       rsult;
959   TableData  tdata;
960 
961   rsult = 0;
962   if (t != NULL) {
963     GetPanelExtra ((PaneL) t, &tdata);
964     rsult = tdata.numLines;
965   }
966   return rsult;
967 }
968 
969 extern Int2 TableVisLines (TablE t)
970 
971 {
972   Int2       rsult;
973   TableData  tdata;
974 
975   rsult = 0;
976   if (t != NULL) {
977     GetPanelExtra ((PaneL) t, &tdata);
978     rsult = tdata.visLines;
979   }
980   return rsult;
981 }
982 
983 static void TableClick (PaneL t, PoinT pt)
984 
985 {
986   TableProc  actn;
987   ColRec     colRec;
988   Int2       column;
989   Boolean    goOn;
990   Int2       off;
991   RecT       r;
992   Int2       row;
993   SlatE      s;
994   RecT       sr;
995   TableData  tdata;
996 
997   if (t != NULL) {
998     actn = NULL;
999     s = (SlatE) Parent (t);
1000     GetPanelExtra (t, &tdata);
1001     ObjectRect (s, &sr);
1002     InsetRect (&sr, 4, 4);
1003     GetOffset (t, NULL, &off);
1004     if (pt.y + off >= tdata.topRow) {
1005       row = (pt.y + off - tdata.topRow) / tdata.lineHeight;
1006       goOn = TRUE;
1007       column = 0;
1008       r.left = 0;
1009       r.right = 0;
1010       r.top = tdata.topRow + row * tdata.lineHeight;
1011       r.bottom = r.top + tdata.lineHeight;
1012       OffsetRect (&r, 0, -off);
1013       while (goOn && column < tdata.numColumns) {
1014         GetColRec ((TablE) t, column, &colRec);
1015         actn = colRec.click;
1016         r.left = colRec.pos;
1017         r.right = r.left + colRec.width;
1018         if (RectInRect (&r, &sr) && PtInRect (pt, &r)) {
1019           goOn = FALSE;
1020           if (actn != NULL) {
1021             actn ((TablE) t, row + 1, column + 1);
1022           }
1023           actn = tdata.action;
1024           if (actn != NULL) {
1025             actn ((TablE) t, row + 1, column + 1);
1026           }
1027         } else {
1028           column++;
1029         }
1030       }
1031     }
1032   }
1033 }
1034 
1035 static void NewTable (TablE t, Int2 minwid, FonT font, TableProc actn)
1036 
1037 {
1038   PoinT      npt;
1039   RecT       r;
1040   SlatE      s;
1041   TableData  tdata;
1042 
1043   SelectFont (systemFont);
1044   s = (SlatE) Parent (t);
1045   ObjectRect (s, &r);
1046   InsetRect (&r, 4, 4);
1047   GetNextPosition (t, &npt);
1048   tdata.title = NULL;
1049   tdata.numPages = 0;
1050   tdata.pageHandles = NULL;
1051   tdata.numColumns = 0;
1052   tdata.colHandles = NULL;
1053   tdata.font = font;
1054   tdata.topRow = npt.y;
1055   tdata.leftMargin = npt.x;
1056   tdata.rightMargin = npt.x + minwid;
1057   tdata.width = tdata.rightMargin - tdata.leftMargin;
1058   SelectFont (font);
1059   tdata.lineHeight = LineHeight ();
1060   SelectFont (systemFont);
1061   tdata.visLines = (r.bottom - r.top + 1) / tdata.lineHeight;
1062   tdata.numLines = 0;
1063   tdata.currentRow = 0;
1064   tdata.tabRow = 0;
1065   tdata.returnRow = 1;
1066   tdata.currentColumn = 0;
1067   tdata.action = actn;
1068   SetPanelExtra ((PaneL) t, &tdata);
1069   LoadRect (&r, npt.x, npt.y, tdata.rightMargin, npt.y);
1070   RegisterRect ((PaneL) t, &r);
1071   Break (t);
1072 }
1073 
1074 static void ResetTable (PaneL t)
1075 
1076 {
1077   Int2       i;
1078   PagePtr    pagePtr;
1079   TableData  tdata;
1080 
1081   GetPanelExtra (t, &tdata);
1082   if (tdata.title != NULL) {
1083     HandFree (tdata.title);
1084   }
1085   if (tdata.pageHandles != NULL) {
1086     pagePtr = (PagePtr) HandLock (tdata.pageHandles);
1087     for (i = 0; i < tdata.numPages; i++) {
1088       HandFree (pagePtr [i]);
1089     }
1090     HandUnlock (tdata.pageHandles);
1091     HandFree (tdata.pageHandles);
1092   }
1093   if (tdata.colHandles != NULL) {
1094     HandFree (tdata.colHandles);
1095   }
1096   NewTable ((TablE) t, tdata.width, tdata.font, tdata.action);
1097 }
1098 
1099 extern TablE TablePanel (SlatE s, Int2 minwid, FonT font, TableProc actn)
1100 
1101 {
1102   TablE   t;
1103   WindoW  tempPort;
1104 
1105   t = NULL;
1106   if (s != NULL) {
1107     tempPort = SavePort (s);
1108     t = (TablE) CustomPanel (s, DrawTable, sizeof (TableData), ResetTable);
1109     if (t != NULL) {
1110       SetPanelClick ((PaneL) t, TableClick, NULL, NULL, NULL);
1111       NewTable (t, minwid * stdCharWidth, font, actn);
1112     }
1113     RestorePort (tempPort);
1114   }
1115   return t;
1116 }
1117 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.