Skip to content

Commit

Permalink
added change avatar gui
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavChuiko committed Feb 5, 2022
1 parent c914f12 commit 3e44f41
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 9 deletions.
Binary file removed client/data/img/user-avatar4.png
Binary file not shown.
6 changes: 4 additions & 2 deletions client/inc/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include <pthread.h>
#include <gtk/gtk.h>
#include <cairo.h>
// #include <netinet/in.h>
// #include <arpa/inet.h>

#ifdef __MACH__
#include "../../libraries/openssl/openssl/ssl.h"
Expand Down Expand Up @@ -56,6 +54,9 @@ void destroy_popup_window(GtkWidget *widget, gpointer chat_screen);
void build_authorizatioin_window();
void build_chat_window();

//DRAW AVATAR
gboolean draw_user_avatar(GtkWidget *widget, cairo_t *cr, gpointer data);

//AUTH
void build_login_menu();
void build_signup_menu();
Expand Down Expand Up @@ -182,6 +183,7 @@ void send_button_click(GtkWidget *widget, gpointer new_message_field);

void build_change_password_window(GtkWidget *widget, gpointer data);
void build_change_login_window(GtkWidget *widget, gpointer data);
void build_change_avatar_window(GtkWidget *widget, gpointer data);
void build_change_chat_name_window(GtkWidget *widget, gpointer data);

void leave_chat_btn_click(GtkWidget *widget, gpointer data);
Expand Down
80 changes: 80 additions & 0 deletions client/src/gui/build_change_avatar_window.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "../../inc/client.h"

void change_avatar_btn_click(GtkWidget *widget, gpointer data) {
GtkWidget *toplevel = gtk_widget_get_toplevel(widget);
(void)data;

GtkWidget *path_field = get_widget_by_name_r(toplevel, "path_field");
GtkWidget *path_notify_label = get_widget_by_name_r(toplevel, "path_notify_label");

char *new_avatar = (char*)gtk_entry_get_text(GTK_ENTRY(path_field));

handle_update_user_image(new_avatar);
handle_get_user_image(utils->current_user->user_id, &utils->current_user->avatar_path);

gtk_widget_destroy(toplevel);
}

void build_change_avatar_window(GtkWidget *widget, gpointer data) {
if (widget){};
(void)data;

GtkWidget *popup_window = create_popup_window(450, 0);
GtkWidget *change_login_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);

gtk_container_add(GTK_CONTAINER(popup_window), change_login_box);
add_class(change_login_box, "popup_menu");

GtkWidget *change_avatar_title = gtk_label_new("Change avatar");
gtk_widget_set_halign(GTK_WIDGET(change_avatar_title), GTK_ALIGN_CENTER);
add_class(change_avatar_title, "popup_window_title");

GtkWidget *path_label = gtk_label_new("New avatar path:");
gtk_widget_set_halign(GTK_WIDGET(path_label), GTK_ALIGN_START);
add_class(path_label, "input-field_title");
GtkWidget *path_field = gtk_entry_new();
gtk_widget_set_name(path_field, "path_field");
gtk_entry_set_placeholder_text(GTK_ENTRY(path_field), "Avatar path");
// g_signal_connect(G_OBJECT(path_field), "focus-out-event", G_CALLBACK(focus_out_login_field), NULL);
add_class(path_field, "input-field");
add_class(path_field, "input-field--name");

GtkWidget *path_notify_label = gtk_label_new(" ");
gtk_widget_set_halign(path_notify_label, GTK_ALIGN_START);
gtk_widget_set_name(path_notify_label, "path_notify_label");
add_class(path_notify_label, "notify-label");

//btn box
GtkWidget *btn_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
gtk_widget_set_halign(GTK_WIDGET(btn_box), GTK_ALIGN_CENTER);
add_class(btn_box, "popup_btn_box");

GtkWidget *change_login_btn = gtk_button_new_with_label("Submit");
gtk_widget_set_size_request(GTK_WIDGET(change_login_btn), 150, 50);
g_signal_connect(G_OBJECT(change_login_btn), "enter-notify-event", G_CALLBACK(on_crossing), NULL);
g_signal_connect(G_OBJECT(change_login_btn), "leave-notify-event", G_CALLBACK(on_crossing), NULL);
g_signal_connect(G_OBJECT(change_login_btn), "clicked", G_CALLBACK(change_avatar_btn_click), NULL);
add_class(change_login_btn, "btn");
add_class(change_login_btn, "btn--blue");

GtkWidget *cancel_btn = gtk_button_new_with_label("Cancel");
gtk_widget_set_size_request(GTK_WIDGET(cancel_btn), 150, 50);
g_signal_connect(G_OBJECT(cancel_btn), "enter-notify-event", G_CALLBACK(on_crossing), NULL);
g_signal_connect(G_OBJECT(cancel_btn), "leave-notify-event", G_CALLBACK(on_crossing), NULL);
g_signal_connect(G_OBJECT(cancel_btn), "clicked", G_CALLBACK(destroy_popup_window), NULL);
add_class(cancel_btn, "btn");
add_class(cancel_btn, "btn--dark-blue");

gtk_box_pack_end(GTK_BOX(btn_box), change_login_btn, FALSE, FALSE, 0);
gtk_box_pack_end(GTK_BOX(btn_box), cancel_btn, FALSE, FALSE, 0);
//

gtk_box_pack_start(GTK_BOX(change_login_box), change_avatar_title, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(change_login_box), path_label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(change_login_box), path_field, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(change_login_box), path_notify_label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(change_login_box), btn_box, FALSE, FALSE, 0);

gtk_widget_show_all(popup_window);
gtk_widget_grab_focus(popup_window);
}
6 changes: 6 additions & 0 deletions client/src/gui/build_change_login_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ void change_login_btn_click(GtkWidget *widget, gpointer data) {

int response_code = handle_edit_username_request(new_login);
handle_edit_username_response_code(response_code, login_field, login_notify_label);

if (response_code == R_SUCCESS)
{
GtkWidget *leftbar_footer_username = get_widget_by_name_r(main_window, "leftbar_footer_username");
gtk_label_set_text(GTK_LABEL(leftbar_footer_username), utils->current_user->name);
}
}

void build_change_login_window(GtkWidget *widget, gpointer data) {
Expand Down
7 changes: 5 additions & 2 deletions client/src/gui/build_chat_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ void build_leftbar(GtkWidget *chat_screen)
GtkWidget *leftbar_footer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
add_class(leftbar_footer, "leftbar_footer");

GtkWidget *user_avatar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
GtkWidget *user_avatar = gtk_drawing_area_new();
gtk_widget_set_size_request(GTK_WIDGET(user_avatar), 27, 27);
g_signal_connect(G_OBJECT(user_avatar), "draw", G_CALLBACK(draw_user_avatar), (gpointer)27); // Получить avatar пользовтеля
gtk_widget_set_halign(user_avatar, GTK_ALIGN_START);
gtk_widget_set_valign(user_avatar, GTK_ALIGN_CENTER);
gtk_box_pack_start(GTK_BOX(leftbar_footer), user_avatar, FALSE, FALSE, 0);
add_class(user_avatar, "chatlist_item_avatar");

GtkWidget *username = gtk_label_new(utils->current_user->name);
gtk_widget_set_name(username, "leftbar_footer_username");
Expand Down Expand Up @@ -106,6 +106,9 @@ void build_leftbar(GtkWidget *chat_screen)
GtkWidget *change_password_btn = gtk_menu_item_new_with_label("Change password");
g_signal_connect(change_password_btn, "activate", G_CALLBACK(build_change_password_window), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), change_password_btn);
GtkWidget *change_avatar_btn = gtk_menu_item_new_with_label("Change avatar");
g_signal_connect(change_avatar_btn, "activate", G_CALLBACK(build_change_avatar_window), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), change_avatar_btn);
GtkWidget *delete_account_btn = gtk_menu_item_new_with_label("Delete account");
g_signal_connect(delete_account_btn, "activate", G_CALLBACK(build_confirm_delete_account_window), NULL);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), delete_account_btn);
Expand Down
2 changes: 1 addition & 1 deletion client/src/gui/chatlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

static gboolean da_expose(GtkWidget *widget, cairo_t *cr, gpointer data)
{
char* a = "client/data/img/user_avatars/avatar2.png";
char* a = "client/data/img/user_avatars/avatar4.png";
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_scale(a, 42, 42, FALSE, NULL); // get_pixbuf_with_size(a, 34, 34);
gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
g_object_unref(G_OBJECT(pixbuf));
Expand Down
31 changes: 31 additions & 0 deletions client/src/gui/draw_avatar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "../../inc/client.h"

gboolean draw_user_avatar(GtkWidget *widget, cairo_t *cr, gpointer data)
{
if (widget) {}
// (void)data;
int size = (int)data;
// char* a = utils->current_user->avatar_path;
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_scale(utils->current_user->avatar_path, size, size, FALSE, NULL); // get_pixbuf_with_size(a, 34, 34);
gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
g_object_unref(G_OBJECT(pixbuf));

double x = 0,
y = 0,
width = size,
height = size,
aspect = 1.0, /* aspect ratio */
corner_radius = height / 2.0; /* and corner curvature radius */
double radius = corner_radius / aspect;
double degrees = 3.14159265358979 / 180.0;

cairo_new_sub_path (cr);
cairo_arc (cr, x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
cairo_arc (cr, x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
cairo_arc (cr, x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
cairo_close_path (cr);

cairo_fill(cr);
return FALSE;
}
6 changes: 2 additions & 4 deletions client/src/gui/update_chat_field.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "../../inc/client.h"



void add_message(t_msg *message) {
GtkWidget *chat_field = get_widget_by_name_r(main_window, "chat_field");

Expand All @@ -14,14 +12,14 @@ void add_message(t_msg *message) {
add_class(message_box, "msg_box");
gtk_box_pack_start(GTK_BOX(chat_field), message_box, FALSE, FALSE, 0);

GtkWidget *avatar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
GtkWidget *avatar = gtk_drawing_area_new();
gtk_widget_set_size_request(GTK_WIDGET(avatar), 27, 27);
g_signal_connect(G_OBJECT(avatar), "draw", G_CALLBACK(draw_user_avatar), (gpointer)27); // Получить avatar пользовтеля
gtk_widget_set_halign(avatar, cur_user ? GTK_ALIGN_START : GTK_ALIGN_END);
gtk_widget_set_valign(avatar, GTK_ALIGN_START);
if (!cur_user) {
gtk_box_pack_start(GTK_BOX(message_box), avatar, FALSE, FALSE, 0);
}
add_class(avatar, "chatlist_item_avatar");

GtkWidget *message_text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_widget_set_halign(GTK_WIDGET(message_text), cur_user ? GTK_ALIGN_START : GTK_ALIGN_END);
Expand Down

0 comments on commit 3e44f41

Please sign in to comment.