aboutsummaryrefslogtreecommitdiffstats
path: root/lib/debugger/src/dbg_ui_edit_win.erl
blob: 2e9a685b57f95748427d436fda42602f847f0fe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2002-2012. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
-module(dbg_ui_edit_win).
-compile([{nowarn_deprecated_function,{gs,button,2}},
          {nowarn_deprecated_function,{gs,config,2}},
          {nowarn_deprecated_function,{gs,entry,2}},
          {nowarn_deprecated_function,{gs,label,2}},
          {nowarn_deprecated_function,{gs,read,2}},
          {nowarn_deprecated_function,{gs,window,2}}]).

%% External exports
-export([create_win/5, get_window/1,
	 handle_event/2]).

-record(winInfo, {window,   % gsobj()
		  entry,    % gsobj()
		  button,   % gsobj()
		  type      % atom()
		 }).

%%====================================================================
%% External exports
%%====================================================================

%%--------------------------------------------------------------------
%% create_win(GS, Pos, Title, Prompt, {Type, Value}) -> #winInfo{}
%%   GS = graphics system identifier
%%   Pos = {X, Y}
%%     X = Y = integer()
%%   Title = string()
%%   Prompt = atom()
%%   Type = term | atom | float | integer | string
%%   Value = term()
%%--------------------------------------------------------------------
create_win(GS, {X, Y}, Title, Prompt, {Type, Value}) ->
    Pad=8,

    Font = dbg_ui_win:font(normal),

    %% Window
    Win = gs:window(GS, [{title, Title}, {x, X}, {y, Y},
			 {destroy, true}]),

    %% Label
    {Wlbl, Hlbl} = dbg_ui_win:min_size([Prompt], 50, 30),
    gs:label(Win, [{x, Pad}, {y, Pad}, {width, Wlbl}, {height, Hlbl},
		   {align, e}, {label, {text, Prompt}}, {font, Font}]),


    %% Entry
    {Went, _Hent} = dbg_ui_win:min_size([Value], 100, 20),
    Ent = gs:entry(Win, [{x, Pad+Wlbl}, {y, Pad},
			 {width, Went}, {height, Hlbl},
			 {text, Value},
			 {keypress, true}]),

    %% OK and Cancel buttons
    W = Pad + Wlbl + Went + Pad,
    {Wbtn, Hbtn} = dbg_ui_win:min_size(["Cancel"], 70, 30),
    Ybtn = Pad + Hlbl + Pad,
    Btn = gs:button(Win, [{x, Pad}, {y, Ybtn},
			  {width, Wbtn}, {height, Hbtn},
			  {label, {text,"OK"}}, {font, Font}]),
    gs:button(Win, [{x, W-Pad-Wbtn}, {y, Ybtn},
		    {width, Wbtn}, {height, Hbtn},
		    {label, {text,"Cancel"}}, {font, Font}]),

    H = Ybtn + Hbtn + Pad,
    gs:config(Win, [{width, W}, {height, H}, {map, true}]),

    #winInfo{window=Win, entry=Ent, button=Btn, type=Type}.

%%--------------------------------------------------------------------
%% get_window(WinInfo) -> Window
%%   WinInfo = #winInfo{}
%%   Window = gsobj()
%%--------------------------------------------------------------------
get_window(WinInfo) ->
    WinInfo#winInfo.window.

%%--------------------------------------------------------------------
%% handle_event(GSEvent, WinInfo) -> Command
%% GSEvent = {gs, Id, Event, Data, Arg}
%% WinInfo = #winInfo{}
%% Command = ignore
%%         | stopped
%%         | {edit, Value}
%%--------------------------------------------------------------------
handle_event({gs, _Id, destroy, _Data, _Arg}, _WinInfo) ->
    stopped;
handle_event({gs, Id, keypress, Data, ['Return'|_]}, WinInfo) ->
    gs:config(WinInfo#winInfo.button, flash),
    handle_event({gs, Id, click, Data, ["OK"]}, WinInfo);
handle_event({gs, _Id, click, _Data, ["OK"|_]}, WinInfo) ->
    Ent = WinInfo#winInfo.entry,
    Str = gs:read(Ent, text),
    Type = WinInfo#winInfo.type,
    case erl_scan:string(Str) of
	{ok, Tokens, _EndLine} when Type==term ->
	    case erl_parse:parse_term(Tokens++[{dot, 1}]) of
		{ok, Value} -> {edit, Value};
		_Error -> ignore
	    end;
	{ok, [{Type, _Line, Value}], _EndLine} when Type/=term ->
	    {edit, Value};
	_ ->
	    ignore
    end;
handle_event({gs, _Id, click, _Data, ["Cancel"|_]}, _WinInfo) ->
    stopped;
handle_event(_GSEvent, _WinInfo) ->
    ignore.