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.
5 // Qt Binding for aNetHack 3.4
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.
17 #define QT_CLEAN_NAMESPACE
20 #include <qpushbutton.h>
21 #include <qbuttongroup.h>
23 #include <qlineedit.h>
25 #include <qpe/qpeapplication.h>
27 #include <qapplication.h>
30 #include <qcheckbox.h>
33 #include <qlistview.h>
34 #include <qmessagebox.h>
38 #include <qcombobox.h>
39 #include <qscrollview.h>
41 #include <qttableview.h>
42 // Should stop using QTableView
43 #define QTableView QtTableView
45 #include <qtableview.h>
47 #include <qmainwindow.h>
48 #include <qwidgetstack.h>
52 #include <ktopwidget.h>
62 //////////////////////////////////////////////////////////////
64 // The beautiful, abstracted and well-modelled classes...
66 //////////////////////////////////////////////////////////////
68 class aNetHackQtGlyphs
;
70 class aNetHackQtLineEdit
: public QLineEdit
74 aNetHackQtLineEdit(QWidget
*parent
, const char *name
);
76 void fakeEvent(int key
, int ascii
, int state
);
79 class aNetHackQtSettings
: public QDialog
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();
98 void toggleGlyphSize();
99 void setGlyphSize(bool);
111 QFont normal
, normalfixed
, large
;
113 aNetHackQtGlyphs
*theglyphs
;
119 class aNetHackQtKeyBuffer
122 aNetHackQtKeyBuffer();
127 void Put(int k
, int ascii
, int state
);
129 void Put(const char *str
);
135 int TopAscii() const;
136 int TopState() const;
139 enum { maxkey
= 64 };
146 class aNetHackQtClickBuffer
149 aNetHackQtClickBuffer();
154 void Put(int x
, int y
, int mod
);
163 enum { maxclick
= 64 };
170 class aNetHackQtSavedGameSelector
: public QDialog
173 aNetHackQtSavedGameSelector(const char **saved
);
178 class aNetHackQtPlayerSelector
: private QDialog
182 enum { R_None
= -1, R_Quit
= -2, R_Rand
= -3 };
184 aNetHackQtPlayerSelector(aNetHackQtKeyBuffer
&);
187 virtual void done(int);
193 void selectName(const QString
&n
);
197 void selectGender(int);
198 void selectAlignment(int);
204 aNetHackQtKeyBuffer
&keysource
;
207 QRadioButton
**gender
;
208 QRadioButton
**alignment
;
209 bool fully_specified_role
;
212 class aNetHackQtStringRequestor
: QDialog
216 aNetHackQtLineEdit input
;
219 aNetHackQtKeyBuffer
&keysource
;
221 virtual void done(int);
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
235 aNetHackQtKeyBuffer
&keysource
;
238 aNetHackQtExtCmdRequestor(aNetHackQtKeyBuffer
&ks
);
246 class 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
);
271 class aNetHackQtGlyphs
284 return size
.height();
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
);
294 QPixmap pm
, pm1
, pm2
;
299 class BlackScrollView
: public QScrollView
304 viewport()->setBackgroundColor(black
);
308 class aNetHackQtMapWindow
: public QWidget
, public aNetHackQtWindow
312 aNetHackQtClickBuffer
&clicksink
;
313 unsigned short glyph
[ROWNO
][COLNO
];
320 BlackScrollView viewport
;
321 QPixmap pet_annotation
;
327 void Changed(int x
, int y
);
334 void moveMessages(int x
, int y
);
340 virtual void paintEvent(QPaintEvent
*);
341 virtual void mousePressEvent(QMouseEvent
*);
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
);
360 void displayMessages(bool block
);
361 void putMessage(int attr
, const char *text
);
362 void clearMessages();
367 class aNetHackQtScrollText
;
368 class aNetHackQtMessageWindow
: QObject
, public aNetHackQtWindow
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
*);
385 aNetHackQtScrollText
*list
;
387 aNetHackQtMapWindow
*map
;
393 class aNetHackQtLabelledIcon
: public QWidget
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();
410 void dissipateHighlight();
415 void resizeEvent(QResizeEvent
*);
418 void initHighlight();
419 void setAlignments();
420 void highlight(const QPalette
&highlight
);
425 int turn_count
; /* last time the value changed */
433 class aNetHackQtStatusWindow
: QWidget
, public aNetHackQtWindow
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();
449 void resizeEvent(QResizeEvent
*);
455 enum { hilight_time
= 1 };
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
;
520 void checkTurnEvents();
523 class aNetHackQtMenuDialog
: public QDialog
527 aNetHackQtMenuDialog();
533 virtual void done(int);
536 void resizeEvent(QResizeEvent
*);
542 class aNetHackQtMenuWindow
: public QTableView
, public aNetHackQtWindow
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
);
564 void ToggleSelect(int);
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
);
594 return identifier
.a_void
!= 0;
598 QArray
<MenuItem
> item
;
605 aNetHackQtKeyBuffer
&keysource
;
607 aNetHackQtMenuDialog
*dialog
;
625 class aNetHackQtTextListBox
;
627 class aNetHackQtRIP
: public QWidget
630 static QPixmap
*pixmap
;
635 aNetHackQtRIP(QWidget
*parent
);
637 void setLines(char **l
, int n
);
640 virtual void paintEvent(QPaintEvent
*event
);
641 QSize
sizeHint() const;
644 class aNetHackQtTextWindow
: public QDialog
, public aNetHackQtWindow
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
);
663 virtual void done(int);
664 virtual void keyPressEvent(QKeyEvent
*);
670 aNetHackQtKeyBuffer
&keysource
;
677 aNetHackQtTextListBox
*lines
;
682 class aNetHackQtMenuOrTextWindow
: public aNetHackQtWindow
685 aNetHackQtWindow
*actual
;
686 aNetHackQtKeyBuffer
&keysource
;
689 aNetHackQtMenuOrTextWindow(aNetHackQtKeyBuffer
&);
691 virtual QWidget
*Widget();
694 virtual void Clear();
695 virtual bool Destroy();
696 virtual void Display(bool block
);
697 virtual void PutStr(int attr
, const char *text
);
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
713 aNetHackQtDelay(int ms
);
715 virtual void timerEvent(QTimerEvent
*timer
);
718 class aNetHackQtInvUsageWindow
: public QWidget
721 aNetHackQtInvUsageWindow(QWidget
*parent
);
722 virtual void paintEvent(QPaintEvent
*);
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.
743 class aNetHackQtMainWindow
: public KTopLevelWidget
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();
758 void doMenuItem(int);
759 void doKeys(const QString
&);
762 virtual void resizeEvent(QResizeEvent
*);
763 virtual void keyPressEvent(QKeyEvent
*);
764 virtual void keyReleaseEvent(QKeyEvent
*event
);
765 virtual void closeEvent(QCloseEvent
*);
771 void raiseMessages();
782 aNetHackQtMessageWindow
*message
;
783 aNetHackQtMapWindow
*map
;
784 aNetHackQtStatusWindow
*status
;
785 aNetHackQtInvUsageWindow
*invusage
;
787 aNetHackQtKeyBuffer
&keysink
;
794 class aNetHackQtYnDialog
: QDialog
798 const char *question
;
801 aNetHackQtKeyBuffer
&keysource
;
804 virtual void keyPressEvent(QKeyEvent
*);
805 virtual void done(int);
811 aNetHackQtYnDialog(aNetHackQtKeyBuffer
&keysource
, const char *,
818 #define aNetHackQtBindBase KApplication
820 #define aNetHackQtBindBase QPEApplication
822 #define aNetHackQtBindBase QApplication
825 class aNetHackQtBind
: aNetHackQtBindBase
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
;
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
,
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
,
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();
887 virtual bool notify(QObject
*receiver
, QEvent
*event
);