/*  GadTools layout toolkit
**
**  Copyright © 1993-1995 by Olaf `Olsen' Barthel
**  Freely distributable.
*/

#include "gtlayout_global.h"

#ifdef DO_PICKSHORTCUTS
VOID __regargs
LTP_SelectKeys(LayoutHandle *handle,ObjectNode *group)
{
	UBYTE		 bunch;
	ObjectNode	*node;
	ULONG		 page;
	STRPTR		*lines = NULL;

	SCANPAGE(group,node,page)
	{
		bunch = 0;

		switch(node -> Type)
		{
			case GROUP_KIND:

				LTP_SelectKeys(handle,node);
				break;

			case TEXT_KIND:

				if(!node -> Special . Text . UsePicker)
					break;

			case FRACTION_KIND:
			case PASSWORD_KIND:
			case STRING_KIND:

				if(node -> Special . String . UsePicker)
					bunch = 255;

			case BUTTON_KIND:

				if(node -> Type == BUTTON_KIND && !node -> NoKey && node -> Special . Button . Lines)
				{
					lines = node -> Special . Button . Lines;

					node -> Key = 0;
				}

			case BOOPSI_KIND:
			case CHECKBOX_KIND:
			case INTEGER_KIND:

				bunch++;

			case LISTVIEW_KIND:
			case MX_KIND:
			case CYCLE_KIND:
			case PALETTE_KIND:
			case SCROLLER_KIND:
			case SLIDER_KIND:
#ifdef DO_LEVEL_KIND
			case LEVEL_KIND:
#endif	/* DO_LEVEL_KIND */
#ifdef DO_TAPEDECK_KIND
			case TAPEDECK_KIND:
#endif	/* DO_TAPEDECK_KIND */

				bunch++;

				if(node -> Label && !node -> Key && !node -> NoKey)
				{
					WORD	i,len,code,glyph;
					STRPTR	label;

					if(lines)
					{
						BOOL GotIt = FALSE;
						WORD j;

						for(j = 0 ; !GotIt && lines[j] ; j++)
						{
							len = strlen(lines[j]);

							for(i = 0 ; !GotIt && i < len ; i++)
							{
								if(lines[j][i] == '_' && i != len - 1)
								{
									STRPTR NewLabel = LTP_Alloc(handle,len);

									if(!NewLabel)
										return;
									else
									{
										if(i)
											CopyMem(lines[j],NewLabel,i);

										CopyMem(&lines[j][i + 1],&NewLabel[i],len - i);

										lines[j] = NewLabel;

										node -> Special . Button . KeyStroke = &NewLabel[i];

										code = ToLower(*node -> Special . Button . KeyStroke);

										GotIt = TRUE;

										node -> Key = code;

										handle -> Keys[code] = handle -> Keys[LTP_Keys[1][code]] = TRUE;
									}
								}
							}
						}

						if(GotIt)
							break;
					}

					do
					{
						if(lines)
						{
							if(!(*lines))
								break;

							label = *lines;
						}
						else
							label = node -> Label;

						len = strlen(label);

						for(i = 0 ; i < len ; i++)
						{
							glyph	= ToLower(label[i]);
							code	= -1;

							if(bunch == 1)	// Support increment & decrement
							{
								if(LTP_Keys[0][glyph] && LTP_Keys[1][glyph])
									code = LTP_Keys[0][glyph];
							}
							else
							{
								if(bunch == 2)
								{
									if(LTP_Keys[0][glyph])
										code = LTP_Keys[0][glyph];
									else
									{
										if(LTP_Keys[1][glyph] && LTP_Keys[0][glyph])
										{
											if(ToLower(LTP_Keys[1][glyph]) == glyph)
												code = LTP_Keys[0][glyph];
										}
									}
								}
							}

							if(code != -1 && !handle -> Keys[code])
							{
								if(lines)
								{
									node -> Special . Button . KeyStroke = &label[i];

									node -> Key = code;

									handle -> Keys[code] = handle -> Keys[LTP_Keys[1][code]] = TRUE;

									lines = NULL;

									break;
								}
								else
								{
									STRPTR newLabel;

									if(newLabel = LTP_Alloc(handle,len + 2))
									{
										node -> Label	= newLabel;
										node -> Key	= code;

										if(i)
											CopyMem(label,newLabel,i);

										newLabel[i] = '_';

										CopyMem(label + i,&newLabel[i + 1],len - i);

										handle -> Keys[code] = handle -> Keys[LTP_Keys[1][code]] = TRUE;

										break;
									}
								}
							}
						}

						if(lines)
							lines++;
					}
					while(lines);
				}

				break;
		}
	}
}
#endif
