chiark / gitweb /
debian/changelog: start -4~
[vtwm.git] / applets.c
1 /*
2  * Copyright 1989 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and its
5  * documentation for any purpose and without fee is hereby granted, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in advertising
9  * or publicity pertaining to distribution of the software without specific,
10  * written prior permission.  M.I.T. makes no representations about the
11  * suitability of this software for any purpose.  It is provided "as is"
12  * without express or implied warranty.
13  *
14  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
16  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
18  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
19  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21
22 /**********************************************************************
23  *
24  * applets.c
25  *
26  * Applet region related routines
27  *
28  * 4/26/99 D. J. Hawkey Jr.
29  *
30  **********************************************************************/
31
32 #include <stdio.h>
33 #include <string.h>
34 #include "twm.h"
35 #include "screen.h"
36 #include "regions.h"
37 #include "gram.h"
38 #include "parse.h"
39 #include "util.h"
40
41 extern void twmrc_error_prefix();
42 extern int MatchName();
43
44 extern void splitRegionEntry();
45 extern int roundEntryUp();
46 extern RegionEntry *prevRegionEntry();
47 extern void mergeRegionEntries();
48 extern void downRegionEntry();
49 extern RootRegion *AddRegion();
50
51 int appletWidth(tmp_win)
52 TwmWindow *tmp_win;
53 {
54         /* submitted by Tim Wiess - 8/23/02 */
55         if (Scr->NoBorders || LookInList(Scr->NoBorder, tmp_win->full_name, &tmp_win->class))
56                 return tmp_win->attr.width;
57         else
58                 return Scr->BorderWidth * 2 + tmp_win->attr.width;
59 }
60
61 int appletHeight(tmp_win)
62 TwmWindow *tmp_win;
63 {
64         /* submitted by Tim Wiess - 8/23/02 */
65         /* added 'tmp_win->title_height +' - djhjr - 11/11/03 */
66         if (Scr->NoBorders || LookInList(Scr->NoBorder, tmp_win->full_name, &tmp_win->class))
67                 return tmp_win->title_height + tmp_win->attr.height;
68         else
69                 return Scr->BorderWidth * 2 + tmp_win->title_height + tmp_win->attr.height;
70 }
71
72 int PlaceApplet(tmp_win, def_x, def_y, final_x, final_y)
73 TwmWindow *tmp_win;
74 int def_x, def_y;
75 int *final_x, *final_y;
76 {
77     RootRegion  *rr;
78     RegionEntry *re;
79     int                 matched, w, h;
80
81         for (rr = Scr->FirstAppletRegion; rr; rr = rr->next)
82         {
83                 matched = 0;
84                 w = roundEntryUp (appletWidth (tmp_win), rr->stepx);
85                 h = roundEntryUp (appletHeight (tmp_win), rr->stepy);
86
87                 for (re = rr->entries; re; re = re->next)
88                 {
89                         if (!matched)
90                         {
91                                 /* these were 'match()' - djhjr - 10/20/01 */
92                                 if (MatchName(tmp_win->full_name, re->u.name, &re->re, re->type))
93                                         if (MatchName(tmp_win->class.res_name, re->u.name, &re->re, re->type))
94                                                 if (MatchName(tmp_win->class.res_class, re->u.name, &re->re, re->type))
95                                                         continue;
96
97                                 matched = 1;
98                         }
99
100                         if (re->usedby) continue;
101 /* don't include grid spacing - djhjr - 5/22/99
102                         if (re->w < w || re->h < h) continue;
103 */
104                         if (re->w < appletWidth(tmp_win) || re->h < appletHeight(tmp_win))
105                                 continue;
106
107                         splitRegionEntry (re, rr->grav1, rr->grav2, w, h);
108                         re->usedby = USEDBY_TWIN;
109                         re->u.twm_win = tmp_win;
110
111 /* evenly spaced applet placement - djhjr - 4/24/99
112                         *final_x = re->x + (re->w - appletWidth (tmp_win)) / 2;
113                         *final_y = re->y + (re->h - appletHeight (tmp_win)) / 2;
114 */
115                         *final_x = re->x;
116                         *final_y = re->y;
117
118                         /* adjust for region gravity - djhjr 4/26/99 */
119                         if (rr->grav2 == D_EAST)
120                                 *final_x += re->w - appletWidth(tmp_win);
121                         if (rr->grav1 == D_SOUTH)
122                                 *final_y += re->h - appletHeight(tmp_win);
123
124                         return 1;
125                 }
126         }
127
128         *final_x = def_x;
129         *final_y = def_y;
130
131         return 0;
132 }
133
134 static RegionEntry *
135 FindAppletEntry (tmp_win, rrp)
136     TwmWindow   *tmp_win;
137     RootRegion  **rrp;
138 {
139     RootRegion  *rr;
140     RegionEntry *re;
141
142     for (rr = Scr->FirstAppletRegion; rr; rr = rr->next) {
143         for (re = rr->entries; re; re=re->next)
144             if (re->u.twm_win == tmp_win) {
145                 if (rrp)
146                     *rrp = rr;
147                 return re;
148             }
149     }
150     return 0;
151 }
152
153 void
154 AppletDown (tmp_win)
155     TwmWindow   *tmp_win;
156 {
157     RegionEntry *re;
158     RootRegion  *rr;
159
160     re = FindAppletEntry (tmp_win, &rr);
161     if (re)
162         downRegionEntry(rr, re);
163 }
164
165 RootRegion *
166 AddAppletRegion(geom, grav1, grav2, stepx, stepy)
167 char *geom;
168 int grav1, grav2, stepx, stepy;
169 {
170     RootRegion *rr;
171
172     rr = AddRegion(geom, grav1, grav2, stepx, stepy);
173
174     if (Scr->LastAppletRegion)
175         Scr->LastAppletRegion->next = rr;
176     Scr->LastAppletRegion = rr;
177     if (!Scr->FirstAppletRegion)
178         Scr->FirstAppletRegion = rr;
179
180         return rr;
181 }
182
183 void
184 AddToAppletList(list_head, name, type)
185 RootRegion *list_head;
186 char *name;
187 short type;
188 {
189     RegionEntry *nptr;
190
191     if (!list_head) return;     /* ignore empty inserts */
192
193     nptr = (RegionEntry *)malloc(sizeof(RegionEntry));
194     if (nptr == NULL)
195     {
196         twmrc_error_prefix();
197         fprintf (stderr, "unable to allocate %d bytes for RegionEntry\n",
198                  sizeof(RegionEntry));
199         Done();
200     }
201
202     nptr->next = list_head->entries;
203     /* djhjr - 10/20/01 */
204     nptr->type = type;
205     nptr->usedby = USEDBY_NAME;
206     nptr->u.name = (char*)strdup(name);
207     list_head->entries = nptr;
208 }    
209