-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathURLView.cpp
More file actions
1147 lines (925 loc) · 32.3 KB
/
URLView.cpp
File metadata and controls
1147 lines (925 loc) · 32.3 KB
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
/* URLView 2.11
written by William Kakes of Tall Hill Software.
This class provides an underlined and clickable BStringView
that will launch the web browser, e-mail program, or FTP client
when clicked on. Other features include hover-highlighting,
right-click menus, and drag-and-drop support.
You are free to use URLView in your own programs (both open-source
and closed-source) free of charge, but a mention in your read me
file or your program's about box would be appreciated. See
http://www.tallhill.com for current contact information.
URLView is provided as-is, with no warranties of any kind. If
you use it, you are on your own.
*/
#include "URLView.h"
#include <Alert.h>
#include <Application.h>
#include <Bitmap.h>
#include <fs_attr.h>
#include <MenuItem.h>
#include <NodeInfo.h>
#include <Path.h>
#include <Roster.h>
#include <Clipboard.h>
#include <unistd.h>
// #include <printf.h>
#include "global.h"
#ifndef ZETA
#define _T(str) str
#else
#include <locale/Locale.h>
#endif
URLView::URLView( BRect frame, const char *name, const char *label,
const char *url, uint32 resizingMode, uint32 flags )
: BStringView( frame, name, label, resizingMode, flags ) {
// Set the default values for the other definable instance variables.
this->color = blue;
this->clickColor = dark_green;
this->hoverColor = dark_blue;
this->disabledColor = gray;
this->hoverEnabled = true;
this->draggable = true;
this->iconSize = 16;
this->underlineThickness = 1;
// The link should be enabled by default (unless the URL is invalid, which
// is handled by the SetURL() function).
enabled = true;
// Set the instance variables.
this->url = 0;
SetURL( url );
// Create the cursor to use when over the link.
this->linkCursor = new BCursor( B_CURSOR_ID_FOLLOW_LINK); //new BCursor( url_cursor );
// The link is not currently selected.
selected = false;
// The URL is currently not hover-colored.
hovering = false;
// The user has not dragged out of the view.
draggedOut = false;
// The user has not yet opened the popup menu.
inPopup = false;
// Initialize the attributes list (there are 14 standard
// Person attributes).
attributes = new BList( 14 );
}
URLView::~URLView() {
delete url;
delete linkCursor;
// Delete all the attributes.
KeyPair *item;
for( int i = 0; (item = (KeyPair *) attributes->ItemAt(i)); i++ ) {
delete item->key;
delete item->value;
delete item;
}
delete attributes;
}
void URLView::AttachedToWindow() {
// When the view is first attached, we want to draw the link
// in the normal color. Also, we want to set our background color
// to meet that of our parent.
if( IsEnabled() )
SetHighColor( color );
else
SetHighColor( disabledColor );
if( Parent() != NULL ) {
SetLowColor( Parent()->ViewColor() );
SetViewColor( Parent()->ViewColor() );
}
}
void URLView::Draw( BRect updateRect ) {
BRect rect = Frame();
rect.OffsetTo( B_ORIGIN );
// We want 'g's, etc. to go below the underline. When the BeOS can
// do underlining of any font, this code can be removed.
font_height height;
GetFontHeight( &height );
float descent = height.descent;
// We want to be sensitive to the SetAlignment() function.
float left, right;
if( Alignment() == B_ALIGN_RIGHT ) {
left = rect.right - StringWidth( Text() ) + 1;
right = rect.right - 1;
}
else if( Alignment() == B_ALIGN_CENTER ) {
left = rect.left + (rect.Width() / 2) - (StringWidth( Text() ) / 2);
right = left + StringWidth( Text() ) - 2;
}
else {
left = rect.left;
right = left + StringWidth( Text() ) - 2;
}
// Draw the underline in the requested thickness.
// Note: If the link is disabled, we don't want to draw the underline.
if( IsEnabled() ) {
FillRect( BRect( (float) left,
(float) (rect.bottom - descent),
(float) right,
(float) (rect.bottom - descent + (underlineThickness - 1)) ) );
}
MovePenTo( BPoint( left, rect.bottom - descent - 1 ) );
// Note: DrawString() draws the text at one pixel above the pen's
// current y coordinate.
DrawString( Text() );
}
void URLView::MessageReceived( BMessage *message )
{
entry_ref ref;
switch (message->what)
{
case 'DDCP':
{
// Is this a message from Tracker in response to our drag-and-drop?
//if( message->what == 'DDCP' ) {
// Tracker will send back the name and path of the created file.
// We need to read this information.
entry_ref ref;
message->FindRef( "directory", &ref );
BEntry entry( &ref );
BPath path( &entry );
BString *fullName = new BString( path.Path() );
fullName->Append( "/" );
fullName->Append( message->FindString( "name" ) );
BString *title = new BString( Text() );
// Set the new file as a bookmark or as a person as appropriate.
if( IsEmailLink() ) {
CreatePerson( fullName, title );
}
else CreateBookmark( fullName, title );
delete fullName;
delete title;
}
break;
default:
BView::MessageReceived(message);
break;
}
}
void URLView::MouseDown( BPoint point ) {
// If the link isn't enabled, don't do anything.
if( !IsEnabled() )
return;
// See which mouse buttons were clicked.
int32 buttons = Window()->CurrentMessage()->FindInt32( "buttons" );
// We want to highlight the text if the user clicks on
// the URL. We want to be sure to only register a click
// if the user clicks on the link text itself and not just
// anywhere in the view.
if( GetTextRect().Contains( point ) ) {
SetHighColor( clickColor );
// Set the link as selected and track the mouse.
selected = true;
SetMouseEventMask( B_POINTER_EVENTS );
// Remember where the user clicked so we can correctly
// offset the transparent URL if the user drags.
BRect frame = Frame();
frame.OffsetTo( B_ORIGIN );
dragOffset = point;
if( Alignment() == B_ALIGN_RIGHT ) {
dragOffset.x -= frame.Width() - StringWidth( Text() );
}
else if( Alignment() == B_ALIGN_CENTER ) {
dragOffset.x -= (frame.Width() / 2) - (StringWidth( Text() ) / 2);
}
// Pop up the context menu?
if( buttons == B_SECONDARY_MOUSE_BUTTON ) inPopup = true;
}
//Redraw();
}
void URLView::MouseMoved( BPoint point, uint32 transit,
const BMessage *message ) {
// If the link isn't enabled, don't do anything.
if( !IsEnabled() )
return;
// Make sure the window is the active one.
if( !Window()->IsActive() ) return;
// See which mouse buttons were clicked.
int32 buttons = Window()->CurrentMessage()->FindInt32( "buttons" );
// Is the user currently dragging the link? (i.e. is a mouse button
// currently down?)
bool alreadyDragging = (buttons != 0);
switch( transit ) {
case( B_ENTERED_VIEW ):
// Should we set the cursor to the link cursor?
if( GetTextRect().Contains( point ) && !draggedOut ) {
if( !alreadyDragging ) be_app->SetCursor( linkCursor );
// Did the user leave and re-enter the view while
// holding down the mouse button? If so, highlight
// the link.
if( selected ) {
SetHighColor( clickColor );
//Redraw();
}
// Should we hover-highlight the link?
else if( hoverEnabled && !alreadyDragging ) {
if( buttons == 0 ) {
SetHighColor( hoverColor );
//Redraw();
hovering = true;
}
}
}
break;
case( B_EXITED_VIEW ):
// We want to restore the link to it normal color and the
// mouse cursor to the normal hand. However, we should only
// set the color and re-draw if it is needed.
if( selected && !draggedOut ) {
be_app->SetCursor( B_HAND_CURSOR );
SetHighColor( color );
//Redraw();
// Is the user drag-and-dropping a bookmark or person?
if( draggable ) {
// = true;
if( IsEmailLink() ) DoPersonDrag();
else DoBookmarkDrag();
}
}
// Is the link currently hover-highlighted? If so, restore
// the normal color now.
else if( hovering && !alreadyDragging ) {
be_app->SetCursor( B_HAND_CURSOR );
SetHighColor( color );
//Redraw();
hovering = false;
}
// Change the cursor back to the hand.
else {
be_app->SetCursor( B_HAND_CURSOR );
}
break;
case( B_INSIDE_VIEW ):
// The user could either be moving out of the view or
// back into it here, so we must handle both cases.
// In the first case, the cursor is now over the link.
if( GetTextRect().Contains( point ) && !draggedOut ) {
// We only want to change the cursor if not dragging.
if( !alreadyDragging ) be_app->SetCursor( linkCursor );
if( selected ) {
if( draggable ) {
// If the user moves the mouse more than ten
// pixels, begin the drag.
if( (point.x - dragOffset.x) > 10 ||
(dragOffset.x - point.x) > 10 ||
(point.y - dragOffset.y) > 10 ||
(dragOffset.y - point.y) > 10 ) {
draggedOut = true;
// Draw the appropriate drag object, etc.
if( IsEmailLink() ) DoPersonDrag();
else DoBookmarkDrag();
SetHighColor( color );
//Redraw();
}
}
else {
// Since the link is not draggable, highlight it
// as long as the user holds the button down and
// has the mouse cursor over it (like a standard
// button).
SetHighColor( clickColor );
//Redraw();
}
}
// The link isn't currently selected? If hover-highlighting
// is enabled, highlight the link.
else if( hoverEnabled && !alreadyDragging ) {
SetHighColor( hoverColor );
//Redraw();
hovering = true;
}
}
// In this case, the mouse cursor is not over the link, so we
// need to restore the original link color, etc.
else if( !draggedOut ) {
be_app->SetCursor( B_HAND_CURSOR );
if( selected ) {
SetHighColor( color );
// Is the user dragging the link?
if( draggable ) {
draggedOut = true;
if( IsEmailLink() ) DoPersonDrag();
else DoBookmarkDrag();
}
}
// Is the mouse cursor hovering over the link?
else if( hovering ) {
SetHighColor( color );
//Redraw();
hovering = false;
}
}
break;
}
}
void URLView::MouseUp( BPoint point ) {
// If the link isn't enabled, don't do anything.
if( !IsEnabled() )
return;
// Do we want to show the right-click menu?
if( inPopup && GetTextRect().Contains( point ) ) {
BPopUpMenu *popup = CreatePopupMenu();
// Work around a current bug in Be's popup menus.
point.y = point.y - 6;
// Display the popup menu.
BMenuItem *selected = popup->Go( ConvertToScreen( point ) , false, true );
// Did the user select an item?
if( selected ) {
BString label( selected->Label() );
// Did the user select the first item? If so, launch the URL.
if( label.FindFirst( "Open" ) != B_ERROR ||
label.FindFirst( "Send" ) != B_ERROR ||
label.FindFirst( "Connect" ) != B_ERROR ) {
LaunchURL();
}
// Did the user select the second item?
else if( label.FindFirst( "Copy" ) != B_ERROR ) {
CopyToClipboard();
}
}
// If not, restore the normal link color.
else {
SetHighColor( color );
}
}
// If the link was clicked on (and not dragged), run the program
// that should handle the URL.
if( selected && GetTextRect().Contains( point ) &&
!draggedOut && !inPopup ) {
LaunchURL();
}
selected = false;
draggedOut = false;
inPopup = false;
// Should we restore the hovering-highlighted color or the original
// link color?
if( GetTextRect().Contains( point ) && !draggedOut &&
!inPopup && hoverEnabled ) {
SetHighColor( hoverColor );
}
else if( !hovering )
{
SetHighColor( color );
}
}
void URLView::WindowActivated( bool active ) {
// Be sure that if the user clicks on a link prompting the opening of
// a new window (i.e. launching NetPositive) the URL is not left drawn
// with the hover color.
if( !active ) {
if( IsEnabled() ) {
SetHighColor( color );
//Redraw();
}
}
}
void URLView::AddAttribute( const char *name, const char *value ) {
// Add an attribute (name and corresponding value) to the object
// that will be dragged out (i.e. to fill in Person fields, etc.)
KeyPair *newPair = new KeyPair;
newPair->key = new BString( name );
newPair->value = new BString( value );
attributes->AddItem( newPair );
}
bool URLView::IsEnabled() {
// Return whether or not this link is enabled (and therefore clickable).
return enabled;
}
void URLView::SetColor( rgb_color color ) {
// Set the normal link color.
this->color = color;
if( IsEnabled() ) {
Window()->Lock();
SetHighColor( color );
Redraw();
Window()->Unlock();
}
}
void URLView::SetColor( uchar red, uchar green, uchar blue, uchar alpha ) {
// Set the normal link color.
rgb_color color;
color.red = red;
color.green = green;
color.blue = blue;
color.alpha = alpha;
SetColor( color );
}
void URLView::SetClickColor( rgb_color color ) {
// Set the link color used when the link is clicked.
clickColor = color;
}
void URLView::SetClickColor( uchar red, uchar green, uchar blue, uchar alpha ) {
// Set the link color used when the link is clicked.
rgb_color color;
color.red = red;
color.green = green;
color.blue = blue;
color.alpha = alpha;
SetClickColor( color );
}
void URLView::SetDisabledColor( rgb_color color ) {
// Set the color to draw in when the link is disabled.
disabledColor = color;
Window()->Lock();
Redraw();
Window()->Unlock();
}
void URLView::SetDisabledColor( uchar red, uchar green, uchar blue, uchar alpha ) {
// Set the color to draw in when the link is disabled.
rgb_color color;
color.red = red;
color.green = green;
color.blue = blue;
color.alpha = alpha;
SetDisabledColor( color );
}
void URLView::SetDraggable( bool draggable ) {
// Set whether or not this link is draggable.
this->draggable = draggable;
}
void URLView::SetEnabled( bool enabled ) {
// Set whether or not the link is enabled (and therefore clickable).
bool redraw = this->enabled != enabled;
this->enabled = enabled;
if( Window() ) {
Window()->Lock();
if( !enabled )
SetHighColor( disabledColor );
else
SetHighColor( color );
if( redraw )
Invalidate();
Window()->Unlock();
}
}
void URLView::SetHoverColor( rgb_color color ) {
// Set the link color used when the mouse cursor is over it.
hoverColor = color;
}
void URLView::SetHoverColor( uchar red, uchar green, uchar blue, uchar alpha ) {
// Set the color to draw in when the link is disabled.
rgb_color color;
color.red = red;
color.green = green;
color.blue = blue;
color.alpha = alpha;
SetHoverColor( color );
}
void URLView::SetHoverEnabled( bool hover ) {
// Set whether or not to hover-highlight the link.
hoverEnabled = hover;
}
void URLView::SetIconSize( icon_size iconSize ) {
// Set the size of the icon that will be shown when the link is dragged.
if( iconSize == B_MINI_ICON ) this->iconSize = 16;
else this->iconSize = 32;
}
void URLView::SetUnderlineThickness( int thickness ) {
// Set the thickness of the underline in pixels.
underlineThickness = thickness;
}
void URLView::SetURL( const char *url ) {
// Set the URL value.
delete this->url;
this->url = new BString( url );
// If it's an e-mail link, we want to insert "mailto:" to the front
// if the user did not enter it.
if( IsEmailLink() ) {
if( this->url->FindFirst( "mailto:" ) != 0 ) {
this->url->Prepend( "mailto:" );
return;
}
}
// We want to see if the URL is valid. If not, we will disable it.
if( !IsFTPLink() && !IsHTMLLink() )
SetEnabled( false );
}
void URLView::CopyToClipboard() {
// Copy the URL to the clipboard.
BClipboard clipboard( "system" );
BMessage *clip = (BMessage *) NULL;
// Get the important URL (i.e. trim off "mailto:", etc.).
BString newclip = GetImportantURL();
// Be sure to lock the clipboard first.
if( clipboard.Lock() ) {
clipboard.Clear();
if( (clip = clipboard.Data()) ) {
clip->AddData( "text/plain", B_MIME_TYPE, newclip.String(),
newclip.Length() + 1 );
clipboard.Commit();
}
clipboard.Unlock();
}
}
void URLView::CreateBookmark( const BString *fullName, const BString *title ) {
// Read the file defined by the path and the title.
BFile *file = new BFile( fullName->String(), B_WRITE_ONLY );
// Set the file's MIME type to be a bookmark.
BNodeInfo *nodeInfo = new BNodeInfo( file );
nodeInfo->SetType( "application/x-vnd.Be-bookmark" );
delete nodeInfo;
delete file;
// Add all the attributes, both those inherrent to bookmarks and any
// the developer may have defined using AddAttribute().
DIR *d;
int fd;
d = fs_open_attr_dir( fullName->String() );
if( d ) {
fd = open( fullName->String(), O_WRONLY );
fs_write_attr( fd, "META:title", B_STRING_TYPE, 0, title->String(), title->Length() + 1 );
fs_write_attr( fd, "META:url", B_STRING_TYPE, 0, url->String(), url->Length() + 1 );
WriteAttributes( fd );
close( fd );
fs_close_attr_dir( d );
}
}
void URLView::CreatePerson( const BString *fullName, const BString *title ) {
// Read the file defined by the path and the title.
BFile *file = new BFile( fullName->String(), B_WRITE_ONLY );
// Set the file's MIME type to be a person.
BNodeInfo *nodeInfo = new BNodeInfo( file );
nodeInfo->SetType( "application/x-person" );
delete nodeInfo;
delete file;
// Add all the attributes, both those inherrent to person files and any
// the developer may have defined using AddAttribute().
DIR *d;
int fd;
d = fs_open_attr_dir( fullName->String() );
if( d ) {
fd = open( fullName->String(), O_WRONLY );
fs_write_attr( fd, "META:name", B_STRING_TYPE, 0, title->String(), title->Length() + 1 );
BString email = GetImportantURL();
fs_write_attr( fd, "META:email", B_STRING_TYPE, 0, email.String(), email.Length() + 1 );
WriteAttributes( fd );
close( fd );
fs_close_attr_dir( d );
}
}
BPopUpMenu * URLView::CreatePopupMenu() {
// Create the right-click popup menu.
BPopUpMenu *returnMe = new BPopUpMenu( "URLView Popup", false, false );
returnMe->SetAsyncAutoDestruct( true );
entry_ref app;
// Set the text of the first item according to the link type.
if( IsEmailLink() ) {
// Find the name of the default e-mail client.
if( be_roster->FindApp( "text/x-email", &app ) == B_OK ) {
BEntry entry( &app );
BString openLabel( _T("Send e-mail to this address using ") );
char name[B_FILE_NAME_LENGTH];
entry.GetName( name );
openLabel.Append( name );
returnMe->AddItem( new BMenuItem( openLabel.String(), NULL ) );
}
}
else if( IsFTPLink() ) {
// Find the name of the default FTP client.
if( be_roster->FindApp( "application/x-vnd.Be.URL.ftp", &app ) == B_OK ) {
BEntry entry( &app );
BString openLabel( _T("Connect to this server using ") );
char name[B_FILE_NAME_LENGTH];
entry.GetName( name );
openLabel.Append( name );
returnMe->AddItem( new BMenuItem( openLabel.String(), NULL ) );
}
}
else {
// Find the name of the default HTML handler (browser).
if( be_roster->FindApp( "text/html", &app ) == B_OK ) {
BEntry entry( &app );
BString openLabel( _T("Open this link using ") );
char name[B_FILE_NAME_LENGTH];
entry.GetName( name );
openLabel.Append( name );
returnMe->AddItem( new BMenuItem( openLabel.String(), NULL ) );
}
}
returnMe->AddItem( new BMenuItem( _T("Copy this link to the clipboard"), NULL ) );
return returnMe;
}
void URLView::DoBookmarkDrag() {
// Handle all of the bookmark dragging. This includes setting up
// the drag message and drawing the dragged bitmap.
// Set up the drag message to support both BTextView dragging (using
// the URL) and file dropping (to Tracker).
BMessage *dragMessage = new BMessage( B_MIME_DATA );
dragMessage->AddInt32( "be:actions", B_COPY_TARGET );
dragMessage->AddString( "be:types", "application/octet-stream" );
dragMessage->AddString( "be:filetypes", "application/x-vnd.Be-bookmark" );
dragMessage->AddString( "be:type_descriptions", "bookmark" );
dragMessage->AddString( "be:clip_name", Text() );
dragMessage->AddString( "be:url", url->String() );
// This allows the user to drag the URL into a standard BTextView.
BString link = GetImportantURL();
dragMessage->AddData( "text/plain", B_MIME_DATA, link.String(),
link.Length() + 1 );
// Query for the system's icon for bookmarks.
BBitmap *bookmarkIcon = new BBitmap( BRect( 0, 0, iconSize - 1,
iconSize - 1 ), B_CMAP8 );
BMimeType mime( "application/x-vnd.Be-bookmark" );
if( iconSize == 16 ) mime.GetIcon( bookmarkIcon, B_MINI_ICON );
else mime.GetIcon( bookmarkIcon, B_LARGE_ICON );
// Find the size of the bitmap to drag. If the text is bigger than the
// icon, use that size. Otherwise, use the icon's. Center the icon
// vertically in the bitmap.
BRect urlRect = GetURLRect();
BRect rect = urlRect;
rect.right += iconSize + 4;
if( (rect.bottom - rect.top) < iconSize ) {
int adjustment = (int) ((iconSize - (rect.bottom - rect.top)) / 2) + 1;
rect.top -= adjustment;
rect.bottom += adjustment;
}
// Make sure the rectangle starts at 0,0.
rect.bottom += 0 - rect.top;
rect.top = 0;
// Create the bitmap to draw the dragged image in.
BBitmap *dragBitmap = new BBitmap( rect, B_RGBA32, true );
BView *dragView = new BView( rect, "Drag View", 0, 0 );
dragBitmap->Lock();
dragBitmap->AddChild( dragView );
BRect frameRect = dragView->Frame();
// Make the background of the dragged image transparent.
dragView->SetHighColor( B_TRANSPARENT_COLOR );
dragView->FillRect( frameRect );
// We want 'g's, etc. to go below the underline. When the BeOS can
// do underlining of any font, this code can be removed.
font_height height;
GetFontHeight( &height );
float descent = height.descent;
// Find the vertical center of the view so we can vertically
// center everything.
int centerPixel = (int) ((frameRect.bottom - frameRect.top) / 2);
int textCenter = (int) (descent + underlineThickness) + centerPixel;
// We want to draw everything only half opaque.
dragView->SetDrawingMode( B_OP_ALPHA );
dragView->SetHighColor( color.red, color.green, color.blue, 128.0 );
dragView->SetBlendingMode( B_CONSTANT_ALPHA, B_ALPHA_COMPOSITE );
// Center the icon in the view.
dragView->MovePenTo( BPoint( frameRect.left,
centerPixel - (iconSize / 2) ) );
dragView->DrawBitmap( bookmarkIcon );
// Draw the text in the same font (size, etc.) as the link view.
// Note: DrawString() draws the text at one pixel above the pen's
// current y coordinate.
BFont font;
GetFont( &font );
dragView->SetFont( &font );
dragView->MovePenTo( BPoint( frameRect.left + iconSize + 4, textCenter ) );
dragView->DrawString( url->String() );
// Draw the underline in the requested thickness.
dragView->FillRect( BRect( (float) frameRect.left + iconSize + 4,
(float) (textCenter + 1),
(float) StringWidth( url->String() ) + iconSize + 4,
(float) textCenter + underlineThickness ) );
// Be sure to flush the view buffer so everything is drawn.
dragView->Flush();
dragBitmap->Unlock();
// The URL's label is probably not the same size as the URL's
// address, which is what we're going to draw. So horizontally
// offset the bitmap proportionally to where the user clicked
// on the link.
float horiz = dragOffset.x / GetTextRect().Width();
dragOffset.x = horiz * frameRect.right;
DragMessage( dragMessage, dragBitmap, B_OP_ALPHA,
BPoint( dragOffset.x, (rect.Height() / 2) + 2 ), this );
delete dragMessage;
draggedOut = true;
}
void URLView::DoPersonDrag() {
// Handle all of the bookmark dragging. This includes setting up
// the drag message and drawing the dragged bitmap.
// Set up the drag message to support both BTextView dragging (using
// the e-mail address) and file dropping (to Tracker).
BMessage *dragMessage = new BMessage( B_MIME_DATA );
dragMessage->AddInt32( "be:actions", B_COPY_TARGET );
dragMessage->AddString( "be:types", "application/octet-stream" );
dragMessage->AddString( "be:filetypes", "application/x-person" );
dragMessage->AddString( "be:type_descriptions", "person" );
dragMessage->AddString( "be:clip_name", Text() );
// This allows the user to drag the e-mail address into a
// standard BTextView.
BString email = GetImportantURL();
dragMessage->AddData( "text/plain", B_MIME_DATA, email.String(),
email.Length() + 1 );
// Query for the system's icon for bookmarks.
BBitmap *personIcon = new BBitmap( BRect( 0, 0, iconSize - 1,
iconSize - 1 ), B_CMAP8 );
#ifdef ZETA
BMimeType mime( "application/x-vnd.Be-PEPL" );
#else
BMimeType mime( "application/x-person" );
#endif
if( iconSize == 16 ) mime.GetIcon( personIcon, B_MINI_ICON );
else mime.GetIcon( personIcon, B_LARGE_ICON );
// Find the size of the bitmap to drag. If the text is bigger than the
// icon, use that size. Otherwise, use the icon's. Center the icon
// vertically in the bitmap.
BRect rect = GetTextRect();
rect.right += iconSize + 4;
if( (rect.bottom - rect.top) < iconSize ) {
int adjustment = (int) ((iconSize - (rect.bottom - rect.top)) / 2) + 1;
rect.top -= adjustment;
rect.bottom += adjustment;
}
// Make sure the rectangle starts at 0,0.
rect.bottom += 0 - rect.top;
rect.top = 0;
// Create the bitmap to draw the dragged image in.
BBitmap *dragBitmap = new BBitmap( rect, B_RGBA32, true );
BView *dragView = new BView( rect, "Drag View", 0, 0 );
dragBitmap->Lock();
dragBitmap->AddChild( dragView );
BRect frameRect = dragView->Frame();
// Make the background of the dragged image transparent.
dragView->SetHighColor( B_TRANSPARENT_COLOR );
dragView->FillRect( frameRect );
// We want 'g's, etc. to go below the underline. When the BeOS can
// do underlining of any font, this code can be removed.
font_height height;
GetFontHeight( &height );
float descent = height.descent;
// Find the vertical center of the view so we can vertically
// center everything.
int centerPixel = (int) ((frameRect.bottom - frameRect.top) / 2);
int textCenter = (int) (descent + underlineThickness) + centerPixel;
// We want to draw everything only half opaque.
dragView->SetDrawingMode( B_OP_ALPHA );
dragView->SetHighColor( 0.0, 0.0, 0.0, 128.0 );
dragView->SetBlendingMode( B_CONSTANT_ALPHA, B_ALPHA_COMPOSITE );
// Center the icon in the view.
dragView->MovePenTo( BPoint( frameRect.left,
centerPixel - (iconSize / 2) ) );
dragView->DrawBitmap( personIcon );
// Draw the text in the same font (size, etc.) as the link view.
// Note: DrawString() draws the text at one pixel above the pen's
// current y coordinate.
BFont font;
GetFont( &font );
dragView->SetFont( &font );
dragView->MovePenTo( BPoint( frameRect.left + iconSize + 4, textCenter ) );
dragView->DrawString( Text() );
// Be sure to flush the view buffer so everything is drawn.
dragView->Flush();
dragBitmap->Unlock();
// The Person icon adds some width to the bitmap that we are
// going to draw. So horizontally offset the bitmap proportionally
// to where the user clicked on the link.
float horiz = dragOffset.x / GetTextRect().Width();
dragOffset.x = horiz * frameRect.right;
DragMessage( dragMessage, dragBitmap, B_OP_ALPHA,
BPoint( dragOffset.x,
(rect.Height() + underlineThickness) / 2 + 2), this );
delete dragMessage;
draggedOut = true;
}
BString URLView::GetImportantURL() {
// Return the relevant portion of the URL (i.e. strip off "mailto:" from
// e-mail address URLs).
BString returnMe;
if( IsEmailLink() ) url->CopyInto( returnMe, 7, url->CountChars() - 6 );
else url->CopyInto( returnMe, 0, url->CountChars() );
return returnMe;
}
BRect URLView::GetTextRect() {
// This function will return a BRect that contains only the text
// and the underline, so the mouse can change and the link will
// be activated only when the mouse is over the text itself, not
// just within the view.
// Note: We'll use bounding boxes, because they are the most
// accurate, and since the user is interacting with the
// view, off-by-one-pixel errors look bad.
const char *textArray[1];
textArray[0] = Text();
escapement_delta delta;
delta.nonspace = 0;
delta.space = 0;
escapement_delta escapements[1];
escapements[0] = delta;
BRect returnMe;
BRect rectArray[1];
rectArray[0] = returnMe;