[hatari-devel] SDL GUI code cleanup |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/hatari-devel Archives
]
Hi,
Attached patch replaces few "magic values" in SDL GUI code with defines.
Functional change related to this is using common define (with negative
value) for "not found", instead of current mix of using object index 0
and value -1 for that.
Mainly I just wanted some feedback on whether the define names are OK,
but I'm also wondering about 1.9 release. Is this too risky change
before release or should I push it after release?
- Eero
diff -r fb8bfdf825a6 src/includes/sdlgui.h
--- a/src/includes/sdlgui.h Sat Jun 13 21:27:31 2015 +0300
+++ b/src/includes/sdlgui.h Sat Jun 13 21:32:33 2015 +0300
@@ -12,8 +12,10 @@
#include <SDL.h>
+/* object types: */
enum
{
+ SG_END = -1,
SGBOX,
SGTEXT,
SGEDITFIELD,
@@ -53,11 +55,11 @@
#define SGARROWDOWN 2
#define SGFOLDER 5
-/* Return codes: */
+/* Object matching return codes: (negative so they aren't mixed with object indeces) */
#define SDLGUI_ERROR -1
#define SDLGUI_QUIT -2
#define SDLGUI_UNKNOWNEVENT -3
-
+#define SDLGUI_NOTFOUND -4
typedef struct
{
diff -r fb8bfdf825a6 src/gui-sdl/dlgAbout.c
--- a/src/gui-sdl/dlgAbout.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgAbout.c Sat Jun 13 21:32:33 2015 +0300
@@ -36,7 +36,7 @@
{ SGTEXT, 0, 0, 1,19, 38,1, "WITHOUT ANY WARRANTY. See the GNU Ge-" },
{ SGTEXT, 0, 0, 1,20, 38,1, "neral Public License for more details." },
{ SGBUTTON, SG_DEFAULT, 0, 16,23, 8,1, "OK" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgAlert.c
--- a/src/gui-sdl/dlgAlert.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgAlert.c Sat Jun 13 21:32:33 2015 +0300
@@ -49,7 +49,7 @@
{ SGTEXT, 0, 0, 1,4, 50,1, dlglines[3] },
{ SGBUTTON, SG_DEFAULT, 0, 5,5, 8,1, "OK" },
{ SGBUTTON, SG_CANCEL, 0, 24,5, 8,1, "Cancel" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgDevice.c
--- a/src/gui-sdl/dlgDevice.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgDevice.c Sat Jun 13 21:32:33 2015 +0300
@@ -70,7 +70,7 @@
{ SGTEXT, 0, 0, 3,20, 46,1, dlgMidiOutName },
{ SGBUTTON, SG_DEFAULT, 0, 16,22, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgFileSelect.c
--- a/src/gui-sdl/dlgFileSelect.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgFileSelect.c Sat Jun 13 21:32:33 2015 +0300
@@ -91,7 +91,7 @@
{ SGCHECKBOX, SG_EXIT, 0, 2,23, 19,1, "_Show hidden files" },
{ SGBUTTON, SG_DEFAULT, 0, 32,23, 8,1, "OK" },
{ SGBUTTON, SG_CANCEL, 0, 50,23, 8,1, "Cancel" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgFloppy.c
--- a/src/gui-sdl/dlgFloppy.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgFloppy.c Sat Jun 13 21:32:33 2015 +0300
@@ -69,7 +69,7 @@
{ SGRADIOBUT, 0, 0, 28,16, 4,1, "O_n" },
{ SGRADIOBUT, 0, 0, 34,16, 6,1, "A_uto" },
{ SGBUTTON, SG_DEFAULT, 0, 22,18, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
@@ -85,7 +85,7 @@
{ SGBUTTON, 0, 0, 3,4, 10,1, "Drive _A:" },
{ SGBUTTON, 0, 0, 15,4, 10,1, "Drive _B:" },
{ SGBUTTON, SG_CANCEL, 0, 27,4, 10,1, "_Cancel" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgHalt.c
--- a/src/gui-sdl/dlgHalt.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgHalt.c Sat Jun 13 21:32:33 2015 +0300
@@ -30,7 +30,7 @@
{ SGBUTTON, 0, 0, 6,5, 12,1, "Cold reset" },
{ SGBUTTON, 0, 0, 28,3, 18,1, "Console debugger" },
{ SGBUTTON, SG_CANCEL, 0, 28,5, 18,1, "Quit Hatari" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgHardDisk.c
--- a/src/gui-sdl/dlgHardDisk.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgHardDisk.c Sat Jun 13 21:32:33 2015 +0300
@@ -72,7 +72,7 @@
{ SGCHECKBOX, 0, 0, 2,17, 16,1, "_Boot from HD" },
{ SGBUTTON, SG_DEFAULT, 0, 22,20, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgJoystick.c
--- a/src/gui-sdl/dlgJoystick.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgJoystick.c Sat Jun 13 21:32:33 2015 +0300
@@ -54,7 +54,7 @@
{ SGCHECKBOX, 0, 0, 2,13, 17,1, "Enable _autofire" },
{ SGBUTTON, SG_DEFAULT, 0, 6,16, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
@@ -68,7 +68,7 @@
{ SGBOX, 0, 0, 0,0, 28,5, NULL },
{ SGTEXT, 0, 0, 2,1, 24,1, sKeyInstruction },
{ SGTEXT, 0, 0, 2,3, 24,1, sKeyName },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgKeyboard.c
--- a/src/gui-sdl/dlgKeyboard.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgKeyboard.c Sat Jun 13 21:32:33 2015 +0300
@@ -39,7 +39,7 @@
{ SGBUTTON, 0, 0, 36, 7, 8,1, "_Browse" },
{ SGCHECKBOX, 0, 0, 2,10, 41,1, "_Disable key repeat in fast forward mode" },
{ SGBUTTON, SG_DEFAULT, 0, 13,12, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgMain.c
--- a/src/gui-sdl/dlgMain.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgMain.c Sat Jun 13 21:32:33 2015 +0300
@@ -60,7 +60,7 @@
{ SGBUTTON, SG_DEFAULT, 0, 21,15, 8,3, "OK" },
{ SGBUTTON, 0, 0, 36,15, 10,1, "_Quit" },
{ SGBUTTON, SG_CANCEL, 0, 36,17, 10,1, "Cancel" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgMemory.c
--- a/src/gui-sdl/dlgMemory.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgMemory.c Sat Jun 13 21:32:33 2015 +0300
@@ -69,7 +69,7 @@
{ SGCHECKBOX, 0, 0, 2,19, 34,1, "_Load/save state at start-up/exit" },
{ SGBUTTON, SG_DEFAULT, 0, 10,22, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgNewDisk.c
--- a/src/gui-sdl/dlgNewDisk.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgNewDisk.c Sat Jun 13 21:32:33 2015 +0300
@@ -50,7 +50,7 @@
{ SGRADIOBUT, 0, SG_SELECTED, 17,9, 3,1, "_2" },
{ SGBUTTON, SG_DEFAULT, 0, 4,12, 8,1, "_Create" },
{ SGBUTTON, SG_CANCEL, 0, 18,12, 6,1, "_Back" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
#define DEFAULT_DISK_NAME "new_disk.st"
diff -r fb8bfdf825a6 src/gui-sdl/dlgRom.c
--- a/src/gui-sdl/dlgRom.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgRom.c Sat Jun 13 21:32:33 2015 +0300
@@ -38,7 +38,7 @@
{ SGTEXT, 0, 0, 2,16, 46,1, NULL },
{ SGTEXT, 0, 0, 2,19, 25,1, "A reset is needed after changing these options." },
{ SGBUTTON, SG_DEFAULT, 0, 16,21, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/dlgScreen.c
--- a/src/gui-sdl/dlgScreen.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgScreen.c Sat Jun 13 21:32:33 2015 +0300
@@ -73,7 +73,7 @@
{ SGRADIOBUT, SG_EXIT, 0, 18,13, 11,1, "1_6 colors" },
{ SGBUTTON, SG_DEFAULT, 0, 7,16, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
@@ -164,7 +164,7 @@
#else
{ SGBUTTON, SG_DEFAULT, 0, 17,18, 20,1, "Back to main menu" },
#endif
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
/* for record button */
diff -r fb8bfdf825a6 src/gui-sdl/dlgSound.c
--- a/src/gui-sdl/dlgSound.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgSound.c Sat Jun 13 21:32:33 2015 +0300
@@ -70,7 +70,7 @@
{ SGBUTTON, 0,0, 12,20, 16,1, NULL }, /* text set later, see below */
{ SGBUTTON, SG_DEFAULT, 0, 10,23, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
#define RECORD_START "_Record sound"
diff -r fb8bfdf825a6 src/gui-sdl/dlgSystem.c
--- a/src/gui-sdl/dlgSystem.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/dlgSystem.c Sat Jun 13 21:32:33 2015 +0300
@@ -130,7 +130,7 @@
{ SGTEXT, 0, 0, 47,20, 12,1, "" },
#endif
{ SGBUTTON, SG_DEFAULT, 0, 21,23, 20,1, "Back to main menu" },
- { -1, 0, 0, 0,0, 0,0, NULL }
+ { SG_END, 0, 0, 0,0, 0,0, NULL }
};
diff -r fb8bfdf825a6 src/gui-sdl/sdlgui.c
--- a/src/gui-sdl/sdlgui.c Sat Jun 13 21:27:31 2015 +0300
+++ b/src/gui-sdl/sdlgui.c Sat Jun 13 21:32:33 2015 +0300
@@ -24,14 +24,13 @@
#if WITH_SDL2
#define SDL_SRCCOLORKEY SDL_TRUE
#define SDLKey SDL_Keycode
-
#endif
static SDL_Surface *pSdlGuiScrn; /* Pointer to the actual main SDL screen surface */
static SDL_Surface *pSmallFontGfx = NULL; /* The small font graphics */
static SDL_Surface *pBigFontGfx = NULL; /* The big font graphics */
static SDL_Surface *pFontGfx = NULL; /* The actual font graphics */
-static int current_object = 0; /* Current selected object */
+static int current_object = SDLGUI_NOTFOUND;/* Current selected object */
static struct {
Uint32 darkbar, midbar, lightbar;
@@ -764,7 +763,7 @@
{
int i;
- for (i = 0; dlg[i].type != -1; i++)
+ for (i = 0; dlg[i].type != SG_END; i++)
{
SDLGui_DrawObj(dlg, i);
}
@@ -775,16 +774,17 @@
/*-----------------------------------------------------------------------*/
/**
* Search an object at a certain position.
- * Return object index or -1 if it wasn't found.
+ * If found, return its index, otherwise SDLGUI_NOTFOUND.
*/
static int SDLGui_FindObj(const SGOBJ *dlg, int fx, int fy)
{
int len, i;
- int ob = -1;
+ int ob = SDLGUI_NOTFOUND;
int xpos, ypos;
len = 0;
- while (dlg[len].type != -1) len++;
+ while (dlg[len].type != SG_END)
+ len++;
xpos = fx / sdlgui_fontwidth;
ypos = fy / sdlgui_fontheight;
@@ -817,35 +817,37 @@
/*-----------------------------------------------------------------------*/
/**
* Search an object with a special flag (e.g. SG_DEFAULT or SG_CANCEL).
+ * If found, return its index, otherwise SDLGUI_NOTFOUND.
*/
static int SDLGui_SearchFlags(const SGOBJ *dlg, int flag)
{
int i = 0;
- while (dlg[i].type != -1)
+ while (dlg[i].type != SG_END)
{
if (dlg[i].flags & flag)
return i;
i++;
}
- return 0;
+ return SDLGUI_NOTFOUND;
}
/*-----------------------------------------------------------------------*/
/**
* Search an object with a special state (e.g. SG_FOCUSED).
+ * If found, return its index, otherwise SDLGUI_NOTFOUND.
*/
static int SDLGui_SearchState(const SGOBJ *dlg, int state)
{
int i = 0;
- while (dlg[i].type != -1)
+ while (dlg[i].type != SG_END)
{
if (dlg[i].state & state)
return i;
i++;
}
- return 0;
+ return SDLGUI_NOTFOUND;
}
/*-----------------------------------------------------------------------*/
@@ -871,7 +873,7 @@
unsigned int i;
memset(used, 0, sizeof(used));
- for (i = 0; dlg[i].type != -1; i++)
+ for (i = 0; dlg[i].type != SG_END; i++)
{
if (!SDLGui_CanHaveShortcut(dlg[i].type))
continue;
@@ -911,7 +913,8 @@
/*-----------------------------------------------------------------------*/
/**
- * Search a next button to focus and focus it
+ * Search a next button to focus and focus it.
+ * If found, return its index, otherwise current index.
*/
static int SDLGui_FocusNext(SGOBJ *dlg, int i, int inc)
{
@@ -924,13 +927,13 @@
i += inc;
/* wrap */
- if (dlg[i].type == -1)
+ if (dlg[i].type == SG_END)
{
i = 0;
}
else if (i == 0)
{
- while (dlg[i].type != -1)
+ while (dlg[i].type != SG_END)
i++;
i--;
}
@@ -954,12 +957,13 @@
/*-----------------------------------------------------------------------*/
/**
- * Handle button selection, either with mouse or keyboard
+ * Handle button selection, either with mouse or keyboard.
+ * If handled, return its index, otherwise SDLGUI_NOTFOUND.
*/
static int SDLGui_HandleSelection(SGOBJ *dlg, int obj, int oldbutton)
{
SDL_Rect rct;
- int i, retbutton = 0;
+ int i, retbutton = SDLGUI_NOTFOUND;
switch (dlg[obj].type)
{
@@ -1041,33 +1045,36 @@
/*-----------------------------------------------------------------------*/
/**
- * If given shortcut matches item, handle that & return it's code,
- * otherwise return zero.
+ * If object with given shortcut is found, handle that.
+ * If found, return its index, otherwise SDLGUI_NOTFOUND.
*/
static int SDLGui_HandleShortcut(SGOBJ *dlg, int key)
{
int i = 0;
- while (dlg[i].type != -1)
+ while (dlg[i].type != SG_END)
{
if (dlg[i].shortcut == key)
return SDLGui_HandleSelection(dlg, i, i);
i++;
}
- return 0;
+ return SDLGUI_NOTFOUND;
}
/*-----------------------------------------------------------------------*/
/**
- * Show and process a dialog. Returns the button number that has been
- * pressed or SDLGUI_UNKNOWNEVENT if an unsupported event occurred (will be
- * stored in parameter pEventOut).
+ * 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)
{
- int obj=0;
- int oldbutton=0;
- int retbutton=0;
- int i, j, b, value;
+ int oldbutton = SDLGUI_NOTFOUND;
+ int retbutton = SDLGUI_NOTFOUND;
+ int i, j, b, value, obj;
SDLKey key;
int focused;
SDL_Event sdlEvent;
@@ -1112,15 +1119,17 @@
/* focus default button if nothing else is focused */
focused = SDLGui_SearchState(dlg, SG_FOCUSED);
- if (!focused)
+ if (focused == SDLGUI_NOTFOUND)
{
int defocus = SDLGui_SearchFlags(dlg, SG_DEFAULT);
- if (defocus)
+ if (defocus != SDLGUI_NOTFOUND)
{
dlg[focused].state &= ~SG_FOCUSED;
dlg[defocus].state |= SG_FOCUSED;
focused = defocus;
}
+ else
+ defocus = 0;
}
SDLGui_SetShortcuts(dlg);
@@ -1133,25 +1142,23 @@
/* 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 >= 0 && dlg[current_object].type == SGSCROLLBAR) {
+ if (current_object != SDLGUI_NOTFOUND && dlg[current_object].type == SGSCROLLBAR) {
+ obj = current_object;
if (b & SDL_BUTTON(1)) {
- obj = current_object;
dlg[obj].state |= SG_MOUSEDOWN;
- oldbutton = obj;
- retbutton = obj;
}
- else {
- obj = current_object;
- current_object = 0;
+ else
+ {
+ current_object = SDLGUI_NOTFOUND;
dlg[obj].state &= ~SG_MOUSEDOWN;
- retbutton = obj;
- oldbutton = obj;
}
+ oldbutton = obj;
+ retbutton = obj;
}
else {
obj = SDLGui_FindObj(dlg, i, j);
current_object = obj;
- if (obj > 0 && (dlg[obj].flags&SG_TOUCHEXIT) )
+ if (obj != SDLGUI_NOTFOUND && (dlg[obj].flags&SG_TOUCHEXIT) )
{
oldbutton = obj;
if (b & SDL_BUTTON(1))
@@ -1171,7 +1178,7 @@
#endif
/* The main loop */
- while (retbutton == 0 && !bQuitProgram)
+ while (retbutton == SDLGUI_NOTFOUND && !bQuitProgram)
{
if (SDL_WaitEvent(&sdlEvent) == 1) /* Wait for events */
@@ -1191,7 +1198,7 @@
}
/* 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 (obj != SDLGUI_NOTFOUND)
{
if (dlg[obj].type==SGBUTTON)
{
@@ -1224,17 +1231,17 @@
}
/* 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 (obj != SDLGUI_NOTFOUND)
{
retbutton = SDLGui_HandleSelection(dlg, obj, oldbutton);
}
- if (oldbutton > 0 && dlg[oldbutton].type == SGBUTTON)
+ 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 = 0;
+ oldbutton = SDLGUI_NOTFOUND;
}
break;