summaryrefslogtreecommitdiff
path: root/iup/src/mot/iupmot_tips.c
blob: 25bdbea9f59813e1b95bc95221d6f66c619c1fa7 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/** \file
 * \brief Motif Driver TIPS Control
 *
 * See Copyright Notice in "iup.h"
 */

#include <Xm/Xm.h>
#include <Xm/Label.h>

#include <stdio.h>

#include "iup.h"

#include "iup_attrib.h"
#include "iup_str.h"
#include "iup_drv.h"
#include "iup_drvfont.h"

#include "iupmot_drv.h"
#include "iupmot_color.h"


/* Default offset from cursor */
#define TIP_X_OFFSET    12
#define TIP_Y_OFFSET    12

typedef struct _Imottips
{
  Widget        Dialog;
  Widget        Label;
  XtIntervalId  ShowTimerId;       /* Timer: from enterwin to show tip    */
  XtIntervalId  HideTimerId;       /* Timer: from visible to hide tip    */
  unsigned long ShowInterval;      /* Time for tipPopupTimerId         */
  unsigned long HideInterval;      /* Time for tipActiveTimerId        */
  int           Visible;
  Ihandle*      ih;
} Imottips;

static Imottips mot_tips = {0, 0, 0, 0, 0, 0};

static void motTipsShow(void)
{
  Window root, child;
  int cx, cy, x, y;
  unsigned int keys;
  char* value;

  XQueryPointer(iupmot_display, RootWindow(iupmot_display, iupmot_screen),
                &root, &child, &x, &y, &cx, &cy, &keys);

  value = iupAttribGet(mot_tips.ih, "TIPRECT");
  if (value)
  {
    int x1, x2, y1, y2, wx = x, wy = y;
    sscanf(value, "%d %d %d %d", &x1, &y1, &x2, &y2);
    iupdrvScreenToClient(mot_tips.ih, &wx, &wy);
    if (wx < x1 || wx > x2 || wy < y1 || wy > y2)
    {
      iupmotTipLeaveNotify();
      return;
    }
  }
  
  XtVaSetValues(mot_tips.Dialog, 
    XmNx, (XtArgVal)(x+TIP_X_OFFSET),
    XmNy, (XtArgVal)(y+TIP_Y_OFFSET), 
    NULL);

  if (mot_tips.Visible)
    XRaiseWindow(iupmot_display, XtWindow(mot_tips.Dialog));
  else
    XtMapWidget(mot_tips.Dialog);

  mot_tips.ShowTimerId = (XtIntervalId) NULL;
  mot_tips.Visible = TRUE;
}

static void motTipsHide(void)
{
  mot_tips.HideTimerId = (XtIntervalId) NULL;
  iupmotTipLeaveNotify();
}

static int motTipsSet(Ihandle *ih)
{
  char* tipText = iupAttribGet(ih, "TIP");
  if (!tipText)
    return FALSE;

  if (!mot_tips.Dialog)
  {
    mot_tips.Dialog = XtVaAppCreateShell(" ", "tip",
      overrideShellWidgetClass, iupmot_display,
      XmNmappedWhenManaged, False,
      NULL);

    mot_tips.Label = XtVaCreateManagedWidget("label tip",
      xmLabelWidgetClass, mot_tips.Dialog,     /* must use IupGetAttribute to use inheritance */
      XmNforeground, iupmotColorGetPixelStr(IupGetAttribute(ih, "TIPFGCOLOR")),
      XmNbackground, iupmotColorGetPixelStr(IupGetAttribute(ih, "TIPBGCOLOR")),
      NULL);

    XtRealizeWidget(mot_tips.Dialog);
  }

  /* set label font */
  {
    XmFontList fontlist = (XmFontList)iupmotGetFontListAttrib(ih);
    char* value = iupAttribGetStr(ih, "TIPFONT");
    if (value)
    {
      if (iupStrEqualNoCase(value, "SYSTEM"))
        fontlist = NULL;
      else
        fontlist = iupmotGetFontList(iupAttribGet(ih, "TIPFOUNDRY"), value);
    }

    if (fontlist)
      XtVaSetValues(mot_tips.Label, XmNfontList, fontlist, NULL);
  }
 
  /* set label contents */
  iupmotSetString(mot_tips.Label, XmNlabelString, tipText);

  /* set label size */
  {
    int lw = 0, lh = 0;
    iupdrvFontGetMultiLineStringSize(ih, tipText, &lw, &lh);

    /* add room for margin */
    lw += 2*(2);
    lh += 2*(2);  

    XtVaSetValues(mot_tips.Label,
      XmNwidth, (XtArgVal)lw,
      XmNheight, (XtArgVal)lh,
      NULL);
    XtVaSetValues(mot_tips.Dialog,
      XmNwidth, (XtArgVal)lw,
      XmNheight, (XtArgVal)lh,
      NULL);
  }

  mot_tips.ShowTimerId  = (XtIntervalId) NULL;
  mot_tips.HideTimerId  = (XtIntervalId) NULL;
  mot_tips.Visible      = FALSE;
  mot_tips.ShowInterval = 500;  /* 1/2 second to show */

  {
    int delay = IupGetInt(ih, "TIPDELAY");  /* must use IupGetInt to use inheritance */
    mot_tips.HideInterval = delay + mot_tips.ShowInterval;
  }

  return TRUE;
}

int iupdrvBaseSetTipVisibleAttrib(Ihandle* ih, const char* value)
{
  /* must use IupGetAttribute to use inheritance */
  if (!IupGetAttribute(ih, "TIP"))
    return 0;

  if (iupStrBoolean(value))
    iupmotTipEnterNotify(ih);
  else
    iupmotTipLeaveNotify();

  return 0;
}

void iupmotTipEnterNotify(Ihandle *ih)
{
  iupmotTipLeaveNotify();

  if (motTipsSet(ih) == FALSE)
    return;

  mot_tips.ih = ih;

  mot_tips.ShowTimerId = XtAppAddTimeOut( /* EnterWin to Show */
    iupmot_appcontext,
    mot_tips.ShowInterval,
    (XtTimerCallbackProc)motTipsShow,
    NULL);
  mot_tips.HideTimerId = XtAppAddTimeOut( /* Visible to Hide */
    iupmot_appcontext,
    mot_tips.HideInterval,
    (XtTimerCallbackProc)motTipsHide,
    NULL);
}

void iupmotTipLeaveNotify(void)
{
  if (mot_tips.ShowTimerId)
  {
    XtRemoveTimeOut(mot_tips.ShowTimerId);
    mot_tips.ShowTimerId = (XtIntervalId) NULL;
  }
  if (mot_tips.HideTimerId)
  {
    XtRemoveTimeOut(mot_tips.HideTimerId);
    mot_tips.HideTimerId = (XtIntervalId) NULL;
  }
  if (mot_tips.Visible)
  {
    XtUnmapWidget(mot_tips.Dialog);
    mot_tips.Visible = FALSE;
  }
}

void iupmotTipsFinish(void)
{
  if (mot_tips.Dialog)
  {
    XtDestroyWidget(mot_tips.Dialog);
    memset(&mot_tips, 0, sizeof(Imottips));
  }
}

int iupdrvBaseSetTipAttrib(Ihandle* ih, const char* value)
{
  /* implement this stub here because of the other drivers. */
  (void)ih;
  (void)value;
  return 1;
}