htmlview.monkey2 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. Namespace mojox
  2. #rem monkeydoc The HtmlView class.
  3. #end
  4. Class HtmlView Extends ScrollableView
  5. #rem monkeydoc Invoked when an anchor is clicked.
  6. #end
  7. Field AnchorClicked:Void( url:String )
  8. #rem monkeydoc Creates a new HtmlView.
  9. #end
  10. Method New()
  11. Layout="fill"
  12. Style=GetStyle( "HtmlView" )
  13. _context=New litehtml.context
  14. _context.load_master_stylesheet( stringio.LoadString( "theme::htmlview_master_css.css" ) )
  15. _container=New document_container( Self )
  16. _baseUrl=filesystem.CurrentDir()
  17. AnchorClicked=Go
  18. End
  19. #rem monkeydoc Base URL.
  20. This is used as the root directory for relative anchors in the page.
  21. #end
  22. Property BaseUrl:String()
  23. Return _baseUrl
  24. Setter( baseUrl:String )
  25. If Not baseUrl.EndsWith( "/" ) baseUrl+="/"
  26. _baseUrl=baseUrl
  27. End
  28. #rem monkeydoc HTML source.
  29. #end
  30. Property HtmlSource:String()
  31. Return _source
  32. Setter( htmlSource:String )
  33. _source=htmlSource
  34. _document=New litehtml.document( _source,_container,_context )
  35. _layoutSize=Null
  36. _renderSize=Null
  37. RequestRender()
  38. End
  39. #rem monkeydoc Goto a url.
  40. #end
  41. Method Go( url:String )
  42. If url.Contains( "#" )
  43. Return
  44. Endif
  45. Local root:=ExtractRootDir( url )
  46. If root="http://" Or root="https://"
  47. #If __DESKTOP_TARGET__
  48. requesters.OpenUrl( url )
  49. #Endif
  50. Return
  51. Endif
  52. If Not root
  53. url=BaseUrl+url
  54. Endif
  55. Local src:=stringio.LoadString( url )
  56. If ExtractExt( url )=".md"
  57. src=hoedown.MarkdownToHtml( src )
  58. Local wrapper:=stringio.LoadString( "theme::markdown_wrapper.html" )
  59. src=wrapper.Replace( "${CONTENT}",src )
  60. End
  61. BaseUrl=ExtractDir( url )
  62. HtmlSource=src
  63. End
  64. Private
  65. Field _context:litehtml.context
  66. Field _container:litehtml.document_container
  67. Field _anchorClicked:String
  68. Field _baseUrl:String
  69. Field _source:String
  70. Field _document:litehtml.document
  71. Field _layoutSize:Vec2i
  72. Field _renderSize:Vec2i
  73. Method OnThemeChanged() Override
  74. _document=New litehtml.document( _source,_container,_context )
  75. _layoutSize=Null
  76. _renderSize=Null
  77. End
  78. Method OnMeasureContent2:Vec2i( size:Vec2i ) Override
  79. If Not _document Return New Vec2i( 0,0 )
  80. If size.x=_layoutSize.x Return _renderSize
  81. _layoutSize=size
  82. _document.render( size.x )
  83. _renderSize=New Vec2i( _document.width(),_document.height() )
  84. Return _renderSize
  85. End
  86. Method OnRenderContent( canvas:Canvas ) Override
  87. If Not _document Return
  88. Local clip:litehtml.position
  89. clip.x=canvas.Scissor.X
  90. clip.y=canvas.Scissor.Y
  91. clip.width=canvas.Scissor.Width
  92. clip.height=canvas.Scissor.Height
  93. _document.draw( canvas,0,0,Varptr clip )
  94. End
  95. Method OnContentMouseEvent( event:MouseEvent ) Override
  96. If Not _document Return
  97. Local x:=event.Location.X
  98. Local y:=event.Location.Y
  99. _anchorClicked=""
  100. Select event.Type
  101. Case EventType.MouseDown
  102. _document.on_lbutton_down( x,y,x,y )
  103. Case EventType.MouseMove
  104. _document.on_mouse_over( x,y,x,y )
  105. Case EventType.MouseUp
  106. _document.on_lbutton_up( x,y,x,y )
  107. _document.on_mouse_leave()
  108. Case EventType.MouseWheel
  109. Return
  110. End
  111. event.Eat()
  112. RequestRender() 'Not ideal, but necessary for link highlighting...
  113. If _anchorClicked AnchorClicked( _anchorClicked )
  114. End
  115. End
  116. Class document_container Extends litehtml.document_container
  117. Field _view:HtmlView
  118. Method ToFont:Font( hfont:Object )
  119. Return Cast<Font>( hfont )
  120. End
  121. Method ToCanvas:Canvas( hdc:Object )
  122. Return Cast<Canvas>( hdc )
  123. End
  124. Method GetImage:Image( src:String )
  125. Return App.Theme.OpenImage( src )
  126. End
  127. Method New( view:HtmlView )
  128. _view=view
  129. End
  130. Method set_color( canvas:Canvas,color:litehtml.web_color )
  131. canvas.Color=New Color( color.red/255.0,color.green/255.0,color.blue/255.0,1 )
  132. End
  133. Method make_url:String( href:String )
  134. Return _view._baseUrl+href
  135. End
  136. Method create_font:Object( faceName:String,size:Int,weight:Int,style:litehtml.font_style,decoration:UInt,fm:litehtml.font_metrics Ptr ) Override
  137. Local face:="DejaVuSans"
  138. If faceName.Contains( "monospace" ) face+="Mono"
  139. Local font:=App.Theme.OpenFont( face,size )
  140. Local height:=font.Height
  141. fm->height=height
  142. fm->ascent=height
  143. fm->descent=0
  144. fm->x_height=height
  145. fm->draw_spaces=True
  146. Return font
  147. End
  148. Method delete_font( font:Object ) Override
  149. End
  150. Method text_width:Int( text:String,hfont:Object ) Override
  151. Local font:=ToFont( hfont )
  152. Return font.TextWidth( text )
  153. End
  154. Method draw_text( hdc:Object,text:String,hfont:Object,color:litehtml.web_color Ptr,pos:litehtml.position Ptr ) Override
  155. Local canvas:=ToCanvas( hdc )
  156. Local font:=ToFont( hfont )
  157. canvas.Font=font
  158. set_color( canvas,color[0] )
  159. canvas.DrawText( text,pos->x,pos->y )
  160. Return
  161. End
  162. Method pt_to_px:Int( pt:Int ) Override
  163. Return 0
  164. End
  165. Method get_default_font_size:Int() Override
  166. Return 16 * App.Theme.Scale.y
  167. End
  168. Method get_default_font_name:String() Override
  169. Return "DejaVuSans"
  170. End
  171. Method draw_list_marker( hdc:Object,marker:litehtml.list_marker Ptr ) Override
  172. If marker->marker_type=litehtml.list_style_type_none Return
  173. Local canvas:=ToCanvas( hdc )
  174. set_color( canvas,marker->color )
  175. Select marker->marker_type
  176. Case litehtml.list_style_type_disc
  177. canvas.DrawOval( marker->pos.x,marker->pos.y,marker->pos.width,marker->pos.height )
  178. Default
  179. canvas.DrawRect( marker->pos.x,marker->pos.y,marker->pos.width,marker->pos.height )
  180. End
  181. End
  182. Method load_image( src:String,baseurl:String,redraw_on_ready:Bool ) Override
  183. GetImage( src )
  184. End
  185. Method get_image_size( src:String,baseurl:String,sz:litehtml.size Ptr ) Override
  186. Local image:=GetImage( src )
  187. If Not image Return
  188. sz->width=image.Width
  189. sz->height=image.Height
  190. End
  191. Method draw_background( hdc:Object,img_src:String,img_baseurl:String,bg:litehtml.background_paint Ptr ) Override
  192. Local canvas:=ToCanvas( hdc )
  193. Local image:=GetImage( img_src )
  194. If image
  195. canvas.Color=Color.White
  196. canvas.DrawImage( image,bg->position_x,bg->position_y )
  197. Return
  198. Endif
  199. set_color( canvas,bg->color )
  200. ' canvas.DrawRect( bg->clip_box.x,bg->clip_box.y,bg->clip_box.width,bg->clip_box.height )
  201. canvas.DrawRect( bg->border_box.x,bg->border_box.y,bg->border_box.width,bg->border_box.height )
  202. End
  203. Method draw_border( canvas:Canvas,border:litehtml.border,x:Int,y:Int,w:Int,h:Int )
  204. If border.style<>litehtml.border_style_solid Or border.width<1 Return
  205. set_color( canvas,border.color )
  206. canvas.DrawRect( x,y,w,h )
  207. End
  208. Method draw_borders( hdc:Object,borders:litehtml.borders Ptr,pos:litehtml.position Ptr,root:Bool ) Override
  209. Local canvas:=ToCanvas( hdc )
  210. Local x:=pos->x,y:=pos->y
  211. Local w:=pos->width,h:=pos->height
  212. draw_border( canvas,borders->left,x,y,1,h )
  213. draw_border( canvas,borders->top,x,y,w,1 )
  214. draw_border( canvas,borders->right,x+w-1,y,1,h )
  215. draw_border( canvas,borders->bottom,x,y+h-1,w,1 )
  216. End
  217. Method set_caption( caption:String ) Override
  218. End
  219. Method set_base_url( baseurl:String ) Override
  220. End
  221. Method on_anchor_click( url:String ) Override
  222. _view._anchorClicked=url
  223. End
  224. Method set_cursor( cursor:String ) Override
  225. End
  226. Method import_css:String( url:String,baseurl:String ) Override
  227. Local css:=stringio.LoadString( make_url( url ) )
  228. Return css
  229. End
  230. Method set_clip( pos:litehtml.position Ptr,radiuses:litehtml.border_radiuses Ptr ) Override
  231. End
  232. Method del_clip() Override
  233. End
  234. Method get_client_rect( client:litehtml.position Ptr ) Override
  235. ' If _view._rendering Print "get client rect"
  236. client->x=0
  237. client->y=0
  238. client->width=_view._layoutSize.x
  239. client->height=_view._layoutSize.y
  240. End
  241. Method get_media_features( media:litehtml.media_features Ptr ) Override
  242. ' If _view._rendering Print "get media features"
  243. media->type=litehtml.media_type_screen
  244. media->width=_view._layoutSize.x
  245. media->height=_view._layoutSize.y
  246. media->device_width=1920
  247. media->device_height=1080
  248. media->color=8
  249. media->color_index=0
  250. media->monochrome=0
  251. media->resolution=96
  252. End
  253. Method get_language:String() Override
  254. Return ""
  255. End
  256. Method get_culture:String() Override
  257. Return ""
  258. End
  259. End