Merge branch 'master' into cde-next
[oweals/cde.git] / cde / programs / dtfile / FilterP.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 libraries 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 /* $XConsortium: FilterP.c /main/4 1995/11/02 14:39:12 rswiston $ */
24 /************************************<+>*************************************
25  ****************************************************************************
26  *
27  *   FILE:           FilterP.c
28  *
29  *   COMPONENT_NAME: Desktop File Manager (dtfile)
30  *
31  *   Description:    Processing functions for the file filter dialog
32  *
33  *   FUNCTIONS: FilterChange
34  *              FilterClose
35  *              NewFileTypeSelected
36  *              SelectAllFileTypes
37  *              ShowFilterDialog
38  *              UnselectAllFileTypes
39  *
40  *   (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
41  *   (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
42  *   (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
43  *   (c) Copyright 1993, 1994, 1995 Novell, Inc.
44  *
45  ****************************************************************************
46  ************************************<+>*************************************/
47
48 #include <Xm/XmP.h>
49 #include <Xm/Xm.h>
50 #include <Xm/RowColumn.h>
51
52 #include <Dt/Icon.h>
53
54 #include <Dt/HourGlass.h>
55 #include <Dt/FileM.h>
56 #include "Encaps.h"
57 #include "SharedProcs.h"
58
59 #include "FileMgr.h"
60 #include "Desktop.h"
61 #include "Main.h"
62 #include "Common.h"
63 #include "Filter.h"
64
65
66 /********    Static Function Declarations    ********/
67
68 static void FilterChange(
69                         XtPointer client_data,
70                         DialogData *old_dialog_data,
71                         DialogData *new_dialog_data,
72                         XtPointer call_data) ;
73 static void FilterClose(
74                         XtPointer client_data,
75                         DialogData *old_dialog_data,
76                         DialogData *new_dialog_data) ;
77
78 /********    End Static Function Declarations    ********/
79
80
81
82 /************************************************************************
83  *
84  *  ShowFilterDialog
85  *      Callback functions invoked from the File Filter ... menu
86  *      item.  This function displays the file filter dialog.
87  *
88  ************************************************************************/
89
90 void
91 ShowFilterDialog(
92         Widget w,
93         XtPointer client_data,
94         XtPointer callback )
95 {
96    FileMgrRec  * file_mgr_rec;
97    DialogData  * dialog_data;
98    FileMgrData * file_mgr_data;
99    FilterRec * filter_rec;
100    Arg args[1];
101    Widget mbar;
102    char *tmpStr, *tempStr;
103
104
105    /*  Set the menu item to insensitive to prevent multiple  */
106    /*  dialogs from being posted and get the area under the  */
107    /*  menu pane redrawn.                                    */
108
109    if (w)
110    {
111       mbar = XmGetPostedFromWidget(XtParent(w));
112       XmUpdateDisplay (w);
113       XtSetArg(args[0], XmNuserData, &file_mgr_rec);
114       XtGetValues(mbar, args, 1);
115
116       /* Ignore accelerators when we're insensitive */
117       if ((file_mgr_rec->menuStates & FILTER) == 0)
118       {
119          XSetInputFocus(XtDisplay(w),
120                         XtWindow(file_mgr_rec->filterBtn_child),
121                         RevertToParent, CurrentTime);
122          return;
123       }
124    }
125    else
126    {
127       /* Done only during a restore session */
128       file_mgr_rec = (FileMgrRec *)client_data;
129    }
130
131
132    /* Ignore accelerators received after we're unposted */
133    if ((dialog_data = _DtGetInstanceData ((XtPointer)file_mgr_rec)) == NULL)
134       return;
135    file_mgr_data = (FileMgrData *) dialog_data->data;
136
137    file_mgr_rec->menuStates &= ~FILTER;
138
139    _DtTurnOnHourGlass (file_mgr_rec->shell);
140
141    _DtShowDialog (file_mgr_rec->shell, NULL, (XtPointer)file_mgr_rec, 
142                file_mgr_data->filter_edit, FilterChange,
143                (XtPointer)file_mgr_rec, FilterClose, (XtPointer)file_mgr_rec,
144                NULL, False, False, NULL, NULL);
145
146    filter_rec = (FilterRec *)_DtGetDialogInstance(file_mgr_data->filter_edit);
147
148    if(file_mgr_data->title != NULL && 
149                strcmp(file_mgr_data->helpVol, DTFILE_HELP_NAME) != 0)
150    {
151       tmpStr = (GETMESSAGE(13,19, "Set Filter Options"));
152       tempStr = (char *)XtMalloc(strlen(tmpStr) +
153                                  strlen(file_mgr_data->title) + 5);
154       sprintf(tempStr, "%s - %s", file_mgr_data->title, tmpStr);
155    }
156    else
157    {
158       tmpStr = (GETMESSAGE(13,24, "File Manager - Set Filter Options"));
159       tempStr = XtNewString(tmpStr);
160    }
161    XtSetArg (args[0], XmNtitle, tempStr);
162    XtSetValues (filter_rec->shell, args, 1);
163    XtFree(tempStr);
164
165    file_mgr_rec->filterBtn_child=filter_rec->shell;
166    _DtTurnOffHourGlass (file_mgr_rec->shell);
167
168    {
169      Widget hb = NULL;
170
171      if( XtIsRealized( filter_rec->shell ) )
172      {
173        /* In order for the set increment to work,
174           the top level shell has to be realized.
175           This seemed to be a good place for it.
176        */
177        XtSetArg( args[0], XmNhorizontalScrollBar, &hb );
178        XtGetValues( filter_rec->scrolled_window, args, 1 );
179        if( hb )
180        {
181          XtSetArg( args[0], XmNincrement, 40 );
182          XtSetValues( hb, args, 1 );
183        }
184      }
185    }
186 }
187
188
189
190
191 /************************************************************************
192  *
193  *  FilterChange
194  *      Callback functions invoked from the file filter dialog's
195  *      apply button being pressed.  This function updates and redisplays
196  *      the current set of objects being viewed.
197  *
198  ************************************************************************/
199
200 static void
201 FilterChange(
202         XtPointer client_data,
203         DialogData *old_dialog_data,
204         DialogData *new_dialog_data,
205         XtPointer call_data )
206 {
207    FileMgrRec  * file_mgr_rec = (FileMgrRec *) client_data;
208    DialogData  * dialog_data;
209    FileMgrData * file_mgr_data;
210    FilterData  * filter_data;
211    XtPointer save_data;
212
213
214    dialog_data = _DtGetInstanceData ((XtPointer)file_mgr_rec);
215    file_mgr_data = (FileMgrData *) dialog_data->data;
216
217    /*
218     * Replace the active values with those just applied in the dialog,
219     * and then free up the old active values.
220     */
221
222    save_data = file_mgr_data->filter_active->data;
223    file_mgr_data->filter_active->data = new_dialog_data->data;
224    new_dialog_data->data = save_data;
225    _DtFreeDialogData (new_dialog_data);
226
227    filter_data = (FilterData *) file_mgr_data->filter_active->data;
228    filter_data->displayed = False;
229
230
231    /* Refilter the contents of the current directory */
232
233    FileMgrRedisplayFiles (file_mgr_rec, file_mgr_data, False);
234 }
235
236
237
238
239 /************************************************************************
240  *
241  *  FilterClose
242  *      Callback function invoked from the file filter dialog's close
243  *      button.  This function resensitizes the menu item, and saves the
244  *      interim dialog values.
245  *
246  ************************************************************************/
247
248 static void
249 FilterClose(
250         XtPointer client_data,
251         DialogData *old_dialog_data,
252         DialogData *new_dialog_data )
253 {
254    FileMgrRec * file_mgr_rec = (FileMgrRec *) client_data;
255    XtPointer save_data;
256
257
258    /* Free up the old interim values, and save the new ones */
259
260    save_data = old_dialog_data->data;
261    old_dialog_data->data = new_dialog_data->data;
262    new_dialog_data->data = save_data;
263    _DtFreeDialogData (new_dialog_data);
264
265
266    /* Resensitize the associated menubutton */
267    file_mgr_rec->menuStates |= FILTER;
268 }
269
270 void
271 NewFileTypeSelected (
272    Widget  w,
273    XtPointer client_data,
274    XtPointer callback)
275 {
276    XmAnyCallbackStruct * callback_data = (XmAnyCallbackStruct *) callback;
277    Arg args[10];
278    Pixel background_color;
279    Pixel foreground_color;
280    Pixel pixmap_background;
281    FilterRec *filter_rec;
282    XmManagerWidget mgr;
283    FTData * user_data;
284
285    filter_rec = (FilterRec *)client_data;
286    mgr = (XmManagerWidget)filter_rec->file_window;
287
288    if (callback_data->reason == XmCR_ACTIVATE)
289    {
290       XtSetArg (args[0], XmNbackground, &background_color);
291       XtSetArg (args[1], XmNforeground, &foreground_color);
292       XtSetArg (args[2], XmNtopShadowColor, &pixmap_background);
293       XtGetValues ((Widget)mgr, args, 3);
294
295       XtSetArg(args[0], XmNuserData, &user_data);
296       XtGetValues(w, args, 1);
297
298       if(user_data->selected)
299       {
300          /* Draw the old selected icon in normal state */
301          XtSetArg (args[0], XmNpixmapForeground, black_pixel);
302          XtSetArg (args[1], XmNforeground, foreground_color);
303          XtSetArg (args[2], XmNarmColor, white_pixel);
304
305          if (background_color == white_pixel)
306          {
307             XtSetArg (args[3], XmNbackground, white_pixel);
308             XtSetArg (args[4], XmNpixmapBackground, white_pixel);
309          }
310          else if (background_color == black_pixel)
311          {
312             XtSetArg (args[3], XmNbackground, black_pixel);
313             XtSetArg (args[4], XmNpixmapBackground, white_pixel);
314          }
315          else
316          {
317             XtSetArg (args[3], XmNbackground, background_color);
318             XtSetArg (args[4], XmNpixmapBackground, pixmap_background);
319          }
320          XtSetValues(w, args, 5);
321          user_data->selected = False;
322
323       }
324       else
325       {
326
327          /* Draw the selected icon as selected */
328          if (background_color == white_pixel)
329          {
330             XtSetArg(args[0], XmNbackground, black_pixel);
331             XtSetArg(args[1], XmNforeground, white_pixel);
332          }
333          else if (background_color == black_pixel)
334          {
335             XtSetArg(args[0], XmNbackground, white_pixel);
336             XtSetArg(args[1], XmNforeground, black_pixel);
337          }
338          else
339          {
340             XtSetArg(args[0], XmNbackground, white_pixel);
341             XtSetArg(args[1], XmNforeground, black_pixel);
342          }
343    
344          XtSetArg(args[2], XmNpixmapBackground, white_pixel);
345          XtSetArg(args[3], XmNpixmapForeground, black_pixel);
346          XtSetArg(args[4], XmNarmColor, white_pixel);
347          XtSetValues(w, args, 5);
348          user_data->selected = True;
349       }
350    
351    }
352    else if (callback_data->reason == XmCR_HIGHLIGHT)
353       DrawHighlight(w, NULL, NULL, NOT_DESKTOP);
354    else if (callback_data->reason == XmCR_UNHIGHLIGHT)
355       DrawUnhighlight(w, NOT_DESKTOP);
356    else if (callback_data->reason == XmCR_SHADOW)
357       DrawShadowTh(w, NULL, NOT_DESKTOP);
358  
359 }
360
361 void
362 SelectAllFileTypes (
363    Widget  w,
364    XtPointer client_data,
365    XtPointer callback)
366 {
367    Arg args[6];
368    FilterRec *filter_rec;
369    Pixel background_color;
370    XmManagerWidget mgr;
371    FTData * user_data;
372    int num_of_filetypes, i;
373
374    filter_rec = (FilterRec *)client_data;
375    mgr = (XmManagerWidget)filter_rec->file_window;
376    num_of_filetypes = mgr->composite.num_children;
377
378    XtSetArg (args[0], XmNbackground, &background_color);
379    XtGetValues ((Widget)mgr, args, 1);
380
381    for(i = 0; i < num_of_filetypes; i++)
382    {
383       XtSetArg(args[0], XmNuserData, &user_data);
384       XtGetValues(mgr->composite.children[i], args, 1);
385
386       if(!user_data->selected)
387       {
388          /* Draw the selected icon as selected */
389          if (background_color == white_pixel)
390          {
391             XtSetArg(args[0], XmNbackground, black_pixel);
392             XtSetArg(args[1], XmNforeground, white_pixel);
393          }
394          else if (background_color == black_pixel)
395          {
396             XtSetArg(args[0], XmNbackground, white_pixel);
397             XtSetArg(args[1], XmNforeground, black_pixel);
398          }
399          else
400          {
401             XtSetArg(args[0], XmNbackground, white_pixel);
402             XtSetArg(args[1], XmNforeground, black_pixel);
403          }
404
405          XtSetArg(args[2], XmNpixmapBackground, white_pixel);
406          XtSetArg(args[3], XmNpixmapForeground, black_pixel);
407          XtSetArg(args[4], XmNarmColor, white_pixel);
408          XtSetValues(mgr->composite.children[i], args, 5);
409
410          user_data->selected = True;
411       }
412    }
413
414 }
415
416 void
417 UnselectAllFileTypes (
418    Widget  w,
419    XtPointer client_data,
420    XtPointer callback)
421 {
422    Arg args[6];
423    FilterRec *filter_rec;
424    Pixel background_color;
425    Pixel foreground_color;
426    Pixel pixmap_background;
427    XmManagerWidget mgr;
428    FTData * user_data;
429    int num_of_filetypes, i;
430
431    filter_rec = (FilterRec *)client_data;
432    mgr = (XmManagerWidget)filter_rec->file_window;
433    num_of_filetypes = mgr->composite.num_children;
434
435    XtSetArg (args[0], XmNbackground, &background_color);
436    XtSetArg (args[1], XmNforeground, &foreground_color);
437    XtSetArg (args[2], XmNtopShadowColor, &pixmap_background);
438    XtGetValues ((Widget)mgr, args, 3);
439
440    for(i = 0; i < num_of_filetypes; i++)
441    {
442       XtSetArg(args[0], XmNuserData, &user_data);
443       XtGetValues(mgr->composite.children[i], args, 1);
444
445       if(user_data->selected)
446       {
447          /* Draw the old selected icon in normal state */
448          XtSetArg (args[0], XmNpixmapForeground, black_pixel);
449          XtSetArg (args[1], XmNforeground, foreground_color);
450          XtSetArg (args[2], XmNarmColor, white_pixel);
451
452          if (background_color == white_pixel)
453          {
454             XtSetArg (args[3], XmNbackground, white_pixel);
455             XtSetArg (args[4], XmNpixmapBackground, white_pixel);
456          }
457          else if (background_color == black_pixel)
458          {
459             XtSetArg (args[3], XmNbackground, black_pixel);
460             XtSetArg (args[4], XmNpixmapBackground, white_pixel);
461          }
462          else
463          {
464             XtSetArg (args[3], XmNbackground, background_color);
465             XtSetArg (args[4], XmNpixmapBackground, pixmap_background);
466          }
467          XtSetValues(mgr->composite.children[i], args, 5);
468
469          user_data->selected = False;
470       }
471    }
472 }