[WIP] Fix the madness

This commit is contained in:
Lea Anthony
2021-01-12 20:39:19 +11:00
parent 1a7507f524
commit 0f7acd39fc
9 changed files with 1032 additions and 999 deletions
+112
View File
@@ -0,0 +1,112 @@
////
//// Created by Lea Anthony on 6/1/21.
////
//
#include "ffenestri_darwin.h"
#include "common.h"
#include "contextmenus_darwin.h"
#include "menu_darwin.h"
ContextMenu* NewContextMenu(JsonNode* menuData, ContextMenuStore *store) {
ContextMenu* result = malloc(sizeof(ContextMenu));
result->menu = NewMenu(menuData);
result->nsmenu = NULL;
result->menu->menuType = ContextMenuType;
result->menu->parentData = store;
return result;
}
ContextMenu* GetContextMenuByID(ContextMenuStore* store, const char *contextMenuID) {
return (ContextMenu*)hashmap_get(&store->contextMenuStore, (char*)contextMenuID, strlen(contextMenuID));
}
void DeleteContextMenu(ContextMenu* contextMenu) {
// Free Menu
DeleteMenu(contextMenu->menu);
// Free context menu
free(contextMenu);
}
int freeContextMenu(void *const context, struct hashmap_element_s *const e) {
DeleteContextMenu(e->data);
return -1;
}
void ProcessContextMenus(ContextMenuStore* store) {
// Decode the context menus JSON
store->processedContextMenus = json_decode(store->contextMenusAsJSON);
if( store->processedContextMenus == NULL ) {
ABORT("[ProcessContextMenus] Unable to parse Context Menus JSON: %s", store->contextMenusAsJSON);
}
// // Get the context menu items
// JsonNode *contextMenuItems = json_find_member(store->processedContextMenus, "Items");
// if( contextMenuItems == NULL ) {
// ABORT("[ProcessContextMenus] Unable to find Items in processedContextMenus!");
// }
// Iterate context menus
JsonNode *contextMenu;
json_foreach(contextMenu, store->processedContextMenus) {
const char* ID = getJSONString(contextMenu, "ID");
if ( ID == NULL ) {
ABORT("Unable to read ID of contextMenu\n");
}
JsonNode* processedMenu = json_find_member(contextMenu, "ProcessedMenu");
if ( processedMenu == NULL ) {
ABORT("Unable to read ProcessedMenu of contextMenu\n");
}
// Create a new context menu instance
ContextMenu *thisContextMenu = NewContextMenu(processedMenu, store);
// Store the item in the context menu map
hashmap_put(&store->contextMenuStore, (char*)ID, strlen(ID), thisContextMenu);
}
}
void ShowContextMenu(ContextMenuStore* store, id mainWindow, const char *contextMenuID, const char *contextMenuData) {
// If no context menu ID was given, abort
if( contextMenuID == NULL ) {
return;
}
ContextMenu* contextMenu = GetContextMenuByID(store, contextMenuID);
// We don't need the ID now
MEMFREE(contextMenuID);
if( contextMenu == NULL ) {
// Free context menu data
if( contextMenuData != NULL ) {}
MEMFREE(contextMenuData);
return;
}
// We need to store the context menu data. Free existing data if we have it
// and set to the new value.
FREE_AND_SET(store->contextMenuData, contextMenuData);
// Grab the content view and show the menu
id contentView = msg(mainWindow, s("contentView"));
// Get the triggering event
id menuEvent = msg(mainWindow, s("currentEvent"));
if( contextMenu->nsmenu == NULL ) {
// GetMenu creates the NSMenu
contextMenu->nsmenu = GetMenu(contextMenu->menu);
}
// Show popup
msg(c("NSMenu"), s("popUpContextMenu:withEvent:forView:"), contextMenu->nsmenu, menuEvent, contentView);
}
+7 -231
View File
@@ -5,7 +5,7 @@
#ifndef CONTEXTMENU_DARWIN_H
#define CONTEXTMENU_DARWIN_H
#include "common.h"
#include "json.h"
#include "menu_darwin.h"
#include "contextmenustore_darwin.h"
@@ -16,237 +16,13 @@ typedef struct {
} ContextMenu;
ContextMenu* NewContextMenu(JsonNode* menuData, ContextMenuStore *store) {
ContextMenu* result = malloc(sizeof(ContextMenu));
result->menu = NewMenu(menuData);
result->nsmenu = NULL;
result->menu->menuType = ContextMenuType;
result->menu->parentData = store;
return result;
}
ContextMenu* NewContextMenu(JsonNode* menuData, ContextMenuStore* store);
ContextMenu* GetContextMenuByID( ContextMenuStore* store, const char *contextMenuID);
void DeleteContextMenu(ContextMenu* contextMenu);
int freeContextMenu(void *const context, struct hashmap_element_s *const e);
void ProcessContextMenus( ContextMenuStore* store);
ContextMenuStore* NewContextMenuStore(const char* contextMenusAsJSON) {
ContextMenuStore* result = malloc(sizeof(ContextMenuStore));
// Init members
result->contextMenusAsJSON = contextMenusAsJSON;
result->processedContextMenus = NULL;
result->contextMenuData = NULL;
// Allocate Context Menu Store
if( 0 != hashmap_create((const unsigned)4, &result->contextMenuStore)) {
ABORT("[NewContextMenus] Not enough memory to allocate contextMenuStore!");
}
return result;
}
ContextMenu* GetContextMenuByID(ContextMenuStore* store, const char *contextMenuID) {
return (ContextMenu*)hashmap_get(&store->contextMenuStore, (char*)contextMenuID, strlen(contextMenuID));
}
void DeleteContextMenu(ContextMenu* contextMenu) {
// Free Menu
DeleteMenu(contextMenu->menu);
// Free context menu
free(contextMenu);
}
int freeContextMenu(void *const context, struct hashmap_element_s *const e) {
DeleteContextMenu(e->data);
return -1;
}
void DeleteContextMenuStore(ContextMenuStore* store) {
// Guard against NULLs
if( store == NULL ) {
return;
}
// Delete context menus
if( hashmap_num_entries(&store->contextMenuStore) > 0 ) {
if (0 != hashmap_iterate_pairs(&store->contextMenuStore, freeContextMenu, NULL)) {
ABORT("[DeleteContextMenuStore] Failed to release contextMenuStore entries!");
}
}
// Free context menu hashmap
hashmap_destroy(&store->contextMenuStore);
// Destroy processed Context Menus
if( store->processedContextMenus != NULL) {
json_delete(store->processedContextMenus);
store->processedContextMenus = NULL;
}
// Delete any context menu data we may have stored
if( store->contextMenuData != NULL ) {
MEMFREE(store->contextMenuData);
}
}
void ProcessContextMenus(ContextMenuStore* store) {
// Decode the context menus JSON
store->processedContextMenus = json_decode(store->contextMenusAsJSON);
if( store->processedContextMenus == NULL ) {
ABORT("[ProcessContextMenus] Unable to parse Context Menus JSON: %s", store->contextMenusAsJSON);
}
// // Get the context menu items
// JsonNode *contextMenuItems = json_find_member(store->processedContextMenus, "Items");
// if( contextMenuItems == NULL ) {
// ABORT("[ProcessContextMenus] Unable to find Items in processedContextMenus!");
// }
// Iterate context menus
JsonNode *contextMenu;
json_foreach(contextMenu, store->processedContextMenus) {
const char* ID = getJSONString(contextMenu, "ID");
if ( ID == NULL ) {
ABORT("Unable to read ID of contextMenu\n");
}
JsonNode* processedMenu = json_find_member(contextMenu, "ProcessedMenu");
if ( processedMenu == NULL ) {
ABORT("Unable to read ProcessedMenu of contextMenu\n");
}
// Create a new context menu instance
ContextMenu *thisContextMenu = NewContextMenu(processedMenu, store);
// Store the item in the context menu map
hashmap_put(&store->contextMenuStore, (char*)ID, strlen(ID), thisContextMenu);
}
}
//
//
//bool ContextMenuExists(ContextMenus *contextMenus, const char* contextMenuID) {
// return hashmap_get(&contextMenus->contextMenuStore, contextMenuID, strlen(contextMenuID)) != NULL;
//}
//
//bool AddContextMenu(ContextMenu* contextMenu) {
//
// // Check if we already have this
// if( ContextMenuExists(contextMenu->ID) ) {
// return false;
// }
//
// // Store the context menu
// if (0 != hashmap_put(&contextMenus->contextMenuStore, contextMenu->ID, strlen(contextMenu->ID), contextMenu)) {
// ABORT("Unable to add context menu with ID '%s'", contextMenu->ID);
// }
//
// return true;
//}
//
//ContextMenus* NewContextMenus(const char* contextMenusAsJSON) {
//
// ContextMenus* result = malloc(sizeof(ContextMenus));
//
// // Allocate Context Menu Store
// if( 0 != hashmap_create((const unsigned)4, &result->contextMenuStore)) {
// ABORT("[NewContextMenus] Not enough memory to allocate contextMenuStore!");
// }
//
// //
//
// return result;
//}
//
//void ProcessContextMenus() {
// // Parse the context menu json
// processedContextMenus = json_decode(contextMenusAsJSON);
// if( processedContextMenus == NULL ) {
// // Parse error!
// ABORT("Unable to parse Context Menus JSON: %s", contextMenusAsJSON);
// }
//
// JsonNode *contextMenuItems = json_find_member(processedContextMenus, "Items");
// if( contextMenuItems == NULL ) {
// // Parse error!
// ABORT("Unable to find Items in Context menus");
// }
// // Iterate context menus
// JsonNode *contextMenu;
// json_foreach(contextMenu, contextMenuItems) {
// Menu *contextMenu = NewMenu()
//
// // Store the item in the context menu map
// hashmap_put(&contextMenuMap, (char*)contextMenu->key, strlen(contextMenu->key), menu);
// }
//
//}
//
//ContextMenu* NewContextMenu() {
// ContextMenu* result = malloc(sizeof(ContextMenu));
//
// result->menu = NewMenu(contextMenuAsJSON);
//
// return result;
//}
//
//
//void InitContextMenuStore() {
//
//}
//
//
//void DeleteContextMenuStore() {
// // Free radio group members
// if( hashmap_num_entries(&contextMenuStore) > 0 ) {
// if (0 != hashmap_iterate_pairs(&contextMenuStore, freeContextMenu, NULL)) {
// ABORT("[DeleteContextMenuStore] Failed to release contextMenuStore entries!");
// }
// }
//}
//
void ShowContextMenu(ContextMenuStore* store, id mainWindow, const char *contextMenuID, const char *contextMenuData) {
// If no context menu ID was given, abort
if( contextMenuID == NULL ) {
return;
}
ContextMenu* contextMenu = GetContextMenuByID(store, contextMenuID);
// We don't need the ID now
MEMFREE(contextMenuID);
if( contextMenu == NULL ) {
// Free context menu data
if( contextMenuData != NULL ) {}
MEMFREE(contextMenuData);
return;
}
// We need to store the context menu data. Free existing data if we have it
// and set to the new value.
FREE_AND_SET(store->contextMenuData, contextMenuData);
// Grab the content view and show the menu
id contentView = msg(mainWindow, s("contentView"));
// Get the triggering event
id menuEvent = msg(mainWindow, s("currentEvent"));
if( contextMenu->nsmenu == NULL ) {
// GetMenu creates the NSMenu
contextMenu->nsmenu = GetMenu(contextMenu->menu);
}
// Show popup
msg(c("NSMenu"), s("popUpContextMenu:withEvent:forView:"), contextMenu->nsmenu, menuEvent, contentView);
}
void ShowContextMenu(ContextMenuStore* store, id mainWindow, const char *contextMenuID, const char *contextMenuData);
#endif //CONTEXTMENU_DARWIN_H
@@ -0,0 +1,50 @@
#include "contextmenus_darwin.h"
#include "contextmenustore_darwin.h"
ContextMenuStore* NewContextMenuStore(const char* contextMenusAsJSON) {
ContextMenuStore* result = malloc(sizeof(ContextMenuStore));
// Init members
result->contextMenusAsJSON = contextMenusAsJSON;
result->processedContextMenus = NULL;
result->contextMenuData = NULL;
// Allocate Context Menu Store
if( 0 != hashmap_create((const unsigned)4, &result->contextMenuStore)) {
ABORT("[NewContextMenus] Not enough memory to allocate contextMenuStore!");
}
return result;
}
void DeleteContextMenuStore(ContextMenuStore* store) {
// Guard against NULLs
if( store == NULL ) {
return;
}
// Delete context menus
if( hashmap_num_entries(&store->contextMenuStore) > 0 ) {
if (0 != hashmap_iterate_pairs(&store->contextMenuStore, freeContextMenu, NULL)) {
ABORT("[DeleteContextMenuStore] Failed to release contextMenuStore entries!");
}
}
// Free context menu hashmap
hashmap_destroy(&store->contextMenuStore);
// Destroy processed Context Menus
if( store->processedContextMenus != NULL) {
json_delete(store->processedContextMenus);
store->processedContextMenus = NULL;
}
// Delete any context menu data we may have stored
if( store->contextMenuData != NULL ) {
MEMFREE(store->contextMenuData);
}
}
@@ -5,6 +5,8 @@
#ifndef CONTEXTMENUSTORE_DARWIN_H
#define CONTEXTMENUSTORE_DARWIN_H
#include "common.h"
typedef struct {
// This is our context menu store which keeps track
// of all instances of ContextMenus
@@ -21,4 +23,7 @@ typedef struct {
} ContextMenuStore;
#endif //ASSETS_C_CONTEXTMENUSTORE_DARWIN_H
ContextMenuStore* NewContextMenuStore(const char* contextMenusAsJSON);
void DeleteContextMenuStore(ContextMenuStore* store);
#endif //CONTEXTMENUSTORE_DARWIN_H
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+18
View File
@@ -0,0 +1,18 @@
//
// Created by Lea Anthony on 12/1/21.
//
#include "common.h"
#include "traymenu_darwin.h"
TrayMenu* NewTrayMenu(const char* menuJSON) {
TrayMenu* result = malloc(sizeof(TrayMenu));
return result;
}
void DeleteTrayMenu(TrayMenu* trayMenu) {
// Free the tray menu memory
MEMFREE(trayMenu);
}
+3
View File
@@ -17,4 +17,7 @@ typedef struct {
} TrayMenu;
TrayMenu* NewTrayMenu(const char *trayJSON);
void DeleteTrayMenu(TrayMenu* trayMenu);
#endif //TRAYMENU_DARWIN_H
@@ -4,6 +4,7 @@
#include "common.h"
#include "traymenustore_darwin.h"
#include "traymenu_darwin.h"
TrayMenuStore* NewTrayMenuStore() {
@@ -18,11 +19,28 @@ TrayMenuStore* NewTrayMenuStore() {
}
void AddTrayMenuToStore(TrayMenuStore* store, const char* menuJSON) {
TrayMenu* newMenu = NewTrayMenu(menuJSON);
const char *ID = "TEST";
hashmap_put(&store->trayMenuMap, ID, strlen(ID), newMenu);
}
int freeTrayMenu(void *const context, struct hashmap_element_s *const e) {
DeleteTrayMenu(e->data);
return -1;
}
void DeleteTrayMenuStore(TrayMenuStore *trayMenuStore) {
// Delete context menus
if( hashmap_num_entries(&trayMenuStore->trayMenuMap) > 0 ) {
if (0 != hashmap_iterate_pairs(&trayMenuStore->trayMenuMap, freeTrayMenu, NULL)) {
ABORT("[DeleteContextMenuStore] Failed to release contextMenuStore entries!");
}
}
// Destroy tray menu map
hashmap_destroy(&trayMenuStore->trayMenuMap);
}