joeydev/src/utils.c

194 lines
4.5 KiB
C

/*
* JoeyDev
* Copyright (C) 2018-2023 Scott Duensing <scott@kangaroopunch.com>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include "utils.h"
typedef struct WindowListS {
GtkWidget *key;
WindowDataT *value;
} WindowListT;
static WindowListT *_windowList = NULL;
char *utilCreateString(char *format, ...) {
va_list args;
char *string;
va_start(args, format);
string = utilCreateStringVArgs(format, args);
va_end(args);
return string;
}
__attribute__((__format__(__printf__, 1, 0)))
char *utilCreateStringVArgs(char *format, va_list args) {
va_list argsCopy;
int32_t size = 0;
char *buffer = NULL;
va_copy(argsCopy, args);
size = vsnprintf(NULL, 0, format, argsCopy) + 1;
va_end(argsCopy);
buffer = calloc(1, (size_t)size);
if (buffer) {
vsnprintf(buffer, (size_t)size, format, args);
}
return buffer;
}
void utilEnsureBufferSize(unsigned char **buffer, int *length, int wanted) {
unsigned char *temp = NULL;
if (*length < wanted) {
*length = *length + 1024;
temp = realloc(*buffer, *length);
if (temp == NULL) {
//***TODO*** Something bad happened.
} else {
*buffer = temp;
}
}
}
gboolean utilFileExists(char *filename) {
FILE *f = fopen(filename, "rb");
if (f) {
fclose(f);
return TRUE;
}
return FALSE;
}
WindowDataT *utilGetWindowData(GtkWidget *window) {
return hmget(_windowList, window);
}
GtkWidget *utilGetWindowFromMemory(char *resource, char *name, gpointer userData) {
GtkBuilder *gtkBuilder;
GtkWidget *window;
gtkBuilder = gtk_builder_new();
if (gtk_builder_add_from_resource(gtkBuilder, resource, NULL) == 0) {
printf("Failed to build UI '%s'!\n", name);
exit(1);
}
window = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, name));
gtk_builder_connect_signals(gtkBuilder, userData);
g_object_unref(G_OBJECT(gtkBuilder));
gtk_widget_show(window);
return window;
}
gboolean utilGetWidgetsFromMemory(char *resource, char *name[], GtkWidget **widgets[], gpointer userData) {
GtkBuilder *gtkBuilder;
int x;
gtkBuilder = gtk_builder_new();
if (gtk_builder_add_from_resource(gtkBuilder, resource, NULL) == 0) {
printf("Failed to build UI '%s'!\n", name[0]);
exit(1);
}
x = 0;
while (name[x] != NULL) {
*widgets[x] = GTK_WIDGET(gtk_builder_get_object(gtkBuilder, name[x]));
x++;
}
gtk_builder_connect_signals(gtkBuilder, userData);
g_object_unref(G_OBJECT(gtkBuilder));
return TRUE;
}
gboolean utilQuestionDialog(GtkWidget *parent, char *title, char *question) {
GtkWidget *dialog;
int response;
dialog = gtk_message_dialog_new(
GTK_WINDOW(parent),
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_OK_CANCEL,
"%s", question);
gtk_window_set_title(GTK_WINDOW(dialog), title);
response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return (response == GTK_RESPONSE_OK) ? TRUE : FALSE;
}
void utilWindowRegister(gpointer windowData) {
WindowDataT *w = (WindowDataT *)windowData;
hmput(_windowList, w->window, windowData);
}
int utilWindowsCloseAll(void) {
WindowDataT *w;
while (hmlen(_windowList) > 0) {
w = (WindowDataT *)_windowList[0].value;
if (w->closeWindow(w->window, w) == TRUE) {
// User canceled closing.
break;
}
// Mark it clean and close it ourselves.
w->isDirty = FALSE;
gtk_window_close(GTK_WINDOW(w->window));
// Unregister it.
utilWindowUnRegister(w);
}
return utilWindowsOpen();
}
int utilWindowsOpen(void) {
return hmlen(_windowList);
}
gboolean utilWindowUnRegister(gpointer windowData) {
int result;
WindowDataT *w = (WindowDataT *)windowData;
result = hmdel(_windowList, w->window);
return (result == 1) ? TRUE : FALSE;
}