Dillo v3.2.0-170-gacf511bb
Loading...
Searching...
No Matches
findbar.cc
Go to the documentation of this file.
1/*
2 * File: findbar.cc
3 *
4 * Copyright (C) 2005-2007 Jorge Arellano Cid <jcid@dillo.org>
5 * Copyright (C) 2026 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13#include <FL/Fl.H>
14#include <FL/Fl_Window.H>
15#include "findbar.hh"
16
17#include "msg.h"
18#include "pixmaps.h"
19#include "uicmd.hh"
20#include "bw.h"
21
22/*
23 * Local sub class
24 * (Used to handle escape in the findbar, may also avoid some shortcuts).
25 */
26class MyInput : public Fl_Input {
27public:
28 MyInput (int x, int y, int w, int h, const char* l=0) :
29 Fl_Input(x,y,w,h,l) {};
30 int handle(int e);
31 int d_position();
32 void d_position(int p);
33};
34
35/* FLTK 1.4 deprecated "position()" for "insert_position()", so we make
36 * a backward compatible wrapper. */
37int MyInput::d_position()
38{
39#if FL_API_VERSION < 10400
40 return MyInput::position();
41#else
42 return MyInput::insert_position();
43#endif
44}
45
46void MyInput::d_position(int p)
47{
48#if FL_API_VERSION < 10400
49 MyInput::position(p);
50#else
51 MyInput::insert_position(p);
52#endif
53}
54
55int MyInput::handle(int e)
56{
57 _MSG("findbar MyInput::handle()\n");
58 int ret = 1, k = Fl::event_key();
59 unsigned modifier = Fl::event_state() & (FL_SHIFT| FL_CTRL| FL_ALT|FL_META);
60
61 if (e == FL_KEYBOARD) {
62 if (k == FL_Page_Down || k == FL_Page_Up || k == FL_Up || k == FL_Down) {
63 // Let them through for key commands and viewport motion.
64 return 0;
65 }
66 if (modifier == FL_SHIFT) {
67 if (k == FL_Left || k == FL_Right) {
68 // Let these keys get to the UI
69 return 0;
70 }
71 } else if (modifier == FL_CTRL) {
72 if (k == 'a' || k == 'e') {
73 d_position(k == 'a' ? 0 : size());
74 return 1;
75 } else if (k == 'k') {
76 cut(d_position(), size());
77 return 1;
78 } else if (k == 'd') {
79 cut(d_position(), d_position()+1);
80 return 1;
81 } else if (k == 'h' || k == 'i' || k == 'j' || k == 'l' || k == 'm') {
82 // Fl_Input wants to use ^H as backspace, and also "insert a few
83 // selected control characters literally", but this gets in the way
84 // of key commands.
85 return 0;
86 }
87 } else if (k == FL_Escape && modifier == 0) {
88 // Avoid clearing the text with Esc, just hide the findbar.
89 return 0;
90 }
91 }
92
93 if (ret)
94 ret = Fl_Input::handle(e);
95 return ret;
96}
97
101void Findbar::search_cb(Fl_Widget *, void *vfb)
102{
103 Findbar *fb = (Findbar *)vfb;
104 const char *key = fb->i->value();
105 bool case_sens = fb->check_btn->value();
106
107 if (key[0] != '\0')
109 key, case_sens, false);
110}
111
115void Findbar::searchBackwards_cb(Fl_Widget *, void *vfb)
116{
117 Findbar *fb = (Findbar *)vfb;
118 const char *key = fb->i->value();
119 bool case_sens = fb->check_btn->value();
120
121 if (key[0] != '\0') {
123 key, case_sens, true);
124 }
125}
126
130void Findbar::hide_cb(Fl_Widget *, void *vfb)
131{
133}
134
138Findbar::Findbar(int width, int height) :
139 Fl_Group(0, 0, width, height)
140{
141 int button_width = 70;
142 int gap = 2;
143 int border = 2;
144 int input_width = width - (2 * border + 4 * (button_width + gap));
145 int x = 0;
146
147 Fl_Group::current(0);
148
149 height -= 2 * border;
150
151 box(FL_THIN_UP_BOX);
152
153 hide_btn = new CustButton(x, border, 16, height, 0);
154 hideImg = new Fl_Pixmap(new_s_xpm);
155 hide_btn->image(hideImg);
156 x += 16 + gap;
157 hide_btn->callback(hide_cb, this);
158 hide_btn->clear_visible_focus();
159 hide_btn->box(FL_THIN_UP_BOX);
160 hide_btn->set_tooltip("Hide");
161 add(hide_btn);
162
163 i = new MyInput(x, border, input_width, height);
164 x += input_width + gap;
165 resizable(i);
166 i->when(FL_WHEN_NEVER);
167 add(i);
168
169 next_btn = new CustButton(x, border, button_width, height, "Next");
170 x += button_width + gap;
171 next_btn->shortcut(FL_Enter);
172 next_btn->callback(search_cb, this);
173 next_btn->clear_visible_focus();
174 next_btn->box(FL_THIN_UP_BOX);
175 next_btn->set_tooltip("Find next occurrence of the search phrase\n"
176 "shortcut: Enter");
177 add(next_btn);
178
179 prev_btn= new CustButton(x, border, button_width, height, "Previous");
180 x += button_width + gap;
181 prev_btn->shortcut(FL_SHIFT+FL_Enter);
182 prev_btn->callback(searchBackwards_cb, this);
183 prev_btn->clear_visible_focus();
184 prev_btn->box(FL_THIN_UP_BOX);
185 prev_btn->set_tooltip("Find previous occurrence of the search phrase\n"
186 "shortcut: Shift+Enter");
187 add(prev_btn);
188
189 check_btn = new Fl_Check_Button(x, border, 2*button_width, height,
190 "Case-sensitive");
191 x += 2 * button_width + gap;
192 check_btn->clear_visible_focus();
193 add(check_btn);
194
195}
196
198{
199 delete hideImg;
200}
201
205int Findbar::handle(int event)
206{
207 int k = Fl::event_key();
208 unsigned modifier = Fl::event_state() & (FL_SHIFT| FL_CTRL| FL_ALT|FL_META);
209
210 if (event == FL_KEYBOARD && modifier == 0 && k == FL_Escape) {
211 /* let the UI handle it */
212 return 0;
213 }
214
215 return Fl_Group::handle(event);
216}
217
222{
224 dReturn_if (bw == NULL);
225
226 // It takes more than just calling show() to do the trick
227 Fl_Group::show();
228
229 /* select text even if already focused */
230 i->take_focus();
231#if FL_API_VERSION < 10400
232 i->position(i->size(), 0);
233#else
234 i->insert_position(i->size(), 0);
235#endif
236}
237
#define _MSG(...)
Definition bookmarks.c:44
A button that highlights on mouse over.
Definition tipwin.hh:49
Searchbar to find text in page.
Definition findbar.hh:16
static void hide_cb(Fl_Widget *, void *)
Hide the search bar.
Definition findbar.cc:130
int handle(int event)
Handle events.
Definition findbar.cc:205
Fl_Pixmap * hideImg
Definition findbar.hh:19
CustButton * hide_btn
Definition findbar.hh:17
void show()
Show the findbar and focus the input field.
Definition findbar.cc:221
Fl_Check_Button * check_btn
Definition findbar.hh:18
static void searchBackwards_cb(Fl_Widget *, void *)
Find previous occurrence of input key.
Definition findbar.cc:115
~Findbar()
Definition findbar.cc:197
CustButton * next_btn
Definition findbar.hh:17
static void search_cb(Fl_Widget *, void *)
Find next occurrence of input key.
Definition findbar.cc:101
CustButton * prev_btn
Definition findbar.hh:17
Fl_Input * i
Definition findbar.hh:20
Findbar(int width, int height)
Construct text search bar.
Definition findbar.cc:138
void set_tooltip(const char *s)
Definition tipwin.cc:162
#define dReturn_if(expr)
Definition dlib.h:93
static const char *const new_s_xpm[]
Definition pixmaps.h:1413
Contains the specific data for a single window.
Definition bw.h:27
void a_UIcmd_findtext_search(BrowserWindow *bw, const char *key, int case_sens, int backward)
Definition uicmd.cc:1708
BrowserWindow * a_UIcmd_get_bw_by_widget(void *v_wid)
Definition uicmd.cc:554
void a_UIcmd_findbar_toggle(BrowserWindow *bw, int on)
Definition uicmd.cc:1741