如何解决openMotif和C编程发出将字符串传输到Dialog Widget的问题
|| 我的代码有问题。目的是显示在词典中找到的单词(加载到列表中)并返回翻译。一切正常,除了对话框中显示的字符串仅显示一种奇怪的编码,而不是我希望他显示的单词。在终端中,程序运行正常,但我认为问题在于以正确的方式为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 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 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,&argc,argv,NULL);
main_w = XtVaCreateManagedWidget(\"main_window\",xmMainWindowWidgetClass,top_wid,XmNheight,300,XmNwidth,NULL);
menu_bar = XmCreateMenuBar(main_w,\"main_list\",0);
XtManageChild(menu_bar);
/* widget chercher */
prompt = XtVaCreateManagedWidget(\"Chercher\",xmCascadeButtonWidgetClass,XmNmnemonic,\'P\',NULL);
/* callback de chercher */
XtAddCallback(prompt,XmNactivateCallback,prompt_pop_up,NULL);
/* pour fermer */
Widget quit = XtVaCreateManagedWidget(\"Fermer\",NULL);
XtAddCallback(quit,quit_call,NULL);
XtRealizeWidget(top_wid);
XtAppMainLoop(app);
return 0;
}
void prompt_pop_up(Widget W,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 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,XmCHARSET_TEXT,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],xm_string2);
/* bouton OK */XtSetArg(args[1],XmNdefaultButtonType,XmDIALOG_OK_BUTTON);
/* dialogue pour renvoyer la chaine trouvée */
dialog = XmCreateinformationDialog(top_wid,\"Resultat\",2);
/*pour enLever les boutons inutiles,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,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中调试代码,而只能在终端中进行调试。当我将其发布到Eclipse时,它给我很多错误,好像找不到库一样,有没有办法解决它?
解决方法
多亏了此链接,我才设法包含了开放式的日食库以供日食使用:
http://www.ferdychristant.com/blog/articles/DOMM-72MPPE
在-l框中,我添加了X11和Xm,现在可以正常工作
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。