Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mditest.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// GUI MDI features
4///
5/// \macro_code
6///
7/// \authors Ilka Antcheva, Fons Rademakers
8
9#include <cstdio>
10#include <cstdlib>
11
12#include <TApplication.h>
13#include <TGClient.h>
14#include <TGFrame.h>
15#include <TGButton.h>
16#include <TGTextEntry.h>
17#include <TGCanvas.h>
18#include <TGMenu.h>
19#include <TGMdi.h>
20#include <TGMsgBox.h>
21#include <TGSlider.h>
22#include <TGListBox.h>
23#include <RQ_OBJECT.h>
24
25enum EMenuIds {
26 M_FILE_NEW,
27 M_FILE_CLOSE,
28 M_FILE_EXIT,
29
30 M_WINDOW_HOR,
31 M_WINDOW_VERT,
32 M_WINDOW_CASCADE,
33 M_WINDOW_OPAQUE,
34 M_WINDOW_ARRANGE,
35
36 M_HELP_CONTENTS,
37 M_HELP_ABOUT
38};
39
40//----------------------------------------------------------------------
41
42class TGMdiTestSubclass {
43
44 RQ_OBJECT("TGMdiTestSubclass")
45
46protected:
47 TGMdiFrame *fMdiFrame;
48 TGCanvas *fCanvasWindow;
49 TGCompositeFrame *fContainer;
50
51public:
52 TGMdiTestSubclass(TGMdiMainFrame *main, int w, int h);
53
54 TGMdiFrame *GetMdiFrame() const { return fMdiFrame; }
55 virtual Bool_t CloseWindow();
56};
57
58class TGMdiHintTest {
59
60 RQ_OBJECT("TGMdiHintTest")
61
62protected:
63 TGMdiFrame *fMdiFrame;
64 TGTextEntry *fWName;
65 TGCheckButton *fClose, *fMenu, *fMin, *fMax, *fSize, *fHelp;
66
67public:
68 TGMdiHintTest(TGMdiMainFrame *main, int w, int h);
69
70 void HandleButtons();
71 void HandleText(const char *);
72};
73
74class TGAppMainFrame {
75
76 RQ_OBJECT("TGAppMainFrame")
77
78protected:
79 TGMainFrame *fMain;
80 TGMdiMainFrame *fMainFrame;
81 TGMdiMenuBar *fMenuBar;
82 TGLayoutHints *fMenuBarItemLayout;
83 TGPopupMenu *fMenuFile, *fMenuWindow, *fMenuHelp;
84
85 void InitMenu();
86 void CloseWindow();
87
88public:
89 TGAppMainFrame(const TGWindow *p, int w, int h);
90
91 void HandleMenu(Int_t id);
92};
93
94//----------------------------------------------------------------------
95
96TGAppMainFrame::TGAppMainFrame(const TGWindow *p, int w, int h)
97{
98 fMain = new TGMainFrame(p, w, h, kVerticalFrame);
99 fMenuBar = new TGMdiMenuBar(fMain, 10, 10);
100 fMain->AddFrame(fMenuBar, new TGLayoutHints(kLHintsTop | kLHintsExpandX));
101
102 fMainFrame = new TGMdiMainFrame(fMain, fMenuBar, 300, 300);
103 fMain->AddFrame(fMainFrame, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
104
105 const TGPicture *pbg = gClient->GetPicture("mditestbg.xpm");
106 if (pbg)
107 fMainFrame->GetContainer()->SetBackgroundPixmap(pbg->GetPicture());
108
109 TGMdiFrame *mdiFrame;
110
111 //--- 1
112
113 TGMdiTestSubclass *t = new TGMdiTestSubclass(fMainFrame, 320, 240);
114 mdiFrame = t->GetMdiFrame();
115 mdiFrame->SetMdiHints(kMdiClose | kMdiMenu);
116 mdiFrame->SetWindowName("One");
117 mdiFrame->MapSubwindows();
118 mdiFrame->Layout();
119
120 //--- 2
121
122 ULong_t ic;
123 gClient->GetColorByName("red", ic);
124 mdiFrame = new TGMdiFrame(fMainFrame, 200, 200, kOwnBackground, ic);
125 mdiFrame->AddFrame(new TGTextButton(mdiFrame, new TGHotString("&Press me!"), 1),
128 mdiFrame->SetWindowName("Two");
129 mdiFrame->MapSubwindows();
130 mdiFrame->Layout();
131 mdiFrame->Move(150, 200);
132
133 //--- 3
134
135 gClient->GetColorByName("green", ic);
136 mdiFrame = new TGMdiFrame(fMainFrame, 200, 200, kOwnBackground, ic);
137 mdiFrame->AddFrame(new TGTextButton(mdiFrame, new TGHotString("Button 1"), 11),
139 mdiFrame->AddFrame(new TGTextButton(mdiFrame, new TGHotString("Button 2"), 12),
142 mdiFrame->SetWindowName("Three");
143 mdiFrame->MapSubwindows();
144 mdiFrame->Layout();
145 mdiFrame->Move(180, 220);
146
147 //--- 4
148
149 gClient->GetColorByName("blue", ic);
150 mdiFrame = new TGMdiFrame(fMainFrame, 200, 400, kOwnBackground, ic);
151
152 TGListBox *fListBox = new TGListBox(mdiFrame, 1);
153 fListBox->AddEntry("Entry 1", 1);
154 fListBox->AddEntry("Entry 2", 2);
155 fListBox->AddEntry("Entry 3", 3);
156 fListBox->AddEntry("Entry 4", 4);
157 fListBox->AddEntry("Entry 5", 5);
158 fListBox->AddEntry("Entry 6", 6);
159 fListBox->AddEntry("Entry 7", 7);
160 fListBox->AddEntry("Entry 8", 8);
161 fListBox->AddEntry("Entry 9", 9);
162 fListBox->Resize(100, 70);
163 fListBox->SetMultipleSelections(kFALSE);
164 mdiFrame->AddFrame(fListBox, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY));
165 mdiFrame->AddFrame(new TGHSlider(mdiFrame, 50, kSlider1, 1, kHorizontalFrame, ic),
167 mdiFrame->Move(400, 300);
168 mdiFrame->SetWindowName("Four");
169 mdiFrame->MapSubwindows();
170 mdiFrame->Layout();
171
172 //--- 5
173
174 new TGMdiHintTest(fMainFrame, 200, 200);
175
176 InitMenu();
177
178 fMain->SetWindowName("MDI test");
179 fMain->SetClassHints("mdi test", "mdi test");
180
181 if (pbg && pbg->GetWidth() > 600 && pbg->GetHeight() > 400)
182 fMain->Resize(pbg->GetWidth(), pbg->GetHeight() + 25);
183 else
184 fMain->Resize(640, 400);
185
186 fMain->MapSubwindows();
187 fMain->MapWindow();
188 fMain->Layout();
189}
190
191void TGAppMainFrame::HandleMenu(Int_t id)
192{
193 // Handle menu items.
194
195 switch (id) {
196 case M_FILE_NEW: new TGMdiFrame(fMainFrame, 200, 100); break;
197
198 case M_FILE_CLOSE: fMainFrame->Close(fMainFrame->GetCurrent()); break;
199
200 case M_FILE_EXIT: CloseWindow(); break;
201
202 case M_WINDOW_HOR: fMainFrame->TileHorizontal(); break;
203
204 case M_WINDOW_VERT: fMainFrame->TileVertical(); break;
205
206 case M_WINDOW_CASCADE: fMainFrame->Cascade(); break;
207
208 case M_WINDOW_ARRANGE: fMainFrame->ArrangeMinimized(); break;
209
210 case M_WINDOW_OPAQUE:
211 if (fMenuWindow->IsEntryChecked(M_WINDOW_OPAQUE)) {
212 fMenuWindow->UnCheckEntry(M_WINDOW_OPAQUE);
213 fMainFrame->SetResizeMode(kMdiNonOpaque);
214 } else {
215 fMenuWindow->CheckEntry(M_WINDOW_OPAQUE);
216 fMainFrame->SetResizeMode(kMdiOpaque);
217 }
218 break;
219
220 default: fMainFrame->SetCurrent(id); break;
221 }
222}
223
224void TGAppMainFrame::InitMenu()
225{
226 fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0);
227
228 fMenuFile = new TGPopupMenu(gClient->GetRoot());
229 fMenuFile->AddEntry(new TGHotString("&New Window"), M_FILE_NEW);
230 fMenuFile->AddEntry(new TGHotString("&Close Window"), M_FILE_CLOSE);
231 fMenuFile->AddSeparator();
232 fMenuFile->AddEntry(new TGHotString("E&xit"), M_FILE_EXIT);
233
234 fMenuWindow = new TGPopupMenu(gClient->GetRoot());
235 fMenuWindow->AddEntry(new TGHotString("Tile &Horizontally"), M_WINDOW_HOR);
236 fMenuWindow->AddEntry(new TGHotString("Tile &Vertically"), M_WINDOW_VERT);
237 fMenuWindow->AddEntry(new TGHotString("&Cascade"), M_WINDOW_CASCADE);
238 fMenuWindow->AddSeparator();
239 fMenuWindow->AddPopup(new TGHotString("&Windows"), fMainFrame->GetWinListMenu());
240 fMenuWindow->AddSeparator();
241 fMenuWindow->AddEntry(new TGHotString("&Arrange icons"), M_WINDOW_ARRANGE);
242 fMenuWindow->AddSeparator();
243 fMenuWindow->AddEntry(new TGHotString("&Opaque resize"), M_WINDOW_OPAQUE);
244
245 fMenuWindow->CheckEntry(M_WINDOW_OPAQUE);
246
247 fMenuHelp = new TGPopupMenu(gClient->GetRoot());
248 fMenuHelp->AddEntry(new TGHotString("&Contents"), M_HELP_CONTENTS);
249 fMenuHelp->AddSeparator();
250 fMenuHelp->AddEntry(new TGHotString("&About"), M_HELP_ABOUT);
251
252 fMenuHelp->DisableEntry(M_HELP_CONTENTS);
253 fMenuHelp->DisableEntry(M_HELP_ABOUT);
254
255 // menu message are handled by the class' HandleMenu() method
256 fMenuFile->Connect("Activated(Int_t)", "TGAppMainFrame", this, "HandleMenu(Int_t)");
257 fMenuWindow->Connect("Activated(Int_t)", "TGAppMainFrame", this, "HandleMenu(Int_t)");
258 fMenuHelp->Connect("Activated(Int_t)", "TGAppMainFrame", this, "HandleMenu(Int_t)");
259
260 fMenuBar->AddPopup(new TGHotString("&File"), fMenuFile, fMenuBarItemLayout);
261 fMenuBar->AddPopup(new TGHotString("&Windows"), fMenuWindow, fMenuBarItemLayout);
262 fMenuBar->AddPopup(new TGHotString("&Help"), fMenuHelp, fMenuBarItemLayout);
263}
264
265void TGAppMainFrame::CloseWindow()
266{
268}
269
270//----------------------------------------------------------------------
271
272TGMdiTestSubclass::TGMdiTestSubclass(TGMdiMainFrame *main, int w, int h)
273{
274 fMdiFrame = new TGMdiFrame(main, w, h);
275 fMdiFrame->Connect("CloseWindow()", "TGMdiTestSubclass", this, "CloseWindow()");
276 fMdiFrame->DontCallClose();
277
278 fCanvasWindow = new TGCanvas(fMdiFrame, 400, 240);
279 fContainer = new TGCompositeFrame(fCanvasWindow->GetViewPort(), 10, 10, kHorizontalFrame | kOwnBackground,
280 fMdiFrame->GetWhitePixel());
281 fContainer->SetLayoutManager(new TGTileLayout(fContainer, 8));
282 fCanvasWindow->SetContainer(fContainer);
283
284 for (int i = 0; i < 256; ++i)
285 fCanvasWindow->AddFrame(new TGFrame(fCanvasWindow->GetContainer(), 32, 32, kOwnBackground, (i + 1) & 255),
287
288 fMdiFrame->AddFrame(fCanvasWindow, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
289
290 fMdiFrame->SetWindowIcon(gClient->GetPicture("ofolder_t.xpm"));
291}
292
293Bool_t TGMdiTestSubclass::CloseWindow()
294{
295 int ret = 0;
296
297 new TGMsgBox(gClient->GetRoot(), fMdiFrame, fMdiFrame->GetWindowName(), "Really want to close the window?",
299
300 if (ret == kMBYes)
301 return fMdiFrame->CloseWindow();
302
303 return kFALSE;
304}
305
306//----------------------------------------------------------------------
307
308TGMdiHintTest::TGMdiHintTest(TGMdiMainFrame *main, int w, int h)
309{
310 fMdiFrame = new TGMdiFrame(main, w, h);
311
312 fClose = new TGCheckButton(fMdiFrame, new TGHotString("Close"), 11);
313 fMenu = new TGCheckButton(fMdiFrame, new TGHotString("Menu (left icon)"), 12);
314 fMin = new TGCheckButton(fMdiFrame, new TGHotString("Minimize"), 13);
315 fMax = new TGCheckButton(fMdiFrame, new TGHotString("Maximize"), 14);
316 fSize = new TGCheckButton(fMdiFrame, new TGHotString("Resize"), 15);
317 fHelp = new TGCheckButton(fMdiFrame, new TGHotString("Help"), 16);
318
319 TGLayoutHints *lh = new TGLayoutHints(kLHintsLeft | kLHintsTop, 5, 100, 5, 0);
320
321 fMdiFrame->AddFrame(fClose, lh);
322 fMdiFrame->AddFrame(fMenu, lh);
323 fMdiFrame->AddFrame(fMin, lh);
324 fMdiFrame->AddFrame(fMax, lh);
325 fMdiFrame->AddFrame(fSize, lh);
326 fMdiFrame->AddFrame(fHelp, lh);
327
328 fClose->SetState(kButtonDown);
329 fMin->SetState(kButtonDown);
330 fMenu->SetState(kButtonDown);
331 fMax->SetState(kButtonDown);
332 fSize->SetState(kButtonDown);
333
334 fClose->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
335 fMenu->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
336 fMin->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
337 fMax->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
338 fSize->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
339 fHelp->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
340
341 fWName = new TGTextEntry(fMdiFrame, (const char *)"", 20);
342 fMdiFrame->AddFrame(fWName, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 5, 5));
343
344 fWName->GetBuffer()->AddText(0, "MDI hints test");
345 fWName->Connect("TextChanged(char*)", "TGMdiHintTest", this, "HandleText(char*)");
346
347 fMdiFrame->SetMdiHints(kMdiDefaultHints);
348 fMdiFrame->SetWindowName(fWName->GetBuffer()->GetString());
349
350 fMdiFrame->SetWindowIcon(gClient->GetPicture("app_t.xpm"));
351
352 fMdiFrame->MapSubwindows();
353 fMdiFrame->Layout();
354}
355
356void TGMdiHintTest::HandleButtons()
357{
358 int hints = 0;
359
360 if (fClose->GetState() != kButtonUp)
361 hints |= kMdiClose;
362 if (fMenu->GetState() != kButtonUp)
363 hints |= kMdiMenu;
364 if (fMin->GetState() != kButtonUp)
365 hints |= kMdiMinimize;
366 if (fMax->GetState() != kButtonUp)
367 hints |= kMdiMaximize;
368 if (fSize->GetState() != kButtonUp)
369 hints |= kMdiSize;
370 if (fHelp->GetState() != kButtonUp)
371 hints |= kMdiHelp;
372
373 fMdiFrame->SetMdiHints(hints);
374}
375
376void TGMdiHintTest::HandleText(const char *)
377{
378 fMdiFrame->SetWindowName(fWName->GetBuffer()->GetString());
379}
380
381void mditest()
382{
383 new TGAppMainFrame(gClient->GetRoot(), 640, 400);
384}
385
386//----------------------------------------------------------------------
387
388#ifdef STANDALONE
389int main(int argc, char **argv)
390{
391 TApplication theApp("MdiTest", &argc, argv);
392
393 mditest();
394
395 theApp.Run();
396
397 return 0;
398}
399#endif
dim_t fSize
@ kVerticalFrame
Definition GuiTypes.h:381
@ kHorizontalFrame
Definition GuiTypes.h:382
@ kOwnBackground
Definition GuiTypes.h:391
int main()
Definition Prototype.cxx:12
#define RQ_OBJECT(sender_class)
Definition RQ_OBJECT.h:87
#define h(i)
Definition RSha256.hxx:106
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
unsigned long ULong_t
Definition RtypesCore.h:55
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
R__EXTERN TApplication * gApplication
@ kButtonDown
Definition TGButton.h:54
@ kButtonUp
Definition TGButton.h:53
#define gClient
Definition TGClient.h:157
@ kLHintsExpandY
Definition TGLayout.h:31
@ kLHintsLeft
Definition TGLayout.h:24
@ kLHintsCenterY
Definition TGLayout.h:28
@ kLHintsNormal
Definition TGLayout.h:32
@ kLHintsCenterX
Definition TGLayout.h:25
@ kLHintsTop
Definition TGLayout.h:27
@ kLHintsExpandX
Definition TGLayout.h:30
@ kMdiNonOpaque
@ kMdiOpaque
@ kMdiMinimize
@ kMdiHelp
@ kMdiMenu
@ kMdiSize
@ kMdiMaximize
@ kMdiClose
@ kMdiDefaultHints
@ kMBNo
Definition TGMsgBox.h:32
@ kMBYes
Definition TGMsgBox.h:31
@ kMBIconExclamation
Definition TGMsgBox.h:24
@ kSlider1
Definition TGSlider.h:30
winID h TVirtualViewer3D TVirtualGLPainter p
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
virtual void Terminate(Int_t status=0)
Terminate the application by call TSystem::Exit() unless application has been told to return from Run...
virtual EButtonState GetState() const
Definition TGButton.h:112
A frame containing two scrollbars (a horizontal and a vertical) and a viewport.
Definition TGCanvas.h:192
Selects different options.
Definition TGButton.h:264
void SetState(EButtonState state, Bool_t emit=kFALSE) override
Set check button state.
The base class for composite widgets (menu bars, list boxes, etc.).
Definition TGFrame.h:287
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1164
void Layout() override
Layout the elements of the composite frame.
Definition TGFrame.cxx:1257
A subclasses of TGWindow, and is used as base class for some simple widgets (buttons,...
Definition TGFrame.h:80
Concrete class for horizontal slider.
Definition TGSlider.h:119
TGHotString is a string with a "hot" character underlined.
Definition TGString.h:42
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
A listbox is a box, possibly with scrollbar, containing entries.
Definition TGListBox.h:221
void Resize(UInt_t w, UInt_t h) override
Resize the listbox widget.
virtual void AddEntry(TGString *s, Int_t id)
Add entry with specified string and id to listbox.
virtual void SetMultipleSelections(Bool_t multi=kTRUE)
Definition TGListBox.h:259
Defines top level windows that interact with the system Window Manager.
Definition TGFrame.h:397
This file contains the TGMdiFrame class.
Definition TGMdiFrame.h:42
virtual void SetMdiHints(ULong_t mdihints)
Set MDI hints, also used to identify titlebar buttons.
void SetWindowName(const char *name) override
Set MDI window name (set titlebar title).
void Move(Int_t x, Int_t y) override
Move MDI window at position x, y.
This file contains the TGMdiMainFrame class.
Bool_t SetCurrent(TGMdiFrameList *newcurrent)
Set current (active) MDI child window (by frame list).
void SetResizeMode(Int_t mode=kMdiDefaultResizeMode)
Set MDI windows resize mode (opaque or transparent).
TGMdiFrame * GetCurrent() const
Return pointer on current (active) MDI child window.
virtual void ArrangeMinimized()
This is an attempt to an "smart" minimized window re-arrangement.
TGPopupMenu * GetWinListMenu() const
virtual void TileHorizontal()
virtual void TileVertical()
virtual void Cascade()
virtual Int_t Close(TGMdiFrame *frame)
Close MDI child window mdiframe.
TGFrame * GetContainer() const
void AddPopup(TGHotString *s, TGPopupMenu *menu, TGLayoutHints *l)
Add popup menu to the MDI menu bar with layout hints l.
Definition TGMdiMenu.cxx:87
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
UInt_t GetHeight() const
Definition TGPicture.h:53
Pixmap_t GetPicture() const
Definition TGPicture.h:54
UInt_t GetWidth() const
Definition TGPicture.h:52
This class creates a popup menu object.
Definition TGMenu.h:110
virtual Bool_t IsEntryChecked(Int_t id)
Return true if menu item is checked.
Definition TGMenu.cxx:1845
virtual void AddPopup(TGHotString *s, TGPopupMenu *popup, TGMenuEntry *before=nullptr, const TGPicture *p=nullptr)
Add a (cascading) popup menu to a popup menu.
Definition TGMenu.cxx:1152
virtual void CheckEntry(Int_t id)
Check a menu entry (i.e. add a check mark in front of it).
Definition TGMenu.cxx:1782
virtual void DisableEntry(Int_t id)
Disable entry (disabled entries appear in a sunken relieve).
Definition TGMenu.cxx:1724
virtual void UnCheckEntry(Int_t id)
Uncheck menu entry (i.e. remove check mark).
Definition TGMenu.cxx:1807
virtual void AddSeparator(TGMenuEntry *before=nullptr)
Add a menu separator to the menu.
Definition TGMenu.cxx:1060
virtual void AddEntry(TGHotString *s, Int_t id, void *ud=nullptr, const TGPicture *p=nullptr, TGMenuEntry *before=nullptr)
Add a menu entry.
Definition TGMenu.cxx:990
void AddText(Int_t pos, const char *text)
const char * GetString() const
Yield an action as soon as it is clicked.
Definition TGButton.h:142
A TGTextEntry is a one line text input widget.
Definition TGTextEntry.h:24
TGTextBuffer * GetBuffer() const
This is a layout manager for the TGListView widget.
Definition TGLayout.h:303
ROOT GUI Window base class.
Definition TGWindow.h:23
virtual void SetBackgroundPixmap(Pixmap_t pixmap)
set background pixmap
Definition TGWindow.cxx:248
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition TQObject.cxx:869