Skip to content

Commit

Permalink
Improve TextView
Browse files Browse the repository at this point in the history
  • Loading branch information
can-lehmann committed Mar 31, 2024
1 parent acfe3b8 commit 6323696
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,8 @@ A text editor with support for formatted text.
- `editable: bool = true`
- `acceptsTab: bool = true`
- `indent: int = 0`
- `wrapMode: WrapMode = WrapNone`
- `textMargin: Margin`


## ListBoxRow
Expand Down
14 changes: 13 additions & 1 deletion owlkettle/bindings/gtk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ type
GTK_LICENSE_APACHE_2_0 = 16
GTK_LICENSE_MPL_2_0 = 17
GTK_LICENSE_0BSD = 18


GtkWrapMode* = enum
GTK_WRAP_NONE
GTK_WRAP_CHAR
GTK_WRAP_WORD
GTK_WRAP_WORD_CHAR

GtkTextIter* = object
a, b: pointer
c, d, e, f, g, h: cint
Expand Down Expand Up @@ -976,6 +982,7 @@ proc gtk_separator_new*(orient: GtkOrientation): GtkWidget
proc gtk_text_buffer_new*(tagTable: GtkTextTagTable): GtkTextBuffer
proc gtk_text_buffer_get_line_count*(buffer: GtkTextBuffer): cint
proc gtk_text_buffer_get_char_count*(buffer: GtkTextBuffer): cint
proc gtk_text_buffer_set_modified*(buffer: GtkTextBuffer, modified: cbool)
proc gtk_text_buffer_get_modified*(buffer: GtkTextBuffer): cbool
proc gtk_text_buffer_get_can_redo*(buffer: GtkTextBuffer): cbool
proc gtk_text_buffer_get_can_undo*(buffer: GtkTextBuffer): cbool
Expand Down Expand Up @@ -1045,6 +1052,11 @@ proc gtk_text_view_set_cursor_visible*(textView: GtkWidget, isVisible: cbool)
proc gtk_text_view_set_editable*(textView: GtkWidget, editable: cbool)
proc gtk_text_view_set_accepts_tab*(textView: GtkWidget, acceptsTab: cbool)
proc gtk_text_view_set_indent*(textView: GtkWidget, indent: cint)
proc gtk_text_view_set_top_margin*(textView: GtkWidget, margin: cint)
proc gtk_text_view_set_bottom_margin*(textView: GtkWidget, margin: cint)
proc gtk_text_view_set_left_margin*(textView: GtkWidget, margin: cint)
proc gtk_text_view_set_right_margin*(textView: GtkWidget, margin: cint)
proc gtk_text_view_set_wrap_mode*(textView: GtkWidget, mode: GtkWrapMode)

# Gtk.ListBox
proc gtk_list_box_new*(): GtkWidget
Expand Down
37 changes: 36 additions & 1 deletion owlkettle/widgets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,8 @@ type

TagStyle* = object
background*: Option[string]
paragraphBackground*: Option[string]
backgroundFullHeight*: Option[bool]
foreground*: Option[string]
family*: Option[string]
size*: Option[int]
Expand Down Expand Up @@ -2365,8 +2367,13 @@ proc registerTag*(buffer: TextBuffer, name: string, style: TagStyle): TextTag =
result = gtk_text_buffer_create_tag(buffer.gtk, name.cstring, nil)
for attr, value in fieldPairs(style):
if value.isSome:
let name = case attr:
of "paragraphBackground": "paragraph-background"
of "backgroundFullHeight": "background-full-height"
else: attr

var gvalue = g_value_new(get(value))
g_object_set_property(result.pointer, attr.cstring, gvalue.addr)
g_object_set_property(result.pointer, name.cstring, gvalue.addr)
g_value_unset(gvalue.addr)

proc lookupTag*(buffer: TextBuffer, name: string): TextTag =
Expand Down Expand Up @@ -2424,6 +2431,9 @@ proc text*(buffer: TextBuffer, hiddenChars: bool = true): string =
proc isModified*(buffer: TextBuffer): bool =
result = gtk_text_buffer_get_modified(buffer.gtk) != 0

proc `isModified=`*(buffer: TextBuffer, modified: bool) =
gtk_text_buffer_set_modified(buffer.gtk, cbool(modified))

proc hasSelection*(buffer: TextBuffer): bool =
result = gtk_text_buffer_get_has_selection(buffer.gtk) != 0

Expand Down Expand Up @@ -2466,6 +2476,9 @@ proc removeAllTags*(buffer: TextBuffer, a, b: TextIter) =
proc removeAllTags*(buffer: TextBuffer, slice: TextSlice) =
buffer.removeAllTags(slice.a, slice.b)

proc removeAllTags*(buffer: TextBuffer) =
buffer.removeAllTags(buffer.startIter, buffer.endIter)

proc canRedo*(buffer: TextBuffer): bool = bool(gtk_text_buffer_get_can_redo(buffer.gtk) != 0)
proc canUndo*(buffer: TextBuffer): bool = bool(gtk_text_buffer_get_can_undo(buffer.gtk) != 0)
proc redo*(buffer: TextBuffer) = gtk_text_buffer_redo(buffer.gtk)
Expand Down Expand Up @@ -2537,6 +2550,15 @@ proc `line=`*(iter: TextIter, val: int) = gtk_text_iter_set_line(iter.unsafeAddr
proc `lineOffset=`*(iter: TextIter, val: int) = gtk_text_iter_set_line_offset(iter.unsafeAddr, cint(val))
{.pop.}

type
WrapMode* = enum
WrapNone
WrapChar
WrapWord
WrapWordChar

proc toGtk(mode: WrapMode): GtkWrapMode = GtkWrapMode(ord(mode))

renderable TextView of BaseWidget:
## A text editor with support for formatted text.

Expand All @@ -2546,6 +2568,8 @@ renderable TextView of BaseWidget:
editable: bool = true
acceptsTab: bool = true
indent: int = 0
wrapMode: WrapMode = WrapNone
textMargin: Margin

hooks:
beforeBuild:
Expand All @@ -2571,6 +2595,17 @@ renderable TextView of BaseWidget:
property:
gtk_text_view_set_indent(state.internalWidget, cint(state.indent))

hooks wrapMode:
property:
gtk_text_view_set_wrap_mode(state.internalWidget, toGtk(state.wrapMode))

hooks textMargin:
property:
gtk_text_view_set_top_margin(state.internalWidget, cint(state.textMargin.top))
gtk_text_view_set_bottom_margin(state.internalWidget, cint(state.textMargin.bottom))
gtk_text_view_set_left_margin(state.internalWidget, cint(state.textMargin.left))
gtk_text_view_set_right_margin(state.internalWidget, cint(state.textMargin.right))

hooks buffer:
property:
if state.buffer.isNil:
Expand Down

0 comments on commit 6323696

Please sign in to comment.