-
Notifications
You must be signed in to change notification settings - Fork 11
/
HTML.cpp
1802 lines (1540 loc) · 56.4 KB
/
HTML.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
// This class is a message box that has two buttons, ok and cancel instead of
// just the ok button of a message box. We use a message box class for the ok button
// and implement another button here.
//
// $NoKeywords: $
//=============================================================================//
#include "vgui_controls/pch_vgui_controls.h"
#include <vgui_controls/EditablePanel.h>
#include <vgui_controls/Menu.h>
#include <vgui_controls/MessageBox.h>
#include "FileSystem.h"
#include "../vgui2/src/vgui_key_translation.h"
#undef PostMessage
#undef MessageBox
#include "OfflineMode.h"
// memdbgon must be the last include file in a .cpp file
#include "tier0/memdbgon.h"
using namespace vgui2;
#define HL1_USER_AGENT "GoldSrc"
//-----------------------------------------------------------------------------
// Purpose: A simple passthrough panel to render the border onto the HTML widget
//-----------------------------------------------------------------------------
class HTMLInterior : public Panel
{
DECLARE_CLASS_SIMPLE( HTMLInterior, Panel );
public:
HTMLInterior( HTML *parent ) : BaseClass( parent, "HTMLInterior" )
{
m_pHTML = parent;
SetPaintBackgroundEnabled( false );
SetKeyBoardInputEnabled( false );
SetMouseInputEnabled( false );
}
private:
HTML *m_pHTML;
};
//-----------------------------------------------------------------------------
// Purpose: container class for any external popup windows the browser requests
//-----------------------------------------------------------------------------
class HTMLPopup : public vgui2::Frame
{
DECLARE_CLASS_SIMPLE( HTMLPopup, vgui2::Frame );
class PopupHTML : public vgui2::HTML
{
DECLARE_CLASS_SIMPLE( PopupHTML, vgui2::HTML );
public:
PopupHTML( Frame *parent, const char *pchName, bool allowJavaScript , bool bPopupWindow ) : HTML( parent, pchName, allowJavaScript, bPopupWindow ) { m_pParent = parent; }
virtual void OnSetHTMLTitle( const char *pchTitle )
{
BaseClass::OnSetHTMLTitle( pchTitle );
m_pParent->SetTitle( pchTitle, true );
}
private:
Frame *m_pParent;
};
public:
HTMLPopup( Panel *parent, const char *pchURL, const char *pchTitle ) : Frame( NULL, "HtmlPopup", true )
{
m_pHTML = new PopupHTML( this, "htmlpopupchild", true, true );
m_pHTML->OpenURL( pchURL, NULL, false );
SetTitle( pchTitle, true );
}
~HTMLPopup()
{
}
enum
{
vert_inset = 40,
horiz_inset = 6
};
void PerformLayout()
{
BaseClass::PerformLayout();
int wide, tall;
GetSize( wide, tall );
m_pHTML->SetPos( horiz_inset, vert_inset );
m_pHTML->SetSize( wide - horiz_inset*2, tall - vert_inset*2 );
}
void SetBounds( int x, int y, int wide, int tall )
{
BaseClass::SetBounds( x, y, wide + horiz_inset*2, tall + vert_inset*2 );
}
MESSAGE_FUNC( OnCloseWindow, "OnCloseWindow" )
{
Close();
}
private:
PopupHTML *m_pHTML;
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
HTML::HTML(Panel *parent, const char *name, bool allowJavaScript, bool bPopupWindow)
: Panel(parent, name)
#ifndef NO_STEAM
, m_NeedsPaint(this, &HTML::BrowserNeedsPaint)
, m_StartRequest(this, &HTML::BrowserStartRequest)
, m_URLChanged(this, &HTML::BrowserURLChanged)
, m_FinishedRequest(this, &HTML::BrowserFinishedRequest)
, m_LinkInNewTab(this, &HTML::BrowserOpenNewTab)
, m_ChangeTitle(this, &HTML::BrowserSetHTMLTitle)
, m_NewWindow(this, &HTML::BrowserPopupHTMLWindow)
, m_FileLoadDialog(this, &HTML::BrowserFileLoadDialog)
, m_SearchResults(this, &HTML::BrowserSearchResults)
, m_CloseBrowser(this, &HTML::BrowserClose)
, m_HorizScroll(this, &HTML::BrowserHorizontalScrollBarSizeResponse)
, m_VertScroll(this, &HTML::BrowserVerticalScrollBarSizeResponse)
, m_LinkAtPosResp(this, &HTML::BrowserLinkAtPositionResponse)
, m_JSAlert(this, &HTML::BrowserJSAlert)
, m_JSConfirm(this, &HTML::BrowserJSConfirm)
, m_CanGoBackForward(this, &HTML::BrowserCanGoBackandForward)
, m_SetCursor(this, &HTML::BrowserSetCursor)
, m_StatusText(this, &HTML::BrowserStatusText)
, m_ShowTooltip(this, &HTML::BrowserShowToolTip)
, m_UpdateTooltip(this, &HTML::BrowserUpdateToolTip)
, m_HideTooltip(this, &HTML::BrowserHideToolTip)
#endif
{
m_iHTMLTextureID = 0;
m_bCanGoBack = false;
m_bCanGoForward = false;
m_bInFind = false;
m_bRequestingDragURL = false;
m_bRequestingCopyLink = false;
m_flZoom = 100.0f;
m_bNeedsFullTextureUpload = false;
m_pInteriorPanel = new HTMLInterior( this );
SetPostChildPaintEnabled( true );
m_unBrowserHandle = INVALID_HTMLBROWSER;
#ifndef NO_STEAM
m_SteamAPIContext.Init();
if ( m_SteamAPIContext.SteamHTMLSurface() )
{
m_SteamAPIContext.SteamHTMLSurface()->Init();
SteamAPICall_t hSteamAPICall = m_SteamAPIContext.SteamHTMLSurface()->CreateBrowser( HL1_USER_AGENT, NULL );
m_SteamCallResultBrowserReady.Set( hSteamAPICall, this, &HTML::OnBrowserReady );
}
else
{
Warning("Unable to access SteamHTMLSurface\n");
}
#endif
m_iScrollBorderX=m_iScrollBorderY=0;
m_bScrollBarEnabled = true;
m_bContextMenuEnabled = true;
m_bNewWindowsOnly = false;
m_iMouseX = m_iMouseY = 0;
m_iDragStartX = m_iDragStartY = 0;
m_nViewSourceAllowedIndex = -1;
m_iWideLastHTMLSize = m_iTalLastHTMLSize = 0;
_hbar = new ScrollBar(this, "HorizScrollBar", false);
_hbar->SetVisible(false);
_hbar->AddActionSignalTarget(this);
_vbar = new ScrollBar(this, "VertScrollBar", true);
_vbar->SetVisible(false);
_vbar->AddActionSignalTarget(this);
m_pFindBar = new HTML::CHTMLFindBar( this );
m_pFindBar->SetZPos( 2 );
m_pFindBar->SetVisible( false );
m_pContextMenu = new Menu( this, "contextmenu" );
m_pContextMenu->AddMenuItem( "#vgui_HTMLBack", new KeyValues( "Command", "command", "back" ), this );
m_pContextMenu->AddMenuItem( "#vgui_HTMLForward", new KeyValues( "Command", "command", "forward" ), this );
m_pContextMenu->AddMenuItem( "#vgui_HTMLReload", new KeyValues( "Command", "command", "reload" ), this );
m_pContextMenu->AddMenuItem( "#vgui_HTMLStop", new KeyValues( "Command", "command", "stop" ), this );
m_pContextMenu->AddSeparator();
m_pContextMenu->AddMenuItem( "#vgui_HTMLCopyUrl", new KeyValues( "Command", "command", "copyurl" ), this );
m_iCopyLinkMenuItemID = m_pContextMenu->AddMenuItem( "#vgui_HTMLCopyLink", new KeyValues( "Command", "command", "copylink" ), this );
m_pContextMenu->AddMenuItem( "#TextEntry_Copy", new KeyValues( "Command", "command", "copy" ), this );
m_pContextMenu->AddMenuItem( "#TextEntry_Paste", new KeyValues( "Command", "command", "paste" ), this );
m_pContextMenu->AddSeparator();
m_nViewSourceAllowedIndex = m_pContextMenu->AddMenuItem( "#vgui_HTMLViewSource", new KeyValues( "Command", "command", "viewsource" ), this );
}
//-----------------------------------------------------------------------------
// Purpose: browser is ready to show pages
//-----------------------------------------------------------------------------
void HTML::OnBrowserReady( HTML_BrowserReady_t *pBrowserReady, bool bIOFailure )
{
m_unBrowserHandle = pBrowserReady->unBrowserHandle;
BrowserResize();
if (!m_sPendingURLLoad.IsEmpty())
{
PostURL( m_sPendingURLLoad, m_sPendingPostData, false );
m_sPendingURLLoad.Clear();
}
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
HTML::~HTML()
{
m_pContextMenu->MarkForDeletion();
if ( m_SteamAPIContext.SteamHTMLSurface() )
{
m_SteamAPIContext.SteamHTMLSurface()->RemoveBrowser( m_unBrowserHandle );
}
FOR_EACH_VEC( m_vecHCursor, i )
{
// BR FIXME!
// surface()->DeleteCursor( m_vecHCursor[i].m_Cursor );
}
m_vecHCursor.RemoveAll();
}
//-----------------------------------------------------------------------------
// Purpose: Handle message to change our cursor
//-----------------------------------------------------------------------------
void HTML::OnSetCursorVGUI( int cursor )
{
SetCursor( (HCursor)cursor );
}
//-----------------------------------------------------------------------------
// Purpose: sets up colors/fonts/borders
//-----------------------------------------------------------------------------
void HTML::ApplySchemeSettings(IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
BrowserResize();
}
//-----------------------------------------------------------------------------
// Purpose: overrides panel class, paints a texture of the HTML window as a background
//-----------------------------------------------------------------------------
void HTML::Paint()
{
//VPROF_BUDGET( "HTML::Paint()", VPROF_BUDGETGROUP_OTHER_VGUI );
BaseClass::Paint();
if ( m_iHTMLTextureID != 0 )
{
surface()->DrawSetTexture( m_iHTMLTextureID );
int tw = 0, tt = 0;
surface()->DrawSetColor( Color( 255, 255, 255, 255 ) );
GetSize( tw, tt );
surface()->DrawTexturedRect( 0, 0, tw, tt );
}
// If we have scrollbars, we need to draw the bg color under them, since the browser
// bitmap is a checkerboard under them, and they are transparent in the in-game client
if ( m_iScrollBorderX > 0 || m_iScrollBorderY > 0 )
{
int w, h;
GetSize( w, h );
IBorder *border = GetBorder();
int left = 0, top = 0, right = 0, bottom = 0;
if ( border )
{
border->GetInset( left, top, right, bottom );
}
surface()->DrawSetColor( GetBgColor() );
if ( m_iScrollBorderX )
{
surface()->DrawFilledRect( w-m_iScrollBorderX - right, top, w, h - bottom );
}
if ( m_iScrollBorderY )
{
surface()->DrawFilledRect( left, h-m_iScrollBorderY - bottom, w-m_iScrollBorderX - right, h );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: causes a repaint when the layout changes
//-----------------------------------------------------------------------------
void HTML::PerformLayout()
{
BaseClass::PerformLayout();
Repaint();
int vbarInset = _vbar->IsVisible() ? _vbar->GetWide() : 0;
int maxw = GetWide() - vbarInset;
m_pInteriorPanel->SetBounds( 0, 0, maxw, GetTall() );
IScheme *pClientScheme = vgui2::scheme()->GetIScheme( vgui2::scheme()->GetScheme( "ClientScheme" ) );
int iSearchInsetY = 5;
int iSearchInsetX = 5;
int iSearchTall = 24;
int iSearchWide = 150;
const char *resourceString = pClientScheme->GetResourceString( "HTML.SearchInsetY");
if ( resourceString )
{
iSearchInsetY = atoi(resourceString);
}
resourceString = pClientScheme->GetResourceString( "HTML.SearchInsetX");
if ( resourceString )
{
iSearchInsetX = atoi(resourceString);
}
resourceString = pClientScheme->GetResourceString( "HTML.SearchTall");
if ( resourceString )
{
iSearchTall = atoi(resourceString);
}
resourceString = pClientScheme->GetResourceString( "HTML.SearchWide");
if ( resourceString )
{
iSearchWide = atoi(resourceString);
}
m_pFindBar->SetBounds( GetWide() - iSearchWide - iSearchInsetX - vbarInset, m_pFindBar->BIsHidden() ? -1*iSearchTall-5: iSearchInsetY, iSearchWide, iSearchTall );
}
//-----------------------------------------------------------------------------
// Purpose: updates the underlying HTML surface widgets position
//-----------------------------------------------------------------------------
void HTML::OnMove()
{
BaseClass::OnMove();
// tell cef where we are on the screen so plugins can correctly render
int nPanelAbsX, nPanelAbsY;
ipanel()->GetAbsPos( GetVPanel(), nPanelAbsX, nPanelAbsY );
}
//-----------------------------------------------------------------------------
// Purpose: opens the URL, will accept any URL that IE accepts
//-----------------------------------------------------------------------------
void HTML::OpenURL(const char *URL, const char *postData, bool force)
{
PostURL( URL, postData, force );
}
//-----------------------------------------------------------------------------
// Purpose: opens the URL, will accept any URL that IE accepts
//-----------------------------------------------------------------------------
void HTML::PostURL(const char *URL, const char *pchPostData, bool force)
{
if ( m_unBrowserHandle == INVALID_HTMLBROWSER )
{
m_sPendingURLLoad = URL;
m_sPendingPostData = pchPostData;
return;
}
if ( IsSteamInOfflineMode() && !force )
{
const char *baseDir = getenv("HTML_OFFLINE_DIR");
if ( baseDir )
{
// get the app we need to run
char htmlLocation[_MAX_PATH];
char otherName[128];
char fileLocation[_MAX_PATH];
if ( ! g_pFullFileSystem->FileExists( baseDir ) )
{
Q_snprintf( otherName, sizeof(otherName), "%senglish.html", OFFLINE_FILE );
baseDir = otherName;
}
g_pFullFileSystem->GetLocalCopy( baseDir ); // put this file on disk for IE to load
g_pFullFileSystem->GetLocalPath( baseDir, fileLocation, sizeof(fileLocation) );
Q_snprintf(htmlLocation, sizeof(htmlLocation), "file://%s", fileLocation);
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->LoadURL( m_unBrowserHandle, htmlLocation, NULL );
}
else
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->LoadURL( m_unBrowserHandle, URL, NULL );
}
}
else
{
if ( pchPostData && Q_strlen(pchPostData) > 0 )
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->LoadURL( m_unBrowserHandle, URL, pchPostData );
}
else
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->LoadURL( m_unBrowserHandle, URL, NULL );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: opens the URL, will accept any URL that IE accepts
//-----------------------------------------------------------------------------
bool HTML::StopLoading()
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->StopLoad( m_unBrowserHandle );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: refreshes the current page
//-----------------------------------------------------------------------------
bool HTML::Refresh()
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->Reload( m_unBrowserHandle );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Tells the browser control to go back
//-----------------------------------------------------------------------------
void HTML::GoBack()
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->GoBack( m_unBrowserHandle );
}
//-----------------------------------------------------------------------------
// Purpose: Tells the browser control to go forward
//-----------------------------------------------------------------------------
void HTML::GoForward()
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->GoForward( m_unBrowserHandle );
}
//-----------------------------------------------------------------------------
// Purpose: Checks if the browser can go back further
//-----------------------------------------------------------------------------
bool HTML::BCanGoBack()
{
return m_bCanGoBack;
}
//-----------------------------------------------------------------------------
// Purpose: Checks if the browser can go forward further
//-----------------------------------------------------------------------------
bool HTML::BCanGoFoward()
{
return m_bCanGoForward;
}
//-----------------------------------------------------------------------------
// Purpose: handle resizing
//-----------------------------------------------------------------------------
void HTML::OnSizeChanged(int wide,int tall)
{
BaseClass::OnSizeChanged(wide,tall);
UpdateSizeAndScrollBars();
}
//-----------------------------------------------------------------------------
// Purpose: Run javascript in the page
//-----------------------------------------------------------------------------
void HTML::RunJavascript( const char *pchScript )
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->ExecuteJavascript( m_unBrowserHandle, pchScript );
}
//-----------------------------------------------------------------------------
// Purpose: helper to convert UI mouse codes to CEF ones
//-----------------------------------------------------------------------------
ISteamHTMLSurface::EHTMLMouseButton ConvertMouseCodeToCEFCode( MouseCode code )
{
switch( code )
{
default:
case MOUSE_LEFT:
return ISteamHTMLSurface::eHTMLMouseButton_Left;
break;
case MOUSE_RIGHT:
return ISteamHTMLSurface::eHTMLMouseButton_Right;
break;
case MOUSE_MIDDLE:
return ISteamHTMLSurface::eHTMLMouseButton_Middle;
break;
}
}
//-----------------------------------------------------------------------------
// Purpose: passes mouse clicks to the control
//-----------------------------------------------------------------------------
void HTML::OnMousePressed(MouseCode code)
{
m_sDragURL = NULL;
// mouse4 = back button
if ( code == MOUSE_4 )
{
PostActionSignal( new KeyValues( "HTMLBackRequested" ) );
return;
}
if ( code == MOUSE_5 )
{
PostActionSignal( new KeyValues( "HTMLForwardRequested" ) );
return;
}
if ( code == MOUSE_RIGHT && m_bContextMenuEnabled )
{
GetLinkAtPosition( m_iMouseX, m_iMouseY );
Menu::PlaceContextMenu( this, m_pContextMenu );
return;
}
// ask for the focus to come to this window
RequestFocus();
// now tell the browser about the click
// ignore right clicks if context menu has been disabled
if ( code != MOUSE_RIGHT )
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->MouseDown( m_unBrowserHandle, ConvertMouseCodeToCEFCode( code ) );
}
if ( code == MOUSE_LEFT )
{
input()->GetCursorPos( m_iDragStartX, m_iDragStartY );
int htmlx, htmly;
ipanel()->GetAbsPos( GetVPanel(), htmlx, htmly );
GetLinkAtPosition( m_iDragStartX - htmlx, m_iDragStartY - htmly );
m_bRequestingDragURL = true;
// make sure we get notified when the mouse gets released
if ( !m_sDragURL.IsEmpty() )
{
input()->SetMouseCapture( GetVPanel() );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: passes mouse up events
//-----------------------------------------------------------------------------
void HTML::OnMouseReleased(MouseCode code)
{
if ( code == MOUSE_LEFT )
{
input()->SetMouseCapture( NULL );
input()->SetCursorOveride( 0 );
if ( !m_sDragURL.IsEmpty() && input()->GetMouseOver() != GetVPanel() && input()->GetMouseOver() )
{
// post the text as a drag drop to the target panel
KeyValuesAD kv( "DragDrop" );
if ( ipanel()->RequestInfo( input()->GetMouseOver(), kv )
&& kv->GetPtr( "AcceptPanel" ) != NULL )
{
VPANEL vpanel = (VPANEL)kv->GetPtr( "AcceptPanel" );
ivgui()->PostMessage( vpanel, new KeyValues( "DragDrop", "text", m_sDragURL.Get() ), GetVPanel() );
}
}
m_sDragURL = NULL;
}
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->MouseUp( m_unBrowserHandle, ConvertMouseCodeToCEFCode( code ) );
}
//-----------------------------------------------------------------------------
// Purpose: keeps track of where the cursor is
//-----------------------------------------------------------------------------
void HTML::OnCursorMoved(int x,int y)
{
// Only do this when we are over the current panel
if ( vgui2::input()->GetMouseOver() == GetVPanel() )
{
m_iMouseX = x;
m_iMouseY = y;
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->MouseMove( m_unBrowserHandle, m_iMouseX, m_iMouseY );
}
else if ( !m_sDragURL.IsEmpty() )
{
if ( !input()->GetMouseOver() )
{
// we're not over any vgui window, switch to the OS implementation of drag/drop
// BR FIXME
// surface()->StartDragDropText( m_sDragURL );
m_sDragURL = NULL;
}
}
if ( !m_sDragURL.IsEmpty() && !input()->GetCursorOveride() )
{
// if we've dragged far enough (in global coordinates), set to use the drag cursor
int gx, gy;
input()->GetCursorPos( gx, gy );
if ( abs(m_iDragStartX-gx) + abs(m_iDragStartY-gy) > 3 )
{
// input()->SetCursorOveride( dc_alias );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: passes double click events to the browser
//-----------------------------------------------------------------------------
void HTML::OnMouseDoublePressed(MouseCode code)
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->MouseDoubleClick( m_unBrowserHandle, ConvertMouseCodeToCEFCode( code ) );
}
//-----------------------------------------------------------------------------
// Purpose: return the bitmask of any modifier keys that are currently down
//-----------------------------------------------------------------------------
int GetKeyModifiers()
{
// Any time a key is pressed reset modifier list as well
int nModifierCodes = 0;
if (vgui2::input()->IsKeyDown( KEY_LCONTROL ) || vgui2::input()->IsKeyDown( KEY_RCONTROL ))
nModifierCodes |= ISteamHTMLSurface::k_eHTMLKeyModifier_CtrlDown;
if (vgui2::input()->IsKeyDown( KEY_LALT ) || vgui2::input()->IsKeyDown( KEY_RALT ))
nModifierCodes |= ISteamHTMLSurface::k_eHTMLKeyModifier_AltDown;
if (vgui2::input()->IsKeyDown( KEY_LSHIFT ) || vgui2::input()->IsKeyDown( KEY_RSHIFT ))
nModifierCodes |= ISteamHTMLSurface::k_eHTMLKeyModifier_ShiftDown;
#ifdef OSX
// for now pipe through the cmd-key to be like the control key so we get copy/paste
if (vgui2::input()->IsKeyDown( KEY_LWIN ) || vgui2::input()->IsKeyDown( KEY_RWIN ))
nModifierCodes |= ISteamHTMLSurface::k_eHTMLKeyModifier_CtrlDown;
#endif
return nModifierCodes;
}
//-----------------------------------------------------------------------------
// Purpose: passes key presses to the browser (we don't current do this)
//-----------------------------------------------------------------------------
void HTML::OnKeyTyped(wchar_t unichar)
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->KeyChar( m_unBrowserHandle, unichar, (ISteamHTMLSurface::EHTMLKeyModifiers)GetKeyModifiers() );
}
//-----------------------------------------------------------------------------
// Purpose: pop up the find dialog
//-----------------------------------------------------------------------------
void HTML::ShowFindDialog()
{
IScheme *pClientScheme = vgui2::scheme()->GetIScheme( vgui2::scheme()->GetScheme( "ClientScheme" ) );
if ( !pClientScheme )
return;
m_pFindBar->SetVisible( true );
m_pFindBar->RequestFocus();
m_pFindBar->SetText( "" );
m_pFindBar->HideCountLabel();
m_pFindBar->SetHidden( false );
int x = 0, y = 0, h = 0, w = 0;
m_pFindBar->GetBounds( x, y, w, h );
m_pFindBar->SetPos( x, -1*h );
int iSearchInsetY = 0;
const char *resourceString = pClientScheme->GetResourceString( "HTML.SearchInsetY");
if ( resourceString )
{
iSearchInsetY = atoi(resourceString);
}
float flAnimationTime = 0.0f;
resourceString = pClientScheme->GetResourceString( "HTML.SearchAnimationTime");
if ( resourceString )
{
flAnimationTime = atof(resourceString);
}
GetAnimationController()->RunAnimationCommand( m_pFindBar, "ypos", iSearchInsetY, 0.0f, flAnimationTime, AnimationController::INTERPOLATOR_LINEAR );
}
//-----------------------------------------------------------------------------
// Purpose: hide the find dialog
//-----------------------------------------------------------------------------
void HTML::HideFindDialog()
{
IScheme *pClientScheme = vgui2::scheme()->GetIScheme( vgui2::scheme()->GetScheme( "ClientScheme" ) );
if ( !pClientScheme )
return;
int x = 0, y = 0, h = 0, w = 0;
m_pFindBar->GetBounds( x, y, w, h );
float flAnimationTime = 0.0f;
const char *resourceString = pClientScheme->GetResourceString( "HTML.SearchAnimationTime");
if ( resourceString )
{
flAnimationTime = atof(resourceString);
}
GetAnimationController()->RunAnimationCommand( m_pFindBar, "ypos", -1*h-5, 0.0f, flAnimationTime, AnimationController::INTERPOLATOR_LINEAR );
m_pFindBar->SetHidden( true );
StopFind();
}
//-----------------------------------------------------------------------------
// Purpose: is the find dialog visible?
//-----------------------------------------------------------------------------
bool HTML::FindDialogVisible()
{
return m_pFindBar->IsVisible() && !m_pFindBar->BIsHidden();
}
//-----------------------------------------------------------------------------
// Purpose: passes key presses to the browser
//-----------------------------------------------------------------------------
void HTML::OnKeyCodeTyped(KeyCode code)
{
switch( code )
{
case KEY_PAGEDOWN:
{
int val = _vbar->GetValue();
val += 200;
_vbar->SetValue(val);
break;
}
case KEY_PAGEUP:
{
int val = _vbar->GetValue();
val -= 200;
_vbar->SetValue(val);
break;
}
case KEY_F5:
{
Refresh();
break;
}
case KEY_F:
{
if ( (input()->IsKeyDown(KEY_LCONTROL) || input()->IsKeyDown(KEY_RCONTROL) )
|| ( IsOSX() && ( input()->IsKeyDown(KEY_LWIN) || input()->IsKeyDown(KEY_RWIN) ) ) )
{
if ( !FindDialogVisible() )
{
ShowFindDialog();
}
else
{
HideFindDialog();
}
break;
}
}
case KEY_ESCAPE:
{
if ( FindDialogVisible() )
{
HideFindDialog();
break;
}
}
case KEY_TAB:
{
if ( input()->IsKeyDown(KEY_LCONTROL) || input()->IsKeyDown(KEY_RCONTROL) )
{
// pass control-tab to parent (through baseclass)
BaseClass::OnKeyTyped( code );
return;
}
break;
}
}
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->KeyDown( m_unBrowserHandle, KeyCode_VGUIToVirtualKey(code), (ISteamHTMLSurface::EHTMLKeyModifiers)GetKeyModifiers() );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void HTML::OnKeyCodeReleased(KeyCode code)
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->KeyUp( m_unBrowserHandle, KeyCode_VGUIToVirtualKey( code ), (ISteamHTMLSurface::EHTMLKeyModifiers)GetKeyModifiers() );
}
//-----------------------------------------------------------------------------
// Purpose: scrolls the vertical scroll bar on a web page
//-----------------------------------------------------------------------------
void HTML::OnMouseWheeled(int delta)
{
if (_vbar )
{
int val = _vbar->GetValue();
val -= (delta * 100.0/3.0 ); // 100 for every 3 lines matches chromes code
_vbar->SetValue(val);
}
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->MouseWheel( m_unBrowserHandle, delta* 100.0/3.0 );
}
//-----------------------------------------------------------------------------
// Purpose: Inserts a custom URL handler
//-----------------------------------------------------------------------------
void HTML::AddCustomURLHandler(const char *customProtocolName, vgui2::Panel *target)
{
int index = m_CustomURLHandlers.AddToTail();
m_CustomURLHandlers[index].hPanel = target;
Q_strncpy(m_CustomURLHandlers[index].url, customProtocolName, sizeof(m_CustomURLHandlers[index].url));
}
//-----------------------------------------------------------------------------
// Purpose: shared code for sizing the HTML surface window
//-----------------------------------------------------------------------------
void HTML::BrowserResize()
{
if (m_unBrowserHandle == INVALID_HTMLBROWSER)
return;
int w,h;
GetSize( w, h );
int right = 0, bottom = 0;
// TODO::STYLE
/*
IAppearance *pAppearance = GetAppearance();
int left = 0, top = 0;
if ( pAppearance )
{
pAppearance->GetInset( left, top, right, bottom );
}
*/
if ( m_iWideLastHTMLSize != ( w - m_iScrollBorderX - right ) || m_iTalLastHTMLSize != ( h - m_iScrollBorderY - bottom ) )
{
m_iWideLastHTMLSize = w - m_iScrollBorderX - right;
m_iTalLastHTMLSize = h - m_iScrollBorderY - bottom;
if ( m_iTalLastHTMLSize <= 0 )
{
SetTall( 64 );
m_iTalLastHTMLSize = 64 - bottom;
}
{
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->SetSize( m_unBrowserHandle, m_iWideLastHTMLSize, m_iTalLastHTMLSize );
}
// webkit forgets the scroll offset when you resize (it saves the scroll in a DC and a resize throws away the DC)
// so just tell it after the resize
int scrollV = _vbar->GetValue();
int scrollH = _hbar->GetValue();
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->SetHorizontalScroll( m_unBrowserHandle, scrollH );
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->SetVerticalScroll( m_unBrowserHandle, scrollV );
}
}
//-----------------------------------------------------------------------------
// Purpose: when a slider moves causes the IE images to re-render itself
//-----------------------------------------------------------------------------
void HTML::OnSliderMoved()
{
if(_hbar->IsVisible())
{
int scrollX = _hbar->GetValue();
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->SetHorizontalScroll( m_unBrowserHandle, scrollX );
}
if(_vbar->IsVisible())
{
int scrollY=_vbar->GetValue();
if (m_SteamAPIContext.SteamHTMLSurface())
m_SteamAPIContext.SteamHTMLSurface()->SetVerticalScroll( m_unBrowserHandle, scrollY );
}
// post a message that the slider has moved
PostActionSignal( new KeyValues( "HTMLSliderMoved" ) );
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
bool HTML::IsScrolledToBottom()
{
if ( !_vbar->IsVisible() )
return true;
return m_scrollVertical.m_nScroll >= m_scrollVertical.m_nMax;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
bool HTML::IsScrollbarVisible()
{
return _vbar->IsVisible();
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void HTML::SetScrollbarsEnabled(bool state)
{
m_bScrollBarEnabled = state;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void HTML::SetContextMenuEnabled(bool state)
{
m_bContextMenuEnabled = state;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void HTML::SetViewSourceEnabled(bool state)
{
m_pContextMenu->SetItemVisible( m_nViewSourceAllowedIndex, state );
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void HTML::NewWindowsOnly( bool state )
{
m_bNewWindowsOnly = state;
}
//-----------------------------------------------------------------------------
// Purpose: called when our children have finished painting