1 // Copyright (c) Warwick Allison, 1999.
2 // Qt4 conversion copyright (c) Ray Chason, 2012-2014.
3 // NetHack may be freely redistributed.  See license for details.
4 
5 // qt_streq.cpp -- string requestor
6 
7 extern "C" {
8 #include "hack.h"
9 }
10 
11 #include "qt_pre.h"
12 #include <QtGui/QtGui>
13 #if QT_VERSION >= 0x050000
14 #include <QtWidgets/QtWidgets>
15 #endif
16 #include "qt_post.h"
17 #include "qt_streq.h"
18 #include "qt_str.h"
19 #include "qt_set.h"
20 
21 namespace nethack_qt_ {
22 
23 // temporary
24 void centerOnMain(QWidget *);
25 // end temporary
26 
NetHackQtStringRequestor(QWidget * parent,const char * p,const char * cancelstr,const char * okaystr)27 NetHackQtStringRequestor::NetHackQtStringRequestor(QWidget *parent,
28         const char *p, const char *cancelstr, const char *okaystr) :
29     QDialog(parent),
30     prompt(QString::fromLatin1(p),this),
31     input(this,"input")
32 {
33     if (qt_settings)
34         input.setFont(qt_settings->normalFixedFont());
35 
36     cancel=new QPushButton(cancelstr,this);
37     connect(cancel,SIGNAL(clicked()),this,SLOT(reject()));
38 
39     okay = new QPushButton(okaystr, this);
40     connect(okay,SIGNAL(clicked()),this,SLOT(accept()));
41     connect(&input,SIGNAL(returnPressed()),this,SLOT(accept()));
42     okay->setDefault(true);
43 
44     setFocusPolicy(Qt::StrongFocus);
45 }
46 
resizeEvent(QResizeEvent *)47 void NetHackQtStringRequestor::resizeEvent(QResizeEvent*)
48 {
49     const int margin=5;
50     const int gutter=5;
51 
52     int h = (height() - margin * 2 - gutter);
53     int w = (width() - margin * 2 - gutter);
54     int ifw = input.hasFrame() ? 3 : 0; // hack alert for input.frameWidth()
55     if (prompt.text().size() > 16) {
56         h /= 3;
57         prompt.setGeometry(margin + ifw * 2 + 1, margin, w + gutter, h);
58         input.setGeometry(width() * 1 / 5 - ifw, margin + h + gutter,
59                           w * 4 / 5, h);
60     } else {
61         h /= 2;
62         prompt.setGeometry(margin + ifw * 2 + 1, margin, w * 2 / 5, h);
63         input.setGeometry(prompt.geometry().right() + gutter
64                            - (ifw * 2 + 1) - ifw * 2,
65                           margin, w * 3 / 5, h);
66     }
67 
68     cancel->setGeometry(margin, input.geometry().bottom() + gutter, w / 2, h);
69     okay->setGeometry(cancel->geometry().right() + gutter,
70                       cancel->geometry().y(), w / 2, h);
71 }
72 
SetDefault(const char * d)73 void NetHackQtStringRequestor::SetDefault(const char *d)
74 {
75     input.setText(d);
76 }
77 
Get(char * buffer,int maxchar,int minchar)78 bool NetHackQtStringRequestor::Get(char *buffer, int maxchar, int minchar)
79 {
80     input.setMaxLength(maxchar - 1);
81 
82     const QString &txt = prompt.text();
83     int pw = fontMetrics().width(txt),
84         ww = minchar * input.fontMetrics().width(QChar('X'));
85     int heightfactor = ((txt.size() > 16) ? 3 : 2) * 2; // 2 or 3 lines high
86     int widthfudge = (((txt.size() > 16) ? 1 : 2) * 5) * 2; // 5: margn, guttr
87     resize(pw + ww + widthfudge, fontMetrics().height() * heightfactor);
88 
89 #ifdef EDIT_GETLIN
90     input.setText(buffer);
91 #endif
92     centerOnMain(this);
93     show();
94     // Make sure that setFocus() really does change keyboard focus.
95     // This allows typing to go directly to the NetHackQtLineEdit
96     // widget without clicking on or in it first.  Not needed for
97     // qt_getline() but is needed for menu Search to prevent typed
98     // characters being treated as making menu selections.
99     if (!input.isActiveWindow())
100         input.activateWindow();
101     input.setFocus();
102     exec();
103 
104     if (result()) {
105         str_copy(buffer, input.text().toLatin1().constData(), maxchar);
106 	return true;
107     } else {
108 	return false;
109     }
110 }
111 
112 } // namespace nethack_qt_
113