dialog.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Dialog box features module for the Midnight Commander
  3. */
  4. /** \file dialog.h
  5. * \brief Header: dialog box features module
  6. */
  7. #ifndef MC__DIALOG_H
  8. #define MC__DIALOG_H
  9. #include <sys/types.h> /* size_t */
  10. #include "lib/global.h"
  11. #include "lib/hook.h" /* hook_t */
  12. #include "lib/keybind.h" /* global_keymap_t */
  13. #include "lib/tty/mouse.h" /* mouse_h */
  14. /*** defined constants ***************************************************************************/
  15. /* Common return values */
  16. #define B_EXIT 0
  17. #define B_CANCEL 1
  18. #define B_ENTER 2
  19. #define B_HELP 3
  20. #define B_USER 100
  21. #define dlg_move(h, _y, _x) tty_gotoyx (((Dlg_head *)(h))->y + (_y), ((Dlg_head *)(h))->x + (_x))
  22. /*** enums ***************************************************************************************/
  23. /* Dialog messages */
  24. typedef enum
  25. {
  26. DLG_INIT = 0, /* Initialize dialog */
  27. DLG_IDLE = 1, /* The idle state is active */
  28. DLG_DRAW = 2, /* Draw dialog on screen */
  29. DLG_FOCUS = 3, /* A widget has got focus */
  30. DLG_UNFOCUS = 4, /* A widget has been unfocused */
  31. DLG_RESIZE = 5, /* Window size has changed */
  32. DLG_KEY = 6, /* Key before sending to widget */
  33. DLG_HOTKEY_HANDLED = 7, /* A widget has got the hotkey */
  34. DLG_POST_KEY = 8, /* The key has been handled */
  35. DLG_UNHANDLED_KEY = 9, /* Key that no widget handled */
  36. DLG_ACTION = 10, /* State of check- and radioboxes has changed
  37. * and listbox current entry has changed */
  38. DLG_VALIDATE = 11, /* Dialog is to be closed */
  39. DLG_END = 12 /* Shut down dialog */
  40. } dlg_msg_t;
  41. /* Flags for create_dlg */
  42. typedef enum
  43. {
  44. DLG_REVERSE = (1 << 5), /* Tab order is opposite to the add order */
  45. DLG_WANT_TAB = (1 << 4), /* Should the tab key be sent to the dialog? */
  46. DLG_WANT_IDLE = (1 << 3), /* Dialog wants idle events */
  47. DLG_COMPACT = (1 << 2), /* Suppress spaces around the frame */
  48. DLG_TRYUP = (1 << 1), /* Try to move two lines up the dialog */
  49. DLG_CENTER = (1 << 0), /* Center the dialog */
  50. DLG_NONE = 0 /* No options */
  51. } dlg_flags_t;
  52. /* Dialog state */
  53. typedef enum
  54. {
  55. DLG_CONSTRUCT = 0, /* Dialog has been constructed but not run yet */
  56. DLG_ACTIVE = 1, /* Dialog is visible and active */
  57. DLG_SUSPENDED = 2, /* Dialog is suspended */
  58. DLG_CLOSED = 3 /* Dialog is closed */
  59. } dlg_state_t;
  60. /* Dialog color constants */
  61. typedef enum
  62. {
  63. DLG_COLOR_NORMAL,
  64. DLG_COLOR_FOCUS,
  65. DLG_COLOR_HOT_NORMAL,
  66. DLG_COLOR_HOT_FOCUS,
  67. DLG_COLOR_TITLE,
  68. DLG_COLOR_COUNT
  69. } dlg_colors_enum_t;
  70. /*** typedefs(not structures) ********************************************************************/
  71. /* get string representation of shortcut assigned with command */
  72. /* as menu is a widget of dialog, ask dialog about shortcut string */
  73. typedef char *(*dlg_shortcut_str) (unsigned long command);
  74. /* get dialog name to show in dialog list */
  75. typedef char *(*dlg_title_str) (const Dlg_head * h, size_t len);
  76. typedef int dlg_colors_t[DLG_COLOR_COUNT];
  77. /* Dialog callback */
  78. typedef cb_ret_t (*dlg_cb_fn) (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data);
  79. /* menu command execution */
  80. typedef cb_ret_t (*menu_exec_fn) (int command);
  81. /*** structures declarations (and typedefs of structures)*****************************************/
  82. struct Dlg_head
  83. {
  84. /* Set by the user */
  85. gboolean modal; /* type of dialog: modal or not */
  86. dlg_flags_t flags; /* User flags */
  87. const char *help_ctx; /* Name of the help entry */
  88. dlg_colors_t color; /* Color set. Unused in viewer and editor */
  89. char *title; /* Title of the dialog */
  90. /* Set and received by the user */
  91. int ret_value; /* Result of run_dlg() */
  92. /* Geometry */
  93. int x, y; /* Position relative to screen origin */
  94. int cols, lines; /* Width and height of the window */
  95. /* Internal flags */
  96. dlg_state_t state;
  97. gboolean fullscreen; /* Parents dialogs don't need refresh */
  98. gboolean winch_pending; /* SIGWINCH signal has been got. Resize dialog after rise */
  99. int mouse_status; /* For the autorepeat status of the mouse */
  100. /* Internal variables */
  101. GList *widgets; /* widgets list */
  102. GList *current; /* Curently active widget */
  103. unsigned long widget_id; /* maximum id of all widgets */
  104. void *data; /* Data can be passed to dialog */
  105. char *event_group; /* Name of event group for this dialog */
  106. dlg_cb_fn callback;
  107. mouse_h mouse;
  108. dlg_shortcut_str get_shortcut; /* Shortcut string */
  109. dlg_title_str get_title; /* useless for modal dialogs */
  110. };
  111. /*** global variables defined in .c file *********************************************************/
  112. /* Color styles for normal and error dialogs */
  113. extern dlg_colors_t dialog_colors;
  114. extern dlg_colors_t alarm_colors;
  115. extern GList *top_dlg;
  116. /* A hook list for idle events */
  117. extern hook_t *idle_hook;
  118. extern int fast_refresh;
  119. extern int mouse_close_dialog;
  120. extern const global_keymap_t *dialog_map;
  121. /*** declarations of public functions ************************************************************/
  122. /* draw box in window */
  123. void draw_box (Dlg_head * h, int y, int x, int ys, int xs, gboolean single);
  124. /* Creates a dialog head */
  125. Dlg_head *create_dlg (gboolean modal, int y1, int x1, int lines, int cols,
  126. const int *colors, dlg_cb_fn callback, mouse_h mouse_handler,
  127. const char *help_ctx, const char *title, dlg_flags_t flags);
  128. void dlg_set_default_colors (void);
  129. unsigned long add_widget_autopos (Dlg_head * dest, void *w, widget_pos_flags_t pos_flags,
  130. const void *before);
  131. unsigned long add_widget (Dlg_head * dest, void *w);
  132. unsigned long add_widget_before (Dlg_head * h, void *w, void *before);
  133. void del_widget (void *w);
  134. /* sets size of dialog, leaving positioning to automatic mehtods
  135. according to dialog flags */
  136. void dlg_set_size (Dlg_head * h, int lines, int cols);
  137. /* this function allows to set dialog position */
  138. void dlg_set_position (Dlg_head * h, int y1, int x1, int y2, int x2);
  139. void init_dlg (Dlg_head * h);
  140. int run_dlg (Dlg_head * d);
  141. void destroy_dlg (Dlg_head * h);
  142. void dlg_run_done (Dlg_head * h);
  143. void dlg_save_history (Dlg_head * h);
  144. void dlg_process_event (Dlg_head * h, int key, Gpm_Event * event);
  145. char *dlg_get_title (const Dlg_head * h, size_t len);
  146. /* To activate/deactivate the idle message generation */
  147. void set_idle_proc (Dlg_head * d, int enable);
  148. void dlg_redraw (Dlg_head * h);
  149. void dlg_broadcast_msg (Dlg_head * h, widget_msg_t message, gboolean reverse);
  150. /* Default callback for dialogs */
  151. cb_ret_t default_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data);
  152. /* Default paint routine for dialogs */
  153. void common_dialog_repaint (Dlg_head * h);
  154. void dlg_replace_widget (Widget * old, Widget * new);
  155. int dlg_overlap (Widget * a, Widget * b);
  156. void dlg_erase (Dlg_head * h);
  157. void dlg_stop (Dlg_head * h);
  158. /* Widget selection */
  159. void dlg_select_widget (void *w);
  160. void dlg_set_top_widget (void *w);
  161. void dlg_one_up (Dlg_head * h);
  162. void dlg_one_down (Dlg_head * h);
  163. gboolean dlg_focus (Dlg_head * h);
  164. Widget *find_widget_type (const Dlg_head * h, callback_fn callback);
  165. Widget *dlg_find_by_id (const Dlg_head * h, unsigned long id);
  166. void dlg_select_by_id (const Dlg_head * h, unsigned long id);
  167. /* Redraw all dialogs */
  168. void do_refresh (void);
  169. /* Used in load_prompt() */
  170. void update_cursor (Dlg_head * h);
  171. /*** inline functions ****************************************************************************/
  172. /* Return TRUE if the widget is active, FALSE otherwise */
  173. static inline gboolean
  174. dlg_widget_active (void *w)
  175. {
  176. Widget *w1 = (Widget *) w;
  177. return ((Widget *) w1->owner->current->data == w1);
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. static inline unsigned long
  181. dlg_get_current_widget_id (const struct Dlg_head *h)
  182. {
  183. return ((Widget *) h->current->data)->id;
  184. }
  185. #endif /* MC__DIALOG_H */