/* 
Copyright (c) 1991-2000 UserLand Software, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a 
copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the 
following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/ 


#include "applet.h"
#include "appletbits.h"
#include "appletfilesinternal.h"
#include "appletfilelabel.h"
#include <Icons.h>




typedef struct tylabelinfo {
	
	hdlstring hstring;
	
	RGBColor rgb;
	} tylabelinfo;
	
#define ctlabels 8
	
static tylabelinfo labelcache [ctlabels];

static boolean needinit = true;


boolean filegetlabelinfo (short ixlabel, RGBColor *rgb, bigstring bslabel) {
	
	if (needinit) {
		
		short i;
		
		needinit = false;
		
		clearbytes (&labelcache, longsizeof (labelcache));
		
		for (i = 0; i < ctlabels; i++) {
			
			OSErr ec;
			RGBColor rgb;
			bigstring bs;
			
			ec = GetLabel (i, &rgb, bs);
			
			if (ec == noErr) {
				
				newheapstring (bs, &labelcache [i].hstring);
				
				labelcache [i].rgb = rgb;
				}
			} /*for*/
		}
	
	/*get the string and rgb from the label cache*/ {
		
		hdlstring hstring = labelcache [ixlabel].hstring;
		
		if (hstring == nil)
			return (false);
			
		*rgb = labelcache [ixlabel].rgb;
		
		copyheapstring (hstring, bslabel);
		
		return (true);
		}
	} /*filegetlabelinfo*/
	
	
static boolean label2index (bigstring bslabel, short *ixlabel) {
	
	short i;
	bigstring bs;
	RGBColor rgb;
	
	for (i = 0; i < 8; i++) {
		
		filegetlabelinfo (i, &rgb, bs);
		
		if (equalstrings (bs, bslabel)) {
			
			*ixlabel = i;
			
			return (true);
			}
		} /*for*/
		
	return (false);
	} /*label2index*/


boolean filesetlabel (ptrfilespec pfs, short ixlabel) {
	
	CInfoPBRec pb;
	
	if (!getmacfileinfo (pfs, &pb))
		return (false);
		
	pb.hFileInfo.ioFlFndrInfo.fdFlags &= 0xFFF1; /*clear out old index*/
	
	pb.hFileInfo.ioFlFndrInfo.fdFlags |= ixlabel << 1; /*slam in new index*/
	
	return (setmacfileinfo (pfs, &pb));
	} /*filesetlabel*/