~ubuntu-branches/ubuntu/wily/flrig/wily

« back to all changes in this revision

Viewing changes to src/widgets/flinput2.cxx

  • Committer: Package Import Robot
  • Author(s): Kamal Mostafa
  • Date: 2014-10-25 11:17:10 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20141025111710-n32skgya3l9u1brw
Tags: 1.3.17-1
* New upstream release (Closes: #761839)
* Debian Standards-Version: 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// =====================================================================
 
2
//
 
3
// flinput2.cxx
 
4
//
 
5
// Author: Stelios Buononos, M0GLD
 
6
// Copyright: 2010
 
7
//
 
8
// This file is part of flrig.
 
9
//
 
10
// flrig is free software; you can redistribute it and/or modify
 
11
// it under the terms of the GNU General Public License as published by
 
12
// the Free Software Foundation; either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// flrig is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU General Public License
 
21
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
// ----------------------------------------------------------------------------
 
23
 
 
24
#include <cctype>
 
25
 
 
26
#include <FL/Fl.H>
 
27
#include <FL/Fl_Window.H>
 
28
#include <FL/Fl_Widget.H>
 
29
#include <FL/Fl_Input.H>
 
30
#include <FL/Fl_Menu_Item.H>
 
31
#include <FL/Fl_Tooltip.H>
 
32
 
 
33
#include "config.h"
 
34
 
 
35
#include "icons.h"
 
36
#include "flinput2.h"
 
37
#include "gettext.h"
 
38
#include "util.h"
 
39
 
 
40
enum { OP_UNDO, OP_CUT, OP_COPY, OP_PASTE, OP_DELETE, OP_CLEAR, OP_SELECT_ALL };
 
41
 
 
42
static Fl_Menu_Item cmenu[] = {
 
43
        { make_icon_label(_("Undo"), edit_undo_icon), 0, 0, 0, FL_MENU_DIVIDER, _FL_MULTI_LABEL },
 
44
        { make_icon_label(_("Cut"), edit_cut_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
 
45
        { make_icon_label(_("Copy"), edit_copy_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
 
46
        { make_icon_label(_("Paste"), edit_paste_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
 
47
        { make_icon_label(_("Delete"), trash_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
 
48
        { make_icon_label(_("Clear"), edit_clear_icon), 0, 0, 0, FL_MENU_DIVIDER, _FL_MULTI_LABEL },
 
49
        { make_icon_label(_("Select All"), edit_select_all_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
 
50
        { 0 }
 
51
};
 
52
static bool cmenu_init = false;
 
53
 
 
54
 
 
55
Fl_Input2::Fl_Input2(int x, int y, int w, int h, const char* l)
 
56
        : Fl_Input(x, y, w, h, l)
 
57
{
 
58
        if (!cmenu_init) {
 
59
                for (size_t i = 0; i < sizeof(cmenu)/sizeof(*cmenu) - 1; i++)
 
60
                        if (cmenu[i].labeltype() == _FL_MULTI_LABEL)
 
61
                                set_icon_label(&cmenu[i]);
 
62
                cmenu_init = true;
 
63
        }
 
64
}
 
65
 
 
66
int Fl_Input2::handle(int event)
 
67
{
 
68
        switch (event) {
 
69
        case FL_KEYBOARD: {
 
70
                int b = Fl::event_key();
 
71
                int p = position();
 
72
                // stop the move-to-next-field madness, we have Tab for that!
 
73
                if (unlikely((b == FL_Left && p == 0) || (b == FL_Right && p == size()) ||
 
74
                             (b == FL_Up && line_start(p) == 0) ||
 
75
                             (b == FL_Down && line_end(p) == size())))
 
76
                        return 1;
 
77
                else if (unlikely(Fl::event_state() & (FL_ALT | FL_META))) {
 
78
                        switch (b) {
 
79
                        case 'c': { // capitalise
 
80
                                if (readonly() || p == size())
 
81
                                        return 1;
 
82
 
 
83
                                while (p < size() && isspace(*(value() + p)))
 
84
                                        p++;
 
85
                                if (p == size())
 
86
                                        return 1;
 
87
                                char c = toupper(*(value() + p));
 
88
                                replace(p, p + 1, &c, 1);
 
89
                                position(word_end(p));
 
90
                        }
 
91
                                return 1;
 
92
                        case 'u': case 'l': { // upper/lower case
 
93
                                if (readonly() || p == size())
 
94
                                        return 1;
 
95
                                while (p < size() && isspace(*(value() + p)))
 
96
                                        p++;
 
97
                                int n = word_end(p) - p;
 
98
                                if (n == 0)
 
99
                                        return 1;
 
100
 
 
101
                                char* s = new char[n];
 
102
                                memcpy(s, value() + p, n);
 
103
                                if (b == 'u')
 
104
                                        for (int i = 0; i < n; i++)
 
105
                                                s[i] = toupper(s[i]);
 
106
                                else
 
107
                                        for (int i = 0; i < n; i++)
 
108
                                                s[i] = tolower(s[i]);
 
109
                                replace(p, p + n, s, n);
 
110
                                position(p + n);
 
111
                                delete [] s;
 
112
                                return 1;
 
113
                        }
 
114
                        default:
 
115
                                break;
 
116
                        }
 
117
                }
 
118
        }
 
119
                return Fl_Input::handle(event);
 
120
        case FL_MOUSEWHEEL: {
 
121
                if (!((type() & (FL_MULTILINE_INPUT | FL_MULTILINE_OUTPUT)) && Fl::event_inside(this)))
 
122
                        return Fl_Input::handle(event);
 
123
                int d;
 
124
                if (!((d = Fl::event_dy()) || (d = Fl::event_dx())))
 
125
                        return Fl_Input::handle(event);
 
126
                if (Fl::focus() != this)
 
127
                        take_focus();
 
128
                up_down_position(d + (d > 0 ? line_end(position()) : line_start(position())));
 
129
                return 1;
 
130
        }
 
131
        case FL_PUSH:
 
132
                if (Fl::event_button() == FL_RIGHT_MOUSE)
 
133
                        break;
 
134
                // fall through
 
135
        default:
 
136
                return Fl_Input::handle(event);
 
137
        }
 
138
 
 
139
        bool sel = position() != mark(), ro = readonly();
 
140
        set_active(&cmenu[OP_UNDO], !ro);
 
141
        set_active(&cmenu[OP_CUT], !ro && sel);
 
142
        set_active(&cmenu[OP_COPY], sel);
 
143
        set_active(&cmenu[OP_PASTE], !ro);
 
144
        set_active(&cmenu[OP_DELETE], !ro && sel);
 
145
        set_active(&cmenu[OP_CLEAR], !ro && size());
 
146
        set_active(&cmenu[OP_SELECT_ALL], size());
 
147
 
 
148
        take_focus();
 
149
        window()->cursor(FL_CURSOR_DEFAULT);
 
150
        int t = Fl_Tooltip::enabled();
 
151
        Fl_Tooltip::disable();
 
152
        const Fl_Menu_Item* m = cmenu->popup(Fl::event_x(), Fl::event_y());
 
153
        Fl_Tooltip::enable(t);
 
154
 
 
155
        if (!m)
 
156
                return 1;
 
157
        switch (m - cmenu) {
 
158
        case OP_UNDO:
 
159
                undo();
 
160
                break;
 
161
        case OP_CUT:
 
162
                cut();
 
163
                copy_cuts();
 
164
                break;
 
165
        case OP_COPY:
 
166
                copy(1);
 
167
                break;
 
168
        case OP_PASTE:
 
169
                Fl::paste(*this, 1);
 
170
                break;
 
171
        case OP_DELETE:
 
172
                cut();
 
173
                break;
 
174
        case OP_CLEAR:
 
175
                cut(0, size());
 
176
                break;
 
177
        case OP_SELECT_ALL:
 
178
                position(0, size());
 
179
                break;
 
180
        }
 
181
 
 
182
        return 1;
 
183
}