Resizing Windows Programmatically

We’re not done with our Calculator — we still haven’t built a system for entering the hexadecimal “numbers” A, B, C, D, E, and F. Probably the easiest way to enter these hex numbers is to add another six buttons to the keypad and put the letters on them. (Naturally, these buttons will have the tags 10-15.) Because we don’t need these buttons to be displayed all the time, our Cocoa Calculator will do something that no physical calculator can do: it will make itself bigger when it is in hex mode (to make room for the extra buttons), and then make itself smaller when they are no longer needed (i.e., in other bases).

To accomplish this magic, we need to learn more about how the NSMatrix, NSCell, and NSWindow classes work:

  • When we want to make our Calculator window bigger, the first question to ask is “How much bigger?” We’ll need to insert space for two more rows of buttons (six new hex-only buttons in total). Each NSMatrix knows the size of its cells as well as the spacing between cells. We’ll need to query our NSMatrix to find out how much larger the NSWindow needs to be in order to hold two more rows of buttons.

  • After we know how much space to add, we’ll need to resize the window and make sure that every object in the window moves to the appropriate place during the resize operation.

  • After the window is resized, we’ll need to create the six additional buttons that we want and set their tags appropriately.

  • Finally, when we make the Calculator window ...

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.