M.E.
M.E.

Reputation: 5495

Setting the title of a Motif Window

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?

Upvotes: 1

Views: 372

Answers (3)

user1336365
user1336365

Reputation: 91

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.

Upvotes: 1

n. m. could be an AI
n. m. could be an AI

Reputation: 120001

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.

Upvotes: 4

alagner
alagner

Reputation: 4062

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);

Upvotes: 0

Related Questions