forked from EionRobb/pidgin-win7
/
pidgin-win7.c
executable file
·1486 lines (1272 loc) · 42.9 KB
/
pidgin-win7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*/
#include "pidgin-win7.h"
static gboolean pixbuf_to_hbitmaps_normal(GdkPixbuf *pixbuf, HBITMAP *color, HBITMAP *mask);
static gboolean pixbuf_to_hbitmaps_alpha_winxp(GdkPixbuf *pixbuf, HBITMAP *color, HBITMAP *mask);
static HICON pixbuf_to_hicon (GdkPixbuf *pixbuf);
static HBITMAP create_alpha_bitmap (gint width, gint height, guchar **outdata);
// PidginConversation -> HWND map
static GHashTable *win7_pconv_hwnd = NULL;
// HWND -> PidginConversation map
static GHashTable *win7_hwnd_pconv = NULL;
HWND
win7_get_hwnd_from_pconv(PidginConversation *gtkconv)
{
return g_hash_table_lookup(win7_pconv_hwnd, gtkconv);
}
PidginConversation *
win7_get_pconv_from_hwnd(HWND hwnd)
{
return g_hash_table_lookup(win7_hwnd_pconv, hwnd);
}
HWND
win7_get_hwnd_from_conv(PurpleConversation *conv)
{
PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
return win7_get_hwnd_from_pconv(gtkconv);
}
PurpleConversation *
win7_get_conv_from_hwnd(HWND hwnd)
{
PidginConversation *gtkconv = win7_get_pconv_from_hwnd(hwnd);
return gtkconv->active_conv;
}
void
win7_jumplist_pref_cb(const gchar *name, PurplePrefType type, gconstpointer val, gpointer data)
{
if (val)
win7_enable_jumplist(data);
else
win7_disable_jumplist(data);
}
void
win7_overlay_pref_cb(const gchar *name, PurplePrefType type, gconstpointer val, gpointer data)
{
if (val)
win7_enable_overlay(data);
else
win7_disable_overlay(data);
}
void
win7_convwin_pref_cb(const gchar *name, PurplePrefType type, gconstpointer val, gpointer data)
{
if (val)
win7_enable_convwin(data);
else
win7_disable_convwin(data);
}
void
win7_tabthumb_each_cb(gpointer key, gpointer value, gpointer user_data)
{
BOOL fForceIconic;
BOOL fHasIconicBitmap;
HWND hTab = value;
if (purple_prefs_get_bool(PREF_TAB_THUMBS))
{
fForceIconic = fHasIconicBitmap = TRUE;
} else {
fForceIconic = fHasIconicBitmap = FALSE;
}
DwmSetWindowAttribute(hTab, DWMWA_FORCE_ICONIC_REPRESENTATION,
&fForceIconic, sizeof(fForceIconic));
DwmSetWindowAttribute(hTab, DWMWA_HAS_ICONIC_BITMAP, &fHasIconicBitmap,
sizeof(fHasIconicBitmap));
}
void
win7_tabthumb_pref_cb(const gchar *name, PurplePrefType type, gconstpointer val, gpointer data)
{
g_hash_table_foreach(win7_pconv_hwnd, win7_tabthumb_each_cb, data);
}
void
win7_progress_pref_cb(const gchar *name, PurplePrefType type, gconstpointer val, gpointer data)
{
if (val)
win7_enable_progress(data);
else
win7_disable_progress(data);
}
gboolean
win7_enable_jumplist(PidginWin7Store *store)
{
if (store->pcdl ||
SUCCEEDED(CoCreateInstance(&CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER, &IID_ICustomDestinationList, (void**)&store->pcdl)))
{
pidgin_win7_create_jumplist(store->pcdl);
purple_signal_connect(purple_get_core(), "uri-handler", this_plugin, PURPLE_CALLBACK(uri_handler), NULL);
return TRUE;
} else {
return FALSE;
}
}
void
win7_disable_jumplist(PidginWin7Store *store)
{
if (store && store->pcdl)
{
pidgin_win7_delete_jumplist(store->pcdl);
store->pcdl = NULL;
}
purple_signal_disconnect(purple_get_core(), "uri-handler", this_plugin, PURPLE_CALLBACK(uri_handler));
}
gboolean
win7_enable_progress(PidginWin7Store *store)
{
if (store->itl ||
SUCCEEDED(CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_ALL, &IID_ITaskbarList3, (void**)&store->itl)))
{
void *ft_handle = purple_xfers_get_handle();
purple_signal_connect(ft_handle, "file-recv-accept", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-recv-start", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-recv-cancel", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-recv-complete", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-recv-request", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-send-accept", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-send-start", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-send-cancel", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
purple_signal_connect(ft_handle, "file-send-complete", this_plugin, PURPLE_CALLBACK(ft_update), store->itl);
return TRUE;
} else {
return FALSE;
}
}
void
win7_disable_progress(PidginWin7Store *store)
{
void *ft_handle = purple_xfers_get_handle();
purple_signal_disconnect(ft_handle, "file-recv-accept", this_plugin, PURPLE_CALLBACK(ft_update));
purple_signal_disconnect(ft_handle, "file-recv-start", this_plugin, PURPLE_CALLBACK(ft_update));
purple_signal_disconnect(ft_handle, "file-recv-cancel", this_plugin, PURPLE_CALLBACK(ft_update));
purple_signal_disconnect(ft_handle, "file-recv-complete", this_plugin, PURPLE_CALLBACK(ft_update));
purple_signal_disconnect(ft_handle, "file-send-accept", this_plugin, PURPLE_CALLBACK(ft_update));
purple_signal_disconnect(ft_handle, "file-send-start", this_plugin, PURPLE_CALLBACK(ft_update));
purple_signal_disconnect(ft_handle, "file-send-cancel", this_plugin, PURPLE_CALLBACK(ft_update));
purple_signal_disconnect(ft_handle, "file-send-complete", this_plugin, PURPLE_CALLBACK(ft_update));
}
gboolean
win7_enable_overlay(PidginWin7Store *store)
{
if (store->itl ||
SUCCEEDED(CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_ALL, &IID_ITaskbarList3, (void**)&store->itl)))
{
// Hijack the pidgin_blist_set_visible function
PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
pidgin_old_set_visible = ops->set_visible;
ops->set_visible = win7_blist_set_visible;
PidginBuddyList *blist = pidgin_blist_get_default_gtk_blist();
// The buddy list MUST be shown before we put the taskbar icon on it
purple_blist_show();
purple_blist_set_visible(TRUE);
if (blist && blist->window)
store->blist_destroy_handler_id = g_signal_connect(G_OBJECT(blist->window), "delete_event", G_CALLBACK(blist_delete_event_cb), NULL);
purple_signal_connect(purple_savedstatuses_get_handle(), "savedstatus-changed", this_plugin, PURPLE_CALLBACK(win7_on_status_change), store);
pidgin_blist_visibility_manager_add();
if (blist && blist->window)
{
// Hijack the signal when the window is closed from pidgin
guint signal_id = g_signal_lookup("delete_event", GTK_TYPE_WINDOW);
store->pidgin_destroy_handler_id = g_signal_handler_find(blist->window, G_SIGNAL_MATCH_ID,
signal_id, 0, NULL, NULL, NULL);
g_signal_handler_block(blist->window, store->pidgin_destroy_handler_id);
}
// Update the icon in the taskbar
win7_on_status_change(purple_savedstatus_get_current(), NULL, store);
return TRUE;
} else {
return FALSE;
}
}
void
win7_disable_overlay(PidginWin7Store *store)
{
PidginBuddyList *blist = pidgin_blist_get_default_gtk_blist();
if (blist && blist->window)
{
//Unset the taskbar icon
win7_on_status_change(NULL, NULL, store);
if (g_signal_handler_is_connected(G_OBJECT(blist->window), store->blist_destroy_handler_id))
{
g_signal_handler_disconnect(G_OBJECT(blist->window), store->blist_destroy_handler_id);
}
}
g_signal_handler_unblock(blist->window, store->pidgin_destroy_handler_id);
purple_signal_disconnect(purple_savedstatuses_get_handle(), "savedstatus-changed", this_plugin, PURPLE_CALLBACK(win7_on_status_change));
// Un-hijack the pidgin_blist_set_visible function
PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
if (ops->set_visible == win7_blist_set_visible)
{
ops->set_visible = pidgin_old_set_visible;
pidgin_blist_visibility_manager_remove();
}
}
gboolean
win7_enable_convwin(PidginWin7Store *store)
{
if (store->itl ||
SUCCEEDED(CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_ALL, &IID_ITaskbarList3, (void**)&store->itl)))
{
win7_init_conv_windows(store->itl);
// Connect to the signals for creating, deleting, focusing conversations
purple_signal_connect(pidgin_conversations_get_handle(), "conversation-switched", this_plugin, PURPLE_CALLBACK(on_conv_switch), store->itl);
purple_signal_connect(pidgin_conversations_get_handle(), "conversation-hiding", this_plugin, PURPLE_CALLBACK(on_conv_hide), store->itl);
purple_signal_connect(purple_conversations_get_handle(), "deleting-conversation", this_plugin, PURPLE_CALLBACK(on_conv_delete), store->itl);
purple_signal_connect(purple_conversations_get_handle(), "conversation-created", this_plugin, PURPLE_CALLBACK(on_conv_create), store->itl);
purple_signal_connect(purple_conversations_get_handle(), "conversation-updated", this_plugin, PURPLE_CALLBACK(win7_update_icon), NULL);
return TRUE;
} else {
return FALSE;
}
}
void
win7_disable_convwin(PidginWin7Store *store)
{
purple_signal_disconnect(pidgin_conversations_get_handle(), "conversation-switched", this_plugin, PURPLE_CALLBACK(on_conv_switch));
purple_signal_disconnect(pidgin_conversations_get_handle(), "conversation-hiding", this_plugin, PURPLE_CALLBACK(on_conv_hide));
purple_signal_disconnect(purple_conversations_get_handle(), "deleting-conversation", this_plugin, PURPLE_CALLBACK(on_conv_delete));
purple_signal_disconnect(purple_conversations_get_handle(), "conversation-created", this_plugin, PURPLE_CALLBACK(on_conv_create));
purple_signal_disconnect(purple_conversations_get_handle(), "conversation-updated", this_plugin, PURPLE_CALLBACK(win7_update_icon));
if (store && store->itl)
{
win7_destroy_conv_windows(store->itl);
}
}
static void
win7_blist_set_visible(PurpleBuddyList *list, gboolean show)
{
PidginBuddyList *gtkblist = pidgin_blist_get_default_gtk_blist();
if (!(gtkblist && gtkblist->window))
return;
if (show) {
pidgin_old_set_visible(list, show);
} else {
if (!GTK_WIDGET_VISIBLE(gtkblist->window))
gtk_widget_show(gtkblist->window);
gtk_window_iconify(GTK_WINDOW(gtkblist->window));
}
}
static gboolean
live_conv_cb(gpointer user_data)
{
PurpleConversation *conv = user_data;
PidginConversation *pconv = PIDGIN_CONVERSATION(conv);
gint width, height;
HBITMAP hbitmap, mask;
GdkPixbuf *pixbuf = NULL;
HWND hwnd = g_hash_table_lookup(win7_pconv_hwnd, pconv);
gtk_window_get_size(GTK_WINDOW(pconv->win->window), &width, &height);
// Make a 32bit transparent pixbuf
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
pixbuf = gdk_pixbuf_get_from_drawable(pixbuf, GDK_DRAWABLE(pconv->win->window->window), NULL, 0, 0, 0, 0, width, height);
POINT offset = { 0, 0 };
pixbuf_to_hbitmaps_alpha_winxp(pixbuf, &hbitmap, &mask);
//DwmSetIconicLivePreviewBitmap(hwnd, hbitmap, &offset, DWM_SIT_DISPLAYFRAME);
DwmSetIconicLivePreviewBitmap(hwnd, hbitmap, &offset, 0);
DeleteObject(mask);
DeleteObject(hbitmap);
g_object_unref(pixbuf);
return FALSE;
}
/* From pidginsnarl */
static void
showConversation(PurpleConversation *conv){
if(conv){
PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
pidgin_conv_switch_active_conversation(conv);
pidgin_conv_present_conversation(conv);
if(gtkconv != NULL && gtkconv->win != NULL){
pidgin_conv_window_switch_gtkconv(gtkconv->win, gtkconv);
gtk_window_present(GTK_WINDOW(gtkconv->win->window));
pidgin_conv_window_raise(gtkconv->win);
}
}
}
LRESULT CALLBACK
win7_conv_handler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
PurpleConversation *conv;
PidginConversation *pconv;
HBITMAP hbitmap, mask;
GdkPixbuf *pixbuf = NULL;
PidginWin7Store *store = (PidginWin7Store *)this_plugin->extra;
pconv = win7_get_pconv_from_hwnd(hwnd);
if (!pconv)
return DefWindowProcW(hwnd, msg, wparam, lparam);
conv = pconv->active_conv;
switch(msg)
{
case WM_CREATE:
{
// Create taskbar buttons?
//TODO We dont have thumbnail buttons yet
} break;
case WM_CLOSE:
{
// Handle the thumbnail close button
purple_conversation_destroy(conv);
return 0;
} break;
case WM_ACTIVATE:
{
// Handle the thumbnail being clicked on
if (LOWORD(wparam))
{
showConversation(conv);
return 0;
}
} break;
case WM_COMMAND:
{
// Handle one of the buttons on the thumbnail being pressed
//TODO We dont have thumbnail buttons yet
} break;
case WM_DWMSENDICONICTHUMBNAIL:
{
// Thumbnail needs to be drawn
gconstpointer data = NULL;
PurpleStoredImage *custom_img = NULL;
size_t len;
GdkPixbuf *temp_pixbuf = NULL;
/*PurpleAccount *account = purple_conversation_get_account(conv);
PurpleBuddy *buddy = purple_find_buddy(account, purple_conversation_get_name(conv));
if (buddy)
{
PurpleContact *contact = purple_buddy_get_contact(buddy);
if (contact)
{
custom_img = purple_buddy_icons_node_find_custom_icon((PurpleBlistNode*)contact);
if (custom_img)
{
data = purple_imgstore_get_data(custom_img);
len = purple_imgstore_get_size(custom_img);
}
}
}*/
if (data == NULL)
{
PurpleBuddyIcon *icon = purple_conv_im_get_icon(PURPLE_CONV_IM(conv));
if (icon)
{
data = purple_buddy_icon_get_data(icon, &len);
}
}
if (data == NULL)
{
pixbuf = pidgin_conv_get_tab_icon(conv, FALSE);
}
if (data && len && !pixbuf)
{
GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, data, len, NULL);
gdk_pixbuf_loader_close(loader, NULL);
purple_imgstore_unref(custom_img);
pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
}
if (pixbuf)
{
gint original_width = gdk_pixbuf_get_width (pixbuf);
gint original_height = gdk_pixbuf_get_height (pixbuf);
gfloat ratio = original_width / (gfloat) original_height;
gint maxwidth = HIWORD(lparam);
gint maxheight = LOWORD(lparam);
gint newwidth, newheight;
newheight = maxheight;
newwidth = maxheight * ratio;
if (newwidth > maxwidth)
{
newwidth = maxwidth;
newheight = maxwidth * (1/ratio);
}
temp_pixbuf = gdk_pixbuf_scale_simple(pixbuf, newwidth, newheight, GDK_INTERP_BILINEAR);
g_object_unref(pixbuf);
pixbuf = temp_pixbuf;
if (!gdk_pixbuf_get_has_alpha(pixbuf))
{
temp_pixbuf = gdk_pixbuf_add_alpha(pixbuf, FALSE, 0, 0, 0);
g_object_unref(pixbuf);
pixbuf = temp_pixbuf;
}
pixbuf_to_hbitmaps_alpha_winxp(pixbuf, &hbitmap, &mask);
DwmSetIconicThumbnail(hwnd, hbitmap, 1);
DeleteObject(mask);
DeleteObject(hbitmap);
g_object_unref(pixbuf);
return 0;
}
} break;
case WM_DWMSENDICONICLIVEPREVIEWBITMAP:
{
// Preview needs to be drawn
//TODO block pidgin events from handling tab change
guint signal_id = g_signal_lookup("switch_page", GTK_TYPE_NOTEBOOK);
if (!store->pidgin_before_switch_conv_handler_id)
{
store->pidgin_before_switch_conv_handler_id =
g_signal_handler_find(pconv->win->notebook, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_UNBLOCKED | G_SIGNAL_MATCH_DATA,
signal_id, 0, NULL, NULL, pconv->win);
}
g_signal_handler_block(pconv->win->notebook, store->pidgin_before_switch_conv_handler_id);
if (!store->pidgin_after_switch_conv_handler_id)
{
/*store->pidgin_after_switch_conv_handler_id =
g_signal_handler_find(pconv->win->notebook, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_UNBLOCKED | G_SIGNAL_MATCH_DATA,
signal_id, 0, NULL, NULL, pconv->win);*/
store->pidgin_after_switch_conv_handler_id = store->pidgin_before_switch_conv_handler_id + 1;
}
g_signal_handler_block(pconv->win->notebook, store->pidgin_after_switch_conv_handler_id);
/*if (!store->pidgin_third_switch_conv_handler_id)
{
store->pidgin_third_switch_conv_handler_id =
g_signal_handler_find(pconv->win->notebook, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_UNBLOCKED | G_SIGNAL_MATCH_DATA,
signal_id, 0, NULL, NULL, pconv->win);
}
g_signal_handler_block(pconv->win->notebook, store->pidgin_third_switch_conv_handler_id);*/
/*gulong next_signal_handler = 0;
while ((next_signal_handler = g_signal_handler_find(pconv->win->notebook, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_UNBLOCKED, signal_id, 0, NULL, NULL, NULL)))
{
g_signal_handler_block(pconv->win->notebook, next_signal_handler);
purple_debug_info("win7", "blocked: %u\n", next_signal_handler);
}*/
// Switch to the right conversation, then take a screenshot
pidgin_conv_window_switch_gtkconv(pconv->win, pconv);
gtk_window_present(GTK_WINDOW(pconv->win->window));
g_signal_handler_unblock(pconv->win->notebook, store->pidgin_before_switch_conv_handler_id);
g_signal_handler_unblock(pconv->win->notebook, store->pidgin_after_switch_conv_handler_id);
// Tab updates could take up to 22ms
purple_timeout_add(30, (GSourceFunc)live_conv_cb, conv);
return 0;
} break;
}
return DefWindowProcW(hwnd, msg, wparam, lparam);
}
static void
win7_update_icon(PurpleConversation *conv, PurpleConvUpdateType type, gpointer user_data)
{
HWND hTab;
GdkPixbuf *icon;
FLASHWINFO info;
hTab = win7_get_hwnd_from_conv(conv);
if (hTab)
{
switch(type)
{
case PURPLE_CONV_UPDATE_ADD:
case PURPLE_CONV_UPDATE_REMOVE:
case PURPLE_CONV_UPDATE_ACCOUNT:
case PURPLE_CONV_UPDATE_TOPIC:
case PURPLE_CONV_ACCOUNT_ONLINE:
case PURPLE_CONV_ACCOUNT_OFFLINE:
case PURPLE_CONV_UPDATE_AWAY:
case PURPLE_CONV_UPDATE_ICON:
case PURPLE_CONV_UPDATE_TITLE:
{
// Set the icon for the 'window'
icon = pidgin_conv_get_tab_icon(conv, FALSE);
if (icon)
{
SendMessage(hTab, WM_SETICON, ICON_SMALL, (LPARAM)pixbuf_to_hicon(icon));
g_object_unref(icon);
}
wchar_t *wlabel = g_utf8_to_utf16(purple_conversation_get_title(conv), -1, NULL, NULL, NULL);
// Set the title for the 'window'
//SendMessageW(hTab, WM_SETTEXT, 0, (LPARAM)wlabel);
SetWindowTextW(hTab, wlabel);
g_free(wlabel);
// Invalidate the thumbnail preview
DwmInvalidateIconicBitmaps(hTab);
} break;
case PURPLE_CONV_UPDATE_UNSEEN:
{
// Flash the conversation 'window'
memset(&info, 0, sizeof(FLASHWINFO));
info.cbSize = sizeof(FLASHWINFO);
info.hwnd = hTab;
if (purple_conversation_get_data(conv, "unseen-count"))
{
info.uCount = 1;
info.dwFlags = FLASHW_ALL | FLASHW_TIMER;
} else {
info.dwFlags = FLASHW_STOP;
}
FlashWindowEx(&info);
info.dwTimeout = 0;
} break;
default:
break;
}
}
}
/* Create hidden window to process convtab messages */
static HWND
win7_create_hiddenwin(PidginConversation *pconv)
{
HWND hTab;
DWORD lasterror;
BOOL fForceIconic = TRUE;
BOOL fHasIconicBitmap = TRUE;
LPCWSTR wname = L"WinpidginConvThumbCls";
wchar_t *wlabel;
/* Create the window */
wlabel = g_utf8_to_utf16(purple_conversation_get_title(pconv->active_conv), -1, NULL, NULL, NULL);
hTab = CreateWindowW(wname, wlabel, 0, 0, 0, 0, 0, GetDesktopWindow(), NULL, winpidgin_exe_hinstance(), 0);
g_free(wlabel);
lasterror = GetLastError();
if (lasterror)
purple_debug_error("win7", "CreateWindow error %d\n", lasterror);
win7_update_icon(pconv->active_conv, PURPLE_CONV_UPDATE_ICON, NULL);
if (purple_prefs_get_bool(PREF_TAB_THUMBS))
{
DwmSetWindowAttribute(hTab, DWMWA_FORCE_ICONIC_REPRESENTATION,
&fForceIconic, sizeof(fForceIconic));
DwmSetWindowAttribute(hTab, DWMWA_HAS_ICONIC_BITMAP, &fHasIconicBitmap,
sizeof(fHasIconicBitmap));
}
g_hash_table_insert(win7_pconv_hwnd, pconv, hTab);
g_hash_table_insert(win7_hwnd_pconv, hTab, pconv);
return hTab;
}
static void
win7_destroy_hiddenwin(PurpleConversation *conv)
{
HWND hTab;
PidginConversation *pconv;
hTab = win7_get_hwnd_from_conv(conv);
if(hTab)
{
DestroyWindow(hTab);
g_hash_table_remove(win7_hwnd_pconv, hTab);
}
pconv = PIDGIN_CONVERSATION(conv);
g_hash_table_remove(win7_pconv_hwnd, pconv);
}
static void
win7_init_conv_windows(ITaskbarList3 *itl)
{
WNDCLASSEXW wcex;
GList *convs;
PurpleConversation *conv;
LPCWSTR wname = L"WinpidginConvThumbCls";
DWORD lasterror;
ATOM thumbcls;
if (win7_hwnd_pconv == NULL)
win7_hwnd_pconv = g_hash_table_new(NULL, NULL);
if (win7_pconv_hwnd == NULL)
win7_pconv_hwnd = g_hash_table_new(NULL, NULL);
wcex.cbSize = sizeof(wcex);
wcex.style = 0;
wcex.lpfnWndProc = (WNDPROC) win7_conv_handler; //DefWindowProcW;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = winpidgin_exe_hinstance();
wcex.hIcon = NULL;
wcex.hCursor = NULL,
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = wname;
wcex.hIconSm = NULL;
thumbcls = RegisterClassExW(&wcex);
lasterror = GetLastError();
if (lasterror)
purple_debug_error("win7", "RegisterClassEx error %d\n", lasterror);
//loop over all conversations
convs = purple_get_conversations();
for(; convs; convs = g_list_next(convs))
{
//apply the on_conv_create to them
conv = (PurpleConversation *) convs->data;
on_conv_create(conv, itl);
}
}
static void
win7_destroy_conv_windows(ITaskbarList3 *itl)
{
GList *convs;
PurpleConversation *conv;
LPCWSTR wname = L"WinpidginConvThumbCls";
//loop over all conversations
convs = purple_get_conversations();
for(; convs; convs = g_list_next(convs))
{
//apply the on_conv_delete to them
conv = (PurpleConversation *) convs->data;
on_conv_delete(conv, itl);
}
UnregisterClassW(wname, NULL);
}
static void
on_conv_switch(PurpleConversation *conv, gpointer user_data)
{
ITaskbarList3 *itl = (ITaskbarList3 *)user_data;
PidginConversation *pconv = PIDGIN_CONVERSATION(conv);
HWND hWin = GDK_WINDOW_HWND(gtk_widget_get_window(pconv->win->window));
HWND hTab = win7_get_hwnd_from_pconv(pconv);
if (!hTab)
hTab = win7_create_hiddenwin(pconv);
itl->lpVtbl->SetTabActive(itl, hTab, hWin, 0);
}
static void
on_conv_delete(PurpleConversation *conv, gpointer user_data)
{
ITaskbarList3 *itl = (ITaskbarList3 *)user_data;
HWND hTab = win7_get_hwnd_from_conv(conv);
if (hTab)
itl->lpVtbl->UnregisterTab(itl, hTab);
win7_destroy_hiddenwin(conv);
}
static void
on_conv_hide(PidginConversation *pconv, gpointer user_data)
{
on_conv_delete(pconv->active_conv, user_data);
}
static void
on_conv_create(PurpleConversation *conv, gpointer user_data)
{
const gchar *label;
wchar_t *wlabel;
ITaskbarList3 *itl = (ITaskbarList3 *)user_data;
PidginConversation *pconv = PIDGIN_CONVERSATION(conv);
HWND hWin = GDK_WINDOW_HWND(gtk_widget_get_window(pconv->win->window));
HWND hTab = win7_get_hwnd_from_pconv(pconv);
if (!hTab)
hTab = win7_create_hiddenwin(pconv);
itl->lpVtbl->RegisterTab(itl, hTab, hWin);
itl->lpVtbl->SetTabOrder(itl, hTab, NULL);
label = purple_conversation_get_title(conv);
wlabel = g_utf8_to_utf16(label, -1, NULL, NULL, NULL);
itl->lpVtbl->SetThumbnailTooltip(itl, hTab, wlabel);
g_free(wlabel);
}
static gboolean
blist_delete_event_cb(GtkWidget *w, GdkEvent *e, gpointer user_data)
{
// Prevent the window from being closed,
// minimise instead
gtk_window_iconify(GTK_WINDOW(w));
// return true to prevent the event from propegating
return TRUE;
}
/* This is all pinched from gtkdocklet-win32.c */
#define WIN32_GDI_FAILED(api) printf("GDI FAILED %s\n", api)
static HICON cached_icons[PURPLE_STATUS_NUM_PRIMITIVES + 2];
static GtkWidget *image = NULL;
static HBITMAP
create_alpha_bitmap (gint width, gint height,
guchar **outdata)
{
BITMAPV5HEADER bi;
HDC hdc;
HBITMAP hBitmap;
ZeroMemory (&bi, sizeof (BITMAPV5HEADER));
bi.bV5Size = sizeof (BITMAPV5HEADER);
bi.bV5Height = height;
bi.bV5Width = width;
bi.bV5Planes = 1;
bi.bV5BitCount = 32;
bi.bV5Compression = BI_BITFIELDS;
/* The following mask specification specifies a supported 32 BPP
* alpha format for Windows XP (BGRA format).
*/
bi.bV5RedMask = 0x00FF0000;
bi.bV5GreenMask = 0x0000FF00;
bi.bV5BlueMask = 0x000000FF;
bi.bV5AlphaMask = 0xFF000000;
/* Create the DIB section with an alpha channel. */
hdc = GetDC (NULL);
if (!hdc)
{
WIN32_GDI_FAILED ("GetDC");
return NULL;
}
hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS,
(PVOID *) outdata, NULL, (DWORD)0);
if (hBitmap == NULL)
WIN32_GDI_FAILED ("CreateDIBSection");
ReleaseDC (NULL, hdc);
return hBitmap;
}
static HBITMAP
create_color_bitmap (gint width, gint height,
guchar **outdata,
gint bits)
{
struct {
BITMAPV4HEADER bmiHeader;
RGBQUAD bmiColors[2];
} bmi;
HDC hdc;
HBITMAP hBitmap;
ZeroMemory (&bmi, sizeof (bmi));
bmi.bmiHeader.bV4Size = sizeof (BITMAPV4HEADER);
bmi.bmiHeader.bV4Height = height;
bmi.bmiHeader.bV4Width = width;
bmi.bmiHeader.bV4Planes = 1;
bmi.bmiHeader.bV4BitCount = bits;
bmi.bmiHeader.bV4V4Compression = BI_RGB;
/* when bits is 1, these will be used.
* bmiColors[0] already zeroed from ZeroMemory()
*/
bmi.bmiColors[1].rgbBlue = 0xFF;
bmi.bmiColors[1].rgbGreen = 0xFF;
bmi.bmiColors[1].rgbRed = 0xFF;
hdc = GetDC (NULL);
if (!hdc)
{
WIN32_GDI_FAILED ("GetDC");
return NULL;
}
hBitmap = CreateDIBSection (hdc, (BITMAPINFO *)&bmi, DIB_RGB_COLORS,
(PVOID *) outdata, NULL, (DWORD)0);
if (hBitmap == NULL)
WIN32_GDI_FAILED ("CreateDIBSection");
ReleaseDC (NULL, hdc);
return hBitmap;
}
static gboolean
pixbuf_to_hbitmaps_alpha_winxp (GdkPixbuf *pixbuf,
HBITMAP *color,
HBITMAP *mask)
{
/* Based on code from
* http://www.dotnet247.com/247reference/msgs/13/66301.aspx
*/
HBITMAP hColorBitmap, hMaskBitmap;
guchar *indata, *inrow;
guchar *colordata, *colorrow, *maskdata, *maskbyte;
gint width, height, i, i_offset, j, j_offset, rowstride;
guint maskstride, mask_bit;
width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
hColorBitmap = create_alpha_bitmap (width, height, &colordata);
if (!hColorBitmap)
return FALSE;
hMaskBitmap = create_color_bitmap (width, height, &maskdata, 1);
if (!hMaskBitmap)
{
DeleteObject (hColorBitmap);
return FALSE;
}
/* MSDN says mask rows are aligned to "LONG" boundaries */
maskstride = (((width + 31) & ~31) >> 3);
indata = gdk_pixbuf_get_pixels (pixbuf);
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
/*if (width > height)
{
i_offset = 0;
j_offset = (width - height) / 2;
}
else
{
i_offset = (height - width) / 2;
j_offset = 0;
}*/
i_offset = 0;
j_offset = 0;
for (j = 0; j < height; j++)
{
colorrow = colordata + 4*(j+j_offset)*width + 4*i_offset;
maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
mask_bit = (0x80 >> (i_offset % 8));
inrow = indata + (height-j-1)*rowstride;
for (i = 0; i < width; i++)
{
colorrow[4*i+0] = inrow[4*i+2];
colorrow[4*i+1] = inrow[4*i+1];
colorrow[4*i+2] = inrow[4*i+0];
colorrow[4*i+3] = inrow[4*i+3];
if (inrow[4*i+3] == 0)
maskbyte[0] |= mask_bit; /* turn ON bit */
else
maskbyte[0] &= ~mask_bit; /* turn OFF bit */
mask_bit >>= 1;
if (mask_bit == 0)
{
mask_bit = 0x80;
maskbyte++;
}
}
}
*color = hColorBitmap;
*mask = hMaskBitmap;
return TRUE;
}
static gboolean
pixbuf_to_hbitmaps_normal (GdkPixbuf *pixbuf,
HBITMAP *color,
HBITMAP *mask)
{
/* Based on code from
* http://www.dotnet247.com/247reference/msgs/13/66301.aspx
*/
HBITMAP hColorBitmap, hMaskBitmap;
guchar *indata, *inrow;
guchar *colordata, *colorrow, *maskdata, *maskbyte;
gint width, height, i, i_offset, j, j_offset, rowstride, nc, bmstride;
gboolean has_alpha;
guint maskstride, mask_bit;
width = gdk_pixbuf_get_width (pixbuf); /* width of icon */
height = gdk_pixbuf_get_height (pixbuf); /* height of icon */
hColorBitmap = create_color_bitmap (width, height, &colordata, 24);
if (!hColorBitmap)
return FALSE;
hMaskBitmap = create_color_bitmap (width, height, &maskdata, 1);
if (!hMaskBitmap)
{
DeleteObject (hColorBitmap);
return FALSE;
}
/* rows are always aligned on 4-byte boundarys */
bmstride = width * 3;
if (bmstride % 4 != 0)
bmstride += 4 - (bmstride % 4);
/* MSDN says mask rows are aligned to "LONG" boundaries */
maskstride = (((width + 31) & ~31) >> 3);
indata = gdk_pixbuf_get_pixels (pixbuf);
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
nc = gdk_pixbuf_get_n_channels (pixbuf);
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
/*if (width > height)
{
i_offset = 0;
j_offset = (width - height) / 2;
}
else
{
i_offset = (height - width) / 2;
j_offset = 0;
}*/
i_offset = 0;
j_offset = 0;
for (j = 0; j < height; j++)
{
colorrow = colordata + (j+j_offset)*bmstride + 3*i_offset;
maskbyte = maskdata + (j+j_offset)*maskstride + i_offset/8;
mask_bit = (0x80 >> (i_offset % 8));
inrow = indata + (height-j-1)*rowstride;
for (i = 0; i < width; i++)
{
if (has_alpha && inrow[nc*i+3] < 128)
{
colorrow[3*i+0] = colorrow[3*i+1] = colorrow[3*i+2] = 0;
maskbyte[0] |= mask_bit; /* turn ON bit */
}
else
{
colorrow[3*i+0] = inrow[nc*i+2];
colorrow[3*i+1] = inrow[nc*i+1];
colorrow[3*i+2] = inrow[nc*i+0];
maskbyte[0] &= ~mask_bit; /* turn OFF bit */
}
mask_bit >>= 1;
if (mask_bit == 0)
{
mask_bit = 0x80;
maskbyte++;
}
}
}
*color = hColorBitmap;
*mask = hMaskBitmap;