Logo ROOT  
Reference Guide
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 <stdio.h>
10#include <stdlib.h>
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//----------------------------------------------------------------------
42
43class TGMdiTestSubclass {
44
45RQ_OBJECT("TGMdiTestSubclass")
46
47protected:
48 TGMdiFrame *fMdiFrame;
49 TGCanvas *fCanvasWindow;
50 TGCompositeFrame *fContainer;
51
52public:
53 TGMdiTestSubclass(TGMdiMainFrame *main, int w, int h);
54
55 TGMdiFrame *GetMdiFrame() const { return fMdiFrame; }
56 virtual Bool_t CloseWindow();
57};
58
59class TGMdiHintTest {
60
61RQ_OBJECT("TGMdiHintTest")
62
63protected:
64 TGMdiFrame *fMdiFrame;
65 TGTextEntry *fWName;
66 TGCheckButton *fClose, *fMenu, *fMin, *fMax, *fSize, *fHelp;
67
68public:
69 TGMdiHintTest(TGMdiMainFrame *main, int w, int h);
70
71 void HandleButtons();
72 void HandleText(const char *);
73};
74
75class TGAppMainFrame {
76
77RQ_OBJECT("TGAppMainFrame")
78
79protected:
80 TGMainFrame *fMain;
81 TGMdiMainFrame *fMainFrame;
82 TGMdiMenuBar *fMenuBar;
83 TGLayoutHints *fMenuBarItemLayout;
84 TGPopupMenu *fMenuFile, *fMenuWindow, *fMenuHelp;
85
86 void InitMenu();
87 void CloseWindow();
88
89public:
90 TGAppMainFrame(const TGWindow *p, int w, int h);
91
92 void HandleMenu(Int_t id);
93};
94
95//----------------------------------------------------------------------
96
97TGAppMainFrame::TGAppMainFrame(const TGWindow *p, int w, int h)
98{
99 fMain = new TGMainFrame(p, w, h, kVerticalFrame);
100 fMenuBar = new TGMdiMenuBar(fMain, 10, 10);
101 fMain->AddFrame(fMenuBar, new TGLayoutHints(kLHintsTop | kLHintsExpandX));
102
103 fMainFrame = new TGMdiMainFrame(fMain, fMenuBar, 300, 300);
104 fMain->AddFrame(fMainFrame, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
105
106 const TGPicture *pbg = gClient->GetPicture("mditestbg.xpm");
107 if (pbg)
108 fMainFrame->GetContainer()->SetBackgroundPixmap(pbg->GetPicture());
109
110 TGMdiFrame *mdiFrame;
111
112 //--- 1
113
114 TGMdiTestSubclass *t = new TGMdiTestSubclass(fMainFrame, 320, 240);
115 mdiFrame = t->GetMdiFrame();
116 mdiFrame->SetMdiHints(kMdiClose | kMdiMenu);
117 mdiFrame->SetWindowName("One");
118 mdiFrame->MapSubwindows();
119 mdiFrame->Layout();
120
121 //--- 2
122
123 ULong_t ic;
124 gClient->GetColorByName("red", ic);
125 mdiFrame = new TGMdiFrame(fMainFrame, 200, 200,
126 kOwnBackground, ic);
127 mdiFrame->AddFrame(new TGTextButton(mdiFrame, new TGHotString("&Press me!"), 1),
130 mdiFrame->SetWindowName("Two");
131 mdiFrame->MapSubwindows();
132 mdiFrame->Layout();
133 mdiFrame->Move(150, 200);
134
135 //--- 3
136
137 gClient->GetColorByName("green", ic);
138 mdiFrame = new TGMdiFrame(fMainFrame, 200, 200, kOwnBackground, ic);
139 mdiFrame->AddFrame(new TGTextButton(mdiFrame, new TGHotString("Button 1"), 11),
141 mdiFrame->AddFrame(new TGTextButton(mdiFrame, new TGHotString("Button 2"), 12),
144 mdiFrame->SetWindowName("Three");
145 mdiFrame->MapSubwindows();
146 mdiFrame->Layout();
147 mdiFrame->Move(180, 220);
148
149 //--- 4
150
151 gClient->GetColorByName("blue", ic);
152 mdiFrame = new TGMdiFrame(fMainFrame, 200, 400, kOwnBackground, ic);
153
154 TGListBox *fListBox = new TGListBox(mdiFrame,1);
155 fListBox->AddEntry("Entry 1", 1);
156 fListBox->AddEntry("Entry 2", 2);
157 fListBox->AddEntry("Entry 3", 3);
158 fListBox->AddEntry("Entry 4", 4);
159 fListBox->AddEntry("Entry 5", 5);
160 fListBox->AddEntry("Entry 6", 6);
161 fListBox->AddEntry("Entry 7", 7);
162 fListBox->AddEntry("Entry 8", 8);
163 fListBox->AddEntry("Entry 9", 9);
164 fListBox->Resize(100,70);
165 fListBox->SetMultipleSelections(kFALSE);
166 mdiFrame->AddFrame(fListBox,
168 mdiFrame->AddFrame(new TGHSlider(mdiFrame, 50, kSlider1, 1,
169 kHorizontalFrame, ic),
171 mdiFrame->Move(400, 300);
172 mdiFrame->SetWindowName("Four");
173 mdiFrame->MapSubwindows();
174 mdiFrame->Layout();
175
176 //--- 5
177
178 new TGMdiHintTest(fMainFrame, 200, 200);
179
180 InitMenu();
181
182 fMain->SetWindowName("MDI test");
183 fMain->SetClassHints("mdi test", "mdi test");
184
185 if (pbg && pbg->GetWidth() > 600 && pbg->GetHeight() > 400)
186 fMain->Resize(pbg->GetWidth(), pbg->GetHeight()+25);
187 else
188 fMain->Resize(640, 400);
189
190 fMain->MapSubwindows();
191 fMain->MapWindow();
192 fMain->Layout();
193}
194
195void TGAppMainFrame::HandleMenu(Int_t id)
196{
197 // Handle menu items.
198
199 switch (id) {
200 case M_FILE_NEW:
201 new TGMdiFrame(fMainFrame, 200, 100);
202 break;
203
204 case M_FILE_CLOSE:
205 fMainFrame->Close(fMainFrame->GetCurrent());
206 break;
207
208 case M_FILE_EXIT:
209 CloseWindow();
210 break;
211
212 case M_WINDOW_HOR:
213 fMainFrame->TileHorizontal();
214 break;
215
216 case M_WINDOW_VERT:
217 fMainFrame->TileVertical();
218 break;
219
220 case M_WINDOW_CASCADE:
221 fMainFrame->Cascade();
222 break;
223
224 case M_WINDOW_ARRANGE:
225 fMainFrame->ArrangeMinimized();
226 break;
227
228 case M_WINDOW_OPAQUE:
229 if (fMenuWindow->IsEntryChecked(M_WINDOW_OPAQUE)) {
230 fMenuWindow->UnCheckEntry(M_WINDOW_OPAQUE);
231 fMainFrame->SetResizeMode(kMdiNonOpaque);
232 } else {
233 fMenuWindow->CheckEntry(M_WINDOW_OPAQUE);
234 fMainFrame->SetResizeMode(kMdiOpaque);
235 }
236 break;
237
238 default:
239 fMainFrame->SetCurrent(id);
240 break;
241 }
242}
243
244void TGAppMainFrame::InitMenu()
245{
246 fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0);
247
248 fMenuFile = new TGPopupMenu(gClient->GetRoot());
249 fMenuFile->AddEntry(new TGHotString("&New Window"), M_FILE_NEW);
250 fMenuFile->AddEntry(new TGHotString("&Close Window"), M_FILE_CLOSE);
251 fMenuFile->AddSeparator();
252 fMenuFile->AddEntry(new TGHotString("E&xit"), M_FILE_EXIT);
253
254 fMenuWindow = new TGPopupMenu(gClient->GetRoot());
255 fMenuWindow->AddEntry(new TGHotString("Tile &Horizontally"), M_WINDOW_HOR);
256 fMenuWindow->AddEntry(new TGHotString("Tile &Vertically"), M_WINDOW_VERT);
257 fMenuWindow->AddEntry(new TGHotString("&Cascade"), M_WINDOW_CASCADE);
258 fMenuWindow->AddSeparator();
259 fMenuWindow->AddPopup(new TGHotString("&Windows"), fMainFrame->GetWinListMenu());
260 fMenuWindow->AddSeparator();
261 fMenuWindow->AddEntry(new TGHotString("&Arrange icons"), M_WINDOW_ARRANGE);
262 fMenuWindow->AddSeparator();
263 fMenuWindow->AddEntry(new TGHotString("&Opaque resize"), M_WINDOW_OPAQUE);
264
265 fMenuWindow->CheckEntry(M_WINDOW_OPAQUE);
266
267 fMenuHelp = new TGPopupMenu(gClient->GetRoot());
268 fMenuHelp->AddEntry(new TGHotString("&Contents"), M_HELP_CONTENTS);
269 fMenuHelp->AddSeparator();
270 fMenuHelp->AddEntry(new TGHotString("&About"), M_HELP_ABOUT);
271
272 fMenuHelp->DisableEntry(M_HELP_CONTENTS);
273 fMenuHelp->DisableEntry(M_HELP_ABOUT);
274
275 // menu message are handled by the class' HandleMenu() method
276 fMenuFile->Connect("Activated(Int_t)", "TGAppMainFrame", this,
277 "HandleMenu(Int_t)");
278 fMenuWindow->Connect("Activated(Int_t)", "TGAppMainFrame", this,
279 "HandleMenu(Int_t)");
280 fMenuHelp->Connect("Activated(Int_t)", "TGAppMainFrame", this,
281 "HandleMenu(Int_t)");
282
283 fMenuBar->AddPopup(new TGHotString("&File"), fMenuFile, fMenuBarItemLayout);
284 fMenuBar->AddPopup(new TGHotString("&Windows"),fMenuWindow,fMenuBarItemLayout);
285 fMenuBar->AddPopup(new TGHotString("&Help"), fMenuHelp, fMenuBarItemLayout);
286}
287
288void TGAppMainFrame::CloseWindow()
289{
291}
292
293//----------------------------------------------------------------------
294
295TGMdiTestSubclass::TGMdiTestSubclass(TGMdiMainFrame *main, int w, int h)
296{
297 fMdiFrame = new TGMdiFrame(main, w, h);
298 fMdiFrame->Connect("CloseWindow()", "TGMdiTestSubclass", this, "CloseWindow()");
299 fMdiFrame->DontCallClose();
300
301 fCanvasWindow = new TGCanvas(fMdiFrame, 400, 240);
302 fContainer = new TGCompositeFrame(fCanvasWindow->GetViewPort(), 10, 10,
304 fMdiFrame->GetWhitePixel());
305 fContainer->SetLayoutManager(new TGTileLayout(fContainer, 8));
306 fCanvasWindow->SetContainer(fContainer);
307
308 for (int i = 0; i < 256; ++i)
309 fCanvasWindow->AddFrame(new TGFrame(fCanvasWindow->GetContainer(),
310 32, 32, kOwnBackground, (i+1) & 255),
312
313 fMdiFrame->AddFrame(fCanvasWindow, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
314
315 fMdiFrame->SetWindowIcon(gClient->GetPicture("ofolder_t.xpm"));
316}
317
318Bool_t TGMdiTestSubclass::CloseWindow()
319{
320 int ret = 0;
321
322 new TGMsgBox(gClient->GetRoot(), fMdiFrame,
323 fMdiFrame->GetWindowName(), "Really want to close the window?",
325
326 if (ret == kMBYes) return fMdiFrame->CloseWindow();
327
328 return kFALSE;
329}
330
331
332//----------------------------------------------------------------------
333
334TGMdiHintTest::TGMdiHintTest(TGMdiMainFrame *main, int w, int h)
335{
336 fMdiFrame = new TGMdiFrame(main, w, h);
337
338 fClose = new TGCheckButton(fMdiFrame, new TGHotString("Close"), 11);
339 fMenu = new TGCheckButton(fMdiFrame, new TGHotString("Menu (left icon)"), 12);
340 fMin = new TGCheckButton(fMdiFrame, new TGHotString("Minimize"), 13);
341 fMax = new TGCheckButton(fMdiFrame, new TGHotString("Maximize"), 14);
342 fSize = new TGCheckButton(fMdiFrame, new TGHotString("Resize"), 15);
343 fHelp = new TGCheckButton(fMdiFrame, new TGHotString("Help"), 16);
344
345 TGLayoutHints *lh = new TGLayoutHints(kLHintsLeft | kLHintsTop, 5, 100, 5, 0);
346
347 fMdiFrame->AddFrame(fClose, lh);
348 fMdiFrame->AddFrame(fMenu, lh);
349 fMdiFrame->AddFrame(fMin, lh);
350 fMdiFrame->AddFrame(fMax, lh);
351 fMdiFrame->AddFrame(fSize, lh);
352 fMdiFrame->AddFrame(fHelp, lh);
353
354 fClose->SetState(kButtonDown);
355 fMin->SetState(kButtonDown);
356 fMenu->SetState(kButtonDown);
357 fMax->SetState(kButtonDown);
358 fSize->SetState(kButtonDown);
359
360 fClose->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
361 fMenu->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
362 fMin->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
363 fMax->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
364 fSize->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
365 fHelp->Connect("Clicked()", "TGMdiHintTest", this, "HandleButtons()");
366
367 fWName = new TGTextEntry(fMdiFrame, (const char *)"", 20);
368 fMdiFrame->AddFrame(fWName, new TGLayoutHints(kLHintsTop | kLHintsExpandX,
369 5, 5, 5, 5));
370
371 fWName->GetBuffer()->AddText(0, "MDI hints test");
372 fWName->Connect("TextChanged(char*)", "TGMdiHintTest", this, "HandleText(char*)");
373
374 fMdiFrame->SetMdiHints(kMdiDefaultHints);
375 fMdiFrame->SetWindowName(fWName->GetBuffer()->GetString());
376
377 fMdiFrame->SetWindowIcon(gClient->GetPicture("app_t.xpm"));
378
379 fMdiFrame->MapSubwindows();
380 fMdiFrame->Layout();
381}
382
383void TGMdiHintTest::HandleButtons()
384{
385 int hints = 0;
386
387 if (fClose->GetState() != kButtonUp) hints |= kMdiClose;
388 if (fMenu->GetState() != kButtonUp) hints |= kMdiMenu;
389 if (fMin->GetState() != kButtonUp) hints |= kMdiMinimize;
390 if (fMax->GetState() != kButtonUp) hints |= kMdiMaximize;
391 if (fSize->GetState() != kButtonUp) hints |= kMdiSize;
392 if (fHelp->GetState() != kButtonUp) hints |= kMdiHelp;
393
394 fMdiFrame->SetMdiHints(hints);
395}
396
397void TGMdiHintTest::HandleText(const char *)
398{
399 fMdiFrame->SetWindowName(fWName->GetBuffer()->GetString());
400}
401
402void mditest()
403{
404 new TGAppMainFrame(gClient->GetRoot(), 640, 400);
405}
406
407//----------------------------------------------------------------------
408
409#ifdef STANDALONE
410int main(int argc, char **argv)
411{
412 TApplication theApp("MdiTest", &argc, argv);
413
414 mditest();
415
416 theApp.Run();
417
418 return 0;
419}
420#endif
size_t fSize
@ kVerticalFrame
Definition: GuiTypes.h:381
@ kHorizontalFrame
Definition: GuiTypes.h:382
@ kOwnBackground
Definition: GuiTypes.h:391
#define RQ_OBJECT(sender_class)
Definition: RQ_OBJECT.h:87
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
unsigned long ULong_t
Definition: RtypesCore.h:53
bool Bool_t
Definition: RtypesCore.h:61
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
@ kButtonDown
Definition: TGButton.h:54
@ kButtonUp
Definition: TGButton.h:53
#define gClient
Definition: TGClient.h:166
@ kLHintsExpandY
Definition: TGLayout.h:38
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsCenterY
Definition: TGLayout.h:35
@ kLHintsNormal
Definition: TGLayout.h:39
@ kLHintsCenterX
Definition: TGLayout.h:32
@ kLHintsTop
Definition: TGLayout.h:34
@ kLHintsExpandX
Definition: TGLayout.h:37
@ kMdiNonOpaque
@ kMdiOpaque
@ kMdiMinimize
@ kMdiHelp
@ kMdiMenu
@ kMdiSize
@ kMdiMaximize
@ kMdiClose
@ kMdiDefaultHints
@ kMBNo
Definition: TGMsgBox.h:43
@ kMBYes
Definition: TGMsgBox.h:42
@ kMBIconExclamation
Definition: TGMsgBox.h:35
@ kSlider1
Definition: TGSlider.h:56
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
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
virtual void SetState(EButtonState state, Bool_t emit=kFALSE)
Set check button state.
Definition: TGButton.cxx:1202
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1101
virtual void Layout()
Layout the elements of the composite frame.
Definition: TGFrame.cxx:1241
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1148
virtual void Resize(UInt_t w, UInt_t h)
Resize the listbox widget.
Definition: TGListBox.cxx:1420
virtual void AddEntry(TGString *s, Int_t id)
Add entry with specified string and id to listbox.
Definition: TGListBox.cxx:1212
virtual void SetMultipleSelections(Bool_t multi=kTRUE)
Definition: TGListBox.h:327
virtual void Move(Int_t x, Int_t y)
Move MDI window at position x, y.
Definition: TGMdiFrame.cxx:136
virtual void SetMdiHints(ULong_t mdihints)
Set MDI hints, also used to identify titlebar buttons.
Definition: TGMdiFrame.cxx:93
void SetWindowName(const char *name)
Set MDI window name (set titlebar title).
Definition: TGMdiFrame.cxx:102
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:85
UInt_t GetHeight() const
Definition: TGPicture.h:64
Pixmap_t GetPicture() const
Definition: TGPicture.h:65
UInt_t GetWidth() const
Definition: TGPicture.h:63
virtual void AddPopup(TGHotString *s, TGPopupMenu *popup, TGMenuEntry *before=0, const TGPicture *p=0)
Add a (cascading) popup menu to a popup menu.
Definition: TGMenu.cxx:1150
virtual Bool_t IsEntryChecked(Int_t id)
Return true if menu item is checked.
Definition: TGMenu.cxx:1843
virtual void AddEntry(TGHotString *s, Int_t id, void *ud=0, const TGPicture *p=0, TGMenuEntry *before=0)
Add a menu entry.
Definition: TGMenu.cxx:988
virtual void AddSeparator(TGMenuEntry *before=0)
Add a menu separator to the menu.
Definition: TGMenu.cxx:1058
virtual void CheckEntry(Int_t id)
Check a menu entry (i.e. add a check mark in front of it).
Definition: TGMenu.cxx:1780
virtual void DisableEntry(Int_t id)
Disable entry (disabled entries appear in a sunken relieve).
Definition: TGMenu.cxx:1722
virtual void UnCheckEntry(Int_t id)
Uncheck menu entry (i.e. remove check mark).
Definition: TGMenu.cxx:1805
void AddText(Int_t pos, const char *text)
Definition: TGTextBuffer.h:49
const char * GetString() const
Definition: TGTextBuffer.h:47
TGTextBuffer * GetBuffer() const
Definition: TGTextEntry.h:127
virtual void SetBackgroundPixmap(Pixmap_t pixmap)
set background pixmap
Definition: TGWindow.cxx:238
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:866
int main(int argc, char **argv)