Пример #1
0
/**
 * Draw single object based on its type
 */
static void SDLGui_DrawObj(const SGOBJ *dlg, int i)
{
	switch (dlg[i].type)
	{
	case SGBOX:
		SDLGui_DrawBox(dlg, i);
		break;
	case SGTEXT:
		SDLGui_DrawText(dlg, i);
		break;
	case SGEDITFIELD:
		SDLGui_DrawEditField(dlg, i);
		break;
	case SGBUTTON:
		SDLGui_DrawButton(dlg, i);
		break;
	case SGRADIOBUT:
		SDLGui_DrawRadioButton(dlg, i);
		break;
	case SGCHECKBOX:
		SDLGui_DrawCheckBox(dlg, i);
		break;
	case SGPOPUP:
		SDLGui_DrawPopupButton(dlg, i);
		break;
	case SGSCROLLBAR:
		SDLGui_DrawScrollbar(dlg, i);
		break;
	}
}
Пример #2
0
/**
 * Draw a whole dialog.
 */
void SDLGui_DrawDialog(const SGOBJ *dlg)
{
	int i;
	for (i = 0; dlg[i].type != -1; i++)
	{
		switch (dlg[i].type)
		{
		 case SGBOX:
			SDLGui_DrawBox(dlg, i);
			break;
		 case SGTEXT:
			SDLGui_DrawText(dlg, i);
			break;
		 case SGEDITFIELD:
			SDLGui_DrawEditField(dlg, i);
			break;
		 case SGBUTTON:
			SDLGui_DrawButton(dlg, i);
			break;
		 case SGRADIOBUT:
			SDLGui_DrawRadioButton(dlg, i);
			break;
		 case SGCHECKBOX:
			SDLGui_DrawCheckBox(dlg, i);
			break;
		 case SGPOPUP:
			SDLGui_DrawPopupButton(dlg, i);
			break;
        case SGSCROLLBAR:
 			SDLGui_DrawScrollbar(dlg, i);
 			break;
		}
	}
	SDL_UpdateRect(pSdlGuiScrn, 0,0,0,0);
}
Пример #3
0
/**
 * Draw a whole dialog.
 */
void SDLGui_DrawDialog(const SGOBJ *dlg)
{
	int32_t i;

	memset(bmp, 0, sizeof(bmp));

	for (i = 0; dlg[i].type != -1; i++)
	{
		switch (dlg[i].type)
		{
		 case SGBOX:
			SDLGui_DrawBox(dlg, i);
			break;
		 case SGTEXT:
			SDLGui_DrawText(dlg, i);
			break;
		 case SGEDITFIELD:
			SDLGui_DrawEditField(dlg, i);
			break;
		 case SGBUTTON:
			SDLGui_DrawButton(dlg, i);
			break;
		 case SGRADIOBUT:
			SDLGui_DrawRadioButton(dlg, i);
			break;
		 case SGCHECKBOX:
			SDLGui_DrawCheckBox(dlg, i);
			break;
		 case SGPOPUP:
			SDLGui_DrawPopupButton(dlg, i);
			break;
		 case SGSCROLLBAR:
			SDLGui_DrawScrollbar(dlg, i);
			break;
		}
	}

}
Пример #4
0
/*
  Draw an object.
*/
void SDLGui_DrawObject(SGOBJ *dlg, int objnum)
{
  switch (dlg[objnum].type)
  {
    case SGBOX:
      SDLGui_DrawBox(dlg, objnum);
      break;
    case SGTEXT:
      SDLGui_DrawText(dlg, objnum);
      break;
    case SGEDITFIELD:
      SDLGui_DrawEditField(dlg, objnum);
      break;
    case SGBUTTON:
      SDLGui_DrawButton(dlg, objnum);
      break;
    case SGCHECKBOX:
      SDLGui_DrawCheckBox(dlg, objnum);
      break;
    case SGPOPUP:
      SDLGui_DrawPopupButton(dlg, objnum);
      break;
  }
}
Пример #5
0
/**SDLGui_DoDialog
 * Show and process a dialog. Returns the button number that has been
 * pressed or SDLGUI_UNKNOWNEVENT if an unsupported event occured (will be
 * stored in parameter pEventOut).
 */
int32_t SDLGui_DoDialog(SGOBJ *dlg, int/*SDL_Event */*pEventOut)
{
	int32_t obj=0;
	int32_t oldbutton=0;
	int32_t retbutton=0;
	int32_t i, j, b;
	int32_t /*SDL_Event*/ sdlEvent;//SDL_Event sdlEvent;
	SDL_Rect rct;
	Uint32 grey;

	SDL_Rect dlgrect, bgrect;

        grey = 0xC0C0C0FF;

	dlgrect.x = dlg[0].x * sdlgui_fontwidth;
	dlgrect.y = dlg[0].y * sdlgui_fontheight;
	dlgrect.w = dlg[0].w * sdlgui_fontwidth;
	dlgrect.h = dlg[0].h * sdlgui_fontheight;

	bgrect.x = bgrect.y = 0;
	bgrect.w = dlgrect.w;
	bgrect.h = dlgrect.h;



	/* (Re-)draw the dialog */
	SDLGui_DrawDialog(dlg);

	input_gui();

        if(touch!=-1){b=1;boutc=1;}
	else {b=0;boutc=0;}

	i=gmx;j=gmy;

	/* If current object is the scrollbar, and mouse is still down, we can scroll it */
	/* also if the mouse pointer has left the scrollbar */
	if (dlg[current_object].type == SGSCROLLBAR) {
		if (b == 1/*& SDL_BUTTON(1))*/) {
			obj = current_object;
			dlg[obj].state |= SG_MOUSEDOWN;
			oldbutton = obj;
			retbutton = obj;

		}
		else {
			obj = current_object;
			current_object = 0;
			dlg[obj].state &= SG_MOUSEUP;
			//retbutton = obj;
			//oldbutton = obj;
			okold=1;

		}
	}
	else {
		obj = SDLGui_FindObj(dlg, i, j);
		current_object = obj;
		if (obj > 0 && (dlg[obj].flags&SG_TOUCHEXIT) )
		{
			oldbutton = obj;
			if (b ==1/*2*/ /*& SDL_BUTTON(1)*/)
			{
				dlg[obj].state |= SG_SELECTED;
				retbutton = obj;
			}else{
                                dlg[obj].state &= ~SG_SELECTED;
			}
		}
	}

        /* The main loop */
	while (retbutton == 0 && !bQuitProgram)
	{

		input_gui();

		draw_cross(gmx,gmy);

                if(touch!=-1 && okold==0 ){

			okold=1;

			obj = SDLGui_FindObj(dlg, gmx, gmy);
				if (obj>0)
				{
					if (dlg[obj].type==SGBUTTON)
					{
						dlg[obj].state |= SG_SELECTED;
						SDLGui_DrawButton(dlg, obj);


						oldbutton=obj;//dbgoldobj=obj;
					}
                                        if (dlg[obj].type==SGSCROLLBAR)
					{
						dlg[obj].state |= SG_MOUSEDOWN;
						oldbutton=obj;

					}
					if ( dlg[obj].flags&SG_TOUCHEXIT )
					{
						dlg[obj].state |= SG_SELECTED;
						retbutton = obj;
					}
				}

		}
		else if(touch==-1 && okold==1){
			okold=0;

				// It was the left button: Find the object under the mouse cursor /
				obj = SDLGui_FindObj(dlg, gmx, gmy);
				if (obj>0)
				{
					switch (dlg[obj].type)
					{
					 case SGBUTTON:
						if (oldbutton==obj){
							retbutton=obj;}
						break;

					 case SGSCROLLBAR:
 						dlg[obj].state &= SG_MOUSEUP;

						if (oldbutton==obj)
							retbutton=obj;
						break;
					 case SGEDITFIELD:
						SDLGui_EditField(dlg, obj);
						break;
					 case SGRADIOBUT:
						for (i = obj-1; i > 0 && dlg[i].type == SGRADIOBUT; i--)
						{
							dlg[i].state &= ~SG_SELECTED;  // Deselect all radio buttons in this group /

							DrawBoxF((dlg[0].x+dlg[i].x)*fontwidth,(dlg[0].y+dlg[i].y)*fontheight,0,fontwidth ,fontheight,grey);
							SDLGui_DrawRadioButton(dlg, i);

						}
						for (i = obj+1; dlg[i].type == SGRADIOBUT; i++)
						{
							dlg[i].state &= ~SG_SELECTED;  // Deselect all radio buttons in this group /

                                                        DrawBoxF((dlg[0].x+dlg[i].x)*fontwidth,(dlg[0].y+dlg[i].y)*fontheight,0,fontwidth ,fontheight,grey);
							SDLGui_DrawRadioButton(dlg, i);

						}
						dlg[obj].state |= SG_SELECTED;  // Select this radio button

                                                DrawBoxF((dlg[0].x+dlg[obj].x)*fontwidth,(dlg[0].y+dlg[obj].y)*fontheight,0,fontwidth ,fontheight,grey);
						SDLGui_DrawRadioButton(dlg, obj);

						break;
					 case SGCHECKBOX:
						dlg[obj].state ^= SG_SELECTED;

                                                DrawBoxF((dlg[0].x+dlg[obj].x)*fontwidth,(dlg[0].y+dlg[obj].y)*fontheight,0,fontwidth ,fontheight,grey);
						SDLGui_DrawCheckBox(dlg, obj);

						break;
					 case SGPOPUP:
						dlg[obj].state |= SG_SELECTED;
						SDLGui_DrawPopupButton(dlg, obj);

						retbutton=obj;
						break;
					}
				}
				if (oldbutton > 0)
				{
					dlg[oldbutton].state &= ~SG_SELECTED;
					SDLGui_DrawButton(dlg, oldbutton);
					oldbutton = 0;//dbgoldobj=0;
				}
				if (obj >= 0 && (dlg[obj].flags&SG_EXIT))
				{
					if(dlg[obj].type==SGBUTTON)dlg[obj].state &= ~SG_SELECTED;
					retbutton = obj;
				}


		}

		if(retbutton ==0)retbutton = 1;//SG_TOUCHEXIT  ;

	}


	if (retbutton == SDLGUI_QUIT)
		bQuitProgram = true;

	return retbutton;
}
Пример #6
0
/**
 * Show and process a dialog. Returns either:
 * - index of the GUI item that was invoked
 * - SDLGUI_UNKNOWNEVENT if an unsupported event occurred
 *   (will be stored in parameter pEventOut)
 * - SDLGUI_QUIT if user wants to close Hatari
 * - SDLGUI_ERROR if unable to show dialog
 * GUI item indeces are positive, other return values are negative
 */
int SDLGui_DoDialog(SGOBJ *dlg, SDL_Event *pEventOut, bool KeepCurrentObject)
{
	int oldbutton = SDLGUI_NOTFOUND;
	int retbutton = SDLGUI_NOTFOUND;
	int i, j, b, value, obj;
	SDLKey key;
	int focused;
	SDL_Event sdlEvent;
	SDL_Surface *pBgSurface;
	SDL_Rect dlgrect, bgrect;
	SDL_Joystick *joy = NULL;
#if !WITH_SDL2
	int nOldUnicodeMode;
#endif

	/* In the case of dialog using a scrollbar, we must keep the previous */
	/* value of current_object, as the same dialog is displayed in a loop */
	/* to handle scrolling. For other dialogs, we need to reset current_object */
	/* (ie no object selected at start when displaying the dialog) */
	if ( !KeepCurrentObject )
		current_object = 0;

	if (pSdlGuiScrn->h / sdlgui_fontheight < dlg[0].h)
	{
		fprintf(stderr, "Screen size too small for dialog!\n");
		return SDLGUI_ERROR;
	}

	dlgrect.x = dlg[0].x * sdlgui_fontwidth;
	dlgrect.y = dlg[0].y * sdlgui_fontheight;
	dlgrect.w = dlg[0].w * sdlgui_fontwidth;
	dlgrect.h = dlg[0].h * sdlgui_fontheight;

	bgrect.x = bgrect.y = 0;
	bgrect.w = dlgrect.w;
	bgrect.h = dlgrect.h;

	/* Save background */
	pBgSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, dlgrect.w, dlgrect.h, pSdlGuiScrn->format->BitsPerPixel,
	                                  pSdlGuiScrn->format->Rmask, pSdlGuiScrn->format->Gmask, pSdlGuiScrn->format->Bmask, pSdlGuiScrn->format->Amask);
	if (pSdlGuiScrn->format->palette != NULL)
	{
		SDL_SetColors(pBgSurface, pSdlGuiScrn->format->palette->colors, 0, pSdlGuiScrn->format->palette->ncolors-1);
	}

	if (pBgSurface != NULL)
	{
		SDL_BlitSurface(pSdlGuiScrn,  &dlgrect, pBgSurface, &bgrect);
	}
	else
	{
		fprintf(stderr, "SDLGUI_DoDialog: CreateRGBSurface failed: %s\n", SDL_GetError());
	}
	SDLGui_DebugPrintDialog(dlg);

	/* focus default button if nothing else is focused */
	focused = SDLGui_SearchState(dlg, SG_FOCUSED);
	if (focused == SDLGUI_NOTFOUND)
	{
		int defbutton = SDLGui_SearchFlags(dlg, SG_DEFAULT);
		if (defbutton != SDLGUI_NOTFOUND)
		{
			dlg[defbutton].state |= SG_FOCUSED;
			focused = defbutton;
		}
	}
	Dprintf(("focused: %d\n", focused));
	SDLGui_SetShortcuts(dlg);

	/* (Re-)draw the dialog */
	SDLGui_DrawDialog(dlg);

	/* Is the left mouse button still pressed? Yes -> Handle TOUCHEXIT objects here */
	SDL_PumpEvents();
	b = SDL_GetMouseState(&i, &j);

	/* If current object is the scrollbar, and mouse is still down, we can scroll it */
	/* also if the mouse pointer has left the scrollbar */
	if (current_object != SDLGUI_NOTFOUND && dlg[current_object].type == SGSCROLLBAR) {
		obj = current_object;
		retbutton = obj;
		oldbutton = obj;
		if (b & SDL_BUTTON(1))
		{
			dlg[obj].state |= SG_MOUSEDOWN;
		}
		else
		{
			current_object = 0;
			dlg[obj].state &= ~SG_MOUSEDOWN;
		}
	}
	else {
		obj = SDLGui_FindObj(dlg, i, j);
		current_object = obj;
		if (obj != SDLGUI_NOTFOUND && (dlg[obj].flags&SG_TOUCHEXIT) )
		{
			oldbutton = obj;
			if (b & SDL_BUTTON(1))
			{
				dlg[obj].state |= SG_SELECTED;
				retbutton = obj;
			}
		}
	}

	if (SDL_NumJoysticks() > 0)
		joy = SDL_JoystickOpen(0);

#if !WITH_SDL2
	/* Enable unicode translation to get shifted etc chars with SDL_PollEvent */
	nOldUnicodeMode = SDL_EnableUNICODE(true);
#endif
	Dprintf(("ENTER - obj: %d, old: %d, ret: %d\n", obj, oldbutton, retbutton));

	/* The main loop */
	while (retbutton == SDLGUI_NOTFOUND && !bQuitProgram)
	{
		if (SDL_WaitEvent(&sdlEvent) == 1)  /* Wait for events */
		
			switch (sdlEvent.type)
			{
			 case SDL_QUIT:
				retbutton = SDLGUI_QUIT;
				break;

			 case SDL_MOUSEBUTTONDOWN:
				if (sdlEvent.button.button != SDL_BUTTON_LEFT)
				{
					/* Not left mouse button -> unsupported event */
					if (pEventOut)
						retbutton = SDLGUI_UNKNOWNEVENT;
					break;
				}
				/* It was the left button: Find the object under the mouse cursor */
				SDLGui_ScaleMouseButtonCoordinates(&sdlEvent.button);
				obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y);
				if (obj != SDLGUI_NOTFOUND)
				{
					if (dlg[obj].type==SGBUTTON)
					{
						dlg[obj].state |= SG_SELECTED;
						SDLGui_DrawButton(dlg, obj);
						SDL_UpdateRect(pSdlGuiScrn, (dlg[0].x+dlg[obj].x)*sdlgui_fontwidth-2, (dlg[0].y+dlg[obj].y)*sdlgui_fontheight-2,
						               dlg[obj].w*sdlgui_fontwidth+4, dlg[obj].h*sdlgui_fontheight+4);
						oldbutton=obj;
					}
					if (dlg[obj].type==SGSCROLLBAR)
					{
						dlg[obj].state |= SG_MOUSEDOWN;
						oldbutton=obj;
					}
					if ( dlg[obj].flags&SG_TOUCHEXIT )
					{
						dlg[obj].state |= SG_SELECTED;
						retbutton = obj;
					}
				}
				break;

			 case SDL_MOUSEBUTTONUP:
				if (sdlEvent.button.button != SDL_BUTTON_LEFT)
				{
					/* Not left mouse button -> unsupported event */
					if (pEventOut)
						retbutton = SDLGUI_UNKNOWNEVENT;
					break;
				}
				/* It was the left button: Find the object under the mouse cursor */
				SDLGui_ScaleMouseButtonCoordinates(&sdlEvent.button);
				obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y);
				if (obj != SDLGUI_NOTFOUND)
				{
					retbutton = SDLGui_HandleSelection(dlg, obj, oldbutton);
				}
				if (oldbutton != SDLGUI_NOTFOUND && dlg[oldbutton].type == SGBUTTON)
				{
					dlg[oldbutton].state &= ~SG_SELECTED;
					SDLGui_DrawButton(dlg, oldbutton);
					SDL_UpdateRect(pSdlGuiScrn, (dlg[0].x+dlg[oldbutton].x)*sdlgui_fontwidth-2, (dlg[0].y+dlg[oldbutton].y)*sdlgui_fontheight-2,
					               dlg[oldbutton].w*sdlgui_fontwidth+4, dlg[oldbutton].h*sdlgui_fontheight+4);
					oldbutton = SDLGUI_NOTFOUND;
				}
				break;

			 case SDL_JOYAXISMOTION:
				value = sdlEvent.jaxis.value;
				if (value < -3200 || value > 3200)
				{
					if(sdlEvent.jaxis.axis == 0)
					{
						/* Left-right movement */
						if (value < 0)
							retbutton = SDLGui_HandleShortcut(dlg, SG_SHORTCUT_LEFT);
						else
							retbutton = SDLGui_HandleShortcut(dlg, SG_SHORTCUT_RIGHT);
					}
					else if(sdlEvent.jaxis.axis == 1)
					{
						/* Up-Down movement */
						if (value < 0)
						{
							SDLGui_RemoveFocus(dlg, focused);
							focused = SDLGui_FocusNext(dlg, focused, -1);
						}
						else
						{
							SDLGui_RemoveFocus(dlg, focused);
							focused = SDLGui_FocusNext(dlg, focused, +1);
						}
					}
				}
				break;

			 case SDL_JOYBUTTONDOWN:
				retbutton = SDLGui_HandleSelection(dlg, focused, focused);
				break;

			 case SDL_JOYBALLMOTION:
			 case SDL_JOYHATMOTION:
			 case SDL_MOUSEMOTION:
				break;

			 case SDL_KEYDOWN:                     /* Key pressed */
				key = sdlEvent.key.keysym.sym;
				/* keyboard shortcuts are with modifiers */
				if (sdlEvent.key.keysym.mod & KMOD_LALT
				    || sdlEvent.key.keysym.mod & KMOD_RALT)
				{
					if (key == SDLK_LEFT)
						retbutton = SDLGui_HandleShortcut(dlg, SG_SHORTCUT_LEFT);
					else if (key == SDLK_RIGHT)
						retbutton = SDLGui_HandleShortcut(dlg, SG_SHORTCUT_RIGHT);
					else if (key == SDLK_UP)
						retbutton = SDLGui_HandleShortcut(dlg, SG_SHORTCUT_UP);
					else if (key == SDLK_DOWN)
						retbutton = SDLGui_HandleShortcut(dlg, SG_SHORTCUT_DOWN);
					else
					{
#if !WITH_SDL2
						/* unicode member is needed to handle shifted etc special chars */
						key = sdlEvent.key.keysym.unicode;
#endif
						if (key >= 33 && key <= 126)
							retbutton = SDLGui_HandleShortcut(dlg, toupper(key));
					}
					if (!retbutton && pEventOut)
						retbutton = SDLGUI_UNKNOWNEVENT;
					break;
				}
				switch (key)
				{
				 case SDLK_UP:
				 case SDLK_LEFT:
					SDLGui_RemoveFocus(dlg, focused);
					focused = SDLGui_FocusNext(dlg, focused, -1);
					break;
				 case SDLK_TAB:
				 case SDLK_DOWN:
				 case SDLK_RIGHT:
					SDLGui_RemoveFocus(dlg, focused);
					focused = SDLGui_FocusNext(dlg, focused, +1);
					break;
				 case SDLK_HOME:
					SDLGui_RemoveFocus(dlg, focused);
					focused = SDLGui_FocusNext(dlg, 1, +1);
					break;
				 case SDLK_END:
					SDLGui_RemoveFocus(dlg, focused);
					focused = SDLGui_FocusNext(dlg, 1, -1);
					break;
				 case SDLK_SPACE:
				 case SDLK_RETURN:
				 case SDLK_KP_ENTER:
					retbutton = SDLGui_HandleSelection(dlg, focused, focused);
					break;
				 case SDLK_ESCAPE:
					retbutton = SDLGui_SearchFlags(dlg, SG_CANCEL);
					break;
				 default:
					if (pEventOut)
						retbutton = SDLGUI_UNKNOWNEVENT;
					break;
				}
				break;

#if WITH_SDL2
			 case SDL_WINDOWEVENT:
				if (sdlEvent.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
				{
					SDL_UpdateRect(pSdlGuiScrn, 0, 0, 0, 0);
				}
				break;
#endif

			 default:
				if (pEventOut)
					retbutton = SDLGUI_UNKNOWNEVENT;
				break;
			}
	}

	/* Restore background */
	if (pBgSurface)
	{
		SDL_BlitSurface(pBgSurface, &bgrect, pSdlGuiScrn,  &dlgrect);
		SDL_FreeSurface(pBgSurface);
	}

	/* Copy event data of unsupported events if caller wants to have it */
	if (retbutton == SDLGUI_UNKNOWNEVENT && pEventOut)
		memcpy(pEventOut, &sdlEvent, sizeof(SDL_Event));

	if (retbutton == SDLGUI_QUIT)
		bQuitProgram = true;

#if !WITH_SDL2
	SDL_EnableUNICODE(nOldUnicodeMode);
#endif
	if (joy)
		SDL_JoystickClose(joy);

	Dprintf(("EXIT - ret: %d, current: %d\n", retbutton, current_object));
	return retbutton;
}
Пример #7
0
/**
 * Show and process a dialog. Returns the button number that has been
 * pressed or SDLGUI_UNKNOWNEVENT if an unsupported event occured (will be
 * stored in parameter pEventOut).
 */
int SDLGui_DoDialog(SGOBJ *dlg, SDL_Event *pEventOut)
{
	int obj=0;
	int oldbutton=0;
	int retbutton=0;
	int i, j, b;
	SDL_Event sdlEvent;
	SDL_Rect rct;
	Uint32 grey;
	SDL_Surface *pBgSurface;
	SDL_Rect dlgrect, bgrect;

	if (pSdlGuiScrn->h / sdlgui_fontheight < dlg[0].h)
	{
		fprintf(stderr, "Screen size too small for dialog!\n");
		return SDLGUI_ERROR;
	}

	grey = SDL_MapRGB(pSdlGuiScrn->format,181,183,170);

	dlgrect.x = dlg[0].x * sdlgui_fontwidth;
 	dlgrect.y = dlg[0].y * sdlgui_fontheight;
 	dlgrect.w = dlg[0].w * sdlgui_fontwidth;
 	dlgrect.h = dlg[0].h * sdlgui_fontheight;

	bgrect.x = bgrect.y = 0;
	bgrect.w = dlgrect.w;
	bgrect.h = dlgrect.h;

	/* Save background */
	pBgSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, dlgrect.w, dlgrect.h, pSdlGuiScrn->format->BitsPerPixel,
	                                  pSdlGuiScrn->format->Rmask, pSdlGuiScrn->format->Gmask, pSdlGuiScrn->format->Bmask, pSdlGuiScrn->format->Amask);
	if (pSdlGuiScrn->format->palette != NULL)
	{
		SDL_SetPaletteColors(pBgSurface->format->palette, pSdlGuiScrn->format->palette->colors, 0, pSdlGuiScrn->format->palette->ncolors-1);
	}

	if (pBgSurface != NULL)
	{
		SDL_BlitSurface(pSdlGuiScrn,  &dlgrect, pBgSurface, &bgrect);
	}
	else
	{
		fprintf(stderr, "SDLGUI_DoDialog: CreateRGBSurface failed: %s\n", SDL_GetError());
	}

	/* (Re-)draw the dialog */
	SDLGui_DrawDialog(dlg);

	/* Is the left mouse button still pressed? Yes -> Handle TOUCHEXIT objects here */
	SDL_PumpEvents();
	b = SDL_GetMouseState(&i, &j);

 	/* If current object is the scrollbar, and mouse is still down, we can scroll it */
 	/* also if the mouse pointer has left the scrollbar */
 	if (dlg[current_object].type == SGSCROLLBAR) {
 		if (b & SDL_BUTTON(1)) {
 			obj = current_object;
 			dlg[obj].state |= SG_MOUSEDOWN;
 			oldbutton = obj;
 			retbutton = obj;
 		}
 		else {
			obj = current_object;
 			current_object = 0;
 			dlg[obj].state &= SG_MOUSEUP;
			retbutton = obj;
            oldbutton = obj;
		}
	}
    else {
 		obj = SDLGui_FindObj(dlg, i, j);
 		current_object = obj;
 		if (obj > 0 && (dlg[obj].flags&SG_TOUCHEXIT) )
 		{
 			oldbutton = obj;
			if (b & SDL_BUTTON(1))
 			{
 				dlg[obj].state |= SG_SELECTED;
 				retbutton = obj;
 			}
 		}
 	}

    
	/* The main loop */
	while (retbutton == 0 && !bQuitProgram)
	{
		if (SDL_WaitEvent(&sdlEvent) == 1)  /* Wait for events */
            
			switch (sdlEvent.type)
			{
			 case SDL_QUIT:
				retbutton = SDLGUI_QUIT;
				break;

			 case SDL_MOUSEBUTTONDOWN:
				if (sdlEvent.button.button != SDL_BUTTON_LEFT)
				{
					/* Not left mouse button -> unsupported event */
					if (pEventOut)
						retbutton = SDLGUI_UNKNOWNEVENT;
					break;
				}
				/* It was the left button: Find the object under the mouse cursor */
				obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y);
				if (obj>0)
				{
					if (dlg[obj].type==SGBUTTON)
					{
						dlg[obj].state |= SG_SELECTED;
						SDLGui_DrawButton(dlg, obj);
						SDL_UpdateRect(pSdlGuiScrn, (dlg[0].x+dlg[obj].x)*sdlgui_fontwidth-2, (dlg[0].y+dlg[obj].y)*sdlgui_fontheight-2,
 						               dlg[obj].w*sdlgui_fontwidth+4, dlg[obj].h*sdlgui_fontheight+4);
 						oldbutton=obj;
 					}
 					if (dlg[obj].type==SGSCROLLBAR)
 					{
 						dlg[obj].state |= SG_MOUSEDOWN;
						oldbutton=obj;
					}
					if ( dlg[obj].flags&SG_TOUCHEXIT )
					{
						dlg[obj].state |= SG_SELECTED;
						retbutton = obj;
					}
				}
				break;

			 case SDL_MOUSEBUTTONUP:
				if (sdlEvent.button.button != SDL_BUTTON_LEFT)
				{
					/* Not left mouse button -> unsupported event */
					if (pEventOut)
						retbutton = SDLGUI_UNKNOWNEVENT;
					break;
				}
				/* It was the left button: Find the object under the mouse cursor */
				obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y);
				if (obj>0)
				{
					switch (dlg[obj].type)
					{
					 case SGBUTTON:
						if (oldbutton==obj)
							retbutton=obj;
						break;
                        case SGSCROLLBAR:
  						dlg[obj].state &= SG_MOUSEUP;
 
 						if (oldbutton==obj)
							retbutton=obj;
 						break;
 					case SGEDITFIELD:
						SDLGui_EditField(dlg, obj);
						break;
					 case SGRADIOBUT:
						for (i = obj-1; i > 0 && dlg[i].type == SGRADIOBUT; i--)
						{
							dlg[i].state &= ~SG_SELECTED;  /* Deselect all radio buttons in this group */
							rct.x = (dlg[0].x+dlg[i].x)*sdlgui_fontwidth;
 							rct.y = (dlg[0].y+dlg[i].y)*sdlgui_fontheight;
							rct.w = sdlgui_fontwidth;
							rct.h = sdlgui_fontheight;
							SDL_FillRect(pSdlGuiScrn, &rct, grey); /* Clear old */
							SDLGui_DrawRadioButton(dlg, i);
							SDL_UpdateRects(pSdlGuiScrn, 1, &rct);
						}
						for (i = obj+1; dlg[i].type == SGRADIOBUT; i++)
						{
							dlg[i].state &= ~SG_SELECTED;  /* Deselect all radio buttons in this group */
							rct.x = (dlg[0].x+dlg[i].x)*sdlgui_fontwidth;
 							rct.y = (dlg[0].y+dlg[i].y)*sdlgui_fontheight;
 							rct.w = sdlgui_fontwidth;
 							rct.h = sdlgui_fontheight;
							SDL_FillRect(pSdlGuiScrn, &rct, grey); /* Clear old */
							SDLGui_DrawRadioButton(dlg, i);
							SDL_UpdateRects(pSdlGuiScrn, 1, &rct);
						}
						dlg[obj].state |= SG_SELECTED;  /* Select this radio button */
                            rct.x = (dlg[0].x+dlg[obj].x)*sdlgui_fontwidth;
 						rct.y = (dlg[0].y+dlg[obj].y)*sdlgui_fontheight;
 						rct.w = sdlgui_fontwidth;
 						rct.h = sdlgui_fontheight;
						SDL_FillRect(pSdlGuiScrn, &rct, grey); /* Clear old */
						SDLGui_DrawRadioButton(dlg, obj);
						SDL_UpdateRects(pSdlGuiScrn, 1, &rct);
                            retbutton = obj; // added by andreas_g
						break;
					 case SGCHECKBOX:
						dlg[obj].state ^= SG_SELECTED;
                        rct.x = (dlg[0].x+dlg[obj].x)*sdlgui_fontwidth;
 						rct.y = (dlg[0].y+dlg[obj].y)*sdlgui_fontheight;
 						rct.w = sdlgui_fontwidth;
 						rct.h = sdlgui_fontheight;
						SDL_FillRect(pSdlGuiScrn, &rct, grey); /* Clear old */
						SDLGui_DrawCheckBox(dlg, obj);
						SDL_UpdateRects(pSdlGuiScrn, 1, &rct);
                            retbutton = obj; // added by andreas_g
						break;
					 case SGPOPUP:
						dlg[obj].state |= SG_SELECTED;
						SDLGui_DrawPopupButton(dlg, obj);
                            SDL_UpdateRect(pSdlGuiScrn, (dlg[0].x+dlg[obj].x)*sdlgui_fontwidth-2, (dlg[0].y+dlg[obj].y)*sdlgui_fontheight-2,
 						               dlg[obj].w*sdlgui_fontwidth+4, dlg[obj].h*sdlgui_fontheight+4);
						retbutton=obj;
						break;
					}
				}
				if (oldbutton > 0 && dlg[oldbutton].type == SGBUTTON)
				{
					dlg[oldbutton].state &= ~SG_SELECTED;
					SDLGui_DrawButton(dlg, oldbutton);
					SDL_UpdateRect(pSdlGuiScrn, (dlg[0].x+dlg[oldbutton].x)*sdlgui_fontwidth-2, (dlg[0].y+dlg[oldbutton].y)*sdlgui_fontheight-2,
 					               dlg[oldbutton].w*sdlgui_fontwidth+4, dlg[oldbutton].h*sdlgui_fontheight+4);
					oldbutton = 0;
				}
				if (obj >= 0 && (dlg[obj].flags&SG_EXIT))
				{
					retbutton = obj;
				}
				break;

			 case SDL_JOYAXISMOTION:
			 case SDL_JOYBALLMOTION:
			 case SDL_JOYHATMOTION:
			 case SDL_MOUSEMOTION:
				break;

			 case SDL_KEYDOWN:                     /* Key pressed */
				if (sdlEvent.key.keysym.sym == SDLK_RETURN
				    || sdlEvent.key.keysym.sym == SDLK_KP_ENTER)
				{
					retbutton = SDLGui_SearchFlaggedButton(dlg, SG_DEFAULT);
				}
				else if (sdlEvent.key.keysym.sym == SDLK_ESCAPE)
				{
					retbutton = SDLGui_SearchFlaggedButton(dlg, SG_CANCEL);
				}
				else if (pEventOut)
				{
					retbutton = SDLGUI_UNKNOWNEVENT;
				}
				break;

			 default:
				if (pEventOut)
					retbutton = SDLGUI_UNKNOWNEVENT;
				break;
			}
	}

	/* Restore background */
	if (pBgSurface)
	{
		SDL_BlitSurface(pBgSurface, &bgrect, pSdlGuiScrn,  &dlgrect);
		SDL_FreeSurface(pBgSurface);
	}

	/* Copy event data of unsupported events if caller wants to have it */
	if (retbutton == SDLGUI_UNKNOWNEVENT && pEventOut)
		memcpy(pEventOut, &sdlEvent, sizeof(SDL_Event));

	if (retbutton == SDLGUI_QUIT)
		bQuitProgram = true;

	return retbutton;
}