servo: Make Box into a class

Source-Repo: https://github.com/servo/servo
Source-Revision: 6c6f7f99e4edda02b13f44a3ef11e75db7d67b36
This commit is contained in:
Patrick Walton 2012-06-14 18:55:04 -07:00
parent 666413af22
commit 53fb871df5
7 changed files with 67 additions and 67 deletions

View File

@ -34,17 +34,25 @@ class appearance {
}
}
enum box = {
tree: tree::fields<@box>,
node: Node,
mut bounds: Rect<au>,
kind: BoxKind,
appearance: appearance
};
class Box {
let tree: tree::fields<@Box>;
let node: Node;
let kind: BoxKind;
let mut bounds: Rect<au>;
let appearance: appearance;
new(node: Node, kind: BoxKind) {
self.tree = tree::empty();
self.node = node;
self.kind = kind;
self.bounds = geometry::zero_rect_au();
self.appearance = appearance();
}
}
enum layout_data = {
mut computed_style: computed_style,
mut box: option<@box>
mut box: option<@Box>
};
enum ntree { ntree }
@ -59,27 +67,27 @@ impl of tree::rd_tree_ops<Node> for ntree {
}
enum btree { btree }
impl of tree::rd_tree_ops<@box> for btree {
fn each_child(node: @box, f: fn(&&@box) -> bool) {
impl of tree::rd_tree_ops<@Box> for btree {
fn each_child(node: @Box, f: fn(&&@Box) -> bool) {
tree::each_child(self, node, f)
}
fn with_tree_fields<R>(&&b: @box, f: fn(tree::fields<@box>) -> R) -> R {
fn with_tree_fields<R>(&&b: @Box, f: fn(tree::fields<@Box>) -> R) -> R {
f(b.tree)
}
}
impl of tree::wr_tree_ops<@box> for btree {
fn add_child(node: @box, child: @box) {
impl of tree::wr_tree_ops<@Box> for btree {
fn add_child(node: @Box, child: @Box) {
tree::add_child(self, node, child)
}
fn with_tree_fields<R>(&&b: @box, f: fn(tree::fields<@box>) -> R) -> R {
fn with_tree_fields<R>(&&b: @Box, f: fn(tree::fields<@Box>) -> R) -> R {
f(b.tree)
}
}
impl layout_methods_priv for @box {
impl layout_methods_priv for @Box {
#[doc="Dumps the box tree, for debugging, with indentation."]
fn dump_indent(indent: uint) {
let mut s = "";
@ -95,7 +103,7 @@ impl layout_methods_priv for @box {
}
}
impl layout_methods for @box {
impl layout_methods for @Box {
#[doc="The main reflow routine."]
fn reflow(available_width: au) {
alt self.kind {
@ -167,7 +175,7 @@ mod test {
}
*/
fn flat_bounds(root: @box) -> [Rect<au>] {
fn flat_bounds(root: @Box) -> [Rect<au>] {
let mut r = [];
for tree::each_child(btree, root) {|c|
r += flat_bounds(c);

View File

@ -7,7 +7,7 @@ import layout::base::*; // FIXME: Can't get around import *; resolve bug.
import util::tree;
#[doc="The public block layout methods."]
impl block_layout_methods for @box {
impl block_layout_methods for @Box {
#[doc="The main reflow routine for block layout."]
fn reflow_block(available_width: au) {
assert self.kind == BlockBox;

View File

@ -4,8 +4,8 @@ import dom::base::{ElementData, HTMLDivElement, HTMLImageElement, Element, Text,
import dom::style::{display_type, di_block, di_inline, di_none};
import dom::rcu::reader_methods;
import gfx::geometry;
import layout::base::{BlockBox, BoxKind, InlineBox, IntrinsicBox, NodeMethods, TextBox};
import layout::base::{appearance, box, btree, ntree, rd_tree_ops, wr_tree_ops};
import layout::base::{BlockBox, Box, BoxKind, InlineBox, IntrinsicBox, NodeMethods, TextBox};
import layout::base::{appearance, btree, ntree, rd_tree_ops, wr_tree_ops};
import layout::style::style::{style_methods};
import layout::text::text_box;
import util::tree;
@ -17,7 +17,7 @@ enum ctxt = {
// The parent node that we're scanning.
parent_node: Node,
// The parent box that these boxes will be added to.
parent_box: @box,
parent_box: @Box,
//
// The current anonymous box that we're currently appending inline nodes to.
@ -25,18 +25,10 @@ enum ctxt = {
// See CSS2 9.2.1.1.
//
mut anon_box: option<@box>
mut anon_box: option<@Box>
};
fn new_box(n: Node, kind: BoxKind) -> @box {
@box({tree: tree::empty(),
node: n,
mut bounds: geometry::zero_rect_au(),
kind: kind,
appearance: appearance() })
}
fn create_context(parent_node: Node, parent_box: @box) -> ctxt {
fn create_context(parent_node: Node, parent_box: @Box) -> ctxt {
ret ctxt({
parent_node: parent_node,
parent_box: parent_box,
@ -71,14 +63,14 @@ impl methods for ctxt {
let anon_box = alt self.anon_box {
none {
//
// the anonymous box inherits the attributes of its parents for now, so
// The anonymous box inherits the attributes of its parents for now, so
// that properties of intrinsic boxes are not spread to their parenting
// anonymous box.
//
// TODO: check what css actually specifies
// TODO: check what CSS actually specifies
//
let b = new_box(self.parent_node, InlineBox);
let b = @Box(self.parent_node, InlineBox);
self.anon_box = some(b);
b
}
@ -178,9 +170,9 @@ impl box_builder_priv for Node {
impl box_builder_methods for Node {
#[doc="Creates boxes for this node. This is the entry point."]
fn construct_boxes() -> @box {
fn construct_boxes() -> @Box {
let box_kind = self.determine_box_kind();
let my_box = new_box(self, box_kind);
let my_box = @Box(self, box_kind);
alt box_kind {
BlockBox | InlineBox {
let cx = create_context(self, my_box);

View File

@ -10,7 +10,7 @@ import layout::style::style::*; // ditto
import util::tree;
#[doc="The main reflow routine for inline layout."]
impl inline_layout_methods for @box {
impl inline_layout_methods for @Box {
fn reflow_inline(available_width: au) {
assert self.kind == InlineBox;

View File

@ -5,22 +5,24 @@ them to be rendered
"];
import task::*;
import comm::*;
import gfx::geometry::{au, au_to_px, box, px_to_au};
import geom::point::Point2D;
import geom::rect::Rect;
import gfx::renderer;
import box_builder::box_builder_methods;
import dl = display_list;
import dom::base::Node;
import dom::rcu::scope;
import dom::style::stylesheet;
import gfx::geometry::{au, au_to_px, box, px_to_au};
import gfx::renderer;
import layout::base::*;
import layout::style::apply::apply_style_methods;
import layout::style::apply::ApplyStyleBoxMethods;
import layout::style::style::style_methods;
import box_builder::box_builder_methods;
import dl = display_list;
import util::color::methods;
import geom::point::Point2D;
import geom::rect::Rect;
import task::*;
import comm::*;
enum Msg {
BuildMsg(Node, stylesheet),
PingMsg(chan<content::PingMsg>),
@ -68,7 +70,7 @@ Builds a display list for a box and all its children.
passed-in box.
"]
fn build_display_list_from_origin(box: @base::box, origin: Point2D<au>)
fn build_display_list_from_origin(box: @Box, origin: Point2D<au>)
-> dl::display_list {
let box_origin = Point2D(
px_to_au(au_to_px(origin.x) + au_to_px(box.bounds.origin.x)),
@ -86,7 +88,7 @@ fn build_display_list_from_origin(box: @base::box, origin: Point2D<au>)
ret list;
}
fn build_display_list(box : @base::box) -> dl::display_list {
fn build_display_list(box : @Box) -> dl::display_list {
ret build_display_list_from_origin(box, Point2D(au(0), au(0)));
}
@ -98,8 +100,7 @@ Args:
-origin: the coordinates of upper-left corner of the passed in box.
"]
fn box_to_display_item(box: @base::box, origin: Point2D<au>)
-> dl::display_item {
fn box_to_display_item(box: @Box, origin: Point2D<au>) -> dl::display_item {
let mut item;
#debug("request to display a box from origin %?", origin);
@ -108,27 +109,26 @@ fn box_to_display_item(box: @base::box, origin: Point2D<au>)
alt (box.appearance.background_image, box.appearance.background_color) {
(some(image), some(*)) | (some(image), none) {
item = dl::display_item({
item_type: dl::display_item_image(~copy *image),
bounds: bounds
});
item = dl::display_item({
item_type: dl::display_item_image(~copy *image),
bounds: bounds
});
}
(none, some(col)) {
#debug("Assigning color %? to box with bounds %?", col, bounds);
item = dl::display_item({
item_type: dl::display_item_solid_color(col.red, col.green,
col.blue),
bounds: bounds
});
item = dl::display_item({
item_type: dl::display_item_solid_color(col.red, col.green, col.blue),
bounds: bounds
});
}
(none, none) {
let r = rand::rng();
item = dl::display_item({
item_type: dl::display_item_solid_color(r.next() as u8,
r.next() as u8,
r.next() as u8),
bounds: bounds
});
item = dl::display_item({
item_type: dl::display_item_solid_color(r.next() as u8,
r.next() as u8,
r.next() as u8),
bounds: bounds
});
}
}

View File

@ -5,7 +5,7 @@ import image::base::load;
import layout::base::*;
import style::style_methods;
impl apply_style_methods for @box {
impl ApplyStyleBoxMethods for @Box {
fn apply_style_for_subtree() {
self.apply_style();
for btree.each_child(self) {

View File

@ -16,7 +16,7 @@ class text_box {
}
#[doc="The main reflow routine for text layout."]
impl text_layout_methods for @box {
impl text_layout_methods for @Box {
fn reflow_text(_available_width: au, subbox: @text_box) {
alt self.kind {
TextBox(*) { /* ok */ }