Give the main frame visibility on events

This commit is contained in:
2025-03-31 13:58:48 +09:00
parent d4f2b3f2b0
commit 9aa2c2f79d
3 changed files with 128 additions and 98 deletions
+110 -45
View File
@@ -1,39 +1,103 @@
-module(gd_sophia_editor).
-export([new/3, get_text/1, set_text/2]).
-export([new/1, update/2,
get_text/1, set_text/2]).
-include("$zx_include/zx_logger.hrl").
-include_lib("wx/include/wx.hrl").
new(Parent, Id, Opts) ->
STC = wxStyledTextCtrl:new(Parent, [{id, Id} | Opts]),
%% Set up the container lexer
wxStyledTextCtrl:setLexer(STC, ?wxSTC_LEX_CONTAINER),
%%% Formatting Constants
%% Style labels
-define(DEFAULT, 0).
-define(KEYWORD, 1).
-define(IDENTIFIER, 2).
-define(COMMENT, 3).
-define(STRING, 4).
-define(NUMBER, 5).
-define(OPERATOR, 6).
%% Color palette
% Intensities:
-define(H, 255). % High
-define(M, 192). % Medium
-define(L, 128). % Low
-define(Z, 0). % Zilch
% RGB values
% R G B
-define(black, {?Z, ?Z, ?Z}).
-define(light_red, {?H, ?Z, ?Z}).
-define(light_green, {?Z, ?H, ?Z}).
-define(light_blue, {?Z, ?Z, ?H}).
-define(yellow, {?H, ?H, ?Z}).
-define(light_magenta, {?H, ?Z, ?H}).
-define(light_cyan, {?Z, ?H, ?H}).
-define(high_white, {?H, ?H, ?H}).
-define(red, {?L, ?Z, ?Z}).
-define(green, {?Z, ?L, ?Z}).
-define(blue, {?Z, ?Z, ?L}).
-define(brown, {?L, ?L, ?Z}).
-define(magenta, {?L, ?Z, ?L}).
-define(cyan, {?Z, ?L, ?L}).
-define(grey, {?L, ?L, ?L}).
-define(white, {?M, ?M, ?M}).
styles() ->
[?DEFAULT,
?KEYWORD,
?IDENTIFIER,
?COMMENT,
?STRING,
?NUMBER,
?OPERATOR].
palette(light) ->
#{?DEFAULT => ?black,
?KEYWORD => ?blue,
?IDENTIFIER => ?cyan,
?COMMENT => ?grey,
?STRING => ?red,
?NUMBER => ?magenta,
?OPERATOR => ?brown};
palette(dark) ->
#{?DEFAULT => ?white,
?KEYWORD => ?light_cyan,
?IDENTIFIER => ?green,
?COMMENT => ?grey,
?STRING => ?light_red,
?NUMBER => ?light_magenta,
?OPERATOR => ?yellow}.
color_mode() ->
light.
new(Parent) ->
STC = wxStyledTextCtrl:new(Parent),
ok = wxStyledTextCtrl:setLexer(STC, ?wxSTC_LEX_CONTAINER),
FontSize = 13,
Mono = wxFont:new(FontSize, ?wxFONTFAMILY_TELETYPE, ?wxFONTSTYLE_NORMAL,
?wxFONTWEIGHT_NORMAL, [{face, "Monospace"}]),
lists:foreach(
fun(Style) ->
wxStyledTextCtrl:styleSetFont(STC, Style, Mono)
end,
lists:seq(0, 6)
),
%%
wxStyledTextCtrl:styleSetFont(STC, ?wxSTC_STYLE_DEFAULT, Mono),
%% Define styles
set_colors(STC),
%% Connect the styling event
wxStyledTextCtrl:connect(
STC, 'stc_styleneeded',
[{callback, fun(Event, _) -> style_needed(Event, STC) end}]),
Mono = wxFont:new(FontSize,
?wxFONTFAMILY_TELETYPE,
?wxFONTSTYLE_NORMAL,
?wxFONTWEIGHT_NORMAL,
[{face, "Monospace"}]),
SetMonospace = fun(Style) -> wxStyledTextCtrl:styleSetFont(STC, Style, Mono) end,
ok = lists:foreach(SetMonospace, styles()),
ok = wxStyledTextCtrl:styleSetFont(STC, ?wxSTC_STYLE_DEFAULT, Mono),
ok = set_colors(STC),
STC.
get_text(STC) ->
wxStyledTextCtrl:getText(STC).
set_text(STC, Text) ->
wxStyledTextCtrl:setText(STC, Text),
ok = wxStyledTextCtrl:setText(STC, Text),
%% Force Scintilla to request styling for the entire text
wxStyledTextCtrl:colourise(STC, 0, -1).
@@ -41,15 +105,16 @@ set_text(STC, Text) ->
set_colors(STC) ->
wxStyledTextCtrl:styleClearAll(STC),
wxStyledTextCtrl:styleSetForeground(STC, 0, {0, 0, 0}), % Default: Black
wxStyledTextCtrl:styleSetForeground(STC, 1, {0, 0, 192}), % Keywords: Deep Blue
wxStyledTextCtrl:styleSetForeground(STC, 2, {0, 0, 0}), % Identifiers: Black
wxStyledTextCtrl:styleSetForeground(STC, 3, {128, 128, 128}), % Comments: Gray
wxStyledTextCtrl:styleSetForeground(STC, 4, {163, 21, 21}), % Strings/Chars: Reddish
wxStyledTextCtrl:styleSetForeground(STC, 5, {128, 0, 128}), % Numbers: Purple
wxStyledTextCtrl:styleSetForeground(STC, 6, {0, 0, 0}). % Operators: Black
Palette = palette(color_mode()),
Colorize =
fun(Style) ->
Color = maps:get(Style, Palette),
wxStyledTextCtrl:styleSetForeground(STC, Style, Color)
end,
lists:foreach(Colorize, styles()).
style_needed(_Event, STC) ->
update(_Event, STC) ->
try
StartPos = wxStyledTextCtrl:getEndStyled(STC),
EndPos = wxStyledTextCtrl:getLength(STC),
@@ -91,21 +156,21 @@ classify_style(Type, Value) ->
case Type of
symbol ->
case lists:member(Value, keywords()) of
true -> 1;
false -> 6
true -> ?KEYWORD;
false -> ?OPERATOR
end;
id -> 2;
qid -> 2;
con -> 2;
qcon -> 2;
tvar -> 2;
string -> 4;
char -> 4;
int -> 5;
hex -> 5;
bytes -> 5;
skip -> 3;
_Other -> 1
id -> ?IDENTIFIER;
qid -> ?IDENTIFIER;
con -> ?IDENTIFIER;
qcon -> ?IDENTIFIER;
tvar -> ?IDENTIFIER;
string -> ?STRING;
char -> ?STRING;
int -> ?NUMBER;
hex -> ?NUMBER;
bytes -> ?NUMBER;
skip -> ?COMMENT;
_Other -> ?DEFAULT
end.
type_and_value({Type,_Line,Value}) -> {Type, Value};