Mon, 11 Dec 2006 05:29:50 +0000
[gaim-migrate @ 17955]
And the gtk plugins from the plugin pack. I've waited on listhandler until rekkanoryo (John Bailey) quits being indecisive...
| 15231 | 1 | /* |
| 2 | * Markerline - Draw a line to indicate new messages in a conversation. | |
| 3 | * Copyright (C) 2006 | |
| 4 | * | |
| 5 | * This program is free software; you can redistribute it and/or | |
| 6 | * modify it under the terms of the GNU General Public License as | |
| 7 | * published by the Free Software Foundation; either version 2 of the | |
| 8 | * License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This program is distributed in the hope that it will be useful, but | |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with this program; if not, write to the Free Software | |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
| 18 | * 02111-1307, USA. | |
| 19 | */ | |
| 20 | #include "internal.h" | |
| 21 | ||
| 22 | #define PLUGIN_ID "gtk-plugin_pack-markerline" | |
| 23 | #define PLUGIN_NAME "Markerline" | |
| 24 | #define PLUGIN_STATIC_NAME "Markerline" | |
| 25 | #define PLUGIN_SUMMARY "Draw a line to indicate new messages in a conversation." | |
| 26 | #define PLUGIN_DESCRIPTION "Draw a line to indicate new messages in a conversation." | |
| 27 | #define PLUGIN_AUTHOR "Sadrul H Chowdhury <sadrul@users.sourceforge.net>" | |
| 28 | ||
| 29 | /* System headers */ | |
| 30 | #include <gdk/gdk.h> | |
| 31 | #include <glib.h> | |
| 32 | #include <gtk/gtk.h> | |
| 33 | ||
| 34 | /* Gaim headers */ | |
| 35 | #include <gtkconv.h> | |
| 36 | #include <gtkimhtml.h> | |
| 37 | #include <gtkplugin.h> | |
| 38 | #include <version.h> | |
| 39 | ||
| 40 | #define PREF_PREFIX "/plugins/gtk/" PLUGIN_ID | |
| 41 | #define PREF_IMS PREF_PREFIX "/ims" | |
| 42 | #define PREF_CHATS PREF_PREFIX "/chats" | |
| 43 | ||
| 44 | static int | |
| 45 | imhtml_expose_cb(GtkWidget *widget, GdkEventExpose *event, GaimGtkConversation *gtkconv) | |
| 46 | { | |
| 47 | int y, last_y, offset; | |
| 48 | GdkRectangle visible_rect; | |
| 49 | GtkTextIter iter; | |
| 50 | GdkRectangle buf; | |
| 51 | int pad; | |
| 52 | GaimConversation *conv = gtkconv->active_conv; | |
| 53 | GaimConversationType type = gaim_conversation_get_type(conv); | |
| 54 | ||
| 55 | if ((type == GAIM_CONV_TYPE_CHAT && !gaim_prefs_get_bool(PREF_CHATS)) || | |
| 56 | (type == GAIM_CONV_TYPE_IM && !gaim_prefs_get_bool(PREF_IMS))) | |
| 57 | return FALSE; | |
| 58 | ||
| 59 | gtk_text_view_get_visible_rect(GTK_TEXT_VIEW(widget), &visible_rect); | |
| 60 | ||
| 61 | offset = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "markerline")); | |
| 62 | if (offset) | |
| 63 | { | |
| 64 | gtk_text_buffer_get_iter_at_offset(gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget)), | |
| 65 | &iter, offset); | |
| 66 | ||
| 67 | gtk_text_view_get_iter_location(GTK_TEXT_VIEW(widget), &iter, &buf); | |
| 68 | last_y = buf.y + buf.height; | |
| 69 | pad = (gtk_text_view_get_pixels_below_lines(GTK_TEXT_VIEW(widget)) + | |
| 70 | gtk_text_view_get_pixels_above_lines(GTK_TEXT_VIEW(widget))) / 2; | |
| 71 | last_y += pad; | |
| 72 | } | |
| 73 | else | |
| 74 | last_y = 0; | |
| 75 | ||
| 76 | gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT, | |
| 77 | 0, last_y, 0, &y); | |
| 78 | ||
| 79 | if (y >= event->area.y) | |
| 80 | { | |
| 81 | GdkColor red = {0, 0xffff, 0, 0}; | |
| 82 | GdkGC *gc = gdk_gc_new(GDK_DRAWABLE(event->window)); | |
| 83 | ||
| 84 | gdk_gc_set_rgb_fg_color(gc, &red); | |
| 85 | gdk_draw_line(event->window, gc, | |
| 86 | 0, y, visible_rect.width, y); | |
| 87 | gdk_gc_unref(gc); | |
| 88 | } | |
| 89 | return FALSE; | |
| 90 | } | |
| 91 | ||
| 92 | static void | |
| 93 | update_marker_for_gtkconv(GaimGtkConversation *gtkconv) | |
| 94 | { | |
| 95 | GtkTextIter iter; | |
| 96 | GtkTextBuffer *buffer; | |
| 97 | g_return_if_fail(gtkconv != NULL); | |
| 98 | ||
| 99 | buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(gtkconv->imhtml)); | |
| 100 | ||
| 101 | if (!gtk_text_buffer_get_char_count(buffer)) | |
| 102 | return; | |
| 103 | ||
| 104 | gtk_text_buffer_get_end_iter(buffer, &iter); | |
| 105 | ||
| 106 | g_object_set_data(G_OBJECT(gtkconv->imhtml), "markerline", | |
| 107 | GINT_TO_POINTER(gtk_text_iter_get_offset(&iter))); | |
| 108 | gtk_widget_queue_draw(gtkconv->imhtml); | |
| 109 | } | |
| 110 | ||
| 111 | static gboolean | |
| 112 | focus_removed(GtkWidget *widget, GdkEventVisibility *event, GaimGtkWindow *win) | |
| 113 | { | |
| 114 | GaimConversation *conv; | |
| 115 | GaimGtkConversation *gtkconv; | |
| 116 | ||
| 117 | conv = gaim_gtk_conv_window_get_active_conversation(win); | |
| 118 | g_return_val_if_fail(conv != NULL, FALSE); | |
| 119 | ||
| 120 | gtkconv = GAIM_GTK_CONVERSATION(conv); | |
| 121 | update_marker_for_gtkconv(gtkconv); | |
| 122 | ||
| 123 | return FALSE; | |
| 124 | } | |
| 125 | ||
| 126 | #if 0 | |
| 127 | static gboolean | |
| 128 | window_resized(GtkWidget *w, GdkEventConfigure *event, GaimGtkWindow *win) | |
| 129 | { | |
| 130 | GList *list; | |
| 131 | ||
| 132 | list = gaim_gtk_conv_window_get_gtkconvs(win); | |
| 133 | ||
| 134 | for (; list; list = list->next) | |
| 135 | update_marker_for_gtkconv(list->data); | |
| 136 | ||
| 137 | return FALSE; | |
| 138 | } | |
| 139 | ||
| 140 | static gboolean | |
| 141 | imhtml_resize_cb(GtkWidget *w, GtkAllocation *allocation, GaimGtkConversation *gtkconv) | |
| 142 | { | |
| 143 | gtk_widget_queue_draw(w); | |
| 144 | return FALSE; | |
| 145 | } | |
| 146 | #endif | |
| 147 | ||
| 148 | static void | |
| 149 | page_switched(GtkWidget *widget, GtkWidget *page, gint num, GaimGtkWindow *win) | |
| 150 | { | |
| 151 | focus_removed(NULL, NULL, win); | |
| 152 | } | |
| 153 | ||
| 154 | static void | |
| 155 | detach_from_gtkconv(GaimGtkConversation *gtkconv, gpointer null) | |
| 156 | { | |
| 157 | g_signal_handlers_disconnect_by_func(G_OBJECT(gtkconv->imhtml), imhtml_expose_cb, gtkconv); | |
| 158 | } | |
| 159 | ||
| 160 | static void | |
| 161 | detach_from_gaim_gtk_window(GaimGtkWindow *win, gpointer null) | |
| 162 | { | |
| 163 | g_list_foreach(gaim_gtk_conv_window_get_gtkconvs(win), (GFunc)detach_from_gtkconv, NULL); | |
| 164 | g_signal_handlers_disconnect_by_func(G_OBJECT(win->notebook), page_switched, win); | |
| 165 | g_signal_handlers_disconnect_by_func(G_OBJECT(win->window), focus_removed, win); | |
| 166 | ||
| 167 | gtk_widget_queue_draw(win->window); | |
| 168 | } | |
| 169 | ||
| 170 | static void | |
| 171 | attach_to_gtkconv(GaimGtkConversation *gtkconv, gpointer null) | |
| 172 | { | |
| 173 | detach_from_gtkconv(gtkconv, NULL); | |
| 174 | g_signal_connect(G_OBJECT(gtkconv->imhtml), "expose_event", | |
| 175 | G_CALLBACK(imhtml_expose_cb), gtkconv); | |
| 176 | } | |
| 177 | ||
| 178 | static void | |
| 179 | attach_to_gaim_gtk_window(GaimGtkWindow *win, gpointer null) | |
| 180 | { | |
| 181 | g_list_foreach(gaim_gtk_conv_window_get_gtkconvs(win), (GFunc)attach_to_gtkconv, NULL); | |
| 182 | ||
| 183 | g_signal_connect(G_OBJECT(win->window), "focus_out_event", | |
| 184 | G_CALLBACK(focus_removed), win); | |
| 185 | ||
| 186 | g_signal_connect(G_OBJECT(win->notebook), "switch_page", | |
| 187 | G_CALLBACK(page_switched), win); | |
| 188 | ||
| 189 | gtk_widget_queue_draw(win->window); | |
| 190 | } | |
| 191 | ||
| 192 | static void | |
| 193 | detach_from_all_windows() | |
| 194 | { | |
| 195 | g_list_foreach(gaim_gtk_conv_windows_get_list(), (GFunc)detach_from_gaim_gtk_window, NULL); | |
| 196 | } | |
| 197 | ||
| 198 | static void | |
| 199 | attach_to_all_windows() | |
| 200 | { | |
| 201 | g_list_foreach(gaim_gtk_conv_windows_get_list(), (GFunc)attach_to_gaim_gtk_window, NULL); | |
| 202 | } | |
| 203 | ||
| 204 | static void | |
| 205 | conv_created(GaimConversation *conv, gpointer null) | |
| 206 | { | |
| 207 | GaimGtkConversation *gtkconv = GAIM_GTK_CONVERSATION(conv); | |
| 208 | GaimGtkWindow *win; | |
| 209 | ||
| 210 | if (!gtkconv) | |
| 211 | return; | |
| 212 | ||
| 213 | win = gaim_gtkconv_get_window(gtkconv); | |
| 214 | ||
| 215 | detach_from_gaim_gtk_window(win, NULL); | |
| 216 | attach_to_gaim_gtk_window(win, NULL); | |
| 217 | } | |
| 218 | ||
| 219 | static gboolean | |
| 220 | plugin_load(GaimPlugin *plugin) | |
| 221 | { | |
| 222 | attach_to_all_windows(); | |
| 223 | ||
| 224 | gaim_signal_connect(gaim_conversations_get_handle(), "conversation-created", | |
| 225 | plugin, GAIM_CALLBACK(conv_created), NULL); | |
| 226 | ||
| 227 | return TRUE; | |
| 228 | } | |
| 229 | ||
| 230 | static gboolean | |
| 231 | plugin_unload(GaimPlugin *plugin) | |
| 232 | { | |
| 233 | detach_from_all_windows(); | |
| 234 | ||
| 235 | return TRUE; | |
| 236 | } | |
| 237 | ||
| 238 | static GaimPluginPrefFrame * | |
| 239 | get_plugin_pref_frame(GaimPlugin *plugin) | |
| 240 | { | |
| 241 | GaimPluginPrefFrame *frame; | |
| 242 | GaimPluginPref *pref; | |
| 243 | ||
| 244 | frame = gaim_plugin_pref_frame_new(); | |
| 245 | ||
| 246 | pref = gaim_plugin_pref_new_with_label(_("Draw Markerline in ")); | |
| 247 | gaim_plugin_pref_frame_add(frame, pref); | |
| 248 | ||
| 249 | pref = gaim_plugin_pref_new_with_name_and_label(PREF_IMS, | |
| 250 | _("_IM windows")); | |
| 251 | gaim_plugin_pref_frame_add(frame, pref); | |
| 252 | ||
| 253 | pref = gaim_plugin_pref_new_with_name_and_label(PREF_CHATS, | |
| 254 | _("C_hat windows")); | |
| 255 | gaim_plugin_pref_frame_add(frame, pref); | |
| 256 | ||
| 257 | return frame; | |
| 258 | } | |
| 259 | ||
| 260 | static GaimPluginUiInfo prefs_info = { | |
| 261 | get_plugin_pref_frame, | |
| 262 | 0, | |
| 263 | NULL, | |
| 264 | }; | |
| 265 | ||
| 266 | static GaimPluginInfo info = { | |
| 267 | GAIM_PLUGIN_MAGIC, /* Magic */ | |
| 268 | GAIM_MAJOR_VERSION, /* Gaim Major Version */ | |
| 269 | GAIM_MINOR_VERSION, /* Gaim Minor Version */ | |
| 270 | GAIM_PLUGIN_STANDARD, /* plugin type */ | |
| 271 | GAIM_GTK_PLUGIN_TYPE, /* ui requirement */ | |
| 272 | 0, /* flags */ | |
| 273 | NULL, /* dependencies */ | |
| 274 | GAIM_PRIORITY_DEFAULT, /* priority */ | |
| 275 | ||
| 276 | PLUGIN_ID, /* plugin id */ | |
| 277 | N_(PLUGIN_NAME), /* name */ | |
| 278 | VERSION, /* version */ | |
| 279 | N_(PLUGIN_SUMMARY), /* summary */ | |
| 280 | N_(PLUGIN_DESCRIPTION), /* description */ | |
| 281 | PLUGIN_AUTHOR, /* author */ | |
| 282 | GAIM_WEBSITE, /* website */ | |
| 283 | ||
| 284 | plugin_load, /* load */ | |
| 285 | plugin_unload, /* unload */ | |
| 286 | NULL, /* destroy */ | |
| 287 | ||
| 288 | NULL, /* ui_info */ | |
| 289 | NULL, /* extra_info */ | |
| 290 | &prefs_info, /* prefs_info */ | |
| 291 | NULL /* actions */ | |
| 292 | }; | |
| 293 | ||
| 294 | static void | |
| 295 | init_plugin(GaimPlugin *plugin) | |
| 296 | { | |
| 297 | gaim_prefs_add_none(PREF_PREFIX); | |
| 298 | gaim_prefs_add_bool(PREF_IMS, FALSE); | |
| 299 | gaim_prefs_add_bool(PREF_CHATS, TRUE); | |
| 300 | } | |
| 301 | ||
| 302 | GAIM_INIT_PLUGIN(PLUGIN_STATIC_NAME, init_plugin, info) |