[dia] style: rework text[line]
- From: Zander Brown <zbrown src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [dia] style: rework text[line]
- Date: Tue, 21 Jul 2020 01:07:36 +0000 (UTC)
commit 109725bee8f48f67e74748506618ad47528b0901
Author: Zander Brown <zbrown gnome org>
Date: Tue Jul 21 00:30:36 2020 +0100
style: rework text[line]
lib/text.c | 695 ++++++++++++++++++++++++++++++++++-----------------------
lib/textline.h | 2 +-
2 files changed, 413 insertions(+), 284 deletions(-)
---
diff --git a/lib/text.c b/lib/text.c
index fd744e1f..4cfb7c7c 100644
--- a/lib/text.c
+++ b/lib/text.c
@@ -57,7 +57,7 @@ struct TextObjectChange {
gunichar ch;
int pos;
int row;
- gchar *str;
+ char *str;
/* the owning object ... */
DiaObject *obj;
@@ -69,157 +69,219 @@ struct TextObjectChange {
#define CURSOR_HEIGHT_RATIO 20
-/* *** Encapsulation functions for transferring to text_line *** */
-gchar *
-text_get_line(const Text *text, int line)
+/**
+ * text_get_line:
+ *
+ * Encapsulation functions for transferring to text_line
+ *
+ * Since: dawn-of-time
+ */
+char *
+text_get_line (const Text *text, int line)
{
- return text_line_get_string(text->lines[line]);
+ return text_line_get_string (text->lines[line]);
}
-/** Raw sets one line to a given text, not copying, not freeing.
+
+/**
+ * text_set_line:
+ *
+ * Raw sets one line to a given text, not copying, not freeing.
+ *
+ * Since: dawn-of-time
*/
static void
-text_set_line(Text *text, int line_no, gchar *line)
+text_set_line (Text *text, int line_no, char *line)
{
- text_line_set_string(text->lines[line_no], line);
+ text_line_set_string (text->lines[line_no], line);
}
-/** Set the text of a line, freeing, copying and mallocing as required.
+
+/**
+ * text_set_line_text:
+ *
+ * Set the text of a line, freeing, copying and mallocing as required.
* Updates strlen and row_width entries, but not max_width.
+ *
+ * Since: dawn-of-time
*/
static void
-text_set_line_text(Text *text, int line_no, gchar *line)
+text_set_line_text (Text *text, int line_no, char *line)
{
- text_set_line(text, line_no, line);
+ text_set_line (text, line_no, line);
}
-/** Delete the line, freeing appropriately and moving stuff up.
+
+/**
+ * text_delete_line:
+ *
+ * Delete the line, freeing appropriately and moving stuff up.
* This function circumvents the normal free/alloc cycle of
- * text_set_line_text. */
+ * text_set_line_text.
+ *
+ * Since: dawn-of-time
+ */
static void
-text_delete_line(Text *text, int line_no)
+text_delete_line (Text *text, int line_no)
{
- int i;
-
g_clear_pointer (&text->lines[line_no], g_free);
- for (i = line_no; i < text->numlines - 1; i++) {
+
+ for (int i = line_no; i < text->numlines - 1; i++) {
text->lines[i] = text->lines[i+1];
}
+
text->numlines -= 1;
- text->lines = g_realloc(text->lines, sizeof(TextLine *)*text->numlines);
+ text->lines = g_realloc (text->lines, sizeof (TextLine *) * text->numlines);
}
-/** Insert a new (empty) line at line_no.
+
+/**
+ * text_insert_line:
+ *
+ * Insert a new (empty) line at line_no.
* This function circumvents the normal free/alloc cycle of
- * text_set_line_text. */
+ * text_set_line_text.
+ *
+ * Since: dawn-of-time
+ */
static void
-text_insert_line(Text *text, int line_no)
+text_insert_line (Text *text, int line_no)
{
- int i;
text->numlines += 1;
- text->lines = g_realloc(text->lines, sizeof(char *)*text->numlines);
+ text->lines = g_realloc (text->lines, sizeof (char *) * text->numlines);
- for (i = text->numlines - 1; i > line_no; i--) {
+ for (int i = text->numlines - 1; i > line_no; i--) {
text->lines[i] = text->lines[i - 1];
}
- text->lines[line_no] = text_line_new("", text->font, text->height);;
+ text->lines[line_no] = text_line_new ("", text->font, text->height);
}
-/** Get the in-diagram width of the given line.
- * @param text The text object;
- * @param line_no The index of the line in the text object, starting at 0.
- * @returns The width in cm of the indicated line.
+
+/**
+ * text_get_line_width:
+ * @text: The text object;
+ * @line_no: The index of the line in the text object, starting at 0.
+ *
+ * Get the in-diagram width of the given line.
+ *
+ * Returns: The width in cm of the indicated line.
+ *
+ * Since: dawn-of-time
*/
-real
-text_get_line_width(const Text *text, int line_no)
+double
+text_get_line_width (const Text *text, int line_no)
{
- return text_line_get_width(text->lines[line_no]);
+ return text_line_get_width (text->lines[line_no]);
}
-/** Get the number of characters of the given line.
- * @param text The text object;
- * @param line_no The index of the line in the text object, starting at 0.
- * @returns The number of UTF-8 characters of the indicated line.
+
+/**
+ * text_get_line_strlen:
+ * @text: The text object;
+ * @line_no: The index of the line in the text object, starting at 0.
+ *
+ * Get the number of characters of the given line.
+ *
+ * Returns: The number of UTF-8 characters of the indicated line.
+ *
+ * Since: dawn-of-time
*/
int
-text_get_line_strlen(const Text *text, int line_no)
+text_get_line_strlen (const Text *text, int line_no)
{
- return g_utf8_strlen(text_line_get_string(text->lines[line_no]), -1);
+ return g_utf8_strlen (text_line_get_string (text->lines[line_no]), -1);
}
-real
-text_get_max_width(Text *text)
+
+double
+text_get_max_width (Text *text)
{
return text->max_width;
}
-/** Get the *average* ascent of this Text object.
- * @param a Text object
- * @returns the average of the ascents of each line (height above baseline)
+
+/**
+ * text_get_ascent:
+ * @text: Text object
+ *
+ * Get the *average* ascent of this #Text object.
+ *
+ * Returns: the average of the ascents of each line (height above baseline)
*/
-real
-text_get_ascent(Text *text)
+double
+text_get_ascent (Text *text)
{
return text->ascent;
}
-/** Get the *average* descent of this Text object.
- * @param a Text object
- * @returns the average of the descents of each line (height below baseline)
+
+/**
+ * text_get_descent:
+ * @a: #Text object
+ *
+ * Get the *average* descent of this Text object.
+ *
+ * Returns: the average of the descents of each line (height below baseline)
+ *
+ * Since: dawn-of-time
*/
-real
-text_get_descent(Text *text)
+double
+text_get_descent (Text *text)
{
return text->descent;
}
-static ObjectChange *text_create_change(Text *text, TextChangeType type,
- gunichar ch, int pos, int row,
- DiaObject *obj);
+
+static ObjectChange *text_create_change (Text *text,
+ TextChangeType type,
+ gunichar ch,
+ int pos,
+ int row,
+ DiaObject *obj);
+
static void
-calc_width(Text *text)
+calc_width (Text *text)
{
- real width;
- int i;
+ double width;
width = 0.0;
- for (i = 0; i < text->numlines; i++) {
- width = MAX(width, text_get_line_width(text, i));
+ for (int i = 0; i < text->numlines; i++) {
+ width = MAX (width, text_get_line_width (text, i));
}
text->max_width = width;
}
+
static void
calc_ascent_descent(Text *text)
{
- real sig_a = 0.0,sig_d = 0.0;
- gint i;
+ double sig_a = 0.0,sig_d = 0.0;
- for ( i = 0; i < text->numlines; i++) {
- sig_a += text_line_get_ascent(text->lines[i]);
- sig_d += text_line_get_descent(text->lines[i]);
+ for (int i = 0; i < text->numlines; i++) {
+ sig_a += text_line_get_ascent (text->lines[i]);
+ sig_d += text_line_get_descent (text->lines[i]);
}
- text->ascent = sig_a / (real)text->numlines;
- text->descent = sig_d / (real)text->numlines;
+ text->ascent = sig_a / (double) text->numlines;
+ text->descent = sig_d / (double) text->numlines;
}
+
static void
-free_string(Text *text)
+free_string (Text *text)
{
- int i;
-
- for (i=0;i<text->numlines;i++) {
+ for (int i = 0; i < text->numlines; i++) {
text_line_destroy(text->lines[i]);
}
g_clear_pointer (&text->lines, g_free);
}
+
static void
-set_string(Text *text, const char *string)
+set_string (Text *text, const char *string)
{
int numlines, i;
const char *s,*s2;
@@ -237,36 +299,36 @@ set_string(Text *text, const char *string)
}
numlines = 1;
if (s != NULL)
- while ( (s = g_utf8_strchr(s, -1, '\n')) != NULL ) {
+ while ( (s = g_utf8_strchr (s, -1, '\n')) != NULL ) {
numlines++;
if (*s) {
- s = g_utf8_next_char(s);
+ s = g_utf8_next_char (s);
}
}
text->numlines = numlines;
- text->lines = g_new0(TextLine *, numlines);
+ text->lines = g_new0 (TextLine *, numlines);
for (i = 0; i < numlines; i++) {
- text->lines[i] = text_line_new("", text->font, text->height);
+ text->lines[i] = text_line_new ("", text->font, text->height);
}
s = fallback ? fallback : string;
if (s == NULL) {
- text_set_line_text(text, 0, "");
+ text_set_line_text (text, 0, "");
return;
}
for (i = 0; i < numlines; i++) {
- gchar *string_line;
- s2 = g_utf8_strchr(s, -1, '\n');
+ char *string_line;
+ s2 = g_utf8_strchr (s, -1, '\n');
if (s2 == NULL) { /* No newline */
- s2 = s + strlen(s);
+ s2 = s + strlen (s);
}
- string_line = g_strndup(s, s2 - s);
- text_set_line_text(text, i, string_line);
+ string_line = g_strndup (s, s2 - s);
+ text_set_line_text (text, i, string_line);
g_clear_pointer (&string_line, g_free);
s = s2;
if (*s) {
- s = g_utf8_next_char(s);
+ s = g_utf8_next_char (s);
}
}
@@ -274,25 +336,28 @@ set_string(Text *text, const char *string)
text->cursor_row = text->numlines - 1;
}
- if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row)) {
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ if (text->cursor_pos > text_get_line_strlen (text, text->cursor_row)) {
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
}
g_clear_pointer (&fallback, g_free);
}
+
void
-text_set_string(Text *text, const char *string)
+text_set_string (Text *text, const char *string)
{
- if (text->lines != NULL)
- free_string(text);
+ if (text->lines != NULL) {
+ free_string (text);
+ }
- set_string(text, string);
+ set_string (text, string);
}
+
Text *
new_text (const char *string,
DiaFont *font,
- real height,
+ double height,
Point *pos,
Color *color,
Alignment align)
@@ -323,7 +388,10 @@ new_text (const char *string,
return text;
}
-/*!
+
+/**
+ * new_text_default:
+ *
* Fallback function returning a default initialized text object.
*/
Text *
@@ -331,7 +399,7 @@ new_text_default (Point *pos, Color *color, Alignment align)
{
Text *text;
DiaFont *font;
- real font_height;
+ double font_height;
attributes_get_default_font (&font, &font_height);
text = new_text ("", font, font_height, pos, color, align);
@@ -340,8 +408,9 @@ new_text_default (Point *pos, Color *color, Alignment align)
return text;
}
+
Text *
-text_copy(Text *text)
+text_copy (Text *text)
{
Text *copy;
int i;
@@ -377,6 +446,7 @@ text_copy(Text *text)
return copy;
}
+
void
text_destroy (Text *text)
{
@@ -385,34 +455,35 @@ text_destroy (Text *text)
g_clear_pointer (&text, g_free);
}
+
void
-text_set_height(Text *text, real height)
+text_set_height (Text *text, double height)
{
- int i;
text->height = height;
- for (i = 0; i < text->numlines; i++) {
- text_line_set_height(text->lines[i], height);
+ for (int i = 0; i < text->numlines; i++) {
+ text_line_set_height (text->lines[i], height);
}
- calc_width(text);
- calc_ascent_descent(text);
+ calc_width (text);
+ calc_ascent_descent (text);
}
-real
-text_get_height(const Text *text)
+
+double
+text_get_height (const Text *text)
{
return text->height;
}
+
void
text_set_font (Text *text, DiaFont *font)
{
DiaFont *old_font = text->font;
- int i;
text->font = g_object_ref (font);
g_clear_object (&old_font);
- for (i = 0; i < text->numlines; i++) {
+ for (int i = 0; i < text->numlines; i++) {
text_line_set_font (text->lines[i], font);
}
@@ -420,14 +491,16 @@ text_set_font (Text *text, DiaFont *font)
calc_ascent_descent (text);
}
+
void
-text_set_position(Text *text, Point *pos)
+text_set_position (Text *text, Point *pos)
{
text->position = *pos;
}
+
void
-text_set_color(Text *text, Color *col)
+text_set_color (Text *text, Color *col)
{
text->color = *col;
}
@@ -482,8 +555,8 @@ text_calc_boundingbox (Text *text, DiaRectangle *box)
box->left -= height/(CURSOR_HEIGHT_RATIO*2);
} else {
/* Half the cursor width. Assume that
- if it isn't at position zero, it might be
- at the last position possible. */
+ if it isn't at position zero, it might be
+ at the last position possible. */
box->right += height/(CURSOR_HEIGHT_RATIO*2);
}
@@ -495,20 +568,20 @@ text_calc_boundingbox (Text *text, DiaRectangle *box)
char *
-text_get_string_copy(const Text *text)
+text_get_string_copy (const Text *text)
{
- int num,i;
+ int num;
char *str;
num = 0;
- for (i=0;i<text->numlines;i++) {
+ for (int i = 0; i < text->numlines; i++) {
/* This is for allocation, so it should not use g_utf8_strlen() */
- num += strlen(text_get_line(text, i))+1;
+ num += strlen (text_get_line (text, i)) + 1;
}
str = g_malloc0 (num);
- for (i = 0; i < text->numlines; i++) {
+ for (int i = 0; i < text->numlines; i++) {
strcat (str, text_get_line (text, i));
if (i != (text->numlines - 1)) {
strcat (str, "\n");
@@ -518,16 +591,18 @@ text_get_string_copy(const Text *text)
return str;
}
-real
-text_distance_from(Text *text, Point *point)
+
+double
+text_distance_from (Text *text, Point *point)
{
- real dx, dy;
- real topy, bottomy;
- real left, right;
+ double dx, dy;
+ double topy, bottomy;
+ double left, right;
int line;
topy = text->position.y - text->ascent;
- bottomy = text->position.y + text->descent + text->height*(text->numlines-1);
+ bottomy = text->position.y + text->descent + text->height *
+ (text->numlines - 1);
if (point->y <= topy) {
dy = topy - point->y;
line = 0;
@@ -536,9 +611,10 @@ text_distance_from(Text *text, Point *point)
line = text->numlines - 1;
} else {
dy = 0.0;
- line = (int) floor( (point->y - topy) / text->height );
- if (line >= text->numlines)
+ line = (int) floor ((point->y - topy) / text->height);
+ if (line >= text->numlines) {
line = text->numlines - 1;
+ }
}
left = text->position.x;
@@ -567,17 +643,18 @@ text_distance_from(Text *text, Point *point)
return dx + dy;
}
+
void
text_draw (Text *text, DiaRenderer *renderer)
{
dia_renderer_draw_text (renderer, text);
if (DIA_IS_INTERACTIVE_RENDERER (renderer) && (text->focus.has_focus)) {
- real curs_x, curs_y;
- real str_width_first;
- real str_width_whole;
+ double curs_x, curs_y;
+ double str_width_first;
+ double str_width_whole;
Point p1, p2;
- real height = text->ascent+text->descent;
+ double height = text->ascent+text->descent;
curs_y = text->position.y - text->ascent + text->cursor_row*text->height;
dia_renderer_set_font (renderer, text->font, text->height);
@@ -609,36 +686,40 @@ text_draw (Text *text, DiaRenderer *renderer)
p2.y = curs_y + height;
dia_renderer_set_linestyle (renderer, LINESTYLE_SOLID, 0.0);
- dia_renderer_set_linewidth (renderer, height/CURSOR_HEIGHT_RATIO);
+ dia_renderer_set_linewidth (renderer, height / CURSOR_HEIGHT_RATIO);
dia_renderer_draw_line (renderer, &p1, &p2, &color_black);
}
}
+
void
-text_grab_focus(Text *text, DiaObject *object)
+text_grab_focus (Text *text, DiaObject *object)
{
text->focus.obj = object;
- request_focus(&text->focus);
+ request_focus (&text->focus);
}
+
void
-text_set_cursor_at_end( Text* text )
+text_set_cursor_at_end (Text* text )
{
- text->cursor_row = text->numlines - 1 ;
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row) ;
+ text->cursor_row = text->numlines - 1;
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
}
+
typedef enum {
WORD_START = 1,
WORD_END
} CursorMovement;
+
static void
-text_move_cursor(Text *text, CursorMovement mv)
+text_move_cursor (Text *text, CursorMovement mv)
{
- gchar *str = text_line_get_string(text->lines[text->cursor_row]);
- gchar *p = str;
- int curmax = text_get_line_strlen(text, text->cursor_row);
+ char *str = text_line_get_string (text->lines[text->cursor_row]);
+ char *p = str;
+ int curmax = text_get_line_strlen (text, text->cursor_row);
if (text->cursor_pos > 0 && text->cursor_pos <= curmax) {
int i;
for (i = 0; i < text->cursor_pos; ++i)
@@ -647,7 +728,7 @@ text_move_cursor(Text *text, CursorMovement mv)
if (WORD_START == mv && text->cursor_pos < 1) {
if (text->cursor_row) {
text->cursor_row--;
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
}
return;
} else if (WORD_END == mv && text->cursor_pos == curmax) {
@@ -675,6 +756,7 @@ text_move_cursor(Text *text, CursorMovement mv)
}
}
+
/* The renderer is only used to determine where the click is, so is not
* required when no point is given. */
void
@@ -682,10 +764,10 @@ text_set_cursor (Text *text,
Point *clicked_point,
DiaRenderer *renderer)
{
- real str_width_whole;
- real str_width_first;
- real top;
- real start_x;
+ double str_width_whole;
+ double str_width_first;
+ double top;
+ double start_x;
int row;
int i;
@@ -731,9 +813,9 @@ text_set_cursor (Text *text,
/* Do an ugly linear search for the cursor index:
TODO: Change to binary search */
{
- real min_dist = G_MAXDOUBLE;
+ double min_dist = G_MAXDOUBLE;
for (i = 0; i <= text_get_line_strlen (text, row); i++) {
- real dist;
+ double dist;
str_width_first = dia_renderer_get_text_width (renderer, text_get_line (text, row), i);
dist = fabs (clicked_point->x - (start_x + str_width_first));
if (dist < min_dist) {
@@ -750,21 +832,23 @@ text_set_cursor (Text *text,
}
}
+
static void
-text_join_lines(Text *text, int first_line)
+text_join_lines (Text *text, int first_line)
{
- gchar *combined_line;
+ char *combined_line;
int len1;
- len1 = text_get_line_strlen(text, first_line);
+ len1 = text_get_line_strlen (text, first_line);
- combined_line = g_strconcat(text_get_line(text, first_line),
- text_get_line(text, first_line + 1), NULL);
- text_delete_line(text, first_line);
- text_set_line_text(text, first_line, combined_line);
+ combined_line = g_strconcat (text_get_line (text, first_line),
+ text_get_line (text, first_line + 1), NULL);
+ text_delete_line (text, first_line);
+ text_set_line_text (text, first_line, combined_line);
g_clear_pointer (&combined_line, g_free);
- text->max_width = MAX(text->max_width, text_get_line_width(text, first_line));
+ text->max_width = MAX (text->max_width,
+ text_get_line_width (text, first_line));
text->cursor_row = first_line;
text->cursor_pos = len1;
@@ -783,27 +867,29 @@ text_delete_forward (Text *text)
row = text->cursor_row;
- if (text->cursor_pos >= text_get_line_strlen(text, row)) {
- if (row + 1 < text->numlines)
- text_join_lines(text, row);
+ if (text->cursor_pos >= text_get_line_strlen (text, row)) {
+ if (row + 1 < text->numlines) {
+ text_join_lines (text, row);
+ }
return;
}
- line = text_get_line(text, row);
- utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos));
- utf8_after = g_utf8_offset_to_pointer(utf8_before, 1);
- str1 = g_strndup(line, utf8_before - line);
- str = g_strconcat(str1, utf8_after, NULL);
- text_set_line_text(text, row, str);
+ line = text_get_line (text, row);
+ utf8_before = g_utf8_offset_to_pointer (line, (glong)(text->cursor_pos));
+ utf8_after = g_utf8_offset_to_pointer (utf8_before, 1);
+ str1 = g_strndup (line, utf8_before - line);
+ str = g_strconcat (str1, utf8_after, NULL);
+ text_set_line_text (text, row, str);
g_clear_pointer (&str1, g_free);
g_clear_pointer (&str, g_free);
- if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ if (text->cursor_pos > text_get_line_strlen (text, text->cursor_row)) {
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
+ }
width = 0.0;
for (i = 0; i < text->numlines; i++) {
- width = MAX(width, text_get_line_width(text, i));
+ width = MAX (width, text_get_line_width (text, i));
}
text->max_width = width;
}
@@ -822,31 +908,34 @@ text_delete_backward (Text *text)
row = text->cursor_row;
if (text->cursor_pos <= 0) {
- if (row > 0)
- text_join_lines(text, row-1);
+ if (row > 0) {
+ text_join_lines (text, row - 1);
+ }
return;
}
- line = text_get_line(text, row);
- utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos - 1));
- utf8_after = g_utf8_offset_to_pointer(utf8_before, 1);
- str1 = g_strndup(line, utf8_before - line);
- str = g_strconcat(str1, utf8_after, NULL);
- text_set_line_text(text, row, str);
+ line = text_get_line (text, row);
+ utf8_before = g_utf8_offset_to_pointer (line, (glong) (text->cursor_pos - 1));
+ utf8_after = g_utf8_offset_to_pointer (utf8_before, 1);
+ str1 = g_strndup (line, utf8_before - line);
+ str = g_strconcat (str1, utf8_after, NULL);
+ text_set_line_text (text, row, str);
g_clear_pointer (&str, g_free);
g_clear_pointer (&str1, g_free);
text->cursor_pos --;
- if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
+ if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row)) {
text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ }
width = 0.0;
for (i = 0; i < text->numlines; i++) {
- width = MAX(width, text_get_line_width(text, i));
+ width = MAX (width, text_get_line_width (text, i));
}
text->max_width = width;
}
+
static void
text_split_line (Text *text)
{
@@ -857,13 +946,13 @@ text_split_line (Text *text)
char *str1, *str2;
/* Split the lines at cursor_pos */
- line = text_get_line(text, text->cursor_row);
- text_insert_line(text, text->cursor_row);
- utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos));
- str1 = g_strndup(line, utf8_before - line);
- str2 = g_strdup(utf8_before); /* Must copy before dealloc */
- text_set_line_text(text, text->cursor_row, str1);
- text_set_line_text(text, text->cursor_row + 1, str2);
+ line = text_get_line (text, text->cursor_row);
+ text_insert_line (text, text->cursor_row);
+ utf8_before = g_utf8_offset_to_pointer (line, (glong) (text->cursor_pos));
+ str1 = g_strndup (line, utf8_before - line);
+ str2 = g_strdup (utf8_before); /* Must copy before dealloc */
+ text_set_line_text (text, text->cursor_row, str1);
+ text_set_line_text (text, text->cursor_row + 1, str2);
g_clear_pointer (&str2, g_free);
g_clear_pointer (&str1, g_free);
@@ -871,8 +960,8 @@ text_split_line (Text *text)
text->cursor_pos = 0;
width = 0.0;
- for (i=0;i<text->numlines;i++) {
- width = MAX(width, text_get_line_width(text, i));
+ for (i = 0; i < text->numlines; i++) {
+ width = MAX (width, text_get_line_width (text, i));
}
text->max_width = width;
}
@@ -895,20 +984,21 @@ text_insert_char (Text *text, gunichar c)
row = text->cursor_row;
/* Copy the before and after parts with the new char in between */
- line = text_get_line(text, row);
- utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos));
- str1 = g_strndup(line, utf8_before - line);
- str = g_strconcat(str1, ch, utf8_before, NULL);
- text_set_line_text(text, row, str);
+ line = text_get_line (text, row);
+ utf8_before = g_utf8_offset_to_pointer (line, (glong) (text->cursor_pos));
+ str1 = g_strndup (line, utf8_before - line);
+ str = g_strconcat (str1, ch, utf8_before, NULL);
+ text_set_line_text (text, row, str);
g_clear_pointer (&str, g_free);
g_clear_pointer (&str1, g_free);
text->cursor_pos++;
- text->max_width = MAX(text->max_width, text_get_line_width(text, row));
+ text->max_width = MAX (text->max_width, text_get_line_width (text, row));
}
+
gboolean
-text_delete_key_handler(Focus *focus, ObjectChange ** change)
+text_delete_key_handler (Focus *focus, ObjectChange ** change)
{
Text *text;
int row, i;
@@ -919,28 +1009,32 @@ text_delete_key_handler(Focus *focus, ObjectChange ** change)
row = text->cursor_row;
if (text->cursor_pos >= text_get_line_strlen(text, row)) {
if (row+1 < text->numlines) {
- *change = text_create_change(text, TYPE_JOIN_ROW, 'Q',
- text->cursor_pos, row, focus->obj);
+ *change = text_create_change (text, TYPE_JOIN_ROW, 'Q',
+ text->cursor_pos, row, focus->obj);
} else {
return FALSE;
}
} else {
utf = text_get_line(text, row);
- for (i = 0; i < text->cursor_pos; i++)
+ for (i = 0; i < text->cursor_pos; i++) {
utf = g_utf8_next_char (utf);
+ }
c = g_utf8_get_char (utf);
*change = text_create_change (text, TYPE_DELETE_FORWARD, c,
- text->cursor_pos, text->cursor_row, focus->obj);
+ text->cursor_pos, text->cursor_row, focus->obj);
}
text_delete_forward(text);
- return TRUE;;
+ return TRUE;
}
+
static int
-text_key_event(Focus *focus,
- guint keystate, guint keyval,
- const gchar *str, int strlen,
- ObjectChange **change)
+text_key_event (Focus *focus,
+ guint keystate,
+ guint keyval,
+ const char *str,
+ int strlen,
+ ObjectChange **change)
{
Text *text;
int return_val = FALSE;
@@ -959,8 +1053,9 @@ text_key_event(Focus *focus,
if (text->cursor_row<0)
text->cursor_row = 0;
- if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ if (text->cursor_pos > text_get_line_strlen (text, text->cursor_row)) {
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
+ }
break;
case GDK_Down:
@@ -969,27 +1064,33 @@ text_key_event(Focus *focus,
if (text->cursor_row >= text->numlines)
text->cursor_row = text->numlines - 1;
- if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ if (text->cursor_pos > text_get_line_strlen (text, text->cursor_row)) {
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
+ }
break;
case GDK_Left:
case GDK_KP_Left:
- if (keystate & GDK_CONTROL_MASK)
- text_move_cursor(text, WORD_START);
- else
+ if (keystate & GDK_CONTROL_MASK) {
+ text_move_cursor (text, WORD_START);
+ } else {
text->cursor_pos--;
- if (text->cursor_pos<0)
+ }
+ if (text->cursor_pos < 0) {
text->cursor_pos = 0;
+ }
break;
case GDK_Right:
case GDK_KP_Right:
- if (keystate & GDK_CONTROL_MASK)
- text_move_cursor(text, WORD_END);
- else
+ if (keystate & GDK_CONTROL_MASK) {
+ text_move_cursor(text, WORD_END);
+ } else {
text->cursor_pos++;
- if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ }
+
+ if (text->cursor_pos > text_get_line_strlen (text, text->cursor_row)) {
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
+ }
break;
case GDK_Home:
case GDK_KP_Home:
@@ -997,43 +1098,45 @@ text_key_event(Focus *focus,
break;
case GDK_End:
case GDK_KP_End:
- text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
+ text->cursor_pos = text_get_line_strlen (text, text->cursor_row);
break;
case GDK_Delete:
case GDK_KP_Delete:
- return_val = text_delete_key_handler(focus, change);
+ return_val = text_delete_key_handler (focus, change);
break;
case GDK_BackSpace:
return_val = TRUE;
row = text->cursor_row;
if (text->cursor_pos <= 0) {
if (row > 0) {
- *change = text_create_change(text, TYPE_JOIN_ROW, 'Q',
- text_get_line_strlen(text, row-1), row-1,
- focus->obj);
+ *change = text_create_change (text, TYPE_JOIN_ROW, 'Q',
+ text_get_line_strlen (text, row-1),
+ row-1,
+ focus->obj);
} else {
return_val = FALSE;
break;
}
} else {
- utf = text_get_line(text, row);
- for (i = 0; i < (text->cursor_pos - 1); i++)
+ utf = text_get_line (text, row);
+ for (i = 0; i < (text->cursor_pos - 1); i++) {
utf = g_utf8_next_char (utf);
+ }
c = g_utf8_get_char (utf);
*change = text_create_change (text, TYPE_DELETE_BACKWARD, c,
text->cursor_pos - 1,
text->cursor_row,
- focus->obj);
+ focus->obj);
}
- text_delete_backward(text);
+ text_delete_backward (text);
break;
case GDK_Return:
case GDK_KP_Enter:
return_val = TRUE;
- *change = text_create_change(text, TYPE_SPLIT_ROW, 'Q',
- text->cursor_pos, text->cursor_row,
- focus->obj);
- text_split_line(text);
+ *change = text_create_change (text, TYPE_SPLIT_ROW, 'Q',
+ text->cursor_pos, text->cursor_row,
+ focus->obj);
+ text_split_line (text);
break;
case GDK_Shift_L:
case GDK_Shift_R:
@@ -1047,19 +1150,20 @@ text_key_event(Focus *focus,
break;
default:
if (str || (strlen>0)) {
- if (str && *str == '\r')
+ if (str && *str == '\r') {
break; /* avoid putting junk into our string */
+ }
return_val = TRUE;
- *change = change_list_create();
+ *change = change_list_create();
for (utf = str; utf && *utf && strlen > 0 ;
- utf = g_utf8_next_char (utf), strlen--) {
- ObjectChange *step;
+ utf = g_utf8_next_char (utf), strlen--) {
+ ObjectChange *step;
c = g_utf8_get_char (utf);
step = text_create_change (text, TYPE_INSERT_CHAR, c,
text->cursor_pos, text->cursor_row,
- focus->obj);
- change_list_add (*change, step);
+ focus->obj);
+ change_list_add (*change, step);
text_insert_char (text, c);
}
}
@@ -1069,34 +1173,38 @@ text_key_event(Focus *focus,
return return_val;
}
-int text_is_empty(const Text *text)
+
+int
+text_is_empty (const Text *text)
{
- int i;
- for (i = 0; i < text->numlines; i++) {
- if (text_get_line_strlen(text, i) != 0) {
+ for (int i = 0; i < text->numlines; i++) {
+ if (text_get_line_strlen (text, i) != 0) {
return FALSE;
}
}
+
return TRUE;
}
+
int
-text_delete_all(Text *text, ObjectChange **change, DiaObject *obj)
+text_delete_all (Text *text, ObjectChange **change, DiaObject *obj)
{
- if (!text_is_empty(text)) {
- *change = text_create_change(text, TYPE_DELETE_ALL,
- 0, text->cursor_pos, text->cursor_row,
- obj);
+ if (!text_is_empty (text)) {
+ *change = text_create_change (text, TYPE_DELETE_ALL,
+ 0, text->cursor_pos, text->cursor_row,
+ obj);
- text_set_string(text, "");
- calc_ascent_descent(text);
+ text_set_string (text, "");
+ calc_ascent_descent (text);
return TRUE;
}
return FALSE;
}
+
void
-data_add_text(AttributeNode attr, Text *text, DiaContext *ctx)
+data_add_text (AttributeNode attr, Text *text, DiaContext *ctx)
{
DataNode composite;
char *str;
@@ -1121,46 +1229,51 @@ data_add_text(AttributeNode attr, Text *text, DiaContext *ctx)
Text *
-data_text(AttributeNode text_attr, DiaContext *ctx)
+data_text (AttributeNode text_attr, DiaContext *ctx)
{
char *string = NULL;
DiaFont *font;
- real height;
- Point pos = {0.0, 0.0};
+ double height;
+ Point pos = { 0.0, 0.0 };
Color col;
Alignment align;
AttributeNode attr;
Text *text;
- attr = composite_find_attribute(text_attr, "string");
- if (attr != NULL)
- string = data_string(attribute_first_data(attr), ctx);
+ attr = composite_find_attribute (text_attr, "string");
+ if (attr != NULL) {
+ string = data_string (attribute_first_data (attr), ctx);
+ }
height = 1.0;
- attr = composite_find_attribute(text_attr, "height");
- if (attr != NULL)
- height = data_real(attribute_first_data(attr), ctx);
+ attr = composite_find_attribute (text_attr, "height");
+ if (attr != NULL) {
+ height = data_real (attribute_first_data (attr), ctx);
+ }
- attr = composite_find_attribute(text_attr, "font");
+ attr = composite_find_attribute (text_attr, "font");
if (attr != NULL) {
- font = data_font(attribute_first_data(attr), ctx);
+ font = data_font (attribute_first_data (attr), ctx);
} else {
- font = dia_font_new_from_style(DIA_FONT_SANS,1.0);
+ font = dia_font_new_from_style (DIA_FONT_SANS,1.0);
}
- attr = composite_find_attribute(text_attr, "pos");
- if (attr != NULL)
- data_point(attribute_first_data(attr), &pos, ctx);
+ attr = composite_find_attribute (text_attr, "pos");
+ if (attr != NULL) {
+ data_point (attribute_first_data (attr), &pos, ctx);
+ }
col = color_black;
- attr = composite_find_attribute(text_attr, "color");
- if (attr != NULL)
- data_color(attribute_first_data(attr), &col, ctx);
+ attr = composite_find_attribute (text_attr, "color");
+ if (attr != NULL) {
+ data_color (attribute_first_data (attr), &col, ctx);
+ }
align = ALIGN_LEFT;
- attr = composite_find_attribute(text_attr, "alignment");
- if (attr != NULL)
- align = data_enum(attribute_first_data(attr), ctx);
+ attr = composite_find_attribute (text_attr, "alignment");
+ if (attr != NULL) {
+ align = data_enum (attribute_first_data (attr), ctx);
+ }
text = new_text (string ? string : "", font, height, &pos, &col, align);
g_clear_object (&font);
@@ -1168,8 +1281,9 @@ data_text(AttributeNode text_attr, DiaContext *ctx)
return text;
}
+
void
-text_get_attributes(Text *text, TextAttributes *attr)
+text_get_attributes (Text *text, TextAttributes *attr)
{
DiaFont *old_font;
old_font = attr->font;
@@ -1181,13 +1295,14 @@ text_get_attributes(Text *text, TextAttributes *attr)
attr->alignment = text->alignment;
}
+
void
-text_set_attributes(Text *text, TextAttributes *attr)
+text_set_attributes (Text *text, TextAttributes *attr)
{
if (text->font != attr->font) {
- text_set_font(text, attr->font);
+ text_set_font (text, attr->font);
}
- text_set_height(text, attr->height);
+ text_set_height (text, attr->height);
text->position = attr->position;
text->color = attr->color;
text->alignment = attr->alignment;
@@ -1237,7 +1352,7 @@ text_change_apply (struct TextObjectChange *change, DiaObject *obj)
static void
-text_change_revert(struct TextObjectChange *change, DiaObject *obj)
+text_change_revert (struct TextObjectChange *change, DiaObject *obj)
{
Text *text = change->text;
switch (change->type) {
@@ -1300,23 +1415,29 @@ static PropDescription _prop_descs[] = {
PROP_DESC_END
};
+
static GPtrArray *
make_posision_and_size_prop_list (void)
{
GPtrArray *props;
- props = prop_list_from_descs(_prop_descs,pdtpp_true);
+ props = prop_list_from_descs (_prop_descs, pdtpp_true);
return props;
}
+
static ObjectChange *
-text_create_change(Text *text, TextChangeType type,
- gunichar ch, int pos, int row, DiaObject *obj)
+text_create_change (Text *text,
+ TextChangeType type,
+ gunichar ch,
+ int pos,
+ int row,
+ DiaObject *obj)
{
struct TextObjectChange *change;
- change = g_new0(struct TextObjectChange, 1);
+ change = g_new0 (struct TextObjectChange, 1);
change->obj = obj;
change->props = make_posision_and_size_prop_list ();
@@ -1332,44 +1453,52 @@ text_create_change(Text *text, TextChangeType type,
change->ch = ch;
change->pos = pos;
change->row = row;
- if (type == TYPE_DELETE_ALL)
- change->str = text_get_string_copy(text);
- else
+
+ if (type == TYPE_DELETE_ALL) {
+ change->str = text_get_string_copy (text);
+ } else {
change->str = NULL;
- return (ObjectChange *)change;
+ }
+
+ return (ObjectChange *) change;
}
+
gboolean
-apply_textattr_properties(GPtrArray *props,
- Text *text, const gchar *textname,
- TextAttributes *attrs)
+apply_textattr_properties (GPtrArray *props,
+ Text *text,
+ const char *textname,
+ TextAttributes *attrs)
{
TextProperty *textprop =
- (TextProperty *)find_prop_by_name_and_type(props,textname,PROP_TYPE_TEXT);
+ (TextProperty *) find_prop_by_name_and_type (props, textname, PROP_TYPE_TEXT);
if ((!textprop) ||
((textprop->common.experience & (PXP_LOADED|PXP_SFO))==0 )) {
/* most likely we're called after the dialog box has been applied */
- text_set_attributes(text,attrs);
+ text_set_attributes (text, attrs);
return TRUE;
}
return FALSE;
}
+
gboolean
-apply_textstr_properties(GPtrArray *props,
- Text *text, const gchar *textname,
- const gchar *str)
+apply_textstr_properties (GPtrArray *props,
+ Text *text,
+ const char *textname,
+ const char *str)
{
TextProperty *textprop =
- (TextProperty *)find_prop_by_name_and_type(props,textname,PROP_TYPE_TEXT);
+ (TextProperty *) find_prop_by_name_and_type (props, textname, PROP_TYPE_TEXT);
if ((!textprop) ||
- ((textprop->common.experience & (PXP_LOADED|PXP_SFO))==0 )) {
+ ((textprop->common.experience & (PXP_LOADED | PXP_SFO))==0 )) {
/* most likely we're called after the dialog box has been applied */
- text_set_string(text,str);
+ text_set_string (text, str);
return TRUE;
}
+
return FALSE;
}
diff --git a/lib/textline.h b/lib/textline.h
index 36edc7d1..f94a7485 100644
--- a/lib/textline.h
+++ b/lib/textline.h
@@ -50,7 +50,7 @@ struct _TextLine {
/* Whether nothing has changed in this object since values were computed. */
gboolean clean;
- /** Copies of the real fields to keep track of changes caused by
+ /* Copies of the real fields to keep track of changes caused by
* properties setting. These may go away if we create TextLine properties.
*/
char *chars_cache;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]