1 /* 2 * Copyright 2002-2006, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Mattias Sundblad 7 * Andrew Bachmann 8 */ 9 10 11 #include <Alert.h> 12 #include <Autolock.h> 13 #include <Path.h> 14 #include <MenuItem.h> 15 #include <CharacterSet.h> 16 #include <CharacterSetRoster.h> 17 #include <Screen.h> 18 #include <stdio.h> 19 20 #include "Constants.h" 21 #include "StyledEditApp.h" 22 #include "StyledEditWindow.h" 23 24 using namespace BPrivate; 25 26 StyledEditApp * styled_edit_app; 27 BRect gWindowRect(7-15, 26-15, 507, 426); 28 29 30 void 31 cascade() 32 { 33 BScreen screen(NULL); 34 BRect screenBorder = screen.Frame(); 35 float left = gWindowRect.left + 15; 36 if (left + gWindowRect.Width() > screenBorder.right) 37 left = 7; 38 39 float top = gWindowRect.top + 15; 40 if (top + gWindowRect.Height() > screenBorder.bottom) 41 top = 26; 42 43 gWindowRect.OffsetTo(BPoint(left,top)); 44 } 45 46 47 void 48 uncascade() 49 { 50 BScreen screen(NULL); 51 BRect screenBorder = screen.Frame(); 52 53 float left = gWindowRect.left - 15; 54 if (left < 7) { 55 left = screenBorder.right - gWindowRect.Width() - 7; 56 left = left - ((int)left % 15) + 7; 57 } 58 59 float top = gWindowRect.top - 15; 60 if (top < 26) { 61 top = screenBorder.bottom - gWindowRect.Height() - 26; 62 top = top - ((int)left % 15) + 26; 63 } 64 65 gWindowRect.OffsetTo(BPoint(left,top)); 66 } 67 68 69 // #pragma mark - 70 71 72 StyledEditApp::StyledEditApp() 73 : BApplication(APP_SIGNATURE) 74 { 75 fOpenPanel= new BFilePanel(); 76 BMenuBar * menuBar = 77 dynamic_cast<BMenuBar*>(fOpenPanel->Window()->FindView("MenuBar")); 78 79 fOpenAsEncoding = 0; 80 fOpenPanelEncodingMenu= new BMenu("Encoding"); 81 menuBar->AddItem(fOpenPanelEncodingMenu); 82 fOpenPanelEncodingMenu->SetRadioMode(true); 83 84 BCharacterSetRoster roster; 85 BCharacterSet charset; 86 while (roster.GetNextCharacterSet(&charset) == B_NO_ERROR) { 87 BString name(charset.GetPrintName()); 88 const char* mime = charset.GetMIMEName(); 89 if (mime) { 90 name.Append(" ("); 91 name.Append(mime); 92 name.Append(")"); 93 } 94 BMenuItem* item = new BMenuItem(name.String(), new BMessage(OPEN_AS_ENCODING)); 95 item->SetTarget(this); 96 fOpenPanelEncodingMenu->AddItem(item); 97 if (charset.GetFontID() == fOpenAsEncoding) 98 item->SetMarked(true); 99 } 100 101 fWindowCount = 0; 102 fNextUntitledWindow = 1; 103 104 styled_edit_app = this; 105 } 106 107 108 void 109 StyledEditApp::DispatchMessage(BMessage *msg, BHandler *handler) 110 { 111 if (msg->what == B_ARGV_RECEIVED) { 112 int32 argc; 113 if (msg->FindInt32("argc", &argc) != B_OK) 114 argc = 0; 115 116 const char** argv = new const char*[argc]; 117 for (int arg = 0; arg < argc; arg++) { 118 if (msg->FindString("argv", arg, &argv[arg]) != B_OK) { 119 argv[arg] = ""; 120 } 121 } 122 const char* cwd; 123 if (msg->FindString("cwd", &cwd) != B_OK) 124 cwd = ""; 125 126 ArgvReceived(argc, argv, cwd); 127 } else 128 BApplication::DispatchMessage(msg, handler); 129 } 130 131 132 void 133 StyledEditApp::MessageReceived(BMessage *message) 134 { 135 switch (message->what) { 136 case MENU_NEW: 137 OpenDocument(); 138 break; 139 case MENU_OPEN: 140 fOpenPanel->Show(); 141 break; 142 case B_SILENT_RELAUNCH: 143 OpenDocument(); 144 break; 145 case OPEN_AS_ENCODING: 146 void* ptr; 147 if (message->FindPointer("source", &ptr) == B_OK 148 && fOpenPanelEncodingMenu != 0) { 149 fOpenAsEncoding = (uint32)fOpenPanelEncodingMenu->IndexOf((BMenuItem*)ptr); 150 } 151 break; 152 153 default: 154 BApplication::MessageReceived(message); 155 break; 156 } 157 } 158 159 160 void 161 StyledEditApp::OpenDocument() 162 { 163 cascade(); 164 new StyledEditWindow(gWindowRect, fNextUntitledWindow++, fOpenAsEncoding); 165 fWindowCount++; 166 } 167 168 169 void 170 StyledEditApp::OpenDocument(entry_ref* ref) 171 { 172 cascade(); 173 new StyledEditWindow(gWindowRect, ref, fOpenAsEncoding); 174 fWindowCount++; 175 } 176 177 178 void 179 StyledEditApp::CloseDocument() 180 { 181 uncascade(); 182 fWindowCount--; 183 if (fWindowCount == 0) { 184 BAutolock lock(this); 185 Quit(); 186 } 187 } 188 189 190 void 191 StyledEditApp::RefsReceived(BMessage *message) 192 { 193 int32 index = 0; 194 entry_ref ref; 195 196 while (message->FindRef("refs", index++, &ref) == B_OK) { 197 OpenDocument(&ref); 198 } 199 } 200 201 202 void 203 StyledEditApp::ArgvReceived(int32 argc, const char* argv[], const char* cwd) 204 { 205 for (int i = 1 ; (i < argc) ; i++) { 206 BPath path; 207 if (argv[i][0] == '/') 208 path.SetTo(argv[i]); 209 else 210 path.SetTo(cwd, argv[i]); 211 212 if (path.InitCheck() != B_OK) { 213 fprintf(stderr, "Setting path failed: \""); 214 if (argv[i][0] == '/') 215 fprintf(stderr, "%s\".\n", argv[i]); 216 else 217 fprintf(stderr, "%s/%s\".\n", cwd, argv[i]); 218 continue; 219 } 220 221 entry_ref ref; 222 status_t status = get_ref_for_path(path.Path(), &ref); 223 if (status != B_OK) { 224 fprintf(stderr, "Could not open \"%s\": %s.\n", path.Path(), strerror(status)); 225 continue; 226 } 227 228 OpenDocument(&ref); 229 } 230 } 231 232 233 void 234 StyledEditApp::ReadyToRun() 235 { 236 if (fWindowCount == 0) 237 OpenDocument(); 238 } 239 240 241 int32 242 StyledEditApp::NumberOfWindows() 243 { 244 return fWindowCount; 245 } 246 247 248 // #pragma mark - 249 250 251 int 252 main(int argc, char** argv) 253 { 254 StyledEditApp styledEdit; 255 styledEdit.Run(); 256 return 0; 257 } 258 259