Skip to content

Commit

Permalink
Roughly implemented mhtml input control buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
indigoparadox committed May 24, 2024
1 parent 55c5d3c commit 7eda7c3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 14 deletions.
34 changes: 32 additions & 2 deletions src/mhtml.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
/*! \brief Indicates a text tag contains CSS information to be parsed. */
#define MHTML_TAG_FLAG_STYLE 0x02

#define MHTML_INPUT_TYPE_BUTTON 0x01

#include <mparser.h>
#include <mcss.h>

Expand All @@ -38,7 +40,9 @@
f( CLASS, 2 ) \
f( ID, 3 ) \
f( NAME, 4 ) \
f( SRC, 5 )
f( SRC, 5 ) \
f( TYPE, 6 ) \
f( VALUE, 7 )

#define MHTML_TAG_TABLE( f ) \
f( 0, NONE, void* none;, NONE ) \
Expand All @@ -51,7 +55,8 @@
f( 7, SPAN, void* none;, INLINE ) \
f( 8, BR, void* none;, BLOCK ) \
f( 9, STYLE, void* none;, NONE ) \
f( 10, IMG, char src[MHTML_SRC_HREF_SZ_MAX + 1]; size_t src_sz;, BLOCK )
f( 10, IMG, char src[MHTML_SRC_HREF_SZ_MAX + 1]; size_t src_sz;, BLOCK ) \
f( 11, INPUT, uint8_t input_type; char name[MCSS_ID_SZ_MAX + 1]; size_t name_sz; char value[MCSS_ID_SZ_MAX + 1]; size_t value_sz;, INLINE )

#define MHTML_PARSER_PSTATE_TABLE( f ) \
f( MHTML_PSTATE_NONE, 0 ) \
Expand Down Expand Up @@ -557,11 +562,36 @@ MERROR_RETVAL mhtml_push_attrib_val( struct MHTML_PARSER* parser ) {
parser->tags[parser->tag_iter].base.id_sz = parser->token_sz;

} else if( MHTML_ATTRIB_KEY_SRC == parser->attrib_key ) {
/* TODO: Validate tag type. */
strncpy(
parser->tags[parser->tag_iter].IMG.src,
parser->token,
MHTML_SRC_HREF_SZ_MAX );
parser->tags[parser->tag_iter].IMG.src_sz = parser->token_sz;

} else if( MHTML_ATTRIB_KEY_TYPE == parser->attrib_key ) {
/* TODO: Validate tag type. */

if( 0 == strncpy( parser->token, "button", 7 ) ) {
parser->tags[parser->tag_iter].INPUT.input_type =
MHTML_INPUT_TYPE_BUTTON;
}

} else if( MHTML_ATTRIB_KEY_NAME == parser->attrib_key ) {
/* TODO: Validate tag type. */
strncpy(
parser->tags[parser->tag_iter].INPUT.name,
parser->token,
MCSS_ID_SZ_MAX );
parser->tags[parser->tag_iter].INPUT.name_sz = parser->token_sz;

} else if( MHTML_ATTRIB_KEY_VALUE == parser->attrib_key ) {
/* TODO: Validate tag type. */
strncpy(
parser->tags[parser->tag_iter].INPUT.value,
parser->token,
MCSS_ID_SZ_MAX );
parser->tags[parser->tag_iter].INPUT.value_sz = parser->token_sz;
}

cleanup:
Expand Down
81 changes: 69 additions & 12 deletions src/mhtmr.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct MHTMR_RENDER_TREE {
size_t nodes_sz_max;
/* TODO: Make this per-node. */
MAUG_MHANDLE font_h;
struct RETROGUI gui;
};

/* TODO: Function names should be verb_noun! */
Expand Down Expand Up @@ -412,6 +413,9 @@ MERROR_RETVAL mhtmr_tree_create(

retval = retrofont_load( "unscii-8.hex", &(tree->font_h), 8, 33, 93 );

/* Create a GUI handler just for this tree. */
retval = retrogui_init( &(tree->gui) );

cleanup:

return retval;
Expand Down Expand Up @@ -562,10 +566,10 @@ MERROR_RETVAL mhtmr_tree_size(
maug_mlock( tree->font_h, font );
maug_cleanup_if_null_alloc( struct RETROFONT*, font );

/* TODO: Constrain node text size to parent size. */
retrofont_string_sz(
NULL, tag_content, mhtml_tag( parser, tag_idx )->TEXT.content_sz,
font,
/* Constrain node text size to parent size. */
mhtmr_node_parent( tree, node_idx )->w,
mhtmr_node_parent( tree, node_idx )->h,
&(mhtmr_node( tree, node_idx )->w),
Expand All @@ -578,6 +582,30 @@ MERROR_RETVAL mhtmr_tree_size(

maug_munlock( mhtml_tag( parser, tag_idx )->TEXT.content, tag_content );

} else if(
0 <= tag_idx &&
MHTML_TAG_TYPE_INPUT == mhtml_tag( parser, tag_idx )->base.type
) {

maug_mlock( tree->font_h, font );
maug_cleanup_if_null_alloc( struct RETROFONT*, font );

retrofont_string_sz(
NULL,
mhtml_tag( parser, tag_idx )->INPUT.value,
mhtml_tag( parser, tag_idx )->INPUT.value_sz,
font,
mhtmr_node_parent( tree, node_idx )->w,
mhtmr_node_parent( tree, node_idx )->h,
&(mhtmr_node( tree, node_idx )->w),
&(mhtmr_node( tree, node_idx )->h), 0 );

/* Add space for borders and stuff. (TODO: Add to retrogui!) */
mhtmr_node( tree, node_idx )->w += 8;
mhtmr_node( tree, node_idx )->h += 8;

maug_munlock( tree->font_h, font );

} else if(
0 <= tag_idx &&
MHTML_TAG_TYPE_IMG == mhtml_tag( parser, tag_idx )->base.type
Expand Down Expand Up @@ -1090,6 +1118,8 @@ void mhtmr_tree_draw(
union MHTML_TAG* tag = NULL;
struct MHTMR_RENDER_NODE* node = NULL;
struct RETROFONT* font = NULL;
union RETROGUI_CTL ctl;
MERROR_RETVAL retval = MERROR_OK;

node = mhtmr_node( tree, node_idx );

Expand Down Expand Up @@ -1135,7 +1165,7 @@ void mhtmr_tree_draw(
}

} else if( MHTML_TAG_TYPE_IMG == tag->base.type ) {
/* TODO: Load and blit the image. */
/* Blit the image. */

if( retroflat_bitmap_ok( &(mhtmr_node( tree, node_idx )->bitmap) ) ) {
retroflat_blit_bitmap(
Expand All @@ -1149,6 +1179,31 @@ void mhtmr_tree_draw(
mhtmr_node( tree, node_idx )->h */ );
}

} else if( MHTML_TAG_TYPE_INPUT == tag->base.type ) {
/* Push the control (for the client renderer to redraw later). */

/* TODO: This shouldn't be in the drawing callback! */

retrogui_lock( &(tree->gui) );

if(
/* Use the same ID for the node and control it creates. */
MERROR_OK != retrogui_init_ctl(
&ctl, RETROGUI_CTL_TYPE_BUTTON, node_idx )
) {
retrogui_unlock( &(tree->gui) );
goto cleanup;
}

ctl.base.x = mhtmr_node( tree, node_idx )->x;
ctl.base.y = mhtmr_node( tree, node_idx )->y;
ctl.base.w = mhtmr_node( tree, node_idx )->w;
ctl.base.h = mhtmr_node( tree, node_idx )->h;

retrogui_push_ctl( &(tree->gui), &ctl );

retrogui_unlock( &(tree->gui) );

} else {
if( RETROFLAT_COLOR_NULL != node->bg ) {
retroflat_rect(
Expand All @@ -1161,21 +1216,21 @@ void mhtmr_tree_draw(
}
}

mhtmr_tree_draw( parser, tree, node->first_child, d + 1 );

mhtmr_tree_draw( parser, tree, node->next_sibling, d );
cleanup:

#if 0
/* Perform the actual render. */
if( retrogui_is_locked( &(tree->gui) ) ) {
retrogui_unlock( &(tree->gui) );
}

if( MERROR_OK != retval ) {
error_printf( "failed drawing node: " SIZE_T_FMT, node_idx );
}

memcpy( &r, parent_r, sizeof( struct MHTML_RENDER ) );
/* Keep trying to render children, tho. */

/* Descend into children/siblings. */
r.y += effect_style.PADDING_TOP;
r.x += effect_style.PADDING_LEFT;
mhtmr_tree_draw( parser, tree, node->first_child, d + 1 );

#endif
mhtmr_tree_draw( parser, tree, node->next_sibling, d );
}

void mhtmr_tree_dump(
Expand Down Expand Up @@ -1221,6 +1276,8 @@ void mhtmr_tree_free( struct MHTMR_RENDER_TREE* tree ) {

/* TODO: Free bitmaps from img! */

/* TODO: Free GUI! */

if( NULL != tree->font_h ) {
maug_mfree( tree->font_h );
}
Expand Down

0 comments on commit 7eda7c3

Please sign in to comment.