Make Box into a class

This commit is contained in:
Patrick Walton 2012-06-14 18:55:04 -07:00
parent 9ed8b20778
commit 6c6f7f99e4
7 changed files with 67 additions and 67 deletions

View file

@ -34,17 +34,25 @@ class appearance {
} }
} }
enum box = { class Box {
tree: tree::fields<@box>, let tree: tree::fields<@Box>;
node: Node, let node: Node;
mut bounds: Rect<au>, let kind: BoxKind;
kind: BoxKind, let mut bounds: Rect<au>;
appearance: appearance 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 = { enum layout_data = {
mut computed_style: computed_style, mut computed_style: computed_style,
mut box: option<@box> mut box: option<@Box>
}; };
enum ntree { ntree } enum ntree { ntree }
@ -59,27 +67,27 @@ impl of tree::rd_tree_ops<Node> for ntree {
} }
enum btree { btree } enum btree { btree }
impl of tree::rd_tree_ops<@box> for btree { impl of tree::rd_tree_ops<@Box> for btree {
fn each_child(node: @box, f: fn(&&@box) -> bool) { fn each_child(node: @Box, f: fn(&&@Box) -> bool) {
tree::each_child(self, node, f) 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) f(b.tree)
} }
} }
impl of tree::wr_tree_ops<@box> for btree { impl of tree::wr_tree_ops<@Box> for btree {
fn add_child(node: @box, child: @box) { fn add_child(node: @Box, child: @Box) {
tree::add_child(self, node, child) 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) f(b.tree)
} }
} }
impl layout_methods_priv for @box { impl layout_methods_priv for @Box {
#[doc="Dumps the box tree, for debugging, with indentation."] #[doc="Dumps the box tree, for debugging, with indentation."]
fn dump_indent(indent: uint) { fn dump_indent(indent: uint) {
let mut s = ""; 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."] #[doc="The main reflow routine."]
fn reflow(available_width: au) { fn reflow(available_width: au) {
alt self.kind { 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 = []; let mut r = [];
for tree::each_child(btree, root) {|c| for tree::each_child(btree, root) {|c|
r += flat_bounds(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; import util::tree;
#[doc="The public block layout methods."] #[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."] #[doc="The main reflow routine for block layout."]
fn reflow_block(available_width: au) { fn reflow_block(available_width: au) {
assert self.kind == BlockBox; 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::style::{display_type, di_block, di_inline, di_none};
import dom::rcu::reader_methods; import dom::rcu::reader_methods;
import gfx::geometry; import gfx::geometry;
import layout::base::{BlockBox, BoxKind, InlineBox, IntrinsicBox, NodeMethods, TextBox}; import layout::base::{BlockBox, Box, BoxKind, InlineBox, IntrinsicBox, NodeMethods, TextBox};
import layout::base::{appearance, box, btree, ntree, rd_tree_ops, wr_tree_ops}; import layout::base::{appearance, btree, ntree, rd_tree_ops, wr_tree_ops};
import layout::style::style::{style_methods}; import layout::style::style::{style_methods};
import layout::text::text_box; import layout::text::text_box;
import util::tree; import util::tree;
@ -17,7 +17,7 @@ enum ctxt = {
// The parent node that we're scanning. // The parent node that we're scanning.
parent_node: Node, parent_node: Node,
// The parent box that these boxes will be added to. // 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. // The current anonymous box that we're currently appending inline nodes to.
@ -25,18 +25,10 @@ enum ctxt = {
// See CSS2 9.2.1.1. // See CSS2 9.2.1.1.
// //
mut anon_box: option<@box> mut anon_box: option<@Box>
}; };
fn new_box(n: Node, kind: BoxKind) -> @box { fn create_context(parent_node: Node, parent_box: @Box) -> ctxt {
@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 {
ret ctxt({ ret ctxt({
parent_node: parent_node, parent_node: parent_node,
parent_box: parent_box, parent_box: parent_box,
@ -71,14 +63,14 @@ impl methods for ctxt {
let anon_box = alt self.anon_box { let anon_box = alt self.anon_box {
none { 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 // that properties of intrinsic boxes are not spread to their parenting
// anonymous box. // 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); self.anon_box = some(b);
b b
} }
@ -178,9 +170,9 @@ impl box_builder_priv for Node {
impl box_builder_methods for Node { impl box_builder_methods for Node {
#[doc="Creates boxes for this node. This is the entry point."] #[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 box_kind = self.determine_box_kind();
let my_box = new_box(self, box_kind); let my_box = @Box(self, box_kind);
alt box_kind { alt box_kind {
BlockBox | InlineBox { BlockBox | InlineBox {
let cx = create_context(self, my_box); let cx = create_context(self, my_box);

View file

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

View file

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

View file

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

View file

@ -16,7 +16,7 @@ class text_box {
} }
#[doc="The main reflow routine for text layout."] #[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) { fn reflow_text(_available_width: au, subbox: @text_box) {
alt self.kind { alt self.kind {
TextBox(*) { /* ok */ } TextBox(*) { /* ok */ }