/*  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)
				{
#ifdef OLD_STYLE_DEFAULT_KEY
					if(node -> Key && node -> Special . Button . Lines)
#else
					if(node -> Key && (node -> Special . Button . Lines || node -> Special . Button . ReturnKey))
#endif
					{
						BOOL	GotIt = FALSE;
						WORD	i,j,len;
						STRPTR	label;

						if(node -> Special . Button . Lines)
							lines = node -> Special . Button . Lines;

						for(j = 0 ; ; j++)
						{
							if(lines)
							{
								if(!(label = lines[j]))
									break;
							}
							else
								label = node -> Label;

							len = strlen(label);

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

									if(!NewLabel)
										return;
									else
									{
										if(i)
											CopyMem(label,NewLabel,i);

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

										if(lines)
											lines[j] = NewLabel;
										else
											node -> Label = NewLabel;

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

										GotIt = TRUE;
									}
								}
							}

							if(!lines || GotIt)
								break;
						}

						break;
					}

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

			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:
#if defined(DO_POPUP_KIND) && defined(DO_BOOPSI_KIND)
			case POPUP_KIND:
#endif
#if defined(DO_TAB_KIND) && defined(DO_BOOPSI_KIND)
			case TAB_KIND:
#endif
#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;

					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
