NHDT->ANH, nethack->anethack, nhdat->anhdat
[aNetHack.git] / include / qt_win.h
blob73b45eae27af58f04cf172a47bb5fa5b09f7d95a
1 // aNetHack 0.0.1 qt_win.h $ANH-Date: 1447755972 2015/11/17 10:26:12 $ $ANH-Branch: master $:$ANH-Revision: 1.17 $
2 // Copyright (c) Warwick Allison, 1999.
3 // aNetHack may be freely redistributed. See license for details.
4 //
5 // Qt Binding for aNetHack 3.4
6 //
7 // Unfortunately, this doesn't use Qt as well as I would like,
8 // primarily because aNetHack is fundamentally a getkey-type
9 // program rather than being event driven (hence the ugly key
10 // and click buffer rather), but also because this is my first
11 // major application of Qt.
14 #ifndef qt_win_h
15 #define qt_win_h
17 #define QT_CLEAN_NAMESPACE
19 #include <qdialog.h>
20 #include <qpushbutton.h>
21 #include <qbuttongroup.h>
22 #include <qlabel.h>
23 #include <qlineedit.h>
24 #if defined(QWS)
25 #include <qpe/qpeapplication.h>
26 #else
27 #include <qapplication.h>
28 #endif
29 #include <qspinbox.h>
30 #include <qcheckbox.h>
31 #include <qfile.h>
32 #include <qlistbox.h>
33 #include <qlistview.h>
34 #include <qmessagebox.h>
35 #include <qpixmap.h>
36 #include <qimage.h>
37 #include <qarray.h>
38 #include <qcombobox.h>
39 #include <qscrollview.h>
40 #if QT_VERSION >= 300
41 #include <qttableview.h>
42 // Should stop using QTableView
43 #define QTableView QtTableView
44 #else
45 #include <qtableview.h>
46 #endif
47 #include <qmainwindow.h>
48 #include <qwidgetstack.h>
50 #ifdef KDE
51 #include <kapp.h>
52 #include <ktopwidget.h>
53 #endif
55 #include "qt_clust.h"
57 class QVBox;
58 class QMenuBar;
59 class QRadioButton;
60 class NhPSListView;
62 //////////////////////////////////////////////////////////////
64 // The beautiful, abstracted and well-modelled classes...
66 //////////////////////////////////////////////////////////////
68 class aNetHackQtGlyphs;
70 class aNetHackQtLineEdit : public QLineEdit
72 public:
73 aNetHackQtLineEdit();
74 aNetHackQtLineEdit(QWidget *parent, const char *name);
76 void fakeEvent(int key, int ascii, int state);
79 class aNetHackQtSettings : public QDialog
81 Q_OBJECT
82 public:
83 // Size of window - used to decide default sizes
84 aNetHackQtSettings(int width, int height);
86 aNetHackQtGlyphs &glyphs();
87 const QFont &normalFont();
88 const QFont &normalFixedFont();
89 const QFont &largeFont();
91 bool ynInMessages();
93 signals:
94 void fontChanged();
95 void tilesChanged();
97 public slots:
98 void toggleGlyphSize();
99 void setGlyphSize(bool);
101 private:
102 QSpinBox tilewidth;
103 QSpinBox tileheight;
104 QLabel widthlbl;
105 QLabel heightlbl;
106 QCheckBox whichsize;
107 QSize othersize;
109 QComboBox fontsize;
111 QFont normal, normalfixed, large;
113 aNetHackQtGlyphs *theglyphs;
115 private slots:
116 void resizeTiles();
119 class aNetHackQtKeyBuffer
121 public:
122 aNetHackQtKeyBuffer();
124 bool Empty() const;
125 bool Full() const;
127 void Put(int k, int ascii, int state);
128 void Put(char a);
129 void Put(const char *str);
130 int GetKey();
131 int GetAscii();
132 int GetState();
134 int TopKey() const;
135 int TopAscii() const;
136 int TopState() const;
138 private:
139 enum { maxkey = 64 };
140 int key[maxkey];
141 int ascii[maxkey];
142 int state[maxkey];
143 int in, out;
146 class aNetHackQtClickBuffer
148 public:
149 aNetHackQtClickBuffer();
151 bool Empty() const;
152 bool Full() const;
154 void Put(int x, int y, int mod);
156 int NextX() const;
157 int NextY() const;
158 int NextMod() const;
160 void Get();
162 private:
163 enum { maxclick = 64 };
164 struct ClickRec {
165 int x, y, mod;
166 } click[maxclick];
167 int in, out;
170 class aNetHackQtSavedGameSelector : public QDialog
172 public:
173 aNetHackQtSavedGameSelector(const char **saved);
175 int choose();
178 class aNetHackQtPlayerSelector : private QDialog
180 Q_OBJECT
181 public:
182 enum { R_None = -1, R_Quit = -2, R_Rand = -3 };
184 aNetHackQtPlayerSelector(aNetHackQtKeyBuffer &);
186 protected:
187 virtual void done(int);
189 public slots:
190 void Quit();
191 void Random();
193 void selectName(const QString &n);
194 void selectRole();
195 void selectRace();
196 void setupOthers();
197 void selectGender(int);
198 void selectAlignment(int);
200 public:
201 bool Choose();
203 private:
204 aNetHackQtKeyBuffer &keysource;
205 NhPSListView *role;
206 NhPSListView *race;
207 QRadioButton **gender;
208 QRadioButton **alignment;
209 bool fully_specified_role;
212 class aNetHackQtStringRequestor : QDialog
214 private:
215 QLabel prompt;
216 aNetHackQtLineEdit input;
217 QPushButton *okay;
218 QPushButton *cancel;
219 aNetHackQtKeyBuffer &keysource;
221 virtual void done(int);
223 public:
224 aNetHackQtStringRequestor(aNetHackQtKeyBuffer &, const char *p,
225 const char *cancelstr = "Cancel");
226 void SetDefault(const char *);
227 bool Get(char *buffer, int maxchar = 80);
228 virtual void resizeEvent(QResizeEvent *);
231 class aNetHackQtExtCmdRequestor : public QDialog
233 Q_OBJECT
235 aNetHackQtKeyBuffer &keysource;
237 public:
238 aNetHackQtExtCmdRequestor(aNetHackQtKeyBuffer &ks);
239 int get();
241 private slots:
242 void cancel();
243 void done(int i);
246 class aNetHackQtWindow
248 public:
249 aNetHackQtWindow();
250 virtual ~aNetHackQtWindow();
252 virtual QWidget *Widget() = 0;
254 virtual void Clear();
255 virtual void Display(bool block);
256 virtual bool Destroy();
257 virtual void CursorTo(int x, int y);
258 virtual void PutStr(int attr, const char *text);
259 virtual void StartMenu();
260 virtual void AddMenu(int glyph, const ANY_P *identifier, char ch,
261 char gch, int attr, const char *str, bool presel);
262 virtual void EndMenu(const char *prompt);
263 virtual int SelectMenu(int how, MENU_ITEM_P **menu_list);
264 virtual void ClipAround(int x, int y);
265 virtual void PrintGlyph(int x, int y, int glyph);
266 virtual void UseRIP(int how, time_t when);
268 int nhid;
271 class aNetHackQtGlyphs
273 public:
274 aNetHackQtGlyphs();
277 width() const
279 return size.width();
282 height() const
284 return size.height();
286 void toggleSize();
287 void setSize(int w, int h);
289 void drawGlyph(QPainter &, int glyph, int pixelx, int pixely);
290 void drawCell(QPainter &, int glyph, int cellx, int celly);
292 private:
293 QImage img;
294 QPixmap pm, pm1, pm2;
295 QSize size;
296 int tiles_per_row;
299 class BlackScrollView : public QScrollView
301 public:
302 BlackScrollView()
304 viewport()->setBackgroundColor(black);
308 class aNetHackQtMapWindow : public QWidget, public aNetHackQtWindow
310 Q_OBJECT
311 private:
312 aNetHackQtClickBuffer &clicksink;
313 unsigned short glyph[ROWNO][COLNO];
314 unsigned short &
315 Glyph(int x, int y)
317 return glyph[y][x];
319 QPoint cursor;
320 BlackScrollView viewport;
321 QPixmap pet_annotation;
322 Clusterizer change;
323 QFont *rogue_font;
324 QString messages;
325 QRect messages_rect;
327 void Changed(int x, int y);
329 signals:
330 void resized();
332 private slots:
333 void updateTiles();
334 void moveMessages(int x, int y);
335 #ifdef SAFERHANGUP
336 void timeout();
337 #endif
339 protected:
340 virtual void paintEvent(QPaintEvent *);
341 virtual void mousePressEvent(QMouseEvent *);
343 public:
344 aNetHackQtMapWindow(aNetHackQtClickBuffer &click_sink);
345 ~aNetHackQtMapWindow();
347 virtual QWidget *Widget();
348 virtual bool Destroy();
350 virtual void Clear();
351 virtual void Display(bool block);
352 virtual void CursorTo(int x, int y);
353 virtual void PutStr(int attr, const char *text);
354 virtual void ClipAround(int x, int y);
355 virtual void PrintGlyph(int x, int y, int glyph);
357 void Scroll(int dx, int dy);
359 // For messages
360 void displayMessages(bool block);
361 void putMessage(int attr, const char *text);
362 void clearMessages();
364 void clickCursor();
367 class aNetHackQtScrollText;
368 class aNetHackQtMessageWindow : QObject, public aNetHackQtWindow
370 Q_OBJECT
371 public:
372 aNetHackQtMessageWindow();
373 ~aNetHackQtMessageWindow();
375 virtual QWidget *Widget();
376 virtual void Clear();
377 virtual void Display(bool block);
378 virtual void PutStr(int attr, const char *text);
380 void Scroll(int dx, int dy);
382 void setMap(aNetHackQtMapWindow *);
384 private:
385 aNetHackQtScrollText *list;
386 bool changed;
387 aNetHackQtMapWindow *map;
389 private slots:
390 void updateFont();
393 class aNetHackQtLabelledIcon : public QWidget
395 public:
396 aNetHackQtLabelledIcon(QWidget *parent, const char *label);
397 aNetHackQtLabelledIcon(QWidget *parent, const char *label,
398 const QPixmap &icon);
400 enum { NoNum = -99999 };
401 void setLabel(const char *, bool lower = TRUE); // a string
402 void setLabel(const char *, long, const char *tail = ""); // a number
403 void setLabel(const char *, long show_value, long comparative_value,
404 const char *tail = "");
405 void setIcon(const QPixmap &);
406 virtual void setFont(const QFont &);
408 void highlightWhenChanging();
409 void lowIsGood();
410 void dissipateHighlight();
412 virtual void show();
414 protected:
415 void resizeEvent(QResizeEvent *);
417 private:
418 void initHighlight();
419 void setAlignments();
420 void highlight(const QPalette &highlight);
421 void unhighlight();
423 bool low_is_good;
424 int prev_value;
425 int turn_count; /* last time the value changed */
426 QPalette hl_good;
427 QPalette hl_bad;
429 QLabel *label;
430 QLabel *icon;
433 class aNetHackQtStatusWindow : QWidget, public aNetHackQtWindow
435 Q_OBJECT
436 public:
437 aNetHackQtStatusWindow();
439 virtual QWidget *Widget();
441 virtual void Clear();
442 virtual void Display(bool block);
443 virtual void CursorTo(int x, int y);
444 virtual void PutStr(int attr, const char *text);
446 void fadeHighlighting();
448 protected:
449 void resizeEvent(QResizeEvent *);
451 private slots:
452 void doUpdate();
454 private:
455 enum { hilight_time = 1 };
457 QPixmap p_str;
458 QPixmap p_dex;
459 QPixmap p_con;
460 QPixmap p_int;
461 QPixmap p_wis;
462 QPixmap p_cha;
464 QPixmap p_chaotic;
465 QPixmap p_neutral;
466 QPixmap p_lawful;
468 QPixmap p_satiated;
469 QPixmap p_hungry;
471 QPixmap p_confused;
472 QPixmap p_sick_fp;
473 QPixmap p_sick_il;
474 QPixmap p_blind;
475 QPixmap p_stunned;
476 QPixmap p_hallu;
478 QPixmap p_encumber[5];
480 aNetHackQtLabelledIcon name;
481 aNetHackQtLabelledIcon dlevel;
483 aNetHackQtLabelledIcon str;
484 aNetHackQtLabelledIcon dex;
485 aNetHackQtLabelledIcon con;
486 aNetHackQtLabelledIcon intel;
487 aNetHackQtLabelledIcon wis;
488 aNetHackQtLabelledIcon cha;
490 aNetHackQtLabelledIcon gold;
491 aNetHackQtLabelledIcon hp;
492 aNetHackQtLabelledIcon power;
493 aNetHackQtLabelledIcon ac;
494 aNetHackQtLabelledIcon level;
495 aNetHackQtLabelledIcon exp;
496 aNetHackQtLabelledIcon align;
498 aNetHackQtLabelledIcon time;
499 aNetHackQtLabelledIcon score;
501 aNetHackQtLabelledIcon hunger;
502 aNetHackQtLabelledIcon confused;
503 aNetHackQtLabelledIcon sick_fp;
504 aNetHackQtLabelledIcon sick_il;
505 aNetHackQtLabelledIcon blind;
506 aNetHackQtLabelledIcon stunned;
507 aNetHackQtLabelledIcon hallu;
508 aNetHackQtLabelledIcon encumber;
510 QFrame hline1;
511 QFrame hline2;
512 QFrame hline3;
514 int cursy;
516 bool first_set;
518 void nullOut();
519 void updateStats();
520 void checkTurnEvents();
523 class aNetHackQtMenuDialog : public QDialog
525 Q_OBJECT
526 public:
527 aNetHackQtMenuDialog();
529 void Accept();
530 void Reject();
531 void SetResult(int);
533 virtual void done(int);
535 protected:
536 void resizeEvent(QResizeEvent *);
538 signals:
539 void Resized();
542 class aNetHackQtMenuWindow : public QTableView, public aNetHackQtWindow
544 Q_OBJECT
545 public:
546 aNetHackQtMenuWindow(aNetHackQtKeyBuffer &);
547 ~aNetHackQtMenuWindow();
549 virtual QWidget *Widget();
551 virtual void StartMenu();
552 virtual void AddMenu(int glyph, const ANY_P *identifier, char ch,
553 char gch, int attr, const char *str, bool presel);
554 virtual void EndMenu(const char *prompt);
555 virtual int SelectMenu(int how, MENU_ITEM_P **menu_list);
557 public slots:
558 void All();
559 void ChooseNone();
560 void Invert();
561 void Search();
563 void Layout();
564 void ToggleSelect(int);
566 protected:
567 virtual void keyPressEvent(QKeyEvent *);
568 // virtual void mouseDoubleClickEvent(QMouseEvent*);
569 virtual void mousePressEvent(QMouseEvent *);
570 virtual void mouseReleaseEvent(QMouseEvent *);
571 virtual void mouseMoveEvent(QMouseEvent *);
572 virtual void focusOutEvent(QFocusEvent *);
573 virtual void focusInEvent(QFocusEvent *);
575 virtual void paintCell(QPainter *, int, int);
576 virtual int cellWidth(int col);
578 private:
579 struct MenuItem {
580 MenuItem();
581 ~MenuItem();
583 int glyph;
584 ANY_P identifier;
585 int attr;
586 const char *str;
587 int count;
588 char ch;
589 bool selected;
591 bool
592 Selectable() const
594 return identifier.a_void != 0;
598 QArray<MenuItem> item;
600 int itemcount;
601 int str_width;
602 bool str_fixed;
603 int next_accel;
605 aNetHackQtKeyBuffer &keysource;
607 aNetHackQtMenuDialog *dialog;
609 QPushButton *ok;
610 QPushButton *cancel;
611 QPushButton *all;
612 QPushButton *none;
613 QPushButton *invert;
614 QPushButton *search;
615 QLabel prompt;
617 int how;
619 bool has_glyphs;
621 int pressed;
622 bool was_sel;
625 class aNetHackQtTextListBox;
627 class aNetHackQtRIP : public QWidget
629 private:
630 static QPixmap *pixmap;
631 char **line;
632 int riplines;
634 public:
635 aNetHackQtRIP(QWidget *parent);
637 void setLines(char **l, int n);
639 protected:
640 virtual void paintEvent(QPaintEvent *event);
641 QSize sizeHint() const;
644 class aNetHackQtTextWindow : public QDialog, public aNetHackQtWindow
646 Q_OBJECT
647 public:
648 aNetHackQtTextWindow(aNetHackQtKeyBuffer &);
649 ~aNetHackQtTextWindow();
651 virtual QWidget *Widget();
653 virtual void Clear();
654 virtual bool Destroy();
655 virtual void Display(bool block);
656 virtual void PutStr(int attr, const char *text);
657 virtual void UseRIP(int how, time_t when);
659 public slots:
660 void Search();
662 protected:
663 virtual void done(int);
664 virtual void keyPressEvent(QKeyEvent *);
666 private slots:
667 void doUpdate();
669 private:
670 aNetHackQtKeyBuffer &keysource;
672 bool use_rip;
673 bool str_fixed;
675 QPushButton ok;
676 QPushButton search;
677 aNetHackQtTextListBox *lines;
679 aNetHackQtRIP rip;
682 class aNetHackQtMenuOrTextWindow : public aNetHackQtWindow
684 private:
685 aNetHackQtWindow *actual;
686 aNetHackQtKeyBuffer &keysource;
688 public:
689 aNetHackQtMenuOrTextWindow(aNetHackQtKeyBuffer &);
691 virtual QWidget *Widget();
693 // Text
694 virtual void Clear();
695 virtual bool Destroy();
696 virtual void Display(bool block);
697 virtual void PutStr(int attr, const char *text);
699 // Menu
700 virtual void StartMenu();
701 virtual void AddMenu(int glyph, const ANY_P *identifier, char ch,
702 char gch, int attr, const char *str, bool presel);
703 virtual void EndMenu(const char *prompt);
704 virtual int SelectMenu(int how, MENU_ITEM_P **menu_list);
707 class aNetHackQtDelay : QObject
709 private:
710 int msec;
712 public:
713 aNetHackQtDelay(int ms);
714 void wait();
715 virtual void timerEvent(QTimerEvent *timer);
718 class aNetHackQtInvUsageWindow : public QWidget
720 public:
721 aNetHackQtInvUsageWindow(QWidget *parent);
722 virtual void paintEvent(QPaintEvent *);
724 private:
725 void drawWorn(QPainter &painter, obj *, int x, int y, bool canbe = TRUE);
728 // This class is the main widget for aNetHack
730 // It is a collection of Message, Map, and Status windows. In the current
731 // version of anethack there is only one of each, and this class makes this
732 // assumption, not showing itself until all are inserted.
734 // This class simply knows how to layout such children sensibly.
736 // Since it is only responsible for layout, the class does not
737 // note the actual class of the windows.
739 #ifndef KDE
740 #include "qt_kde0.h"
741 #endif
743 class aNetHackQtMainWindow : public KTopLevelWidget
745 Q_OBJECT
746 public:
747 aNetHackQtMainWindow(aNetHackQtKeyBuffer &);
749 void AddMessageWindow(aNetHackQtMessageWindow *window);
750 void AddMapWindow(aNetHackQtMapWindow *window);
751 void AddStatusWindow(aNetHackQtStatusWindow *window);
752 void RemoveWindow(aNetHackQtWindow *window);
753 void updateInventory();
755 void fadeHighlighting();
757 public slots:
758 void doMenuItem(int);
759 void doKeys(const QString &);
761 protected:
762 virtual void resizeEvent(QResizeEvent *);
763 virtual void keyPressEvent(QKeyEvent *);
764 virtual void keyReleaseEvent(QKeyEvent *event);
765 virtual void closeEvent(QCloseEvent *);
767 private slots:
768 void layout();
769 void raiseMap();
770 void zoomMap();
771 void raiseMessages();
772 void raiseStatus();
774 private:
775 void ShowIfReady();
777 #ifdef KDE
778 KMenuBar *menubar;
779 #else
780 QMenuBar *menubar;
781 #endif
782 aNetHackQtMessageWindow *message;
783 aNetHackQtMapWindow *map;
784 aNetHackQtStatusWindow *status;
785 aNetHackQtInvUsageWindow *invusage;
787 aNetHackQtKeyBuffer &keysink;
788 QWidgetStack *stack;
789 int dirkey;
791 const char **macro;
794 class aNetHackQtYnDialog : QDialog
796 Q_OBJECT
797 private:
798 const char *question;
799 const char *choices;
800 char def;
801 aNetHackQtKeyBuffer &keysource;
803 protected:
804 virtual void keyPressEvent(QKeyEvent *);
805 virtual void done(int);
807 private slots:
808 void doneItem(int);
810 public:
811 aNetHackQtYnDialog(aNetHackQtKeyBuffer &keysource, const char *,
812 const char *, char);
814 char Exec();
817 #ifdef KDE
818 #define aNetHackQtBindBase KApplication
819 #elif defined(QWS)
820 #define aNetHackQtBindBase QPEApplication
821 #else
822 #define aNetHackQtBindBase QApplication
823 #endif
825 class aNetHackQtBind : aNetHackQtBindBase
827 private:
828 // Single-instance preservation...
829 aNetHackQtBind(int &argc, char **argv);
831 static aNetHackQtBind *instance;
833 static aNetHackQtKeyBuffer keybuffer;
834 static aNetHackQtClickBuffer clickbuffer;
836 static QWidget *splash;
837 static aNetHackQtMainWindow *main;
839 public:
840 static void qt_init_nhwindows(int *argc, char **argv);
841 static void qt_player_selection();
842 static void qt_askname();
843 static void qt_get_nh_event();
844 static void qt_exit_nhwindows(const char *);
845 static void qt_suspend_nhwindows(const char *);
846 static void qt_resume_nhwindows();
847 static winid qt_create_nhwindow(int type);
848 static void qt_clear_nhwindow(winid wid);
849 static void qt_display_nhwindow(winid wid, BOOLEAN_P block);
850 static void qt_destroy_nhwindow(winid wid);
851 static void qt_curs(winid wid, int x, int y);
852 static void qt_putstr(winid wid, int attr, const char *text);
853 static void qt_display_file(const char *filename, BOOLEAN_P must_exist);
854 static void qt_start_menu(winid wid);
855 static void qt_add_menu(winid wid, int glyph, const ANY_P *identifier,
856 CHAR_P ch, CHAR_P gch, int attr, const char *str,
857 BOOLEAN_P presel);
858 static void qt_end_menu(winid wid, const char *prompt);
859 static int qt_select_menu(winid wid, int how, MENU_ITEM_P **menu_list);
860 static void qt_update_inventory();
861 static void qt_mark_synch();
862 static void qt_wait_synch();
864 static void qt_cliparound(int x, int y);
865 static void qt_cliparound_window(winid wid, int x, int y);
866 static void qt_print_glyph(winid wid, XCHAR_P x, XCHAR_P y,
867 int glyph, int bkglyph);
868 static void qt_raw_print(const char *str);
869 static void qt_raw_print_bold(const char *str);
870 static int qt_nhgetch();
871 static int qt_nh_poskey(int *x, int *y, int *mod);
872 static void qt_nhbell();
873 static int qt_doprev_message();
874 static char qt_yn_function(const char *question, const char *choices,
875 CHAR_P def);
876 static void qt_getlin(const char *prompt, char *line);
877 static int qt_get_ext_cmd();
878 static void qt_number_pad(int);
879 static void qt_delay_output();
880 static void qt_start_screen();
881 static void qt_end_screen();
883 static void qt_outrip(winid wid, int how, time_t when);
884 static int qt_kbhit();
886 private:
887 virtual bool notify(QObject *receiver, QEvent *event);
890 #endif