* Pidgin is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA #include "gtkwhiteboard.h" PIDGIN_WHITEBOARD_BRUSH_UP, PIDGIN_WHITEBOARD_BRUSH_DOWN, PIDGIN_WHITEBOARD_BRUSH_MOTION } PidginWhiteboardBrushState; #define PIDGIN_TYPE_WHITEBOARD (pidgin_whiteboard_get_type()) G_DECLARE_FINAL_TYPE(PidginWhiteboard, pidgin_whiteboard, PIDGIN, WHITEBOARD, * @cr: Cairo context for drawing * @surface: Cairo surface for drawing * @wb: Backend data for this whiteboard * @drawing_area: Drawing area * @color_button: A color chooser widget * @brush_color: Foreground color * @brush_size: Brush size * @brush_state: The @PidginWhiteboardBrushState state of the brush cairo_surface_t *surface; PidginWhiteboardBrushState brush_state; /* Tracks last position of the mouse when drawing */ /* Tracks how many brush motions made */ G_DEFINE_TYPE(PidginWhiteboard, pidgin_whiteboard, GTK_TYPE_WINDOW) /****************************************************************************** *****************************************************************************/ pidgin_whiteboard_rgb24_to_rgba(int color_rgb, GdkRGBA *color) color->red = ((color_rgb >> 16) & 0xFF) / 255.0f; color->green = ((color_rgb >> 8) & 0xFF) / 255.0f; color->blue = (color_rgb & 0xFF) / 255.0f; whiteboard_close_cb(GtkWidget *widget, GdkEvent *event, G_GNUC_UNUSED gpointer data) PidginWhiteboard *gtkwb = PIDGIN_WHITEBOARD(widget); g_clear_object(>kwb->wb); static gboolean pidgin_whiteboard_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data) PidginWhiteboard *gtkwb = (PidginWhiteboard*)data; GtkAllocation allocation; GdkRGBA white = {1.0, 1.0, 1.0, 1.0}; cairo_destroy(gtkwb->cr); cairo_surface_destroy(gtkwb->surface); gtk_widget_get_allocation(widget, &allocation); gtkwb->surface = cairo_image_surface_create( CAIRO_FORMAT_RGB24, allocation.width, allocation.height); gtkwb->cr = cr = cairo_create(gtkwb->surface); gdk_cairo_set_source_rgba(cr, &white); cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); pidgin_whiteboard_draw_event(GtkWidget *widget, cairo_t *cr, PidginWhiteboard *gtkwb = _gtkwb; cairo_set_source_surface(cr, gtkwb->surface, 0, 0); pidgin_whiteboard_set_canvas_as_icon(PidginWhiteboard *gtkwb) /* Makes an icon from the whiteboard's canvas 'image' */ pixbuf = gdk_pixbuf_get_from_surface(gtkwb->surface, 0, 0, gtkwb->width, gtk_window_set_icon(GTK_WINDOW(gtkwb), pixbuf); pidgin_whiteboard_draw_brush_point(PurpleWhiteboard *wb, int x, int y, PidginWhiteboard *gtkwb = purple_whiteboard_get_ui_data(wb); GtkWidget *widget = gtkwb->drawing_area; cairo_t *gfx_con = gtkwb->cr; /* Interpret and convert color */ pidgin_whiteboard_rgb24_to_rgba(color, &rgba); gdk_cairo_set_source_rgba(gfx_con, &rgba); cairo_arc(gfx_con, x, y, size / 2.0, 0.0, 2.0 * M_PI); gtk_widget_queue_draw_area(widget, x - size / 2, y - size / 2, size, /* Uses Bresenham's algorithm (as provided by Wikipedia) */ pidgin_whiteboard_draw_brush_line(PurpleWhiteboard *wb, int x0, int y0, int x1, int y1, int color, int size) gboolean steep = abs(y1 - y0) > abs(x1 - x0); pidgin_whiteboard_draw_brush_point(wb, y, x, color, size); pidgin_whiteboard_draw_brush_point(wb, x, y, color, size); pidgin_whiteboard_draw_brush_point(wb, y, x, color, pidgin_whiteboard_draw_brush_point(wb, x, y, color, static gboolean pidgin_whiteboard_brush_down(GtkWidget *widget, GdkEventButton *event, gpointer data) PidginWhiteboard *gtkwb = (PidginWhiteboard*)data; PurpleWhiteboard *wb = gtkwb->wb; GList *draw_list = purple_whiteboard_get_draw_list(wb); if (gtkwb->brush_state != PIDGIN_WHITEBOARD_BRUSH_UP) { /* Potential double-click DOWN to DOWN? */ gtkwb->brush_state = PIDGIN_WHITEBOARD_BRUSH_DOWN; gtkwb->brush_state = PIDGIN_WHITEBOARD_BRUSH_DOWN; if (event->button == GDK_BUTTON_PRIMARY && gtkwb->cr != NULL) { /* Check if draw_list has contents; if so, clear it */ purple_whiteboard_draw_list_destroy(draw_list); /* Set tracking variables */ gtkwb->last_x = event->x; gtkwb->last_y = event->y; draw_list = g_list_append(draw_list, GINT_TO_POINTER(gtkwb->last_x)); draw_list = g_list_append(draw_list, GINT_TO_POINTER(gtkwb->last_y)); pidgin_whiteboard_draw_brush_point(gtkwb->wb, gtkwb->brush_color, gtkwb->brush_size); purple_whiteboard_set_draw_list(wb, draw_list); static gboolean pidgin_whiteboard_brush_motion(GtkWidget *widget, GdkEventMotion *event, gpointer data) PidginWhiteboard *gtkwb = (PidginWhiteboard*)data; PurpleWhiteboard *wb = gtkwb->wb; GList *draw_list = purple_whiteboard_get_draw_list(wb); gdk_window_get_device_position(event->window, event->device, &x, &y, if (state & GDK_BUTTON1_MASK && gtkwb->cr != NULL) { if ((gtkwb->brush_state != PIDGIN_WHITEBOARD_BRUSH_DOWN) && (gtkwb->brush_state != PIDGIN_WHITEBOARD_BRUSH_MOTION)) { "***Bad brush state transition %d to MOTION\n", gtkwb->brush_state = PIDGIN_WHITEBOARD_BRUSH_MOTION; gtkwb->brush_state = PIDGIN_WHITEBOARD_BRUSH_MOTION; /* NOTE 100 is a temporary constant for how many deltas/motions in a if (gtkwb->motion_count == 100) { draw_list = g_list_append(draw_list, GINT_TO_POINTER(dx)); draw_list = g_list_append(draw_list, GINT_TO_POINTER(dy)); /* Send draw list to the draw_list handler */ purple_whiteboard_send_draw_list(gtkwb->wb, draw_list); /* The brush stroke is finished, clear the list for another one */ purple_whiteboard_draw_list_destroy(draw_list); /* Reset motion tracking */ draw_list = g_list_append( draw_list, GINT_TO_POINTER(gtkwb->last_x)); draw_list = g_list_append( draw_list, GINT_TO_POINTER(gtkwb->last_y)); draw_list = g_list_append(draw_list, GINT_TO_POINTER(dx)); draw_list = g_list_append(draw_list, GINT_TO_POINTER(dy)); pidgin_whiteboard_draw_brush_line( gtkwb->wb, gtkwb->last_x, gtkwb->last_y, x, y, gtkwb->brush_color, gtkwb->brush_size); /* Set tracking variables */ purple_whiteboard_set_draw_list(wb, draw_list); static gboolean pidgin_whiteboard_brush_up(GtkWidget *widget, GdkEventButton *event, gpointer data) PidginWhiteboard *gtkwb = (PidginWhiteboard*)data; PurpleWhiteboard *wb = gtkwb->wb; GList *draw_list = purple_whiteboard_get_draw_list(wb); if ((gtkwb->brush_state != PIDGIN_WHITEBOARD_BRUSH_DOWN) && (gtkwb->brush_state != PIDGIN_WHITEBOARD_BRUSH_MOTION)) { purple_debug_error("gtkwhiteboard", "***Bad brush state transition %d to UP\n", gtkwb->brush_state = PIDGIN_WHITEBOARD_BRUSH_UP; gtkwb->brush_state = PIDGIN_WHITEBOARD_BRUSH_UP; if (event->button == GDK_BUTTON_PRIMARY && gtkwb->cr != NULL) { /* If the brush was never moved, express two sets of two deltas That's a * 'point,' but not for Yahoo! if (gtkwb->motion_count == 0) { /* For Yahoo!, a (0 0) indicates the end of drawing */ /* FIXME: Yahoo Doodle specific! */ for (index = 0; index < 2; index++) { draw_list = g_list_append(draw_list, 0); draw_list = g_list_append(draw_list, 0); /* Send draw list to protocol draw_list handler */ purple_whiteboard_send_draw_list(gtkwb->wb, draw_list); pidgin_whiteboard_set_canvas_as_icon(gtkwb); /* The brush stroke is finished, clear the list for another one purple_whiteboard_draw_list_destroy(draw_list); purple_whiteboard_set_draw_list(wb, NULL); static void pidgin_whiteboard_set_dimensions(PurpleWhiteboard *wb, int width, int height) PidginWhiteboard *gtkwb = purple_whiteboard_get_ui_data(wb); static void pidgin_whiteboard_set_brush(PurpleWhiteboard *wb, int size, int color) PidginWhiteboard *gtkwb = purple_whiteboard_get_ui_data(wb); gtkwb->brush_size = size; gtkwb->brush_color = color; static void pidgin_whiteboard_clear(PurpleWhiteboard *wb) PidginWhiteboard *gtkwb = purple_whiteboard_get_ui_data(wb); GtkWidget *drawing_area = gtkwb->drawing_area; GtkAllocation allocation; GdkRGBA white = {1.0, 1.0, 1.0, 1.0}; gtk_widget_get_allocation(drawing_area, &allocation); gdk_cairo_set_source_rgba(cr, &white); cairo_rectangle(cr, 0, 0, allocation.width, allocation.height); gtk_widget_queue_draw_area(drawing_area, 0, 0, allocation.width, allocation.height); static void pidgin_whiteboard_button_clear_press(GtkWidget *widget, gpointer data) PidginWhiteboard *gtkwb = (PidginWhiteboard*)(data); /* Confirm whether the user really wants to clear */ GtkWidget *dialog = gtk_message_dialog_new( GTK_WINDOW(gtkwb), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "%s", _("Do you really want to clear?")); gint response = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); if (response == GTK_RESPONSE_YES) pidgin_whiteboard_clear(gtkwb->wb); pidgin_whiteboard_set_canvas_as_icon(gtkwb); /* Do protocol specific clearing procedures */ purple_whiteboard_send_clear(gtkwb->wb); pidgin_whiteboard_button_save_press(GtkWidget *widget, gpointer _gtkwb) PidginWhiteboard *gtkwb = _gtkwb; GtkFileChooserNative *chooser; chooser = gtk_file_chooser_native_new(_("Save File"), GTK_WINDOW(gtkwb), GTK_FILE_CHOOSER_ACTION_SAVE, _("_Save"), _("_Cancel")); gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(chooser), gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), result = gtk_native_dialog_run(GTK_NATIVE_DIALOG(chooser)); if (result == GTK_RESPONSE_ACCEPT) { gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser)); pixbuf = gdk_pixbuf_get_from_surface( gtkwb->surface, 0, 0, gtkwb->width, gtkwb->height); success = gdk_pixbuf_save(pixbuf, filename, "png", NULL, "compression", "9", NULL); purple_debug_info("gtkwhiteboard", "whiteboard saved to \"%s\"", filename); purple_notify_error(NULL, _("Whiteboard"), _("Unable to save the file"), NULL, NULL); purple_debug_error("gtkwhiteboard", "whiteboard " "couldn't be saved to \"%s\"", filename); color_selected(GtkColorButton *button, PidginWhiteboard *gtkwb) PurpleWhiteboard *wb = gtkwb->wb; gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(button), &color); new_color = (unsigned int)(color.red * 255) << 16; new_color |= (unsigned int)(color.green * 255) << 8; new_color |= (unsigned int)(color.blue * 255); purple_whiteboard_get_brush(wb, &old_size, &old_color); purple_whiteboard_send_brush(wb, old_size, new_color); pidgin_whiteboard_create(PurpleWhiteboard *wb) gtkwb = PIDGIN_WHITEBOARD(g_object_new(PIDGIN_TYPE_WHITEBOARD, NULL)); purple_whiteboard_set_ui_data(wb, gtkwb); /* Get dimensions (default?) for the whiteboard canvas */ if (!purple_whiteboard_get_dimensions(wb, >kwb->width, /* Give some initial board-size */ if (!purple_whiteboard_get_brush(wb, >kwb->brush_size, /* Give some initial brush-info */ gtkwb->brush_color = 0xff0000; /* Try and set window title as the name of the buddy, else just use buddy = purple_blist_find_buddy(purple_whiteboard_get_account(wb), purple_whiteboard_get_who(wb)); gtk_window_set_title(GTK_WINDOW(gtkwb), ? purple_buddy_get_contact_alias(buddy) : purple_whiteboard_get_who(wb)); gtk_widget_set_name(GTK_WIDGET(gtkwb), purple_whiteboard_get_who(wb)); gtk_widget_set_size_request(GTK_WIDGET(gtkwb->drawing_area), gtkwb->width, gtkwb->height); pidgin_whiteboard_rgb24_to_rgba(gtkwb->brush_color, &color); gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(gtkwb->color_button), /* Make all this (window) visible */ gtk_widget_show(GTK_WIDGET(gtkwb)); pidgin_whiteboard_set_canvas_as_icon(gtkwb); /* TODO Specific protocol/whiteboard assignment here? Needs a UI Op? */ /* Set default brush size and color */ ds->brush_size = DOODLE_BRUSH_MEDIUM; pidgin_whiteboard_destroy(PurpleWhiteboard *wb) g_return_if_fail(wb != NULL); gtkwb = purple_whiteboard_get_ui_data(wb); g_return_if_fail(gtkwb != NULL); /* TODO Ask if user wants to save picture before the session is closed purple_whiteboard_set_ui_data(wb, NULL); /****************************************************************************** *****************************************************************************/ pidgin_whiteboard_init(PidginWhiteboard *self) gtk_widget_init_template(GTK_WIDGET(self)); self->brush_state = PIDGIN_WHITEBOARD_BRUSH_UP; pidgin_whiteboard_finalize(GObject *obj) PidginWhiteboard *gtkwb = PIDGIN_WHITEBOARD(obj); /* Clear graphical memory */ g_clear_pointer(>kwb->cr, cairo_destroy); g_clear_pointer(>kwb->surface, cairo_surface_destroy); G_OBJECT_CLASS(pidgin_whiteboard_parent_class)->finalize(obj); pidgin_whiteboard_class_init(PidginWhiteboardClass *klass) GObjectClass *obj_class = G_OBJECT_CLASS(klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); obj_class->finalize = pidgin_whiteboard_finalize; gtk_widget_class_set_template_from_resource( widget_class, "/im/pidgin/Pidgin/Whiteboard/whiteboard.ui"); gtk_widget_class_bind_template_child(widget_class, PidginWhiteboard, gtk_widget_class_bind_template_child(widget_class, PidginWhiteboard, gtk_widget_class_bind_template_callback( widget_class, whiteboard_close_cb); gtk_widget_class_bind_template_callback( widget_class, pidgin_whiteboard_draw_event); gtk_widget_class_bind_template_callback( widget_class, pidgin_whiteboard_configure_event); gtk_widget_class_bind_template_callback( widget_class, pidgin_whiteboard_brush_down); gtk_widget_class_bind_template_callback( widget_class, pidgin_whiteboard_brush_motion); gtk_widget_class_bind_template_callback( widget_class, pidgin_whiteboard_brush_up); gtk_widget_class_bind_template_callback( widget_class, pidgin_whiteboard_button_clear_press); gtk_widget_class_bind_template_callback( widget_class, pidgin_whiteboard_button_save_press); gtk_widget_class_bind_template_callback( widget_class, color_selected); /****************************************************************************** *****************************************************************************/ static PurpleWhiteboardUiOps ui_ops = pidgin_whiteboard_create, pidgin_whiteboard_destroy, pidgin_whiteboard_set_dimensions, pidgin_whiteboard_set_brush, pidgin_whiteboard_draw_brush_point, pidgin_whiteboard_draw_brush_line, pidgin_whiteboard_get_ui_ops(void)