Skip to content

Commit

Permalink
NOTE: Improvements to Information line management made it auto-sizabl…
Browse files Browse the repository at this point in the history
…e when the message is longest than window width.
  • Loading branch information
Micheus committed Dec 1, 2011
1 parent fd9b024 commit a19117f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/wings_io.erl
Expand Up @@ -410,8 +410,11 @@ gradient_rect_burst(X, Y, W, H, Color) ->
GradColors = [0.882353, 0.882353, 0.850980, 0.807843, 0.776471, 0.729412,
0.701961, 0.666667, 0.619608, 0.741176, 0.733333, 0.760784,
0.784314, 0.811765, 0.854902, 0.890196, 0.890196],
K = if H=<17 -> H;
true -> H-1
end,
Draw_Line = fun(Idx) ->
GreyValue = lists:nth(round((Idx/H)*17)+1, GradColors),
GreyValue = lists:nth(trunc((Idx/K)*17)+1, GradColors),
LineColor = mul_color(Color, GreyValue),
set_color(LineColor),
gl:vertex2f(X-0.5+W, Y-0.5+H-Idx),
Expand Down
2 changes: 1 addition & 1 deletion src/wings_msg.erl
Expand Up @@ -23,7 +23,7 @@
-include("wings.hrl").

-import(lists, [reverse/1]).
-define(SEP, [$\s,$\s,160]). %Two and a half.
-define(SEP, [160,160,160,160,160]). %Equivalent to two and a half space character width.
-define(CSEP, 160). %Short space.

button(LmbMsg) ->
Expand Down
120 changes: 98 additions & 22 deletions src/wings_wm.erl
Expand Up @@ -110,10 +110,12 @@ init() ->
desktop_event(_) -> keep.

message(Message) ->
wings_io:putback_event({wm,{message,get(wm_active),Message}}).
MsgData0=get_window_data(message),
put_window_data(message,MsgData0#win{z=highest_z()}),
wings_io:putback_event({wm,{message,get(wm_active),Message}}).

message_right(Right) ->
wings_io:putback_event({wm,{message_right,get(wm_active),Right}}).
wings_io:putback_event({wm,{message_right,get(wm_active),Right}}).

message(Message, Right) ->
message(Message),
Expand Down Expand Up @@ -899,21 +901,21 @@ wm_event({message,Name,Msg}) ->
none -> ok;
#win{stk=[#se{msg=Msg}|_]} -> ok;
#win{stk=[Top0|Stk]}=Data0 ->
Top = Top0#se{msg=Msg},
Data = Data0#win{stk=[Top|Stk]},
put_window_data(Name, Data),
dirty()
Top = Top0#se{msg=Msg},
Data = Data0#win{stk=[Top|Stk]},
put_window_data(Name, Data),
dirty()
end;
wm_event({message_right,Name,Right0}) ->
Right = lists:flatten(Right0),
case lookup_window_data(Name) of
none -> ok;
#win{stk=[#se{msg_right=Right}|_]} -> ok;
#win{stk=[Top0|Stk]}=Data0 ->
Top = Top0#se{msg_right=Right},
Data = Data0#win{stk=[Top|Stk]},
put_window_data(Name, Data),
dirty()
Top = Top0#se{msg_right=Right},
Data = Data0#win{stk=[Top|Stk]},
put_window_data(Name, Data),
dirty()
end;
wm_event({menubar,Name,Menubar}) ->
case lookup_window_data(Name) of
Expand Down Expand Up @@ -1156,8 +1158,9 @@ message_event(redraw) ->
undefined -> keep;
Active ->
#win{stk=[#se{msg=Msg,msg_right=Right}|_]} = get_window_data(Active),
message_redraw(Msg, Right)
end;
Msg0=calculate_win_size(Msg,Right),
message_redraw(Msg0, Right)
end;
message_event({action,_}=Action) ->
send(geom, Action);
message_event(got_focus) ->
Expand All @@ -1176,24 +1179,21 @@ message_redraw(Msg, Right) ->
{unix,darwin} -> 27;
_ -> 0
end,

case Right of
[] -> ok;
_ ->
Col = e3d_vec:mul(wings_pref:get_value(info_line_bg),0.6),
Cw = ?CHAR_WIDTH,
CH = ?CHAR_HEIGHT,
Lh = ?LINE_HEIGHT,
MsgW = wings_text:width(lists:flatten(Msg)),

RightW = wings_text:width(Right),
RMarg = RMarg0 + 2*Cw,
if
MsgW+RightW < W - RMarg ->
Pos = W - RightW - RMarg,
wings_io:sunken_rect(Pos-12, -CH, RightW+12, Lh, Col, Col),
wings_io:set_color(wings_pref:get_value(info_line_text)),
wings_io:text_at(Pos-6, -1, Right);
true -> ok
end
Pos = W - RightW - RMarg,
wings_io:sunken_rect(Pos-12, -CH, RightW+12, Lh-1, Col, Col),
wings_io:set_color(wings_pref:get_value(info_line_text)),
wings_io:text_at(Pos-6, -1, Right)
end,
case OsType of
{unix,darwin} ->
Expand All @@ -1209,9 +1209,85 @@ message_setup() ->
{W,H} = win_size(),
wings_io:gradient_rect_burst(0, 0, W, H, wings_pref:get_value(info_line_bg)),
wings_io:set_color(wings_pref:get_value(info_line_text)),
gl:translatef(10, H-5.375, 0),
gl:translatef(10, ?LINE_HEIGHT+2, 0),
{W,H}.

calculate_win_size(Msg,Right) ->
% this '+24' came from message_redraw() used in 'sunken_rect' function
Right_W=wings_text:width(lists:flatten(Right))+24,

OsType = get(wings_os_type),
RMarg0 = case OsType of
{unix,darwin} -> 27;
_ -> 0
end,

{Msg0,WinSize}=check_text_width(Msg,Right_W+RMarg0),
resize_msg_win(WinSize),
Msg0.

resize_msg_win({X0,Y0,W0,H0}) ->
MsgData0=get_window_data(message),
put_window_data(message,MsgData0#win{x=X0,y=Y0,w=W0,h=H0}),
OldActive = put(wm_active, message),
{_,TopH} = get(wm_top_size),
Y = TopH-(Y0+H0),
ViewPort = {X0,Y,W0,H0},
case put(wm_viewport, ViewPort) of
ViewPort -> ok;
_ -> gl:viewport(X0,Y,W0,H0)
end,
case OldActive of
undefined -> none;
_ -> put(wm_active, OldActive)
end.

check_text_width(Msg,RightW) ->
{W,H0}=win_size(desktop),
MaxW=W-RightW,
Msg0=strip_lines(Msg,MaxW),
NrLn=count_lines(Msg0),
InfoH = case NrLn of
1 -> ?LINE_HEIGHT;
_ -> round(NrLn*?LINE_HEIGHT)
end,
H=InfoH+trunc(?LINE_HEIGHT/2)-1,
Y = H0-InfoH+?CHAR_HEIGHT+2,
{Msg0,{0,Y,W,H}}.

strip_lines(Msg,W) ->
Msg0=lists:flatten(Msg),
Tw=wings_text:width(Msg0),
if Tw=<W -> Msg;
true ->
break_line(Msg0,W)
end.

break_line(Msg,W) ->
Msg0=split_msg(Msg,[]),
MsgSpl=string:tokens(Msg0," "),
{_,MsgSp2}=lists:foldl(fun(E,{WAcc,Acc}) ->
WAcc0 = WAcc+wings_text:width(E)+?CHAR_WIDTH,
if WAcc0>W ->
{0,Acc++[$\n]++E};
true -> {WAcc0,Acc++E++[" "]}
end
end ,{0,[]}, MsgSpl),
MsgSp2.

split_msg([],Acc) -> Acc;
split_msg([{_Fmt,_Txt}=H|T],Acc) ->
split_msg(T,Acc++[H]);
split_msg([H|T],Acc) ->
split_msg(T,Acc++[H]).

count_lines([]) -> 1;
count_lines([$\n|S]) ->
count_lines(S)+1;
count_lines([_|S]) ->
count_lines(S).


%%%
%%% Toplevel: create a window and a controller window at the same time.
%%% The controller adds a title bar (allowing the window to be moved) and
Expand Down
2 changes: 1 addition & 1 deletion src/wings_wm_toplevel.erl
Expand Up @@ -142,7 +142,7 @@ get_ctrl_event(Cs) ->
{replace,fun(Ev) -> ctrl_event(Ev, Cs) end}.

ctrl_event(redraw, Cs) ->
ctrl_message(),
% ctrl_message(),
ctrl_redraw(Cs);
ctrl_event(#mousebutton{button=1,state=?SDL_PRESSED},
#ctrl{state=moving,prev_focus=Focus}=Cs) ->
Expand Down

0 comments on commit a19117f

Please sign in to comment.