NetSurf
Data Structures | Macros | Functions | Variables
textarea.c File Reference

Single/Multi-line UTF-8 text area (implementation) More...

#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <swis.h>
#include <oslib/colourtrans.h>
#include <oslib/osbyte.h>
#include <oslib/serviceinternational.h>
#include <oslib/wimp.h>
#include <oslib/wimpspriteop.h>
#include "utils/log.h"
#include "utils/utf8.h"
#include "utils/utils.h"
#include "riscos/gui.h"
#include "riscos/oslib_pre7.h"
#include "riscos/textarea.h"
#include "riscos/ucstables.h"
#include "riscos/wimp.h"
#include "riscos/wimp_event.h"
#include "riscos/wimputils.h"
Include dependency graph for textarea.c:

Go to the source code of this file.

Data Structures

struct  line_info
 
struct  text_area
 

Macros

#define MARGIN_LEFT   8
 
#define MARGIN_RIGHT   8
 
#define MAGIC   (('T'<<24) | ('E'<<16) | ('X'<<8) | 'T')
 
#define LINE_CHUNK_SIZE   256
 

Functions

static void ro_textarea_reflow (struct text_area *ta, unsigned int line)
 Reflow a text area from the given line onwards. More...
 
static bool ro_textarea_mouse_click (wimp_pointer *pointer)
 Handle mouse clicks in a text area. More...
 
static bool ro_textarea_key_press (wimp_key *key)
 Handle key presses in a text area. More...
 
static void ro_textarea_redraw (wimp_draw *redraw)
 Handle WIMP redraw requests for text areas. More...
 
static void ro_textarea_redraw_internal (wimp_draw *redraw, bool update)
 Internal textarea redraw routine. More...
 
static void ro_textarea_open (wimp_open *open)
 Handle a WIMP open window request. More...
 
uintptr_t ro_textarea_create (wimp_w parent, wimp_i icon, unsigned int flags, const char *font_family, unsigned int font_size, rufl_style font_style)
 Create a text area. More...
 
bool ro_textarea_update (uintptr_t self)
 Update the a text area following a change in the parent icon. More...
 
void ro_textarea_destroy (uintptr_t self)
 Destroy a text area. More...
 
bool ro_textarea_set_text (uintptr_t self, const char *text)
 Set the text in a text area, discarding any current text. More...
 
int ro_textarea_get_text (uintptr_t self, char *buf, unsigned int len)
 Extract the text from a text area. More...
 
void ro_textarea_insert_text (uintptr_t self, unsigned int index, const char *text)
 Insert text into the text area. More...
 
void ro_textarea_replace_text (uintptr_t self, unsigned int start, unsigned int end, const char *text)
 Replace text in a text area. More...
 
void ro_textarea_set_caret (uintptr_t self, unsigned int caret)
 Set the caret's position. More...
 
void ro_textarea_set_caret_xy (uintptr_t self, int x, int y)
 Set the caret's position. More...
 
unsigned int ro_textarea_get_caret (uintptr_t self)
 Get the caret's position. More...
 

Variables

static wimp_window text_area_definition
 

Detailed Description

Single/Multi-line UTF-8 text area (implementation)

Definition in file textarea.c.

Macro Definition Documentation

◆ LINE_CHUNK_SIZE

#define LINE_CHUNK_SIZE   256

Definition at line 83 of file textarea.c.

◆ MAGIC

#define MAGIC   (('T'<<24) | ('E'<<16) | ('X'<<8) | 'T')

Definition at line 55 of file textarea.c.

◆ MARGIN_LEFT

#define MARGIN_LEFT   8

Definition at line 46 of file textarea.c.

◆ MARGIN_RIGHT

#define MARGIN_RIGHT   8

Definition at line 47 of file textarea.c.

Function Documentation

◆ ro_textarea_create()

uintptr_t ro_textarea_create ( wimp_w  parent,
wimp_i  icon,
unsigned int  flags,
const char *  font_family,
unsigned int  font_size,
rufl_style  font_style 
)

Create a text area.

Parameters
parentParent window
iconIcon in parent window to replace
flagsText area flags
font_familyRUfl font family to use, or NULL for default
font_sizeFont size to use (pt * 16), or 0 for default
font_styleFont style to use, or 0 for default
Returns
Opaque handle for textarea or 0 on error
Todo:
Better line height calculation

Definition at line 133 of file textarea.c.

References text_area::caret_pos, text_area::char_off, text_area::flags, text_area::font_family, text_area::font_size, text_area::font_style, text_area::icon, text_area::line, text_area::line_count, text_area::line_height, text_area::line_spacing, text_area::lines, MAGIC, text_area::magic, NSLOG, parent, text_area::parent, ro_gui_wimp_event_register_keypress(), ro_gui_wimp_event_register_mouse_click(), ro_gui_wimp_event_register_open_window(), ro_gui_wimp_event_register_redraw_window(), ro_gui_wimp_event_set_user_data(), ro_textarea_destroy(), ro_textarea_key_press(), ro_textarea_mouse_click(), ro_textarea_open(), ro_textarea_redraw(), ro_textarea_update(), text_area::text, text_area::text_alloc, text_area_definition, text_area::text_len, TEXTAREA_READONLY, and text_area::window.

Here is the call graph for this function:

◆ ro_textarea_destroy()

void ro_textarea_destroy ( uintptr_t  self)

Destroy a text area.

Parameters
selfText area to destroy

Definition at line 305 of file textarea.c.

References text_area::font_family, MAGIC, text_area::magic, NSLOG, ro_gui_wimp_event_finalise(), text_area::text, and text_area::window.

Referenced by ro_textarea_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_get_caret()

unsigned int ro_textarea_get_caret ( uintptr_t  self)

Get the caret's position.

Parameters
selfText area
Returns
0-based character index of caret location, or -1 on error

Definition at line 677 of file textarea.c.

References line_info::b_start, text_area::caret_pos, text_area::char_off, text_area::line, text_area::lines, MAGIC, text_area::magic, NSLOG, text_area::text, text_area::text_len, and utf8_next().

Referenced by ro_textarea_key_press(), and ro_textarea_set_caret().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_get_text()

int ro_textarea_get_text ( uintptr_t  self,
char *  buf,
unsigned int  len 
)

Extract the text from a text area.

Parameters
selfText area
bufPointer to buffer to receive data, or NULL to read length required
lenLength (bytes) of buffer pointed to by buf, or 0 to read length
Returns
Length (bytes) written/required or -1 on error

Definition at line 372 of file textarea.c.

References MAGIC, text_area::magic, NSLOG, text_area::text, and text_area::text_len.

◆ ro_textarea_insert_text()

void ro_textarea_insert_text ( uintptr_t  self,
unsigned int  index,
const char *  text 
)

Insert text into the text area.

Parameters
selfText area
index0-based character index to insert at
textUTF-8 text to insert
Todo:
calculate line to reflow from

Definition at line 404 of file textarea.c.

References MAGIC, text_area::magic, NSLOG, ro_textarea_reflow(), text_area::text, text(), text_area::text_alloc, text_area::text_len, utf8_length(), and utf8_next().

Referenced by ro_textarea_key_press(), and ro_textarea_replace_text().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_key_press()

bool ro_textarea_key_press ( wimp_key *  key)
static

Handle key presses in a text area.

Parameters
keyKey pressed state block
Returns
true if press handled, false otherwise
Todo:
handle command keys
Todo:
Move caret up a line
Todo:
Move caret down a line
Todo:
line start
Todo:
line end
Todo:
line end

pass on RETURN and ESCAPE to the parent icon

Definition at line 915 of file textarea.c.

References fallthrough, IS_WIMP_KEY, NSLOG, os_version, RISCOS5, ro_gui_wimp_event_get_user_data(), ro_textarea_get_caret(), ro_textarea_insert_text(), ro_textarea_redraw_internal(), ro_textarea_replace_text(), ro_textarea_set_caret(), ro_textarea_set_text(), TEXTAREA_MULTILINE, TEXTAREA_READONLY, utf8_from_ucs4(), and utf8_length().

Referenced by ro_textarea_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_mouse_click()

bool ro_textarea_mouse_click ( wimp_pointer *  pointer)
static

Handle mouse clicks in a text area.

Parameters
pointerMouse click state block
Returns
true if click handled, false otherwise

Definition at line 899 of file textarea.c.

References ro_gui_wimp_event_get_user_data(), and ro_textarea_set_caret_xy().

Referenced by ro_textarea_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_open()

void ro_textarea_open ( wimp_open *  open)
static

Handle a WIMP open window request.

Parameters
openOpenWindow block

Definition at line 1191 of file textarea.c.

References NSLOG.

Referenced by ro_textarea_create().

Here is the caller graph for this function:

◆ ro_textarea_redraw()

void ro_textarea_redraw ( wimp_draw *  redraw)
static

Handle WIMP redraw requests for text areas.

Parameters
redrawRedraw request block

Definition at line 1065 of file textarea.c.

References ro_textarea_redraw_internal().

Referenced by ro_textarea_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_redraw_internal()

void ro_textarea_redraw_internal ( wimp_draw *  redraw,
bool  update 
)
static

Internal textarea redraw routine.

Parameters
redrawRedraw/update request block
updateTrue if update, false if full redraw

Definition at line 1076 of file textarea.c.

References line_info::b_length, line_info::b_start, colourtrans_SET_BG_GCOL, colourtrans_USE_ECFS_GCOL, text_area::flags, text_area::font_family, text_area::font_size, text_area::font_style, line(), text_area::line_count, text_area::line_height, text_area::line_spacing, text_area::lines, MARGIN_LEFT, NSLOG, ro_gui_wimp_event_get_user_data(), text_area::text, and TEXTAREA_READONLY.

Referenced by ro_textarea_key_press(), and ro_textarea_redraw().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_reflow()

void ro_textarea_reflow ( struct text_area ta,
unsigned int  line 
)
static

Reflow a text area from the given line onwards.

Todo:
Selection handling
Parameters
taText area to reflow
lineLine number to begin reflow on
Todo:
pay attention to line parameter
Todo:
create horizontal scrollbar if needed

Definition at line 704 of file textarea.c.

References line_info::b_length, line_info::b_start, text_area::flags, text_area::font_family, text_area::font_size, text_area::font_style, LINE_CHUNK_SIZE, text_area::line_count, text_area::line_height, text_area::line_spacing, text_area::lines, MARGIN_LEFT, MARGIN_RIGHT, NSLOG, parent, PTR_WIMP_OPEN, ro_get_vscroll_width(), ro_gui_wimp_check_window_furniture(), ro_gui_wimp_update_window_furniture(), ro_textarea_reflow(), text_area::text, text(), text_area::text_len, TEXTAREA_MULTILINE, text_area::vis_height, text_area::vis_width, and text_area::window.

Referenced by ro_textarea_insert_text(), ro_textarea_reflow(), ro_textarea_replace_text(), ro_textarea_set_text(), and ro_textarea_update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_replace_text()

void ro_textarea_replace_text ( uintptr_t  self,
unsigned int  start,
unsigned int  end,
const char *  text 
)

Replace text in a text area.

Parameters
selfText area
startStart character index of replaced section (inclusive)
endEnd character index of replaced section (exclusive)
textUTF-8 text to insert
Todo:
calculate line to reflow from

Definition at line 458 of file textarea.c.

References MAGIC, text_area::magic, NSLOG, ro_textarea_insert_text(), ro_textarea_reflow(), text_area::text, text(), text_area::text_alloc, text_area::text_len, utf8_length(), and utf8_next().

Referenced by ro_textarea_key_press().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_set_caret()

void ro_textarea_set_caret ( uintptr_t  self,
unsigned int  caret 
)

Set the caret's position.

Parameters
selfText area
caret0-based character index to place caret at

Definition at line 529 of file textarea.c.

References line_info::b_length, line_info::b_start, text_area::caret_pos, text_area::char_off, text_area::font_family, text_area::font_size, text_area::font_style, text_area::line, text_area::line_count, text_area::line_height, text_area::line_spacing, text_area::lines, MAGIC, text_area::magic, MARGIN_LEFT, NSLOG, ro_convert_pixels_to_os_units(), ro_textarea_get_caret(), text_area::text, text_area::text_len, utf8_length(), utf8_next(), and text_area::window.

Referenced by ro_textarea_key_press(), and ro_textarea_set_caret_xy().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_set_caret_xy()

void ro_textarea_set_caret_xy ( uintptr_t  self,
int  x,
int  y 
)

Set the caret's position.

Parameters
selfText area
xX position of caret on the screen
yY position of caret on the screen

Definition at line 609 of file textarea.c.

References line_info::b_length, line_info::b_start, text_area::flags, text_area::font_family, text_area::font_size, text_area::font_style, line(), text_area::line_count, text_area::line_height, text_area::line_spacing, text_area::lines, MAGIC, text_area::magic, MARGIN_LEFT, NSLOG, ro_convert_pixels_to_os_units(), ro_textarea_set_caret(), text_area::text, text_area::text_len, TEXTAREA_READONLY, utf8_next(), and text_area::window.

Referenced by ro_textarea_mouse_click().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_set_text()

bool ro_textarea_set_text ( uintptr_t  self,
const char *  text 
)

Set the text in a text area, discarding any current text.

Parameters
selfText area
textUTF-8 text to set text area's contents to
Returns
true on success, false on memory exhaustion

Definition at line 334 of file textarea.c.

References MAGIC, text_area::magic, NSLOG, ro_textarea_reflow(), text_area::text, text(), text_area::text_alloc, and text_area::text_len.

Referenced by ro_textarea_key_press().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ro_textarea_update()

bool ro_textarea_update ( uintptr_t  self)

Update the a text area following a change in the parent icon.

Parameters
selfText area to update

Definition at line 218 of file textarea.c.

References text_area::flags, text_area::icon, MAGIC, text_area::magic, NSLOG, text_area::parent, PTR_WIMP_OPEN, ro_get_hscroll_height(), ro_get_vscroll_width(), ro_textarea_reflow(), TEXTAREA_READONLY, text_area::vis_height, text_area::vis_width, and text_area::window.

Referenced by ro_textarea_create().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ text_area_definition

wimp_window text_area_definition
static
Initial value:
= {
{0, 0, 16, 16},
0,
0,
wimp_TOP,
wimp_WINDOW_NEW_FORMAT | wimp_WINDOW_NO_BOUNDS,
wimp_COLOUR_BLACK,
wimp_COLOUR_LIGHT_GREY,
wimp_COLOUR_LIGHT_GREY,
wimp_COLOUR_VERY_LIGHT_GREY,
wimp_COLOUR_DARK_GREY,
wimp_COLOUR_MID_LIGHT_GREY,
wimp_COLOUR_CREAM,
0,
{0, -16384, 16384, 0},
wimp_ICON_TEXT | wimp_ICON_HCENTRED | wimp_ICON_VCENTRED,
wimp_BUTTON_CLICK << wimp_ICON_BUTTON_TYPE_SHIFT,
wimpspriteop_AREA,
1,
1,
{""},
0,
{}
}

Definition at line 90 of file textarea.c.

Referenced by ro_textarea_create().