From 2a358fe514c64cd244915ce511bfb68cbb4f24a4 Mon Sep 17 00:00:00 2001
From: ejno <none>
Date: Fri, 28 Jun 2013 12:55:11 +0000
Subject: [PATCH] WindowPolicy approach for reusing Untitled document numbers
---
src/apps/stylededit/StyledEditApp.cpp | 3 +-
src/apps/stylededit/StyledEditApp.h | 1 -
src/apps/stylededit/StyledEditWindow.cpp | 10 ++--
src/apps/stylededit/StyledEditWindow.h | 9 +++-
src/apps/stylededit/WindowPolicy.h | 79 ++++++++++++++++++++++++++++++
5 files changed, 93 insertions(+), 9 deletions(-)
create mode 100644 src/apps/stylededit/WindowPolicy.h
diff --git a/src/apps/stylededit/StyledEditApp.cpp b/src/apps/stylededit/StyledEditApp.cpp
index 0460af8..5782cda 100644
a
|
b
|
StyledEditApp::StyledEditApp()
|
128 | 128 | fOpenPanelEncodingMenu = NULL; |
129 | 129 | |
130 | 130 | fWindowCount = 0; |
131 | | fNextUntitledWindow = 1; |
132 | 131 | fBadArguments = false; |
133 | 132 | } |
134 | 133 | |
… |
… |
void
|
172 | 171 | StyledEditApp::OpenDocument() |
173 | 172 | { |
174 | 173 | cascade(); |
175 | | new StyledEditWindow(gWindowRect, fNextUntitledWindow++, fOpenAsEncoding); |
| 174 | new StyledEditWindow(gWindowRect, fOpenAsEncoding); |
176 | 175 | fWindowCount++; |
177 | 176 | } |
178 | 177 | |
diff --git a/src/apps/stylededit/StyledEditApp.h b/src/apps/stylededit/StyledEditApp.h
index 71f1f74..0de971f 100644
a
|
b
|
private:
|
49 | 49 | BMenu* fOpenPanelEncodingMenu; |
50 | 50 | uint32 fOpenAsEncoding; |
51 | 51 | int32 fWindowCount; |
52 | | int32 fNextUntitledWindow; |
53 | 52 | bool fBadArguments; |
54 | 53 | }; |
55 | 54 | |
diff --git a/src/apps/stylededit/StyledEditWindow.cpp b/src/apps/stylededit/StyledEditWindow.cpp
index 4eee321..89d82cd 100644
a
|
b
|
bs_printf(BString* string, const char* format, ...)
|
79 | 79 | // #pragma mark - |
80 | 80 | |
81 | 81 | |
82 | | StyledEditWindow::StyledEditWindow(BRect frame, int32 id, uint32 encoding) |
| 82 | UntitledWindowPolicy StyledEditWindow::fPolicy = UntitledWindowPolicy(); |
| 83 | |
| 84 | StyledEditWindow::StyledEditWindow(BRect frame, uint32 encoding) |
83 | 85 | : BWindow(frame, "untitled", B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS) |
84 | 86 | { |
85 | 87 | _InitWindow(encoding); |
86 | | BString unTitled(B_TRANSLATE("Untitled ")); |
87 | | unTitled << id; |
88 | | SetTitle(unTitled.String()); |
| 88 | fPolicy.OnOpen(this); |
89 | 89 | fSaveItem->SetEnabled(true); |
90 | 90 | // allow saving empty files |
91 | 91 | Show(); |
… |
… |
StyledEditWindow::~StyledEditWindow()
|
112 | 112 | void |
113 | 113 | StyledEditWindow::Quit() |
114 | 114 | { |
| 115 | fPolicy.OnQuit(this); |
| 116 | |
115 | 117 | _SwitchNodeMonitor(false); |
116 | 118 | |
117 | 119 | _SaveAttrs(); |
diff --git a/src/apps/stylededit/StyledEditWindow.h b/src/apps/stylededit/StyledEditWindow.h
index bf239a6..fb95cef 100644
a
|
b
|
|
15 | 15 | #include <Node.h> |
16 | 16 | #include <Window.h> |
17 | 17 | |
| 18 | |
| 19 | #include "WindowPolicy.h" |
| 20 | |
| 21 | |
18 | 22 | struct entry_ref; |
19 | 23 | |
20 | 24 | class BFilePanel; |
… |
… |
class StyledEditView;
|
29 | 33 | |
30 | 34 | class StyledEditWindow : public BWindow { |
31 | 35 | public: |
32 | | StyledEditWindow(BRect frame, int32 id, |
33 | | uint32 encoding = 0); |
| 36 | StyledEditWindow(BRect frame, uint32 encoding = 0); |
34 | 37 | StyledEditWindow(BRect frame, entry_ref* ref, |
35 | 38 | uint32 encoding = 0); |
36 | 39 | virtual ~StyledEditWindow(); |
… |
… |
private:
|
171 | 174 | node_ref fNodeRef; |
172 | 175 | node_ref fFolderNodeRef; |
173 | 176 | bool fNagOnNodeChange; |
| 177 | |
| 178 | static UntitledWindowPolicy fPolicy; |
174 | 179 | }; |
175 | 180 | |
176 | 181 | |
diff --git a/src/apps/stylededit/WindowPolicy.h b/src/apps/stylededit/WindowPolicy.h
new file mode 100644
index 0000000..d662441
-
|
+
|
|
| 1 | #ifndef WINDOW_POLICY_H |
| 2 | #define WINDOW_POLICY_H |
| 3 | |
| 4 | |
| 5 | #include <interface/Window.h> |
| 6 | |
| 7 | |
| 8 | #undef B_TRANSLATION_CONTEXT |
| 9 | #define B_TRANSLATION_CONTEXT "StyledEditWindow" |
| 10 | |
| 11 | |
| 12 | class UntitledWindowPolicy |
| 13 | { |
| 14 | struct WindowData |
| 15 | { |
| 16 | BWindow *window; |
| 17 | int32 fID; |
| 18 | |
| 19 | |
| 20 | WindowData(BWindow *win, int32 id) : window(win), fID(id) {} |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | int fNextUntitled; |
| 25 | BList fWindows; |
| 26 | |
| 27 | |
| 28 | public: |
| 29 | UntitledWindowPolicy() : fNextUntitled(1), fWindows() {} |
| 30 | |
| 31 | |
| 32 | ~UntitledWindowPolicy() |
| 33 | { |
| 34 | for (int32 i = 0; i < fWindows.CountItems(); i++) |
| 35 | delete reinterpret_cast<WindowData *>(fWindows.ItemAt(i)); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void OnOpen(BWindow *window) |
| 40 | { |
| 41 | BString title(B_TRANSLATE("Untitled ")); |
| 42 | |
| 43 | WindowData *d = NULL; |
| 44 | for (int32 i = 0; i < fWindows.CountItems(); i++) { |
| 45 | WindowData *t = reinterpret_cast<WindowData *>(fWindows.ItemAt(i)); |
| 46 | if (t->window == NULL) { |
| 47 | d = t; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (d) { |
| 53 | d->window = window; |
| 54 | title << d->fID; |
| 55 | window->SetTitle(title); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | title << fNextUntitled; |
| 60 | window->SetTitle(title); |
| 61 | fWindows.AddItem(new WindowData(window, fNextUntitled++)); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void OnQuit(const BWindow *window) |
| 66 | { |
| 67 | for (int32 i = 0; i < fWindows.CountItems(); i++) { |
| 68 | WindowData *t = reinterpret_cast<WindowData *>(fWindows.ItemAt(i)); |
| 69 | if (!t) continue; |
| 70 | if (t->window == window) { |
| 71 | t->window = NULL; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | #endif // WINDOW_POLICY_H |