Skip to content
Snippets Groups Projects
Commit ae38ece7 authored by Younes Oubkis's avatar Younes Oubkis
Browse files

Cleanup Unfinished

parent 41147f0b
Branches
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"typeindex": "c", "typeindex": "c",
"typeinfo": "c", "typeinfo": "c",
"gtk.h": "c", "gtk.h": "c",
"stdarg.h": "c" "stdarg.h": "c",
"stdlib.h": "c"
} }
} }
\ No newline at end of file
No preview for this file type
...@@ -2,7 +2,10 @@ CFLAGS := -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/ ...@@ -2,7 +2,10 @@ CFLAGS := -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/
LIBS := -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 LIBS := -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0
LINKS := $(wildcard lib/*.c) LINKS := $(wildcard lib/*.c)
LOCAL := $(wildcard *.c)
LOCAL_FILTERED := $(filter-out app.c,$(LOCAL))
app: app.c lib/shapes.c lib/ui.c lib/lib.h app: app.c lib/shapes.c lib/ui.c lib/lib.h
$(shell unset GTK_PATH) $(shell unset GTK_PATH)
echo $(LINKS) are linked to executable echo $(LINKS) are linked to executable
gcc $(CFLAGS) app.c -o ../bin/app $(LIBS) $(LINKS) gcc $(CFLAGS) app.c -o ../bin/app $(LIBS) $(LINKS) $(LOCAL_FILTERED) -lm
\ No newline at end of file \ No newline at end of file
#include <stdio.h> #include <stdio.h>
#include <gtk-3.0/gtk/gtk.h> #include <gtk-3.0/gtk/gtk.h>
#include "lib/lib.h" #include <lib/lib.h>
#include "app.h"
#define set_source_color gdk_cairo_set_source_rgba t_custom_toolbar *toolbar;
#define MAX_SHAPES 10
t_shape shapes[MAX_SHAPES + 1];
GdkRGBA red = {1.0, 0.0, 0.0, 0.8};
GdkRGBA yellow = {1.0, 1.0, 0.0, 0.6};
GdkRGBA blue = {0.0, 0.5, 1.0, 0.4};
int width = 800; int width = 800;
int height = 600; int height = 600;
gboolean drawing_area_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
void handle_freehand_click(float x, float y)
{ {
if (!event->button == GDK_BUTTON_PRIMARY) // Left Click
return FALSE;
double clickX = event->x;
double clickY = event->y;
g_print("X%f Y%f clicked\n", clickX, clickY);
} }
gboolean on_button_clicked(GtkWidget *button, gpointer user_data) gboolean on_button_clicked(GtkWidget *button, gpointer user_data)
{ {
g_print("Button clicked!\n"); g_print("Button clicked!\n");
return TRUE;
} }
gboolean on_shape_choice(GtkWidget *button, gpointer user_data) gboolean on_shape_choice(GtkWidget *button, gpointer user_data)
{ {
current_input = CREATE_SHAPE;
int d = (int)user_data;
shape_type = d;
g_print("Data: %d\n", d);
return TRUE;
} }
gboolean draw(GtkWidget *widget, cairo_t *c, gpointer data) gboolean on_select_clicked(GtkWidget *button, gpointer user_data)
{
set_source_color(c, &red);
cairo_rectangle(c, 10, 10, 60, 40);
cairo_fill(c);
for (t_shape *s = shapes; s; s++)
{
if (!s->flag)
continue;
if (s->flag == -1)
break;
t_vertex *v = s->vertices;
for (int i = 0; i < s->vertexCount; i++, v++)
{ {
cairo_move_to(c, v->X, v->Y); current_input = SELECTION;
} }
cairo_fill(c);
cairo_close_path(c);
}
/*set_source_color(c, &yellow);
cairo_arc(c, 65, 50, 30, 0, 2 * G_PI);
cairo_fill(c);
set_source_color(c, &blue); }
cairo_move_to(c, 10, 70);
cairo_line_to(c, 70, 70);
cairo_line_to(c, 40, 18);
cairo_fill(c);*/
return FALSE; /* TRUE to stop other handlers from being invoked for the event.
FALSE to propagate the event further. */
}
static void static void
toolbar_auswerten(GtkWidget *button, gpointer data) toolbar_auswerten(GtkWidget *button, gpointer data)
...@@ -101,7 +69,7 @@ int create_window_layout(int argc, char **argv) ...@@ -101,7 +69,7 @@ int create_window_layout(int argc, char **argv)
} }
gtk_init(&argc, &argv); gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello"); gtk_window_set_title(GTK_WINDOW(window), "Hello");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
...@@ -110,24 +78,25 @@ int create_window_layout(int argc, char **argv) ...@@ -110,24 +78,25 @@ int create_window_layout(int argc, char **argv)
GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5); GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
GtkWidget *button = gtk_button_new_with_label("Quit"); GtkWidget *button = gtk_button_new_with_label("Quit");
g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *drawing_area = gtk_drawing_area_new(); drawing_area = gtk_drawing_area_new();
gtk_widget_add_events(drawing_area, GDK_BUTTON_PRESS_MASK); gtk_widget_add_events(drawing_area, GDK_BUTTON_PRESS_MASK);
g_signal_connect(drawing_area, "draw", G_CALLBACK(draw), NULL); g_signal_connect(drawing_area, "draw", G_CALLBACK(draw), NULL);
g_signal_connect(GTK_WIDGET(drawing_area), "button-press-event", G_CALLBACK(drawing_area_click), NULL); g_signal_connect(GTK_WIDGET(drawing_area), "button-press-event", G_CALLBACK(drawing_area_click), NULL);
gtk_widget_set_size_request(drawing_area, width, height); gtk_widget_set_size_request(drawing_area, width, height);
t_custom_toolbar *toolbar = create_toolbar(); toolbar = create_toolbar(4);
GtkToolItem *triangle_btn = create_and_bind_toolbutton(toolbar, "Triangle"); GtkToolItem *triangle_btn = create_and_bind_toolbutton(toolbar, "Triangle");
GtkToolItem *rectangle_btn = create_and_bind_toolbutton(toolbar, "Rectangle"); GtkToolItem *rectangle_btn = create_and_bind_toolbutton(toolbar, "Rectangle");
GtkToolItem *circle_btn = create_and_bind_toolbutton(toolbar, "Circle"); GtkToolItem *circle_btn = create_and_bind_toolbutton(toolbar, "Circle");
GtkToolItem *clear_btn = create_and_bind_toolbutton(toolbar, "Clear");
bind_click_signal(triangle_btn, on_shape_choice, TRIANGLE); bind_click_signal(triangle_btn, on_shape_choice, TRIANGLE);
bind_click_signal(rectangle_btn, on_shape_choice, RECTANGLE); bind_click_signal(rectangle_btn, on_shape_choice, RECTANGLE);
bind_click_signal(circle_btn, on_shape_choice, CIRCLE); bind_click_signal(circle_btn, on_shape_choice, CIRCLE);
bind_click_signal(clear_btn, on_clear, CIRCLE);
gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(drawing_area)); gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(drawing_area));
gtk_container_add(GTK_CONTAINER(vbox), button); gtk_container_add(GTK_CONTAINER(vbox), button);
...@@ -140,6 +109,7 @@ int create_window_layout(int argc, char **argv) ...@@ -140,6 +109,7 @@ int create_window_layout(int argc, char **argv)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
current_input = SELECTION;
for (int i = 0; i < MAX_SHAPES; i++) for (int i = 0; i < MAX_SHAPES; i++)
{ {
shapes[i] = NULL_SHAPE(); shapes[i] = NULL_SHAPE();
......
#include <lib/lib.h>
#ifndef APP
#define APP
#include <gtk-3.0/gtk/gtk.h>
#define MAX_SHAPES 10
#define set_source_color gdk_cairo_set_source_rgba
GdkRGBA red = {1.0, 0.0, 0.0, 0.8};
GdkRGBA yellow = {1.0, 1.0, 0.0, 0.6};
GdkRGBA blue = {0.0, 0.5, 1.0, 0.4};
GdkRGBA black = {0.0, 0.0, 0.0, 1.0};
GtkWidget *window;
GtkWidget *drawing_area;
gboolean on_clear(GtkWidget *button, gpointer user_data);
t_shape shapes[MAX_SHAPES + 1];
gboolean drawing_area_click(GtkWidget *widget, GdkEventButton *event, gpointer data);
void add_shape(t_shape new_shape);
gboolean draw(GtkWidget *widget, cairo_t *c, gpointer data);
#endif
\ No newline at end of file
#include "./lib/lib.h"
#include "app.h"
gboolean drawing_area_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
if (!event->button == GDK_BUTTON_PRIMARY) // Left Click
return FALSE;
double clickX = event->x;
double clickY = event->y;
if (!current_input == CREATE_SHAPE && current_input != FREEHAND)
return TRUE;
if (current_input == CREATE_SHAPE)
handle_shape_click(clickX, clickY);
else
handle_freehand_click(clickX, clickY);
g_print("X%f Y%f clicked\n", clickX, clickY);
}
void handle_shape_click(float x, float y)
{
switch (shape_type)
{
case CIRCLE:
{
static t_vertex start;
static t_vertex end;
if (click_count == 0)
{
start = point(x, y);
click_positions[click_count++] = start;
return;
}
if (click_count == 1)
{
end = point(x, y);
t_shape new_circle = create_circle(start, end);
add_shape(new_circle);
click_count = 0;
return;
}
}
case TRIANGLE:
{
static t_vertex p[3];
if (click_count < 2)
{
p[click_count] = point(x, y);
click_count++;
}
else
{
t_shape new_triangle = create_triangle(p[0], p[1], point(x, y));
add_shape(new_triangle);
click_count = 0;
return;
}
return;
}
break;
}
}
void add_shape(t_shape new_shape)
{
t_shape null = NULL_SHAPE();
for (int i = 0; i < MAX_SHAPES; i++)
{
// Find free Null Shape
if (shapes[i].flag == 0)
{
shapes[i] = new_shape;
gtk_widget_queue_draw(drawing_area);
click_count = 0;
return;
}
}
}
gboolean on_clear(GtkWidget *button, gpointer user_data)
{
for (int i = 0; i < MAX_SHAPES; i++)
{
shapes[i] = NULL_SHAPE();
}
gtk_widget_queue_draw(drawing_area);
}
gboolean draw(GtkWidget *widget, cairo_t *c, gpointer data)
{
set_source_color(c, &red);
for (t_shape *s = shapes; s; s++)
{
if (!s->flag)
continue;
if (s->flag == -1)
break;
t_vertex *v = s->vertices;
if (s->type == CIRCLE)
{
float radius = hypotenuse(s->vertices[0], s->vertices[1]);
cairo_arc(c, s->vertices[0].X, s->vertices[0].Y, radius, 0, G_PI * 2);
}
if (s->type == TRIANGLE)
{
for (int i = 0; i < 2; i++)
{
cairo_move_to(c, s->vertices[i].X, s->vertices[i].Y);
cairo_arc(c, s->vertices[0].X, s->vertices[0].Y, 5, 0, G_PI * 2);
cairo_line_to(c, s->vertices[i + 1].X, s->vertices[i + 1].Y);
g_print("%d X%f Y%f \n", i, s->vertices[i].X, s->vertices[i].Y);
}
cairo_close_path(c);
}
cairo_fill(c);
cairo_close_path(c);
}
for (int i = 0; i < click_count; i++)
{
set_source_color(c, &black);
cairo_arc(c, click_positions[i].X, click_positions[i].Y, 2.0, 0, G_PI * 2);
}
/*set_source_color(c, &yellow);
cairo_arc(c, 65, 50, 30, 0, 2 * G_PI);
cairo_fill(c);
*/
return FALSE; /* TRUE to stop other handlers from being invoked for the event.
FALSE to propagate the event further. */
}
\ No newline at end of file
#ifndef LIB #ifndef LIB
#define LIB #define LIB
#define UNDEFINED 0
#include <gtk-3.0/gtk/gtk.h> #include <gtk-3.0/gtk/gtk.h>
enum INPUTTYPE current_input;
int shape_type;
int click_count = 0;
t_vertex click_positions[4];
#define UNDEFINED 0
enum SHAPETYPE { TRIANGLE,CIRCLE,RECTANGLE }; enum SHAPETYPE { TRIANGLE,CIRCLE,RECTANGLE };
enum INPUTTYPE { SELECTION, CREATE_SHAPE, FREEHAND };
typedef gboolean (*SignalHandlerPointer)(GtkWidget *widget, gpointer user_data); typedef gboolean (*SignalHandlerPointer)(GtkWidget *widget, gpointer user_data);
typedef struct vertex typedef struct vertex
{ {
int X; float X;
int Y; float Y;
} t_vertex; } t_vertex;
typedef struct shape typedef struct shape
...@@ -29,16 +34,21 @@ typedef struct custom_toolbar ...@@ -29,16 +34,21 @@ typedef struct custom_toolbar
{ {
GtkWidget *widget; GtkWidget *widget;
int len; int len;
GtkWidget *toolitems; int in_counter;
GtkToolItem **toolitems;
} t_custom_toolbar; } t_custom_toolbar;
t_shape create_square(int x1,int y1,int x2, int y2); t_shape create_square(float x1,float y1,float x2, float y2);
t_shape* create_triangle(int x1,int y1,int x2, int y2); t_shape create_triangle(t_vertex p1,t_vertex p2,t_vertex p3);
t_shape NULL_SHAPE(); t_shape NULL_SHAPE();
t_custom_toolbar *create_toolbar(); t_custom_toolbar *create_toolbar(int length);
t_vertex point(float x, float y);
GtkToolItem *create_and_bind_toolbutton(t_custom_toolbar *toolbar,const gchar *name); GtkToolItem *create_and_bind_toolbutton(t_custom_toolbar *toolbar,const gchar *name);
void bind_click_signal(GtkToolItem *item, SignalHandlerPointer sp, int data); void bind_click_signal(GtkToolItem *item, SignalHandlerPointer sp, int data);
float hypotenuse(t_vertex a, t_vertex b);
t_shape create_circle(t_vertex start, t_vertex end);
#endif #endif
\ No newline at end of file
#include "lib.h" #include "lib.h"
#include <stdlib.h> #include <stdlib.h>
#include <math.h>
t_vertex point(float x, float y)
t_vertex point(int x, int y)
{ {
t_vertex v; t_vertex v;
v.X = x; v.X = x;
...@@ -21,7 +21,7 @@ void create_shapes_array(t_shape* arr,int len) ...@@ -21,7 +21,7 @@ void create_shapes_array(t_shape* arr,int len)
} }
} }
t_shape create_square(int x1,int y1,int x2, int y2) t_shape create_square(float x1,float y1,float x2, float y2)
{ {
t_shape shape; t_shape shape;
shape.vertexCount = 4; shape.vertexCount = 4;
...@@ -37,6 +37,34 @@ t_shape create_square(int x1,int y1,int x2, int y2) ...@@ -37,6 +37,34 @@ t_shape create_square(int x1,int y1,int x2, int y2)
return shape; return shape;
} }
t_shape create_circle(t_vertex start, t_vertex end)
{
t_shape shape;
shape.vertexCount = 2;
shape.vertices = malloc(sizeof(t_vertex)*2);
shape.type = CIRCLE;
shape.flag = 1;
t_vertex* v = shape.vertices;
v[0] = start;
v[1] = end;
return shape;
}
t_shape create_triangle(t_vertex p1,t_vertex p2, t_vertex p3)
{
t_shape shape;
shape.vertexCount = 3;
shape.vertices = malloc(sizeof(t_vertex)*3);
shape.type = TRIANGLE;
shape.flag = 1;
t_vertex* v = shape.vertices;
v[0] = p1;
v[1] = p2;
v[2] = p3;
return shape;
}
t_shape NULL_SHAPE() t_shape NULL_SHAPE()
{ {
t_shape s; t_shape s;
...@@ -66,3 +94,10 @@ void clear_shape(t_shape *shape) ...@@ -66,3 +94,10 @@ void clear_shape(t_shape *shape)
free(shape->vertices); free(shape->vertices);
free(shape); free(shape);
} }
float hypotenuse(t_vertex a, t_vertex b)
{
float x_hat = (a.X - b.X)*(a.X-b.X);
float y_hat = (a.Y - b.Y) * (a.Y-b.Y);
return sqrt(x_hat+y_hat);
}
\ No newline at end of file
#include "lib.h"
#include <stdio.h> #include <stdio.h>
#include <gtk-3.0/gtk/gtk.h> #include <gtk-3.0/gtk/gtk.h>
#include "lib.h"
#include <stdarg.h> #include <stdarg.h>
t_custom_toolbar *create_toolbar(int length) t_custom_toolbar *create_toolbar(int length)
{ {
t_custom_toolbar* ct = malloc(sizeof(t_custom_toolbar)); t_custom_toolbar* ct = malloc(sizeof(t_custom_toolbar));
ct->len = length; ct->len = length;
ct->in_counter = 0;
GtkWidget *toolbar = gtk_toolbar_new(); GtkWidget *toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS); gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
ct->widget = toolbar; ct->widget = toolbar;
ct->toolitems = malloc(sizeof(GtkToolItem)*length);
return ct; return ct;
} }
...@@ -20,11 +23,15 @@ GtkToolItem *create_and_bind_toolbutton(t_custom_toolbar *toolbar,const gchar *n ...@@ -20,11 +23,15 @@ GtkToolItem *create_and_bind_toolbutton(t_custom_toolbar *toolbar,const gchar *n
{ {
GtkToolItem *b = gtk_tool_button_new(NULL,name); GtkToolItem *b = gtk_tool_button_new(NULL,name);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar->widget),b,-1); gtk_toolbar_insert(GTK_TOOLBAR(toolbar->widget),b,-1);
toolbar->toolitems[toolbar->in_counter++] = b;
return b; return b;
} }
void bind_click_signal(GtkToolItem *item, SignalHandlerPointer sp, int data) void bind_click_signal(GtkToolItem *item, SignalHandlerPointer sp, int data)
{ {
g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(sp),data); g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(sp),GINT_TO_POINTER(data));
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment