1 // NetHack 3.6 qt_win.h $NHDT-Date: 1447755972 2015/11/17 10:26:12 $ $NHDT-Branch: master $:$NHDT-Revision: 1.17 $
2 // Copyright (c) Warwick Allison, 1999.
3 // NetHack may be freely redistributed. See license for details.
5 // Qt Binding for NetHack 3.4
7 // Unfortunately, this doesn't use Qt as well as I would like,
8 // primarily because NetHack 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 NetHackQtGlyphs
;
70 class NetHackQtLineEdit
: public QLineEdit
74 NetHackQtLineEdit(QWidget
*parent
, const char *name
);
76 void fakeEvent(int key
, int ascii
, int state
);
79 class NetHackQtSettings
: public QDialog
83 // Size of window - used to decide default sizes
84 NetHackQtSettings(int width
, int height
);
86 NetHackQtGlyphs
&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 NetHackQtGlyphs
*theglyphs
;
119 class NetHackQtKeyBuffer
122 NetHackQtKeyBuffer();
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 NetHackQtClickBuffer
149 NetHackQtClickBuffer();
154 void Put(int x
, int y
, int mod
);
163 enum { maxclick
= 64 };
170 class NetHackQtSavedGameSelector
: public QDialog
173 NetHackQtSavedGameSelector(const char **saved
);
178 class NetHackQtPlayerSelector
: private QDialog
182 enum { R_None
= -1, R_Quit
= -2, R_Rand
= -3 };
184 NetHackQtPlayerSelector(NetHackQtKeyBuffer
&);
187 virtual void done(int);
193 void selectName(const QString
&n
);
197 void selectGender(int);
198 void selectAlignment(int);
204 NetHackQtKeyBuffer
&keysource
;
207 QRadioButton
**gender
;
208 QRadioButton
**alignment
;
209 bool fully_specified_role
;
212 class NetHackQtStringRequestor
: QDialog
216 NetHackQtLineEdit input
;
219 NetHackQtKeyBuffer
&keysource
;
221 virtual void done(int);
224 NetHackQtStringRequestor(NetHackQtKeyBuffer
&, 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 NetHackQtExtCmdRequestor
: public QDialog
235 NetHackQtKeyBuffer
&keysource
;
238 NetHackQtExtCmdRequestor(NetHackQtKeyBuffer
&ks
);
246 class NetHackQtWindow
250 virtual ~NetHackQtWindow();
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 NetHackQtGlyphs
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 NetHackQtMapWindow
: public QWidget
, public NetHackQtWindow
312 NetHackQtClickBuffer
&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 NetHackQtMapWindow(NetHackQtClickBuffer
&click_sink
);
345 ~NetHackQtMapWindow();
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 NetHackQtScrollText
;
368 class NetHackQtMessageWindow
: QObject
, public NetHackQtWindow
372 NetHackQtMessageWindow();
373 ~NetHackQtMessageWindow();
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(NetHackQtMapWindow
*);
385 NetHackQtScrollText
*list
;
387 NetHackQtMapWindow
*map
;
393 class NetHackQtLabelledIcon
: public QWidget
396 NetHackQtLabelledIcon(QWidget
*parent
, const char *label
);
397 NetHackQtLabelledIcon(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 NetHackQtStatusWindow
: QWidget
, public NetHackQtWindow
437 NetHackQtStatusWindow();
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 NetHackQtLabelledIcon name
;
481 NetHackQtLabelledIcon dlevel
;
483 NetHackQtLabelledIcon str
;
484 NetHackQtLabelledIcon dex
;
485 NetHackQtLabelledIcon con
;
486 NetHackQtLabelledIcon intel
;
487 NetHackQtLabelledIcon wis
;
488 NetHackQtLabelledIcon cha
;
490 NetHackQtLabelledIcon gold
;
491 NetHackQtLabelledIcon hp
;
492 NetHackQtLabelledIcon power
;
493 NetHackQtLabelledIcon ac
;
494 NetHackQtLabelledIcon level
;
495 NetHackQtLabelledIcon exp
;
496 NetHackQtLabelledIcon align
;
498 NetHackQtLabelledIcon time
;
499 NetHackQtLabelledIcon score
;
501 NetHackQtLabelledIcon hunger
;
502 NetHackQtLabelledIcon confused
;
503 NetHackQtLabelledIcon sick_fp
;
504 NetHackQtLabelledIcon sick_il
;
505 NetHackQtLabelledIcon blind
;
506 NetHackQtLabelledIcon stunned
;
507 NetHackQtLabelledIcon hallu
;
508 NetHackQtLabelledIcon encumber
;
520 void checkTurnEvents();
523 class NetHackQtMenuDialog
: public QDialog
527 NetHackQtMenuDialog();
533 virtual void done(int);
536 void resizeEvent(QResizeEvent
*);
542 class NetHackQtMenuWindow
: public QTableView
, public NetHackQtWindow
546 NetHackQtMenuWindow(NetHackQtKeyBuffer
&);
547 ~NetHackQtMenuWindow();
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 NetHackQtKeyBuffer
&keysource
;
607 NetHackQtMenuDialog
*dialog
;
625 class NetHackQtTextListBox
;
627 class NetHackQtRIP
: public QWidget
630 static QPixmap
*pixmap
;
635 NetHackQtRIP(QWidget
*parent
);
637 void setLines(char **l
, int n
);
640 virtual void paintEvent(QPaintEvent
*event
);
641 QSize
sizeHint() const;
644 class NetHackQtTextWindow
: public QDialog
, public NetHackQtWindow
648 NetHackQtTextWindow(NetHackQtKeyBuffer
&);
649 ~NetHackQtTextWindow();
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 NetHackQtKeyBuffer
&keysource
;
677 NetHackQtTextListBox
*lines
;
682 class NetHackQtMenuOrTextWindow
: public NetHackQtWindow
685 NetHackQtWindow
*actual
;
686 NetHackQtKeyBuffer
&keysource
;
689 NetHackQtMenuOrTextWindow(NetHackQtKeyBuffer
&);
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 NetHackQtDelay
: QObject
713 NetHackQtDelay(int ms
);
715 virtual void timerEvent(QTimerEvent
*timer
);
718 class NetHackQtInvUsageWindow
: public QWidget
721 NetHackQtInvUsageWindow(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 NetHack
730 // It is a collection of Message, Map, and Status windows. In the current
731 // version of nethack 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 NetHackQtMainWindow
: public KTopLevelWidget
747 NetHackQtMainWindow(NetHackQtKeyBuffer
&);
749 void AddMessageWindow(NetHackQtMessageWindow
*window
);
750 void AddMapWindow(NetHackQtMapWindow
*window
);
751 void AddStatusWindow(NetHackQtStatusWindow
*window
);
752 void RemoveWindow(NetHackQtWindow
*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 NetHackQtMessageWindow
*message
;
783 NetHackQtMapWindow
*map
;
784 NetHackQtStatusWindow
*status
;
785 NetHackQtInvUsageWindow
*invusage
;
787 NetHackQtKeyBuffer
&keysink
;
794 class NetHackQtYnDialog
: QDialog
798 const char *question
;
801 NetHackQtKeyBuffer
&keysource
;
804 virtual void keyPressEvent(QKeyEvent
*);
805 virtual void done(int);
811 NetHackQtYnDialog(NetHackQtKeyBuffer
&keysource
, const char *,
818 #define NetHackQtBindBase KApplication
820 #define NetHackQtBindBase QPEApplication
822 #define NetHackQtBindBase QApplication
825 class NetHackQtBind
: NetHackQtBindBase
828 // Single-instance preservation...
829 NetHackQtBind(int &argc
, char **argv
);
831 static NetHackQtBind
*instance
;
833 static NetHackQtKeyBuffer keybuffer
;
834 static NetHackQtClickBuffer clickbuffer
;
836 static QWidget
*splash
;
837 static NetHackQtMainWindow
*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
);