Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSvc / DtUtil1 / WmMarquee.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /************************************<+>*************************************
24  ****************************************************************************
25  **
26  **   File:     WmMarquee.c
27  **
28  **   RCS:      $XConsortium: WmMarquee.c /main/4 1995/10/26 15:12:59 rswiston $
29  **
30  **   Project:  DT Workspace Manager
31  **
32  **   Description: Get Marquee Selection Data
33  **
34  ** (c) Copyright 1993, 1994 Hewlett-Packard Company
35  ** (c) Copyright 1993, 1994 International Business Machines Corp.
36  ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
37  ** (c) Copyright 1993, 1994 Novell, Inc.
38  **
39  ****************************************************************************
40  ************************************<+>*************************************/
41 #include <stdio.h>
42 #include <X11/Xlib.h>
43 #include <X11/Xutil.h>
44 #include <Tt/tttk.h>
45 #include <Dt/Wsm.h> 
46 #include <Dt/WsmM.h>
47 #include "WsmP.h" 
48 #include <Dt/Service.h>
49 #include <Xm/Xm.h>
50
51 /*************************************<->*************************************
52  *
53  *  Tt_callback_action _WsSelectionCB (Tt_message m, Tt_pattern p)
54  *
55  *
56  *  Description:
57  *  -----------
58  *  Internal function called for a marquee selection
59  *
60  *
61  *  Inputs:
62  *  ------
63  *  m           - ToolTalk message
64  *  p           - ToolTalk pattern
65  *
66  *  Outputs:
67  *  --------
68  *  Return      - ToolTalk callback status
69  *
70  *  Comments:
71  *  ---------
72  * 
73  *************************************<->***********************************/
74
75 static Tt_callback_action
76 _WsSelectionCB (Tt_message m, Tt_pattern p)
77 {
78     struct _DtWsmCBContext      *pCbCtx;
79     int                         type, val;
80     Position                    x, y;
81     Dimension                   width, height;
82
83     Widget                      widget;
84     DtWsmMarqueeSelectionProc   marquee_selection;
85     XtPointer                   client_data;
86
87     /*
88      * user data 0: Widget              widget;
89      * user data 1: DtWsmWsModifiedProc ws_modify;
90      * user data 2: XtPointer           client_data;
91      */
92     widget = (Widget)tt_pattern_user(p, 0);
93     marquee_selection = (DtWsmMarqueeSelectionProc)tt_pattern_user(p, 1);
94     client_data = (XtPointer)tt_pattern_user(p, 2);
95
96     /*
97      * 0th arg: screen number, string, not used
98      */
99
100     tt_message_arg_ival(m, 1, &type);   /* type */
101     tt_message_arg_ival(m, 2, &val);    /* x */
102     x = val;
103     tt_message_arg_ival(m, 3, &val);    /* y */
104     y = val;
105     tt_message_arg_ival(m, 4, &val);    /* width */
106     width = val;
107     tt_message_arg_ival(m, 5, &val);    /* height */
108     height = val;
109
110     /*
111      * Call registered callback function.
112      */
113     (*marquee_selection)(widget, type, x, y, width, height, client_data);
114     return TT_CALLBACK_PROCESSED;
115 } /* END OF FUNCTION _DtWsmWsModifyHandler */
116
117 /*************************************<->*************************************
118  *
119  *  DtWsmCBContext * DtWsmAddMarqueeSelectionCallback (widget, 
120  *                                                      marquee_select, 
121  *                                                      client_data)
122  *
123  *
124  *  Description:
125  *  -----------
126  *  Register a function to be called when a marquee selection is made
127  *
128  *
129  *  Inputs:
130  *  ------
131  *  widget              - widget for this client
132  *  marquee_select      - function to call for marquee select
133  *  client_data         - additional data to pass back to client when called.
134  *
135  *  Outputs:
136  *  --------
137  *  Return      - ptr to callback context data (opaque) 
138  *
139  *  Comments:
140  *  ---------
141  *  The callback context data ptr should be saved if you intend to
142  *  removed this callback at some point in the future. 
143  * 
144  *************************************<->***********************************/
145 DtWsmCBContext 
146 _DtWsmAddMarqueeSelectionCallback (
147         Widget                          widget,
148         DtWsmMarqueeSelectionProc       marquee_selection,
149         XtPointer                       client_data)
150 {
151     struct _DtWsmCBContext *pCbCtx;
152     int         screen;
153     String      sName;
154     char        sNum[32];
155
156     Tt_status   status;
157     Tt_pattern  pattern;
158     char *      sessId;
159
160     /*
161      * This function register a ToolTalk pattern for every
162      * callback added.
163      */
164     _DtSvcInitToolTalk(widget);
165
166     pattern = tt_pattern_create();
167     status = tt_ptr_error(pattern);
168     if (status != TT_OK) {
169         return NULL;
170     }
171
172     if (tt_pattern_category_set(pattern, TT_OBSERVE) != TT_OK) {
173         return NULL;
174     }
175     if (tt_pattern_scope_add(pattern, TT_SESSION) != TT_OK) {
176         return NULL;
177     }
178     sessId = tt_default_session();
179     if (tt_pattern_session_add(pattern, sessId) != TT_OK) {
180         return NULL;
181     }
182     tt_free(sessId);
183
184     screen = XScreenNumberOfScreen(XtScreen(widget));
185     sprintf(sNum, "%d", screen);
186     sName = _DtWsmSelectionNameForScreen (screen);
187
188     /*
189      * Only receive DtMarquee_Selection notice from the screen
190      * we registered with.
191      */
192     status = tt_pattern_arg_add(pattern, TT_IN, Tttk_string, sNum);
193     if (status != TT_OK) {
194         return NULL;
195     }
196
197     if (tt_pattern_op_add(pattern, "DtMarquee_Selection") != TT_OK) {
198         return NULL;
199     }
200
201     if (tt_pattern_state_add(pattern, TT_SENT) != TT_OK) {
202         return NULL;
203     }
204
205     /*
206      * Store information needed by the callback in the user data
207      * fields of the pattern.
208      */
209     status = tt_pattern_user_set(pattern, 0, (void *)widget);
210     if (status != TT_OK) {
211         return NULL;
212     }
213     status = tt_pattern_user_set(pattern, 1, (void *)marquee_selection);
214     if (status != TT_OK) {
215         return NULL;
216     }
217     status = tt_pattern_user_set(pattern, 2, (void *)client_data);
218     if (status != TT_OK) {
219         return NULL;
220     }
221
222     /*
223      * _WsSelectionCB is the ToolTalk callback which will call
224      * the user callback.
225      */
226     if (tt_pattern_callback_add(pattern, _WsSelectionCB) != TT_OK) {
227         return NULL;
228     }
229
230     if (tt_pattern_register(pattern) != TT_OK) {
231         return NULL;
232     }
233
234     /*
235      * Allocate data to remember stuff about this callback
236      */
237     pCbCtx = (struct _DtWsmCBContext * ) 
238                 XtMalloc (sizeof(struct _DtWsmCBContext));
239
240     /* 
241      * Save what we want to remember
242      */
243     pCbCtx->pattern = pattern;
244     pCbCtx->widget = widget;
245     pCbCtx->ws_cb = marquee_selection;
246     pCbCtx->client_data = client_data;
247
248     XtFree (sName);
249
250     return (pCbCtx);
251
252 } /* END OF FUNCTION DtWsmAddMarqueeSelectionCallback */