Back to index

tetex-bin  3.0
xm_prefs_scroll.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 Stefan Ulrich
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to
00006  * deal in the Software without restriction, including without limitation the
00007  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00008  * sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00017  * PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM,
00018  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  */
00023 
00024 /*
00025  * Panel 5 (Scrolling behaviour) for xdvik preferences dialog.
00026  */
00027 
00028 #include "xdvi-config.h"
00029 #include "xdvi.h"
00030 
00031 #include "x_util.h"
00032 #include "xm_colorsel.h"
00033 #include "topic-window.h"
00034 #include "util.h"
00035 #include "events.h"
00036 #include "dvi-init.h"
00037 #include "statusline.h"
00038 
00039 #include "xm_prefsP.h"
00040 #include "xm_prefs_scroll.h"
00041 #include "my-snprintf.h"
00042 
00043 #ifdef MOTIF /* entire file */
00044 
00045 #include <ctype.h>
00046 
00047 #include <X11/Xatom.h>
00048 
00049 #include <Xm/Xm.h>
00050 #include <Xm/Protocols.h>
00051 #include <Xm/DialogS.h>
00052 #include <Xm/LabelG.h>
00053 #include <Xm/PushB.h>
00054 #include <Xm/PushBG.h>
00055 #include <Xm/Form.h>
00056 #include <Xm/Frame.h>
00057 #include <Xm/PanedW.h>
00058 #include <Xm/DrawnB.h>
00059 #include <Xm/RowColumn.h>
00060 #include <Xm/Scale.h>
00061 #include <Xm/ToggleBG.h>
00062 #include <Xm/Text.h>
00063 #include <Xm/TextF.h>
00064 #include <Xm/CascadeBG.h>
00065 
00066 /*
00067  * Handy defaults
00068  */
00069 static Arg one_of_many = { XmNindicatorType, XmONE_OF_MANY };
00070 
00071 static void
00072 select_unit_cb(Widget w, XtPointer client_data, XtPointer call_data)
00073 {
00074     struct topic_info *info = (struct topic_info *)client_data;
00075     struct prefs_choice *prefs = (struct prefs_choice *)(info->data);
00076 
00077     Widget pulldown = XtParent(w);
00078     Widget form = XtParent(XtParent(pulldown));
00079     Widget text_x, text_y;
00080     
00081     const char *w_name = XtName(w);
00082 
00083     UNUSED(call_data);
00084     
00085     fprintf(stderr, "setting name: %s\n", w_name);
00086     XtVaSetValues(pulldown,  XmNuserData, (XtPointer)w_name, NULL);
00087 
00088     if (get_widget_by_name(&text_x, form, Xdvi_HOME_POSITION_X_OFF_TEXT, True)
00089        && get_widget_by_name(&text_y, form, Xdvi_HOME_POSITION_Y_OFF_TEXT, True)) {
00090        const char *buf_x = XmTextFieldGetString(text_x);
00091        const char *buf_y = XmTextFieldGetString(text_y);
00092 
00093        int val_x = strtol(buf_x, (char **)NULL, 10);
00094        int val_y = strtol(buf_y, (char **)NULL, 10);
00095        
00096        static char x_off[LENGTH_OF_INT + 16];
00097        static char y_off[LENGTH_OF_INT + 16];
00098 
00099        SNPRINTF(x_off, LENGTH_OF_INT + 16, "%s%s", buf_x, w_name);
00100        SNPRINTF(y_off, LENGTH_OF_INT + 16, "%s%s", buf_y, w_name);
00101 
00102        resource.sidemargin = x_off;
00103        resource.topmargin = y_off;
00104 
00105        resource.sidemargin_int = val_x;
00106        resource.topmargin_int = val_y;
00107 
00108        store_preference(&(prefs->db), "sideMargin", "%s", x_off);
00109        store_preference(&(prefs->db), "topMargin", "%s", y_off);
00110 
00111        globals.ev.flags |= EV_NEWPAGE;
00112        XFlush(DISP);
00113        
00114        XtFree((char *)buf_x);
00115        XtFree((char *)buf_y);
00116     }
00117 }
00118 
00119 static void
00120 set_offset_cb(Widget w, XtPointer client_data, XtPointer call_data)
00121 {
00122     struct topic_info *info = (struct topic_info *)client_data;
00123     struct prefs_choice *prefs = (struct prefs_choice *)(info->data);
00124 
00125     Widget form = XtParent(w);
00126     Widget pulldown;
00127     
00128     UNUSED(call_data);
00129 
00130     if (get_widget_by_name(&pulldown, form, Xdvi_HOME_POSITION_UNITS_PULLDOWN, True)) {
00131        char *ptr = NULL;
00132        const char *w_name = XtName(w);
00133        
00134        XtVaGetValues(pulldown, XmNuserData, &ptr, NULL);
00135 
00136        if (ptr == NULL) {
00137            XDVI_WARNING((stderr, "XmNuserData for %s is NULL in set_offset_cb()\n",
00138                        Xdvi_HOME_POSITION_UNITS_PULLDOWN));
00139            return;
00140        }
00141        fprintf(stderr, "UNIT: |%s|\n", ptr);
00142        
00143        if (strcmp(w_name, Xdvi_HOME_POSITION_X_OFF_TEXT) == 0) {
00144            static char x_off[LENGTH_OF_INT + 16];
00145            const char *buf = XmTextFieldGetString(w);
00146            int val = strtol(buf, (char **)NULL, 10);
00147            
00148            SNPRINTF(x_off, LENGTH_OF_INT + 16, "%s%s", buf, ptr);
00149 
00150            XtFree((char *)buf);
00151            
00152            resource.sidemargin = x_off;
00153            resource.sidemargin_int = val;
00154            store_preference(&(prefs->db), "sideMargin", "%s", x_off);
00155 
00156            goto_page(current_page, resource.keep_flag ? NULL : home, True);
00157        }
00158        else if (strcmp(w_name, Xdvi_HOME_POSITION_Y_OFF_TEXT) == 0) {
00159            static char y_off[LENGTH_OF_INT + 16];
00160            const char *buf = XmTextFieldGetString(w);
00161            int val = strtol(buf, (char **)NULL, 10);
00162            
00163            SNPRINTF(y_off, LENGTH_OF_INT + 16, "%s%s", buf, ptr);
00164 
00165            XtFree((char *)buf);
00166            
00167            resource.topmargin = y_off;
00168            resource.topmargin_int = val;
00169            store_preference(&(prefs->db), "topMargin", "%s", y_off);
00170 
00171            goto_page(current_page, resource.keep_flag ? NULL : home, True);
00172        }
00173        else {
00174            XDVI_WARNING((stderr, "unexpected widget name `%s' in set_offset_cb()", w_name));
00175        }
00176     }
00177 }
00178 
00179 static void
00180 home_position_cb(Widget w, XtPointer client_data, XtPointer call_data)
00181 {
00182     struct topic_info *info = (struct topic_info *)client_data;
00183     struct prefs_choice *prefs = (struct prefs_choice *)(info->data);
00184 
00185     Widget parent = XtParent(XtParent(w));
00186     Widget toggle_home, toggle_curr, label_x, label_y, text_x, text_y, units_menu;
00187     
00188     UNUSED(call_data);
00189 
00190     resource.use_current_offset = !resource.use_current_offset;
00191 
00192     if (get_widget_by_name(&toggle_home, parent, Xdvi_HOME_POSITION_STR, True)
00193        && get_widget_by_name(&toggle_curr, parent, Xdvi_SCROLL_CURRENT_STR, True)
00194        && get_widget_by_name(&label_x, parent, Xdvi_HOME_POSITION_X_STR, True)
00195        && get_widget_by_name(&label_y, parent, Xdvi_HOME_POSITION_Y_STR, True)
00196        && get_widget_by_name(&text_x, parent, Xdvi_HOME_POSITION_X_OFF_TEXT, True)
00197        && get_widget_by_name(&text_y, parent, Xdvi_HOME_POSITION_Y_OFF_TEXT, True)
00198        && get_widget_by_name(&units_menu, parent, Xdvi_HOME_POSITION_UNITS_MENU, True)) {
00199 
00200        XmToggleButtonGadgetSetState(toggle_home, !resource.use_current_offset, False);
00201        XmToggleButtonGadgetSetState(toggle_curr, resource.use_current_offset, False);
00202 
00203        XtSetSensitive(label_x, !resource.keep_flag && !resource.use_current_offset);
00204        XtSetSensitive(label_y, !resource.keep_flag && !resource.use_current_offset);
00205        XtSetSensitive(text_x, !resource.keep_flag && !resource.use_current_offset);
00206        XtSetSensitive(text_y, !resource.keep_flag && !resource.use_current_offset);
00207        XtSetSensitive(units_menu, !resource.keep_flag && !resource.use_current_offset);    
00208     }
00209 
00210     store_preference(&(prefs->db), "useCurrentOffset", "%s", resource.use_current_offset ? "True" : "False");
00211 }
00212 
00213 static void
00214 keep_cb(Widget w, XtPointer client_data, XtPointer call_data)
00215 {
00216     struct topic_info *info = (struct topic_info *)client_data;
00217     struct prefs_choice *prefs = (struct prefs_choice *)(info->data);
00218 
00219     Widget parent, keep, unkeep, toggle_home, toggle_curr, label_x, label_y, text_x, text_y, units_menu;
00220 
00221     UNUSED(call_data);
00222     
00223     resource.keep_flag = !resource.keep_flag;
00224     
00225     parent = XtParent(w);
00226 
00227     if (get_widget_by_name(&keep, parent, Xdvi_SCROLL_KEEP_STR, True)
00228        && get_widget_by_name(&unkeep, parent, Xdvi_SCROLL_UNKEEP_STR, True)
00229        && get_widget_by_name(&toggle_home, parent, Xdvi_HOME_POSITION_STR, True)
00230        && get_widget_by_name(&toggle_curr, parent, Xdvi_SCROLL_CURRENT_STR, True)
00231        && get_widget_by_name(&label_x, parent, Xdvi_HOME_POSITION_X_STR, True)
00232        && get_widget_by_name(&label_y, parent, Xdvi_HOME_POSITION_Y_STR, True)
00233        && get_widget_by_name(&text_x, parent, Xdvi_HOME_POSITION_X_OFF_TEXT, True)
00234        && get_widget_by_name(&text_y, parent, Xdvi_HOME_POSITION_Y_OFF_TEXT, True)
00235        && get_widget_by_name(&units_menu, parent, Xdvi_HOME_POSITION_UNITS_MENU, True)) {
00236 
00237        XmToggleButtonGadgetSetState(keep, resource.keep_flag, False);
00238        XmToggleButtonGadgetSetState(unkeep, !resource.keep_flag, False);
00239 
00240        XtSetSensitive(label_x, !resource.keep_flag && !resource.use_current_offset);
00241        XtSetSensitive(label_y, !resource.keep_flag && !resource.use_current_offset);
00242        XtSetSensitive(text_x, !resource.keep_flag && !resource.use_current_offset);
00243        XtSetSensitive(text_y, !resource.keep_flag && !resource.use_current_offset);
00244        XtSetSensitive(units_menu, !resource.keep_flag && !resource.use_current_offset);
00245        XtSetSensitive(toggle_home, !resource.keep_flag);
00246        XtSetSensitive(toggle_curr, !resource.keep_flag);
00247     }
00248 
00249     store_preference(&(prefs->db), "keepPosition", "%s", resource.keep_flag ? "True" : "False");
00250 
00251 }
00252 
00253 static Widget
00254 h_create_scrolling(Widget parent, struct topic_info *info)
00255 {
00256     Widget form, form1, form2, size_option, curr_option;
00257     Widget x_off_label, x_off_text;
00258     Widget y_off_label, y_off_text;
00259     Widget off_units_menu, cascade, entry1, entry2, entry3;
00260 
00261     Widget scroll_keep, scroll_unkeep;
00262     
00263     char x_buf[LENGTH_OF_INT + 16];
00264     char y_buf[LENGTH_OF_INT + 16];
00265     int n;
00266     Arg args[8];
00267 
00268     fprintf(stderr, "sidemargin: %s, topmargin: %s\n", resource.sidemargin, resource.topmargin);
00269     
00270     if (resource.sidemargin != NULL) {
00271        char *ptr;
00272        strcpy(x_buf, resource.sidemargin);
00273        ptr = x_buf;
00274        if (*ptr == '-')
00275            ptr++;
00276        while (isdigit((int)*ptr))
00277            ptr++;
00278        *ptr = '\0';
00279     }
00280     else
00281        strcpy(x_buf, "0");
00282     
00283     if (resource.topmargin != NULL) {
00284        char *ptr;
00285        strcpy(y_buf, resource.topmargin);
00286        ptr = y_buf;
00287        if (*ptr == '-')
00288            ptr++;
00289        while (isdigit((int)*ptr))
00290            ptr++;
00291        *ptr = '\0';
00292     }
00293     else
00294        strcpy(y_buf, "0");
00295     
00296     form = XmCreateForm(parent, "offsets_form", NULL, 0);
00297 
00298     scroll_keep = XmCreateToggleButtonGadget(form, Xdvi_SCROLL_KEEP_STR, &one_of_many, 1);
00299     XtVaSetValues(scroll_keep,
00300                 XmNtopAttachment, XmATTACH_FORM,
00301                 XmNleftAttachment, XmATTACH_FORM,
00302                 NULL);
00303     scroll_unkeep = XmCreateToggleButtonGadget(form, Xdvi_SCROLL_UNKEEP_STR, &one_of_many, 1);
00304     XtVaSetValues(scroll_unkeep,
00305                 XmNtopAttachment, XmATTACH_WIDGET,
00306                 XmNtopWidget, scroll_keep,
00307                 XmNleftAttachment, XmATTACH_FORM,
00308                 NULL);
00309 
00310     XmToggleButtonGadgetSetState(scroll_keep, resource.keep_flag, False);
00311     XmToggleButtonGadgetSetState(scroll_unkeep, !resource.keep_flag, False);
00312 
00313     XtAddCallback(scroll_keep, XmNvalueChangedCallback, keep_cb, (XtPointer)info);
00314     XtAddCallback(scroll_unkeep, XmNvalueChangedCallback, keep_cb, (XtPointer)info);
00315     
00316     XtManageChild(scroll_keep);
00317     XtManageChild(scroll_unkeep);
00318     
00319     form1 = XmCreateForm(form, "offsets_form1", NULL, 0);
00320     XtVaSetValues(form1,
00321                 XmNtopAttachment, XmATTACH_WIDGET,
00322                 XmNtopWidget, scroll_unkeep,
00323                 XmNleftAttachment, XmATTACH_FORM,
00324                 XmNleftOffset, 20,
00325                 NULL);
00326     
00327     size_option = XmCreateToggleButtonGadget(form1, Xdvi_HOME_POSITION_STR, &one_of_many, 1);
00328     XtVaSetValues(size_option,
00329                 XmNtopAttachment, XmATTACH_FORM,
00330                 XmNleftAttachment, XmATTACH_FORM,
00331                 XmNbottomAttachment, XmATTACH_FORM,
00332                 NULL);
00333     x_off_label = XmCreateLabelGadget(form1, Xdvi_HOME_POSITION_X_STR, NULL, 0);
00334     XtVaSetValues(x_off_label,
00335                 XmNtopAttachment, XmATTACH_FORM,
00336                 XmNleftAttachment, XmATTACH_WIDGET,
00337                 XmNleftWidget, size_option,
00338                 XmNbottomAttachment, XmATTACH_FORM,
00339                 NULL);
00340     x_off_text = XtVaCreateManagedWidget(Xdvi_HOME_POSITION_X_OFF_TEXT,
00341                                     xmTextFieldWidgetClass, form1,
00342                                     XmNcolumns, 4,
00343                                     XmNtopAttachment, XmATTACH_FORM,
00344                                     XmNleftAttachment, XmATTACH_WIDGET,
00345                                     XmNleftWidget, x_off_label,
00346                                     XmNbottomAttachment, XmATTACH_FORM,
00347                                     XmNvalue, x_buf,
00348 /*                                  XmNvalue, buf, */
00349                                     NULL);
00350     y_off_label = XmCreateLabelGadget(form1, Xdvi_HOME_POSITION_Y_STR, NULL, 0);
00351     XtVaSetValues(y_off_label,
00352                 XmNtopAttachment, XmATTACH_FORM,
00353                 XmNleftAttachment, XmATTACH_WIDGET,
00354                 XmNleftWidget, x_off_text,
00355                 XmNleftOffset, 10,
00356                 XmNbottomAttachment, XmATTACH_FORM,
00357                 NULL);
00358     y_off_text = XtVaCreateManagedWidget(Xdvi_HOME_POSITION_Y_OFF_TEXT,
00359                                     xmTextFieldWidgetClass, form1,
00360                                     XmNcolumns, 4,
00361                                     XmNtopAttachment, XmATTACH_FORM,
00362                                     XmNleftAttachment, XmATTACH_WIDGET,
00363                                     XmNleftWidget, y_off_label,
00364                                     XmNbottomAttachment, XmATTACH_FORM,
00365                                     XmNvalue, y_buf,
00366                                     NULL);
00367     n = 0;
00368     XtSetArg(args[n], XmNuserData, (XtPointer)"in"); n++;
00369     off_units_menu = XmCreatePulldownMenu(form1, Xdvi_HOME_POSITION_UNITS_PULLDOWN, args, n);
00370 
00371     n = 0;
00372     XtSetArg(args[n], XmNsubMenuId, off_units_menu); n++;
00373     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
00374     XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
00375     XtSetArg(args[n], XmNleftWidget, y_off_text); n++;
00376     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
00377     cascade = XmCreateOptionMenu(form1, Xdvi_HOME_POSITION_UNITS_MENU, args, n);
00378     entry1 = XtVaCreateManagedWidget("in", xmPushButtonGadgetClass, off_units_menu,
00379                                  NULL);
00380     XtAddCallback(entry1, XmNactivateCallback, select_unit_cb, info);
00381     entry2 = XtVaCreateManagedWidget("cm", xmPushButtonGadgetClass, off_units_menu,
00382                                  NULL);
00383     XtAddCallback(entry2, XmNactivateCallback, select_unit_cb, info);
00384     entry3 = XtVaCreateManagedWidget("Pixel", xmPushButtonGadgetClass, off_units_menu,
00385                                  NULL);
00386     XtAddCallback(entry3, XmNactivateCallback, select_unit_cb, info);
00387 
00388     XtAddCallback(x_off_text, XmNvalueChangedCallback, set_offset_cb, (XtPointer)info);
00389     XtAddCallback(y_off_text, XmNvalueChangedCallback, set_offset_cb, (XtPointer)info);
00390     
00391     XtManageChild(size_option);
00392     XtManageChild(x_off_label);
00393     XtManageChild(x_off_text);
00394     XtManageChild(y_off_label);
00395     XtManageChild(y_off_text);
00396     XtManageChild(cascade);
00397 
00398     form2 = XmCreateForm(form, "offsets_form2", NULL, 0);
00399     XtVaSetValues(form2,
00400                 XmNtopAttachment, XmATTACH_WIDGET,
00401                 XmNtopWidget, form1,
00402                 XmNleftAttachment, XmATTACH_FORM,
00403                 XmNleftOffset, 20,
00404                 NULL);
00405 
00406     curr_option = XmCreateToggleButtonGadget(form2, Xdvi_SCROLL_CURRENT_STR, &one_of_many, 1);
00407     XtVaSetValues(curr_option,
00408                 XmNtopAttachment, XmATTACH_FORM,
00409                 XmNleftAttachment, XmATTACH_FORM,
00410                 XmNbottomAttachment, XmATTACH_FORM,
00411                 NULL);
00412     XtManageChild(curr_option);
00413     
00414     XtSetSensitive(x_off_label, !resource.keep_flag && !resource.use_current_offset);
00415     XtSetSensitive(x_off_text, !resource.keep_flag && !resource.use_current_offset);
00416     XtSetSensitive(y_off_label, !resource.keep_flag && !resource.use_current_offset);
00417     XtSetSensitive(y_off_text, !resource.keep_flag && !resource.use_current_offset);
00418     XtSetSensitive(cascade, !resource.keep_flag && !resource.use_current_offset);
00419     XtSetSensitive(size_option, !resource.keep_flag);
00420     XtSetSensitive(curr_option, !resource.keep_flag);
00421     
00422     XmToggleButtonGadgetSetState(size_option, !resource.use_current_offset, False);
00423     XmToggleButtonGadgetSetState(curr_option, resource.use_current_offset, False);
00424 
00425     XtAddCallback(size_option, XmNvalueChangedCallback, home_position_cb, (XtPointer)info);
00426     XtAddCallback(curr_option, XmNvalueChangedCallback, home_position_cb, (XtPointer)info);
00427     
00428     XtManageChild(form1);
00429     XtManageChild(form2);
00430     
00431     return form;
00432 }
00433 
00434 Widget
00435 prefs_scrolling(struct topic_info *info)
00436 {
00437     Widget form;
00438     Widget scrolling_form, scrolling_frame;
00439 
00440     form = XtVaCreateWidget("form", xmFormWidgetClass,
00441                          info->right_form,
00442                          XmNtopAttachment, XmATTACH_FORM,
00443                          XmNleftAttachment, XmATTACH_FORM,
00444                          XmNrightAttachment, XmATTACH_FORM,
00445                          XmNbottomAttachment, XmATTACH_FORM,
00446                          NULL);
00447 
00448     scrolling_frame = XmCreateFrame(form, "scrolling_frame", NULL, 0);
00449     h_attach_below(scrolling_frame,  NULL);
00450     
00451     scrolling_form = h_create_scrolling(scrolling_frame, info);
00452     XtVaSetValues(scrolling_form,
00453                 XmNtopAttachment, XmATTACH_WIDGET,
00454                 XmNtopWidget, scrolling_frame,
00455                 XmNleftAttachment, XmATTACH_FORM,
00456                 XmNrightAttachment, XmATTACH_FORM,
00457                 XmNbottomAttachment, XmATTACH_FORM,
00458                 NULL);
00459     
00460     XtManageChild(scrolling_form);
00461     XtManageChild(scrolling_frame);
00462 
00463     return form;
00464 }
00465 
00466 #else
00467 /* silence `empty compilation unit' warnings */
00468 static void bar(void); static void foo() { bar(); } static void bar(void) { foo(); }
00469 #endif /* MOTIF */
00470