summaryrefslogtreecommitdiff
path: root/include/xt-common.h
blob: b2536309d21e66f17bec2f862935177ab0b16fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* xt-common.h: declarations that all programs using Xt need.

Copyright (C) 1992, 2011 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifndef XT_COMMON_H
#define XT_COMMON_H

/* See the kpathsea/INSTALL file for the purpose of the FOIL...  */
#ifndef FOIL_X_WCHAR_T
#define wchar_t foil_x_defining_wchar_t
#define X_WCHAR
#endif
#undef input /* the XWMHints structure has a field named `input' */
#undef output
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>


/* Our own definitions.  */

/* It is convenient to have this type for declaring action procedures.  */
typedef void action_proc_type (Widget, XEvent *, String *, Cardinal *);

/* We virtually always want an arg array and its length together in
   parameter lists.  */
#define XTARG(arg_array) (arg_array), XtNumber (arg_array)

/* Assign a value to an Xt argument.  */
#define XTASSIGN_ARG(arg, val) (arg).value = (XtArgVal) (val)

/* We use command buttons the same way in all programs, so here are
   macros to make it easy to use them.  */

/* Our buttons only have one procedure associated with them.  This macro
   expands into an XtCallbackRec array.  */
#define SINGLE_CALLBACK(proc, data)					\
  { { (XtCallbackProc) proc, (XtPointer) (data) },			\
    { NULL, NULL }							\
  }

/* This macro declares a command widget.  The routine that performs the
   action should be named `NAME_command'; it will be passed the
   CLIENT_DATA argument.  The string TITLE appears in the window.  */
#define DECLARE_BUTTON(name, title, client_data)			\
  XtCallbackRec name##_callback_list[]					\
    = SINGLE_CALLBACK (name##_command, client_data);			\
  Arg name##_args[]							\
    = { { XtNfromHoriz,	(XtArgVal) NULL }, /* We assign to this below.  */\
        { XtNlabel,	(XtArgVal) title },				\
        { XtNcallback,	(XtArgVal) name##_callback_list },		\
      };								\
  Widget name##_widget /* The invoker supplies the semicolon.  */
  

/* This macro defines a command button NAME, by using the variables
   that DECLARE_BUTTON creates.  It also keeps track of the widget that
   should be to the left of the new one, by assuming a variable
   `widget_to_the_left'.  */

#define DEFINE_BUTTON(name, parent)					\
  name##_args[0].value = (XtArgVal) widget_to_the_left;			\
  name##_widget = XtCreateManagedWidget (#name, commandWidgetClass,	\
					 parent, XTARG (name##_args));	\
  widget_to_the_left = name##_widget
  /* The invoker supplies the semicolon.  */


/* Parse a character code in STR; if invalid, give a warning.  If valid,
   assign the value to CODE.  Return whether it was valid.  */
#define XTPARSE_CHARCODE(code, str, widget)				\
  ({									\
    boolean valid;							\
    charcode_type test = parse_charcode (str, &valid);			\
									\
    if (valid)								\
      code = test;							\
    else								\
      {									\
        string s = concat3 ("`", str, "': invalid character code");	\
        x_warning (XtParent (widget), s);				\
        free (s);							\
      };								\
    valid;								\
  })


/* Find the widget whose name is NAME in the widget tree rooted at
   TOP.  If no such widget can be found, give a fatal error.
   
   We use the extra variable `root' in case `top' is an expression
   involving some other widget named `w'.  */
#define XFIND_WIDGET(top, name)						\
    ({									\
      Widget root = top;						\
      Widget w = XtNameToWidget (root, name);				\
      if (w == NULL)							\
        {								\
          string s = concat3 ("Cannot find widget `", name, "'."); 	\
          XtErrorMsg ("noWidget", "FindWidget", "Error", s, NULL, 0);	\
        }								\
      w;								\
    })


/* For consistency, so we can give `Pointer' as a `type' below.  */
typedef XtPointer Pointer;

/* We define our widget resources in a predictable way.  We assume an
   `OFFSET' macro has been defined as, e.g.,
   #define OFFSET(field) XtOffset (BitmapWidget, bitmap.field)
*/
#define DEFINE_RESOURCE(name, field_name, class_name, type,		\
                        default_type, default)				\
  { XtN##name, XtC##class_name, XtR##type, sizeof (type),		\
    OFFSET (field_name), XtR##default_type, (XtPointer) (default) }

#define IMMEDIATE_RESOURCE2(name, field_name, class_name, type, default)\
  DEFINE_RESOURCE (name, field_name, class_name, type, Immediate, default)

#define IMMEDIATE_RESOURCE(name, class_name, type, default)		\
  IMMEDIATE_RESOURCE2 (name, name, class_name, type, default)

#define STRING_RESOURCE(name, class_name, default)			\
  DEFINE_RESOURCE (name, name, class_name, String, String, default)

#endif /* not XT_COMMON_H */