Skip to content

Commit

Permalink
Propose graphics object store
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-niklaus committed Mar 30, 2024
1 parent 76b278c commit bed8d6d
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 125 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ set(sources
src/drawing.h
src/coordlist_ops.c
src/coordlist_ops.h
src/objects.c
src/objects.h
src/main.c
src/main.h
src/input.c
Expand Down
56 changes: 55 additions & 1 deletion src/callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ gboolean on_expose (GtkWidget *widget,
{
GromitData *data = (GromitData *) user_data;

if(data->debug || 1)
if(data->debug)
g_printerr("DEBUG: got draw event\n");

cairo_save (cr);
Expand Down Expand Up @@ -930,6 +930,60 @@ void on_intro(GtkMenuItem *menuitem,
gtk_widget_show_all (assistant);
}

void on_edit_config(GtkMenuItem *menuitem,
gpointer user_data)
{
/*
Check if user config does not exist or is empty.
If so, copy system config to user config.
*/
gchar *user_config_path = g_strjoin (G_DIR_SEPARATOR_S, g_get_user_config_dir(), "gromit-mpx.cfg", NULL);
GFile *user_config_file = g_file_new_for_path(user_config_path);

guint64 user_config_size = 0;
GFileInfo *user_config_info = g_file_query_info(user_config_file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, NULL);
if (user_config_info != NULL) {
user_config_size = g_file_info_get_size(user_config_info);
g_object_unref(user_config_info);
}

if (!g_file_query_exists(user_config_file, NULL) || user_config_size == 0) {
g_print("User config does not exist or is empty, copying system config\n");

gchar *system_config_path = g_strjoin (G_DIR_SEPARATOR_S, SYSCONFDIR, "gromit-mpx", "gromit-mpx.cfg", NULL);
GFile *system_config_file = g_file_new_for_path(system_config_path);

GError *error = NULL;
gboolean result = g_file_copy(system_config_file, user_config_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
if (!result) {
g_printerr("Error copying system config to user config: %s\n", error->message);
g_error_free(error);
}

g_object_unref(system_config_file);
g_free(system_config_path);
}


/*
Open user config for editing.
*/
gchar *user_config_uri = g_strjoin (G_DIR_SEPARATOR_S, "file://", user_config_path, NULL);

gtk_show_uri_on_window (NULL,
user_config_uri,
GDK_CURRENT_TIME,
NULL);

/*
Clean up
*/
g_object_unref(user_config_file);
g_free(user_config_path);
g_free(user_config_uri);
}


void on_issues(GtkMenuItem *menuitem,
gpointer user_data)
{
Expand Down
104 changes: 104 additions & 0 deletions src/coordlist_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,110 @@ static GList *build_section_list(GList *const coords,
return section_list;
}

void coord_list_prepend (GromitData *data,
GdkDevice* dev,
gint x,
gint y,
gint width)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GromitStrokeCoordinate *point;

point = g_malloc (sizeof (GromitStrokeCoordinate));
point->x = x;
point->y = y;
point->width = width;

devdata->coordlist = g_list_prepend (devdata->coordlist, point);
}


void coord_list_free (GromitData *data,
GdkDevice* dev)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GList *ptr;
ptr = devdata->coordlist;

while (ptr)
{
g_free (ptr->data);
ptr = ptr->next;
}

g_list_free (devdata->coordlist);

devdata->coordlist = NULL;
}

/*
* for double-ended arrows, two separate calls are required
*/

gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowType arrow_end,
gint *x0,
gint *y0,
gint *ret_width,
gfloat *ret_direction)
{
gint r2, dist;
gboolean success = FALSE;
GromitStrokeCoordinate *cur_point, *valid_point;
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);
GList *ptr = devdata->coordlist;
gfloat width;

valid_point = NULL;

if (ptr)
{
if (arrow_end == GROMIT_ARROW_START)
ptr = g_list_last(ptr);
cur_point = ptr->data;
*x0 = cur_point->x;
*y0 = cur_point->y;
r2 = search_radius * search_radius;
dist = 0;

while (ptr && dist < r2)
{
if (arrow_end == GROMIT_ARROW_END)
ptr = ptr->next;
else
ptr = ptr->prev;
if (ptr)
{
cur_point = ptr->data;
dist = (cur_point->x - *x0) * (cur_point->x - *x0) +
(cur_point->y - *y0) * (cur_point->y - *y0);
width = cur_point->width * devdata->cur_context->arrowsize;
if (width * 2 <= dist &&
(!valid_point || valid_point->width < cur_point->width))
valid_point = cur_point;
}
}

if (valid_point)
{
*ret_width = MAX (valid_point->width * devdata->cur_context->arrowsize,
2);
*ret_direction = atan2 (*y0 - valid_point->y, *x0 - valid_point->x);
success = TRUE;
}
}

return success;
}


// ----------------------- orthogonalize path ------------------------

void orthogonalize(GList *const coords,
Expand Down
104 changes: 0 additions & 104 deletions src/drawing.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,107 +105,3 @@ void draw_arrow (GromitData *data,
data->painted = 1;
}


void coord_list_prepend (GromitData *data,
GdkDevice* dev,
gint x,
gint y,
gint width)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GromitStrokeCoordinate *point;

point = g_malloc (sizeof (GromitStrokeCoordinate));
point->x = x;
point->y = y;
point->width = width;

devdata->coordlist = g_list_prepend (devdata->coordlist, point);
}


void coord_list_free (GromitData *data,
GdkDevice* dev)
{
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);

GList *ptr;
ptr = devdata->coordlist;

while (ptr)
{
g_free (ptr->data);
ptr = ptr->next;
}

g_list_free (devdata->coordlist);

devdata->coordlist = NULL;
}

/*
* for double-ended arrows, two separate calls are required
*/

gboolean coord_list_get_arrow_param (GromitData *data,
GdkDevice *dev,
gint search_radius,
GromitArrowType arrow_end,
gint *x0,
gint *y0,
gint *ret_width,
gfloat *ret_direction)
{
gint r2, dist;
gboolean success = FALSE;
GromitStrokeCoordinate *cur_point, *valid_point;
/* get the data for this device */
GromitDeviceData *devdata = g_hash_table_lookup(data->devdatatable, dev);
GList *ptr = devdata->coordlist;
gfloat width;

valid_point = NULL;

if (ptr)
{
if (arrow_end == GROMIT_ARROW_START)
ptr = g_list_last(ptr);
cur_point = ptr->data;
*x0 = cur_point->x;
*y0 = cur_point->y;
r2 = search_radius * search_radius;
dist = 0;

while (ptr && dist < r2)
{
if (arrow_end == GROMIT_ARROW_END)
ptr = ptr->next;
else
ptr = ptr->prev;
if (ptr)
{
cur_point = ptr->data;
dist = (cur_point->x - *x0) * (cur_point->x - *x0) +
(cur_point->y - *y0) * (cur_point->y - *y0);
width = cur_point->width * devdata->cur_context->arrowsize;
if (width * 2 <= dist &&
(!valid_point || valid_point->width < cur_point->width))
valid_point = cur_point;
}
}

if (valid_point)
{
*ret_width = MAX (valid_point->width * devdata->cur_context->arrowsize,
2);
*ret_direction = atan2 (*y0 - valid_point->y, *x0 - valid_point->x);
success = TRUE;
}
}

return success;
}

15 changes: 7 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,7 @@ void snap_undo_state (GromitData *data)
data->redo_depth = 0;
}

void clear_surface (cairo_surface_t *dst)
{
cairo_t *cr = cairo_create(dst);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_paint (cr);
cairo_destroy(cr);
}


void copy_surface (cairo_surface_t *dst, cairo_surface_t *src)
{
Expand Down Expand Up @@ -904,6 +897,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv)

GtkWidget* sep1_item = gtk_separator_menu_item_new();
GtkWidget* intro_item = gtk_menu_item_new_with_mnemonic(_("_Introduction"));
GtkWidget* edit_config_item = gtk_menu_item_new_with_mnemonic(_("_Edit Config"));
GtkWidget* issues_item = gtk_menu_item_new_with_mnemonic(_("_Report Bug / Request Feature"));
GtkWidget* support_item = gtk_menu_item_new_with_mnemonic(_("_Support Gromit-MPX"));
GtkWidget* about_item = gtk_menu_item_new_with_mnemonic(_("_About"));
Expand All @@ -926,6 +920,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv)

gtk_menu_shell_append (GTK_MENU_SHELL (menu), sep1_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), intro_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), edit_config_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), issues_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), support_item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), about_item);
Expand Down Expand Up @@ -988,6 +983,9 @@ void setup_main_app (GromitData *data, int argc, char ** argv)
g_signal_connect(G_OBJECT (intro_item), "activate",
G_CALLBACK (on_intro),
data);
g_signal_connect(G_OBJECT (edit_config_item), "activate",
G_CALLBACK (on_edit_config),
data);
g_signal_connect(G_OBJECT (issues_item), "activate",
G_CALLBACK (on_issues),
data);
Expand All @@ -1012,6 +1010,7 @@ void setup_main_app (GromitData *data, int argc, char ** argv)

gtk_widget_show (sep1_item);
gtk_widget_show (intro_item);
gtk_widget_show (edit_config_item);
gtk_widget_show (issues_item);
gtk_widget_show (support_item);
gtk_widget_show (about_item);
Expand Down
11 changes: 1 addition & 10 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#define GROMIT_MPX_MAIN_H

#include "build-config.h"
#include "cairo.h"

#include <glib.h>
#include <glib/gi18n.h>
Expand Down Expand Up @@ -143,8 +142,6 @@ typedef struct
GHashTable *tool_config;

cairo_surface_t *backbuffer;
cairo_surface_t *cache_buffer;
guint32 cache_id;
/* Auxiliary backbuffer for tools like LINE or RECT */
cairo_surface_t *aux_backbuffer;

Expand Down Expand Up @@ -174,10 +171,6 @@ typedef struct

gboolean show_intro_on_startup;


GList *gfx_store;


} GromitData;


Expand All @@ -189,9 +182,7 @@ void parse_print_help (gpointer key, gpointer value, gpointer user_data);

void select_tool (GromitData *data, GdkDevice *device, GdkDevice *slave_device, guint state);

void copy_surface(cairo_surface_t *dst, cairo_surface_t *src);
void clear_surface (cairo_surface_t *dst);

void copy_surface (cairo_surface_t *dst, cairo_surface_t *src);
void snap_undo_state(GromitData *data);
void undo_drawing (GromitData *data);
void redo_drawing (GromitData *data);
Expand Down

0 comments on commit bed8d6d

Please sign in to comment.