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

Source Code Insert

parent 82f2d041
Branches
Tags
No related merge requests found
CFLAGS := -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/fribidi -I/usr/include/harfbuzz -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/x86_64-linux-gnu -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g
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)
app: app.c lib/shapes.c lib/ui.c lib/lib.h
$(shell unset GTK_PATH)
echo $(LINKS) are linked to executable
gcc $(CFLAGS) app.c -o app $(LIBS) $(LINKS)
\ No newline at end of file
src/app 0 → 100755
File added
src/app.c 0 → 100644
#include <stdio.h>
#include <gtk-3.0/gtk/gtk.h>
#include "lib/lib.h"
#define set_source_color gdk_cairo_set_source_rgba
#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 height = 600;
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;
g_print("X%f Y%f clicked\n", clickX, clickY);
}
gboolean on_button_clicked(GtkWidget *button, gpointer user_data)
{
g_print("Button clicked!\n");
}
gboolean draw(GtkWidget *widget, cairo_t *c, gpointer 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);
}
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
toolbar_auswerten(GtkWidget *button, gpointer data)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new(
GTK_WINDOW(data),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
"Es wurde ein Toolbar-Button betaetigt");
/* Sobald Dialog bestätigt, auch wieder zerstören */
g_signal_connect(dialog, "response",
G_CALLBACK(gtk_widget_destroy), NULL);
/* Dialogbox anzeigen */
gtk_widget_show(dialog);
}
int create_window_layout(int argc, char **argv)
{
if (argc > 1 && argc != 3)
{
printf("Usage: ./app [WIDTH] [HEIGHT]\n or just ./app\n\n");
return -1;
}
if (argc == 3)
{
width = atoi(argv[1]);
height = atoi(argv[2]);
}
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello");
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
GtkWidget *toolbar_vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
GtkWidget *button = gtk_button_new_with_label("Quit");
g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *drawing_area = gtk_drawing_area_new();
gtk_widget_add_events(drawing_area, GDK_BUTTON_PRESS_MASK);
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);
gtk_widget_set_size_request(drawing_area, width, height);
t_custom_toolbar *toolbar = create_toolbar();
GtkToolItem *test1_btn = create_and_bind_toolbutton(toolbar,"test with callback");
GtkToolItem *test2_btn = create_and_bind_toolbutton(toolbar,"test 3");
GtkToolItem *test3_btn = create_and_bind_toolbutton(toolbar,"test 4");
bind_click_signal(test1_btn,on_button_clicked);
gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(drawing_area));
gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(toolbar->widget));
gtk_container_add(GTK_CONTAINER(vbox), button);
gtk_container_add(GTK_CONTAINER(vbox), hbox);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(window);
}
int main(int argc, char **argv)
{
for (int i = 0; i < MAX_SHAPES; i++)
{
shapes[i] = NULL_SHAPE();
}
shapes[MAX_SHAPES].flag = -1;
shapes[0] = create_square(20, 20, 40, 40);
create_window_layout(argc, argv);
printf("Starting GTK window with dimensions W:%d, H:%d\n", width, height);
gtk_main();
printf("Closing GTK App.\n");
return 0;
}
\ No newline at end of file
#ifndef LIB
#define LIB
#include <gtk-3.0/gtk/gtk.h>
#define UNDEFINED 0
#define TRIANGLE 1
#define SQUARE 2
#define RECT 3
typedef gboolean (*SignalHandlerPointer)(GtkWidget *widget, gpointer user_data);
typedef struct vertex
{
int X;
int Y;
} t_vertex;
typedef struct shape
{
int type;
t_vertex* vertices;
int vertexCount;
int flag;
} t_shape;
typedef struct custom_toolbar
{
GtkWidget *widget;
int len;
GtkWidget *toolitems;
} t_custom_toolbar;
t_shape create_square(int x1,int y1,int x2, int y2);
t_shape* create_triangle(int x1,int y1,int x2, int y2);
t_shape NULL_SHAPE();
t_custom_toolbar *create_toolbar();
GtkToolItem *create_and_bind_toolbutton(t_custom_toolbar *toolbar,const gchar *name);
void bind_click_signal(GtkToolItem *item, SignalHandlerPointer sp);
#endif
\ No newline at end of file
#include "lib.h"
#include <stdlib.h>
t_vertex point(int x, int y)
{
t_vertex v;
v.X = x;
v.Y = y;
return v;
}
void create_shapes_array(t_shape* arr,int len)
{
if(len <= 0)
return;
for(int i = 0; i < len; i ++)
{
arr[i] = NULL_SHAPE();
}
}
t_shape create_square(int x1,int y1,int x2, int y2)
{
t_shape shape;
shape.vertexCount = 4;
shape.vertices = malloc(sizeof(t_vertex)*4);
shape.type = SQUARE;
shape.flag = 1;
t_vertex* v = shape.vertices;
v[0] = point(x1,y1);
v[1] = point(x1,y1);
v[2] = point(x1,y1);
v[3] = point(x1,y1);
return shape;
}
t_shape NULL_SHAPE()
{
t_shape s;
s.flag = 0;
s.type = 0;
s.vertexCount = 0;
s.vertices = 0;
return s;
}
void order_shape(t_shape* shape)
{
if(shape->vertexCount <= 1)
return;
for(int i = 0; i < shape->vertexCount; i++)
{
}
return;
}
void clear_shape(t_shape *shape)
{
free(shape->vertices);
free(shape);
}
\ No newline at end of file
#include <stdio.h>
#include <gtk-3.0/gtk/gtk.h>
#include "lib.h"
#include <stdarg.h>
t_custom_toolbar *create_toolbar(int length)
{
t_custom_toolbar* ct = malloc(sizeof(t_custom_toolbar));
ct->len = length;
GtkWidget *toolbar = gtk_toolbar_new();
gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
ct->widget = toolbar;
return ct;
}
GtkToolItem *create_and_bind_toolbutton(t_custom_toolbar *toolbar,const gchar *name)
{
GtkToolItem *b = gtk_tool_button_new(NULL, name);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar->widget),b,-1);
return b;
}
void bind_click_signal(GtkToolItem *item, SignalHandlerPointer sp)
{
g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(sp), NULL);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment