1 /* $XConsortium: simple_drag.c /main/4 1995/07/15 20:40:14 drk $ */
2 /*
3  * Motif
4  *
5  * Copyright (c) 1987-2012, The Open Group. All rights reserved.
6  *
7  * These libraries and programs are free software; you can
8  * redistribute them and/or modify them under the terms of the GNU
9  * Lesser General Public License as published by the Free Software
10  * Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * These libraries and programs are distributed in the hope that
14  * they will be useful, but WITHOUT ANY WARRANTY; without even the
15  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE. See the GNU Lesser General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with these librararies and programs; if not, write
21  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
22  * Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 /*
26  * HISTORY
27  */
28 #include <Xm/MainW.h>
29 #include <Xm/CascadeB.h>
30 #include <Xm/MessageB.h>
31 #include <Xm/PushBG.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/ScrollBar.h>
34 #include <Xm/Text.h>
35 #include <Xm/TransferP.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 void CreateMenus(Widget);
41 int  Play(Widget);
42 void HelpCB(Widget, XtPointer, XtPointer);
43 void QuitCB(Widget, XtPointer, XtPointer);
44 void OutputAnAtomName(Widget, Atom);
45 void StartDrag(Widget, XEvent *);
46 void ConvertCallback(Widget, XtPointer, XtPointer);
47 
48 Widget  toplevel;
49 
50 /******************************************************************
51 main:
52 ******************************************************************/
53 int
main(int argc,char ** argv)54 main(int argc, char **argv)
55 {
56     static Widget  MainWindow;
57     XtAppContext   app_context;
58     Widget         Frame1, RC1;
59     Widget         Text1, ScrollBar1;
60     char    dragTranslations[] = "#override <Btn2Down>: StartDrag()";
61     static  XtActionsRec  dragActions[] =
62     {
63         {"StartDrag", (XtActionProc)StartDrag}
64     };
65     XtTranslations            parsed_xlations;
66 
67     XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);
68 
69     toplevel = XtOpenApplication(&app_context, "Test", NULL, 0,
70                                 &argc, argv, NULL, sessionShellWidgetClass,
71                                 NULL, 0);
72 
73     MainWindow = XtVaCreateManagedWidget("MainWindow1",
74                                     xmMainWindowWidgetClass, toplevel,
75                                         NULL);
76 
77     CreateMenus(MainWindow);
78 
79 
80     /* Create a RowColumn to contain the ScrollBar and Text widgets. */
81     RC1 = XtVaCreateManagedWidget("RC1",
82                                     xmRowColumnWidgetClass, MainWindow,
83                                     NULL);
84 
85     /* Create a ScrollBar. */
86     parsed_xlations = XtParseTranslationTable(dragTranslations);
87     XtAppAddActions(app_context, dragActions, XtNumber(dragActions));
88     ScrollBar1 = XtVaCreateManagedWidget("SB1",
89                                     xmScrollBarWidgetClass, RC1,
90                                     XmNorientation, XmHORIZONTAL,
91                                     XmNtranslations, parsed_xlations,
92                                     NULL);
93   /* Associate a convert callback with the ScrollBar. */
94    XtAddCallback(ScrollBar1, XmNconvertCallback, ConvertCallback, NULL);
95 
96 
97   /* Create a text widget; it will be a potential drop site. */
98    Text1 = XtVaCreateManagedWidget("Text",
99                                     xmTextWidgetClass, RC1,
100                                     XmNeditMode, XmMULTI_LINE_EDIT,
101                                     XmNrows, 25,
102                                     XmNcolumns, 25,
103                                     NULL);
104 
105    XtRealizeWidget(toplevel);
106    XtAppMainLoop(app_context);
107 
108    return 0;    /* make compiler happy */
109 }
110 
111 
112 /**************************************************************************
113 StartDrag: When the user presses <Btn2Down>, this routine will be called.
114 **************************************************************************/
115 void
StartDrag(Widget w,XEvent * event)116 StartDrag(Widget w,
117           XEvent *event)
118 {
119  Arg       args[2];
120  Cardinal  n=0;
121 
122   XtSetArg(args[n], XmNdragOperations, XmDROP_COPY); n++;
123   XmeDragSource(w, NULL, event, args, n);
124 }
125 
126 
127 /**********************************************************************
128 ConvertCallback: Handles requests to convert targets.  Actually,
129 our ConvertCallback procedure only knows how to convert the following
130 targets:
131   * MOTIF_EXPORT_TARGETS
132   * TARGETS
133   * COMPOUND_TEXT
134 **********************************************************************/
135 void
ConvertCallback(Widget w,XtPointer ignore,XtPointer call_data)136 ConvertCallback(Widget  w,
137                      XtPointer ignore,
138                      XtPointer call_data)
139 {
140  XmConvertCallbackStruct  *ccs = (XmConvertCallbackStruct *)call_data;
141  int  value;
142  Atom COMPOUND_TEXT = XInternAtom(XtDisplay(w), XmSCOMPOUND_TEXT, False);
143  Atom TARGETS = XInternAtom(XtDisplay(w), "TARGETS", False);
144  Atom MOTIF_EXPORT_TARGETS =
145    XInternAtom(XtDisplay(w), XmS_MOTIF_EXPORT_TARGETS, False);
146  int n;
147 
148   printf("\nNow in ConvertCallback.\n");
149   printf("\tSelection: ");
150   OutputAnAtomName((Widget)w, ccs->selection);
151   printf("\tTarget: ");
152   OutputAnAtomName((Widget)w, ccs->target);
153 
154  /* XmeDragSource is going to call ConvertCallback and ask
155     it to convert MOTIF_EXPORT_TARGETS. */
156   if ( (ccs->target == MOTIF_EXPORT_TARGETS) ||
157        (ccs->target == TARGETS))
158     {
159  /* We have to create a list of targets that ConvertCallback
160     can convert.  For simplicity, we are going to restrict
161     that list to one target: COMPOUND_TEXT. */
162      Atom *targs = (Atom *) XtMalloc(sizeof(Atom) * 1);
163      if (targs == NULL) {
164       ccs->status = XmCONVERT_REFUSE;
165       printf("Refuse.\n");
166       return;
167      }
168      n = 0;
169      targs[n] = COMPOUND_TEXT; n++;
170 
171      ccs->value = (XtPointer) targs;
172      ccs->type = XA_ATOM;
173      ccs->length = n;
174      ccs->format = 32;
175      ccs->status = XmCONVERT_DONE;  /* Yes, we converted the target. */
176    }
177 
178 
179  /* If the drop site supports COMPOUND_TEXT as an import target, then
180     the drop site will ask ConvertCallback to convert the
181     value to COMPOUND_TEXT format. */
182    else if (ccs->target == COMPOUND_TEXT) {
183      char     *passtext;
184      char     *ctext;
185      XmString  cstring;
186      char      ValueAsAString[10];
187 
188   /* The part of the ScrollBar that we are transferring is its
189      XmNvalue resource. */
190      XtVaGetValues(w, XmNvalue, &value, NULL);
191 
192   /* Convert XmNvalue to COMPOUND_TEXT. */
193        sprintf(ValueAsAString, "%d", value);
194        cstring = XmStringCreateLocalized(ValueAsAString);
195        ctext = XmCvtXmStringToCT(cstring);
196        passtext = XtMalloc(strlen(ctext)+1);
197        memcpy(passtext, ctext, strlen(ctext)+1);
198   /* Assign converted string to XmConvertCallbackStruct. */
199      ccs->value  = (XtPointer)passtext;
200      ccs->type   = XA_STRING;
201      ccs->length = strlen(passtext);
202      ccs->format = 8;
203      ccs->status = XmCONVERT_DONE;
204    }
205 
206    else  {
207      /* Unexpected target. */
208      ccs->status = XmCONVERT_REFUSE;
209    }
210 }
211 
212 
213 
214 /**************************************************************************
215 CreateMenus: This function generates the menu bar and the submenus.
216 **************************************************************************/
217 void
CreateMenus(Widget parent_of_menu_bar)218 CreateMenus(Widget parent_of_menu_bar)
219 {
220  XmString   file, help;
221  Widget     menubar, FilePullDown, HelpPullDown;
222  Widget     overview, quit, Help1;
223 
224  /* Create the menubar itself. */
225    file = XmStringCreateSimple("File");
226    help = XmStringCreateSimple("Help");
227 
228    menubar      = (Widget)XmCreateMenuBar(parent_of_menu_bar, "menubar",
229                                           NULL, 0);
230    FilePullDown = (Widget)XmCreatePulldownMenu(menubar, "FilePullDown",
231                                                NULL, 0);
232    HelpPullDown = (Widget)XmCreatePulldownMenu(menubar, "HelpPullDown",
233                                                  NULL, 0);
234 
235  /******************************FILE*********************************/
236     XtVaCreateManagedWidget("File", xmCascadeButtonWidgetClass, menubar,
237                              XmNlabelString, file,
238                              XmNmnemonic, 'F',
239                              XmNsubMenuId, FilePullDown,
240                              NULL);
241     quit = XtVaCreateManagedWidget("Quit", xmPushButtonGadgetClass,
242                                     FilePullDown, NULL);
243     XtAddCallback(quit, XmNactivateCallback, QuitCB, NULL);
244 
245 
246  /******************************HELP*********************************/
247     Help1 = XtVaCreateManagedWidget("Help", xmCascadeButtonWidgetClass,
248                              menubar,
249                              XmNlabelString, help,
250                              XmNmnemonic, 'H',
251                              XmNsubMenuId, HelpPullDown,
252                              NULL);
253     XtVaSetValues(menubar, XmNmenuHelpWidget, Help1, NULL);
254     overview = XtVaCreateManagedWidget("Overview", xmPushButtonGadgetClass,
255                                     HelpPullDown, NULL);
256     XtAddCallback(overview, XmNactivateCallback, HelpCB, (XtPointer)1);
257 
258     XmStringFree(file);
259     XmStringFree(help);
260 
261     XtManageChild(menubar);
262 }
263 
264 
265 
266 /*********************************************************************
267 HelpCB: Provide help when the user asks for it.
268 *********************************************************************/
269 void
HelpCB(Widget w,XtPointer cd,XtPointer cb)270 HelpCB(Widget   w,
271        XtPointer cd,
272        XtPointer cb
273       )
274 {
275  int       what_kind_of_help = (int)cd;
276  char      help_string[500];
277  XmString  hs_as_cs;
278  Widget    dialog_general_help;
279  Arg       arg[3];
280 
281  sprintf(help_string,
282 "This program demonstrates how to add a convert callback\n\
283 to an application and how to add drag capability to the \n\
284 ScrollBar widget.  You should place the cursor on the slider \n\
285 of the ScrollBar.  Then press <Btn2> to begin a drag operation.\n\
286 A DragIcon will appear.  Move the DragIcon to the Text widget \n\
287 and 'drop' the DragIcon anywhere inside the Text widget.\n\
288 The application will transfer the XmNvalue of the ScrollBar \n\
289 to the Text widget.");
290 
291    hs_as_cs = XmStringCreateLtoR(help_string,
292                                  XmFONTLIST_DEFAULT_TAG);
293 
294    XtSetArg(arg[0], XmNmessageString, hs_as_cs);
295    dialog_general_help = (Widget)XmCreateMessageDialog(toplevel,
296                                              "message", arg, 1);
297    XmStringFree(hs_as_cs);
298 
299    switch (what_kind_of_help)  {
300      case 1: XtManageChild(dialog_general_help);
301              break;
302      default: /* no other help */
303              break;
304    }
305 
306 }
307 
308 
309 
310 /*******************************************************************************
311 QuitCB: Exit
312 *******************************************************************************/
313 void
QuitCB(Widget w,XtPointer cd,XtPointer cb)314 QuitCB(Widget w, XtPointer cd, XtPointer cb)
315 {
316   exit(0);
317 }
318 
319 
320 
321 /**********************************************************************
322 OutputAnAtomName: Translates a target from its internal atom format into
323 a human readable character string.
324 **********************************************************************/
325 void
OutputAnAtomName(Widget w,Atom target)326 OutputAnAtomName(Widget w,
327             Atom target)
328 {
329  char  *AtomName = malloc(sizeof(char *) * 34);
330 
331   AtomName = XGetAtomName(XtDisplay(w), target);
332   printf("\t%s\n", AtomName);
333 }
334