Programming with GraphApp

How can I change fonts on Labels, Buttons etc

There is a function called settextfont which does this:
    title = newlabel(...);
    settextfont(title, newfont("Times", Bold, 18));
  
The example given above illustrates how to set a label's text font to be a bold Times 18-pixel tall font. Note that settextfont may not work properly in MS-Windows for anything but labels, due to limitations within that operating system. It should work for most widgets under X-Windows.

I've created a Label but the name doesn't appear within it. Why?

If the label is too small, the text may 'wrap' to the next line, which is off the bottom of the label's drawable area. Try making your label bigger.

Another thing that can happen is the label might be oscured by another control. Try moving nearby controls away and see if that improves things.

Why don't the fonts work properly?

Sometimes when you attempt to get a font using the newfont function, the return value will either be NULL or the font won't be the correct size. The newfont function tries its best to give you the font you've requested, but sometimes it fails.

There are many reasons for this. Which fonts and font sizes are installed on your machine may not be the same as on someone else's machine, so writing portable code which relies on font sizes can be a problem.

Solution? Some guidelines:

When I change the background colour of a window, the labels appear inside an ugly white rectangle. Why?

This bug only appears when using light colours in the MS-Windows version of the library. It is a bug in Windows itself, due to the way Windows uses solid colour brushes to paint the backgrounds of buttons, labels, radio buttons and checkboxes.

Currently the only solution to this problem is to fiddle with the background colours of your windows until it looks nice.

How do you get gprintf to print to the correct window?

There are two ways: you can do all your drawing in a window's 'redraw function', or you can use the drawto function.

If you use a 'redraw function', the window calls the specified function every time the window needs to be drawn. Hence, the window will always look good. You can use gprintf inside your call-back:

    void draw_window_1(window w, rect r)
    {
      gprintf("First window!\n");
    }

    void draw_window_2(window w, rect r)
    {
      gprintf("Second window!\n");
    }

    void main(void)
    {
      window w1, w2;

      w1 = newwindow("First", rect(10,10,100,100), StandardWindow);
      setredraw(w1, draw_window_1);
      show(w1);

      w2 = newwindow("Second", rect(10,10,200,100), StandardWindow);
      setredraw(w2, draw_window_2);
      show(w2);
    }
  
Whenever either window is drawn, the appropriate function is called. Alternately, you can use the drawto function to choose which window to draw to:
    window w1, w2;

    void main(void)
    {
      w1 = newwindow("First", rect(10,10,100,100), StandardWindow);
      show(w1);

      w2 = newwindow("Second", rect(10,10,200,100), StandardWindow);
      show(w2);

      drawto(w1);
      gprintf("First window!\n");

      drawto(w2);
      gprintf("Second window!\n");
    }
  
Each call to drawto can be followed by many calls to gprintf or any of the other drawing functions. The drawto function is not necessary when using redraw functions (first example).

How can I add more controls to a window after creating another window?

Use the addto function.

After creating a window, the library adds any new controls you create to that window. If you create a second window, the new controls will be added to this second window, because it was created more recently.

To add more controls to the first window you need to call the addto function, and supply as its parameter the first window. Then create the new controls.

How can I have multiple lines of text in a button or checkbox?

There is no easy way to do this. MS-Windows will replace newline characters with an ugly vertical black line in buttons, checkboxes and radio buttons, because there is an assumption in that operating system that no-one would ever want to have multiple lines of text in a button.

Similarly, Motif will strip the newlines out of a text string used in buttons. Only the Athena version will properly allow multiple lines of text, and only because I wrote the code for the buttons myself.

The short answer is: don't do it. Your code won't be portable.