From 9e6f5343c8d7ec85e5df4d44ec9d909ed2f5b2fe Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Fri, 17 Jul 2015 14:36:45 +0200 Subject: Add basic key editing --- rcm-client-receive.c | 220 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 182 insertions(+), 38 deletions(-) diff --git a/rcm-client-receive.c b/rcm-client-receive.c index 60e84f9..f645fd7 100644 --- a/rcm-client-receive.c +++ b/rcm-client-receive.c @@ -16,6 +16,16 @@ #define WINDOW_WIDTH 300 #define WINDOW_HEIGHT 300 +struct remote { + char *name; + guint16 width; + guint16 height; + GList *buttons; + GtkWidget *widget; + GtkWidget *grid; + GtkWidget *edit_button; +}; + enum rcbutton_type { RCBUTTON_TYPE_NORMAL, RCBUTTON_TYPE_BLANK @@ -25,16 +35,9 @@ struct rcbutton { char *name; enum rcbutton_type type; GtkWidget *button; -}; - -struct remote { - char *name; - guint16 width; - guint16 height; - GList *buttons; - GtkWidget *widget; - GtkWidget *grid; - GtkWidget *edit_button; + guint x; + guint y; + struct remote *remote; }; struct state { @@ -46,6 +49,7 @@ struct state { static struct state state; +/* static void quick_message(GtkWidget *widget, gchar *message) { @@ -63,6 +67,7 @@ quick_message(GtkWidget *widget, gchar *message) gtk_container_add(GTK_CONTAINER(content_area), label); gtk_widget_show_all(dialog); } +*/ /* Hacky McHack - Estimate the size needed for a button with ABCDEF */ static int @@ -192,6 +197,115 @@ out: gtk_button_set_image(GTK_BUTTON(button), img); } +static GtkWidget * +new_button_widget(struct rcbutton *rcb, const gchar *protocol, guint64 scancode, + const gchar *keycode); + +static GtkWidget * +new_blank_widget(struct rcbutton *rcb); + +static void +edit_button_dialog(GtkButton *button, struct rcbutton *rcb) +{ + GtkWindow *parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(button))); + GtkWidget *dialog; + GtkWidget *content_area; + GtkWidget *grid; + GtkWidget *icon; + GtkWidget *keycode_label; + GtkWidget *keycode_cbox; + GtkWidget *comment_label; + GtkWidget *comment_entry; + gint r; + unsigned i; + /* FIXME: Get keymap and use keycodes from there */ + const char *keycodes[] = { "KEY_VIDEO", "KEY_TV", "KEY_OK", NULL }; + + dialog = gtk_dialog_new_with_buttons("Edit Button", parent, + GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL, + "Delete", GTK_RESPONSE_NO, + "OK", GTK_RESPONSE_ACCEPT, + "Cancel", GTK_RESPONSE_REJECT, + NULL); + + grid = gtk_grid_new(); + gtk_grid_set_column_spacing(GTK_GRID(grid), 12); + gtk_grid_set_row_spacing(GTK_GRID(grid), 12); + gtk_container_set_border_width(GTK_CONTAINER(grid), 12); + + icon = gtk_image_new_from_icon_name("preferences-desktop-keyboard", GTK_ICON_SIZE_DIALOG); + gtk_widget_set_valign(icon, GTK_ALIGN_START); + gtk_grid_attach(GTK_GRID(grid), icon, 0, 1, 1, 2); + + keycode_label = gtk_label_new("Keycode:"); + + gtk_widget_set_halign(keycode_label, GTK_ALIGN_START); + keycode_cbox = gtk_combo_box_text_new(); + for (i = 0; keycodes[i]; i++) { + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(keycode_cbox), + NULL, keycodes[i]); + if (rcb->name && !strcmp(rcb->name, keycodes[i])) + gtk_combo_box_set_active(GTK_COMBO_BOX(keycode_cbox), i); + } + gtk_grid_attach(GTK_GRID(grid), keycode_label, 1, 1, 1, 1); + gtk_grid_attach(GTK_GRID(grid), keycode_cbox, 2, 1, 1, 1); + + comment_label = gtk_label_new("Comment:"); + gtk_widget_set_halign(comment_label, GTK_ALIGN_START); + comment_entry = gtk_entry_new(); + gtk_entry_set_max_length(GTK_ENTRY(comment_entry), 100); + gtk_entry_set_placeholder_text(GTK_ENTRY(comment_entry), "No comment"); + gtk_grid_attach(GTK_GRID(grid), comment_label, 1, 2, 1, 1); + gtk_grid_attach(GTK_GRID(grid), comment_entry, 2, 2, 1, 1); + + content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + gtk_container_set_border_width(GTK_CONTAINER(content_area), 12); + gtk_container_add(GTK_CONTAINER(content_area), grid); + gtk_widget_show_all(dialog); + + gchar *keycode; + r = gtk_dialog_run(GTK_DIALOG(dialog)); + switch (r) { + case GTK_RESPONSE_ACCEPT: + keycode = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(keycode_cbox)); + if (!keycode) + goto out; + + printf("New keycode is %s\n", keycode); + + if (rcb->name) + g_free(rcb->name); + + if (rcb->button) + gtk_widget_destroy(rcb->button); + + rcb->name = keycode; + rcb->type = RCBUTTON_TYPE_NORMAL; + rcb->button = new_button_widget(rcb, "dummy", 0xf001f001, keycode); + gtk_widget_show_all(rcb->button); + break; + + case GTK_RESPONSE_NO: + if (rcb->type == RCBUTTON_TYPE_BLANK) + goto out; + + g_free(rcb->name); + rcb->name = NULL; + rcb->type = RCBUTTON_TYPE_BLANK; + gtk_widget_destroy(rcb->button); + rcb->button = new_blank_widget(rcb); + gtk_revealer_set_reveal_child(GTK_REVEALER(rcb->button), true); + gtk_widget_show_all(rcb->button); + break; + + default: + break; + } + +out: + gtk_widget_destroy(dialog); +} + static void edit_button(GtkButton *button, gpointer user_data) { @@ -203,10 +317,8 @@ edit_button(GtkButton *button, gpointer user_data) switch (rcb->type) { case RCBUTTON_TYPE_NORMAL: - quick_message(GTK_WIDGET(button), "Asked to edit a button\n"); - break; case RCBUTTON_TYPE_BLANK: - quick_message(GTK_WIDGET(button), "Asked to add a button\n"); + edit_button_dialog(button, rcb); break; default: break; @@ -215,19 +327,16 @@ edit_button(GtkButton *button, gpointer user_data) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), false); } -static struct rcbutton * -new_blank_add(GtkGrid *grid, guint x, guint y) +static GtkWidget * +new_blank_widget(struct rcbutton *rcb) { - struct rcbutton *rcb; GtkWidget *revealer; GtkWidget *button; - rcb = g_malloc0(sizeof(*rcb)); - rcb->type = RCBUTTON_TYPE_BLANK; revealer = gtk_revealer_new(); gtk_revealer_set_reveal_child(GTK_REVEALER(revealer), false); gtk_revealer_set_transition_type(GTK_REVEALER(revealer), GTK_REVEALER_TRANSITION_TYPE_CROSSFADE); - gtk_grid_attach(grid, revealer, x, y, 1, 1); + gtk_grid_attach(GTK_GRID(rcb->remote->grid), revealer, rcb->x, rcb->y, 1, 1); button = gtk_toggle_button_new(); g_signal_connect(button, "clicked", G_CALLBACK(edit_button), rcb); @@ -235,28 +344,58 @@ new_blank_add(GtkGrid *grid, guint x, guint y) gtk_widget_set_tooltip_text(button, "Add key"); gtk_container_add(GTK_CONTAINER(revealer), button); create_button_img(button, "KEY_RESERVED"); - rcb->button = revealer; - return rcb; + + return revealer; } static struct rcbutton * -new_button_add(GtkGrid *grid, const gchar *protocol, guint64 scancode, - const char *keycode, GdkRGBA *color, guint x, guint y) +new_blank_add(struct remote *remote, guint x, guint y) { struct rcbutton *rcb; + + rcb = g_malloc0(sizeof(*rcb)); + rcb->type = RCBUTTON_TYPE_BLANK; + rcb->remote = remote; + rcb->x = x; + rcb->y = y; + rcb->button = new_blank_widget(rcb); + + return rcb; +} + +static GtkWidget * +new_button_widget(struct rcbutton *rcb, const gchar *protocol, guint64 scancode, + const gchar *keycode) +{ + GtkWidget *button; gchar *tooltip; + button = gtk_toggle_button_new(); + g_signal_connect(button, "clicked", G_CALLBACK(edit_button), rcb); + gtk_widget_set_sensitive(button, false); + tooltip = g_strdup_printf("Protocol: %s\nScancode: 0x%08" PRIx64 "\nKeycode : %s", protocol, scancode, keycode); + gtk_widget_set_tooltip_text(button, tooltip); + g_free(tooltip); + gtk_grid_attach(GTK_GRID(rcb->remote->grid), button, rcb->x, rcb->y, 1, 1); + create_button_img(button, keycode); + + return button; +} + +static struct rcbutton * +new_button_add(struct remote *remote, const gchar *protocol, guint64 scancode, + const char *keycode, guint x, guint y) +{ + struct rcbutton *rcb; + rcb = g_malloc0(sizeof(*rcb)); rcb->type = RCBUTTON_TYPE_NORMAL; + rcb->remote = remote; rcb->name = strdup(keycode); - rcb->button = gtk_toggle_button_new(); - g_signal_connect(rcb->button, "clicked", G_CALLBACK(edit_button), rcb); - gtk_widget_set_sensitive(rcb->button, false); - tooltip = g_strdup_printf("Protocol: %s\nScancode: 0x%08" PRIx64 "\nKeycode : %s", protocol, scancode, keycode); - gtk_widget_set_tooltip_text(rcb->button, tooltip); - g_free(tooltip); - gtk_grid_attach(grid, rcb->button, x, y, 1, 1); - create_button_img(rcb->button, keycode); + rcb->x = x; + rcb->y = y; + rcb->button = new_button_widget(rcb, protocol, scancode, keycode); + return rcb; } @@ -307,7 +446,7 @@ resize_layout(struct remote *remote, guint16 new_width, guint16 new_height) { guint16 old_width = remote->width; guint16 old_height = remote->height; - unsigned x, y; + guint16 x, y; GList *new_buttons = NULL; printf("Resizing...\n"); @@ -346,7 +485,7 @@ resize_layout(struct remote *remote, guint16 new_width, guint16 new_height) printf("\tCopied button at %ux%u\n", x, y); } else { struct rcbutton *rcb; - rcb = new_blank_add(GTK_GRID(remote->grid), x, y); + rcb = new_blank_add(remote, x, y); new_buttons = g_list_append(new_buttons, rcb); gtk_revealer_set_reveal_child(GTK_REVEALER(rcb->button), true); printf("\tNew button at %ux%u\n", x, y); @@ -376,6 +515,7 @@ resize_layout_dialog(GtkButton *button, gpointer user_data) GtkWidget *dialog; GtkWidget *content_area; GtkWidget *grid; + GtkWidget *icon; GtkWidget *width_label; GtkAdjustment *width_adj; GtkWidget *width_input; @@ -397,15 +537,19 @@ resize_layout_dialog(GtkButton *button, gpointer user_data) gtk_grid_set_row_spacing(GTK_GRID(grid), 12); gtk_container_set_border_width(GTK_CONTAINER(grid), 12); + icon = gtk_image_new_from_icon_name("document-page-setup", GTK_ICON_SIZE_DIALOG); + gtk_widget_set_valign(icon, GTK_ALIGN_START); + gtk_grid_attach(GTK_GRID(grid), icon, 0, 1, 1, 2); + width_label = gtk_label_new("Width:"); - gtk_widget_set_halign(width_label, GTK_ALIGN_END); + gtk_widget_set_halign(width_label, GTK_ALIGN_START); width_adj = gtk_adjustment_new(remote->width, 1.0, 100.0, 1.0, 5.0, 0.0); width_input = gtk_spin_button_new(width_adj, 1.0, 0); gtk_grid_attach(GTK_GRID(grid), width_label, 1, 1, 1, 1); gtk_grid_attach(GTK_GRID(grid), width_input, 2, 1, 1, 1); height_label = gtk_label_new("Height:"); - gtk_widget_set_halign(height_label, GTK_ALIGN_END); + gtk_widget_set_halign(height_label, GTK_ALIGN_START); height_adj = gtk_adjustment_new(remote->height, 1.0, 100.0, 1.0, 5.0, 0.0); height_input = gtk_spin_button_new(height_adj, 1.0, 0); gtk_grid_attach(GTK_GRID(grid), height_label, 1, 2, 1, 1); @@ -594,13 +738,13 @@ get_keymap(RCDevice *object, const gchar *keymap_name) if (!strcmp(type, "button")) { struct rcbutton *rcb; - rcb = new_button_add(GTK_GRID(grid), protocol, scancode, - keycode, NULL, col, row); + rcb = new_button_add(remote, protocol, scancode, + keycode, col, row); remote->buttons = g_list_append(remote->buttons, rcb); printf("Got a button: %s:0x%08" PRIx64 ":%s\n", protocol, scancode, keycode); } else if (!strcmp(type, "blank")) { struct rcbutton *rcb; - rcb = new_blank_add(GTK_GRID(grid), col, row); + rcb = new_blank_add(remote, col, row); remote->buttons = g_list_append(remote->buttons, rcb); printf("Got a blank\n"); } else -- cgit v1.2.3