summaryrefslogtreecommitdiff
path: root/dialogline.cc
blob: fe7ad01ab332b91c33f1ef69a76b7b688a88e18e (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

#include <config.h>

#include <stdarg.h>
#include <stdlib.h>

#include "dialogline.h"
#include "editor.h"
#include "inputline.h"
#include "question.h"
#include "dbg.h"

DialogLine::DialogLine(Editor *aApp)
{
    app = aApp;
    modal_widget = NULL;
    transient = false;
}

void DialogLine::resize(int lines, int columns, int y, int x)
{
    Label::resize(lines, columns, y, x);
    if (modal_widget)
	modal_widget->resize(lines, columns, y, x);
}

void DialogLine::show_message(const char *msg)
{
    set_text(msg);
    transient = true;
}

void DialogLine::show_error_message(const char *msg)
{
    set_text(msg);
    transient = false;
}

void DialogLine::show_message_fmt(const char *fmt, ...)
{
    u8string msg;
    va_list ap;
    va_start(ap, fmt);
    msg.vcformat(fmt, ap);
    va_end(ap);
    show_message(msg.c_str());
}

INTERACTIVE void DialogLine::layout_windows()
{
    app->layout_windows();
    // move the cursor back to the modal widget
    if (modal_widget)
	modal_widget->invalidate_view();
}

INTERACTIVE void DialogLine::refresh()
{
    app->refresh();
    // move the cursor back to the modal widget
    if (modal_widget)
	modal_widget->invalidate_view();
}

void DialogLine::clear_transient_message()
{
    if (!empty() && transient)
	set_text("");
}

INTERACTIVE void DialogLine::cancel_modal()
{
    modal_canceled = true;
}

// modalize() - does event pumping on some interactive widget.

void DialogLine::modalize(Widget *wgt)
{
    modal_widget = wgt;
    wgt->resize(window_height(), window_width(), window_begy(), window_begx());
    modal_canceled = false;
    while (wgt->is_modal() && !modal_canceled) {
	Event evt;
	wgt->update();
	doupdate();
	get_next_event(evt, wgt->wnd);
	if (!handle_event(evt))
	    wgt->handle_event(evt);
    }
    modal_widget = NULL;
    show_message(modal_canceled ? _("Canceled") : "");
}

bool DialogLine::ask_yes_or_no(const char *msg, bool *canceled)
{
    Question ipt(msg);
    modalize(&ipt);
    if (canceled)
    	*canceled = modal_canceled;
    return modal_canceled ? false : ipt.get_answer();
}

int DialogLine::get_number(const char *msg, int default_num, bool *canceled)
{
    u8string cstrnum;
    cstrnum.cformat("%d", default_num);
    unistring strnum;
    strnum.init_from_utf8(cstrnum.c_str());
    InputLine ipt(msg, strnum);
    modalize(&ipt);
    if (canceled)
    	*canceled = modal_canceled;
    if (modal_canceled) {
	return 0;
    } else {
	cstrnum.init_from_unichars(ipt.get_text());
	return atoi(cstrnum.c_str());
    }
}

unistring DialogLine::query(const char *msg, const char *default_text,
			    int history_set, InputLine::CompleteType complete,
			    bool *alt_kbd)
{
    unistring text;
    text.init_from_utf8(default_text);
    return query(msg, text, history_set, complete, alt_kbd);
}

unistring DialogLine::query(const char *msg, const unistring &default_text,
			    int history_set, InputLine::CompleteType complete,
			    bool *alt_kbd)
{
    InputLine ipt(msg, default_text, history_set, complete);
    if (alt_kbd)
	ipt.set_alt_kbd(*alt_kbd);
    modalize(&ipt);
    if (alt_kbd)
	*alt_kbd = ipt.get_alt_kbd();
    return modal_canceled ? unistring() : ipt.get_text();
}

void DialogLine::update()
{
    if (modal_widget)
	modal_widget->update();
    else
	Label::update();
}

// immediate_update() - called when we want to update the terminal
// immediately, without having to wait for the event pump. As an intentional
// side effect, the terminal cursor moves to this window.
// TODO: move to the Widget class?

void DialogLine::immediate_update()
{
    if (terminal::is_interactive()) {
	update();
	doupdate();
    }
}