9. The application's shell
Chapter 8 gave the studio a window. This chapter gives it the shell the next chapters fill: menus in the title bar with their keyboard shortcuts, a bar of workspace buttons, four panels docked side by side in a layout that is kept between runs, and a look and a palette chosen from the View menu.
The whole program: tut09_shell.F90.
What this chapter adds to chapter 8's program, tut08_window.F90:
draw_menus: The menus, in the title bar, and their shortcutsdraw_tools: The workspace buttons, in a bar along the left sidedraw_panels: The dock space and the four panels; each panel says what the next chapters put in itdraw_dialogs: The questions and boxes the menus and the frame open, at the top levelset_look: The look (the geometry) changes; the palette staysset_palette: The palette (the colours) changes; the look stays- changed:
draw_studio,open_window
Menus and shortcuts
if (ig_begin_main_menu_bar()) then
if (ig_begin_menu('File')) then
if (ig_menu_item(ICON_POWER_SQUARE//' Quit', shortcut='Ctrl+Q')) ask_quit = .true.
call ig_end_menu()
endif
if (ig_begin_menu('View')) then
do k = 1_int32, size(PANELS, kind=int32)
if (ig_menu_item(trim(PANELS(k)), selected=shown(k))) continue ! a pick toggles the check mark
enddo
call ig_separator()
if (ig_begin_menu(ICON_LAYOUT//' Look')) then
do k = 1_int32, size(LOOKS, kind=int32)
on = look == LOOKS(k)
if (ig_menu_item(trim(LOOKS(k)), selected=on)) call set_look(trim(LOOKS(k)))
enddo
call ig_end_menu()
endif
if (ig_begin_menu(ICON_PALETTE//' Palette')) then
do k = 1_int32, size(PALETTES, kind=int32)
on = palette == PALETTES(k)
if (ig_menu_item(trim(PALETTES(k)), selected=on)) call set_palette(trim(PALETTES(k)))
enddo
call ig_end_menu()
endif
call ig_separator()
if (ig_menu_item('Reset layout', enabled=have_default)) call app%load_layout(DEFAULT_LAYOUT)
call ig_end_menu()
endif
if (ig_begin_menu('Help')) then
if (ig_menu_item(ICON_INFO//' About', shortcut='F1')) about = .true.
call ig_end_menu()
endif
call ig_end_main_menu_bar()
endif
if (ig_shortcut('ctrl+q')) ask_quit = .true. ! every frame, outside the menus: cmd+q on macOS
if (ig_shortcut('f1')) about = .true.ig_begin_menu(label)andig_end_menu()are a menu, or a submenu inside one (Look, Palette);ig_end_menufollows only a begin that returned.true..ig_menu_item(label, shortcut=, selected=, enabled=)is.true.on the frame it is picked. Withselectedit shows a check mark, and a pick toggles the variable: the panels' items show and hide the panels directly. For a choice of one among several (the look) the program passes a copy,on, and changes the choice itself.enabled=.false.greys an item out: Reset layout until there is a layout to go back to.shortcut='Ctrl+Q'is only the text on the right of the item. The keys are read byig_shortcut('ctrl+q'), every frame and outside the menus, since the menu is closed most of the time. On macOSctrlis the cmd key, as for every shortcut of Dear ImGui (Shortcuts).- The labels carry icons: an
ICON_*constant is a UTF-8 string, joined to the text with//.
A bar of workspace buttons
if (ig_begin_side_bar('tools', 'left', 76.0_real64)) then ! left out of the dock space
do k = 1_int32, size(PANELS, kind=int32)
if (ig_tool_button(trim(PANELS(k)), trim(ICONS(k)), width=60.0_real64, selected=current == k)) then
current = k
shown(k) = .true.
if (ig_focus_window(trim(PANELS(k)))) continue ! a docked panel: its tab comes forward
endif
call ig_tooltip('Show the '//trim(PANELS(k))//' panel')
enddo
endif
call ig_end()ig_begin_side_bar('tools', 'left', 76)is a bar along the left side of the window, below the title bar: a window without title, not movable, which the dock space leaves free (A bar along a side). It is always ended withig_end.ig_tool_button(label, icon, selected=)is a workspace button, an icon above its label; the selected one is framed in the accent colour.ig_tooltipexplains the button under the pointer.ig_focus_window(name)brings a panel forward; for a docked panel, its tab.
Docked panels, kept between runs
if (save_default) then ! the frame after the layout was set
call app%save_layout(DEFAULT_LAYOUT, comment='the studio as the program lays it out')
if (ig_focus_window('Input')) continue ! of the two tabs, Input in front
have_default = .true.
save_default = .false.
endif
root = ig_dock_id('studio')
if (ig_dock_layout_begin(root)) then ! no layout yet: the first run, or no ini file
call ig_dock_split(root, 'left', 0.25_real64, left, right)
call ig_dock_split(right, 'down', 0.35_real64, run, work)
call ig_dock_window('Case', left)
call ig_dock_window('Result', work)
call ig_dock_window('Input', work) ! the same node: Result and Input are tabs
call ig_dock_window('Run', run)
call ig_dock_layout_end(root)
save_default = .true.
endif
call ig_dock_space(root) ! every frame, after the bars, before the panelsif (shown(1)) then
if (ig_begin('Case', open=shown(1))) call ig_text_disabled('No case open.') ! open: the tab gets a close button
call ig_end()
endifig_dock_id('studio')is the dock space's id: the same name gives the same id in every frame and every run (A dock space).ig_dock_layout_end(root)finishes the layout begun byig_dock_layout_begin(root)(below), inside theif.ig_dock_space(root)fills what the title bar and the side bar leave with a dock space, every frame and before the panels. The user drags panels by their tabs to dock them elsewhere, side by side or as tabs, or out to float.ig_dock_layout_begin(root)is.true.when the dock space has no layout yet: the program then splits it (ig_dock_split: a fraction of the node on one side) and puts each panel into a node (ig_dock_window); Input and Result share a node, as two tabs.ini_file='tut09_shell.ini'inapp%init(below) makes Dear ImGui keep the layout in that file: the user's arrangement comes back at the next run, andig_dock_layout_beginis then.false..app%save_layoutwrites the layout the program set, on the frame after it was set, and View > Reset layout loads it back withapp%load_layout: the user's arrangement is undone, the program's restored (Save and load a layout).ig_begin('Case', open=shown(1))gives the panel's tab a close button, which setsshown(1)to.false.: the program stops drawing the panel, and View > Case or the Case button bring it back.
call app%init('FIGURE studio', 1100_int32, 680_int32, decorations='figure', buttons=side, &
ini_file='tut09_shell.ini') ! Dear ImGui keeps the panels' layout here, between runsLooks and palettes
call app%set_style(look=name) ! rounding, padding, spacing: the colours stay
look = nameA style has two parts, chosen separately (Looks and palettes): the look, the geometry (rounding, padding, spacing), and the palette, the colours. app%set_style(look=name) changes the first and keeps the colours; app%set_style(palette=name) the second and keeps the geometry. The title bar drawn by FIGURE takes the palette's colours with the rest. The names are passed trimmed: 'fluent ', with the blank of the array's padding, is not a look.
About
if (about) then
call ig_open_popup('About the studio')
about = .false.
endif
call ig_set_next_window(400.0_real64, 230.0_real64, 320.0_real64, 160.0_real64) ! where it opens, and its size
if (ig_begin_popup_modal('About the studio')) then
call ig_text(ICON_FLAME//' FIGURE studio')
call ig_text_wrapped('The solver studio of FIGURE''s tutorial: a case, its input, a run of the solver and '// &
'its result, in one window.')
call ig_text('Look: '//trim(look)//', palette: '//trim(palette))
if (ig_button('Close')) call ig_close_current_popup()
call ig_end_popup()
endifThe About box is a modal opened at the top level, as chapter 8's question; ig_set_next_window places it and sizes it the first time it appears, and ig_text_wrapped wraps its text at the box's edge.
What you learned
Menus and submenus with check marks, disabled items and icons; shortcuts read every frame; a side bar of workspace buttons; panels docked in a layout the program sets, the user changes, Dear ImGui keeps between runs and the program restores; looks and palettes. Reference: Menus, popups and shortcuts, Docking, Styles, Widgets.
Run it: build it as chapter 7 shows, then ./tut09_shell.
Next: 10. The case: opening a case from FIGURE's file browser or by a drop, and asking before changes are lost.