actions.c revision 33c89af1
1/* 2 * $Xorg: actions.c,v 1.4 2001/02/09 02:05:29 xorgcvs Exp $ 3 * 4Copyright 1989, 1998 The Open Group 5 6Permission to use, copy, modify, distribute, and sell this software and its 7documentation for any purpose is hereby granted without fee, provided that 8the above copyright notice appear in all copies and that both that 9copyright notice and this permission notice appear in supporting 10documentation. 11 12The above copyright notice and this permission notice shall be included in 13all copies or substantial portions of the Software. 14 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22Except as contained in this notice, the name of The Open Group shall not be 23used in advertising or otherwise to promote the sale, use or other dealings 24in this Software without prior written authorization from The Open Group. 25 */ 26/* $XFree86: xc/programs/editres/actions.c,v 1.6tsi Exp $ */ 27 28#include <stdio.h> 29#include <stdlib.h> 30#include <X11/Intrinsic.h> 31#include <X11/Xutil.h> 32 33#include <X11/Xaw/Cardinals.h> 34#include <X11/Xmu/CharSet.h> 35 36#include "editresP.h" 37 38/* 39 * Private data. 40 */ 41 42struct ActionValues { 43 String name; 44 int type; 45}; 46 47static struct ActionValues select_values[] = { 48 { "widget", (int) SelectWidget }, 49 { "all", (int) SelectAll }, 50 { "nothing", (int) SelectNone }, 51 { "invert", (int) SelectInvert }, 52 { "children", (int) SelectChildren }, 53 { "descendants", (int) SelectDescendants }, 54 { "parent", (int) SelectParent }, 55 { "ancestors", (int) SelectAncestors } 56}; 57 58static struct ActionValues label_values[] = { 59 { "name", (int) NameLabel }, 60 { "class", (int) ClassLabel }, 61 { "id", (int) IDLabel }, 62 { "window", (int) WindowLabel }, 63 { "toggle", (int) ToggleLabel } 64}; 65 66static void EnableGetVal ( Widget w, XEvent *event, 67 String *params, Cardinal * num_params ); 68static void SelectAction ( Widget w, XEvent *event, 69 String *params, Cardinal *num_params ); 70static void RelabelAction ( Widget w, XEvent *event, 71 String *params, Cardinal *num_params ); 72static void PopdownFileDialogAction ( Widget w, XEvent *event, 73 String *params, Cardinal *num_params ); 74static void ActionQuit ( Widget w, XEvent *event, 75 String *params, Cardinal *num_params ); 76static WNode * FindTreeNodeFromWidget ( Widget w ); 77static Boolean CheckAndFindEntry ( String action_name, 78 String * params, Cardinal num_params, 79 struct ActionValues * table, 80 Cardinal num_table, int * type ); 81 82/* Function Name: EnableGetVal 83 * Description: sets a global variable to notify the Notify action 84 * for the resource list widet to do GetValues. 85 * Arguments: w - any widget in the widget tree. 86 * event - NOT USED. 87 * params, num_params - the parameters paseed to the action 88 * routine. 89 * 90 */ 91 92Boolean do_get_values = False; 93 94/* ARGSUSED */ 95static void 96EnableGetVal(w, event, params, num_params) 97Widget w; 98XEvent * event; 99String * params; 100Cardinal * num_params; 101{ 102 do_get_values = True; 103} 104 105/* Function Name: SelectAction 106 * Description: 107 * Arguments: w - any widget in the widget tree. 108 * event - NOT USED. 109 * params, num_params - the parameters paseed to the action 110 * routine. 111 * 112 * params[0] - One of "nothing", "parent", "children", "ancestors", 113 * "descendants", "invert", "all" 114 * num_params - must be one. 115 */ 116 117/* ARGSUSED */ 118static void 119SelectAction(w, event, params, num_params) 120Widget w; 121XEvent * event; 122String * params; 123Cardinal * num_params; 124{ 125 WNode * node; 126 int type; 127 128 if (!CheckAndFindEntry("Select", params, *num_params, 129 select_values, XtNumber(select_values), &type)) 130 return; 131 132 switch(type) { 133 case SelectAll: 134 case SelectNone: 135 case SelectInvert: 136 _TreeSelect(global_tree_info, (SelectTypes)type); 137 break; 138 case SelectWidget: 139 _FindWidget(XtParent(w)); 140 break; 141 default: 142 node = FindTreeNodeFromWidget(w); 143 if (node) 144 _TreeActivateNode(node, (SelectTypes)type); 145 else 146 _TreeSelect(global_tree_info, (SelectTypes)type); 147 break; 148 } 149} 150 151/* Function Name: RelabelAction 152 * Description: 153 * Arguments: w - any widget in the widget tree. 154 * event - NOT USED. 155 * params, num_params - the parameters paseed to the action 156 * routine. 157 * 158 * params[0] - One of "name", "class", "id" 159 * num_params - must be one. 160 */ 161 162/* ARGSUSED */ 163static void 164RelabelAction(w, event, params, num_params) 165Widget w; 166XEvent * event; 167String * params; 168Cardinal * num_params; 169{ 170 WNode * node; 171 int type; 172 173 if (!CheckAndFindEntry("Relabel", params, *num_params, 174 label_values, XtNumber(label_values), &type)) 175 return; 176 177 if ((node = FindTreeNodeFromWidget(w)) == NULL) 178 _TreeRelabel(global_tree_info, (LabelTypes)type); 179 else { 180 PrepareToLayoutTree(global_tree_info->tree_widget); 181 _TreeRelabelNode(node, (LabelTypes)type, FALSE); 182 LayoutTree(global_tree_info->tree_widget); 183 } 184} 185 186/* Function Name: PopdownFileDialogAction 187 * Description: Pops down the file dialog widget. 188 * and calls the approipriate handler. 189 * Arguments: w - any child of the dialog widget. 190 * event - the event that caused this action. 191 * params, num_params - params passed to the action routine. 192 * RETURNED none. 193 */ 194 195/* ARGSUSED */ 196 197static void 198PopdownFileDialogAction(w, event, params, num_params) 199Widget w; 200XEvent * event; 201String * params; 202Cardinal * num_params; 203{ 204 char buf[BUFSIZ]; 205 Boolean val; 206 207 if (*num_params != 1) { 208 sprintf(buf, res_labels[2], 209 "PopdownFileDialog"); 210 211 SetMessage(global_screen_data.info_label, buf); 212 return; 213 } 214 215 XmuCopyISOLatin1Lowered(buf, params[0]); 216 217 if (streq(buf, "cancel")) 218 val = FALSE; 219 else if (streq(buf, "okay")) 220 val = TRUE; 221 else { 222 sprintf(buf, res_labels[1], 223 "PopdownFileDialog"); 224 225 SetMessage(global_screen_data.info_label, buf); 226 return; 227 } 228 229 _PopdownFileDialog(w, (XtPointer)(long) val, NULL); 230} 231 232/* Function Name: ActionQuit 233 * Description: This function prints a message to stdout. 234 * Arguments: w - ** UNUSED ** 235 * call_data - ** UNUSED ** 236 * client_data - ** UNUSED ** 237 * Returns: none 238 */ 239 240/* ARGSUSED */ 241static void 242ActionQuit(w, event, params, num_params) 243Widget w; 244XEvent * event; 245String * params; 246Cardinal * num_params; 247{ 248 if (w==global_toplevel) { 249 XtDestroyApplicationContext(XtWidgetToApplicationContext(w)); 250 exit(0); 251 } 252 else { 253 if (streq(XtName(w), RESOURCE_BOX)) 254 global_resource_box_up = FALSE; 255 XtPopdown(w); 256 XtDestroyWidget(w); 257 } 258} 259 260/* Function Name: SetApplicationActions 261 * Description: Sets my application actions. 262 * Arguments: app_con - the application context. 263 * Returns: none. 264 */ 265 266static XtActionsRec actions[] = { 267 {"EnableGetVal", EnableGetVal}, 268 {"Select", SelectAction}, 269 {"SVActiveEntry", ModifySVEntry}, 270 {"Relabel", RelabelAction}, 271 {"PopdownFileDialog", PopdownFileDialogAction}, 272 {"quit", ActionQuit} 273}; 274 275void 276SetApplicationActions(app_con) 277XtAppContext app_con; 278{ 279 XtAppAddActions(app_con, actions, XtNumber(actions)); 280 281} 282 283 284/************************************************************ 285 * 286 * Private functions 287 * 288 ************************************************************/ 289 290/* Function Name: CheckAndFindEntry 291 * Description: Checks the args to make sure they are valid, 292 * then parses the arg list to find the correct action 293 * to take. 294 * Arguments: action_name - name of the action (for error messages). 295 * params, num_params - params passed to the action routine. 296 * table, num_table - table to check the parameters against. 297 * RETURNED type - info about the action to take. 298 * Returns: TRUE if the arguments are okay. 299 */ 300 301static Boolean 302CheckAndFindEntry(action_name, params, num_params, table, num_table, type) 303String * params, action_name; 304Cardinal num_params, num_table; 305struct ActionValues * table; 306int * type; 307{ 308 char buf[BUFSIZ]; 309 int i; 310 311 if (num_params != 1) { 312 sprintf(buf, res_labels[2], 313 action_name); 314 SetMessage(global_screen_data.info_label, buf); 315 return(FALSE); 316 } 317 318 XmuCopyISOLatin1Lowered(buf, params[0]); 319 for ( i = 0 ; i < num_table; i++ ) 320 if (streq(buf, table[i].name)) { 321 *type = table[i].type; 322 return(TRUE); 323 } 324 325 sprintf(buf,res_labels[3], 326 action_name); 327 328 for (i = 0; i < num_table; ) { 329 strcat(buf, table[i++].name); 330 331 if (i == (num_table - 1)) 332 strcat(buf, ", or "); 333 else if (i < num_table) 334 strcat(buf, ", "); 335 } 336 337 SetMessage(global_screen_data.info_label, buf); 338 return(FALSE); 339} 340 341/* Function Name: FindTreeNodeFromWidget 342 * Description: finds the tree node associated with a widget. 343 * Arguments: w - widget to check. 344 * Returns: the node associated with this widget, or NULL. 345 */ 346 347static WNode * 348FindTreeNodeFromWidget(w) 349Widget w; 350{ 351 int ret_val; 352 XPointer data_return; 353 354 /* 355 * Yes, I really am casting a widget to a window ** TRUST ME *** 356 */ 357 358 ret_val = XFindContext(XtDisplay(w), (Window) w, NODE_INFO, &data_return); 359 360 if (ret_val == 0) 361 return((WNode *) data_return); 362 return(NULL); 363} 364 365