1

I am trying to set the title of a toplevel Motif 2.1 window.

From O'Reilly Volume Six A, I have seen that in Motif 2.1 XtVaOpenApplication is recommended to create a toplevel Widget.

In this appendix it can be seen how options and XtNumber(options) are used to act on resources via argument list.

I have tried to use it to generate an optional flag -title WINDOW_TITLE while invoking the program, without sucess.

This is what I have tried:

#include <stdlib.h>
#include <stdio.h>
 
#include <Xm/Xm.h>
#include <Xm/PushB.h>
 
static XrmOptionDescRec options[] = {
    { "-title", "XmNtitle", XrmoptionIsArg, NULL },
};
 
int main(int argc, char *argv[]) {

    Widget          toplevel;             /* Top Level Button */
    XtAppContext    app;                  /* Application Context */
    char            *window_title = NULL; /* Top Level Window Title */
    
    /* INITIALIZE TOP LEVEL WINDOW */
    XtSetLanguageProc(NULL, NULL, NULL);
    toplevel = XtVaOpenApplication( &app, argv[0], options, XtNumber(options), &argc, argv, NULL, sessionShellWidgetClass, NULL);
    
    /* REALIZE TOPLEVEL WINDOW AND LAUNCH APPLICATION LOOP */
    XtRealizeWidget(toplevel);
    XtAppMainLoop(app);
    
    return 0;

}

The program compiles but it does not react to -title MYTITLE command line argument.

This is the makefile (works on FreeBSD 12):

test:           test.o
                cc -L/usr/local/lib -O -o test test.o -lXm -lXt -lX11

test.o:         test.c
                cc -I/usr/local/include -c test.c

How can I change the title of the window based on an optional argument named -title?

3
  • I don't know, but I think this parses the command line only. To set the title, you must also act on the parsed values. Commented Jul 10, 2021 at 7:40
  • 2
    AFAIK -title is one of the standard x toolkit command line options and is processed automatically by XtVaOpenApplication. You should not need to specify it in XrmOptionDescRec. Commented Jul 10, 2021 at 9:32
  • That's correct and I was not aware of it. Commented Jul 10, 2021 at 12:12

3 Answers 3

4

The correct xrm option line is

{"-title", ".title", XrmoptionSepArg, NULL}

You don't actually need to specify it because it is in the default Xt option table.

In general you omit XmN when specifying xrm resource names.

Sign up to request clarification or add additional context in comments.

3 Comments

I did not know that -title was part of the default Xt option table. It is not needed as you said. However, I also tried with {"-foo", "title", XrmoptionSepArg, NULL} so you can change the title -as a mere example as it is now clear that it is not needed- with -foo MYTITLE. I have tried removing the dot before the title and it works too. So instead of {"-foo", ".title", XrmoptionSepArg, NULL} it can be {"-foo", "title", XrmoptionSepArg, NULL}. Is there any reason for using the dot?
I'm not sure about the leading dot, looks like it is redundant but it is being used in the X11 sources.
A different but related question about XrmOptionDescRec struct array can be found here: stackoverflow.com/questions/68336568/… Can I use the array of structs to pass non-X11 related parameters? If not, how can I incorporate them into the program?
1

In my Motif programs, I use Xlib directly, since Motif does not seem to handle correctly the UTF8 setting of window titles using XmNtitle property. Most modern window managers expect UTF8 string passed as _NET_MW_NAME.

void setWindowTitleUTF8(Widget w, char *title)
{
 static Atom atoms[3];
 static bool first = TRUE;
 if (first)
  {
   static char *atom_names[] = {"_NET_WM_NAME", "_NET_WM_ICON", "UTF8_STRING"};
   first = FALSE;
   XInternAtoms(XtDisplay(w), atom_names, 3, FALSE, atoms);
  }
 XChangeProperty(XtDisplay(w), XtWindow(w), atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8,
  PropModeReplace, (unsigned char *) title, strlen(title));
}

Make sure you call it on XtParent() of your widget, so it is applied to top level widget shell.

Comments

0

Please investigate further yourself (I'm no X/Motif expert), but sth seems to be off with the argument parsing. Replacing options with NULL ant its size with 0 in call XtVaOpenApplication call seems to do the trick: toplevel = XtVaOpenApplication( &app, argv[0], NULL, 0, &argc, argv, NULL, sessionShellWidgetClass, NULL);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.