1 /*
2     qualitypasswindow.cxx - QualityPassWindow pin entry
3     with Password QualityBar and etc
4 
5     Copyright (C) 2016 Anatoly madRat L. Berenblit
6 
7     Written by Anatoly madRat L. Berenblit <madrat-@users.noreply.github.com>.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.
13 
14     This program is distributed in the hope that it will be useful, but
15     WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with this program; if not, write to the Free Software
21     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22     SPDX-License-Identifier: GPL-2.0+
23 */
24 
25 #include <FL/Fl_Window.H>
26 #include <FL/Fl_Box.H>
27 #include <FL/Fl_Input.H>
28 #include <FL/Fl_Progress.H>
29 
30 #include "qualitypasswindow.h"
31 
32 const char *QualityPassWindow::QUALITY = "Quality";
33 
QualityPassWindow(QualityPassWindow::GetQualityFn qualify,void * ptr)34 QualityPassWindow::QualityPassWindow(QualityPassWindow::GetQualityFn qualify, void* ptr)
35 						: get_quality_(qualify)
36 						,get_quality_user_(ptr)
37 						,quality_(NULL)
38 {
39 	assert(NULL != qualify);
40 }
41 
input_changed(Fl_Widget * input,void * val)42 void QualityPassWindow::input_changed(Fl_Widget *input, void *val)
43 {
44     QualityPassWindow  *self = reinterpret_cast<QualityPassWindow*>(val);
45 
46     assert(NULL != self->get_quality_);   // function should be assigned in ctor
47     assert(NULL != self->quality_);       // quality progress bar must be created in init
48 
49 	if (NULL != self->quality_ && NULL != self->get_quality_)
50 	{
51 		int result = self->get_quality_(self->input_->value(), self->get_quality_user_);
52 		bool isErr = (result <= 0);
53 		if (isErr)
54 			result = -result;
55 		self->quality_->selection_color(isErr?FL_RED:FL_GREEN);
56 		self->quality_->value(std::min(result, 100));
57 	}
58 }
59 
create(QualityPassWindow::GetQualityFn qualify,void * user)60 QualityPassWindow* QualityPassWindow::create(QualityPassWindow::GetQualityFn qualify, void *user)
61 {
62 	QualityPassWindow *p = new QualityPassWindow(qualify, user);
63 	p->init(460, 215);
64 	p->window_->end();
65 	p->input_->take_focus();
66 	return p;
67 }
68 
quality(const char * name)69 void QualityPassWindow::quality(const char *name)
70 {
71 	set_label(quality_, name, QUALITY);
72 }
73 
init(const int cx,const int cy)74 int QualityPassWindow::init(const int cx, const int cy)
75 {
76 	int y = PassWindow::init(cx, cy);
77 	assert(window_ == Fl_Group::current()); // make_window should all add current
78 
79 	input_->when(FL_WHEN_CHANGED);
80 	input_->callback(input_changed, this);
81 
82 	y = input_->y() + input_->h();
83 
84 	quality_ = new Fl_Progress(input_->x(), y+5, input_->w(), 25, QUALITY);
85 	quality_->align(Fl_Align(FL_ALIGN_LEFT | FL_ALIGN_CLIP | FL_ALIGN_WRAP));
86 	quality_->maximum(100.1);
87 	quality_->minimum(0.0);
88 	y = quality_->y() + quality_->h();
89 
90 	error_->position(error_->x(), y+5);
91 
92 	return error_->y() + error_->h();
93 }
94