Проблема программирования openMotif и C для передачи строки в Dialog Widget - PullRequest
0 голосов
/ 25 мая 2011

У меня проблема с моим кодом. Цель - показать слово, найденное в словаре (загруженное в список), и вернуть перевод. Все работает отлично, за исключением того, что строка, показанная в диалоге, просто показывает странную кодировку, а не слово, которое я хочу, чтобы он показал. В терминале программа работает нормально, но проблема, я думаю, заключается в правильном кодировании строки для Xm_string. Вот мой код:

//gcc promt3.c -o promt3 -Wall -L/usr/X11R6/lib -lX11 -lXm

#include <Xm/Xm.h>
#include <Xm/Text.h>
#include <Xm/MainW.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/PushB.h>
#include <Xm/SelectioB.h>
#include <Xm/RowColumn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void prompt_pop_up(Widget W, XtPointer Ev, XtPointer client);

void prompt_activate(Widget W, XtPointer Ev, XtPointer client);
void quit_activate(Widget dialog);

Widget top_wid;

enum {
    MaxLigne = 100, MaxMot = 30, MAX = 7876,
};

struct liste {
    /* lignes du fichier */
    char LIGNE[MaxLigne];

    /* cdr */
    struct liste *cdr;
};
struct liste * ajouter(struct liste * dico, char valeur);
char * rechercher(struct liste *dico, char * recherche);
void decouper(char * ligne, char * separ, char * mot[], int maxmot);

char ligne[MaxLigne];
int i = 0;
char ligne3[MaxLigne];
struct liste * francais = NULL;

void quit_call(Widget W, XtPointer Ev, XtPointer client) {
    printf("Quitting program\n");
    exit(0);
}

int main(int argc, char *argv[])

{

    //preparation du dictionnaire:

    FILE * fichier1 = fopen("dico.tab", "r");
    if (fichier1 == NULL) {
        printf("erreur fichier");
    }
    int k;
    for (k = 0; k < MAX; k++) {
        struct liste * nouveau = malloc(sizeof(*nouveau));

        if (nouveau != NULL) {
            fgets(nouveau->LIGNE, sizeof ligne, fichier1);
            nouveau->cdr = NULL;
            if (francais->LIGNE == NULL) {
                francais = nouveau;
            } else {
                struct liste * p = francais;
                while (p->cdr != NULL) {
                    p = p->cdr;
                }
                p->cdr = nouveau;
            }
        }
        i++;
    }
    printf("Le dictionnaire contient %i entrées\n", i);
    fclose(fichier1);

    XtAppContext app;
    Widget main_w, menu_bar, prompt;

    top_wid = XtVaAppInitialize(&app, "Demos", NULL, 0, &argc, argv, NULL, NULL);

    main_w = XtVaCreateManagedWidget("main_window", xmMainWindowWidgetClass,
            top_wid, XmNheight, 300, XmNwidth, 300, NULL);

    menu_bar = XmCreateMenuBar(main_w, "main_list", NULL, 0);
    XtManageChild(menu_bar);

    /* widget chercher */
    prompt = XtVaCreateManagedWidget("Chercher", xmCascadeButtonWidgetClass,
            menu_bar, XmNmnemonic, 'P', NULL);

    /* callback de chercher */
    XtAddCallback(prompt, XmNactivateCallback, prompt_pop_up, NULL);

    /* pour fermer */

    Widget quit = XtVaCreateManagedWidget("Fermer", xmCascadeButtonWidgetClass,
            menu_bar, NULL);
    XtAddCallback(quit, XmNactivateCallback, quit_call, NULL);

    XtRealizeWidget(top_wid);
    XtAppMainLoop(app);

    return 0;
}

void prompt_pop_up(Widget W, XtPointer Ev, XtPointer client)

{
    Widget dialog;
    Widget effacer1, effacer2;
    XmString xm_string1;

    //structure pour receuillir les données envoyées par le widget
    Arg args[2];

    /* label du dialogue */
    xm_string1 = XmStringCreateLocalized("Entrez le mot a chercher:");
    XtSetArg(args[0], XmNselectionLabelString, xm_string1);

    XtSetArg(args[1], XmNtextString, xm_string1);

    /* Create the WarningDialog */
    dialog = XmCreatePromptDialog(W, "Recherche", args, 3);

    XmStringFree(xm_string1);
    //XmStringFree(xm_string2);


    XtAddCallback(dialog, XmNokCallback, prompt_activate, NULL);

    /*pour effacer les boutons inutiles, par defaut PromtDialog contient 3 boutons: OK, CANCEL, HELP*/
    effacer1 = XmSelectionBoxGetChild(dialog, XmDIALOG_HELP_BUTTON);
    XtUnmanageChild(effacer1);

    effacer2 = XmSelectionBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON);
    XtUnmanageChild(effacer2);

    XtManageChild(dialog);
    XtPopup(XtParent(dialog), XtGrabNone);
}

/* fonction de recherche */

void prompt_activate(Widget W, XtPointer Ev, XtPointer client)

{
    Widget dialog, effacer1, effacer2;
    Arg args[2];
    XmString xm_string, xm_string2;
    //structure pour receuillir les données envoyée par le widget
    XmSelectionBoxCallbackStruct *cbs = (XmSelectionBoxCallbackStruct *) client;

    xm_string = XmStringCreateLocalized("");

    //concatenation de la chaine à rechercher
    xm_string = XmStringConcat(xm_string, cbs->value);

    //la fonction XmStringUnparse permet d'obtenir la chaine contenue dans XmString
    String x = XmStringUnparse(xm_string, NULL, 0, XmCHARSET_TEXT, NULL, 0,
            XmOUTPUT_ALL);

    printf("valeur : %s\n", x); //pour verifier ce qu'elle renvoie --->OK

    char * ligne2 = malloc(MaxMot);

    //on cherche à obtenir la ligne qui nous intéresse, si on l'obtient on imprime les mots qui nous interesent
    ligne2 = rechercher(francais, x);

    if (ligne2 != NULL) {
        char ligne5[MaxLigne];
        char * ligne6[MaxLigne];
        char * mot1[MaxMot];
        char * mot2[MaxMot];
        strcpy(ligne5, ligne2);

        decouper(ligne5, "\n-", ligne6, MaxLigne);
        decouper(ligne6[0], "\t[]", mot1, MaxMot);
        decouper(ligne6[1], "[]", mot2, MaxMot);

        //supprimer le premier espace
        strcpy(mot2[0], mot2[0] + 1);

        //mot2[0] est le mot qu'on recherche
        xm_string2 = XmStringCreateLocalized(mot2[0]);
        //xm_string2 = XmStringConcat(xm_string2,mot2[0]);

        printf("%s,  %s se traduit par: %s, %s\n", x, mot1[1], mot2[0], mot2[1]);

    } else {
        printf("Le mot %s n'est pas dans le dictionnaire\n", x);
        xm_string2 = XmStringCreateLocalized("Mot pas trouve");
    }
    //XtSetArg(args[0], XmNmessageString, xm_string);
    XtSetArg(args[0], XmNmessageString, xm_string2);
    /* bouton OK */XtSetArg(args[1], XmNdefaultButtonType,
            XmDIALOG_OK_BUTTON);

    /* dialogue pour renvoyer la chaine trouvée */
    dialog = XmCreateInformationDialog(top_wid, "Resultat", args, 2);

    /*pour enlever les boutons inutiles, par defaut PromtDialog contient 3 boutons: OK, CANCEL, HELP*/
    effacer1 = XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON);

    effacer2 = XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON);
    XtUnmanageChild(effacer2);
    XtUnmanageChild(effacer1);
    free(x);
    free(xm_string);
    free(xm_string2);
    XtManageChild(dialog);
    XtPopup(XtParent(dialog), XtGrabNone);

}

char * rechercher(struct liste *dico, char * recherche) {
    int k;
    struct liste *p = dico;
    char ligne4[MaxLigne];
    strcpy(ligne4, recherche);
    for (k = 0; k < i; k++) {
        int v = strlen(recherche);
        strcpy(ligne3, p->LIGNE);
        if (strncmp(ligne3, recherche, v) == 0) {
            return ligne3;

        } else {
            p = p->cdr;
        }
    }
    return NULL;
}

void decouper(char * ligne, char * separ, char * mot[], int maxmot) {
    int i;

    mot[0] = strtok(ligne, separ);
    for (i = 1; mot[i - 1] != 0; i++) {
        if (i == maxmot) {
            fprintf(stderr, "Erreur dans la fonction decouper: trop de mots\n");
            mot[i - 1] = 0;
        }
        mot[i] = strtok(NULL, separ);
    }
}

У меня также проблема в том, что я не могу отладить код в eclipse, я могу сделать это только в терминале. Когда я отправляю это сообщение для затмения, оно дает мне много ошибок, как будто библиотеки не найдены, есть ли способ исправить это?

1 Ответ

0 голосов
/ 11 сентября 2011

Благодаря этой ссылке мне удалось включить в eclipse библиотеки с открытыми мотивами: http://www.ferdychristant.com/blog/articles/DOMM-72MPPE В поле -l я добавил X11 и Xm, и теперь он отлично работает

...